Irrlicht 3D Engine
SViewFrustum.h
Go to the documentation of this file.
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 
15 namespace irr
16 {
17 namespace scene
18 {
19 
21 
25  struct SViewFrustum
26  {
27  enum VFPLANES
28  {
41 
44  };
45 
46 
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 
88  const core::aabbox3d<f32> &getBoundingBox() const;
89 
91  inline void recalculateBoundingBox();
92 
95 
98 
100 
101  bool clipLine(core::line3d<f32>& line) const;
102 
105 
108 
111 
112  private:
114  enum E_TRANSFORMATION_STATE_FRUSTUM
115  {
116  ETS_VIEW = 0,
117  ETS_PROJECTION = 1,
118  ETS_COUNT_FRUSTUM
119  };
120 
122  core::matrix4 Matrices[ETS_COUNT_FRUSTUM];
123  };
124 
125 
130  {
132  boundingBox=other.boundingBox;
133 
134  u32 i;
135  for (i=0; i<VF_PLANE_COUNT; ++i)
136  planes[i]=other.planes[i];
137 
138  for (i=0; i<ETS_COUNT_FRUSTUM; ++i)
139  Matrices[i]=other.Matrices[i];
140  }
141 
143  {
144  setFrom ( mat );
145  }
146 
147 
148  inline void SViewFrustum::transform(const core::matrix4& mat)
149  {
150  for (u32 i=0; i<VF_PLANE_COUNT; ++i)
151  mat.transformPlane(planes[i]);
152 
155  }
156 
157 
159  {
160  core::vector3df p;
164 
165  return p;
166  }
167 
169  {
170  core::vector3df p;
174 
175  return p;
176  }
177 
179  {
180  core::vector3df p;
184 
185  return p;
186  }
187 
189  {
190  core::vector3df p;
194 
195  return p;
196  }
197 
199  {
200  core::vector3df p;
204 
205  return p;
206  }
207 
209  {
210  core::vector3df p;
214 
215  return p;
216  }
217 
219  {
220  core::vector3df p;
224 
225  return p;
226  }
227 
229  {
230  core::vector3df p;
234 
235  return p;
236  }
237 
239  {
240  return boundingBox;
241  }
242 
244  {
246 
251  }
252 
255  inline void SViewFrustum::setFrom(const core::matrix4& mat)
256  {
257  // left clipping plane
258  planes[VF_LEFT_PLANE].Normal.X = mat[3 ] + mat[0];
259  planes[VF_LEFT_PLANE].Normal.Y = mat[7 ] + mat[4];
260  planes[VF_LEFT_PLANE].Normal.Z = mat[11] + mat[8];
261  planes[VF_LEFT_PLANE].D = mat[15] + mat[12];
262 
263  // right clipping plane
264  planes[VF_RIGHT_PLANE].Normal.X = mat[3 ] - mat[0];
265  planes[VF_RIGHT_PLANE].Normal.Y = mat[7 ] - mat[4];
266  planes[VF_RIGHT_PLANE].Normal.Z = mat[11] - mat[8];
267  planes[VF_RIGHT_PLANE].D = mat[15] - mat[12];
268 
269  // top clipping plane
270  planes[VF_TOP_PLANE].Normal.X = mat[3 ] - mat[1];
271  planes[VF_TOP_PLANE].Normal.Y = mat[7 ] - mat[5];
272  planes[VF_TOP_PLANE].Normal.Z = mat[11] - mat[9];
273  planes[VF_TOP_PLANE].D = mat[15] - mat[13];
274 
275  // bottom clipping plane
276  planes[VF_BOTTOM_PLANE].Normal.X = mat[3 ] + mat[1];
277  planes[VF_BOTTOM_PLANE].Normal.Y = mat[7 ] + mat[5];
278  planes[VF_BOTTOM_PLANE].Normal.Z = mat[11] + mat[9];
279  planes[VF_BOTTOM_PLANE].D = mat[15] + mat[13];
280 
281  // far clipping plane
282  planes[VF_FAR_PLANE].Normal.X = mat[3 ] - mat[2];
283  planes[VF_FAR_PLANE].Normal.Y = mat[7 ] - mat[6];
284  planes[VF_FAR_PLANE].Normal.Z = mat[11] - mat[10];
285  planes[VF_FAR_PLANE].D = mat[15] - mat[14];
286 
287  // near clipping plane
288  planes[VF_NEAR_PLANE].Normal.X = mat[2];
289  planes[VF_NEAR_PLANE].Normal.Y = mat[6];
290  planes[VF_NEAR_PLANE].Normal.Z = mat[10];
291  planes[VF_NEAR_PLANE].D = mat[14];
292 
293  // normalize normals
294  u32 i;
295  for ( i=0; i != VF_PLANE_COUNT; ++i)
296  {
297  const f32 len = -core::reciprocal_squareroot(
298  planes[i].Normal.getLengthSQ());
299  planes[i].Normal *= len;
300  planes[i].D *= len;
301  }
302 
303  // make bounding box
305  }
306 
311  {
312  u32 index = 0;
313  switch ( state )
314  {
316  index = SViewFrustum::ETS_PROJECTION; break;
317  case video::ETS_VIEW:
318  index = SViewFrustum::ETS_VIEW; break;
319  default:
320  break;
321  }
322  return Matrices [ index ];
323  }
324 
329  {
330  u32 index = 0;
331  switch ( state )
332  {
334  index = SViewFrustum::ETS_PROJECTION; break;
335  case video::ETS_VIEW:
336  index = SViewFrustum::ETS_VIEW; break;
337  default:
338  break;
339  }
340  return Matrices [ index ];
341  }
342 
344  inline bool SViewFrustum::clipLine(core::line3d<f32>& line) const
345  {
346  bool wasClipped = false;
347  for (u32 i=0; i < VF_PLANE_COUNT; ++i)
348  {
349  if (planes[i].classifyPointRelation(line.start) == core::ISREL3D_FRONT)
350  {
351  line.start = line.start.getInterpolated(line.end,
353  wasClipped = true;
354  }
355  if (planes[i].classifyPointRelation(line.end) == core::ISREL3D_FRONT)
356  {
357  line.end = line.start.getInterpolated(line.end,
359  wasClipped = true;
360  }
361  }
362  return wasClipped;
363  }
364 
365 
366 } // end namespace scene
367 } // end namespace irr
368 
369 #endif
370 
void setFrom(const core::matrix4 &mat)
This constructor creates a view frustum based on a projection and/or view matrix. ...
Definition: SViewFrustum.h:255
core::vector3df getNearLeftUp() const
returns the point which is on the near left upper corner inside the the view frustum.
Definition: SViewFrustum.h:198
T D
Distance from origin.
Definition: plane3d.h:231
T Y
Y coordinate of the vector.
Definition: vector3d.h:411
void transform(const core::matrix4 &mat)
transforms the frustum by the matrix
Definition: SViewFrustum.h:148
core::plane3d< f32 > planes[VF_PLANE_COUNT]
all planes enclosing the view frustum.
Definition: SViewFrustum.h:107
float f32
32 bit floating point variable.
Definition: irrTypes.h:104
vector3d< T > getInterpolated(const vector3d< T > &other, f64 d) const
Creates an interpolated vector between this vector and another vector.
Definition: vector3d.h:247
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.h:107
void transformPlane(core::plane3d< f32 > &plane) const
Transforms a plane by this matrix.
Definition: matrix4.h:1179
const core::aabbox3d< f32 > & getBoundingBox() const
returns a bounding box enclosing the whole view frustum
Definition: SViewFrustum.h:238
vector3d< T > Normal
Normal vector of the plane.
Definition: plane3d.h:228
core::vector3df getFarLeftDown() const
returns the point which is on the far left bottom corner inside the the view frustum.
Definition: SViewFrustum.h:168
Left plane of the frustum.
Definition: SViewFrustum.h:34
T X
X coordinate of the vector.
Definition: vector3d.h:408
core::vector3df getNearRightUp() const
returns the point which is on the near right top corner inside the the view frustum.
Definition: SViewFrustum.h:218
core::vector3df getFarLeftUp() const
returns the point which is on the far left upper corner inside the the view frustum.
Definition: SViewFrustum.h:158
Everything in the Irrlicht Engine can be found in this namespace.
Definition: aabbox3d.h:12
Top plane of the frustum.
Definition: SViewFrustum.h:40
3D line between two points with intersection methods.
Definition: line3d.h:18
Defines the view frustum. That&#39;s the space visible by the camera.
Definition: SViewFrustum.h:25
void reset(T x, T y, T z)
Resets the bounding box to a one-point box.
Definition: aabbox3d.h: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.h:195
Projection transformation.
Definition: IVideoDriver.h:59
core::vector3df getFarRightDown() const
returns the point which is on the far right bottom corner inside the the view frustum.
Definition: SViewFrustum.h:188
core::aabbox3d< f32 > boundingBox
bounding box around the view frustum
Definition: SViewFrustum.h:110
Right plane of the frustum.
Definition: SViewFrustum.h:36
core::vector3df getFarRightUp() const
returns the point which is on the far right top corner inside the the view frustum.
Definition: SViewFrustum.h:178
E_TRANSFORMATION_STATE
enumeration for geometry transformation states
Definition: IVideoDriver.h:52
bool clipLine(core::line3d< f32 > &line) const
clips a line to the view frustum.
Definition: SViewFrustum.h:344
vector3d< T > end
End point of line.
Definition: line3d.h:132
unsigned int u32
32 bit unsigned variable.
Definition: irrTypes.h:58
Amount of planes enclosing the view frustum. Should be 6.
Definition: SViewFrustum.h:43
void transformVect(vector3df &vect) const
Transforms the vector by this matrix.
Definition: matrix4.h:1137
core::vector3df cameraPosition
the position of the camera
Definition: SViewFrustum.h:104
core::vector3df getNearRightDown() const
returns the point which is on the near right bottom corner inside the the view frustum.
Definition: SViewFrustum.h:228
SViewFrustum()
Default Constructor.
Definition: SViewFrustum.h:48
Near plane of the frustum. That is the plane nearest to the eye.
Definition: SViewFrustum.h:32
View transformation.
Definition: IVideoDriver.h:55
4x4 matrix. Mostly used as transformation matrix for 3d calculations.
Definition: matrix4.h:45
vector3d< T > start
Start point of line.
Definition: line3d.h:130
void addInternalPoint(const vector3d< T > &p)
Adds a point to the bounding box.
Definition: aabbox3d.h:74
core::matrix4 & getTransform(video::E_TRANSFORMATION_STATE state)
get the given state&#39;s matrix based on frustum E_TRANSFORMATION_STATE
Definition: SViewFrustum.h:310
T Z
Z coordinate of the vector.
Definition: vector3d.h:414
Far plane of the frustum. That is the plane farest away from the eye.
Definition: SViewFrustum.h:30
void recalculateBoundingBox()
recalculates the bounding box member based on the planes
Definition: SViewFrustum.h:243
core::vector3df getNearLeftDown() const
returns the point which is on the near left bottom corner inside the the view frustum.
Definition: SViewFrustum.h:208
REALINLINE f64 reciprocal_squareroot(const f64 x)
Definition: irrMath.h:497
Bottom plane of the frustum.
Definition: SViewFrustum.h:38