-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.cpp
69 lines (52 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
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/constants.hpp>
class Camera {
private:
float fov;
float nearPlane;
float farPlane;
glm::vec3 position;
glm::mat3 rotation;
struct {
float width;
float height;
} viewport = { 800.0f, 450.0f }; //FIXME: Retrieve from platform code or something?!
public:
//FIXME: Supply fov in radians..
Camera() : fov(0.7853981634f), nearPlane(1.0f), farPlane(1000.0f) {}
void setFov(float fov) {
this->fov = fov;
}
void setPosition(glm::vec3 position) {
this->position = position;
}
glm::vec3 getPosition() {
return position;
}
glm::vec2 getViewport() {
return glm::vec2(viewport.width, viewport.height);
}
void lookAt(glm::vec3 point, glm::vec3 up = glm::vec3(0.0f, 0.0f, 1.0f)) {
glm::mat4 m = glm::lookAt(position, point, up); //FIXME: Extract rotation
rotation = glm::mat3(m);
}
//FIXME: a 3x4 / 4x3 would be enough!
glm::mat4 viewMatrix() {
return glm::translate(glm::mat4(rotation), -position); //FIXME: Untested
}
glm::mat4 projectionMatrix() {
return glm::perspectiveFov(fov, viewport.width, viewport.height, nearPlane, farPlane);
}
glm::mat4 viewProjectionMatrix() {
return viewMatrix() * projectionMatrix();
}
glm::vec3 unproject(glm::vec3 windowCoordinates) {
windowCoordinates.y = viewport.height - windowCoordinates.y;
return glm::unProject(windowCoordinates, viewMatrix(), projectionMatrix(), glm::vec4(0.0, 0.0, viewport.width, viewport.height));
}
glm::vec3 project(glm::vec3 objectCoordinates) {
glm::vec3 windowCoordinates = glm::project(objectCoordinates, viewMatrix(), projectionMatrix(), glm::vec4(0.0, 0.0, viewport.width, viewport.height));
windowCoordinates.y = viewport.height - windowCoordinates.y;
return windowCoordinates;
}
};