Merge commit 'origin/master' into gallium-0.2
[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/glut.h>
14 #include <GL/glext.h>
15
16 #include "readtex.h"
17
18 #define TEXTURE_FILE "../images/arch.rgb"
19
20 static int TexWidth = 256, TexHeight = 256;
21 static int WinWidth = 1044, WinHeight = 900;
22 static GLfloat Bias = 0.0;
23 static GLboolean ScaleQuads = GL_FALSE;
24 static GLint Win = 0;
25
26
27
28 static void
29 PrintString(const char *s)
30 {
31 while (*s) {
32 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
33 s++;
34 }
35 }
36
37
38 static void
39 Display(void)
40 {
41 int x, y, bias;
42 char str[100];
43 int texWidth = TexWidth, texHeight = TexHeight;
44
45 glClear(GL_COLOR_BUFFER_BIT);
46
47 glMatrixMode(GL_PROJECTION);
48 glLoadIdentity();
49 glOrtho(0, WinWidth, 0, WinHeight, -1, 1);
50 glMatrixMode(GL_MODELVIEW);
51 glLoadIdentity();
52
53 glColor3f(1,1,1);
54
55 y = WinHeight - 300;
56 x = 4;
57
58 for (bias = -1; bias < 11; bias++) {
59
60 if (ScaleQuads) {
61 if (bias > 0) {
62 if (texWidth == 1 && texHeight == 1)
63 break;
64 texWidth = TexWidth >> bias;
65 texHeight = TexHeight >> bias;
66 if (texWidth < 1)
67 texWidth = 1;
68 if (texHeight < 1)
69 texHeight = 1;
70 }
71 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.0);
72 }
73 else {
74 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, bias);
75 }
76
77 glRasterPos2f(x, y + TexHeight + 5);
78 if (ScaleQuads)
79 sprintf(str, "Texture Level %d: %d x %d",
80 (bias < 0 ? 0 : bias),
81 texWidth, texHeight);
82 else
83 sprintf(str, "Texture LOD Bias = %d", bias);
84 PrintString(str);
85
86 glPushMatrix();
87 glTranslatef(x, y, 0);
88
89 glEnable(GL_TEXTURE_2D);
90
91 glBegin(GL_POLYGON);
92 glTexCoord2f(0, 0); glVertex2f(0, 0);
93 glTexCoord2f(1, 0); glVertex2f(texWidth, 0);
94 glTexCoord2f(1, 1); glVertex2f(texWidth, texHeight);
95 glTexCoord2f(0, 1); glVertex2f(0, texHeight);
96 glEnd();
97
98 glPopMatrix();
99
100 glDisable(GL_TEXTURE_2D);
101
102 x += TexWidth + 4;
103 if (x >= WinWidth) {
104 x = 4;
105 y -= 300;
106 }
107 }
108
109 glutSwapBuffers();
110 }
111
112
113 static void
114 Reshape(int width, int height)
115 {
116 WinWidth = width;
117 WinHeight = height;
118 glViewport(0, 0, width, height);
119 }
120
121
122 static void
123 Key(unsigned char key, int x, int y)
124 {
125 (void) x;
126 (void) y;
127 switch (key) {
128 case 'b':
129 Bias -= 10;
130 break;
131 case 'B':
132 Bias += 10;
133 break;
134 case '0':
135 case '1':
136 case '2':
137 case '3':
138 case '4':
139 case '5':
140 case '6':
141 case '7':
142 case '8':
143 case '9':
144 Bias = 100.0 * (key - '0');
145 break;
146 case 's':
147 ScaleQuads = !ScaleQuads;
148 break;
149 case 27:
150 glutDestroyWindow(Win);
151 exit(0);
152 break;
153 }
154 glutPostRedisplay();
155 }
156
157
158 static void
159 Init(void)
160 {
161 GLfloat maxBias;
162
163 if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
164 printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
165 exit(1);
166 }
167
168 if (!glutExtensionSupported("GL_SGIS_generate_mipmap")) {
169 printf("Sorry, GL_SGIS_generate_mipmap not supported by this renderer.\n");
170 exit(1);
171 }
172
173 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
174
175 if (1) {
176 /* test auto mipmap generation */
177 GLint width, height, i;
178 GLenum format;
179 GLubyte *image = LoadRGBImage(TEXTURE_FILE, &width, &height, &format);
180 if (!image) {
181 printf("Error: could not load texture image %s\n", TEXTURE_FILE);
182 exit(1);
183 }
184 /* resize to TexWidth x TexHeight */
185 if (width != TexWidth || height != TexHeight) {
186 GLubyte *newImage = malloc(TexWidth * TexHeight * 4);
187 gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
188 TexWidth, TexHeight, GL_UNSIGNED_BYTE, newImage);
189 free(image);
190 image = newImage;
191 }
192 printf("Using GL_SGIS_generate_mipmap\n");
193 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
194 glTexImage2D(GL_TEXTURE_2D, 0, format, TexWidth, TexHeight, 0,
195 format, GL_UNSIGNED_BYTE, image);
196 free(image);
197
198 /* make sure mipmap was really generated correctly */
199 width = TexWidth;
200 height = TexHeight;
201 for (i = 0; i < 9; i++) {
202 GLint w, h;
203 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w);
204 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h);
205 printf("Level %d size: %d x %d\n", i, w, h);
206 assert(w == width);
207 assert(h == height);
208 width /= 2;
209 height /= 2;
210 }
211 }
212 else {
213 if (LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
214 printf("Using gluBuildMipmaps()\n");
215 }
216 else {
217 printf("Error: could not load texture image %s\n", TEXTURE_FILE);
218 exit(1);
219 }
220 }
221
222
223 /* mipmapping required for this extension */
224 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
225 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
226 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
227
228 glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT, &maxBias);
229
230 printf("GL_RENDERER: %s\n", (char*) glGetString(GL_RENDERER));
231 printf("LOD bias range: [%g, %g]\n", -maxBias, maxBias);
232
233 printf("Press 's' to toggle quad scaling\n");
234 }
235
236
237 int
238 main(int argc, char *argv[])
239 {
240 glutInit(&argc, argv);
241 glutInitWindowPosition(0, 0);
242 glutInitWindowSize(WinWidth, WinHeight);
243 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
244 Win = glutCreateWindow(argv[0]);
245 glutReshapeFunc(Reshape);
246 glutKeyboardFunc(Key);
247 glutDisplayFunc(Display);
248 Init();
249 glutMainLoop();
250 return 0;
251 }