Nirtcpp 2.1.0
Nirtcpp is a high-performance c++ graphics engine.
Loading...
Searching...
No Matches
SViewFrustum.hpp
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 nirtcpp/nirtcpp.hpp
4
5#ifndef S_VIEW_FRUSTUM_HPP_INCLUDED
6#define S_VIEW_FRUSTUM_HPP_INCLUDED
7
8#include <nirtcpp/core/engine/plane3d.hpp>
9#include <nirtcpp/core/engine/vector3d.hpp>
10#include <nirtcpp/core/engine/line3d.hpp>
11#include <nirtcpp/core/engine/aabbox3d.hpp>
12#include <nirtcpp/core/engine/matrix4.hpp>
13#include <nirtcpp/core/engine/IVideoDriver.hpp>
14
15namespace nirt
16{
17namespace scene
18{
19
21
26 {
27 public:
46
47 public:
49 SViewFrustum() : BoundingRadius(0.f), FarNearDistance(0.f) {}
50
52 //\param zClipFromZero: Clipping of z can be projected from 0 to w when true (D3D style) and from -w to w when false (OGL style).
53 SViewFrustum(const core::matrix4& mat, bool zClipFromZero);
54
55 public:
57 //\param zClipFromZero: Clipping of z can be projected from 0 to w when true (D3D style) and from -w to w when false (OGL style).
58 inline void setFrom(const core::matrix4& mat, bool zClipFromZero);
59
61
62 void transform(const core::matrix4& mat);
63
66
69
72
75
78
81
84
87
90
92 inline void recalculateBoundingBox();
93
95 float getBoundingRadius() const;
96
99
101 void setFarNearDistance(float distance);
102
105
108
110
111 bool clipLine(core::line3d<f32>& line) const;
112
113 public:
114
117
120
123
124 private:
126 enum E_TRANSFORMATION_STATE_FRUSTUM
127 {
128 ETS_VIEW = 0,
129 ETS_PROJECTION = 1,
130 ETS_COUNT_FRUSTUM
131 };
132
134 inline void recalculateBoundingSphere();
135
137 core::matrix4 Matrices[ETS_COUNT_FRUSTUM];
138
139 float BoundingRadius;
140 float FarNearDistance;
141 core::vector3df BoundingCenter;
142 };
143
144 inline SViewFrustum::SViewFrustum(const core::matrix4& mat, bool zClipFromZero)
145 {
146 setFrom(mat, zClipFromZero);
147 }
148
149
151 {
152 for (u32 i=0; i<VF_PLANE_COUNT; ++i)
153 mat.transformPlane(planes[i]);
154
157 }
158
159
169
179
189
199
209
219
229
239
241 {
242 return boundingBox;
243 }
244
259
261 {
262 return BoundingRadius;
263 }
264
266 {
267 return BoundingCenter;
268 }
269
270 inline void SViewFrustum::setFarNearDistance(float distance)
271 {
272 FarNearDistance = distance;
273 }
274
277 inline void SViewFrustum::setFrom(const core::matrix4& mat, bool zClipFromZero)
278 {
279 // left clipping plane
280 planes[VF_LEFT_PLANE].Normal.X = mat[3 ] + mat[0];
281 planes[VF_LEFT_PLANE].Normal.Y = mat[7 ] + mat[4];
282 planes[VF_LEFT_PLANE].Normal.Z = mat[11] + mat[8];
283 planes[VF_LEFT_PLANE].D = mat[15] + mat[12];
284
285 // right clipping plane
286 planes[VF_RIGHT_PLANE].Normal.X = mat[3 ] - mat[0];
287 planes[VF_RIGHT_PLANE].Normal.Y = mat[7 ] - mat[4];
288 planes[VF_RIGHT_PLANE].Normal.Z = mat[11] - mat[8];
289 planes[VF_RIGHT_PLANE].D = mat[15] - mat[12];
290
291 // top clipping plane
292 planes[VF_TOP_PLANE].Normal.X = mat[3 ] - mat[1];
293 planes[VF_TOP_PLANE].Normal.Y = mat[7 ] - mat[5];
294 planes[VF_TOP_PLANE].Normal.Z = mat[11] - mat[9];
295 planes[VF_TOP_PLANE].D = mat[15] - mat[13];
296
297 // bottom clipping plane
298 planes[VF_BOTTOM_PLANE].Normal.X = mat[3 ] + mat[1];
299 planes[VF_BOTTOM_PLANE].Normal.Y = mat[7 ] + mat[5];
300 planes[VF_BOTTOM_PLANE].Normal.Z = mat[11] + mat[9];
301 planes[VF_BOTTOM_PLANE].D = mat[15] + mat[13];
302
303 // far clipping plane
304 planes[VF_FAR_PLANE].Normal.X = mat[3 ] - mat[2];
305 planes[VF_FAR_PLANE].Normal.Y = mat[7 ] - mat[6];
306 planes[VF_FAR_PLANE].Normal.Z = mat[11] - mat[10];
307 planes[VF_FAR_PLANE].D = mat[15] - mat[14];
308
309 // near clipping plane
310 if ( zClipFromZero )
311 {
312 planes[VF_NEAR_PLANE].Normal.X = mat[2];
313 planes[VF_NEAR_PLANE].Normal.Y = mat[6];
314 planes[VF_NEAR_PLANE].Normal.Z = mat[10];
315 planes[VF_NEAR_PLANE].D = mat[14];
316 }
317 else
318 {
319 // near clipping plane
320 planes[VF_NEAR_PLANE].Normal.X = mat[3 ] + mat[2];
321 planes[VF_NEAR_PLANE].Normal.Y = mat[7 ] + mat[6];
322 planes[VF_NEAR_PLANE].Normal.Z = mat[11] + mat[10];
323 planes[VF_NEAR_PLANE].D = mat[15] + mat[14];
324 }
325
326 // normalize normals
327 u32 i;
328 for ( i=0; i != VF_PLANE_COUNT; ++i)
329 {
330 const f32 len = -core::reciprocal_squareroot(
331 planes[i].Normal.getLengthSQ());
332 planes[i].Normal *= len;
333 planes[i].D *= len;
334 }
335
336 // make bounding box
338 }
339
344 {
345 u32 index = 0;
346 switch ( state )
347 {
349 index = SViewFrustum::ETS_PROJECTION; break;
350 case video::ETS_VIEW:
351 index = SViewFrustum::ETS_VIEW; break;
352 default:
353 break;
354 }
355 return Matrices [ index ];
356 }
357
362 {
363 u32 index = 0;
364 switch ( state )
365 {
367 index = SViewFrustum::ETS_PROJECTION; break;
368 case video::ETS_VIEW:
369 index = SViewFrustum::ETS_VIEW; break;
370 default:
371 break;
372 }
373 return Matrices [ index ];
374 }
375
378 {
379 bool wasClipped = false;
380 for (u32 i=0; i < VF_PLANE_COUNT; ++i)
381 {
382 if (planes[i].classifyPointRelation(line.start) == core::ISREL3D_FRONT)
383 {
384 line.start = line.start.getInterpolated(line.end,
385 1.f-planes[i].getKnownIntersectionWithLine(line.start, line.end));
386 wasClipped = true;
387 }
388 if (planes[i].classifyPointRelation(line.end) == core::ISREL3D_FRONT)
389 {
390 line.end = line.start.getInterpolated(line.end,
391 1.f-planes[i].getKnownIntersectionWithLine(line.start, line.end));
392 wasClipped = true;
393 }
394 }
395 return wasClipped;
396 }
397
398 inline void SViewFrustum::recalculateBoundingSphere()
399 {
400 // Find the center
401 const float shortlen = (getNearLeftUp() - getNearRightUp()).getLength();
402 const float longlen = (getFarLeftUp() - getFarRightUp()).getLength();
403
404 const float farlen = FarNearDistance;
405 const float fartocenter = (farlen + (shortlen - longlen) * (shortlen + longlen)/(4*farlen)) / 2;
406 const float neartocenter = farlen - fartocenter;
407
408 BoundingCenter = cameraPosition + -planes[VF_NEAR_PLANE].Normal * neartocenter;
409
410 // Find the radius
411 core::vector3df dir[8];
412 dir[0] = getFarLeftUp() - BoundingCenter;
413 dir[1] = getFarRightUp() - BoundingCenter;
414 dir[2] = getFarLeftDown() - BoundingCenter;
415 dir[3] = getFarRightDown() - BoundingCenter;
416 dir[4] = getNearRightDown() - BoundingCenter;
417 dir[5] = getNearLeftDown() - BoundingCenter;
418 dir[6] = getNearRightUp() - BoundingCenter;
419 dir[7] = getNearLeftUp() - BoundingCenter;
420
421 u32 i = 0;
422 float diam[8] = { 0.f };
423
424 for (i = 0; i < 8; ++i)
425 diam[i] = dir[i].getLengthSQ();
426
427 float longest = 0;
428
429 for (i = 0; i < 8; ++i)
430 {
431 if (diam[i] > longest)
432 longest = diam[i];
433 }
434
435 BoundingRadius = sqrtf(longest);
436 }
437
438} // end namespace scene
439} // end namespace nirt
440
441#endif
4x4 matrix. Mostly used as transformation matrix for 3d calculations.
Definition matrix4.hpp:49
void transformPlane(core::plane3d< f32 > &plane) const
Transforms a plane by this matrix.
Definition matrix4.hpp:1275
void transformVect(vector3df &vect) const
Transforms the vector by this matrix.
Definition matrix4.hpp:1224
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.hpp:22
void addInternalPoint(const vector3d< T > &p)
Adds a point to the bounding box.
Definition aabbox3d.hpp:74
aabbox3d< T > getInterpolated(const aabbox3d< T > &other, f32 d) const
Calculates a new interpolated bounding box.
Definition aabbox3d.hpp:208
void reset(T x, T y, T z)
Resets the bounding box to a one-point box.
Definition aabbox3d.hpp:50
bool getIntersectionWithPlanes(const plane3d< T > &o1, const plane3d< T > &o2, vector3d< T > &outPoint) const
Get the intersection point with two other planes if there is one.
Definition plane3d.hpp:195
T D
Distance from origin.
Definition plane3d.hpp:231
vector3d< T > Normal
Normal vector of the plane.
Definition plane3d.hpp:228
f32 getKnownIntersectionWithLine(const vector3d< T > &linePoint1, const vector3d< T > &linePoint2) const
Get percentage of line between two points where an intersection with this plane happens.
Definition plane3d.hpp:107
T Y
Y coordinate of the vector.
Definition vector3d.hpp:453
T X
X coordinate of the vector.
Definition vector3d.hpp:450
T Z
Z coordinate of the vector.
Definition vector3d.hpp:456
Defines the view frustum. That's the space visible by the camera.
Definition SViewFrustum.hpp:26
float getBoundingRadius() const
get the bounding sphere's radius (of an optimized sphere, not the AABB's)
Definition SViewFrustum.hpp:260
core::vector3df getBoundingCenter() const
get the bounding sphere's radius (of an optimized sphere, not the AABB's)
Definition SViewFrustum.hpp:265
void setFrom(const core::matrix4 &mat, bool zClipFromZero)
This constructor creates a view frustum based on a projection and/or view matrix.
Definition SViewFrustum.hpp:277
core::vector3df getNearRightUp() const
returns the point which is on the near right top corner inside the the view frustum.
Definition SViewFrustum.hpp:220
core::matrix4 & getTransform(video::E_TRANSFORMATION_STATE state)
get the given state's matrix based on frustum E_TRANSFORMATION_STATE
Definition SViewFrustum.hpp:343
void transform(const core::matrix4 &mat)
transforms the frustum by the matrix
Definition SViewFrustum.hpp:150
core::vector3df getNearRightDown() const
returns the point which is on the near right bottom corner inside the the view frustum.
Definition SViewFrustum.hpp:230
core::vector3df getNearLeftDown() const
returns the point which is on the near left bottom corner inside the the view frustum.
Definition SViewFrustum.hpp:210
bool clipLine(core::line3d< f32 > &line) const
clips a line to the view frustum.
Definition SViewFrustum.hpp:377
core::vector3df getNearLeftUp() const
returns the point which is on the near left upper corner inside the the view frustum.
Definition SViewFrustum.hpp:200
core::vector3df cameraPosition
the position of the camera
Definition SViewFrustum.hpp:116
void recalculateBoundingBox()
recalculates the bounding box and sphere based on the planes
Definition SViewFrustum.hpp:245
core::plane3d< f32 > planes[VF_PLANE_COUNT]
all planes enclosing the view frustum.
Definition SViewFrustum.hpp:119
core::vector3df getFarRightDown() const
returns the point which is on the far right bottom corner inside the the view frustum.
Definition SViewFrustum.hpp:190
core::vector3df getFarLeftUp() const
returns the point which is on the far left upper corner inside the the view frustum.
Definition SViewFrustum.hpp:160
core::vector3df getFarLeftDown() const
returns the point which is on the far left bottom corner inside the the view frustum.
Definition SViewFrustum.hpp:170
const core::aabbox3d< f32 > & getBoundingBox() const
returns a bounding box enclosing the whole view frustum
Definition SViewFrustum.hpp:240
core::vector3df getFarRightUp() const
returns the point which is on the far right top corner inside the the view frustum.
Definition SViewFrustum.hpp:180
void setFarNearDistance(float distance)
the cam should tell the frustum the distance between far and near
Definition SViewFrustum.hpp:270
SViewFrustum()
Default Constructor.
Definition SViewFrustum.hpp:49
core::aabbox3d< f32 > boundingBox
bounding box around the view frustum
Definition SViewFrustum.hpp:122
VFPLANES
Definition SViewFrustum.hpp:29
@ VF_FAR_PLANE
Far plane of the frustum. That is the plane furthest away from the eye.
Definition SViewFrustum.hpp:31
@ VF_TOP_PLANE
Top plane of the frustum.
Definition SViewFrustum.hpp:41
@ VF_NEAR_PLANE
Near plane of the frustum. That is the plane nearest to the eye.
Definition SViewFrustum.hpp:33
@ VF_RIGHT_PLANE
Right plane of the frustum.
Definition SViewFrustum.hpp:37
@ VF_LEFT_PLANE
Left plane of the frustum.
Definition SViewFrustum.hpp:35
@ VF_BOTTOM_PLANE
Bottom plane of the frustum.
Definition SViewFrustum.hpp:39
@ VF_PLANE_COUNT
Amount of planes enclosing the view frustum. Should be 6.
Definition SViewFrustum.hpp:44
E_TRANSFORMATION_STATE
enumeration for geometry transformation states
Definition IVideoDriver.hpp:54
@ ETS_VIEW
View transformation.
Definition IVideoDriver.hpp:56
@ ETS_PROJECTION
Projection transformation.
Definition IVideoDriver.hpp:60
As of Nirtcpp 1.6, position2d is a synonym for vector2d.
Definition vector3d.hpp:11
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.hpp:64
float f32
32 bit floating point variable.
Definition irrTypes.hpp:110

Nirtcpp    @cppfx.xyz

Esvcpp    esv::print