Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: third_party/gles_book_examples/Chapter_8/Simple_VertexShader/Simple_VertexShader.c

Issue 527016: Added gyp file for examples. Removed unnecessary functions. Added header file... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // 1 //
2 // Book: OpenGL(R) ES 2.0 Programming Guide 2 // Book: OpenGL(R) ES 2.0 Programming Guide
3 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner 3 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
4 // ISBN-10: 0321502795 4 // ISBN-10: 0321502795
5 // ISBN-13: 9780321502797 5 // ISBN-13: 9780321502797
6 // Publisher: Addison-Wesley Professional 6 // Publisher: Addison-Wesley Professional
7 // URLs: http://safari.informit.com/9780321563835 7 // URLs: http://safari.informit.com/9780321563835
8 // http://www.opengles-book.com 8 // http://www.opengles-book.com
9 // 9 //
10 10
11 // Simple_VertexShader.c 11 // Simple_VertexShader.c
12 // 12 //
13 // This is a simple example that draws a rotating cube in perspective 13 // This is a simple example that draws a rotating cube in perspective
14 // using a vertex shader to transform the object 14 // using a vertex shader to transform the object
15 // 15 //
16
17 #include "Simple_VertexShader.h"
16 #include <stdlib.h> 18 #include <stdlib.h>
17 #include "esUtil.h"
18
19 typedef struct
20 {
21 // Handle to a program object
22 GLuint programObject;
23
24 // Attribute locations
25 GLint positionLoc;
26
27 // Uniform locations
28 GLint mvpLoc;
29
30 // Vertex daata
31 GLfloat *vertices;
32 GLuint *indices;
33 int numIndices;
34
35 // Rotation angle
36 GLfloat angle;
37
38 // MVP matrix
39 ESMatrix mvpMatrix;
40 } UserData;
41 19
42 /// 20 ///
43 // Initialize the shader and program object 21 // Initialize the shader and program object
44 // 22 //
45 int Init ( ESContext *esContext ) 23 int svsInit ( ESContext *esContext )
46 { 24 {
47 UserData *userData = esContext->userData; 25 SVSUserData *userData = esContext->userData;
48 GLbyte vShaderStr[] = 26 GLbyte vShaderStr[] =
49 "uniform mat4 u_mvpMatrix; \n" 27 "uniform mat4 u_mvpMatrix; \n"
50 "attribute vec4 a_position; \n" 28 "attribute vec4 a_position; \n"
51 "void main() \n" 29 "void main() \n"
52 "{ \n" 30 "{ \n"
53 " gl_Position = u_mvpMatrix * a_position; \n" 31 " gl_Position = u_mvpMatrix * a_position; \n"
54 "} \n"; 32 "} \n";
55 33
34 // TODO(alokp): Shaders containing "precision" do not compile.
56 GLbyte fShaderStr[] = 35 GLbyte fShaderStr[] =
57 "precision mediump float; \n" 36 "//precision mediump float; \n"
58 "void main() \n" 37 "void main() \n"
59 "{ \n" 38 "{ \n"
60 " gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 ); \n" 39 " gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 ); \n"
61 "} \n"; 40 "} \n";
62 41
63 // Load the shaders and get a linked program object 42 // Load the shaders and get a linked program object
64 userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); 43 userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );
65 44
66 // Get the attribute locations 45 // Get the attribute locations
67 userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_pos ition" ); 46 userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_pos ition" );
68 47
69 // Get the uniform locations 48 // Get the uniform locations
70 userData->mvpLoc = glGetUniformLocation( userData->programObject, "u_mvpMatri x" ); 49 userData->mvpLoc = glGetUniformLocation( userData->programObject, "u_mvpMatri x" );
71 50
72 // Generate the vertex data 51 // Generate the vertex data
73 userData->numIndices = esGenCube( 1.0, &userData->vertices, 52 userData->numIndices = esGenCube( 1.0, &userData->vertices,
74 NULL, NULL, &userData->indices ); 53 NULL, NULL, &userData->indices );
75 54
76 // Starting rotation angle for the cube 55 // Starting rotation angle for the cube
77 userData->angle = 45.0f; 56 userData->angle = 45.0f;
78 57
79 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); 58 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
80 return TRUE; 59 return TRUE;
81 } 60 }
82 61
83
84 /// 62 ///
85 // Update MVP matrix based on time 63 // Update MVP matrix based on time
86 // 64 //
87 void Update ( ESContext *esContext, float deltaTime ) 65 void svsUpdate ( ESContext *esContext, float deltaTime )
88 { 66 {
89 UserData *userData = (UserData*) esContext->userData; 67 SVSUserData *userData = (SVSUserData*) esContext->userData;
90 ESMatrix perspective; 68 ESMatrix perspective;
91 ESMatrix modelview; 69 ESMatrix modelview;
92 float aspect; 70 float aspect;
93 71
94 // Compute a rotation angle based on time to rotate the cube 72 // Compute a rotation angle based on time to rotate the cube
95 userData->angle += ( deltaTime * 40.0f ); 73 userData->angle += ( deltaTime * 40.0f );
96 if( userData->angle >= 360.0f ) 74 if( userData->angle >= 360.0f )
97 userData->angle -= 360.0f; 75 userData->angle -= 360.0f;
98 76
99 // Compute the window aspect ratio 77 // Compute the window aspect ratio
(...skipping 13 matching lines...) Expand all
113 esRotate( &modelview, userData->angle, 1.0, 0.0, 1.0 ); 91 esRotate( &modelview, userData->angle, 1.0, 0.0, 1.0 );
114 92
115 // Compute the final MVP by multiplying the 93 // Compute the final MVP by multiplying the
116 // modevleiw and perspective matrices together 94 // modevleiw and perspective matrices together
117 esMatrixMultiply( &userData->mvpMatrix, &modelview, &perspective ); 95 esMatrixMultiply( &userData->mvpMatrix, &modelview, &perspective );
118 } 96 }
119 97
120 /// 98 ///
121 // Draw a triangle using the shader pair created in Init() 99 // Draw a triangle using the shader pair created in Init()
122 // 100 //
123 void Draw ( ESContext *esContext ) 101 void svsDraw ( ESContext *esContext )
124 { 102 {
125 UserData *userData = esContext->userData; 103 SVSUserData *userData = esContext->userData;
126 104
127 // Set the viewport 105 // Set the viewport
128 glViewport ( 0, 0, esContext->width, esContext->height ); 106 glViewport ( 0, 0, esContext->width, esContext->height );
129 107
130 108
131 // Clear the color buffer 109 // Clear the color buffer
132 glClear ( GL_COLOR_BUFFER_BIT ); 110 glClear ( GL_COLOR_BUFFER_BIT );
133 111
134 // Use the program object 112 // Use the program object
135 glUseProgram ( userData->programObject ); 113 glUseProgram ( userData->programObject );
136 114
137 // Load the vertex position 115 // Load the vertex position
138 glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, 116 glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT,
139 GL_FALSE, 3 * sizeof(GLfloat), userData->vertices ); 117 GL_FALSE, 3 * sizeof(GLfloat), userData->vertices );
140 118
141 glEnableVertexAttribArray ( userData->positionLoc ); 119 glEnableVertexAttribArray ( userData->positionLoc );
142 120
143 121
144 // Load the MVP matrix 122 // Load the MVP matrix
145 glUniformMatrix4fv( userData->mvpLoc, 1, GL_FALSE, (GLfloat*) &userData->mvpM atrix.m[0][0] ); 123 glUniformMatrix4fv( userData->mvpLoc, 1, GL_FALSE, (GLfloat*) &userData->mvpM atrix.m[0][0] );
146 124
147 // Draw the cube 125 // Draw the cube
148 glDrawElements ( GL_TRIANGLES, userData->numIndices, GL_UNSIGNED_INT, userDat a->indices ); 126 glDrawElements ( GL_TRIANGLES, userData->numIndices, GL_UNSIGNED_INT, userDat a->indices );
149 127
150 eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface ); 128 // TODO(alokp): glFlush should not be necessary with SwapBuffers.
129 glFlush();
151 } 130 }
152 131
153 /// 132 ///
154 // Cleanup 133 // Cleanup
155 // 134 //
156 void ShutDown ( ESContext *esContext ) 135 void svsShutDown ( ESContext *esContext )
157 { 136 {
158 UserData *userData = esContext->userData; 137 SVSUserData *userData = esContext->userData;
159 138
160 if ( userData->vertices != NULL ) 139 if ( userData->vertices != NULL )
161 { 140 {
162 free ( userData->vertices ); 141 free ( userData->vertices );
163 } 142 }
164 143
165 if ( userData->indices != NULL ) 144 if ( userData->indices != NULL )
166 { 145 {
167 free ( userData->indices ); 146 free ( userData->indices );
168 } 147 }
169 148
170 // Delete program object 149 // Delete program object
171 glDeleteProgram ( userData->programObject ); 150 glDeleteProgram ( userData->programObject );
172 } 151 }
173
174
175 int main ( int argc, char *argv[] )
176 {
177 ESContext esContext;
178 UserData userData;
179
180 esInitContext ( &esContext );
181 esContext.userData = &userData;
182
183 esCreateWindow ( &esContext, "Simple Texture 2D", 320, 240, ES_WINDOW_RGB );
184
185 if ( !Init ( &esContext ) )
186 return 0;
187
188 esRegisterDrawFunc ( &esContext, Draw );
189 esRegisterUpdateFunc ( &esContext, Update );
190
191 esMainLoop ( &esContext );
192
193 ShutDown ( &esContext );
194 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698