2
* GL_ARB_multitexture demo
4
* Command line options:
5
* -info print GL implementation information
8
* Brian Paul November 1998 This program is in the public domain.
9
* Modified on 12 Feb 2002 for > 2 texture units.
22
#define TEXTURE_1_FILE "../images/girl.rgb"
23
#define TEXTURE_2_FILE "../images/reflect.rgb"
31
static GLint Frames = 0;
32
static GLboolean Animate = GL_TRUE;
33
static GLint NumUnits = 1;
34
static GLboolean TexEnabled[8];
36
static GLfloat Drift = 0.0;
37
static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
40
static void Idle( void )
45
Drift = glutGet(GLUT_ELAPSED_TIME) * 0.001;
47
for (i = 0; i < NumUnits; i++) {
48
glActiveTextureARB(GL_TEXTURE0_ARB + i);
49
glMatrixMode(GL_TEXTURE);
52
glTranslatef(Drift, 0.0, 0.0);
56
glTranslatef(0.0, Drift, 0.0);
59
float tx = 0.5, ty = 0.5;
60
glTranslatef(tx, ty, 0.0);
61
glRotatef(180.0 * Drift, 0, 0, 1);
62
glScalef(1.0/i, 1.0/i, 1.0/i);
63
glTranslatef(-tx, -ty + i * 0.1, 0.0);
66
glMatrixMode(GL_MODELVIEW);
73
static void DrawObject(void)
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 };
79
if (!TexEnabled[0] && !TexEnabled[1])
80
glColor3f(0.1, 0.1, 0.1); /* add onto this */
82
glColor3f(1, 1, 1); /* modulate this */
85
for (j = 0; j < 4; j++ ) {
86
for (i = 0; i < NumUnits; i++) {
88
glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i,
89
tex_coords[j], tex_coords[j+1]);
91
glVertex2f( vtx_coords[j], vtx_coords[j+1] );
97
static void Display( void )
99
glClear( GL_COLOR_BUFFER_BIT );
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);
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);
127
static void Reshape( int width, int height )
129
glViewport( 0, 0, width, height );
130
glMatrixMode( GL_PROJECTION );
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 );
136
glTranslatef( 0.0, 0.0, -70.0 );
140
static void ToggleUnit(int unit)
142
TexEnabled[unit] = !TexEnabled[unit];
143
glActiveTextureARB(GL_TEXTURE0_ARB + unit);
144
if (TexEnabled[unit])
145
glEnable(GL_TEXTURE_2D);
147
glDisable(GL_TEXTURE_2D);
149
for (unit = 0; unit < NumUnits; unit++)
150
printf("%d ", (int) TexEnabled[unit]);
155
static void ModeMenu(int entry)
157
if (entry >= TEX0 && entry <= TEX7) {
159
GLint i = entry - TEX0;
162
else if (entry==ANIMATE) {
169
else if (entry==QUIT) {
177
static void Key( unsigned char key, int x, int y )
217
static void SpecialKey( int key, int x, int y )
241
static void Init( int argc, char *argv[] )
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");
252
glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits);
253
printf("%d texture units supported\n", NumUnits);
257
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
258
printf("%d x %d max texture size\n", size, size);
260
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
262
for (i = 0; i < NumUnits; i++) {
264
TexEnabled[i] = GL_TRUE;
266
TexEnabled[i] = GL_FALSE;
269
/* allocate two texture objects */
270
glGenTextures(NumUnits, texObj);
272
/* setup the texture objects */
273
for (i = 0; i < NumUnits; i++) {
275
glActiveTextureARB(GL_TEXTURE0_ARB + i);
276
glBindTexture(GL_TEXTURE_2D, texObj[i]);
278
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
279
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
282
if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
283
printf("Error: couldn't load texture image\n");
288
if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
289
printf("Error: couldn't load texture image\n");
295
GLubyte image[8][8][3];
297
for (i = 0; i < 8; i++) {
298
for (j = 0; j < 8; j++) {
311
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0,
312
GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *) image);
315
/* Bind texObj[i] to ith texture unit */
317
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
319
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
322
glEnable(GL_TEXTURE_2D);
325
glShadeModel(GL_FLAT);
326
glClearColor(0.3, 0.3, 0.4, 1.0);
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));
337
int main( int argc, char *argv[] )
341
glutInitWindowSize( 300, 300 );
342
glutInit( &argc, argv );
343
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
344
glutCreateWindow(argv[0] );
349
glutReshapeFunc( Reshape );
350
glutKeyboardFunc( Key );
351
glutSpecialFunc( SpecialKey );
352
glutDisplayFunc( Display );
356
glutCreateMenu(ModeMenu);
358
for (i = 0; i < NumUnits; i++) {
360
sprintf(s, "Toggle Texture %d", i);
361
glutAddMenuEntry(s, TEX0 + i);
363
glutAddMenuEntry("Toggle Animation", ANIMATE);
364
glutAddMenuEntry("Quit", QUIT);
365
glutAttachMenu(GLUT_RIGHT_BUTTON);