OpenGL - Transforming a cube
I've been learning OpenGL, and I decided to code a function to draw a unit cube centered at 0,0,0 so I could then transform it as I wished. It is made of 6 faces.
However, I can only transform one of the faces on my cube :(
Here's the code:
void myUnitCube() {
glPushMatrix();
glNormal3f(0.0,0.0, 1.0);
glTranslated(0.0,0.0,-0.5);
glRotated(180, 0.0,1.0,0.0);
glRectf(-0.5, -0.5, 0.5, 0.5);
glPopMatrix();
glPushMatrix();
glNormal3f(0.0,0.0, 1.0);
glTranslated(0.0,0.0,0.5);
glRectf(-0.5, -0.5, 0.5, 0.5);
glPopMatrix();
glPushMatrix();
glNormal3f(0.0,0.0, 1.0);
glTranslated(0.5,0.0,0.0);
glRotated(90, 0.0,1.0,0.0);
glRectf(-0.5, -0.5, 0.5, 0.5);
glPopMatrix();
glPushMatrix();
glNormal3f(0.0,开发者_StackOverflow1.0, 0.0);
glTranslated(-0.5,0.0,0.0);
glRotated(-90, 0.0,1.0,0.0);
glRectf(-0.5, -0.5, 0.5, 0.5);
glPopMatrix();
glPushMatrix();
glNormal3f(0.0,0.0, 0.0);
glTranslated(0.0,-0.5,0.0);
glRotated(90, 1.0,0.0,0.0);
glRectf(-0.5, -0.5, 0.5, 0.5);
glPopMatrix();
glPushMatrix();
glNormal3f(0.0,0.0, 0.0);
glTranslated(0.0,0.5,0.0);
glRotated(-90, 1.0,0.0,0.0);
glRectf(-0.5, -0.5, 0.5, 0.5);
glPopMatrix();
}
If I call myUnitCube()
after:
glPushMatrix();
glTranslated(-4,0,-3);
glPushMatrix();
glScaled(8,0.1,6);
The result is that only the first face to be drawn gets scaled. How do I work around this?
I understand this situation arises because of the pop
s but I need them...
Thanks!
Are your matrix pushes and pops matched? Based on the code you gave, they are not. This may be the cause of your problem.
精彩评论