2 * Test mipmap generation and lod bias.
19 #define TEXTURE_FILE "../images/arch.rgb"
21 static int TexWidth
= 256, TexHeight
= 256;
22 static int WinWidth
= 1044, WinHeight
= 900;
23 static GLfloat Bias
= 0.0;
24 static GLboolean ScaleQuads
= GL_FALSE
;
25 static GLboolean Linear
= GL_FALSE
;
31 PrintString(const char *s
)
34 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
45 int texWidth
= TexWidth
, texHeight
= TexHeight
;
47 glClear(GL_COLOR_BUFFER_BIT
);
49 glMatrixMode(GL_PROJECTION
);
51 glOrtho(0, WinWidth
, 0, WinHeight
, -1, 1);
52 glMatrixMode(GL_MODELVIEW
);
58 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
59 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
62 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST_MIPMAP_NEAREST
);
63 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
69 for (bias
= -1; bias
< 11; bias
++) {
73 if (texWidth
== 1 && texHeight
== 1)
75 texWidth
= TexWidth
>> bias
;
76 texHeight
= TexHeight
>> bias
;
82 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
, GL_TEXTURE_LOD_BIAS_EXT
, 0.0);
85 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
, GL_TEXTURE_LOD_BIAS_EXT
, bias
);
88 glRasterPos2f(x
, y
+ TexHeight
+ 5);
90 sprintf(str
, "Texture Level %d: %d x %d",
91 (bias
< 0 ? 0 : bias
),
94 sprintf(str
, "Texture LOD Bias = %d", bias
);
98 glTranslatef(x
, y
, 0);
100 glEnable(GL_TEXTURE_2D
);
103 glTexCoord2f(0, 0); glVertex2f(0, 0);
104 glTexCoord2f(1, 0); glVertex2f(texWidth
, 0);
105 glTexCoord2f(1, 1); glVertex2f(texWidth
, texHeight
);
106 glTexCoord2f(0, 1); glVertex2f(0, texHeight
);
111 glDisable(GL_TEXTURE_2D
);
125 Reshape(int width
, int height
)
129 glViewport(0, 0, width
, height
);
134 Key(unsigned char key
, int x
, int y
)
158 Bias
= 100.0 * (key
- '0');
161 ScaleQuads
= !ScaleQuads
;
164 glutDestroyWindow(Win
);
177 if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
178 printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
182 if (!glutExtensionSupported("GL_SGIS_generate_mipmap")) {
183 printf("Sorry, GL_SGIS_generate_mipmap not supported by this renderer.\n");
187 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
190 /* test auto mipmap generation */
191 GLint width
, height
, i
;
193 GLubyte
*image
= LoadRGBImage(TEXTURE_FILE
, &width
, &height
, &format
);
195 printf("Error: could not load texture image %s\n", TEXTURE_FILE
);
198 /* resize to TexWidth x TexHeight */
199 if (width
!= TexWidth
|| height
!= TexHeight
) {
200 GLubyte
*newImage
= malloc(TexWidth
* TexHeight
* 4);
201 gluScaleImage(format
, width
, height
, GL_UNSIGNED_BYTE
, image
,
202 TexWidth
, TexHeight
, GL_UNSIGNED_BYTE
, newImage
);
206 printf("Using GL_SGIS_generate_mipmap\n");
207 glTexParameteri(GL_TEXTURE_2D
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
208 glTexImage2D(GL_TEXTURE_2D
, 0, format
, TexWidth
, TexHeight
, 0,
209 format
, GL_UNSIGNED_BYTE
, image
);
212 /* make sure mipmap was really generated correctly */
215 for (i
= 0; i
< 9; i
++) {
217 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_WIDTH
, &w
);
218 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_HEIGHT
, &h
);
219 printf("Level %d size: %d x %d\n", i
, w
, h
);
227 if (LoadRGBMipmaps(TEXTURE_FILE
, GL_RGB
)) {
228 printf("Using gluBuildMipmaps()\n");
231 printf("Error: could not load texture image %s\n", TEXTURE_FILE
);
237 /* mipmapping required for this extension */
238 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
240 glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT
, &maxBias
);
242 printf("GL_RENDERER: %s\n", (char*) glGetString(GL_RENDERER
));
243 printf("LOD bias range: [%g, %g]\n", -maxBias
, maxBias
);
245 printf("Press 's' to toggle quad scaling\n");
250 main(int argc
, char *argv
[])
252 glutInit(&argc
, argv
);
253 glutInitWindowPosition(0, 0);
254 glutInitWindowSize(WinWidth
, WinHeight
);
255 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
);
256 Win
= glutCreateWindow(argv
[0]);
258 glutReshapeFunc(Reshape
);
259 glutKeyboardFunc(Key
);
260 glutDisplayFunc(Display
);