Home | Namespaces | Hierarchy | Alphabetical List | Class list | Files | Namespace Members | Class members | File members | Tutorials
SViewFrustum.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2010 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 
76  const core::aabbox3d<f32> &getBoundingBox() const;
77 
79  inline void recalculateBoundingBox();
80 
83 
86 
88 
89  bool clipLine(core::line3d<f32>& line) const;
90 
93 
96 
99 
100  private:
102  enum E_TRANSFORMATION_STATE_FRUSTUM
103  {
104  ETS_VIEW = 0,
105  ETS_PROJECTION = 1,
106  ETS_COUNT_FRUSTUM
107  };
108 
110  core::matrix4 Matrices[ETS_COUNT_FRUSTUM];
111  };
112 
113 
118  {
120  boundingBox=other.boundingBox;
121 
122  u32 i;
123  for (i=0; i<VF_PLANE_COUNT; ++i)
124  planes[i]=other.planes[i];
125 
126  for (i=0; i<ETS_COUNT_FRUSTUM; ++i)
127  Matrices[i]=other.Matrices[i];
128  }
129 
131  {
132  setFrom ( mat );
133  }
134 
135 
136  inline void SViewFrustum::transform(const core::matrix4& mat)
137  {
138  for (u32 i=0; i<VF_PLANE_COUNT; ++i)
139  mat.transformPlane(planes[i]);
140 
143  }
144 
145 
147  {
148  core::vector3df p;
152 
153  return p;
154  }
155 
157  {
158  core::vector3df p;
162 
163  return p;
164  }
165 
167  {
168  core::vector3df p;
172 
173  return p;
174  }
175 
177  {
178  core::vector3df p;
182 
183  return p;
184  }
185 
187  {
188  return boundingBox;
189  }
190 
192  {
194 
199  }
200 
203  inline void SViewFrustum::setFrom(const core::matrix4& mat)
204  {
205  // left clipping plane
206  planes[VF_LEFT_PLANE].Normal.X = mat[3 ] + mat[0];
207  planes[VF_LEFT_PLANE].Normal.Y = mat[7 ] + mat[4];
208  planes[VF_LEFT_PLANE].Normal.Z = mat[11] + mat[8];
209  planes[VF_LEFT_PLANE].D = mat[15] + mat[12];
210 
211  // right clipping plane
212  planes[VF_RIGHT_PLANE].Normal.X = mat[3 ] - mat[0];
213  planes[VF_RIGHT_PLANE].Normal.Y = mat[7 ] - mat[4];
214  planes[VF_RIGHT_PLANE].Normal.Z = mat[11] - mat[8];
215  planes[VF_RIGHT_PLANE].D = mat[15] - mat[12];
216 
217  // top clipping plane
218  planes[VF_TOP_PLANE].Normal.X = mat[3 ] - mat[1];
219  planes[VF_TOP_PLANE].Normal.Y = mat[7 ] - mat[5];
220  planes[VF_TOP_PLANE].Normal.Z = mat[11] - mat[9];
221  planes[VF_TOP_PLANE].D = mat[15] - mat[13];
222 
223  // bottom clipping plane
224  planes[VF_BOTTOM_PLANE].Normal.X = mat[3 ] + mat[1];
225  planes[VF_BOTTOM_PLANE].Normal.Y = mat[7 ] + mat[5];
226  planes[VF_BOTTOM_PLANE].Normal.Z = mat[11] + mat[9];
227  planes[VF_BOTTOM_PLANE].D = mat[15] + mat[13];
228 
229  // far clipping plane
230  planes[VF_FAR_PLANE].Normal.X = mat[3 ] - mat[2];
231  planes[VF_FAR_PLANE].Normal.Y = mat[7 ] - mat[6];
232  planes[VF_FAR_PLANE].Normal.Z = mat[11] - mat[10];
233  planes[VF_FAR_PLANE].D = mat[15] - mat[14];
234 
235  // near clipping plane
236  planes[VF_NEAR_PLANE].Normal.X = mat[2];
237  planes[VF_NEAR_PLANE].Normal.Y = mat[6];
238  planes[VF_NEAR_PLANE].Normal.Z = mat[10];
239  planes[VF_NEAR_PLANE].D = mat[14];
240 
241  // normalize normals
242  u32 i;
243  for ( i=0; i != VF_PLANE_COUNT; ++i)
244  {
245  const f32 len = -core::reciprocal_squareroot(
246  planes[i].Normal.getLengthSQ());
247  planes[i].Normal *= len;
248  planes[i].D *= len;
249  }
250 
251  // make bounding box
253  }
254 
259  {
260  u32 index = 0;
261  switch ( state )
262  {
264  index = SViewFrustum::ETS_PROJECTION; break;
265  case video::ETS_VIEW:
266  index = SViewFrustum::ETS_VIEW; break;
267  default:
268  break;
269  }
270  return Matrices [ index ];
271  }
272 
277  {
278  u32 index = 0;
279  switch ( state )
280  {
282  index = SViewFrustum::ETS_PROJECTION; break;
283  case video::ETS_VIEW:
284  index = SViewFrustum::ETS_VIEW; break;
285  default:
286  break;
287  }
288  return Matrices [ index ];
289  }
290 
292  inline bool SViewFrustum::clipLine(core::line3d<f32>& line) const
293  {
294  bool wasClipped = false;
295  for (u32 i=0; i < VF_PLANE_COUNT; ++i)
296  {
297  if (planes[i].classifyPointRelation(line.start) == core::ISREL3D_FRONT)
298  {
299  line.start = line.start.getInterpolated(line.end,
301  wasClipped = true;
302  }
303  if (planes[i].classifyPointRelation(line.end) == core::ISREL3D_FRONT)
304  {
305  line.end = line.start.getInterpolated(line.end,
307  wasClipped = true;
308  }
309  }
310  return wasClipped;
311  }
312 
313 
314 } // end namespace scene
315 } // end namespace irr
316 
317 #endif
318 

The Irrlicht Engine
The Irrlicht Engine Documentation © 2003-2010 by Nikolaus Gebhardt. Generated on Fri Mar 21 2014 04:40:17 by Doxygen (1.8.1.2)