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