Add more structure constructor tests.
[piglit/hramrach.git] / tests / texturing / crossbar.c
blobbc23ed0156159bb358161d3d220f8f4edda3d60d
1 /*
2 * (C) Copyright IBM Corporation 2005
3 * All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 /**
26 * \file crossbar.c
28 * Simple test of GL_ARB_texture_env_crossbar functionality. Several squares
29 * are drawn with different texture combine modes, but all should be rendered
30 * with the same final color.
32 * \author Ian Romanick <idr@us.ibm.com>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <GL/glut.h>
40 static const GLint tests[][8] = {
41 { 1, GL_REPLACE, GL_PRIMARY_COLOR, GL_PRIMARY_COLOR,
42 2, GL_REPLACE, GL_TEXTURE, GL_PRIMARY_COLOR },
43 { 3, GL_REPLACE, GL_PRIMARY_COLOR, GL_PRIMARY_COLOR,
44 2, GL_SUBTRACT, GL_TEXTURE0, GL_TEXTURE1 },
45 { 2, GL_REPLACE, GL_PRIMARY_COLOR, GL_PRIMARY_COLOR,
46 2, GL_REPLACE, GL_TEXTURE0, GL_TEXTURE0 },
47 { 2, GL_REPLACE, GL_PRIMARY_COLOR, GL_PRIMARY_COLOR,
48 1, GL_SUBTRACT, GL_TEXTURE0, GL_TEXTURE1 },
49 { 3, GL_ADD, GL_TEXTURE1, GL_TEXTURE1,
50 2, GL_MODULATE, GL_TEXTURE1, GL_PREVIOUS },
51 { 3, GL_ADD, GL_TEXTURE1, GL_TEXTURE1,
52 4, GL_MODULATE, GL_TEXTURE0, GL_PREVIOUS },
55 #define NUM_TESTS (sizeof(tests) / sizeof(tests[0]))
57 static int Width = 100 * (NUM_TESTS + 1);
58 static int Height = 100;
59 static int Interactive = 1;
62 static void DoFrame( void )
64 unsigned i;
66 glClearColor(0.0, 0.0, 1.0, 0);
67 glClear( GL_COLOR_BUFFER_BIT );
69 glPushMatrix();
71 /* This is the "reference" square.
74 glActiveTexture( GL_TEXTURE0 );
75 glDisable( GL_TEXTURE_2D );
76 glActiveTexture( GL_TEXTURE1 );
77 glDisable( GL_TEXTURE_2D );
79 glTranslatef(1.5, 0.0, 0.0);
80 glBegin(GL_QUADS);
81 glColor3f( 0.5, 0.5, 0.5 );
82 glVertex2f(-1, -1);
83 glVertex2f( 1, -1);
84 glVertex2f( 1, 1);
85 glVertex2f(-1, 1);
86 glEnd();
88 for ( i = 0 ; i < NUM_TESTS ; i++ ) {
89 glActiveTexture( GL_TEXTURE0 );
90 glEnable( GL_TEXTURE_2D );
91 glBindTexture( GL_TEXTURE_2D, tests[i][0] );
92 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE );
93 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, tests[i][1] );
94 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB, tests[i][2] );
95 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB, tests[i][3] );
97 glActiveTexture( GL_TEXTURE1 );
98 glEnable( GL_TEXTURE_2D );
99 glBindTexture( GL_TEXTURE_2D, tests[i][4] );
100 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE );
101 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, tests[i][5] );
102 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB, tests[i][6] );
103 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB, tests[i][7] );
105 glCallList(1);
108 glPopMatrix();
112 static int DoTest( void )
114 int i;
115 GLfloat probe[NUM_TESTS+1][4];
116 GLfloat dr, dg, db;
117 GLfloat dmax;
119 glReadBuffer( GL_BACK );
121 dmax = 0;
122 for( i = 0; i <= NUM_TESTS; ++i ) {
123 glReadPixels(Width*(2*i+1)/((NUM_TESTS+1)*2), Height/2, 1, 1, GL_RGBA, GL_FLOAT, probe[i]);
124 printf("Probe %i: %f,%f,%f\n", i, probe[i][0], probe[i][1], probe[i][2]);
125 dr = probe[i][0] - 0.5f;
126 dg = probe[i][1] - 0.5f;
127 db = probe[i][2] - 0.5f;
128 printf(" Delta: %f,%f,%f\n", dr, dg, db);
129 if (dr > dmax) dmax = dr;
130 else if (-dr > dmax) dmax = -dr;
131 if (dg > dmax) dmax = dg;
132 else if (-dg > dmax) dmax = -dg;
133 if (db > dmax) dmax = db;
134 else if (-db > dmax) dmax = -db;
137 printf("Max delta: %f\n", dmax);
139 if (dmax >= 0.07) // roughly 1/128
140 return 0;
141 else
142 return 1;
145 static void Display( void )
147 if (Interactive) {
148 DoFrame();
149 glutSwapBuffers();
150 } else {
151 int success, retry;
153 printf("\nFirst frame\n-----------\n");
154 DoFrame();
155 success = DoTest();
156 glutSwapBuffers();
158 printf("\nSecond frame\n------------\n");
159 glMatrixMode( GL_PROJECTION );
160 glLoadIdentity();
161 glOrtho( 0, 3*(NUM_TESTS+1), -1.5, 1.5, -1, 1 );
162 glMatrixMode( GL_MODELVIEW );
163 glLoadIdentity();
165 DoFrame();
166 retry = DoTest();
167 glutSwapBuffers();
169 if (retry && success) {
170 printf("\nPIGLIT: { 'result': 'pass' }\n");
171 } else if (retry || success) {
172 printf("\nPIGLIT: { 'result': 'warn', 'note': 'Inconsistent results in first and second frame' }\n");
173 } else {
174 printf("\nPIGLIT: { 'result': 'fail' }\n");
177 exit(0);
182 static void Reshape( int width, int height )
184 Width = width;
185 Height = height;
186 glViewport( 0, 0, width, height );
187 glMatrixMode( GL_PROJECTION );
188 glLoadIdentity();
189 glOrtho( 0, 3*(NUM_TESTS+1), -1.5, 1.5, -1, 1 );
190 glMatrixMode( GL_MODELVIEW );
191 glLoadIdentity();
195 static void Key( unsigned char key, int x, int y )
197 (void) x;
198 (void) y;
199 switch (key) {
200 case 27:
201 exit(0);
202 break;
204 glutPostRedisplay();
208 static void Init( void )
210 const char * const ver_string = (const char * const)
211 glGetString( GL_VERSION );
212 float ver = strtof( ver_string, NULL );
213 GLint tex_units;
214 GLint temp[ 256 ];
217 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
218 printf("GL_VERSION = %s\n", ver_string);
220 if ( (!glutExtensionSupported("GL_ARB_multitexture")
221 && (ver < 1.3))
222 || (!glutExtensionSupported("GL_ARB_texture_env_combine")
223 && !glutExtensionSupported("GL_EXT_texture_env_combine")
224 && (ver < 1.3))
225 || (!glutExtensionSupported("GL_ARB_texture_env_crossbar")
226 && !glutExtensionSupported("GL_NV_texture_env_combine4")
227 && (ver < 1.4)) ) {
228 printf("\nSorry, this program requires GL_ARB_multitexture and either\n"
229 "GL_ARB_texture_env_combine or GL_EXT_texture_env_combine (or OpenGL 1.3).\n"
230 "Either GL_ARB_texture_env_crossbar or GL_NV_texture_env_combine4 (or\n"
231 "OpenGL 1.4) are also required.\n");
232 if (!Interactive)
233 printf("PIGLIT: {'result': 'fail' }\n");
234 exit(1);
237 glGetIntegerv( GL_MAX_TEXTURE_UNITS, & tex_units );
238 if ( tex_units < 2 ) {
239 printf("\nSorry, this program requires at least 2 texture units.\n");
240 if (!Interactive)
241 printf("PIGLIT: {'result': 'fail' }\n");
242 exit(1);
245 if (Interactive)
246 printf("\nAll %u squares should be the same color.\n", NUM_TESTS + 1);
248 (void) memset( temp, 0x00, sizeof( temp ) );
249 glBindTexture( GL_TEXTURE_2D, 1 );
250 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
251 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
252 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0,
253 GL_RGBA, GL_UNSIGNED_BYTE, temp );
255 (void) memset( temp, 0x7f, sizeof( temp ) );
256 glBindTexture( GL_TEXTURE_2D, 2 );
257 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
258 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
259 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0,
260 GL_RGBA, GL_UNSIGNED_BYTE, temp );
262 (void) memset( temp, 0xff, sizeof( temp ) );
263 glBindTexture( GL_TEXTURE_2D, 3 );
264 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
265 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
266 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0,
267 GL_RGBA, GL_UNSIGNED_BYTE, temp );
269 (void) memset( temp, 0x3f, sizeof( temp ) );
270 glBindTexture( GL_TEXTURE_2D, 4 );
271 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
272 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
273 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0,
274 GL_RGBA, GL_UNSIGNED_BYTE, temp );
277 glNewList( 1, GL_COMPILE );
278 glTranslatef(3.0, 0, 0);
279 glBegin(GL_QUADS);
280 glColor3f( 0.9, 0.0, 0.0 );
281 glMultiTexCoord2f( GL_TEXTURE0, 0.5, 0.5 );
282 glMultiTexCoord2f( GL_TEXTURE1, 0.5, 0.5 );
283 glVertex2f(-1, -1);
284 glVertex2f( 1, -1);
285 glVertex2f( 1, 1);
286 glVertex2f(-1, 1);
287 glEnd();
288 glEndList();
290 Reshape(Width, Height);
294 int main( int argc, char *argv[] )
296 glutInit( &argc, argv );
297 if (argc == 2 && !strcmp(argv[1], "-auto"))
298 Interactive = 0;
299 glutInitWindowPosition( 0, 0 );
300 glutInitWindowSize( Width, Height );
301 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
302 glutCreateWindow( "GL_ARB_texture_env_crossbar test" );
303 glutReshapeFunc( Reshape );
304 glutKeyboardFunc( Key );
305 glutDisplayFunc( Display );
306 Init();
307 glutMainLoop();
308 return 0;