Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(625)

Unified Diff: third_party/gles_book_examples/Chapter_2/Hello_Triangle/Hello_Triangle.c

Issue 527016: Added gyp file for examples. Removed unnecessary functions. Added header file... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/gles_book_examples/Chapter_2/Hello_Triangle/Hello_Triangle.c
===================================================================
--- third_party/gles_book_examples/Chapter_2/Hello_Triangle/Hello_Triangle.c (revision 35821)
+++ third_party/gles_book_examples/Chapter_2/Hello_Triangle/Hello_Triangle.c (working copy)
@@ -14,70 +14,18 @@
// a minimal vertex/fragment shader. The purpose of this
// example is to demonstrate the basic concepts of
// OpenGL ES 2.0 rendering.
-#include <stdlib.h>
-#include "esUtil.h"
-typedef struct
-{
- // Handle to a program object
- GLuint programObject;
+#include "Hello_Triangle.h"
-} UserData;
+#include <stdlib.h>
///
-// Create a shader object, load the shader source, and
-// compile the shader.
-//
-GLuint LoadShader ( GLenum type, const char *shaderSrc )
-{
- GLuint shader;
- GLint compiled;
-
- // Create the shader object
- shader = glCreateShader ( type );
-
- if ( shader == 0 )
- return 0;
-
- // Load the shader source
- glShaderSource ( shader, 1, &shaderSrc, NULL );
-
- // Compile the shader
- glCompileShader ( shader );
-
- // Check the compile status
- glGetShaderiv ( shader, GL_COMPILE_STATUS, &compiled );
-
- if ( !compiled )
- {
- GLint infoLen = 0;
-
- glGetShaderiv ( shader, GL_INFO_LOG_LENGTH, &infoLen );
-
- if ( infoLen > 1 )
- {
- char* infoLog = malloc (sizeof(char) * infoLen );
-
- glGetShaderInfoLog ( shader, infoLen, NULL, infoLog );
- esLogMessage ( "Error compiling shader:\n%s\n", infoLog );
-
- free ( infoLog );
- }
-
- glDeleteShader ( shader );
- return 0;
- }
-
- return shader;
-
-}
-
-///
// Initialize the shader and program object
//
-int Init ( ESContext *esContext )
+int htInit ( ESContext *esContext )
{
- UserData *userData = esContext->userData;
+ HTUserData *userData = esContext->userData;
+
GLbyte vShaderStr[] =
"attribute vec4 vPosition; \n"
"void main() \n"
@@ -85,63 +33,29 @@
" 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";
+ "//precision mediump float; \n"
+ "void main() \n"
+ "{ \n"
+ " gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n"
+ "} \n";
- GLuint vertexShader;
- GLuint fragmentShader;
- GLuint programObject;
- GLint linked;
+ GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f,
+ -0.5f, -0.5f, 0.0f,
+ 0.5f, -0.5f, 0.0f };
- // Load the vertex/fragment shaders
- vertexShader = LoadShader ( GL_VERTEX_SHADER, vShaderStr );
- fragmentShader = LoadShader ( GL_FRAGMENT_SHADER, fShaderStr );
+ userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );
+ if ( userData->programObject == 0 ) return FALSE;
- // Create the program object
- programObject = glCreateProgram ( );
-
- if ( programObject == 0 )
- return 0;
-
- glAttachShader ( programObject, vertexShader );
- glAttachShader ( programObject, fragmentShader );
-
// Bind vPosition to attribute 0
- glBindAttribLocation ( programObject, 0, "vPosition" );
+ glBindAttribLocation ( userData->programObject, 0, "vPosition" );
- // Link the program
- glLinkProgram ( programObject );
+ 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 );
- // Check the link status
- glGetProgramiv ( programObject, GL_LINK_STATUS, &linked );
-
- if ( !linked )
- {
- GLint infoLen = 0;
-
- glGetProgramiv ( programObject, GL_INFO_LOG_LENGTH, &infoLen );
-
- if ( infoLen > 1 )
- {
- char* infoLog = malloc (sizeof(char) * infoLen );
-
- glGetProgramInfoLog ( programObject, infoLen, NULL, infoLog );
- esLogMessage ( "Error linking program:\n%s\n", infoLog );
-
- free ( infoLog );
- }
-
- glDeleteProgram ( programObject );
- return FALSE;
- }
-
- // Store the program object
- userData->programObject = programObject;
-
glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
return TRUE;
}
@@ -149,13 +63,10 @@
///
// Draw a triangle using the shader pair created in Init()
//
-void Draw ( ESContext *esContext )
+void htDraw ( ESContext *esContext )
{
- UserData *userData = esContext->userData;
- GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f,
- -0.5f, -0.5f, 0.0f,
- 0.5f, -0.5f, 0.0f };
-
+ HTUserData *userData = esContext->userData;
+
// Set the viewport
glViewport ( 0, 0, esContext->width, esContext->height );
@@ -166,29 +77,33 @@
glUseProgram ( userData->programObject );
// Load the vertex data
- glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vVertices );
+ glBindBuffer ( GL_ARRAY_BUFFER, userData->vbo );
glEnableVertexAttribArray ( 0 );
+ glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, 0 );
glDrawArrays ( GL_TRIANGLES, 0, 3 );
- eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
+ // Nothing is drawn or application crashes without glFlush.
+ // TODO(alokp): glFlush should not be necessary with SwapBuffers().
+ glFlush();
}
-
-int main ( int argc, char *argv[] )
+///
+// Cleanup
+//
+void htShutDown ( ESContext *esContext )
{
- ESContext esContext;
- UserData userData;
+ HTUserData *userData = esContext->userData;
- esInitContext ( &esContext );
- esContext.userData = &userData;
-
- esCreateWindow ( &esContext, "Hello Triangle", 320, 240, ES_WINDOW_RGB );
-
- if ( !Init ( &esContext ) )
- return 0;
-
- esRegisterDrawFunc ( &esContext, Draw );
-
- esMainLoop ( &esContext );
+ // Delete program object
+ if ( userData->programObject != 0 )
+ {
+ glDeleteProgram ( userData->programObject );
+ userData->programObject = 0;
+ }
+ if ( userData->vbo != 0 )
+ {
+ glDeleteBuffers ( 1, &userData->vbo );
+ userData->vbo = 0;
+ }
}

Powered by Google App Engine
This is Rietveld 408576698