Merge commit 'origin/gallium-master-merge'
[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 static int win = 0;
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 glutDestroyWindow(win);
176 exit(0);
177 break;
178 }
179 glutPostRedisplay();
180 }
181
182
183 static void SpecialKey( int key, int x, int y )
184 {
185 const GLfloat step = 3.0;
186 (void) x;
187 (void) y;
188 switch (key) {
189 case GLUT_KEY_UP:
190 Xrot -= step;
191 break;
192 case GLUT_KEY_DOWN:
193 Xrot += step;
194 break;
195 case GLUT_KEY_LEFT:
196 Yrot -= step;
197 break;
198 case GLUT_KEY_RIGHT:
199 Yrot += step;
200 break;
201 }
202 glutPostRedisplay();
203 }
204
205
206 static void Init( void )
207 {
208 GLfloat maxBias;
209
210 if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
211 printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
212 exit(1);
213 }
214
215 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
216
217 if (glutExtensionSupported("GL_SGIS_generate_mipmap")) {
218 /* test auto mipmap generation */
219 GLint width, height, i;
220 GLenum format;
221 GLubyte *image = LoadRGBImage(TEXTURE_FILE, &width, &height, &format);
222 if (!image) {
223 printf("Error: could not load texture image %s\n", TEXTURE_FILE);
224 exit(1);
225 }
226 /* resize to 256 x 256 */
227 if (width != 256 || height != 256) {
228 GLubyte *newImage = malloc(256 * 256 * 4);
229 gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
230 256, 256, GL_UNSIGNED_BYTE, newImage);
231 free(image);
232 image = newImage;
233 }
234 printf("Using GL_SGIS_generate_mipmap\n");
235 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
236 glTexImage2D(GL_TEXTURE_2D, 0, format, 256, 256, 0,
237 format, GL_UNSIGNED_BYTE, image);
238 free(image);
239
240 /* make sure mipmap was really generated correctly */
241 width = height = 256;
242 for (i = 0; i < 9; i++) {
243 GLint w, h;
244 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w);
245 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h);
246 printf("Level %d size: %d x %d\n", i, w, h);
247 assert(w == width);
248 assert(h == height);
249 width /= 2;
250 height /= 2;
251 }
252
253 }
254 else if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
255 printf("Error: could not load texture image %s\n", TEXTURE_FILE);
256 exit(1);
257 }
258
259 /* mipmapping required for this extension */
260 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
261 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
262 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
263
264 glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT, &maxBias);
265 printf("LOD bias range: [%g, %g]\n", -maxBias, maxBias);
266 BiasMin = -100 * maxBias;
267 BiasMax = 100 * maxBias;
268
269 /* Since we have (about) 8 mipmap levels, no need to bias beyond
270 * the range [-1, +8].
271 */
272 if (BiasMin < -100)
273 BiasMin = -100;
274 if (BiasMax > 800)
275 BiasMax = 800;
276 }
277
278
279 int main( int argc, char *argv[] )
280 {
281 glutInit( &argc, argv );
282 glutInitWindowPosition( 0, 0 );
283 glutInitWindowSize( 350, 350 );
284 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
285 win = glutCreateWindow(argv[0]);
286 glutReshapeFunc( Reshape );
287 glutKeyboardFunc( Key );
288 glutSpecialFunc( SpecialKey );
289 glutDisplayFunc( Display );
290 if (Anim)
291 glutIdleFunc(Idle);
292 Init();
293 glutMainLoop();
294 return 0;
295 }