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
++) {
58 glRasterPos2f(x
, y
+ TexHeight
+ 5);
59 sprintf(str
, "Texture LOD Bias = %d", bias
);
63 glTranslatef(x
, y
, 0);
65 glEnable(GL_TEXTURE_2D
);
69 texWidth
= TexWidth
>> bias
;
70 texHeight
= TexHeight
>> bias
;
76 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
, GL_TEXTURE_LOD_BIAS_EXT
, 0.0);
79 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
, GL_TEXTURE_LOD_BIAS_EXT
, bias
);
83 glTexCoord2f(0, 0); glVertex2f(0, 0);
84 glTexCoord2f(1, 0); glVertex2f(texWidth
, 0);
85 glTexCoord2f(1, 1); glVertex2f(texWidth
, texHeight
);
86 glTexCoord2f(0, 1); glVertex2f(0, texHeight
);
91 glDisable(GL_TEXTURE_2D
);
105 Reshape(int width
, int height
)
109 glViewport(0, 0, width
, height
);
114 Key(unsigned char key
, int x
, int y
)
135 Bias
= 100.0 * (key
- '0');
138 ScaleQuads
= !ScaleQuads
;
153 if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
154 printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
158 if (!glutExtensionSupported("GL_SGIS_generate_mipmap")) {
159 printf("Sorry, GL_SGIS_generate_mipmap not supported by this renderer.\n");
163 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
166 /* test auto mipmap generation */
167 GLint width
, height
, i
;
169 GLubyte
*image
= LoadRGBImage(TEXTURE_FILE
, &width
, &height
, &format
);
171 printf("Error: could not load texture image %s\n", TEXTURE_FILE
);
174 /* resize to TexWidth x TexHeight */
175 if (width
!= TexWidth
|| height
!= TexHeight
) {
176 GLubyte
*newImage
= malloc(TexWidth
* TexHeight
* 4);
177 gluScaleImage(format
, width
, height
, GL_UNSIGNED_BYTE
, image
,
178 TexWidth
, TexHeight
, GL_UNSIGNED_BYTE
, newImage
);
182 printf("Using GL_SGIS_generate_mipmap\n");
183 glTexParameteri(GL_TEXTURE_2D
, GL_GENERATE_MIPMAP_SGIS
, GL_TRUE
);
184 glTexImage2D(GL_TEXTURE_2D
, 0, format
, TexWidth
, TexHeight
, 0,
185 format
, GL_UNSIGNED_BYTE
, image
);
188 /* make sure mipmap was really generated correctly */
191 for (i
= 0; i
< 9; i
++) {
193 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_WIDTH
, &w
);
194 glGetTexLevelParameteriv(GL_TEXTURE_2D
, i
, GL_TEXTURE_HEIGHT
, &h
);
195 printf("Level %d size: %d x %d\n", i
, w
, h
);
203 if (LoadRGBMipmaps(TEXTURE_FILE
, GL_RGB
)) {
204 printf("Using gluBuildMipmaps()\n");
207 printf("Error: could not load texture image %s\n", TEXTURE_FILE
);
213 /* mipmapping required for this extension */
214 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
215 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
216 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
218 glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT
, &maxBias
);
220 printf("GL_RENDERER: %s\n", (char*) glGetString(GL_RENDERER
));
221 printf("LOD bias range: [%g, %g]\n", -maxBias
, maxBias
);
223 printf("Press 's' to toggle quad scaling\n");
228 main(int argc
, char *argv
[])
230 glutInit(&argc
, argv
);
231 glutInitWindowPosition(0, 0);
232 glutInitWindowSize(WinWidth
, WinHeight
);
233 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
);
234 glutCreateWindow(argv
[0]);
235 glutReshapeFunc(Reshape
);
236 glutKeyboardFunc(Key
);
237 glutDisplayFunc(Display
);