~ubuntu-branches/ubuntu/saucy/emscripten/saucy-proposed

« back to all changes in this revision

Viewing changes to tests/glbook/Chapter_9/TextureWrap/TextureWrap_orig.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
// TextureWrap.c
 
12
//
 
13
//    This is an example that demonstrates the three texture
 
14
//    wrap modes available on 2D textures
 
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 location
 
29
   GLint samplerLoc;
 
30
 
 
31
   // Offset location
 
32
   GLint offsetLoc;
 
33
 
 
34
   // Texture handle
 
35
   GLuint textureId;
 
36
 
 
37
} UserData;
 
38
 
 
39
///
 
40
//  Generate an RGB8 checkerboard image
 
41
//
 
42
GLubyte* GenCheckImage( int width, int height, int checkSize )
 
43
{
 
44
   int x,
 
45
       y;
 
46
   GLubyte *pixels = malloc( width * height * 3 );
 
47
   
 
48
   if ( pixels == NULL )
 
49
      return NULL;
 
50
 
 
51
   for ( y = 0; y < height; y++ )
 
52
      for ( x = 0; x < width; x++ )
 
53
      {
 
54
         GLubyte rColor = 0;
 
55
         GLubyte bColor = 0;
 
56
 
 
57
         if ( ( x / checkSize ) % 2 == 0 )
 
58
         {
 
59
            rColor = 255 * ( ( y / checkSize ) % 2 );
 
60
            bColor = 255 * ( 1 - ( ( y / checkSize ) % 2 ) );
 
61
         }
 
62
         else
 
63
         {
 
64
            bColor = 255 * ( ( y / checkSize ) % 2 );
 
65
            rColor = 255 * ( 1 - ( ( y / checkSize ) % 2 ) );
 
66
         }
 
67
 
 
68
         pixels[(y * height + x) * 3] = rColor;
 
69
         pixels[(y * height + x) * 3 + 1] = 0;
 
70
         pixels[(y * height + x) * 3 + 2] = bColor; 
 
71
      } 
 
72
 
 
73
   return pixels;
 
74
}
 
75
 
 
76
///
 
77
// Create a mipmapped 2D texture image 
 
78
//
 
79
GLuint CreateTexture2D( )
 
80
{
 
81
   // Texture object handle
 
82
   GLuint textureId;
 
83
   int    width = 256,
 
84
          height = 256;
 
85
   GLubyte *pixels;
 
86
      
 
87
   pixels = GenCheckImage( width, height, 64 );
 
88
   if ( pixels == NULL )
 
89
      return 0;
 
90
 
 
91
   // Generate a texture object
 
92
   glGenTextures ( 1, &textureId );
 
93
 
 
94
   // Bind the texture object
 
95
   glBindTexture ( GL_TEXTURE_2D, textureId );
 
96
 
 
97
   // Load mipmap level 0
 
98
   glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, width, height, 
 
99
                  0, GL_RGB, GL_UNSIGNED_BYTE, pixels );
 
100
   
 
101
   // Set the filtering mode
 
102
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
 
103
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 
104
 
 
105
   return textureId;
 
106
 
 
107
}
 
108
 
 
109
 
 
110
///
 
111
// Initialize the shader and program object
 
112
//
 
113
int Init ( ESContext *esContext )
 
114
{
 
115
   esContext->userData = malloc(sizeof(UserData));
 
116
        
 
117
   UserData *userData = esContext->userData;
 
118
   GLbyte vShaderStr[] =
 
119
      "uniform float u_offset;      \n"
 
120
      "attribute vec4 a_position;   \n"
 
121
      "attribute vec2 a_texCoord;   \n"
 
122
      "varying vec2 v_texCoord;     \n"
 
123
      "void main()                  \n"
 
124
      "{                            \n"
 
125
      "   gl_Position = a_position; \n"
 
126
      "   gl_Position.x += u_offset;\n"
 
127
      "   v_texCoord = a_texCoord;  \n"
 
128
      "}                            \n";
 
129
   
 
130
   GLbyte fShaderStr[] =  
 
131
      "precision mediump float;                            \n"
 
132
      "varying vec2 v_texCoord;                            \n"
 
133
      "uniform sampler2D s_texture;                        \n"
 
134
      "void main()                                         \n"
 
135
      "{                                                   \n"
 
136
      "  gl_FragColor = texture2D( s_texture, v_texCoord );\n"
 
137
      "}                                                   \n";
 
138
 
 
139
   // Load the shaders and get a linked program object
 
140
   userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );
 
