2015-07-26 21:39:49 +00:00
|
|
|
#include "CRenderBucket.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include "CDrawUtil.h"
|
|
|
|
#include "CGraphics.h"
|
2015-11-26 10:42:42 +00:00
|
|
|
#include "CRenderer.h"
|
2015-07-26 21:39:49 +00:00
|
|
|
|
|
|
|
CRenderBucket::CRenderBucket()
|
|
|
|
{
|
|
|
|
mEstSize = 0;
|
|
|
|
mSize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRenderBucket::SetSortType(ESortType Type)
|
|
|
|
{
|
|
|
|
mSortType = Type;
|
|
|
|
}
|
|
|
|
|
2015-08-15 04:04:56 +00:00
|
|
|
void CRenderBucket::Add(const SRenderablePtr& ptr)
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
if (mSize >= mEstSize)
|
2015-08-15 04:04:56 +00:00
|
|
|
mRenderables.push_back(ptr);
|
2015-07-26 21:39:49 +00:00
|
|
|
else
|
2015-08-15 04:04:56 +00:00
|
|
|
mRenderables[mSize] = ptr;
|
2015-07-26 21:39:49 +00:00
|
|
|
|
|
|
|
mSize++;
|
|
|
|
}
|
|
|
|
|
2015-11-25 21:37:34 +00:00
|
|
|
void CRenderBucket::Sort(CCamera* pCamera)
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
|
|
|
struct {
|
|
|
|
CCamera *pCamera;
|
2015-08-15 04:04:56 +00:00
|
|
|
bool operator()(SRenderablePtr left, SRenderablePtr right) {
|
2015-07-26 21:39:49 +00:00
|
|
|
CVector3f cPos = pCamera->Position();
|
2015-09-01 17:05:48 +00:00
|
|
|
CVector3f cDir = pCamera->Direction();
|
2015-07-26 21:39:49 +00:00
|
|
|
|
|
|
|
CVector3f distL = left.AABox.ClosestPointAlongVector(cDir) - cPos;
|
|
|
|
float dotL = distL.Dot(cDir);
|
|
|
|
CVector3f distR = right.AABox.ClosestPointAlongVector(cDir) - cPos;
|
|
|
|
float dotR = distR.Dot(cDir);
|
|
|
|
return (dotL > dotR);
|
|
|
|
}
|
|
|
|
} backToFront;
|
2015-11-25 21:37:34 +00:00
|
|
|
backToFront.pCamera = pCamera;
|
2015-07-26 21:39:49 +00:00
|
|
|
|
|
|
|
if (mSortType == BackToFront)
|
2015-08-15 04:04:56 +00:00
|
|
|
std::stable_sort(mRenderables.begin(), mRenderables.begin() + mSize, backToFront);
|
2015-07-26 21:39:49 +00:00
|
|
|
|
|
|
|
// Test: draw node bounding boxes + vertices used for sorting
|
|
|
|
/*for (u32 iNode = 0; iNode < mNodes.size(); iNode++)
|
|
|
|
{
|
|
|
|
SMeshPointer *pNode = &mNodes[iNode];
|
|
|
|
CVector3f Vert = pNode->AABox.ClosestPointAlongVector(Camera.GetDirection());
|
|
|
|
CDrawUtil::DrawWireCube(pNode->AABox, CColor::skWhite);
|
|
|
|
|
|
|
|
CVector3f Dist = Vert - Camera.GetPosition();
|
|
|
|
float Dot = Dist.Dot(Camera.GetDirection());
|
|
|
|
if (Dot < 0.f) Dot = -Dot;
|
|
|
|
if (Dot > 50.f) Dot = 50.f;
|
|
|
|
float Intensity = 1.f - (Dot / 50.f);
|
|
|
|
CColor CubeColor(Intensity, Intensity, Intensity, 1.f);
|
|
|
|
|
|
|
|
CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(Vert).ToMatrix4f();
|
|
|
|
CGraphics::UpdateMVPBlock();
|
|
|
|
CDrawUtil::DrawCube(CubeColor);
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void CRenderBucket::Clear()
|
|
|
|
{
|
|
|
|
mEstSize = mSize;
|
2015-08-15 04:04:56 +00:00
|
|
|
if (mRenderables.size() > mSize) mRenderables.resize(mSize);
|
2015-07-26 21:39:49 +00:00
|
|
|
mSize = 0;
|
|
|
|
}
|
|
|
|
|
2015-11-26 10:42:42 +00:00
|
|
|
void CRenderBucket::Draw(const SViewInfo& ViewInfo)
|
2015-07-26 21:39:49 +00:00
|
|
|
{
|
2015-11-26 10:42:42 +00:00
|
|
|
ERenderOptions Options = ViewInfo.pRenderer->RenderOptions();
|
|
|
|
|
2015-07-26 21:39:49 +00:00
|
|
|
for (u32 n = 0; n < mSize; n++)
|
|
|
|
{
|
2015-08-15 04:04:56 +00:00
|
|
|
if (mRenderables[n].Command == eDrawMesh)
|
2015-11-27 23:28:35 +00:00
|
|
|
mRenderables[n].pRenderable->Draw(Options, mRenderables[n].ComponentIndex, ViewInfo);
|
2015-07-26 21:39:49 +00:00
|
|
|
|
2015-08-15 04:04:56 +00:00
|
|
|
else if (mRenderables[n].Command == eDrawSelection)
|
|
|
|
mRenderables[n].pRenderable->DrawSelection();
|
2015-07-26 21:39:49 +00:00
|
|
|
|
|
|
|
// todo: implementation for eDrawExtras
|
|
|
|
}
|
|
|
|
}
|