Clone of mesa.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

vbo-drawelements.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Basic VBO */
  2. #include <assert.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include <GL/glew.h>
  8. #include <GL/glut.h>
  9. struct {
  10. GLfloat pos[3];
  11. GLubyte color[4];
  12. } verts[] =
  13. {
  14. { { 0.9, -0.9, 0.0 },
  15. { 0x00, 0x00, 0xff, 0x00 }
  16. },
  17. { { 0.9, 0.9, 0.0 },
  18. { 0x00, 0xff, 0x00, 0x00 }
  19. },
  20. { { -0.9, 0.9, 0.0 },
  21. { 0xff, 0x00, 0x00, 0x00 }
  22. },
  23. { { -0.9, -0.9, 0.0 },
  24. { 0xff, 0xff, 0xff, 0x00 }
  25. },
  26. };
  27. GLuint indices[] = { 0, 1, 2, 3 };
  28. GLuint arrayObj, elementObj;
  29. static void Init( void )
  30. {
  31. GLint errno;
  32. GLuint prognum;
  33. static const char *prog1 =
  34. "!!ARBvp1.0\n"
  35. "MOV result.color, vertex.color;\n"
  36. "MOV result.position, vertex.position;\n"
  37. "END\n";
  38. glGenProgramsARB(1, &prognum);
  39. glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prognum);
  40. glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
  41. strlen(prog1), (const GLubyte *) prog1);
  42. assert(glIsProgramARB(prognum));
  43. errno = glGetError();
  44. printf("glGetError = %d\n", errno);
  45. if (errno != GL_NO_ERROR)
  46. {
  47. GLint errorpos;
  48. glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
  49. printf("errorpos: %d\n", errorpos);
  50. printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB));
  51. }
  52. glEnableClientState( GL_VERTEX_ARRAY );
  53. glEnableClientState( GL_COLOR_ARRAY );
  54. glGenBuffersARB(1, &arrayObj);
  55. glGenBuffersARB(1, &elementObj);
  56. glBindBufferARB(GL_ARRAY_BUFFER_ARB, arrayObj);
  57. glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, elementObj);
  58. glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts, GL_STATIC_DRAW_ARB);
  59. glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(indices), indices, GL_STATIC_DRAW_ARB);
  60. glVertexPointer( 3, GL_FLOAT, sizeof(verts[0]), 0 );
  61. glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(verts[0]), (void *)(3*sizeof(float)) );
  62. }
  63. static void Display( void )
  64. {
  65. glClearColor(0.3, 0.3, 0.3, 1);
  66. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  67. glEnable(GL_VERTEX_PROGRAM_ARB);
  68. glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_INT, NULL );
  69. glFlush();
  70. }
  71. static void Reshape( int width, int height )
  72. {
  73. glViewport( 0, 0, width, height );
  74. glMatrixMode( GL_PROJECTION );
  75. glLoadIdentity();
  76. glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
  77. glMatrixMode( GL_MODELVIEW );
  78. glLoadIdentity();
  79. /*glTranslatef( 0.0, 0.0, -15.0 );*/
  80. }
  81. static void Key( unsigned char key, int x, int y )
  82. {
  83. (void) x;
  84. (void) y;
  85. switch (key) {
  86. case 27:
  87. exit(0);
  88. break;
  89. }
  90. glutPostRedisplay();
  91. }
  92. int main( int argc, char *argv[] )
  93. {
  94. glutInit( &argc, argv );
  95. glutInitWindowPosition( 0, 0 );
  96. glutInitWindowSize( 250, 250 );
  97. glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH );
  98. glutCreateWindow(argv[0]);
  99. glewInit();
  100. glutReshapeFunc( Reshape );
  101. glutKeyboardFunc( Key );
  102. glutDisplayFunc( Display );
  103. Init();
  104. glutMainLoop();
  105. return 0;
  106. }