Merge remote branch 'main/master' into radeon-rewrite
[mesa.git] / progs / tests / crossbar.c
1 /*
2 * (C) Copyright IBM Corporation 2005
3 * All Rights Reserved.
4 *
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:
11 *
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.
15 *
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.
23 */
24
25 /**
26 * \file crossbar.c
27 *
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.
31 *
32 * \author Ian Romanick <idr@us.ibm.com>
33 */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <GL/glew.h>
39 #include <GL/glut.h>
40
41 static const GLint tests[][8] = {
42 { 1, GL_REPLACE, GL_PRIMARY_COLOR, GL_PRIMARY_COLOR,
43 2, GL_REPLACE, GL_TEXTURE, GL_PRIMARY_COLOR },
44 { 3, GL_REPLACE, GL_PRIMARY_COLOR, GL_PRIMARY_COLOR,
45 2, GL_SUBTRACT, GL_TEXTURE0, GL_TEXTURE1 },
46 { 2, GL_REPLACE, GL_PRIMARY_COLOR, GL_PRIMARY_COLOR,
47 2, GL_REPLACE, GL_TEXTURE0, GL_TEXTURE0 },
48 { 2, GL_REPLACE, GL_PRIMARY_COLOR, GL_PRIMARY_COLOR,
49 1, GL_SUBTRACT, GL_TEXTURE0, GL_TEXTURE1 },
50 { 3, GL_ADD, GL_TEXTURE1, GL_TEXTURE1,
51 2, GL_MODULATE, GL_TEXTURE1, GL_PREVIOUS },
52 { 3, GL_ADD, GL_TEXTURE1, GL_TEXTURE1,
53 4, GL_MODULATE, GL_TEXTURE0, GL_PREVIOUS },
54 };
55
56 #define NUM_TESTS (sizeof(tests) / sizeof(tests[0]))
57
58 static int Width = 100 * (NUM_TESTS + 1);
59 static int Height = 200;
60 static const GLfloat Near = 5.0, Far = 25.0;
61
62
63 static void Display( void )
64 {
65 unsigned i;
66
67
68 glClearColor(0.2, 0.2, 0.8, 0);
69 glClear( GL_COLOR_BUFFER_BIT );
70
71 glPushMatrix();
72
73 /* This is the "reference" square.
74 */
75
76 glActiveTexture( GL_TEXTURE0 );
77 glDisable( GL_TEXTURE_2D );
78 glActiveTexture( GL_TEXTURE1 );
79 glDisable( GL_TEXTURE_2D );
80
81 glTranslatef(-(NUM_TESTS * 1.5), 0, 0);
82 glBegin(GL_QUADS);
83 glColor3f( 0.5, 0.5, 0.5 );
84 glVertex2f(-1, -1);
85 glVertex2f( 1, -1);
86 glVertex2f( 1, 1);
87 glVertex2f(-1, 1);
88 glEnd();
89
90 for ( i = 0 ; i < NUM_TESTS ; i++ ) {
91 glActiveTexture( GL_TEXTURE0 );
92 glEnable( GL_TEXTURE_2D );
93 glBindTexture( GL_TEXTURE_2D, tests[i][0] );
94 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE );
95 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, tests[i][1] );
96 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB, tests[i][2] );
97 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB, tests[i][3] );
98
99 glActiveTexture( GL_TEXTURE1 );
100 glEnable( GL_TEXTURE_2D );
101 glBindTexture( GL_TEXTURE_2D, tests[i][4] );
102 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE );
103 glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, tests[i][5] );
104 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB, tests[i][6] );
105 glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB, tests[i][7] );
106
107 glCallList(1);
108 }
109
110 glPopMatrix();
111
112 glutSwapBuffers();
113 }
114
115
116 static void Reshape( int width, int height )
117 {
118 GLfloat ar = (float) width / (float) height;
119 Width = width;
120 Height = height;
121 glViewport( 0, 0, width, height );
122 glMatrixMode( GL_PROJECTION );
123 glLoadIdentity();
124 glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
125 glMatrixMode( GL_MODELVIEW );
126 glLoadIdentity();
127 glTranslatef( 0.0, 0.0, -15.0 );
128 }
129
130
131 static void Key( unsigned char key, int x, int y )
132 {
133 (void) x;
134 (void) y;
135 switch (key) {
136 case 27:
137 exit(0);
138 break;
139 }
140 glutPostRedisplay();
141 }
142
143
144 static void Init( void )
145 {
146 const char * const ver_string = (const char * const)
147 glGetString( GL_VERSION );
148 float ver = strtof( ver_string, NULL );
149 GLint tex_units;
150 GLint temp[ 256 ];
151
152
153 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
154 printf("GL_VERSION = %s\n", ver_string);
155
156 if ( (!glutExtensionSupported("GL_ARB_multitexture")
157 && (ver < 1.3))
158 || (!glutExtensionSupported("GL_ARB_texture_env_combine")
159 && !glutExtensionSupported("GL_EXT_texture_env_combine")
160 && (ver < 1.3))
161 || (!glutExtensionSupported("GL_ARB_texture_env_crossbar")
162 && !glutExtensionSupported("GL_NV_texture_env_combine4")
163 && (ver < 1.4)) ) {
164 printf("\nSorry, this program requires GL_ARB_multitexture and either\n"
165 "GL_ARB_texture_env_combine or GL_EXT_texture_env_combine (or OpenGL 1.3).\n"
166 "Either GL_ARB_texture_env_crossbar or GL_NV_texture_env_combine4 (or\n"
167 "OpenGL 1.4) are also required.\n");
168 exit(1);
169 }
170
171 glGetIntegerv( GL_MAX_TEXTURE_UNITS, & tex_units );
172 if ( tex_units < 2 ) {
173 printf("\nSorry, this program requires at least 2 texture units.\n");
174 exit(1);
175 }
176
177 printf("\nAll %u squares should be the same color.\n", NUM_TESTS + 1);
178
179 (void) memset( temp, 0x00, sizeof( temp ) );
180 glBindTexture( GL_TEXTURE_2D, 1 );
181 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
182 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
183 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0,
184 GL_RGBA, GL_UNSIGNED_BYTE, temp );
185
186 (void) memset( temp, 0x7f, sizeof( temp ) );
187 glBindTexture( GL_TEXTURE_2D, 2 );
188 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
189 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
190 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0,
191 GL_RGBA, GL_UNSIGNED_BYTE, temp );
192
193 (void) memset( temp, 0xff, sizeof( temp ) );
194 glBindTexture( GL_TEXTURE_2D, 3 );
195 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
196 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
197 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0,
198 GL_RGBA, GL_UNSIGNED_BYTE, temp );
199
200 (void) memset( temp, 0x3f, sizeof( temp ) );
201 glBindTexture( GL_TEXTURE_2D, 4 );
202 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
203 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
204 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0,
205 GL_RGBA, GL_UNSIGNED_BYTE, temp );
206
207
208 glNewList( 1, GL_COMPILE );
209 glTranslatef(3.0, 0, 0);
210 glBegin(GL_QUADS);
211 glColor3f( 0.9, 0.0, 0.0 );
212 glMultiTexCoord2f( GL_TEXTURE0, 0.5, 0.5 );
213 glMultiTexCoord2f( GL_TEXTURE1, 0.5, 0.5 );
214 glVertex2f(-1, -1);
215 glVertex2f( 1, -1);
216 glVertex2f( 1, 1);
217 glVertex2f(-1, 1);
218 glEnd();
219 glEndList();
220 }
221
222
223 int main( int argc, char *argv[] )
224 {
225 glutInit( &argc, argv );
226 glutInitWindowPosition( 0, 0 );
227 glutInitWindowSize( Width, Height );
228 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
229 glutCreateWindow( "GL_ARB_texture_env_crossbar test" );
230 glewInit();
231 glutReshapeFunc( Reshape );
232 glutKeyboardFunc( Key );
233 glutDisplayFunc( Display );
234 Init();
235 glutMainLoop();
236 return 0;
237 }