GL_EXT_secondary_color test
[mesa.git] / progs / tests / seccolor.c
1 /*
2 * Exercise GL_EXT_secondary_color
3 */
4
5
6 #define GL_GLEXT_PROTOTYPES
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <math.h>
10 #include <GL/glut.h>
11
12 static int Width = 600;
13 static int Height = 200;
14 static GLfloat Near = 5.0, Far = 25.0;
15
16
17 static void Display( void )
18 {
19 GLfloat t;
20
21 glClearColor(0.2, 0.2, 0.8, 0);
22 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
23
24 for (t = 0.0; t <= 1.0; t += 0.25) {
25 GLfloat x = t * 10.0 - 5.0;
26 GLfloat g = t;
27
28 /* top row: untextured */
29 glColor3f(1, 0, 0);
30 glPushMatrix();
31 glTranslatef(x, 1.2, 0);
32 #if defined(GL_EXT_secondary_color)
33 glSecondaryColor3fEXT(0, g, 0);
34 #endif
35 glBegin(GL_POLYGON);
36 glVertex2f(-1, -1);
37 glVertex2f( 1, -1);
38 glVertex2f( 1, 1);
39 glVertex2f(-1, 1);
40 glEnd();
41 glPopMatrix();
42
43 /* bottom row: textured */
44 glColor3f(1, 1, 1);
45 glEnable(GL_TEXTURE_2D);
46 glPushMatrix();
47 glTranslatef(x, -1.2, 0);
48 #if defined(GL_EXT_secondary_color)
49 glSecondaryColor3fEXT(0, g, 0);
50 #endif
51 glBegin(GL_POLYGON);
52 glTexCoord2f(0, 0); glVertex2f(-1, -1);
53 glTexCoord2f(1, 0); glVertex2f( 1, -1);
54 glTexCoord2f(1, 1); glVertex2f( 1, 1);
55 glTexCoord2f(0, 1); glVertex2f(-1, 1);
56 glEnd();
57 glPopMatrix();
58 glDisable(GL_TEXTURE_2D);
59 }
60 glutSwapBuffers();
61 }
62
63
64 static void Reshape( int width, int height )
65 {
66 GLfloat ar = (float) width / (float) height;
67 Width = width;
68 Height = height;
69 glViewport( 0, 0, width, height );
70 glMatrixMode( GL_PROJECTION );
71 glLoadIdentity();
72 glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
73 glMatrixMode( GL_MODELVIEW );
74 glLoadIdentity();
75 glTranslatef( 0.0, 0.0, -15.0 );
76 }
77
78
79 static void Key( unsigned char key, int x, int y )
80 {
81 (void) x;
82 (void) y;
83 switch (key) {
84 case 27:
85 exit(0);
86 break;
87 }
88 glutPostRedisplay();
89 }
90
91
92 static void Init( void )
93 {
94 GLubyte image[4*4][3];
95 GLint i;
96 if (!glutExtensionSupported("GL_EXT_secondary_color")) {
97 printf("Sorry, this program requires GL_EXT_secondary_color\n");
98 exit(1);
99 }
100
101 /* setup red texture with one back texel */
102 for (i = 0; i < 4*4; i++) {
103 if (i == 0) {
104 image[i][0] = 0;
105 image[i][1] = 0;
106 image[i][2] = 0;
107 }
108 else {
109 image[i][0] = 255;
110 image[i][1] = 0;
111 image[i][2] = 0;
112 }
113 }
114 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0,
115 GL_RGB, GL_UNSIGNED_BYTE, image);
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
118 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
119
120 #if defined(GL_EXT_secondary_color)
121 glEnable(GL_COLOR_SUM_EXT);
122 #endif
123 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
124
125 printf("Squares should be colored from red -> orange -> yellow.\n");
126 printf("Top row is untextured.\n");
127 printf("Bottom row is textured (red texture with one black texel).\n");
128 printf("Rows should be identical, except for lower-left texel.\n");
129 }
130
131
132 int main( int argc, char *argv[] )
133 {
134 glutInit( &argc, argv );
135 glutInitWindowPosition( 0, 0 );
136 glutInitWindowSize( Width, Height );
137 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
138 glutCreateWindow(argv[0]);
139 glutReshapeFunc( Reshape );
140 glutKeyboardFunc( Key );
141 glutDisplayFunc( Display );
142 Init();
143 glutMainLoop();
144 return 0;
145 }