blob: d64ae2ee264ad1cf1bb6e06460dbb014b888123f [file] [log] [blame]
Brian Paulf13a3302000-06-27 15:55:47 +00001
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
59GLfloat lightCheck[4] = { 0.7, 0.7, 0.7, 1.0 };
60GLfloat darkCheck[4] = { 0.3, 0.3, 0.3, 1.0 };
61
62GLfloat labelColor0[4] = { 1.0, 1.0, 1.0, 1.0 };
63GLfloat labelColor1[4] = { 1.0, 1.0, 0.4, 1.0 };
64GLfloat *labelInfoColor = labelColor0;
65GLfloat labelLevelColor0[4] = { 0.8, 0.8, 0.1, 1.0 };
66GLfloat labelLevelColor1[4] = { 0.0, 0.0, 0.0, 1.0 };
67
Brian Paul5479e932001-03-27 17:38:28 +000068GLboolean doubleBuffered = GL_TRUE;
Brian Paulf13a3302000-06-27 15:55:47 +000069GLboolean drawBackground = GL_FALSE;
70GLboolean drawBlended = GL_TRUE;
71GLboolean drawSmooth = GL_FALSE;
72GLboolean drawTextured = GL_TRUE;
73GLboolean displayLevelInfo = GL_FALSE;
74
75int textureWidth = 64;
76int textureHeight = 64;
77
78int winWidth = 580, winHeight = 720;
79
80struct formatInfo {
81 GLenum baseFormat;
82 GLenum internalFormat;
83 char *name;
84};
85
86#define NUM_LUMINANCE_FORMATS (sizeof(luminanceFormats) / sizeof(luminanceFormats[0]))
87struct 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]))
97struct 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]))
107struct 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]))
117struct 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]))
129struct 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]))
142struct formatInfo rgbaFormats[] =
143{
Brian Paul345a71a2000-09-15 23:59:46 +0000144 { GL_RGBA, GL_RGBA, "RGBA" },
Brian Paulf13a3302000-06-27 15:55:47 +0000145 { 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
154struct baseFormatInfo {
155 struct formatInfo *format;
156 int current, number;
157};
158
159#define NUM_BASE_FORMATS (sizeof(baseFormats) / sizeof(baseFormats[0]))
160int baseFormat;
161struct 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]))
Brian Paul95210bc2003-04-21 14:50:12 +0000172int envColor = 0;
Brian Paulf13a3302000-06-27 15:55:47 +0000173GLfloat 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
182struct envModeInfo {
183 GLenum mode;
184 char *name;
185};
186
187/* allow for run-time check for GL_EXT_texture_env_add */
188int NUM_ENV_MODES = 5;
189struct 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
Brian Paul02e8a032000-06-27 17:04:43 +0000200static void checkErrors( void )
Brian Paulf13a3302000-06-27 15:55:47 +0000201{
202 GLenum error;
203
Brian Paulf13a3302000-06-27 15:55:47 +0000204 while ( (error = glGetError()) != GL_NO_ERROR ) {
205 fprintf( stderr, "Error: %s\n", (char *) gluErrorString( error ) );
206 }
207}
208
Brian Paulae188722000-09-13 23:01:52 +0000209static void drawString( const char *string, GLfloat x, GLfloat y,
210 const GLfloat color[4] )
Brian Paulf13a3302000-06-27 15:55:47 +0000211{
212 glColor4fv( color );
213 glRasterPos2f( x, y );
214
215 while ( *string ) {
216 glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_10, *string );
217 string++;
218 }
219}
220
Brian Paulae188722000-09-13 23:01:52 +0000221static void drawStringOutline( const char *string, GLfloat x, GLfloat y,
222 const GLfloat color[4],
223 const GLfloat outline[4] )
Brian Paulf13a3302000-06-27 15:55:47 +0000224{
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
232static 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
246static void end2D( void )
247{
248 glMatrixMode( GL_PROJECTION );
249 glPopMatrix();
250 glMatrixMode( GL_MODELVIEW );
251 glPopMatrix();
252}
253
254static 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 */
Brian Paul02e8a032000-06-27 17:04:43 +0000268static void keyboard( unsigned char c, int x, int y )
Brian Paulf13a3302000-06-27 15:55:47 +0000269{
270 switch ( c ) {
271 case 'c':
Brian Paul95210bc2003-04-21 14:50:12 +0000272 envColor++;
273 envColor = envColor % (int) NUM_ENV_COLORS;
Brian Paulf13a3302000-06-27 15:55:47 +0000274 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 */
Brian Paul02e8a032000-06-27 17:04:43 +0000300static void special( int key, int x, int y )
Brian Paulf13a3302000-06-27 15:55:47 +0000301{
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
Brian Paul02e8a032000-06-27 17:04:43 +0000331static void
Brian Paulf13a3302000-06-27 15:55:47 +0000332reshape( int w, int h )
333{
334 winWidth = w;
335 winHeight = h;
336 /* No need to call glViewPort here since "draw" calls it! */
337}
338
Brian Paulae188722000-09-13 23:01:52 +0000339static void loadTexture( int width, int height,
340 const struct formatInfo *format )
Brian Paulf13a3302000-06-27 15:55:47 +0000341{
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 textureFormat = GL_INTENSITY;
358 break;
359 case GL_ALPHA:
360 alphaSize = 1;
361 textureFormat = GL_ALPHA;
362 break;
363 case GL_LUMINANCE_ALPHA:
364 luminanceSize = 1;
365 alphaSize = 1;
366 textureFormat = GL_LUMINANCE_ALPHA;
367 break;
368 case GL_RGB:
369 rgbSize = 3;
370 textureFormat = GL_RGB;
371 break;
372 case GL_RGBA:
373 rgbSize = 3;
374 alphaSize = 1;
375 textureFormat = GL_RGBA;
376 break;
377 default:
378 fprintf(stderr, "bad internal format info\n");
379 return;
380 }
381
382 elementsPerGroup = luminanceSize + alphaSize + rgbSize;
383 elementSize = sizeof(GLubyte);
384 groupSize = elementsPerGroup * elementSize;
385 rowSize = width * groupSize;
386
387 if ( (texImage = (GLubyte *) malloc( height * rowSize ) ) == NULL ) {
388 fprintf( stderr, "texture malloc failed\n" );
389 return;
390 }
391
392 for ( i = 0 ; i < height ; i++ )
393 {
394 p = texImage + i * rowSize;
395
396 for ( j = 0 ; j < width ; j++ )
397 {
398 if ( luminanceSize > 0 )
399 {
400 /**
401 ** +-----+-----+
402 ** | | |
403 ** | W | LG |
404 ** | | |
405 ** +-----+-----+
406 ** | | |
407 ** | DG | B |
408 ** | | |
409 ** +-----+-----+
410 **/
411 if ( i > height / 2 ) {
412 if ( j < width / 2 ) {
413 p[0] = 0xff;
414 } else {
415 p[0] = 0xaa;
416 }
417 } else {
418 if ( j < width / 2 ) {
419 p[0] = 0x55;
420 } else {
421 p[0] = 0x00;
422 }
423 }
424 p += elementSize;
425 }
426
427 if ( rgbSize > 0 )
428 {
429 /**
430 ** +-----+-----+
431 ** | | |
432 ** | R | G |
433 ** | | |
434 ** +-----+-----+
435 ** | | |
436 ** | Y | B |
437 ** | | |
438 ** +-----+-----+
439 **/
440 if ( i > height / 2 ) {
441 if ( j < width / 2 ) {
442 p[0] = 0xff;
443 p[1] = 0x00;
444 p[2] = 0x00;
445 } else {
446 p[0] = 0x00;
447 p[1] = 0xff;
448 p[2] = 0x00;
449 }
450 } else {
451 if ( j < width / 2 ) {
452 p[0] = 0xff;
453 p[1] = 0xff;
454 p[2] = 0x00;
455 } else {
456 p[0] = 0x00;
457 p[1] = 0x00;
458 p[2] = 0xff;
459 }
460 }
461 p += 3 * elementSize;
462 }
463
464 if ( alphaSize > 0 )
465 {
466 /**
467 ** +-----------+
468 ** | W |
469 ** | +-----+ |
470 ** | | | |
471 ** | | B | |
472 ** | | | |
473 ** | +-----+ |
474 ** | |
475 ** +-----------+
476 **/
477 int i2 = i - height / 2;
478 int j2 = j - width / 2;
479 int h8 = height / 8;
480 int w8 = width / 8;
481 if ( -h8 <= i2 && i2 <= h8 && -w8 <= j2 && j2 <= w8 ) {
482 p[0] = 0x00;
483 } else if ( -2 * h8 <= i2 && i2 <= 2 * h8 && -2 * w8 <= j2 && j2 <= 2 * w8 ) {
484 p[0] = 0x55;
485 } else if ( -3 * h8 <= i2 && i2 <= 3 * h8 && -3 * w8 <= j2 && j2 <= 3 * w8 ) {
486 p[0] = 0xaa;
487 } else {
488 p[0] = 0xff;
489 }
490 p += elementSize;
491 }
492 }
493 }
494
495 glTexImage2D( GL_TEXTURE_2D, 0,
496 format->internalFormat, width, height, 0,
497 textureFormat, GL_UNSIGNED_BYTE, texImage );
498
499 free( texImage );
500}
501
Brian Paulae188722000-09-13 23:01:52 +0000502static void drawCheck( int w, int h, const GLfloat lightCheck[4],
503 const GLfloat darkCheck[4] )
Brian Paulf13a3302000-06-27 15:55:47 +0000504{
505 float dw = 2.0 / w;
506 float dh = 2.0 / h;
507 int i, j;
508
509 for ( i = 0 ; i < w ; i++ ) {
510 GLfloat x0 = -1.0 + i * dw;
511 GLfloat x1 = x0 + dw;
512
513 glBegin( GL_QUAD_STRIP );
514
515 for ( j = 0 ; j <= h ; j++ ) {
516 GLfloat y = -1.0 + j * dh;
517
518 if ( (i ^ j) & 1 ) {
519 glColor4fv( lightCheck );
520 } else {
521 glColor4fv( darkCheck );
522 }
523
524 glVertex2f( x0, y );
525 glVertex2f( x1, y );
526 }
527
528 glEnd();
529 }
530}
531
Gareth Hughes53933fe2001-03-28 20:25:14 +0000532static const char *lookupFormat( GLint format )
533{
534 switch ( format ) {
535 case GL_RGBA:
536 return "GL_RGBA";
537 case GL_RGB:
538 return "GL_RGB";
539 case GL_ALPHA:
540 return "GL_ALPHA";
541 case GL_LUMINANCE:
542 return "GL_LUMINANCE";
543 case GL_LUMINANCE_ALPHA:
544 return "GL_LUMINANCE_ALPHA";
545 case GL_INTENSITY:
546 return "GL_INTENSITY";
547 case GL_COLOR_INDEX:
548 return "GL_COLOR_INDEX";
549 case GL_BGRA:
550 return "GL_BGRA";
551 case GL_BGR:
552 return "GL_BGR";
553 default:
554 return "unknown format";
555 }
556}
557
Brian Paulf13a3302000-06-27 15:55:47 +0000558static void drawSample( int x, int y, int w, int h,
Brian Paulae188722000-09-13 23:01:52 +0000559 const struct formatInfo *format,
560 const struct envModeInfo *envMode )
Brian Paulf13a3302000-06-27 15:55:47 +0000561{
562 glViewport( x, y, w, h );
563 glScissor( x, y, w, h );
564
565 glClearColor( 0.1, 0.1, 0.1, 1.0 );
566 glClear( GL_COLOR_BUFFER_BIT );
567
568 begin2D( w, h );
569 drawString( format->name, 10, h - 15, labelInfoColor );
570 drawString( envMode->name, 10, 5, labelInfoColor );
571 end2D();
572
573 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, envMode->mode );
574 glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, envColors[envColor] );
575
Brian Paul63c113a2000-09-26 15:25:01 +0000576 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
577 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
Brian Paulf13a3302000-06-27 15:55:47 +0000578
579 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
580 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
581
582 loadTexture( textureWidth, textureHeight, format );
583
584 if ( drawBackground ) {
585 drawCheck( 15, 15, lightCheck, darkCheck );
586 }
587 if ( drawBlended ) {
588 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
589 glEnable( GL_BLEND );
590 }
591 if ( drawSmooth ) {
592 glShadeModel( GL_SMOOTH );
593 }
Brian Paulf4f4dab2000-08-21 21:05:39 +0000594 else {
595 glShadeModel( GL_FLAT );
596 glColor4f(1, 1, 1, 1);
597 }
Brian Paulf13a3302000-06-27 15:55:47 +0000598 if ( drawTextured ) {
599 glEnable( GL_TEXTURE_2D );
600 }
601
Brian Paul5479e932001-03-27 17:38:28 +0000602 /*
603 * if (drawSmooth) then draw quad which goes from purple at the
604 * bottom (100% alpha) to green at the top (50% alpha).
605 */
Brian Paulf13a3302000-06-27 15:55:47 +0000606 glBegin( GL_QUADS );
Brian Paulf4f4dab2000-08-21 21:05:39 +0000607 if ( drawSmooth ) glColor4f( 1.0, 0.0, 1.0, 1.0 );
Brian Paulf13a3302000-06-27 15:55:47 +0000608 glTexCoord2f( 0.0, 0.0 );
609 glVertex2f( -0.8, -0.8 );
610
Brian Paulf4f4dab2000-08-21 21:05:39 +0000611 if ( drawSmooth ) glColor4f( 1.0, 0.0, 1.0, 1.0 );
Brian Paulf13a3302000-06-27 15:55:47 +0000612 glTexCoord2f( 1.0, 0.0 );
613 glVertex2f( 0.8, -0.8 );
614
Brian Paul5479e932001-03-27 17:38:28 +0000615 if ( drawSmooth ) glColor4f( 0.0, 1.0, 0.0, 0.5 );
Brian Paulf13a3302000-06-27 15:55:47 +0000616 glTexCoord2f( 1.0, 1.0 );
617 glVertex2f( 0.8, 0.8 );
618
Brian Paul5479e932001-03-27 17:38:28 +0000619 if ( drawSmooth ) glColor4f( 0.0, 1.0, 0.0, 0.5 );
Brian Paulf13a3302000-06-27 15:55:47 +0000620 glTexCoord2f( 0.0, 1.0 );
621 glVertex2f( -0.8, 0.8 );
622 glEnd();
623
624 glDisable( GL_BLEND );
625 glShadeModel( GL_FLAT );
626 glDisable( GL_TEXTURE_2D );
627
Brian Paul043654b2000-08-21 20:04:55 +0000628 if ( envMode->mode == GL_DECAL &&
629 (format->baseFormat == GL_ALPHA ||
630 format->baseFormat == GL_LUMINANCE ||
631 format->baseFormat == GL_LUMINANCE_ALPHA ||
632 format->baseFormat == GL_INTENSITY)) {
633 /* undefined format/mode combination */
634 begin2D( w, h );
635 drawStringOutline( "UNDEFINED MODE", 15, h / 2,
636 labelLevelColor0, labelLevelColor1 );
637 end2D();
638 }
639 else if ( displayLevelInfo ) {
Gareth Hughes53933fe2001-03-28 20:25:14 +0000640 GLint width, height, border, format;
Brian Paulf13a3302000-06-27 15:55:47 +0000641 GLint redSize, greenSize, blueSize, alphaSize;
642 GLint luminanceSize, intensitySize;
643 char buf[255];
644
645 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width );
646 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height );
647 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER, &border );
Gareth Hughes53933fe2001-03-28 20:25:14 +0000648 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &format );
Brian Paulf13a3302000-06-27 15:55:47 +0000649 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &redSize );
650 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE, &greenSize );
651 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE, &blueSize );
652 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &alphaSize );
653 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_LUMINANCE_SIZE, &luminanceSize );
654 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_INTENSITY_SIZE, &intensitySize );
655
656 begin2D( w, h );
657 sprintf( buf, "dimensions: %d x %d", width, height );
658 drawStringOutline( buf, 15, h / 2 + 20, labelLevelColor0, labelLevelColor1 );
659
660 sprintf( buf, "border: %d", border );
661 drawStringOutline( buf, 15, h / 2 + 10, labelLevelColor0, labelLevelColor1 );
662
Gareth Hughes53933fe2001-03-28 20:25:14 +0000663 sprintf( buf, "internal format:" );
Brian Paulf13a3302000-06-27 15:55:47 +0000664 drawStringOutline( buf, 15, h / 2, labelLevelColor0, labelLevelColor1 );
665
Gareth Hughes53933fe2001-03-28 20:25:14 +0000666 sprintf( buf, " %s", lookupFormat( format ) );
Brian Paulf13a3302000-06-27 15:55:47 +0000667 drawStringOutline( buf, 15, h / 2 - 10, labelLevelColor0, labelLevelColor1 );
668
Gareth Hughes53933fe2001-03-28 20:25:14 +0000669 sprintf( buf, "sizes:" );
670 drawStringOutline( buf, 15, h / 2 - 20, labelLevelColor0, labelLevelColor1 );
671
Brian Paulf13a3302000-06-27 15:55:47 +0000672 sprintf( buf, " %d / %d / %d / %d / %d / %d",
673 redSize, greenSize, blueSize, alphaSize,
674 luminanceSize, intensitySize );
Gareth Hughes53933fe2001-03-28 20:25:14 +0000675 drawStringOutline( buf, 15, h / 2 - 30, labelLevelColor0, labelLevelColor1 );
Brian Paulf13a3302000-06-27 15:55:47 +0000676
677 end2D();
678 }
679}
680
681static void display( void )
682{
683 int numX = NUM_ENV_MODES, numY = NUM_BASE_FORMATS;
684 float xBase = (float) winWidth * 0.01;
685 float xOffset = (winWidth - xBase) / numX;
686 float xSize = max( xOffset - xBase, 1 );
687 float yBase = (float) winHeight * 0.01;
688 float yOffset = (winHeight - yBase) / numY;
689 float ySize = max( yOffset - yBase, 1 );
690 float x, y;
691 int i, j;
692
693 glViewport( 0, 0, winWidth, winHeight );
694 glDisable( GL_SCISSOR_TEST );
695 glClearColor( 0.0, 0.0, 0.0, 0.0 );
696 glClear( GL_COLOR_BUFFER_BIT );
697 glEnable( GL_SCISSOR_TEST );
698
699 x = xBase;
700 y = (winHeight - 1) - yOffset;
701
702 for ( i = 0 ; i < NUM_BASE_FORMATS ; i++ )
703 {
704 struct formatInfo *format;
705
706 if ( i == baseFormat ) {
707 labelInfoColor = labelColor1;
708 } else {
709 labelInfoColor = labelColor0;
710 }
711
712 format = &baseFormats[i].format[baseFormats[i].current];
713
714 for ( j = 0 ; j < NUM_ENV_MODES ; j++ ) {
715 struct envModeInfo *envMode;
716
717 envMode = &envModes[j];
718 drawSample( x, y, xSize, ySize, format, envMode );
719 x += xOffset;
720 }
721
722 x = xBase;
723 y -= yOffset;
724 }
725
726 if ( doubleBuffered ) {
727 glutSwapBuffers();
728 } else {
729 glFlush();
730 }
731
732 checkErrors();
733}
734
735static void usage( char *name )
736{
737 fprintf( stderr, "usage: %s [ options ]\n", name );
738 fprintf( stderr, "\n" );
739 fprintf( stderr, "options:\n" );
740 fprintf( stderr, " -sb single buffered\n" );
741 fprintf( stderr, " -db double buffered\n" );
742 fprintf( stderr, " -info print OpenGL driver info\n" );
743}
744
745static void instructions( void )
746{
747 fprintf( stderr, "texenv - texture environment and internal format test\n" );
748 fprintf( stderr, "\n" );
749 fprintf( stderr, " [c] - cycle through background colors\n" );
750 fprintf( stderr, " [g] - toggle background\n" );
751 fprintf( stderr, " [b] - toggle blend\n" );
752 fprintf( stderr, " [s] - toggle smooth shading\n" );
753 fprintf( stderr, " [t] - toggle texturing\n" );
754 fprintf( stderr, " [i] - toggle information display\n" );
Brian Paulae188722000-09-13 23:01:52 +0000755 fprintf( stderr, " up/down - select row\n" );
756 fprintf( stderr, " left/right - change row's internal format\n" );
Brian Paulf13a3302000-06-27 15:55:47 +0000757}
758
759int main( int argc, char *argv[] )
760{
761 GLboolean info = GL_FALSE;
762 int i;
763
764 glutInit( &argc, argv );
765
766 for ( i = 1 ; i < argc ; i++ ) {
767 if ( !strcmp( "-sb", argv[i] ) ) {
768 doubleBuffered = GL_FALSE;
769 } else if ( !strcmp( "-db", argv[i] ) ) {
770 doubleBuffered = GL_TRUE;
771 } else if ( !strcmp( "-info", argv[i] ) ) {
772 info = GL_TRUE;
773 } else {
774 usage( argv[0] );
775 exit( 1 );
776 }
777 }
778
779 if ( doubleBuffered ) {
780 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
781 } else {
782 glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
783 }
784
785 glutInitWindowSize( winWidth, winHeight );
Brian Paul345a71a2000-09-15 23:59:46 +0000786 glutInitWindowPosition( 0, 0 );
Brian Paulf13a3302000-06-27 15:55:47 +0000787 glutCreateWindow( "Texture Environment Test" );
788
789 initialize();
790 instructions();
791
792 if ( info ) {
793 printf( "\n" );
794 printf( "GL_RENDERER = %s\n", (char *) glGetString( GL_RENDERER ) );
795 printf( "GL_VERSION = %s\n", (char *) glGetString( GL_VERSION ) );
796 printf( "GL_VENDOR = %s\n", (char *) glGetString( GL_VENDOR ) ) ;
797 printf( "GL_EXTENSIONS = %s\n", (char *) glGetString( GL_EXTENSIONS ) );
798 }
799
800#if GL_EXT_texture_env_add
801 if ( !glutExtensionSupported( "GL_EXT_texture_env_add" ) ) {
802 fprintf( stderr, "missing extension: GL_EXT_texture_env_add\n" );
803 NUM_ENV_MODES--;
804 }
805#endif
806
807 glutDisplayFunc( display );
808 glutReshapeFunc( reshape );
809 glutKeyboardFunc( keyboard );
810 glutSpecialFunc( special );
811 glutMainLoop();
812
813 return 0;
814}