blob: 23488b1616e0feeccef0f827b11be738510870f3 [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 Paul417719c2008-05-20 15:11:33 -060045static int win = 0;
Brian Paul7f86da62009-09-15 17:08:33 -060046static GLuint TexObj = 0;
Brian Paulaa80e292000-03-20 22:00:22 +000047
48
49static void
50PrintString(const char *s)
51{
52 while (*s) {
53 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
54 s++;
55 }
56}
57
58static void Idle( void )
59{
Brian Pauld21cdb62000-03-22 18:38:47 +000060 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;
Brian Paulaa80e292000-03-20 22:00:22 +000073 if (Bias < BiasMin) {
74 Bias = BiasMin;
Brian Pauld21cdb62000-03-22 18:38:47 +000075 BiasStepSign = +1;
Brian Paulaa80e292000-03-20 22:00:22 +000076 }
77 else if (Bias > BiasMax) {
78 Bias = BiasMax;
Brian Pauld21cdb62000-03-22 18:38:47 +000079 BiasStepSign = -1;
Brian Paulaa80e292000-03-20 22:00:22 +000080 }
81
82 glutPostRedisplay();
83}
84
85
86static 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
132static void Reshape( int width, int height )
133{
134 glViewport( 0, 0, width, height );
135}
136
137
138static 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;
Brian4697cee2008-02-11 09:46:10 -0700163 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;
Brian Paulaa80e292000-03-20 22:00:22 +0000175 case 27:
Brian Paul417719c2008-05-20 15:11:33 -0600176 glutDestroyWindow(win);
Brian Paulaa80e292000-03-20 22:00:22 +0000177 exit(0);
178 break;
179 }
180 glutPostRedisplay();
181}
182
183
184static 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
207static void Init( void )
208{
Brian Paul87c964d2001-11-06 15:53:00 +0000209 GLfloat maxBias;
210
Brian Paul76d2a772002-12-10 16:51:28 +0000211 if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
Brian Paulaa80e292000-03-20 22:00:22 +0000212 printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
213 exit(1);
214 }
215
216 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Brian Paul76d2a772002-12-10 16:51:28 +0000217
Brian Paul7f86da62009-09-15 17:08:33 -0600218 glGenTextures(1, &TexObj);
219 glBindTexture(GL_TEXTURE_2D, TexObj);
220
Brian Paul76d2a772002-12-10 16:51:28 +0000221 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)) {
Brian Paulaa80e292000-03-20 22:00:22 +0000259 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);
Brian Paul87c964d2001-11-06 15:53:00 +0000267
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;
Brian Paulf1324132004-03-26 18:05:36 +0000272
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;
Brian Paulaa80e292000-03-20 22:00:22 +0000280}
281
282
283int main( int argc, char *argv[] )
284{
Brian Paulaa80e292000-03-20 22:00:22 +0000285 glutInitWindowSize( 350, 350 );
Brian Paul263f4322009-12-18 08:12:55 -0700286 glutInit( &argc, argv );
Brian Paulaa80e292000-03-20 22:00:22 +0000287 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
Brian Paul417719c2008-05-20 15:11:33 -0600288 win = glutCreateWindow(argv[0]);
Brian Paulaa80e292000-03-20 22:00:22 +0000289 glutReshapeFunc( Reshape );
290 glutKeyboardFunc( Key );
291 glutSpecialFunc( SpecialKey );
292 glutDisplayFunc( Display );
293 if (Anim)
294 glutIdleFunc(Idle);
295 Init();
296 glutMainLoop();
297 return 0;
298}