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