progs/tests: compile with SCons and glew
[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 static GLint BaseLevel = 0, MaxLevel = 8;
58 static GLfloat MinLod = -1, MaxLod = 9;
59 static GLfloat LodBias = 0.0;
60 static GLboolean NearestFilter = GL_TRUE;
61
62
63 static void
64 InitValues(void)
65 {
66 BaseLevel = 0;
67 MaxLevel = 8;
68 MinLod = -1;
69 MaxLod = 9;
70 LodBias = 0.0;
71 NearestFilter = GL_TRUE;
72 }
73
74
75 static void MakeImage(int level, int width, int height, const GLubyte color[4])
76 {
77 const int makeStripes = 0;
78 GLubyte img[256*256*3];
79 int i, j;
80 for (i = 0; i < height; i++) {
81 for (j = 0; j < width; j++) {
82 int k = (i * width + j) * 3;
83 int p = (i/8) & makeStripes;
84 if (p == 0) {
85 img[k + 0] = color[0];
86 img[k + 1] = color[1];
87 img[k + 2] = color[2];
88 }
89 else {
90 img[k + 0] = 0;
91 img[k + 1] = 0;
92 img[k + 2] = 0;
93 }
94 }
95 }
96
97 glTexImage2D(GL_TEXTURE_2D, level, GL_RGB, width, height, 0,
98 GL_RGB, GL_UNSIGNED_BYTE, img);
99 }
100
101
102 static void makeImages(void)
103 {
104 static const GLubyte colors[8][3] = {
105 {128, 128, 128 },
106 { 0, 255, 255 },
107 { 255, 255, 0 },
108 { 255, 0, 255 },
109 { 255, 0, 0 },
110 { 0, 255, 0 },
111 { 0, 0, 255 },
112 { 255, 255, 255 }
113 };
114 int i, sz = 128;
115
116 for (i = 0; i < 8; i++) {
117 MakeImage(i, sz, sz, colors[i]);
118 sz /= 2;
119 }
120 }
121
122 static void myinit(void)
123 {
124 InitValues();
125
126 glEnable(GL_DEPTH_TEST);
127 glDepthFunc(GL_LESS);
128 glShadeModel(GL_FLAT);
129
130 glTranslatef(0.0, 0.0, -3.6);
131
132 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
133 makeImages();
134 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
135 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
136 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
137 glEnable(GL_TEXTURE_2D);
138 }
139
140 static void display(void)
141 {
142 GLfloat tcm = 4.0;
143 printf("BASE_LEVEL=%d MAX_LEVEL=%d MIN_LOD=%.2g MAX_LOD=%.2g Bias=%.2g Filter=%s\n",
144 BaseLevel, MaxLevel, MinLod, MaxLod, LodBias,
145 NearestFilter ? "NEAREST" : "LINEAR");
146 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, BaseLevel);
147 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, MaxLevel);
148
149 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, MinLod);
150 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, MaxLod);
151
152 if (NearestFilter) {
153 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
154 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
155 GL_NEAREST_MIPMAP_NEAREST);
156 }
157 else {
158 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
159 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
160 GL_LINEAR_MIPMAP_LINEAR);
161 }
162
163 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, LodBias);
164
165 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
166 glBegin(GL_QUADS);
167 glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
168 glTexCoord2f(0.0, tcm); glVertex3f(-2.0, 1.0, 0.0);
169 glTexCoord2f(tcm, tcm); glVertex3f(3000.0, 1.0, -6000.0);
170 glTexCoord2f(tcm, 0.0); glVertex3f(3000.0, -1.0, -6000.0);
171 glEnd();
172 glFlush();
173 }
174
175 static void myReshape(int w, int h)
176 {
177 glViewport(0, 0, w, h);
178 glMatrixMode(GL_PROJECTION);
179 glLoadIdentity();
180 gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30000.0);
181 glMatrixMode(GL_MODELVIEW);
182 glLoadIdentity();
183 }
184
185 static void
186 key(unsigned char k, int x, int y)
187 {
188 (void) x;
189 (void) y;
190 switch (k) {
191 case 'b':
192 BaseLevel--;
193 if (BaseLevel < 0)
194 BaseLevel = 0;
195 break;
196 case 'B':
197 BaseLevel++;
198 if (BaseLevel > 10)
199 BaseLevel = 10;
200 break;
201 case 'm':
202 MaxLevel--;
203 if (MaxLevel < 0)
204 MaxLevel = 0;
205 break;
206 case 'M':
207 MaxLevel++;
208 if (MaxLevel > 10)
209 MaxLevel = 10;
210 break;
211 case 'l':
212 LodBias -= 0.25;
213 break;
214 case 'L':
215 LodBias += 0.25;
216 break;
217 case 'n':
218 MinLod -= 0.25;
219 break;
220 case 'N':
221 MinLod += 0.25;
222 break;
223 case 'x':
224 MaxLod -= 0.25;
225 break;
226 case 'X':
227 MaxLod += 0.25;
228 break;
229 case 'f':
230 NearestFilter = !NearestFilter;
231 break;
232 case ' ':
233 InitValues();
234 break;
235 case 27: /* Escape */
236 exit(0);
237 break;
238 default:
239 return;
240 }
241 glutPostRedisplay();
242 }
243
244
245 static void usage(void)
246 {
247 printf("usage:\n");
248 printf(" b/B decrease/increase GL_TEXTURE_BASE_LEVEL\n");
249 printf(" m/M decrease/increase GL_TEXTURE_MAX_LEVEL\n");
250 printf(" n/N decrease/increase GL_TEXTURE_MIN_LOD\n");
251 printf(" x/X decrease/increase GL_TEXTURE_MAX_LOD\n");
252 printf(" l/L decrease/increase GL_TEXTURE_LOD_BIAS\n");
253 printf(" f toggle nearest/linear filtering\n");
254 printf(" SPACE reset values\n");
255 }
256
257
258 int main(int argc, char** argv)
259 {
260 glutInit(&argc, argv);
261 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
262 glutInitWindowSize (600, 600);
263 glutCreateWindow (argv[0]);
264 glewInit();
265 myinit();
266 glutReshapeFunc (myReshape);
267 glutDisplayFunc(display);
268 glutKeyboardFunc(key);
269 usage();
270 glutMainLoop();
271 return 0; /* ANSI C requires main to return int. */
272 }