2 // Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
7 // Based on Simple_VertexShader.c from
8 // Book: OpenGL(R) ES 2.0 Programming Guide
9 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
10 // ISBN-10: 0321502795
11 // ISBN-13: 9780321502797
12 // Publisher: Addison-Wesley Professional
13 // URLs: http://safari.informit.com/9780321563835
14 // http://www.opengles-book.com
16 #include "SampleApplication.h"
17 #include "shader_utils.h"
18 #include "texture_utils.h"
19 #include "geometry_utils.h"
24 class PostSubBufferSample : public SampleApplication
27 PostSubBufferSample::PostSubBufferSample()
28 : SampleApplication("PostSubBuffer", 1280, 720)
32 virtual bool initialize()
34 mPostSubBufferNV = (PFNEGLPOSTSUBBUFFERNVPROC)eglGetProcAddress("eglPostSubBufferNV");
35 if (!mPostSubBufferNV)
37 std::cerr << "Could not load eglPostSubBufferNV.";
41 const std::string vs = SHADER_SOURCE
43 uniform mat4 u_mvpMatrix;
44 attribute vec4 a_position;
45 attribute vec2 a_texcoord;
46 varying vec2 v_texcoord;
49 gl_Position = u_mvpMatrix * a_position;
50 v_texcoord = a_texcoord;
54 const std::string fs = SHADER_SOURCE
56 precision mediump float;
57 varying vec2 v_texcoord;
60 gl_FragColor = vec4(v_texcoord.x, v_texcoord.y, 1.0, 1.0);
64 mProgram = CompileProgram(vs, fs);
70 // Get the attribute locations
71 mPositionLoc = glGetAttribLocation(mProgram, "a_position");
72 mTexcoordLoc = glGetAttribLocation(mProgram, "a_texcoord");
74 // Get the uniform locations
75 mMVPMatrixLoc = glGetUniformLocation(mProgram, "u_mvpMatrix");
77 // Generate the geometry data
78 GenerateCubeGeometry(0.5f, &mCube);
80 // Set an initial rotation
83 // Clear the whole window surface to blue.
84 glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
85 glClear(GL_COLOR_BUFFER_BIT);
86 SampleApplication::swap();
88 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
90 glEnable(GL_CULL_FACE);
95 virtual void destroy()
97 glDeleteProgram(mProgram);
100 virtual void step(float dt, double totalTime)
102 mRotation = fmod(mRotation + (dt * 40.0f), 360.0f);
104 Matrix4 perspectiveMatrix = Matrix4::perspective(60.0f, float(getWindow()->getWidth()) / getWindow()->getHeight(),
107 Matrix4 modelMatrix = Matrix4::translate(Vector3(0.0f, 0.0f, -2.0f)) *
108 Matrix4::rotate(mRotation, Vector3(1.0f, 0.0f, 1.0f));
110 Matrix4 viewMatrix = Matrix4::identity();
112 Matrix4 mvpMatrix = perspectiveMatrix * viewMatrix * modelMatrix;
115 glUniformMatrix4fv(mMVPMatrixLoc, 1, GL_FALSE, mvpMatrix.data);
121 glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
123 // Clear the color buffer
124 glClear(GL_COLOR_BUFFER_BIT);
126 // Use the program object
127 glUseProgram(mProgram);
129 // Load the vertex position
130 glVertexAttribPointer(mPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, mCube.positions.data());
131 glEnableVertexAttribArray(mPositionLoc);
133 // Load the texcoord data
134 glVertexAttribPointer(mTexcoordLoc, 2, GL_FLOAT, GL_FALSE, 0, mCube.texcoords.data());
135 glEnableVertexAttribArray(mTexcoordLoc);
138 glDrawElements(GL_TRIANGLES, mCube.indices.size(), GL_UNSIGNED_SHORT, mCube.indices.data());
143 // Instead of letting the application call eglSwapBuffers, call eglPostSubBufferNV here instead
144 size_t windowWidth = getWindow()->getWidth();
145 size_t windowHeight = getWindow()->getHeight();
146 EGLDisplay display = getWindow()->getDisplay();
147 EGLSurface surface = getSurface();
148 mPostSubBufferNV(display, surface, 60, 60, windowWidth - 120, windowHeight - 120);
152 // Handle to a program object
155 // Attribute locations
160 GLuint mMVPMatrixLoc;
168 // eglPostSubBufferNV entry point
169 PFNEGLPOSTSUBBUFFERNVPROC mPostSubBufferNV;
172 int main(int argc, char **argv)
174 PostSubBufferSample app;