summaryrefslogtreecommitdiff
path: root/progs/demos/texenv.c
blob: 260ca8718f0e3be0675bfe1e94f2569c79a8ee29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817

/* Copyright (c) Mark J. Kilgard, 1994. */

/**
 * (c) Copyright 1993, Silicon Graphics, Inc.
 * ALL RIGHTS RESERVED
 * Permission to use, copy, modify, and distribute this software for
 * any purpose and without fee is hereby granted, provided that the above
 * copyright notice appear in all copies and that both the copyright notice
 * and this permission notice appear in supporting documentation, and that
 * the name of Silicon Graphics, Inc. not be used in advertising
 * or publicity pertaining to distribution of the software without specific,
 * written prior permission.
 *
 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * US Government Users Restricted Rights
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
 * clause at DFARS 252.227-7013 and/or in similar or successor
 * clauses in the FAR or the DOD or NASA FAR Supplement.
 * Unpublished-- rights reserved under the copyright laws of the
 * United States.  Contractor/manufacturer is Silicon Graphics,
 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
 *
 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
 */

/*
 * Demonstrates texture environment modes and internal image formats.
 */

/*
 * Hacked on, updated by Gareth Hughes <gareth@valinux.com>
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/glut.h>

#undef max
#undef min
#define max( a, b )	((a) >= (b) ? (a) : (b))
#define min( a, b )	((a) <= (b) ? (a) : (b))

GLfloat lightCheck[4] = { 0.7, 0.7, 0.7, 1.0 };
GLfloat darkCheck[4] = { 0.3, 0.3, 0.3, 1.0 };

GLfloat labelColor0[4] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat labelColor1[4] = { 1.0, 1.0, 0.4, 1.0 };
GLfloat *labelInfoColor = labelColor0;
GLfloat labelLevelColor0[4] = { 0.8, 0.8, 0.1, 1.0 };
GLfloat labelLevelColor1[4] = { 0.0, 0.0, 0.0, 1.0 };

GLboolean doubleBuffered = GL_TRUE;
GLboolean drawBackground = GL_FALSE;
GLboolean drawBlended = GL_TRUE;
GLboolean drawSmooth = GL_FALSE;
GLboolean drawTextured = GL_TRUE;
GLboolean displayLevelInfo = GL_FALSE;

int textureWidth = 64;
int textureHeight = 64;

int winWidth = 580, winHeight = 720;
static int Win;


struct formatInfo {
   GLenum	baseFormat;
   GLenum	internalFormat;
   char		*name;
};

#define NUM_LUMINANCE_FORMATS	(sizeof(luminanceFormats) / sizeof(luminanceFormats[0]))
struct formatInfo luminanceFormats[] =
{
   { GL_LUMINANCE, GL_LUMINANCE, "LUMINANCE" },
   { GL_LUMINANCE, GL_LUMINANCE4, "LUMINANCE4" },
   { GL_LUMINANCE, GL_LUMINANCE8, "LUMINANCE8" },
   { GL_LUMINANCE, GL_LUMINANCE12, "LUMINANCE12" },
   { GL_LUMINANCE, GL_LUMINANCE16, "LUMINANCE16" },
};

#define NUM_ALPHA_FORMATS	(sizeof(alphaFormats) / sizeof(alphaFormats[0]))
struct formatInfo alphaFormats[] =
{
   { GL_ALPHA, GL_ALPHA, "ALPHA" },
   { GL_ALPHA, GL_ALPHA4, "ALPHA4" },
   { GL_ALPHA, GL_ALPHA8, "ALPHA8" },
   { GL_ALPHA, GL_ALPHA12, "ALPHA12" },
   { GL_ALPHA, GL_ALPHA16, "ALPHA16" },
};

#define NUM_INTENSITY_FORMATS	(sizeof(intensityFormats) / sizeof(intensityFormats[0]))
struct formatInfo intensityFormats[] =
{
   { GL_INTENSITY, GL_INTENSITY, "INTENSITY" },
   { GL_INTENSITY, GL_INTENSITY4, "INTENSITY4" },
   { GL_INTENSITY, GL_INTENSITY8, "INTENSITY8" },
   { GL_INTENSITY, GL_INTENSITY12, "INTENSITY12" },
   { GL_INTENSITY, GL_INTENSITY16, "INTENSITY16" },
};

#define NUM_LUMINANCE_ALPHA_FORMATS	(sizeof(luminanceAlphaFormats) / sizeof(luminanceAlphaFormats[0]))
struct formatInfo luminanceAlphaFormats[] =
{
   { GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, "LUMINANCE_ALPHA" },
   { GL_LUMINANCE_ALPHA, GL_LUMINANCE4_ALPHA4, "LUMINANCE4_ALPHA4" },
   { GL_LUMINANCE_ALPHA, GL_LUMINANCE6_ALPHA2, "LUMINANCE6_ALPHA2" },
   { GL_LUMINANCE_ALPHA, GL_LUMINANCE8_ALPHA8, "LUMINANCE8_ALPHA8" },
   { GL_LUMINANCE_ALPHA, GL_LUMINANCE12_ALPHA4, "LUMINANCE12_ALPHA4" },
   { GL_LUMINANCE_ALPHA, GL_LUMINANCE12_ALPHA12, "LUMINANCE12_ALPHA12" },
   { GL_LUMINANCE_ALPHA, GL_LUMINANCE16_ALPHA16, "LUMINANCE16_ALPHA16" },
};

#define NUM_RGB_FORMATS		(sizeof(rgbFormats) / sizeof(rgbFormats[0]))
struct formatInfo rgbFormats[] =
{
   { GL_RGB, GL_RGB, "RGB" },
   { GL_RGB, GL_R3_G3_B2, "R3_G3_B2" },
   { GL_RGB, GL_RGB4, "RGB4" },
   { GL_RGB, GL_RGB5, "RGB5" },
   { GL_RGB, GL_RGB8, "RGB8" },
   { GL_RGB, GL_RGB10, "RGB10" },
   { GL_RGB, GL_RGB12, "RGB12" },
   { GL_RGB, GL_RGB16, "RGB16" },
};

#define NUM_RGBA_FORMATS	(sizeof(rgbaFormats) / sizeof(rgbaFormats[0]))
struct formatInfo rgbaFormats[] =
{
   { GL_RGBA, GL_RGBA, "RGBA" },
   { GL_RGBA, GL_RGBA2, "RGBA2" },
   { GL_RGBA, GL_RGBA4, "RGBA4" },
   { GL_RGBA, GL_RGB5_A1, "RGB5_A1" },
   { GL_RGBA, GL_RGBA8, "RGBA8" },
   { GL_RGBA, GL_RGB10_A2, "RGB10_A2" },
   { GL_RGBA, GL_RGBA12, "RGBA12" },
   { GL_RGBA, GL_RGBA16, "RGBA16" },
};

struct baseFormatInfo {
   struct	formatInfo *format;
   int		current, number;
};

#define NUM_BASE_FORMATS	(sizeof(baseFormats) / sizeof(baseFormats[0]))
int baseFormat;
struct baseFormatInfo baseFormats[] =
{
   { luminanceFormats, 0, NUM_LUMINANCE_FORMATS },
   { alphaFormats, 0, NUM_ALPHA_FORMATS },
   { intensityFormats, 0, NUM_INTENSITY_FORMATS },
   { luminanceAlphaFormats, 0, NUM_LUMINANCE_ALPHA_FORMATS },
   { rgbFormats, 0, NUM_RGB_FORMATS },
   { rgbaFormats, 0, NUM_RGBA_FORMATS },
};

#define NUM_ENV_COLORS		(sizeof(envColors) / sizeof(envColors[0]))
int envColor = 0;
GLfloat envColors[][4] =
{
   { 0.0, 0.0, 0.0, 1.0 },
   { 1.0, 0.0, 0.0, 1.0 },
   { 0.0, 1.0, 0.0, 1.0 },
   { 0.0, 0.0, 1.0, 1.0 },
   { 1.0, 1.0, 1.0, 1.0 },
};

struct envModeInfo {
   GLenum	mode;
   char		*name;
};

/* allow for run-time check for GL_EXT_texture_env_add */
int NUM_ENV_MODES = 5;
struct envModeInfo envModes[] =
{
   { GL_REPLACE, "REPLACE" },
   { GL_MODULATE, "MODULATE" },
   { GL_BLEND, "BLEND" },
   { GL_DECAL, "DECAL" },
#if GL_EXT_texture_env_add
   { GL_ADD, "ADD" },
#endif
};

