#include #include #include #define X1 -1.0 #define X2 1.0 #define Y1 -1.0 #define Y2 1.0 #define display display_obj static GLfloat vbuf[] = {X1, Y1, 0, X1, Y2, 0, X2, Y2, 0, X2, Y1, 0}; void display_normal(void) { GLuint lid; glClear(GL_COLOR_BUFFER_BIT); glColor3f (0.0, 0.0, 1.0); glVertexPointer (3, GL_FLOAT, 0, vbuf); glEnableClientState (GL_VERTEX_ARRAY); lid = glGenLists(1); glNewList(lid, GL_COMPILE); glDrawArrays(GL_LINE_LOOP, 0, 4); glEndList(); glCallList(lid); glDeleteLists(lid, 1); glFlush(); } void display_obj(void) { GLuint id = 0; GLuint lid = 0; glClear(GL_COLOR_BUFFER_BIT); glColor3f (0.0, 0.0, 1.0); glGenBuffers(1, &id); glBindBuffer(GL_ARRAY_BUFFER, id); glBufferData(GL_ARRAY_BUFFER, sizeof(vbuf), vbuf, GL_STATIC_DRAW); glVertexPointer(3, GL_FLOAT, 0, 0); glEnableClientState(GL_VERTEX_ARRAY); #if 0 glDrawArrays(GL_LINE_LOOP, 0, 4); /* DrawArrays outside a display list is OK */ #else /* DrawArrays within a display list causes a segment fault */ lid = glGenLists(1); glNewList(lid, GL_COMPILE); glDrawArrays(GL_LINE_LOOP, 0, 4); glEndList(); glCallList(lid); glDeleteLists(lid, 1); #endif glDeleteBuffers(1, &id); glFlush(); } static void key(unsigned char k, int x, int y) { switch (k) { case 27: exit(0); break; default: return; } glutPostRedisplay(); } static void reshape(int width, int height) { float ar = (float)width / (float)height + 2.0; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D (-ar, ar, -ar, ar); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(250, 250); glutInitWindowPosition(100, 100); glutCreateWindow("Buffer Objects"); glutReshapeFunc(reshape); glutDisplayFunc(display); glutKeyboardFunc(key); glutMainLoop(); return 0; }