Anonymous
Камера неожиданно сдвигается при изменении состояния окна на полноэкранную GLFW GLM
Сообщение
Anonymous » 02 мар 2025, 08:22
Каждый раз, когда я нажимаю F11 (кнопка полноэкранного экрана), она неожиданно немного меняет вращение камеры. Который иногда смещается вправо, влево, вниз или вверх. Когда я использую GLFW_CURSOR_NORMAL , чтобы увидеть, как он работает, курсор выходит за пределы окна для нескольких кадров, прежде чем окно полноэкранное и курсор вернется в центр. Я пытался использовать CHATGPT, но, кажется, не работает. Я хочу, чтобы камера не сдвигала вращение, когда окно меняется его размер (например, на самом деле игровой камеру) < /p>
Вот моя камера: < /p>
Код: Выделить всё
Camera::Camera(GLFWwindow* window, int width, int height, glm::vec3 position) : width(width), height(height), Position(position)
{
glfwSetCursorPos(window, (width / 2), (height / 2));
}
void Camera::Update(GLFWwindow* window, float FOVdegree, float nearPlane, float farPlane)
{
glfwGetWindowSize(window, &width, &height);
Model = glm::mat4(1.0f);
View = glm::mat4(1.0f);
Projection = glm::mat4(1.0f);
View = glm::lookAt(Position, Position + Orientation, Up);
Projection = glm::perspective(glm::radians(FOVdegree), (float)width / height, nearPlane, farPlane);
CameraMatrix = Projection * View * Model;
}
void Camera::Matrix(const Shader& Shader, const char* uniform)
{
glUniformMatrix4fv(glGetUniformLocation(Shader.programID, uniform), 1, GL_FALSE, glm::value_ptr(CameraMatrix));
}
void Camera::Input(GLFWwindow* window)
{
// Speed calculation
this->Framerate();
speed = this->Speed * deltaTime * (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) ? SprintSpeed : 1.0f);
// Keyboard
{
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_E) == GLFW_PRESS)
Position += speed * Up;
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
Position += speed * -Up;
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
Position += speed * -Up;
}
// Mouse
{
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
double mouseX, mouseY;
glfwGetCursorPos(window, &mouseX, &mouseY);
float rotateX = sensitivity * (float)(mouseY - (height / 2)) / height;
float rotateY = sensitivity * (float)(mouseX - (width / 2)) / width;
glm::vec3 orientation = glm::rotate(Orientation, glm::radians(-rotateX), glm::normalize(glm::cross(Orientation, Up)));
if (glm::abs(glm::angle(orientation, Up) - glm::radians(90.0f)) toggleDelay)
{
Wireframe();
lastToggleTime = currentTime;
}
}
}
// Other unrelated functions
< /code>
И вот мой полноэкранный код (на случай, если он вам понадобится): < /p>
void Window::Fullscreen()
{
if (glfwGetWindowMonitor(window) == nullptr)
{
glfwGetWindowSize(window, &width, &height);
glfwGetWindowPos(window, &x, &y);
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* videomode = (GLFWvidmode*)glfwGetVideoMode(monitor);
glfwSetWindowMonitor(window, monitor, 0, 0, videomode->width, videomode->height, videomode->refreshRate);
glfwSwapBuffers(window);
}
else
{
glfwSetWindowMonitor(window, nullptr, x, y, width, height, 0);
glfwSwapBuffers(window);
}
}
Спасибо!
Подробнее здесь:
https://stackoverflow.com/questions/794 ... n-glfw-glm
1740892960
Anonymous
Каждый раз, когда я нажимаю F11 (кнопка полноэкранного экрана), она неожиданно немного меняет вращение камеры. Который иногда смещается вправо, влево, вниз или вверх. Когда я использую GLFW_CURSOR_NORMAL , чтобы увидеть, как он работает, курсор выходит за пределы окна для нескольких кадров, прежде чем окно полноэкранное и курсор вернется в центр. Я пытался использовать CHATGPT, но, кажется, не работает. Я хочу, чтобы камера не сдвигала вращение, когда окно меняется его размер (например, на самом деле игровой камеру) < /p> Вот моя камера: < /p> [code]Camera::Camera(GLFWwindow* window, int width, int height, glm::vec3 position) : width(width), height(height), Position(position) { glfwSetCursorPos(window, (width / 2), (height / 2)); } void Camera::Update(GLFWwindow* window, float FOVdegree, float nearPlane, float farPlane) { glfwGetWindowSize(window, &width, &height); Model = glm::mat4(1.0f); View = glm::mat4(1.0f); Projection = glm::mat4(1.0f); View = glm::lookAt(Position, Position + Orientation, Up); Projection = glm::perspective(glm::radians(FOVdegree), (float)width / height, nearPlane, farPlane); CameraMatrix = Projection * View * Model; } void Camera::Matrix(const Shader& Shader, const char* uniform) { glUniformMatrix4fv(glGetUniformLocation(Shader.programID, uniform), 1, GL_FALSE, glm::value_ptr(CameraMatrix)); } void Camera::Input(GLFWwindow* window) { // Speed calculation this->Framerate(); speed = this->Speed * deltaTime * (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) ? SprintSpeed : 1.0f); // Keyboard { 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_E) == GLFW_PRESS) Position += speed * Up; if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) Position += speed * -Up; if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) Position += speed * -Up; } // Mouse { glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); double mouseX, mouseY; glfwGetCursorPos(window, &mouseX, &mouseY); float rotateX = sensitivity * (float)(mouseY - (height / 2)) / height; float rotateY = sensitivity * (float)(mouseX - (width / 2)) / width; glm::vec3 orientation = glm::rotate(Orientation, glm::radians(-rotateX), glm::normalize(glm::cross(Orientation, Up))); if (glm::abs(glm::angle(orientation, Up) - glm::radians(90.0f)) toggleDelay) { Wireframe(); lastToggleTime = currentTime; } } } // Other unrelated functions < /code> И вот мой полноэкранный код (на случай, если он вам понадобится): < /p> void Window::Fullscreen() { if (glfwGetWindowMonitor(window) == nullptr) { glfwGetWindowSize(window, &width, &height); glfwGetWindowPos(window, &x, &y); GLFWmonitor* monitor = glfwGetPrimaryMonitor(); const GLFWvidmode* videomode = (GLFWvidmode*)glfwGetVideoMode(monitor); glfwSetWindowMonitor(window, monitor, 0, 0, videomode->width, videomode->height, videomode->refreshRate); glfwSwapBuffers(window); } else { glfwSetWindowMonitor(window, nullptr, x, y, width, height, 0); glfwSwapBuffers(window); } } [/code] Спасибо! Подробнее здесь: [url]https://stackoverflow.com/questions/79478657/camera-unexpectedly-shifts-when-change-window-state-to-fullscreen-glfw-glm[/url]