我在根据鼠标移动进行旋转时遇到问题。
我使用类似这样的相机类: http://hamelot.io/visualization/moderngl-camera/
以下是代码:
#include "Camera.h"
#include <gl/glew.h>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Mouse.hpp>
Camera::Camera() : _viewportX(0), _viewportY(0), _windowX(1920), _windowY(1080), _lastX(0), _lastY(0), _aspect(1), _nearClip(.001f), _farClip(1000.0f), _cameraHeading(0), _cameraPitch(0), _grab(false)
{
_cameraType = CameraType::FREE;
_cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
_fov = 45.0f;
_cameraPosDelta = glm::vec3(0);
_cameraScale = 0.01f;
_maxPitch = 5;
_maxHeading = 5;
_moveCamera = false;
}
Camera::~Camera()
{
}
void Camera::reset()
{
_cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
}
void Camera::update()
{
_oldDirection = _cameraDirection;
_cameraDirection = glm::normalize(_cameraLookAt - _cameraPos);
// We need to set the matrix state, this is important because lighting won't work otherwise
glViewport(_viewportX, _viewportY, _windowX, _windowY);
if (_cameraType == CameraType::ORTHO)
{
_projection = glm::ortho(-1.5f * float(_aspect), 1.5f * float(_aspect), -1.5f, 1.5f, -10.0f, 10.0f);
}
else
{
_projection = glm::perspective(_fov, _aspect, _nearClip, _farClip);
// Axis for pitch rotation
glm::vec3 axis = glm::cross(_cameraDirection, _cameraUp);
// Compute quaternion for pitch based on the camera pitch angle
glm::quat pitchQuat = glm::angleAxis(_cameraPitch, axis);
// Determine heading quaternion from the camera up vector and the heading angle
glm::quat headingQuat = glm::angleAxis(_cameraHeading, _cameraUp);
// Add the two quats
glm::quat tempQuat = glm::cross(pitchQuat, headingQuat);
tempQuat = glm::normalize(tempQuat);
// Update the direction from the quaternion
_cameraDirection = glm::rotate(tempQuat, _cameraDirection);
// add the camera delta
_cameraPos += _cameraPosDelta;
// set the lookat matrix to be infront of the camera
_cameraLookAt = _cameraPos + _cameraDirection * 1.0f;
// Damping for smooth camera
_cameraHeading *= 0.5f;
_cameraPitch *= 0.5f;
_cameraPosDelta *= 0.8f;
}
// compute the mvp
_view = glm::lookAt(_cameraPos, _cameraLookAt, _cameraUp);
}
void Camera::moveKeyboard()
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
processMovement(UP);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::E))
processMovement(DOWN);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
processMovement(LEFT);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
processMovement(RIGHT);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
processMovement(FORWARD);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
processMovement(BACK);
}
void Camera::changePitch(float degrees)
{
//Check bounds with the max pitch rate so that we aren't moving too fast
if (degrees < -_maxPitch)
{
degrees = -_maxPitch;
}
else if (degrees > _maxPitch)
{
degrees = _maxPitch;
}
_cameraPitch += degrees;
// Check bounds for cameraPitch
if (_cameraPitch > 360.0f)
{
_cameraPitch -= 360.0f;
}
else if (_cameraPitch < -360.0f)
{
_cameraPitch += 360.0f;
}
}
void Camera::changeHeading(float degrees)
{
//Check bounds with the max Heading rate so that we aren't moving too fast
if (degrees < -_maxHeading)
{
degrees = -_maxHeading;
}
else if (degrees > _maxHeading)
{
degrees = _maxHeading;
}
_cameraHeading += degrees;
// This controls how the heading is changed if the camera is pointed straight up or down
// The heading delta direction changes
if (_cameraPitch > 90 && _cameraPitch < 270 || (_cameraPitch < -90 && _cameraPitch > -270))
{
_cameraHeading -= degrees;
}
else
{
_cameraHeading += degrees;
}
// Check bounds for cameraHeading
if (_cameraHeading > 360.0f)
{
_cameraHeading -= 360.0f;
}
else if (_cameraHeading < -360.0f)
{
_cameraHeading += 360.0f;
}
}
void Camera::processMouseMovement(sf::RenderWindow& window)
{
auto mousePos = sf::Mouse::getPosition(window);
if (_lastX == 0 && _lastY == 0)
{
_lastX = _windowX / 2;
_lastY = _windowY / 2;
}
if (mousePos != sf::Vector2i(_lastX, _lastY))
{
GLfloat xOffset = (_windowX / 2) - mousePos.x;
GLfloat yOffset = (_windowY / 2) - mousePos.y;
xOffset *= _cameraScale;
yOffset *= _cameraScale;
if (_moveCamera)
{
changeHeading(.08f * xOffset);
changePitch(.08f * yOffset);
}
}
sf::Mouse::setPosition(sf::Vector2i(_windowX / 2, _windowY / 2), window);
}
void Camera::setMode(CameraType type)
{
_cameraType = type;
_cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
}
void Camera::setPosition(glm::vec3 pos)
{
_cameraPos = pos;
}
void Camera::setLookAt(glm::vec3 pos)
{
_cameraLookAt = pos;
}
void Camera::setFOV(double fov)
{
_fov = fov;
}
void Camera::setViewport(int locX, int locY, int width, int height)
{
_viewportX = locX;
_viewportY = locY;
_windowX = width;
_windowY = height;
_aspect = static_cast<double>(_windowX) / static_cast<double>(_windowY);
}
void Camera::setClipping(double nearClipDistance, double farClipDistance)
{
_nearClip = nearClipDistance;
_farClip = farClipDistance;
}
void Camera::processMouseButtons()
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
{
_moveCamera = true;
}
else
{
_moveCamera = false;
}
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Right))
{
_grab = true;
}
else
{
_grab = false;
}
}
CameraType Camera::getMode()
{
return _cameraType;
}
void Camera::getViewPort(int& locX, int& locY, int& width, int& height)
{
locX = _viewportX;
locY = _viewportY;
width = _windowX;
height = _windowY;
}
void Camera::getMatrices(glm::mat4& view, glm::mat4& projection)
{
projection = _projection;
view = _view;
}
void Camera::processMovement(CameraDirection direction)
{
if (_cameraType == FREE)
{
switch (direction)
{
case UP:
_cameraPosDelta -= _cameraUp * _cameraScale;
break;
case DOWN:
_cameraPosDelta += _cameraUp * _cameraScale;
break;
case LEFT:
_cameraPosDelta -= glm::cross(_cameraDirection, _cameraUp) * _cameraScale;
break;
case RIGHT:
_cameraPosDelta += glm::cross(_cameraDirection, _cameraUp) * _cameraScale;
break;
case FORWARD:
_cameraPosDelta += _cameraDirection * _cameraScale;
break;
case BACK:
_cameraPosDelta -= _cameraDirection * _cameraScale;
break;
case DEFAULT:
break;
}
}
}
我试图根据相机方向(或lookAt)进行模型旋转。我已经完成了以下基础知识:
float xRot = glm::dot(_oldDirection.x, _cameraDirection.x);
xRot = acos(xRot);
这给了我两个向量的角度,并将其插入我的模型轮换中:
model[1][1] *= cos(xRot * (PI / 180));
model[2][1] *= -sin(xRot * (PI / 180));
model[1][2] *= sin(xRot * (PI / 180));
model[2][2] *= cos(xRot * (PI / 180));
我面临的问题是:
如果我向一个方向旋转太多,模型会锁定,要修复它我必须旋转另一个方向。
相机类无关的问题:我正在获得一种万向节锁。如果我快速向下移动鼠标(我的意思是我必须尽可能向下推它)屏幕将变为灰色,屏幕/相机将锁定。当我将鼠标向上推时,情况也是如此,我不能认为两侧没有万向节锁。
如果您可以将我链接到任何资源或帮助我,那将是非常好的,谢谢!
答案 0 :(得分:0)
你似乎是从航向/俯仰/滚转角度计算四元数,这是错误的顺序。要使用四元数来平滑移动相机,鼠标/键盘/任何控件都会不断更新四元数。绘制时,将此四元数转换为矩阵并将其用于相机方向。 (Quat - &gt;旋转矩阵应该是3D数学库的一部分。)
搜索“Ken Shoemake四元数”以获取解释和示例代码。
希望这有帮助。
答案 1 :(得分:0)
我一直在寻找一种实现基于四元数的专用相机的简单方法。令人惊讶的是,我无法在glm中找到任何好的实现,所以在这里。
原理。 - 分别积累框架俯仰偏航和滚动。 - 计算世界空间中的偏航(x)以及局部(默认)空间中的俯仰(x)和滚动(z)。
void Render::Node::setRotationByXYZ(float inX, float inY, float inZ) {
x += inX/100;
y += inY/100;
z += inZ/100;
glm::quat qX = normalize(glm::quat(cos(x/2), sin(x/2), 0, 0));
glm::quat qY = normalize(glm::quat(cos(y/2), 0, sin(y/2), 0));
glm::quat qZ = normalize(glm::quat(cos(z/2), 0, 0, sin(z/2)));
glm::quat qXWorld = glm::inverse(glm::quat(1.0,0.0,0.0,0.0))*qX;
glm::quat q = qXWorld*qZ*qY;
this->rotation = q;
}
通常你会转换为矩阵并通过单位矩阵进行转换,但使用quat标识值的工作原理相同。 如果你不需要滚动,只需省略qZ。这对我有用,但你的乘法顺序当然可能不同。 这是迄今为止我能提出的最简单的方法,我希望它可以帮助其他人。
答案 2 :(得分:0)
我认为你可能会发现有用的是glm :: lookAt:
的替代品void Camera::lookAt(Quaternion rot, Vertex Pos)
{
view = Matrix<4>();
Vertex v1 = (rot*Vertex(0.0f,0.0f,1.0f)).normalize(); // forward
Vertex v2 = (rot*Vertex(0.0f,1.0f,0.0f)).normalize().cross(v1); // up
Vertex v3 = v1.cross(v2);
view[0][0] = v2.x;
view[0][1] = v3.x;
view[0][2] = v1.x;
view[1][0] = v2.y;
view[1][1] = v3.y;
view[1][2] = v1.y;
view[2][0] = v2.z;
view[2][1] = v3.z;
view[2][2] = v1.z;
view[3][0] = -v2.dot(Pos);
view[3][1] = -v3.dot(Pos);
view[3][2] = -v1.dot(Pos);
//Comment this out if you aren't using the left hand coordinate system
view = view.transpose();
}
非常感谢joe stevens提到这种方法:http://joestevens.net/post/20063172257/alternate-view-matrix-construction
我为了自己的目的重写了这个,但无论如何都要查看原文。
然后,您可以将相机送入Quaternion及其中心位置,它将生成一个漂亮的视图矩阵。 这意味着您可以在每次旋转时简单地将偏航,俯仰,滚动添加到四元数,或者甚至更好地使用四元数作为旋转源。