141
 
 
142
   // Get the attribute locations
 
143
   userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" );
 
144
   userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_texCoord" );
 
145
   
 
146
   // Get the sampler location
 
147
   userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" );
 
148
 
 
149
   // Get the offset location
 
150
   userData->offsetLoc = glGetUniformLocation( userData->programObject, "u_offset" );
 
151
 
 
152
   // Load the texture
 
153
   userData->textureId = CreateTexture2D ();
 
154
 
 
155
   glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
 
156
   return GL_TRUE;
 
157
}
 
158
 
 
159
///
 
160
// Draw a triangle using the shader pair created in Init()
 
161
//
 
162
void Draw ( ESContext *esContext )
 
163
{
 
164
   UserData *userData = esContext->userData;
 
165
   GLfloat vVertices[] = { -0.3f,  0.3f, 0.0f, 1.0f,  // Position 0
 
166
                           -1.0f,  -1.0f,              // TexCoord 0 
 
167
                           -0.3f, -0.3f, 0.0f, 1.0f, // Position 1
 
168
                           -1.0f,  2.0f,              // TexCoord 1
 
169
                            0.3f, -0.3f, 0.0f, 1.0f, // Position 2
 
170
                            2.0f,  2.0f,              // TexCoord 2
 
171
                            0.3f,  0.3f, 0.0f, 1.0f,  // Position 3
 
172
                            2.0f,  -1.0f               // TexCoord 3
 
173
                         };
 
174
   GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
 
175
      
 
176
   // Set the viewport
 
177
   glViewport ( 0, 0, esContext->width, esContext->height );
 
178
   
 
179
   // Clear the color buffer
 
180
   glClear ( GL_COLOR_BUFFER_BIT );
 
181
 
 
182
   // Use the program object
 
183
   glUseProgram ( userData->programObject );
 
184
 
 
185
   // Load the vertex position
 
186
   glVertexAttribPointer ( userData->positionLoc, 4, GL_FLOAT, 
 
187
                           GL_FALSE, 6 * sizeof(GLfloat), vVertices );
 
188
   // Load the texture coordinate
 
189
   glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT,
 
190
                           GL_FALSE, 6 * sizeof(GLfloat), &vVertices[4] );
 
191
 
 
192
   glEnableVertexAttribArray ( userData->positionLoc );
 
193
   glEnableVertexAttribArray ( userData->texCoordLoc );
 
194
 
 
195
   // Bind the texture
 
196
   glActiveTexture ( GL_TEXTURE0 );
 
197
   glBindTexture ( GL_TEXTURE_2D, userData->textureId );
 
198
 
 
199
   // Set the sampler texture unit to 0
 
200
   glUniform1i ( userData->samplerLoc, 0 );
 
201
 
 
202
   // Draw quad with repeat wrap mode
 
203
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
 
204
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
 
205
   glUniform1f ( userData->offsetLoc, -0.7f );   
 
206
   glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
 
207
 
 
208
   // Draw quad with clamp to edge wrap mode
 
209
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
 
210
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
 
211
   glUniform1f ( userData->offsetLoc, 0.0f );
 
212
   glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
 
213
 
 
214
   // Draw quad with mirrored repeat
 
215
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT );
 
216
   glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT );
 
217
   glUniform1f ( userData->offsetLoc, 0.7f );
 
218
   glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
 
219
 
 
220
}
 
221
 
 
222
///
 
223
// Cleanup
 
224
//
 
225
void ShutDown ( ESContext *esContext )
 
226
{
 
227
   UserData *userData = esContext->userData;
 
228
 
 
229
   // Delete texture object
 
230
   glDeleteTextures ( 1, &userData->textureId );
 
231
 
 
232
   // Delete program object
 
233
   glDeleteProgram ( userData->programObject );
 
234
 
 
235
   free ( esContext->userData);
 
236
}
 
237
 
 
238
int main ( int argc, char *argv[] )
 
239
{
 
240
   ESContext esContext;
 
241
   UserData  userData;
 
242
 
 
243
   esInitContext ( &esContext );
 
244
   esContext.userData = &userData;
 
245
 
 
246
   esCreateWindow ( &esContext, "MipMap 2D", 320, 240, ES_WINDOW_RGB );
 
247
 
 
248
   if ( !Init ( &esContext ) )
 
249
      return 0;
 
250
 
 
251
   esRegisterDrawFunc ( &esContext, Draw );
 
252
 
 
253
   esMainLoop ( &esContext );
 
254
 
 
255
   ShutDown ( &esContext );
 
256
}
 
257