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