Navigation
Artikel
Stuff
RSS Feeds
|
Sourcecodes - DirectX Kamera KlasseSprachenübersicht/C / C++/ C#/Spieleprogrammierung/DirectX Keywords: DirectX, Direct3D Kamera Hallo,
scene.h: /**************************************************************************/ // /includes/graphic/scene.h // // This file manages the camera and the light /**************************************************************************/ /**************************************************************************/ // Defines /**************************************************************************/ #ifndef FILE_14_SCENE #define FILE_14_SCENE /**************************************************************************/ // Header /**************************************************************************/ #include <input_manager.h> #include <errorfile.h> #include <d3dx9math.h> namespace bn_Core { enum SC_CAMERA_MOVE { MOVE_FORWARD = 1, MOVE_BACK = 2, MOVE_LEFT = 3, MOVE_RIGHT = 4, MOVE_JUMP_UP = 5, MOVE_JUMP_DOWN = 6, MOVE_LOOK_UP = 7, MOVE_LOOK_DOWN = 8, MOVE_LOOK_SIDE = 9 }; //Class for the camera class DLLEXPORT bn_Scene { public: //Constructor bn_Scene(); //Destructor ~bn_Scene(); void Initialise(LPDIRECT3DDEVICE9 lpD3DDevice,int iScreenWidth,int iScreenHeight); //Sets the camera void Update(); //Moves the camera void MoveCamera(float fFramerate,SC_CAMERA_MOVE Type,float fMoveSpeed,float fMouseMove = 0.0f,float fMaxAngle = (D3DX_PI/2.0)); //Sets the states void SetState(); void SetLightPosition(int x,int y,int z); //Gets the position D3DXVECTOR3 GetPosition(); //Sets the light void SetLight(bool bState,D3DLIGHT9 *pLight,int iCount); //Returns the light state bool GetLightState(); void SetAngle(float fX, float fY = 0.0f){ m_fAngleX = fX; m_fAngleY = fY;} D3DXMATRIX* GetViewMatrix(){return &m_matViewMatrix;} D3DXMATRIX* GetProjectMatrix(){return &m_matProjectMatrix;} protected: //Pointer of a Device LPDIRECT3DDEVICE9 m_lpD3DDevice; //Position of the camera D3DXVECTOR3 m_vPosition; D3DXVECTOR3 m_vLookAt; D3DXVECTOR3 m_vUp; D3DXMATRIX m_matViewMatrix; D3DXMATRIX m_matProjectMatrix; //Light D3DLIGHT9 m_Light; //Speed && Angle for the x rotation float m_fSpeed,m_fAngleX,m_fAngleY; //LightState bool m_bLightState; //Size of the screen int m_iScreenWidth,m_iScreenHeight; //If it's intialized bool m_bInitialise; }; } #endif
scene.cpp: //******************************* FOR MORE SEE THE .h file ********************************// #include "Scene.h" //Constructor bn_Core::bn_Scene::bn_Scene() { //Sets the member variables m_vPosition = D3DXVECTOR3(0.0f,9.0f, 0.0f); m_vLookAt = D3DXVECTOR3(0.0f, 1.0f, 0.0f); m_vUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f); m_fSpeed = 3.0f; m_fAngleX = 0.0f; m_fAngleY = 0.0f; m_bInitialise = false; m_bLightState = true; } //Destructor bn_Core::bn_Scene::~bn_Scene() { } //Gets the position D3DXVECTOR3 bn_Core::bn_Scene::GetPosition() { //Returns it return m_vPosition; } void bn_Core::SetLightPosition(int x,int y,int z) { m_Light.Position.x += x; m_Light.Position.y = y; m_Light.Position.z = z; this->SetLight(true,&m_Light,0); } void bn_Core::bn_Scene::Initialise(LPDIRECT3DDEVICE9 lpD3DDevice,int iScreenWidth,int iScreenHeight) { m_lpD3DDevice = NULL; //Sets the device m_lpD3DDevice = lpD3DDevice; //Sets the member variables m_iScreenWidth = iScreenWidth; m_iScreenHeight = iScreenHeight; //Updates the camera this->Update(); //Sets the protection D3DXMatrixPerspectiveFovLH(&m_matProjectMatrix, D3DX_PI/4, (float)m_iScreenWidth / (float)m_iScreenHeight, 1.0f, 100.0f ); //Sets the transform m_lpD3DDevice->SetTransform(D3DTS_PROJECTION,&m_matProjectMatrix); //Sets it true m_bInitialise = true; //Sets the light ZeroMemory(&m_Light,sizeof(D3DLIGHT9)); //Sets the type // m_Light.Type = D3DLIGHT_POINT; m_Light.Position.x += 0.0f; m_Light.Position.y = 0.0f; m_Light.Position.z = 0.0f; // Create a white spotlight m_Light.Type = D3DLIGHT_POINT; m_Light.Diffuse.r = 1.0f; m_Light.Diffuse.g = 1.0f; m_Light.Diffuse.b = 1.0f; m_Light.Range = 1000.0f; // Direction always points away and down m_Light.Direction.x = 0.0f; m_Light.Direction.y = -0.5f; m_Light.Direction.z = -1.0f; m_Light.Falloff = 1.0f; m_Light.Theta = D3DXToRadian(10.0f); m_Light.Phi = D3DXToRadian(15.0f); // m_lpD3DDevice->SetRenderState(D3DRS_AMBIENT,0x00F0F0F0); this->SetState(); //Sets the light true this->SetLight(m_bLightState,&m_Light,0); D3DLIGHT9 Light; //Sets the light ZeroMemory(&Light,sizeof(D3DLIGHT9)); Light.Position.x += 5.0f; Light.Position.y = 3.0f; Light.Position.z = 30.0f; // Create a white spotlight Light.Type = D3DLIGHT_POINT; Light.Diffuse.r = 1.0f; Light.Diffuse.g = 1.0f; Light.Diffuse.b = 1.0f; Light.Range = 1000.0f; // Direction always points away and down Light.Direction.x = 0.0f; Light.Direction.y = -0.5f; Light.Direction.z = -1.0f; Light.Falloff = 1.0f; Light.Theta = D3DXToRadian(10.0f); Light.Phi = D3DXToRadian(15.0f); //Sets the light true this->SetLight(m_bLightState,&Light,1); } //Sets the states void bn_Core::bn_Scene::SetState() { //Sets the Render State DXTestForError(m_lpD3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE),"Error by setting render state",14,1); DXTestForError(m_lpD3DDevice->SetRenderState(D3DRS_ALPHAREF, 0x01),"Error by setting render state",14,1); DXTestForError(m_lpD3DDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL),"Error by setting render state",14,1); } //Sets the light void bn_Core::bn_Scene::SetLight(bool bState,D3DLIGHT9 *pLight,int iCount) { bState = false; //Sets the light nr. 0 m_lpD3DDevice->SetLight(iCount,pLight); m_lpD3DDevice->LightEnable(iCount,bState); m_lpD3DDevice->SetRenderState(D3DRS_LIGHTING,bState); m_lpD3DDevice->SetRenderState( D3DRS_AMBIENT, 0x00333333 ); //Sets the member var m_bLightState = bState; } //Returns the light state bool bn_Core::bn_Scene::GetLightState() { return m_bLightState; } //Sets the camera void bn_Core::bn_Scene::Update() { //If it's initialised if (m_bInitialise) { //Line of sight x -z m_vLookAt.x = sinf(m_fAngleY) + m_vPosition.x; m_vLookAt.z = cosf(m_fAngleY) + m_vPosition.z; //Height of sight m_vLookAt.y = sinf(m_fAngleX) + m_vPosition.y; //Sets the matrix D3DXMatrixLookAtLH(&m_matViewMatrix,&m_vPosition,&m_vLookAt,&m_vUp); //Sets the camera m_lpD3DDevice->SetTransform(D3DTS_VIEW,&m_matViewMatrix); } } //Moves the camera void bn_Core::bn_Scene::MoveCamera(float fFramerate,SC_CAMERA_MOVE iType,float fMoveSpeed,float fMouseMove,float fMaxAngle) { //Division by 0 if (fFramerate == 0) fFramerate = 25; //Jump up if (iType == MOVE_JUMP_UP) m_vPosition.y += fMoveSpeed; //Jump down if (iType == MOVE_JUMP_DOWN) m_vPosition.y -= fMoveSpeed; //Sets it true bool bAngleUP = true; //If the max angle exist if (fMaxAngle != 0.0f) { //Sets it false bAngleUP = false; //Sets it true, if it's under the max angle if ((iType == MOVE_LOOK_UP) && (m_fAngleX > -fMaxAngle)) bAngleUP = true; else if ((iType == MOVE_LOOK_DOWN) && (m_fAngleX < fMaxAngle)) bAngleUP = true; } //Angle Up if ((iType == MOVE_LOOK_UP) && bAngleUP) m_fAngleX -= fMouseMove * fMoveSpeed / fFramerate; //Angle Down if ((iType == MOVE_LOOK_DOWN) && bAngleUP) m_fAngleX -= fMouseMove * fMoveSpeed / fFramerate; //Angle Left/Right if (iType == MOVE_LOOK_SIDE) m_fAngleY += fMouseMove * fMoveSpeed / fFramerate; //Move forward if (iType == MOVE_FORWARD) { m_vPosition.x += fMoveSpeed * sinf(m_fAngleY) / fFramerate; m_vPosition.z += fMoveSpeed * cosf(m_fAngleY) / fFramerate; } //Move backward if (iType == MOVE_BACK) { m_vPosition.x -= fMoveSpeed * sinf(m_fAngleY) / fFramerate; m_vPosition.z -= fMoveSpeed * cosf(m_fAngleY) / fFramerate; } //Move right if (iType == MOVE_RIGHT) { m_vPosition.x += fMoveSpeed * sinf(D3DX_PI/2.0f + m_fAngleY) / fFramerate; m_vPosition.z += fMoveSpeed * cosf(D3DX_PI/2.0f + m_fAngleY) / fFramerate; } //Move left if (iType == MOVE_LEFT) { m_vPosition.x -= fMoveSpeed * sinf(D3DX_PI/2.0f + m_fAngleY) / fFramerate; m_vPosition.z -= fMoveSpeed * cosf(D3DX_PI/2.0f + m_fAngleY) / fFramerate; } } Gibt es noch irgendwelche Fragen, oder wollen Sie über den Artikel diskutieren? Sprachenübersicht/C / C++/ C#/Spieleprogrammierung/DirectX/DirectX Kamera Klasse |