static void checkErrors( void )
{
   GLenum error;

   while ( (error = glGetError()) != GL_NO_ERROR ) {
      fprintf( stderr, "Error: %s\n", (char *) gluErrorString( error ) );
   }
}

static void drawString( const char *string, GLfloat x, GLfloat y,
                        const GLfloat color[4] )
{
   glColor4fv( color );
   glRasterPos2f( x, y );

   while ( *string ) {
      glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_10, *string );
      string++;
   }
}

static void drawStringOutline( const char *string, GLfloat x, GLfloat y,
			       const GLfloat color[4],
                               const GLfloat outline[4] )
{
   drawString( string, x - 1, y, outline );
   drawString( string, x + 1, y, outline );
   drawString( string, x, y - 1, outline );
   drawString( string, x, y + 1, outline );
   drawString( string, x, y, color );
}

static void begin2D( int width, int height )
{
   glMatrixMode( GL_PROJECTION );

   glPushMatrix();
   glLoadIdentity();

   glOrtho( 0, width, 0, height, -1, 1 );
   glMatrixMode( GL_MODELVIEW );

   glPushMatrix();
   glLoadIdentity();
}

static void end2D( void )
{
   glMatrixMode( GL_PROJECTION );
   glPopMatrix();
   glMatrixMode( GL_MODELVIEW );
   glPopMatrix();
}

