Skylicht Engine
Loading...
Searching...
No Matches
SViewFrustum.h
1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine".
3// For conditions of distribution and use, see copyright notice in irrlicht.h
4
5#ifndef __S_VIEW_FRUSTUM_H_INCLUDED__
6#define __S_VIEW_FRUSTUM_H_INCLUDED__
7
8#include "plane3d.h"
9#include "vector3d.h"
10#include "line3d.h"
11#include "aabbox3d.h"
12#include "matrix4.h"
13#include "IVideoDriver.h"
14
15namespace irr
16{
17namespace scene
18{
19
21
26 {
45
46
48 SViewFrustum() : BoundingRadius(0.f), FarNearDistance(0.f) {}
49
51 SViewFrustum(const SViewFrustum& other);
52
54 SViewFrustum(const core::matrix4& mat);
55
57 inline void setFrom(const core::matrix4& mat);
58
60
61 void transform(const core::matrix4& mat);
62
65
68
71
74
77
80
83
86
89
91 inline void recalculateBoundingBox();
92
94 float getBoundingRadius() const;
95
98
100 void setFarNearDistance(float distance);
101
104
107
108 void setTransform(video::E_TRANSFORMATION_STATE state, const core::matrix4& mat);
109
111
112 bool clipLine(core::line3d<f32>& line) const;
113
116
119
122
123 private:
125 enum E_TRANSFORMATION_STATE_FRUSTUM
126 {
127 ETS_VIEW = 0,
128 ETS_PROJECTION = 1,
129 ETS_COUNT_FRUSTUM
130 };
131
133 inline void recalculateBoundingSphere();
134
136 core::matrix4 Matrices[ETS_COUNT_FRUSTUM];
137
138 float BoundingRadius;
139 float FarNearDistance;
140 core::vector3df BoundingCenter;
141 };
142
143
148 {
151
152 u32 i;
153 for (i=0; i<VF_PLANE_COUNT; ++i)
154 planes[i]=other.planes[i];
155
156 for (i=0; i<ETS_COUNT_FRUSTUM; ++i)
157 Matrices[i]=other.Matrices[i];
158
159 BoundingRadius = other.BoundingRadius;
160 FarNearDistance = other.FarNearDistance;
161 BoundingCenter = other.BoundingCenter;
162 }
163
165 {
166 setFrom(mat);
167 }
168
169
171 {
172 for (u32 i=0; i<VF_PLANE_COUNT; ++i)
173 mat.transformPlane(planes[i]);
174
177 }
178
179
189
199
209
219
229
239
249
259
261 {
262 return boundingBox;
263 }
264
266 {
267 boundingBox.reset ( cameraPosition );
268
269 boundingBox.addInternalPoint(getFarLeftUp());
270 boundingBox.addInternalPoint(getFarRightUp());
271 boundingBox.addInternalPoint(getFarLeftDown());
272 boundingBox.addInternalPoint(getFarRightDown());
273
274 // Also recalculate the bounding sphere when the bbox changes
275 recalculateBoundingSphere();
276 }
277
279 {
280 return BoundingRadius;
281 }
282
284 {
285 return BoundingCenter;
286 }
287
288 inline void SViewFrustum::setFarNearDistance(float distance)
289 {
290 FarNearDistance = distance;
291 }
292
295 inline void SViewFrustum::setFrom(const core::matrix4& mat)
296 {
297 // left clipping plane
298 planes[VF_LEFT_PLANE].Normal.X = mat[3 ] + mat[0];
299 planes[VF_LEFT_PLANE].Normal.Y = mat[7 ] + mat[4];
300 planes[VF_LEFT_PLANE].Normal.Z = mat[11] + mat[8];
301 planes[VF_LEFT_PLANE].D = mat[15] + mat[12];
302
303 // right clipping plane
304 planes[VF_RIGHT_PLANE].Normal.X = mat[3 ] - mat[0];
305 planes[VF_RIGHT_PLANE].Normal.Y = mat[7 ] - mat[4];
306 planes[VF_RIGHT_PLANE].Normal.Z = mat[11] - mat[8];
307 planes[VF_RIGHT_PLANE].D = mat[15] - mat[12];
308
309 // top clipping plane
310 planes[VF_TOP_PLANE].Normal.X = mat[3 ] - mat[1];
311 planes[VF_TOP_PLANE].Normal.Y = mat[7 ] - mat[5];
312 planes[VF_TOP_PLANE].Normal.Z = mat[11] - mat[9];
313 planes[VF_TOP_PLANE].D = mat[15] - mat[13];
314
315 // bottom clipping plane
316 planes[VF_BOTTOM_PLANE].Normal.X = mat[3 ] + mat[1];
317 planes[VF_BOTTOM_PLANE].Normal.Y = mat[7 ] + mat[5];
318 planes[VF_BOTTOM_PLANE].Normal.Z = mat[11] + mat[9];
319 planes[VF_BOTTOM_PLANE].D = mat[15] + mat[13];
320
321 // far clipping plane
322 planes[VF_FAR_PLANE].Normal.X = mat[3 ] - mat[2];
323 planes[VF_FAR_PLANE].Normal.Y = mat[7 ] - mat[6];
324 planes[VF_FAR_PLANE].Normal.Z = mat[11] - mat[10];
325 planes[VF_FAR_PLANE].D = mat[15] - mat[14];
326
327 // near clipping plane
328 planes[VF_NEAR_PLANE].Normal.X = mat[2];
329 planes[VF_NEAR_PLANE].Normal.Y = mat[6];
330 planes[VF_NEAR_PLANE].Normal.Z = mat[10];
331 planes[VF_NEAR_PLANE].D = mat[14];
332
333 // normalize normals
334 u32 i;
335 for ( i=0; i != VF_PLANE_COUNT; ++i)
336 {
337 const f32 len = -core::reciprocal_squareroot(
338 planes[i].Normal.getLengthSQ());
339 planes[i].Normal *= len;
340 planes[i].D *= len;
341 }
342
343 // make bounding box
345 }
346
351 {
352 u32 index = 0;
353 switch ( state )
354 {
356 index = SViewFrustum::ETS_PROJECTION; break;
357 case video::ETS_VIEW:
358 index = SViewFrustum::ETS_VIEW; break;
359 default:
360 break;
361 }
362 return Matrices [ index ];
363 }
364
369 {
370 u32 index = 0;
371 switch ( state )
372 {
374 index = SViewFrustum::ETS_PROJECTION; break;
375 case video::ETS_VIEW:
376 index = SViewFrustum::ETS_VIEW; break;
377 default:
378 break;
379 }
380 return Matrices [ index ];
381 }
382
383 inline void SViewFrustum::setTransform(video::E_TRANSFORMATION_STATE state, const core::matrix4& mat)
384 {
385 u32 index = 0;
386 switch (state)
387 {
389 index = SViewFrustum::ETS_PROJECTION; break;
390 case video::ETS_VIEW:
391 index = SViewFrustum::ETS_VIEW; break;
392 default:
393 break;
394 }
395
396 Matrices[index] = mat;
397 }
398
401 {
402 bool wasClipped = false;
403 for (u32 i=0; i < VF_PLANE_COUNT; ++i)
404 {
405 if (planes[i].classifyPointRelation(line.start) == core::ISREL3D_FRONT)
406 {
407 line.start = line.start.getInterpolated(line.end,
408 planes[i].getKnownIntersectionWithLine(line.start, line.end));
409 wasClipped = true;
410 }
411 if (planes[i].classifyPointRelation(line.end) == core::ISREL3D_FRONT)
412 {
413 line.end = line.start.getInterpolated(line.end,
414 planes[i].getKnownIntersectionWithLine(line.start, line.end));
415 wasClipped = true;
416 }
417 }
418 return wasClipped;
419 }
420
421 inline void SViewFrustum::recalculateBoundingSphere()
422 {
423 // Find the center
424 const float shortlen = (getNearLeftUp() - getNearRightUp()).getLength();
425 const float longlen = (getFarLeftUp() - getFarRightUp()).getLength();
426
427 const float farlen = FarNearDistance;
428 const float fartocenter = (farlen + (shortlen - longlen) * (shortlen + longlen)/(4*farlen)) / 2;
429 const float neartocenter = farlen - fartocenter;
430
431 BoundingCenter = cameraPosition + -planes[VF_NEAR_PLANE].Normal * neartocenter;
432
433 // Find the radius
434 core::vector3df dir[8];
435 dir[0] = getFarLeftUp() - BoundingCenter;
436 dir[1] = getFarRightUp() - BoundingCenter;
437 dir[2] = getFarLeftDown() - BoundingCenter;
438 dir[3] = getFarRightDown() - BoundingCenter;
439 dir[4] = getNearRightDown() - BoundingCenter;
440 dir[5] = getNearLeftDown() - BoundingCenter;
441 dir[6] = getNearRightUp() - BoundingCenter;
442 dir[7] = getNearLeftUp() - BoundingCenter;
443
444 u32 i = 0;
445 float diam[8] = { 0.f };
446
447 for (i = 0; i < 8; ++i)
448 diam[i] = dir[i].getLengthSQ();
449
450 float longest = 0;
451
452 for (i = 0; i < 8; ++i)
453 {
454 if (diam[i] > longest)
455 longest = diam[i];
456 }
457
458 BoundingRadius = sqrtf(longest);
459 }
460
461} // end namespace scene
462} // end namespace irr
463
464#endif
465
void transformVect(vector3df &vect) const
Transforms the vector by this matrix.
Definition matrix4.h:1140
void transformPlane(core::plane3d< f32 > &plane) const
Transforms a plane by this matrix.
Definition matrix4.h:1182
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.h:22
3D line between two points with intersection methods.
Definition line3d.h:19
vector3d< T > start
Start point of line.
Definition line3d.h:130
vector3d< T > end
End point of line.
Definition line3d.h:132
Template plane class with some intersection testing methods.
Definition plane3d.h:34
vector3d< T > Normal
Normal vector of the plane.
Definition plane3d.h:228
vector3d< f32 > vector3df
Typedef for a f32 3d vector.
Definition vector3d.h:445
CMatrix4< f32 > matrix4
Typedef for f32 matrix.
Definition matrix4.h:2241
All scene management can be found in this namespace: Mesh loading, special scene nodes like octrees a...
Definition CIndexBuffer.h:13
E_TRANSFORMATION_STATE
enumeration for geometry transformation states
Definition IVideoDriver.h:57
@ ETS_VIEW
View transformation.
Definition IVideoDriver.h:59
@ ETS_PROJECTION
Projection transformation.
Definition IVideoDriver.h:63
Everything in the Irrlicht Engine can be found in this namespace.
Definition Skylicht.h:33
float f32
32 bit floating point variable.
Definition irrTypes.h:104
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.h:58
Defines the view frustum. That's the space visible by the camera.
Definition SViewFrustum.h:26
const core::aabbox3d< f32 > & getBoundingBox() const
returns a bounding box enclosing the whole view frustum
Definition SViewFrustum.h:260
void setFrom(const core::matrix4 &mat)
This constructor creates a view frustum based on a projection and/or view matrix.
Definition SViewFrustum.h:295
float getBoundingRadius() const
get the bounding sphere's radius (of an optimized sphere, not the AABB's)
Definition SViewFrustum.h:278
core::vector3df getFarRightUp() const
returns the point which is on the far right top corner inside the the view frustum.
Definition SViewFrustum.h:200
void recalculateBoundingBox()
recalculates the bounding box and sphere based on the planes
Definition SViewFrustum.h:265
core::vector3df getNearRightUp() const
returns the point which is on the near right top corner inside the the view frustum.
Definition SViewFrustum.h:240
core::vector3df getNearRightDown() const
returns the point which is on the near right bottom corner inside the the view frustum.
Definition SViewFrustum.h:250
SViewFrustum()
Default Constructor.
Definition SViewFrustum.h:48
core::vector3df cameraPosition
the position of the camera
Definition SViewFrustum.h:115
core::vector3df getFarLeftDown() const
returns the point which is on the far left bottom corner inside the the view frustum.
Definition SViewFrustum.h:190
bool clipLine(core::line3d< f32 > &line) const
clips a line to the view frustum.
Definition SViewFrustum.h:400
core::vector3df getBoundingCenter() const
get the bounding sphere's radius (of an optimized sphere, not the AABB's)
Definition SViewFrustum.h:283
core::vector3df getFarLeftUp() const
returns the point which is on the far left upper corner inside the the view frustum.
Definition SViewFrustum.h:180
core::vector3df getFarRightDown() const
returns the point which is on the far right bottom corner inside the the view frustum.
Definition SViewFrustum.h:210
void setFarNearDistance(float distance)
the cam should tell the frustum the distance between far and near
Definition SViewFrustum.h:288
core::matrix4 & getTransform(video::E_TRANSFORMATION_STATE state)
get the given state's matrix based on frustum E_TRANSFORMATION_STATE
Definition SViewFrustum.h:350
core::aabbox3d< f32 > boundingBox
bounding box around the view frustum
Definition SViewFrustum.h:121
core::vector3df getNearLeftUp() const
returns the point which is on the near left upper corner inside the the view frustum.
Definition SViewFrustum.h:220
VFPLANES
Definition SViewFrustum.h:28
@ VF_FAR_PLANE
Far plane of the frustum. That is the plane farest away from the eye.
Definition SViewFrustum.h:30
@ VF_LEFT_PLANE
Left plane of the frustum.
Definition SViewFrustum.h:34
@ VF_PLANE_COUNT
Amount of planes enclosing the view frustum. Should be 6.
Definition SViewFrustum.h:43
@ VF_BOTTOM_PLANE
Bottom plane of the frustum.
Definition SViewFrustum.h:38
@ VF_RIGHT_PLANE
Right plane of the frustum.
Definition SViewFrustum.h:36
@ VF_NEAR_PLANE
Near plane of the frustum. That is the plane nearest to the eye.
Definition SViewFrustum.h:32
@ VF_TOP_PLANE
Top plane of the frustum.
Definition SViewFrustum.h:40
core::vector3df getNearLeftDown() const
returns the point which is on the near left bottom corner inside the the view frustum.
Definition SViewFrustum.h:230
void transform(const core::matrix4 &mat)
transforms the frustum by the matrix
Definition SViewFrustum.h:170
core::plane3d< f32 > planes[VF_PLANE_COUNT]
all planes enclosing the view frustum.
Definition SViewFrustum.h:118