diff options
author | alokp@chromium.org <alokp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-13 17:20:44 +0000 |
---|---|---|
committer | alokp@chromium.org <alokp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-13 17:20:44 +0000 |
commit | bd0dae9836979fb4d33574e34ecde51b911e87c6 (patch) | |
tree | 24a6d98dd5e1b98265224d64650057ed9491282d | |
parent | 8e65f11ba2e6ba7ebbc81e23aef288969b0f5ebd (diff) | |
download | chromium_src-bd0dae9836979fb4d33574e34ecde51b911e87c6.zip chromium_src-bd0dae9836979fb4d33574e34ecde51b911e87c6.tar.gz chromium_src-bd0dae9836979fb4d33574e34ecde51b911e87c6.tar.bz2 |
Added simple vertex shader demo. I had to make the following modifications to third-party code:
- Changed client-side vertex array to VBO
- Used GL_UNSIGNED_SHORT instead of GL_UNSIGNED_INT in glDrawElements because GL_UNSIGNED_INT is not supported for by OpenGL ES 2.0 for indices
BUG=26099
TEST=Run simple_vertex_shader executable, you should see a rotating cube.
Review URL: http://codereview.chromium.org/543015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@36127 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | gpu/demos/demos.gyp | 11 | ||||
-rw-r--r-- | gpu/demos/simple_vertex_shader/main.cc | 67 | ||||
-rw-r--r-- | third_party/gles2_book/Chapter_8/Simple_VertexShader/Simple_VertexShader.c | 41 | ||||
-rw-r--r-- | third_party/gles2_book/Chapter_8/Simple_VertexShader/Simple_VertexShader.h | 7 | ||||
-rw-r--r-- | third_party/gles2_book/Common/Include/esUtil.h | 4 | ||||
-rw-r--r-- | third_party/gles2_book/Common/Source/esShapes.c | 12 |
6 files changed, 111 insertions, 31 deletions
diff --git a/gpu/demos/demos.gyp b/gpu/demos/demos.gyp index e40d5b5..43ecb50 100644 --- a/gpu/demos/demos.gyp +++ b/gpu/demos/demos.gyp @@ -34,6 +34,17 @@ 'hello_triangle/main.cc', ], }, + { + 'target_name': 'simple_vertex_shader', + 'type': 'executable', + 'dependencies': [ + 'app_framework', + '../../third_party/gles2_book/gles2_book.gyp:simple_vertex_shader', + ], + 'sources': [ + 'simple_vertex_shader/main.cc', + ], + }, ] } diff --git a/gpu/demos/simple_vertex_shader/main.cc b/gpu/demos/simple_vertex_shader/main.cc new file mode 100644 index 0000000..d042942 --- /dev/null +++ b/gpu/demos/simple_vertex_shader/main.cc @@ -0,0 +1,67 @@ +// Copyright (c) 2009 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 single triangle with +// a minimal vertex/fragment shader. The purpose of this +// example is to demonstrate the basic concepts of +// OpenGL ES 2.0 rendering. + +#include "gpu/demos/app_framework/application.h" +#include "third_party/gles2_book/Chapter_8/Simple_VertexShader/Simple_VertexShader.h" + +namespace gpu_demos { +class SimpleVertexShader : public Application { + public: + SimpleVertexShader(); + ~SimpleVertexShader(); + + bool Init(); + + protected: + virtual void Draw(); + + private: + ESContext context_; + SVSUserData user_data_; + + DISALLOW_COPY_AND_ASSIGN(SimpleVertexShader); +}; + +SimpleVertexShader::SimpleVertexShader() { + esInitContext(&context_); + + memset(&user_data_, 0, sizeof(SVSUserData)); + context_.userData = &user_data_; +} + +SimpleVertexShader::~SimpleVertexShader() { + svsShutDown(&context_); +} + +bool SimpleVertexShader::Init() { + if (!Application::InitRenderContext()) return false; + + context_.width = width(); + context_.height = height(); + if (!svsInit(&context_)) return false; + svsUpdate(&context_, 0.0f); + + return true; +} + +void SimpleVertexShader::Draw() { + svsDraw(&context_); +} +} // namespace gpu_demos + +int main(int argc, char *argv[]) { + gpu_demos::SimpleVertexShader 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_8/Simple_VertexShader/Simple_VertexShader.c b/third_party/gles2_book/Chapter_8/Simple_VertexShader/Simple_VertexShader.c index ad6f188..8b9c217 100644 --- a/third_party/gles2_book/Chapter_8/Simple_VertexShader/Simple_VertexShader.c +++ b/third_party/gles2_book/Chapter_8/Simple_VertexShader/Simple_VertexShader.c @@ -23,6 +23,10 @@ int svsInit ( ESContext *esContext ) { SVSUserData *userData = esContext->userData; + int numVertices = 24; + GLfloat *vertices = NULL; + GLushort *indices = NULL; + GLbyte vShaderStr[] = "uniform mat4 u_mvpMatrix; \n" "attribute vec4 a_position; \n" @@ -41,6 +45,7 @@ int svsInit ( ESContext *esContext ) // Load the shaders and get a linked program object userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); + if ( userData->programObject == 0 ) return FALSE; // Get the attribute locations userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" ); @@ -49,9 +54,17 @@ int svsInit ( ESContext *esContext ) userData->mvpLoc = glGetUniformLocation( userData->programObject, "u_mvpMatrix" ); // Generate the vertex data - userData->numIndices = esGenCube( 1.0, &userData->vertices, - NULL, NULL, &userData->indices ); - + userData->numIndices = esGenCube( 1.0, &vertices, NULL, NULL, &indices ); + glGenBuffers ( 2, userData->vboIds ); + glBindBuffer ( GL_ARRAY_BUFFER, userData->vboIds[0] ); + glBufferData ( GL_ARRAY_BUFFER, 3 * numVertices * sizeof(GLfloat), + vertices, GL_STATIC_DRAW ); + glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, userData->vboIds[1] ); + glBufferData ( GL_ELEMENT_ARRAY_BUFFER, userData->numIndices * sizeof(GL_UNSIGNED_SHORT), + indices, GL_STATIC_DRAW ); + if ( vertices != NULL ) free ( vertices ); + if ( indices != NULL ) free ( indices ); + // Starting rotation angle for the cube userData->angle = 45.0f; @@ -105,7 +118,6 @@ void svsDraw ( ESContext *esContext ) // Set the viewport glViewport ( 0, 0, esContext->width, esContext->height ); - // Clear the color buffer glClear ( GL_COLOR_BUFFER_BIT ); @@ -113,17 +125,15 @@ void svsDraw ( ESContext *esContext ) glUseProgram ( userData->programObject ); // Load the vertex position - glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, - GL_FALSE, 3 * sizeof(GLfloat), userData->vertices ); - glEnableVertexAttribArray ( userData->positionLoc ); - - + glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, + GL_FALSE, 3 * sizeof(GLfloat), 0 ); + // Load the MVP matrix glUniformMatrix4fv( userData->mvpLoc, 1, GL_FALSE, (GLfloat*) &userData->mvpMatrix.m[0][0] ); // Draw the cube - glDrawElements ( GL_TRIANGLES, userData->numIndices, GL_UNSIGNED_INT, userData->indices ); + glDrawElements ( GL_TRIANGLES, userData->numIndices, GL_UNSIGNED_SHORT, 0 ); // TODO(alokp): glFlush should not be necessary with SwapBuffers. glFlush(); @@ -136,15 +146,8 @@ void svsShutDown ( ESContext *esContext ) { SVSUserData *userData = esContext->userData; - if ( userData->vertices != NULL ) - { - free ( userData->vertices ); - } - - if ( userData->indices != NULL ) - { - free ( userData->indices ); - } + // Delete program object + glDeleteBuffers ( 2, userData->vboIds ); // Delete program object glDeleteProgram ( userData->programObject ); diff --git a/third_party/gles2_book/Chapter_8/Simple_VertexShader/Simple_VertexShader.h b/third_party/gles2_book/Chapter_8/Simple_VertexShader/Simple_VertexShader.h index e3c8832..b581185 100644 --- a/third_party/gles2_book/Chapter_8/Simple_VertexShader/Simple_VertexShader.h +++ b/third_party/gles2_book/Chapter_8/Simple_VertexShader/Simple_VertexShader.h @@ -28,10 +28,9 @@ typedef struct // Uniform locations GLint mvpLoc; - // Vertex daata - GLfloat *vertices; - GLuint *indices; - int numIndices; + // Vertex data + int numIndices; + GLuint vboIds[2]; // Rotation angle GLfloat angle; diff --git a/third_party/gles2_book/Common/Include/esUtil.h b/third_party/gles2_book/Common/Include/esUtil.h index a1a72c4..9897e1b 100644 --- a/third_party/gles2_book/Common/Include/esUtil.h +++ b/third_party/gles2_book/Common/Include/esUtil.h @@ -95,7 +95,7 @@ extern GLuint esLoadProgram ( const char *vertShaderSrc, const char *fragShaderS /// if it is not NULL ) as a GL_TRIANGLE_STRIP // extern int esGenSphere ( int numSlices, float radius, GLfloat **vertices, GLfloat **normals, - GLfloat **texCoords, GLuint **indices ); + GLfloat **texCoords, GLushort **indices ); // /// \brief Generates geometry for a cube. Allocates memory for the vertex data and stores @@ -109,7 +109,7 @@ extern int esGenSphere ( int numSlices, float radius, GLfloat **vertices, GLfloa /// if it is not NULL ) as a GL_TRIANGLES // extern int esGenCube ( float scale, GLfloat **vertices, GLfloat **normals, - GLfloat **texCoords, GLuint **indices ); + GLfloat **texCoords, GLushort **indices ); // /// \brief Loads a 24-bit TGA image from a file diff --git a/third_party/gles2_book/Common/Source/esShapes.c b/third_party/gles2_book/Common/Source/esShapes.c index ddb75c7..dee7efb 100644 --- a/third_party/gles2_book/Common/Source/esShapes.c +++ b/third_party/gles2_book/Common/Source/esShapes.c @@ -52,7 +52,7 @@ /// if it is not NULL ) as a GL_TRIANGLE_STRIP // int esGenSphere ( int numSlices, float radius, GLfloat **vertices, GLfloat **normals, - GLfloat **texCoords, GLuint **indices ) + GLfloat **texCoords, GLushort **indices ) { int i; int j; @@ -72,7 +72,7 @@ int esGenSphere ( int numSlices, float radius, GLfloat **vertices, GLfloat **nor *texCoords = malloc ( sizeof(GLfloat) * 2 * numVertices ); if ( indices != NULL ) - *indices = malloc ( sizeof(GLuint) * numIndices ); + *indices = malloc ( sizeof(GLushort) * numIndices ); for ( i = 0; i < numParallels + 1; i++ ) { @@ -108,7 +108,7 @@ int esGenSphere ( int numSlices, float radius, GLfloat **vertices, GLfloat **nor // Generate the indices if ( indices != NULL ) { - GLuint *indexBuf = (*indices); + GLushort *indexBuf = (*indices); for ( i = 0; i < numParallels ; i++ ) { for ( j = 0; j < numSlices; j++ ) @@ -139,7 +139,7 @@ int esGenSphere ( int numSlices, float radius, GLfloat **vertices, GLfloat **nor /// if it is not NULL ) as a GL_TRIANGLE_STRIP // int esGenCube ( float scale, GLfloat **vertices, GLfloat **normals, - GLfloat **texCoords, GLuint **indices ) + GLfloat **texCoords, GLushort **indices ) { int i; int numVertices = 24; @@ -256,7 +256,7 @@ int esGenCube ( float scale, GLfloat **vertices, GLfloat **normals, // Generate the indices if ( indices != NULL ) { - GLuint cubeIndices[] = + GLushort cubeIndices[] = { 0, 2, 1, 0, 3, 2, @@ -272,7 +272,7 @@ int esGenCube ( float scale, GLfloat **vertices, GLfloat **normals, 20, 22, 21 }; - *indices = malloc ( sizeof(GLuint) * numIndices ); + *indices = malloc ( sizeof(GLushort) * numIndices ); memcpy( *indices, cubeIndices, sizeof( cubeIndices ) ); } |