CameraManager.cpp 5.8 KB
Newer Older
1
#include <iostream>
2
3
4
5
#include "vkcv/camera/CameraManager.hpp"

namespace vkcv{

6
7
    CameraManager::CameraManager(Window &window, float width, float height)
    : m_window(window)
8
    {
9
        bindCameraToEvents();
10
11
12
        m_activeCameraIndex = 0;
        m_lastX = window.getWidth() / 2.0f;
        m_lastY = window.getHeight() / 2.0f;
13
14
15

    }

16
17
    CameraManager::~CameraManager() {}

18
    void CameraManager::bindCameraToEvents() {
19
20
21
        m_keyHandle = m_window.e_key.add( [&](int key, int scancode, int action, int mods) { this->keyCallback(key, scancode, action, mods); });
        m_mouseMoveHandle = m_window.e_mouseMove.add( [&]( double offsetX, double offsetY) {this->mouseMoveCallback( offsetX, offsetY);} );
        m_mouseScrollHandle =  m_window.e_mouseScroll.add([&](double offsetX, double offsetY) {this->scrollCallback( offsetX, offsetY);} );
Sebastian Gaida's avatar
Sebastian Gaida committed
22
        m_mouseButtonHandle = m_window.e_mouseButton.add([&] (int button, int action, int mods) {this->mouseButtonCallback( button,  action,  mods);});
23
        m_resizeHandle = m_window.e_resize.add([&](int width, int height) {this->resizeCallback(width, height);});
Sebastian Gaida's avatar
Sebastian Gaida committed
24
    }
25

26
27
28
    void CameraManager::resizeCallback(int width, int height) {
        for (size_t i = 0; i < m_cameras.size(); i++) {
            getCamera(i).setRatio(static_cast<float>(width) / static_cast<float>(height));;
Sebastian Gaida's avatar
Sebastian Gaida committed
29
        }
30
31
    }

32
    void CameraManager::mouseButtonCallback(int button, int action, int mods){
33
34
35
36
37
38
39
        if(button == GLFW_MOUSE_BUTTON_2 && action == GLFW_PRESS){
            glfwSetInputMode(m_window.getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED);
        }
        else if(button == GLFW_MOUSE_BUTTON_2 && action == GLFW_RELEASE){
            glfwSetInputMode(m_window.getWindow(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
        }
        getControllerByType(getControllerType(getActiveCameraIndex())).mouseButtonCallback(button, action, mods, getActiveCamera());
40
41
    }

42
    void CameraManager::mouseMoveCallback(double x, double y){
43
44
45
46
47
48
49
        auto xoffset = static_cast<float>(x - m_lastX);
		auto yoffset = static_cast<float>(y - m_lastY);
        
        m_lastX = x;
        m_lastY = y;

        getControllerByType(getControllerType(getActiveCameraIndex())).mouseMoveCallback(xoffset, yoffset, getActiveCamera());
50
    }
51

52
    void CameraManager::scrollCallback(double offsetX, double offsetY) {
53
54

        getControllerByType(getControllerType(getActiveCameraIndex())).scrollCallback(offsetX, offsetY, getActiveCamera());
55
    }
Sebastian Gaida's avatar
Sebastian Gaida committed
56

57
58
59
60
61
    void CameraManager::keyCallback(int key, int scancode, int action, int mods)  {
        switch (action) {
            case GLFW_RELEASE:
                switch (key) {
                    case GLFW_KEY_TAB:
62
63
                        if (m_activeCameraIndex + 1 == m_cameras.size()) {
                            m_activeCameraIndex = 0;
64
65
                        }
                        else {
66
                            m_activeCameraIndex++;
67
68
69
70
71
72
73
                        }
                        return;
                    case GLFW_KEY_ESCAPE:
                        glfwSetWindowShouldClose(m_window.getWindow(), 1);
                        return;
                }
            default:
74
                getControllerByType(getControllerType(getActiveCameraIndex())).keyCallback(key, scancode, action, mods, getActiveCamera());
75
        }
76
        
77
    }
78

79
    int CameraManager::addCamera() {
80
81
82
83
        Camera camera;
        m_cameras.push_back(camera);  // TODO: is there another way we can do this?
        m_cameras.back().setPerspective(glm::radians(60.0f), m_window.getWidth() / m_window.getHeight(), 0.1f, 10.0f);
        m_cameraControllerTypes.push_back(ControllerType::NONE);
84
        return m_cameras.size() - 1;
85
86
    }

87
88
89
90
91
92
    int CameraManager::addCamera(ControllerType controllerType) {
        Camera camera;
        m_cameras.push_back(camera);  // TODO: is there another way we can do this?
        m_cameras.back().setPerspective(glm::radians(60.0f), m_window.getWidth() / m_window.getHeight(), 0.1f, 10.0f);
        m_cameraControllerTypes.push_back(controllerType);
        return m_cameras.size() - 1;
93
94
    }

95
    Camera& CameraManager::getCamera(uint32_t cameraIndex) {
96
97
98
        if (cameraIndex < 0 || cameraIndex > m_cameras.size() - 1) {
            throw std::runtime_error("Invalid camera index.");
        }
99
100
        return m_cameras[cameraIndex];
    }
101

102
103
104
    Camera& CameraManager::getActiveCamera() {
        return m_cameras[getActiveCameraIndex()];
    }
105

106
    void CameraManager::setActiveCamera(uint32_t cameraIndex) {
107
108
109
        if (cameraIndex < 0 || cameraIndex > m_cameras.size() - 1) {
            throw std::runtime_error("Invalid camera index.");
        }
110
        m_activeCameraIndex = cameraIndex;
111
    }
112

113
114
    uint32_t CameraManager::getActiveCameraIndex() {
        return m_activeCameraIndex;
115
116
    }

117
    void CameraManager::setControllerType(uint32_t cameraIndex, ControllerType controllerType) {
118
119
120
        if (cameraIndex < 0 || cameraIndex > m_cameras.size() - 1) {
            throw std::runtime_error("Invalid camera index.");
        }
121
        m_cameraControllerTypes[cameraIndex] = controllerType;
122
123
    }

124
    ControllerType CameraManager::getControllerType(uint32_t cameraIndex) {
125
126
127
        if (cameraIndex < 0 || cameraIndex > m_cameras.size() - 1) {
            throw std::runtime_error("Invalid camera index.");
        }
128
        return m_cameraControllerTypes[cameraIndex];
129
    }
130

131
132
133
134
135
136
137
138
139
    CameraController& CameraManager::getControllerByType(ControllerType controllerType) {
        switch(controllerType) {
            case ControllerType::PILOT:
                return m_pilotController;
            case ControllerType::TRACKBALL:
                return m_trackController;
            default:
                return m_pilotController;
        }
140
141
142
    }

    void CameraManager::update(double deltaTime) {
143
144
            getControllerByType(getControllerType(getActiveCameraIndex())).updateCamera(deltaTime, getActiveCamera());
        }
145
}