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

Side by Side Diff: third_party/gles2_book/Chapter_9/TextureWrap/TextureWrap.c

Issue 545065: Modified texture wrap example to use VBOs instead of client-side vertex array... (Closed) Base URL: svn://chrome-svn/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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
(...skipping 16 matching lines...) Expand all
27 27
28 // Sampler location 28 // Sampler location
29 GLint samplerLoc; 29 GLint samplerLoc;
30 30
31 // Offset location 31 // Offset location
32 GLint offsetLoc; 32 GLint offsetLoc;
33 33
34 // Texture handle 34 // Texture handle
35 GLuint textureId; 35 GLuint textureId;
36 36
37 // Vertex buffer object handle
38 GLuint vboIds[2];
39
37 } UserData; 40 } UserData;
38 41
39 /// 42 ///
40 // Generate an RGB8 checkerboard image 43 // Generate an RGB8 checkerboard image
41 // 44 //
42 GLubyte* GenCheckImage( int width, int height, int checkSize ) 45 static GLubyte* GenCheckImage( int width, int height, int checkSize )
43 { 46 {
44 int x, 47 int x,
45 y; 48 y;
46 GLubyte *pixels = malloc( width * height * 3 ); 49 GLubyte *pixels = malloc( width * height * 3 );
47 50
48 if ( pixels == NULL ) 51 if ( pixels == NULL )
49 return NULL; 52 return NULL;
50 53
51 for ( y = 0; y < height; y++ ) 54 for ( y = 0; y < height; y++ )
52 for ( x = 0; x < width; x++ ) 55 for ( x = 0; x < width; x++ )
(...skipping 16 matching lines...) Expand all
69 pixels[(y * height + x) * 3 + 1] = 0; 72 pixels[(y * height + x) * 3 + 1] = 0;
70 pixels[(y * height + x) * 3 + 2] = bColor; 73 pixels[(y * height + x) * 3 + 2] = bColor;
71 } 74 }
72 75
73 return pixels; 76 return pixels;
74 } 77 }
75 78
76 /// 79 ///
77 // Create a mipmapped 2D texture image 80 // Create a mipmapped 2D texture image
78 // 81 //
79 GLuint CreateTexture2D( ) 82 static GLuint CreateTexture2D( )
80 { 83 {
81 // Texture object handle 84 // Texture object handle
82 GLuint textureId; 85 GLuint textureId;
83 int width = 256, 86 int width = 256,
84 height = 256; 87 height = 256;
85 GLubyte *pixels; 88 GLubyte *pixels;
86 89
87 pixels = GenCheckImage( width, height, 64 ); 90 pixels = GenCheckImage( width, height, 64 );
88 if ( pixels == NULL ) 91 if ( pixels == NULL )
89 return 0; 92 return 0;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 130
128 GLbyte fShaderStr[] = 131 GLbyte fShaderStr[] =
129 "precision mediump float; \n" 132 "precision mediump float; \n"
130 "varying vec2 v_texCoord; \n" 133 "varying vec2 v_texCoord; \n"
131 "uniform sampler2D s_texture; \n" 134 "uniform sampler2D s_texture; \n"
132 "void main() \n" 135 "void main() \n"
133 "{ \n" 136 "{ \n"
134 " gl_FragColor = texture2D( s_texture, v_texCoord );\n" 137 " gl_FragColor = texture2D( s_texture, v_texCoord );\n"
135 "} \n"; 138 "} \n";
136 139
140 GLfloat vVertices[] = { -0.3f, 0.3f, 0.0f, 1.0f, // Position 0
141 -1.0f, -1.0f, // TexCoord 0
142 -0.3f, -0.3f, 0.0f, 1.0f, // Position 1
143 -1.0f, 2.0f, // TexCoord 1
144 0.3f, -0.3f, 0.0f, 1.0f, // Position 2
145 2.0f, 2.0f, // TexCoord 2
146 0.3f, 0.3f, 0.0f, 1.0f, // Position 3
147 2.0f, -1.0f // TexCoord 3
148 };
149 GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
150
137 // Load the shaders and get a linked program object 151 // Load the shaders and get a linked program object
138 userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); 152 userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );
139 153
140 // Get the attribute locations 154 // Get the attribute locations
141 userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_pos ition" ); 155 userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_pos ition" );
142 userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_tex Coord" ); 156 userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_tex Coord" );
143 157
144 // Get the sampler location 158 // Get the sampler location
145 userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_tex ture" ); 159 userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_tex ture" );
146 160
147 // Get the offset location 161 // Get the offset location
148 userData->offsetLoc = glGetUniformLocation( userData->programObject, "u_offse t" ); 162 userData->offsetLoc = glGetUniformLocation( userData->programObject, "u_offse t" );
149 163
150 // Load the texture 164 // Load the texture
151 userData->textureId = CreateTexture2D (); 165 userData->textureId = CreateTexture2D ();
152 166
167 // Load vertex data
168 glGenBuffers ( 2, userData->vboIds );
169 glBindBuffer ( GL_ARRAY_BUFFER, userData->vboIds[0] );
170 glBufferData ( GL_ARRAY_BUFFER, sizeof(vVertices),
171 vVertices, GL_STATIC_DRAW );
172 glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, userData->vboIds[1] );
173 glBufferData ( GL_ELEMENT_ARRAY_BUFFER, sizeof(indices),
174 indices, GL_STATIC_DRAW );
175
153 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); 176 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
154 return TRUE; 177 return TRUE;
155 } 178 }
156 179
157 /// 180 ///
158 // Draw a triangle using the shader pair created in Init() 181 // Draw a triangle using the shader pair created in Init()
159 // 182 //
183 #define VTX_POS_SIZE 4
184 #define VTX_TEX_SIZE 2
185 #define VTX_STRIDE (6 * sizeof(GLfloat))
160 void Draw ( ESContext *esContext ) 186 void Draw ( ESContext *esContext )
161 { 187 {
162 UserData *userData = esContext->userData; 188 UserData *userData = esContext->userData;
163 GLfloat vVertices[] = { -0.3f, 0.3f, 0.0f, 1.0f, // Position 0 189 GLuint offset = 0;
164 -1.0f, -1.0f, // TexCoord 0
165 -0.3f, -0.3f, 0.0f, 1.0f, // Position 1
166 -1.0f, 2.0f, // TexCoord 1
167 0.3f, -0.3f, 0.0f, 1.0f, // Position 2
168 2.0f, 2.0f, // TexCoord 2
169 0.3f, 0.3f, 0.0f, 1.0f, // Position 3
170 2.0f, -1.0f // TexCoord 3
171 };
172 GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
173 190
174 // Set the viewport 191 // Set the viewport
175 glViewport ( 0, 0, esContext->width, esContext->height ); 192 glViewport ( 0, 0, esContext->width, esContext->height );
176 193
177 // Clear the color buffer 194 // Clear the color buffer
178 glClear ( GL_COLOR_BUFFER_BIT ); 195 glClear ( GL_COLOR_BUFFER_BIT );
179 196
180 // Use the program object 197 // Use the program object
181 glUseProgram ( userData->programObject ); 198 glUseProgram ( userData->programObject );
182 199
183 // Load the vertex position 200 // Load the vertex position
184 glVertexAttribPointer ( userData->positionLoc, 4, GL_FLOAT, 201 glVertexAttribPointer ( userData->positionLoc, VTX_POS_SIZE, GL_FLOAT,
185 GL_FALSE, 6 * sizeof(GLfloat), vVertices ); 202 GL_FALSE, VTX_STRIDE, (GLvoid*) offset );
186 // Load the texture coordinate 203 // Load the texture coordinate
187 glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT, 204 offset += VTX_POS_SIZE * sizeof(GLfloat);
188 GL_FALSE, 6 * sizeof(GLfloat), &vVertices[4] ); 205 glVertexAttribPointer ( userData->texCoordLoc, VTX_TEX_SIZE, GL_FLOAT,
206 GL_FALSE, VTX_STRIDE, (GLvoid*) offset );
189 207
190 glEnableVertexAttribArray ( userData->positionLoc ); 208 glEnableVertexAttribArray ( userData->positionLoc );
191 glEnableVertexAttribArray ( userData->texCoordLoc ); 209 glEnableVertexAttribArray ( userData->texCoordLoc );
192 210
193 // Bind the texture 211 // Bind the texture
194 glActiveTexture ( GL_TEXTURE0 ); 212 glActiveTexture ( GL_TEXTURE0 );
195 glBindTexture ( GL_TEXTURE_2D, userData->textureId ); 213 glBindTexture ( GL_TEXTURE_2D, userData->textureId );
196 214
197 // Set the sampler texture unit to 0 215 // Set the sampler texture unit to 0
198 glUniform1i ( userData->samplerLoc, 0 ); 216 glUniform1i ( userData->samplerLoc, 0 );
199 217
200 // Draw quad with repeat wrap mode 218 // Draw quad with repeat wrap mode
201 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); 219 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
202 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); 220 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
203 glUniform1f ( userData->offsetLoc, -0.7f ); 221 glUniform1f ( userData->offsetLoc, -0.7f );
204 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices ); 222 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 );
205 223
206 // Draw quad with clamp to edge wrap mode 224 // Draw quad with clamp to edge wrap mode
207 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); 225 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
208 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); 226 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
209 glUniform1f ( userData->offsetLoc, 0.0f ); 227 glUniform1f ( userData->offsetLoc, 0.0f );
210 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices ); 228 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 );
211 229
212 // Draw quad with mirrored repeat 230 // Draw quad with mirrored repeat
213 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT ); 231 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT );
214 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT ); 232 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT );
215 glUniform1f ( userData->offsetLoc, 0.7f ); 233 glUniform1f ( userData->offsetLoc, 0.7f );
216 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices ); 234 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 );
217 235
218 eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface ); 236 eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
219 } 237 }
220 238
221 /// 239 ///
222 // Cleanup 240 // Cleanup
223 // 241 //
224 void ShutDown ( ESContext *esContext ) 242 void ShutDown ( ESContext *esContext )
225 { 243 {
226 UserData *userData = esContext->userData; 244 UserData *userData = esContext->userData;
227 245
228 // Delete texture object 246 // Delete texture object
229 glDeleteTextures ( 1, &userData->textureId ); 247 glDeleteTextures ( 1, &userData->textureId );
230 248
231 // Delete program object 249 // Delete program object
232 glDeleteProgram ( userData->programObject ); 250 glDeleteProgram ( userData->programObject );
251
252 // Delete vertex buffer objects
253 glDeleteBuffers ( 2, userData->vboIds );
233 } 254 }
234 255
235 256
236 int main ( int argc, char *argv[] ) 257 int main ( int argc, char *argv[] )
237 { 258 {
238 ESContext esContext; 259 ESContext esContext;
239 UserData userData; 260 UserData userData;
240 261
241 esInitContext ( &esContext ); 262 esInitContext ( &esContext );
242 esContext.userData = &userData; 263 esContext.userData = &userData;
243 264
244 esCreateWindow ( &esContext, "MipMap 2D", 640, 480, ES_WINDOW_RGB ); 265 esCreateWindow ( &esContext, "MipMap 2D", 640, 480, ES_WINDOW_RGB );
245 266
246 if ( !Init ( &esContext ) ) 267 if ( !Init ( &esContext ) )
247 return 0; 268 return 0;
248 269
249 esRegisterDrawFunc ( &esContext, Draw ); 270 esRegisterDrawFunc ( &esContext, Draw );
250 271
251 esMainLoop ( &esContext ); 272 esMainLoop ( &esContext );
252 273
253 ShutDown ( &esContext ); 274 ShutDown ( &esContext );
254 } 275 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698