1 /* Test GL_TEXTURE_BASE_LEVEL and GL_TEXTURE_MAX_LEVEL
7 /* Copyright (c) Mark J. Kilgard, 1994. */
10 * (c) Copyright 1993, Silicon Graphics, Inc.
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.
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.
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.
43 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
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
59 #define TEXTURE_FILE "../images/girl.rgb"
61 static GLint BaseLevel
= 0, MaxLevel
= 9;
62 static GLfloat MinLod
= -1, MaxLod
= 9;
63 static GLfloat LodBias
= 0.0;
64 static GLboolean NearestFilter
= GL_TRUE
;
65 static GLuint texImage
, texColor
, texCurrent
;
76 NearestFilter
= GL_TRUE
;
81 MakeImage(int level
, int width
, int height
, const GLubyte color
[4])
83 const int makeStripes
= 0;
84 GLubyte img
[512 * 512 * 3];
86 for (i
= 0; i
< height
; i
++) {
87 for (j
= 0; j
< width
; j
++) {
88 int k
= (i
* width
+ j
) * 3;
89 int p
= (i
/ 8) & makeStripes
;
91 img
[k
+ 0] = color
[0];
92 img
[k
+ 1] = color
[1];
93 img
[k
+ 2] = color
[2];
103 glTexImage2D(GL_TEXTURE_2D
, level
, GL_RGB
, width
, height
, 0,
104 GL_RGB
, GL_UNSIGNED_BYTE
, img
);
109 makeImages(int image
)
113 if (glutExtensionSupported("GL_SGIS_generate_mipmap") && image
) {
114 /* test auto mipmap generation */
115 GLint width
, height
, i
;
117 GLubyte
*image
= LoadRGBImage(TEXTURE_FILE
, &width
, &height
, &format
);
119 printf("Error: could not load texture image %s\n", TEXTURE_FILE
);
123 if (width
!= WIDTH
|| height
!= HEIGHT
) {
124 GLubyte
*newImage
= malloc(WIDTH
* HEIGHT
* 4);
125 gluScaleImage(format
, width
, height
, GL_UNSIGNED_BYTE
, image
,
126 WIDTH
, HEIGHT
, GL_UNSIGNED_BYTE
, newImage
);
130 printf("Using GL_SGIS_generate_mipmap\n");
131 glTexParameteri(GL_TEXTURE_2D
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
132 glTexImage2D(GL_TEXTURE_2D
, 0, format
, WIDTH
, HEIGHT
, 0,
133 format
, GL_UNSIGNED_BYTE
, image
);
134 glTexParameteri(GL_TEXTURE_2D
, GL_GENERATE_MIPMAP_SGIS
, GL_FALSE
);
137 /* make sure mipmap was really generated correctly */
140 for (i
= 0; i
< 10; i
++) {
142 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_WIDTH
, &w
);
143 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_HEIGHT
, &h
);
144 printf("Level %d size: %d x %d\n", i
, w
, h
);
150 static const GLubyte colors
[10][3] = {
164 for (i
= 0; i
< 10; i
++) {
165 MakeImage(i
, sz
, sz
, colors
[i
]);
166 printf("Level %d size: %d x %d\n", i
, sz
, sz
);
177 glEnable(GL_DEPTH_TEST
);
178 glDepthFunc(GL_LESS
);
179 glShadeModel(GL_FLAT
);
181 glTranslatef(0.0, 0.0, -3.6);
183 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
184 glGenTextures(1, &texImage
);
185 glBindTexture(GL_TEXTURE_2D
, texImage
);
187 glGenTextures(1, &texColor
);
188 glBindTexture(GL_TEXTURE_2D
, texColor
);
191 texCurrent
= texImage
;
193 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
194 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
195 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_DECAL
);
196 glEnable(GL_TEXTURE_2D
);
203 glBindTexture(GL_TEXTURE_2D
, texCurrent
);
206 ("BASE_LEVEL=%d MAX_LEVEL=%d MIN_LOD=%.2g MAX_LOD=%.2g Bias=%.2g Filter=%s\n",
207 BaseLevel
, MaxLevel
, MinLod
, MaxLod
, LodBias
,
208 NearestFilter
? "NEAREST" : "LINEAR");
209 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_BASE_LEVEL
, BaseLevel
);
210 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAX_LEVEL
, MaxLevel
);
212 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MIN_LOD
, MinLod
);
213 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MAX_LOD
, MaxLod
);
216 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
217 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
218 GL_NEAREST_MIPMAP_NEAREST
);
221 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
222 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
223 GL_LINEAR_MIPMAP_LINEAR
);
226 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
, GL_TEXTURE_LOD_BIAS_EXT
, LodBias
);
228 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
230 glTexCoord2f(0.0, 0.0);
231 glVertex3f(-2.0, -1.0, 0.0);
232 glTexCoord2f(0.0, tcm
);
233 glVertex3f(-2.0, 1.0, 0.0);
234 glTexCoord2f(tcm
* 3000.0, tcm
);
235 glVertex3f(3000.0, 1.0, -6000.0);
236 glTexCoord2f(tcm
* 3000.0, 0.0);
237 glVertex3f(3000.0, -1.0, -6000.0);
243 myReshape(int w
, int h
)
245 glViewport(0, 0, w
, h
);
246 glMatrixMode(GL_PROJECTION
);
248 gluPerspective(60.0, 1.0 * (GLfloat
) w
/ (GLfloat
) h
, 1.0, 30000.0);
249 glMatrixMode(GL_MODELVIEW
);
258 printf(" b/B decrease/increase GL_TEXTURE_BASE_LEVEL\n");
259 printf(" m/M decrease/increase GL_TEXTURE_MAX_LEVEL\n");
260 printf(" n/N decrease/increase GL_TEXTURE_MIN_LOD\n");
261 printf(" x/X decrease/increase GL_TEXTURE_MAX_LOD\n");
262 printf(" l/L decrease/increase GL_TEXTURE_LOD_BIAS\n");
263 printf(" f toggle nearest/linear filtering\n");
264 printf(" t toggle texture color/image\n");
265 printf(" SPACE reset values\n");
269 key(unsigned char k
, int x
, int y
)
313 NearestFilter
= !NearestFilter
;
316 if (texCurrent
== texColor
)
317 texCurrent
= texImage
;
319 texCurrent
= texColor
;
327 case 27: /* Escape */
338 main(int argc
, char **argv
)
340 glutInit(&argc
, argv
);
341 glutInitDisplayMode(GLUT_SINGLE
| GLUT_RGB
| GLUT_DEPTH
);
342 glutInitWindowSize(600, 600);
343 glutCreateWindow(argv
[0]);
346 glutReshapeFunc(myReshape
);
347 glutDisplayFunc(display
);
348 glutKeyboardFunc(key
);
351 return 0; /* ANSI C requires main to return int. */