• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 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.
5 //
6 
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
15 
16 #include "SampleApplication.h"
17 #include "texture_utils.h"
18 #include "util/Matrix.h"
19 #include "util/geometry_utils.h"
20 #include "util/shader_utils.h"
21 
22 #include <cmath>
23 #include <iostream>
24 
25 class PostSubBufferSample : public SampleApplication
26 {
27   public:
PostSubBufferSample(int argc,char ** argv)28     PostSubBufferSample(int argc, char **argv) : SampleApplication("PostSubBuffer", argc, argv) {}
29 
initialize()30     bool initialize() override
31     {
32         mPostSubBufferNV = (PFNEGLPOSTSUBBUFFERNVPROC)eglGetProcAddress("eglPostSubBufferNV");
33         if (!mPostSubBufferNV)
34         {
35             std::cerr << "Could not load eglPostSubBufferNV.";
36             return false;
37         }
38 
39         constexpr char kVS[] = R"(uniform mat4 u_mvpMatrix;
40 attribute vec4 a_position;
41 attribute vec2 a_texcoord;
42 varying vec2 v_texcoord;
43 void main()
44 {
45     gl_Position = u_mvpMatrix * a_position;
46     v_texcoord = a_texcoord;
47 })";
48 
49         constexpr char kFS[] = R"(precision mediump float;
50 varying vec2 v_texcoord;
51 void main()
52 {
53     gl_FragColor = vec4(v_texcoord.x, v_texcoord.y, 1.0, 1.0);
54 })";
55 
56         mProgram = CompileProgram(kVS, kFS);
57         if (!mProgram)
58         {
59             return false;
60         }
61 
62         // Get the attribute locations
63         mPositionLoc = glGetAttribLocation(mProgram, "a_position");
64         mTexcoordLoc = glGetAttribLocation(mProgram, "a_texcoord");
65 
66         // Get the uniform locations
67         mMVPMatrixLoc = glGetUniformLocation(mProgram, "u_mvpMatrix");
68 
69         // Generate the geometry data
70         GenerateCubeGeometry(0.5f, &mCube);
71 
72         // Set an initial rotation
73         mRotation = 45.0f;
74 
75         // Clear the whole window surface to blue.
76         glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
77         glClear(GL_COLOR_BUFFER_BIT);
78         SampleApplication::swap();
79 
80         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
81         glCullFace(GL_BACK);
82         glEnable(GL_CULL_FACE);
83 
84         return true;
85     }
86 
destroy()87     void destroy() override { glDeleteProgram(mProgram); }
88 
step(float dt,double totalTime)89     void step(float dt, double totalTime) override
90     {
91         mRotation = fmod(mRotation + (dt * 40.0f), 360.0f);
92 
93         Matrix4 perspectiveMatrix = Matrix4::perspective(
94             60.0f, float(getWindow()->getWidth()) / getWindow()->getHeight(), 1.0f, 20.0f);
95 
96         Matrix4 modelMatrix = Matrix4::translate(angle::Vector3(0.0f, 0.0f, -2.0f)) *
97                               Matrix4::rotate(mRotation, angle::Vector3(1.0f, 0.0f, 1.0f));
98 
99         Matrix4 viewMatrix = Matrix4::identity();
100 
101         Matrix4 mvpMatrix = perspectiveMatrix * viewMatrix * modelMatrix;
102 
103         // Load the matrices
104         glUniformMatrix4fv(mMVPMatrixLoc, 1, GL_FALSE, mvpMatrix.data);
105     }
106 
draw()107     void draw() override
108     {
109         // Set the viewport
110         glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
111 
112         // Clear the color buffer
113         glClear(GL_COLOR_BUFFER_BIT);
114 
115         // Use the program object
116         glUseProgram(mProgram);
117 
118         // Load the vertex position
119         glVertexAttribPointer(mPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, mCube.positions.data());
120         glEnableVertexAttribArray(mPositionLoc);
121 
122         // Load the texcoord data
123         glVertexAttribPointer(mTexcoordLoc, 2, GL_FLOAT, GL_FALSE, 0, mCube.texcoords.data());
124         glEnableVertexAttribArray(mTexcoordLoc);
125 
126         // Draw the cube
127         glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(mCube.indices.size()), GL_UNSIGNED_SHORT,
128                        mCube.indices.data());
129     }
130 
swap()131     void swap() override
132     {
133         // Instead of letting the application call eglSwapBuffers, call eglPostSubBufferNV here
134         // instead
135         EGLint windowWidth  = static_cast<EGLint>(getWindow()->getWidth());
136         EGLint windowHeight = static_cast<EGLint>(getWindow()->getHeight());
137         EGLDisplay display  = getDisplay();
138         EGLSurface surface  = getSurface();
139         mPostSubBufferNV(display, surface, 60, 60, windowWidth - 120, windowHeight - 120);
140     }
141 
142   private:
143     // Handle to a program object
144     GLuint mProgram;
145 
146     // Attribute locations
147     GLint mPositionLoc;
148     GLint mTexcoordLoc;
149 
150     // Uniform locations
151     GLuint mMVPMatrixLoc;
152 
153     // Current rotation
154     float mRotation;
155 
156     // Geometry data
157     CubeGeometry mCube;
158 
159     // eglPostSubBufferNV entry point
160     PFNEGLPOSTSUBBUFFERNVPROC mPostSubBufferNV;
161 };
162 
main(int argc,char ** argv)163 int main(int argc, char **argv)
164 {
165     PostSubBufferSample app(argc, argv);
166     return app.run();
167 }
168