Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bufferobj.c 8.9KB

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