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