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