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_TextureCubemap.c
13
// This is a simple example that draws a sphere with a cubemap image applied.
20
// Handle to a program object
23
// Attribute locations
42
// Create a simple cubemap with a 1x1 face with a different
43
// color for each face
44
GLuint CreateSimpleTextureCubemap( )
48
GLubyte cubePixels[6][3] =
64
// Generate a texture object
65
glGenTextures ( 1, &textureId );
67
// Bind the texture object
68
glBindTexture ( GL_TEXTURE_CUBE_MAP, textureId );
70
// Load the cube face - Positive X
71
glTexImage2D ( GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, 1, 1, 0,
72
GL_RGB, GL_UNSIGNED_BYTE, &cubePixels[0] );
74
// Load the cube face - Negative X
75
glTexImage2D ( GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, 1, 1, 0,
76
GL_RGB, GL_UNSIGNED_BYTE, &cubePixels[1] );
78
// Load the cube face - Positive Y
79
glTexImage2D ( GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB, 1, 1, 0,
80
GL_RGB, GL_UNSIGNED_BYTE, &cubePixels[2] );
82
// Load the cube face - Negative Y
83
glTexImage2D ( GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGB, 1, 1, 0,
84
GL_RGB, GL_UNSIGNED_BYTE, &cubePixels[3] );
86
// Load the cube face - Positive Z
87
glTexImage2D ( GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB, 1, 1, 0,
88
GL_RGB, GL_UNSIGNED_BYTE, &cubePixels[4] );
90
// Load the cube face - Negative Z
91
glTexImage2D ( GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGB, 1, 1, 0,
92
GL_RGB, GL_UNSIGNED_BYTE, &cubePixels[5] );
94
// Set the filtering mode
95
glTexParameteri ( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
96
glTexParameteri ( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
104
// Initialize the shader and program object
106
int Init ( ESContext *esContext )
108
//esContext->userData = malloc(sizeof(UserData));
109
UserData *userData = esContext->userData;
110
GLbyte vShaderStr[] =
111
"attribute vec4 a_position; \n"
112
"attribute vec3 a_normal; \n"
113
"varying vec3 v_normal; \n"
116
" gl_Position = a_position; \n"
117
" v_normal = a_normal; \n"
120
GLbyte fShaderStr[] =
121
"precision mediump float; \n"
122
"varying vec3 v_normal; \n"
123
"uniform samplerCube s_texture; \n"
126
" gl_FragColor = textureCube( s_texture, v_normal );\n"
129
// Load the shaders and get a linked program object
130
userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );
132
// Get the attribute locations
133
userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" );
134
userData->normalLoc = glGetAttribLocation ( userData->programObject, "a_normal" );
136
// Get the sampler locations
137
userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" );
140
userData->textureId = CreateSimpleTextureCubemap ();
142
// Generate the vertex data
143
userData->numIndices = esGenSphere ( 20, 0.75f, &userData->vertices, &userData->normals,
144
NULL, &userData->indices );
146
glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
151
// Draw a triangle using the shader pair created in Init()
153
void Draw ( ESContext *esContext )
155
UserData *userData = esContext->userData;
158
glViewport ( 0, 0, esContext->width, esContext->height );
160
// Clear the color buffer
161
glClear ( GL_COLOR_BUFFER_BIT );
163
glCullFace ( GL_BACK );
164
glEnable ( GL_CULL_FACE );
166
// Use the program object
167
glUseProgram ( userData->programObject );
169
// Load the vertex position
170
glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT,
171
GL_FALSE, 0, userData->vertices );
173
glVertexAttribPointer ( userData->normalLoc, 3, GL_FLOAT,
174
GL_FALSE, 0, userData->normals );
176
glEnableVertexAttribArray ( userData->positionLoc );
177
glEnableVertexAttribArray ( userData->normalLoc );
180
glActiveTexture ( GL_TEXTURE0 );
181
glBindTexture ( GL_TEXTURE_CUBE_MAP, userData->textureId );
183
// Set the sampler texture unit to 0
184
glUniform1i ( userData->samplerLoc, 0 );
186
glDrawElements ( GL_TRIANGLES, userData->numIndices,
187
GL_UNSIGNED_INT, userData->indices );
193
void ShutDown ( ESContext *esContext )
195
UserData *userData = esContext->userData;
197
// Delete texture object
198
glDeleteTextures ( 1, &userData->textureId );
200
// Delete program object
201
glDeleteProgram ( userData->programObject );
203
free ( userData->vertices );
204
free ( userData->normals );
206
//free ( esContext->userData);
209
int main ( int argc, char *argv[] )
214
esInitContext ( &esContext );
215
esContext.userData = &userData;
217
esCreateWindow ( &esContext, "Simple Texture Cubemap", 320, 240, ES_WINDOW_RGB );
219
if ( !Init ( &esContext ) )
222
esRegisterDrawFunc ( &esContext, Draw );
224
esMainLoop ( &esContext );
226
ShutDown ( &esContext );