blob: a4db22e26e43484a27819659278fb495ef143273 [file] [log] [blame]
Brian Paulaa80e292000-03-20 22:00:22 +00001/*
2 * GL_EXT_texture_lod_bias demo
3 *
4 * Thanks to Michael Vance for implementing this extension in Mesa.
5 *
6 * Brian Paul
7 * 20 March 2000
8 *
9 * Copyright (C) 2000 Brian Paul All Rights Reserved.
10 *
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:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
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.
27 */
28
29
Brian Paul76d2a772002-12-10 16:51:28 +000030#include <assert.h>
Brian Paulaa80e292000-03-20 22:00:22 +000031#include <stdlib.h>
32#include <stdio.h>
33#include <math.h>
34#include <GL/glut.h>
35#include <GL/glext.h>
36
Brian Paul575d24a2005-01-09 17:15:41 +000037#include "readtex.h"
Brian Paulaa80e292000-03-20 22:00:22 +000038
39#define TEXTURE_FILE "../images/girl.rgb"
40
41static GLfloat Xrot = 0, Yrot = -30, Zrot = 0;
42static GLboolean Anim = GL_TRUE;
Brian Pauld21cdb62000-03-22 18:38:47 +000043static GLint Bias = 0, BiasStepSign = +1; /* ints avoid fp precision problem */
Brian Paul87c964d2001-11-06 15:53:00 +000044static GLint BiasMin = -400, BiasMax = 400;
Brian Paulaa80e292000-03-20 22:00:22 +000045
46
47
48static void
49PrintString(const char *s)
50{
51 while (*s) {
52 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
53 s++;
54 }
55}
56
57static void Idle( void )
58{
Brian Pauld21cdb62000-03-22 18:38:47 +000059 static int lastTime = 0;
60 int time = glutGet(GLUT_ELAPSED_TIME);
61 int step;
62
63 if (lastTime == 0)
64 lastTime = time;
65 else if (time - lastTime < 10)
66 return;
67
68 step = (time - lastTime) / 10 * BiasStepSign;
69 lastTime = time;
70
71 Bias += step;
Brian Paulaa80e292000-03-20 22:00:22 +000072 if (Bias < BiasMin) {
73 Bias = BiasMin;
Brian Pauld21cdb62000-03-22 18:38:47 +000074 BiasStepSign = +1;
Brian Paulaa80e292000-03-20 22:00:22 +000075 }
76 else if (Bias > BiasMax) {
77 Bias = BiasMax;
Brian Pauld21cdb62000-03-22 18:38:47 +000078 BiasStepSign = -1;
Brian Paulaa80e292000-03-20 22:00:22 +000079 }
80
81 glutPostRedisplay();
82}
83
84
85static void Display( void )
86{
87 char str[100];
88
89 glClear( GL_COLOR_BUFFER_BIT );
90
91 glMatrixMode( GL_PROJECTION );
92 glLoadIdentity();
93 glOrtho(-1, 1, -1, 1, -1, 1);
94 glMatrixMode( GL_MODELVIEW );
95 glLoadIdentity();
96
97 glDisable(GL_TEXTURE_2D);
98 glColor3f(1,1,1);
99 glRasterPos3f(-0.9, -0.9, 0.0);
100 sprintf(str, "Texture LOD Bias = %4.1f", Bias * 0.01);
101 PrintString(str);
102
103 glMatrixMode( GL_PROJECTION );
104 glLoadIdentity();
105 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
106 glMatrixMode( GL_MODELVIEW );
107 glLoadIdentity();
108 glTranslatef( 0.0, 0.0, -8.0 );
109
110 glPushMatrix();
111 glRotatef(Xrot, 1, 0, 0);
112 glRotatef(Yrot, 0, 1, 0);
113 glRotatef(Zrot, 0, 0, 1);
114
115 glEnable(GL_TEXTURE_2D);
116 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.01 * Bias);
117
118 glBegin(GL_POLYGON);
119 glTexCoord2f(0, 0); glVertex2f(-1, -1);
120 glTexCoord2f(2, 0); glVertex2f( 1, -1);
121 glTexCoord2f(2, 2); glVertex2f( 1, 1);
122 glTexCoord2f(0, 2); glVertex2f(-1, 1);
123 glEnd();
124
125 glPopMatrix();
126
127 glutSwapBuffers();
128}
129
130
131static void Reshape( int width, int height )
132{
133 glViewport( 0, 0, width, height );
134}
135
136
137static void Key( unsigned char key, int x, int y )
138{
139 const GLfloat step = 3.0;
140 (void) x;
141 (void) y;
142 switch (key) {
143 case 'a':
144 Anim = !Anim;
145 if (Anim)
146 glutIdleFunc(Idle);
147 else
148 glutIdleFunc(NULL);
149 break;
150 case 'z':
151 Zrot -= step;
152 break;
153 case 'Z':
154 Zrot += step;
155 break;
156 case 'b':
157 Bias -= 10;
158 break;
159 case 'B':
160 Bias += 10;
161 break;
162 case 27:
163 exit(0);
164 break;
165 }
166 glutPostRedisplay();
167}
168
169
170static void SpecialKey( int key, int x, int y )
171{
172 const GLfloat step = 3.0;
173 (void) x;
174 (void) y;
175 switch (key) {
176 case GLUT_KEY_UP:
177 Xrot -= step;
178 break;
179 case GLUT_KEY_DOWN:
180 Xrot += step;
181 break;
182 case GLUT_KEY_LEFT:
183 Yrot -= step;
184 break;
185 case GLUT_KEY_RIGHT:
186 Yrot += step;
187 break;
188 }
189 glutPostRedisplay();
190}
191
192
193static void Init( void )
194{
Brian Paul87c964d2001-11-06 15:53:00 +0000195 GLfloat maxBias;
196
Brian Paul76d2a772002-12-10 16:51:28 +0000197 if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
Brian Paulaa80e292000-03-20 22:00:22 +0000198 printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
199 exit(1);
200 }
201
202 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Brian Paul76d2a772002-12-10 16:51:28 +0000203
204 if (glutExtensionSupported("GL_SGIS_generate_mipmap")) {
205 /* test auto mipmap generation */
206 GLint width, height, i;
207 GLenum format;
208 GLubyte *image = LoadRGBImage(TEXTURE_FILE, &width, &height, &format);
209 if (!image) {
210 printf("Error: could not load texture image %s\n", TEXTURE_FILE);
211 exit(1);
212 }
213 /* resize to 256 x 256 */
214 if (width != 256 || height != 256) {
215 GLubyte *newImage = malloc(256 * 256 * 4);
216 gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
217 256, 256, GL_UNSIGNED_BYTE, newImage);
218 free(image);
219 image = newImage;
220 }
221 printf("Using GL_SGIS_generate_mipmap\n");
222 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
223 glTexImage2D(GL_TEXTURE_2D, 0, format, 256, 256, 0,
224 format, GL_UNSIGNED_BYTE, image);
225 free(image);
226
227 /* make sure mipmap was really generated correctly */
228 width = height = 256;
229 for (i = 0; i < 9; i++) {
230 GLint w, h;
231 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w);
232 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h);
233 printf("Level %d size: %d x %d\n", i, w, h);
234 assert(w == width);
235 assert(h == height);
236 width /= 2;
237 height /= 2;
238 }
239
240 }
241 else if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
Brian Paulaa80e292000-03-20 22:00:22 +0000242 printf("Error: could not load texture image %s\n", TEXTURE_FILE);
243 exit(1);
244 }
245
246 /* mipmapping required for this extension */
247 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
248 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
249 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
Brian Paul87c964d2001-11-06 15:53:00 +0000250
251 glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT, &maxBias);
252 printf("LOD bias range: [%g, %g]\n", -maxBias, maxBias);
253 BiasMin = -100 * maxBias;
254 BiasMax = 100 * maxBias;
Brian Paulf1324132004-03-26 18:05:36 +0000255
256 /* Since we have (about) 8 mipmap levels, no need to bias beyond
257 * the range [-1, +8].
258 */
259 if (BiasMin < -100)
260 BiasMin = -100;
261 if (BiasMax > 800)
262 BiasMax = 800;
Brian Paulaa80e292000-03-20 22:00:22 +0000263}
264
265
266int main( int argc, char *argv[] )
267{
268 glutInit( &argc, argv );
269 glutInitWindowPosition( 0, 0 );
270 glutInitWindowSize( 350, 350 );
271 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
272 glutCreateWindow(argv[0]);
273 glutReshapeFunc( Reshape );
274 glutKeyboardFunc( Key );
275 glutSpecialFunc( SpecialKey );
276 glutDisplayFunc( Display );
277 if (Anim)
278 glutIdleFunc(Idle);
279 Init();
280 glutMainLoop();
281 return 0;
282}