我想在OpenGL中围绕其中心点旋转2D图像。
这是我目前的代码:
void Plane::draw() {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, plane_image);
glBegin (GL_QUADS);
glTexCoord2f (0.0, 1.0);
glVertex2f (pos.x-6.0, pos.y+6.0);
glTexCoord2f (1.0, 1.0);
glVertex2f (pos.x+6.0, pos.y+6.0);
glTexCoord2f (1.0, 0.0);
glVertex2f (pos.x+6.0, pos.y-6.0);
glTexCoord2f (0.0, 0.0);
glVertex2f (pos.x-6.0, pos.y-6.0);
glEnd ();
glDisable(GL_TEXTURE_2D);
}
我必须以原点为中心开始四边形,然后将其翻译为+ x,+ y。转换和旋转应该在创建四边形之前进行。感谢BWG和Skyler!
解决方案:
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, plane_image);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(pos.x,pos.y,0);
glRotatef(45,0,0,1);
glBegin (GL_QUADS);
glTexCoord2f (0.0, 1.0);
glVertex2f (-6.0, 6.0);
glTexCoord2f (1.0, 1.0);
glVertex2f (6.0, 6.0);
glTexCoord2f (1.0, 0.0);
glVertex2f (6.0, -6.0);
glTexCoord2f (0.0, 0.0);
glVertex2f (-6.0, -6.0);
glEnd ();
glPopMatrix();
glDisable(GL_TEXTURE_2D);