Only convert configs if screen creation was successful.
[mesa.git] / progs / demos / texenv.c
1
2 /* Copyright (c) Mark J. Kilgard, 1994. */
3
4 /**
5 * (c) Copyright 1993, Silicon Graphics, Inc.
6 * ALL RIGHTS RESERVED
7 * Permission to use, copy, modify, and distribute this software for
8 * any purpose and without fee is hereby granted, provided that the above
9 * copyright notice appear in all copies and that both the copyright notice
10 * and this permission notice appear in supporting documentation, and that
11 * the name of Silicon Graphics, Inc. not be used in advertising
12 * or publicity pertaining to distribution of the software without specific,
13 * written prior permission.
14 *
15 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
16 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
18 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
20 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
21 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
22 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
23 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
24 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
26 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
27 *
28 * US Government Users Restricted Rights
29 * Use, duplication, or disclosure by the Government is subject to
30 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
31 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
32 * clause at DFARS 252.227-7013 and/or in similar or successor
33 * clauses in the FAR or the DOD or NASA FAR Supplement.
34 * Unpublished-- rights reserved under the copyright laws of the
35 * United States. Contractor/manufacturer is Silicon Graphics,
36 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
37 *
38 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
39 */
40
41 /*
42 * Demonstrates texture environment modes and internal image formats.
43 */
44
45 /*
46 * Hacked on, updated by Gareth Hughes <gareth@valinux.com>
47 */
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <GL/glut.h>
53
54 #undef max
55 #undef min
56 #define max( a, b ) ((a) >= (b) ? (a) : (b))
57 #define min( a, b ) ((a) <= (b) ? (a) : (b))
58
59 GLfloat lightCheck[4] = { 0.7, 0.7, 0.7, 1.0 };
60 GLfloat darkCheck[4] = { 0.3, 0.3, 0.3, 1.0 };
61
62 GLfloat labelColor0[4] = { 1.0, 1.0, 1.0, 1.0 };
63 GLfloat labelColor1[4] = { 1.0, 1.0, 0.4, 1.0 };
64 GLfloat *labelInfoColor = labelColor0;
65 GLfloat labelLevelColor0[4] = { 0.8, 0.8, 0.1, 1.0 };
66 GLfloat labelLevelColor1[4] = { 0.0, 0.0, 0.0, 1.0 };
67
68 GLboolean doubleBuffered = GL_TRUE;
69 GLboolean drawBackground = GL_FALSE;
70 GLboolean drawBlended = GL_TRUE;
71 GLboolean drawSmooth = GL_FALSE;
72 GLboolean drawTextured = GL_TRUE;
73 GLboolean displayLevelInfo = GL_FALSE;
74
75 int textureWidth = 64;
76 int textureHeight = 64;
77
78 int winWidth = 580, winHeight = 720;
79
80 struct formatInfo {
81 GLenum baseFormat;
82 GLenum internalFormat;
83 char *name;
84 };
85
86 #define NUM_LUMINANCE_FORMATS (sizeof(luminanceFormats) / sizeof(luminanceFormats[0]))
87 struct formatInfo luminanceFormats[] =
88 {
89 { GL_LUMINANCE, GL_LUMINANCE, "LUMINANCE" },
90 { GL_LUMINANCE, GL_LUMINANCE4, "LUMINANCE4" },
91 { GL_LUMINANCE, GL_LUMINANCE8, "LUMINANCE8" },
92 { GL_LUMINANCE, GL_LUMINANCE12, "LUMINANCE12" },
93 { GL_LUMINANCE, GL_LUMINANCE16, "LUMINANCE16" },
94 };
95
96 #define NUM_ALPHA_FORMATS (sizeof(alphaFormats) / sizeof(alphaFormats[0]))
97 struct formatInfo alphaFormats[] =
98 {
99 { GL_ALPHA, GL_ALPHA, "ALPHA" },
100 { GL_ALPHA, GL_ALPHA4, "ALPHA4" },
101 { GL_ALPHA, GL_ALPHA8, "ALPHA8" },
102 { GL_ALPHA, GL_ALPHA12, "ALPHA12" },
103 { GL_ALPHA, GL_ALPHA16, "ALPHA16" },
104 };
105
106 #define NUM_INTENSITY_FORMATS (sizeof(intensityFormats) / sizeof(intensityFormats[0]))
107 struct formatInfo intensityFormats[] =
108 {
109 { GL_INTENSITY, GL_INTENSITY, "INTENSITY" },
110 { GL_INTENSITY, GL_INTENSITY4, "INTENSITY4" },
111 { GL_INTENSITY, GL_INTENSITY8, "INTENSITY8" },
112 { GL_INTENSITY, GL_INTENSITY12, "INTENSITY12" },
113 { GL_INTENSITY, GL_INTENSITY16, "INTENSITY16" },
114 };
115
116 #define NUM_LUMINANCE_ALPHA_FORMATS (sizeof(luminanceAlphaFormats) / sizeof(luminanceAlphaFormats[0]))
117 struct formatInfo luminanceAlphaFormats[] =
118 {
119 { GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, "LUMINANCE_ALPHA" },
120 { GL_LUMINANCE_ALPHA, GL_LUMINANCE4_ALPHA4, "LUMINANCE4_ALPHA4" },
121 { GL_LUMINANCE_ALPHA, GL_LUMINANCE6_ALPHA2, "LUMINANCE6_ALPHA2" },
122 { GL_LUMINANCE_ALPHA, GL_LUMINANCE8_ALPHA8, "LUMINANCE8_ALPHA8" },
123 { GL_LUMINANCE_ALPHA, GL_LUMINANCE12_ALPHA4, "LUMINANCE12_ALPHA4" },
124 { GL_LUMINANCE_ALPHA, GL_LUMINANCE12_ALPHA12, "LUMINANCE12_ALPHA12" },
125 { GL_LUMINANCE_ALPHA, GL_LUMINANCE16_ALPHA16, "LUMINANCE16_ALPHA16" },
126 };
127
128 #define NUM_RGB_FORMATS (sizeof(rgbFormats) / sizeof(rgbFormats[0]))
129 struct formatInfo rgbFormats[] =
130 {
131 { GL_RGB, GL_RGB, "RGB" },
132 { GL_RGB, GL_R3_G3_B2, "R3_G3_B2" },
133 { GL_RGB, GL_RGB4, "RGB4" },
134 { GL_RGB, GL_RGB5, "RGB5" },
135 { GL_RGB, GL_RGB8, "RGB8" },
136 { GL_RGB, GL_RGB10, "RGB10" },
137 { GL_RGB, GL_RGB12, "RGB12" },
138 { GL_RGB, GL_RGB16, "RGB16" },
139 };
140
141 #define NUM_RGBA_FORMATS (sizeof(rgbaFormats) / sizeof(rgbaFormats[0]))
142 struct formatInfo rgbaFormats[] =
143 {
144 { GL_RGBA, GL_RGBA, "RGBA" },
145 { GL_RGBA, GL_RGBA2, "RGBA2" },
146 { GL_RGBA, GL_RGBA4, "RGBA4" },
147 { GL_RGBA, GL_RGB5_A1, "RGB5_A1" },
148 { GL_RGBA, GL_RGBA8, "RGBA8" },
149 { GL_RGBA, GL_RGB10_A2, "RGB10_A2" },
150 { GL_RGBA, GL_RGBA12, "RGBA12" },
151 { GL_RGBA, GL_RGBA16, "RGBA16" },
152 };
153
154 struct baseFormatInfo {
155 struct formatInfo *format;
156 int current, number;
157 };
158
159 #define NUM_BASE_FORMATS (sizeof(baseFormats) / sizeof(baseFormats[0]))
160 int baseFormat;
161 struct baseFormatInfo baseFormats[] =
162 {
163 { luminanceFormats, 0, NUM_LUMINANCE_FORMATS },
164 { alphaFormats, 0, NUM_ALPHA_FORMATS },
165 { intensityFormats, 0, NUM_INTENSITY_FORMATS },
166 { luminanceAlphaFormats, 0, NUM_LUMINANCE_ALPHA_FORMATS },
167 { rgbFormats, 0, NUM_RGB_FORMATS },
168 { rgbaFormats, 0, NUM_RGBA_FORMATS },
169 };
170
171 #define NUM_ENV_COLORS (sizeof(envColors) / sizeof(envColors[0]))
172 int envColor = 0;
173 GLfloat envColors[][4] =
174 {
175 { 0.0, 0.0, 0.0, 1.0 },
176 { 1.0, 0.0, 0.0, 1.0 },
177 { 0.0, 1.0, 0.0, 1.0 },
178 { 0.0, 0.0, 1.0, 1.0 },
179 { 1.0, 1.0, 1.0, 1.0 },
180 };
181
182 struct envModeInfo {
183 GLenum mode;
184 char *name;
185 };
186
187 /* allow for run-time check for GL_EXT_texture_env_add */
188 int NUM_ENV_MODES = 5;
189 struct envModeInfo envModes[] =
190 {
191 { GL_REPLACE, "REPLACE" },
192 { GL_MODULATE, "MODULATE" },
193 { GL_BLEND, "BLEND" },
194 { GL_DECAL, "DECAL" },
195 #if GL_EXT_texture_env_add
196 { GL_ADD, "ADD" },
197 #endif
198 };
199
200 static void checkErrors( void )
201 {
202 GLenum error;
203
204 while ( (error = glGetError()) != GL_NO_ERROR ) {
205 fprintf( stderr, "Error: %s\n", (char *) gluErrorString( error ) );
206 }
207 }
208
209 static void drawString( const char *string, GLfloat x, GLfloat y,
210 const GLfloat color[4] )
211 {
212 glColor4fv( color );
213 glRasterPos2f( x, y );
214
215 while ( *string ) {
216 glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_10, *string );
217 string++;
218 }
219 }
220
221 static void drawStringOutline( const char *string, GLfloat x, GLfloat y,
222 const GLfloat color[4],
223 const GLfloat outline[4] )
224 {
225 drawString( string, x - 1, y, outline );
226 drawString( string, x + 1, y, outline );
227 drawString( string, x, y - 1, outline );
228 drawString( string, x, y + 1, outline );
229 drawString( string, x, y, color );
230 }
231
232 static void begin2D( int width, int height )
233 {
234 glMatrixMode( GL_PROJECTION );
235
236 glPushMatrix();
237 glLoadIdentity();
238
239 glOrtho( 0, width, 0, height, -1, 1 );
240 glMatrixMode( GL_MODELVIEW );
241
242 glPushMatrix();
243 glLoadIdentity();
244 }
245
246 static void end2D( void )
247 {
248 glMatrixMode( GL_PROJECTION );
249 glPopMatrix();
250 glMatrixMode( GL_MODELVIEW );
251 glPopMatrix();
252 }
253
254 static void initialize( void )
255 {
256 glMatrixMode( GL_PROJECTION );
257 glLoadIdentity();
258
259 glOrtho( -1.5, 1.5, -1.5, 1.5, -1.5, 1.5 );
260
261 glMatrixMode(GL_MODELVIEW);
262 glLoadIdentity();
263
264 glShadeModel( GL_FLAT );
265 }
266
267 /* ARGSUSED1 */
268 static void keyboard( unsigned char c, int x, int y )
269 {
270 switch ( c ) {
271 case 'c':
272 envColor++;
273 envColor = envColor % (int) NUM_ENV_COLORS;
274 break;
275 case 'g':
276 drawBackground = !drawBackground;
277 break;
278 case 'b':
279 drawBlended = !drawBlended;
280 break;
281 case 's':
282 drawSmooth = !drawSmooth;
283 break;
284 case 't':
285 drawTextured = !drawTextured;
286 break;
287 case 'i':
288 displayLevelInfo = !displayLevelInfo;
289 break;
290 case 27: /* Escape key should force exit. */
291 exit(0);
292 break;
293 default:
294 break;
295 }
296 glutPostRedisplay();
297 }
298
299 /* ARGSUSED1 */
300 static void special( int key, int x, int y )
301 {
302 switch ( key ) {
303 case GLUT_KEY_DOWN:
304 if ( ++baseFormat > NUM_BASE_FORMATS - 1 ) {
305 baseFormat = 0;
306 }
307 break;
308 case GLUT_KEY_UP:
309 if ( --baseFormat < 0 ) {
310 baseFormat = NUM_BASE_FORMATS - 1;
311 }
312 break;
313 case GLUT_KEY_LEFT:
314 --baseFormats[baseFormat].current;
315 if ( baseFormats[baseFormat].current < 0 ) {
316 baseFormats[baseFormat].current = baseFormats[baseFormat].number - 1;
317 }
318 break;
319 case GLUT_KEY_RIGHT:
320 ++baseFormats[baseFormat].current;
321 if ( baseFormats[baseFormat].current > baseFormats[baseFormat].number - 1 ) {
322 baseFormats[baseFormat].current = 0;
323 }
324 break;
325 default:
326 break;
327 }
328 glutPostRedisplay();
329 }
330
331 static void
332 reshape( int w, int h )
333 {
334 winWidth = w;
335 winHeight = h;
336 /* No need to call glViewPort here since "draw" calls it! */
337 }
338
339 static void loadTexture( int width, int height,
340 const struct formatInfo *format )
341 {
342 int luminanceSize = 0;
343 int alphaSize = 0;
344 int rgbSize = 0;
345 GLenum textureFormat;
346 GLubyte *texImage, *p;
347 int elementsPerGroup, elementSize, groupSize, rowSize;
348 int i, j;
349
350 switch ( format->baseFormat ) {
351 case GL_LUMINANCE:
352 luminanceSize = 1;
353 textureFormat = GL_LUMINANCE;
354 break;
355 case GL_INTENSITY:
356 luminanceSize = 1;
357 /* Note: format=GL_INTENSITY for glTexImage is not legal */
358 textureFormat = GL_LUMINANCE;
359 break;
360 case GL_ALPHA:
361 alphaSize = 1;
362 textureFormat = GL_ALPHA;
363 break;
364 case GL_LUMINANCE_ALPHA:
365 luminanceSize = 1;
366 alphaSize = 1;
367 textureFormat = GL_LUMINANCE_ALPHA;
368 break;
369 case GL_RGB:
370 rgbSize = 3;
371 textureFormat = GL_RGB;
372 break;
373 case GL_RGBA:
374 rgbSize = 3;
375 alphaSize = 1;
376 textureFormat = GL_RGBA;
377 break;
378 default:
379 fprintf(stderr, "bad internal format info\n");
380 return;
381 }
382
383 elementsPerGroup = luminanceSize + alphaSize + rgbSize;
384 elementSize = sizeof(GLubyte);
385 groupSize = elementsPerGroup * elementSize;
386 rowSize = width * groupSize;
387
388 if ( (texImage = (GLubyte *) malloc( height * rowSize ) ) == NULL ) {
389 fprintf( stderr, "texture malloc failed\n" );
390 return;
391 }
392
393 for ( i = 0 ; i < height ; i++ )
394 {
395 p = texImage + i * rowSize;
396
397 for ( j = 0 ; j < width ; j++ )
398 {
399 if ( luminanceSize > 0 )
400 {
401 /**
402 ** +-----+-----+
403 ** | | |
404 ** | W | LG |
405 ** | | |
406 ** +-----+-----+
407 ** | | |
408 ** | DG | B |
409 ** | | |
410 ** +-----+-----+
411 **/
412 if ( i > height / 2 ) {
413 if ( j < width / 2 ) {
414 p[0] = 0xff;
415 } else {
416 p[0] = 0xaa;
417 }
418 } else {
419 if ( j < width / 2 ) {
420 p[0] = 0x55;
421 } else {
422 p[0] = 0x00;
423 }
424 }
425 p += elementSize;
426 }
427
428 if ( rgbSize > 0 )
429 {
430 /**
431 ** +-----+-----+
432 ** | | |
433 ** | R | G |
434 ** | | |
435 ** +-----+-----+
436 ** | | |
437 ** | Y | B |
438 ** | | |
439 ** +-----+-----+
440 **/
441 if ( i > height / 2 ) {
442 if ( j < width / 2 ) {
443 p[0] = 0xff;
444 p[1] = 0x00;
445 p[2] = 0x00;
446 } else {
447 p[0] = 0x00;
448 p[1] = 0xff;
449 p[2] = 0x00;
450 }
451 } else {
452 if ( j < width / 2 ) {
453 p[0] = 0xff;
454 p[1] = 0xff;
455 p[2] = 0x00;
456 } else {
457 p[0] = 0x00;
458 p[1] = 0x00;
459 p[2] = 0xff;
460 }
461 }
462 p += 3 * elementSize;
463 }
464
465 if ( alphaSize > 0 )
466 {
467 /**
468 ** +-----------+
469 ** | W |
470 ** | +-----+ |
471 ** | | | |
472 ** | | B | |
473 ** | | | |
474 ** | +-----+ |
475 ** | |
476 ** +-----------+
477 **/
478 int i2 = i - height / 2;
479 int j2 = j - width / 2;
480 int h8 = height / 8;
481 int w8 = width / 8;
482 if ( -h8 <= i2 && i2 <= h8 && -w8 <= j2 && j2 <= w8 ) {
483 p[0] = 0x00;
484 } else if ( -2 * h8 <= i2 && i2 <= 2 * h8 && -2 * w8 <= j2 && j2 <= 2 * w8 ) {
485 p[0] = 0x55;
486 } else if ( -3 * h8 <= i2 && i2 <= 3 * h8 && -3 * w8 <= j2 && j2 <= 3 * w8 ) {
487 p[0] = 0xaa;
488 } else {
489 p[0] = 0xff;
490 }
491 p += elementSize;
492 }
493 }
494 }
495
496 glTexImage2D( GL_TEXTURE_2D, 0,
497 format->internalFormat, width, height, 0,
498 textureFormat, GL_UNSIGNED_BYTE, texImage );
499
500 free( texImage );
501 }
502
503 static void drawCheck( int w, int h, const GLfloat lightCheck[4],
504 const GLfloat darkCheck[4] )
505 {
506 float dw = 2.0 / w;
507 float dh = 2.0 / h;
508 int i, j;
509
510 for ( i = 0 ; i < w ; i++ ) {
511 GLfloat x0 = -1.0 + i * dw;
512 GLfloat x1 = x0 + dw;
513
514 glBegin( GL_QUAD_STRIP );
515
516 for ( j = 0 ; j <= h ; j++ ) {
517 GLfloat y = -1.0 + j * dh;
518
519 if ( (i ^ j) & 1 ) {
520 glColor4fv( lightCheck );
521 } else {
522 glColor4fv( darkCheck );
523 }
524
525 glVertex2f( x0, y );
526 glVertex2f( x1, y );
527 }
528
529 glEnd();
530 }
531 }
532
533 static const char *lookupFormat( GLint format )
534 {
535 switch ( format ) {
536 case GL_RGBA:
537 return "GL_RGBA";
538 case GL_RGB:
539 return "GL_RGB";
540 case GL_ALPHA:
541 return "GL_ALPHA";
542 case GL_LUMINANCE:
543 return "GL_LUMINANCE";
544 case GL_LUMINANCE_ALPHA:
545 return "GL_LUMINANCE_ALPHA";
546 case GL_INTENSITY:
547 return "GL_INTENSITY";
548 case GL_COLOR_INDEX:
549 return "GL_COLOR_INDEX";
550 case GL_BGRA:
551 return "GL_BGRA";
552 case GL_BGR:
553 return "GL_BGR";
554 default:
555 return "unknown format";
556 }
557 }
558
559 static void drawSample( int x, int y, int w, int h,
560 const struct formatInfo *format,
561 const struct envModeInfo *envMode )
562 {
563 glViewport( x, y, w, h );
564 glScissor( x, y, w, h );
565
566 glClearColor( 0.1, 0.1, 0.1, 1.0 );
567 glClear( GL_COLOR_BUFFER_BIT );
568
569 begin2D( w, h );
570 drawString( format->name, 10, h - 15, labelInfoColor );
571 drawString( envMode->name, 10, 5, labelInfoColor );
572 end2D();
573
574 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, envMode->mode );
575 glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, envColors[envColor] );
576
577 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
578 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
579
580 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
581 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
582
583 loadTexture( textureWidth, textureHeight, format );
584
585 if ( drawBackground ) {
586 drawCheck( 15, 15, lightCheck, darkCheck );
587 }
588 if ( drawBlended ) {
589 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
590 glEnable( GL_BLEND );
591 }
592 if ( drawSmooth ) {
593 glShadeModel( GL_SMOOTH );
594 }
595 else {
596 glShadeModel( GL_FLAT );
597 glColor4f(1, 1, 1, 1);
598 }
599 if ( drawTextured ) {
600 glEnable( GL_TEXTURE_2D );
601 }
602
603 /*
604 * if (drawSmooth) then draw quad which goes from purple at the
605 * bottom (100% alpha) to green at the top (50% alpha).
606 */
607 glBegin( GL_QUADS );
608 if ( drawSmooth ) glColor4f( 1.0, 0.0, 1.0, 1.0 );
609 glTexCoord2f( 0.0, 0.0 );
610 glVertex2f( -0.8, -0.8 );
611
612 if ( drawSmooth ) glColor4f( 1.0, 0.0, 1.0, 1.0 );
613 glTexCoord2f( 1.0, 0.0 );
614 glVertex2f( 0.8, -0.8 );
615
616 if ( drawSmooth ) glColor4f( 0.0, 1.0, 0.0, 0.5 );
617 glTexCoord2f( 1.0, 1.0 );
618 glVertex2f( 0.8, 0.8 );
619
620 if ( drawSmooth ) glColor4f( 0.0, 1.0, 0.0, 0.5 );
621 glTexCoord2f( 0.0, 1.0 );
622 glVertex2f( -0.8, 0.8 );
623 glEnd();
624
625 glDisable( GL_BLEND );
626 glShadeModel( GL_FLAT );
627 glDisable( GL_TEXTURE_2D );
628
629 if ( envMode->mode == GL_DECAL &&
630 (format->baseFormat == GL_ALPHA ||
631 format->baseFormat == GL_LUMINANCE ||
632 format->baseFormat == GL_LUMINANCE_ALPHA ||
633 format->baseFormat == GL_INTENSITY)) {
634 /* undefined format/mode combination */
635 begin2D( w, h );
636 drawStringOutline( "UNDEFINED MODE", 15, h / 2,
637 labelLevelColor0, labelLevelColor1 );
638 end2D();
639 }
640 else if ( displayLevelInfo ) {
641 GLint width, height, border, format;
642 GLint redSize, greenSize, blueSize, alphaSize;
643 GLint luminanceSize, intensitySize;
644 char buf[255];
645
646 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width );
647 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height );
648 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER, &border );
649 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &format );
650 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &redSize );
651 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE, &greenSize );
652 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE, &blueSize );
653 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &alphaSize );
654 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_LUMINANCE_SIZE, &luminanceSize );
655 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_INTENSITY_SIZE, &intensitySize );
656
657 begin2D( w, h );
658 sprintf( buf, "dimensions: %d x %d", width, height );
659 drawStringOutline( buf, 15, h / 2 + 20, labelLevelColor0, labelLevelColor1 );
660
661 sprintf( buf, "border: %d", border );
662 drawStringOutline( buf, 15, h / 2 + 10, labelLevelColor0, labelLevelColor1 );
663
664 sprintf( buf, "internal format:" );
665 drawStringOutline( buf, 15, h / 2, labelLevelColor0, labelLevelColor1 );
666
667 sprintf( buf, " %s", lookupFormat( format ) );
668 drawStringOutline( buf, 15, h / 2 - 10, labelLevelColor0, labelLevelColor1 );
669
670 sprintf( buf, "sizes:" );
671 drawStringOutline( buf, 15, h / 2 - 20, labelLevelColor0, labelLevelColor1 );
672
673 sprintf( buf, " %d / %d / %d / %d / %d / %d",
674 redSize, greenSize, blueSize, alphaSize,
675 luminanceSize, intensitySize );
676 drawStringOutline( buf, 15, h / 2 - 30, labelLevelColor0, labelLevelColor1 );
677
678 end2D();
679 }
680 }
681
682 static void display( void )
683 {
684 int numX = NUM_ENV_MODES, numY = NUM_BASE_FORMATS;
685 float xBase = (float) winWidth * 0.01;
686 float xOffset = (winWidth - xBase) / numX;
687 float xSize = max( xOffset - xBase, 1 );
688 float yBase = (float) winHeight * 0.01;
689 float yOffset = (winHeight - yBase) / numY;
690 float ySize = max( yOffset - yBase, 1 );
691 float x, y;
692 int i, j;
693
694 glViewport( 0, 0, winWidth, winHeight );
695 glDisable( GL_SCISSOR_TEST );
696 glClearColor( 0.0, 0.0, 0.0, 0.0 );
697 glClear( GL_COLOR_BUFFER_BIT );
698 glEnable( GL_SCISSOR_TEST );
699
700 x = xBase;
701 y = (winHeight - 1) - yOffset;
702
703 for ( i = 0 ; i < NUM_BASE_FORMATS ; i++ )
704 {
705 struct formatInfo *format;
706
707 if ( i == baseFormat ) {
708 labelInfoColor = labelColor1;
709 } else {
710 labelInfoColor = labelColor0;
711 }
712
713 format = &baseFormats[i].format[baseFormats[i].current];
714
715 for ( j = 0 ; j < NUM_ENV_MODES ; j++ ) {
716 struct envModeInfo *envMode;
717
718 envMode = &envModes[j];
719 drawSample( x, y, xSize, ySize, format, envMode );
720 x += xOffset;
721 }
722
723 x = xBase;
724 y -= yOffset;
725 }
726
727 if ( doubleBuffered ) {
728 glutSwapBuffers();
729 } else {
730 glFlush();
731 }
732
733 checkErrors();
734 }
735
736 static void usage( char *name )
737 {
738 fprintf( stderr, "usage: %s [ options ]\n", name );
739 fprintf( stderr, "\n" );
740 fprintf( stderr, "options:\n" );
741 fprintf( stderr, " -sb single buffered\n" );
742 fprintf( stderr, " -db double buffered\n" );
743 fprintf( stderr, " -info print OpenGL driver info\n" );
744 }
745
746 static void instructions( void )
747 {
748 fprintf( stderr, "texenv - texture environment and internal format test\n" );
749 fprintf( stderr, "\n" );
750 fprintf( stderr, " [c] - cycle through background colors\n" );
751 fprintf( stderr, " [g] - toggle background\n" );
752 fprintf( stderr, " [b] - toggle blend\n" );
753 fprintf( stderr, " [s] - toggle smooth shading\n" );
754 fprintf( stderr, " [t] - toggle texturing\n" );
755 fprintf( stderr, " [i] - toggle information display\n" );
756 fprintf( stderr, " up/down - select row\n" );
757 fprintf( stderr, " left/right - change row's internal format\n" );
758 }
759
760 int main( int argc, char *argv[] )
761 {
762 GLboolean info = GL_FALSE;
763 int i;
764
765 glutInit( &argc, argv );
766
767 for ( i = 1 ; i < argc ; i++ ) {
768 if ( !strcmp( "-sb", argv[i] ) ) {
769 doubleBuffered = GL_FALSE;
770 } else if ( !strcmp( "-db", argv[i] ) ) {
771 doubleBuffered = GL_TRUE;
772 } else if ( !strcmp( "-info", argv[i] ) ) {
773 info = GL_TRUE;
774 } else {
775 usage( argv[0] );
776 exit( 1 );
777 }
778 }
779
780 if ( doubleBuffered ) {
781 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
782 } else {
783 glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
784 }
785
786 glutInitWindowSize( winWidth, winHeight );
787 glutInitWindowPosition( 0, 0 );
788 glutCreateWindow( "Texture Environment Test" );
789
790 initialize();
791 instructions();
792
793 if ( info ) {
794 printf( "\n" );
795 printf( "GL_RENDERER = %s\n", (char *) glGetString( GL_RENDERER ) );
796 printf( "GL_VERSION = %s\n", (char *) glGetString( GL_VERSION ) );
797 printf( "GL_VENDOR = %s\n", (char *) glGetString( GL_VENDOR ) ) ;
798 printf( "GL_EXTENSIONS = %s\n", (char *) glGetString( GL_EXTENSIONS ) );
799 }
800
801 #if GL_EXT_texture_env_add
802 if ( !glutExtensionSupported( "GL_EXT_texture_env_add" ) ) {
803 fprintf( stderr, "missing extension: GL_EXT_texture_env_add\n" );
804 NUM_ENV_MODES--;
805 }
806 #endif
807
808 glutDisplayFunc( display );
809 glutReshapeFunc( reshape );
810 glutKeyboardFunc( keyboard );
811 glutSpecialFunc( special );
812 glutMainLoop();
813
814 return 0;
815 }