Foryxled_Dev
Foryxled_Dev

Reputation: 35

Camera unexpectedly shifts when change window state to fullscreen glfw glm

Every time I press F11 (Fullscreen button), it unexpectedly shifts my camera rotation a bit. Which sometimes shifts to the right, left, down or up. When I use GLFW_CURSOR_NORMAL to see how it works, the cursor goes outside the window for a few frames before the window fullscreen and cursor goes back to the center. What I want is the camera to not shifts the rotation when the window changes it's size (Like an actually game camera do)

Here is my Camera 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)) <= glm::radians(85.0f))
        {
            Orientation = orientation;
        }

        Orientation = glm::rotate(Orientation, glm::radians(-rotateY), Up);

        glfwSetCursorPos(window, (width / 2), (height / 2));
    }

    // Wireframe
    {
        double currentTime = glfwGetTime();
        
        if (glfwGetKey(window, GLFW_KEY_F3) == GLFW_PRESS && currentTime - lastToggleTime > toggleDelay)
        {
            Wireframe();
            lastToggleTime = currentTime;
        }
    }
}

// Other unrelated functions

and here is my fullscreen code (Just in case you need it):

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

Upvotes: -1

Views: 37

Answers (0)

Related Questions