-
Notifications
You must be signed in to change notification settings - Fork 7
/
Camera.cpp
107 lines (79 loc) · 2.21 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
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
/****************************************************************************
Copyright (C) 2012 Adrian Blumer ([email protected])
Copyright (C) 2012 Pascal Spörri ([email protected])
Copyright (C) 2012 Sabina Schellenberg ([email protected])
All Rights Reserved.
You may use, distribute and modify this code under the terms of the
MIT license (http://opensource.org/licenses/MIT).
*****************************************************************************/
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;
Camera::Camera()
{
SetProjection();
}
void Camera::SetProjection(float angle, float aspect, float near, float far)
{
_angle = angle;
_aspect = aspect;
_near = near;
_far = far;
_projMatrix = glm::perspective(angle,aspect,near,far);
}
void Camera::SetAspectRatio(float aspect)
{
_aspect = aspect;
_projMatrix = glm::perspective(_angle,_aspect,_near,_far);
}
const glm::mat4x4 &Camera::ProjMatrix()
{
return _projMatrix;
}
const glm::mat4x4 &Camera::ViewMatrix()
{
// glm::mat4_cast(_forward);
recomputeViewMatrix();
return _viewMatrix;
}
void Camera::TranslateGlobal(const vec3 &delta)
{
_position += delta;
recomputeViewMatrix();
}
void Camera::TranslateLocal(const vec3 &delta)
{
vec4 d = vec4(delta,0.0f);
d = mat4_cast(inverse(_forward))*d;
_position += vec3(d.x,d.y,d.z);
}
void Camera::LocalRotate(const vec3 &axis, float angle)
{
// deg to rad
float a = M_PI*angle/180.0f;
vec3 ax = normalize(axis);
float sina = sinf(a/2.0f);
float cosa = cosf(a/2.0f);
ax *= sina;
float s = cosa;
fquat offset(s,ax);
_forward = offset * _forward;
}
void Camera::GlobalRotate(const vec3 &axis, float angle)
{
// deg to rad
float a = M_PI*angle/180.0f;
vec3 ax = normalize(axis);
float sina = sinf(a/2.0f);
float cosa = cosf(a/2.0f);
ax *= sina;
float s = cosa;
fquat offset(s,ax);
_forward = _forward*offset;
}
void Camera::recomputeViewMatrix()
{
normalize(_forward);
glm::mat4 Identity = glm::mat4(1.0f); // identity matrix
_viewMatrix = mat4_cast(_forward)*glm::translate(Identity,-_position);
}