2
// Book: OpenGL(R) ES 2.0 Programming Guide
3
// Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
5
// ISBN-13: 9780321502797
6
// Publisher: Addison-Wesley Professional
7
// URLs: http://safari.informit.com/9780321563835
8
// http://www.opengles-book.com
11
// Simple_VertexShader.c
13
// This is a simple example that draws a rotating cube in perspective
14
// using a vertex shader to transform the object
21
// Handle to a program object
24
// Attribute locations
43
// Initialize the shader and program object
45
int Init ( ESContext *esContext )
47
esContext->userData = malloc(sizeof(UserData));
49
UserData *userData = esContext->userData;
51
"uniform mat4 u_mvpMatrix; \n"
52
"attribute vec4 a_position; \n"
55
" gl_Position = u_mvpMatrix * a_position; \n"
59
"precision mediump float; \n"
62
" gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 ); \n"
65
// Load the shaders and get a linked program object
66
userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );
68
// Get the attribute locations
69
userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" );
71
// Get the uniform locations
72
userData->mvpLoc = glGetUniformLocation( userData->programObject, "u_mvpMatrix" );
74
// Generate the vertex data
75
userData->numIndices = esGenCube( 1.0, &userData->vertices,
76
NULL, NULL, &userData->indices );
78
// Starting rotation angle for the cube
79
userData->angle = 45.0f;
81
glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
87
// Update MVP matrix based on time
89
void Update ( ESContext *esContext, float deltaTime )
91
UserData *userData = (UserData*) esContext->userData;
96
// Compute a rotation angle based on time to rotate the cube
97
userData->angle += ( deltaTime * 40.0f );
98
if( userData->angle >= 360.0f )
99
userData->angle -= 360.0f;
101
// Compute the window aspect ratio
102
aspect = (GLfloat) esContext->width / (GLfloat) esContext->height;
104
// Generate a perspective matrix with a 60 degree FOV
105
esMatrixLoadIdentity( &perspective );
106
esPerspective( &perspective, 60.0f, aspect, 1.0f, 20.0f );
108
// Generate a model view matrix to rotate/translate the cube
109
esMatrixLoadIdentity( &modelview );
111
// Translate away from the viewer
112
esTranslate( &modelview, 0.0, 0.0, -2.0 );
115
esRotate( &modelview, userData->angle, 1.0, 0.0, 1.0 );
117
// Compute the final MVP by multiplying the
118
// modevleiw and perspective matrices together
119
esMatrixMultiply( &userData->mvpMatrix, &modelview, &perspective );
123
// Draw a triangle using the shader pair created in Init()
125
void Draw ( ESContext *esContext )
127
UserData *userData = esContext->userData;
130
glViewport ( 0, 0, esContext->width, esContext->height );
133
// Clear the color buffer
134
glClear ( GL_COLOR_BUFFER_BIT );
136
// Use the program object
137
glUseProgram ( userData->programObject );
139
// Load the vertex position
140
glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT,
141
GL_FALSE, 3 * sizeof(GLfloat), userData->vertices );
143
glEnableVertexAttribArray ( userData->positionLoc );
146
// Load the MVP matrix
147
glUniformMatrix4fv( userData->mvpLoc, 1, GL_FALSE, (GLfloat*) &userData->mvpMatrix.m[0][0] );
150
glDrawElements ( GL_TRIANGLES, userData->numIndices, GL_UNSIGNED_INT, userData->indices );
156
void ShutDown ( ESContext *esContext )
158
UserData *userData = esContext->userData;
160
if ( userData->vertices != NULL )
162
free ( userData->vertices );
165
if ( userData->indices != NULL )
167
free ( userData->indices );
170
// Delete program object
171
glDeleteProgram ( userData->programObject );
176
int main ( int argc, char *argv[] )
181
esInitContext ( &esContext );
182
esContext.userData = &userData;
184
esCreateWindow ( &esContext, "Simple Texture 2D", 320, 240, ES_WINDOW_RGB );
186
if ( !Init ( &esContext ) )
189
esRegisterDrawFunc ( &esContext, Draw );
190
esRegisterUpdateFunc ( &esContext, Update );
192
esMainLoop ( &esContext );
194
ShutDown ( &esContext );