forked from bridgecommand/bc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
198 lines (161 loc) · 4.63 KB
/
Camera.cpp
File metadata and controls
198 lines (161 loc) · 4.63 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
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
192
193
194
195
196
197
198
/* Bridge Command 5.0 Ship Simulator
Copyright (C) 2014 James Packer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY Or FITNESS For A PARTICULAR PURPOSE. See the
GNU General Public License For more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
#include "Camera.hpp"
#include "Constants.hpp"
#include <cmath>
using namespace irr;
Camera::Camera()
{
}
Camera::~Camera()
{
//dtor
}
void Camera::load(irr::scene::ISceneManager* smgr, irr::scene::ISceneNode* parent, std::vector<irr::core::vector3df> views, irr::f32 hFOV)
{
this->hFOV = hFOV;
camera = smgr->addCameraSceneNode(0, core::vector3df(0,0,0), core::vector3df(0,0,1));
camera->setFarValue(6*M_IN_NM);//Todo: This should depend on the current (variable) fog range
camera->setFOV(hFOV/camera->getAspectRatio()); //Convert horizontal field of view to vertical
this->parent = parent;
this->views = views;
currentView = 0;
lookAngle = 0;
lookUpAngle = 0;
}
irr::scene::ISceneNode* Camera::getSceneNode() const
{
return camera;
}
irr::core::vector3df Camera::getPosition() const
{
camera->updateAbsolutePosition();//ToDo: This may be needed, but seems odd that it's required
return camera->getAbsolutePosition();
}
void Camera::lookUp()
{
lookUpAngle++;
if (lookUpAngle>40)
{
lookUpAngle=40;
}
}
void Camera::lookDown()
{
lookUpAngle--;
if (lookUpAngle<-40)
{
lookUpAngle=-40;
}
}
void Camera::lookLeft()
{
lookAngle--;
if (lookAngle<0)
{
lookAngle+=360;
}
}
void Camera::lookRight()
{
lookAngle++;
if (lookAngle>=360)
{
lookAngle-=360;
}
}
void Camera::lookAhead()
{
lookAngle = 0;
lookUpAngle = 0;
}
void Camera::lookAstern()
{
lookAngle = 180;
lookUpAngle = 0;
}
void Camera::lookPort()
{
lookAngle = 270;
lookUpAngle = 0;
}
void Camera::lookStbd()
{
lookAngle = 90;
lookUpAngle = 0;
}
irr::f32 Camera::getLook() const
{
return lookAngle;
}
irr::f32 Camera::getLookUp() const
{
return lookUpAngle;
}
void Camera::changeView()
{
currentView++;
if (currentView==views.size()) {
currentView = 0;
}
}
irr::u32 Camera::getView() const
{
return currentView;
}
void Camera::setHFOV(irr::f32 hFOV)
{
this->hFOV=hFOV;
}
void Camera::updateViewport(irr::f32 aspect)
{
camera->setAspectRatio(aspect);
irr::f32 vFOV = 2*atan(tan(hFOV/2)/aspect); //Calculate vertical field of view angle from horizontal one
camera->setFOV(vFOV);
}
void Camera::setActive()
{
camera->getSceneManager()->setActiveCamera(camera);
}
void Camera::setNearValue(irr::f32 zn)
{
camera->setNearValue(zn);
}
void Camera::setFarValue(irr::f32 zf)
{
camera->setFarValue(zf);
}
void Camera::update()
{
//link camera rotation to shipNode
// get transformation matrix of node
core::matrix4 m;
m.setRotationDegrees(parent->getRotation());
// transform forward vector of camera
core::vector3df frv(1.0f*std::sin(irr::core::DEGTORAD*lookAngle), 1.0f*std::sin(irr::core::DEGTORAD*lookUpAngle), 1.0f*std::cos(irr::core::DEGTORAD*lookAngle));
m.transformVect(frv);
// transform upvector of camera
core::vector3df upv(0.0f, 1.0f, 0.0f);
m.transformVect(upv);
// transform camera offset ('offset' is relative to the local ship coordinates, and stays the same.)
//'offsetTransformed' is transformed into the global coordinates
core::vector3df offsetTransformed;
m.transformVect(offsetTransformed,views[currentView]);
//move camera and angle
camera->setPosition(parent->getPosition() + offsetTransformed);
camera->setUpVector(upv); //set up vector of camera
camera->setTarget(parent->getPosition() + offsetTransformed + frv); //set target of camera (look at point)
camera->updateAbsolutePosition();
//also set rotation, so we can get camera's direction
camera->setRotation(parent->getRotation());
}