Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Side by Side Diff: third_party/gles2_book/Chapter_9/Simple_Texture2D/Simple_Texture2D.c

Issue 543063: Added simple texture demo.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 10 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // 1 //
2 // Book: OpenGL(R) ES 2.0 Programming Guide 2 // Book: OpenGL(R) ES 2.0 Programming Guide
3 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner 3 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
4 // ISBN-10: 0321502795 4 // ISBN-10: 0321502795
5 // ISBN-13: 9780321502797 5 // ISBN-13: 9780321502797
6 // Publisher: Addison-Wesley Professional 6 // Publisher: Addison-Wesley Professional
7 // URLs: http://safari.informit.com/9780321563835 7 // URLs: http://safari.informit.com/9780321563835
8 // http://www.opengles-book.com 8 // http://www.opengles-book.com
9 // 9 //
10 10
11 // Simple_Texture2D.c 11 // Simple_Texture2D.c
12 // 12 //
13 // This is a simple example that draws a quad with a 2D 13 // This is a simple example that draws a quad with a 2D
14 // texture image. The purpose of this example is to demonstrate 14 // texture image. The purpose of this example is to demonstrate
15 // the basics of 2D texturing 15 // the basics of 2D texturing
16 // 16 //
17 #include <stdlib.h> 17 #include <stdlib.h>
18 #include "esUtil.h" 18 #include "Simple_Texture2D.h"
19
20 typedef struct
21 {
22 // Handle to a program object
23 GLuint programObject;
24
25 // Attribute locations
26 GLint positionLoc;
27 GLint texCoordLoc;
28
29 // Sampler location
30 GLint samplerLoc;
31
32 // Texture handle
33 GLuint textureId;
34
35 } UserData;
36 19
37 /// 20 ///
38 // Create a simple 2x2 texture image with four different colors 21 // Create a simple 2x2 texture image with four different colors
39 // 22 //
40 GLuint CreateSimpleTexture2D( ) 23 static GLuint CreateSimpleTexture2D( )
41 { 24 {
42 // Texture object handle 25 // Texture object handle
43 GLuint textureId; 26 GLuint textureId;
44 27
45 // 2x2 Image, 3 bytes per pixel (R, G, B) 28 // 2x2 Image, 3 bytes per pixel (R, G, B)
46 GLubyte pixels[4 * 3] = 29 GLubyte pixels[4 * 3] =
47 { 30 {
48 255, 0, 0, // Red 31 255, 0, 0, // Red
49 0, 255, 0, // Green 32 0, 255, 0, // Green
50 0, 0, 255, // Blue 33 0, 0, 255, // Blue
(...skipping 17 matching lines...) Expand all
68 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); 51 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
69 52
70 return textureId; 53 return textureId;
71 54
72 } 55 }
73 56
74 57
75 /// 58 ///
76 // Initialize the shader and program object 59 // Initialize the shader and program object
77 // 60 //
78 int Init ( ESContext *esContext ) 61 int stInit ( ESContext *esContext )
79 { 62 {
80 UserData *userData = esContext->userData; 63 STUserData *userData = esContext->userData;
81 GLbyte vShaderStr[] = 64 GLbyte vShaderStr[] =
82 "attribute vec4 a_position; \n" 65 "attribute vec4 a_position; \n"
83 "attribute vec2 a_texCoord; \n" 66 "attribute vec2 a_texCoord; \n"
84 "varying vec2 v_texCoord; \n" 67 "varying vec2 v_texCoord; \n"
85 "void main() \n" 68 "void main() \n"
86 "{ \n" 69 "{ \n"
87 " gl_Position = a_position; \n" 70 " gl_Position = a_position; \n"
88 " v_texCoord = a_texCoord; \n" 71 " v_texCoord = a_texCoord; \n"
89 "} \n"; 72 "} \n";
90 73
91 GLbyte fShaderStr[] = 74 GLbyte fShaderStr[] =
92 "precision mediump float; \n" 75 "precision mediump float; \n"
93 "varying vec2 v_texCoord; \n" 76 "varying vec2 v_texCoord; \n"
94 "uniform sampler2D s_texture; \n" 77 "uniform sampler2D s_texture; \n"
95 "void main() \n" 78 "void main() \n"
96 "{ \n" 79 "{ \n"
97 " gl_FragColor = texture2D( s_texture, v_texCoord );\n" 80 " gl_FragColor = texture2D( s_texture, v_texCoord );\n"
98 "} \n"; 81 "} \n";
99 82
83 GLfloat vVertices[] = { -0.5f, 0.5f, 0.0f, // Position 0
84 0.0f, 0.0f, // TexCoord 0
85 -0.5f, -0.5f, 0.0f, // Position 1
86 0.0f, 1.0f, // TexCoord 1
87 0.5f, -0.5f, 0.0f, // Position 2
88 1.0f, 1.0f, // TexCoord 2
89 0.5f, 0.5f, 0.0f, // Position 3
90 1.0f, 0.0f // TexCoord 3
91 };
92 GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
93
100 // Load the shaders and get a linked program object 94 // Load the shaders and get a linked program object
101 userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); 95 userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );
102 96
103 // Get the attribute locations 97 // Get the attribute locations
104 userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_pos ition" ); 98 userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_pos ition" );
105 userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_tex Coord" ); 99 userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_tex Coord" );
106 100
107 // Get the sampler location 101 // Get the sampler location
108 userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_tex ture" ); 102 userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_tex ture" );
109 103
110 // Load the texture 104 // Load the texture
111 userData->textureId = CreateSimpleTexture2D (); 105 userData->textureId = CreateSimpleTexture2D ();
112 106
107 // Load vertex data
108 glGenBuffers ( 2, userData->vboIds );
109 glBindBuffer ( GL_ARRAY_BUFFER, userData->vboIds[0] );
110 glBufferData ( GL_ARRAY_BUFFER, sizeof(vVertices),
111 vVertices, GL_STATIC_DRAW);
112 glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, userData->vboIds[1] );
113 glBufferData ( GL_ELEMENT_ARRAY_BUFFER, sizeof(indices),
114 indices, GL_STATIC_DRAW );
115
113 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); 116 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
114 return TRUE; 117 return TRUE;
115 } 118 }
116 119
117 /// 120 ///
118 // Draw a triangle using the shader pair created in Init() 121 // Draw a triangle using the shader pair created in Init()
119 // 122 //
120 void Draw ( ESContext *esContext ) 123 #define VTX_POS_SIZE 3
124 #define VTX_TEX_SIZE 2
125 #define VTX_STRIDE (5 * sizeof(GLfloat))
126 void stDraw ( ESContext *esContext )
121 { 127 {
122 UserData *userData = esContext->userData; 128 STUserData *userData = esContext->userData;
123 GLfloat vVertices[] = { -0.5f, 0.5f, 0.0f, // Position 0 129 GLuint offset = 0;
124 0.0f, 0.0f, // TexCoord 0
125 -0.5f, -0.5f, 0.0f, // Position 1
126 0.0f, 1.0f, // TexCoord 1
127 0.5f, -0.5f, 0.0f, // Position 2
128 1.0f, 1.0f, // TexCoord 2
129 0.5f, 0.5f, 0.0f, // Position 3
130 1.0f, 0.0f // TexCoord 3
131 };
132 GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
133 130
134 // Set the viewport 131 // Set the viewport
135 glViewport ( 0, 0, esContext->width, esContext->height ); 132 glViewport ( 0, 0, esContext->width, esContext->height );
136 133
137 // Clear the color buffer 134 // Clear the color buffer
138 glClear ( GL_COLOR_BUFFER_BIT ); 135 glClear ( GL_COLOR_BUFFER_BIT );
139 136
140 // Use the program object 137 // Use the program object
141 glUseProgram ( userData->programObject ); 138 glUseProgram ( userData->programObject );
142 139
143 // Load the vertex position 140 // Load the vertex position
144 glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, 141 glVertexAttribPointer ( userData->positionLoc, VTX_POS_SIZE, GL_FLOAT,
145 GL_FALSE, 5 * sizeof(GLfloat), vVertices ); 142 GL_FALSE, VTX_STRIDE, (GLvoid*) offset );
146 // Load the texture coordinate 143 // Load the texture coordinate
147 glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT, 144 offset += VTX_POS_SIZE * sizeof(GLfloat);
148 GL_FALSE, 5 * sizeof(GLfloat), &vVertices[3] ); 145 glVertexAttribPointer ( userData->texCoordLoc, VTX_TEX_SIZE, GL_FLOAT,
146 GL_FALSE, VTX_STRIDE, (GLvoid*) offset );
149 147
150 glEnableVertexAttribArray ( userData->positionLoc ); 148 glEnableVertexAttribArray ( userData->positionLoc );
151 glEnableVertexAttribArray ( userData->texCoordLoc ); 149 glEnableVertexAttribArray ( userData->texCoordLoc );
152 150
153 // Bind the texture 151 // Bind the texture
154 glActiveTexture ( GL_TEXTURE0 ); 152 glActiveTexture ( GL_TEXTURE0 );
155 glBindTexture ( GL_TEXTURE_2D, userData->textureId ); 153 glBindTexture ( GL_TEXTURE_2D, userData->textureId );
156 154
157 // Set the sampler texture unit to 0 155 // Set the sampler texture unit to 0
158 glUniform1i ( userData->samplerLoc, 0 ); 156 glUniform1i ( userData->samplerLoc, 0 );
159 157
160 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices ); 158 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 );
161
162 eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
163 } 159 }
164 160
165 /// 161 ///
166 // Cleanup 162 // Cleanup
167 // 163 //
168 void ShutDown ( ESContext *esContext ) 164 void stShutDown ( ESContext *esContext )
169 { 165 {
170 UserData *userData = esContext->userData; 166 STUserData *userData = esContext->userData;
171 167
172 // Delete texture object 168 // Delete texture object
173 glDeleteTextures ( 1, &userData->textureId ); 169 glDeleteTextures ( 1, &userData->textureId );
174 170
171 // Delete VBOs
172 glDeleteBuffers ( 2, userData->vboIds );
173
175 // Delete program object 174 // Delete program object
176 glDeleteProgram ( userData->programObject ); 175 glDeleteProgram ( userData->programObject );
177 } 176 }
178
179
180 int main ( int argc, char *argv[] )
181 {
182 ESContext esContext;
183 UserData userData;
184
185 esInitContext ( &esContext );
186 esContext.userData = &userData;
187
188 esCreateWindow ( &esContext, "Simple Texture 2D", 320, 240, ES_WINDOW_RGB );
189
190 if ( !Init ( &esContext ) )
191 return 0;
192
193 esRegisterDrawFunc ( &esContext, Draw );
194
195 esMainLoop ( &esContext );
196
197 ShutDown ( &esContext );
198 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698