1 /* $Id: multipal.c,v 1.1 2000/11/18 17:12:33 brianp 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.1 2000/11/18 17:12:33 brianp
16 * test texture palettes with multitexture
18 * Revision 1.7 2000/11/01 16:02:01 brianp
19 * print number of texture units
21 * Revision 1.6 2000/05/23 23:21:00 brianp
22 * set default window pos
24 * Revision 1.5 2000/02/02 17:31:45 brianp
27 * Revision 1.4 2000/02/02 01:07:21 brianp
28 * limit Drift to [0, 1]
30 * Revision 1.3 1999/10/21 16:40:32 brianp
31 * added -info command line option
33 * Revision 1.2 1999/10/13 12:02:13 brianp
34 * use texture objects now
36 * Revision 1.1.1.1 1999/08/19 00:55:40 jtg
39 * Revision 1.3 1999/03/28 18:20:49 brianp
42 * Revision 1.2 1998/11/05 04:34:04 brianp
43 * moved image files to ../images/ directory
45 * Revision 1.1 1998/11/03 01:36:33 brianp
57 #include "../util/readtex.c" /* I know, this is a hack. */
59 #define TEXTURE_1_FILE "../images/tile.rgb"
60 #define TEXTURE_2_FILE "../images/reflect.rgb"
68 static GLboolean Animate
= GL_TRUE
;
70 static GLfloat Drift
= 0.0;
71 static GLfloat Xrot
= 20.0, Yrot
= 30.0, Zrot
= 0.0;
75 static void Idle( void )
82 #ifdef GL_ARB_multitexture
83 glActiveTextureARB(GL_TEXTURE0_ARB
);
85 glMatrixMode(GL_TEXTURE
);
87 glTranslatef(Drift
, 0.0, 0.0);
88 glMatrixMode(GL_MODELVIEW
);
90 #ifdef GL_ARB_multitexture
91 glActiveTextureARB(GL_TEXTURE1_ARB
);
93 glMatrixMode(GL_TEXTURE
);
95 glTranslatef(0.0, Drift
, 0.0);
96 glMatrixMode(GL_MODELVIEW
);
103 static void DrawObject(void)
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);
112 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 2.0, 0.0);
113 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 1.0, 0.0);
114 glVertex2f(1.0, -1.0);
116 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 2.0, 2.0);
117 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 1.0, 1.0);
118 glVertex2f(1.0, 1.0);
120 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 0.0, 2.0);
121 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 0.0, 1.0);
122 glVertex2f(-1.0, 1.0);
124 glTexCoord2f(0.0, 0.0);
125 glVertex2f(-1.0, -1.0);
127 glTexCoord2f(1.0, 0.0);
128 glVertex2f(1.0, -1.0);
130 glTexCoord2f(1.0, 1.0);
131 glVertex2f(1.0, 1.0);
133 glTexCoord2f(0.0, 1.0);
134 glVertex2f(-1.0, 1.0);
142 static void Display( void )
144 glClear( GL_COLOR_BUFFER_BIT
);
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);
158 static void Reshape( int width
, int height
)
160 glViewport( 0, 0, width
, height
);
161 glMatrixMode( GL_PROJECTION
);
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
);
167 glTranslatef( 0.0, 0.0, -70.0 );
171 static void ModeMenu(int entry
)
173 GLboolean enable0
= GL_FALSE
, enable1
= GL_FALSE
;
177 else if (entry
==TEX1
) {
180 else if (entry
==TEXBOTH
) {
184 else if (entry
==ANIMATE
) {
187 else if (entry
==QUIT
) {
191 if (entry
!= ANIMATE
) {
192 #ifdef GL_ARB_multitexture
193 glActiveTextureARB(GL_TEXTURE0_ARB
);
196 glEnable(GL_TEXTURE_2D
);
199 glDisable(GL_TEXTURE_2D
);
201 #ifdef GL_ARB_multitexture
202 glActiveTextureARB(GL_TEXTURE1_ARB
);
205 glEnable(GL_TEXTURE_2D
);
208 glDisable(GL_TEXTURE_2D
);
215 static void Key( unsigned char key
, int x
, int y
)
228 static void SpecialKey( int key
, int x
, int y
)
252 static void load_tex(const char *fname
, int channel
)
259 GLubyte table
[256][4];
261 image
= LoadRGBImage(fname
, &w
, &h
, &format
);
265 printf("%s %d x %d\n", fname
, w
, h
);
266 grayImage
= malloc(w
* h
* 1);
268 for (i
= 0; i
< w
* h
; i
++) {
269 int g
= (image
[i
*3+0] + image
[i
*3+1] + image
[i
*3+2]) / 3;
274 glTexImage2D(GL_TEXTURE_2D
, 0, GL_COLOR_INDEX
, w
, h
, 0, GL_COLOR_INDEX
,
275 GL_UNSIGNED_BYTE
, grayImage
);
277 for (i
= 0; i
< 256; i
++) {
278 table
[i
][0] = channel
? i
: 0;
280 table
[i
][2] = channel
? 0 : i
;
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 */
297 static void Init( int argc
, char *argv
[] )
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");
308 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB
, &units
);
309 printf("%d texture units supported\n", units
);
311 /* allocate two texture objects */
312 glGenTextures(2, texObj
);
314 /* setup texture obj 0 */
315 glBindTexture(GL_TEXTURE_2D
, texObj
[0]);
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
);
322 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
323 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
326 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
328 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
330 load_tex(TEXTURE_1_FILE
, 0);
332 if (!LoadRGBMipmaps(TEXTURE_1_FILE
, GL_RGB
)) {
333 printf("Error: couldn't load texture image\n");
338 /* setup texture obj 1 */
339 glBindTexture(GL_TEXTURE_2D
, texObj
[1]);
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
);
346 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
347 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
350 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
352 load_tex(TEXTURE_2_FILE
, 1);
354 if (!LoadRGBMipmaps(TEXTURE_2_FILE
, GL_RGB
)) {
355 printf("Error: couldn't load texture image\n");
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]);
368 glShadeModel(GL_FLAT
);
369 glClearColor(0.3, 0.3, 0.4, 1.0);
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
));
382 int main( int argc
, char *argv
[] )
384 glutInit( &argc
, argv
);
385 glutInitWindowSize( 300, 300 );
386 glutInitWindowPosition( 0, 0 );
387 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
388 glutCreateWindow(argv
[0] );
392 glutReshapeFunc( Reshape
);
393 glutKeyboardFunc( Key
);
394 glutSpecialFunc( SpecialKey
);
395 glutDisplayFunc( Display
);
396 glutIdleFunc( Idle
);
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
);