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
41
GLuint vertPosObject, indicesObject;
45
// Initialize the shader and program object
47
int Init ( ESContext *esContext )
49
esContext->userData = malloc(sizeof(UserData));
51
UserData *userData = esContext->userData;
53
"uniform mat4 u_mvpMatrix; \n"
54
"attribute vec4 a_position; \n"
57
" gl_Position = u_mvpMatrix * a_position; \n"
61
"precision mediump float; \n"
64
" gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 ); \n"
67
// Load the shaders and get a linked program object
68
userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );
70
// Get the attribute locations
71
userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" );
73
// Get the uniform locations
74
userData->mvpLoc = glGetUniformLocation( userData->programObject, "u_mvpMatrix" );
76
// Generate the vertex data
77
userData->numIndices = esGenCube( 1.0, &userData->vertices,
78
NULL, NULL, &userData->indices );
80
// Starting rotation angle for the cube
81
userData->angle = 45.0f;
83
glGenBuffers(1, &userData->vertPosObject);
84
glBindBuffer(GL_ARRAY_BUFFER, userData->vertPosObject);
85
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat) * 3, userData->vertices, GL_STATIC_DRAW);
87
glGenBuffers(1, &userData->indicesObject);
88
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, userData->indicesObject);
89
glBufferData(GL_ELEMENT_ARRAY_BUFFER, userData->numIndices * sizeof(GLushort), userData->indices, GL_STATIC_DRAW);
91
glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
97
// Update MVP matrix based on time
99
void Update ( ESContext *esContext, float deltaTime )
101
UserData *userData = (UserData*) esContext->userData;
102
ESMatrix perspective;
106
// Compute a rotation angle based on time to rotate the cube
107
userData->angle += ( deltaTime * 40.0f );
108
if( userData->angle >= 360.0f )
109
userData->angle -= 360.0f;
111
// Compute the window aspect ratio
112
aspect = (GLfloat) esContext->width / (GLfloat) esContext->height;
114
// Generate a perspective matrix with a 60 degree FOV
115
esMatrixLoadIdentity( &perspective );
116
esPerspective( &perspective, 60.0f, aspect, 1.0f, 20.0f );
118
// Generate a model view matrix to rotate/translate the cube
119
esMatrixLoadIdentity( &modelview );
121
// Translate away from the viewer
122
esTranslate( &modelview, 0.0, 0.0, -2.0 );
125
esRotate( &modelview, userData->angle, 1.0, 0.0, 1.0 );
127
// Compute the final MVP by multiplying the
128
// modevleiw and perspective matrices together
129
esMatrixMultiply( &userData->mvpMatrix, &modelview, &perspective );
133
// Draw a triangle using the shader pair created in Init()
135
void Draw ( ESContext *esContext )
137
UserData *userData = esContext->userData;
140
glViewport ( 0, 0, esContext->width, esContext->height );
143
// Clear the color buffer
144
glClear ( GL_COLOR_BUFFER_BIT );
146
// Use the program object
147
glUseProgram ( userData->programObject );
149
// Load the vertex position
150
glBindBuffer(GL_ARRAY_BUFFER, userData->vertPosObject);
151
glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT,
153
glEnableVertexAttribArray ( userData->positionLoc );
156
// Load the index buffer
157
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, userData->indicesObject);
159
// Load the MVP matrix
160
glUniformMatrix4fv( userData->mvpLoc, 1, GL_FALSE, (GLfloat*) &userData->mvpMatrix.m[0][0] );
163
glDrawElements ( GL_TRIANGLES, userData->numIndices, GL_UNSIGNED_SHORT, 0 );
169
void ShutDown ( ESContext *esContext )
171
UserData *userData = esContext->userData;
173
if ( userData->vertices != NULL )
175
free ( userData->vertices );
178
if ( userData->indices != NULL )
180
free ( userData->indices );
183
// Delete program object
184
glDeleteProgram ( userData->programObject );
189
int main ( int argc, char *argv[] )
194
esInitContext ( &esContext );
195
esContext.userData = &userData;
197
esCreateWindow ( &esContext, "Simple Texture 2D", 320, 240, ES_WINDOW_RGB );
199
if ( !Init ( &esContext ) )
202
esRegisterDrawFunc ( &esContext, Draw );
203
esRegisterUpdateFunc ( &esContext, Update );
205
esMainLoop ( &esContext );
207
ShutDown ( &esContext );