NL3D::CFrustum Class Reference

#include <frustum.h>


Detailed Description

A Screen frustum, perspective or orthogonal. No default ctor. Usefull for camera.
Author:
Lionel Berenguier

Nevrax France

Date:
2000

Definition at line 47 of file frustum.h.

Public Member Functions

 CFrustum (float left, float right, float bottom, float top, float znear, float zfar, bool perspective=true)
 ctor.

 CFrustum ()
 Un-initialized frustum.

void getValues (float &left, float &right, float &bottom, float &top, float &znear, float &zfar) const
 Get the value of the frustum.

void init (float width, float height, float znear, float zfar, bool perspective=true)
 Init a centered frustum.

void init (float left, float right, float bottom, float top, float znear, float zfar, bool perspective=true)
 Init a frustum.

void initPerspective (float fov, float aspectRatio, float znear, float zfar)
CVector project (const CVector &vec) const
CVector projectZ (const CVector &vec) const
CVector unProject (const CVector &vec) const
CVector unProjectZ (const CVector &vec) const

Data Fields

float Bottom
 NB: znear and zfar must be >0 (if perspective).

float Far
 NB: znear and zfar must be >0 (if perspective).

float Left
 NB: znear and zfar must be >0 (if perspective).

float Near
 NB: znear and zfar must be >0 (if perspective).

bool Perspective
float Right
 NB: znear and zfar must be >0 (if perspective).

float Top
 NB: znear and zfar must be >0 (if perspective).


Constructor & Destructor Documentation

NL3D::CFrustum::CFrustum  )  [inline]
 

Un-initialized frustum.

Definition at line 56 of file frustum.h.

References NL3D_MEM_FRUSTRUM.

00057         {
00058                 NL3D_MEM_FRUSTRUM
00059         }

NL3D::CFrustum::CFrustum float  left,
float  right,
float  bottom,
float  top,
float  znear,
float  zfar,
bool  perspective = true
[inline]
 

ctor.

Definition at line 61 of file frustum.h.

References init(), and NL3D_MEM_FRUSTRUM.

00062         {
00063                 NL3D_MEM_FRUSTRUM
00064                 init( left,  right,  bottom,  top,  znear,  zfar, perspective);
00065         }


Member Function Documentation

void NL3D::CFrustum::getValues float &  left,
float &  right,
float &  bottom,
float &  top,
float &  znear,
float &  zfar
const
 

Get the value of the frustum.

Definition at line 64 of file frustum.cpp.

References NL3D_MEM_FRUSTRUM.

Referenced by NL3D::CViewport::getRayWithPoint().

00065 {
00066         NL3D_MEM_FRUSTRUM
00067         left= Left;
00068         right= Right;
00069         bottom= Bottom;
00070         top= Top;
00071         znear= Near;
00072         zfar= Far;
00073 }

void NL3D::CFrustum::init float  width,
float  height,
float  znear,
float  zfar,
bool  perspective = true
 

Init a centered frustum.

Definition at line 51 of file frustum.cpp.

References height, init(), NL3D_MEM_FRUSTRUM, and width.

00052 {
00053         NL3D_MEM_FRUSTRUM
00054         init(-width/2, width/2, -height/2, height/2, znear, zfar, perspective);
00055 }

void NL3D::CFrustum::init float  left,
float  right,
float  bottom,
float  top,
float  znear,
float  zfar,
bool  perspective = true
 

Init a frustum.

Definition at line 39 of file frustum.cpp.

References NL3D_MEM_FRUSTRUM.

Referenced by NL3D::CEvent3dMouseListener::CEvent3dMouseListener(), CFrustum(), init(), initPerspective(), InitZBuffer(), and NL3D::CCamera::setFrustum().

00040 {
00041         NL3D_MEM_FRUSTRUM
00042         Left= left;
00043         Right= right;
00044         Bottom= bottom;
00045         Top= top;
00046         Near= znear;
00047         Far= zfar;
00048         Perspective= perspective;
00049 }

void NL3D::CFrustum::initPerspective float  fov,
float  aspectRatio,
float  znear,
float  zfar
 

Setup a perspective frustum, giving a fov in radians.

Parameters:
fov the horizontal angle of view, in radians. (Pi/2 as example)
aspectRatio the ratio horizontal/vertical (1.33 as example).
znear the front clipping plane distance.
zfar the back clipping plane distance.

