progs/tests: compile with SCons and glew
[mesa.git] / progs / tests / bug_3195.c
1 /*
2 * Copyright (C) 2000 Brian Paul All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22 /**
23 * \file bug_3195.c
24 *
25 * Simple regression test for bug #3195. A bug in the i180 driver caused
26 * a segfault (inside the driver) when the LOD bias is adjusted and no texture
27 * is enabled. This test, which is based on progs/demos/lodbias.c, sets up
28 * all the texturing, disables all textures, adjusts the LOD bias, then
29 * re-enables \c GL_TEXTURE_2D.
30 *
31 * \author Brian Paul
32 * \author Ian Romanick <idr@us.ibm.com>
33 */
34
35 #include <assert.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <math.h>
39 #include <GL/glew.h>
40 #include <GL/glut.h>
41 #include <GL/glext.h>
42
43 #include "readtex.h"
44
45 #define TEXTURE_FILE "../images/girl.rgb"
46
47 static GLfloat Xrot = 0, Yrot = -30, Zrot = 0;
48 static GLint Bias = 0, BiasStepSign = +1; /* ints avoid fp precision problem */
49 static GLint BiasMin = -400, BiasMax = 400;
50
51
52
53 static void
54 PrintString(const char *s)
55 {
56 while (*s) {
57 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
58 s++;
59 }
60 }
61
62 static void Idle( void )
63 {
64 static int lastTime = 0;
65 int time = glutGet(GLUT_ELAPSED_TIME);
66 int step;
67
68 if (lastTime == 0)
69 lastTime = time;
70 else if (time - lastTime < 10)
71 return;
72
73 step = (time - lastTime) / 10 * BiasStepSign;
74 lastTime = time;
75
76 Bias += step;
77 if (Bias < BiasMin) {
78 exit(0);
79 }
80 else if (Bias > BiasMax) {
81 Bias = BiasMax;
82 BiasStepSign = -1;
83 }
84
85 glutPostRedisplay();
86 }
87
88
89 static void Display( void )
90 {
91 char str[100];
92
93 glClear( GL_COLOR_BUFFER_BIT );
94
95 glMatrixMode( GL_PROJECTION );
96 glLoadIdentity();
97 glOrtho(-1, 1, -1, 1, -1, 1);
98 glMatrixMode( GL_MODELVIEW );
99 glLoadIdentity();
100
101 glDisable(GL_TEXTURE_2D);
102 glColor3f(1,1,1);
103 glRasterPos3f(-0.9, -0.9, 0.0);
104 sprintf(str, "Texture LOD Bias = %4.1f", Bias * 0.01);
105 PrintString(str);
106
107 glMatrixMode( GL_PROJECTION );
108 glLoadIdentity();
109 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
110 glMatrixMode( GL_MODELVIEW );
111 glLoadIdentity();
112 glTranslatef( 0.0, 0.0, -8.0 );
113
114 glPushMatrix();
115 glRotatef(Xrot, 1, 0, 0);
116 glRotatef(Yrot, 0, 1, 0);
117 glRotatef(Zrot, 0, 0, 1);
118
119 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.01 * Bias);
120 glEnable(GL_TEXTURE_2D);
121
122 glBegin(GL_POLYGON);
123 glTexCoord2f(0, 0); glVertex2f(-1, -1);
124 glTexCoord2f(2, 0); glVertex2f( 1, -1);
125 glTexCoord2f(2, 2); glVertex2f( 1, 1);
126 glTexCoord2f(0, 2); glVertex2f(-1, 1);
127 glEnd();
128
129 glPopMatrix();
130
131 glutSwapBuffers();
132 }
133
134
135 static void Reshape( int width, int height )
136 {
137 glViewport( 0, 0, width, height );
138 }
139
140
141 static void Key( unsigned char key, int x, int y )
142 {
143 (void) x;
144 (void) y;
145 switch (key) {
146 case 27:
147 exit(0);
148 break;
149 }
150 glutPostRedisplay();
151 }
152
153
154 static void SpecialKey( int key, int x, int y )
155 {
156 const GLfloat step = 3.0;
157 (void) x;
158 (void) y;
159 switch (key) {
160 case GLUT_KEY_UP:
161 Xrot -= step;
162 break;
163 case GLUT_KEY_DOWN:
164 Xrot += step;
165 break;
166 case GLUT_KEY_LEFT:
167 Yrot -= step;
168 break;
169 case GLUT_KEY_RIGHT:
170 Yrot += step;
171 break;
172 }
173 glutPostRedisplay();
174 }
175
176
177 static void Init( void )
178 {
179 GLfloat maxBias;
180 const char * const ver_string = (const char * const)
181 glGetString( GL_VERSION );
182
183 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
184 printf("GL_VERSION = %s\n", ver_string);
185
186 printf("\nThis program should function nearly identically to Mesa's lodbias demo.\n"
187 "It should cycle through the complet LOD bias range once and exit. If bug\n"
188 "#3195 still exists, the demo should crash almost immediatly.\n");
189 printf("This is a regression test for bug #3195.\n");
190 printf("https://bugs.freedesktop.org/show_bug.cgi?id=3195\n");
191
192 if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
193 printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
194 exit(1);
195 }
196
197 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
198
199 if (glutExtensionSupported("GL_SGIS_generate_mipmap")) {
200 /* test auto mipmap generation */
201 GLint width, height, i;
202 GLenum format;
203 GLubyte *image = LoadRGBImage(TEXTURE_FILE, &width, &height, &format);
204 if (!image) {
205 printf("Error: could not load texture image %s\n", TEXTURE_FILE);
206 exit(1);
207 }
208 /* resize to 256 x 256 */
209 if (width != 256 || height != 256) {
210 GLubyte *newImage = malloc(256 * 256 * 4);
211 gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
212 256, 256, GL_UNSIGNED_BYTE, newImage);
213 free(image);
214 image = newImage;
215 }
216 printf("Using GL_SGIS_generate_mipmap\n");
217 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
218 glTexImage2D(GL_TEXTURE_2D, 0, format, 256, 256, 0,
219 format, GL_UNSIGNED_BYTE, image);
220 free(image);
221
222 /* make sure mipmap was really generated correctly */
223 width = height = 256;
224 for (i = 0; i < 9; i++) {
225 GLint w, h;
226 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w);
227 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h);
228 printf("Level %d size: %d x %d\n", i, w, h);
229 assert(w == width);
230 assert(h == height);
231 width /= 2;
232 height /= 2;
233 }
234
235 }
236 else if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
237 printf("Error: could not load texture image %s\n", TEXTURE_FILE);
238 exit(1);
239 }
240
241 /* mipmapping required for this extension */
242 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
243 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
244 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
245
246 glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT, &maxBias);
247 printf("LOD bias range: [%g, %g]\n", -maxBias, maxBias);
248 BiasMin = -100 * maxBias;
249 BiasMax = 100 * maxBias;
250
251 /* Since we have (about) 8 mipmap levels, no need to bias beyond
252 * the range [-1, +8].
253 */
254 if (BiasMin < -100)
255 BiasMin = -100;
256 if (BiasMax > 800)
257 BiasMax = 800;
258 }
259
260
261 int main( int argc, char *argv[] )
262 {
263 glutInit( &argc, argv );
264 glutInitWindowPosition( 0, 0 );
265 glutInitWindowSize( 350, 350 );
266 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
267 glutCreateWindow( "Bug #3195 Test" );
268 glewInit();
269 glutReshapeFunc( Reshape );
270 glutKeyboardFunc( Key );
271 glutSpecialFunc( SpecialKey );
272 glutDisplayFunc( Display );
273 glutIdleFunc(Idle);
274 Init();
275 glutMainLoop();
276 return 0;
277 }