jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * GL_ARB_multitexture demo |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 4 | * |
| 5 | * Command line options: |
| 6 | * -info print GL implementation information |
| 7 | * |
| 8 | * |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 9 | * Brian Paul November 1998 This program is in the public domain. |
Brian Paul | cbd9a02 | 2002-02-13 02:23:33 +0000 | [diff] [blame] | 10 | * Modified on 12 Feb 2002 for > 2 texture units. |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | |
| 14 | #include <math.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | #include <GL/glut.h> |
| 19 | |
pesco | d1ff1f6 | 2000-12-24 22:53:54 +0000 | [diff] [blame] | 20 | #include "readtex.c" /* I know, this is a hack. */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 21 | |
| 22 | #define TEXTURE_1_FILE "../images/girl.rgb" |
| 23 | #define TEXTURE_2_FILE "../images/reflect.rgb" |
| 24 | |
| 25 | #define TEX0 1 |
Brian Paul | cbd9a02 | 2002-02-13 02:23:33 +0000 | [diff] [blame] | 26 | #define TEX7 8 |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 27 | #define ANIMATE 10 |
| 28 | #define QUIT 100 |
| 29 | |
| 30 | static GLboolean Animate = GL_TRUE; |
Brian Paul | cbd9a02 | 2002-02-13 02:23:33 +0000 | [diff] [blame] | 31 | static GLint NumUnits = 1; |
| 32 | static GLboolean TexEnabled[8]; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 33 | |
| 34 | static GLfloat Drift = 0.0; |
Brian Paul | 785774d | 2003-05-30 15:30:16 +0000 | [diff] [blame] | 35 | static GLfloat drift_increment = 0.005; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 36 | static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0; |
| 37 | |
| 38 | |
| 39 | |
| 40 | static void Idle( void ) |
| 41 | { |
| 42 | if (Animate) { |
Brian Paul | cbd9a02 | 2002-02-13 02:23:33 +0000 | [diff] [blame] | 43 | GLint i; |
| 44 | |
Brian Paul | 785774d | 2003-05-30 15:30:16 +0000 | [diff] [blame] | 45 | Drift += drift_increment; |
Brian Paul | b45c71a | 2000-02-02 17:31:45 +0000 | [diff] [blame] | 46 | if (Drift >= 1.0) |
Brian Paul | d2702f0 | 2000-02-02 01:07:21 +0000 | [diff] [blame] | 47 | Drift = 0.0; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 48 | |
Brian Paul | cbd9a02 | 2002-02-13 02:23:33 +0000 | [diff] [blame] | 49 | for (i = 0; i < NumUnits; i++) { |
| 50 | glActiveTextureARB(GL_TEXTURE0_ARB + i); |
| 51 | glMatrixMode(GL_TEXTURE); |
| 52 | glLoadIdentity(); |
| 53 | if (i == 0) { |
| 54 | glTranslatef(Drift, 0.0, 0.0); |
Keith Whitwell | 740f7de | 2004-01-27 16:18:00 +0000 | [diff] [blame^] | 55 | glScalef(2, 2, 1); |
Brian Paul | cbd9a02 | 2002-02-13 02:23:33 +0000 | [diff] [blame] | 56 | } |
| 57 | else if (i == 1) { |
| 58 | glTranslatef(0.0, Drift, 0.0); |
| 59 | } |
| 60 | else { |
| 61 | glTranslatef(0.5, 0.5, 0.0); |
| 62 | glRotatef(180.0 * Drift, 0, 0, 1); |
| 63 | glScalef(1.0/i, 1.0/i, 1.0/i); |
| 64 | glTranslatef(-0.5, -0.5, 0.0); |
| 65 | } |
| 66 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 67 | glMatrixMode(GL_MODELVIEW); |
| 68 | |
| 69 | glutPostRedisplay(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | |
| 74 | static void DrawObject(void) |
| 75 | { |
Brian Paul | cbd9a02 | 2002-02-13 02:23:33 +0000 | [diff] [blame] | 76 | GLint i; |
Brian Paul | 785774d | 2003-05-30 15:30:16 +0000 | [diff] [blame] | 77 | GLint j; |
| 78 | static const GLfloat tex_coords[] = { 0.0, 0.0, 1.0, 1.0, 0.0 }; |
| 79 | static const GLfloat vtx_coords[] = { -1.0, -1.0, 1.0, 1.0, -1.0 }; |
Brian Paul | cbd9a02 | 2002-02-13 02:23:33 +0000 | [diff] [blame] | 80 | |
| 81 | if (!TexEnabled[0] && !TexEnabled[1]) |
| 82 | glColor3f(0.1, 0.1, 0.1); /* add onto this */ |
| 83 | else |
| 84 | glColor3f(1, 1, 1); /* modulate this */ |
| 85 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 86 | glBegin(GL_QUADS); |
| 87 | |
Brian Paul | 785774d | 2003-05-30 15:30:16 +0000 | [diff] [blame] | 88 | /* Toggle between the vector and scalar entry points. This is done purely |
| 89 | * to hit multiple paths in the driver. |
| 90 | */ |
| 91 | if ( Drift > 0.49 ) { |
| 92 | for (j = 0; j < 4; j++ ) { |
| 93 | for (i = 0; i < NumUnits; i++) |
| 94 | glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, |
| 95 | tex_coords[j], tex_coords[j+1]); |
| 96 | glVertex2f( vtx_coords[j], vtx_coords[j+1] ); |
| 97 | } |
| 98 | } |
| 99 | else { |
| 100 | for (j = 0; j < 4; j++ ) { |
| 101 | for (i = 0; i < NumUnits; i++) |
| 102 | glMultiTexCoord2fvARB(GL_TEXTURE0_ARB + i, & tex_coords[j]); |
| 103 | glVertex2fv( & vtx_coords[j] ); |
| 104 | } |
| 105 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 106 | |
| 107 | glEnd(); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | |
| 112 | static void Display( void ) |
| 113 | { |
Brian Paul | 785774d | 2003-05-30 15:30:16 +0000 | [diff] [blame] | 114 | static GLint T0 = 0; |
| 115 | static GLint Frames = 0; |
| 116 | GLint t; |
| 117 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 118 | glClear( GL_COLOR_BUFFER_BIT ); |
| 119 | |
| 120 | glPushMatrix(); |
| 121 | glRotatef(Xrot, 1.0, 0.0, 0.0); |
| 122 | glRotatef(Yrot, 0.0, 1.0, 0.0); |
| 123 | glRotatef(Zrot, 0.0, 0.0, 1.0); |
| 124 | glScalef(5.0, 5.0, 5.0); |
| 125 | DrawObject(); |
| 126 | glPopMatrix(); |
| 127 | |
| 128 | glutSwapBuffers(); |
Brian Paul | 785774d | 2003-05-30 15:30:16 +0000 | [diff] [blame] | 129 | |
| 130 | Frames++; |
| 131 | |
| 132 | t = glutGet(GLUT_ELAPSED_TIME); |
| 133 | if (t - T0 >= 250) { |
| 134 | GLfloat seconds = (t - T0) / 1000.0; |
| 135 | drift_increment = 2.2 * seconds / Frames; |
| 136 | T0 = t; |
| 137 | Frames = 0; |
| 138 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
| 142 | static void Reshape( int width, int height ) |
| 143 | { |
| 144 | glViewport( 0, 0, width, height ); |
| 145 | glMatrixMode( GL_PROJECTION ); |
| 146 | glLoadIdentity(); |
| 147 | glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 ); |
| 148 | /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/ |
| 149 | glMatrixMode( GL_MODELVIEW ); |
| 150 | glLoadIdentity(); |
| 151 | glTranslatef( 0.0, 0.0, -70.0 ); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | static void ModeMenu(int entry) |
| 156 | { |
Brian Paul | cbd9a02 | 2002-02-13 02:23:33 +0000 | [diff] [blame] | 157 | if (entry >= TEX0 && entry <= TEX7) { |
| 158 | /* toggle */ |
| 159 | GLint i = entry - TEX0; |
| 160 | TexEnabled[i] = !TexEnabled[i]; |
| 161 | glActiveTextureARB(GL_TEXTURE0_ARB + i); |
| 162 | if (TexEnabled[i]) |
| 163 | glEnable(GL_TEXTURE_2D); |
| 164 | else |
| 165 | glDisable(GL_TEXTURE_2D); |
| 166 | printf("Enabled: "); |
| 167 | for (i = 0; i < NumUnits; i++) |
| 168 | printf("%d ", (int) TexEnabled[i]); |
| 169 | printf("\n"); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 170 | } |
| 171 | else if (entry==ANIMATE) { |
| 172 | Animate = !Animate; |
| 173 | } |
| 174 | else if (entry==QUIT) { |
| 175 | exit(0); |
| 176 | } |
| 177 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 178 | glutPostRedisplay(); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | static void Key( unsigned char key, int x, int y ) |
| 183 | { |
| 184 | (void) x; |
| 185 | (void) y; |
| 186 | switch (key) { |
| 187 | case 27: |
| 188 | exit(0); |
| 189 | break; |
| 190 | } |
| 191 | glutPostRedisplay(); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | static void SpecialKey( int key, int x, int y ) |
| 196 | { |
| 197 | float step = 3.0; |
| 198 | (void) x; |
| 199 | (void) y; |
| 200 | |
| 201 | switch (key) { |
| 202 | case GLUT_KEY_UP: |
| 203 | Xrot += step; |
| 204 | break; |
| 205 | case GLUT_KEY_DOWN: |
| 206 | Xrot -= step; |
| 207 | break; |
| 208 | case GLUT_KEY_LEFT: |
| 209 | Yrot += step; |
| 210 | break; |
| 211 | case GLUT_KEY_RIGHT: |
| 212 | Yrot -= step; |
| 213 | break; |
| 214 | } |
| 215 | glutPostRedisplay(); |
| 216 | } |
| 217 | |
| 218 | |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 219 | static void Init( int argc, char *argv[] ) |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 220 | { |
Brian Paul | cbd9a02 | 2002-02-13 02:23:33 +0000 | [diff] [blame] | 221 | GLuint texObj[8]; |
| 222 | GLint size, i; |
Brian Paul | 1a3b8ff | 1999-10-13 12:02:13 +0000 | [diff] [blame] | 223 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 224 | const char *exten = (const char *) glGetString(GL_EXTENSIONS); |
| 225 | if (!strstr(exten, "GL_ARB_multitexture")) { |
| 226 | printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n"); |
| 227 | exit(1); |
| 228 | } |
| 229 | |
Brian Paul | cbd9a02 | 2002-02-13 02:23:33 +0000 | [diff] [blame] | 230 | glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits); |
| 231 | printf("%d texture units supported\n", NumUnits); |
| 232 | if (NumUnits > 8) |
| 233 | NumUnits = 8; |
Brian Paul | 563d26b | 2000-11-01 16:02:01 +0000 | [diff] [blame] | 234 | |
Brian Paul | 4d99e5b | 2001-06-20 19:12:30 +0000 | [diff] [blame] | 235 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size); |
| 236 | printf("%d x %d max texture size\n", size, size); |
| 237 | |
Brian Paul | 6886019 | 2001-06-13 14:33:16 +0000 | [diff] [blame] | 238 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 239 | |
Brian Paul | cbd9a02 | 2002-02-13 02:23:33 +0000 | [diff] [blame] | 240 | for (i = 0; i < NumUnits; i++) { |
| 241 | if (i < 2) |
| 242 | TexEnabled[i] = GL_TRUE; |
| 243 | else |
| 244 | TexEnabled[i] = GL_FALSE; |
| 245 | } |
| 246 | |
Brian Paul | 1a3b8ff | 1999-10-13 12:02:13 +0000 | [diff] [blame] | 247 | /* allocate two texture objects */ |
Brian Paul | cbd9a02 | 2002-02-13 02:23:33 +0000 | [diff] [blame] | 248 | glGenTextures(NumUnits, texObj); |
Brian Paul | 1a3b8ff | 1999-10-13 12:02:13 +0000 | [diff] [blame] | 249 | |
Brian Paul | cbd9a02 | 2002-02-13 02:23:33 +0000 | [diff] [blame] | 250 | /* setup the texture objects */ |
| 251 | for (i = 0; i < NumUnits; i++) { |
| 252 | |
| 253 | glActiveTextureARB(GL_TEXTURE0_ARB + i); |
| 254 | glBindTexture(GL_TEXTURE_2D, texObj[i]); |
| 255 | |
| 256 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 257 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 258 | |
| 259 | if (i == 0) { |
| 260 | if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) { |
| 261 | printf("Error: couldn't load texture image\n"); |
| 262 | exit(1); |
| 263 | } |
| 264 | } |
| 265 | else if (i == 1) { |
| 266 | if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) { |
| 267 | printf("Error: couldn't load texture image\n"); |
| 268 | exit(1); |
| 269 | } |
| 270 | } |
| 271 | else { |
| 272 | /* checker */ |
| 273 | GLubyte image[8][8][3]; |
| 274 | GLint i, j; |
| 275 | for (i = 0; i < 8; i++) { |
| 276 | for (j = 0; j < 8; j++) { |
| 277 | if ((i + j) & 1) { |
| 278 | image[i][j][0] = 50; |
| 279 | image[i][j][1] = 50; |
| 280 | image[i][j][2] = 50; |
| 281 | } |
| 282 | else { |
| 283 | image[i][j][0] = 25; |
| 284 | image[i][j][1] = 25; |
| 285 | image[i][j][2] = 25; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, |
| 290 | GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *) image); |
| 291 | } |
| 292 | |
| 293 | /* Bind texObj[i] to ith texture unit */ |
| 294 | if (i < 2) |
| 295 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 296 | else |
| 297 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD); |
| 298 | |
| 299 | if (TexEnabled[i]) |
| 300 | glEnable(GL_TEXTURE_2D); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 301 | } |
| 302 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 303 | glShadeModel(GL_FLAT); |
| 304 | glClearColor(0.3, 0.3, 0.4, 1.0); |
| 305 | |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 306 | if (argc > 1 && strcmp(argv[1], "-info")==0) { |
| 307 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 308 | printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); |
| 309 | printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); |
| 310 | printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS)); |
| 311 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | |
| 315 | int main( int argc, char *argv[] ) |
| 316 | { |
Brian Paul | cbd9a02 | 2002-02-13 02:23:33 +0000 | [diff] [blame] | 317 | GLint i; |
| 318 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 319 | glutInit( &argc, argv ); |
| 320 | glutInitWindowSize( 300, 300 ); |
Brian Paul | 4d59844 | 2000-05-23 23:21:00 +0000 | [diff] [blame] | 321 | glutInitWindowPosition( 0, 0 ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 322 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); |
| 323 | glutCreateWindow(argv[0] ); |
| 324 | |
Brian Paul | ac12609 | 1999-10-21 16:39:06 +0000 | [diff] [blame] | 325 | Init( argc, argv ); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 326 | |
| 327 | glutReshapeFunc( Reshape ); |
| 328 | glutKeyboardFunc( Key ); |
| 329 | glutSpecialFunc( SpecialKey ); |
| 330 | glutDisplayFunc( Display ); |
| 331 | glutIdleFunc( Idle ); |
| 332 | |
| 333 | glutCreateMenu(ModeMenu); |
Brian Paul | cbd9a02 | 2002-02-13 02:23:33 +0000 | [diff] [blame] | 334 | |
| 335 | for (i = 0; i < NumUnits; i++) { |
| 336 | char s[100]; |
| 337 | sprintf(s, "Toggle Texture %d", i); |
| 338 | glutAddMenuEntry(s, TEX0 + i); |
| 339 | } |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 340 | glutAddMenuEntry("Toggle Animation", ANIMATE); |
| 341 | glutAddMenuEntry("Quit", QUIT); |
| 342 | glutAttachMenu(GLUT_RIGHT_BUTTON); |
| 343 | |
| 344 | glutMainLoop(); |
| 345 | return 0; |
| 346 | } |