2 * GL_EXT_texture_lod_bias demo
4 * Thanks to Michael Vance for implementing this extension in Mesa.
9 * Copyright (C) 2000 Brian Paul All Rights Reserved.
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
25 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 #define TEXTURE_FILE "../images/girl.rgb"
41 static GLfloat Xrot
= 0, Yrot
= -30, Zrot
= 0;
42 static GLboolean Anim
= GL_TRUE
;
43 static GLint Bias
= 0, BiasStepSign
= +1; /* ints avoid fp precision problem */
44 static GLint BiasMin
= -400, BiasMax
= 400;
49 PrintString(const char *s
)
52 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
57 static void Idle( void )
59 static int lastTime
= 0;
60 int time
= glutGet(GLUT_ELAPSED_TIME
);
65 else if (time
- lastTime
< 10)
68 step
= (time
- lastTime
) / 10 * BiasStepSign
;
76 else if (Bias
> BiasMax
) {
85 static void Display( void )
89 glClear( GL_COLOR_BUFFER_BIT
);
91 glMatrixMode( GL_PROJECTION
);
93 glOrtho(-1, 1, -1, 1, -1, 1);
94 glMatrixMode( GL_MODELVIEW
);
97 glDisable(GL_TEXTURE_2D
);
99 glRasterPos3f(-0.9, -0.9, 0.0);
100 sprintf(str
, "Texture LOD Bias = %4.1f", Bias
* 0.01);
103 glMatrixMode( GL_PROJECTION
);
105 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
106 glMatrixMode( GL_MODELVIEW
);
108 glTranslatef( 0.0, 0.0, -8.0 );
111 glRotatef(Xrot
, 1, 0, 0);
112 glRotatef(Yrot
, 0, 1, 0);
113 glRotatef(Zrot
, 0, 0, 1);
115 glEnable(GL_TEXTURE_2D
);
116 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
, GL_TEXTURE_LOD_BIAS_EXT
, 0.01 * Bias
);
119 glTexCoord2f(0, 0); glVertex2f(-1, -1);
120 glTexCoord2f(2, 0); glVertex2f( 1, -1);
121 glTexCoord2f(2, 2); glVertex2f( 1, 1);
122 glTexCoord2f(0, 2); glVertex2f(-1, 1);
131 static void Reshape( int width
, int height
)
133 glViewport( 0, 0, width
, height
);
137 static void Key( unsigned char key
, int x
, int y
)
139 const GLfloat step
= 3.0;
172 Bias
= 100.0 * (key
- '0');
182 static void SpecialKey( int key
, int x
, int y
)
184 const GLfloat step
= 3.0;
205 static void Init( void )
209 if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
210 printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
214 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
216 if (glutExtensionSupported("GL_SGIS_generate_mipmap")) {
217 /* test auto mipmap generation */
218 GLint width
, height
, i
;
220 GLubyte
*image
= LoadRGBImage(TEXTURE_FILE
, &width
, &height
, &format
);
222 printf("Error: could not load texture image %s\n", TEXTURE_FILE
);
225 /* resize to 256 x 256 */
226 if (width
!= 256 || height
!= 256) {
227 GLubyte
*newImage
= malloc(256 * 256 * 4);
228 gluScaleImage(format
, width
, height
, GL_UNSIGNED_BYTE
, image
,
229 256, 256, GL_UNSIGNED_BYTE
, newImage
);
233 printf("Using GL_SGIS_generate_mipmap\n");
234 glTexParameteri(GL_TEXTURE_2D
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
235 glTexImage2D(GL_TEXTURE_2D
, 0, format
, 256, 256, 0,
236 format
, GL_UNSIGNED_BYTE
, image
);
239 /* make sure mipmap was really generated correctly */
240 width
= height
= 256;
241 for (i
= 0; i
< 9; i
++) {
243 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_WIDTH
, &w
);
244 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_HEIGHT
, &h
);
245 printf("Level %d size: %d x %d\n", i
, w
, h
);
253 else if (!LoadRGBMipmaps(TEXTURE_FILE
, GL_RGB
)) {
254 printf("Error: could not load texture image %s\n", TEXTURE_FILE
);
258 /* mipmapping required for this extension */
259 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
260 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
261 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
263 glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT
, &maxBias
);
264 printf("LOD bias range: [%g, %g]\n", -maxBias
, maxBias
);
265 BiasMin
= -100 * maxBias
;
266 BiasMax
= 100 * maxBias
;
268 /* Since we have (about) 8 mipmap levels, no need to bias beyond
269 * the range [-1, +8].
278 int main( int argc
, char *argv
[] )
280 glutInit( &argc
, argv
);
281 glutInitWindowPosition( 0, 0 );
282 glutInitWindowSize( 350, 350 );
283 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
284 glutCreateWindow(argv
[0]);
285 glutReshapeFunc( Reshape
);
286 glutKeyboardFunc( Key
);
287 glutSpecialFunc( SpecialKey
);
288 glutDisplayFunc( Display
);