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

« back to all changes in this revision

Viewing changes to tests/glbook/Chapter_10/MultiTexture/MultiTexture.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
// MultiTexture.c
 
12
//
 
13
//    This is an example that draws a quad with a basemap and
 
14
//    lightmap to demonstrate multitexturing.
 
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
   GLint  texCoordLoc;
 
27
 
 
28
   // Sampler locations
 
29
   GLint baseMapLoc;
 
30
   GLint lightMapLoc;
 
31
 
 
32
   // Texture handle
 
33
   GLuint baseMapTexId;
 
34
   GLuint lightMapTexId;
 
35
 
 
36
   GLuint vertexObject, indexObject;
 
37
 
 
38
} UserData;
 
39
 
 
40
 
 
41
///
 
42
// Load texture from disk
 
43
//
 
44
GLuint LoadTexture ( char *fileName )
 
45
{
 
46
   int width,
 
47
       height;
 
48
   char *buffer = esLoadTGA ( fileName, &width, &height );
 
49
   GLuint texId;
 
50
 
 
51
   if ( buffer == NULL )
 
52
   {
 
53
      esLogMessage ( "Error loading (%s) image.\n", fileName );
 
54
      return 0;
 
55
   }
 
56
 
 
57
   glGenTextures ( 1, &texId );
 
58
   glBindTexture ( GL_TEXTURE_2D, texId );
 
59
 
 
60
   glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer );
 
61
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
 
62
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 
63
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
 
64
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
 
65
 
 
66
   free ( buffer );
 
67
 
 
68
   return texId;
 
69
}
 
70
 
 
71
 
 
72
 
 
73
///
 
74
// Initialize the shader and program object
 
75
//
 
76
int Init ( ESContext *esContext )
 
77
{
 
78
   UserData *userData = esContext->userData;
 
79
   GLbyte vShaderStr[] =  
 
80
      "attribute vec4 a_position;   \n"
 
81
      "attribute vec2 a_texCoord;   \n"
 
82
      "varying vec2 v_texCoord;     \n"
 
83
      "void main()                  \n"
 
84
      "{                            \n"
 
85
      "   gl_Position = a_position; \n"
 
86
      "   v_texCoord = a_texCoord;  \n"
 
87
      "}                            \n";
 
88
   
 
89
   GLbyte fShaderStr[] =  
 
90
      "precision mediump float;                            \n"
 
91
      "varying vec2 v_texCoord;                            \n"
 
92
      "uniform sampler2D s_baseMap;                        \n"
 
93
      "uniform sampler2D s_lightMap;                       \n"
 
94
      "void main()                                         \n"
 
95
      "{                                                   \n"
 
96
      "  vec4 baseColor;                                   \n"
 
97
      "  vec4 lightColor;                                  \n"
 
98
      "                                                    \n"
 
99
      "  baseColor = texture2D( s_baseMap, v_texCoord );   \n"
 
100
      "  lightColor = texture2D( s_lightMap, v_texCoord ); \n"
 
101
      "  gl_FragColor = baseColor * (lightColor + 0.25);   \n"
 
102
      "}                                                   \n";
 
103
 
 
104
   // Load the shaders and get a linked program object
 
105
   userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );
 
106
 
 
107
   // Get the attribute locations
 
108
   userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" );
 
109
   userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_texCoord" );
 
110
   
 
111
   // Get the sampler location
 
112
   userData->baseMapLoc = glGetUniformLocation ( userData->programObject, "s_baseMap" );
 
113
   userData->lightMapLoc = glGetUniformLocation ( userData->programObject, "s_lightMap" );
 
114
 
 
115
   // Load the textures
 
116
   userData->baseMapTexId = LoadTexture ( "basemap.tga" );
 
117
   userData->lightMapTexId = LoadTexture ( "lightmap.tga" );
 
118
 
 
119
   if ( userData->baseMapTexId == 0 || userData->lightMapTexId == 0 )
 
120
      return FALSE;
 
121
 
 
122
   GLfloat vVertices[] = { -0.5,  0.5, 0.0,  // Position 0
 
123
                            0.0,  0.0,       // TexCoord 0
 
124
                           -0.5, -0.5, 0.0,  // Position 1
 
125
                            0.0,  1.0,       // TexCoord 1
 
126
                            0.5, -0.5, 0.0,  // Position 2
 
127
                            1.0,  1.0,       // TexCoord 2
 
128
                            0.5,  0.5, 0.0,  // Position 3
 
129
                            1.0,  0.0        // TexCoord 3
 
130
                         };
 
