3 * Test multitexture and paletted textures.
11 #include <stddef.h> /* for ptrdiff_t, referenced by GL.h when GL_GLEXT_LEGACY defined */
15 #define GL_GLEXT_LEGACY
19 #include "../util/readtex.c" /* I know, this is a hack. */
21 #define TEXTURE_1_FILE "../images/tile.rgb"
22 #define TEXTURE_2_FILE "../images/reflect.rgb"
30 static GLboolean Animate
= GL_TRUE
;
32 static GLfloat Drift
= 0.0;
33 static GLfloat Xrot
= 20.0, Yrot
= 30.0, Zrot
= 0.0;
37 static void Idle( void )
44 #ifdef GL_ARB_multitexture
45 glActiveTextureARB(GL_TEXTURE0_ARB
);
47 glMatrixMode(GL_TEXTURE
);
49 glTranslatef(Drift
, 0.0, 0.0);
50 glMatrixMode(GL_MODELVIEW
);
52 #ifdef GL_ARB_multitexture
53 glActiveTextureARB(GL_TEXTURE1_ARB
);
55 glMatrixMode(GL_TEXTURE
);
57 glTranslatef(0.0, Drift
, 0.0);
58 glMatrixMode(GL_MODELVIEW
);
65 static void DrawObject(void)
69 #ifdef GL_ARB_multitexture
70 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 0.0, 0.0);
71 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 0.0, 0.0);
72 glVertex2f(-1.0, -1.0);
74 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 2.0, 0.0);
75 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 1.0, 0.0);
76 glVertex2f(1.0, -1.0);
78 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 2.0, 2.0);
79 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 1.0, 1.0);
82 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 0.0, 2.0);
83 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 0.0, 1.0);
84 glVertex2f(-1.0, 1.0);
86 glTexCoord2f(0.0, 0.0);
87 glVertex2f(-1.0, -1.0);
89 glTexCoord2f(1.0, 0.0);
90 glVertex2f(1.0, -1.0);
92 glTexCoord2f(1.0, 1.0);
95 glTexCoord2f(0.0, 1.0);
96 glVertex2f(-1.0, 1.0);
104 static void Display( void )
106 glClear( GL_COLOR_BUFFER_BIT
);
109 glRotatef(Xrot
, 1.0, 0.0, 0.0);
110 glRotatef(Yrot
, 0.0, 1.0, 0.0);
111 glRotatef(Zrot
, 0.0, 0.0, 1.0);
112 glScalef(5.0, 5.0, 5.0);
120 static void Reshape( int width
, int height
)
122 glViewport( 0, 0, width
, height
);
123 glMatrixMode( GL_PROJECTION
);
125 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
126 /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
127 glMatrixMode( GL_MODELVIEW
);
129 glTranslatef( 0.0, 0.0, -70.0 );
133 static void ModeMenu(int entry
)
135 GLboolean enable0
= GL_FALSE
, enable1
= GL_FALSE
;
139 else if (entry
==TEX1
) {
142 else if (entry
==TEXBOTH
) {
146 else if (entry
==ANIMATE
) {
149 else if (entry
==QUIT
) {
153 if (entry
!= ANIMATE
) {
154 #ifdef GL_ARB_multitexture
155 glActiveTextureARB(GL_TEXTURE0_ARB
);
158 glEnable(GL_TEXTURE_2D
);
161 glDisable(GL_TEXTURE_2D
);
163 #ifdef GL_ARB_multitexture
164 glActiveTextureARB(GL_TEXTURE1_ARB
);
167 glEnable(GL_TEXTURE_2D
);
170 glDisable(GL_TEXTURE_2D
);
177 static void Key( unsigned char key
, int x
, int y
)
190 static void SpecialKey( int key
, int x
, int y
)
214 static void load_tex(const char *fname
, int channel
)
221 GLubyte table
[256][4];
223 image
= LoadRGBImage(fname
, &w
, &h
, &format
);
227 printf("%s %d x %d\n", fname
, w
, h
);
228 grayImage
= malloc(w
* h
* 1);
230 for (i
= 0; i
< w
* h
; i
++) {
231 int g
= (image
[i
*3+0] + image
[i
*3+1] + image
[i
*3+2]) / 3;
236 glTexImage2D(GL_TEXTURE_2D
, 0, GL_COLOR_INDEX
, w
, h
, 0, GL_COLOR_INDEX
,
237 GL_UNSIGNED_BYTE
, grayImage
);
239 for (i
= 0; i
< 256; i
++) {
240 table
[i
][0] = channel
? i
: 0;
242 table
[i
][2] = channel
? 0 : i
;
246 glColorTableEXT(GL_TEXTURE_2D
, /* target */
247 GL_RGBA
, /* internal format */
248 256, /* table size */
249 GL_RGBA
, /* table format */
250 GL_UNSIGNED_BYTE
, /* table type */
251 table
); /* the color table */
259 static void Init( int argc
, char *argv
[] )
264 if (!glutExtensionSupported("GL_ARB_multitexture")) {
265 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
268 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
269 printf("Sorry, GL_EXT_paletted_texture not supported by this renderer.\n");
273 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB
, &units
);
274 printf("%d texture units supported\n", units
);
276 /* allocate two texture objects */
277 glGenTextures(2, texObj
);
279 /* setup texture obj 0 */
280 glBindTexture(GL_TEXTURE_2D
, texObj
[0]);
282 /* linear filtering looks much nicer but is much slower for Mesa */
283 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
284 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
287 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
288 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
291 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
293 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
295 load_tex(TEXTURE_1_FILE
, 0);
297 if (!LoadRGBMipmaps(TEXTURE_1_FILE
, GL_RGB
)) {
298 printf("Error: couldn't load texture image\n");
303 /* setup texture obj 1 */
304 glBindTexture(GL_TEXTURE_2D
, texObj
[1]);
306 /* linear filtering looks much nicer but is much slower for Mesa */
307 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
308 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
311 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
312 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
315 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
317 load_tex(TEXTURE_2_FILE
, 1);
319 if (!LoadRGBMipmaps(TEXTURE_2_FILE
, GL_RGB
)) {
320 printf("Error: couldn't load texture image\n");
325 /* now bind the texture objects to the respective texture units */
326 #ifdef GL_ARB_multitexture
327 glActiveTextureARB(GL_TEXTURE0_ARB
);
328 glBindTexture(GL_TEXTURE_2D
, texObj
[0]);
329 glActiveTextureARB(GL_TEXTURE1_ARB
);
330 glBindTexture(GL_TEXTURE_2D
, texObj
[1]);
333 glShadeModel(GL_FLAT
);
334 glClearColor(0.3, 0.3, 0.4, 1.0);
338 if (argc
> 1 && strcmp(argv
[1], "-info")==0) {
339 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
340 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
341 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
342 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
347 int main( int argc
, char *argv
[] )
349 glutInit( &argc
, argv
);
350 glutInitWindowSize( 300, 300 );
351 glutInitWindowPosition( 0, 0 );
352 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
353 glutCreateWindow(argv
[0] );
358 glutReshapeFunc( Reshape
);
359 glutKeyboardFunc( Key
);
360 glutSpecialFunc( SpecialKey
);
361 glutDisplayFunc( Display
);
362 glutIdleFunc( Idle
);
364 glutCreateMenu(ModeMenu
);
365 glutAddMenuEntry("Texture 0", TEX0
);
366 glutAddMenuEntry("Texture 1", TEX1
);
367 glutAddMenuEntry("Multi-texture", TEXBOTH
);
368 glutAddMenuEntry("Toggle Animation", ANIMATE
);
369 glutAddMenuEntry("Quit", QUIT
);
370 glutAttachMenu(GLUT_RIGHT_BUTTON
);