2

I've stumbled on an issue in OpenGL that I can't explain, unless I somehow misunderstood how glPushMatrix() / glPopMatrix() work.

Shouldn't the two pieces of code below have exactly the same effect?

  glPushMatrix();
  glVertex3f(0,0,0);
  glTranslatef(c->e->coords[0], c->e->coords[1], c->e->coords[2]);
  glPopMatrix();

vs

  glVertex3f(c->e->coords[0], c->e->coords[1], c->e->coords[2]);

In my application, only when I use the second one do I see anything. I tried with both pieces of code in the exact same place in the code, changing nothing else, and the one with push/pop matrix doesn't draw anything on screen.

houbysoft
  • 32,532
  • 24
  • 103
  • 156

3 Answers3

5

You can't call glPushMatrix()/PopMatrix() inside a glBegin()/glEnd() block.

glBegin():

Only a subset of GL commands can be used between glBegin and glEnd. The commands are glVertex, glColor, glSecondaryColor, glIndex, glNormal, glFogCoord, glTexCoord, glMultiTexCoord, glVertexAttrib, glEvalCoord, glEvalPoint, glArrayElement, glMaterial, and glEdgeFlag. Also, it is acceptable to use glCallList or glCallLists to execute display lists that include only the preceding commands. If any other GL command is executed between glBegin and glEnd, the error flag is set and the command is ignored.

Recommendation: Use vertex arrays or vertex buffer objects instead of classic immediate-mode glBegin()/glEnd() blocks.

genpfault
  • 51,148
  • 11
  • 85
  • 139
2

There are several problems here. First, you can't mix matrix operations with vertex ops--I assume this is shorthand and you skipped glBegin()/glEnd(). But the main problem is that you sent the vertex at the origin before you translated. Reverse those two lines and you should get the desired result.

Drew Hall
  • 28,429
  • 12
  • 61
  • 81
  • Yea I thought about that, I tried both ways (that is one with the translation first, and one with the translation second). The problem was that this was in a glBegin()/glEnd() block. Thanks – houbysoft Jun 15 '12 at 01:53
-1

You're close, but I think you should swap the order of glVertex/glTranslate in your first snippet.

The moment you call glVertex, it is drawn using the current matrix on the top of the stack. Calling translate only effects subsequent vertices being drawn; you can't move a vertex that's already been submitted.

Tim
  • 35,413
  • 11
  • 95
  • 121