AaronCameron.net
No ads. No Profit. No Master, But Truth.
Not a Member? - Login or Create an Account
Wednesday the 23rd of May 2012 @ 05:39pm
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cRSkyMobileSprite.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2002  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 "renderObjects/cRSkyMobileSprite.h"
00026 
00027 #include "n2l/materials.h"
00028 
00029 #include "renderObjects/cRFrustum.h"
00030 
00031 #include <iostream>
00032 using namespace std;
00033 
00034 namespace n2l
00035 {
00036     /**************************************************************************/
00037     cRSkyMobileSprite::cRSkyMobileSprite( const tString &iName,
00038         const cAutoPtr<const cGLTexture> &iTexture )
00039     {
00040         if (!iTexture.isSet())
00041             throw cBadDataUseException( "cRSkyMobileSprite::cRSkyMobileSprite",
00042                 "Cannot create a sprite with no texture for sprite :\"" +
00043                 iName + "\"" );
00044 
00045         mName = iName;
00046         mTexture = iTexture;
00047     }
00048 
00049     /**************************************************************************/
00050 //  cRSkyMobileSprite::cRSkyMobileSprite( const cRSkyMobileSprite &iSprite )
00051 //  {
00052         
00053 //  }
00054 
00055     /**************************************************************************/
00056     const tVector3f cRSkyMobileSprite::mappedPos() const
00057     {
00058         tVector3f avg = mA;
00059         avg += mB;
00060         avg += mC;
00061         avg += mD;
00062         avg /= 4.0f;
00063         return avg;
00064     }
00065 
00066     /**************************************************************************/
00067     const tVector3f cRSkyMobileSprite::mappedSize() const
00068     {
00069     //  tVector3f size( fabsf(mB.x()-mC.x()), fabsf(mA.y()-mB.y()), .0f );
00070         return tVector3f( (mB-mC).magnitude(), (mA-mB).magnitude() );
00071     }
00072 
00073     /**************************************************************************/
00074     void cRSkyMobileSprite::relocate( const tVector3f &iViewpoint )
00075     {
00076         processAtLocation( mPos - iViewpoint );
00077     }
00078 
00079     /**************************************************************************/
00080     void cRSkyMobileSprite::locate( const tVector3f &iPos,
00081         const tVector3f &iSize, const tFloat &iZRot, const tFloat &iEqlDist )
00082     {
00083         mPos = iPos;
00084         mSize = iSize;
00085         mZRot = iZRot;
00086         mEqlDist = iEqlDist;
00087 
00088         processAtLocation( mPos );
00089     }
00090 
00091     /**************************************************************************/
00092     void cRSkyMobileSprite::draw( const cRFrustum &iFrustum,
00093         const tVector3f &iPosAdjust ) const
00094     {
00095 
00096         if (!iFrustum.testQuad( mA+iPosAdjust, mB+iPosAdjust, mC+iPosAdjust,
00097             mD+iPosAdjust))
00098         {
00099             return;
00100         }
00101 
00102         mTexture->bind();
00103 
00104         glBegin( GL_QUADS );
00105             glTexCoord2f( 0,0 );
00106             mA.glVertex3f();
00107             
00108             glTexCoord2f( 0,1 );
00109             mB.glVertex3f();
00110 
00111             glTexCoord2f( 1,1 );
00112             mC.glVertex3f();
00113 
00114             glTexCoord2f( 1,0 );
00115             mD.glVertex3f();
00116         glEnd();
00117     }
00118 
00119 
00120     /**************************************************************************/
00121     void cRSkyMobileSprite::processAtLocation( const tVector3f &iPos )
00122     {
00123         static const tFloat NintyRad = n2lDegToRad( 90.0f );
00124         static const tFloat OneEightRad = n2lDegToRad( 180.0f );
00125         static const tVector3f Zero(.0f, .0f, .0f);
00126         static const tVector3f FDir( .0f,.0f,1.0f );
00127         static const tVector3f Up( .0f,1.0f,.0f );
00128         static const tVector3f Left( 1.0f,.0f,.0f );
00129 
00130         const tVector3f Dir = iPos.normalized();
00131         const tFloat Dist = iPos.magnitude();
00132         const tVector3f HalfSize = mSize * .5f;
00133 
00134         tFloat theta = - (Dir.anglefR( Left ) - NintyRad);
00135         if (Dir.cross(Left).y()<.0) theta = OneEightRad-theta;
00136 
00137         tVector3f tmp = Dir;
00138         tmp.rotateR( theta, Up.x(),Up.y(),Up.z() );
00139         tFloat delta = (tmp.anglefR( Up ) - NintyRad );
00140 
00141         tPlane3f EqPl( FDir, FDir*mEqlDist );
00142 
00143         mA = FDir*Dist + (Up * HalfSize.y()) + (Left * HalfSize.x());
00144         mB = FDir*Dist - (Up * HalfSize.y()) + (Left * HalfSize.x());
00145         mC = FDir*Dist - (Up * HalfSize.y()) - (Left * HalfSize.x());
00146         mD = FDir*Dist + (Up * HalfSize.y()) - (Left * HalfSize.x());
00147 
00148         mA = EqPl.intersect( Zero, mA );
00149         mB = EqPl.intersect( Zero, mB );
00150         mC = EqPl.intersect( Zero, mC );
00151         mD = EqPl.intersect( Zero, mD );
00152 
00153         mA.rotateR( theta, Up.x(), Up.y(), Up.z() );
00154         mB.rotateR( theta, Up.x(), Up.y(), Up.z() );
00155         mC.rotateR( theta, Up.x(), Up.y(), Up.z() );
00156         mD.rotateR( theta, Up.x(), Up.y(), Up.z() );
00157 
00158         mA.rotateR( delta, Left.x(), Left.y(), Left.z() );
00159         mB.rotateR( delta, Left.x(), Left.y(), Left.z() );
00160         mC.rotateR( delta, Left.x(), Left.y(), Left.z() );
00161         mD.rotateR( delta, Left.x(), Left.y(), Left.z() );
00162 
00163     }
00164 
00165 } // namespace
©2012 Aaron Cameron