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