#include #include #define GL_GLEXT_PROTOTYPES 1 #include #include #define WINDSIZEX 300 #define WINDSIZEY 300 #define TRIANGLE_COUNT1 1 #define TRIANGLE_COUNT2 2 #define TRIANGLE_ARRAY_BYTES (sizeof (GLfloat) * 2 * 3 * TRIANGLE_COUNT1) void display (void) { GLuint vbo_name; GLfloat *triangle_array1; GLfloat *triangle_array2; int i, v; triangle_array2 = malloc (sizeof (GLfloat) * 2 * 3 * TRIANGLE_COUNT2); glClearColor (0.0f, 0.0f, 0.0f, 0.0f); glClear (GL_COLOR_BUFFER_BIT); glEnableClientState (GL_VERTEX_ARRAY); glGenBuffers (1, &vbo_name); glBindBuffer (GL_ARRAY_BUFFER, vbo_name); glBufferData (GL_ARRAY_BUFFER, TRIANGLE_ARRAY_BYTES, NULL, GL_STATIC_DRAW); triangle_array1 = glMapBuffer (GL_ARRAY_BUFFER, GL_WRITE_ONLY); if (triangle_array1 == NULL) { fprintf (stderr, "Couldn't map VBO\n"); exit (1); } v = 0; for (i = 0; i < TRIANGLE_COUNT1; i++) { // triangle_array1 [v++] = -1.; triangle_array1 [v++] = -1.; // /| triangle_array1 [v++] = 1.; triangle_array1 [v++] = -1.; // / | triangle_array1 [v++] = 1.; triangle_array1 [v++] = 1.; // /__| } v = 0; for (i = 0; i < TRIANGLE_COUNT2; i++) { // ___ triangle_array2 [v++] = -1.; triangle_array2 [v++] = -1.; // | / triangle_array2 [v++] = -1.; triangle_array2 [v++] = 1.; // | / triangle_array2 [v++] = 1.; triangle_array2 [v++] = 1.; // |/ } glUnmapBuffer (GL_ARRAY_BUFFER); glVertexPointer (2, GL_FLOAT, 0, NULL); glDrawArrays (GL_TRIANGLES, 0, 3 * TRIANGLE_COUNT1); triangle_array1 = glMapBuffer (GL_ARRAY_BUFFER, GL_WRITE_ONLY); /* START DELIBERATE BUG */ glVertexPointer (2, GL_FLOAT, 0, triangle_array2); glDrawArrays (GL_POINTS, 0, 3 * TRIANGLE_COUNT2); glDisableClientState (GL_VERTEX_ARRAY); glDisable (GL_COLOR_LOGIC_OP); /* END DELIBARATE BUG */ /* Not sure if this is needed, but it crashes with.. haven't tested without */ #if 1 glGenBuffers (1, &vbo_name); glBindBuffer (GL_ARRAY_BUFFER, vbo_name); glBufferData (GL_ARRAY_BUFFER, TRIANGLE_ARRAY_BYTES, NULL, GL_STATIC_DRAW); triangle_array1 = glMapBuffer (GL_ARRAY_BUFFER, GL_WRITE_ONLY); v = 0; for (i = 0; i < TRIANGLE_COUNT1; i++) { // triangle_array1 [v++] = -1.; triangle_array1 [v++] = -1.; // /| triangle_array1 [v++] = 1.; triangle_array1 [v++] = -1.; // / | triangle_array1 [v++] = 1.; triangle_array1 [v++] = 1.; // /__| } #endif glVertexPointer (2, GL_FLOAT, 0, NULL); glDrawArrays (GL_TRIANGLES, 0, 3 * TRIANGLE_COUNT1); glBindBuffer (GL_ARRAY_BUFFER, 0); glDeleteBuffers (1, &vbo_name); glutSwapBuffers(); free (triangle_array2); } int main (int argc, char** argv) { glutInit (&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (WINDSIZEX, WINDSIZEY); glutCreateWindow (argv[0]); glutDisplayFunc (display); glMatrixMode (GL_PROJECTION); gluPerspective (/* field of view in degree */ 40.0, /* aspect ratio */ 1.0, /* Z near */ 1.0, /* Z far */ 70.0); glMatrixMode(GL_MODELVIEW); gluLookAt (0.0, 0.0, 5.0, /* eye is at (0,0,5) */ 0.0, 0.0, 0.0, /* center is at (0,0,0) */ 0.0, 1.0, 0.); /* up is in positive Y direction */ glutMainLoop (); return 0; }