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