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