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.
21 #define TEXTURE_1_FILE "../images/girl.rgb"
22 #define TEXTURE_2_FILE "../images/reflect.rgb"
29 static GLboolean Animate
= GL_TRUE
;
30 static GLint NumUnits
= 1;
31 static GLboolean TexEnabled
[8];
33 static GLfloat Drift
= 0.0;
34 static GLfloat Xrot
= 20.0, Yrot
= 30.0, Zrot
= 0.0;
37 static void Idle( void )
42 Drift
= glutGet(GLUT_ELAPSED_TIME
) * 0.001;
44 for (i
= 0; i
< NumUnits
; i
++) {
45 glActiveTextureARB(GL_TEXTURE0_ARB
+ i
);
46 glMatrixMode(GL_TEXTURE
);
49 glTranslatef(Drift
, 0.0, 0.0);
53 glTranslatef(0.0, Drift
, 0.0);
56 float tx
= 0.5, ty
= 0.5;
57 glTranslatef(tx
, ty
, 0.0);
58 glRotatef(180.0 * Drift
, 0, 0, 1);
59 glScalef(1.0/i
, 1.0/i
, 1.0/i
);
60 glTranslatef(-tx
, -ty
+ i
* 0.1, 0.0);
63 glMatrixMode(GL_MODELVIEW
);
70 static void DrawObject(void)
72 static const GLfloat tex_coords
[] = { 0.0, 0.0, 1.0, 1.0, 0.0 };
73 static const GLfloat vtx_coords
[] = { -1.0, -1.0, 1.0, 1.0, -1.0 };
76 if (!TexEnabled
[0] && !TexEnabled
[1])
77 glColor3f(0.1, 0.1, 0.1); /* add onto this */
79 glColor3f(1, 1, 1); /* modulate this */
82 for (j
= 0; j
< 4; j
++ ) {
83 for (i
= 0; i
< NumUnits
; i
++) {
85 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
+ i
,
86 tex_coords
[j
], tex_coords
[j
+1]);
88 glVertex2f( vtx_coords
[j
], vtx_coords
[j
+1] );
94 static void Display( void )
96 glClear( GL_COLOR_BUFFER_BIT
);
99 glRotatef(Xrot
, 1.0, 0.0, 0.0);
100 glRotatef(Yrot
, 0.0, 1.0, 0.0);
101 glRotatef(Zrot
, 0.0, 0.0, 1.0);
102 glScalef(5.0, 5.0, 5.0);
110 static void Reshape( int width
, int height
)
112 glViewport( 0, 0, width
, height
);
113 glMatrixMode( GL_PROJECTION
);
115 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
116 /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
117 glMatrixMode( GL_MODELVIEW
);
119 glTranslatef( 0.0, 0.0, -70.0 );
123 static void ToggleUnit(int unit
)
125 TexEnabled
[unit
] = !TexEnabled
[unit
];
126 glActiveTextureARB(GL_TEXTURE0_ARB
+ unit
);
127 if (TexEnabled
[unit
])
128 glEnable(GL_TEXTURE_2D
);
130 glDisable(GL_TEXTURE_2D
);
132 for (unit
= 0; unit
< NumUnits
; unit
++)
133 printf("%d ", (int) TexEnabled
[unit
]);
138 static void ModeMenu(int entry
)
140 if (entry
>= TEX0
&& entry
<= TEX7
) {
142 GLint i
= entry
- TEX0
;
145 else if (entry
==ANIMATE
) {
152 else if (entry
==QUIT
) {
160 static void Key( unsigned char key
, int x
, int y
)
200 static void SpecialKey( int key
, int x
, int y
)
224 static void Init( int argc
, char *argv
[] )
229 const char *exten
= (const char *) glGetString(GL_EXTENSIONS
);
230 if (!strstr(exten
, "GL_ARB_multitexture")) {
231 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
235 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB
, &NumUnits
);
236 printf("%d texture units supported\n", NumUnits
);
240 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, &size
);
241 printf("%d x %d max texture size\n", size
, size
);
243 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
245 for (i
= 0; i
< NumUnits
; i
++) {
247 TexEnabled
[i
] = GL_TRUE
;
249 TexEnabled
[i
] = GL_FALSE
;
252 /* allocate two texture objects */
253 glGenTextures(NumUnits
, texObj
);
255 /* setup the texture objects */
256 for (i
= 0; i
< NumUnits
; i
++) {
258 glActiveTextureARB(GL_TEXTURE0_ARB
+ i
);
259 glBindTexture(GL_TEXTURE_2D
, texObj
[i
]);
261 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
262 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
265 if (!LoadRGBMipmaps(TEXTURE_1_FILE
, GL_RGB
)) {
266 printf("Error: couldn't load texture image\n");
271 if (!LoadRGBMipmaps(TEXTURE_2_FILE
, GL_RGB
)) {
272 printf("Error: couldn't load texture image\n");
278 GLubyte image
[8][8][3];
280 for (i
= 0; i
< 8; i
++) {
281 for (j
= 0; j
< 8; j
++) {
294 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, 8, 8, 0,
295 GL_RGB
, GL_UNSIGNED_BYTE
, (GLvoid
*) image
);
298 /* Bind texObj[i] to ith texture unit */
300 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
302 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_ADD
);
305 glEnable(GL_TEXTURE_2D
);
308 glShadeModel(GL_FLAT
);
309 glClearColor(0.3, 0.3, 0.4, 1.0);
311 if (argc
> 1 && strcmp(argv
[1], "-info")==0) {
312 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
313 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
314 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
315 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
320 int main( int argc
, char *argv
[] )
324 glutInit( &argc
, argv
);
325 glutInitWindowSize( 300, 300 );
326 glutInitWindowPosition( 0, 0 );
327 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
328 glutCreateWindow(argv
[0] );
332 glutReshapeFunc( Reshape
);
333 glutKeyboardFunc( Key
);
334 glutSpecialFunc( SpecialKey
);
335 glutDisplayFunc( Display
);
339 glutCreateMenu(ModeMenu
);
341 for (i
= 0; i
< NumUnits
; i
++) {
343 sprintf(s
, "Toggle Texture %d", i
);
344 glutAddMenuEntry(s
, TEX0
+ i
);
346 glutAddMenuEntry("Toggle Animation", ANIMATE
);
347 glutAddMenuEntry("Quit", QUIT
);
348 glutAttachMenu(GLUT_RIGHT_BUTTON
);