9edb86e57524aea61ca6835412eb056c8ae2114b
[mesa.git] / progs / tests / bufferobj.c
1 /*
2 * Test GL_ARB_vertex_buffer_object
3 *
4 * Brian Paul
5 * 16 Sep 2003
6 */
7
8
9 #define GL_GLEXT_PROTOTYPES
10 #include <assert.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <math.h>
14 #include <GL/glut.h>
15
16 #define NUM_OBJECTS 10
17
18 struct object
19 {
20 GLuint VertexBufferID;
21 GLuint ColorBufferID;
22 GLuint ElementsBufferID;
23 GLuint NumVerts;
24 GLuint VertexOffset;
25 GLuint ColorOffset;
26 GLuint VertexStride;
27 GLuint ColorStride;
28 GLuint NumElements;
29 };
30
31 static struct object Objects[NUM_OBJECTS];
32 static GLuint NumObjects;
33
34 static GLuint Win;
35
36 static GLfloat Xrot = 0, Yrot = 0, Zrot = 0;
37 static GLboolean Anim = GL_TRUE;
38
39
40 static void CheckError(int line)
41 {
42 GLenum err = glGetError();
43 if (err) {
44 printf("GL Error 0x%x at line %d\n", (int) err, line);
45 }
46 }
47
48
49 static void DrawObject( const struct object *obj )
50 {
51 glBindBufferARB(GL_ARRAY_BUFFER_ARB, obj->VertexBufferID);
52 glVertexPointer(3, GL_FLOAT, obj->VertexStride, (void *) obj->VertexOffset);
53 glEnable(GL_VERTEX_ARRAY);
54
55 /* test push/pop attrib */
56 /* XXX this leads to a segfault with NVIDIA's 53.36 driver */
57 #if 0
58 if (1)
59 {
60 glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
61 /*glVertexPointer(3, GL_FLOAT, 0, (void *) (obj->VertexOffset + 10000));*/
62 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 999999);
63 glPopClientAttrib();
64 }
65 #endif
66 glBindBufferARB(GL_ARRAY_BUFFER_ARB, obj->ColorBufferID);
67 glColorPointer(3, GL_FLOAT, obj->ColorStride, (void *) obj->ColorOffset);
68 glEnable(GL_COLOR_ARRAY);
69
70 if (obj->NumElements > 0) {
71 /* indexed arrays */
72 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, obj->ElementsBufferID);
73 glDrawElements(GL_LINE_LOOP, obj->NumElements, GL_UNSIGNED_INT, NULL);
74 }
75 else {
76 /* non-indexed arrays */
77 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
78 glDrawArrays(GL_LINE_LOOP, 0, obj->NumVerts);
79 }
80 }
81
82
83 static void Idle( void )
84 {
85 Zrot = 0.05 * glutGet(GLUT_ELAPSED_TIME);
86 glutPostRedisplay();
87 }
88
89
90 static void Display( void )
91 {
92 int i;
93
94 glClear( GL_COLOR_BUFFER_BIT );
95
96 for (i = 0; i < NumObjects; i++) {
97 float x = 7.0 * ((float) i / (NumObjects-1) - 0.5);
98 glPushMatrix();
99 glTranslatef(x, 0, 0);
100 glRotatef(Xrot, 1, 0, 0);
101 glRotatef(Yrot, 0, 1, 0);
102 glRotatef(Zrot, 0, 0, 1);
103
104 DrawObject(Objects + i);
105
106 glPopMatrix();
107 }
108
109 CheckError(__LINE__);
110 glutSwapBuffers();
111 }
112
113
114 static void Reshape( int width, int height )
115 {
116 float ar = (float) width / (float) height;
117 glViewport( 0, 0, width, height );
118 glMatrixMode( GL_PROJECTION );
119 glLoadIdentity();
120 glFrustum( -ar, ar, -1.0, 1.0, 5.0, 25.0 );
121 glMatrixMode( GL_MODELVIEW );
122 glLoadIdentity();
123 glTranslatef( 0.0, 0.0, -15.0 );
124 }
125
126
127 static void FreeBuffers(void)
128 {
129 int i;
130 for (i = 0; i < NUM_OBJECTS; i++) {
131 glDeleteBuffersARB(1, &Objects[i].VertexBufferID);
132 glDeleteBuffersARB(1, &Objects[i].ColorBufferID);
133 glDeleteBuffersARB(1, &Objects[i].ElementsBufferID);
134 }
135 }
136
137
138 static void Key( unsigned char key, int x, int y )
139 {
140 const GLfloat step = 3.0;
141 (void) x;
142 (void) y;
143 switch (key) {
144 case 'a':
145 Anim = !Anim;
146 if (Anim)
147 glutIdleFunc(Idle);
148 else
149 glutIdleFunc(NULL);
150 break;
151 case 'z':
152 Zrot -= step;
153 break;
154 case 'Z':
155 Zrot += step;
156 break;
157 case 27:
158 FreeBuffers();
159 glutDestroyWindow(Win);
160 exit(0);
161 break;
162 }
163 glutPostRedisplay();
164 }
165
166
167 static void SpecialKey( int key, int x, int y )
168 {
169 const GLfloat step = 3.0;
170 (void) x;
171 (void) y;
172 switch (key) {
173 case GLUT_KEY_UP:
174 Xrot -= step;
175 break;
176 case GLUT_KEY_DOWN:
177 Xrot += step;
178 break;
179 case GLUT_KEY_LEFT:
180 Yrot -= step;
181 break;
182 case GLUT_KEY_RIGHT:
183 Yrot += step;
184 break;
185 }
186 glutPostRedisplay();
187 }
188
189
190 /*
191 * Non-interleaved position/color data.
192 */
193 static void MakeObject1(struct object *obj)
194 {
195 GLfloat *v, *c;
196 void *p;
197 int i;
198 GLubyte buffer[500];
199
200 for (i = 0; i < 500; i++)
201 buffer[i] = i & 0xff;
202
203 obj->VertexBufferID = 0;
204 glGenBuffersARB(1, &obj->VertexBufferID);
205 obj->ColorBufferID = obj->VertexBufferID;
206 assert(obj->VertexBufferID != 0);
207 glBindBufferARB(GL_ARRAY_BUFFER_ARB, obj->VertexBufferID);
208 glBufferDataARB(GL_ARRAY_BUFFER_ARB, 500, buffer, GL_STATIC_DRAW_ARB);
209
210 for (i = 0; i < 500; i++)
211 buffer[i] = 0;
212
213 glGetBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, 500, buffer);
214
215 for (i = 0; i < 500; i++)
216 assert(buffer[i] == (i & 0xff));
217
218 glGetBufferParameterivARB(GL_ARRAY_BUFFER_ARB, GL_BUFFER_MAPPED_ARB, &i);
219 assert(!i);
220
221 glGetBufferParameterivARB(GL_ARRAY_BUFFER_ARB, GL_BUFFER_USAGE_ARB, &i);
222
223 v = (GLfloat *) glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB);
224
225 /* do some sanity tests */
226 glGetBufferPointervARB(GL_ARRAY_BUFFER_ARB, GL_BUFFER_MAP_POINTER_ARB, &p);
227 assert(p == v);
228
229 glGetBufferParameterivARB(GL_ARRAY_BUFFER_ARB, GL_BUFFER_SIZE_ARB, &i);
230 assert(i == 500);
231
232 glGetBufferParameterivARB(GL_ARRAY_BUFFER_ARB, GL_BUFFER_USAGE_ARB, &i);
233 assert(i == GL_STATIC_DRAW_ARB);
234
235 glGetBufferParameterivARB(GL_ARRAY_BUFFER_ARB, GL_BUFFER_ACCESS_ARB, &i);
236 assert(i == GL_WRITE_ONLY_ARB);
237
238 glGetBufferParameterivARB(GL_ARRAY_BUFFER_ARB, GL_BUFFER_MAPPED_ARB, &i);
239 assert(i);
240
241 /* Make rectangle */
242 v[0] = -1; v[1] = -1; v[2] = 0;
243 v[3] = 1; v[4] = -1; v[5] = 0;
244 v[6] = 1; v[7] = 1; v[8] = 0;
245 v[9] = -1; v[10] = 1; v[11] = 0;
246 c = v + 12;
247 c[0] = 1; c[1] = 0; c[2] = 0;
248 c[3] = 1; c[4] = 0; c[5] = 0;
249 c[6] = 1; c[7] = 0; c[8] = 1;
250 c[9] = 1; c[10] = 0; c[11] = 1;
251 obj->NumVerts = 4;
252 obj->VertexOffset = 0;
253 obj->ColorOffset = 3 * sizeof(GLfloat) * obj->NumVerts;
254 obj->VertexStride = 0;
255 obj->ColorStride = 0;
256 obj->NumElements = 0;
257
258 glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
259
260 glGetBufferPointervARB(GL_ARRAY_BUFFER_ARB, GL_BUFFER_MAP_POINTER_ARB, &p);
261 assert(!p);
262
263 glGetBufferParameterivARB(GL_ARRAY_BUFFER_ARB, GL_BUFFER_MAPPED_ARB, &i);
264 assert(!i);
265 }
266
267
268 /*
269 * Interleaved position/color data.
270 */
271 static void MakeObject2(struct object *obj)
272 {
273 GLfloat *v;
274 int start = 40; /* bytes, to test non-zero array offsets */
275
276 glGenBuffersARB(1, &obj->VertexBufferID);
277 obj->ColorBufferID = obj->VertexBufferID;
278
279 glBindBufferARB(GL_ARRAY_BUFFER_ARB, obj->VertexBufferID);
280 glBufferDataARB(GL_ARRAY_BUFFER_ARB, 1000, NULL, GL_STATIC_DRAW_ARB);
281 v = (GLfloat *) glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB);
282
283 v += start / sizeof(GLfloat);
284
285 /* Make triangle: interleaved colors, then positions */
286 /* R G B X Y Z */
287 v[0] = 0; v[1] = 1; v[2] = 0; v[3] = -1; v[4] = -1; v[5] = 0;
288 v[6] = 0; v[7] = 1; v[8] = 0; v[9] = 1; v[10] = -1; v[11] = 0;
289 v[12] = 1; v[13] = 1; v[14] = 0; v[15] = 0; v[16] = 1; v[17] = 0;
290
291 obj->NumVerts = 3;
292 obj->VertexOffset = start + 3 * sizeof(GLfloat);
293 obj->ColorOffset = start;
294 obj->VertexStride = 6 * sizeof(GLfloat);
295 obj->ColorStride = 6 * sizeof(GLfloat);
296
297 obj->NumElements = 0;
298
299 glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
300 }
301
302
303 /*
304 * Use an index buffer and glDrawElements().
305 */
306 static void MakeObject3(struct object *obj)
307 {
308 GLfloat vertexData[1000];
309 GLfloat *v, *c;
310 GLuint *i;
311 int bytes;
312
313 /* Make rectangle */
314 v = vertexData;
315 v[0] = -1; v[1] = -0.5; v[2] = 0;
316 v[3] = 1; v[4] = -0.5; v[5] = 0;
317 v[6] = 1; v[7] = 0.5; v[8] = 0;
318 v[9] = -1; v[10] = 0.5; v[11] = 0;
319 c = vertexData + 12;
320 c[0] = 0; c[1] = 0; c[2] = 1;
321 c[3] = 0; c[4] = 0; c[5] = 1;
322 c[6] = 0; c[7] = 1; c[8] = 1;
323 c[9] = 0; c[10] = 1; c[11] = 1;
324 obj->NumVerts = 4;
325 obj->VertexOffset = 0;
326 obj->ColorOffset = 3 * sizeof(GLfloat) * obj->NumVerts;
327 obj->VertexStride = 0;
328 obj->ColorStride = 0;
329
330 bytes = obj->NumVerts * (3 + 3) * sizeof(GLfloat);
331
332 /* Don't use glMap/UnmapBuffer for this object */
333 glGenBuffersARB(1, &obj->VertexBufferID);
334 obj->ColorBufferID = obj->VertexBufferID;
335
336 glBindBufferARB(GL_ARRAY_BUFFER_ARB, obj->VertexBufferID);
337 glBufferDataARB(GL_ARRAY_BUFFER_ARB, bytes, vertexData, GL_STATIC_DRAW_ARB);
338
339 /* Setup a buffer of indices to test the ELEMENTS path */
340 glGenBuffersARB(1, &obj->ElementsBufferID);
341 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, obj->ElementsBufferID);
342 glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 100, NULL, GL_STATIC_DRAW_ARB);
343 i = (GLuint *) glMapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, GL_READ_WRITE_ARB);
344 i[0] = 0;
345 i[1] = 1;
346 i[2] = 2;
347 i[3] = 3;
348 glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB);
349 obj->NumElements = 4;
350 }
351
352
353 /*
354 * Vertex and color data in different buffers.
355 */
356 static void MakeObject4(struct object *obj)
357 {
358 static const GLfloat vertexData[] = {
359 0, -1, 0,
360 0.5, 0, 0,
361 0, 1, 0,
362 -0.5, 0, 0
363 };
364 static const GLfloat colorData[] = {
365 1, 1, 1,
366 1, 1, 0,
367 .5, .5, 0,
368 1, 1, 0
369 };
370
371 obj->VertexOffset = 0;
372 obj->VertexStride = 0;
373 obj->ColorOffset = 0;
374 obj->ColorStride = 0;
375 obj->NumVerts = 4;
376
377 glGenBuffersARB(1, &obj->VertexBufferID);
378 glBindBufferARB(GL_ARRAY_BUFFER_ARB, obj->VertexBufferID);
379 glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(vertexData), vertexData,
380 GL_STATIC_DRAW_ARB);
381
382 glGenBuffersARB(1, &obj->ColorBufferID);
383 glBindBufferARB(GL_ARRAY_BUFFER_ARB, obj->ColorBufferID);
384 glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(colorData), colorData,
385 GL_STATIC_DRAW_ARB);
386
387 /* Setup a buffer of indices to test the ELEMENTS path */
388 obj->ElementsBufferID = 0;
389 obj->NumElements = 0;
390 }
391
392
393
394 static void Init( void )
395 {
396 if (!glutExtensionSupported("GL_ARB_vertex_buffer_object")) {
397 printf("GL_ARB_vertex_buffer_object not found!\n");
398 exit(0);
399 }
400 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
401
402 /* Test buffer object deletion */
403 if (1) {
404 static GLubyte data[1000];
405 GLuint id = 999;
406 glBindBufferARB(GL_ARRAY_BUFFER_ARB, id);
407 glBufferDataARB(GL_ARRAY_BUFFER_ARB, 1000, data, GL_STATIC_DRAW_ARB);
408 glVertexPointer(3, GL_FLOAT, 0, (void *) 0);
409 glDeleteBuffersARB(1, &id);
410 assert(!glIsBufferARB(id));
411 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
412 glVertexPointer(3, GL_FLOAT, 0, (void *) 0);
413 assert(!glIsBufferARB(id));
414 }
415
416 MakeObject1(Objects + 0);
417 MakeObject2(Objects + 1);
418 MakeObject3(Objects + 2);
419 MakeObject4(Objects + 3);
420 NumObjects = 4;
421 }
422
423
424 int main( int argc, char *argv[] )
425 {
426 glutInit( &argc, argv );
427 glutInitWindowPosition( 0, 0 );
428 glutInitWindowSize( 600, 300 );
429 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
430 Win = glutCreateWindow(argv[0]);
431 glutReshapeFunc( Reshape );
432 glutKeyboardFunc( Key );
433 glutSpecialFunc( SpecialKey );
434 glutDisplayFunc( Display );
435 if (Anim)
436 glutIdleFunc(Idle);
437 Init();
438 glutMainLoop();
439 return 0;
440 }