OLD | NEW |
---|---|
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 | 16 |
17 #include "Simple_VertexShader.h" | 17 #include "Simple_VertexShader.h" |
18 #include <stdlib.h> | 18 #include <stdlib.h> |
19 | 19 |
20 /// | 20 /// |
21 // Initialize the shader and program object | 21 // Initialize the shader and program object |
22 // | 22 // |
23 int svsInit ( ESContext *esContext ) | 23 int svsInit ( ESContext *esContext ) |
24 { | 24 { |
25 SVSUserData *userData = esContext->userData; | 25 SVSUserData *userData = esContext->userData; |
26 int numVertices = 24; | |
27 GLfloat *vertices = NULL; | |
28 GLushort *indices = NULL; | |
29 | |
26 GLbyte vShaderStr[] = | 30 GLbyte vShaderStr[] = |
27 "uniform mat4 u_mvpMatrix; \n" | 31 "uniform mat4 u_mvpMatrix; \n" |
28 "attribute vec4 a_position; \n" | 32 "attribute vec4 a_position; \n" |
29 "void main() \n" | 33 "void main() \n" |
30 "{ \n" | 34 "{ \n" |
31 " gl_Position = u_mvpMatrix * a_position; \n" | 35 " gl_Position = u_mvpMatrix * a_position; \n" |
32 "} \n"; | 36 "} \n"; |
33 | 37 |
34 // TODO(alokp): Shaders containing "precision" do not compile. | 38 // TODO(alokp): Shaders containing "precision" do not compile. |
35 GLbyte fShaderStr[] = | 39 GLbyte fShaderStr[] = |
36 "//precision mediump float; \n" | 40 "//precision mediump float; \n" |
37 "void main() \n" | 41 "void main() \n" |
38 "{ \n" | 42 "{ \n" |
39 " gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 ); \n" | 43 " gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 ); \n" |
40 "} \n"; | 44 "} \n"; |
41 | 45 |
42 // Load the shaders and get a linked program object | 46 // Load the shaders and get a linked program object |
43 userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); | 47 userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); |
48 if ( userData->programObject == 0 ) return FALSE; | |
44 | 49 |
45 // Get the attribute locations | 50 // Get the attribute locations |
46 userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_pos ition" ); | 51 userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_pos ition" ); |
47 | 52 |
48 // Get the uniform locations | 53 // Get the uniform locations |
49 userData->mvpLoc = glGetUniformLocation( userData->programObject, "u_mvpMatri x" ); | 54 userData->mvpLoc = glGetUniformLocation( userData->programObject, "u_mvpMatri x" ); |
50 | 55 |
51 // Generate the vertex data | 56 // Generate the vertex data |
52 userData->numIndices = esGenCube( 1.0, &userData->vertices, | 57 userData->numIndices = esGenCube( 1.0, &vertices, NULL, NULL, &indices ); |
53 NULL, NULL, &userData->indices ); | 58 glGenBuffers ( 2, userData->vboIds ); |
54 | 59 glBindBuffer ( GL_ARRAY_BUFFER, userData->vboIds[0] ); |
60 glBufferData ( GL_ARRAY_BUFFER, 3 * numVertices * sizeof(GLfloat), | |
61 vertices, GL_STATIC_DRAW ); | |
62 glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, userData->vboIds[1] ); | |
63 glBufferData ( GL_ELEMENT_ARRAY_BUFFER, userData->numIndices * sizeof(GL_UNSI GNED_SHORT), | |
64 indices, GL_STATIC_DRAW ); | |
65 if ( vertices != NULL ) free ( vertices ); | |
66 if ( indices != NULL ) free ( indices ); | |
67 | |
55 // Starting rotation angle for the cube | 68 // Starting rotation angle for the cube |
56 userData->angle = 45.0f; | 69 userData->angle = 45.0f; |
57 | 70 |
58 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); | 71 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); |
59 return TRUE; | 72 return TRUE; |
60 } | 73 } |
61 | 74 |
62 /// | 75 /// |
63 // Update MVP matrix based on time | 76 // Update MVP matrix based on time |
64 // | 77 // |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 /// | 111 /// |
99 // Draw a triangle using the shader pair created in Init() | 112 // Draw a triangle using the shader pair created in Init() |
100 // | 113 // |
101 void svsDraw ( ESContext *esContext ) | 114 void svsDraw ( ESContext *esContext ) |
102 { | 115 { |
103 SVSUserData *userData = esContext->userData; | 116 SVSUserData *userData = esContext->userData; |
104 | 117 |
105 // Set the viewport | 118 // Set the viewport |
106 glViewport ( 0, 0, esContext->width, esContext->height ); | 119 glViewport ( 0, 0, esContext->width, esContext->height ); |
107 | 120 |
108 | |
109 // Clear the color buffer | 121 // Clear the color buffer |
110 glClear ( GL_COLOR_BUFFER_BIT ); | 122 glClear ( GL_COLOR_BUFFER_BIT ); |
111 | 123 |
112 // Use the program object | 124 // Use the program object |
113 glUseProgram ( userData->programObject ); | 125 glUseProgram ( userData->programObject ); |
114 | 126 |
115 // Load the vertex position | 127 // Load the vertex position |
128 glEnableVertexAttribArray ( userData->positionLoc ); | |
greggman
2010/01/13 03:10:43
Any particular reason you switched the order of th
alokp
2010/01/13 03:52:59
no particular reason. does it matter?
| |
116 glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, | 129 glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, |
117 GL_FALSE, 3 * sizeof(GLfloat), userData->vertices ); | 130 GL_FALSE, 3 * sizeof(GLfloat), 0 ); |
118 | 131 |
119 glEnableVertexAttribArray ( userData->positionLoc ); | |
120 | |
121 | |
122 // Load the MVP matrix | 132 // Load the MVP matrix |
123 glUniformMatrix4fv( userData->mvpLoc, 1, GL_FALSE, (GLfloat*) &userData->mvpM atrix.m[0][0] ); | 133 glUniformMatrix4fv( userData->mvpLoc, 1, GL_FALSE, (GLfloat*) &userData->mvpM atrix.m[0][0] ); |
124 | 134 |
125 // Draw the cube | 135 // Draw the cube |
126 glDrawElements ( GL_TRIANGLES, userData->numIndices, GL_UNSIGNED_INT, userDat a->indices ); | 136 glDrawElements ( GL_TRIANGLES, userData->numIndices, GL_UNSIGNED_SHORT, 0 ); |
127 | 137 |
128 // TODO(alokp): glFlush should not be necessary with SwapBuffers. | 138 // TODO(alokp): glFlush should not be necessary with SwapBuffers. |
129 glFlush(); | 139 glFlush(); |
130 } | 140 } |
131 | 141 |
132 /// | 142 /// |
133 // Cleanup | 143 // Cleanup |
134 // | 144 // |
135 void svsShutDown ( ESContext *esContext ) | 145 void svsShutDown ( ESContext *esContext ) |
136 { | 146 { |
137 SVSUserData *userData = esContext->userData; | 147 SVSUserData *userData = esContext->userData; |
138 | 148 |
139 if ( userData->vertices != NULL ) | 149 // Delete program object |
140 { | 150 glDeleteBuffers ( 2, userData->vboIds ); |
141 free ( userData->vertices ); | |
142 } | |
143 | |
144 if ( userData->indices != NULL ) | |
145 { | |
146 free ( userData->indices ); | |
147 } | |
148 | 151 |
149 // Delete program object | 152 // Delete program object |
150 glDeleteProgram ( userData->programObject ); | 153 glDeleteProgram ( userData->programObject ); |
151 } | 154 } |
OLD | NEW |