diff options
author | alokp@chromium.org <alokp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-14 21:06:11 +0000 |
---|---|---|
committer | alokp@chromium.org <alokp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-14 21:06:11 +0000 |
commit | c1823aa513cae301ce28438e62b0eab05c3a4e5f (patch) | |
tree | db20df6ce4305c9beda843dc90eff1f60f721c8c | |
parent | 9925f734681d1f0dd8f30576a0c4cebfa4339190 (diff) | |
download | chromium_src-c1823aa513cae301ce28438e62b0eab05c3a4e5f.zip chromium_src-c1823aa513cae301ce28438e62b0eab05c3a4e5f.tar.gz chromium_src-c1823aa513cae301ce28438e62b0eab05c3a4e5f.tar.bz2 |
Added simple texture demo.
BUG=26099
TEST=Run simple_texture_2d executable. You should see a square with checker-board pattern.
Review URL: http://codereview.chromium.org/543063
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@36267 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | gpu/demos/demos.gyp | 11 | ||||
-rw-r--r-- | gpu/demos/simple_texture_2d/main.cc | 65 | ||||
-rw-r--r-- | third_party/gles2_book/Chapter_9/Simple_Texture2D/Simple_Texture2D.c | 104 | ||||
-rw-r--r-- | third_party/gles2_book/Chapter_9/Simple_Texture2D/Simple_Texture2D.h | 49 | ||||
-rw-r--r-- | third_party/gles2_book/gles2_book.gyp | 11 |
5 files changed, 177 insertions, 63 deletions
diff --git a/gpu/demos/demos.gyp b/gpu/demos/demos.gyp index a7596c1..0f55b0f 100644 --- a/gpu/demos/demos.gyp +++ b/gpu/demos/demos.gyp @@ -46,6 +46,17 @@ ], }, { + 'target_name': 'simple_texture_2d', + 'type': 'executable', + 'dependencies': [ + 'app_framework', + '../../third_party/gles2_book/gles2_book.gyp:simple_texture_2d', + ], + 'sources': [ + 'simple_texture_2d/main.cc', + ], + }, + { 'target_name': 'simple_vertex_shader', 'type': 'executable', 'dependencies': [ diff --git a/gpu/demos/simple_texture_2d/main.cc b/gpu/demos/simple_texture_2d/main.cc new file mode 100644 index 0000000..7ba8691 --- /dev/null +++ b/gpu/demos/simple_texture_2d/main.cc @@ -0,0 +1,65 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This is a simple example that draws a quad with a 2D +// texture image. The purpose of this example is to demonstrate +// the basics of 2D texturing + +#include "gpu/demos/app_framework/application.h" +#include "third_party/gles2_book/Chapter_9/Simple_Texture2D/Simple_Texture2D.h" + +namespace gpu_demos { +class SimpleTexture2D : public Application { + public: + SimpleTexture2D(); + ~SimpleTexture2D(); + + bool Init(); + + protected: + virtual void Draw(float); + + private: + ESContext context_; + STUserData user_data_; + + DISALLOW_COPY_AND_ASSIGN(SimpleTexture2D); +}; + +SimpleTexture2D::SimpleTexture2D() { + esInitContext(&context_); + + memset(&user_data_, 0, sizeof(STUserData)); + context_.userData = &user_data_; +} + +SimpleTexture2D::~SimpleTexture2D() { + stShutDown(&context_); +} + +bool SimpleTexture2D::Init() { + if (!Application::InitRenderContext()) return false; + + context_.width = width(); + context_.height = height(); + if (!stInit(&context_)) return false; + + return true; +} + +void SimpleTexture2D::Draw(float) { + stDraw(&context_); +} +} // namespace gpu_demos + +int main(int argc, char *argv[]) { + gpu_demos::SimpleTexture2D app; + if (!app.Init()) { + printf("Could not init.\n"); + return EXIT_FAILURE; + } + + app.MainLoop(); + return EXIT_SUCCESS; +} diff --git a/third_party/gles2_book/Chapter_9/Simple_Texture2D/Simple_Texture2D.c b/third_party/gles2_book/Chapter_9/Simple_Texture2D/Simple_Texture2D.c index 03d2a6c..12200fd 100644 --- a/third_party/gles2_book/Chapter_9/Simple_Texture2D/Simple_Texture2D.c +++ b/third_party/gles2_book/Chapter_9/Simple_Texture2D/Simple_Texture2D.c @@ -15,29 +15,12 @@ // the basics of 2D texturing // #include <stdlib.h> -#include "esUtil.h" - -typedef struct -{ - // Handle to a program object - GLuint programObject; - - // Attribute locations - GLint positionLoc; - GLint texCoordLoc; - - // Sampler location - GLint samplerLoc; - - // Texture handle - GLuint textureId; - -} UserData; +#include "Simple_Texture2D.h" /// // Create a simple 2x2 texture image with four different colors // -GLuint CreateSimpleTexture2D( ) +static GLuint CreateSimpleTexture2D( ) { // Texture object handle GLuint textureId; @@ -75,9 +58,9 @@ GLuint CreateSimpleTexture2D( ) /// // Initialize the shader and program object // -int Init ( ESContext *esContext ) +int stInit ( ESContext *esContext ) { - UserData *userData = esContext->userData; + STUserData *userData = esContext->userData; GLbyte vShaderStr[] = "attribute vec4 a_position; \n" "attribute vec2 a_texCoord; \n" @@ -97,6 +80,17 @@ int Init ( ESContext *esContext ) " gl_FragColor = texture2D( s_texture, v_texCoord );\n" "} \n"; + 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 }; + // Load the shaders and get a linked program object userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); @@ -110,6 +104,15 @@ int Init ( ESContext *esContext ) // Load the texture userData->textureId = CreateSimpleTexture2D (); + // 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; } @@ -117,19 +120,13 @@ int Init ( ESContext *esContext ) /// // Draw a triangle using the shader pair created in Init() // -void Draw ( ESContext *esContext ) +#define VTX_POS_SIZE 3 +#define VTX_TEX_SIZE 2 +#define VTX_STRIDE (5 * sizeof(GLfloat)) +void stDraw ( 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 }; + STUserData *userData = esContext->userData; + GLuint offset = 0; // Set the viewport glViewport ( 0, 0, esContext->width, esContext->height ); @@ -141,11 +138,12 @@ void Draw ( ESContext *esContext ) glUseProgram ( userData->programObject ); // Load the vertex position - glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, - GL_FALSE, 5 * 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, 5 * sizeof(GLfloat), &vVertices[3] ); + 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 ); @@ -157,42 +155,22 @@ void Draw ( ESContext *esContext ) // Set the sampler texture unit to 0 glUniform1i ( userData->samplerLoc, 0 ); - glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices ); - - eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface ); + glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 ); } /// // Cleanup // -void ShutDown ( ESContext *esContext ) +void stShutDown ( ESContext *esContext ) { - UserData *userData = esContext->userData; + STUserData *userData = esContext->userData; // Delete texture object glDeleteTextures ( 1, &userData->textureId ); + // Delete VBOs + glDeleteBuffers ( 2, userData->vboIds ); + // Delete program object glDeleteProgram ( userData->programObject ); } - - -int main ( int argc, char *argv[] ) -{ - ESContext esContext; - UserData userData; - - esInitContext ( &esContext ); - esContext.userData = &userData; - - esCreateWindow ( &esContext, "Simple Texture 2D", 320, 240, ES_WINDOW_RGB ); - - if ( !Init ( &esContext ) ) - return 0; - - esRegisterDrawFunc ( &esContext, Draw ); - - esMainLoop ( &esContext ); - - ShutDown ( &esContext ); -} diff --git a/third_party/gles2_book/Chapter_9/Simple_Texture2D/Simple_Texture2D.h b/third_party/gles2_book/Chapter_9/Simple_Texture2D/Simple_Texture2D.h new file mode 100644 index 0000000..835b63f --- /dev/null +++ b/third_party/gles2_book/Chapter_9/Simple_Texture2D/Simple_Texture2D.h @@ -0,0 +1,49 @@ +// +// Book: OpenGL(R) ES 2.0 Programming Guide +// Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner +// ISBN-10: 0321502795 +// ISBN-13: 9780321502797 +// Publisher: Addison-Wesley Professional +// URLs: http://safari.informit.com/9780321563835 +// http://www.opengles-book.com +// + +#ifndef SIMPLE_TEXTURE_2D_H +#define SIMPLE_TEXTURE_2D_H + +#include "esUtil.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +typedef struct +{ + // Handle to a program object + GLuint programObject; + + // Attribute locations + GLint positionLoc; + GLint texCoordLoc; + + // Sampler location + GLint samplerLoc; + + // Texture handle + GLuint textureId; + + // Vertex buffer object handles + GLuint vboIds[2]; + +} STUserData; + +extern int stInit ( ESContext *esContext ); + +extern void stDraw ( ESContext *esContext ); + +extern void stShutDown ( ESContext *esContext ); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // SIMPLE_TEXTURE_2D_H diff --git a/third_party/gles2_book/gles2_book.gyp b/third_party/gles2_book/gles2_book.gyp index d94fcc3..b71b5e1 100644 --- a/third_party/gles2_book/gles2_book.gyp +++ b/third_party/gles2_book/gles2_book.gyp @@ -51,6 +51,17 @@ ], }, { + 'target_name': 'simple_texture_2d', + 'type': 'static_library', + 'dependencies': [ + 'es_util', + ], + 'sources': [ + 'Chapter_9/Simple_Texture2D/Simple_Texture2D.c', + 'Chapter_9/Simple_Texture2D/Simple_Texture2D.h', + ], + }, + { 'target_name': 'simple_vertex_shader', 'type': 'static_library', 'dependencies': [ |