blob: 724f15e1a386d8af1a67ee81d3c3d8ad988463a0 [file] [log] [blame]
Brian Paul49789532008-05-20 11:01:17 -06001/**
2 * Test multi-texturing with GL shading language.
3 *
4 * Copyright (C) 2008 Brian Paul All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
22 */
23
24
25
26#include <assert.h>
27#include <math.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Keith Whitwellb799af92009-06-29 14:13:58 +010031#include <GL/glew.h>
Brian Paul49789532008-05-20 11:01:17 -060032#include "GL/glut.h"
33#include "readtex.h"
34#include "extfuncs.h"
35#include "shaderutil.h"
36
37static const char *Demo = "multitex";
38
Brian Paulc0dd9122008-08-16 09:36:46 -060039static const char *VertFile = "multitex.vert";
40static const char *FragFile = "multitex.frag";
Brian Paul49789532008-05-20 11:01:17 -060041
42static const char *TexFiles[2] =
43 {
44 "../images/tile.rgb",
45 "../images/tree2.rgba"
46 };
47
48
49static GLuint Program;
50
Brian Paul8e8b25c2009-02-02 16:50:45 -070051static GLfloat Xrot = 0.0, Yrot = .0, Zrot = 0.0;
Brian Paul49789532008-05-20 11:01:17 -060052static GLfloat EyeDist = 10;
53static GLboolean Anim = GL_TRUE;
Brian Paul8e8b25c2009-02-02 16:50:45 -070054static GLboolean UseArrays = GL_TRUE;
55
56static GLint VertCoord_attr = -1, TexCoord0_attr = -1, TexCoord1_attr = -1;
Brian Paul49789532008-05-20 11:01:17 -060057
58
59/* value[0] = tex unit */
60static struct uniform_info Uniforms[] = {
61 { "tex1", 1, GL_INT, { 0, 0, 0, 0 }, -1 },
62 { "tex2", 1, GL_INT, { 1, 0, 0, 0 }, -1 },
63 END_OF_UNIFORMS
64};
65
66
Brian Paul8e8b25c2009-02-02 16:50:45 -070067static const GLfloat Tex0Coords[4][2] = {
68 { 0.0, 0.0 }, { 2.0, 0.0 }, { 2.0, 2.0 }, { 0.0, 2.0 }
69};
70
71static const GLfloat Tex1Coords[4][2] = {
72 { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 }
73};
74
75static const GLfloat VertCoords[4][2] = {
76 { -3.0, -3.0 }, { 3.0, -3.0 }, { 3.0, 3.0 }, { -3.0, 3.0 }
77};
78
79
Brian Paul49789532008-05-20 11:01:17 -060080static void
Brian Paul8e8b25c2009-02-02 16:50:45 -070081DrawPolygonArray(void)
Brian Paul49789532008-05-20 11:01:17 -060082{
Brian Paul8e8b25c2009-02-02 16:50:45 -070083 if (VertCoord_attr >= 0) {
84 glVertexAttribPointer_func(VertCoord_attr, 2, GL_FLOAT, GL_FALSE,
85 0, VertCoords);
86 glEnableVertexAttribArray_func(VertCoord_attr);
87 }
88 else {
89 glVertexPointer(2, GL_FLOAT, 0, VertCoords);
90 glEnable(GL_VERTEX_ARRAY);
91 }
Brian Paul49789532008-05-20 11:01:17 -060092
Brian Paul8e8b25c2009-02-02 16:50:45 -070093 glVertexAttribPointer_func(TexCoord0_attr, 2, GL_FLOAT, GL_FALSE,
94 0, Tex0Coords);
95 glEnableVertexAttribArray_func(TexCoord0_attr);
Brian Paul49789532008-05-20 11:01:17 -060096
Brian Paul8e8b25c2009-02-02 16:50:45 -070097 glVertexAttribPointer_func(TexCoord1_attr, 2, GL_FLOAT, GL_FALSE,
98 0, Tex1Coords);
99 glEnableVertexAttribArray_func(TexCoord1_attr);
Brian Paul49789532008-05-20 11:01:17 -0600100
Brian Paul8e8b25c2009-02-02 16:50:45 -0700101 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
102}
Brian Paul49789532008-05-20 11:01:17 -0600103
Brian Paul8e8b25c2009-02-02 16:50:45 -0700104
105static void
106DrawPolygonVert(void)
107{
108 GLuint i;
109
110 glBegin(GL_TRIANGLE_FAN);
111
112 for (i = 0; i < 4; i++) {
113 glVertexAttrib2fv_func(TexCoord0_attr, Tex0Coords[i]);
114 glVertexAttrib2fv_func(TexCoord1_attr, Tex1Coords[i]);
115
116 if (VertCoord_attr >= 0)
117 glVertexAttrib2fv_func(VertCoord_attr, VertCoords[i]);
118 else
119 glVertex2fv(VertCoords[i]);
120 }
Brian Paul49789532008-05-20 11:01:17 -0600121
122 glEnd();
Brian Paul49789532008-05-20 11:01:17 -0600123}
124
125
126static void
127draw(void)
128{
129 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
130
131 glPushMatrix(); /* modelview matrix */
132 glTranslatef(0.0, 0.0, -EyeDist);
133 glRotatef(Zrot, 0, 0, 1);
134 glRotatef(Yrot, 0, 1, 0);
135 glRotatef(Xrot, 1, 0, 0);
136
Brian Paul8e8b25c2009-02-02 16:50:45 -0700137 if (UseArrays)
138 DrawPolygonArray();
139 else
140 DrawPolygonVert();
Brian Paul49789532008-05-20 11:01:17 -0600141
142 glPopMatrix();
143
144 glutSwapBuffers();
145}
146
147
148static void
149idle(void)
150{
151 GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
152 Yrot = t;
153 glutPostRedisplay();
154}
155
156
157static void
158key(unsigned char k, int x, int y)
159{
160 (void) x;
161 (void) y;
162 switch (k) {
Brian Paul49789532008-05-20 11:01:17 -0600163 case 'a':
Brian Paul8e8b25c2009-02-02 16:50:45 -0700164 UseArrays = !UseArrays;
165 printf("Arrays: %d\n", UseArrays);
166 break;
167 case ' ':
Brian Paul49789532008-05-20 11:01:17 -0600168 Anim = !Anim;
169 if (Anim)
170 glutIdleFunc(idle);
171 else
172 glutIdleFunc(NULL);
173 break;
174 case 'z':
175 EyeDist -= 0.5;
176 if (EyeDist < 3.0)
177 EyeDist = 3.0;
178 break;
179 case 'Z':
180 EyeDist += 0.5;
181 if (EyeDist > 90.0)
182 EyeDist = 90;
183 break;
184 case 27:
185 exit(0);
186 }
187 glutPostRedisplay();
188}
189
190
191static void
192specialkey(int key, int x, int y)
193{
194 GLfloat step = 2.0;
195 (void) x;
196 (void) y;
197 switch (key) {
198 case GLUT_KEY_UP:
199 Xrot += step;
200 break;
201 case GLUT_KEY_DOWN:
202 Xrot -= step;
203 break;
204 case GLUT_KEY_LEFT:
205 Yrot -= step;
206 break;
207 case GLUT_KEY_RIGHT:
208 Yrot += step;
209 break;
210 }
211 glutPostRedisplay();
212}
213
214
215/* new window size or exposure */
216static void
217Reshape(int width, int height)
218{
219 GLfloat ar = (float) width / (float) height;
220 glViewport(0, 0, (GLint)width, (GLint)height);
221 glMatrixMode(GL_PROJECTION);
222 glLoadIdentity();
223 glFrustum(-2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0);
224 glMatrixMode(GL_MODELVIEW);
225 glLoadIdentity();
226}
227
228
229static void
230InitTextures(void)
231{
232 GLenum filter = GL_LINEAR;
233 int i;
234
235 for (i = 0; i < 2; i++) {
236 GLint imgWidth, imgHeight;
237 GLenum imgFormat;
238 GLubyte *image = NULL;
239
240 image = LoadRGBImage(TexFiles[i], &imgWidth, &imgHeight, &imgFormat);
241 if (!image) {
242 printf("Couldn't read %s\n", TexFiles[i]);
243 exit(0);
244 }
245
246 glActiveTexture(GL_TEXTURE0 + i);
247 glBindTexture(GL_TEXTURE_2D, 42 + i);
248 gluBuild2DMipmaps(GL_TEXTURE_2D, 4, imgWidth, imgHeight,
249 imgFormat, GL_UNSIGNED_BYTE, image);
250 free(image);
251
252 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
253 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
254 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
255 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
256 }
257}
258
259
260static GLuint
261CreateProgram(const char *vertProgFile, const char *fragProgFile,
262 struct uniform_info *uniforms)
263{
264 GLuint fragShader, vertShader, program;
265
266 vertShader = CompileShaderFile(GL_VERTEX_SHADER, vertProgFile);
267 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, fragProgFile);
268 assert(vertShader);
269 program = LinkShaders(vertShader, fragShader);
270
271 glUseProgram_func(program);
272
273 InitUniforms(program, uniforms);
274
Brian Paul58fadc62009-05-22 13:00:49 -0600275 VertCoord_attr = glGetAttribLocation_func(program, "VertCoord");
276 if (VertCoord_attr > 0) {
277 /* We want the VertCoord attrib to have position zero so that
278 * the call to glVertexAttrib(0, xyz) triggers vertex processing.
279 * Otherwise, if TexCoord0 or TexCoord1 gets position 0 we'd have
280 * to set that attribute last (which is a PITA to manage).
281 */
282 glBindAttribLocation_func(program, 0, "VertCoord");
283 /* re-link */
284 glLinkProgram_func(program);
285 /* VertCoord_attr should be zero now */
286 VertCoord_attr = glGetAttribLocation_func(program, "VertCoord");
287 assert(VertCoord_attr == 0);
288 }
289
Brian Paul8e8b25c2009-02-02 16:50:45 -0700290 TexCoord0_attr = glGetAttribLocation_func(program, "TexCoord0");
291 TexCoord1_attr = glGetAttribLocation_func(program, "TexCoord1");
Brian Paul58fadc62009-05-22 13:00:49 -0600292
Brian Paul8e8b25c2009-02-02 16:50:45 -0700293 printf("TexCoord0_attr = %d\n", TexCoord0_attr);
294 printf("TexCoord1_attr = %d\n", TexCoord1_attr);
295 printf("VertCoord_attr = %d\n", VertCoord_attr);
296
Brian Paul49789532008-05-20 11:01:17 -0600297 return program;
298}
299
300
301static void
302InitPrograms(void)
303{
304 Program = CreateProgram(VertFile, FragFile, Uniforms);
305}
306
307
308static void
309InitGL(void)
310{
311 const char *version = (const char *) glGetString(GL_VERSION);
312
313 if (version[0] != '2' || version[1] != '.') {
314 printf("Warning: this program expects OpenGL 2.0\n");
315 /*exit(1);*/
316 }
317 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
318
319 GetExtensionFuncs();
320
321 InitTextures();
322 InitPrograms();
323
324 glEnable(GL_DEPTH_TEST);
325
326 glClearColor(.6, .6, .9, 0);
327 glColor3f(1.0, 1.0, 1.0);
328}
329
330
331int
332main(int argc, char *argv[])
333{
334 glutInit(&argc, argv);
335 glutInitWindowSize(500, 400);
336 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
337 glutCreateWindow(Demo);
Keith Whitwellb799af92009-06-29 14:13:58 +0100338 glewInit();
Brian Paul49789532008-05-20 11:01:17 -0600339 glutReshapeFunc(Reshape);
340 glutKeyboardFunc(key);
341 glutSpecialFunc(specialkey);
342 glutDisplayFunc(draw);
343 if (Anim)
344 glutIdleFunc(idle);
345 InitGL();
346 glutMainLoop();
347 return 0;
348}