test GL_ARB_vertex_buffer_object
[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 NumVerts;
22 GLuint VertexOffset;
23 GLuint ColorOffset;
24 };
25
26 static struct object Objects[NUM_OBJECTS];
27 static GLuint NumObjects;
28
29 static GLfloat Xrot = 0, Yrot = 0, Zrot = 0;
30 static GLboolean Anim = GL_TRUE;
31
32
33 static void CheckError(int line)
34 {
35 GLenum err = glGetError();
36 if (err) {
37 printf("GL Error %d at line %d\n", (int) err, line);
38 }
39 }
40
41
42 static void DrawObject( const struct object *obj )
43 {
44 glBindBufferARB(GL_ARRAY_BUFFER_ARB, obj->BufferID);
45 glVertexPointer(3, GL_FLOAT, 0, (void *) obj->VertexOffset);
46 glEnable(GL_VERTEX_ARRAY);
47 glColorPointer(3, GL_FLOAT, 0, (void *) obj->ColorOffset);
48 glEnable(GL_COLOR_ARRAY);
49 glDrawArrays(GL_LINE_LOOP, 0, obj->NumVerts);
50 }
51
52
53 static void Idle( void )
54 {
55 Zrot = 0.05 * glutGet(GLUT_ELAPSED_TIME);
56 glutPostRedisplay();
57 }
58
59
60 static void Display( void )
61 {
62 int i;
63
64 glClear( GL_COLOR_BUFFER_BIT );
65
66 for (i = 0; i < NumObjects; i++) {
67 float x = 5.0 * ((float) i / (NumObjects-1) - 0.5);
68 glPushMatrix();
69 glTranslatef(x, 0, 0);
70 glRotatef(Xrot, 1, 0, 0);
71 glRotatef(Yrot, 0, 1, 0);
72 glRotatef(Zrot, 0, 0, 1);
73
74 DrawObject(Objects + i);
75
76 glPopMatrix();
77 }
78
79 CheckError(__LINE__);
80 glutSwapBuffers();
81 }
82
83
84 static void Reshape( int width, int height )
85 {
86 float ar = (float) width / (float) height;
87 glViewport( 0, 0, width, height );
88 glMatrixMode( GL_PROJECTION );
89 glLoadIdentity();
90 glFrustum( -ar, ar, -1.0, 1.0, 5.0, 25.0 );
91 glMatrixMode( GL_MODELVIEW );
92 glLoadIdentity();
93 glTranslatef( 0.0, 0.0, -15.0 );
94 }
95
96
97 static void Key( unsigned char key, int x, int y )
98 {
99 const GLfloat step = 3.0;
100 (void) x;
101 (void) y;
102 switch (key) {
103 case 'a':
104 Anim = !Anim;
105 if (Anim)
106 glutIdleFunc(Idle);
107 else
108 glutIdleFunc(NULL);
109 break;
110 case 'z':
111 Zrot -= step;
112 break;
113 case 'Z':
114 Zrot += step;
115 break;
116 case 27:
117 exit(0);
118 break;
119 }
120 glutPostRedisplay();
121 }
122
123
124 static void SpecialKey( int key, int x, int y )
125 {
126 const GLfloat step = 3.0;
127 (void) x;
128 (void) y;
129 switch (key) {
130 case GLUT_KEY_UP:
131 Xrot -= step;
132 break;
133 case GLUT_KEY_DOWN:
134 Xrot += step;
135 break;
136 case GLUT_KEY_LEFT:
137 Yrot -= step;
138 break;
139 case GLUT_KEY_RIGHT:
140 Yrot += step;
141 break;
142 }
143 glutPostRedisplay();
144 }
145
146
147
148 static void MakeObject1(struct object *obj)
149 {
150 GLfloat *v, *c;
151
152 glGenBuffersARB(1, &obj->BufferID);
153 glBindBufferARB(GL_ARRAY_BUFFER_ARB, obj->BufferID);
154 glBufferDataARB(GL_ARRAY_BUFFER_ARB, 1000, NULL, GL_STATIC_DRAW_ARB);
155 v = (GLfloat *) glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_READ_WRITE_ARB);
156
157 /* Make rectangle */
158 v[0] = -1; v[1] = -1; v[2] = 0;
159 v[3] = 1; v[4] = -1; v[5] = 0;
160 v[6] = 1; v[7] = 1; v[8] = 0;
161 v[9] = -1; v[10] = 1; v[11] = 0;
162 c = v + 12;
163 c[0] = 1; c[1] = 0; c[2] = 0;
164 c[3] = 1; c[4] = 0; c[5] = 0;
165 c[6] = 1; c[7] = 0; c[8] = 1;
166 c[9] = 1; c[10] = 0; c[11] = 1;
167 obj->NumVerts = 4;
168 obj->VertexOffset = 0;
169 obj->ColorOffset = 3 * sizeof(GLfloat) * obj->NumVerts;
170
171 glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
172 }
173
174
175 static void MakeObject2(struct object *obj)
176 {
177 GLfloat *v, *c;
178
179 glGenBuffersARB(1, &obj->BufferID);
180 glBindBufferARB(GL_ARRAY_BUFFER_ARB, obj->BufferID);
181 glBufferDataARB(GL_ARRAY_BUFFER_ARB, 1000, NULL, GL_STATIC_DRAW_ARB);
182 v = (GLfloat *) glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_READ_WRITE_ARB);
183
184 /* Make triangle */
185 v[0] = -1; v[1] = -1; v[2] = 0;
186 v[3] = 1; v[4] = -1; v[5] = 0;
187 v[6] = 0; v[7] = 1; v[8] = 0;
188 c = v + 9;
189 c[0] = 0; c[1] = 1; c[2] = 0;
190 c[3] = 0; c[4] = 1; c[5] = 0;
191 c[6] = 1; c[7] = 1; c[8] = 0;
192 obj->NumVerts = 3;
193 obj->VertexOffset = 0;
194 obj->ColorOffset = 3 * sizeof(GLfloat) * obj->NumVerts;
195
196 glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
197 }
198
199
200 static void MakeObject3(struct object *obj)
201 {
202 GLfloat *v, *c;
203
204 glGenBuffersARB(1, &obj->BufferID);
205 glBindBufferARB(GL_ARRAY_BUFFER_ARB, obj->BufferID);
206 glBufferDataARB(GL_ARRAY_BUFFER_ARB, 1000, NULL, GL_STATIC_DRAW_ARB);
207 v = (GLfloat *) glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_READ_WRITE_ARB);
208
209 /* Make rectangle */
210 v[0] = -1; v[1] = -0.5; v[2] = 0;
211 v[3] = 1; v[4] = -0.5; v[5] = 0;
212 v[6] = 1; v[7] = 0.5; v[8] = 0;
213 v[9] = -1; v[10] = 0.5; v[11] = 0;
214 c = v + 12;
215 c[0] = 0; c[1] = 0; c[2] = 1;
216 c[3] = 0; c[4] = 0; c[5] = 1;
217 c[6] = 0; c[7] = 1; c[8] = 1;
218 c[9] = 0; c[10] = 1; c[11] = 1;
219 obj->NumVerts = 4;
220 obj->VertexOffset = 0;
221 obj->ColorOffset = 3 * sizeof(GLfloat) * obj->NumVerts;
222
223 glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
224 }
225
226
227
228 static void Init( void )
229 {
230 if (!glutExtensionSupported("GL_ARB_vertex_buffer_object")) {
231 printf("GL_ARB_vertex_buffer_object not found!\n");
232 exit(0);
233 }
234 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
235
236 MakeObject1(Objects + 0);
237 MakeObject2(Objects + 1);
238 MakeObject3(Objects + 2);
239 NumObjects = 3;
240 }
241
242
243 int main( int argc, char *argv[] )
244 {
245 glutInit( &argc, argv );
246 glutInitWindowPosition( 0, 0 );
247 glutInitWindowSize( 600, 300 );
248 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
249 glutCreateWindow(argv[0]);
250 glutReshapeFunc( Reshape );
251 glutKeyboardFunc( Key );
252 glutSpecialFunc( SpecialKey );
253 glutDisplayFunc( Display );
254 if (Anim)
255 glutIdleFunc(Idle);
256 Init();
257 glutMainLoop();
258 return 0;
259 }