PilotCameraController.cpp 5.54 KB
Newer Older
1
#include "vkcv/camera/PilotCameraController.hpp"
2

3
#include <iostream>
4
#include <GLFW/glfw3.h>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

namespace vkcv {

    PilotCameraController::PilotCameraController() {
        m_forward = false;
        m_backward = false;
        m_upward = false;
        m_downward = false;
        m_left = false;
        m_right = false;

        m_rotationActive = false;

        m_cameraSpeed = 2.0f;

        m_fov_nsteps = 100;
        m_fov_min = 10;
        m_fov_max = 120;

        m_lastX = 0.0;
        m_lastY = 0.0;
    }

    void PilotCameraController::changeFov(double offset){
        float fov = m_camera->getFov();
        float fov_range = m_fov_max - m_fov_min;
31
        float fov_stepsize = glm::radians(fov_range) / static_cast<float>(m_fov_nsteps);
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
87
88
89
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
115
116
117
118
119
120
121
122
123
124
125
        fov -= (float) offset*fov_stepsize;
        if (fov < glm::radians(m_fov_min)) {
            fov = glm::radians(m_fov_min);
        }
        if (fov > glm::radians(m_fov_max)) {
            fov = glm::radians(m_fov_max);
        }
        m_camera->setFov(fov);
    }

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

        // handle pitch rotation
        float pitch = m_camera->getPitch() - yOffset;
        if (pitch > 89.0f) {
            pitch = 89.0f;
        }
        if (pitch < -89.0f) {
            pitch = -89.0f;
        }
        m_camera->setPitch(pitch);
    }

    glm::mat4 PilotCameraController::updateView(double deltaTime){
        updatePosition(deltaTime);
        glm::vec3 position = m_camera->getPosition();
        glm::vec3 front = m_camera->getFront();
        glm::vec3 up = m_camera->getUp();
        m_camera->lookAt(position, position + front, up);
        return m_camera->getView();
    }

    glm::vec3 PilotCameraController::updatePosition(double deltaTime ){
        glm::vec3 position = m_camera->getPosition();
        glm::vec3 front = m_camera->getFront();
        glm::vec3 up = m_camera->getUp();
        position += (m_cameraSpeed * front * static_cast<float> (m_forward) * static_cast<float>(deltaTime));
        position -= (m_cameraSpeed * front * static_cast<float> (m_backward) * static_cast<float>(deltaTime));
        position += (glm::normalize(glm::cross(front, up)) * m_cameraSpeed * static_cast<float> (m_left) * static_cast<float>(deltaTime));
        position -= (glm::normalize(glm::cross(front, up)) * m_cameraSpeed * static_cast<float> (m_right) * static_cast<float>(deltaTime));
        position -= up * m_cameraSpeed * static_cast<float> (m_upward) * static_cast<float>(deltaTime);
        position += up * m_cameraSpeed * static_cast<float> (m_downward) * static_cast<float>(deltaTime);
        m_camera->setPosition(position);
        return position;
    }

    void PilotCameraController::setCamera(Camera &camera) {
        m_camera = &camera;
        m_camera->setYaw(180.0f);
    }

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

    void PilotCameraController::keyCallback(int key, int scancode, int action, int mods) {
        switch (key) {
            case GLFW_KEY_W:
                moveForward(action);
                break;
            case GLFW_KEY_S:
                moveBackward(action);
                break;
            case GLFW_KEY_A:
                moveLeft(action);
                break;
            case GLFW_KEY_D:
                moveRight(action);
                break;
            case GLFW_KEY_E:
                moveUpward(action);
                break;
            case GLFW_KEY_Q:
                moveDownward(action);
                break;
            default:
                break;
        }
    }

    void PilotCameraController::scrollCallback(double offsetX, double offsetY) {
        changeFov(offsetY);
    }

    void PilotCameraController::mouseMoveCallback(double x, double y) {
126
127
128
		auto xoffset = static_cast<float>(x - m_lastX);
		auto yoffset = static_cast<float>(y - m_lastY);
        
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
        m_lastX = x;
        m_lastY = y;

        if(!m_rotationActive){
            return;
        }

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

        panView(xoffset , yoffset);
    }

    void PilotCameraController::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;
        }
    }


    void PilotCameraController::moveForward(int action){
        m_forward = static_cast<bool>(action);
    }

    void PilotCameraController::moveBackward(int action){
        m_backward = static_cast<bool>(action);
    }

    void PilotCameraController::moveLeft(int action){
        m_left = static_cast<bool>(action);
    }

    void PilotCameraController::moveRight(int action){
        m_right = static_cast<bool>(action);
    }

    void PilotCameraController::moveUpward(int action){
        m_upward = static_cast<bool>(action);
    }

    void PilotCameraController::moveDownward(int action){
        m_downward = static_cast<bool>(action);
    }

}