mesa: rework null texel fetch/store funcs
[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 static GLuint TexObj = 0;
47
48
49 static void
50 PrintString(const char *s)
51 {
52 while (*s) {
53 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
54 s++;
55 }
56 }
57
58 static void Idle( void )
59 {
60 static int lastTime = 0;
61 int time = glutGet(GLUT_ELAPSED_TIME);
62 int step;
63
64 if (lastTime == 0)
65 lastTime = time;
66 else if (time - lastTime < 10)
67 return;
68
69 step = (time - lastTime) / 10 * BiasStepSign;
70 lastTime = time;
71
72 Bias += step;
73 if (Bias < BiasMin) {
74 Bias = BiasMin;
75 BiasStepSign = +1;
76 }
77 else if (Bias > BiasMax) {
78 Bias = BiasMax;
79 BiasStepSign = -1;
80 }
81
82 glutPostRedisplay();
83 }
84
85
86 static void Display( void )
87 {
88 char str[100];
89
90 glClear( GL_COLOR_BUFFER_BIT );
91
92 glMatrixMode( GL_PROJECTION );
93 glLoadIdentity();
94 glOrtho(-1, 1, -1, 1, -1, 1);
95 glMatrixMode( GL_MODELVIEW );
96 glLoadIdentity();
97
98 glDisable(GL_TEXTURE_2D);
99 glColor3f(1,1,1);
100 glRasterPos3f(-0.9, -0.9, 0.0);
101 sprintf(str, "Texture LOD Bias = %4.1f", Bias * 0.01);
102 PrintString(str);
103
104 glMatrixMode( GL_PROJECTION );
105 glLoadIdentity();
106 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
107 glMatrixMode( GL_MODELVIEW );
108 glLoadIdentity();
109 glTranslatef( 0.0, 0.0, -8.0 );
110
111 glPushMatrix();
112 glRotatef(Xrot, 1, 0, 0);
113 glRotatef(Yrot, 0, 1, 0);
114 glRotatef(Zrot, 0, 0, 1);
115
116 glEnable(GL_TEXTURE_2D);
117 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.01 * Bias);
118
119 glBegin(GL_POLYGON);
120 glTexCoord2f(0, 0); glVertex2f(-1, -1);
121 glTexCoord2f(2, 0); glVertex2f( 1, -1);
122 glTexCoord2f(2, 2); glVertex2f( 1, 1);
123 glTexCoord2f(0, 2); glVertex2f(-1, 1);
124 glEnd();
125
126 glPopMatrix();
127
128 glutSwapBuffers();
129 }
130
131
132 static void Reshape( int width, int height )
133 {
134 glViewport( 0, 0, width, height );
135 }
136
137
138 static void Key( unsigned char key, int x, int y )
139 {
140 const GLfloat step = 3.0;
141 (void) x;
142 (void) y;
143 switch (key) {
144 case 'a':
145 Anim = !Anim;
146 if (Anim)
147 glutIdleFunc(Idle);
148 else
149 glutIdleFunc(NULL);
150 break;
151 case 'z':
152 Zrot -= step;
153 break;
154 case 'Z':
155 Zrot += step;
156 break;
157 case 'b':
158 Bias -= 10;
159 break;
160 case 'B':
161 Bias += 10;
162 break;
163 case '0':
164 case '1':
165 case '2':
166 case '3':
167 case '4':
168 case '5':
169 case '6':
170 case '7':
171 case '8':
172 case '9':
173 Bias = 100.0 * (key - '0');
174 break;
175 case 27:
176 glutDestroyWindow(win);
177 exit(0);
178 break;
179 }
180 glutPostRedisplay();
181 }
182
183
184 static void SpecialKey( int key, int x, int y )
185 {
186 const GLfloat step = 3.0;
187 (void) x;
188 (void) y;
189 switch (key) {
190 case GLUT_KEY_UP:
191 Xrot -= step;
192 break;
193 case GLUT_KEY_DOWN:
194 Xrot += step;
195 break;
196 case GLUT_KEY_LEFT:
197 Yrot -= step;
198 break;
199 case GLUT_KEY_RIGHT:
200 Yrot += step;
201 break;
202 }
203 glutPostRedisplay();
204 }
205
206
207 static void Init( void )
208 {
209 GLfloat maxBias;
210
211 if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
212 printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
213 exit(1);
214 }
215
216 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
217
218 glGenTextures(1, &TexObj);
219 glBindTexture(GL_TEXTURE_2D, TexObj);
220
221 if (glutExtensionSupported("GL_SGIS_generate_mipmap")) {
222 /* test auto mipmap generation */
223 GLint width, height, i;
224 GLenum format;
225 GLubyte *image = LoadRGBImage(TEXTURE_FILE, &width, &height, &format);
226 if (!image) {
227 printf("Error: could not load texture image %s\n", TEXTURE_FILE);
228 exit(1);
229 }
230 /* resize to 256 x 256 */
231 if (width != 256 || height != 256) {
232 GLubyte *newImage = malloc(256 * 256 * 4);
233 gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
234 256, 256, GL_UNSIGNED_BYTE, newImage);
235 free(image);
236 image = newImage;
237 }
238 printf("Using GL_SGIS_generate_mipmap\n");
239 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
240 glTexImage2D(GL_TEXTURE_2D, 0, format, 256, 256, 0,
241 format, GL_UNSIGNED_BYTE, image);
242 free(image);
243
244 /* make sure mipmap was really generated correctly */
245 width = height = 256;
246 for (i = 0; i < 9; i++) {
247 GLint w, h;
248 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w);
249 glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h);
250 printf("Level %d size: %d x %d\n", i, w, h);
251 assert(w == width);
252 assert(h == height);
253 width /= 2;
254 height /= 2;
255 }
256
257 }
258 else if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
259 printf("Error: could not load texture image %s\n", TEXTURE_FILE);
260 exit(1);
261 }
262
263 /* mipmapping required for this extension */
264 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
265 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
266 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
267
268 glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT, &maxBias);
269 printf("LOD bias range: [%g, %g]\n", -maxBias, maxBias);
270 BiasMin = -100 * maxBias;
271 BiasMax = 100 * maxBias;
272
273 /* Since we have (about) 8 mipmap levels, no need to bias beyond
274 * the range [-1, +8].
275 */
276 if (BiasMin < -100)
277 BiasMin = -100;
278 if (BiasMax > 800)
279 BiasMax = 800;
280 }
281
282
283 int main( int argc, char *argv[] )
284 {
285 glutInit( &argc, argv );
286 glutInitWindowPosition( 0, 0 );
287 glutInitWindowSize( 350, 350 );
288 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
289 win = glutCreateWindow(argv[0]);
290 glutReshapeFunc( Reshape );
291 glutKeyboardFunc( Key );
292 glutSpecialFunc( SpecialKey );
293 glutDisplayFunc( Display );
294 if (Anim)
295 glutIdleFunc(Idle);
296 Init();
297 glutMainLoop();
298 return 0;
299 }