所以我一直在学习opengl和3d游戏编程,所以我开始尝试制作相机课,并实现使用鼠标的移动和查看。
移动完美无瑕,但是用鼠标环顾四周并不能正常工作。
只有在偏航角为0°时才能正常查找,然后我才能向上看。但是当yaw <0或yaw> 0时,向上查找将不会笔直,而更像是)或(。
我还注意到,当偏航角不为0时,无论向下还是向上看,偏航力都为0。
我不明白为什么会发生这种情况,为什么俯仰仅在偏航为0时才能正确运行,而为什么在偏航为0时却不正确呢?
在学习完本教程后,真的没有错
处理鼠标移动和更新
void Camera::processMouseMovement(GLfloat xOffset, GLfloat yOffset)
{
GLboolean constrainPitch = true;
xOffset *= this->mouseSensitivity;
yOffset *= this->mouseSensitivity;
this->yaw += xOffset;
this->pitch += yOffset;
if(constrainPitch)
{
if(this->pitch > 90.0f)
{
this->pitch = 90.0f;
}
if(this->pitch < -90.0f)
{
this->pitch = -90.0f;
}
}
this->updateCameraVectors();
}
这是我的更新功能
void Camera::updateCameraVectors()
{
Vector3 front;
Matrix4 mat4;
front.x = cos((DEG2RAD * this->yaw) * cos(DEG2RAD * this->pitch));
front.y = sin(DEG2RAD * this->pitch);
front.z = sin((DEG2RAD * this->yaw) * cos(DEG2RAD * this->pitch));
this->front = mat4.normalize(front);
this->right = mat4.normalize(mat4.cross(this->front, this->worldUp));
this->up = mat4.normalize(mat4.cross(this->right, this->front));
std::cout << "yaw: " << yaw << std::endl;
std::cout << "pitch: " << pitch << std::endl;
}
在main.cpp
Camera camera(Vector3(0.0f, 0.0f, 0.0f));
GLfloat lastX = width / 2.0f;
GLfloat lastY = height / 2.0f;
GLboolean key[1024];
GLboolean firstMouse = true;
GLfloat deltaTime = 0.0f;
GLfloat lastFrame = 0.0f;
使用glfw输入,在main.cpp中为aslo
glfwSetKeyCallback(window, keyCallBack);
glfwSetCursorPosCallback(window, mouseCallBack);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
在主循环中
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
glfwPollEvents();
doMovement();
proj = proj.perspective(camera.getZoom(), float(700 / 600), 1.0f, 1000.0f);
glUniformMatrix4fv(uniProj, 1, GL_FALSE, proj.get());
view = camera.getViewMatrix();
鼠标回调函数
void mouseCallBack(GLFWwindow *window, GLdouble xPos, GLdouble yPos)
{
if(firstMouse)
{
lastX = xPos;
lastY = yPos;
firstMouse = false;
}
GLfloat xOffset = lastX - xPos;
GLfloat yOffset = yPos - lastY;
lastX = xPos;
lastY = yPos;
std::cout << "mouse position x: " << xPos << std::endl;
std::cout << "mouse position y: " << yPos << std::endl;
camera.processMouseMovement(xOffset, yOffset);
}
谢谢:)
答案 0 :(得分:1)
假设front
是您的平均每日前进向量,我将这样计算:
front.x = cos(DEG2RAD * this->yaw) * sin(DEG2RAD * this->pitch);
front.y = sin(DEG2RAD * this->yaw);
front.z = cos(DEG2RAD * this->yaw) * cos(DEG2RAD * this->pitch);
我不太了解我的三角函数,以了解您的代码在做什么。
更新:我盯着你的代码了一会儿...看来,代码中括号的外部集是一个错误。我的代码与代码之间的唯一其他区别是我在计算forward.x时使用了sin
,它将交换一个轴。