~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/glbook/Chapter_8/Simple_VertexShader/Simple_VertexShader.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Book:      OpenGL(R) ES 2.0 Programming Guide
 
3
// Authors:   Aaftab Munshi, Dan Ginsburg, Dave Shreiner
 
4
// ISBN-10:   0321502795
 
5
// ISBN-13:   9780321502797
 
6
// Publisher: Addison-Wesley Professional
 
7
// URLs:      http://safari.informit.com/9780321563835
 
8
//            http://www.opengles-book.com
 
9
//
 
10
 
 
11
// Simple_VertexShader.c
 
12
//
 
13
//    This is a simple example that draws a rotating cube in perspective
 
14
//    using a vertex shader to transform the object
 
15
//
 
16
#include <stdlib.h>
 
17
#include "esUtil.h"
 
18
 
 
19
typedef struct
 
20
{
 
21
   // Handle to a program object
 
22
   GLuint programObject;
 
23
 
 
24
   // Attribute locations
 
25
   GLint  positionLoc;
 
26
 
 
27
   // Uniform locations
 
28
   GLint  mvpLoc;
 
29
   
 
30
   // Vertex daata
 
31
   GLfloat  *vertices;
 
32
   GLushort *indices;
 
33
   int       numIndices;
 
34
 
 
35
   // Rotation angle
 
36
   GLfloat   angle;
 
37
 
 
38
   // MVP matrix
 
39
   ESMatrix  mvpMatrix;
 
40
 
 
41
   GLuint vertPosObject, indicesObject;
 
42
} UserData;
 
43
 
 
44
///
 
45
// Initialize the shader and program object
 
46
//
 
47
int Init ( ESContext *esContext )
 
48
{
 
49
   esContext->userData = malloc(sizeof(UserData));
 
50
        
 
51
   UserData *userData = esContext->userData;
 
52
   GLbyte vShaderStr[] =  
 
53
      "uniform mat4 u_mvpMatrix;                   \n"
 
54
      "attribute vec4 a_position;                  \n"
 
55
      "void main()                                 \n"
 
56
      "{                                           \n"
 
57
      "   gl_Position = u_mvpMatrix * a_position;  \n"
 
58
      "}                                           \n";
 
59
   
 
60
   GLbyte fShaderStr[] =  
 
61
      "precision mediump float;                            \n"
 
62
      "void main()                                         \n"
 
63
      "{                                                   \n"
 
64
      "  gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );        \n"
 
65
      "}                                                   \n";
 
66
 
 
67
   // Load the shaders and get a linked program object
 
68
   userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );
 
69
 
 
70
   // Get the attribute locations
 
71
   userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" );
 
72
 
 
73
   // Get the uniform locations
 
74
   userData->mvpLoc = glGetUniformLocation( userData->programObject, "u_mvpMatrix" );
 
75
   
 
76
   // Generate the vertex data
 
77
   userData->numIndices = esGenCube( 1.0, &userData->vertices,
 
78
                                     NULL, NULL, &userData->indices );
 
79
   
 
80
   // Starting rotation angle for the cube
 
81
   userData->angle = 45.0f;
 
82
 
 
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);
 
86
 
 
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);
 
90
 
 
91
   glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
 
92
   return GL_TRUE;
 
93
}
 
94
 
 
95
 
 
96
///
 
97
// Update MVP matrix based on time
 
98
//
 
99
void Update ( ESContext *esContext, float deltaTime )
 
100
{
 
101
   UserData *userData = (UserData*) esContext->userData;
 
102
   ESMatrix perspective;
 
103
   ESMatrix modelview;
 
104
   float    aspect;
 
105
   
 
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;
 
110
 
 
111
   // Compute the window aspect ratio
 
112
   aspect = (GLfloat) esContext->width / (GLfloat) esContext->height;
 
113
   
 
114
   // Generate a perspective matrix with a 60 degree FOV
 
115
   esMatrixLoadIdentity( &perspective );
 
116
   esPerspective( &perspective, 60.0f, aspect, 1.0f, 20.0f );
 
117
 
 
118
   // Generate a model view matrix to rotate/translate the cube
 
119
   esMatrixLoadIdentity( &modelview );
 
120
 
 
121
   // Translate away from the viewer
 
122
   esTranslate( &modelview, 0.0, 0.0, -2.0 );
 
123
 
 
124
   // Rotate the cube
 
125
   esRotate( &modelview, userData->angle, 1.0, 0.0, 1.0 );
 
126
   
 
127
   // Compute the final MVP by multiplying the 
 
128
   // modevleiw and perspective matrices together
 
129
   esMatrixMultiply( &userData->mvpMatrix, &modelview, &perspective );
 
130
}
 
131
 
 
132
///
 
133
// Draw a triangle using the shader pair created in Init()
 
134
//
 
135
void Draw ( ESContext *esContext )
 
136
{
 
137
   UserData *userData = esContext->userData;
 
138
   
 
139
   // Set the viewport
 
140
   glViewport ( 0, 0, esContext->width, esContext->height );
 
141
   
 
142
   
 
143
   // Clear the color buffer
 
144
   glClear ( GL_COLOR_BUFFER_BIT );
 
145
 
 
146
   // Use the program object
 
147
   glUseProgram ( userData->programObject );
 
148
 
 
149
   // Load the vertex position
 
150
   glBindBuffer(GL_ARRAY_BUFFER, userData->vertPosObject);
 
151
   glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, 
 
152
                           GL_FALSE, 0, 0 );
 
153
   glEnableVertexAttribArray ( userData->positionLoc );
 
154
   
 
155
   
 
156
   // Load the index buffer
 
157
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, userData->indicesObject);
 
158
 
 
159
   // Load the MVP matrix
 
160
   glUniformMatrix4fv( userData->mvpLoc, 1, GL_FALSE, (GLfloat*) &userData->mvpMatrix.m[0][0] );
 
161
   
 
162
   // Draw the cube
 
163
   glDrawElements ( GL_TRIANGLES, userData->numIndices, GL_UNSIGNED_SHORT, 0 );
 
164
}
 
165
 
 
166
///
 
167
// Cleanup
 
168
//
 
169
void ShutDown ( ESContext *esContext )
 
170
{
 
171
   UserData *userData = esContext->userData;
 
172
 
 
173
   if ( userData->vertices != NULL )
 
174
   {
 
175
      free ( userData->vertices );
 
176
   }
 
177
 
 
178
   if ( userData->indices != NULL )
 
179
   {
 
180
      free ( userData->indices );
 
181
   }
 
182
 
 
183
   // Delete program object
 
184
   glDeleteProgram ( userData->programObject );
 
185
 
 
186
   free(userData);
 
187
}
 
188
 
 
189
int main ( int argc, char *argv[] )
 
190
{
 
191
   ESContext esContext;
 
192
   UserData  userData;
 
193
 
 
194
   esInitContext ( &esContext );
 
195
   esContext.userData = &userData;
 
196
 
 
197
   esCreateWindow ( &esContext, "Simple Texture 2D", 320, 240, ES_WINDOW_RGB );
 
198
 
 
199
   if ( !Init ( &esContext ) )
 
200
      return 0;
 
201
 
 
202
   esRegisterDrawFunc ( &esContext, Draw );
 
203
   esRegisterUpdateFunc ( &esContext, Update );
 
204
 
 
205
   esMainLoop ( &esContext );
 
206
 
 
207
   ShutDown ( &esContext );
 
208
}
 
209