Hey! I’m having a problem with glDrawElements too! I’m studying using the OpenGL redbook.
This is what my code is showing:
http://dl.dropbox.com/u/42786/OrderIssue.png
Is was suppose to show a spinning cube, but the faces should be right and they just appear weird. Any one has any idea what’s going on?
I thought the problem was with the order of the indices, but I tried a lot of combinations and it didn’t worked. =/
My code:
static GLfloat spin = 0.0;
static GLfloat box1[][6] =
{// X Y Z R G B
{1.0, 1.0, 10.0, 1.0, 0.0, 0.0} , // 0 -- RED
{10.0, 1.0, 10.0, 0.0, 1.0, 0.0}, // 1 -- GREEN
{10.0, 10.0, 10.0, 0.0, 0.0, 1.0 }, // 2 -- BLUE
{1.0, 10.0, 10.0, 1.0, 1.0, 1.0 }, // 3 -- WHITE
{1.0, 1.0, 1.0, 0.5, 0.5, 0.5}, // 4 -- GRAY
{10.0, 1.0, 1.0, 1.0, 1.0, 0.0 }, // 5 -- YELLOW
{10.0, 10.0, 1.0, 1.0, 0.0, 1.0}, // 6 -- MAGENT
{1.0, 10.0, 1.0, 0.0, 1.0, 1.0 } // 7 -- CYAN
};
GLubyte indices[][4] = {
{ 0, 3, 2, 1 },
{ 4, 7, 2, 5 },
{ 0, 3, 7, 4 },
{ 5, 6, 2, 1 },
{ 7, 3, 2, 6 },
{ 0, 4, 5, 1 } };
void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
GLuint bufPointer[2];
glGenBuffers(2, bufPointer);
glBindBuffer(GL_ARRAY_BUFFER, bufPointer[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(box1), box1, GL_STATIC_DRAW);
glColorPointer(3, GL_FLOAT, 6*sizeof(GLfloat), BUFFER_OFFSET(3*sizeof(GLfloat)));
glVertexPointer(3, GL_FLOAT, 6*sizeof(GLfloat), BUFFER_OFFSET(0));
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufPointer[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glRotated(spin, 1, 1, 1);
glScaled(40, 40, 40);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
glPopMatrix();
glutSwapBuffers();
}
void process(void){
spin += 0.05;
if (spin > 360.0) spin = spin - 360.0;
glutPostRedisplay();
}
void reshape(int w, int h){
glViewport(0, 0, (GLdouble) w, (GLdouble) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-w, w, -h, h, -1000.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL Test");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(process);
glutMainLoop();
return 0;
}
Thanks!