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
18 #include "../util/readtex.c" /* I know, this is a hack. */
20 #define TEXTURE_1_FILE "../images/tile.rgb"
21 #define TEXTURE_2_FILE "../images/reflect.rgb"
29 static GLboolean Animate
= GL_TRUE
;
31 static GLfloat Drift
= 0.0;
32 static GLfloat Xrot
= 20.0, Yrot
= 30.0, Zrot
= 0.0;
36 static void Idle( void )
43 #ifdef GL_ARB_multitexture
44 glActiveTextureARB(GL_TEXTURE0_ARB
);
46 glMatrixMode(GL_TEXTURE
);
48 glTranslatef(Drift
, 0.0, 0.0);
49 glMatrixMode(GL_MODELVIEW
);
51 #ifdef GL_ARB_multitexture
52 glActiveTextureARB(GL_TEXTURE1_ARB
);
54 glMatrixMode(GL_TEXTURE
);
56 glTranslatef(0.0, Drift
, 0.0);
57 glMatrixMode(GL_MODELVIEW
);
64 static void DrawObject(void)
68 #ifdef GL_ARB_multitexture
69 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 0.0, 0.0);
70 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 0.0, 0.0);
71 glVertex2f(-1.0, -1.0);
73 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 2.0, 0.0);
74 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 1.0, 0.0);
75 glVertex2f(1.0, -1.0);
77 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 2.0, 2.0);
78 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 1.0, 1.0);
81 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 0.0, 2.0);
82 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 0.0, 1.0);
83 glVertex2f(-1.0, 1.0);
85 glTexCoord2f(0.0, 0.0);
86 glVertex2f(-1.0, -1.0);
88 glTexCoord2f(1.0, 0.0);
89 glVertex2f(1.0, -1.0);
91 glTexCoord2f(1.0, 1.0);
94 glTexCoord2f(0.0, 1.0);
95 glVertex2f(-1.0, 1.0);
103 static void Display( void )
105 glClear( GL_COLOR_BUFFER_BIT
);
108 glRotatef(Xrot
, 1.0, 0.0, 0.0);
109 glRotatef(Yrot
, 0.0, 1.0, 0.0);
110 glRotatef(Zrot
, 0.0, 0.0, 1.0);
111 glScalef(5.0, 5.0, 5.0);
119 static void Reshape( int width
, int height
)
121 glViewport( 0, 0, width
, height
);
122 glMatrixMode( GL_PROJECTION
);
124 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
125 /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
126 glMatrixMode( GL_MODELVIEW
);
128 glTranslatef( 0.0, 0.0, -70.0 );
132 static void ModeMenu(int entry
)
134 GLboolean enable0
= GL_FALSE
, enable1
= GL_FALSE
;
138 else if (entry
==TEX1
) {
141 else if (entry
==TEXBOTH
) {
145 else if (entry
==ANIMATE
) {
148 else if (entry
==QUIT
) {
152 if (entry
!= ANIMATE
) {
153 #ifdef GL_ARB_multitexture
154 glActiveTextureARB(GL_TEXTURE0_ARB
);
157 glEnable(GL_TEXTURE_2D
);
160 glDisable(GL_TEXTURE_2D
);
162 #ifdef GL_ARB_multitexture
163 glActiveTextureARB(GL_TEXTURE1_ARB
);
166 glEnable(GL_TEXTURE_2D
);
169 glDisable(GL_TEXTURE_2D
);
176 static void Key( unsigned char key
, int x
, int y
)
189 static void SpecialKey( int key
, int x
, int y
)
213 static void load_tex(const char *fname
, int channel
)
220 GLubyte table
[256][4];
222 image
= LoadRGBImage(fname
, &w
, &h
, &format
);
226 printf("%s %d x %d\n", fname
, w
, h
);
227 grayImage
= malloc(w
* h
* 1);
229 for (i
= 0; i
< w
* h
; i
++) {
230 int g
= (image
[i
*3+0] + image
[i
*3+1] + image
[i
*3+2]) / 3;
235 glTexImage2D(GL_TEXTURE_2D
, 0, GL_COLOR_INDEX
, w
, h
, 0, GL_COLOR_INDEX
,
236 GL_UNSIGNED_BYTE
, grayImage
);
238 for (i
= 0; i
< 256; i
++) {
239 table
[i
][0] = channel
? i
: 0;
241 table
[i
][2] = channel
? 0 : i
;
245 glColorTableEXT(GL_TEXTURE_2D
, /* target */
246 GL_RGBA
, /* internal format */
247 256, /* table size */
248 GL_RGBA
, /* table format */
249 GL_UNSIGNED_BYTE
, /* table type */
250 table
); /* the color table */
258 static void Init( int argc
, char *argv
[] )
263 if (!glutExtensionSupported("GL_ARB_multitexture")) {
264 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
267 if (!glutExtensionSupported("GL_EXT_paletted_texture")) {
268 printf("Sorry, GL_EXT_paletted_texture not supported by this renderer.\n");
272 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB
, &units
);
273 printf("%d texture units supported\n", units
);
275 /* allocate two texture objects */
276 glGenTextures(2, texObj
);
278 /* setup texture obj 0 */
279 glBindTexture(GL_TEXTURE_2D
, texObj
[0]);
281 /* linear filtering looks much nicer but is much slower for Mesa */
282 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
283 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
286 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
287 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
290 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
292 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
294 load_tex(TEXTURE_1_FILE
, 0);
296 if (!LoadRGBMipmaps(TEXTURE_1_FILE
, GL_RGB
)) {
297 printf("Error: couldn't load texture image\n");
302 /* setup texture obj 1 */
303 glBindTexture(GL_TEXTURE_2D
, texObj
[1]);
305 /* linear filtering looks much nicer but is much slower for Mesa */
306 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
307 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
310 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
311 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
314 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
316 load_tex(TEXTURE_2_FILE
, 1);
318 if (!LoadRGBMipmaps(TEXTURE_2_FILE
, GL_RGB
)) {
319 printf("Error: couldn't load texture image\n");
324 /* now bind the texture objects to the respective texture units */
325 #ifdef GL_ARB_multitexture
326 glActiveTextureARB(GL_TEXTURE0_ARB
);
327 glBindTexture(GL_TEXTURE_2D
, texObj
[0]);
328 glActiveTextureARB(GL_TEXTURE1_ARB
);
329 glBindTexture(GL_TEXTURE_2D
, texObj
[1]);
332 glShadeModel(GL_FLAT
);
333 glClearColor(0.3, 0.3, 0.4, 1.0);
337 if (argc
> 1 && strcmp(argv
[1], "-info")==0) {
338 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
339 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
340 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
341 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
346 int main( int argc
, char *argv
[] )
348 glutInit( &argc
, argv
);
349 glutInitWindowSize( 300, 300 );
350 glutInitWindowPosition( 0, 0 );
351 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
352 glutCreateWindow(argv
[0] );
356 glutReshapeFunc( Reshape
);
357 glutKeyboardFunc( Key
);
358 glutSpecialFunc( SpecialKey
);
359 glutDisplayFunc( Display
);
360 glutIdleFunc( Idle
);
362 glutCreateMenu(ModeMenu
);
363 glutAddMenuEntry("Texture 0", TEX0
);
364 glutAddMenuEntry("Texture 1", TEX1
);
365 glutAddMenuEntry("Multi-texture", TEXBOTH
);
366 glutAddMenuEntry("Toggle Animation", ANIMATE
);
367 glutAddMenuEntry("Quit", QUIT
);
368 glutAttachMenu(GLUT_RIGHT_BUTTON
);