我是 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);
}
问题依然存在......
沿着圆形路径移动鼠标几次后......