3 * Demonstrates mixed texgen/non-texgen texture coordinates.
13 #define max( a, b ) ((a) >= (b) ? (a) : (b))
14 #define min( a, b ) ((a) <= (b) ? (a) : (b))
16 GLfloat labelColor0
[4] = { 1.0, 1.0, 1.0, 1.0 };
17 GLfloat labelColor1
[4] = { 1.0, 1.0, 0.4, 1.0 };
18 GLfloat
*labelInfoColor
= labelColor0
;
20 GLboolean doubleBuffered
= GL_TRUE
;
21 GLboolean drawTextured
= GL_TRUE
;
23 int textureWidth
= 64;
24 int textureHeight
= 64;
26 int winWidth
= 580, winHeight
= 720;
28 const GLfloat texmat_swap_rq
[16] = { 1.0, 0.0, 0.0, 0.0,
33 const GLfloat nullPlane
[4] = { 0.0, 0.0, 0.0, 0.0 };
34 const GLfloat ObjPlaneS1
[4] = { 1.0, 0.0, 1.0, 0.0 };
35 const GLfloat ObjPlaneS2
[4] = { 0.5, 0.0, 0.0, 0.0 };
36 const GLfloat ObjPlaneS3
[4] = { 1.0, 0.0, 0.0, 0.0 };
37 const GLfloat ObjPlaneT
[4] = { 0.0, 1.0, 0.0, 0.0 };
38 const GLfloat ObjPlaneT2
[4] = { 0.0, 0.5, 0.0, 0.0 };
39 const GLfloat ObjPlaneT3
[4] = { 0.0, 1.0, 0.0, 0.0 };
40 const GLfloat ObjPlaneR
[4] = { 0.0, 0.0, 1.0, 0.0 };
41 const GLfloat ObjPlaneQ
[4] = { 0.0, 0.0, 0.0, 0.5 };
44 static void checkErrors( void )
48 while ( (error
= glGetError()) != GL_NO_ERROR
) {
49 fprintf( stderr
, "Error: %s\n", (char *) gluErrorString( error
) );
53 static void drawString( const char *string
, GLfloat x
, GLfloat y
,
54 const GLfloat color
[4] )
57 glRasterPos2f( x
, y
);
60 glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_10
, *string
);
65 static void begin2D( int width
, int height
)
67 glMatrixMode( GL_PROJECTION
);
72 glOrtho( 0, width
, 0, height
, -1, 1 );
73 glMatrixMode( GL_MODELVIEW
);
79 static void end2D( void )
81 glMatrixMode( GL_PROJECTION
);
83 glMatrixMode( GL_MODELVIEW
);
87 static void initialize( void )
89 glMatrixMode( GL_PROJECTION
);
92 glOrtho( -1.5, 1.5, -1.5, 1.5, -1.5, 1.5 );
94 glMatrixMode(GL_MODELVIEW
);
97 glShadeModel( GL_FLAT
);
101 static void keyboard( unsigned char c
, int x
, int y
)
105 drawTextured
= !drawTextured
;
107 case 27: /* Escape key should force exit. */
117 static void special( int key
, int x
, int y
)
135 reshape( int w
, int h
)
139 /* No need to call glViewPort here since "draw" calls it! */
142 static void loadTexture( int width
, int height
)
146 GLubyte
*texImage
, *p
;
147 int elementsPerGroup
, elementSize
, groupSize
, rowSize
;
151 elementsPerGroup
= alphaSize
+ rgbSize
;
152 elementSize
= sizeof(GLubyte
);
153 groupSize
= elementsPerGroup
* elementSize
;
154 rowSize
= width
* groupSize
;
156 if ( (texImage
= (GLubyte
*) malloc( height
* rowSize
) ) == NULL
) {
157 fprintf( stderr
, "texture malloc failed\n" );
161 for ( i
= 0 ; i
< height
; i
++ )
163 p
= texImage
+ i
* rowSize
;
165 for ( j
= 0 ; j
< width
; j
++ )
180 if ( i
> height
/ 2 ) {
181 if ( j
< width
/ 2 ) {
191 if ( j
< width
/ 2 ) {
201 p
+= 3 * elementSize
;
217 int i2
= i
- height
/ 2;
218 int j2
= j
- width
/ 2;
221 if ( -h8
<= i2
&& i2
<= h8
&& -w8
<= j2
&& j2
<= w8
) {
223 } else if ( -2 * h8
<= i2
&& i2
<= 2 * h8
&& -2 * w8
<= j2
&& j2
<= 2 * w8
) {
225 } else if ( -3 * h8
<= i2
&& i2
<= 3 * h8
&& -3 * w8
<= j2
&& j2
<= 3 * w8
) {
235 glTexImage2D( GL_TEXTURE_2D
, 0,
236 GL_RGBA
, width
, height
, 0,
237 GL_RGBA
, GL_UNSIGNED_BYTE
, texImage
);
243 static void drawSample( int x
, int y
, int w
, int h
,
244 int texgenenabled
, int coordnr
)
248 glViewport( x
, y
, w
, h
);
249 glScissor( x
, y
, w
, h
);
251 glClearColor( 0.1, 0.1, 0.1, 1.0 );
252 glClear( GL_COLOR_BUFFER_BIT
);
255 if (texgenenabled
== 2) {
256 sprintf( buf
, "TexCoord%df", coordnr
);
257 drawString( buf
, 10, h
- 15, labelInfoColor
);
258 sprintf( buf
, "texgen enabled for %s coordinate(s)", coordnr
== 2 ? "S" : "S/T");
259 drawString( buf
, 10, 5, labelInfoColor
);
261 else if (texgenenabled
== 0) {
262 sprintf( buf
, "TexCoord%df", coordnr
);
263 drawString( buf
, 10, h
- 15, labelInfoColor
);
264 drawString( "no texgen", 10, 5, labelInfoColor
);
266 else if (texgenenabled
== 1) {
267 drawString( "no TexCoord", 10, h
- 15, labelInfoColor
);
268 sprintf( buf
, "texgen enabled for %s coordinate(s)",
269 coordnr
== 2 ? "S/T" : (coordnr
== 3 ? "S/T/R" : "S/T/R/Q"));
270 drawString( buf
, 10, 5, labelInfoColor
);
275 glTexEnvi( GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
277 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
278 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
280 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
281 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
283 loadTexture( textureWidth
, textureHeight
);
285 if ( drawTextured
) {
286 glEnable( GL_TEXTURE_2D
);
289 glDisable( GL_TEXTURE_GEN_S
);
290 glDisable( GL_TEXTURE_GEN_T
);
291 glDisable( GL_TEXTURE_GEN_R
);
292 glDisable( GL_TEXTURE_GEN_Q
);
294 glMatrixMode( GL_TEXTURE
);
296 glMatrixMode( GL_MODELVIEW
);
301 switch (texgenenabled
) {
304 glTexCoord2f( 0.0, 0.0 );
305 glVertex2f( -0.8, -0.8 );
307 glTexCoord2f( 1.0, 0.0 );
308 glVertex2f( 0.8, -0.8 );
310 glTexCoord2f( 1.0, 1.0 );
311 glVertex2f( 0.8, 0.8 );
313 glTexCoord2f( 0.0, 1.0 );
314 glVertex2f( -0.8, 0.8 );
318 glTranslatef( -0.8, -0.8, 0.0 );
319 glScalef( 1.6, 1.6, 1.0 );
320 glTexGeni( GL_S
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
321 glTexGeni( GL_T
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
322 glTexGeni( GL_R
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
323 glTexGeni( GL_Q
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
324 glTexGenfv(GL_S
, GL_OBJECT_PLANE
, ObjPlaneS3
);
325 glTexGenfv(GL_T
, GL_OBJECT_PLANE
, ObjPlaneT3
);
326 glTexGenfv(GL_R
, GL_OBJECT_PLANE
, nullPlane
);
327 glTexGenfv(GL_Q
, GL_OBJECT_PLANE
, nullPlane
);
329 glEnable( GL_TEXTURE_GEN_S
);
330 glEnable( GL_TEXTURE_GEN_T
);
332 /* Issue a texcoord here to be sure Q isn't left over from a
337 glVertex2f( 0.0, 0.0 );
338 glVertex2f( 1.0, 0.0 );
339 glVertex2f( 1.0, 1.0 );
340 glVertex2f( 0.0, 1.0 );
344 /* make sure that texgen T and non-texgen S coordinate are wrong */
345 glTexGeni( GL_S
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
346 glTexGeni( GL_T
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
347 glTexGeni( GL_R
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
348 glTexGeni( GL_Q
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
349 glTexGenfv(GL_S
, GL_OBJECT_PLANE
, ObjPlaneS1
);
350 glTexGenfv(GL_T
, GL_OBJECT_PLANE
, nullPlane
);
351 glTexGenfv(GL_R
, GL_OBJECT_PLANE
, nullPlane
);
352 glTexGenfv(GL_Q
, GL_OBJECT_PLANE
, nullPlane
);
354 glEnable( GL_TEXTURE_GEN_S
);
357 /* use z coordinate to get correct texgen values... */
358 glTexCoord2f( 0.0, 0.0 );
359 glVertex3f( -0.8, -0.8, 0.8 );
361 glTexCoord2f( 0.0, 0.0 );
362 glVertex3f( 0.8, -0.8, 0.2 );
364 glTexCoord2f( 0.0, 1.0 );
365 glVertex3f( 0.8, 0.8, 0.2 );
367 glTexCoord2f( 0.0, 1.0 );
368 glVertex3f( -0.8, 0.8, 0.8 );
374 glMatrixMode( GL_TEXTURE
);
375 glLoadMatrixf( texmat_swap_rq
);
376 glMatrixMode( GL_MODELVIEW
);
377 glTranslatef( -0.8, -0.8, 0.0 );
378 glScalef( 1.6, 1.6, 1.0 );
379 switch (texgenenabled
) {
382 glTexCoord3f( 0.0, 0.0, 0.5 );
383 glVertex2f( 0.0, 0.0 );
385 glTexCoord3f( 0.5, 0.0, 0.5 );
386 glVertex2f( 1.0, 0.0 );
388 glTexCoord3f( 0.5, 0.5, 0.5 );
389 glVertex2f( 1.0, 1.0 );
391 glTexCoord3f( 0.0, 0.5, 0.5 );
392 glVertex2f( 0.0, 1.0 );
396 glTexGeni( GL_S
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
397 glTexGeni( GL_T
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
398 glTexGeni( GL_R
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
399 glTexGeni( GL_Q
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
400 glTexGenfv(GL_S
, GL_OBJECT_PLANE
, ObjPlaneS2
);
401 glTexGenfv(GL_T
, GL_OBJECT_PLANE
, ObjPlaneT2
);
402 glTexGenfv(GL_R
, GL_OBJECT_PLANE
, ObjPlaneR
);
403 glTexGenfv(GL_Q
, GL_OBJECT_PLANE
, nullPlane
);
405 glEnable( GL_TEXTURE_GEN_S
);
406 glEnable( GL_TEXTURE_GEN_T
);
407 glEnable( GL_TEXTURE_GEN_R
);
409 glTexCoord1f( 0.0 ); /* to make sure Q is 1.0 */
411 glVertex3f( 0.0, 0.0, 0.5 );
412 glVertex3f( 1.0, 0.0, 0.5 );
413 glVertex3f( 1.0, 1.0, 0.5 );
414 glVertex3f( 0.0, 1.0, 0.5 );
418 /* make sure that texgen R/Q and non-texgen S/T coordinates are wrong */
419 glTexGeni( GL_S
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
420 glTexGeni( GL_T
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
421 glTexGeni( GL_R
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
422 glTexGeni( GL_Q
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
423 glTexGenfv(GL_S
, GL_OBJECT_PLANE
, ObjPlaneS2
);
424 glTexGenfv(GL_T
, GL_OBJECT_PLANE
, ObjPlaneT2
);
425 glTexGenfv(GL_R
, GL_OBJECT_PLANE
, nullPlane
);
426 glTexGenfv(GL_Q
, GL_OBJECT_PLANE
, nullPlane
);
428 glEnable( GL_TEXTURE_GEN_S
);
429 glEnable( GL_TEXTURE_GEN_T
);
432 glTexCoord3f( 0.0, 0.0, 0.5 );
433 glVertex2f( 0.0, 0.0);
435 glTexCoord3f( 0.0, 0.0, 0.5 );
436 glVertex2f( 1.0, 0.0);
438 glTexCoord3f( 0.0, 0.0, 0.5 );
439 glVertex2f( 1.0, 1.0);
441 glTexCoord3f( 0.0, 0.0, 0.5 );
442 glVertex2f( 0.0, 1.0);
448 switch (texgenenabled
) {
451 /* don't need r coordinate but still setting it I'm mean */
452 glTexCoord4f( 0.0, 0.0, 0.0, 0.5 );
453 glVertex2f( -0.8, -0.8 );
455 glTexCoord4f( 0.5, 0.0, 0.2, 0.5 );
456 glVertex2f( 0.8, -0.8 );
458 glTexCoord4f( 0.5, 0.5, 0.5, 0.5 );
459 glVertex2f( 0.8, 0.8 );
461 glTexCoord4f( 0.0, 0.5, 0.5, 0.5 );
462 glVertex2f( -0.8, 0.8 );
466 glTranslatef( -0.8, -0.8, 0.0 );
467 glScalef( 1.6, 1.6, 1.0 );
468 /* make sure that texgen R/Q and non-texgen S/T coordinates are wrong */
469 glTexGeni( GL_S
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
470 glTexGeni( GL_T
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
471 glTexGeni( GL_R
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
472 glTexGeni( GL_Q
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
473 glTexGenfv(GL_S
, GL_OBJECT_PLANE
, ObjPlaneS2
);
474 glTexGenfv(GL_T
, GL_OBJECT_PLANE
, ObjPlaneT2
);
475 glTexGenfv(GL_R
, GL_OBJECT_PLANE
, ObjPlaneR
);
476 glTexGenfv(GL_Q
, GL_OBJECT_PLANE
, ObjPlaneQ
);
478 glEnable( GL_TEXTURE_GEN_S
);
479 glEnable( GL_TEXTURE_GEN_T
);
480 glEnable( GL_TEXTURE_GEN_R
);
481 glEnable( GL_TEXTURE_GEN_Q
);
484 glVertex2f( 0.0, 0.0 );
485 glVertex2f( 1.0, 0.0 );
486 glVertex2f( 1.0, 1.0 );
487 glVertex2f( 0.0, 1.0 );
491 glTranslatef( -0.8, -0.8, 0.0 );
492 glScalef( 1.6, 1.6, 1.0 );
493 /* make sure that texgen R/Q and non-texgen S/T coordinates are wrong */
494 glTexGeni( GL_S
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
495 glTexGeni( GL_T
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
496 glTexGeni( GL_R
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
497 glTexGeni( GL_Q
, GL_TEXTURE_GEN_MODE
, GL_OBJECT_LINEAR
);
498 glTexGenfv(GL_S
, GL_OBJECT_PLANE
, ObjPlaneS2
);
499 glTexGenfv(GL_T
, GL_OBJECT_PLANE
, ObjPlaneT2
);
500 glTexGenfv(GL_R
, GL_OBJECT_PLANE
, nullPlane
);
501 glTexGenfv(GL_Q
, GL_OBJECT_PLANE
, nullPlane
);
503 glEnable( GL_TEXTURE_GEN_S
);
504 glEnable( GL_TEXTURE_GEN_T
);
507 glTexCoord4f( 0.0, 0.0, 0.0, 0.5 );
508 glVertex2f( 0.0, 0.0 );
510 glTexCoord4f( 0.0, 0.0, 0.2, 0.5 );
511 glVertex2f( 1.0, 0.0 );
513 glTexCoord4f( 0.0, 0.0, 0.5, 0.5 );
514 glVertex2f( 1.0, 1.0 );
516 glTexCoord4f( 0.0, 0.0, 0.75, 0.5 );
517 glVertex2f( 0.0, 1.0 );
525 glDisable( GL_TEXTURE_2D
);
529 static void display( void )
531 int numX
= 3, numY
= 3;
532 float xBase
= (float) winWidth
* 0.01;
533 float xOffset
= (winWidth
- xBase
) / numX
;
534 float xSize
= max( xOffset
- xBase
, 1 );
535 float yBase
= (float) winHeight
* 0.01;
536 float yOffset
= (winHeight
- yBase
) / numY
;
537 float ySize
= max( yOffset
- yBase
, 1 );
541 glViewport( 0, 0, winWidth
, winHeight
);
542 glDisable( GL_SCISSOR_TEST
);
543 glClearColor( 0.0, 0.0, 0.0, 0.0 );
544 glClear( GL_COLOR_BUFFER_BIT
);
545 glEnable( GL_SCISSOR_TEST
);
548 y
= (winHeight
- 1) - yOffset
;
550 for ( i
= 0 ; i
< numY
; i
++ )
553 labelInfoColor
= labelColor1
;
556 for ( j
= 0 ; j
< numX
; j
++ ) {
557 drawSample( x
, y
, xSize
, ySize
, i
, j
+2 );
565 if ( doubleBuffered
) {
574 static void usage( char *name
)
576 fprintf( stderr
, "usage: %s [ options ]\n", name
);
577 fprintf( stderr
, "\n" );
578 fprintf( stderr
, "options:\n" );
579 fprintf( stderr
, " -sb single buffered\n" );
580 fprintf( stderr
, " -db double buffered\n" );
581 fprintf( stderr
, " -info print OpenGL driver info\n" );
584 static void instructions( void )
586 fprintf( stderr
, "texgenmix - mixed texgen/non-texgen texture coordinate test\n" );
587 fprintf( stderr
, "all quads should look the same!\n" );
588 fprintf( stderr
, "\n" );
589 fprintf( stderr
, " [t] - toggle texturing\n" );
592 int main( int argc
, char *argv
[] )
594 GLboolean info
= GL_FALSE
;
597 glutInit( &argc
, argv
);
599 for ( i
= 1 ; i
< argc
; i
++ ) {
600 if ( !strcmp( "-sb", argv
[i
] ) ) {
601 doubleBuffered
= GL_FALSE
;
602 } else if ( !strcmp( "-db", argv
[i
] ) ) {
603 doubleBuffered
= GL_TRUE
;
604 } else if ( !strcmp( "-info", argv
[i
] ) ) {
612 if ( doubleBuffered
) {
613 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
615 glutInitDisplayMode( GLUT_RGB
| GLUT_SINGLE
);
618 glutInitWindowSize( winWidth
, winHeight
);
619 glutInitWindowPosition( 0, 0 );
620 glutCreateWindow( "Mixed texgen/non-texgen texture coordinate test" );
627 printf( "GL_RENDERER = %s\n", (char *) glGetString( GL_RENDERER
) );
628 printf( "GL_VERSION = %s\n", (char *) glGetString( GL_VERSION
) );
629 printf( "GL_VENDOR = %s\n", (char *) glGetString( GL_VENDOR
) ) ;
630 printf( "GL_EXTENSIONS = %s\n", (char *) glGetString( GL_EXTENSIONS
) );
633 glutDisplayFunc( display
);
634 glutReshapeFunc( reshape
);
635 glutKeyboardFunc( keyboard
);
636 glutSpecialFunc( special
);