TrackballCameraController.cpp 3.66 KB
Newer Older
1
2
#include "vkcv/camera/TrackballCameraController.hpp"

3
4
#include <GLFW/glfw3.h>

5
6
7
8
9
10
namespace vkcv {

    TrackballCameraController::TrackballCameraController() {
        m_rotationActive = false;
        m_radius = 3.0f;
        m_cameraSpeed = 2.5f;
11
        m_scrollSensitivity = 0.2f;
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    }

    void TrackballCameraController::setRadius(const float radius) {
        if (radius < 0.1f) {
            m_radius = 0.1f;
        }
        else {
            m_radius = radius;
        }
    }

    void TrackballCameraController::panView(double xOffset, double yOffset) {
        // handle yaw rotation
        float yaw = m_camera->getYaw() + xOffset * m_cameraSpeed;
        if (yaw < 0.0f) {
            yaw += 360.0f;
        }
        else if (yaw > 360.0f) {
            yaw -= 360.0f;
        }
        m_camera->setYaw(yaw);

        // handle pitch rotation
        float pitch = m_camera->getPitch() + yOffset * m_cameraSpeed;
        if (pitch < 0.0f) {
            pitch += 360.0f;
        }
        else if (pitch > 360.0f) {
            pitch -= 360.0f;
        }
        m_camera->setPitch(pitch);
    }

    glm::vec3 TrackballCameraController::updatePosition() {
        float yaw = m_camera->getYaw();
        float pitch = m_camera->getPitch();
        glm::vec3 yAxis = glm::vec3(0.0f, 1.0f, 0.0f);
        glm::vec3 xAxis = glm::vec3(1.0f, 0.0f, 0.0f);

        glm::mat4 rotationY = glm::rotate(glm::mat4(1.0f), glm::radians(yaw), yAxis);
        glm::mat4 rotationX = glm::rotate(rotationY, glm::radians(pitch), xAxis);
        glm::vec3 translate = glm::vec3(0.0f, 0.0f, m_radius);
        translate = glm::vec3(rotationX * glm::vec4(translate, 0.0f));
        glm::vec3 center = m_camera->getCenter();
        glm::vec3 position = center +translate;
        m_camera->setPosition(position);
        glm::vec3 up = glm::vec3(rotationX * glm::vec4(glm::vec3(0.0f, 1.0f, 0.0f), 0.0f));
        m_camera->setUp(up);
        return position;
    }

    glm::mat4 TrackballCameraController::updateView() {
        updatePosition();
        glm::vec3 position = m_camera->getPosition();
        glm::vec3 center = m_camera->getCenter();
        glm::vec3 up = m_camera->getUp();
        m_camera->lookAt(position, center, up);
        return m_camera->getView();
    }

    void TrackballCameraController::updateRadius(double offset) {
        setRadius(m_radius - offset * m_scrollSensitivity);
    }

    void TrackballCameraController::updateCamera(double deltaTime) {
        updateView();
    }

    void TrackballCameraController::keyCallback(int key, int scancode, int action, int mods) {}

    void TrackballCameraController::scrollCallback(double offsetX, double offsetY) {
        updateRadius(offsetY);
    }

    void TrackballCameraController::mouseMoveCallback(double x, double y) {
87
88
89
        auto xoffset = static_cast<float>(x - m_lastX);
        auto yoffset = static_cast<float>(y - m_lastY);
        
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
        m_lastX = x;
        m_lastY = y;

        if(!m_rotationActive){
            return;
        }

        float sensitivity = 0.05f;
        xoffset *= sensitivity;
        yoffset *= sensitivity;

        panView(xoffset , yoffset);
    }

    void TrackballCameraController::mouseButtonCallback(int button, int action, int mods) {
        if(button == GLFW_MOUSE_BUTTON_2 && m_rotationActive == false && action == GLFW_PRESS){
            glfwSetInputMode(m_window->getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
            m_rotationActive = true;
        }
        else if(button == GLFW_MOUSE_BUTTON_2 && m_rotationActive == true && action == GLFW_RELEASE){
            glfwSetInputMode(m_window->getWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
            m_rotationActive = false;
        }
    }
}