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