static void initialize( void )
{
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity();

   glOrtho( -1.5, 1.5, -1.5, 1.5, -1.5, 1.5 );

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

   glShadeModel( GL_FLAT );
}

/* ARGSUSED1 */
static void keyboard( unsigned char c, int x, int y )
{
   switch ( c ) {
   case 'c':
      envColor++;
      envColor = envColor % (int) NUM_ENV_COLORS;
      break;
   case 'g':
      drawBackground = !drawBackground;
      break;
   case 'b':
      drawBlended = !drawBlended;
      break;
   case 's':
      drawSmooth = !drawSmooth;
      break;
   case 't':
      drawTextured = !drawTextured;
      break;
   case 'i':
      displayLevelInfo = !displayLevelInfo;
      break;
   case 27:             /* Escape key should force exit. */
      glutDestroyWindow(Win);
      exit(0);
      break;
   default:
      break;
   }
   glutPostRedisplay();
}

/* ARGSUSED1 */
static void special( int key, int x, int y )
{
   switch ( key ) {
   case GLUT_KEY_DOWN:
      if ( ++baseFormat > NUM_BASE_FORMATS - 1 ) {
	 baseFormat = 0;
      }
      break;
   case GLUT_KEY_UP:
      if ( --baseFormat < 0 ) {
	 baseFormat = NUM_BASE_FORMATS - 1;
      }
      break;
   case GLUT_KEY_LEFT:
      --baseFormats[baseFormat].current;
      if ( baseFormats[baseFormat].current < 0 ) {
	 baseFormats[baseFormat].current = baseFormats[baseFormat].number - 1;
      }
      break;
   case GLUT_KEY_RIGHT:
      ++baseFormats[baseFormat].current;
      if ( baseFormats[baseFormat].current > baseFormats[baseFormat].number - 1 ) {
	 baseFormats[baseFormat].current = 0;
      }
      break;
   default:
      break;
   }
   glutPostRedisplay();
}

