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