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