home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2006 July & August
/
PCWorld_2006-07-08_cd.bin
/
temacd
/
planearcade
/
planearcade.exe
/
Tank3.bmp
/
camera.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2004-11-21
|
5KB
|
224 lines
#include "main.h"
//ENGINE
CAMERA Camera;
//MATICA
D3DXMATRIXA16 matView; //pohladu
//------------------------------------------------------------------
// Name: CAMERA()
// Desc: konstruktor
//------------------------------------------------------------------
CAMERA::CAMERA()
{
ActPos = Get3D(0.0f,0.0f,0.0f);
Pos = Get3D(0.0f,0.0f,0.0f);
Rot = Get3D(0.0f,0.0f,0.0f);
Angle1 = D3DX_PI/5.7f;
Angle2 = D3DX_PI/5.7f;
MaxDis = 3000.0f;
}
//------------------------------------------------------------------
// Name: Refresh()
// Desc: Obnovi maticu pohladu
//------------------------------------------------------------------
void CAMERA::RefreshYawPitchRoll()
{
//vypocita maticu
matView = GetMatrix(Pos,
Get3D(-Rot.X,Rot.Y,Rot.Z),
Get3D(1.0f,1.0f,1.0f));
D3DXMatrixInverse(&matView,NULL,&matView);
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
//vypocita frustrum roviny
CalcFrustrumPlanes();
}
//------------------------------------------------------------------
// Name: CalcFrustrumPlanes()
// Desc: Vypocita frustrum roviny
//------------------------------------------------------------------
void CAMERA::CalcFrustrumPlanes()
{
VECTOR3D Pom;
VECTOR3D Lom;
//Back
Pom.Y =Pos.Y +(1.0f * sinf(Rot.X)) ;
Pom.Z =Pos.Z +((cosf(Rot.X) *1.0f) * cosf(Rot.Y));
Pom.X =Pos.X +((cosf(Rot.X) *1.0f) * sinf(Rot.Y));
Back = CreatePlanePoint(Pos,Pom);
//Front
Pom.Z =Pos.Z +((cosf(Rot.X) *MaxDis) * cosf(Rot.Y));
Pom.X =Pos.X +((cosf(Rot.X) *MaxDis) * sinf(Rot.Y));
Pom.Y =Pos.Y +(MaxDis * sinf(Rot.X)) ;
Lom.Y =Pos.Y +((MaxDis-1.0f) * sinf(Rot.X)) ;
Lom.Z =Pos.Z +((cosf(Rot.X) *(MaxDis-1.0f)) * cosf(Rot.Y));
Lom.X =Pos.X +((cosf(Rot.X) *(MaxDis-1.0f)) * sinf(Rot.Y));
Front = CreatePlanePoint(Pom,Lom);
//Left
Pom.Y =Pos.Y +(1.0f * sinf(Rot.X)) ;
Pom.Z =Pos.Z +((cosf(Rot.X) *1.0f) * cosf(Rot.Y+Angle1));
Pom.X =Pos.X +((cosf(Rot.X) *1.0f) * sinf(Rot.Y+Angle1));
Left = CreatePlanePoint(Pos,Pom);
//Right
Pom.Y =Pos.Y +(1.0f * sinf(Rot.X)) ;
Pom.Z =Pos.Z +((cosf(Rot.X) *1.0f) * cosf(Rot.Y-Angle1));
Pom.X =Pos.X +((cosf(Rot.X) *1.0f) * sinf(Rot.Y-Angle1));
Right = CreatePlanePoint(Pos,Pom);
//Up
Pom.Y =Pos.Y +(1.0f * sinf(Rot.X-Angle2)) ;
Pom.Z =Pos.Z +((cosf(Rot.X-Angle2) *1.0f) * cosf(Rot.Y));
Pom.X =Pos.X +((cosf(Rot.X-Angle2) *1.0f) * sinf(Rot.Y));
Up = CreatePlanePoint(Pos,Pom);
//Down
Pom.Y =Pos.Y +(1.0f * sinf(Rot.X+Angle2)) ;
Pom.Z =Pos.Z +((cosf(Rot.X+Angle2) *1.0f) * cosf(Rot.Y));
Pom.X =Pos.X +((cosf(Rot.X+Angle2) *1.0f) * sinf(Rot.Y));
Down = CreatePlanePoint(Pos,Pom);
}
//------------------------------------------------------------------
// Name: LookAt()
// Desc: Vypocita orezavacie roviny
//------------------------------------------------------------------
void CAMERA::RefreshLookAt()
{
//oneskorenie
Pos.X += (ActPos.X-Pos.X)*Power(0.11f);
Pos.Y += (ActPos.Y-Pos.Y)*Power(0.11f);
Pos.Z += (ActPos.Z-Pos.Z)*Power(0.11f);
//vypocita maticu pohladu
D3DXVECTOR3 vEyePt( Pos.X, Pos.Y, Pos.Z );
D3DXVECTOR3 vLookatPt( Lok.X, Lok.Y,Lok.Z );
D3DXVECTOR3 vUpVec(0.0f, 1.0f, 0.0f );
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
//obnovi matico pohladu
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView);
//vypocita rotaciu
VECTOR3D Sme;
Sub(&Sme,Lok,Pos);
Normalize(&Sme);
Rot.X = asinf(Sme.Y);
Rot.Y = asinf(Sme.X);
if (Sme.Z <= 0.0f)
{
Rot.Y = 3.1418f - Rot.Y;
}
//vypocita frustrum roviny
CalcFrustrumPlanes();
}
//------------------------------------------------------------------
// Name: FrustrumSphere()
// Desc: frustrum culling pre gulu v 3D, funkcia vracia true
// ak sa gula nachadza v polhlade kamery
//------------------------------------------------------------------
bool CAMERA::FrustrumSphere(VECTOR3D S,float P)
{
bool VLeft = false;
bool VRight = false;
bool VUp = false;
bool VDown = false;
bool VBack = false;
bool VFront = false;
//BACK
if (PointPlane(Back,S) > -P)
VBack = true;
//FRONT
if (VBack == true)
{
if (PointPlane(Front,S) > -P)
VFront = true;
}
//LEFT
if (VFront == true)
{
if (PointPlane(Left,S) > -P)
VLeft = true;
}
//RIGHT
if (VLeft == true)
{
if (PointPlane(Right,S) > -P)
VRight = true;
}
//UP
if (VRight == true)
{
if (PointPlane(Up,S) > -P)
VUp = true;
}
//DOWN
if (VUp == true)
{
if (PointPlane(Down,S) > -P)
VDown = true;
}
return VDown;
}
//------------------------------------------------------------------
// Name: FrustrumBox()
// Desc: frustrum culling kvader, funkcia vracia true ak sa kvader
// nachadza v poh╛ade kamery
//------------------------------------------------------------------
bool CAMERA::FrustrumBox(VECTOR3D Min,VECTOR3D Max)
{
VECTOR3D Centre;
float Polomer;
Centre.X = (Min.X + Max.X)/2.0f;
Centre.Y = (Min.Y + Max.Y)/2.0f;
Centre.Z = (Min.Z + Max.Z)/2.0f;
Polomer = CalcDistance(Centre,Min);
bool Vys = FrustrumSphere(Centre,Polomer);
return Vys;
}