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;
46 static GLuint TexObj
= 0;
50 PrintString(const char *s
)
53 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
58 static void Idle( void )
60 static int lastTime
= 0;
61 int time
= glutGet(GLUT_ELAPSED_TIME
);
66 else if (time
- lastTime
< 10)
69 step
= (time
- lastTime
) / 10 * BiasStepSign
;
77 else if (Bias
> BiasMax
) {
86 static void Display( void )
90 glClear( GL_COLOR_BUFFER_BIT
);
92 glMatrixMode( GL_PROJECTION
);
94 glOrtho(-1, 1, -1, 1, -1, 1);
95 glMatrixMode( GL_MODELVIEW
);
98 glDisable(GL_TEXTURE_2D
);
100 glRasterPos3f(-0.9, -0.9, 0.0);
101 sprintf(str
, "Texture LOD Bias = %4.1f", Bias
* 0.01);
104 glMatrixMode( GL_PROJECTION
);
106 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
107 glMatrixMode( GL_MODELVIEW
);
109 glTranslatef( 0.0, 0.0, -8.0 );
112 glRotatef(Xrot
, 1, 0, 0);
113 glRotatef(Yrot
, 0, 1, 0);
114 glRotatef(Zrot
, 0, 0, 1);
116 glEnable(GL_TEXTURE_2D
);
117 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
, GL_TEXTURE_LOD_BIAS_EXT
, 0.01 * Bias
);
120 glTexCoord2f(0, 0); glVertex2f(-1, -1);
121 glTexCoord2f(2, 0); glVertex2f( 1, -1);
122 glTexCoord2f(2, 2); glVertex2f( 1, 1);
123 glTexCoord2f(0, 2); glVertex2f(-1, 1);
132 static void Reshape( int width
, int height
)
134 glViewport( 0, 0, width
, height
);
138 static void Key( unsigned char key
, int x
, int y
)
140 const GLfloat step
= 3.0;
173 Bias
= 100.0 * (key
- '0');
176 glutDestroyWindow(win
);
184 static void SpecialKey( int key
, int x
, int y
)
186 const GLfloat step
= 3.0;
207 static void Init( void )
211 if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
212 printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
216 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
218 glGenTextures(1, &TexObj
);
219 glBindTexture(GL_TEXTURE_2D
, TexObj
);
221 if (glutExtensionSupported("GL_SGIS_generate_mipmap")) {
222 /* test auto mipmap generation */
223 GLint width
, height
, i
;
225 GLubyte
*image
= LoadRGBImage(TEXTURE_FILE
, &width
, &height
, &format
);
227 printf("Error: could not load texture image %s\n", TEXTURE_FILE
);
230 /* resize to 256 x 256 */
231 if (width
!= 256 || height
!= 256) {
232 GLubyte
*newImage
= malloc(256 * 256 * 4);
233 gluScaleImage(format
, width
, height
, GL_UNSIGNED_BYTE
, image
,
234 256, 256, GL_UNSIGNED_BYTE
, newImage
);
238 printf("Using GL_SGIS_generate_mipmap\n");
239 glTexParameteri(GL_TEXTURE_2D
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
240 glTexImage2D(GL_TEXTURE_2D
, 0, format
, 256, 256, 0,
241 format
, GL_UNSIGNED_BYTE
, image
);
244 /* make sure mipmap was really generated correctly */
245 width
= height
= 256;
246 for (i
= 0; i
< 9; i
++) {
248 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_WIDTH
, &w
);
249 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_HEIGHT
, &h
);
250 printf("Level %d size: %d x %d\n", i
, w
, h
);
258 else if (!LoadRGBMipmaps(TEXTURE_FILE
, GL_RGB
)) {
259 printf("Error: could not load texture image %s\n", TEXTURE_FILE
);
263 /* mipmapping required for this extension */
264 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
265 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
266 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
268 glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT
, &maxBias
);
269 printf("LOD bias range: [%g, %g]\n", -maxBias
, maxBias
);
270 BiasMin
= -100 * maxBias
;
271 BiasMax
= 100 * maxBias
;
273 /* Since we have (about) 8 mipmap levels, no need to bias beyond
274 * the range [-1, +8].
283 int main( int argc
, char *argv
[] )
285 glutInitWindowSize( 350, 350 );
286 glutInit( &argc
, argv
);
287 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
288 win
= glutCreateWindow(argv
[0]);
289 glutReshapeFunc( Reshape
);
290 glutKeyboardFunc( Key
);
291 glutSpecialFunc( SpecialKey
);
292 glutDisplayFunc( Display
);