在 OpenGL 中,对于以下 2D 情况,我如何旋转这个四边形 - 并且只有这个四边形(场景中的其他所有东西都应该保持原样)?
// Draw in immediate mode
glBegin(GL_QUADS); // begin drawing quads
glVertex2f(box.x,box.y); // top-left corner
glVertex2f(box.x+box.w,box.y); // top-right corner
glVertex2f(box.x+box.w,box.y+box.h); // bottom-right corner
glVertex2f(box.x,box.y+box.h); // bottom-left corner
glEnd(); // end drawing quads
glRotatef(angle, x,y,z)
似乎旋转了我的整个场景。
最佳答案
封装在glPushMatrix和popMatrix中
glPushMatrix(GL_MODELVIEW);
glRotatef(angle, x,y,z);
// Draw in immediate mode
glBegin(GL_QUADS); // begin drawing quads
glVertex2f(box.x,box.y); // top-left corner
glVertex2f(box.x+box.w,box.y); // top-right corner
glVertex2f(box.x+box.w,box.y+box.h); // bottom-right corner
glVertex2f(box.x,box.y+box.h); // bottom-left corner
glEnd(); // end drawing quads
glPopMatrix(GL_MODELVIEW);
基本上,在上面的示例中,您将 modevview 矩阵向上插入堆栈的一个位置,将其保存在本质中。然后旋转模型 View 并绘制四边形。 之后,您在模型 View 中退回一个位置,恢复到旋转和绘图之前的状态。
关于c++ - 在 2D 中仅旋转一个四边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9233929/