user1354784
user1354784

Reputation: 406

Cube only displays if I call glPopMatrix after glEnd (not the other way around)

I am encountering a very weird issue in OpenGl. The following code produces a yellow cube as expected

glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (GLfloat[]){ 1, 1, 0, 1 });
glBegin(GL_LINE_LOOP);
glutSolidCube(1);
glPopMatrix();
glEnd();

However when I put glpopMatrix() after glEnd(), I just get a black screen without a cube.

glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (GLfloat[]){ 1, 1, 0, 1 });
glBegin(GL_LINE_LOOP);
glutSolidCube(1);
glEnd();
glPopMatrix();

To me the second approach makes more sense, (push, begin, end then pop) and I really have no idea why it does not work. Any help is appreciated, thanks!

Upvotes: 0

Views: 30

Answers (1)

vallentin
vallentin

Reputation: 26207

The problem is not the placement of the glPopMatrix() call. The problem is with glBegin() and glEnd(). Remove them, glutSolidCube() does that already.

If you're using FreeGLUT then glutSolidCube() won't even be using glBegin() and glEnd(), and will be using vertex arrays under the hood. So to put it simply, you're probably just confusing your driver and that's why you're getting the weird result.

Upvotes: 2

Related Questions