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