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
= -90.0, Yrot
= .0, Zrot
= 0.0;
51 static GLfloat EyeDist
= 10;
52 static GLboolean Anim
= GL_TRUE
;
55 /* value[0] = tex unit */
56 static struct uniform_info Uniforms
[] = {
57 { "tex1", 1, GL_INT
, { 0, 0, 0, 0 }, -1 },
58 { "tex2", 1, GL_INT
, { 1, 0, 0, 0 }, -1 },
64 DrawPolygon(GLfloat size
)
67 glRotatef(90, 1, 0, 0);
71 glMultiTexCoord2f(GL_TEXTURE0
, 0, 0);
72 glMultiTexCoord2f(GL_TEXTURE1
, 0, 0);
73 glVertex2f(-size
, -size
);
75 glMultiTexCoord2f(GL_TEXTURE0
, 2, 0);
76 glMultiTexCoord2f(GL_TEXTURE1
, 1, 0);
77 glVertex2f( size
, -size
);
79 glMultiTexCoord2f(GL_TEXTURE0
, 2, 2);
80 glMultiTexCoord2f(GL_TEXTURE1
, 1, 1);
81 glVertex2f( size
, size
);
83 glMultiTexCoord2f(GL_TEXTURE0
, 0, 2);
84 glMultiTexCoord2f(GL_TEXTURE1
, 0, 1);
85 glVertex2f(-size
, size
);
95 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
97 glPushMatrix(); /* modelview matrix */
98 glTranslatef(0.0, 0.0, -EyeDist
);
99 glRotatef(Zrot
, 0, 0, 1);
100 glRotatef(Yrot
, 0, 1, 0);
101 glRotatef(Xrot
, 1, 0, 0);
114 GLfloat t
= 0.05 * glutGet(GLUT_ELAPSED_TIME
);
121 key(unsigned char k
, int x
, int y
)
152 specialkey(int key
, int x
, int y
)
175 /* new window size or exposure */
177 Reshape(int width
, int height
)
179 GLfloat ar
= (float) width
/ (float) height
;
180 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
181 glMatrixMode(GL_PROJECTION
);
183 glFrustum(-2.0*ar
, 2.0*ar
, -2.0, 2.0, 4.0, 100.0);
184 glMatrixMode(GL_MODELVIEW
);
192 GLenum filter
= GL_LINEAR
;
195 for (i
= 0; i
< 2; i
++) {
196 GLint imgWidth
, imgHeight
;
198 GLubyte
*image
= NULL
;
200 image
= LoadRGBImage(TexFiles
[i
], &imgWidth
, &imgHeight
, &imgFormat
);
202 printf("Couldn't read %s\n", TexFiles
[i
]);
206 glActiveTexture(GL_TEXTURE0
+ i
);
207 glBindTexture(GL_TEXTURE_2D
, 42 + i
);
208 gluBuild2DMipmaps(GL_TEXTURE_2D
, 4, imgWidth
, imgHeight
,
209 imgFormat
, GL_UNSIGNED_BYTE
, image
);
212 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
213 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
214 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, filter
);
215 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, filter
);
221 CreateProgram(const char *vertProgFile
, const char *fragProgFile
,
222 struct uniform_info
*uniforms
)
224 GLuint fragShader
, vertShader
, program
;
226 vertShader
= CompileShaderFile(GL_VERTEX_SHADER
, vertProgFile
);
227 fragShader
= CompileShaderFile(GL_FRAGMENT_SHADER
, fragProgFile
);
229 program
= LinkShaders(vertShader
, fragShader
);
231 glUseProgram_func(program
);
233 InitUniforms(program
, uniforms
);
242 Program
= CreateProgram(VertFile
, FragFile
, Uniforms
);
249 const char *version
= (const char *) glGetString(GL_VERSION
);
251 if (version
[0] != '2' || version
[1] != '.') {
252 printf("Warning: this program expects OpenGL 2.0\n");
255 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER
));
262 glEnable(GL_DEPTH_TEST
);
264 glClearColor(.6, .6, .9, 0);
265 glColor3f(1.0, 1.0, 1.0);
270 main(int argc
, char *argv
[])
272 glutInit(&argc
, argv
);
273 glutInitWindowSize(500, 400);
274 glutInitDisplayMode(GLUT_RGB
| GLUT_DEPTH
| GLUT_DOUBLE
);
275 glutCreateWindow(Demo
);
276 glutReshapeFunc(Reshape
);
277 glutKeyboardFunc(key
);
278 glutSpecialFunc(specialkey
);
279 glutDisplayFunc(draw
);