1 /* $Id: multipal.c,v 1.2 2002/01/16 01:03:25 kschultz Exp $ */
4 * GL_ARB_multitexture demo
6 * Command line options:
7 * -info print GL implementation information
10 * Brian Paul November 1998 This program is in the public domain.
14 * $Log: multipal.c,v $
15 * Revision 1.2 2002/01/16 01:03:25 kschultz
16 * get tests working on windows (Robert Bergkvist)
18 * Revision 1.1 2000/11/18 17:12:33 brianp
19 * test texture palettes with multitexture
21 * Revision 1.7 2000/11/01 16:02:01 brianp
22 * print number of texture units
24 * Revision 1.6 2000/05/23 23:21:00 brianp
25 * set default window pos
27 * Revision 1.5 2000/02/02 17:31:45 brianp
30 * Revision 1.4 2000/02/02 01:07:21 brianp
31 * limit Drift to [0, 1]
33 * Revision 1.3 1999/10/21 16:40:32 brianp
34 * added -info command line option
36 * Revision 1.2 1999/10/13 12:02:13 brianp
37 * use texture objects now
39 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
42 * Revision 1.3 1999/03/28 18:20:49 brianp
45 * Revision 1.2 1998/11/05 04:34:04 brianp
46 * moved image files to ../images/ directory
48 * Revision 1.1 1998/11/03 01:36:33 brianp
61 #define GL_GLEXT_LEGACY
64 #include "../util/readtex.c" /* I know, this is a hack. */
66 #define TEXTURE_1_FILE "../images/tile.rgb"
67 #define TEXTURE_2_FILE "../images/reflect.rgb"
75 static GLboolean Animate
= GL_TRUE
;
77 static GLfloat Drift
= 0.0;
78 static GLfloat Xrot
= 20.0, Yrot
= 30.0, Zrot
= 0.0;
82 static void Idle( void )
89 #ifdef GL_ARB_multitexture
90 glActiveTextureARB(GL_TEXTURE0_ARB
);
92 glMatrixMode(GL_TEXTURE
);
94 glTranslatef(Drift
, 0.0, 0.0);
95 glMatrixMode(GL_MODELVIEW
);
97 #ifdef GL_ARB_multitexture
98 glActiveTextureARB(GL_TEXTURE1_ARB
);
100 glMatrixMode(GL_TEXTURE
);
102 glTranslatef(0.0, Drift
, 0.0);
103 glMatrixMode(GL_MODELVIEW
);
110 static void DrawObject(void)
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);
119 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 2.0, 0.0);
120 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 1.0, 0.0);
121 glVertex2f(1.0, -1.0);
123 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 2.0, 2.0);
124 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 1.0, 1.0);
125 glVertex2f(1.0, 1.0);
127 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 0.0, 2.0);
128 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 0.0, 1.0);
129 glVertex2f(-1.0, 1.0);
131 glTexCoord2f(0.0, 0.0);
132 glVertex2f(-1.0, -1.0);
134 glTexCoord2f(1.0, 0.0);
135 glVertex2f(1.0, -1.0);
137 glTexCoord2f(1.0, 1.0);
138 glVertex2f(1.0, 1.0);
140 glTexCoord2f(0.0, 1.0);
141 glVertex2f(-1.0, 1.0);
149 static void Display( void )
151 glClear( GL_COLOR_BUFFER_BIT
);
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);
165 static void Reshape( int width
, int height
)
167 glViewport( 0, 0, width
, height
);
168 glMatrixMode( GL_PROJECTION
);
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
);
174 glTranslatef( 0.0, 0.0, -70.0 );
178 static void ModeMenu(int entry
)
180 GLboolean enable0
= GL_FALSE
, enable1
= GL_FALSE
;
184 else if (entry
==TEX1
) {
187 else if (entry
==TEXBOTH
) {
191 else if (entry
==ANIMATE
) {
194 else if (entry
==QUIT
) {
198 if (entry
!= ANIMATE
) {
199 #ifdef GL_ARB_multitexture
200 glActiveTextureARB(GL_TEXTURE0_ARB
);
203 glEnable(GL_TEXTURE_2D
);
206 glDisable(GL_TEXTURE_2D
);
208 #ifdef GL_ARB_multitexture
209 glActiveTextureARB(GL_TEXTURE1_ARB
);
212 glEnable(GL_TEXTURE_2D
);
215 glDisable(GL_TEXTURE_2D
);
222 static void Key( unsigned char key
, int x
, int y
)
235 static void SpecialKey( int key
, int x
, int y
)
259 static void load_tex(const char *fname
, int channel
)
266 GLubyte table
[256][4];
268 image
= LoadRGBImage(fname
, &w
, &h
, &format
);
272 printf("%s %d x %d\n", fname
, w
, h
);
273 grayImage
= malloc(w
* h
* 1);
275 for (i
= 0; i
< w
* h
; i
++) {
276 int g
= (image
[i
*3+0] + image
[i
*3+1] + image
[i
*3+2]) / 3;
281 glTexImage2D(GL_TEXTURE_2D
, 0, GL_COLOR_INDEX
, w
, h
, 0, GL_COLOR_INDEX
,
282 GL_UNSIGNED_BYTE
, grayImage
);
284 for (i
= 0; i
< 256; i
++) {
285 table
[i
][0] = channel
? i
: 0;
287 table
[i
][2] = channel
? 0 : i
;
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 */
304 static void Init( int argc
, char *argv
[] )
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");
315 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB
, &units
);
316 printf("%d texture units supported\n", units
);
318 /* allocate two texture objects */
319 glGenTextures(2, texObj
);
321 /* setup texture obj 0 */
322 glBindTexture(GL_TEXTURE_2D
, texObj
[0]);
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
);
329 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
330 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
333 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
335 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
337 load_tex(TEXTURE_1_FILE
, 0);
339 if (!LoadRGBMipmaps(TEXTURE_1_FILE
, GL_RGB
)) {
340 printf("Error: couldn't load texture image\n");
345 /* setup texture obj 1 */
346 glBindTexture(GL_TEXTURE_2D
, texObj
[1]);
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
);
353 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
354 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
357 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
359 load_tex(TEXTURE_2_FILE
, 1);
361 if (!LoadRGBMipmaps(TEXTURE_2_FILE
, GL_RGB
)) {
362 printf("Error: couldn't load texture image\n");
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]);
375 glShadeModel(GL_FLAT
);
376 glClearColor(0.3, 0.3, 0.4, 1.0);
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
));
389 int main( int argc
, char *argv
[] )
391 glutInit( &argc
, argv
);
392 glutInitWindowSize( 300, 300 );
393 glutInitWindowPosition( 0, 0 );
394 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
395 glutCreateWindow(argv
[0] );
399 glutReshapeFunc( Reshape
);
400 glutKeyboardFunc( Key
);
401 glutSpecialFunc( SpecialKey
);
402 glutDisplayFunc( Display
);
403 glutIdleFunc( Idle
);
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
);