Definition at line 56 of file frustum.cpp.

References init(), NL3D_MEM_FRUSTRUM, and w.

Referenced by NL3D::CCamera::setPerspective().

00057 {
00058         NL3D_MEM_FRUSTRUM
00059         float   w,h;
00060         w= 2*znear*(float)tan(fov/2);
00061         h= aspectRatio != 0.f ? w/aspectRatio : 0.f;
00062         init(w,h,znear,zfar,true);
00063 }

CVector NL3D::CFrustum::project const CVector vec  )  const
 

project a vector (x,y,z) onto frustum.

Parameters:
vec the point in 3D frustum space. Axis is NL3D axis: Xright, Yfront, Ztop.
Returns:
the point in 2D: Xright, Ytop, Z=0. Screen is mapped to X:[0..1], Y: [0..1].

Definition at line 77 of file frustum.cpp.

References NL3D_MEM_FRUSTRUM, w, NLMISC::CVector::x, NLMISC::CVector::y, and NLMISC::CVector::z.

Referenced by transformVectorToZBuffer().

00078 {
00079         NL3D_MEM_FRUSTRUM
00080         CVector         ret;
00081         float           decalX, decalY;
00082         float           w, h;
00083         float           OOw, OOh;
00084 
00085         // Fast transform to openGL like axis.
00086         CVector         pt;
00087         pt.x= vec.x;
00088         pt.y= vec.z;
00089         pt.z= -vec.y;
00090 
00091         decalX= (Right+Left);
00092         decalY= (Top+Bottom);
00093         w= Right-Left;
00094         h= Top-Bottom;
00095         OOw= 1.0f/w;
00096         OOh= 1.0f/h;
00097 
00098         // project to -1..+1.
00099         if(Perspective)
00100         {
00101                 ret.x= (2*Near*pt.x + decalX*pt.z)*OOw;
00102                 ret.x/= -pt.z;
00103                 ret.y= (2*Near*pt.y + decalY*pt.z)*OOh;
00104                 ret.y/= -pt.z;
00105         }
00106         else
00107         {
00108                 ret.x= (2*pt.x-decalX)*OOw;
00109                 ret.y= (2*pt.y-decalY)*OOh;
00110         }
00111 
00112 
00113         // Map it to 0..1.
00114         ret.x= 0.5f*(ret.x+1);
00115         ret.y= 0.5f*(ret.y+1);
00116         ret.z= 0;
00117 
00118         return ret;
00119 }

CVector NL3D::CFrustum::projectZ const CVector vec  )  const
 

project a vector (x,y,z) onto frustum.

Parameters:
vec the point in 3D frustum space. Axis is NL3D axis: Xright, Yfront, Ztop.
Returns:
the point in 2D: Xright, Ytop, Z=0. Screen is mapped to X:[0..1], Y: [0..1] Z is a positive screen depth in world units.

Definition at line 123 of file frustum.cpp.

References NL3D_MEM_FRUSTRUM, w, NLMISC::CVector::x, NLMISC::CVector::y, and NLMISC::CVector::z.

00124 {
00125         NL3D_MEM_FRUSTRUM
00126                 CVector         ret;
00127         float           decalX, decalY;
00128         float           w, h;
00129         float           OOw, OOh;
00130         
00131         // Fast transform to openGL like axis.
00132         CVector         pt;
00133         pt.x= vec.x;
00134         pt.y= vec.z;
00135         pt.z= -vec.y;
00136         
00137         decalX= (Right+Left);
00138         decalY= (Top+Bottom);
00139         w= Right-Left;
00140         h= Top-Bottom;
00141         OOw= 1.0f/w;
00142         OOh= 1.0f/h;
00143         
00144         // project to -1..+1.
00145         if(Perspective)
00146         {
00147                 ret.x= (2*Near*pt.x + decalX*pt.z)*OOw;
00148                 ret.x/= -pt.z;
00149                 ret.y= (2*Near*pt.y + decalY*pt.z)*OOh;
00150                 ret.y/= -pt.z;
00151         }
00152         else
00153         {
00154                 ret.x= (2*pt.x-decalX)*OOw;
00155                 ret.y= (2*pt.y-decalY)*OOh;
00156         }
00157         
00158         
00159         // Map it to 0..1.
00160         ret.x= 0.5f*(ret.x+1);
00161         ret.y= 0.5f*(ret.y+1);
00162         ret.z= pt.z;
00163         
00164         return ret;
00165 }