static void
reshape( int w, int h )
{
   winWidth = w;
   winHeight = h;
   /* No need to call glViewPort here since "draw" calls it! */
}

static void loadTexture( int width, int height,
                         const struct formatInfo *format )
{
   int		luminanceSize = 0;
   int		alphaSize = 0;
   int		rgbSize = 0;
   GLenum	textureFormat;
   GLubyte	*texImage, *p;
   int		elementsPerGroup, elementSize, groupSize, rowSize;
   int		i, j;

   switch ( format->baseFormat ) {
   case GL_LUMINANCE:
      luminanceSize = 1;
      textureFormat = GL_LUMINANCE;
      break;
   case GL_INTENSITY:
      luminanceSize = 1;
      /* Note: format=GL_INTENSITY for glTexImage is not legal */
      textureFormat = GL_LUMINANCE;
      break;
   case GL_ALPHA:
      alphaSize = 1;
      textureFormat = GL_ALPHA;
      break;
   case GL_LUMINANCE_ALPHA:
      luminanceSize = 1;
      alphaSize = 1;
      textureFormat = GL_LUMINANCE_ALPHA;
      break;
   case GL_RGB:
      rgbSize = 3;
      textureFormat = GL_RGB;
      break;
   case GL_RGBA:
      rgbSize = 3;
      alphaSize = 1;
      textureFormat = GL_RGBA;
      break;
   default:
      fprintf(stderr, "bad internal format info\n");
      return;
   }

   elementsPerGroup = luminanceSize + alphaSize + rgbSize;
   elementSize = sizeof(GLubyte);
   groupSize = elementsPerGroup * elementSize;
   rowSize = width * groupSize;

   if ( (texImage = (GLubyte *) malloc( height * rowSize ) ) == NULL ) {
      fprintf( stderr, "texture malloc failed\n" );
      return;
   }

   for ( i = 0 ; i < height ; i++ )
   {
      p = texImage + i * rowSize;

      for ( j = 0 ; j < width ; j++ )
      {
	 if ( luminanceSize > 0 )
	 {
	    /**
	     ** +-----+-----+
	     ** |     |     |
	     ** |  W  | LG  |
	     ** |     |     |
	     ** +-----+-----+
	     ** |     |     |
	     ** | DG  |  B  |
	     ** |     |     |
	     ** +-----+-----+
	     **/
	    if ( i > height / 2 ) {
	       if ( j < width / 2 ) {
		  p[0] = 0xff;
	       } else {
		  p[0] = 0xaa;
	       }
	    } else {
	       if ( j < width / 2 ) {
		  p[0] = 0x55;
	       } else {
		  p[0] = 0x00;
	       }
	    }
	    p += elementSize;
	 }

	 if ( rgbSize > 0 )
	 {
	    /**
	     ** +-----+-----+
	     ** |     |     |
	     ** |  R  |  G  |
	     ** |     |     |
	     ** +-----+-----+
	     ** |     |     |
	     ** |  Y  |  B  |
	     ** |     |     |
	     ** +-----+-----+
	     **/
	    if ( i > height / 2 ) {
	       if ( j < width / 2 ) {
		  p[0] = 0xff;
		  p[1] = 0x00;
		  p[2] = 0x00;
	       } else {
		  p[0] = 0x00;
		  p[1] = 0xff;
		  p[2] = 0x00;
	       }
	    } else {
	       if ( j < width / 2 ) {
		  p[0] = 0xff;
		  p[1] = 0xff;
		  p[2] = 0x00;
	       } else {
		  p[0] = 0x00;
		  p[1] = 0x00;
		  p[2] = 0xff;
	       }
	    }
	    p += 3 * elementSize;
	 }

	 if ( alphaSize > 0 )
	 {
	    /**
	     ** +-----------+
	     ** |     W     |
	     ** |  +-----+  |
	     ** |  |     |  |
	     ** |  |  B  |  |
	     ** |  |     |  |
	     ** |  +-----+  |
	     ** |           |
	     ** +-----------+
	     **/
	    int i2 = i - height / 2;
	    int j2 = j - width / 2;
	    int h8 = height / 8;
	    int w8 = width / 8;
	    if ( -h8 <= i2 && i2 <= h8 && -w8 <= j2 && j2 <= w8 ) {
	       p[0] = 0x00;
	    } else if ( -2 * h8 <= i2 && i2 <= 2 * h8 && -2 * w8 <= j2 && j2 <= 2 * w8 ) {
	       p[0] = 0x55;
	    } else if ( -3 * h8 <= i2 && i2 <= 3 * h8 && -3 * w8 <= j2 && j2 <= 3 * w8 ) {
	       p[0] = 0xaa;
	    } else {
	       p[0] = 0xff;
	    }
	    p += elementSize;
	 }
      }
   }

   glTexImage2D( GL_TEXTURE_2D, 0,
		 format->internalFormat, width, height, 0,
		 textureFormat, GL_UNSIGNED_BYTE, texImage );

   free( texImage );
}

