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
;
55 static GLint VertCoord_attr
= -1, TexCoord0_attr
= -1, TexCoord1_attr
= -1;
58 /* value[0] = tex unit */
59 static struct uniform_info Uniforms
[] = {
60 { "tex1", 1, GL_INT
, { 0, 0, 0, 0 }, -1 },
61 { "tex2", 1, GL_INT
, { 1, 0, 0, 0 }, -1 },
66 static const GLfloat Tex0Coords
[4][2] = {
67 { 0.0, 0.0 }, { 2.0, 0.0 }, { 2.0, 2.0 }, { 0.0, 2.0 }
70 static const GLfloat Tex1Coords
[4][2] = {
71 { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 }
74 static const GLfloat VertCoords
[4][2] = {
75 { -3.0, -3.0 }, { 3.0, -3.0 }, { 3.0, 3.0 }, { -3.0, 3.0 }
80 DrawPolygonArray(void)
82 if (VertCoord_attr
>= 0) {
83 glVertexAttribPointer_func(VertCoord_attr
, 2, GL_FLOAT
, GL_FALSE
,
85 glEnableVertexAttribArray_func(VertCoord_attr
);
88 glVertexPointer(2, GL_FLOAT
, 0, VertCoords
);
89 glEnable(GL_VERTEX_ARRAY
);
92 glVertexAttribPointer_func(TexCoord0_attr
, 2, GL_FLOAT
, GL_FALSE
,
94 glEnableVertexAttribArray_func(TexCoord0_attr
);
96 glVertexAttribPointer_func(TexCoord1_attr
, 2, GL_FLOAT
, GL_FALSE
,
98 glEnableVertexAttribArray_func(TexCoord1_attr
);
100 glDrawArrays(GL_TRIANGLE_FAN
, 0, 4);
105 DrawPolygonVert(void)
109 glBegin(GL_TRIANGLE_FAN
);
111 for (i
= 0; i
< 4; i
++) {
112 glVertexAttrib2fv_func(TexCoord0_attr
, Tex0Coords
[i
]);
113 glVertexAttrib2fv_func(TexCoord1_attr
, Tex1Coords
[i
]);
115 if (VertCoord_attr
>= 0)
116 glVertexAttrib2fv_func(VertCoord_attr
, VertCoords
[i
]);
118 glVertex2fv(VertCoords
[i
]);
128 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
130 glPushMatrix(); /* modelview matrix */
131 glTranslatef(0.0, 0.0, -EyeDist
);
132 glRotatef(Zrot
, 0, 0, 1);
133 glRotatef(Yrot
, 0, 1, 0);
134 glRotatef(Xrot
, 1, 0, 0);
150 GLfloat t
= 0.05 * glutGet(GLUT_ELAPSED_TIME
);
157 key(unsigned char k
, int x
, int y
)
163 UseArrays
= !UseArrays
;
164 printf("Arrays: %d\n", UseArrays
);
191 specialkey(int key
, int x
, int y
)
214 /* new window size or exposure */
216 Reshape(int width
, int height
)
218 GLfloat ar
= (float) width
/ (float) height
;
219 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
220 glMatrixMode(GL_PROJECTION
);
222 glFrustum(-2.0*ar
, 2.0*ar
, -2.0, 2.0, 4.0, 100.0);
223 glMatrixMode(GL_MODELVIEW
);
231 GLenum filter
= GL_LINEAR
;
234 for (i
= 0; i
< 2; i
++) {
235 GLint imgWidth
, imgHeight
;
237 GLubyte
*image
= NULL
;
239 image
= LoadRGBImage(TexFiles
[i
], &imgWidth
, &imgHeight
, &imgFormat
);
241 printf("Couldn't read %s\n", TexFiles
[i
]);
245 glActiveTexture(GL_TEXTURE0
+ i
);
246 glBindTexture(GL_TEXTURE_2D
, 42 + i
);
247 gluBuild2DMipmaps(GL_TEXTURE_2D
, 4, imgWidth
, imgHeight
,
248 imgFormat
, GL_UNSIGNED_BYTE
, image
);
251 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
252 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
253 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, filter
);
254 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, filter
);
260 CreateProgram(const char *vertProgFile
, const char *fragProgFile
,
261 struct uniform_info
*uniforms
)
263 GLuint fragShader
, vertShader
, program
;
265 vertShader
= CompileShaderFile(GL_VERTEX_SHADER
, vertProgFile
);
266 fragShader
= CompileShaderFile(GL_FRAGMENT_SHADER
, fragProgFile
);
268 program
= LinkShaders(vertShader
, fragShader
);
270 glUseProgram_func(program
);
272 InitUniforms(program
, uniforms
);
274 TexCoord0_attr
= glGetAttribLocation_func(program
, "TexCoord0");
275 TexCoord1_attr
= glGetAttribLocation_func(program
, "TexCoord1");
276 VertCoord_attr
= glGetAttribLocation_func(program
, "VertCoord");
277 printf("TexCoord0_attr = %d\n", TexCoord0_attr
);
278 printf("TexCoord1_attr = %d\n", TexCoord1_attr
);
279 printf("VertCoord_attr = %d\n", VertCoord_attr
);
288 Program
= CreateProgram(VertFile
, FragFile
, Uniforms
);
295 const char *version
= (const char *) glGetString(GL_VERSION
);
297 if (version
[0] != '2' || version
[1] != '.') {
298 printf("Warning: this program expects OpenGL 2.0\n");
301 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
308 glEnable(GL_DEPTH_TEST
);
310 glClearColor(.6, .6, .9, 0);
311 glColor3f(1.0, 1.0, 1.0);
316 main(int argc
, char *argv
[])
318 glutInit(&argc
, argv
);
319 glutInitWindowSize(500, 400);
320 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
321 glutCreateWindow(Demo
);
322 glutReshapeFunc(Reshape
);
323 glutKeyboardFunc(key
);
324 glutSpecialFunc(specialkey
);
325 glutDisplayFunc(draw
);