Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Test GL_ARB_vertex_buffer_object |
| 3 | * |
| 4 | * Brian Paul |
| 5 | * 16 Sep 2003 |
| 6 | */ |
| 7 | |
| 8 | |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 9 | #include <assert.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <math.h> |
Keith Whitwell | a58065d | 2009-03-10 13:11:23 +0000 | [diff] [blame] | 13 | #include <GL/glew.h> |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 14 | #include <GL/glut.h> |
| 15 | |
| 16 | #define NUM_OBJECTS 10 |
| 17 | |
| 18 | struct object |
| 19 | { |
Brian Paul | c710077 | 2009-02-26 14:51:41 -0700 | [diff] [blame] | 20 | GLuint VertexBufferID; |
| 21 | GLuint ColorBufferID; |
Brian Paul | de8d410 | 2003-09-17 18:15:47 +0000 | [diff] [blame] | 22 | GLuint ElementsBufferID; |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 23 | GLuint NumVerts; |
| 24 | GLuint VertexOffset; |
| 25 | GLuint ColorOffset; |
Brian Paul | dd22f6f | 2009-02-03 15:17:26 -0700 | [diff] [blame] | 26 | GLuint VertexStride; |
| 27 | GLuint ColorStride; |
Brian Paul | de8d410 | 2003-09-17 18:15:47 +0000 | [diff] [blame] | 28 | GLuint NumElements; |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | static struct object Objects[NUM_OBJECTS]; |
| 32 | static GLuint NumObjects; |
| 33 | |
Brian Paul | 0c46a12 | 2006-08-25 22:13:08 +0000 | [diff] [blame] | 34 | static GLuint Win; |
| 35 | |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 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) { |
Brian Paul | 47a6749 | 2004-08-10 15:34:51 +0000 | [diff] [blame] | 44 | printf("GL Error 0x%x at line %d\n", (int) err, line); |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | |
| 49 | static void DrawObject( const struct object *obj ) |
| 50 | { |
Brian Paul | c710077 | 2009-02-26 14:51:41 -0700 | [diff] [blame] | 51 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, obj->VertexBufferID); |
Brian Paul | dd22f6f | 2009-02-03 15:17:26 -0700 | [diff] [blame] | 52 | glVertexPointer(3, GL_FLOAT, obj->VertexStride, (void *) obj->VertexOffset); |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 53 | glEnable(GL_VERTEX_ARRAY); |
Brian Paul | b87c5d0 | 2004-03-22 16:28:02 +0000 | [diff] [blame] | 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 |
Brian Paul | c710077 | 2009-02-26 14:51:41 -0700 | [diff] [blame] | 66 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, obj->ColorBufferID); |
Brian Paul | dd22f6f | 2009-02-03 15:17:26 -0700 | [diff] [blame] | 67 | glColorPointer(3, GL_FLOAT, obj->ColorStride, (void *) obj->ColorOffset); |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 68 | glEnable(GL_COLOR_ARRAY); |
Brian Paul | b87c5d0 | 2004-03-22 16:28:02 +0000 | [diff] [blame] | 69 | |
Brian Paul | de8d410 | 2003-09-17 18:15:47 +0000 | [diff] [blame] | 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 | } |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 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++) { |
Brian Paul | c710077 | 2009-02-26 14:51:41 -0700 | [diff] [blame] | 97 | float x = 7.0 * ((float) i / (NumObjects-1) - 0.5); |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 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 | |
Brian Paul | 0c46a12 | 2006-08-25 22:13:08 +0000 | [diff] [blame] | 127 | static void FreeBuffers(void) |
| 128 | { |
| 129 | int i; |
Brian Paul | c710077 | 2009-02-26 14:51:41 -0700 | [diff] [blame] | 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 | } |
Brian Paul | 0c46a12 | 2006-08-25 22:13:08 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 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: |
Brian Paul | 0c46a12 | 2006-08-25 22:13:08 +0000 | [diff] [blame] | 158 | FreeBuffers(); |
| 159 | glutDestroyWindow(Win); |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 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 | |
Brian Paul | c710077 | 2009-02-26 14:51:41 -0700 | [diff] [blame] | 190 | /* |
| 191 | * Non-interleaved position/color data. |
| 192 | */ |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 193 | static void MakeObject1(struct object *obj) |
| 194 | { |
| 195 | GLfloat *v, *c; |
Brian Paul | 4524274 | 2003-10-14 15:49:12 +0000 | [diff] [blame] | 196 | void *p; |
| 197 | int i; |
Brian Paul | 35b52c7 | 2003-10-21 14:56:53 +0000 | [diff] [blame] | 198 | GLubyte buffer[500]; |
| 199 | |
| 200 | for (i = 0; i < 500; i++) |
| 201 | buffer[i] = i & 0xff; |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 202 | |
Brian Paul | c710077 | 2009-02-26 14:51:41 -0700 | [diff] [blame] | 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); |
Brian Paul | 35b52c7 | 2003-10-21 14:56:53 +0000 | [diff] [blame] | 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 | |
Brian Paul | 1ceeac2 | 2003-09-17 18:20:52 +0000 | [diff] [blame] | 223 | v = (GLfloat *) glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB); |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 224 | |
Brian Paul | 4524274 | 2003-10-14 15:49:12 +0000 | [diff] [blame] | 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); |
Brian Paul | 35b52c7 | 2003-10-21 14:56:53 +0000 | [diff] [blame] | 230 | assert(i == 500); |
Brian Paul | 4524274 | 2003-10-14 15:49:12 +0000 | [diff] [blame] | 231 | |
| 232 | glGetBufferParameterivARB(GL_ARRAY_BUFFER_ARB, GL_BUFFER_USAGE_ARB, &i); |
| 233 | assert(i == GL_STATIC_DRAW_ARB); |
| 234 | |
Brian Paul | 35b52c7 | 2003-10-21 14:56:53 +0000 | [diff] [blame] | 235 | glGetBufferParameterivARB(GL_ARRAY_BUFFER_ARB, GL_BUFFER_ACCESS_ARB, &i); |
| 236 | assert(i == GL_WRITE_ONLY_ARB); |
| 237 | |
Brian Paul | 4524274 | 2003-10-14 15:49:12 +0000 | [diff] [blame] | 238 | glGetBufferParameterivARB(GL_ARRAY_BUFFER_ARB, GL_BUFFER_MAPPED_ARB, &i); |
| 239 | assert(i); |
| 240 | |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 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; |
Brian Paul | dd22f6f | 2009-02-03 15:17:26 -0700 | [diff] [blame] | 254 | obj->VertexStride = 0; |
| 255 | obj->ColorStride = 0; |
Brian Paul | de8d410 | 2003-09-17 18:15:47 +0000 | [diff] [blame] | 256 | obj->NumElements = 0; |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 257 | |
| 258 | glUnmapBufferARB(GL_ARRAY_BUFFER_ARB); |
Brian Paul | 4524274 | 2003-10-14 15:49:12 +0000 | [diff] [blame] | 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); |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | |
Brian Paul | c710077 | 2009-02-26 14:51:41 -0700 | [diff] [blame] | 268 | /* |
| 269 | * Interleaved position/color data. |
| 270 | */ |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 271 | static void MakeObject2(struct object *obj) |
| 272 | { |
Brian Paul | dd22f6f | 2009-02-03 15:17:26 -0700 | [diff] [blame] | 273 | GLfloat *v; |
| 274 | int start = 40; /* bytes, to test non-zero array offsets */ |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 275 | |
Brian Paul | c710077 | 2009-02-26 14:51:41 -0700 | [diff] [blame] | 276 | glGenBuffersARB(1, &obj->VertexBufferID); |
| 277 | obj->ColorBufferID = obj->VertexBufferID; |
| 278 | |
| 279 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, obj->VertexBufferID); |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 280 | glBufferDataARB(GL_ARRAY_BUFFER_ARB, 1000, NULL, GL_STATIC_DRAW_ARB); |
Brian Paul | 1ceeac2 | 2003-09-17 18:20:52 +0000 | [diff] [blame] | 281 | v = (GLfloat *) glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB); |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 282 | |
Brian Paul | dd22f6f | 2009-02-03 15:17:26 -0700 | [diff] [blame] | 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 | |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 291 | obj->NumVerts = 3; |
Brian Paul | dd22f6f | 2009-02-03 15:17:26 -0700 | [diff] [blame] | 292 | obj->VertexOffset = start + 3 * sizeof(GLfloat); |
| 293 | obj->ColorOffset = start; |
| 294 | obj->VertexStride = 6 * sizeof(GLfloat); |
| 295 | obj->ColorStride = 6 * sizeof(GLfloat); |
| 296 | |
Brian Paul | de8d410 | 2003-09-17 18:15:47 +0000 | [diff] [blame] | 297 | obj->NumElements = 0; |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 298 | |
| 299 | glUnmapBufferARB(GL_ARRAY_BUFFER_ARB); |
| 300 | } |
| 301 | |
| 302 | |
Brian Paul | c710077 | 2009-02-26 14:51:41 -0700 | [diff] [blame] | 303 | /* |
| 304 | * Use an index buffer and glDrawElements(). |
| 305 | */ |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 306 | static void MakeObject3(struct object *obj) |
| 307 | { |
Brian Paul | 4524274 | 2003-10-14 15:49:12 +0000 | [diff] [blame] | 308 | GLfloat vertexData[1000]; |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 309 | GLfloat *v, *c; |
Brian Paul | de8d410 | 2003-09-17 18:15:47 +0000 | [diff] [blame] | 310 | GLuint *i; |
Brian Paul | 4524274 | 2003-10-14 15:49:12 +0000 | [diff] [blame] | 311 | int bytes; |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 312 | |
| 313 | /* Make rectangle */ |
Brian Paul | 4524274 | 2003-10-14 15:49:12 +0000 | [diff] [blame] | 314 | v = vertexData; |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 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; |
Brian Paul | 4524274 | 2003-10-14 15:49:12 +0000 | [diff] [blame] | 319 | c = vertexData + 12; |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 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; |
Brian Paul | dd22f6f | 2009-02-03 15:17:26 -0700 | [diff] [blame] | 327 | obj->VertexStride = 0; |
| 328 | obj->ColorStride = 0; |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 329 | |
Brian Paul | 4524274 | 2003-10-14 15:49:12 +0000 | [diff] [blame] | 330 | bytes = obj->NumVerts * (3 + 3) * sizeof(GLfloat); |
| 331 | |
| 332 | /* Don't use glMap/UnmapBuffer for this object */ |
Brian Paul | c710077 | 2009-02-26 14:51:41 -0700 | [diff] [blame] | 333 | glGenBuffersARB(1, &obj->VertexBufferID); |
| 334 | obj->ColorBufferID = obj->VertexBufferID; |
| 335 | |
| 336 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, obj->VertexBufferID); |
Brian Paul | 4524274 | 2003-10-14 15:49:12 +0000 | [diff] [blame] | 337 | glBufferDataARB(GL_ARRAY_BUFFER_ARB, bytes, vertexData, GL_STATIC_DRAW_ARB); |
Brian Paul | de8d410 | 2003-09-17 18:15:47 +0000 | [diff] [blame] | 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; |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
Brian Paul | c710077 | 2009-02-26 14:51:41 -0700 | [diff] [blame] | 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 | |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 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 | |
Brian Paul | b87c5d0 | 2004-03-22 16:28:02 +0000 | [diff] [blame] | 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); |
Brian Paul | 5c6b391 | 2005-01-24 15:41:08 +0000 | [diff] [blame] | 410 | assert(!glIsBufferARB(id)); |
Brian Paul | b87c5d0 | 2004-03-22 16:28:02 +0000 | [diff] [blame] | 411 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); |
| 412 | glVertexPointer(3, GL_FLOAT, 0, (void *) 0); |
Brian Paul | 5c6b391 | 2005-01-24 15:41:08 +0000 | [diff] [blame] | 413 | assert(!glIsBufferARB(id)); |
Brian Paul | b87c5d0 | 2004-03-22 16:28:02 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 416 | MakeObject1(Objects + 0); |
| 417 | MakeObject2(Objects + 1); |
| 418 | MakeObject3(Objects + 2); |
Brian Paul | c710077 | 2009-02-26 14:51:41 -0700 | [diff] [blame] | 419 | MakeObject4(Objects + 3); |
| 420 | NumObjects = 4; |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 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 ); |
Brian Paul | 0c46a12 | 2006-08-25 22:13:08 +0000 | [diff] [blame] | 430 | Win = glutCreateWindow(argv[0]); |
Keith Whitwell | a58065d | 2009-03-10 13:11:23 +0000 | [diff] [blame] | 431 | glewInit(); |
Brian Paul | 6bced01 | 2003-09-17 16:27:07 +0000 | [diff] [blame] | 432 | glutReshapeFunc( Reshape ); |
| 433 | glutKeyboardFunc( Key ); |
| 434 | glutSpecialFunc( SpecialKey ); |
| 435 | glutDisplayFunc( Display ); |
| 436 | if (Anim) |
| 437 | glutIdleFunc(Idle); |
| 438 | Init(); |
| 439 | glutMainLoop(); |
| 440 | return 0; |
| 441 | } |