2 * Test texturing with GL shading language.
4 * Copyright (C) 2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 #include "shaderutil.h"
36 static const char *Demo
= "texdemo1";
38 static const char *ReflectVertFile
= "reflect.vert.txt";
39 static const char *CubeFragFile
= "cubemap.frag.txt";
41 static const char *SimpleVertFile
= "simple.vert.txt";
42 static const char *SimpleTexFragFile
= "shadowtex.frag.txt";
44 static const char *GroundImage
= "../images/tile.rgb";
46 static GLuint Program1
, Program2
;
48 static GLfloat TexXrot
= 0, TexYrot
= 0;
49 static GLfloat Xrot
= 20.0, Yrot
= 20.0, Zrot
= 0.0;
50 static GLfloat EyeDist
= 10;
51 static GLboolean Anim
= GL_TRUE
;
54 static struct uniform_info ReflectUniforms
[] = {
55 { "cubeTex", 1, GL_INT
, { 0, 0, 0, 0 }, -1 },
56 { "lightPos", 3, GL_FLOAT
, { 10, 10, 20, 0 }, -1 },
60 static struct uniform_info SimpleUniforms
[] = {
61 { "tex2d", 1, GL_INT
, { 1, 0, 0, 0 }, -1 },
62 { "lightPos", 3, GL_FLOAT
, { 10, 10, 20, 0 }, -1 },
68 DrawGround(GLfloat size
)
71 glRotatef(90, 1, 0, 0);
74 glTexCoord2f(-2, -2); glVertex2f(-size
, -size
);
75 glTexCoord2f( 2, -2); glVertex2f( size
, -size
);
76 glTexCoord2f( 2, 2); glVertex2f( size
, size
);
77 glTexCoord2f(-2, 2); glVertex2f(-size
, size
);
86 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
88 glEnable(GL_TEXTURE_2D
);
90 glPushMatrix(); /* modelview matrix */
91 glTranslatef(0.0, 0.0, -EyeDist
);
92 glRotatef(Xrot
, 1, 0, 0);
93 glRotatef(Yrot
, 0, 1, 0);
94 glRotatef(Zrot
, 0, 0, 1);
96 /* sphere w/ reflection map */
98 glTranslatef(0, 1, 0);
99 glUseProgram_func(Program1
);
101 /* setup texture matrix */
102 glActiveTexture(GL_TEXTURE0
);
103 glMatrixMode(GL_TEXTURE
);
105 glRotatef(-TexYrot
, 0, 1, 0);
106 glRotatef(-TexXrot
, 1, 0, 0);
108 glEnable(GL_TEXTURE_GEN_S
);
109 glEnable(GL_TEXTURE_GEN_T
);
110 glEnable(GL_TEXTURE_GEN_R
);
111 glutSolidSphere(2.0, 20, 20);
113 glLoadIdentity(); /* texture matrix */
114 glMatrixMode(GL_MODELVIEW
);
118 glUseProgram_func(Program2
);
119 glTranslatef(0, -1.0, 0);
131 GLfloat t
= 0.05 * glutGet(GLUT_ELAPSED_TIME
);
138 key(unsigned char k
, int x
, int y
)
169 specialkey(int key
, int x
, int y
)
192 /* new window size or exposure */
194 Reshape(int width
, int height
)
196 GLfloat ar
= (float) width
/ (float) height
;
197 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
198 glMatrixMode(GL_PROJECTION
);
200 glFrustum(-2.0*ar
, 2.0*ar
, -2.0, 2.0, 4.0, 100.0);
201 glMatrixMode(GL_MODELVIEW
);
209 #define CUBE_TEX_SIZE 64
210 GLubyte image
[CUBE_TEX_SIZE
][CUBE_TEX_SIZE
][3];
211 static const GLubyte colors
[6][3] = {
212 { 255, 0, 0 }, /* face 0 - red */
213 { 0, 255, 255 }, /* face 1 - cyan */
214 { 0, 255, 0 }, /* face 2 - green */
215 { 255, 0, 255 }, /* face 3 - purple */
216 { 0, 0, 255 }, /* face 4 - blue */
217 { 255, 255, 0 } /* face 5 - yellow */
219 static const GLenum targets
[6] = {
220 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
221 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
222 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
223 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
224 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
225 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
230 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
232 /* make colored checkerboard cube faces */
233 for (f
= 0; f
< 6; f
++) {
234 for (i
= 0; i
< CUBE_TEX_SIZE
; i
++) {
235 for (j
= 0; j
< CUBE_TEX_SIZE
; j
++) {
236 if ((i
/4 + j
/4) & 1) {
237 image
[i
][j
][0] = colors
[f
][0];
238 image
[i
][j
][1] = colors
[f
][1];
239 image
[i
][j
][2] = colors
[f
][2];
242 image
[i
][j
][0] = 255;
243 image
[i
][j
][1] = 255;
244 image
[i
][j
][2] = 255;
249 glTexImage2D(targets
[f
], 0, GL_RGB
, CUBE_TEX_SIZE
, CUBE_TEX_SIZE
, 0,
250 GL_RGB
, GL_UNSIGNED_BYTE
, image
);
256 LoadFace(GLenum target
, const char *filename
,
257 GLboolean flipTB
, GLboolean flipLR
)
261 GLubyte
*img
= LoadRGBImage(filename
, &w
, &h
, &format
);
263 printf("Error: couldn't load texture image %s\n", filename
);
266 assert(format
== GL_RGB
);
268 /* <sigh> the way the texture cube mapping works, we have to flip
269 * images to make things look right.
272 const int stride
= 3 * w
;
273 GLubyte temp
[3*1024];
275 for (i
= 0; i
< h
/ 2; i
++) {
276 memcpy(temp
, img
+ i
* stride
, stride
);
277 memcpy(img
+ i
* stride
, img
+ (h
- i
- 1) * stride
, stride
);
278 memcpy(img
+ (h
- i
- 1) * stride
, temp
, stride
);
282 const int stride
= 3 * w
;
286 for (i
= 0; i
< h
; i
++) {
287 row
= img
+ i
* stride
;
288 for (j
= 0; j
< w
/ 2; j
++) {
290 temp
[0] = row
[j
*3+0];
291 temp
[1] = row
[j
*3+1];
292 temp
[2] = row
[j
*3+2];
293 row
[j
*3+0] = row
[k
*3+0];
294 row
[j
*3+1] = row
[k
*3+1];
295 row
[j
*3+2] = row
[k
*3+2];
296 row
[k
*3+0] = temp
[0];
297 row
[k
*3+1] = temp
[1];
298 row
[k
*3+2] = temp
[2];
303 gluBuild2DMipmaps(target
, GL_RGB
, w
, h
, format
, GL_UNSIGNED_BYTE
, img
);
311 LoadFace(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
, "right.rgb", GL_TRUE
, GL_FALSE
);
312 LoadFace(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
, "left.rgb", GL_TRUE
, GL_FALSE
);
313 LoadFace(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
, "top.rgb", GL_FALSE
, GL_TRUE
);
314 LoadFace(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
, "bottom.rgb", GL_FALSE
, GL_TRUE
);
315 LoadFace(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
, "front.rgb", GL_TRUE
, GL_FALSE
);
316 LoadFace(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
, "back.rgb", GL_TRUE
, GL_FALSE
);
321 InitTextures(GLboolean useImageFiles
)
328 glActiveTexture(GL_TEXTURE0
);
329 glBindTexture(GL_TEXTURE_CUBE_MAP
, 1);
338 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_MIN_FILTER
, filter
);
339 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_MAG_FILTER
, filter
);
340 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
341 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
347 GLint imgWidth
, imgHeight
;
349 GLubyte
*image
= NULL
;
351 image
= LoadRGBImage(GroundImage
, &imgWidth
, &imgHeight
, &imgFormat
);
353 printf("Couldn't read %s\n", GroundImage
);
357 glActiveTexture(GL_TEXTURE1
);
358 glBindTexture(GL_TEXTURE_2D
, 2);
359 gluBuild2DMipmaps(GL_TEXTURE_2D
, 3, imgWidth
, imgHeight
,
360 imgFormat
, GL_UNSIGNED_BYTE
, image
);
363 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
364 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
365 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
366 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
372 CreateProgram(const char *vertProgFile
, const char *fragProgFile
,
373 struct uniform_info
*uniforms
)
375 GLuint fragShader
, vertShader
, program
;
377 vertShader
= CompileShaderFile(GL_VERTEX_SHADER
, vertProgFile
);
378 fragShader
= CompileShaderFile(GL_FRAGMENT_SHADER
, fragProgFile
);
379 program
= LinkShaders(vertShader
, fragShader
);
381 glUseProgram_func(program
);
383 InitUniforms(program
, uniforms
);
392 Program1
= CreateProgram(ReflectVertFile
, CubeFragFile
, ReflectUniforms
);
393 Program2
= CreateProgram(SimpleVertFile
, SimpleTexFragFile
, SimpleUniforms
);
398 Init(GLboolean useImageFiles
)
400 const char *version
= (const char *) glGetString(GL_VERSION
);
402 if (version
[0] != '2' || version
[1] != '.') {
403 printf("Warning: this program expects OpenGL 2.0\n");
406 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
410 InitTextures(useImageFiles
);
413 glEnable(GL_DEPTH_TEST
);
415 glClearColor(.6, .6, .9, 0);
416 glColor3f(1.0, 1.0, 1.0);
421 main(int argc
, char *argv
[])
423 glutInit(&argc
, argv
);
424 glutInitWindowSize(500, 400);
425 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
426 glutCreateWindow(Demo
);
427 glutReshapeFunc(Reshape
);
428 glutKeyboardFunc(key
);
429 glutSpecialFunc(specialkey
);
430 glutDisplayFunc(draw
);
433 if (argc
> 1 && strcmp(argv
[1] , "-i") == 0)