blob: a4a8bbe38fa267683ef78be4172cee3302c90376 [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"
Brian Paul49789532008-05-20 11:01:17 -060034#include "shaderutil.h"
35
36static const char *Demo = "multitex";
37
Brian Paulc0dd9122008-08-16 09:36:46 -060038static const char *VertFile = "multitex.vert";
39static const char *FragFile = "multitex.frag";
Brian Paul49789532008-05-20 11:01:17 -060040
41static const char *TexFiles[2] =
42 {
43 "../images/tile.rgb",
44 "../images/tree2.rgba"
45 };
46
47
48static GLuint Program;
49
Brian Paul8e8b25c2009-02-02 16:50:45 -070050static GLfloat Xrot = 0.0, Yrot = .0, Zrot = 0.0;
Brian Paul49789532008-05-20 11:01:17 -060051static GLfloat EyeDist = 10;
52static GLboolean Anim = GL_TRUE;
Brian Paul8e8b25c2009-02-02 16:50:45 -070053static GLboolean UseArrays = GL_TRUE;
Brian Paul891a2bd2009-05-22 13:12:28 -060054static GLboolean UseVBO = GL_TRUE;
55static GLuint VBO = 0;
Brian Paul8e8b25c2009-02-02 16:50:45 -070056
57static GLint VertCoord_attr = -1, TexCoord0_attr = -1, TexCoord1_attr = -1;
Brian Paul49789532008-05-20 11:01:17 -060058
59
60/* value[0] = tex unit */
61static struct uniform_info Uniforms[] = {
62 { "tex1", 1, GL_INT, { 0, 0, 0, 0 }, -1 },
63 { "tex2", 1, GL_INT, { 1, 0, 0, 0 }, -1 },
64 END_OF_UNIFORMS
65};
66
67
Brian Paul8e8b25c2009-02-02 16:50:45 -070068static const GLfloat Tex0Coords[4][2] = {
69 { 0.0, 0.0 }, { 2.0, 0.0 }, { 2.0, 2.0 }, { 0.0, 2.0 }
70};
71
72static const GLfloat Tex1Coords[4][2] = {
73 { 0.0, 0.0 }, { 1.0, 0.0 }, { 1.0, 1.0 }, { 0.0, 1.0 }
74};
75
76static const GLfloat VertCoords[4][2] = {
77 { -3.0, -3.0 }, { 3.0, -3.0 }, { 3.0, 3.0 }, { -3.0, 3.0 }
78};
79
80
Brian Paul891a2bd2009-05-22 13:12:28 -060081
82static void
83SetupVertexBuffer(void)
84{
Brian Paulb96ae1b2009-07-17 22:00:47 -060085 glGenBuffersARB(1, &VBO);
86 glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBO);
Brian Paul891a2bd2009-05-22 13:12:28 -060087
Brian Paulb96ae1b2009-07-17 22:00:47 -060088 glBufferDataARB(GL_ARRAY_BUFFER_ARB,
Brian Paul891a2bd2009-05-22 13:12:28 -060089 sizeof(VertCoords) +
90 sizeof(Tex0Coords) +
91 sizeof(Tex1Coords),
92 NULL,
93 GL_STATIC_DRAW_ARB);
94
95 /* non-interleaved vertex arrays */
96
Brian Paulb96ae1b2009-07-17 22:00:47 -060097 glBufferSubDataARB(GL_ARRAY_BUFFER_ARB,
Brian Paul891a2bd2009-05-22 13:12:28 -060098 0, /* offset */
99 sizeof(VertCoords), /* size */
100 VertCoords); /* data */
101
Brian Paulb96ae1b2009-07-17 22:00:47 -0600102 glBufferSubDataARB(GL_ARRAY_BUFFER_ARB,
Brian Paul891a2bd2009-05-22 13:12:28 -0600103 sizeof(VertCoords), /* offset */
104 sizeof(Tex0Coords), /* size */
105 Tex0Coords); /* data */
106
Brian Paulb96ae1b2009-07-17 22:00:47 -0600107 glBufferSubDataARB(GL_ARRAY_BUFFER_ARB,
Brian Paul891a2bd2009-05-22 13:12:28 -0600108 sizeof(VertCoords) +
109 sizeof(Tex0Coords), /* offset */
110 sizeof(Tex1Coords), /* size */
111 Tex1Coords); /* data */
112
Brian Paulb96ae1b2009-07-17 22:00:47 -0600113 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
Brian Paul891a2bd2009-05-22 13:12:28 -0600114}
115
116
Brian Paul49789532008-05-20 11:01:17 -0600117static void
Brian Paul8e8b25c2009-02-02 16:50:45 -0700118DrawPolygonArray(void)
Brian Paul49789532008-05-20 11:01:17 -0600119{
Brian Paul891a2bd2009-05-22 13:12:28 -0600120 void *vertPtr, *tex0Ptr, *tex1Ptr;
121
122 if (UseVBO) {
Brian Paulb96ae1b2009-07-17 22:00:47 -0600123 glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBO);
Brian Paul891a2bd2009-05-22 13:12:28 -0600124 vertPtr = (void *) 0;
125 tex0Ptr = (void *) sizeof(VertCoords);
126 tex1Ptr = (void *) (sizeof(VertCoords) + sizeof(Tex0Coords));
127 }
128 else {
Brian Paulb96ae1b2009-07-17 22:00:47 -0600129 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
Brian Paul891a2bd2009-05-22 13:12:28 -0600130 vertPtr = VertCoords;
131 tex0Ptr = Tex0Coords;
132 tex1Ptr = Tex1Coords;
133 }
134
Brian Paul8e8b25c2009-02-02 16:50:45 -0700135 if (VertCoord_attr >= 0) {
Brian Paulee0b1bc2009-07-17 13:23:11 -0600136 glVertexAttribPointer(VertCoord_attr, 2, GL_FLOAT, GL_FALSE,
Eric Anholt74504c42009-08-10 15:50:22 -0700137 0, vertPtr);
Brian Paulee0b1bc2009-07-17 13:23:11 -0600138 glEnableVertexAttribArray(VertCoord_attr);
Brian Paul8e8b25c2009-02-02 16:50:45 -0700139 }
140 else {
Brian Paul891a2bd2009-05-22 13:12:28 -0600141 glVertexPointer(2, GL_FLOAT, 0, vertPtr);
Brian Paul820436f2009-07-08 13:58:30 -0600142 glEnableClientState(GL_VERTEX_ARRAY);
Brian Paul8e8b25c2009-02-02 16:50:45 -0700143 }
Brian Paul49789532008-05-20 11:01:17 -0600144
Brian Paulee0b1bc2009-07-17 13:23:11 -0600145 glVertexAttribPointer(TexCoord0_attr, 2, GL_FLOAT, GL_FALSE,
Eric Anholt74504c42009-08-10 15:50:22 -0700146 0, tex0Ptr);
Brian Paulee0b1bc2009-07-17 13:23:11 -0600147 glEnableVertexAttribArray(TexCoord0_attr);
Brian Paul49789532008-05-20 11:01:17 -0600148
Brian Paulee0b1bc2009-07-17 13:23:11 -0600149 glVertexAttribPointer(TexCoord1_attr, 2, GL_FLOAT, GL_FALSE,
Eric Anholt74504c42009-08-10 15:50:22 -0700150 0, tex1Ptr);
Brian Paulee0b1bc2009-07-17 13:23:11 -0600151 glEnableVertexAttribArray(TexCoord1_attr);
Brian Paul49789532008-05-20 11:01:17 -0600152
Brian Paul8e8b25c2009-02-02 16:50:45 -0700153 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Brian Paul891a2bd2009-05-22 13:12:28 -0600154
Brian Paulb96ae1b2009-07-17 22:00:47 -0600155 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
Brian Paul8e8b25c2009-02-02 16:50:45 -0700156}
Brian Paul49789532008-05-20 11:01:17 -0600157
Brian Paul8e8b25c2009-02-02 16:50:45 -0700158
159static void
160DrawPolygonVert(void)
161{
162 GLuint i;
163
164 glBegin(GL_TRIANGLE_FAN);
165
166 for (i = 0; i < 4; i++) {
Brian Paulee0b1bc2009-07-17 13:23:11 -0600167 glVertexAttrib2fv(TexCoord0_attr, Tex0Coords[i]);
168 glVertexAttrib2fv(TexCoord1_attr, Tex1Coords[i]);
Brian Paul8e8b25c2009-02-02 16:50:45 -0700169
170 if (VertCoord_attr >= 0)
Brian Paulee0b1bc2009-07-17 13:23:11 -0600171 glVertexAttrib2fv(VertCoord_attr, VertCoords[i]);
Brian Paul8e8b25c2009-02-02 16:50:45 -0700172 else
173 glVertex2fv(VertCoords[i]);
174 }
Brian Paul49789532008-05-20 11:01:17 -0600175
176 glEnd();
Brian Paul49789532008-05-20 11:01:17 -0600177}
178
179
180static void
181draw(void)
182{
183 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
184
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);
190
Brian Paul8e8b25c2009-02-02 16:50:45 -0700191 if (UseArrays)
192 DrawPolygonArray();
193 else
194 DrawPolygonVert();
Brian Paul49789532008-05-20 11:01:17 -0600195
196 glPopMatrix();
197
198 glutSwapBuffers();
199}
200
201
202static void
203idle(void)
204{
205 GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
206 Yrot = t;
207 glutPostRedisplay();
208}
209
210
211static void
212key(unsigned char k, int x, int y)
213{
214 (void) x;
215 (void) y;
216 switch (k) {
Brian Paul49789532008-05-20 11:01:17 -0600217 case 'a':
Brian Paul8e8b25c2009-02-02 16:50:45 -0700218 UseArrays = !UseArrays;
219 printf("Arrays: %d\n", UseArrays);
220 break;
Brian Paul891a2bd2009-05-22 13:12:28 -0600221 case 'v':
222 UseVBO = !UseVBO;
223 printf("Use VBO: %d\n", UseVBO);
224 break;
Brian Paul8e8b25c2009-02-02 16:50:45 -0700225 case ' ':
Brian Paul49789532008-05-20 11:01:17 -0600226 Anim = !Anim;
227 if (Anim)
228 glutIdleFunc(idle);
229 else
230 glutIdleFunc(NULL);
231 break;
232 case 'z':
233 EyeDist -= 0.5;
234 if (EyeDist < 3.0)
235 EyeDist = 3.0;
236 break;
237 case 'Z':
238 EyeDist += 0.5;
239 if (EyeDist > 90.0)
240 EyeDist = 90;
241 break;
242 case 27:
243 exit(0);
244 }
245 glutPostRedisplay();
246}
247
248
249static void
250specialkey(int key, int x, int y)
251{
252 GLfloat step = 2.0;
253 (void) x;
254 (void) y;
255 switch (key) {
256 case GLUT_KEY_UP:
257 Xrot += step;
258 break;
259 case GLUT_KEY_DOWN:
260 Xrot -= step;
261 break;
262 case GLUT_KEY_LEFT:
263 Yrot -= step;
264 break;
265 case GLUT_KEY_RIGHT:
266 Yrot += step;
267 break;
268 }
269 glutPostRedisplay();
270}
271
272
273/* new window size or exposure */
274static void
275Reshape(int width, int height)
276{
277 GLfloat ar = (float) width / (float) height;
278 glViewport(0, 0, (GLint)width, (GLint)height);
279 glMatrixMode(GL_PROJECTION);
280 glLoadIdentity();
281 glFrustum(-2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0);
282 glMatrixMode(GL_MODELVIEW);
283 glLoadIdentity();
284}
285
286
287static void
288InitTextures(void)
289{
290 GLenum filter = GL_LINEAR;
291 int i;
292
293 for (i = 0; i < 2; i++) {
294 GLint imgWidth, imgHeight;
295 GLenum imgFormat;
296 GLubyte *image = NULL;
297
298 image = LoadRGBImage(TexFiles[i], &imgWidth, &imgHeight, &imgFormat);
299 if (!image) {
300 printf("Couldn't read %s\n", TexFiles[i]);
301 exit(0);
302 }
303
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);
308 free(image);
309
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);
314 }
315}
316
317
318static GLuint
319CreateProgram(const char *vertProgFile, const char *fragProgFile,
320 struct uniform_info *uniforms)
321{
322 GLuint fragShader, vertShader, program;
323
324 vertShader = CompileShaderFile(GL_VERTEX_SHADER, vertProgFile);
325 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, fragProgFile);
326 assert(vertShader);
327 program = LinkShaders(vertShader, fragShader);
328
Brian Paulee0b1bc2009-07-17 13:23:11 -0600329 glUseProgram(program);
Brian Paul49789532008-05-20 11:01:17 -0600330
331 InitUniforms(program, uniforms);
332
Brian Paulee0b1bc2009-07-17 13:23:11 -0600333 VertCoord_attr = glGetAttribLocation(program, "VertCoord");
Brian Paul891a2bd2009-05-22 13:12:28 -0600334 if (VertCoord_attr > 0) {
335 /* We want the VertCoord attrib to have position zero so that
336 * the call to glVertexAttrib(0, xyz) triggers vertex processing.
337 * Otherwise, if TexCoord0 or TexCoord1 gets position 0 we'd have
338 * to set that attribute last (which is a PITA to manage).
339 */
Brian Paulee0b1bc2009-07-17 13:23:11 -0600340 glBindAttribLocation(program, 0, "VertCoord");
Brian Paul891a2bd2009-05-22 13:12:28 -0600341 /* re-link */
Brian Paulee0b1bc2009-07-17 13:23:11 -0600342 glLinkProgram(program);
Brian Paul891a2bd2009-05-22 13:12:28 -0600343 /* VertCoord_attr should be zero now */
Brian Paulee0b1bc2009-07-17 13:23:11 -0600344 VertCoord_attr = glGetAttribLocation(program, "VertCoord");
Brian Paul891a2bd2009-05-22 13:12:28 -0600345 assert(VertCoord_attr == 0);
346 }
347
Brian Paulee0b1bc2009-07-17 13:23:11 -0600348 TexCoord0_attr = glGetAttribLocation(program, "TexCoord0");
349 TexCoord1_attr = glGetAttribLocation(program, "TexCoord1");
Brian Paul891a2bd2009-05-22 13:12:28 -0600350
Brian Paul8e8b25c2009-02-02 16:50:45 -0700351 printf("TexCoord0_attr = %d\n", TexCoord0_attr);
352 printf("TexCoord1_attr = %d\n", TexCoord1_attr);
353 printf("VertCoord_attr = %d\n", VertCoord_attr);
354
Brian Paul49789532008-05-20 11:01:17 -0600355 return program;
356}
357
358
359static void
360InitPrograms(void)
361{
362 Program = CreateProgram(VertFile, FragFile, Uniforms);
363}
364
365
366static void
367InitGL(void)
368{
369 const char *version = (const char *) glGetString(GL_VERSION);
370
371 if (version[0] != '2' || version[1] != '.') {
372 printf("Warning: this program expects OpenGL 2.0\n");
373 /*exit(1);*/
374 }
375 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
Brian Paul891a2bd2009-05-22 13:12:28 -0600376 printf("Usage:\n");
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");
Brian Paul49789532008-05-20 11:01:17 -0600382
383 InitTextures();
384 InitPrograms();
385
Brian Paul891a2bd2009-05-22 13:12:28 -0600386 SetupVertexBuffer();
387
Brian Paul49789532008-05-20 11:01:17 -0600388 glEnable(GL_DEPTH_TEST);
389
390 glClearColor(.6, .6, .9, 0);
391 glColor3f(1.0, 1.0, 1.0);
392}
393
394
395int
396main(int argc, char *argv[])
397{
398 glutInit(&argc, argv);
399 glutInitWindowSize(500, 400);
400 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
401 glutCreateWindow(Demo);
Keith Whitwellb799af92009-06-29 14:13:58 +0100402 glewInit();
Brian Paul49789532008-05-20 11:01:17 -0600403 glutReshapeFunc(Reshape);
404 glutKeyboardFunc(key);
405 glutSpecialFunc(specialkey);
406 glutDisplayFunc(draw);
407 if (Anim)
408 glutIdleFunc(idle);
409 InitGL();
410 glutMainLoop();
411 return 0;
412}