00001
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "K3dPlaneWork.h"
00035
00036 K3dPlaneWork::K3dPlaneWork ( K3dGameData *_pGameData )
00037 {
00038 m_pGameData = _pGameData;
00039
00040 m_pGameData->SetPlaneWork ( this );
00041 m_pVector3Work = m_pGameData->GetVector3Work();
00042 }
00043
00044 K3dPlaneWork::~K3dPlaneWork()
00045 {
00046 }
00047
00049 void K3dPlaneWork::CalcVertexArray ( K3dPlaneObj *_pPlane )
00050 {
00051 K3dVector3Obj kUp ( 0,1,0 );
00052 K3dVector3Obj kPos0, kPos1, kPos2, kPos3;
00053 K3dVector3Obj *pNormal = _pPlane->GetNormal();
00054 float fWidth = _pPlane->GetWidth();
00055 float fHeight = _pPlane->GetHeight();
00056 K3dVector3Obj kCross = m_pVector3Work->Cross ( kUp, *pNormal );
00057
00058 if ( m_pVector3Work->Length(kCross) == 0 )
00059 {
00060 K3dVector3Obj kFront ( 0,0,-1 );
00061 K3dVector3Obj kRight ( 1,0,0 );
00062 float fDot = m_pVector3Work->Dot ( kUp, *pNormal );
00063
00064 if ( fDot>0 )
00065 {
00066 kPos0 = ( -kFront * fHeight );
00067 kPos1 = ( -kRight * fWidth );
00068 kPos2 = ( kFront * fHeight );
00069 kPos3 = ( kRight * fWidth );
00070 }
00071 else if ( fDot < 0 )
00072 {
00073 kPos3 = ( -kFront * fHeight );
00074 kPos2 = ( -kRight * fWidth );
00075 kPos1 = ( kFront * fHeight );
00076 kPos0 = ( kRight * fWidth );
00077 }
00078
00079 if ( _pPlane->GetVertexArray().size() == 4 )
00080 {
00081 *_pPlane->GetVertexArray() [3] = *_pPlane->GetPosition() + ( kPos0 + kPos1 );
00082 *_pPlane->GetVertexArray() [2] = *_pPlane->GetPosition() + ( kPos1 + kPos2 );
00083 *_pPlane->GetVertexArray() [1] = *_pPlane->GetPosition() + ( kPos2 + kPos3 );
00084 *_pPlane->GetVertexArray() [0] = *_pPlane->GetPosition() + ( kPos3 + kPos0 );
00085 }
00086 else
00087 {
00088 cerr << "Error in K3dPlaneVis::CalcVertexArray() -- Number of plane vertices must be 4, current number is " << _pPlane->GetVertexArray().size() << endl;
00089 }
00090 }
00091 else
00092 {
00093 kPos0 = ( kCross * fWidth );
00094 kCross = m_pVector3Work->UnitCross ( kCross, *pNormal );
00095 kPos1 = ( kCross * fHeight );
00096 kCross = m_pVector3Work->UnitCross ( kCross, *pNormal );
00097 kPos2 = ( kCross * fWidth );
00098 kCross = m_pVector3Work->UnitCross ( kCross, *pNormal );
00099 kPos3 = ( kCross * fHeight );
00100
00101 if ( _pPlane->GetVertexArray().size() == 4 )
00102 {
00103
00104 *_pPlane->GetVertexArray() [3] = *_pPlane->GetPosition() + ( kPos0 + kPos1 );
00105 *_pPlane->GetVertexArray() [2] = *_pPlane->GetPosition() + ( kPos1 + kPos2 );
00106 *_pPlane->GetVertexArray() [1] = *_pPlane->GetPosition() + ( kPos2 + kPos3 );
00107 *_pPlane->GetVertexArray() [0] = *_pPlane->GetPosition() + ( kPos3 + kPos0 );
00108 }
00109 else
00110 {
00111 cerr << "Error in K3dPlaneVis::CalcVertexArray() -- Number of plane vertices must be 4, current number is " << _pPlane->GetVertexArray().size() << endl;
00112 }
00113 }
00114 }
00115
00117 void K3dPlaneWork::CalcDistance ( K3dPlaneObj *_pPlane )
00118 {
00119 _pPlane->GetDistance() = m_pVector3Work->Dot ( *_pPlane->GetNormal(), *_pPlane->GetPosition() );
00120 }
00121
00123 void K3dPlaneWork::Init ( K3dPlaneObj *_pPlane )
00124 {
00125 *_pPlane->GetRelNormal() = *_pPlane->GetNormal();
00126 *_pPlane->GetRelPosition() = *_pPlane->GetPosition();
00127 CalcDistance ( _pPlane );
00128 CalcVertexArray ( _pPlane );
00129 }
00130
00135 void K3dPlaneWork::CalcPlane ( K3dPlaneObj *_pPlane, const K3dVector3Obj &_rkPosition, const K3dVector3Obj &_rkNormal )
00136 {
00137 *_pPlane->GetPosition() = _rkPosition;
00138 *_pPlane->GetNormal() = _rkNormal;
00139 Init ( _pPlane );
00140 }
00141
00147 void K3dPlaneWork::CalcPlane ( K3dPlaneObj *_pPlane, const K3dVector3Obj &_rkV0, const K3dVector3Obj &_rkV1, const K3dVector3Obj &_rkV2 )
00148 {
00149 *_pPlane->GetPosition() = _rkV0;
00150
00151 K3dVector3Obj kNormal;
00152 kNormal = m_pVector3Work->Normal ( _rkV0, _rkV1, _rkV2 );
00153 kNormal = m_pVector3Work->Normalize ( kNormal );
00154
00155 *_pPlane->GetNormal() = kNormal;
00156
00157 Init ( _pPlane );
00158 }
00159
00162 void K3dPlaneWork::UpdateMatrix ( K3dPlaneObj *_pPlane )
00163 {
00164
00165 _pPlane->GetMatrix()->GetTranslation ( *_pPlane->GetPosition() );
00166
00167 *_pPlane->GetRotMatrix() = *_pPlane->GetMatrix();
00168 _pPlane->GetRotMatrix()->ResetTranslation();
00169
00170
00171 K3dVector3Obj kRelNormal = *_pPlane->GetRelNormal();
00172 _pPlane->GetRotMatrix()->ChangeVectorGL ( kRelNormal );
00173 *_pPlane->GetNormal() = kRelNormal;
00174 *_pPlane->GetPosition() = *_pPlane->GetRelPosition() + *_pPlane->GetPosition();
00175 CalcDistance ( _pPlane );
00176 }
00177
00179 int K3dPlaneWork::FindPlaneId ( const char* _strName )
00180 {
00181 K3dString strName ( _strName );
00182 for ( int i=0; i<m_pGameData->GetPlaneObjSP().GetNum(); i++ )
00183 {
00184 K3dPlaneObj *pPlane = m_pGameData->GetPlaneObjSP().Get ( i );
00185 if ( strName == pPlane->GetName() )
00186 {
00187 return i;
00188 }
00189 }
00190 return -1;
00191 }
00192
00194 void K3dPlaneWork::UpdatePlanes()
00195 {
00196 for ( int i=0; i<m_pGameData->GetPlaneObjSP().GetNum(); i++ )
00197 {
00198 UpdateMatrix ( m_pGameData->GetPlaneObjSP().Get ( i ) );
00199 }
00200 }
00201
00204 void K3dPlaneWork::CheckPlane ( const int _iPlaneId )
00205 {
00206 K3dPlaneObj *pPlane = m_pGameData->GetPlaneObjSP().Get ( _iPlaneId );
00207 if ( pPlane )
00208 {
00209 cout << "void K3dPlaneObj::CheckPlane()" << endl;
00210 cout << "Address = " << pPlane << endl;
00211 cout << "m_iId = " << pPlane->GetId() << endl;
00212 cout << "m_bIsVisible = " << pPlane->GetIsVisible() << endl;
00213 cout << "Position X = " << pPlane->GetPosition()->GetX() << endl;
00214 cout << "Position Y = " << pPlane->GetPosition()->GetY() << endl;
00215 cout << "Position Z = " << pPlane->GetPosition()->GetZ() << endl;
00216 cout << "Normal X = " << pPlane->GetNormal()->GetX() << endl;
00217 cout << "Normal Y = " << pPlane->GetNormal()->GetY() << endl;
00218 cout << "Normal Z = " << pPlane->GetNormal()->GetZ() << endl;
00219 cout << "Distance = " << pPlane->GetDistance() << endl;
00220 cout << "Num of verts" << pPlane->GetVertexArray().size() << endl;
00221 for ( int i=0; i< ( int ) pPlane->GetVertexArray().size(); i++ )
00222 {
00223 cout << "Vertex " << i << endl;
00224 cout << "Position X = " << pPlane->GetVertexArray() [i]->GetX() << endl;
00225 cout << "Position Y = " << pPlane->GetVertexArray() [i]->GetY() << endl;
00226 cout << "Position Z = " << pPlane->GetVertexArray() [i]->GetZ() << endl;
00227 }
00228 }
00229 else
00230 {
00231 cerr << "Error in void K3dPlaneWork::CheckPlane() -- pPlane = " << pPlane << endl;
00232 }
00233 }
00234
00239 bool K3dPlaneWork::ComparePlane ( K3dPolyPlane *_pPlane,const TPolyPlaneArray &_rPlaneArray )
00240 {
00241 for ( size_t i=0; i<_rPlaneArray.size(); i++ )
00242 {
00243 K3dPolyPlane *pTestPlane = _rPlaneArray[i];
00244
00245 if ( *_pPlane->GetNormal() == *pTestPlane->GetNormal() )
00246 {
00247
00248 int iDist = ( int ) m_pGameData->GetIntersection()->PointPlane ( *_pPlane, *pTestPlane->GetPosition() );
00249 if ( iDist == 0 )
00250 {
00251 return true;
00252 }
00253 }
00254 }
00255 return false;
00256 }
00257
00258