MercuryObjectBase.cpp

Go to the documentation of this file.
00001 #include "MercuryMessages.h"
00002 #include "MercuryObjectBase.h"
00003 #include "MercuryDisplay.h"
00004 #include "MercuryMath.h"
00005 #include "MercuryScreenManager.h"
00006 
00007 MercuryObjectBase::MercuryObjectBase()
00008 :m_parentObject(NULL)
00009 {
00010 }
00011 
00012 void MercuryObjectBase::Init()
00013 {
00014     m_rotMode = RM_NORMAL;
00015     m_scale.SetX(1);
00016     m_scale.SetY(1);
00017     m_scale.SetZ(1);
00018     m_fVisRadius = -1;
00019     m_taintedMatrix = true;
00020     m_drawOrder = 0;
00021     m_xalign = m_yalign = 0;
00022 }
00023 
00024 MercuryPoint MercuryObjectBase::GetGlobalPosition() const
00025 {
00026     if ( !m_parentObject )
00027         return m_position;
00028     return m_parentObject->GetGlobalPosition() + m_position;
00029 }
00030 
00031 void MercuryObjectBase::CalculateMatrices()
00032 {
00033     if ( m_taintedMatrix || (m_rotMode == RM_BILLBOARD) )
00034     {
00035         m_localMatrix.Identity();
00036         
00037         if( m_rotMode == RM_NORMAL )
00038         {
00039             m_localMatrix.Transotale( m_position.x, m_position.y, m_position.z,
00040                                         m_rotation.x, m_rotation.y, m_rotation.z,
00041                                         m_scale.x, m_scale.y, m_scale.z );
00042 
00043             //this must be done after scaling
00044             if ((m_xalign != 0) || (m_yalign != 0))
00045                 m_localMatrix.Translate(m_xalign, m_yalign, 0);
00046             m_taintedMatrix = false;
00047         }
00048         else
00049         {
00050 
00051             m_localMatrix.Translate(m_position.x, m_position.y, m_position.z);
00052 
00053             switch ( m_rotMode )
00054             {
00055             case RM_OFF:
00056                 break;
00057             case RM_BILLBOARD:
00058                 {
00059                     MercuryPoint up(0,0,1);
00060                     static MercuryPoint directionProj;
00061                     static float angle;
00062                     static MercuryPoint axis;
00063                     static MercuryMatrix world;
00064 
00065                     //http://www.lighthouse3d.com/opengl/billboarding/index.php?billCyl
00066                     MercuryPoint gp(GetGlobalPosition());
00067                     MercuryPoint lcp;
00068 
00069                     MercuryScreen* screen = SCREENMAN->GetCurrentScreen();
00070                     if (screen)
00071                     {
00072                         MercuryCamera* camera = screen->GetCamera();
00073                         if (camera)
00074                         {
00075                             lcp = camera->GetPosition();
00076 //                          up = camera->GetUp();
00077                         }   
00078                     }
00079 
00080                     directionProj = GetGlobalPosition() - lcp;
00081                     directionProj.NormalizeSelf();
00082 
00083                     axis = up.CrossProduct(directionProj);
00084                     angle = ACOS(DotProduct(up, directionProj));
00085 
00086                     //The destroys the previous matrix
00087                     world.RotateAngAxis(angle*RADDEG, axis.x, axis.y, axis.z);
00088                     m_localMatrix *= world;
00089                 }
00090                 break;
00091             case RM_MATRIX:
00092                 m_localMatrix *= m_matrix;
00093                 break;
00094             case RM_QUATERNION:
00095                 {
00096                     MercuryMatrix m;
00097                     m_rotQuat.toMatrix4(m);
00098                     m_localMatrix *= m;
00099                 }
00100                 break;
00101             default:
00102                 break;
00103             };
00104 
00105             m_localMatrix.Scale(m_scale.x, m_scale.y, m_scale.z);
00106             if ((m_xalign != 0) || (m_yalign != 0))
00107                 m_localMatrix.Translate(m_xalign, m_yalign, 0);
00108             m_taintedMatrix = false;
00109         }
00110     }
00111 
00112     m_finalMatrix = WorldStack.GetTop() *= m_localMatrix;
00113 }
00114 
00115 MercuryPoint MercuryObjectBase::GetTruePosition() const
00116 {
00117     MercuryPoint pOut;
00118     MercuryMatrix copy( m_finalMatrix );
00119     VectorMultiply( copy, gpZero, pOut );
00120     return pOut;
00121 }
00122 
00123 bool MercuryObjectBase::IsHidden() const
00124 {
00125     if (m_parentObject)
00126         return m_parentObject->IsHidden()||m_hidden;
00127 
00128     return m_hidden;
00129 }
00130 
00131 #include "MercuryObjectFactory.h"
00132 
00133 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, SetX, (float), float );
00134 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, SetY, (float), float );
00135 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, SetZ, (float), float );
00136 CLASS_FUNCTION_NOARGS( MercuryObject, GetX, float );
00137 CLASS_FUNCTION_NOARGS( MercuryObject, GetY, float );
00138 CLASS_FUNCTION_NOARGS( MercuryObject, GetZ, float );
00139 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, MoveX, (float), float );
00140 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, MoveY, (float), float );
00141 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, MoveZ, (float), float );
00142 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, SetRotX, (float), float );
00143 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, SetRotY, (float), float );
00144 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, SetRotZ, (float), float );
00145 CLASS_FUNCTION_NOARGS( MercuryObject, GetRotX, float );
00146 CLASS_FUNCTION_NOARGS( MercuryObject, GetRotY, float );
00147 CLASS_FUNCTION_NOARGS( MercuryObject, GetRotZ, float );
00148 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, MoveRotX, (float), float );
00149 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, MoveRotY, (float), float );
00150 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, MoveRotZ, (float), float );
00151 
00152 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, SetScaleX, (float), float );
00153 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, SetScaleY, (float), float );
00154 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, SetScaleZ, (float), float );
00155 CLASS_FUNCTION_NOARGS( MercuryObject, GetScaleX, float );
00156 CLASS_FUNCTION_NOARGS( MercuryObject, GetScaleY, float );
00157 CLASS_FUNCTION_NOARGS( MercuryObject, GetScaleZ, float );
00158 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, MoveScaleX, (float), float );
00159 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, MoveScaleY, (float), float );
00160 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, MoveScaleZ, (float), float );
00161 
00162 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, SetRotationMode, (RotationMode) (int), int );
00163 
00164 CLASS_FUNCTION_NOARGS( MercuryObject, GetParentObject, void* );
00165 CLASS_FUNCTION_NOARGS( MercuryObject, GetHide, bool );
00166 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, SetHide, (bool), bool );
00167 
00168 CLASS_FUNCTION_NOARGS( MercuryObject, GetDrawOrder, int );
00169 CLASS_FUNCTION_VOID_ONEARG( MercuryObject, SetDrawOrder, (int), int );
00170 
00171 /* 
00172  * Copyright (c) 2006 Joshua Allen
00173  * All rights reserved.
00174  *
00175  * Redistribution and use in source and binary forms, with or
00176  * without modification, are permitted provided that the following
00177  * conditions are met:
00178  *  -   Redistributions of source code must retain the above
00179  *      copyright notice, this list of conditions and the following disclaimer.
00180  *  -   Redistributions in binary form must reproduce the above copyright
00181  *      notice, this list of conditions and the following disclaimer in
00182  *      the documentation and/or other materials provided with the distribution.
00183  *  -   Neither the name of the Mercury Engine nor the names of its
00184  *      contributors may be used to endorse or promote products derived from
00185  *      this software without specific prior written permission.
00186  *
00187  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00188  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00189  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00190  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00191  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00192  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00193  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00194  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00195  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00196  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00197  */

Hosted by SourceForge.net Logo