Я хочу иметь возможность повернуть камеру на 90 градусов и вниз на 90 градусов без сбоя, как это могут делать другие игры (например, Minecraft). Как мне это добиться? < /P>
Вот камера.
#ifndef CAMERA_CLASS_H
#define CAMERA_CLASS_H
#include
#include
#include
#include
#include
#include
#include
#include"shaderClass.h"
class Camera
{
public:
// Stores the main vectors of the camera
glm::vec3 Position;
glm::vec3 Orientation = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 Up = glm::vec3(0.0f, 1.0f, 0.0f);
// Prevents the camera from jumping around when first clicking left click
bool firstClick = true;
// Stores the width and height of the window
int width;
int height;
// Adjust the speed of the camera and it's sensitivity when looking around
float speed = 0.1f;
float sensitivity = 100.0f;
// Camera constructor to set up initial values
Camera(int width, int height, glm::vec3 position);
// Updates and exports the camera matrix to the Vertex Shader
void Matrix(float FOVdeg, float nearPlane, float farPlane, Shader& shader, const char* uniform);
// Handles camera inputs
void Inputs(GLFWwindow* window);
};
#endif
< /code>
И вот код cameman.cpp: < /p>
#include"Camera.h"
Camera::Camera(int width, int height, glm::vec3 position)
{
Camera::width = width;
Camera::height = height;
Position = position;
}
void Camera::Matrix(float FOVdeg, float nearPlane, float farPlane, Shader& shader, const char* uniform)
{
// Initializes matrices since otherwise they will be the null matrix
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
// Makes camera look in the right direction from the right position
view = glm::lookAt(Position, Position + Orientation, Up);
// Adds perspective to the scene
projection = glm::perspective(glm::radians(FOVdeg), (float)width / height, nearPlane, farPlane);
// Exports the camera matrix to the Vertex Shader
glUniformMatrix4fv(glGetUniformLocation(shader.ID, uniform), 1, GL_FALSE, glm::value_ptr(projection * view));
}
void Camera::Inputs(GLFWwindow* window)
{
// Handles key inputs
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
{
Position += speed * Orientation;
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
{
Position += speed * -glm::normalize(glm::cross(Orientation, Up));
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
{
Position += speed * -Orientation;
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
{
Position += speed * glm::normalize(glm::cross(Orientation, Up));
}
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
{
Position += speed * Up;
}
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
{
Position += speed * -Up;
}
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
{
speed = 0.4f;
}
else if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_RELEASE)
{
speed = 0.1f;
}
// Handles mouse inputs
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
{
// Hides mouse cursor
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
// Prevents camera from jumping on the first click
if (firstClick)
{
glfwSetCursorPos(window, (width / 2), (height / 2));
firstClick = false;
}
// Stores the coordinates of the cursor
double mouseX;
double mouseY;
// Fetches the coordinates of the cursor
glfwGetCursorPos(window, &mouseX, &mouseY);
// Normalizes and shifts the coordinates of the cursor such that they begin in the middle of the screen
// and then "transforms" them into degrees
float rotX = sensitivity * (float)(mouseY - (height / 2)) / height;
float rotY = sensitivity * (float)(mouseX - (width / 2)) / width;
// Calculates upcoming vertical change in the Orientation
glm::vec3 newOrientation = glm::rotate(Orientation, glm::radians(-rotX), glm::normalize(glm::cross(Orientation, Up)));
// Decides whether or not the next vertical Orientation is legal or not
if (abs(glm::angle(newOrientation, Up) - glm::radians(90.0f))
Подробнее здесь: [url]https://stackoverflow.com/questions/79758218/how-to-rotate-camera-like-minecraft-using-glm-and-opengl[/url]
Я хочу иметь возможность повернуть камеру на 90 градусов и вниз на 90 градусов без сбоя, как это могут делать другие игры (например, Minecraft). Как мне это добиться? < /P> Вот камера.[code]#ifndef CAMERA_CLASS_H #define CAMERA_CLASS_H
class Camera { public: // Stores the main vectors of the camera glm::vec3 Position; glm::vec3 Orientation = glm::vec3(0.0f, 0.0f, -1.0f); glm::vec3 Up = glm::vec3(0.0f, 1.0f, 0.0f);
// Prevents the camera from jumping around when first clicking left click bool firstClick = true;
// Stores the width and height of the window int width; int height;
// Adjust the speed of the camera and it's sensitivity when looking around float speed = 0.1f; float sensitivity = 100.0f;
// Camera constructor to set up initial values Camera(int width, int height, glm::vec3 position);
// Updates and exports the camera matrix to the Vertex Shader void Matrix(float FOVdeg, float nearPlane, float farPlane, Shader& shader, const char* uniform); // Handles camera inputs void Inputs(GLFWwindow* window); }; #endif < /code> И вот код cameman.cpp: < /p> #include"Camera.h"
Camera::Camera(int width, int height, glm::vec3 position) { Camera::width = width; Camera::height = height; Position = position; }
void Camera::Matrix(float FOVdeg, float nearPlane, float farPlane, Shader& shader, const char* uniform) { // Initializes matrices since otherwise they will be the null matrix glm::mat4 view = glm::mat4(1.0f); glm::mat4 projection = glm::mat4(1.0f);
// Makes camera look in the right direction from the right position view = glm::lookAt(Position, Position + Orientation, Up); // Adds perspective to the scene projection = glm::perspective(glm::radians(FOVdeg), (float)width / height, nearPlane, farPlane);
// Exports the camera matrix to the Vertex Shader glUniformMatrix4fv(glGetUniformLocation(shader.ID, uniform), 1, GL_FALSE, glm::value_ptr(projection * view)); }
void Camera::Inputs(GLFWwindow* window) { // Handles key inputs if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { Position += speed * Orientation; } if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { Position += speed * -glm::normalize(glm::cross(Orientation, Up)); } if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { Position += speed * -Orientation; } if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { Position += speed * glm::normalize(glm::cross(Orientation, Up)); } if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { Position += speed * Up; } if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) { Position += speed * -Up; } if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) { speed = 0.4f; } else if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_RELEASE) { speed = 0.1f; }
// Prevents camera from jumping on the first click if (firstClick) { glfwSetCursorPos(window, (width / 2), (height / 2)); firstClick = false; }
// Stores the coordinates of the cursor double mouseX; double mouseY; // Fetches the coordinates of the cursor glfwGetCursorPos(window, &mouseX, &mouseY);
// Normalizes and shifts the coordinates of the cursor such that they begin in the middle of the screen // and then "transforms" them into degrees float rotX = sensitivity * (float)(mouseY - (height / 2)) / height; float rotY = sensitivity * (float)(mouseX - (width / 2)) / width;
// Calculates upcoming vertical change in the Orientation glm::vec3 newOrientation = glm::rotate(Orientation, glm::radians(-rotX), glm::normalize(glm::cross(Orientation, Up)));
// Decides whether or not the next vertical Orientation is legal or not if (abs(glm::angle(newOrientation, Up) - glm::radians(90.0f))