2 * Test multi-texturing with GL shading language.
4 * Copyright (C) 2008 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
= "multitex";
38 static const char *VertFile
= "multitex.vert";
39 static const char *FragFile
= "multitex.frag";
41 static const char *TexFiles
[2] =
44 "../images/tree2.rgba"
48 static GLuint Program
;
50 static GLfloat Xrot
= 0.0, Yrot
= .0, Zrot
= 0.0;
51 static GLfloat EyeDist
= 10;
52 static GLboolean Anim
= GL_TRUE
;
53 static GLboolean UseArrays
= GL_TRUE
;
54 static GLboolean UseVBO
= GL_TRUE
;
55 static GLuint VBO
= 0;
57 static GLint VertCoord_attr
= -1, TexCoord0_attr
= -1, TexCoord1_attr
= -1;
60 /* value[0] = tex unit */
61 static struct uniform_info Uniforms
[] = {
62 { "tex1", 1, GL_SAMPLER_2D
, { 0, 0, 0, 0 }, -1 },
63 { "tex2", 1, GL_SAMPLER_2D
, { 1, 0, 0, 0 }, -1 },
68 static const GLfloat Tex0Coords
[4][2] = {
69 { 0.0, 0.0 }, { 2.0, 0.0 }, { 2.0, 2.0 }, { 0.0, 2.0 }
72 static const GLfloat Tex1Coords
[4][2] = {
73 { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 }
76 static const GLfloat VertCoords
[4][2] = {
77 { -3.0, -3.0 }, { 3.0, -3.0 }, { 3.0, 3.0 }, { -3.0, 3.0 }
83 SetupVertexBuffer(void)
85 glGenBuffersARB(1, &VBO
);
86 glBindBufferARB(GL_ARRAY_BUFFER_ARB
, VBO
);
88 glBufferDataARB(GL_ARRAY_BUFFER_ARB
,
95 /* non-interleaved vertex arrays */
97 glBufferSubDataARB(GL_ARRAY_BUFFER_ARB
,
99 sizeof(VertCoords
), /* size */
100 VertCoords
); /* data */
102 glBufferSubDataARB(GL_ARRAY_BUFFER_ARB
,
103 sizeof(VertCoords
), /* offset */
104 sizeof(Tex0Coords
), /* size */
105 Tex0Coords
); /* data */
107 glBufferSubDataARB(GL_ARRAY_BUFFER_ARB
,
109 sizeof(Tex0Coords
), /* offset */
110 sizeof(Tex1Coords
), /* size */
111 Tex1Coords
); /* data */
113 glBindBufferARB(GL_ARRAY_BUFFER_ARB
, 0);
118 DrawPolygonArray(void)
120 void *vertPtr
, *tex0Ptr
, *tex1Ptr
;
123 glBindBufferARB(GL_ARRAY_BUFFER_ARB
, VBO
);
124 vertPtr
= (void *) 0;
125 tex0Ptr
= (void *) sizeof(VertCoords
);
126 tex1Ptr
= (void *) (sizeof(VertCoords
) + sizeof(Tex0Coords
));
129 glBindBufferARB(GL_ARRAY_BUFFER_ARB
, 0);
130 vertPtr
= VertCoords
;
131 tex0Ptr
= Tex0Coords
;
132 tex1Ptr
= Tex1Coords
;
135 if (VertCoord_attr
>= 0) {
136 glVertexAttribPointer(VertCoord_attr
, 2, GL_FLOAT
, GL_FALSE
,
138 glEnableVertexAttribArray(VertCoord_attr
);
141 glVertexPointer(2, GL_FLOAT
, 0, vertPtr
);
142 glEnableClientState(GL_VERTEX_ARRAY
);
145 glVertexAttribPointer(TexCoord0_attr
, 2, GL_FLOAT
, GL_FALSE
,
147 glEnableVertexAttribArray(TexCoord0_attr
);
149 glVertexAttribPointer(TexCoord1_attr
, 2, GL_FLOAT
, GL_FALSE
,
151 glEnableVertexAttribArray(TexCoord1_attr
);
153 glDrawArrays(GL_TRIANGLE_FAN
, 0, 4);
155 glBindBufferARB(GL_ARRAY_BUFFER_ARB
, 0);
160 DrawPolygonVert(void)
164 glBegin(GL_TRIANGLE_FAN
);
166 for (i
= 0; i
< 4; i
++) {
167 glVertexAttrib2fv(TexCoord0_attr
, Tex0Coords
[i
]);
168 glVertexAttrib2fv(TexCoord1_attr
, Tex1Coords
[i
]);
170 if (VertCoord_attr
>= 0)
171 glVertexAttrib2fv(VertCoord_attr
, VertCoords
[i
]);
173 glVertex2fv(VertCoords
[i
]);
183 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
185 glPushMatrix(); /* modelview matrix */
186 glTranslatef(0.0, 0.0, -EyeDist
);
187 glRotatef(Zrot
, 0, 0, 1);
188 glRotatef(Yrot
, 0, 1, 0);
189 glRotatef(Xrot
, 1, 0, 0);
205 GLfloat t
= 0.05 * glutGet(GLUT_ELAPSED_TIME
);
212 key(unsigned char k
, int x
, int y
)
218 UseArrays
= !UseArrays
;
219 printf("Arrays: %d\n", UseArrays
);
223 printf("Use VBO: %d\n", UseVBO
);
250 specialkey(int key
, int x
, int y
)
273 /* new window size or exposure */
275 Reshape(int width
, int height
)
277 GLfloat ar
= (float) width
/ (float) height
;
278 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
279 glMatrixMode(GL_PROJECTION
);
281 glFrustum(-2.0*ar
, 2.0*ar
, -2.0, 2.0, 4.0, 100.0);
282 glMatrixMode(GL_MODELVIEW
);
290 GLenum filter
= GL_LINEAR
;
293 for (i
= 0; i
< 2; i
++) {
294 GLint imgWidth
, imgHeight
;
296 GLubyte
*image
= NULL
;
298 image
= LoadRGBImage(TexFiles
[i
], &imgWidth
, &imgHeight
, &imgFormat
);
300 printf("Couldn't read %s\n", TexFiles
[i
]);
304 glActiveTexture(GL_TEXTURE0
+ i
);
305 glBindTexture(GL_TEXTURE_2D
, 42 + i
);
306 gluBuild2DMipmaps(GL_TEXTURE_2D
, 4, imgWidth
, imgHeight
,
307 imgFormat
, GL_UNSIGNED_BYTE
, image
);
310 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
311 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
312 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, filter
);
313 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, filter
);
319 CreateProgram(const char *vertProgFile
, const char *fragProgFile
,
320 struct uniform_info
*uniforms
)
322 GLuint fragShader
, vertShader
, program
;
324 vertShader
= CompileShaderFile(GL_VERTEX_SHADER
, vertProgFile
);
325 fragShader
= CompileShaderFile(GL_FRAGMENT_SHADER
, fragProgFile
);
327 program
= LinkShaders(vertShader
, fragShader
);
329 glUseProgram(program
);
331 SetUniformValues(program
, uniforms
);
332 PrintUniforms(Uniforms
);
334 assert(ValidateShaderProgram(program
));
336 VertCoord_attr
= glGetAttribLocation(program
, "VertCoord");
337 if (VertCoord_attr
> 0) {
338 /* We want the VertCoord attrib to have position zero so that
339 * the call to glVertexAttrib(0, xyz) triggers vertex processing.
340 * Otherwise, if TexCoord0 or TexCoord1 gets position 0 we'd have
341 * to set that attribute last (which is a PITA to manage).
343 glBindAttribLocation(program
, 0, "VertCoord");
345 glLinkProgram(program
);
346 /* VertCoord_attr should be zero now */
347 VertCoord_attr
= glGetAttribLocation(program
, "VertCoord");
348 assert(VertCoord_attr
== 0);
351 TexCoord0_attr
= glGetAttribLocation(program
, "TexCoord0");
352 TexCoord1_attr
= glGetAttribLocation(program
, "TexCoord1");
354 printf("TexCoord0_attr = %d\n", TexCoord0_attr
);
355 printf("TexCoord1_attr = %d\n", TexCoord1_attr
);
356 printf("VertCoord_attr = %d\n", VertCoord_attr
);
365 Program
= CreateProgram(VertFile
, FragFile
, Uniforms
);
372 if (!ShadersSupported())
375 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
377 printf(" a - toggle arrays vs. immediate mode rendering\n");
378 printf(" v - toggle VBO usage for array rendering\n");
379 printf(" z/Z - change viewing distance\n");
380 printf(" SPACE - toggle animation\n");
381 printf(" Esc - exit\n");
388 glEnable(GL_DEPTH_TEST
);
390 glClearColor(.6, .6, .9, 0);
391 glColor3f(1.0, 1.0, 1.0);
396 main(int argc
, char *argv
[])
398 glutInit(&argc
, argv
);
399 glutInitWindowSize(500, 400);
400 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
401 glutCreateWindow(Demo
);
403 glutReshapeFunc(Reshape
);
404 glutKeyboardFunc(key
);
405 glutSpecialFunc(specialkey
);
406 glutDisplayFunc(draw
);