【发布时间】:2016-07-20 13:48:47
【问题描述】:
我的代码在二维四边形上绘制了一个纹理,该纹理应该被平移、旋转和缩放:
double width = ... //window width
double height = ... //window height
double imagewidth = ...
double imageheight = ...
vec2 usertranslation = ... //additional translation in x,y direction from user input
double rot_angle = ... //rotation angle from user input
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, width, 0.0, height, 0.0, -1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// translate to the top center of the image and add user input translation
double transx = (width - imagewidth) / 2 + usertranslation.x;
double transy = height - imageheight + usertranslation.y;
glTranslatef(transx, transy, 0);
//this currently rotates around the center of the image, but what I need
//is to rotate the image around the center of the window.
glTranslatef(imagewidth / 2, imageheight / 2, 0);
glRotatef(rot_angle, 0, 0, 1.0);
glTranslatef(-imagewidth / 2, -imageheight / 2, 0);
//bind the texture and draw the quad
drawTexturedQuad();
现在我的问题是,我需要更改什么才能让图像始终围绕窗口的中心点旋转。我已经尝试过这样的旋转:
glTranslatef(imagewidth / 2, imageheight - usertranslation.y - height, 0);
glRotatef(rot_angle, 0, 0, 1.0);
glTranslatef(-imagewidth / 2, -(imageheight - usertranslation.y - height), 0);
但是,如果图像旋转 180 度(图像在平移时沿相反方向移动),我会得到一个倒置的自上而下的平移。
【问题讨论】: