AaronCameron.net
Not Left, nor right. Just correct.
Not a Member? - Login or Create an Account
Monday the 6th of February 2012 @ 08:48am
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cRFreeCamera.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2003  Aaron Cameron
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact information for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 #include "cRFreeCamera.h"
00026 
00027 #include "n2l/dynVars.h"
00028 #include "n2l/vfs.h"
00029 
00030 #include <GL/glu.h>
00031 
00032 #include <math.h>
00033 
00034 /******************************************************************************/
00035 namespace n2l
00036 {
00037     /**************************************************************************/
00038     cRFreeCamera::cRFreeCamera()
00039     {
00040         init();
00041     }
00042 
00043     /**************************************************************************/
00044     cRFreeCamera::cRFreeCamera( const tVector3f &iPosition,
00045         const tVector3f &iDirection, const tVector3f &iNormal )
00046     {
00047         init();
00048 
00049         mPos = iPosition;
00050         mDir = iDirection;
00051         mNormal = iNormal;
00052         mDir.normalize();
00053         mNormal.normalize();
00054     }
00055 
00056     /**************************************************************************/
00057     cRFreeCamera::cRFreeCamera( const cVfsNodeInterface &iNode )
00058     {
00059         init();
00060         load(iNode);
00061     }
00062 
00063     /**************************************************************************/
00064     cRFreeCamera::cRFreeCamera( const cDynVar &iDef )
00065     {
00066         init();
00067         load(iDef);
00068     }
00069 
00070     /**************************************************************************/
00071     cRFreeCamera::~cRFreeCamera()
00072     {
00073     }
00074 
00075     /**************************************************************************/
00076     void cRFreeCamera::load( const cVfsNodeInterface &iNode )
00077     {
00078         const tUint DataOffset =
00079             vfsNodeFileWithHeader( iNode, "n2l::cRFreeCamera");
00080 
00081         cDynVar def;
00082         def.unserialize( iNode.buffer().c_str()+DataOffset );
00083         load( def );
00084     }
00085 
00086     /**************************************************************************/
00087     void cRFreeCamera::load( const cDynVar &iDef )
00088     {
00089         if (iDef["pos"].isArray())
00090             mPos.set( iDef["pos"][0], iDef["pos"][1], iDef["pos"][2] );
00091 
00092         if (iDef["dir"].isArray()) {
00093             mDir.set( iDef["dir"][0], iDef["dir"][1], iDef["dir"][2] );
00094             mDir.normalize();
00095         }
00096 
00097         if (iDef["normal"].isArray()) {
00098             mNormal.set( iDef["normal"][0], iDef["normal"][1],
00099                 iDef["normal"][2] );
00100             mNormal.normalize();
00101         }
00102         ++mVersion;
00103     }
00104 
00105     /**************************************************************************/
00106     void cRFreeCamera::set( const tVector3f &iPosition,
00107         const tVector3f &iDirection, const tVector3f &iNormal )
00108     {
00109         mPos = iPosition;
00110         mDir = iDirection;
00111         mNormal = iNormal;
00112 
00113         mDir.normalize();
00114         mNormal.normalize();
00115 
00116         ++mVersion;
00117     }
00118 
00119     /**************************************************************************/
00120     void cRFreeCamera::pitchR( const tFloat &iAngle )
00121     {
00122         // Thinking this should be dir X normal
00123         rotateR( iAngle, mDir.cross(mNormal) );
00124         ++mVersion;
00125     }
00126 
00127     /**************************************************************************/
00128     void cRFreeCamera::yawR( const tFloat &iAngle )
00129     {
00130         rotateR( iAngle, mNormal );
00131         ++mVersion;
00132     }
00133 
00134     /**************************************************************************/
00135     void cRFreeCamera::rollR( const tFloat &iAngle )
00136     {
00137         rotateR( iAngle, mDir );
00138         ++mVersion;
00139     }
00140 
00141     /**************************************************************************/
00142     void cRFreeCamera::render() const
00143     {
00144         gluLookAt(  mPos.x(), mPos.y(), mPos.z(),
00145             mDir.x()+mPos.x(), mDir.y()+mPos.y(), mDir.z()+mPos.z(),
00146             mNormal.x(), mNormal.y(), mNormal.z() );
00147     }
00148 
00149     /**************************************************************************/
00150     void cRFreeCamera::move( const tFloat &iForward, const tFloat &iLeft,
00151         const tFloat &iUp )
00152     {
00153         // Forward/Backwards iDelta.x()
00154         mPos += mDir * iForward;
00155         mPos += mNormal.cross(mDir) * iLeft;
00156         mPos += mNormal * iUp;
00157 
00158         ++mVersion;
00159     }
00160 
00161     /**************************************************************************/
00162     void cRFreeCamera::moveBaseBy( const tVector3f &iDelta )
00163     {
00164         mPos += iDelta;
00165         ++mVersion;
00166     }
00167 
00168     /**************************************************************************/
00169     void cRFreeCamera::moveBaseTo( const tVector3f &iPos )
00170     {
00171         mPos = iPos;
00172         ++mVersion;
00173     }
00174 
00175     /**************************************************************************/
00176     void cRFreeCamera::rotateR( const tFloat &iAngle, const tVector3f &iAxis )
00177     {
00178         mDir.rotateR(iAngle, iAxis.x(),iAxis.y(),iAxis.z());
00179         mDir.normalize();
00180         mNormal.rotateR(iAngle, iAxis.x(),iAxis.y(),iAxis.z());
00181         mNormal.normalize();
00182         ++mVersion;
00183     }
00184 
00185     /**************************************************************************/
00186     void cRFreeCamera::init()
00187     {
00188         mPos.set(0, 0, 0);
00189         mDir.set(0, 0, 1);
00190         mNormal.set(0, 1, 0);
00191         mVersion = tUint(this); // Whatever.
00192     }
00193 
00194 } // namespace
©2012 Aaron Cameron