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
;
58 GLenum type
; /**< GL_FLOAT or GL_INT */
62 static struct uniform_info ReflectUniforms
[] = {
63 { "cubeTex", 1, -1, GL_INT
, { 0, 0, 0, 0 } },
64 { "lightPos", 3, -1, GL_FLOAT
, { 10, 10, 20, 0 } },
65 { NULL
, 0, 0, 0, { 0, 0, 0, 0 } }
68 static struct uniform_info SimpleUniforms
[] = {
69 { "tex2d", 1, -1, GL_INT
, { 1, 0, 0, 0 } },
70 { "lightPos", 3, -1, GL_FLOAT
, { 10, 10, 20, 0 } },
71 { NULL
, 0, 0, 0, { 0, 0, 0, 0 } }
78 GLenum err
= glGetError();
80 printf("GL Error %s (0x%x) at line %d\n",
81 gluErrorString(err
), (int) err
, line
);
87 DrawGround(GLfloat size
)
90 glRotatef(90, 1, 0, 0);
93 glTexCoord2f(-2, -2); glVertex2f(-size
, -size
);
94 glTexCoord2f( 2, -2); glVertex2f( size
, -size
);
95 glTexCoord2f( 2, 2); glVertex2f( size
, size
);
96 glTexCoord2f(-2, 2); glVertex2f(-size
, size
);
105 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
107 glEnable(GL_TEXTURE_2D
);
109 glPushMatrix(); /* modelview matrix */
110 glTranslatef(0.0, 0.0, -EyeDist
);
111 glRotatef(Xrot
, 1, 0, 0);
112 glRotatef(Yrot
, 0, 1, 0);
113 glRotatef(Zrot
, 0, 0, 1);
115 /* sphere w/ reflection map */
117 glTranslatef(0, 1, 0);
118 glUseProgram_func(Program1
);
120 /* setup texture matrix */
121 glActiveTexture(GL_TEXTURE0
);
122 glMatrixMode(GL_TEXTURE
);
124 glRotatef(-TexYrot
, 0, 1, 0);
125 glRotatef(-TexXrot
, 1, 0, 0);
127 glEnable(GL_TEXTURE_GEN_S
);
128 glEnable(GL_TEXTURE_GEN_T
);
129 glEnable(GL_TEXTURE_GEN_R
);
130 glutSolidSphere(2.0, 20, 20);
132 glLoadIdentity(); /* texture matrix */
133 glMatrixMode(GL_MODELVIEW
);
137 glUseProgram_func(Program2
);
138 glTranslatef(0, -1.0, 0);
150 GLfloat t
= 0.05 * glutGet(GLUT_ELAPSED_TIME
);
157 key(unsigned char k
, int x
, int y
)
181 glutDestroyWindow(win
);
189 specialkey(int key
, int x
, int y
)
212 /* new window size or exposure */
214 Reshape(int width
, int height
)
216 GLfloat ar
= (float) width
/ (float) height
;
217 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
218 glMatrixMode(GL_PROJECTION
);
220 glFrustum(-2.0*ar
, 2.0*ar
, -2.0, 2.0, 4.0, 100.0);
221 glMatrixMode(GL_MODELVIEW
);
229 #define CUBE_TEX_SIZE 64
230 GLubyte image
[CUBE_TEX_SIZE
][CUBE_TEX_SIZE
][3];
231 static const GLubyte colors
[6][3] = {
232 { 255, 0, 0 }, /* face 0 - red */
233 { 0, 255, 255 }, /* face 1 - cyan */
234 { 0, 255, 0 }, /* face 2 - green */
235 { 255, 0, 255 }, /* face 3 - purple */
236 { 0, 0, 255 }, /* face 4 - blue */
237 { 255, 255, 0 } /* face 5 - yellow */
239 static const GLenum targets
[6] = {
240 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
,
241 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
,
242 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
,
243 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
,
244 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
,
245 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
250 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
252 /* make colored checkerboard cube faces */
253 for (f
= 0; f
< 6; f
++) {
254 for (i
= 0; i
< CUBE_TEX_SIZE
; i
++) {
255 for (j
= 0; j
< CUBE_TEX_SIZE
; j
++) {
256 if ((i
/4 + j
/4) & 1) {
257 image
[i
][j
][0] = colors
[f
][0];
258 image
[i
][j
][1] = colors
[f
][1];
259 image
[i
][j
][2] = colors
[f
][2];
262 image
[i
][j
][0] = 255;
263 image
[i
][j
][1] = 255;
264 image
[i
][j
][2] = 255;
269 glTexImage2D(targets
[f
], 0, GL_RGB
, CUBE_TEX_SIZE
, CUBE_TEX_SIZE
, 0,
270 GL_RGB
, GL_UNSIGNED_BYTE
, image
);
276 LoadFace(GLenum target
, const char *filename
,
277 GLboolean flipTB
, GLboolean flipLR
)
281 GLubyte
*img
= LoadRGBImage(filename
, &w
, &h
, &format
);
283 printf("Error: couldn't load texture image %s\n", filename
);
286 assert(format
== GL_RGB
);
288 /* <sigh> the way the texture cube mapping works, we have to flip
289 * images to make things look right.
292 const int stride
= 3 * w
;
293 GLubyte temp
[3*1024];
295 for (i
= 0; i
< h
/ 2; i
++) {
296 memcpy(temp
, img
+ i
* stride
, stride
);
297 memcpy(img
+ i
* stride
, img
+ (h
- i
- 1) * stride
, stride
);
298 memcpy(img
+ (h
- i
- 1) * stride
, temp
, stride
);
302 const int stride
= 3 * w
;
306 for (i
= 0; i
< h
; i
++) {
307 row
= img
+ i
* stride
;
308 for (j
= 0; j
< w
/ 2; j
++) {
310 temp
[0] = row
[j
*3+0];
311 temp
[1] = row
[j
*3+1];
312 temp
[2] = row
[j
*3+2];
313 row
[j
*3+0] = row
[k
*3+0];
314 row
[j
*3+1] = row
[k
*3+1];
315 row
[j
*3+2] = row
[k
*3+2];
316 row
[k
*3+0] = temp
[0];
317 row
[k
*3+1] = temp
[1];
318 row
[k
*3+2] = temp
[2];
323 gluBuild2DMipmaps(target
, GL_RGB
, w
, h
, format
, GL_UNSIGNED_BYTE
, img
);
331 LoadFace(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
, "right.rgb", GL_TRUE
, GL_FALSE
);
332 LoadFace(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
, "left.rgb", GL_TRUE
, GL_FALSE
);
333 LoadFace(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
, "top.rgb", GL_FALSE
, GL_TRUE
);
334 LoadFace(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
, "bottom.rgb", GL_FALSE
, GL_TRUE
);
335 LoadFace(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
, "front.rgb", GL_TRUE
, GL_FALSE
);
336 LoadFace(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
, "back.rgb", GL_TRUE
, GL_FALSE
);
341 InitTextures(GLboolean useImageFiles
)
348 glActiveTexture(GL_TEXTURE0
);
349 glBindTexture(GL_TEXTURE_CUBE_MAP
, 1);
358 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_MIN_FILTER
, filter
);
359 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_MAG_FILTER
, filter
);
360 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
361 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
367 GLint imgWidth
, imgHeight
;
369 GLubyte
*image
= NULL
;
371 image
= LoadRGBImage(GroundImage
, &imgWidth
, &imgHeight
, &imgFormat
);
373 printf("Couldn't read %s\n", GroundImage
);
377 glActiveTexture(GL_TEXTURE1
);
378 glBindTexture(GL_TEXTURE_2D
, 2);
379 gluBuild2DMipmaps(GL_TEXTURE_2D
, 3, imgWidth
, imgHeight
,
380 imgFormat
, GL_UNSIGNED_BYTE
, image
);
383 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
384 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
385 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
386 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
392 LoadAndCompileShader(GLuint shader
, const char *text
)
396 glShaderSource_func(shader
, 1, (const GLchar
**) &text
, NULL
);
398 glCompileShader_func(shader
);
400 glGetShaderiv_func(shader
, GL_COMPILE_STATUS
, &stat
);
404 glGetShaderInfoLog_func(shader
, 1000, &len
, log
);
405 fprintf(stderr
, "%s: problem compiling shader: %s\n", Demo
, log
);
409 printf("Shader compiled OK\n");
415 * Read a shader from a file.
418 ReadShader(GLuint shader
, const char *filename
)
420 const int max
= 100*1000;
422 char *buffer
= (char*) malloc(max
);
423 FILE *f
= fopen(filename
, "r");
425 fprintf(stderr
, "%s: Unable to open shader file %s\n", Demo
, filename
);
429 n
= fread(buffer
, 1, max
, f
);
430 printf("%s: read %d bytes from shader file %s\n", Demo
, n
, filename
);
433 LoadAndCompileShader(shader
, buffer
);
442 CheckLink(GLuint prog
)
445 glGetProgramiv_func(prog
, GL_LINK_STATUS
, &stat
);
449 glGetProgramInfoLog_func(prog
, 1000, &len
, log
);
450 fprintf(stderr
, "Linker error:\n%s\n", log
);
453 fprintf(stderr
, "Link success!\n");
459 CreateProgram(const char *vertProgFile
, const char *fragProgFile
,
460 struct uniform_info
*uniforms
)
462 GLuint fragShader
= 0, vertShader
= 0, program
= 0;
465 program
= glCreateProgram_func();
467 vertShader
= glCreateShader_func(GL_VERTEX_SHADER
);
468 ReadShader(vertShader
, vertProgFile
);
469 glAttachShader_func(program
, vertShader
);
473 fragShader
= glCreateShader_func(GL_FRAGMENT_SHADER
);
474 ReadShader(fragShader
, fragProgFile
);
475 glAttachShader_func(program
, fragShader
);
478 glLinkProgram_func(program
);
481 glUseProgram_func(program
);
483 assert(glIsProgram_func(program
));
484 assert(glIsShader_func(fragShader
));
485 assert(glIsShader_func(vertShader
));
487 CheckError(__LINE__
);
488 for (i
= 0; uniforms
[i
].name
; i
++) {
490 = glGetUniformLocation_func(program
, uniforms
[i
].name
);
491 printf("Uniform %s location: %d\n", uniforms
[i
].name
,
492 uniforms
[i
].location
);
494 switch (uniforms
[i
].size
) {
496 if (uniforms
[i
].type
== GL_INT
)
497 glUniform1i_func(uniforms
[i
].location
,
498 (GLint
) uniforms
[i
].value
[0]);
500 glUniform1fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
503 glUniform2fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
506 glUniform3fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
509 glUniform4fv_func(uniforms
[i
].location
, 1, uniforms
[i
].value
);
516 CheckError(__LINE__
);
525 Program1
= CreateProgram(ReflectVertFile
, CubeFragFile
, ReflectUniforms
);
526 Program2
= CreateProgram(SimpleVertFile
, SimpleTexFragFile
, SimpleUniforms
);
531 Init(GLboolean useImageFiles
)
533 const char *version
= (const char *) glGetString(GL_VERSION
);
535 if (version
[0] != '2' || version
[1] != '.') {
536 printf("Warning: this program expects OpenGL 2.0\n");
539 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
543 InitTextures(useImageFiles
);
546 glEnable(GL_DEPTH_TEST
);
548 glClearColor(.6, .6, .9, 0);
549 glColor3f(1.0, 1.0, 1.0);
554 main(int argc
, char *argv
[])
556 glutInit(&argc
, argv
);
557 glutInitWindowSize(500, 400);
558 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
559 win
= glutCreateWindow(Demo
);
560 glutReshapeFunc(Reshape
);
561 glutKeyboardFunc(key
);
562 glutSpecialFunc(specialkey
);
563 glutDisplayFunc(draw
);
566 if (argc
> 1 && strcmp(argv
[1] , "-i") == 0)