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');
175 glutDestroyWindow(win
);
183 static void SpecialKey( int key
, int x
, int y
)
185 const GLfloat step
= 3.0;
206 static void Init( void )
210 if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
211 printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
215 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
217 if (glutExtensionSupported("GL_SGIS_generate_mipmap")) {
218 /* test auto mipmap generation */
219 GLint width
, height
, i
;
221 GLubyte
*image
= LoadRGBImage(TEXTURE_FILE
, &width
, &height
, &format
);
223 printf("Error: could not load texture image %s\n", TEXTURE_FILE
);
226 /* resize to 256 x 256 */
227 if (width
!= 256 || height
!= 256) {
228 GLubyte
*newImage
= malloc(256 * 256 * 4);
229 gluScaleImage(format
, width
, height
, GL_UNSIGNED_BYTE
, image
,
230 256, 256, GL_UNSIGNED_BYTE
, newImage
);
234 printf("Using GL_SGIS_generate_mipmap\n");
235 glTexParameteri(GL_TEXTURE_2D
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
236 glTexImage2D(GL_TEXTURE_2D
, 0, format
, 256, 256, 0,
237 format
, GL_UNSIGNED_BYTE
, image
);
240 /* make sure mipmap was really generated correctly */
241 width
= height
= 256;
242 for (i
= 0; i
< 9; i
++) {
244 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_WIDTH
, &w
);
245 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_HEIGHT
, &h
);
246 printf("Level %d size: %d x %d\n", i
, w
, h
);
254 else if (!LoadRGBMipmaps(TEXTURE_FILE
, GL_RGB
)) {
255 printf("Error: could not load texture image %s\n", TEXTURE_FILE
);
259 /* mipmapping required for this extension */
260 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
261 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
262 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
264 glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT
, &maxBias
);
265 printf("LOD bias range: [%g, %g]\n", -maxBias
, maxBias
);
266 BiasMin
= -100 * maxBias
;
267 BiasMax
= 100 * maxBias
;
269 /* Since we have (about) 8 mipmap levels, no need to bias beyond
270 * the range [-1, +8].
279 int main( int argc
, char *argv
[] )
281 glutInit( &argc
, argv
);
282 glutInitWindowPosition( 0, 0 );
283 glutInitWindowSize( 350, 350 );
284 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
285 win
= glutCreateWindow(argv
[0]);
286 glutReshapeFunc( Reshape
);
287 glutKeyboardFunc( Key
);
288 glutSpecialFunc( SpecialKey
);
289 glutDisplayFunc( Display
);