OpenGL -相机轨道点与四元数
OpenGL - Camera Orbiting a Point with Quaternions
所以我目前使用四元数来存储和修改我的OpenGL场景中物体的方向,以及相机的方向。当直接旋转这些对象时(游戏邦注:例如,我想围绕Z轴旋转摄像机Z量,或者我想围绕X轴旋转物体X,然后沿着其局部Z轴平移它T),我没有问题,所以我只能假设我的基本旋转代码是正确的。
然而,我现在正试图实现一个函数,使我的相机在空间中的任意点轨道上运行,而且我有相当困难的时间。这是我到目前为止想出的,它不起作用(这发生在Camera类中)。
//Get the inverse of the orientation, which should represent the orientation
//"from" the focal point to the camera
Quaternion InverseOrient = m_Orientation;
InverseOrient.Invert();
///Rotation
//Create change quaternions for each axis
Quaternion xOffset = Quaternion();
xOffset.FromAxisAngle(xChange * m_TurnSpeed, 1.0, 0.0, 0.0);
Quaternion yOffset = Quaternion();
yOffset.FromAxisAngle(yChange * m_TurnSpeed, 0.0, 1.0, 0.0);
Quaternion zOffset = Quaternion();
zOffset.FromAxisAngle(zChange * m_TurnSpeed, 0.0, 0.0, 1.0);
//Multiply the change quats into the inversed orientation quat
InverseOrient = yOffset * zOffset * xOffset * InverseOrient;
//Translate according to the focal distance
//Start with a vector relative to the position being looked at
sf::Vector3<float> RelativePos(0, 0, -m_FocalDistance);
//Rotate according to the quaternion
RelativePos = InverseOrient.MultVect(RelativePos);
//Add that relative position to the focal point
m_Position.x = m_FocalPoint->x + RelativePos.x;
m_Position.y = m_FocalPoint->y + RelativePos.y;
m_Position.z = m_FocalPoint->z + RelativePos.z;
//Now set the orientation to the inverse of the quaternion
//used to position the camera
m_Orientation = InverseOrient;
m_Orientation.Invert();
最终发生的是相机围绕其他点旋转——当然不是物体,但显然也不是它本身,就好像它在空间中以螺旋路径循环。
所以这显然不是让相机绕点运行的方法,但什么才是呢?
我将首先在球坐标中操作相机,并在必要时转换为四元数。
给定以下假设:
- 相机没有胶卷
- 你正在看的点是[x, y, z]
- 你有偏航,俯仰角
- [0,1,0]为"up"
下面是如何计算一些重要的值:
- 视图向量:v = [vx, vy, vz] = [cos(偏航)*cos(俯仰),sin(俯仰),-sin(偏航)*cos(俯仰)]
- 相机位置:p = [x, y, z] - r*v
- 右向量:v与[0,1,0]的叉乘
- 上向量:v与右向量的叉乘
- 你的视图四元数是[0,vx, vy, vz](这是一个0 w分量的视图向量)
现在在你的模拟中,你可以操作俯仰/偏航,这是非常直观的。如果你想做插值,把前后俯仰+偏航角转换成四元数,然后做四元数球面线性插值。
相关文章:
- 单元测试欧拉到四元数实现失败
- 零四元数和任何向量都不为零的特征积,这是一个错误吗?
- 将四元数旋转的游戏对象旋转另一个四元数时出现问题
- 在OpenGL(GLM/C++)中使用四元数旋转时出现问题
- 想要将 CGAL 与四元数相结合是否合乎逻辑
- Eigen::Quaternion FromTwoVectors() 不返回有效的四元数
- C++四元数方程程序
- 四元数旋转不起作用
- 四元数和翻译
- 使用四元数防止绕某个轴旋转
- 如何使用四元数来描述超过360度的旋转角度?
- 四元数旋转错误
- 为什么我简单的四元数乘法比SSE快
- 特征:将矩阵3d旋转转换为四元数
- 从四元数查看矩阵
- 用四元数绕轴旋转矢量
- 基于四元数的相机不需要的卷轴
- 在C++中使用1D FFT的四元数FFT
- 四元数到轴的角度
- OpenGL -相机轨道点与四元数