1 /* $Id: multipal.c,v 1.6 2003/12/08 09:03:36 joukj Exp $ */
4 * Test multitexture and paletted textures.
13 # include <stddef.h> /* for ptrdiff_t, referenced by GL.h when GL_GLEXT_LEGACY defined */
15 # include <malloc.h> /* for ptrdiff_t, referenced by GL.h when GL_GLEXT_LEGACY defined */
20 #define GL_GLEXT_LEGACY
23 #include "../util/readtex.c" /* I know, this is a hack. */
25 #define TEXTURE_1_FILE "../images/tile.rgb"
26 #define TEXTURE_2_FILE "../images/reflect.rgb"
34 static GLboolean Animate
= GL_TRUE
;
36 static GLfloat Drift
= 0.0;
37 static GLfloat Xrot
= 20.0, Yrot
= 30.0, Zrot
= 0.0;
41 static void Idle( void )
48 #ifdef GL_ARB_multitexture
49 glActiveTextureARB(GL_TEXTURE0_ARB
);
51 glMatrixMode(GL_TEXTURE
);
53 glTranslatef(Drift
, 0.0, 0.0);
54 glMatrixMode(GL_MODELVIEW
);
56 #ifdef GL_ARB_multitexture
57 glActiveTextureARB(GL_TEXTURE1_ARB
);
59 glMatrixMode(GL_TEXTURE
);
61 glTranslatef(0.0, Drift
, 0.0);
62 glMatrixMode(GL_MODELVIEW
);
69 static void DrawObject(void)
73 #ifdef GL_ARB_multitexture
74 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 0.0, 0.0);
75 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 0.0, 0.0);
76 glVertex2f(-1.0, -1.0);
78 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 2.0, 0.0);
79 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 1.0, 0.0);
80 glVertex2f(1.0, -1.0);
82 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 2.0, 2.0);
83 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 1.0, 1.0);
86 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 0.0, 2.0);
87 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 0.0, 1.0);
88 glVertex2f(-1.0, 1.0);
90 glTexCoord2f(0.0, 0.0);
91 glVertex2f(-1.0, -1.0);
93 glTexCoord2f(1.0, 0.0);
94 glVertex2f(1.0, -1.0);
96 glTexCoord2f(1.0, 1.0);
99 glTexCoord2f(0.0, 1.0);
100 glVertex2f(-1.0, 1.0);
108 static void Display( void )
110 glClear( GL_COLOR_BUFFER_BIT
);
113 glRotatef(Xrot
, 1.0, 0.0, 0.0);
114 glRotatef(Yrot
, 0.0, 1.0, 0.0);
115 glRotatef(Zrot
, 0.0, 0.0, 1.0);
116 glScalef(5.0, 5.0, 5.0);
124 static void Reshape( int width
, int height
)
126 glViewport( 0, 0, width
, height
);
127 glMatrixMode( GL_PROJECTION
);
129 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
130 /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
131 glMatrixMode( GL_MODELVIEW
);
133 glTranslatef( 0.0, 0.0, -70.0 );
137 static void ModeMenu(int entry
)
139 GLboolean enable0
= GL_FALSE
, enable1
= GL_FALSE
;
143 else if (entry
==TEX1
) {
146 else if (entry
==TEXBOTH
) {
150 else if (entry
==ANIMATE
) {
153 else if (entry
==QUIT
) {
157 if (entry
!= ANIMATE
) {
158 #ifdef GL_ARB_multitexture
159 glActiveTextureARB(GL_TEXTURE0_ARB
);
162 glEnable(GL_TEXTURE_2D
);
165 glDisable(GL_TEXTURE_2D
);
167 #ifdef GL_ARB_multitexture
168 glActiveTextureARB(GL_TEXTURE1_ARB
);
171 glEnable(GL_TEXTURE_2D
);
174 glDisable(GL_TEXTURE_2D
);
181 static void Key( unsigned char key
, int x
, int y
)
194 static void SpecialKey( int key
, int x
, int y
)
218 static void load_tex(const char *fname
, int channel
)
225 GLubyte table
[256][4];
227 image
= LoadRGBImage(fname
, &w
, &h
, &format
);
231 printf("%s %d x %d\n", fname
, w
, h
);
232 grayImage
= malloc(w
* h
* 1);
234 for (i
= 0; i
< w
* h
; i
++) {
235 int g
= (image
[i
*3+0] + image
[i
*3+1] + image
[i
*3+2]) / 3;
240 glTexImage2D(GL_TEXTURE_2D
, 0, GL_COLOR_INDEX
, w
, h
, 0, GL_COLOR_INDEX
,
241 GL_UNSIGNED_BYTE
, grayImage
);
243 for (i
= 0; i
< 256; i
++) {
244 table
[i
][0] = channel
? i
: 0;
246 table
[i
][2] = channel
? 0 : i
;
250 glColorTableEXT(GL_TEXTURE_2D
, /* target */
251 GL_RGBA
, /* internal format */
252 256, /* table size */
253 GL_RGBA
, /* table format */
254 GL_UNSIGNED_BYTE
, /* table type */
255 table
); /* the color table */
263 static void Init( int argc
, char *argv
[] )
268 if (!glutExtensionSupported("GL_ARB_multitexture")) {
269 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
272 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
273 printf("Sorry, GL_EXT_paletted_texture not supported by this renderer.\n");
277 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB
, &units
);
278 printf("%d texture units supported\n", units
);
280 /* allocate two texture objects */
281 glGenTextures(2, texObj
);
283 /* setup texture obj 0 */
284 glBindTexture(GL_TEXTURE_2D
, texObj
[0]);
286 /* linear filtering looks much nicer but is much slower for Mesa */
287 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
288 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
291 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
292 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
295 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
297 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
299 load_tex(TEXTURE_1_FILE
, 0);
301 if (!LoadRGBMipmaps(TEXTURE_1_FILE
, GL_RGB
)) {
302 printf("Error: couldn't load texture image\n");
307 /* setup texture obj 1 */
308 glBindTexture(GL_TEXTURE_2D
, texObj
[1]);
310 /* linear filtering looks much nicer but is much slower for Mesa */
311 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
312 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
315 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
316 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
319 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
321 load_tex(TEXTURE_2_FILE
, 1);
323 if (!LoadRGBMipmaps(TEXTURE_2_FILE
, GL_RGB
)) {
324 printf("Error: couldn't load texture image\n");
329 /* now bind the texture objects to the respective texture units */
330 #ifdef GL_ARB_multitexture
331 glActiveTextureARB(GL_TEXTURE0_ARB
);
332 glBindTexture(GL_TEXTURE_2D
, texObj
[0]);
333 glActiveTextureARB(GL_TEXTURE1_ARB
);
334 glBindTexture(GL_TEXTURE_2D
, texObj
[1]);
337 glShadeModel(GL_FLAT
);
338 glClearColor(0.3, 0.3, 0.4, 1.0);
342 if (argc
> 1 && strcmp(argv
[1], "-info")==0) {
343 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
344 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
345 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
346 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
351 int main( int argc
, char *argv
[] )
353 glutInit( &argc
, argv
);
354 glutInitWindowSize( 300, 300 );
355 glutInitWindowPosition( 0, 0 );
356 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
357 glutCreateWindow(argv
[0] );
361 glutReshapeFunc( Reshape
);
362 glutKeyboardFunc( Key
);
363 glutSpecialFunc( SpecialKey
);
364 glutDisplayFunc( Display
);
365 glutIdleFunc( Idle
);
367 glutCreateMenu(ModeMenu
);
368 glutAddMenuEntry("Texture 0", TEX0
);
369 glutAddMenuEntry("Texture 1", TEX1
);
370 glutAddMenuEntry("Multi-texture", TEXBOTH
);
371 glutAddMenuEntry("Toggle Animation", ANIMATE
);
372 glutAddMenuEntry("Quit", QUIT
);
373 glutAttachMenu(GLUT_RIGHT_BUTTON
);