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
;
80 static void MakeImage(int level
, int width
, int height
, const GLubyte color
[4])
82 const int makeStripes
= 0;
83 GLubyte img
[512*512*3];
85 for (i
= 0; i
< height
; i
++) {
86 for (j
= 0; j
< width
; j
++) {
87 int k
= (i
* width
+ j
) * 3;
88 int p
= (i
/8) & makeStripes
;
90 img
[k
+ 0] = color
[0];
91 img
[k
+ 1] = color
[1];
92 img
[k
+ 2] = color
[2];
102 glTexImage2D(GL_TEXTURE_2D
, level
, GL_RGB
, width
, height
, 0,
103 GL_RGB
, GL_UNSIGNED_BYTE
, img
);
107 static void makeImages(int image
)
111 if (glutExtensionSupported("GL_SGIS_generate_mipmap") && image
) {
112 /* test auto mipmap generation */
113 GLint width
, height
, i
;
115 GLubyte
*image
= LoadRGBImage(TEXTURE_FILE
, &width
, &height
, &format
);
117 printf("Error: could not load texture image %s\n", TEXTURE_FILE
);
121 if (width
!= WIDTH
|| height
!= HEIGHT
) {
122 GLubyte
*newImage
= malloc(WIDTH
* HEIGHT
* 4);
123 gluScaleImage(format
, width
, height
, GL_UNSIGNED_BYTE
, image
,
124 WIDTH
, HEIGHT
, GL_UNSIGNED_BYTE
, newImage
);
128 printf("Using GL_SGIS_generate_mipmap\n");
129 glTexParameteri(GL_TEXTURE_2D
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
130 glTexImage2D(GL_TEXTURE_2D
, 0, format
, WIDTH
, HEIGHT
, 0,
131 format
, GL_UNSIGNED_BYTE
, image
);
132 glTexParameteri(GL_TEXTURE_2D
, GL_GENERATE_MIPMAP_SGIS
, GL_FALSE
);
135 /* make sure mipmap was really generated correctly */
136 width
= WIDTH
; height
= HEIGHT
;
137 for (i
= 0; i
< 10; i
++) {
139 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_WIDTH
, &w
);
140 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_HEIGHT
, &h
);
141 printf("Level %d size: %d x %d\n", i
, w
, h
);
146 static const GLubyte colors
[10][3] = {
160 for (i
= 0; i
< 10; i
++) {
161 MakeImage(i
, sz
, sz
, colors
[i
]);
162 printf("Level %d size: %d x %d\n", i
, sz
, sz
);
168 static void myinit(void)
172 glEnable(GL_DEPTH_TEST
);
173 glDepthFunc(GL_LESS
);
174 glShadeModel(GL_FLAT
);
176 glTranslatef(0.0, 0.0, -3.6);
178 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
179 glGenTextures(1, &texImage
);
180 glBindTexture(GL_TEXTURE_2D
, texImage
);
182 glGenTextures(1, &texColor
);
183 glBindTexture(GL_TEXTURE_2D
, texColor
);
186 texCurrent
= texImage
;
188 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
189 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
190 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_DECAL
);
191 glEnable(GL_TEXTURE_2D
);
194 static void display(void)
197 glBindTexture(GL_TEXTURE_2D
, texCurrent
);
199 printf("BASE_LEVEL=%d MAX_LEVEL=%d MIN_LOD=%.2g MAX_LOD=%.2g Bias=%.2g Filter=%s\n",
200 BaseLevel
, MaxLevel
, MinLod
, MaxLod
, LodBias
,
201 NearestFilter
? "NEAREST" : "LINEAR");
202 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_BASE_LEVEL
, BaseLevel
);
203 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAX_LEVEL
, MaxLevel
);
205 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MIN_LOD
, MinLod
);
206 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MAX_LOD
, MaxLod
);
209 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
210 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
211 GL_NEAREST_MIPMAP_NEAREST
);
214 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
215 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
216 GL_LINEAR_MIPMAP_LINEAR
);
219 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
, GL_TEXTURE_LOD_BIAS_EXT
, LodBias
);
221 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
223 glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
224 glTexCoord2f(0.0, tcm
); glVertex3f(-2.0, 1.0, 0.0);
225 glTexCoord2f(tcm
* 3000.0, tcm
); glVertex3f(3000.0, 1.0, -6000.0);
226 glTexCoord2f(tcm
* 3000.0, 0.0); glVertex3f(3000.0, -1.0, -6000.0);
231 static void myReshape(int w
, int h
)
233 glViewport(0, 0, w
, h
);
234 glMatrixMode(GL_PROJECTION
);
236 gluPerspective(60.0, 1.0*(GLfloat
)w
/(GLfloat
)h
, 1.0, 30000.0);
237 glMatrixMode(GL_MODELVIEW
);
242 key(unsigned char k
, int x
, int y
)
286 NearestFilter
= !NearestFilter
;
289 if (texCurrent
== texColor
)
290 texCurrent
= texImage
;
292 texCurrent
= texColor
;
297 case 27: /* Escape */
307 static void usage(void)
310 printf(" b/B decrease/increase GL_TEXTURE_BASE_LEVEL\n");
311 printf(" m/M decrease/increase GL_TEXTURE_MAX_LEVEL\n");
312 printf(" n/N decrease/increase GL_TEXTURE_MIN_LOD\n");
313 printf(" x/X decrease/increase GL_TEXTURE_MAX_LOD\n");
314 printf(" l/L decrease/increase GL_TEXTURE_LOD_BIAS\n");
315 printf(" f toggle nearest/linear filtering\n");
316 printf(" t toggle texture color/image\n");
317 printf(" SPACE reset values\n");
321 int main(int argc
, char** argv
)
323 glutInit(&argc
, argv
);
324 glutInitDisplayMode (GLUT_SINGLE
| GLUT_RGB
| GLUT_DEPTH
);
325 glutInitWindowSize (600, 600);
326 glutCreateWindow (argv
[0]);
329 glutReshapeFunc (myReshape
);
330 glutDisplayFunc(display
);
331 glutKeyboardFunc(key
);
334 return 0; /* ANSI C requires main to return int. */