3 * Test multitexture and paletted textures.
12 # include <stddef.h> /* for ptrdiff_t, referenced by GL.h when GL_GLEXT_LEGACY defined */
14 # include <malloc.h> /* for ptrdiff_t, referenced by GL.h when GL_GLEXT_LEGACY defined */
19 #define GL_GLEXT_LEGACY
22 #include "../util/readtex.c" /* I know, this is a hack. */
24 #define TEXTURE_1_FILE "../images/tile.rgb"
25 #define TEXTURE_2_FILE "../images/reflect.rgb"
33 static GLboolean Animate
= GL_TRUE
;
35 static GLfloat Drift
= 0.0;
36 static GLfloat Xrot
= 20.0, Yrot
= 30.0, Zrot
= 0.0;
40 static void Idle( void )
47 #ifdef GL_ARB_multitexture
48 glActiveTextureARB(GL_TEXTURE0_ARB
);
50 glMatrixMode(GL_TEXTURE
);
52 glTranslatef(Drift
, 0.0, 0.0);
53 glMatrixMode(GL_MODELVIEW
);
55 #ifdef GL_ARB_multitexture
56 glActiveTextureARB(GL_TEXTURE1_ARB
);
58 glMatrixMode(GL_TEXTURE
);
60 glTranslatef(0.0, Drift
, 0.0);
61 glMatrixMode(GL_MODELVIEW
);
68 static void DrawObject(void)
72 #ifdef GL_ARB_multitexture
73 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 0.0, 0.0);
74 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 0.0, 0.0);
75 glVertex2f(-1.0, -1.0);
77 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 2.0, 0.0);
78 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 1.0, 0.0);
79 glVertex2f(1.0, -1.0);
81 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 2.0, 2.0);
82 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 1.0, 1.0);
85 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 0.0, 2.0);
86 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 0.0, 1.0);
87 glVertex2f(-1.0, 1.0);
89 glTexCoord2f(0.0, 0.0);
90 glVertex2f(-1.0, -1.0);
92 glTexCoord2f(1.0, 0.0);
93 glVertex2f(1.0, -1.0);
95 glTexCoord2f(1.0, 1.0);
98 glTexCoord2f(0.0, 1.0);
99 glVertex2f(-1.0, 1.0);
107 static void Display( void )
109 glClear( GL_COLOR_BUFFER_BIT
);
112 glRotatef(Xrot
, 1.0, 0.0, 0.0);
113 glRotatef(Yrot
, 0.0, 1.0, 0.0);
114 glRotatef(Zrot
, 0.0, 0.0, 1.0);
115 glScalef(5.0, 5.0, 5.0);
123 static void Reshape( int width
, int height
)
125 glViewport( 0, 0, width
, height
);
126 glMatrixMode( GL_PROJECTION
);
128 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
129 /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
130 glMatrixMode( GL_MODELVIEW
);
132 glTranslatef( 0.0, 0.0, -70.0 );
136 static void ModeMenu(int entry
)
138 GLboolean enable0
= GL_FALSE
, enable1
= GL_FALSE
;
142 else if (entry
==TEX1
) {
145 else if (entry
==TEXBOTH
) {
149 else if (entry
==ANIMATE
) {
152 else if (entry
==QUIT
) {
156 if (entry
!= ANIMATE
) {
157 #ifdef GL_ARB_multitexture
158 glActiveTextureARB(GL_TEXTURE0_ARB
);
161 glEnable(GL_TEXTURE_2D
);
164 glDisable(GL_TEXTURE_2D
);
166 #ifdef GL_ARB_multitexture
167 glActiveTextureARB(GL_TEXTURE1_ARB
);
170 glEnable(GL_TEXTURE_2D
);
173 glDisable(GL_TEXTURE_2D
);
180 static void Key( unsigned char key
, int x
, int y
)
193 static void SpecialKey( int key
, int x
, int y
)
217 static void load_tex(const char *fname
, int channel
)
224 GLubyte table
[256][4];
226 image
= LoadRGBImage(fname
, &w
, &h
, &format
);
230 printf("%s %d x %d\n", fname
, w
, h
);
231 grayImage
= malloc(w
* h
* 1);
233 for (i
= 0; i
< w
* h
; i
++) {
234 int g
= (image
[i
*3+0] + image
[i
*3+1] + image
[i
*3+2]) / 3;
239 glTexImage2D(GL_TEXTURE_2D
, 0, GL_COLOR_INDEX
, w
, h
, 0, GL_COLOR_INDEX
,
240 GL_UNSIGNED_BYTE
, grayImage
);
242 for (i
= 0; i
< 256; i
++) {
243 table
[i
][0] = channel
? i
: 0;
245 table
[i
][2] = channel
? 0 : i
;
249 glColorTableEXT(GL_TEXTURE_2D
, /* target */
250 GL_RGBA
, /* internal format */
251 256, /* table size */
252 GL_RGBA
, /* table format */
253 GL_UNSIGNED_BYTE
, /* table type */
254 table
); /* the color table */
262 static void Init( int argc
, char *argv
[] )
267 if (!glutExtensionSupported("GL_ARB_multitexture")) {
268 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
271 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
272 printf("Sorry, GL_EXT_paletted_texture not supported by this renderer.\n");
276 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB
, &units
);
277 printf("%d texture units supported\n", units
);
279 /* allocate two texture objects */
280 glGenTextures(2, texObj
);
282 /* setup texture obj 0 */
283 glBindTexture(GL_TEXTURE_2D
, texObj
[0]);
285 /* linear filtering looks much nicer but is much slower for Mesa */
286 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
287 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
290 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
291 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
294 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
296 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
298 load_tex(TEXTURE_1_FILE
, 0);
300 if (!LoadRGBMipmaps(TEXTURE_1_FILE
, GL_RGB
)) {
301 printf("Error: couldn't load texture image\n");
306 /* setup texture obj 1 */
307 glBindTexture(GL_TEXTURE_2D
, texObj
[1]);
309 /* linear filtering looks much nicer but is much slower for Mesa */
310 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
311 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
314 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
315 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
318 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
320 load_tex(TEXTURE_2_FILE
, 1);
322 if (!LoadRGBMipmaps(TEXTURE_2_FILE
, GL_RGB
)) {
323 printf("Error: couldn't load texture image\n");
328 /* now bind the texture objects to the respective texture units */
329 #ifdef GL_ARB_multitexture
330 glActiveTextureARB(GL_TEXTURE0_ARB
);
331 glBindTexture(GL_TEXTURE_2D
, texObj
[0]);
332 glActiveTextureARB(GL_TEXTURE1_ARB
);
333 glBindTexture(GL_TEXTURE_2D
, texObj
[1]);
336 glShadeModel(GL_FLAT
);
337 glClearColor(0.3, 0.3, 0.4, 1.0);
341 if (argc
> 1 && strcmp(argv
[1], "-info")==0) {
342 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
343 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
344 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
345 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
350 int main( int argc
, char *argv
[] )
352 glutInit( &argc
, argv
);
353 glutInitWindowSize( 300, 300 );
354 glutInitWindowPosition( 0, 0 );
355 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
356 glutCreateWindow(argv
[0] );
360 glutReshapeFunc( Reshape
);
361 glutKeyboardFunc( Key
);
362 glutSpecialFunc( SpecialKey
);
363 glutDisplayFunc( Display
);
364 glutIdleFunc( Idle
);
366 glutCreateMenu(ModeMenu
);
367 glutAddMenuEntry("Texture 0", TEX0
);
368 glutAddMenuEntry("Texture 1", TEX1
);
369 glutAddMenuEntry("Multi-texture", TEXBOTH
);
370 glutAddMenuEntry("Toggle Animation", ANIMATE
);
371 glutAddMenuEntry("Quit", QUIT
);
372 glutAttachMenu(GLUT_RIGHT_BUTTON
);