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.7KB

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