2015-07-26 21:39:49 +00:00
|
|
|
#include "CLightNode.h"
|
2015-11-25 21:37:34 +00:00
|
|
|
#include <Common/Math.h>
|
2015-07-26 21:39:49 +00:00
|
|
|
#include <Core/CDrawUtil.h>
|
|
|
|
#include <Core/CGraphics.h>
|
|
|
|
#include <Core/CRenderer.h>
|
|
|
|
|
|
|
|
CLightNode::CLightNode(CSceneManager *pScene, CSceneNode *pParent, CLight *Light)
|
|
|
|
: CSceneNode(pScene, pParent)
|
|
|
|
{
|
|
|
|
mpLight = Light;
|
|
|
|
mLocalAABox = CAABox::skOne;
|
|
|
|
mPosition = Light->GetPosition();
|
|
|
|
|
|
|
|
switch (Light->GetType())
|
|
|
|
{
|
|
|
|
case eLocalAmbient: SetName("Ambient Light"); break;
|
|
|
|
case eDirectional: SetName("Directional Light"); break;
|
|
|
|
case eSpot: SetName("Spot Light"); break;
|
|
|
|
case eCustom: SetName("Custom Light"); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ENodeType CLightNode::NodeType()
|
|
|
|
{
|
|
|
|
return eLightNode;
|
|
|
|
}
|
|
|
|
|
2015-11-25 21:37:34 +00:00
|
|
|
void CLightNode::AddToRenderer(CRenderer *pRenderer, const SViewInfo& ViewInfo)
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
2015-11-26 09:05:26 +00:00
|
|
|
if (ViewInfo.GameMode) return;
|
2015-09-27 22:02:53 +00:00
|
|
|
|
2015-11-29 10:00:18 +00:00
|
|
|
if (ViewInfo.ViewFrustum.BoxInFrustum(AABox()))
|
|
|
|
pRenderer->AddOpaqueMesh(this, -1, AABox(), eDrawMesh);
|
|
|
|
|
|
|
|
if (IsSelected() && mpLight->GetType() == eCustom)
|
|
|
|
{
|
|
|
|
CAABox RadiusBox = (CAABox::skOne * 2.f * mpLight->GetRadius()) + mPosition;
|
|
|
|
|
|
|
|
if (ViewInfo.ViewFrustum.BoxInFrustum(RadiusBox))
|
|
|
|
pRenderer->AddOpaqueMesh(this, -1, AABox(), eDrawSelection);
|
|
|
|
}
|
2015-07-26 21:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 23:28:35 +00:00
|
|
|
void CLightNode::Draw(ERenderOptions /*Options*/, int /*ComponentIndex*/, const SViewInfo& ViewInfo)
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
2015-11-26 10:42:42 +00:00
|
|
|
CDrawUtil::DrawLightBillboard(mpLight->GetType(), mpLight->GetColor(), mPosition, BillboardScale(), TintColor(ViewInfo));
|
2015-11-29 10:00:18 +00:00
|
|
|
}
|
2015-07-26 21:39:49 +00:00
|
|
|
|
2015-11-29 10:00:18 +00:00
|
|
|
void CLightNode::DrawSelection()
|
|
|
|
{
|
|
|
|
CDrawUtil::DrawWireSphere(mPosition, mpLight->GetRadius(), mpLight->GetColor());
|
2015-07-26 21:39:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-28 18:37:22 +00:00
|
|
|
void CLightNode::RayAABoxIntersectTest(CRayCollisionTester& Tester, const SViewInfo& /*ViewInfo*/)
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
2015-11-25 21:37:34 +00:00
|
|
|
CVector2f BillScale = BillboardScale();
|
|
|
|
float ScaleXY = (BillScale.x > BillScale.y ? BillScale.x : BillScale.y);
|
2015-09-26 22:55:14 +00:00
|
|
|
|
2015-11-25 21:37:34 +00:00
|
|
|
CAABox BillBox = CAABox(mPosition + CVector3f(-ScaleXY, -ScaleXY, -BillScale.y),
|
|
|
|
mPosition + CVector3f( ScaleXY, ScaleXY, BillScale.y));
|
2015-07-26 21:39:49 +00:00
|
|
|
|
2015-11-25 21:37:34 +00:00
|
|
|
std::pair<bool,float> BoxResult = BillBox.IntersectsRay(Tester.Ray());
|
|
|
|
if (BoxResult.first) Tester.AddNode(this, 0, BoxResult.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
SRayIntersection CLightNode::RayNodeIntersectTest(const CRay& Ray, u32 AssetID, const SViewInfo& ViewInfo)
|
|
|
|
{
|
|
|
|
// todo: come up with a better way to share this code between CScriptNode and CLightNode
|
|
|
|
SRayIntersection out;
|
|
|
|
out.pNode = this;
|
2015-11-28 18:37:22 +00:00
|
|
|
out.ComponentIndex = AssetID;
|
2015-11-25 21:37:34 +00:00
|
|
|
|
|
|
|
CTexture *pBillboard = CDrawUtil::GetLightTexture(mpLight->GetType());
|
|
|
|
|
|
|
|
if (!pBillboard)
|
|
|
|
{
|
|
|
|
out.Hit = false;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 1: check whether the ray intersects with the plane the billboard is on
|
|
|
|
CPlane BillboardPlane(-ViewInfo.pCamera->Direction(), mPosition);
|
|
|
|
std::pair<bool,float> PlaneTest = Math::RayPlaneIntersecton(Ray, BillboardPlane);
|
|
|
|
|
|
|
|
if (PlaneTest.first)
|
|
|
|
{
|
|
|
|
// Step 2: transform the hit point into the plane's local space
|
|
|
|
CVector3f PlaneHitPoint = Ray.PointOnRay(PlaneTest.second);
|
|
|
|
CVector3f RelHitPoint = PlaneHitPoint - mPosition;
|
2015-07-26 21:39:49 +00:00
|
|
|
|
2015-11-25 21:37:34 +00:00
|
|
|
CVector3f PlaneForward = -ViewInfo.pCamera->Direction();
|
|
|
|
CVector3f PlaneRight = -ViewInfo.pCamera->RightVector();
|
|
|
|
CVector3f PlaneUp = ViewInfo.pCamera->UpVector();
|
|
|
|
CQuaternion PlaneRot = CQuaternion::FromAxes(PlaneRight, PlaneForward, PlaneUp);
|
|
|
|
|
|
|
|
CVector3f RotatedHitPoint = PlaneRot.Inverse() * RelHitPoint;
|
|
|
|
CVector2f LocalHitPoint = RotatedHitPoint.xz() / BillboardScale();
|
|
|
|
|
|
|
|
// Step 3: check whether the transformed hit point is in the -1 to 1 range
|
|
|
|
if ((LocalHitPoint.x >= -1.f) && (LocalHitPoint.x <= 1.f) && (LocalHitPoint.y >= -1.f) && (LocalHitPoint.y <= 1.f))
|
|
|
|
{
|
|
|
|
// Step 4: look up the hit texel and check whether it's transparent or opaque
|
|
|
|
CVector2f TexCoord = (LocalHitPoint + CVector2f(1.f)) * 0.5f;
|
|
|
|
TexCoord.x = -TexCoord.x + 1.f;
|
|
|
|
float TexelAlpha = pBillboard->ReadTexelAlpha(TexCoord);
|
|
|
|
|
|
|
|
if (TexelAlpha < 0.25f)
|
|
|
|
out.Hit = false;
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// It's opaque... we have a hit!
|
|
|
|
out.Hit = true;
|
|
|
|
out.Distance = PlaneTest.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
out.Hit = false;
|
|
|
|
}
|
2015-07-26 21:39:49 +00:00
|
|
|
|
|
|
|
else
|
2015-11-25 21:37:34 +00:00
|
|
|
out.Hit = false;
|
|
|
|
|
|
|
|
return out;
|
2015-07-26 21:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CLight* CLightNode::Light()
|
|
|
|
{
|
|
|
|
return mpLight;
|
|
|
|
}
|
2015-11-25 21:37:34 +00:00
|
|
|
|
|
|
|
CVector2f CLightNode::BillboardScale()
|
|
|
|
{
|
2015-11-29 11:28:10 +00:00
|
|
|
return AbsoluteScale().xz() * 0.75f;
|
2015-11-25 21:37:34 +00:00
|
|
|
}
|