now supports > 2 texture units
[mesa.git] / progs / tests / multipal.c
1 /* $Id: multipal.c,v 1.2 2002/01/16 01:03:25 kschultz Exp $ */
2
3 /*
4 * GL_ARB_multitexture demo
5 *
6 * Command line options:
7 * -info print GL implementation information
8 *
9 *
10 * Brian Paul November 1998 This program is in the public domain.
11 */
12
13 /*
14 * $Log: multipal.c,v $
15 * Revision 1.2 2002/01/16 01:03:25 kschultz
16 * get tests working on windows (Robert Bergkvist)
17 *
18 * Revision 1.1 2000/11/18 17:12:33 brianp
19 * test texture palettes with multitexture
20 *
21 * Revision 1.7 2000/11/01 16:02:01 brianp
22 * print number of texture units
23 *
24 * Revision 1.6 2000/05/23 23:21:00 brianp
25 * set default window pos
26 *
27 * Revision 1.5 2000/02/02 17:31:45 brianp
28 * changed > to >=
29 *
30 * Revision 1.4 2000/02/02 01:07:21 brianp
31 * limit Drift to [0, 1]
32 *
33 * Revision 1.3 1999/10/21 16:40:32 brianp
34 * added -info command line option
35 *
36 * Revision 1.2 1999/10/13 12:02:13 brianp
37 * use texture objects now
38 *
39 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
40 * Imported sources
41 *
42 * Revision 1.3 1999/03/28 18:20:49 brianp
43 * minor clean-up
44 *
45 * Revision 1.2 1998/11/05 04:34:04 brianp
46 * moved image files to ../images/ directory
47 *
48 * Revision 1.1 1998/11/03 01:36:33 brianp
49 * Initial revision
50 *
51 */
52
53 #include <assert.h>
54 #include <math.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #ifdef _WIN32
59 #include <windows.h>
60 #endif
61 #define GL_GLEXT_LEGACY
62 #include <GL/glut.h>
63
64 #include "../util/readtex.c" /* I know, this is a hack. */
65
66 #define TEXTURE_1_FILE "../images/tile.rgb"
67 #define TEXTURE_2_FILE "../images/reflect.rgb"
68
69 #define TEX0 1
70 #define TEX1 2
71 #define TEXBOTH 3
72 #define ANIMATE 10
73 #define QUIT 100
74
75 static GLboolean Animate = GL_TRUE;
76
77 static GLfloat Drift = 0.0;
78 static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
79
80
81
82 static void Idle( void )
83 {
84 if (Animate) {
85 Drift += 0.05;
86 if (Drift >= 1.0)
87 Drift = 0.0;
88
89 #ifdef GL_ARB_multitexture
90 glActiveTextureARB(GL_TEXTURE0_ARB);
91 #endif
92 glMatrixMode(GL_TEXTURE);
93 glLoadIdentity();
94 glTranslatef(Drift, 0.0, 0.0);
95 glMatrixMode(GL_MODELVIEW);
96
97 #ifdef GL_ARB_multitexture
98 glActiveTextureARB(GL_TEXTURE1_ARB);
99 #endif
100 glMatrixMode(GL_TEXTURE);
101 glLoadIdentity();
102 glTranslatef(0.0, Drift, 0.0);
103 glMatrixMode(GL_MODELVIEW);
104
105 glutPostRedisplay();
106 }
107 }
108
109
110 static void DrawObject(void)
111 {
112 glBegin(GL_QUADS);
113
114 #ifdef GL_ARB_multitexture
115 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
116 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
117 glVertex2f(-1.0, -1.0);
118
119 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 0.0);
120 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
121 glVertex2f(1.0, -1.0);
122
123 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 2.0, 2.0);
124 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
125 glVertex2f(1.0, 1.0);
126
127 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 2.0);
128 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
129 glVertex2f(-1.0, 1.0);
130 #else
131 glTexCoord2f(0.0, 0.0);
132 glVertex2f(-1.0, -1.0);
133
134 glTexCoord2f(1.0, 0.0);
135 glVertex2f(1.0, -1.0);
136
137 glTexCoord2f(1.0, 1.0);
138 glVertex2f(1.0, 1.0);
139
140 glTexCoord2f(0.0, 1.0);
141 glVertex2f(-1.0, 1.0);
142 #endif
143
144 glEnd();
145 }
146
147
148
149 static void Display( void )
150 {
151 glClear( GL_COLOR_BUFFER_BIT );
152
153 glPushMatrix();
154 glRotatef(Xrot, 1.0, 0.0, 0.0);
155 glRotatef(Yrot, 0.0, 1.0, 0.0);
156 glRotatef(Zrot, 0.0, 0.0, 1.0);
157 glScalef(5.0, 5.0, 5.0);
158 DrawObject();
159 glPopMatrix();
160
161 glutSwapBuffers();
162 }
163
164
165 static void Reshape( int width, int height )
166 {
167 glViewport( 0, 0, width, height );
168 glMatrixMode( GL_PROJECTION );
169 glLoadIdentity();
170 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
171 /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
172 glMatrixMode( GL_MODELVIEW );
173 glLoadIdentity();
174 glTranslatef( 0.0, 0.0, -70.0 );
175 }
176
177
178 static void ModeMenu(int entry)
179 {
180 GLboolean enable0 = GL_FALSE, enable1 = GL_FALSE;
181 if (entry==TEX0) {
182 enable0 = GL_TRUE;
183 }
184 else if (entry==TEX1) {
185 enable1 = GL_TRUE;
186 }
187 else if (entry==TEXBOTH) {
188 enable0 = GL_TRUE;
189 enable1 = GL_TRUE;
190 }
191 else if (entry==ANIMATE) {
192 Animate = !Animate;
193 }
194 else if (entry==QUIT) {
195 exit(0);
196 }
197
198 if (entry != ANIMATE) {
199 #ifdef GL_ARB_multitexture
200 glActiveTextureARB(GL_TEXTURE0_ARB);
201 #endif
202 if (enable0) {
203 glEnable(GL_TEXTURE_2D);
204 }
205 else
206 glDisable(GL_TEXTURE_2D);
207
208 #ifdef GL_ARB_multitexture
209 glActiveTextureARB(GL_TEXTURE1_ARB);
210 #endif
211 if (enable1) {
212 glEnable(GL_TEXTURE_2D);
213 }
214 else
215 glDisable(GL_TEXTURE_2D);
216 }
217
218 glutPostRedisplay();
219 }
220
221
222 static void Key( unsigned char key, int x, int y )
223 {
224 (void) x;
225 (void) y;
226 switch (key) {
227 case 27:
228 exit(0);
229 break;
230 }
231 glutPostRedisplay();
232 }
233
234
235 static void SpecialKey( int key, int x, int y )
236 {
237 float step = 3.0;
238 (void) x;
239 (void) y;
240
241 switch (key) {
242 case GLUT_KEY_UP:
243 Xrot += step;
244 break;
245 case GLUT_KEY_DOWN:
246 Xrot -= step;
247 break;
248 case GLUT_KEY_LEFT:
249 Yrot += step;
250 break;
251 case GLUT_KEY_RIGHT:
252 Yrot -= step;
253 break;
254 }
255 glutPostRedisplay();
256 }
257
258
259 static void load_tex(const char *fname, int channel)
260 {
261 GLubyte *image;
262 GLenum format, type;
263 GLint w, h;
264 GLubyte *grayImage;
265 int i;
266 GLubyte table[256][4];
267
268 image = LoadRGBImage(fname, &w, &h, &format);
269 if (!image)
270 exit(1);
271
272 printf("%s %d x %d\n", fname, w, h);
273 grayImage = malloc(w * h * 1);
274 assert(grayImage);
275 for (i = 0; i < w * h; i++) {
276 int g = (image[i*3+0] + image[i*3+1] + image[i*3+2]) / 3;
277 assert(g < 256);
278 grayImage[i] = g;
279 }
280
281 glTexImage2D(GL_TEXTURE_2D, 0, GL_COLOR_INDEX, w, h, 0, GL_COLOR_INDEX,
282 GL_UNSIGNED_BYTE, grayImage);
283
284 for (i = 0; i < 256; i++) {
285 table[i][0] = channel ? i : 0;
286 table[i][1] = i;
287 table[i][2] = channel ? 0 : i;
288 table[i][3] = 255;
289 }
290
291 glColorTableEXT(GL_TEXTURE_2D, /* target */
292 GL_RGBA, /* internal format */
293 256, /* table size */
294 GL_RGBA, /* table format */
295 GL_UNSIGNED_BYTE, /* table type */
296 table); /* the color table */
297
298 free(grayImage);
299 free(image);
300 }
301
302
303
304 static void Init( int argc, char *argv[] )
305 {
306 GLuint texObj[2];
307 GLint units;
308
309 const char *exten = (const char *) glGetString(GL_EXTENSIONS);
310 if (!strstr(exten, "GL_ARB_multitexture")) {
311 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
312 exit(1);
313 }
314
315 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &units);
316 printf("%d texture units supported\n", units);
317
318 /* allocate two texture objects */
319 glGenTextures(2, texObj);
320
321 /* setup texture obj 0 */
322 glBindTexture(GL_TEXTURE_2D, texObj[0]);
323 #ifdef LINEAR_FILTER
324 /* linear filtering looks much nicer but is much slower for Mesa */
325 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
326 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
327 foo
328 #else
329 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
330 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
331 #endif
332
333 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
334
335 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
336
337 load_tex(TEXTURE_1_FILE, 0);
338 #if 0
339 if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
340 printf("Error: couldn't load texture image\n");
341 exit(1);
342 }
343 #endif
344
345 /* setup texture obj 1 */
346 glBindTexture(GL_TEXTURE_2D, texObj[1]);
347 #ifdef LINEAR_FILTER
348 /* linear filtering looks much nicer but is much slower for Mesa */
349 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
350 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
351 foo
352 #else
353 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
354 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
355 #endif
356
357 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
358
359 load_tex(TEXTURE_2_FILE, 1);
360 #if 0
361 if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
362 printf("Error: couldn't load texture image\n");
363 exit(1);
364 }
365 #endif
366
367 /* now bind the texture objects to the respective texture units */
368 #ifdef GL_ARB_multitexture
369 glActiveTextureARB(GL_TEXTURE0_ARB);
370 glBindTexture(GL_TEXTURE_2D, texObj[0]);
371 glActiveTextureARB(GL_TEXTURE1_ARB);
372 glBindTexture(GL_TEXTURE_2D, texObj[1]);
373 #endif
374
375 glShadeModel(GL_FLAT);
376 glClearColor(0.3, 0.3, 0.4, 1.0);
377
378 ModeMenu(TEXBOTH);
379
380 if (argc > 1 && strcmp(argv[1], "-info")==0) {
381 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
382 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
383 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
384 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
385 }
386 }
387
388
389 int main( int argc, char *argv[] )
390 {
391 glutInit( &argc, argv );
392 glutInitWindowSize( 300, 300 );
393 glutInitWindowPosition( 0, 0 );
394 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
395 glutCreateWindow(argv[0] );
396
397 Init( argc, argv );
398
399 glutReshapeFunc( Reshape );
400 glutKeyboardFunc( Key );
401 glutSpecialFunc( SpecialKey );
402 glutDisplayFunc( Display );
403 glutIdleFunc( Idle );
404
405 glutCreateMenu(ModeMenu);
406 glutAddMenuEntry("Texture 0", TEX0);
407 glutAddMenuEntry("Texture 1", TEX1);
408 glutAddMenuEntry("Multi-texture", TEXBOTH);
409 glutAddMenuEntry("Toggle Animation", ANIMATE);
410 glutAddMenuEntry("Quit", QUIT);
411 glutAttachMenu(GLUT_RIGHT_BUTTON);
412
413 glutMainLoop();
414 return 0;
415 }