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.
13 #define GL_GLEXT_PROTOTYPES
28 static GLboolean Animate
= GL_TRUE
;
29 static GLboolean use_pbo
= 1;
30 static GLboolean whole_rect
= 1;
32 static GLfloat Drift
= 0.0;
33 static GLfloat drift_increment
= 1/255.0;
34 static GLfloat Xrot
= 20.0, Yrot
= 30.0;
36 static GLuint Width
= 1024;
37 static GLuint Height
= 512;
40 static void Idle( void )
44 Drift
+= drift_increment
;
52 static int max( int a
, int b
) { return a
> b
? a
: b
; }
53 static int min( int a
, int b
) { return a
< b
? a
: b
; }
55 static void DrawObject()
57 GLint size
= Width
* Height
* 4;
60 /* XXX: This is extremely important - semantically makes the buffer
61 * contents undefined, but in practice means that the driver can
62 * release the old copy of the texture and allocate a new one
63 * without waiting for outstanding rendering to complete.
65 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT
, size
, NULL
, GL_STREAM_DRAW_ARB
);
68 char *image
= glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
, GL_WRITE_ONLY_ARB
);
70 printf("char %d\n", (unsigned char)(Drift
* 255));
72 memset(image
, size
, (unsigned char)(Drift
* 255));
74 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
);
78 /* BGRA is required for most hardware paths:
80 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB
, 0, GL_RGBA
, Width
, Height
, 0,
81 GL_BGRA
, GL_UNSIGNED_BYTE
, NULL
);
84 static char *image
= NULL
;
89 memset(image
, size
, (unsigned char)(Drift
* 255));
91 /* BGRA should be the fast path for regular uploads as well.
93 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB
, 0, GL_RGBA
, Width
, Height
, 0,
94 GL_BGRA
, GL_UNSIGNED_BYTE
, image
);
116 glTexCoord2f( x
, y
+ h
);
117 glVertex2f( x
, y
+ h
);
119 glTexCoord2f( x
+ w
+ .5, y
+ h
);
120 glVertex2f( x
+ w
, y
+ h
);
122 glTexCoord2f( x
+ w
, y
+ .5);
123 glVertex2f( x
+ w
, y
);
131 static void Display( void )
134 static GLint Frames
= 0;
137 glClear( GL_COLOR_BUFFER_BIT
);
147 t
= glutGet(GLUT_ELAPSED_TIME
);
148 if (t
- T0
>= 1000) {
149 GLfloat seconds
= (t
- T0
) / 1000.0;
151 GLfloat fps
= Frames
/ seconds
;
152 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames
, seconds
, fps
);
154 drift_increment
= 2.2 * seconds
/ Frames
;
161 static void Reshape( int width
, int height
)
163 glViewport( 0, 0, width
, height
);
164 glMatrixMode( GL_PROJECTION
);
166 /* glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 ); */
167 gluOrtho2D( 0, width
, height
, 0 );
168 glMatrixMode( GL_MODELVIEW
);
170 glTranslatef(0.375, 0.375, 0);
174 static void ModeMenu(int entry
)
176 if (entry
==ANIMATE
) {
179 else if (entry
==PBO
) {
182 else if (entry
==QUIT
) {
190 static void Key( unsigned char key
, int x
, int y
)
203 static void SpecialKey( int key
, int x
, int y
)
227 static void Init( int argc
, char *argv
[] )
229 const char *exten
= (const char *) glGetString(GL_EXTENSIONS
);
230 GLuint texObj
, DrawPBO
;
234 if (!strstr(exten
, "GL_ARB_multitexture")) {
235 printf("Sorry, GL_ARB_multitexture not supported by this renderer.\n");
239 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, &size
);
240 printf("%d x %d max texture size\n", size
, size
);
242 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
244 /* allocate two texture objects */
245 glGenTextures(1, &texObj
);
247 /* setup the texture objects */
248 glActiveTextureARB(GL_TEXTURE0_ARB
);
249 glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, texObj
);
251 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
252 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
254 glGenBuffersARB(1, &DrawPBO
);
256 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
, DrawPBO
);
257 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT
,
258 Width
* Height
* 4, NULL
, GL_STREAM_DRAW
);
260 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
262 glEnable(GL_TEXTURE_RECTANGLE_ARB
);
264 glShadeModel(GL_SMOOTH
);
265 glClearColor(0.3, 0.3, 0.4, 1.0);
267 if (argc
> 1 && strcmp(argv
[1], "-info")==0) {
268 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
269 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
270 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
271 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
276 int main( int argc
, char *argv
[] )
280 glutInit( &argc
, argv
);
282 for (i
= 1; i
< argc
; i
++) {
283 if (strcmp(argv
[i
], "-w") == 0) {
284 Width
= atoi(argv
[i
+1]);
286 printf("Error, bad width\n");
291 else if (strcmp(argv
[i
], "-h") == 0) {
292 Height
= atoi(argv
[i
+1]);
294 printf("Error, bad height\n");
301 glutInitWindowSize( Width
, Height
);
302 glutInitWindowPosition( 0, 0 );
303 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
304 glutCreateWindow(argv
[0] );
308 glutReshapeFunc( Reshape
);
309 glutKeyboardFunc( Key
);
310 glutSpecialFunc( SpecialKey
);
311 glutDisplayFunc( Display
);
312 glutIdleFunc( Idle
);
314 glutCreateMenu(ModeMenu
);
315 glutAddMenuEntry("Toggle Animation", ANIMATE
);
316 glutAddMenuEntry("Toggle PBO", PBO
);
317 glutAddMenuEntry("Quit", QUIT
);
318 glutAttachMenu(GLUT_RIGHT_BUTTON
);