/
Camera.cpp
252 lines (215 loc) · 7.52 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*******************************************************************************
* Ruthenium Engine *
* Copyright (c) 2013-2017 Stepanov Dmitriy aka mrDIMAS *
* *
* This file is part of Ruthenium Engine. *
* *
* Ruthenium Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* Ruthenium Engine 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with Ruthenium Engine. If not, see <http://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#include "Precompiled.h"
#include "Renderer.h"
#include "SceneNode.h"
#include "AABB.h"
#include "Camera.h"
#include "Skybox.h"
#include "PointLight.h"
#include "SceneFactory.h"
weak_ptr<Camera> Camera::msCurrentCamera;
void Camera::Update() {
CalculateGlobalTransform();
btVector3 eye = mGlobalTransform.getOrigin();
btVector3 look = eye + mGlobalTransform.getBasis().getColumn(2);
btVector3 up = mGlobalTransform.getBasis().getColumn(1);
btVector3 normlook = mGlobalTransform.getBasis().getColumn(2).normalized();
float f_up[3] = {0.0f, 1.0f, 0.0f};
// sound listener
pfSetListenerOrientation(normlook.m_floats, f_up);
pfSetListenerPosition(eye.x(), eye.y(), eye.z());
// view matrix
D3DXVECTOR3 ep(eye.x(), eye.y(), eye.z());
D3DXVECTOR3 lv(look.x(), look.y(), look.z());
D3DXVECTOR3 uv(up.x(), up.y(), up.z());
D3DXMatrixLookAtRH(&mView, &ep, &lv, &uv);
D3DXMatrixInverse(&mInverseView, nullptr, &mView);
CalculateProjectionMatrix();
CalculateInverseViewProjection();
mFrustum.Build(mViewProjection);
ManagePath();
}
void Camera::SetSkybox(const shared_ptr<ITexture> & up, const shared_ptr<ITexture> & left, const shared_ptr<ITexture> & right, const shared_ptr<ITexture> & forward, const shared_ptr<ITexture> & back) {
if(up && left && right && forward && back) {
mSkybox = shared_ptr<Skybox>(new Skybox(std::dynamic_pointer_cast<Texture>(up), std::dynamic_pointer_cast<Texture>(left), std::dynamic_pointer_cast<Texture>(right), std::dynamic_pointer_cast<Texture>(forward), std::dynamic_pointer_cast<Texture>(back)));
} else {
if(mSkybox) {
mSkybox.reset();
}
}
}
void Camera::SetFrameBrightness(float brightness) {
mFrameBrightness = brightness;
if(mFrameBrightness > 100.0f) {
mFrameBrightness = 100.0f;
}
if(mFrameBrightness < 0.0f) {
mFrameBrightness = 0.0f;
}
}
float Camera::GetFrameBrightness() const {
return mFrameBrightness;
}
void Camera::SetFrameColor(const Vector3 & color) {
mFrameColor = color;
if(mFrameColor.x > 255.0f) {
mFrameColor.x = 255.0f;
}
if(mFrameColor.y > 255.0f) {
mFrameColor.y = 255.0f;
}
if(mFrameColor.z > 255.0f) {
mFrameColor.z = 255.0f;
}
if(mFrameColor.x < 0.0f) {
mFrameColor.x = 0.0f;
}
if(mFrameColor.y < 0.0f) {
mFrameColor.y = 0.0f;
}
if(mFrameColor.z < 0.0f) {
mFrameColor.z = 0.0f;
}
}
Vector3 Camera::GetFrameColor() const {
return mFrameColor;
}
void Camera::CalculateInverseViewProjection() {
D3DXMatrixMultiply(&mViewProjection, &mView, &mProjection);
D3DXMatrixInverse(&invViewProjection, 0, &mViewProjection);
}
void Camera::CalculateProjectionMatrix() {
D3DVIEWPORT9 vp;
pD3D->GetViewport(&vp);
D3DXMatrixPerspectiveFovRH(&mProjection, mFov * 3.14159 / 180.0f, (float)vp.Width / (float)vp.Height, mNearZ, mFarZ);
}
Camera::~Camera() {
}
Camera::Camera(SceneFactory * factory, float fov) :
SceneNode(factory),
mFrameBrightness(100.0f),
mNearZ(0.025f),
mFarZ(6000.0f),
mFov(fov),
mInDepthHack(false),
mPathNewPointDelta(5.0f),
mFrameColor(255.0f, 255.0f, 255.0f) {
// path must contain at least one point, add new one located in camera's position
mNearestPathPointIndex = 0;
mPath.push_back(std::move(unique_ptr<PathPoint>(new PathPoint(GetPosition()))));
CalculateProjectionMatrix();
D3DXMatrixLookAtRH(&mView, &D3DXVECTOR3(0, 100, 100), &D3DXVECTOR3(0, 0, 0), &D3DXVECTOR3(0, 1, 0));
}
void Camera::EnterDepthHack(float depth) {
depth = fabs(depth);
if(depth > 0.001) {
if(!mInDepthHack) {
mDepthHackMatrix = mProjection;
}
mInDepthHack = true;
mProjection._43 -= depth;
CalculateInverseViewProjection();
}
}
void Camera::LeaveDepthHack() {
if(mInDepthHack) {
mInDepthHack = false;
mProjection = mDepthHackMatrix;
CalculateInverseViewProjection();
}
}
// this function builds path of camera by creating points in regular distance between them
void Camera::ManagePath() {
if(mPath.size() > 64) {
// get half of average distance between lights
auto & pointLights = mFactory->GetPointLightList();
if(pointLights.size()) {
float mRangeSum = 0;
for(auto & lWeak : pointLights) {
shared_ptr<PointLight> & light = lWeak.lock();
if(light) {
mRangeSum += light->GetRange();
}
}
mPathNewPointDelta = (mRangeSum / pointLights.size()) / 2;
mLastPosition = Vector3(FLT_MAX, FLT_MAX, FLT_MAX);
}
mPath.clear();
mNearestPathPointIndex = 0;
}
// check how far we from last added point
Vector3 position = GetPosition();
if((position - mLastPosition).Length2() > mPathNewPointDelta) {
mLastPosition = position;
bool addNewPoint = true;
float distToNearest = -FLT_MAX;
// check how far we from other path points, if far enough addNewPoint sets to true
int index = 0;
for(auto & pPoint : mPath) {
float dist = (position - pPoint->GetPosition()).Length2();
if(dist < distToNearest) {
mNearestPathPointIndex = index;
distToNearest = dist;
}
if(dist < mPathNewPointDelta) {
addNewPoint = false;
}
++index;
}
if(addNewPoint) {
mNearestPathPointIndex = mPath.size();
mPath.push_back(std::move(unique_ptr<PathPoint>(new PathPoint(position))));
}
}
}
void Camera::OnResetDevice() {
ManagePath();
}
void Camera::OnLostDevice() {
mPath.clear();
auto & pointLights = mFactory->GetPointLightList();
if(pointLights.size()) {
float mRangeSum = 0;
for(auto & pLight : pointLights) {
shared_ptr<PointLight> & light = pLight.lock();
if(light) {
mRangeSum += light->GetRange();
}
}
mPathNewPointDelta = (mRangeSum / pointLights.size()) / 2;
} else {
mPathNewPointDelta = 0.0f;
}
mLastPosition = Vector3(FLT_MAX, FLT_MAX, FLT_MAX);
// path must contain at least one point, add new one located in camera's position
mNearestPathPointIndex = 0;
mPath.push_back(std::move(unique_ptr<PathPoint>(new PathPoint(GetPosition()))));
}
void Camera::SetActive() {
Camera::msCurrentCamera = std::dynamic_pointer_cast<Camera>(shared_from_this());
}
void Camera::SetFOV(float fov) {
mFov = fov;
}
unique_ptr<PathPoint> & Camera::GetNearestPathPoint() {
return mPath[mNearestPathPointIndex];
}