static void drawCheck( int w, int h, const GLfloat lightCheck[4],
                       const GLfloat darkCheck[4] )
{
   float	dw = 2.0 / w;
   float	dh = 2.0 / h;
   int		i, j;

   for ( i = 0 ; i < w ; i++ ) {
      GLfloat x0 = -1.0 + i * dw;
      GLfloat x1 = x0 + dw;

      glBegin( GL_QUAD_STRIP );

      for ( j = 0 ; j <= h ; j++ ) {
	 GLfloat y = -1.0 + j * dh;

	 if ( (i ^ j) & 1 ) {
	    glColor4fv( lightCheck );
	 } else {
	    glColor4fv( darkCheck );
	 }

	 glVertex2f( x0, y );
	 glVertex2f( x1, y );
      }

      glEnd();
   }
}

static const char *lookupFormat( GLint format )
{
   switch ( format ) {
   case GL_RGBA:
      return "GL_RGBA";
   case GL_RGB:
      return "GL_RGB";
   case GL_ALPHA:
      return "GL_ALPHA";
   case GL_LUMINANCE:
      return "GL_LUMINANCE";
   case GL_LUMINANCE_ALPHA:
      return "GL_LUMINANCE_ALPHA";
   case GL_INTENSITY:
      return "GL_INTENSITY";
   case GL_COLOR_INDEX:
      return "GL_COLOR_INDEX";
   case GL_BGRA:
      return "GL_BGRA";
   case GL_BGR:
      return "GL_BGR";
   default:
      return "unknown format";
   }
}

