...
{0, 0, 255, 0, 0},
{0, 1, 0, 255, 0},
{0, 1, 0, 255, 0},
{1, 1, 255, 0, 0},
{1, 1, 255, 0, 0},
{1, 0, 0, 255, 0},
{1, 0, 0, 255, 0},
{0, 0, 255, 0, 0},
...
The range of GLbyte
is -128 to 127, not 0 to 255.
EDIT: As Vallentin pointed out switching to GL_UNSIGNED_BYTE
works:
(not set up for Qt, sorry)
#include <GL/glew.h>
#include <GL/glut.h>
#define USE_BYTES_FOR_COLOR
struct vrtx
{
GLint x;
GLint y;
#ifdef USE_BYTES_FOR_COLOR
GLubyte r;
GLubyte g;
GLubyte b;
#else
GLfloat r;
GLfloat g;
GLfloat b;
#endif
}
geo[] =
{
#ifdef USE_BYTES_FOR_COLOR
//x, y, r, g, b
{0, 0, 255, 0, 0},
{0, 1, 0, 255, 0},
{0, 1, 0, 255, 0},
{1, 1, 255, 0, 0},
{1, 1, 255, 0, 0},
{1, 0, 0, 255, 0},
{1, 0, 0, 255, 0},
{0, 0, 255, 0, 0},
#else
//x, y, r, g, b
{0, 0, 1, 0, 0},
{0, 1, 0, 1, 0},
{0, 1, 0, 1, 0},
{1, 1, 1, 0, 0},
{1, 1, 1, 0, 0},
{1, 0, 0, 1, 0},
{1, 0, 0, 1, 0},
{0, 0, 1, 0, 0},
#endif
};
GLuint vbo_id;
void init()
{
glGenBuffers( 1, &vbo_id );
glBindBuffer( GL_ARRAY_BUFFER, vbo_id );
glBufferData( GL_ARRAY_BUFFER, sizeof(geo), geo, GL_STATIC_DRAW );
}
void display()
{
glClearColor( 1, 1, 1, 1 );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -1, 2, -1, 2, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
glVertexPointer(2, GL_INT, sizeof(struct vrtx), 0);
#ifdef USE_BYTES_FOR_COLOR
glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(struct vrtx), ((char*)NULL + 8));
#else
glColorPointer(3, GL_FLOAT, sizeof(struct vrtx), ((char*)NULL + 8));
#endif
glDrawArrays(GL_LINES, 0, sizeof(geo) / sizeof(struct vrtx));
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "GLUT" );
glewInit();
init();
glutDisplayFunc( display );
glutMainLoop();
return 0;
}