-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.cpp
191 lines (153 loc) · 3.91 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "StdAfx.h"
#include "Camera.h"
//#include "OpenGL.h"
//double posX, posY, posZ; //Camera Position
//double lookAtX, lookAtY, lookAtZ; //Camera LookAt position
//double distModX, distModY, distModZ; //Distance Modifier camera position relative to LookAt
Vector3 position, lookAt, facing;
Actor *focusTarget;
bool focus;
double theta, phi, radius;
double ** camTrack;
double moveSpeed, zoomSpeed, rotateSpeed;
Camera::Camera(double ** cTrack)
{
camTrack = cTrack;
theta = -PI*3/4;
phi = PI*7/16;
radius = 10;
moveSpeed = 20;
rotateSpeed = 1;
zoomSpeed = 3;
setLA(10, 0, 10);
setY();
updateLocation();
}
void Camera::strafe(int forwardDirection, int sideDirection, int timeElapsed)
{
removeFocus();
double xMove=0, zMove=0;
if(forwardDirection != 0)
{
xMove = forwardDirection * moveSpeed * facing.x * (timeElapsed/1000.0);
zMove = forwardDirection * moveSpeed * facing.z * (timeElapsed/1000.0);
}
else
{
xMove = sideDirection * moveSpeed * facing.z * (timeElapsed/1000.0);
zMove = sideDirection * moveSpeed * -facing.x * (timeElapsed/1000.0);
}
setLA(lookAt.x + xMove, 0, lookAt.z + zMove);
setY();
updateLocation();
}
void Camera::zoom(int zoomDirection)
{
radius += zoomDirection * zoomSpeed;
if(radius < 1)
radius = 1;
else if(focus && radius < 10)
radius = 10;
updateLocation();
}
void Camera::rotate(int rotateDirection, int timeElapsed)
{
theta += rotateDirection * rotateSpeed * (timeElapsed/1000.0);
updateLocation();
}
void Camera::setFocus(Actor &a)
{
focusTarget = &a;
focus = true;
if(radius < 10)
radius = 10;
followFocus();
}
void Camera::followFocus()
{
setLA(focusTarget->getLocation().x, focusTarget->getLocation().y, focusTarget->getLocation().z);
updateLocation();
}
void Camera::updateLocation()
{
double x, y, z, xMod, yMod, zMod;
log("Cam Radius: "+dtos(radius)+"\n");
if(hasFocus())
{
xMod = focusTarget->getLocation().x;
yMod = focusTarget->getLocation().y;
zMod = focusTarget->getLocation().z;
}
else
{
xMod = lookAt.x;
yMod = lookAt.y;
zMod = lookAt.z;
}
x = radius*cos(theta)*sin(phi)+xMod;
z = radius*sin(theta)*sin(phi)+zMod;
y = radius*cos(phi)+yMod;
setVector(position, x, y, z);
setVector(facing, lookAt.x - position.x, lookAt.y - position.y, lookAt.z - position.z);
normalize(facing);
}
void Camera::removeFocus()
{
focus = false;
}
bool Camera::hasFocus()
{
return focus;
}
void Camera::setLA(double x, double y, double z)
{
setVector(lookAt, x, y, z);
if(lookAt.x < 0)
lookAt.x = 0;
if(lookAt.x >= MAX_WORLD_SIZE)
lookAt.x = MAX_WORLD_SIZE-1;
if(lookAt.z < 0)
lookAt.z = 0;
if(lookAt.z >= MAX_WORLD_SIZE)
lookAt.z = MAX_WORLD_SIZE-1;
}
void Camera::setY()
{
int lowX = (MAX_WORLD_SIZE-1)-(MAX_WORLD_SIZE-1)/32, lowXi=31, highXi;
int lowZ = (MAX_WORLD_SIZE-1)-(MAX_WORLD_SIZE-1)/32, lowZi=31, highZi;
while(lowX > lookAt.x)
{
lowX -= (MAX_WORLD_SIZE-1)/32;
lowXi -= 1;
}
highXi = lowXi+1;
while(lowZ > lookAt.z)
{
lowZ -= (MAX_WORLD_SIZE-1)/32;
lowZi -= 1;
}
highZi = lowZi+1;
double h1 = cosineInterpolate(camTrack[lowXi][lowZi], camTrack[highXi][lowZi], (lookAt.x-lowX)/((MAX_WORLD_SIZE-1)/32.0));
double h2 = cosineInterpolate(camTrack[lowXi][highZi], camTrack[highXi][highZi], (lookAt.x-lowX)/((MAX_WORLD_SIZE-1)/32.0));
double h3 = cosineInterpolate(camTrack[lowXi][lowZi], camTrack[lowXi][highZi], (lookAt.z-lowZ)/((MAX_WORLD_SIZE-1)/32.0));
double h4 = cosineInterpolate(camTrack[highXi][lowZi], camTrack[highXi][highZi], (lookAt.z-lowZ)/((MAX_WORLD_SIZE-1)/32.0));
double h5 = cosineInterpolate(h1, h2, (lookAt.z-lowZ)/((MAX_WORLD_SIZE-1)/32.0));
double h6 = cosineInterpolate(h3, h4, (lookAt.x-lowX)/((MAX_WORLD_SIZE-1)/32.0));
lookAt.y = (h5+h6)/2;
}
//Prevent the camera from colliding with terrain
void Camera::adjustHeight(double newHeight)
{
position.y = newHeight;
}
Vector3 Camera::getPosition()
{
return position;
}
Vector3 Camera::getLookAt()
{
return lookAt;
}
Camera::~Camera(void)
{
}