我是GLM的新手并尝试编写fps风格的相机类
我移动鼠标时是否需要重新计算向上矢量?
然而,在我更改向上矢量后,一切看起来都很奇怪
但是,当我保持向上矢量
时,我的程序看起来很麻烦文件:fpscamera.cpp
void FPSCamera::rotateScreen(float dx, float dy){
auto oldDirection=getTarget()-getPosition();
setTarget(getPosition()+rotate(oldDirection, -dx*5, getUp()));
auto oldHoro=rotate(cross(oldDirection,getUp()), -dx*5, getUp());
setTarget(getPosition()+rotate(getTarget()-getPosition(),dy*5, oldHoro));
////////////////HERE////////////////////////
//setUp(normalize(cross(oldHoro,getTarget()-getPosition())));
}
您可以在此处下载源代码和二进制文件
https://docs.google.com/file/d/0B9givuJvSet8ekRReWtRM29ldzg/edit?usp=sharing
mingw-built64 g ++ 4.8(C ++ 11)glfw3 GLM(MathHelper Library)必备
g ++ -std = c ++ 0x -Wall main.cpp mesh.cpp fpscamera.cpp -lglfw3 -lopengl32 -lglu32 -lgdi32
走向目标...... W
左/右转...... Q / E
向左/向右移动...... A / D
==================== 2013/8/6编辑 我编辑我的相机类并添加偏航俯仰滚动功能
void Camera::roll(float among){
if(isChange) resetCacheAxis();
upVec=glm::rotate(upVec, among, cacheY);
isChange=true;
}
void Camera::pitch(float among){
if(isChange) resetCacheAxis();
targetVec=posVec+glm::rotate(targetVec-posVec, among, cacheX);
upVec=glm::normalize(glm::cross(cacheX, targetVec-posVec));
isChange=true;
}
void Camera::yaw(float among){
if(isChange) resetCacheAxis();
targetVec=posVec+glm::rotate(targetVec-posVec, among, cacheZ);
isChange=true;
}
void Camera::resetCacheAxis(){
cacheY=glm::normalize(targetVec-posVec);
cacheZ=glm::normalize(upVec);
cacheX=glm::cross(cacheY, cacheZ);
}
我实现了偏航和俯仰的鼠标摄像头控制
void FPSCamera::rotateScreen(float dx, float dy){
yaw(-dx);
pitch(dy);
}
问题仍然存在......
将鼠标沿圆圈路径移动几次后.....
答案 0 :(得分:1)
相机应绕2轴旋转。向上矢量和strafe矢量。围绕向上矢量旋转使得侧向“看起来”,而围绕该矢量矢量旋转给出了向上和向下的“外观”。
执行旋转时,不仅需要变换方向,还需要变换向上矢量和strafe矢量。你似乎没有这样做。在变换之后必须改变至少2个矢量,因为所有3个矢量必须彼此正交。
请看下面的伪代码:
public static void MouseLook(float x, float y)
{
Matrix xRot = Matrix.CreateFromAxisAngle(Vector3.Up, x * rotationSpeed);
Matrix yRot = Matrix.CreateFromAxisAngle(Vector3.Right, y * rotationSpeed);
viewMatrix = viewMatrix * yRot * xRot;
// Orthogonalize the matrix
OrthoNormalize();
}
private static void OrthoNormalize()
{
Vector3 zAxis = viewMatrix.Forward;
zAxis.Normalize();
Vector3 xAxis = Vector3.Cross(Vector3.Up,zAxis);
xAxis.Normalize();
Vector3 yAxis = Vector3.Cross(xAxis,zAxis);
yAxis.Normalize();
viewMatrix.Forward = zAxis;
viewMatrix.Up = yAxis;
viewMatrix.Right = xAxis;
}
请查看以下问题,因为它可以解答您的需求:Camera Rotation