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
;
29 PrintString(const char *s
)
32 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
43 int texWidth
= TexWidth
, texHeight
= TexHeight
;
45 glClear(GL_COLOR_BUFFER_BIT
);
47 glMatrixMode(GL_PROJECTION
);
49 glOrtho(0, WinWidth
, 0, WinHeight
, -1, 1);
50 glMatrixMode(GL_MODELVIEW
);
58 for (bias
= -1; bias
< 11; bias
++) {
62 if (texWidth
== 1 && texHeight
== 1)
64 texWidth
= TexWidth
>> bias
;
65 texHeight
= TexHeight
>> bias
;
71 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
, GL_TEXTURE_LOD_BIAS_EXT
, 0.0);
74 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
, GL_TEXTURE_LOD_BIAS_EXT
, bias
);
77 glRasterPos2f(x
, y
+ TexHeight
+ 5);
79 sprintf(str
, "Texture Level %d: %d x %d",
80 (bias
< 0 ? 0 : bias
),
83 sprintf(str
, "Texture LOD Bias = %d", bias
);
87 glTranslatef(x
, y
, 0);
89 glEnable(GL_TEXTURE_2D
);
92 glTexCoord2f(0, 0); glVertex2f(0, 0);
93 glTexCoord2f(1, 0); glVertex2f(texWidth
, 0);
94 glTexCoord2f(1, 1); glVertex2f(texWidth
, texHeight
);
95 glTexCoord2f(0, 1); glVertex2f(0, texHeight
);
100 glDisable(GL_TEXTURE_2D
);
114 Reshape(int width
, int height
)
118 glViewport(0, 0, width
, height
);
123 Key(unsigned char key
, int x
, int y
)
144 Bias
= 100.0 * (key
- '0');
147 ScaleQuads
= !ScaleQuads
;
150 glutDestroyWindow(Win
);
163 if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
164 printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
168 if (!glutExtensionSupported("GL_SGIS_generate_mipmap")) {
169 printf("Sorry, GL_SGIS_generate_mipmap not supported by this renderer.\n");
173 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
176 /* test auto mipmap generation */
177 GLint width
, height
, i
;
179 GLubyte
*image
= LoadRGBImage(TEXTURE_FILE
, &width
, &height
, &format
);
181 printf("Error: could not load texture image %s\n", TEXTURE_FILE
);
184 /* resize to TexWidth x TexHeight */
185 if (width
!= TexWidth
|| height
!= TexHeight
) {
186 GLubyte
*newImage
= malloc(TexWidth
* TexHeight
* 4);
187 gluScaleImage(format
, width
, height
, GL_UNSIGNED_BYTE
, image
,
188 TexWidth
, TexHeight
, GL_UNSIGNED_BYTE
, newImage
);
192 printf("Using GL_SGIS_generate_mipmap\n");
193 glTexParameteri(GL_TEXTURE_2D
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
194 glTexImage2D(GL_TEXTURE_2D
, 0, format
, TexWidth
, TexHeight
, 0,
195 format
, GL_UNSIGNED_BYTE
, image
);
198 /* make sure mipmap was really generated correctly */
201 for (i
= 0; i
< 9; i
++) {
203 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_WIDTH
, &w
);
204 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_HEIGHT
, &h
);
205 printf("Level %d size: %d x %d\n", i
, w
, h
);
213 if (LoadRGBMipmaps(TEXTURE_FILE
, GL_RGB
)) {
214 printf("Using gluBuildMipmaps()\n");
217 printf("Error: could not load texture image %s\n", TEXTURE_FILE
);
223 /* mipmapping required for this extension */
224 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
225 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
226 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
228 glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT
, &maxBias
);
230 printf("GL_RENDERER: %s\n", (char*) glGetString(GL_RENDERER
));
231 printf("LOD bias range: [%g, %g]\n", -maxBias
, maxBias
);
233 printf("Press 's' to toggle quad scaling\n");
238 main(int argc
, char *argv
[])
240 glutInit(&argc
, argv
);
241 glutInitWindowPosition(0, 0);
242 glutInitWindowSize(WinWidth
, WinHeight
);
243 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
);
244 Win
= glutCreateWindow(argv
[0]);
245 glutReshapeFunc(Reshape
);
246 glutKeyboardFunc(Key
);
247 glutDisplayFunc(Display
);