diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-04-03 13:27:54 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-04-03 13:27:54 -0700 |
commit | 0a1c635b5ad1612c05a271dda5f49cf8613f6cb7 (patch) | |
tree | 1bd356c292e86bad3d2fcbd1e5ead4a77fc23fc3 /tests/glbook | |
parent | 499bc0dc2a755a2f3ff0e6911383e0639ae25220 (diff) |
add another working glbook test
Diffstat (limited to 'tests/glbook')
-rw-r--r-- | tests/glbook/Chapter_9/Simple_Texture2D/Simple_Texture2D.c | 52 | ||||
-rw-r--r-- | tests/glbook/Makefile | 2 |
2 files changed, 34 insertions, 20 deletions
diff --git a/tests/glbook/Chapter_9/Simple_Texture2D/Simple_Texture2D.c b/tests/glbook/Chapter_9/Simple_Texture2D/Simple_Texture2D.c index cc465c9a..5276bda4 100644 --- a/tests/glbook/Chapter_9/Simple_Texture2D/Simple_Texture2D.c +++ b/tests/glbook/Chapter_9/Simple_Texture2D/Simple_Texture2D.c @@ -32,6 +32,8 @@ typedef struct // Texture handle GLuint textureId; + GLuint vertexObject, indexObject; + } UserData; /// @@ -111,7 +113,27 @@ int Init ( ESContext *esContext ) // Load the texture userData->textureId = CreateSimpleTexture2D (); - glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); + // Setup the vertex data + GLfloat vVertices[] = { -0.5, 0.5, 0.0, // Position 0 + 0.0, 0.0, // TexCoord 0 + -0.5, -0.5, 0.0, // Position 1 + 0.0, 1.0, // TexCoord 1 + 0.5, -0.5, 0.0, // Position 2 + 1.0, 1.0, // TexCoord 2 + 0.5, 0.5, 0.0, // Position 3 + 1.0, 0.0 // TexCoord 3 + }; + GLushort indices[] = { 0, 1, 2, 0, 2, 3 }; + + glGenBuffers(1, &userData->vertexObject); + glBindBuffer(GL_ARRAY_BUFFER, userData->vertexObject ); + glBufferData(GL_ARRAY_BUFFER, sizeof(vVertices), vVertices, GL_STATIC_DRAW ); + + glGenBuffers(1, &userData->indexObject); + glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, userData->indexObject ); + glBufferData ( GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW ); + + glClearColor ( 0.0f, 0.0f, 0.0f, 1.0f ); return GL_TRUE; } @@ -119,34 +141,26 @@ int Init ( ESContext *esContext ) // Draw a triangle using the shader pair created in Init() // void Draw ( ESContext *esContext ) -{ - UserData *userData = esContext->userData; - GLfloat vVertices[] = { -0.5f, 0.5f, 0.0f, // Position 0 - 0.0f, 0.0f, // TexCoord 0 - -0.5f, -0.5f, 0.0f, // Position 1 - 0.0f, 1.0f, // TexCoord 1 - 0.5f, -0.5f, 0.0f, // Position 2 - 1.0f, 1.0f, // TexCoord 2 - 0.5f, 0.5f, 0.0f, // Position 3 - 1.0f, 0.0f // TexCoord 3 - }; - GLushort indices[] = { 0, 1, 2, 0, 2, 3 }; - +{ // Set the viewport glViewport ( 0, 0, esContext->width, esContext->height ); // Clear the color buffer glClear ( GL_COLOR_BUFFER_BIT ); + UserData *userData = esContext->userData; + // Use the program object glUseProgram ( userData->programObject ); // Load the vertex position - glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, - GL_FALSE, 5 * sizeof(GLfloat), vVertices ); + glBindBuffer (GL_ARRAY_BUFFER, userData->vertexObject ); + glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, + GL_FALSE, 5 * 4, 0 ); // Load the texture coordinate glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT, - GL_FALSE, 5 * sizeof(GLfloat), &vVertices[3] ); + GL_FALSE, 5 * 4, + 3 * 4 ); glEnableVertexAttribArray ( userData->positionLoc ); glEnableVertexAttribArray ( userData->texCoordLoc ); @@ -158,8 +172,8 @@ void Draw ( ESContext *esContext ) // Set the sampler texture unit to 0 glUniform1i ( userData->samplerLoc, 0 ); - glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices ); - + glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, userData->indexObject ); + glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 ); } /// diff --git a/tests/glbook/Makefile b/tests/glbook/Makefile index 386d5717..6220fc3c 100644 --- a/tests/glbook/Makefile +++ b/tests/glbook/Makefile @@ -24,7 +24,7 @@ default: all all: ./Chapter_2/Hello_Triangle/CH02_HelloTriangle.bc \ ./Chapter_8/Simple_VertexShader/CH08_SimpleVertexShader.bc \ -# ./Chapter_9/Simple_Texture2D/CH09_SimpleTexture2D.bc \ + ./Chapter_9/Simple_Texture2D/CH09_SimpleTexture2D.bc \ # ./Chapter_9/MipMap2D/CH09_MipMap2D.bc \ # ./Chapter_9/Simple_TextureCubemap/CH09_TextureCubemap.bc \ # ./Chapter_9/TextureWrap/CH09_TextureWrap.bc \ |