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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/**
@file GCamera.cpp
@author Morgan McGuire, matrix@graphics3d.com
@author Jeff Marsceill, 08jcm@williams.edu
@created 2005-07-20
@edited 2007-07-24
*/
#include "G3D/GCamera.h"
#include "G3D/platform.h"
#include "G3D/Rect2D.h"
#include "G3D/BinaryInput.h"
#include "G3D/BinaryOutput.h"
#include "G3D/Ray.h"
#include "G3D/Matrix4.h"
namespace G3D {
GCamera::GCamera() {
setNearPlaneZ(-0.1f);
setFarPlaneZ(-(float)inf());
setFieldOfView((float)toRadians(55.0f), VERTICAL);
}
GCamera::~GCamera() {
}
void GCamera::getCoordinateFrame(CoordinateFrame& c) const {
c = m_cframe;
}
void GCamera::setCoordinateFrame(const CoordinateFrame& c) {
m_cframe = c;
}
void GCamera::setFieldOfView(float angle, FOVDirection dir) {
debugAssert((angle < pi()) && (angle > 0));
m_fieldOfView = angle;
m_direction = dir;
}
float GCamera::imagePlaneDepth() const{
return -m_nearPlaneZ;
}
float GCamera::viewportWidth(const Rect2D& viewport) const {
// Compute the side of a square at the near plane based on our field of view
float s = 2.0f * -m_nearPlaneZ * tan(m_fieldOfView * 0.5f);
if (m_direction == VERTICAL) {
s *= viewport.width() / viewport.height();
}
return s;
}
float GCamera::viewportHeight(const Rect2D& viewport) const {
// Compute the side of a square at the near plane based on our field of view
float s = 2.0f * -m_nearPlaneZ * tan(m_fieldOfView * 0.5f);
if (m_direction == HORIZONTAL) {
s *= viewport.height() / viewport.width();
}
return s;
}
Ray GCamera::worldRay(float x, float y, const Rect2D& viewport) const {
int screenWidth = iFloor(viewport.width());
int screenHeight = iFloor(viewport.height());
Ray out;
out.origin = m_cframe.translation;
float cx = screenWidth / 2.0f;
float cy = screenHeight / 2.0f;
out.direction = Vector3( (x - cx) * viewportWidth(viewport) / screenWidth,
-(y - cy) * viewportHeight(viewport) / screenHeight,
(m_nearPlaneZ) );
out.direction = m_cframe.vectorToWorldSpace(out.direction);
// Normalize the direction (we didn't do it before)
out.direction = out.direction.direction();
return out;
}
/**
This is the matrix that a RenderDevice (or OpenGL) uses as the projection matrix.
@sa RenderDevice::setProjectionAndCameraMatrix, RenderDevice::setProjectionMatrix, Matrix4::perspectiveProjection
*/
void GCamera::getProjectUnitMatrix(const Rect2D& viewport, Matrix4& P) const{
float screenWidth = viewport.width();
float screenHeight = viewport.height();
float r, l, t, b, n, f, x, y;
if(m_direction == VERTICAL){
y = -m_nearPlaneZ * tan(m_fieldOfView / 2);
x = y * (screenWidth / screenHeight);
}
else{ //m_direction == HORIZONTAL
x = -m_nearPlaneZ * tan(m_fieldOfView / 2);
y = x * (screenHeight / screenWidth);
}
n = -m_nearPlaneZ;
f = -m_farPlaneZ;
r = x;
l = -x;
t = y;
b = -y;
P = Matrix4::perspectiveProjection(l, r, b, t, n, f);
}
Vector3 GCamera::projectUnit(const Vector3& point, const Rect2D& viewport) const {
Matrix4 M;
getProjectUnitMatrix(viewport, M);
Vector4 cameraSpacePoint(coordinateFrame().pointToObjectSpace(point), 1.0f);
const Vector4& screenSpacePoint = M * cameraSpacePoint;
return Vector3(screenSpacePoint.xyz() / screenSpacePoint.w);
}
Vector3 GCamera::project(const Vector3& point,
const Rect2D& viewport) const {
// Find the point in the homogeneous cube
const Vector3& cube = projectUnit(point, viewport);
return convertFromUnitToNormal(cube, viewport);
}
Vector3 GCamera::unprojectUnit(const Vector3& v, const Rect2D& viewport) const {
const Vector3& projectedPoint = convertFromUnitToNormal(v, viewport);
return unproject(projectedPoint, viewport);
}
Vector3 GCamera::unproject(const Vector3& v, const Rect2D& viewport) const {
const float n = m_nearPlaneZ;
const float f = m_farPlaneZ;
float z;
if (-f >= inf()) {
// Infinite far plane
z = 1.0f / (((-1.0f / n) * v.z) + 1.0f / n);
} else {
z = 1.0f / ((((1.0f / f) - (1.0f / n)) * v.z) + 1.0f / n);
}
const Ray& ray = worldRay(v.x, v.y, viewport);
// Find out where the ray reaches the specified depth.
const Vector3& out = ray.origin + ray.direction * -z / (ray.direction.dot(m_cframe.lookVector()));
return out;
}
float GCamera::worldToScreenSpaceArea(float area, float z, const Rect2D& viewport) const {
if (z >= 0) {
return (float)inf();
}
return area * (float)square(imagePlaneDepth() / z);
}
void GCamera::getClipPlanes(
const Rect2D& viewport,
Array<Plane>& clip) const {
Frustum fr;
frustum(viewport, fr);
clip.resize(fr.faceArray.size(), DONT_SHRINK_UNDERLYING_ARRAY);
for (int f = 0; f < clip.size(); ++f) {
clip[f] = fr.faceArray[f].plane;
}
}
GCamera::Frustum GCamera::frustum(const Rect2D& viewport) const {
Frustum f;
frustum(viewport, f);
return f;
}
void GCamera::frustum(const Rect2D& viewport, Frustum& fr) const {
// The volume is the convex hull of the vertices definining the view
// frustum and the light source point at infinity.
const float x = viewportWidth(viewport) / 2;
const float y = viewportHeight(viewport) / 2;
const float z = m_nearPlaneZ;
const float w = z / -m_farPlaneZ;
float fovx;
fovx = m_fieldOfView;
if (m_direction == VERTICAL) {
fovx *= x / y;
}
// Near face (ccw from UR)
fr.vertexPos.append(
Vector4( x, y, z, 1),
Vector4(-x, y, z, 1),
Vector4(-x, -y, z, 1),
Vector4( x, -y, z, 1));
// Far face (ccw from UR, from origin)
fr.vertexPos.append(
Vector4( x, y, z, w),
Vector4(-x, y, z, w),
Vector4(-x, -y, z, w),
Vector4( x, -y, z, w));
Frustum::Face face;
// Near plane (wind backwards so normal faces into frustum)
// Recall that nearPlane, farPlane are positive numbers, so
// we need to negate them to produce actual z values.
face.plane = Plane(Vector3(0,0,-1), Vector3(0,0,m_nearPlaneZ));
face.vertexIndex[0] = 3;
face.vertexIndex[1] = 2;
face.vertexIndex[2] = 1;
face.vertexIndex[3] = 0;
fr.faceArray.append(face);
// Right plane
face.plane = Plane(Vector3(-cosf(fovx/2), 0, -sinf(fovx/2)), Vector3::zero());
face.vertexIndex[0] = 0;
face.vertexIndex[1] = 4;
face.vertexIndex[2] = 7;
face.vertexIndex[3] = 3;
fr.faceArray.append(face);
// Left plane
face.plane = Plane(Vector3(-fr.faceArray.last().plane.normal().x, 0, fr.faceArray.last().plane.normal().z), Vector3::zero());
face.vertexIndex[0] = 5;
face.vertexIndex[1] = 1;
face.vertexIndex[2] = 2;
face.vertexIndex[3] = 6;
fr.faceArray.append(face);
// Top plane
face.plane = Plane(Vector3(0, -cosf(m_fieldOfView/2.0f), -sinf(m_fieldOfView/2.0f)), Vector3::zero());
face.vertexIndex[0] = 1;
face.vertexIndex[1] = 5;
face.vertexIndex[2] = 4;
face.vertexIndex[3] = 0;
fr.faceArray.append(face);
// Bottom plane
face.plane = Plane(Vector3(0, -fr.faceArray.last().plane.normal().y, fr.faceArray.last().plane.normal().z), Vector3::zero());
face.vertexIndex[0] = 2;
face.vertexIndex[1] = 3;
face.vertexIndex[2] = 7;
face.vertexIndex[3] = 6;
fr.faceArray.append(face);
// Far plane
if (-m_farPlaneZ < inf()) {
face.plane = Plane(Vector3(0, 0, 1), Vector3(0, 0, m_farPlaneZ));
face.vertexIndex[0] = 4;
face.vertexIndex[1] = 5;
face.vertexIndex[2] = 6;
face.vertexIndex[3] = 7;
fr.faceArray.append(face);
}
// Transform vertices to world space
for (int v = 0; v < fr.vertexPos.size(); ++v) {
fr.vertexPos[v] = m_cframe.toWorldSpace(fr.vertexPos[v]);
}
// Transform planes to world space
for (int p = 0; p < fr.faceArray.size(); ++p) {
// Since there is no scale factor, we don't have to
// worry about the inverse transpose of the normal.
Vector3 normal;
float d;
fr.faceArray[p].plane.getEquation(normal, d);
Vector3 newNormal = m_cframe.rotation * normal;
if (isFinite(d)) {
d = (newNormal * -d + m_cframe.translation).dot(newNormal);
fr.faceArray[p].plane = Plane(newNormal, newNormal * d);
} else {
// When d is infinite, we can't multiply 0's by it without
// generating NaNs.
fr.faceArray[p].plane = Plane::fromEquation(newNormal.x, newNormal.y, newNormal.z, d);
}
}
}
void GCamera::getNearViewportCorners(
const Rect2D& viewport,
Vector3& outUR,
Vector3& outUL,
Vector3& outLL,
Vector3& outLR) const {
// Must be kept in sync with getFrustum()
const float w = viewportWidth(viewport) / 2.0f;
const float h = viewportHeight(viewport) / 2.0f;
const float z = nearPlaneZ();
// Compute the points
outUR = Vector3( w, h, z);
outUL = Vector3(-w, h, z);
outLL = Vector3(-w, -h, z);
outLR = Vector3( w, -h, z);
// Take to world space
outUR = m_cframe.pointToWorldSpace(outUR);
outUL = m_cframe.pointToWorldSpace(outUL);
outLR = m_cframe.pointToWorldSpace(outLR);
outLL = m_cframe.pointToWorldSpace(outLL);
}
void GCamera::getFarViewportCorners(
const Rect2D& viewport,
Vector3& outUR,
Vector3& outUL,
Vector3& outLL,
Vector3& outLR) const {
// Must be kept in sync with getFrustum()
const float w = viewportWidth(viewport) * m_farPlaneZ / m_nearPlaneZ;
const float h = viewportHeight(viewport) * m_farPlaneZ / m_nearPlaneZ;
const float z = m_farPlaneZ;
// Compute the points
outUR = Vector3( w, h, z);
outUL = Vector3(-w, h, z);
outLL = Vector3(-w, -h, z);
outLR = Vector3( w, -h, z);
// Take to world space
outUR = m_cframe.pointToWorldSpace(outUR);
outUL = m_cframe.pointToWorldSpace(outUL);
outLR = m_cframe.pointToWorldSpace(outLR);
outLL = m_cframe.pointToWorldSpace(outLL);
}
void GCamera::setPosition(const Vector3& t) {
m_cframe.translation = t;
}
void GCamera::lookAt(const Vector3& position, const Vector3& up) {
m_cframe.lookAt(position, up);
}
void GCamera::serialize(BinaryOutput& bo) const {
bo.writeFloat32(m_fieldOfView);
bo.writeFloat32(imagePlaneDepth());
debugAssert(nearPlaneZ() < 0.0f);
bo.writeFloat32(nearPlaneZ());
debugAssert(farPlaneZ() < 0.0f);
bo.writeFloat32(farPlaneZ());
m_cframe.serialize(bo);
bo.writeInt8(m_direction);
}
void GCamera::deserialize(BinaryInput& bi) {
m_fieldOfView = bi.readFloat32();
m_nearPlaneZ = bi.readFloat32();
debugAssert(m_nearPlaneZ < 0.0f);
m_farPlaneZ = bi.readFloat32();
debugAssert(m_farPlaneZ < 0.0f);
m_cframe.deserialize(bi);
m_direction = (FOVDirection)bi.readInt8();
}
Vector3 GCamera::convertFromUnitToNormal(const Vector3& in, const Rect2D& viewport) const{
return (in + Vector3(1,1,1)) * 0.5 * Vector3(viewport.width(), -viewport.height(), 1) +
Vector3(viewport.x0(), viewport.y1(), 0);
}
} // namespace
|