Add new demo "fbo_firecube".
[mesa.git] / progs / demos / lodbias.c
1 /*
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
30 #include <assert.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <math.h>
34 #include <GL/glut.h>
35 #include <GL/glext.h>
36
37 #include "readtex.h"
38
39 #define TEXTURE_FILE "../images/girl.rgb"
40
41 static GLfloat Xrot = 0, Yrot = -30, Zrot = 0;
42 static GLboolean Anim = GL_TRUE;
43 static GLint Bias = 0, BiasStepSign = +1; /* ints avoid fp precision problem */
44 static GLint BiasMin = -400, BiasMax = 400;
45
46
47
48 static void
49 PrintString(const char *s)
50 {
51 while (*s) {
52 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
53 s++;
54 }
55 }
56
57 static void Idle( void )
58 {
59 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;
72 if (Bias < BiasMin) {
73 Bias = BiasMin;
74 BiasStepSign = +1;
75 }
76 else if (Bias > BiasMax) {
77 Bias = BiasMax;
78 BiasStepSign = -1;
79 }
80
81 glutPostRedisplay();
82 }
83
84
85 static 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
131 static void Reshape( int width, int height )
132 {
133 glViewport( 0, 0, width, height );
134 }
135
136
137 static 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 '0':
163 case '1':
164 case '2':
165 case '3':
166 case '4':
167 case '5':
168 case '6':
169 case '7':
170 case '8':
171 case '9':
172 Bias = 100.0 * (key - '0');
173 break;
174 case 27:
175 exit(0);
176 break;
177 }
178 glutPostRedisplay();
179 }
180
181
182 static void SpecialKey( int key, int x, int y )
183 {
184 const GLfloat step = 3.0;
185 (void) x;
186 (void) y;
187 switch (key) {
188 case GLUT_KEY_UP:
189 Xrot -= step;
190 break;
191 case GLUT_KEY_DOWN:
192 Xrot += step;
193 break;
194 case GLUT_KEY_LEFT:
195 Yrot -= step;
196 break;
197 case GLUT_KEY_RIGHT:
198 Yrot += step;
199 break;
200 }
201 glutPostRedisplay();
202 }
203
204
205 static void Init( void )
206 {
207 GLfloat maxBias;
208
209 if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
210 printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
211 exit(1);
212 }
213
214 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
215
216 if (glutExtensionSupported("GL_SGIS_generate_mipmap")) {
217 /* test auto mipmap generation */
218 GLint width, height, i;
219 GLenum format;
220 GLubyte *image = LoadRGBImage(TEXTURE_FILE, &width, &height, &format);
221 if (!image) {
222 printf("Error: could not load texture image %s\n", TEXTURE_FILE);
223 exit(1);
224 }
225 /* resize to 256 x 256 */
226 if (width != 256 || height != 256) {
227 GLubyte *newImage = malloc(256 * 256 * 4);
228 gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
229 256, 256, GL_UNSIGNED_BYTE, newImage);
230 free(image);
231 image = newImage;
232 }
233 printf("Using GL_SGIS_generate_mipmap\n");
234 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
235 glTexImage2D(GL_TEXTURE_2D, 0, format, 256, 256, 0,
236 format, GL_UNSIGNED_BYTE, image);
237 free(image);
238
239 /* make sure mipmap was really generated correctly */
240 width = height = 256;
241 for (i = 0; i < 9; i++) {
242 GLint w, h;
243 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w);
244 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h);
245 printf("Level %d size: %d x %d\n", i, w, h);
246 assert(w == width);
247 assert(h == height);
248 width /= 2;
249 height /= 2;
250 }
251
252 }
253 else if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
254 printf("Error: could not load texture image %s\n", TEXTURE_FILE);
255 exit(1);
256 }
257
258 /* mipmapping required for this extension */
259 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
260 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
261 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
262
263 glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT, &maxBias);
264 printf("LOD bias range: [%g, %g]\n", -maxBias, maxBias);
265 BiasMin = -100 * maxBias;
266 BiasMax = 100 * maxBias;
267
268 /* Since we have (about) 8 mipmap levels, no need to bias beyond
269 * the range [-1, +8].
270 */
271 if (BiasMin < -100)
272 BiasMin = -100;
273 if (BiasMax > 800)
274 BiasMax = 800;
275 }
276
277
278 int main( int argc, char *argv[] )
279 {
280 glutInit( &argc, argv );
281 glutInitWindowPosition( 0, 0 );
282 glutInitWindowSize( 350, 350 );
283 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
284 glutCreateWindow(argv[0]);
285 glutReshapeFunc( Reshape );
286 glutKeyboardFunc( Key );
287 glutSpecialFunc( SpecialKey );
288 glutDisplayFunc( Display );
289 if (Anim)
290 glutIdleFunc(Idle);
291 Init();
292 glutMainLoop();
293 return 0;
294 }