131
   GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
 
132
 
 
133
   glGenBuffers(1, &userData->vertexObject);
 
134
   glBindBuffer ( GL_ARRAY_BUFFER, userData->vertexObject );
 
135
   glBufferData ( GL_ARRAY_BUFFER, 5 * 4 * 4, vVertices, GL_STATIC_DRAW );
 
136
 
 
137
   glGenBuffers(1, &userData->indexObject);
 
138
   glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, userData->indexObject );
 
139
   glBufferData ( GL_ELEMENT_ARRAY_BUFFER, 6 * 2, indices, GL_STATIC_DRAW );
 
140
 
 
141
   glClearColor ( 0.0, 0.0, 0.0, 1.0 );
 
142
 
 
143
   return TRUE;
 
144
}
 
145
 
 
146
///
 
147
// Draw a triangle using the shader pair created in Init()
 
148
//
 
149
void Draw ( ESContext *esContext )
 
150
{
 
151
   UserData *userData = esContext->userData;
 
152
 
 
153
   // Set the viewport
 
154
   glViewport ( 0, 0, esContext->width, esContext->height );
 
155
   
 
156
   // Clear the color buffer
 
157
   glClear ( GL_COLOR_BUFFER_BIT );
 
158
 
 
159
   // Use the program object
 
160
   glUseProgram ( userData->programObject );
 
161
 
 
162
   // Load the vertex position
 
163
   glBindBuffer ( GL_ARRAY_BUFFER, userData->vertexObject );
 
164
   glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, 
 
165
                           GL_FALSE, 5 * sizeof(GLfloat), 0 );
 
166
   // Load the texture coordinate
 
167
   glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT,
 
168
                           GL_FALSE, 5 * sizeof(GLfloat), 3 * sizeof(GLfloat) );
 
169
 
 
170
   glEnableVertexAttribArray ( userData->positionLoc );
 
171
   glEnableVertexAttribArray ( userData->texCoordLoc );
 
172
 
 
173
   // Bind the base map
 
174
   glActiveTexture ( GL_TEXTURE0 );
 
175
   glBindTexture ( GL_TEXTURE_2D, userData->baseMapTexId );
 
176
 
 
177
   // Set the base map sampler to texture unit to 0
 
178
   glUniform1i ( userData->baseMapLoc, 0 );
 
179
 
 
180
   // Bind the light map
 
181
   glActiveTexture ( GL_TEXTURE1 );
 
182
   glBindTexture ( GL_TEXTURE_2D, userData->lightMapTexId );
 
183
   
 
184
   // Set the light map sampler to texture unit 1
 
185
   glUniform1i ( userData->lightMapLoc, 1 );
 
186
 
 
187
   glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, userData->indexObject );
 
188
   glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 );
 
189
 
 
190
   eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
 
191
}
 
192
 
 
193
///
 
194
// Cleanup
 
195
//
 
196
void ShutDown ( ESContext *esContext )
 
197
{
 
198
   UserData *userData = esContext->userData;
 
199
 
 
200
   // Delete texture object
 
201
   glDeleteTextures ( 1, &userData->baseMapTexId );
 
202
   glDeleteTextures ( 1, &userData->lightMapTexId );
 
203
 
 
204
   // Delete program object
 
205
   glDeleteProgram ( userData->programObject );
 
206
}
 
207
 
 
208
 
 
209
int main ( int argc, char *argv[] )
 
210
{
 
211
   ESContext esContext;
 
212
   UserData  userData;
 
213
 
 
214
   esInitContext ( &esContext );
 
215
   esContext.userData = &userData;
 
216
 
 
217
   esCreateWindow ( &esContext, "MultiTexture", 320, 240, ES_WINDOW_RGB );
 
218
   
 
219
   if ( !Init ( &esContext ) )
 
220
      return 0;
 
221
 
 
222
   esRegisterDrawFunc ( &esContext, Draw );
 
223
   
 
224
   esMainLoop ( &esContext );
 
225
 
 
226
   ShutDown ( &esContext );
 
227
}