Reputation: 355
I'm new to OpenGL and trying to render two objects, but only one of them should rotate. I learned from here that I can use glPushMatrix
and glPopMatrix
to apply the roration on only one object, but mine does not work.
Here are my codes:
void renderObjs() {
//glPushMatrix();
drawPyramid(0, 0.7, 0, 0.289, 0, 0.167, -0.289, 0, 0.167, 0, 0, -0.33); // a function to draw triangles
//glPopMatrix();
glPushMatrix();
glRotatef(0.3f, 1.f, 1.f, 1.f);
drawPyramid(0 - 0.5, 0.7 - 0.5, 0 - 0.5, 0.289 - 0.5, 0 - 0.5, 0.167 - 0.5, -0.289 - 0.5, 0 - 0.5, 0.167 - 0.5, 0 - 0.5, 0 - 0.5, -0.33 - 0.5);
glPopMatrix();
}
int main() {
// some initialization
...codes here...
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
renderObjs();
glfwSwapBuffers(window);
glfwPollEvents();
}
// other useful functions
...codes here...
}
But neither of my pyramids rotates. Why is this happening? Am I using the glPushMatrix
and glPopMatrix
in the wrong way?
Upvotes: 1
Views: 797
Reputation: 210938
The matrix transformation operations like glRotatef
, specify a matrix and multiply the current matrix by the new matrix. glPushMatrix
pushes the current matrix onto the matrix stack. glPopMatrix
pops a matrix from the matrix stack and sets the current matrix through the popped matrix. The unit of the angle of glRotate
is degrees:
glPushMatrix();
glRotatef(30.0f, 1.f, 1.f, 1.f);
drawPyramid(0 - 0.5, 0.7 - 0.5, 0 - 0.5, 0.289 - 0.5, 0 - 0.5, 0.167 - 0.5, -0.289 - 0.5, 0 - 0.5, 0.167 - 0.5, 0 - 0.5, 0 - 0.5, -0.33 - 0.5);
glPopMatrix();
If you want to rotate the object continuously, you need to increase the rotation angle in each image:
float angle = 0.0f;
void renderObjs() {
//glPushMatrix();
drawPyramid(0, 0.7, 0, 0.289, 0, 0.167, -0.289, 0, 0.167, 0, 0, -0.33); // a function to draw triangles
//glPopMatrix();
glPushMatrix();
glRotatef(angle, 1.f, 1.f, 1.f);
angle += 0.1f
drawPyramid(0 - 0.5, 0.7 - 0.5, 0 - 0.5, 0.289 - 0.5, 0 - 0.5, 0.167 - 0.5, -0.289 - 0.5, 0 - 0.5, 0.167 - 0.5, 0 - 0.5, 0 - 0.5, -0.33 - 0.5);
glPopMatrix();
}
Upvotes: 1