CVector NL3D::CFrustum::unProject const CVector vec  )  const
 

unproject a point from screen to the frustum space.

Parameters:
vec the point on the screen, in Left hand axis (XRight, YTop, ZFront). Z must be in [0..1] and hyperbolic.
Returns:
the point in the frustum space (NL3D right hand axis).

Definition at line 169 of file frustum.cpp.

References NL3D_MEM_FRUSTRUM, nlstop, w, NLMISC::CVector::x, NLMISC::CVector::y, and NLMISC::CVector::z.

00170 {
00171         NL3D_MEM_FRUSTRUM
00172         CVector         ret;
00173         float           decalX, decalY;
00174         float           w, h;
00175 
00176         decalX= (Right+Left);
00177         decalY= (Top+Bottom);
00178         w= Right-Left;
00179         h= Top-Bottom;
00180 
00181         // vec is a vector in a left hand axis.
00182         CVector         pt;
00183         pt.x= vec.x;
00184         pt.y= vec.y;
00185         pt.z= vec.z;
00186         
00187         // Map it to -1..1
00188         pt.x= 2*(pt.x-0.5f);
00189         pt.y= 2*(pt.y-0.5f);
00190 
00191         // Map Z to Near..Far.
00192         // Z IN is 1/Z, and is in 0..1.
00193         // inverse to 1..0.
00194         pt.z= 1-pt.z;
00195         // Map ret.z to 1/Far..1/Near.
00196         pt.z= 1/Far+(1/Near-1/Far)*pt.z;
00197         // Inverse, so ret.z E Near..Far.
00198         pt.z= 1/pt.z;
00199         // Actually, pt.z==w, homogenous coordinate.
00200 
00201 
00202         // unproject
00203         if(Perspective)
00204         {
00205                 // w of homogenous coordinate.
00206                 float   Wh;
00207                 float   Zin;
00208                 Wh= pt.z;
00209                 Zin= -pt.z;
00210 
00211                 // unproject.  (Projection is: x'= x/w.  y'= y/w).
00212                 pt.x= pt.x*Wh;
00213                 pt.y= pt.y*Wh;
00214                 ret.x= (pt.x*w-decalX*Zin)/(2*Near);
00215                 ret.y= (pt.y*h-decalY*Zin)/(2*Near);
00216                 ret.z= Zin;
00217         }
00218         else
00219         {
00220                 // NOT DONE YET.
00221                 nlstop;
00222                 /*ret.x= (pt.x*w+decalX)/2;
00223                 ret.y= (pt.y*h+decalY)/2;
00224                 */
00225         }
00226 
00227         // Fast transform from openGL like axis.
00228         pt =ret;
00229         ret.x= pt.x;
00230         ret.y= -pt.z;
00231         ret.z= pt.y;
00232 
00233         return ret;
00234 }

CVector NL3D::CFrustum::unProjectZ const CVector vec  )  const
 

unproject a point from screen to the frustum space.

Parameters:
vec the point on the screen, in Left hand axis (XRight, YTop, ZFront). Z is a positive depth in world unit.
Returns:
the point in the frustum space (NL3D right hand axis).

Definition at line 238 of file frustum.cpp.

References NL3D_MEM_FRUSTRUM, w, NLMISC::CVector::x, NLMISC::CVector::y, and NLMISC::CVector::z.

