NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
LightDirectionalRenderable.cpp
Go to the documentation of this file.
00001 
00009 #include <d3dx9.h>
00010 #include <windows.h>
00011 
00012 #include "../../../sharedbase/Entity.h"
00013 #include "../../../sharedbase/QuatMath.h"
00014 
00015 #include "LightDirectionalRenderable.h"
00016 
00017 namespace GraphicsCore {
00018   int LightDirectionalRenderable::lightCount = 0;
00019 
00020   LightDirectionalRenderable::LightDirectionalRenderable(EntitySPTR owner, ModuleInterface* renderModule) :
00021       LightAmbientRenderable(owner, renderModule) {
00022     this->SetEnableStatus(true);
00023     this->SetAmbientColor(D3DCOLOR_XRGB(10, 10, 10));
00024     this->SetDiffuseColor(D3DCOLOR_XRGB(255, 255, 255));
00025     this->SetSpecularColor(D3DCOLOR_XRGB(255, 255, 255));
00026     this->SetRange(sqrtf(FLT_MAX));
00027     this->SetDirection(D3DXVECTOR3(0.0f, -1.0f, 0.0f)); // Facing downwards by default.
00028     
00029     this->lightIndex = LightDirectionalRenderable::lightCount++;
00030   }
00031 
00032   LightDirectionalRenderable::~LightDirectionalRenderable() {
00033   }
00034 
00036   D3DXCOLOR LightDirectionalRenderable::GetDiffuseColor() {
00037     return this->diffuseColor;
00038   }
00039 
00041   bool LightDirectionalRenderable::SetDiffuseColor(D3DXCOLOR diffuseColor) {
00042     this->diffuseColor = diffuseColor;
00043     return true;
00044   }
00045 
00047   D3DXCOLOR LightDirectionalRenderable::GetSpecularColor() {
00048     return this->specularColor;
00049   }
00050 
00052   bool LightDirectionalRenderable::SetSpecularColor(D3DXCOLOR specularColor) {
00053     this->specularColor = specularColor;
00054     return true;
00055   }
00056 
00057   D3DXVECTOR3 LightDirectionalRenderable::GetDirection() {
00058     return this->direction;
00059   }
00060 
00061   bool LightDirectionalRenderable::SetDirection(D3DXVECTOR3 direction) {
00062     D3DXVec3Normalize(&(this->direction), &direction);
00063     return true;
00064   }
00065 
00066   float LightDirectionalRenderable::GetRange(void) {
00067     return this->range;
00068   }
00069 
00070   bool LightDirectionalRenderable::SetRange(float range) {
00071     this->range = range;
00072     return true;
00073   }
00074 
00075   void LightDirectionalRenderable::Render(LPDIRECT3DDEVICE9 pd3dDevice) {
00076     if (this->GetEnableStatus()) {
00077       D3DLIGHT9 light;
00078       light.Type = D3DLIGHT_DIRECTIONAL;
00079       light.Diffuse = this->GetDiffuseColor();
00080       light.Specular = this->GetSpecularColor();
00081       light.Ambient = this->GetAmbientColor();
00082       D3DXVECTOR3 dir = this->GetDirection();
00083       RotateVectorByQuaternion(&dir, &this->owner->GetWorldRotation());
00084       light.Direction = dir;
00085       light.Range = this->GetRange();
00086       light.Attenuation0 = light.Attenuation1 = light.Attenuation2 = 0.0f;
00087       
00088       int lightIndex = this->GetLightIndex();
00089       
00090       if (lightIndex <= 7) {
00091         pd3dDevice->SetLight(lightIndex, &light);
00092         pd3dDevice->LightEnable(lightIndex, TRUE);
00093       }
00094     }
00095 
00096   }
00097 }