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

namespace vkcv {

6
7
    Camera::Camera() {
        m_position = glm::vec3(0.0f, 0.0f, -1.0f);
8
        m_up = glm::vec3(0.0f, 1.0f, 0.0f);
9
10
11
        m_center = glm::vec3(0.0f, 0.0f, 0.0f);
        lookAt(m_position, m_center, m_up);
        glm::vec3 front = glm::normalize(m_center - m_position);
12
13
        m_pitch = std::atan2(front.y, std::sqrt(front.x * front.x + front.z * front.z));
        m_yaw = std::atan2(front.x, front.z);
14
    }
15
16
17
18

    Camera::~Camera() = default;

    void Camera::lookAt(glm::vec3 position, glm::vec3 center, glm::vec3 up){
19
20
21
22
		m_position = position;
		m_center = center;
		m_up = up;
        m_view = glm::lookAt(m_position, m_center, m_up);
23
24
    }

25
    void Camera::getNearFar( float &near, float &far) const {
26
27
28
29
        near = m_near;
        far = m_far;
    }

30
    glm::mat4& Camera::getView() {
31
32
33
        return m_view;
    }

34
    glm::mat4& Camera::getProjection() {
35
36
37
        return m_projection;
    }

38
    void Camera::setProjection(const glm::mat4 projection){
39
40
41
        m_projection = projection;
    }

42
43
44
45
    glm::mat4 Camera::getMVP() const {
        return m_projection * m_view;
    }

46
    float Camera::getFov() const {
47
48
49
50
        return m_fov;
    }

    void Camera::setFov( float fov){
51
        setPerspective(fov, m_ratio, m_near, m_far);
52
53
    }

54
55
    float Camera::getRatio() const {
        return m_ratio;
56
57
    }

58
    void Camera::setRatio(float ratio){
59
        setPerspective( m_fov, ratio, m_near, m_far);
60
61
    }

62
63
    void Camera::setNearFar(float near, float far){
        setPerspective(m_fov, m_ratio, near, far);
64
65
    }

66
67
68
69
70
71
72
73
    void Camera::setPerspective(float fov, float ratio, float near, float far) {
    	const glm::mat4 y_correction (
    			1.0f,  0.0f,  0.0f,  0.0f,
    			0.0f, -1.0f,  0.0f,  0.0f,
    			0.0f,  0.0f,  1.0f,  0.0f,
    			0.0f,  0.0f,  0.0f,  1.0f
		);
    	
74
75
76
77
        m_fov = fov;
        m_ratio = ratio;
        m_near = near;
        m_far = far;
78
        m_projection = y_correction * glm::perspective(m_fov, m_ratio, m_near, m_far);
79
80
    }

81
    glm::vec3 Camera::getFront() const {
82
        glm::vec3 direction;
83
84
85
        direction.x = std::sin(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
        direction.y = std::sin(glm::radians(m_pitch));
        direction.z = std::cos(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
86
        return glm::normalize(direction);
87
88
    }

89
    glm::vec3 Camera::getPosition() const {
90
        return m_position;
91
92
93
    }

    void Camera::setPosition( glm::vec3 position ){
94
        m_position = position;
95
    }
96

97
98
99
100
101
102
    glm::vec3 Camera::getCenter() const {
        return m_center;
    }

    void Camera::setCenter(glm::vec3 center) {
        m_center = center;
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    }

    float Camera::getPitch() const {
        return m_pitch;
    }

    void Camera::setPitch(float pitch) {
        m_pitch = pitch;
    }

    float Camera::getYaw() const {
        return m_yaw;
    }

    void Camera::setYaw(float yaw) {
        m_yaw = yaw;
    }

121
122
    glm::vec3 Camera::getUp() const {
        return m_up;
123
    }
124

125
126
    void Camera::setUp(const glm::vec3 &up) {
        m_up = up;
127
128
    }

129
}