-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.cpp
72 lines (59 loc) · 1.8 KB
/
camera.cpp
1
2
3
4
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
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
#include "camera.hpp"
#include "uniform.hpp"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include <glm/gtx/rotate_vector.hpp>
using namespace plush;
Camera::Camera(const glm::vec3 &coord, float fov, float aspect)
: m_fov(fov), m_aspect(aspect), m_coord(coord), m_azimuth(0.0f)
{
}
void Camera::setCoord(const glm::vec3 &newCoord)
{
m_coord = newCoord;
}
void Camera::setAzimuth(float azimuth)
{
m_azimuth = remainder(azimuth, 360.0f);
}
void Camera::setElevation(float elevation)
{
m_elevation = remainder(elevation, 360.0f);
}
glm::mat4 Camera::viewMatrix() const
{
glm::mat4 m = glm::mat4(1.0f);
m = glm::rotate(m, -m_elevation, glm::vec3(1.0f, 0.0f, 0.0f));
m = glm::rotate(m, m_azimuth, glm::vec3(0.0f, 1.0f, 0.0f));
m = glm::translate(m, -m_coord);
return m;
}
glm::mat4 Camera::projectionMatrix() const
{
return glm::perspective(45.0f, 1.33f, 1.0f, 10.0f);
}
glm::mat4 Camera::eyeToWorldMatrix() const
{
return glm::affineInverse(viewMatrix());
}
glm::vec3 Camera::forwardVector() const
{
glm::mat4 eyeToWorldMatrix = glm::affineInverse(viewMatrix());
glm::vec4 result = eyeToWorldMatrix * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f);
return glm::vec3(result);
}
glm::vec3 Camera::walkVector() const
{
return glm::vec3(glm::rotate(glm::vec4(0.0f, 0.0f, -1.0f, 0.0f), -m_azimuth, glm::vec3(0.0f, 1.0f, 0.0f)));
}
void Camera::upload(UniformMat4 &uniform_viewMatrix, UniformMat4 &uniform_projectionMatrix)
{
glm::mat4 m;
m = viewMatrix();
uniform_viewMatrix.set(m);
//glUniformMatrix4fv(uniform_viewMatrix, 1, GL_FALSE, glm::value_ptr(m));
m = projectionMatrix();
uniform_projectionMatrix.set(m);
//glUniformMatrix4fv(uniform_projectionMatrix, 1, GL_FALSE, glm::value_ptr(m));
}