gpt4 book ai didi

c++ - 如何处理基于 quat 相机的旋转?

转载 作者:太空狗 更新时间:2023-10-29 23:13:48 25 4
gpt4 key购买 nike

我在获取基于鼠标移动的旋转时遇到了问题。

我使用的相机类与此类似: 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);

这给出了两个 vector 的角度,我将其插入到我的模型旋转中:

        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));

我面临的问题是:

  • 模型旋转大约是我运动量的 7 倍。如果我将鼠标移动 180 度,模型将旋转 7 倍。
  • 如果我在一个方向上旋转太多,模型会锁定,要修复它,我必须在另一个方向上旋转。

  • 与相机类无关的问题:我得到了一种万向节锁。如果我非常快地向下移动鼠标(我的意思是我必须尽可能向下推它)屏幕将变灰并且屏幕/相机将锁定。当我向上推鼠标时也会发生同样的情况,我不能,我认为两侧没有万向节锁定。

如果您可以将我链接到任何资源或帮助我,那就太好了,谢谢!

最佳答案

您似乎是根据航向角/俯仰角/横滚角计算四元数,这是错误的顺序。要使用四元数实现平滑的相机移动,鼠标/键盘/任何控件都会不断更新四元数。绘制时,将此四元数转换为矩阵并将其用于相机方向。 (Quat -> 旋转矩阵应该是你的 3D 数学库的一部分。)

搜索“Ken Shoemake 四元数”以获取解释和示例代码。

希望这对您有所帮助。

关于c++ - 如何处理基于 quat 相机的旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37104776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com