NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
CameraComponent.cpp
Go to the documentation of this file.
00001 
00014 #include <d3dx9.h>
00015 #include <windows.h>
00016 
00017 #include "CameraComponent.h"
00018 
00019 namespace GraphicsCore {
00020   const float DEFAULT_NEAR_CLIP = 10.0f;
00021   const float DEFAULT_FAR_CLIP = 1000.0f;
00022   const float DEFAULT_FOV = D3DXToRadian(60.0f);
00023   const D3DXVECTOR3 DEFAULT_OFFSET = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
00024   const D3DXQUATERNION DEFAULT_ROTATION = D3DXQUATERNION(0.0f, 0.0f, 0.0f, 1.0f);
00025 
00026   CameraComponent::CameraComponent(EntitySPTR owner, ModuleInterface* renderModule) :
00027       ComponentInterface(owner, renderModule) {
00028     this->SetClipRanges(DEFAULT_NEAR_CLIP, DEFAULT_FAR_CLIP);
00029     this->SetFov(DEFAULT_FOV);
00030     this->SetOffset(DEFAULT_OFFSET);
00031     this->SetRotation(DEFAULT_ROTATION);
00032   }
00033 
00034   CameraComponent::~CameraComponent() {
00035   }
00036 
00037   /* * * * * * * * * * */
00038   /* Field of view
00039   /* * * * * * * * * * */
00040 
00041   float CameraComponent::GetFov() {
00042     return this->fov;
00043   }
00044   ;
00045 
00046   bool CameraComponent::SetFov(float fov) {
00047     bool result = false;
00048     
00049     if (fov > 0.0f && fov <= D3DX_PI) {
00050       this->fov = fov;
00051       result = true;
00052     }
00053     
00054     return result;
00055   }
00056 
00057   /* * * * * * * * * * */
00058   /* Near and Far clip ranges
00059   /* * * * * * * * * * */
00060 
00061   float CameraComponent::GetNearClipRange() {
00062     return this->nearClip;
00063   }
00064 
00065   float CameraComponent::GetFarClipRange() {
00066     return this->farClip;
00067   }
00068 
00069   bool CameraComponent::SetClipRanges(float nearClip, float farClip) {
00070     bool result = false;
00071     
00072     if (nearClip >= 0.0f && farClip >= 0.0f && nearClip < farClip) {
00073       this->nearClip = nearClip;
00074       this->farClip = farClip;
00075       result = true;
00076     }
00077     
00078     return result;
00079   }
00080 
00081   /* * * * * * * * * * */
00082   /* Rotation
00083   /* * * * * * * * * * */
00084 
00085   D3DXQUATERNION CameraComponent::GetRotation() {
00086     return this->rotation;
00087   }
00088 
00089   // Rotation by rot
00090   bool CameraComponent::SetRotation(D3DXQUATERNION rot) {
00091     D3DXQuaternionNormalize(&rot, &rot);
00092     this->rotation = rot;
00093     return true;
00094   }
00095 
00096   // Rotate by yaw, pitch, and roll in radians
00097   bool CameraComponent::SetRotation(float yaw, float pitch, float roll) {
00098     D3DXQUATERNION rot;
00099     D3DXQuaternionRotationYawPitchRoll(&rot, yaw, pitch, roll);
00100     this->SetRotation(rot);
00101     return true;
00102   }
00103 
00104   bool CameraComponent::SetOffset(D3DXVECTOR3 offset) {
00105     this->offset = offset;
00106     return true;
00107   }
00108 
00109   D3DXVECTOR3 CameraComponent::GetOffset() {
00110     return this->offset;
00111   }
00112 
00113   // Offset by delta
00114   bool CameraComponent::ChangeOffset(D3DXVECTOR3 delta) {
00115     this->offset += delta;
00116     return true;
00117   }
00118 
00119   // Rotation by delta
00120   bool CameraComponent::ChangeRotation(D3DXQUATERNION delta) {
00121     D3DXQuaternionNormalize(&delta, &delta);
00122     this->rotation *= delta;
00123     return true;
00124   }
00125 
00126   // Rotate by delta yaw, pitch, and roll in radians
00127   bool CameraComponent::ChangeRotation(float deltaYaw, float deltaPitch, float deltaRoll) {
00128     D3DXQUATERNION delta;
00129     D3DXQuaternionRotationYawPitchRoll(&delta, deltaYaw, deltaPitch, deltaRoll);
00130     this->ChangeRotation(delta);
00131     return true;
00132   }
00133 }