blob: c4174dc173d6a13ee5cd2264d0766b530ebba691 [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;
Brian Paulf34767f2008-06-04 09:30:04 -060079int win;
Brian Paulf13a3302000-06-27 15:55:47 +000080
81struct formatInfo {
82 GLenum baseFormat;
83 GLenum internalFormat;
84 char *name;
85};
86
87#define NUM_LUMINANCE_FORMATS (sizeof(luminanceFormats) / sizeof(luminanceFormats[0]))
88struct formatInfo luminanceFormats[] =
89{
90 { GL_LUMINANCE, GL_LUMINANCE, "LUMINANCE" },
91 { GL_LUMINANCE, GL_LUMINANCE4, "LUMINANCE4" },
92 { GL_LUMINANCE, GL_LUMINANCE8, "LUMINANCE8" },
93 { GL_LUMINANCE, GL_LUMINANCE12, "LUMINANCE12" },
94 { GL_LUMINANCE, GL_LUMINANCE16, "LUMINANCE16" },
95};
96
97#define NUM_ALPHA_FORMATS (sizeof(alphaFormats) / sizeof(alphaFormats[0]))
98struct formatInfo alphaFormats[] =
99{
100 { GL_ALPHA, GL_ALPHA, "ALPHA" },
101 { GL_ALPHA, GL_ALPHA4, "ALPHA4" },
102 { GL_ALPHA, GL_ALPHA8, "ALPHA8" },
103 { GL_ALPHA, GL_ALPHA12, "ALPHA12" },
104 { GL_ALPHA, GL_ALPHA16, "ALPHA16" },
105};
106
107#define NUM_INTENSITY_FORMATS (sizeof(intensityFormats) / sizeof(intensityFormats[0]))
108struct formatInfo intensityFormats[] =
109{
110 { GL_INTENSITY, GL_INTENSITY, "INTENSITY" },
111 { GL_INTENSITY, GL_INTENSITY4, "INTENSITY4" },
112 { GL_INTENSITY, GL_INTENSITY8, "INTENSITY8" },
113 { GL_INTENSITY, GL_INTENSITY12, "INTENSITY12" },
114 { GL_INTENSITY, GL_INTENSITY16, "INTENSITY16" },
115};
116
117#define NUM_LUMINANCE_ALPHA_FORMATS (sizeof(luminanceAlphaFormats) / sizeof(luminanceAlphaFormats[0]))
118struct formatInfo luminanceAlphaFormats[] =
119{
120 { GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, "LUMINANCE_ALPHA" },
121 { GL_LUMINANCE_ALPHA, GL_LUMINANCE4_ALPHA4, "LUMINANCE4_ALPHA4" },
122 { GL_LUMINANCE_ALPHA, GL_LUMINANCE6_ALPHA2, "LUMINANCE6_ALPHA2" },
123 { GL_LUMINANCE_ALPHA, GL_LUMINANCE8_ALPHA8, "LUMINANCE8_ALPHA8" },
124 { GL_LUMINANCE_ALPHA, GL_LUMINANCE12_ALPHA4, "LUMINANCE12_ALPHA4" },
125 { GL_LUMINANCE_ALPHA, GL_LUMINANCE12_ALPHA12, "LUMINANCE12_ALPHA12" },
126 { GL_LUMINANCE_ALPHA, GL_LUMINANCE16_ALPHA16, "LUMINANCE16_ALPHA16" },
127};
128
129#define NUM_RGB_FORMATS (sizeof(rgbFormats) / sizeof(rgbFormats[0]))
130struct formatInfo rgbFormats[] =
131{
132 { GL_RGB, GL_RGB, "RGB" },
133 { GL_RGB, GL_R3_G3_B2, "R3_G3_B2" },
134 { GL_RGB, GL_RGB4, "RGB4" },
135 { GL_RGB, GL_RGB5, "RGB5" },
136 { GL_RGB, GL_RGB8, "RGB8" },
137 { GL_RGB, GL_RGB10, "RGB10" },
138 { GL_RGB, GL_RGB12, "RGB12" },
139 { GL_RGB, GL_RGB16, "RGB16" },
140};
141
142#define NUM_RGBA_FORMATS (sizeof(rgbaFormats) / sizeof(rgbaFormats[0]))
143struct formatInfo rgbaFormats[] =
144{
Brian Paul345a71a2000-09-15 23:59:46 +0000145 { GL_RGBA, GL_RGBA, "RGBA" },
Brian Paulf13a3302000-06-27 15:55:47 +0000146 { GL_RGBA, GL_RGBA2, "RGBA2" },
147 { GL_RGBA, GL_RGBA4, "RGBA4" },
148 { GL_RGBA, GL_RGB5_A1, "RGB5_A1" },
149 { GL_RGBA, GL_RGBA8, "RGBA8" },
150 { GL_RGBA, GL_RGB10_A2, "RGB10_A2" },
151 { GL_RGBA, GL_RGBA12, "RGBA12" },
152 { GL_RGBA, GL_RGBA16, "RGBA16" },
153};
154
155struct baseFormatInfo {
156 struct formatInfo *format;
157 int current, number;
158};
159
160#define NUM_BASE_FORMATS (sizeof(baseFormats) / sizeof(baseFormats[0]))
161int baseFormat;
162struct baseFormatInfo baseFormats[] =
163{
164 { luminanceFormats, 0, NUM_LUMINANCE_FORMATS },
165 { alphaFormats, 0, NUM_ALPHA_FORMATS },
166 { intensityFormats, 0, NUM_INTENSITY_FORMATS },
167 { luminanceAlphaFormats, 0, NUM_LUMINANCE_ALPHA_FORMATS },
168 { rgbFormats, 0, NUM_RGB_FORMATS },
169 { rgbaFormats, 0, NUM_RGBA_FORMATS },
170};
171
172#define NUM_ENV_COLORS (sizeof(envColors) / sizeof(envColors[0]))
Brian Paul95210bc2003-04-21 14:50:12 +0000173int envColor = 0;
Brian Paulf13a3302000-06-27 15:55:47 +0000174GLfloat envColors[][4] =
175{
176 { 0.0, 0.0, 0.0, 1.0 },
177 { 1.0, 0.0, 0.0, 1.0 },
178 { 0.0, 1.0, 0.0, 1.0 },
179 { 0.0, 0.0, 1.0, 1.0 },
180 { 1.0, 1.0, 1.0, 1.0 },
181};
182
183struct envModeInfo {
184 GLenum mode;
185 char *name;
186};
187
188/* allow for run-time check for GL_EXT_texture_env_add */
189int NUM_ENV_MODES = 5;
190struct envModeInfo envModes[] =
191{
192 { GL_REPLACE, "REPLACE" },
193 { GL_MODULATE, "MODULATE" },
194 { GL_BLEND, "BLEND" },
195 { GL_DECAL, "DECAL" },
196#if GL_EXT_texture_env_add
197 { GL_ADD, "ADD" },
198#endif
199};
200
Brian Paul02e8a032000-06-27 17:04:43 +0000201static void checkErrors( void )
Brian Paulf13a3302000-06-27 15:55:47 +0000202{
203 GLenum error;
204
Brian Paulf13a3302000-06-27 15:55:47 +0000205 while ( (error = glGetError()) != GL_NO_ERROR ) {
206 fprintf( stderr, "Error: %s\n", (char *) gluErrorString( error ) );
207 }
208}
209
Brian Paulae188722000-09-13 23:01:52 +0000210static void drawString( const char *string, GLfloat x, GLfloat y,
211 const GLfloat color[4] )
Brian Paulf13a3302000-06-27 15:55:47 +0000212{
213 glColor4fv( color );
214 glRasterPos2f( x, y );
215
216 while ( *string ) {
217 glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_10, *string );
218 string++;
219 }
220}
221
Brian Paulae188722000-09-13 23:01:52 +0000222static void drawStringOutline( const char *string, GLfloat x, GLfloat y,
223 const GLfloat color[4],
224 const GLfloat outline[4] )
Brian Paulf13a3302000-06-27 15:55:47 +0000225{
226 drawString( string, x - 1, y, outline );
227 drawString( string, x + 1, y, outline );
228 drawString( string, x, y - 1, outline );
229 drawString( string, x, y + 1, outline );
230 drawString( string, x, y, color );
231}
232
233static void begin2D( int width, int height )
234{
235 glMatrixMode( GL_PROJECTION );
236
237 glPushMatrix();
238 glLoadIdentity();
239
240 glOrtho( 0, width, 0, height, -1, 1 );
241 glMatrixMode( GL_MODELVIEW );
242
243 glPushMatrix();
244 glLoadIdentity();
245}
246
247static void end2D( void )
248{
249 glMatrixMode( GL_PROJECTION );
250 glPopMatrix();
251 glMatrixMode( GL_MODELVIEW );
252 glPopMatrix();
253}
254
255static void initialize( void )
256{
257 glMatrixMode( GL_PROJECTION );
258 glLoadIdentity();
259
260 glOrtho( -1.5, 1.5, -1.5, 1.5, -1.5, 1.5 );
261
262 glMatrixMode(GL_MODELVIEW);
263 glLoadIdentity();
264
265 glShadeModel( GL_FLAT );
266}
267
268/* ARGSUSED1 */
Brian Paul02e8a032000-06-27 17:04:43 +0000269static void keyboard( unsigned char c, int x, int y )
Brian Paulf13a3302000-06-27 15:55:47 +0000270{
271 switch ( c ) {
272 case 'c':
Brian Paul95210bc2003-04-21 14:50:12 +0000273 envColor++;
274 envColor = envColor % (int) NUM_ENV_COLORS;
Brian Paulf13a3302000-06-27 15:55:47 +0000275 break;
276 case 'g':
277 drawBackground = !drawBackground;
278 break;
279 case 'b':
280 drawBlended = !drawBlended;
281 break;
282 case 's':
283 drawSmooth = !drawSmooth;
284 break;
285 case 't':
286 drawTextured = !drawTextured;
287 break;
288 case 'i':
289 displayLevelInfo = !displayLevelInfo;
290 break;
291 case 27: /* Escape key should force exit. */
Brian Paulf34767f2008-06-04 09:30:04 -0600292 glutDestroyWindow(win);
Brian Paulf13a3302000-06-27 15:55:47 +0000293 exit(0);
294 break;
295 default:
296 break;
297 }
298 glutPostRedisplay();
299}
300
301/* ARGSUSED1 */
Brian Paul02e8a032000-06-27 17:04:43 +0000302static void special( int key, int x, int y )
Brian Paulf13a3302000-06-27 15:55:47 +0000303{
304 switch ( key ) {
305 case GLUT_KEY_DOWN:
306 if ( ++baseFormat > NUM_BASE_FORMATS - 1 ) {
307 baseFormat = 0;
308 }
309 break;
310 case GLUT_KEY_UP:
311 if ( --baseFormat < 0 ) {
312 baseFormat = NUM_BASE_FORMATS - 1;
313 }
314 break;
315 case GLUT_KEY_LEFT:
316 --baseFormats[baseFormat].current;
317 if ( baseFormats[baseFormat].current < 0 ) {
318 baseFormats[baseFormat].current = baseFormats[baseFormat].number - 1;
319 }
320 break;
321 case GLUT_KEY_RIGHT:
322 ++baseFormats[baseFormat].current;
323 if ( baseFormats[baseFormat].current > baseFormats[baseFormat].number - 1 ) {
324 baseFormats[baseFormat].current = 0;
325 }
326 break;
327 default:
328 break;
329 }
330 glutPostRedisplay();
331}
332
Brian Paul02e8a032000-06-27 17:04:43 +0000333static void
Brian Paulf13a3302000-06-27 15:55:47 +0000334reshape( int w, int h )
335{
336 winWidth = w;
337 winHeight = h;
338 /* No need to call glViewPort here since "draw" calls it! */
339}
340
Brian Paulae188722000-09-13 23:01:52 +0000341static void loadTexture( int width, int height,
342 const struct formatInfo *format )
Brian Paulf13a3302000-06-27 15:55:47 +0000343{
344 int luminanceSize = 0;
345 int alphaSize = 0;
346 int rgbSize = 0;
347 GLenum textureFormat;
348 GLubyte *texImage, *p;
349 int elementsPerGroup, elementSize, groupSize, rowSize;
350 int i, j;
351
352 switch ( format->baseFormat ) {
353 case GL_LUMINANCE:
354 luminanceSize = 1;
355 textureFormat = GL_LUMINANCE;
356 break;
357 case GL_INTENSITY:
358 luminanceSize = 1;
Brian Paul22dad682004-06-11 15:07:23 +0000359 /* Note: format=GL_INTENSITY for glTexImage is not legal */
360 textureFormat = GL_LUMINANCE;
Brian Paulf13a3302000-06-27 15:55:47 +0000361 break;
362 case GL_ALPHA:
363 alphaSize = 1;
364 textureFormat = GL_ALPHA;
365 break;
366 case GL_LUMINANCE_ALPHA:
367 luminanceSize = 1;
368 alphaSize = 1;
369 textureFormat = GL_LUMINANCE_ALPHA;
370 break;
371 case GL_RGB:
372 rgbSize = 3;
373 textureFormat = GL_RGB;
374 break;
375 case GL_RGBA:
376 rgbSize = 3;
377 alphaSize = 1;
378 textureFormat = GL_RGBA;
379 break;
380 default:
381 fprintf(stderr, "bad internal format info\n");
382 return;
383 }
384
385 elementsPerGroup = luminanceSize + alphaSize + rgbSize;
386 elementSize = sizeof(GLubyte);
387 groupSize = elementsPerGroup * elementSize;
388 rowSize = width * groupSize;
389
390 if ( (texImage = (GLubyte *) malloc( height * rowSize ) ) == NULL ) {
391 fprintf( stderr, "texture malloc failed\n" );
392 return;
393 }
394
395 for ( i = 0 ; i < height ; i++ )
396 {
397 p = texImage + i * rowSize;
398
399 for ( j = 0 ; j < width ; j++ )
400 {
401 if ( luminanceSize > 0 )
402 {
403 /**
404 ** +-----+-----+
405 ** | | |
406 ** | W | LG |
407 ** | | |
408 ** +-----+-----+
409 ** | | |
410 ** | DG | B |
411 ** | | |
412 ** +-----+-----+
413 **/
414 if ( i > height / 2 ) {
415 if ( j < width / 2 ) {
416 p[0] = 0xff;
417 } else {
418 p[0] = 0xaa;
419 }
420 } else {
421 if ( j < width / 2 ) {
422 p[0] = 0x55;
423 } else {
424 p[0] = 0x00;
425 }
426 }
427 p += elementSize;
428 }
429
430 if ( rgbSize > 0 )
431 {
432 /**
433 ** +-----+-----+
434 ** | | |
435 ** | R | G |
436 ** | | |
437 ** +-----+-----+
438 ** | | |
439 ** | Y | B |
440 ** | | |
441 ** +-----+-----+
442 **/
443 if ( i > height / 2 ) {
444 if ( j < width / 2 ) {
445 p[0] = 0xff;
446 p[1] = 0x00;
447 p[2] = 0x00;
448 } else {
449 p[0] = 0x00;
450 p[1] = 0xff;
451 p[2] = 0x00;
452 }
453 } else {
454 if ( j < width / 2 ) {
455 p[0] = 0xff;
456 p[1] = 0xff;
457 p[2] = 0x00;
458 } else {
459 p[0] = 0x00;
460 p[1] = 0x00;
461 p[2] = 0xff;
462 }
463 }
464 p += 3 * elementSize;
465 }
466
467 if ( alphaSize > 0 )
468 {
469 /**
470 ** +-----------+
471 ** | W |
472 ** | +-----+ |
473 ** | | | |
474 ** | | B | |
475 ** | | | |
476 ** | +-----+ |
477 ** | |
478 ** +-----------+
479 **/
480 int i2 = i - height / 2;
481 int j2 = j - width / 2;
482 int h8 = height / 8;
483 int w8 = width / 8;
484 if ( -h8 <= i2 && i2 <= h8 && -w8 <= j2 && j2 <= w8 ) {
485 p[0] = 0x00;
486 } else if ( -2 * h8 <= i2 && i2 <= 2 * h8 && -2 * w8 <= j2 && j2 <= 2 * w8 ) {
487 p[0] = 0x55;
488 } else if ( -3 * h8 <= i2 && i2 <= 3 * h8 && -3 * w8 <= j2 && j2 <= 3 * w8 ) {
489 p[0] = 0xaa;
490 } else {
491 p[0] = 0xff;
492 }
493 p += elementSize;
494 }
495 }
496 }
497
498 glTexImage2D( GL_TEXTURE_2D, 0,
499 format->internalFormat, width, height, 0,
500 textureFormat, GL_UNSIGNED_BYTE, texImage );
501
502 free( texImage );
503}
504
Brian Paulae188722000-09-13 23:01:52 +0000505static void drawCheck( int w, int h, const GLfloat lightCheck[4],
506 const GLfloat darkCheck[4] )
Brian Paulf13a3302000-06-27 15:55:47 +0000507{
508 float dw = 2.0 / w;
509 float dh = 2.0 / h;
510 int i, j;
511
512 for ( i = 0 ; i < w ; i++ ) {
513 GLfloat x0 = -1.0 + i * dw;
514 GLfloat x1 = x0 + dw;
515
516 glBegin( GL_QUAD_STRIP );
517
518 for ( j = 0 ; j <= h ; j++ ) {
519 GLfloat y = -1.0 + j * dh;
520
521 if ( (i ^ j) & 1 ) {
522 glColor4fv( lightCheck );
523 } else {
524 glColor4fv( darkCheck );
525 }
526
527 glVertex2f( x0, y );
528 glVertex2f( x1, y );
529 }
530
531 glEnd();
532 }
533}
534
Gareth Hughes53933fe2001-03-28 20:25:14 +0000535static const char *lookupFormat( GLint format )
536{
537 switch ( format ) {
538 case GL_RGBA:
539 return "GL_RGBA";
540 case GL_RGB:
541 return "GL_RGB";
542 case GL_ALPHA:
543 return "GL_ALPHA";
544 case GL_LUMINANCE:
545 return "GL_LUMINANCE";
546 case GL_LUMINANCE_ALPHA:
547 return "GL_LUMINANCE_ALPHA";
548 case GL_INTENSITY:
549 return "GL_INTENSITY";
550 case GL_COLOR_INDEX:
551 return "GL_COLOR_INDEX";
552 case GL_BGRA:
553 return "GL_BGRA";
554 case GL_BGR:
555 return "GL_BGR";
556 default:
557 return "unknown format";
558 }
559}
560
Brian Paulf13a3302000-06-27 15:55:47 +0000561static void drawSample( int x, int y, int w, int h,
Brian Paulae188722000-09-13 23:01:52 +0000562 const struct formatInfo *format,
563 const struct envModeInfo *envMode )
Brian Paulf13a3302000-06-27 15:55:47 +0000564{
565 glViewport( x, y, w, h );
566 glScissor( x, y, w, h );
567
568 glClearColor( 0.1, 0.1, 0.1, 1.0 );
569 glClear( GL_COLOR_BUFFER_BIT );
570
571 begin2D( w, h );
572 drawString( format->name, 10, h - 15, labelInfoColor );
573 drawString( envMode->name, 10, 5, labelInfoColor );
574 end2D();
575
576 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, envMode->mode );
577 glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, envColors[envColor] );
578
Brian Paul63c113a2000-09-26 15:25:01 +0000579 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
580 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
Brian Paulf13a3302000-06-27 15:55:47 +0000581
582 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
583 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
584
585 loadTexture( textureWidth, textureHeight, format );
586
587 if ( drawBackground ) {
588 drawCheck( 15, 15, lightCheck, darkCheck );
589 }
590 if ( drawBlended ) {
591 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
592 glEnable( GL_BLEND );
593 }
594 if ( drawSmooth ) {
595 glShadeModel( GL_SMOOTH );
596 }
Brian Paulf4f4dab2000-08-21 21:05:39 +0000597 else {
598 glShadeModel( GL_FLAT );
599 glColor4f(1, 1, 1, 1);
600 }
Brian Paulf13a3302000-06-27 15:55:47 +0000601 if ( drawTextured ) {
602 glEnable( GL_TEXTURE_2D );
603 }
604
Brian Paul5479e932001-03-27 17:38:28 +0000605 /*
606 * if (drawSmooth) then draw quad which goes from purple at the
607 * bottom (100% alpha) to green at the top (50% alpha).
608 */
Brian Paulf13a3302000-06-27 15:55:47 +0000609 glBegin( GL_QUADS );
Brian Paulf4f4dab2000-08-21 21:05:39 +0000610 if ( drawSmooth ) glColor4f( 1.0, 0.0, 1.0, 1.0 );
Brian Paulf13a3302000-06-27 15:55:47 +0000611 glTexCoord2f( 0.0, 0.0 );
612 glVertex2f( -0.8, -0.8 );
613
Brian Paulf4f4dab2000-08-21 21:05:39 +0000614 if ( drawSmooth ) glColor4f( 1.0, 0.0, 1.0, 1.0 );
Brian Paulf13a3302000-06-27 15:55:47 +0000615 glTexCoord2f( 1.0, 0.0 );
616 glVertex2f( 0.8, -0.8 );
617
Brian Paul5479e932001-03-27 17:38:28 +0000618 if ( drawSmooth ) glColor4f( 0.0, 1.0, 0.0, 0.5 );
Brian Paulf13a3302000-06-27 15:55:47 +0000619 glTexCoord2f( 1.0, 1.0 );
620 glVertex2f( 0.8, 0.8 );
621
Brian Paul5479e932001-03-27 17:38:28 +0000622 if ( drawSmooth ) glColor4f( 0.0, 1.0, 0.0, 0.5 );
Brian Paulf13a3302000-06-27 15:55:47 +0000623 glTexCoord2f( 0.0, 1.0 );
624 glVertex2f( -0.8, 0.8 );
625 glEnd();
626
627 glDisable( GL_BLEND );
628 glShadeModel( GL_FLAT );
629 glDisable( GL_TEXTURE_2D );
630
Brian Paul043654b2000-08-21 20:04:55 +0000631 if ( envMode->mode == GL_DECAL &&
632 (format->baseFormat == GL_ALPHA ||
633 format->baseFormat == GL_LUMINANCE ||
634 format->baseFormat == GL_LUMINANCE_ALPHA ||
635 format->baseFormat == GL_INTENSITY)) {
636 /* undefined format/mode combination */
637 begin2D( w, h );
638 drawStringOutline( "UNDEFINED MODE", 15, h / 2,
639 labelLevelColor0, labelLevelColor1 );
640 end2D();
641 }
642 else if ( displayLevelInfo ) {
Gareth Hughes53933fe2001-03-28 20:25:14 +0000643 GLint width, height, border, format;
Brian Paulf13a3302000-06-27 15:55:47 +0000644 GLint redSize, greenSize, blueSize, alphaSize;
645 GLint luminanceSize, intensitySize;
646 char buf[255];
647
648 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width );
649 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height );
650 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER, &border );
Gareth Hughes53933fe2001-03-28 20:25:14 +0000651 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &format );
Brian Paulf13a3302000-06-27 15:55:47 +0000652 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &redSize );
653 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE, &greenSize );
654 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE, &blueSize );
655 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &alphaSize );
656 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_LUMINANCE_SIZE, &luminanceSize );
657 glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_INTENSITY_SIZE, &intensitySize );
658
659 begin2D( w, h );
660 sprintf( buf, "dimensions: %d x %d", width, height );
661 drawStringOutline( buf, 15, h / 2 + 20, labelLevelColor0, labelLevelColor1 );
662
663 sprintf( buf, "border: %d", border );
664 drawStringOutline( buf, 15, h / 2 + 10, labelLevelColor0, labelLevelColor1 );
665
Gareth Hughes53933fe2001-03-28 20:25:14 +0000666 sprintf( buf, "internal format:" );
Brian Paulf13a3302000-06-27 15:55:47 +0000667 drawStringOutline( buf, 15, h / 2, labelLevelColor0, labelLevelColor1 );
668
Gareth Hughes53933fe2001-03-28 20:25:14 +0000669 sprintf( buf, " %s", lookupFormat( format ) );
Brian Paulf13a3302000-06-27 15:55:47 +0000670 drawStringOutline( buf, 15, h / 2 - 10, labelLevelColor0, labelLevelColor1 );
671
Gareth Hughes53933fe2001-03-28 20:25:14 +0000672 sprintf( buf, "sizes:" );
673 drawStringOutline( buf, 15, h / 2 - 20, labelLevelColor0, labelLevelColor1 );
674
Brian Paulf13a3302000-06-27 15:55:47 +0000675 sprintf( buf, " %d / %d / %d / %d / %d / %d",
676 redSize, greenSize, blueSize, alphaSize,
677 luminanceSize, intensitySize );
Gareth Hughes53933fe2001-03-28 20:25:14 +0000678 drawStringOutline( buf, 15, h / 2 - 30, labelLevelColor0, labelLevelColor1 );
Brian Paulf13a3302000-06-27 15:55:47 +0000679
680 end2D();
681 }
682}
683
684static void display( void )
685{
686 int numX = NUM_ENV_MODES, numY = NUM_BASE_FORMATS;
687 float xBase = (float) winWidth * 0.01;
688 float xOffset = (winWidth - xBase) / numX;
689 float xSize = max( xOffset - xBase, 1 );
690 float yBase = (float) winHeight * 0.01;
691 float yOffset = (winHeight - yBase) / numY;
692 float ySize = max( yOffset - yBase, 1 );
693 float x, y;
694 int i, j;
695
696 glViewport( 0, 0, winWidth, winHeight );
697 glDisable( GL_SCISSOR_TEST );
698 glClearColor( 0.0, 0.0, 0.0, 0.0 );
699 glClear( GL_COLOR_BUFFER_BIT );
700 glEnable( GL_SCISSOR_TEST );
701
702 x = xBase;
703 y = (winHeight - 1) - yOffset;
704
705 for ( i = 0 ; i < NUM_BASE_FORMATS ; i++ )
706 {
707 struct formatInfo *format;
708
709 if ( i == baseFormat ) {
710 labelInfoColor = labelColor1;
711 } else {
712 labelInfoColor = labelColor0;
713 }
714
715 format = &baseFormats[i].format[baseFormats[i].current];
716
717 for ( j = 0 ; j < NUM_ENV_MODES ; j++ ) {
718 struct envModeInfo *envMode;
719
720 envMode = &envModes[j];
721 drawSample( x, y, xSize, ySize, format, envMode );
722 x += xOffset;
723 }
724
725 x = xBase;
726 y -= yOffset;
727 }
728
729 if ( doubleBuffered ) {
730 glutSwapBuffers();
731 } else {
732 glFlush();
733 }
734
735 checkErrors();
736}
737
738static void usage( char *name )
739{
740 fprintf( stderr, "usage: %s [ options ]\n", name );
741 fprintf( stderr, "\n" );
742 fprintf( stderr, "options:\n" );
743 fprintf( stderr, " -sb single buffered\n" );
744 fprintf( stderr, " -db double buffered\n" );
745 fprintf( stderr, " -info print OpenGL driver info\n" );
746}
747
748static void instructions( void )
749{
750 fprintf( stderr, "texenv - texture environment and internal format test\n" );
751 fprintf( stderr, "\n" );
752 fprintf( stderr, " [c] - cycle through background colors\n" );
753 fprintf( stderr, " [g] - toggle background\n" );
754 fprintf( stderr, " [b] - toggle blend\n" );
755 fprintf( stderr, " [s] - toggle smooth shading\n" );
756 fprintf( stderr, " [t] - toggle texturing\n" );
757 fprintf( stderr, " [i] - toggle information display\n" );
Brian Paulae188722000-09-13 23:01:52 +0000758 fprintf( stderr, " up/down - select row\n" );
759 fprintf( stderr, " left/right - change row's internal format\n" );
Brian Paulf13a3302000-06-27 15:55:47 +0000760}
761
762int main( int argc, char *argv[] )
763{
764 GLboolean info = GL_FALSE;
765 int i;
766
767 glutInit( &argc, argv );
768
769 for ( i = 1 ; i < argc ; i++ ) {
770 if ( !strcmp( "-sb", argv[i] ) ) {
771 doubleBuffered = GL_FALSE;
772 } else if ( !strcmp( "-db", argv[i] ) ) {
773 doubleBuffered = GL_TRUE;
774 } else if ( !strcmp( "-info", argv[i] ) ) {
775 info = GL_TRUE;
776 } else {
777 usage( argv[0] );
778 exit( 1 );
779 }
780 }
781
782 if ( doubleBuffered ) {
783 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
784 } else {
785 glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
786 }
787
788 glutInitWindowSize( winWidth, winHeight );
Brian Paul345a71a2000-09-15 23:59:46 +0000789 glutInitWindowPosition( 0, 0 );
Brian Paulf34767f2008-06-04 09:30:04 -0600790 win = glutCreateWindow( "Texture Environment Test" );
Brian Paulf13a3302000-06-27 15:55:47 +0000791
792 initialize();
793 instructions();
794
795 if ( info ) {
796 printf( "\n" );
797 printf( "GL_RENDERER = %s\n", (char *) glGetString( GL_RENDERER ) );
798 printf( "GL_VERSION = %s\n", (char *) glGetString( GL_VERSION ) );
799 printf( "GL_VENDOR = %s\n", (char *) glGetString( GL_VENDOR ) ) ;
800 printf( "GL_EXTENSIONS = %s\n", (char *) glGetString( GL_EXTENSIONS ) );
801 }
802
803#if GL_EXT_texture_env_add
804 if ( !glutExtensionSupported( "GL_EXT_texture_env_add" ) ) {
805 fprintf( stderr, "missing extension: GL_EXT_texture_env_add\n" );
806 NUM_ENV_MODES--;
807 }
808#endif
809
810 glutDisplayFunc( display );
811 glutReshapeFunc( reshape );
812 glutKeyboardFunc( keyboard );
813 glutSpecialFunc( special );
814 glutMainLoop();
815
816 return 0;
817}