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"
30 static GLboolean Animate
= GL_TRUE
;
31 static GLint NumUnits
= 1;
32 static GLboolean TexEnabled
[8];
34 static GLfloat Drift
= 0.0;
35 static GLfloat Xrot
= 20.0, Yrot
= 30.0, Zrot
= 0.0;
38 static void Idle( void )
43 Drift
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
45 for (i
= 0; i
< NumUnits
; i
++) {
46 glActiveTextureARB(GL_TEXTURE0_ARB
+ i
);
47 glMatrixMode(GL_TEXTURE
);
50 glTranslatef(Drift
, 0.0, 0.0);
54 glTranslatef(0.0, Drift
, 0.0);
57 float tx
= 0.5, ty
= 0.5;
58 glTranslatef(tx
, ty
, 0.0);
59 glRotatef(180.0 * Drift
, 0, 0, 1);
60 glScalef(1.0/i
, 1.0/i
, 1.0/i
);
61 glTranslatef(-tx
, -ty
+ i
* 0.1, 0.0);
64 glMatrixMode(GL_MODELVIEW
);
71 static void DrawObject(void)
73 static const GLfloat tex_coords
[] = { 0.0, 0.0, 1.0, 1.0, 0.0 };
74 static const GLfloat vtx_coords
[] = { -1.0, -1.0, 1.0, 1.0, -1.0 };
77 if (!TexEnabled
[0] && !TexEnabled
[1])
78 glColor3f(0.1, 0.1, 0.1); /* add onto this */
80 glColor3f(1, 1, 1); /* modulate this */
83 for (j
= 0; j
< 4; j
++ ) {
84 for (i
= 0; i
< NumUnits
; i
++) {
86 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
+ i
,
87 tex_coords
[j
], tex_coords
[j
+1]);
89 glVertex2f( vtx_coords
[j
], vtx_coords
[j
+1] );
95 static void Display( void )
97 glClear( GL_COLOR_BUFFER_BIT
);
100 glRotatef(Xrot
, 1.0, 0.0, 0.0);
101 glRotatef(Yrot
, 0.0, 1.0, 0.0);
102 glRotatef(Zrot
, 0.0, 0.0, 1.0);
103 glScalef(5.0, 5.0, 5.0);
111 static void Reshape( int width
, int height
)
113 glViewport( 0, 0, width
, height
);
114 glMatrixMode( GL_PROJECTION
);
116 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
117 /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
118 glMatrixMode( GL_MODELVIEW
);
120 glTranslatef( 0.0, 0.0, -70.0 );
124 static void ToggleUnit(int unit
)
126 TexEnabled
[unit
] = !TexEnabled
[unit
];
127 glActiveTextureARB(GL_TEXTURE0_ARB
+ unit
);
128 if (TexEnabled
[unit
])
129 glEnable(GL_TEXTURE_2D
);
131 glDisable(GL_TEXTURE_2D
);
133 for (unit
= 0; unit
< NumUnits
; unit
++)
134 printf("%d ", (int) TexEnabled
[unit
]);
139 static void ModeMenu(int entry
)
141 if (entry
>= TEX0
&& entry
<= TEX7
) {
143 GLint i
= entry
- TEX0
;
146 else if (entry
==ANIMATE
) {
153 else if (entry
==QUIT
) {
161 static void Key( unsigned char key
, int x
, int y
)
201 static void SpecialKey( int key
, int x
, int y
)
225 static void Init( int argc
, char *argv
[] )
230 const char *exten
= (const char *) glGetString(GL_EXTENSIONS
);
231 if (!strstr(exten
, "GL_ARB_multitexture")) {
232 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
236 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB
, &NumUnits
);
237 printf("%d texture units supported\n", NumUnits
);
241 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, &size
);
242 printf("%d x %d max texture size\n", size
, size
);
244 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
246 for (i
= 0; i
< NumUnits
; i
++) {
248 TexEnabled
[i
] = GL_TRUE
;
250 TexEnabled
[i
] = GL_FALSE
;
253 /* allocate two texture objects */
254 glGenTextures(NumUnits
, texObj
);
256 /* setup the texture objects */
257 for (i
= 0; i
< NumUnits
; i
++) {
259 glActiveTextureARB(GL_TEXTURE0_ARB
+ i
);
260 glBindTexture(GL_TEXTURE_2D
, texObj
[i
]);
262 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
263 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
266 if (!LoadRGBMipmaps(TEXTURE_1_FILE
, GL_RGB
)) {
267 printf("Error: couldn't load texture image\n");
272 if (!LoadRGBMipmaps(TEXTURE_2_FILE
, GL_RGB
)) {
273 printf("Error: couldn't load texture image\n");
279 GLubyte image
[8][8][3];
281 for (i
= 0; i
< 8; i
++) {
282 for (j
= 0; j
< 8; j
++) {
295 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, 8, 8, 0,
296 GL_RGB
, GL_UNSIGNED_BYTE
, (GLvoid
*) image
);
299 /* Bind texObj[i] to ith texture unit */
301 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
303 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_ADD
);
306 glEnable(GL_TEXTURE_2D
);
309 glShadeModel(GL_FLAT
);
310 glClearColor(0.3, 0.3, 0.4, 1.0);
312 if (argc
> 1 && strcmp(argv
[1], "-info")==0) {
313 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
314 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
315 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
316 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
321 int main( int argc
, char *argv
[] )
325 glutInit( &argc
, argv
);
326 glutInitWindowSize( 300, 300 );
327 glutInitWindowPosition( 0, 0 );
328 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
329 glutCreateWindow(argv
[0] );
334 glutReshapeFunc( Reshape
);
335 glutKeyboardFunc( Key
);
336 glutSpecialFunc( SpecialKey
);
337 glutDisplayFunc( Display
);
341 glutCreateMenu(ModeMenu
);
343 for (i
= 0; i
< NumUnits
; i
++) {
345 sprintf(s
, "Toggle Texture %d", i
);
346 glutAddMenuEntry(s
, TEX0
+ i
);
348 glutAddMenuEntry("Toggle Animation", ANIMATE
);
349 glutAddMenuEntry("Quit", QUIT
);
350 glutAttachMenu(GLUT_RIGHT_BUTTON
);