summaryrefslogtreecommitdiffstats
path: root/third_party
diff options
context:
space:
mode:
authoralokp@chromium.org <alokp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-14 22:13:47 +0000
committeralokp@chromium.org <alokp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-14 22:13:47 +0000
commitf9b8f78ca76643b51de83077644c9a9bdf1d2d43 (patch)
treec47e003b2bca11f1ff0b7b1f515479a7647c3a11 /third_party
parent6d745ac2b40db5df3e3be3d639faef7c3eb1bfaa (diff)
downloadchromium_src-f9b8f78ca76643b51de83077644c9a9bdf1d2d43.zip
chromium_src-f9b8f78ca76643b51de83077644c9a9bdf1d2d43.tar.gz
chromium_src-f9b8f78ca76643b51de83077644c9a9bdf1d2d43.tar.bz2
Modified texture wrap example to use VBOs instead of client-side vertex arrays.
BUG=26099 Review URL: http://codereview.chromium.org/545065 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@36279 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'third_party')
-rw-r--r--third_party/gles2_book/Chapter_9/TextureWrap/TextureWrap.c59
1 files changed, 40 insertions, 19 deletions
diff --git a/third_party/gles2_book/Chapter_9/TextureWrap/TextureWrap.c b/third_party/gles2_book/Chapter_9/TextureWrap/TextureWrap.c
index 2083819..8507f77 100644
--- a/third_party/gles2_book/Chapter_9/TextureWrap/TextureWrap.c
+++ b/third_party/gles2_book/Chapter_9/TextureWrap/TextureWrap.c
@@ -34,12 +34,15 @@ typedef struct
// Texture handle
GLuint textureId;
+ // Vertex buffer object handle
+ GLuint vboIds[2];
+
} UserData;
///
// Generate an RGB8 checkerboard image
//
-GLubyte* GenCheckImage( int width, int height, int checkSize )
+static GLubyte* GenCheckImage( int width, int height, int checkSize )
{
int x,
y;
@@ -76,7 +79,7 @@ GLubyte* GenCheckImage( int width, int height, int checkSize )
///
// Create a mipmapped 2D texture image
//
-GLuint CreateTexture2D( )
+static GLuint CreateTexture2D( )
{
// Texture object handle
GLuint textureId;
@@ -134,6 +137,17 @@ int Init ( ESContext *esContext )
" gl_FragColor = texture2D( s_texture, v_texCoord );\n"
"} \n";
+ GLfloat vVertices[] = { -0.3f, 0.3f, 0.0f, 1.0f, // Position 0
+ -1.0f, -1.0f, // TexCoord 0
+ -0.3f, -0.3f, 0.0f, 1.0f, // Position 1
+ -1.0f, 2.0f, // TexCoord 1
+ 0.3f, -0.3f, 0.0f, 1.0f, // Position 2
+ 2.0f, 2.0f, // TexCoord 2
+ 0.3f, 0.3f, 0.0f, 1.0f, // Position 3
+ 2.0f, -1.0f // TexCoord 3
+ };
+ GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
+
// Load the shaders and get a linked program object
userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );
@@ -150,6 +164,15 @@ int Init ( ESContext *esContext )
// Load the texture
userData->textureId = CreateTexture2D ();
+ // Load vertex data
+ glGenBuffers ( 2, userData->vboIds );
+ glBindBuffer ( GL_ARRAY_BUFFER, userData->vboIds[0] );
+ glBufferData ( GL_ARRAY_BUFFER, sizeof(vVertices),
+ vVertices, GL_STATIC_DRAW );
+ glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, userData->vboIds[1] );
+ glBufferData ( GL_ELEMENT_ARRAY_BUFFER, sizeof(indices),
+ indices, GL_STATIC_DRAW );
+
glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
return TRUE;
}
@@ -157,19 +180,13 @@ int Init ( ESContext *esContext )
///
// Draw a triangle using the shader pair created in Init()
//
+#define VTX_POS_SIZE 4
+#define VTX_TEX_SIZE 2
+#define VTX_STRIDE (6 * sizeof(GLfloat))
void Draw ( ESContext *esContext )
{
UserData *userData = esContext->userData;
- GLfloat vVertices[] = { -0.3f, 0.3f, 0.0f, 1.0f, // Position 0
- -1.0f, -1.0f, // TexCoord 0
- -0.3f, -0.3f, 0.0f, 1.0f, // Position 1
- -1.0f, 2.0f, // TexCoord 1
- 0.3f, -0.3f, 0.0f, 1.0f, // Position 2
- 2.0f, 2.0f, // TexCoord 2
- 0.3f, 0.3f, 0.0f, 1.0f, // Position 3
- 2.0f, -1.0f // TexCoord 3
- };
- GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
+ GLuint offset = 0;
// Set the viewport
glViewport ( 0, 0, esContext->width, esContext->height );
@@ -181,11 +198,12 @@ void Draw ( ESContext *esContext )
glUseProgram ( userData->programObject );
// Load the vertex position
- glVertexAttribPointer ( userData->positionLoc, 4, GL_FLOAT,
- GL_FALSE, 6 * sizeof(GLfloat), vVertices );
+ glVertexAttribPointer ( userData->positionLoc, VTX_POS_SIZE, GL_FLOAT,
+ GL_FALSE, VTX_STRIDE, (GLvoid*) offset );
// Load the texture coordinate
- glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT,
- GL_FALSE, 6 * sizeof(GLfloat), &vVertices[4] );
+ offset += VTX_POS_SIZE * sizeof(GLfloat);
+ glVertexAttribPointer ( userData->texCoordLoc, VTX_TEX_SIZE, GL_FLOAT,
+ GL_FALSE, VTX_STRIDE, (GLvoid*) offset );
glEnableVertexAttribArray ( userData->positionLoc );
glEnableVertexAttribArray ( userData->texCoordLoc );
@@ -201,19 +219,19 @@ void Draw ( ESContext *esContext )
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glUniform1f ( userData->offsetLoc, -0.7f );
- glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
+ glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 );
// Draw quad with clamp to edge wrap mode
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glUniform1f ( userData->offsetLoc, 0.0f );
- glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
+ glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 );
// Draw quad with mirrored repeat
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT );
glUniform1f ( userData->offsetLoc, 0.7f );
- glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
+ glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 );
eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
}
@@ -230,6 +248,9 @@ void ShutDown ( ESContext *esContext )
// Delete program object
glDeleteProgram ( userData->programObject );
+
+ // Delete vertex buffer objects
+ glDeleteBuffers ( 2, userData->vboIds );
}