Merge remote branch 'main/master' into radeon-rewrite
[mesa.git] / progs / tests / mipmap_limits.c
1 /* Test GL_TEXTURE_BASE_LEVEL and GL_TEXTURE_MAX_LEVEL
2 * Brian Paul
3 * 10 May 2006
4 */
5
6
7 /* Copyright (c) Mark J. Kilgard, 1994. */
8
9 /*
10 * (c) Copyright 1993, Silicon Graphics, Inc.
11 * ALL RIGHTS RESERVED
12 * Permission to use, copy, modify, and distribute this software for
13 * any purpose and without fee is hereby granted, provided that the above
14 * copyright notice appear in all copies and that both the copyright notice
15 * and this permission notice appear in supporting documentation, and that
16 * the name of Silicon Graphics, Inc. not be used in advertising
17 * or publicity pertaining to distribution of the software without specific,
18 * written prior permission.
19 *
20 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
21 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
22 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
23 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
24 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
25 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
26 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
27 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
28 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
29 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
30 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
31 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
32 *
33 * US Government Users Restricted Rights
34 * Use, duplication, or disclosure by the Government is subject to
35 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
36 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
37 * clause at DFARS 252.227-7013 and/or in similar or successor
38 * clauses in the FAR or the DOD or NASA FAR Supplement.
39 * Unpublished-- rights reserved under the copyright laws of the
40 * United States. Contractor/manufacturer is Silicon Graphics,
41 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
42 *
43 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
44 */
45 /* mipmap.c
46 * This program demonstrates using mipmaps for texture maps.
47 * To overtly show the effect of mipmaps, each mipmap reduction
48 * level has a solidly colored, contrasting texture image.
49 * Thus, the quadrilateral which is drawn is drawn with several
50 * different colors.
51 */
52 #include <stdlib.h>
53 #include <stdio.h>
54 #include <GL/glew.h>
55 #include <GL/glut.h>
56
57 #include "readtex.h"
58
59 #define TEXTURE_FILE "../images/girl.rgb"
60
61 static GLint BaseLevel = 0, MaxLevel = 9;
62 static GLfloat MinLod = -1, MaxLod = 9;
63 static GLfloat LodBias = 0.0;
64 static GLboolean NearestFilter = GL_TRUE;
65 static GLuint texImage, texColor, texCurrent;
66
67
68 static void
69 InitValues(void)
70 {
71 BaseLevel = 0;
72 MaxLevel = 9;
73 MinLod = -1;
74 MaxLod = 9;
75 LodBias = 0.0;
76 NearestFilter = GL_TRUE;
77 }
78
79
80 static void MakeImage(int level, int width, int height, const GLubyte color[4])
81 {
82 const int makeStripes = 0;
83 GLubyte img[512*512*3];
84 int i, j;
85 for (i = 0; i < height; i++) {
86 for (j = 0; j < width; j++) {
87 int k = (i * width + j) * 3;
88 int p = (i/8) & makeStripes;
89 if (p == 0) {
90 img[k + 0] = color[0];
91 img[k + 1] = color[1];
92 img[k + 2] = color[2];
93 }
94 else {
95 img[k + 0] = 0;
96 img[k + 1] = 0;
97 img[k + 2] = 0;
98 }
99 }
100 }
101
102 glTexImage2D(GL_TEXTURE_2D, level, GL_RGB, width, height, 0,
103 GL_RGB, GL_UNSIGNED_BYTE, img);
104 }
105
106
107 static void makeImages(int image)
108 {
109 #define WIDTH 512
110 #define HEIGHT 512
111 if (glutExtensionSupported("GL_SGIS_generate_mipmap") && image) {
112 /* test auto mipmap generation */
113 GLint width, height, i;
114 GLenum format;
115 GLubyte *image = LoadRGBImage(TEXTURE_FILE, &width, &height, &format);
116 if (!image) {
117 printf("Error: could not load texture image %s\n", TEXTURE_FILE);
118 exit(1);
119 }
120 /* resize */
121 if (width != WIDTH || height != HEIGHT) {
122 GLubyte *newImage = malloc(WIDTH * HEIGHT * 4);
123 gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
124 WIDTH, HEIGHT, GL_UNSIGNED_BYTE, newImage);
125 free(image);
126 image = newImage;
127 }
128 printf("Using GL_SGIS_generate_mipmap\n");
129 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
130 glTexImage2D(GL_TEXTURE_2D, 0, format, WIDTH, HEIGHT, 0,
131 format, GL_UNSIGNED_BYTE, image);
132 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_FALSE);
133 free(image);
134
135 /* make sure mipmap was really generated correctly */
136 width = WIDTH; height = HEIGHT;
137 for (i = 0; i < 10; i++) {
138 GLint w, h;
139 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w);
140 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h);
141 printf("Level %d size: %d x %d\n", i, w, h);
142 width /= 2;
143 height /= 2;
144 }
145 } else {
146 static const GLubyte colors[10][3] = {
147 {128, 128, 128 },
148 { 0, 255, 255 },
149 { 255, 255, 0 },
150 { 255, 0, 255 },
151 { 255, 0, 0 },
152 { 0, 255, 0 },
153 { 0, 0, 255 },
154 { 0, 255, 255 },
155 { 255, 255, 0 },
156 { 255, 255, 255 }
157 };
158 int i, sz = 512;
159
160 for (i = 0; i < 10; i++) {
161 MakeImage(i, sz, sz, colors[i]);
162 printf("Level %d size: %d x %d\n", i, sz, sz);
163 sz /= 2;
164 }
165 }
166 }
167
168 static void myinit(void)
169 {
170 InitValues();
171
172 glEnable(GL_DEPTH_TEST);
173 glDepthFunc(GL_LESS);
174 glShadeModel(GL_FLAT);
175
176 glTranslatef(0.0, 0.0, -3.6);
177
178 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
179 glGenTextures(1, &texImage);
180 glBindTexture(GL_TEXTURE_2D, texImage);
181 makeImages(1);
182 glGenTextures(1, &texColor);
183 glBindTexture(GL_TEXTURE_2D, texColor);
184 makeImages(0);
185
186 texCurrent = texImage;
187
188 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
189 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
190 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
191 glEnable(GL_TEXTURE_2D);
192 }
193
194 static void display(void)
195 {
196 GLfloat tcm = 1.0;
197 glBindTexture(GL_TEXTURE_2D, texCurrent);
198
199 printf("BASE_LEVEL=%d MAX_LEVEL=%d MIN_LOD=%.2g MAX_LOD=%.2g Bias=%.2g Filter=%s\n",
200 BaseLevel, MaxLevel, MinLod, MaxLod, LodBias,
201 NearestFilter ? "NEAREST" : "LINEAR");
202 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, BaseLevel);
203 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, MaxLevel);
204
205 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, MinLod);
206 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, MaxLod);
207
208 if (NearestFilter) {
209 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
210 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
211 GL_NEAREST_MIPMAP_NEAREST);
212 }
213 else {
214 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
215 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
216 GL_LINEAR_MIPMAP_LINEAR);
217 }
218
219 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, LodBias);
220
221 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
222 glBegin(GL_QUADS);
223 glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
224 glTexCoord2f(0.0, tcm); glVertex3f(-2.0, 1.0, 0.0);
225 glTexCoord2f(tcm * 3000.0, tcm); glVertex3f(3000.0, 1.0, -6000.0);
226 glTexCoord2f(tcm * 3000.0, 0.0); glVertex3f(3000.0, -1.0, -6000.0);
227 glEnd();
228 glFlush();
229 }
230
231 static void myReshape(int w, int h)
232 {
233 glViewport(0, 0, w, h);
234 glMatrixMode(GL_PROJECTION);
235 glLoadIdentity();
236 gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30000.0);
237 glMatrixMode(GL_MODELVIEW);
238 glLoadIdentity();
239 }
240
241 static void
242 key(unsigned char k, int x, int y)
243 {
244 (void) x;
245 (void) y;
246 switch (k) {
247 case 'b':
248 BaseLevel--;
249 if (BaseLevel < 0)
250 BaseLevel = 0;
251 break;
252 case 'B':
253 BaseLevel++;
254 if (BaseLevel > 10)
255 BaseLevel = 10;
256 break;
257 case 'm':
258 MaxLevel--;
259 if (MaxLevel < 0)
260 MaxLevel = 0;
261 break;
262 case 'M':
263 MaxLevel++;
264 if (MaxLevel > 10)
265 MaxLevel = 10;
266 break;
267 case 'l':
268 LodBias -= 0.25;
269 break;
270 case 'L':
271 LodBias += 0.25;
272 break;
273 case 'n':
274 MinLod -= 0.25;
275 break;
276 case 'N':
277 MinLod += 0.25;
278 break;
279 case 'x':
280 MaxLod -= 0.25;
281 break;
282 case 'X':
283 MaxLod += 0.25;
284 break;
285 case 'f':
286 NearestFilter = !NearestFilter;
287 break;
288 case 't':
289 if (texCurrent == texColor)
290 texCurrent = texImage;
291 else
292 texCurrent = texColor;
293 break;
294 case ' ':
295 InitValues();
296 break;
297 case 27: /* Escape */
298 exit(0);
299 break;
300 default:
301 return;
302 }
303 glutPostRedisplay();
304 }
305
306
307 static void usage(void)
308 {
309 printf("usage:\n");
310 printf(" b/B decrease/increase GL_TEXTURE_BASE_LEVEL\n");
311 printf(" m/M decrease/increase GL_TEXTURE_MAX_LEVEL\n");
312 printf(" n/N decrease/increase GL_TEXTURE_MIN_LOD\n");
313 printf(" x/X decrease/increase GL_TEXTURE_MAX_LOD\n");
314 printf(" l/L decrease/increase GL_TEXTURE_LOD_BIAS\n");
315 printf(" f toggle nearest/linear filtering\n");
316 printf(" t toggle texture color/image\n");
317 printf(" SPACE reset values\n");
318 }
319
320
321 int main(int argc, char** argv)
322 {
323 glutInit(&argc, argv);
324 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
325 glutInitWindowSize (600, 600);
326 glutCreateWindow (argv[0]);
327 glewInit();
328 myinit();
329 glutReshapeFunc (myReshape);
330 glutDisplayFunc(display);
331 glutKeyboardFunc(key);
332 usage();
333 glutMainLoop();
334 return 0; /* ANSI C requires main to return int. */
335 }