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.

vao_demo.c 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * (C) Copyright IBM Corporation 2006
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * on the rights to use, copy, modify, merge, publish, distribute, sub
  9. * license, and/or sell copies of the Software, and to permit persons to whom
  10. * the Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  19. * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <assert.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <math.h>
  28. #ifdef __darwin__
  29. #include <GLUT/glut.h>
  30. typedef void (* PFNGLBINDVERTEXARRAYAPPLEPROC) (GLuint array);
  31. typedef void (* PFNGLDELETEVERTEXARRAYSAPPLEPROC) (GLsizei n, const GLuint *arrays);
  32. typedef void (* PFNGLGENVERTEXARRAYSAPPLEPROC) (GLsizei n, const GLuint *arrays);
  33. typedef GLboolean (* PFNGLISVERTEXARRAYAPPLEPROC) (GLuint array);
  34. #else
  35. #include <GL/glut.h>
  36. #endif
  37. static PFNGLBINDVERTEXARRAYAPPLEPROC bind_vertex_array = NULL;
  38. static PFNGLGENVERTEXARRAYSAPPLEPROC gen_vertex_arrays = NULL;
  39. static PFNGLDELETEVERTEXARRAYSAPPLEPROC delete_vertex_arrays = NULL;
  40. static PFNGLISVERTEXARRAYAPPLEPROC is_vertex_array = NULL;
  41. static int Width = 400;
  42. static int Height = 200;
  43. static int Win = 0;
  44. static const GLfloat Near = 5.0, Far = 25.0;
  45. static GLfloat angle = 0.0;
  46. static GLuint cube_array_obj = 0;
  47. static GLuint oct_array_obj = 0;
  48. static const GLfloat cube_vert[] = {
  49. -0.5, -0.5, -0.5, 1.0,
  50. 0.5, -0.5, -0.5, 1.0,
  51. 0.5, 0.5, -0.5, 1.0,
  52. -0.5, 0.5, -0.5, 1.0,
  53. -0.5, -0.5, 0.5, 1.0,
  54. 0.5, -0.5, 0.5, 1.0,
  55. 0.5, 0.5, 0.5, 1.0,
  56. -0.5, 0.5, 0.5, 1.0,
  57. -0.5, 0.5, -0.5, 1.0,
  58. 0.5, 0.5, -0.5, 1.0,
  59. 0.5, 0.5, 0.5, 1.0,
  60. -0.5, 0.5, 0.5, 1.0,
  61. -0.5, -0.5, -0.5, 1.0,
  62. 0.5, -0.5, -0.5, 1.0,
  63. 0.5, -0.5, 0.5, 1.0,
  64. -0.5, -0.5, 0.5, 1.0,
  65. 0.5, -0.5, -0.5, 1.0,
  66. 0.5, -0.5, 0.5, 1.0,
  67. 0.5, 0.5, 0.5, 1.0,
  68. 0.5, 0.5, -0.5, 1.0,
  69. -0.5, -0.5, -0.5, 1.0,
  70. -0.5, -0.5, 0.5, 1.0,
  71. -0.5, 0.5, 0.5, 1.0,
  72. -0.5, 0.5, -0.5, 1.0,
  73. };
  74. static const GLfloat cube_color[] = {
  75. 1.0, 0.0, 0.0, 1.0,
  76. 1.0, 0.0, 0.0, 1.0,
  77. 1.0, 0.0, 0.0, 1.0,
  78. 1.0, 0.0, 0.0, 1.0,
  79. 0.0, 1.0, 0.0, 1.0,
  80. 0.0, 1.0, 0.0, 1.0,
  81. 0.0, 1.0, 0.0, 1.0,
  82. 0.0, 1.0, 0.0, 1.0,
  83. 0.0, 0.0, 1.0, 1.0,
  84. 0.0, 0.0, 1.0, 1.0,
  85. 0.0, 0.0, 1.0, 1.0,
  86. 0.0, 0.0, 1.0, 1.0,
  87. 1.0, 0.0, 1.0, 1.0,
  88. 1.0, 0.0, 1.0, 1.0,
  89. 1.0, 0.0, 1.0, 1.0,
  90. 1.0, 0.0, 1.0, 1.0,
  91. 1.0, 1.0, 1.0, 1.0,
  92. 1.0, 1.0, 1.0, 1.0,
  93. 1.0, 1.0, 1.0, 1.0,
  94. 1.0, 1.0, 1.0, 1.0,
  95. 0.5, 0.5, 0.5, 1.0,
  96. 0.5, 0.5, 0.5, 1.0,
  97. 0.5, 0.5, 0.5, 1.0,
  98. 0.5, 0.5, 0.5, 1.0,
  99. };
  100. static const GLfloat oct_vert[] = {
  101. 0.0, 0.0, 0.7071, 1.0,
  102. 0.5, 0.5, 0.0, 1.0,
  103. -0.5, 0.5, 0.0, 1.0,
  104. 0.0, 0.0, 0.7071, 1.0,
  105. 0.5, -0.5, 0.0, 1.0,
  106. -0.5, -0.5, 0.0, 1.0,
  107. 0.0, 0.0, 0.7071, 1.0,
  108. -0.5, -0.5, 0.0, 1.0,
  109. -0.5, 0.5, 0.0, 1.0,
  110. 0.0, 0.0, 0.7071, 1.0,
  111. 0.5, 0.5, 0.0, 1.0,
  112. 0.5, -0.5, 0.0, 1.0,
  113. 0.0, 0.0, -0.7071, 1.0,
  114. 0.5, 0.5, 0.0, 1.0,
  115. -0.5, 0.5, 0.0, 1.0,
  116. 0.0, 0.0, -0.7071, 1.0,
  117. 0.5, -0.5, 0.0, 1.0,
  118. -0.5, -0.5, 0.0, 1.0,
  119. 0.0, 0.0, -0.7071, 1.0,
  120. -0.5, -0.5, 0.0, 1.0,
  121. -0.5, 0.5, 0.0, 1.0,
  122. 0.0, 0.0, -0.7071, 1.0,
  123. 0.5, 0.5, 0.0, 1.0,
  124. 0.5, -0.5, 0.0, 1.0,
  125. };
  126. static const GLfloat oct_color[] = {
  127. 1.0, 0.64, 0.0, 1.0,
  128. 1.0, 0.64, 0.0, 1.0,
  129. 1.0, 0.64, 0.0, 1.0,
  130. 0.8, 0.51, 0.0, 1.0,
  131. 0.8, 0.51, 0.0, 1.0,
  132. 0.8, 0.51, 0.0, 1.0,
  133. 0.5, 0.32, 0.0, 1.0,
  134. 0.5, 0.32, 0.0, 1.0,
  135. 0.5, 0.32, 0.0, 1.0,
  136. 0.2, 0.13, 0.0, 1.0,
  137. 0.2, 0.13, 0.0, 1.0,
  138. 0.2, 0.13, 0.0, 1.0,
  139. 0.2, 0.13, 0.0, 1.0,
  140. 0.2, 0.13, 0.0, 1.0,
  141. 0.2, 0.13, 0.0, 1.0,
  142. 0.5, 0.32, 0.0, 1.0,
  143. 0.5, 0.32, 0.0, 1.0,
  144. 0.5, 0.32, 0.0, 1.0,
  145. 0.8, 0.51, 0.0, 1.0,
  146. 0.8, 0.51, 0.0, 1.0,
  147. 0.8, 0.51, 0.0, 1.0,
  148. 1.0, 0.64, 0.0, 1.0,
  149. 1.0, 0.64, 0.0, 1.0,
  150. 1.0, 0.64, 0.0, 1.0,
  151. };
  152. static void Display( void )
  153. {
  154. glClearColor(0.1, 0.1, 0.4, 0);
  155. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  156. glMatrixMode( GL_MODELVIEW );
  157. glLoadIdentity();
  158. glTranslatef( 0.0, 0.0, -15.0 );
  159. glRotatef( angle, 0.0 * angle , 0.0 * angle, 1.0 );
  160. (*bind_vertex_array)( cube_array_obj );
  161. glPushMatrix();
  162. glTranslatef(-1.5, 0, 0);
  163. glRotatef( angle, 0.3 * angle , 0.8 * angle, 1.0 );
  164. glDrawArrays( GL_QUADS, 0, 4 * 6 );
  165. glPopMatrix();
  166. (*bind_vertex_array)( oct_array_obj );
  167. glPushMatrix();
  168. glTranslatef(1.5, 0, 0);
  169. glRotatef( angle, 0.3 * angle , 0.8 * angle, 1.0 );
  170. glDrawArrays( GL_TRIANGLES, 0, 3 * 8 );
  171. glPopMatrix();
  172. glutSwapBuffers();
  173. }
  174. static void Idle( void )
  175. {
  176. static double t0 = -1.;
  177. double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  178. if (t0 < 0.0)
  179. t0 = t;
  180. dt = t - t0;
  181. t0 = t;
  182. angle += 70.0 * dt; /* 70 degrees per second */
  183. angle = fmod(angle, 360.0); /* prevents eventual overflow */
  184. glutPostRedisplay();
  185. }
  186. static void Visible( int vis )
  187. {
  188. if ( vis == GLUT_VISIBLE ) {
  189. glutIdleFunc( Idle );
  190. }
  191. else {
  192. glutIdleFunc( NULL );
  193. }
  194. }
  195. static void Reshape( int width, int height )
  196. {
  197. GLfloat ar = (float) width / (float) height;
  198. Width = width;
  199. Height = height;
  200. glViewport( 0, 0, width, height );
  201. glMatrixMode( GL_PROJECTION );
  202. glLoadIdentity();
  203. glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
  204. }
  205. static void Key( unsigned char key, int x, int y )
  206. {
  207. (void) x;
  208. (void) y;
  209. switch (key) {
  210. case 27:
  211. glutDestroyWindow(Win);
  212. exit(0);
  213. break;
  214. }
  215. glutPostRedisplay();
  216. }
  217. static void Init( void )
  218. {
  219. const char * const ver_string = (const char * const)
  220. glGetString( GL_VERSION );
  221. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  222. printf("GL_VERSION = %s\n", ver_string);
  223. if ( !glutExtensionSupported("GL_APPLE_vertex_array_object") ) {
  224. printf("Sorry, this program requires GL_APPLE_vertex_array_object\n");
  225. exit(1);
  226. }
  227. bind_vertex_array = (PFNGLBINDVERTEXARRAYAPPLEPROC) glutGetProcAddress( "glBindVertexArrayAPPLE" );
  228. gen_vertex_arrays = (PFNGLGENVERTEXARRAYSAPPLEPROC) glutGetProcAddress( "glGenVertexArraysAPPLE" );
  229. delete_vertex_arrays = (PFNGLDELETEVERTEXARRAYSAPPLEPROC) glutGetProcAddress( "glDeleteVertexArraysAPPLE" );
  230. is_vertex_array = (PFNGLISVERTEXARRAYAPPLEPROC) glutGetProcAddress( "glIsVertexArrayAPPLE" );
  231. assert(bind_vertex_array);
  232. assert(gen_vertex_arrays);
  233. assert(delete_vertex_arrays);
  234. assert(is_vertex_array);
  235. glEnable( GL_DEPTH_TEST );
  236. (*gen_vertex_arrays)( 1, & cube_array_obj );
  237. (*bind_vertex_array)( cube_array_obj );
  238. glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, cube_vert);
  239. glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, cube_color);
  240. glEnableClientState( GL_VERTEX_ARRAY );
  241. glEnableClientState( GL_COLOR_ARRAY );
  242. (*gen_vertex_arrays)( 1, & oct_array_obj );
  243. (*bind_vertex_array)( oct_array_obj );
  244. glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, oct_vert);
  245. glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, oct_color);
  246. glEnableClientState( GL_VERTEX_ARRAY );
  247. glEnableClientState( GL_COLOR_ARRAY );
  248. (*bind_vertex_array)( 0 );
  249. glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, (void *) 0xDEADBEEF );
  250. glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, (void *) 0xBADDC0DE );
  251. }
  252. int main( int argc, char *argv[] )
  253. {
  254. glutInit( &argc, argv );
  255. glutInitWindowPosition( 0, 0 );
  256. glutInitWindowSize( Width, Height );
  257. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  258. Win = glutCreateWindow( "GL_APPLE_vertex_array_object demo" );
  259. glutReshapeFunc( Reshape );
  260. glutKeyboardFunc( Key );
  261. glutDisplayFunc( Display );
  262. glutVisibilityFunc( Visible );
  263. Init();
  264. glutMainLoop();
  265. return 0;
  266. }