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