3 * Demonstrates mixed texgen/non-texgen texture coordinates.
14 #define max( a, b ) ((a) >= (b) ? (a) : (b))
15 #define min( a, b ) ((a) <= (b) ? (a) : (b))
17 GLfloat labelColor0
[4] = { 1.0, 1.0, 1.0, 1.0 };
18 GLfloat labelColor1
[4] = { 1.0, 1.0, 0.4, 1.0 };
19 GLfloat
*labelInfoColor
= labelColor0
;
21 GLboolean doubleBuffered
= GL_TRUE
;
22 GLboolean drawTextured
= GL_TRUE
;
24 int textureWidth
= 64;
25 int textureHeight
= 64;
27 int winWidth
= 580, winHeight
= 720;
29 const GLfloat texmat_swap_rq
[16] = { 1.0, 0.0, 0.0, 0.0,
34 const GLfloat nullPlane
[4] = { 0.0, 0.0, 0.0, 0.0 };
35 const GLfloat ObjPlaneS1
[4] = { 1.0, 0.0, 1.0, 0.0 };
36 const GLfloat ObjPlaneS2
[4] = { 0.5, 0.0, 0.0, 0.0 };
37 const GLfloat ObjPlaneS3
[4] = { 1.0, 0.0, 0.0, 0.0 };
38 const GLfloat ObjPlaneT
[4] = { 0.0, 1.0, 0.0, 0.0 };
39 const GLfloat ObjPlaneT2
[4] = { 0.0, 0.5, 0.0, 0.0 };
40 const GLfloat ObjPlaneT3
[4] = { 0.0, 1.0, 0.0, 0.0 };
41 const GLfloat ObjPlaneR
[4] = { 0.0, 0.0, 1.0, 0.0 };
42 const GLfloat ObjPlaneQ
[4] = { 0.0, 0.0, 0.0, 0.5 };
45 static void checkErrors( void )
49 while ( (error
= glGetError()) != GL_NO_ERROR
) {
50 fprintf( stderr
, "Error: %s\n", (char *) gluErrorString( error
) );
54 static void drawString( const char *string
, GLfloat x
, GLfloat y
,
55 const GLfloat color
[4] )
58 glRasterPos2f( x
, y
);
61 glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_10
, *string
);
66 static void begin2D( int width
, int height
)
68 glMatrixMode( GL_PROJECTION
);
73 glOrtho( 0, width
, 0, height
, -1, 1 );
74 glMatrixMode( GL_MODELVIEW
);
80 static void end2D( void )
82 glMatrixMode( GL_PROJECTION
);
84 glMatrixMode( GL_MODELVIEW
);
88 static void initialize( void )
90 glMatrixMode( GL_PROJECTION
);
93 glOrtho( -1.5, 1.5, -1.5, 1.5, -1.5, 1.5 );
95 glMatrixMode(GL_MODELVIEW
);
98 glShadeModel( GL_FLAT
);
102 static void keyboard( unsigned char c
, int x
, int y
)
106 drawTextured
= !drawTextured
;
108 case 27: /* Escape key should force exit. */
118 static void special( int key
, int x
, int y
)
136 reshape( int w
, int h
)
140 /* No need to call glViewPort here since "draw" calls it! */
143 static void loadTexture( int width
, int height
)
147 GLubyte
*texImage
, *p
;
148 int elementsPerGroup
, elementSize
, groupSize
, rowSize
;
152 elementsPerGroup
= alphaSize
+ rgbSize
;
153 elementSize
= sizeof(GLubyte
);
154 groupSize
= elementsPerGroup
* elementSize
;
155 rowSize
= width
* groupSize
;
157 if ( (texImage
= (GLubyte
*) malloc( height
* rowSize
) ) == NULL
) {
158 fprintf( stderr
, "texture malloc failed\n" );
162 for ( i
= 0 ; i
< height
; i
++ )
164 p
= texImage
+ i
* rowSize
;
166 for ( j
= 0 ; j
< width
; j
++ )
181 if ( i
> height
/ 2 ) {
182 if ( j
< width
/ 2 ) {
192 if ( j
< width
/ 2 ) {
202 p
+= 3 * elementSize
;
218 int i2
= i
- height
/ 2;
219 int j2
= j
- width
/ 2;
222 if ( -h8
<= i2
&& i2
<= h8
&& -w8
<= j2
&& j2
<= w8
) {
224 } else if ( -2 * h8
<= i2
&& i2
<= 2 * h8
&& -2 * w8
<= j2
&& j2
<= 2 * w8
) {
226 } else if ( -3 * h8
<= i2
&& i2
<= 3 * h8
&& -3 * w8
<= j2
&& j2
<= 3 * w8
) {
236 glTexImage2D( GL_TEXTURE_2D
, 0,
237 GL_RGBA
, width
, height
, 0,
238 GL_RGBA
, GL_UNSIGNED_BYTE
, texImage
);
244 static void drawSample( int x
, int y
, int w
, int h
,
245 int texgenenabled
, int coordnr
)
249 glViewport( x
, y
, w
, h
);
250 glScissor( x
, y
, w
, h
);
252 glClearColor( 0.1, 0.1, 0.1, 1.0 );
253 glClear( GL_COLOR_BUFFER_BIT
);
256 if (texgenenabled
== 2) {
257 sprintf( buf
, "TexCoord%df", coordnr
);
258 drawString( buf
, 10, h
- 15, labelInfoColor
);
259 sprintf( buf
, "texgen enabled for %s coordinate(s)", coordnr
== 2 ? "S" : "S/T");
260 drawString( buf
, 10, 5, labelInfoColor
);
262 else if (texgenenabled
== 0) {
263 sprintf( buf
, "TexCoord%df", coordnr
);
264 drawString( buf
, 10, h
- 15, labelInfoColor
);
265 drawString( "no texgen", 10, 5, labelInfoColor
);
267 else if (texgenenabled
== 1) {
268 drawString( "no TexCoord", 10, h
- 15, labelInfoColor
);
269 sprintf( buf
, "texgen enabled for %s coordinate(s)",
270 coordnr
== 2 ? "S/T" : (coordnr
== 3 ? "S/T/R" : "S/T/R/Q"));
271 drawString( buf
, 10, 5, labelInfoColor
);
276 glTexEnvi( GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
278 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
279 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
281 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
282 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
284 loadTexture( textureWidth
, textureHeight
);
286 if ( drawTextured
) {
287 glEnable( GL_TEXTURE_2D
);
290 glDisable( GL_TEXTURE_GEN_S
);
291 glDisable( GL_TEXTURE_GEN_T
);
292 glDisable( GL_TEXTURE_GEN_R
);
293 glDisable( GL_TEXTURE_GEN_Q
);
295 glMatrixMode( GL_TEXTURE
);
297 glMatrixMode( GL_MODELVIEW
);
302 switch (texgenenabled
) {
305 glTexCoord2f( 0.0, 0.0 );
306 glVertex2f( -0.8, -0.8 );
308 glTexCoord2f( 1.0, 0.0 );
309 glVertex2f( 0.8, -0.8 );
311 glTexCoord2f( 1.0, 1.0 );
312 glVertex2f( 0.8, 0.8 );
314 glTexCoord2f( 0.0, 1.0 );
315 glVertex2f( -0.8, 0.8 );
319 glTranslatef( -0.8, -0.8, 0.0 );
320 glScalef( 1.6, 1.6, 1.0 );
321 glTexGeni( GL_S
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
322 glTexGeni( GL_T
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
323 glTexGeni( GL_R
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
324 glTexGeni( GL_Q
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
325 glTexGenfv(GL_S
, GL_OBJECT_PLANE
, ObjPlaneS3
);
326 glTexGenfv(GL_T
, GL_OBJECT_PLANE
, ObjPlaneT3
);
327 glTexGenfv(GL_R
, GL_OBJECT_PLANE
, nullPlane
);
328 glTexGenfv(GL_Q
, GL_OBJECT_PLANE
, nullPlane
);
330 glEnable( GL_TEXTURE_GEN_S
);
331 glEnable( GL_TEXTURE_GEN_T
);
333 /* Issue a texcoord here to be sure Q isn't left over from a
338 glVertex2f( 0.0, 0.0 );
339 glVertex2f( 1.0, 0.0 );
340 glVertex2f( 1.0, 1.0 );
341 glVertex2f( 0.0, 1.0 );
345 /* make sure that texgen T and non-texgen S coordinate are wrong */
346 glTexGeni( GL_S
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
347 glTexGeni( GL_T
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
348 glTexGeni( GL_R
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
349 glTexGeni( GL_Q
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
350 glTexGenfv(GL_S
, GL_OBJECT_PLANE
, ObjPlaneS1
);
351 glTexGenfv(GL_T
, GL_OBJECT_PLANE
, nullPlane
);
352 glTexGenfv(GL_R
, GL_OBJECT_PLANE
, nullPlane
);
353 glTexGenfv(GL_Q
, GL_OBJECT_PLANE
, nullPlane
);
355 glEnable( GL_TEXTURE_GEN_S
);
358 /* use z coordinate to get correct texgen values... */
359 glTexCoord2f( 0.0, 0.0 );
360 glVertex3f( -0.8, -0.8, 0.8 );
362 glTexCoord2f( 0.0, 0.0 );
363 glVertex3f( 0.8, -0.8, 0.2 );
365 glTexCoord2f( 0.0, 1.0 );
366 glVertex3f( 0.8, 0.8, 0.2 );
368 glTexCoord2f( 0.0, 1.0 );
369 glVertex3f( -0.8, 0.8, 0.8 );
375 glMatrixMode( GL_TEXTURE
);
376 glLoadMatrixf( texmat_swap_rq
);
377 glMatrixMode( GL_MODELVIEW
);
378 glTranslatef( -0.8, -0.8, 0.0 );
379 glScalef( 1.6, 1.6, 1.0 );
380 switch (texgenenabled
) {
383 glTexCoord3f( 0.0, 0.0, 0.5 );
384 glVertex2f( 0.0, 0.0 );
386 glTexCoord3f( 0.5, 0.0, 0.5 );
387 glVertex2f( 1.0, 0.0 );
389 glTexCoord3f( 0.5, 0.5, 0.5 );
390 glVertex2f( 1.0, 1.0 );
392 glTexCoord3f( 0.0, 0.5, 0.5 );
393 glVertex2f( 0.0, 1.0 );
397 glTexGeni( GL_S
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
398 glTexGeni( GL_T
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
399 glTexGeni( GL_R
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
400 glTexGeni( GL_Q
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
401 glTexGenfv(GL_S
, GL_OBJECT_PLANE
, ObjPlaneS2
);
402 glTexGenfv(GL_T
, GL_OBJECT_PLANE
, ObjPlaneT2
);
403 glTexGenfv(GL_R
, GL_OBJECT_PLANE
, ObjPlaneR
);
404 glTexGenfv(GL_Q
, GL_OBJECT_PLANE
, nullPlane
);
406 glEnable( GL_TEXTURE_GEN_S
);
407 glEnable( GL_TEXTURE_GEN_T
);
408 glEnable( GL_TEXTURE_GEN_R
);
410 glTexCoord1f( 0.0 ); /* to make sure Q is 1.0 */
412 glVertex3f( 0.0, 0.0, 0.5 );
413 glVertex3f( 1.0, 0.0, 0.5 );
414 glVertex3f( 1.0, 1.0, 0.5 );
415 glVertex3f( 0.0, 1.0, 0.5 );
419 /* make sure that texgen R/Q and non-texgen S/T coordinates are wrong */
420 glTexGeni( GL_S
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
421 glTexGeni( GL_T
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
422 glTexGeni( GL_R
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
423 glTexGeni( GL_Q
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
424 glTexGenfv(GL_S
, GL_OBJECT_PLANE
, ObjPlaneS2
);
425 glTexGenfv(GL_T
, GL_OBJECT_PLANE
, ObjPlaneT2
);
426 glTexGenfv(GL_R
, GL_OBJECT_PLANE
, nullPlane
);
427 glTexGenfv(GL_Q
, GL_OBJECT_PLANE
, nullPlane
);
429 glEnable( GL_TEXTURE_GEN_S
);
430 glEnable( GL_TEXTURE_GEN_T
);
433 glTexCoord3f( 0.0, 0.0, 0.5 );
434 glVertex2f( 0.0, 0.0);
436 glTexCoord3f( 0.0, 0.0, 0.5 );
437 glVertex2f( 1.0, 0.0);
439 glTexCoord3f( 0.0, 0.0, 0.5 );
440 glVertex2f( 1.0, 1.0);
442 glTexCoord3f( 0.0, 0.0, 0.5 );
443 glVertex2f( 0.0, 1.0);
449 switch (texgenenabled
) {
452 /* don't need r coordinate but still setting it I'm mean */
453 glTexCoord4f( 0.0, 0.0, 0.0, 0.5 );
454 glVertex2f( -0.8, -0.8 );
456 glTexCoord4f( 0.5, 0.0, 0.2, 0.5 );
457 glVertex2f( 0.8, -0.8 );
459 glTexCoord4f( 0.5, 0.5, 0.5, 0.5 );
460 glVertex2f( 0.8, 0.8 );
462 glTexCoord4f( 0.0, 0.5, 0.5, 0.5 );
463 glVertex2f( -0.8, 0.8 );
467 glTranslatef( -0.8, -0.8, 0.0 );
468 glScalef( 1.6, 1.6, 1.0 );
469 /* make sure that texgen R/Q and non-texgen S/T coordinates are wrong */
470 glTexGeni( GL_S
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
471 glTexGeni( GL_T
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
472 glTexGeni( GL_R
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
473 glTexGeni( GL_Q
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
474 glTexGenfv(GL_S
, GL_OBJECT_PLANE
, ObjPlaneS2
);
475 glTexGenfv(GL_T
, GL_OBJECT_PLANE
, ObjPlaneT2
);
476 glTexGenfv(GL_R
, GL_OBJECT_PLANE
, ObjPlaneR
);
477 glTexGenfv(GL_Q
, GL_OBJECT_PLANE
, ObjPlaneQ
);
479 glEnable( GL_TEXTURE_GEN_S
);
480 glEnable( GL_TEXTURE_GEN_T
);
481 glEnable( GL_TEXTURE_GEN_R
);
482 glEnable( GL_TEXTURE_GEN_Q
);
485 glVertex2f( 0.0, 0.0 );
486 glVertex2f( 1.0, 0.0 );
487 glVertex2f( 1.0, 1.0 );
488 glVertex2f( 0.0, 1.0 );
492 glTranslatef( -0.8, -0.8, 0.0 );
493 glScalef( 1.6, 1.6, 1.0 );
494 /* make sure that texgen R/Q and non-texgen S/T coordinates are wrong */
495 glTexGeni( GL_S
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
496 glTexGeni( GL_T
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
497 glTexGeni( GL_R
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
498 glTexGeni( GL_Q
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
499 glTexGenfv(GL_S
, GL_OBJECT_PLANE
, ObjPlaneS2
);
500 glTexGenfv(GL_T
, GL_OBJECT_PLANE
, ObjPlaneT2
);
501 glTexGenfv(GL_R
, GL_OBJECT_PLANE
, nullPlane
);
502 glTexGenfv(GL_Q
, GL_OBJECT_PLANE
, nullPlane
);
504 glEnable( GL_TEXTURE_GEN_S
);
505 glEnable( GL_TEXTURE_GEN_T
);
508 glTexCoord4f( 0.0, 0.0, 0.0, 0.5 );
509 glVertex2f( 0.0, 0.0 );
511 glTexCoord4f( 0.0, 0.0, 0.2, 0.5 );
512 glVertex2f( 1.0, 0.0 );
514 glTexCoord4f( 0.0, 0.0, 0.5, 0.5 );
515 glVertex2f( 1.0, 1.0 );
517 glTexCoord4f( 0.0, 0.0, 0.75, 0.5 );
518 glVertex2f( 0.0, 1.0 );
526 glDisable( GL_TEXTURE_2D
);
530 static void display( void )
532 int numX
= 3, numY
= 3;
533 float xBase
= (float) winWidth
* 0.01;
534 float xOffset
= (winWidth
- xBase
) / numX
;
535 float xSize
= max( xOffset
- xBase
, 1 );
536 float yBase
= (float) winHeight
* 0.01;
537 float yOffset
= (winHeight
- yBase
) / numY
;
538 float ySize
= max( yOffset
- yBase
, 1 );
542 glViewport( 0, 0, winWidth
, winHeight
);
543 glDisable( GL_SCISSOR_TEST
);
544 glClearColor( 0.0, 0.0, 0.0, 0.0 );
545 glClear( GL_COLOR_BUFFER_BIT
);
546 glEnable( GL_SCISSOR_TEST
);
549 y
= (winHeight
- 1) - yOffset
;
551 for ( i
= 0 ; i
< numY
; i
++ )
554 labelInfoColor
= labelColor1
;
557 for ( j
= 0 ; j
< numX
; j
++ ) {
558 drawSample( x
, y
, xSize
, ySize
, i
, j
+2 );
566 if ( doubleBuffered
) {
575 static void usage( char *name
)
577 fprintf( stderr
, "usage: %s [ options ]\n", name
);
578 fprintf( stderr
, "\n" );
579 fprintf( stderr
, "options:\n" );
580 fprintf( stderr
, " -sb single buffered\n" );
581 fprintf( stderr
, " -db double buffered\n" );
582 fprintf( stderr
, " -info print OpenGL driver info\n" );
585 static void instructions( void )
587 fprintf( stderr
, "texgenmix - mixed texgen/non-texgen texture coordinate test\n" );
588 fprintf( stderr
, "all quads should look the same!\n" );
589 fprintf( stderr
, "\n" );
590 fprintf( stderr
, " [t] - toggle texturing\n" );
593 int main( int argc
, char *argv
[] )
595 GLboolean info
= GL_FALSE
;
598 glutInit( &argc
, argv
);
600 for ( i
= 1 ; i
< argc
; i
++ ) {
601 if ( !strcmp( "-sb", argv
[i
] ) ) {
602 doubleBuffered
= GL_FALSE
;
603 } else if ( !strcmp( "-db", argv
[i
] ) ) {
604 doubleBuffered
= GL_TRUE
;
605 } else if ( !strcmp( "-info", argv
[i
] ) ) {
613 if ( doubleBuffered
) {
614 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
616 glutInitDisplayMode( GLUT_RGB
| GLUT_SINGLE
);
619 glutInitWindowSize( winWidth
, winHeight
);
620 glutInitWindowPosition( 0, 0 );
621 glutCreateWindow( "Mixed texgen/non-texgen texture coordinate test" );
629 printf( "GL_RENDERER = %s\n", (char *) glGetString( GL_RENDERER
) );
630 printf( "GL_VERSION = %s\n", (char *) glGetString( GL_VERSION
) );
631 printf( "GL_VENDOR = %s\n", (char *) glGetString( GL_VENDOR
) ) ;
632 printf( "GL_EXTENSIONS = %s\n", (char *) glGetString( GL_EXTENSIONS
) );
635 glutDisplayFunc( display
);
636 glutReshapeFunc( reshape
);
637 glutKeyboardFunc( keyboard
);
638 glutSpecialFunc( special
);