00239 {
00240         NL3D_MEM_FRUSTRUM
00241         CVector         ret;
00242         float           decalX, decalY;
00243         float           w, h;
00244 
00245         decalX= (Right+Left);
00246         decalY= (Top+Bottom);
00247         w= Right-Left;
00248         h= Top-Bottom;
00249 
00250         // vec is a vector in a left hand axis.
00251         CVector         pt;
00252         pt.x= vec.x;
00253         pt.y= vec.y;
00254         pt.z= vec.z;
00255         
00256         // Map it to -1..1
00257         pt.x= 2*(pt.x-0.5f);
00258         pt.y= 2*(pt.y-0.5f);
00259 
00260         // unproject
00261         if(Perspective)
00262         {
00263                 // w of homogenous coordinate.
00264                 float   Wh;
00265                 float   Zin;
00266                 Wh= pt.z;
00267                 Zin= -pt.z;
00268 
00269                 // unproject.  (Projection is: x'= x/w.  y'= y/w).
00270                 pt.x= pt.x*Wh;
00271                 pt.y= pt.y*Wh;
00272                 ret.x= (pt.x*w-decalX*Zin)/(2*Near);
00273                 ret.y= (pt.y*h-decalY*Zin)/(2*Near);
00274                 ret.z= Zin;
00275         }
00276         else
00277         {
00278                 // NOT DONE YET.
00279                 //nlstop;
00280                 /*ret.x= (pt.x*w+decalX)/2;
00281                 ret.y= (pt.y*h+decalY)/2;
00282                 */
00283                 // Yoyo: crash avoid for lem
00284                 ret= vec;
00285         }
00286 
00287         // Fast transform from openGL like axis.
00288         pt =ret;
00289         ret.x= pt.x;
00290         ret.y= -pt.z;
00291         ret.z= pt.y;
00292 
00293         return ret;
00294 }


Field Documentation

float NL3D::CFrustum::Bottom
 

NB: znear and zfar must be >0 (if perspective).

Definition at line 51 of file frustum.h.

Referenced by NL3D::CCamera::buildCameraPyramid(), NL3D::CCamera::getFrustum(), NL3D::CCloudScape::render(), NL3D::CDriverUser::restoreMatrixContextMatrixOnly(), and NL3D::CDriverUser::setupMatrixContext().

float NL3D::CFrustum::Far
 

NB: znear and zfar must be >0 (if perspective).

Definition at line 51 of file frustum.h.

Referenced by NL3D::CCamera::buildCameraPyramid(), NL3D::CCamera::getFrustum(), NL3D::CCloudScape::render(), NL3D::CDriverUser::restoreMatrixContextMatrixOnly(), NL3D::CDriverUser::setupMatrixContext(), and NL3D::CCamera::update().

float NL3D::CFrustum::Left
 

NB: znear and zfar must be >0 (if perspective).

Definition at line 51 of file frustum.h.

Referenced by NL3D::CCamera::buildCameraPyramid(), NL3D::CCamera::getFrustum(), NL3D::CCloudScape::render(), NL3D::CDriverUser::restoreMatrixContextMatrixOnly(), and NL3D::CDriverUser::setupMatrixContext().

float NL3D::CFrustum::Near
 

NB: znear and zfar must be >0 (if perspective).

Definition at line 51 of file frustum.h.

Referenced by NL3D::CCamera::buildCameraPyramid(), NL3D::CCamera::getFrustum(), NL3D::CCloudScape::render(), NL3D::CDriverUser::restoreMatrixContextMatrixOnly(), NL3D::CDriverUser::setupMatrixContext(), and NL3D::CCamera::update().

bool NL3D::CFrustum::Perspective
 

Definition at line 52 of file frustum.h.

Referenced by NL3D::CCamera::buildCameraPyramid(), NL3D::CCamera::isOrtho(), NL3D::CCamera::isPerspective(), NL3D::CCloudScape::render(), NL3D::CDriverUser::restoreMatrixContextMatrixOnly(), and NL3D::CDriverUser::setupMatrixContext().

float NL3D::CFrustum::Right
 

NB: znear and zfar must be >0 (if perspective).

Definition at line 51 of file frustum.h.

Referenced by NL3D::CCamera::buildCameraPyramid(), NL3D::CCamera::getFrustum(), NL3D::CCloudScape::render(), NL3D::CDriverUser::restoreMatrixContextMatrixOnly(), and NL3D::CDriverUser::setupMatrixContext().

float NL3D::CFrustum::Top
 

NB: znear and zfar must be >0 (if perspective).

Definition at line 51 of file frustum.h.

Referenced by NL3D::CCamera::buildCameraPyramid(), NL3D::CCamera::getFrustum(), NL3D::CCloudScape::render(), NL3D::CDriverUser::restoreMatrixContextMatrixOnly(), and NL3D::CDriverUser::setupMatrixContext().


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 06:47:00 2004 for NeL by doxygen 1.3.6