progs/tests: disable blending while drawing text
[mesa.git] / progs / tests / mipmap_view.c
1 /*
2 * Test mipmap generation and lod bias.
3 *
4 * Brian Paul
5 * 17 March 2008
6 */
7
8
9 #include <assert.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <math.h>
13 #include <GL/glew.h>
14 #include <GL/glut.h>
15 #include <GL/glext.h>
16
17 #include "readtex.h"
18
19 #define TEXTURE_FILE "../images/arch.rgb"
20
21 #define LEVELS 8
22 #define SIZE (1<<LEVELS)
23 static int TexWidth = SIZE, TexHeight = SIZE;
24 static int WinWidth = 1044, WinHeight = 900;
25 static GLfloat Bias = 0.0;
26 static GLboolean ScaleQuads = GL_FALSE;
27 static GLboolean Linear = GL_FALSE;
28 static GLint Win = 0;
29 static GLint RenderTextureLevel = 0;
30 static GLuint TexObj;
31
32
33
34 static void
35 CheckError(int line)
36 {
37 GLenum err = glGetError();
38 if (err) {
39 printf("GL Error 0x%x at line %d\n", (int) err, line);
40 }
41 }
42
43
44
45 static void
46 PrintString(const char *s)
47 {
48 while (*s) {
49 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
50 s++;
51 }
52 }
53
54
55
56
57 static void
58 MipGenTexture( void )
59 {
60 /* test auto mipmap generation */
61 GLint width, height, i;
62 GLenum format;
63 GLubyte *image = LoadRGBImage(TEXTURE_FILE, &width, &height, &format);
64 if (!image) {
65 printf("Error: could not load texture image %s\n", TEXTURE_FILE);
66 exit(1);
67 }
68 /* resize to TexWidth x TexHeight */
69 if (width != TexWidth || height != TexHeight) {
70 GLubyte *newImage = malloc(TexWidth * TexHeight * 4);
71
72 fprintf(stderr, "rescale %d %d to %d %d\n", width, height,
73 TexWidth, TexHeight);
74 fflush(stderr);
75
76 gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
77 TexWidth, TexHeight, GL_UNSIGNED_BYTE, newImage);
78 free(image);
79 image = newImage;
80 }
81 printf("Using GL_SGIS_generate_mipmap\n");
82 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
83 glTexImage2D(GL_TEXTURE_2D, 0, format, TexWidth, TexHeight, 0,
84 format, GL_UNSIGNED_BYTE, image);
85 free(image);
86
87 /* make sure mipmap was really generated correctly */
88 width = TexWidth;
89 height = TexHeight;
90 for (i = 0; i < 9; i++) {
91 GLint w, h;
92 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w);
93 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h);
94 printf("Level %d size: %d x %d\n", i, w, h);
95 assert(w == width);
96 assert(h == height);
97 width /= 2;
98 height /= 2;
99 }
100
101
102 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_FALSE);
103 }
104
105
106
107 static void
108 ResetTextureLevel( int i )
109 {
110 GLubyte tex2d[SIZE*SIZE][4];
111
112 {
113 GLint Width = TexWidth / (1 << i);
114 GLint Height = TexHeight / (1 << i);
115 GLint s, t;
116
117 for (s = 0; s < Width; s++) {
118 for (t = 0; t < Height; t++) {
119 tex2d[t*Width+s][0] = ((s / 16) % 2) ? 0 : 255;
120 tex2d[t*Width+s][1] = ((t / 16) % 2) ? 0 : 255;
121 tex2d[t*Width+s][2] = 128;
122 tex2d[t*Width+s][3] = 255;
123 }
124 }
125
126 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
127
128 glTexImage2D(GL_TEXTURE_2D, i, GL_RGB, Width, Height, 0,
129 GL_RGBA, GL_UNSIGNED_BYTE, tex2d);
130 }
131 }
132
133
134 static void
135 ResetTexture( void )
136 {
137 #if 0
138 /* This doesn't work so well as the arch texture is 512x512.
139 */
140 LoadRGBMipmaps(TEXTURE_FILE, GL_RGB);
141 #else
142 {
143 int i;
144
145 for (i = 0; i <= LEVELS; i++)
146 {
147 ResetTextureLevel(i);
148 }
149 }
150 #endif
151 }
152
153
154
155
156
157
158
159 static void
160 RenderTexture( void )
161 {
162 GLenum status;
163 GLuint MyFB;
164
165 fprintf(stderr, "RenderTextureLevel %d\n", RenderTextureLevel);
166 fflush(stderr);
167
168 /* gen framebuffer id, delete it, do some assertions, just for testing */
169 glGenFramebuffersEXT(1, &MyFB);
170 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
171 assert(glIsFramebufferEXT(MyFB));
172
173 CheckError(__LINE__);
174
175 /* Render color to texture */
176 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
177 GL_COLOR_ATTACHMENT0_EXT,
178 GL_TEXTURE_2D, TexObj,
179 RenderTextureLevel);
180
181
182
183 CheckError(__LINE__);
184
185
186 glMatrixMode(GL_PROJECTION);
187 glLoadIdentity();
188 glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
189 glMatrixMode(GL_MODELVIEW);
190 glLoadIdentity();
191 glTranslatef(0.0, 0.0, -15.0);
192
193 status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
194 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
195 printf("Framebuffer incomplete!!!\n");
196 }
197
198 glViewport(0, 0,
199 TexWidth / (1 << RenderTextureLevel),
200 TexHeight / (1 << RenderTextureLevel));
201
202 glClearColor(0.5, 0.5, 1.0, 0.0);
203 glClear(GL_COLOR_BUFFER_BIT);
204
205 CheckError(__LINE__);
206
207 glBegin(GL_POLYGON);
208 glColor3f(1, 0, 0);
209 glVertex2f(-1, -1);
210 glColor3f(0, 1, 0);
211 glVertex2f(1, -1);
212 glColor3f(0, 0, 1);
213 glVertex2f(0, 1);
214 glEnd();
215
216
217 /* Bind normal framebuffer */
218 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
219 CheckError(__LINE__);
220
221 glDeleteFramebuffersEXT(1, &MyFB);
222 CheckError(__LINE__);
223
224 glClearColor(0, 0, 0, 0);
225 }
226
227 static void
228 Display(void)
229 {
230 int x, y, bias;
231 char str[100];
232 int texWidth = TexWidth, texHeight = TexHeight;
233
234 glViewport(0, 0, WinHeight, WinHeight);
235
236 glClear(GL_COLOR_BUFFER_BIT);
237
238 glMatrixMode(GL_PROJECTION);
239 glLoadIdentity();
240 glOrtho(0, WinWidth, 0, WinHeight, -1, 1);
241 glMatrixMode(GL_MODELVIEW);
242 glLoadIdentity();
243
244 glColor3f(1,1,1);
245
246 if (Linear) {
247 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
248 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
249 }
250 else {
251 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
252 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
253 }
254
255 y = WinHeight - 300;
256 x = 4;
257
258 for (bias = -1; bias < 11; bias++) {
259
260 if (ScaleQuads) {
261 if (bias > 0) {
262 if (texWidth == 1 && texHeight == 1)
263 break;
264 texWidth = TexWidth >> bias;
265 texHeight = TexHeight >> bias;
266 if (texWidth < 1)
267 texWidth = 1;
268 if (texHeight < 1)
269 texHeight = 1;
270 }
271 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.0);
272 }
273 else {
274 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, bias);
275 }
276
277 glRasterPos2f(x, y + TexHeight + 5);
278 if (ScaleQuads)
279 sprintf(str, "Texture Level %d: %d x %d",
280 (bias < 0 ? 0 : bias),
281 texWidth, texHeight);
282 else
283 sprintf(str, "Texture LOD Bias = %d", bias);
284 PrintString(str);
285
286 glPushMatrix();
287 glTranslatef(x, y, 0);
288
289 glEnable(GL_TEXTURE_2D);
290
291 glBegin(GL_POLYGON);
292 glTexCoord2f(0, 0); glVertex2f(0, 0);
293 glTexCoord2f(1, 0); glVertex2f(texWidth, 0);
294 glTexCoord2f(1, 1); glVertex2f(texWidth, texHeight);
295 glTexCoord2f(0, 1); glVertex2f(0, texHeight);
296 glEnd();
297
298 glPopMatrix();
299
300 glDisable(GL_TEXTURE_2D);
301
302 x += TexWidth + 4;
303 if (x >= WinWidth) {
304 x = 4;
305 y -= 300;
306 }
307 }
308
309 glutSwapBuffers();
310 }
311
312
313 static void
314 Reshape(int width, int height)
315 {
316 WinWidth = width;
317 WinHeight = height;
318 }
319
320
321 static void
322 Key(unsigned char key, int x, int y)
323 {
324 (void) x;
325 (void) y;
326 switch (key) {
327 case 'b':
328 Bias -= 10;
329 break;
330 case 'B':
331 Bias += 10;
332 break;
333 case 'l':
334 Linear = !Linear;
335 break;
336 case 'v':
337 RenderTextureLevel++;
338 break;
339 case 'V':
340 RenderTextureLevel--;
341 break;
342 case 'r':
343 RenderTexture();
344 break;
345 case 'X':
346 ResetTexture();
347 break;
348 case 'x':
349 ResetTextureLevel(RenderTextureLevel);
350 break;
351 case '0':
352 case '1':
353 case '2':
354 case '3':
355 case '4':
356 case '5':
357 case '6':
358 case '7':
359 case '8':
360 case '9':
361 Bias = 100.0 * (key - '0');
362 break;
363 case 's':
364 ScaleQuads = !ScaleQuads;
365 break;
366 case ' ':
367 MipGenTexture();
368 Bias = 0;
369 Linear = 0;
370 RenderTextureLevel = 0;
371 ScaleQuads = 0;
372 break;
373
374 case 27:
375 glutDestroyWindow(Win);
376 exit(0);
377 break;
378 }
379 glutPostRedisplay();
380 }
381
382
383 static void
384 Init(void)
385 {
386 GLfloat maxBias;
387
388 if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
389 printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
390 exit(1);
391 }
392
393 if (!glutExtensionSupported("GL_SGIS_generate_mipmap")) {
394 printf("Sorry, GL_SGIS_generate_mipmap not supported by this renderer.\n");
395 exit(1);
396 }
397
398 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
399
400 glGenTextures(1, &TexObj);
401 glBindTexture(GL_TEXTURE_2D, TexObj);
402
403 if (1)
404 MipGenTexture();
405 else
406 ResetTexture();
407
408 /* mipmapping required for this extension */
409 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
410
411 glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT, &maxBias);
412
413 printf("GL_RENDERER: %s\n", (char*) glGetString(GL_RENDERER));
414 printf("LOD bias range: [%g, %g]\n", -maxBias, maxBias);
415
416 printf("Press 's' to toggle quad scaling\n");
417 }
418
419
420 int
421 main(int argc, char *argv[])
422 {
423 glutInit(&argc, argv);
424 glutInitWindowPosition(0, 0);
425 glutInitWindowSize(WinWidth, WinHeight);
426 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
427 Win = glutCreateWindow(argv[0]);
428 glewInit();
429 glutReshapeFunc(Reshape);
430 glutKeyboardFunc(Key);
431 glutDisplayFunc(Display);
432 Init();
433 glutMainLoop();
434 return 0;
435 }