/
Camera.h
160 lines (115 loc) · 4.76 KB
/
Camera.h
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
tdogl::Camera
Copyright 2012 Thomas Dalling - http://tomdalling.com/
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once
#include <glm/glm.hpp>
namespace tdogl {
/**
A first-person shooter type of camera.
Set the properties of the camera, then use the `matrix` method to get the camera matrix for
use in the vertex shader.
Includes the perspective projection matrix.
*/
class Camera {
public:
Camera();
/**
The position of the camera.
*/
const glm::vec3& position() const;
void setPosition(const glm::vec3& position);
void offsetPosition(const glm::vec3& offset);
/**
The vertical viewing angle of the camera, in degrees.
Determines how "wide" the view of the camera is. Large angles appear to be zoomed out,
as the camera has a wide view. Small values appear to be zoomed in, as the camera has a
very narrow view.
The value must be between 0 and 180.
*/
float fieldOfView() const;
void setFieldOfView(float fieldOfView);
/**
The closest visible distance from the camera.
Objects that are closer to the camera than the near plane distance will not be visible.
Value must be greater than 0.
*/
float nearPlane() const;
/**
The farthest visible distance from the camera.
Objects that are further away from the than the far plane distance will not be visible.
Value must be greater than the near plane
*/
float farPlane() const;
/**
Sets the near and far plane distances.
Everything between the near plane and the var plane will be visible. Everything closer
than the near plane, or farther than the far plane, will not be visible.
@param nearPlane Minimum visible distance from camera. Must be > 0
@param farPlane Maximum visible distance from vamera. Must be > nearPlane
*/
void setNearAndFarPlanes(float nearPlane, float farPlane);
/**
A rotation matrix that determines the direction the camera is looking.
Does not include translation (the camera's position).
*/
glm::mat4 orientation() const;
/**
Offsets the cameras orientation.
The verticle angle is constrained between 85deg and -85deg to avoid gimbal lock.
@param upAngle the angle (in degrees) to offset upwards. Negative values are downwards.
@param rightAngle the angle (in degrees) to offset rightwards. Negative values are leftwards.
*/
void offsetOrientation(float upAngle, float rightAngle);
/**
Orients the camera so that is it directly facing `position`
@param position the position to look at
*/
void lookAt(glm::vec3 position);
/**
The width divided by the height of the screen/window/viewport
Incorrect values will make the 3D scene look stretched.
*/
float viewportAspectRatio() const;
void setViewportAspectRatio(float viewportAspectRatio);
/** A unit vector representing the direction the camera is facing */
glm::vec3 forward() const;
/** A unit vector representing the direction to the right of the camera*/
glm::vec3 right() const;
/** A unit vector representing the direction out of the top of the camera*/
glm::vec3 up() const;
/**
The combined camera transformation matrix, including perspective projection.
This is the complete matrix to use in the vertex shader.
*/
glm::mat4 matrix() const;
/**
The perspective projection transformation matrix
*/
glm::mat4 projection() const;
/**
The translation and rotation matrix of the camera.
Same as the `matrix` method, except the return value does not include the projection
transformation.
*/
glm::mat4 view() const;
private:
glm::vec3 _position;
float _horizontalAngle;
float _verticalAngle;
float _fieldOfView;
float _nearPlane;
float _farPlane;
float _viewportAspectRatio;
void normalizeAngles();
};
}