diff options
author | alokp@chromium.org <alokp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-09 17:06:42 +0000 |
---|---|---|
committer | alokp@chromium.org <alokp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-09 17:06:42 +0000 |
commit | efb8f2987c9b500b3af21f3498df5ac4ae091551 (patch) | |
tree | 02e102d22c835937981b20e178af38d01e366eb0 /third_party/gles2_book/Chapter_2 | |
parent | 1fe3ec310377e2549d05344583251e9e45148553 (diff) | |
download | chromium_src-efb8f2987c9b500b3af21f3498df5ac4ae091551.zip chromium_src-efb8f2987c9b500b3af21f3498df5ac4ae091551.tar.gz chromium_src-efb8f2987c9b500b3af21f3498df5ac4ae091551.tar.bz2 |
Renamed gles_book_examples to gles2_book to make it shorter and more correct. I still need to rename gles_book_examples.gyp to gles2_book.gyp. I will do it in another CL as svn does not like changes in renamed directories.
TBR=apatrick
Review URL: http://codereview.chromium.org/543002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@35875 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'third_party/gles2_book/Chapter_2')
-rw-r--r-- | third_party/gles2_book/Chapter_2/Hello_Triangle/Hello_Triangle.c | 109 | ||||
-rw-r--r-- | third_party/gles2_book/Chapter_2/Hello_Triangle/Hello_Triangle.h | 38 |
2 files changed, 147 insertions, 0 deletions
diff --git a/third_party/gles2_book/Chapter_2/Hello_Triangle/Hello_Triangle.c b/third_party/gles2_book/Chapter_2/Hello_Triangle/Hello_Triangle.c new file mode 100644 index 0000000..6021f687 --- /dev/null +++ b/third_party/gles2_book/Chapter_2/Hello_Triangle/Hello_Triangle.c @@ -0,0 +1,109 @@ +// +// 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 +// + +// Hello_Triangle.c +// +// 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 "Hello_Triangle.h" + +#include <stdlib.h> + +/// +// Initialize the shader and program object +// +int htInit ( ESContext *esContext ) +{ + HTUserData *userData = esContext->userData; + + GLbyte vShaderStr[] = + "attribute vec4 vPosition; \n" + "void main() \n" + "{ \n" + " gl_Position = vPosition; \n" + "} \n"; + + // TODO(alokp): Shaders containing "precision" do not compile. + GLbyte fShaderStr[] = + "//precision mediump float; \n" + "void main() \n" + "{ \n" + " gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n" + "} \n"; + + GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f, + -0.5f, -0.5f, 0.0f, + 0.5f, -0.5f, 0.0f }; + + userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); + if ( userData->programObject == 0 ) return FALSE; + + // Bind vPosition to attribute 0 + glBindAttribLocation ( userData->programObject, 0, "vPosition" ); + + glGenBuffers ( 1, &userData->vbo ); + glBindBuffer ( GL_ARRAY_BUFFER, userData->vbo ); + glBufferData ( GL_ARRAY_BUFFER, sizeof(vVertices), NULL, GL_STATIC_DRAW ); + glBufferSubData ( GL_ARRAY_BUFFER, 0, sizeof(vVertices), vVertices ); + + glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); + return TRUE; +} + +/// +// Draw a triangle using the shader pair created in Init() +// +void htDraw ( ESContext *esContext ) +{ + HTUserData *userData = esContext->userData; + + // Set the viewport + glViewport ( 0, 0, esContext->width, esContext->height ); + + // Clear the color buffer + glClear ( GL_COLOR_BUFFER_BIT ); + + // Use the program object + glUseProgram ( userData->programObject ); + + // Load the vertex data + glBindBuffer ( GL_ARRAY_BUFFER, userData->vbo ); + glEnableVertexAttribArray ( 0 ); + glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, 0 ); + + glDrawArrays ( GL_TRIANGLES, 0, 3 ); + + // Nothing is drawn or application crashes without glFlush. + // TODO(alokp): glFlush should not be necessary with SwapBuffers(). + glFlush(); +} + +/// +// Cleanup +// +void htShutDown ( ESContext *esContext ) +{ + HTUserData *userData = esContext->userData; + + // Delete program object + if ( userData->programObject != 0 ) + { + glDeleteProgram ( userData->programObject ); + userData->programObject = 0; + } + if ( userData->vbo != 0 ) + { + glDeleteBuffers ( 1, &userData->vbo ); + userData->vbo = 0; + } +} diff --git a/third_party/gles2_book/Chapter_2/Hello_Triangle/Hello_Triangle.h b/third_party/gles2_book/Chapter_2/Hello_Triangle/Hello_Triangle.h new file mode 100644 index 0000000..4e90d2f --- /dev/null +++ b/third_party/gles2_book/Chapter_2/Hello_Triangle/Hello_Triangle.h @@ -0,0 +1,38 @@ +// +// 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 HELLO_TRIANGLE_H +#define HELLO_TRIANGLE_H + +#include "esUtil.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +typedef struct +{ + // Handle to a program object + GLuint programObject; + // Handle to vbo object + GLuint vbo; + +} HTUserData; + +extern int htInit ( ESContext *esContext ); + +extern void htDraw ( ESContext *esContext ); + +extern void htShutDown ( ESContext *esContext ); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // HELLO_TRIANGLE_H |