static void drawSample( int x, int y, int w, int h,
			const struct formatInfo *format,
                        const struct envModeInfo *envMode )
{
   glViewport( x, y, w, h );
   glScissor( x, y, w, h );

   glClearColor( 0.1, 0.1, 0.1, 1.0 );
   glClear( GL_COLOR_BUFFER_BIT );

   begin2D( w, h );
   drawString( format->name, 10, h - 15, labelInfoColor );
   drawString( envMode->name, 10, 5, labelInfoColor );
   end2D();

   glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, envMode->mode );
   glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, envColors[envColor] );

   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );

   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );

   loadTexture( textureWidth, textureHeight, format );

   if ( drawBackground ) {
      drawCheck( 15, 15, lightCheck, darkCheck );
   }
   if ( drawBlended ) {
      glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
      glEnable( GL_BLEND );
   }
   if ( drawSmooth ) {
      glShadeModel( GL_SMOOTH );
   }
   else {
      glShadeModel( GL_FLAT );
      glColor4f(1, 1, 1, 1);
   }
   if ( drawTextured ) {
      glEnable( GL_TEXTURE_2D );
   }

   /*
    * if (drawSmooth) then draw quad which goes from purple at the
    * bottom (100% alpha) to green at the top (50% alpha).
    */
   glBegin( GL_QUADS );
      if ( drawSmooth )  glColor4f( 1.0, 0.0, 1.0, 1.0 );
      glTexCoord2f( 0.0, 0.0 );
      glVertex2f( -0.8, -0.8 );

      if ( drawSmooth )  glColor4f( 1.0, 0.0, 1.0, 1.0 );
      glTexCoord2f( 1.0, 0.0 );
      glVertex2f( 0.8, -0.8 );

      if ( drawSmooth )  glColor4f( 0.0, 1.0, 0.0, 0.5 );
      glTexCoord2f( 1.0, 1.0 );
      glVertex2f( 0.8, 0.8 );

      if ( drawSmooth )  glColor4f( 0.0, 1.0, 0.0, 0.5 );
      glTexCoord2f( 0.0, 1.0 );
      glVertex2f( -0.8, 0.8 );
   glEnd();

   glDisable( GL_BLEND );
   glShadeModel( GL_FLAT );
   glDisable( GL_TEXTURE_2D );

   if ( envMode->mode == GL_DECAL &&
        (format->baseFormat == GL_ALPHA ||
         format->baseFormat == GL_LUMINANCE ||
         format->baseFormat == GL_LUMINANCE_ALPHA ||
         format->baseFormat == GL_INTENSITY)) {
      /* undefined format/mode combination */
      begin2D( w, h );
      drawStringOutline( "UNDEFINED MODE", 15, h / 2,
                         labelLevelColor0, labelLevelColor1 );
      end2D();
   }
   else if ( displayLevelInfo ) {
      GLint width, height, border, format;
      GLint redSize, greenSize, blueSize, alphaSize;
      GLint luminanceSize, intensitySize;
      char buf[255];

      glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width );
      glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height );
      glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER, &border );
      glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &format );
      glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &redSize );
      glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE, &greenSize );
      glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE, &blueSize );
      glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &alphaSize );
      glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_LUMINANCE_SIZE, &luminanceSize );
      glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_INTENSITY_SIZE, &intensitySize );

      begin2D( w, h );
      sprintf( buf, "dimensions: %d x %d", width, height );
      drawStringOutline( buf, 15, h / 2 + 20, labelLevelColor0, labelLevelColor1 );

      sprintf( buf, "border: %d", border );
      drawStringOutline( buf, 15, h / 2 + 10, labelLevelColor0, labelLevelColor1 );

      sprintf( buf, "internal format:" );
      drawStringOutline( buf, 15, h / 2, labelLevelColor0, labelLevelColor1 );

      sprintf( buf, "  %s",  lookupFormat( format ) );
      drawStringOutline( buf, 15, h / 2 - 10, labelLevelColor0, labelLevelColor1 );

      sprintf( buf, "sizes:" );
      drawStringOutline( buf, 15, h / 2 - 20, labelLevelColor0, labelLevelColor1 );

      sprintf( buf, "  %d / %d / %d / %d / %d / %d",
	       redSize, greenSize, blueSize, alphaSize,
	       luminanceSize, intensitySize );
      drawStringOutline( buf, 15, h / 2 - 30, labelLevelColor0, labelLevelColor1 );

      end2D();
   }
}

