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