使用OpenGL固定功能旋转和平移2D图像
Rotating and translating a 2D image with OpenGL fixed function
我的代码在2D四边形上绘制了一个纹理,应该进行平移、旋转和缩放:
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度(图像在平移时沿opsite方向移动),我会得到一个倒置的自上而下的平移。
要围绕屏幕中心(0,0)旋转,您可以根据以下公式更改实体的位置:
x=x*cos(角度)-y*sin(角度)
y=x*sin(角度)-y*cos(角度)
其中x和y是实体的位置,角度是旋转角度。如果要在旋转后面对同一点(如旋转中心),则还必须围绕其中心以负角度旋转实体。这与旋转矩阵使用的相同,但
如果要绕特定点旋转,则必须使用相对于该点的坐标。这很容易,只需减去实体坐标的中心点坐标,进行旋转并将中心坐标添加到新位置。这很重要,因为你是相对于另一个点而不是中心的,这就是矢量的工作方式。
相关文章:
- 如何将 2d 数组转换为 svg 或 png 格式的数独图像?
- 将 2D 图像坐标转换为 z = 0 的 3D 世界坐标
- OPENCV(C ) - 从已知的3D对象和相机位置计算图像的2D坐标
- 过剩无法绘制 2D 图像
- C++图像处理 - 将图像文件读入 2D 数组
- VTK 将 2D 图像转换为 3D (C++)
- 2D FFT后的图像结果错误
- 将2D图像映射到3D纹理
- 使用OpenCV?从一组2D图像切片重建3D模型
- 如何使用Direct3D 11 API加载、渲染和转换2D图像
- 如何在C++显示 2D RGB 矩阵的图像
- 使用CUDA从2D图像编程3D模型
- 在 2D 图像阵列中裁剪/插入 - 内存分配问题
- 从 2D 图像堆栈渲染 3D 体积
- 从给定的视点将3D点云射线投射到2D图像
- 为什么2D图像有时被建模为指针对指针(T**)
- 使用OpenCV将云数据转换为2d图像
- 如何使用OpenGL绘制2D图像
- 使用OpenGL固定功能旋转和平移2D图像
- 简单的库,用于在 Linux 上以C++显示 2D 图像