2 * Test mipmap generation and lod bias.
18 #define TEXTURE_FILE "../images/arch.rgb"
20 static int TexWidth
= 256, TexHeight
= 256;
21 static int WinWidth
= 1044, WinHeight
= 900;
22 static GLfloat Bias
= 0.0;
23 static GLboolean ScaleQuads
= GL_FALSE
;
27 PrintString(const char *s
)
30 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
41 int texWidth
= TexWidth
, texHeight
= TexHeight
;
43 glClear(GL_COLOR_BUFFER_BIT
);
45 glMatrixMode(GL_PROJECTION
);
47 glOrtho(0, WinWidth
, 0, WinHeight
, -1, 1);
48 glMatrixMode(GL_MODELVIEW
);
56 for (bias
= -1; bias
< 11; bias
++) {
60 if (texWidth
== 1 && texHeight
== 1)
62 texWidth
= TexWidth
>> bias
;
63 texHeight
= TexHeight
>> bias
;
69 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
, GL_TEXTURE_LOD_BIAS_EXT
, 0.0);
72 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
, GL_TEXTURE_LOD_BIAS_EXT
, bias
);
75 glRasterPos2f(x
, y
+ TexHeight
+ 5);
77 sprintf(str
, "Texture Level %d: %d x %d",
78 (bias
< 0 ? 0 : bias
),
81 sprintf(str
, "Texture LOD Bias = %d", bias
);
85 glTranslatef(x
, y
, 0);
87 glEnable(GL_TEXTURE_2D
);
90 glTexCoord2f(0, 0); glVertex2f(0, 0);
91 glTexCoord2f(1, 0); glVertex2f(texWidth
, 0);
92 glTexCoord2f(1, 1); glVertex2f(texWidth
, texHeight
);
93 glTexCoord2f(0, 1); glVertex2f(0, texHeight
);
98 glDisable(GL_TEXTURE_2D
);
112 Reshape(int width
, int height
)
116 glViewport(0, 0, width
, height
);
121 Key(unsigned char key
, int x
, int y
)
142 Bias
= 100.0 * (key
- '0');
145 ScaleQuads
= !ScaleQuads
;
160 if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
161 printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
165 if (!glutExtensionSupported("GL_SGIS_generate_mipmap")) {
166 printf("Sorry, GL_SGIS_generate_mipmap not supported by this renderer.\n");
170 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
173 /* test auto mipmap generation */
174 GLint width
, height
, i
;
176 GLubyte
*image
= LoadRGBImage(TEXTURE_FILE
, &width
, &height
, &format
);
178 printf("Error: could not load texture image %s\n", TEXTURE_FILE
);
181 /* resize to TexWidth x TexHeight */
182 if (width
!= TexWidth
|| height
!= TexHeight
) {
183 GLubyte
*newImage
= malloc(TexWidth
* TexHeight
* 4);
184 gluScaleImage(format
, width
, height
, GL_UNSIGNED_BYTE
, image
,
185 TexWidth
, TexHeight
, GL_UNSIGNED_BYTE
, newImage
);
189 printf("Using GL_SGIS_generate_mipmap\n");
190 glTexParameteri(GL_TEXTURE_2D
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
191 glTexImage2D(GL_TEXTURE_2D
, 0, format
, TexWidth
, TexHeight
, 0,
192 format
, GL_UNSIGNED_BYTE
, image
);
195 /* make sure mipmap was really generated correctly */
198 for (i
= 0; i
< 9; i
++) {
200 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_WIDTH
, &w
);
201 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_HEIGHT
, &h
);
202 printf("Level %d size: %d x %d\n", i
, w
, h
);
210 if (LoadRGBMipmaps(TEXTURE_FILE
, GL_RGB
)) {
211 printf("Using gluBuildMipmaps()\n");
214 printf("Error: could not load texture image %s\n", TEXTURE_FILE
);
220 /* mipmapping required for this extension */
221 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
222 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
223 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
225 glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT
, &maxBias
);
227 printf("GL_RENDERER: %s\n", (char*) glGetString(GL_RENDERER
));
228 printf("LOD bias range: [%g, %g]\n", -maxBias
, maxBias
);
230 printf("Press 's' to toggle quad scaling\n");
235 main(int argc
, char *argv
[])
237 glutInit(&argc
, argv
);
238 glutInitWindowPosition(0, 0);
239 glutInitWindowSize(WinWidth
, WinHeight
);
240 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
);
241 glutCreateWindow(argv
[0]);
242 glutReshapeFunc(Reshape
);
243 glutKeyboardFunc(Key
);
244 glutDisplayFunc(Display
);