Hi.
I’ve not used vertex buffer objects before but am trying to get up-to-date with current OpenGL practices.
The following results in a blank green screen where I’d expect to see a green screen and a small triangle. Is there anything obviously wrong with the code? The implementations I’ve used definitely support GL_ARB_vertex_buffer_object.
#define GL_GLEXT_PROTOTYPES 1
#include <assert.h>
#include <GL/glut.h>
#include <GL/glext.h>
#define CHECK_GL_ERROR (assert (glGetError() == GL_NO_ERROR))
typedef float vector3 [3];
static const vector3 triangle [] = {
{0.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, /* v0, c0 */
{10.0, 10.0, 0.0}, {0.0, 1.0, 0.0}, /* v1, c1 */
{0.0, 10.0, 0.0}, {0.0, 0.0, 1.0}, /* v2, c2 */
};
static const int indices [] = {0, 1, 2};
static GLuint buffer_data_id;
static GLuint buffer_index_id;
static void
reshape (int w, int h)
{
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, w, 0, h, -1, 100);
}
static void
init (void)
{
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
/* Create buffer for vertex and color data. */
glGenBuffers (1, &buffer_data_id);
CHECK_GL_ERROR;
glBindBuffer (GL_ARRAY_BUFFER, buffer_data_id);
CHECK_GL_ERROR;
/* Allocate and load data. */
glBufferData (GL_ARRAY_BUFFER, sizeof (triangle), triangle, GL_STATIC_DRAW);
CHECK_GL_ERROR;
/* Notify GL that first vertex is at offset 0. */
glVertexPointer (3, GL_FLOAT, sizeof (vector3), NULL);
CHECK_GL_ERROR;
/* Notify GL that first color is at (sizeof (vector3)). */
glColorPointer (3, GL_FLOAT, sizeof (vector3), (void *) (sizeof (vector3)));
CHECK_GL_ERROR;
/* Create index buffer. */
glGenBuffers (1, &buffer_index_id);
CHECK_GL_ERROR;
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, buffer_index_id);
CHECK_GL_ERROR;
/* Allocate and load data. */
glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof (indices), indices, GL_STATIC_DRAW);
CHECK_GL_ERROR;
}
static void
display (void)
{
glClearColor (0.3f, 0.6f, 0.3f, 1.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Bind vertex and element buffers. */
glBindBuffer (GL_ARRAY_BUFFER, buffer_data_id);
CHECK_GL_ERROR;
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, buffer_index_id);
CHECK_GL_ERROR;
/* Enable vertex and color arrays. */
glEnableClientState (GL_VERTEX_ARRAY);
CHECK_GL_ERROR;
glEnableClientState (GL_COLOR_ARRAY);
CHECK_GL_ERROR;
/* Draw data. */
glDrawElements (GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, NULL);
CHECK_GL_ERROR;
glFlush();
CHECK_GL_ERROR;
}
int
main (int argc, char **argv)
{
glutInit (&argc, argv);
glutCreateWindow ("single triangle");
glutDisplayFunc (display);
glutReshapeFunc (reshape);
init ();
glutMainLoop ();
return 0;
}