K3dAim.cpp

Go to the documentation of this file.
00001 
00011 /***************************************************************************
00012  *   Copyright (C) 2007 by Jan Koci   *
00013  *   honza.koci@email.cz   *
00014  *   http://kengine.sourceforge.net/tutorial/
00015  *                                                                         *
00016  *   This program is free software; you can redistribute it and/or modify  *
00017  *   it under the terms of the GNU General Public License as published by  *
00018  *   the Free Software Foundation; either version 2 of the License, or     *
00019  *   (at your option) any later version.                                   *
00020  *                                                                         *
00021  *   This program is distributed in the hope that it will be useful,       *
00022  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00023  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00024  *   GNU General Public License for more details.                          *
00025  *                                                                         *
00026  *   You should have received a copy of the GNU General Public License     *
00027  *   along with this program; if not, write to the                         *
00028  *   Free Software Foundation, Inc.,                                       *
00029  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00030  ***************************************************************************/
00031 
00032 #include "K3dAim.h"
00033 
00034 
00035 K3dAim::K3dAim(K3dGameData *_pGameData)
00036 {
00037         m_pGameData = _pGameData;
00038         m_pRayBuild = m_pGameData->GetRayBuild();
00039 }
00040 
00041 K3dAim::~K3dAim()
00042 {
00043 }
00044 
00045 
00046 
00048 void K3dAim::CalcAimRayAverage(const float _fFarDist)
00049 {
00050         // Get screen width
00051         float fWidth = (float) m_pGameData->GetGraphicOption()->iWidth;
00052         // Get far plane
00053         //float fFar = (float) m_pGameData->GetGraphicOption()->iFarPlane;
00054         m_fFarDist = _fFarDist;
00055         // Calculate far plane width
00056         // Equation is:
00057         // FarPlaneWidth = (atan(a) * FarPlane);
00058         float fFarPlaneWidth = (K3dMath::ATan((float)m_pGameData->GetGraphicOption()->iFovy) * /*fFar*/m_fFarDist);
00059         if(fWidth > 0)
00060         {
00061                 m_fAverage = (fFarPlaneWidth) / fWidth;
00062         }
00063 }
00064 
00067 void K3dAim::CalcAimRay()
00068 {
00069         // Get active camera index
00070         int iCamId = m_pGameData->GetActiveCamId();
00071         if(iCamId>=0)
00072         {
00073                 // Get active camera
00074                 K3dCameraObj *pCamera = m_pGameData->GetCameraObjSP().Get(iCamId);
00075                 //
00076                 // Calculate ray origin and direction
00077                 //
00078                 K3dVector3Obj kCamPos = pCamera->GetCameraPos();
00079                 //float fFar = (float) m_pGameData->GetGraphicOption()->iFarPlane;
00080                 float fWidth = (float) m_pGameData->GetGraphicOption()->iWidth;
00081                 float fHeight = (float) m_pGameData->GetGraphicOption()->iHeight;
00082                 // Get view, up and right camera vector
00083                 K3dVector3Obj kView = pCamera->GetView();
00084                 K3dVector3Obj kUp = pCamera->GetUp();
00085                 K3dVector3Obj kRight = pCamera->GetRight();
00086                 // Get mouse cursor position
00087                 K3dMouse *pMouse = m_pGameData->GetMouse();
00088                 int  *pMousePos;
00089                 pMousePos = pMouse->GetMousePosition();
00090                 // Calculate middle width and height
00091                 float fMiddleWidth = fWidth * (float) 0.5;
00092                 float fMiddleHeight = fHeight * (float) 0.5;
00093                 // Calculate near plane center
00094                 K3dVector3Obj kNearPlaneCenter = kCamPos + (kView * m_fFarDist);
00095                 // Calculate cursor position in 2d
00096                 K3dVector2 kCursorPos;
00097                 kCursorPos[0] = -(pMousePos[1] - fMiddleHeight)* m_fAverage;
00098                 kCursorPos[1] = (pMousePos[0]- fMiddleWidth) * m_fAverage;
00099                 K3dVector3Obj kNearPlaneUp = kUp * kCursorPos[0];
00100                 K3dVector3Obj kNearPlaneRight = kRight * kCursorPos[1];
00101                 //m_kRay.GetDirection() = kNearPlaneCenter + (kNearPlaneUp + kNearPlaneRight);
00102                 //m_kRay.GetOrigin() = kCamPos;
00103 
00104                 m_pRay = m_pGameData->GetRayObjSP().Get(m_pGameData->GetAimRayId());
00105                 *m_pRay->GetDirection() = kNearPlaneCenter + (kNearPlaneUp + kNearPlaneRight);
00106                 *m_pRay->GetOrigin() = kCamPos;
00107         }
00108         else
00109         {
00110                 cerr << "void K3dAim::CalcAimRay() -- Error - Pointer to camera object is NULL!!" << endl;
00111         }
00112 
00113         //return m_kRay;
00114 }
00115 
00117 void K3dAim::CreateAimRay()
00118 {
00119         // Calculate aiming average
00120         CalcAimRayAverage((float) m_pGameData->GetGraphicOption()->iFarPlane);
00121         // Create aiming ray
00122         m_pRay = m_pRayBuild->CreateNewRayObj();
00123         m_pGameData->GetAimRayId() = m_pGameData->GetRayObjSP().GetNum()-1;
00124         // Calculate aiming ray
00125         CalcAimRay();
00126 }
00127 

Generated on Thu Aug 16 23:53:27 2007 for K3dEngine by  doxygen 1.5.0