20aa29f99662c514d9edfcfb0e53ba8788ef0283
3 * GL_ARB_multitexture demo
5 * Command line options:
6 * -info print GL implementation information
9 * Brian Paul November 1998 This program is in the public domain.
10 * Modified on 12 Feb 2002 for > 2 texture units.
20 #include "readtex.c" /* I know, this is a hack. */
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 drift_increment
= 0.005;
36 static GLfloat Xrot
= 20.0, Yrot
= 30.0, Zrot
= 0.0;
40 static void Idle( void )
45 Drift
+= drift_increment
;
49 for (i
= 0; i
< NumUnits
; i
++) {
50 glActiveTextureARB(GL_TEXTURE0_ARB
+ i
);
51 glMatrixMode(GL_TEXTURE
);
54 glTranslatef(Drift
, 0.0, 0.0);
58 glTranslatef(0.0, Drift
, 0.0);
61 glTranslatef(0.5, 0.5, 0.0);
62 glRotatef(180.0 * Drift
, 0, 0, 1);
63 glScalef(1.0/i
, 1.0/i
, 1.0/i
);
64 glTranslatef(-0.5, -0.5, 0.0);
67 glMatrixMode(GL_MODELVIEW
);
74 static void DrawObject(void)
78 static const GLfloat tex_coords
[] = { 0.0, 0.0, 1.0, 1.0, 0.0 };
79 static const GLfloat vtx_coords
[] = { -1.0, -1.0, 1.0, 1.0, -1.0 };
81 if (!TexEnabled
[0] && !TexEnabled
[1])
82 glColor3f(0.1, 0.1, 0.1); /* add onto this */
84 glColor3f(1, 1, 1); /* modulate this */
88 /* Toggle between the vector and scalar entry points. This is done purely
89 * to hit multiple paths in the driver.
92 for (j
= 0; j
< 4; j
++ ) {
93 for (i
= 0; i
< NumUnits
; i
++)
94 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
+ i
,
95 tex_coords
[j
], tex_coords
[j
+1]);
96 glVertex2f( vtx_coords
[j
], vtx_coords
[j
+1] );
100 for (j
= 0; j
< 4; j
++ ) {
101 for (i
= 0; i
< NumUnits
; i
++)
102 glMultiTexCoord2fvARB(GL_TEXTURE0_ARB
+ i
, & tex_coords
[j
]);
103 glVertex2fv( & vtx_coords
[j
] );
112 static void Display( void )
115 static GLint Frames
= 0;
118 glClear( GL_COLOR_BUFFER_BIT
);
121 glRotatef(Xrot
, 1.0, 0.0, 0.0);
122 glRotatef(Yrot
, 0.0, 1.0, 0.0);
123 glRotatef(Zrot
, 0.0, 0.0, 1.0);
124 glScalef(5.0, 5.0, 5.0);
132 t
= glutGet(GLUT_ELAPSED_TIME
);
134 GLfloat seconds
= (t
- T0
) / 1000.0;
135 drift_increment
= 2.2 * seconds
/ Frames
;
142 static void Reshape( int width
, int height
)
144 glViewport( 0, 0, width
, height
);
145 glMatrixMode( GL_PROJECTION
);
147 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
148 /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
149 glMatrixMode( GL_MODELVIEW
);
151 glTranslatef( 0.0, 0.0, -70.0 );
155 static void ModeMenu(int entry
)
157 if (entry
>= TEX0
&& entry
<= TEX7
) {
159 GLint i
= entry
- TEX0
;
160 TexEnabled
[i
] = !TexEnabled
[i
];
161 glActiveTextureARB(GL_TEXTURE0_ARB
+ i
);
163 glEnable(GL_TEXTURE_2D
);
165 glDisable(GL_TEXTURE_2D
);
167 for (i
= 0; i
< NumUnits
; i
++)
168 printf("%d ", (int) TexEnabled
[i
]);
171 else if (entry
==ANIMATE
) {
174 else if (entry
==QUIT
) {
182 static void Key( unsigned char key
, int x
, int y
)
195 static void SpecialKey( int key
, int x
, int y
)
219 static void Init( int argc
, char *argv
[] )
224 const char *exten
= (const char *) glGetString(GL_EXTENSIONS
);
225 if (!strstr(exten
, "GL_ARB_multitexture")) {
226 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
230 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB
, &NumUnits
);
231 printf("%d texture units supported\n", NumUnits
);
235 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, &size
);
236 printf("%d x %d max texture size\n", size
, size
);
238 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
240 for (i
= 0; i
< NumUnits
; i
++) {
242 TexEnabled
[i
] = GL_TRUE
;
244 TexEnabled
[i
] = GL_FALSE
;
247 /* allocate two texture objects */
248 glGenTextures(NumUnits
, texObj
);
250 /* setup the texture objects */
251 for (i
= 0; i
< NumUnits
; i
++) {
253 glActiveTextureARB(GL_TEXTURE0_ARB
+ i
);
254 glBindTexture(GL_TEXTURE_2D
, texObj
[i
]);
256 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
257 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
260 if (!LoadRGBMipmaps(TEXTURE_1_FILE
, GL_RGB
)) {
261 printf("Error: couldn't load texture image\n");
266 if (!LoadRGBMipmaps(TEXTURE_2_FILE
, GL_RGB
)) {
267 printf("Error: couldn't load texture image\n");
273 GLubyte image
[8][8][3];
275 for (i
= 0; i
< 8; i
++) {
276 for (j
= 0; j
< 8; j
++) {
289 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, 8, 8, 0,
290 GL_RGB
, GL_UNSIGNED_BYTE
, (GLvoid
*) image
);
293 /* Bind texObj[i] to ith texture unit */
295 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
297 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_ADD
);
300 glEnable(GL_TEXTURE_2D
);
303 glShadeModel(GL_FLAT
);
304 glClearColor(0.3, 0.3, 0.4, 1.0);
306 if (argc
> 1 && strcmp(argv
[1], "-info")==0) {
307 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
308 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
309 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
310 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
315 int main( int argc
, char *argv
[] )
319 glutInit( &argc
, argv
);
320 glutInitWindowSize( 300, 300 );
321 glutInitWindowPosition( 0, 0 );
322 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
323 glutCreateWindow(argv
[0] );
327 glutReshapeFunc( Reshape
);
328 glutKeyboardFunc( Key
);
329 glutSpecialFunc( SpecialKey
);
330 glutDisplayFunc( Display
);
331 glutIdleFunc( Idle
);
333 glutCreateMenu(ModeMenu
);
335 for (i
= 0; i
< NumUnits
; i
++) {
337 sprintf(s
, "Toggle Texture %d", i
);
338 glutAddMenuEntry(s
, TEX0
+ i
);
340 glutAddMenuEntry("Toggle Animation", ANIMATE
);
341 glutAddMenuEntry("Quit", QUIT
);
342 glutAttachMenu(GLUT_RIGHT_BUTTON
);