static void display( void )
{
   int		numX = NUM_ENV_MODES, numY = NUM_BASE_FORMATS;
   float	xBase = (float) winWidth * 0.01;
   float	xOffset = (winWidth - xBase) / numX;
   float	xSize = max( xOffset - xBase, 1 );
   float	yBase = (float) winHeight * 0.01;
   float	yOffset = (winHeight - yBase) / numY;
   float	ySize = max( yOffset - yBase, 1 );
   float	x, y;
   int		i, j;

   glViewport( 0, 0, winWidth, winHeight );
   glDisable( GL_SCISSOR_TEST );
   glClearColor( 0.0, 0.0, 0.0, 0.0 );
   glClear( GL_COLOR_BUFFER_BIT );
   glEnable( GL_SCISSOR_TEST );

   x = xBase;
   y = (winHeight - 1) - yOffset;

   for ( i = 0 ; i < NUM_BASE_FORMATS ; i++ )
   {
      struct formatInfo *format;

      if ( i == baseFormat ) {
	 labelInfoColor = labelColor1;
      } else {
	 labelInfoColor = labelColor0;
      }

      format = &baseFormats[i].format[baseFormats[i].current];

      for ( j = 0 ; j < NUM_ENV_MODES ; j++ ) {
	 struct envModeInfo *envMode;

	 envMode = &envModes[j];
	 drawSample( x, y, xSize, ySize, format, envMode );
	 x += xOffset;
      }

      x = xBase;
      y -= yOffset;
   }

   if ( doubleBuffered ) {
      glutSwapBuffers();
   } else {
      glFlush();
   }

   checkErrors();
}

static void usage( char *name )
{
   fprintf( stderr, "usage: %s [ options ]\n", name );
   fprintf( stderr, "\n" );
   fprintf( stderr, "options:\n" );
   fprintf( stderr, "    -sb    single buffered\n" );
   fprintf( stderr, "    -db    double buffered\n" );
   fprintf( stderr, "    -info  print OpenGL driver info\n" );
}

static void instructions( void )
{
   fprintf( stderr, "texenv - texture environment and internal format test\n" );
   fprintf( stderr, "\n" );
   fprintf( stderr, "  [c] - cycle through background colors\n" );
   fprintf( stderr, "  [g] - toggle background\n" );
   fprintf( stderr, "  [b] - toggle blend\n" );
   fprintf( stderr, "  [s] - toggle smooth shading\n" );
   fprintf( stderr, "  [t] - toggle texturing\n" );
   fprintf( stderr, "  [i] - toggle information display\n" );
   fprintf( stderr, "  up/down - select row\n" );
   fprintf( stderr, "  left/right - change row's internal format\n" );
}

int main( int argc, char *argv[] )
{
   GLboolean info = GL_FALSE;
   int i;

   glutInitWindowSize( winWidth, winHeight );
   glutInit( &argc, argv );

   for ( i = 1 ; i < argc ; i++ ) {
      if ( !strcmp( "-sb", argv[i] ) ) {
	 doubleBuffered = GL_FALSE;
      } else if ( !strcmp( "-db", argv[i] ) ) {
	 doubleBuffered = GL_TRUE;
      } else if ( !strcmp( "-info", argv[i] ) ) {
	 info = GL_TRUE;
      } else {
	 usage( argv[0] );
	 exit( 1 );
      }
   }

   if ( doubleBuffered ) {
      glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
   } else {
      glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
   }

   Win = glutCreateWindow( "Texture Environment Test" );

   initialize();
   instructions();

   if ( info ) {
      printf( "\n" );
      printf( "GL_RENDERER   = %s\n", (char *) glGetString( GL_RENDERER ) );
      printf( "GL_VERSION    = %s\n", (char *) glGetString( GL_VERSION ) );
      printf( "GL_VENDOR     = %s\n", (char *) glGetString( GL_VENDOR ) ) ;
      printf( "GL_EXTENSIONS = %s\n", (char *) glGetString( GL_EXTENSIONS ) );
   }

#if GL_EXT_texture_env_add
   if ( !glutExtensionSupported( "GL_EXT_texture_env_add" ) ) {
      fprintf( stderr, "missing extension: GL_EXT_texture_env_add\n" );
      NUM_ENV_MODES--;
   }
#endif

   glutDisplayFunc( display );
   glutReshapeFunc( reshape );
   glutKeyboardFunc( keyboard );
   glutSpecialFunc( special );
   glutMainLoop();

   return 0;
}