-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.cpp
More file actions
148 lines (120 loc) · 3.52 KB
/
camera.cpp
File metadata and controls
148 lines (120 loc) · 3.52 KB
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
#include "camera.h"
#if defined(__APPLE__)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
const double Camera::TO_RADS = 3.141592654 / 180.0; // The value of 1 degree in radians
Camera::Camera()
{
initCamera();
}
Camera::~Camera()
{
}
void Camera::initCamera()
{
lastX = 0;
lastY = 0;
// Set position, rotation and speed values to zero
position.zero();
rotation.zero();
speed.zero();
rotation.addX(180);
// How fast we move (higher values mean we move and strafe faster)
movementSpeedFactor = 12.5;
pitchSensitivity = 0.2; // How sensitive mouse movements affect looking up and down
yawSensitivity = 0.2; // How sensitive mouse movements affect looking left and right
// To begin with, we aren't holding down any keys
holdingForward = false;
holdingBackward = false;
holdingLeftStrafe = false;
holdingRightStrafe = false;
}
// Function to convert degrees to radians
const double Camera::toRads(const double &theAngleInDegrees) const
{
return theAngleInDegrees * TO_RADS;
}
// Function to deal with mouse position changes
void Camera::handleMouseMove(int mouseX, int mouseY)
{
rotation.addY((lastX - mouseX) * yawSensitivity);
rotation.addX((lastY - mouseY) * pitchSensitivity);
lastX = mouseX;
lastY = mouseY;
}
void Camera::rotateLeft()
{
rotation.addZ(5*pitchSensitivity);
}
void Camera::rotateRight()
{
rotation.addZ(-5*pitchSensitivity);
}
// Function to calculate which direction we need to move the camera and by what amount
void Camera::move(double deltaTime)
{
// Vector to break up our movement into components along the X, Y and Z axis
Vec3<double> movement;
// Get the sine and cosine of our X and Y axis rotation
double sinXRot = sin( toRads( rotation.getX() ) );
double cosXRot = cos( toRads( rotation.getX() ) );
double sinYRot = sin( toRads( rotation.getY() ) );
double cosYRot = cos( toRads( rotation.getY() ) );
double pitchLimitFactor = cosXRot; // This cancels out moving on the Z axis when we're looking up or down
if (holdingForward)
{
movement.addX(sinYRot * pitchLimitFactor);
movement.addY(-sinXRot);
movement.addZ(-cosYRot * pitchLimitFactor);
}
if (holdingBackward)
{
movement.addX(-sinYRot * pitchLimitFactor);
movement.addY(sinXRot);
movement.addZ(cosYRot * pitchLimitFactor);
}
if (holdingLeftStrafe)
{
movement.addX(-cosYRot);
movement.addZ(-sinYRot);
}
if (holdingRightStrafe)
{
movement.addX(cosYRot);
movement.addZ(sinYRot);
}
// Normalise our movement vector
movement.normalise();
// Calculate our value to keep the movement the same speed regardless of the framerate...
double framerateIndependentFactor = movementSpeedFactor * deltaTime;
// .. and then apply it to our movement vector.
movement *= framerateIndependentFactor;
// Finally, apply the movement to our position
position += movement;
}
void Camera::mouseButtonPressed(int button, int state, int x, int y)
{
if (state == GLUT_DOWN)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
lastX = x;
lastY = y;
break;
// case 3:
// zoom *= 1.2f;
// break;
// case 4:
// zoom /= 1.2f;
// break;
}
}
else if (state == GLUT_UP && button == GLUT_LEFT_BUTTON)
{
lastX = -1;
lastY = -1;
}
}