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.
35 static const char *Demo
= "texdemo1";
37 static const char *ReflectVertFile
= "reflect.vert.txt";
38 static const char *CubeFragFile
= "cubemap.frag.txt";
40 static const char *SimpleVertFile
= "simple.vert.txt";
41 static const char *SimpleTexFragFile
= "shadowtex.frag.txt";
43 static const char *GroundImage
= "../images/tile.rgb";
45 static GLuint Program1
, Program2
;
47 static GLfloat TexXrot
= 0, TexYrot
= 0;
48 static GLfloat Xrot
= 20.0, Yrot
= 20.0, Zrot
= 0.0;
49 static GLfloat EyeDist
= 10;
50 static GLboolean Anim
= GL_TRUE
;
57 GLenum type
; /**< GL_FLOAT or GL_INT */
61 static struct uniform_info ReflectUniforms
[] = {
62 { "cubeTex", 1, -1, GL_INT
, { 0, 0, 0, 0 } },
63 { "lightPos", 3, -1, GL_FLOAT
, { 10, 10, 20, 0 } },
64 { NULL
, 0, 0, 0, { 0, 0, 0, 0 } }
67 static struct uniform_info SimpleUniforms
[] = {
68 { "tex2d", 1, -1, GL_INT
, { 1, 0, 0, 0 } },
69 { "lightPos", 3, -1, GL_FLOAT
, { 10, 10, 20, 0 } },
70 { NULL
, 0, 0, 0, { 0, 0, 0, 0 } }
77 GLenum err
= glGetError();
79 printf("GL Error %s (0x%x) at line %d\n",
80 gluErrorString(err
), (int) err
, line
);
86 DrawGround(GLfloat size
)
89 glRotatef(90, 1, 0, 0);
92 glTexCoord2f(-2, -2); glVertex2f(-size
, -size
);
93 glTexCoord2f( 2, -2); glVertex2f( size
, -size
);
94 glTexCoord2f( 2, 2); glVertex2f( size
, size
);
95 glTexCoord2f(-2, 2); glVertex2f(-size
, size
);
104 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
106 glEnable(GL_TEXTURE_2D
);
108 glPushMatrix(); /* modelview matrix */
109 glTranslatef(0.0, 0.0, -EyeDist
);
110 glRotatef(Xrot
, 1, 0, 0);
111 glRotatef(Yrot
, 0, 1, 0);
112 glRotatef(Zrot
, 0, 0, 1);
114 /* sphere w/ reflection map */
116 glTranslatef(0, 1, 0);
117 glUseProgram_func(Program1
);
119 /* setup texture matrix */
120 glActiveTexture(GL_TEXTURE0
);
121 glMatrixMode(GL_TEXTURE
);
123 glRotatef(-TexYrot
, 0, 1, 0);
124 glRotatef(-TexXrot
, 1, 0, 0);
126 glEnable(GL_TEXTURE_GEN_S
);
127 glEnable(GL_TEXTURE_GEN_T
);
128 glEnable(GL_TEXTURE_GEN_R
);
129 glutSolidSphere(2.0, 20, 20);
131 glLoadIdentity(); /* texture matrix */
132 glMatrixMode(GL_MODELVIEW
);
136 glUseProgram_func(Program2
);
137 glTranslatef(0, -1.0, 0);
149 GLfloat t
= 0.05 * glutGet(GLUT_ELAPSED_TIME
);
156 key(unsigned char k
, int x
, int y
)
187 specialkey(int key
, int x
, int y
)
210 /* new window size or exposure */
212 Reshape(int width
, int height
)
214 GLfloat ar
= (float) width
/ (float) height
;
215 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
216 glMatrixMode(GL_PROJECTION
);
218 glFrustum(-2.0*ar
, 2.0*ar
, -2.0, 2.0, 4.0, 100.0);
219 glMatrixMode(GL_MODELVIEW
);
227 #define CUBE_TEX_SIZE 64
228 GLubyte image
[CUBE_TEX_SIZE
][CUBE_TEX_SIZE
][3];
229 static const GLubyte colors
[6][3] = {
230 { 255, 0, 0 }, /* face 0 - red */
231 { 0, 255, 255 }, /* face 1 - cyan */
232 { 0, 255, 0 }, /* face 2 - green */
233 { 255, 0, 255 }, /* face 3 - purple */
234 { 0, 0, 255 }, /* face 4 - blue */
235 { 255, 255, 0 } /* face 5 - yellow */
237 static const GLenum targets
[6] = {
238 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
239 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
240 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
241 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
242 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
243 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
248 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
250 /* make colored checkerboard cube faces */
251 for (f
= 0; f
< 6; f
++) {
252 for (i
= 0; i
< CUBE_TEX_SIZE
; i
++) {
253 for (j
= 0; j
< CUBE_TEX_SIZE
; j
++) {
254 if ((i
/4 + j
/4) & 1) {
255 image
[i
][j
][0] = colors
[f
][0];
256 image
[i
][j
][1] = colors
[f
][1];
257 image
[i
][j
][2] = colors
[f
][2];
260 image
[i
][j
][0] = 255;
261 image
[i
][j
][1] = 255;
262 image
[i
][j
][2] = 255;
267 glTexImage2D(targets
[f
], 0, GL_RGB
, CUBE_TEX_SIZE
, CUBE_TEX_SIZE
, 0,
268 GL_RGB
, GL_UNSIGNED_BYTE
, image
);
274 LoadFace(GLenum target
, const char *filename
,
275 GLboolean flipTB
, GLboolean flipLR
)
279 GLubyte
*img
= LoadRGBImage(filename
, &w
, &h
, &format
);
281 printf("Error: couldn't load texture image %s\n", filename
);
284 assert(format
== GL_RGB
);
286 /* <sigh> the way the texture cube mapping works, we have to flip
287 * images to make things look right.
290 const int stride
= 3 * w
;
291 GLubyte temp
[3*1024];
293 for (i
= 0; i
< h
/ 2; i
++) {
294 memcpy(temp
, img
+ i
* stride
, stride
);
295 memcpy(img
+ i
* stride
, img
+ (h
- i
- 1) * stride
, stride
);
296 memcpy(img
+ (h
- i
- 1) * stride
, temp
, stride
);
300 const int stride
= 3 * w
;
304 for (i
= 0; i
< h
; i
++) {
305 row
= img
+ i
* stride
;
306 for (j
= 0; j
< w
/ 2; j
++) {
308 temp
[0] = row
[j
*3+0];
309 temp
[1] = row
[j
*3+1];
310 temp
[2] = row
[j
*3+2];
311 row
[j
*3+0] = row
[k
*3+0];
312 row
[j
*3+1] = row
[k
*3+1];
313 row
[j
*3+2] = row
[k
*3+2];
314 row
[k
*3+0] = temp
[0];
315 row
[k
*3+1] = temp
[1];
316 row
[k
*3+2] = temp
[2];
321 gluBuild2DMipmaps(target
, GL_RGB
, w
, h
, format
, GL_UNSIGNED_BYTE
, img
);
329 LoadFace(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
, "right.rgb", GL_TRUE
, GL_FALSE
);
330 LoadFace(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
, "left.rgb", GL_TRUE
, GL_FALSE
);
331 LoadFace(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
, "top.rgb", GL_FALSE
, GL_TRUE
);
332 LoadFace(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
, "bottom.rgb", GL_FALSE
, GL_TRUE
);
333 LoadFace(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
, "front.rgb", GL_TRUE
, GL_FALSE
);
334 LoadFace(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
, "back.rgb", GL_TRUE
, GL_FALSE
);
339 InitTextures(GLboolean useImageFiles
)
346 glActiveTexture(GL_TEXTURE0
);
347 glBindTexture(GL_TEXTURE_CUBE_MAP
, 1);
356 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_MIN_FILTER
, filter
);
357 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_MAG_FILTER
, filter
);
358 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
359 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
365 GLint imgWidth
, imgHeight
;
367 GLubyte
*image
= NULL
;
369 image
= LoadRGBImage(GroundImage
, &imgWidth
, &imgHeight
, &imgFormat
);
371 printf("Couldn't read %s\n", GroundImage
);
375 glActiveTexture(GL_TEXTURE1
);
376 glBindTexture(GL_TEXTURE_2D
, 2);
377 gluBuild2DMipmaps(GL_TEXTURE_2D
, 3, imgWidth
, imgHeight
,
378 imgFormat
, GL_UNSIGNED_BYTE
, image
);
381 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
382 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
383 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
384 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
390 LoadAndCompileShader(GLuint shader
, const char *text
)
394 glShaderSource_func(shader
, 1, (const GLchar
**) &text
, NULL
);
396 glCompileShader_func(shader
);
398 glGetShaderiv_func(shader
, GL_COMPILE_STATUS
, &stat
);
402 glGetShaderInfoLog_func(shader
, 1000, &len
, log
);
403 fprintf(stderr
, "%s: problem compiling shader: %s\n", Demo
, log
);
407 printf("Shader compiled OK\n");
413 * Read a shader from a file.
416 ReadShader(GLuint shader
, const char *filename
)
418 const int max
= 100*1000;
420 char *buffer
= (char*) malloc(max
);
421 FILE *f
= fopen(filename
, "r");
423 fprintf(stderr
, "%s: Unable to open shader file %s\n", Demo
, filename
);
427 n
= fread(buffer
, 1, max
, f
);
428 printf("%s: read %d bytes from shader file %s\n", Demo
, n
, filename
);
431 LoadAndCompileShader(shader
, buffer
);
440 CheckLink(GLuint prog
)
443 glGetProgramiv_func(prog
, GL_LINK_STATUS
, &stat
);
447 glGetProgramInfoLog_func(prog
, 1000, &len
, log
);
448 fprintf(stderr
, "Linker error:\n%s\n", log
);
451 fprintf(stderr
, "Link success!\n");
457 CreateProgram(const char *vertProgFile
, const char *fragProgFile
,
458 struct uniform_info
*uniforms
)
460 GLuint fragShader
= 0, vertShader
= 0, program
= 0;
463 program
= glCreateProgram_func();
465 vertShader
= glCreateShader_func(GL_VERTEX_SHADER
);
466 ReadShader(vertShader
, vertProgFile
);
467 glAttachShader_func(program
, vertShader
);
471 fragShader
= glCreateShader_func(GL_FRAGMENT_SHADER
);
472 ReadShader(fragShader
, fragProgFile
);
473 glAttachShader_func(program
, fragShader
);
476 glLinkProgram_func(program
);
479 glUseProgram_func(program
);
481 assert(glIsProgram_func(program
));
482 assert(glIsShader_func(fragShader
));
483 assert(glIsShader_func(vertShader
));
485 CheckError(__LINE__
);
486 for (i
= 0; uniforms
[i
].name
; i
++) {
488 = glGetUniformLocation_func(program
, uniforms
[i
].name
);
489 printf("Uniform %s location: %d\n", uniforms
[i
].name
,
490 uniforms
[i
].location
);
492 switch (uniforms
[i
].size
) {
494 if (uniforms
[i
].type
== GL_INT
)
495 glUniform1i_func(uniforms
[i
].location
,
496 (GLint
) uniforms
[i
].value
[0]);
498 glUniform1fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
501 glUniform2fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
504 glUniform3fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
507 glUniform4fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
514 CheckError(__LINE__
);
523 Program1
= CreateProgram(ReflectVertFile
, CubeFragFile
, ReflectUniforms
);
524 Program2
= CreateProgram(SimpleVertFile
, SimpleTexFragFile
, SimpleUniforms
);
529 Init(GLboolean useImageFiles
)
531 const char *version
= (const char *) glGetString(GL_VERSION
);
533 if (version
[0] != '2' || version
[1] != '.') {
534 printf("Warning: this program expects OpenGL 2.0\n");
537 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
541 InitTextures(useImageFiles
);
544 glEnable(GL_DEPTH_TEST
);
546 glClearColor(.6, .6, .9, 0);
547 glColor3f(1.0, 1.0, 1.0);
552 main(int argc
, char *argv
[])
554 glutInit(&argc
, argv
);
555 glutInitWindowSize(500, 400);
556 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
557 glutCreateWindow(Demo
);
558 glutReshapeFunc(Reshape
);
559 glutKeyboardFunc(key
);
560 glutSpecialFunc(specialkey
);
561 glutDisplayFunc(draw
);
564 if (argc
> 1 && strcmp(argv
[1] , "-i") == 0)