Brian Paul | aa80e29 | 2000-03-20 22:00:22 +0000 | [diff] [blame] | 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 <stdlib.h> |
| 31 | #include <stdio.h> |
| 32 | #include <math.h> |
| 33 | #include <GL/glut.h> |
| 34 | #include <GL/glext.h> |
| 35 | |
pesco | d1ff1f6 | 2000-12-24 22:53:54 +0000 | [diff] [blame^] | 36 | #include "readtex.c" /* I know, this is a hack. */ |
Brian Paul | aa80e29 | 2000-03-20 22:00:22 +0000 | [diff] [blame] | 37 | |
| 38 | #define TEXTURE_FILE "../images/girl.rgb" |
| 39 | |
| 40 | static GLfloat Xrot = 0, Yrot = -30, Zrot = 0; |
| 41 | static GLboolean Anim = GL_TRUE; |
Brian Paul | d21cdb6 | 2000-03-22 18:38:47 +0000 | [diff] [blame] | 42 | static GLint Bias = 0, BiasStepSign = +1; /* ints avoid fp precision problem */ |
| 43 | static GLint BiasMin = -200, BiasMax = 500; |
Brian Paul | aa80e29 | 2000-03-20 22:00:22 +0000 | [diff] [blame] | 44 | |
| 45 | |
| 46 | |
| 47 | static void |
| 48 | PrintString(const char *s) |
| 49 | { |
| 50 | while (*s) { |
| 51 | glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s); |
| 52 | s++; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static void Idle( void ) |
| 57 | { |
Brian Paul | d21cdb6 | 2000-03-22 18:38:47 +0000 | [diff] [blame] | 58 | 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 Paul | aa80e29 | 2000-03-20 22:00:22 +0000 | [diff] [blame] | 71 | if (Bias < BiasMin) { |
| 72 | Bias = BiasMin; |
Brian Paul | d21cdb6 | 2000-03-22 18:38:47 +0000 | [diff] [blame] | 73 | BiasStepSign = +1; |
Brian Paul | aa80e29 | 2000-03-20 22:00:22 +0000 | [diff] [blame] | 74 | } |
| 75 | else if (Bias > BiasMax) { |
| 76 | Bias = BiasMax; |
Brian Paul | d21cdb6 | 2000-03-22 18:38:47 +0000 | [diff] [blame] | 77 | BiasStepSign = -1; |
Brian Paul | aa80e29 | 2000-03-20 22:00:22 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | glutPostRedisplay(); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | static 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 | |
| 130 | static void Reshape( int width, int height ) |
| 131 | { |
| 132 | glViewport( 0, 0, width, height ); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | static 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 | |
| 169 | static 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 | |
| 192 | static 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 | |
| 213 | int 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 | } |