blob: b2fab0b4eb7e4713e5d88e45dd8f22fda68e0ce1 [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
30#include <stdlib.h>
31#include <stdio.h>
32#include <math.h>
33#include <GL/glut.h>
34#include <GL/glext.h>
35
pescod1ff1f62000-12-24 22:53:54 +000036#include "readtex.c" /* I know, this is a hack. */
Brian Paulaa80e292000-03-20 22:00:22 +000037
38#define TEXTURE_FILE "../images/girl.rgb"
39
40static GLfloat Xrot = 0, Yrot = -30, Zrot = 0;
41static GLboolean Anim = GL_TRUE;
Brian Pauld21cdb62000-03-22 18:38:47 +000042static GLint Bias = 0, BiasStepSign = +1; /* ints avoid fp precision problem */
43static GLint BiasMin = -200, BiasMax = 500;
Brian Paulaa80e292000-03-20 22:00:22 +000044
45
46
47static void
48PrintString(const char *s)
49{
50 while (*s) {
51 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
52 s++;
53 }
54}
55
56static void Idle( void )
57{
Brian Pauld21cdb62000-03-22 18:38:47 +000058 static int lastTime = 0;
59 int time = glutGet(GLUT_ELAPSED_TIME);
60 int step;
61
62 if (lastTime == 0)
63 lastTime = time;
64 else if (time - lastTime < 10)
65 return;
66
67 step = (time - lastTime) / 10 * BiasStepSign;
68 lastTime = time;
69
70 Bias += step;
Brian Paulaa80e292000-03-20 22:00:22 +000071 if (Bias < BiasMin) {
72 Bias = BiasMin;
Brian Pauld21cdb62000-03-22 18:38:47 +000073 BiasStepSign = +1;
Brian Paulaa80e292000-03-20 22:00:22 +000074 }
75 else if (Bias > BiasMax) {
76 Bias = BiasMax;
Brian Pauld21cdb62000-03-22 18:38:47 +000077 BiasStepSign = -1;
Brian Paulaa80e292000-03-20 22:00:22 +000078 }
79
80 glutPostRedisplay();
81}
82
83
84static void Display( void )
85{
86 char str[100];
87
88 glClear( GL_COLOR_BUFFER_BIT );
89
90 glMatrixMode( GL_PROJECTION );
91 glLoadIdentity();
92 glOrtho(-1, 1, -1, 1, -1, 1);
93 glMatrixMode( GL_MODELVIEW );
94 glLoadIdentity();
95
96 glDisable(GL_TEXTURE_2D);
97 glColor3f(1,1,1);
98 glRasterPos3f(-0.9, -0.9, 0.0);
99 sprintf(str, "Texture LOD Bias = %4.1f", Bias * 0.01);
100 PrintString(str);
101
102 glMatrixMode( GL_PROJECTION );
103 glLoadIdentity();
104 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
105 glMatrixMode( GL_MODELVIEW );
106 glLoadIdentity();
107 glTranslatef( 0.0, 0.0, -8.0 );
108
109 glPushMatrix();
110 glRotatef(Xrot, 1, 0, 0);
111 glRotatef(Yrot, 0, 1, 0);
112 glRotatef(Zrot, 0, 0, 1);
113
114 glEnable(GL_TEXTURE_2D);
115 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.01 * Bias);
116
117 glBegin(GL_POLYGON);
118 glTexCoord2f(0, 0); glVertex2f(-1, -1);
119 glTexCoord2f(2, 0); glVertex2f( 1, -1);
120 glTexCoord2f(2, 2); glVertex2f( 1, 1);
121 glTexCoord2f(0, 2); glVertex2f(-1, 1);
122 glEnd();
123
124 glPopMatrix();
125
126 glutSwapBuffers();
127}
128
129
130static void Reshape( int width, int height )
131{
132 glViewport( 0, 0, width, height );
133}
134
135
136static void Key( unsigned char key, int x, int y )
137{
138 const GLfloat step = 3.0;
139 (void) x;
140 (void) y;
141 switch (key) {
142 case 'a':
143 Anim = !Anim;
144 if (Anim)
145 glutIdleFunc(Idle);
146 else
147 glutIdleFunc(NULL);
148 break;
149 case 'z':
150 Zrot -= step;
151 break;
152 case 'Z':
153 Zrot += step;
154 break;
155 case 'b':
156 Bias -= 10;
157 break;
158 case 'B':
159 Bias += 10;
160 break;
161 case 27:
162 exit(0);
163 break;
164 }
165 glutPostRedisplay();
166}
167
168
169static void SpecialKey( int key, int x, int y )
170{
171 const GLfloat step = 3.0;
172 (void) x;
173 (void) y;
174 switch (key) {
175 case GLUT_KEY_UP:
176 Xrot -= step;
177 break;
178 case GLUT_KEY_DOWN:
179 Xrot += step;
180 break;
181 case GLUT_KEY_LEFT:
182 Yrot -= step;
183 break;
184 case GLUT_KEY_RIGHT:
185 Yrot += step;
186 break;
187 }
188 glutPostRedisplay();
189}
190
191
192static void Init( void )
193{
194 const char *exten = (const char *) glGetString(GL_EXTENSIONS);
195 if (!strstr(exten, "GL_EXT_texture_lod_bias")) {
196 printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
197 exit(1);
198 }
199
200 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
201 if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
202 printf("Error: could not load texture image %s\n", TEXTURE_FILE);
203 exit(1);
204 }
205
206 /* mipmapping required for this extension */
207 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
208 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
209 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
210}
211
212
213int main( int argc, char *argv[] )
214{
215 glutInit( &argc, argv );
216 glutInitWindowPosition( 0, 0 );
217 glutInitWindowSize( 350, 350 );
218 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
219 glutCreateWindow(argv[0]);
220 glutReshapeFunc( Reshape );
221 glutKeyboardFunc( Key );
222 glutSpecialFunc( SpecialKey );
223 glutDisplayFunc( Display );
224 if (Anim)
225 glutIdleFunc(Idle);
226 Init();
227 glutMainLoop();
228 return 0;
229}