void SpotLight::ApplyLight() { // Set spot light position and direction Float3 dir3(GetWorldMatrixPtr()->_31, GetWorldMatrixPtr()->_32, GetWorldMatrixPtr()->_33); dir3.normalize(); XMFLOAT3 pos(&GetWorldMatrixPtr()->_41); cBufferData.spotLight.direction = XMFLOAT3(dir3.x, dir3.y, dir3.z); cBufferData.spotLight.position = pos; D3D11_MAPPED_SUBRESOURCE edit; Renderer::theContextPtr->Map(cBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &edit); memcpy(edit.pData, &cBufferData, sizeof(cBufferData)); Renderer::theContextPtr->Unmap(cBuffer, 0); Renderer::theContextPtr->VSSetConstantBuffers(cBufferData.SPOT_LIGHT_REGISTER_SLOT, 1, &cBuffer.p); Renderer::theContextPtr->PSSetConstantBuffers(cBufferData.SPOT_LIGHT_REGISTER_SLOT, 1, &cBuffer.p); // Check if the camera is inside the lit area Float3 toCamera = ViewPortManager::GetReference().GetActiveViewPosition() - *(Float3 *)&pos.x; toCamera.normalize(); if(toCamera.dotProduct(dir3) > cBufferData.spotLight.cutoff) techniqueNameOverride = "RenderSpotLightInside"; else techniqueNameOverride = "RenderSpotLightOutside"; AddToContextSet(); }
void SpotLight::ApplyLight(ID3DXEffect *effectPtr) { D3DXHANDLE handle; D3DXMATRIX lightViewProj; D3DXMATRIX lightView; float4x4 invertMat; const float4x4 &worldMat = *(float4x4 *)GetWorldMatrixPtr(); handle = effectPtr->GetParameterByName(name.c_str(), "Position"); D3DXVECTOR4 positionExponent(worldMat.WAxis.x, worldMat.WAxis.y, worldMat.WAxis.z, 1); effectPtr->SetVector(handle, &positionExponent); handle = effectPtr->GetParameterByName(name.c_str(), "Direction"); float3 dir(worldMat.ZAxis.x, worldMat.ZAxis.y, worldMat.ZAxis.z); dir.normalize(); D3DXVECTOR4 directionCutOff(dir.x, dir.y, dir.z,1); effectPtr->SetVector(handle, &directionCutOff); handle = effectPtr->GetParameterByName(name.c_str(), "Attenuation"); effectPtr->SetVector(handle, &D3DXVECTOR4(attenuation, 1)); handle = effectPtr->GetParameterByName(name.c_str(), "Range"); effectPtr->SetFloat(handle, radius); handle = effectPtr->GetParameterByName(name.c_str(), "Exponent"); effectPtr->SetFloat(handle, exponent); handle = effectPtr->GetParameterByName(name.c_str(), "Cutoff"); effectPtr->SetFloat(handle, cutOff); lightModelPtr->GetShapes()[0]->AddToContextSet(); // Check if the camera is inside the lit area float3 toCamera = (ViewPortManager::GetReference().GetActiveViewPosition() - *(float3 *)&positionExponent).normalize(); if(toCamera.dotProduct(dir) > cutOff) effectPtr->SetTechnique("RenderSpotLightInside"); else effectPtr->SetTechnique("RenderSpotLightOutside"); // fo' dah debuggin' //DebugRenderer::GetInstance()->DrawCone( // lightModelPtr->GetShapes()[0]->GetFramePtr()->GetWorldMatrix()); Light::ApplyLight(effectPtr); }