mirror of
https://github.com/AxioDL/PrimeWorldEditor.git
synced 2025-12-15 16:16:14 +00:00
Added support for depth groups on the renderer
This commit is contained in:
@@ -4,7 +4,8 @@
|
||||
#include "CRenderer.h"
|
||||
#include <algorithm>
|
||||
|
||||
void CRenderBucket::Add(const SRenderablePtr& rkPtr)
|
||||
// ************ CSubBucket ************
|
||||
void CRenderBucket::CSubBucket::Add(const SRenderablePtr& rkPtr)
|
||||
{
|
||||
if (mSize >= mEstSize)
|
||||
mRenderables.push_back(rkPtr);
|
||||
@@ -14,63 +15,84 @@ void CRenderBucket::Add(const SRenderablePtr& rkPtr)
|
||||
mSize++;
|
||||
}
|
||||
|
||||
void CRenderBucket::Sort(CCamera* pCamera)
|
||||
void CRenderBucket::CSubBucket::Sort(const CCamera* pkCamera, bool DebugVisualization)
|
||||
{
|
||||
if (mEnableDepthSort)
|
||||
std::stable_sort(mRenderables.begin(), mRenderables.begin() + mSize,
|
||||
[&, pkCamera](const SRenderablePtr& rkLeft, const SRenderablePtr& rkRight) -> bool
|
||||
{
|
||||
std::stable_sort(mRenderables.begin(), mRenderables.begin() + mSize,
|
||||
[&, pCamera](const SRenderablePtr& rkLeft, const SRenderablePtr& rkRight) -> bool
|
||||
CVector3f CamPos = pkCamera->Position();
|
||||
CVector3f CamDir = pkCamera->Direction();
|
||||
|
||||
CVector3f DistL = rkLeft.AABox.ClosestPointAlongVector(CamDir) - CamPos;
|
||||
CVector3f DistR = rkRight.AABox.ClosestPointAlongVector(CamDir) - CamPos;
|
||||
float DotL = DistL.Dot(CamDir);
|
||||
float DotR = DistR.Dot(CamDir);
|
||||
return (DotL > DotR);
|
||||
});
|
||||
|
||||
if (DebugVisualization)
|
||||
{
|
||||
for (u32 iPtr = 0; iPtr < mSize; iPtr++)
|
||||
{
|
||||
CVector3f CamPos = pCamera->Position();
|
||||
CVector3f CamDir = pCamera->Direction();
|
||||
SRenderablePtr *pPtr = &mRenderables[iPtr];
|
||||
CVector3f Point = pPtr->AABox.ClosestPointAlongVector(pkCamera->Direction());
|
||||
CDrawUtil::DrawWireCube(pPtr->AABox, CColor::skWhite);
|
||||
|
||||
CVector3f DistL = rkLeft.AABox.ClosestPointAlongVector(CamDir) - CamPos;
|
||||
CVector3f DistR = rkRight.AABox.ClosestPointAlongVector(CamDir) - CamPos;
|
||||
float DotL = DistL.Dot(CamDir);
|
||||
float DotR = DistR.Dot(CamDir);
|
||||
return (DotL > DotR);
|
||||
});
|
||||
CVector3f Dist = Point - pkCamera->Position();
|
||||
float Dot = Dist.Dot(pkCamera->Direction());
|
||||
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);
|
||||
|
||||
if (mEnableDepthSortDebugVisualization)
|
||||
{
|
||||
for (u32 iPtr = 0; iPtr < mSize; iPtr++)
|
||||
{
|
||||
SRenderablePtr *pPtr = &mRenderables[iPtr];
|
||||
CVector3f Point = pPtr->AABox.ClosestPointAlongVector(pCamera->Direction());
|
||||
CDrawUtil::DrawWireCube(pPtr->AABox, CColor::skWhite);
|
||||
|
||||
CVector3f Dist = Point - pCamera->Position();
|
||||
float Dot = Dist.Dot(pCamera->Direction());
|
||||
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(Point);
|
||||
CGraphics::UpdateMVPBlock();
|
||||
CDrawUtil::DrawCube(CubeColor);
|
||||
}
|
||||
CGraphics::sMVPBlock.ModelMatrix = CTransform4f::TranslationMatrix(Point);
|
||||
CGraphics::UpdateMVPBlock();
|
||||
CDrawUtil::DrawCube(CubeColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CRenderBucket::Clear()
|
||||
void CRenderBucket::CSubBucket::Clear()
|
||||
{
|
||||
mEstSize = mSize;
|
||||
if (mRenderables.size() > mSize) mRenderables.resize(mSize);
|
||||
mSize = 0;
|
||||
}
|
||||
|
||||
void CRenderBucket::Draw(const SViewInfo& rkViewInfo)
|
||||
void CRenderBucket::CSubBucket::Draw(const SViewInfo& rkViewInfo)
|
||||
{
|
||||
FRenderOptions Options = rkViewInfo.pRenderer->RenderOptions();
|
||||
|
||||
for (u32 iPtr = 0; iPtr < mSize; iPtr++)
|
||||
{
|
||||
if (mRenderables[iPtr].Command == eDrawMesh)
|
||||
mRenderables[iPtr].pRenderable->Draw(Options, mRenderables[iPtr].ComponentIndex, rkViewInfo);
|
||||
const SRenderablePtr& rkPtr = mRenderables[iPtr];
|
||||
|
||||
else if (mRenderables[iPtr].Command == eDrawSelection)
|
||||
mRenderables[iPtr].pRenderable->DrawSelection();
|
||||
if (rkPtr.Command == eDrawMesh)
|
||||
rkPtr.pRenderable->Draw(Options, rkPtr.ComponentIndex, rkViewInfo);
|
||||
|
||||
else if (rkPtr.Command == eDrawSelection)
|
||||
rkPtr.pRenderable->DrawSelection();
|
||||
}
|
||||
}
|
||||
|
||||
// ************ CRenderBucket ************
|
||||
void CRenderBucket::Add(const SRenderablePtr& rkPtr, bool Transparent)
|
||||
{
|
||||
if (Transparent)
|
||||
mTransparentSubBucket.Add(rkPtr);
|
||||
else
|
||||
mOpaqueSubBucket.Add(rkPtr);
|
||||
}
|
||||
|
||||
void CRenderBucket::Clear()
|
||||
{
|
||||
mOpaqueSubBucket.Clear();
|
||||
mTransparentSubBucket.Clear();
|
||||
}
|
||||
|
||||
void CRenderBucket::Draw(const SViewInfo& rkViewInfo)
|
||||
{
|
||||
mOpaqueSubBucket.Draw(rkViewInfo);
|
||||
mTransparentSubBucket.Sort(rkViewInfo.pCamera, mEnableDepthSortDebugVisualization);
|
||||
mTransparentSubBucket.Draw(rkViewInfo);
|
||||
}
|
||||
|
||||
@@ -12,27 +12,35 @@
|
||||
|
||||
class CRenderBucket
|
||||
{
|
||||
bool mEnableDepthSort;
|
||||
bool mEnableDepthSortDebugVisualization;
|
||||
std::vector<SRenderablePtr> mRenderables;
|
||||
u32 mEstSize;
|
||||
u32 mSize;
|
||||
|
||||
class CSubBucket
|
||||
{
|
||||
std::vector<SRenderablePtr> mRenderables;
|
||||
u32 mEstSize;
|
||||
u32 mSize;
|
||||
|
||||
public:
|
||||
CSubBucket()
|
||||
: mEstSize(0)
|
||||
, mSize(0)
|
||||
{}
|
||||
|
||||
void Add(const SRenderablePtr &rkPtr);
|
||||
void Sort(const CCamera *pkCamera, bool DebugVisualization);
|
||||
void Clear();
|
||||
void Draw(const SViewInfo& rkViewInfo);
|
||||
};
|
||||
|
||||
CSubBucket mOpaqueSubBucket;
|
||||
CSubBucket mTransparentSubBucket;
|
||||
|
||||
public:
|
||||
CRenderBucket()
|
||||
: mEnableDepthSort(false)
|
||||
, mEnableDepthSortDebugVisualization(false)
|
||||
, mEstSize(0)
|
||||
, mSize(0)
|
||||
: mEnableDepthSortDebugVisualization(false)
|
||||
{}
|
||||
|
||||
inline void SetDepthSortingEnabled(bool Enabled)
|
||||
{
|
||||
mEnableDepthSort = Enabled;
|
||||
}
|
||||
|
||||
void Add(const SRenderablePtr& rkPtr);
|
||||
void Sort(CCamera* pCamera);
|
||||
void Add(const SRenderablePtr& rkPtr, bool Transparent);
|
||||
void Clear();
|
||||
void Draw(const SViewInfo& rkViewInfo);
|
||||
};
|
||||
|
||||
@@ -24,7 +24,6 @@ CRenderer::CRenderer()
|
||||
, mInitialized(false)
|
||||
, mContextIndex(-1)
|
||||
{
|
||||
mTransparentBucket.SetDepthSortingEnabled(true);
|
||||
sNumRenderers++;
|
||||
}
|
||||
|
||||
@@ -127,11 +126,21 @@ void CRenderer::RenderBuckets(const SViewInfo& rkViewInfo)
|
||||
glDepthRange(0.f, 1.f);
|
||||
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
||||
|
||||
mOpaqueBucket.Draw(rkViewInfo);
|
||||
mOpaqueBucket.Clear();
|
||||
mTransparentBucket.Sort(rkViewInfo.pCamera);
|
||||
mTransparentBucket.Draw(rkViewInfo);
|
||||
mTransparentBucket.Clear();
|
||||
mBackgroundBucket.Draw(rkViewInfo);
|
||||
mBackgroundBucket.Clear();
|
||||
ClearDepthBuffer();
|
||||
mMidgroundBucket.Draw(rkViewInfo);
|
||||
mMidgroundBucket.Clear();
|
||||
ClearDepthBuffer();
|
||||
RenderBloom();
|
||||
ClearDepthBuffer();
|
||||
rkViewInfo.pCamera->LoadMatrices();
|
||||
mForegroundBucket.Draw(rkViewInfo);
|
||||
mForegroundBucket.Clear();
|
||||
ClearDepthBuffer();
|
||||
mUIBucket.Draw(rkViewInfo);
|
||||
mUIBucket.Clear();
|
||||
ClearDepthBuffer();
|
||||
}
|
||||
|
||||
void CRenderer::RenderBloom()
|
||||
@@ -266,24 +275,32 @@ void CRenderer::RenderSky(CModel *pSkyboxModel, const SViewInfo& rkViewInfo)
|
||||
pSkyboxModel->Draw(mOptions, 0);
|
||||
}
|
||||
|
||||
void CRenderer::AddOpaqueMesh(IRenderable *pRenderable, int AssetID, const CAABox& rkAABox, ERenderCommand Command)
|
||||
void CRenderer::AddMesh(IRenderable *pRenderable, int ComponentIndex, const CAABox& rkAABox, bool Transparent, ERenderCommand Command, EDepthGroup DepthGroup /*= eMidground*/)
|
||||
{
|
||||
SRenderablePtr Ptr;
|
||||
Ptr.pRenderable = pRenderable;
|
||||
Ptr.ComponentIndex = AssetID;
|
||||
Ptr.ComponentIndex = ComponentIndex;
|
||||
Ptr.AABox = rkAABox;
|
||||
Ptr.Command = Command;
|
||||
mOpaqueBucket.Add(Ptr);
|
||||
}
|
||||
|
||||
void CRenderer::AddTransparentMesh(IRenderable *pRenderable, int AssetID, const CAABox& rkAABox, ERenderCommand Command)
|
||||
{
|
||||
SRenderablePtr Ptr;
|
||||
Ptr.pRenderable = pRenderable;
|
||||
Ptr.ComponentIndex = AssetID;
|
||||
Ptr.AABox = rkAABox;
|
||||
Ptr.Command = Command;
|
||||
mTransparentBucket.Add(Ptr);
|
||||
switch (DepthGroup)
|
||||
{
|
||||
case eBackground:
|
||||
mBackgroundBucket.Add(Ptr, Transparent);
|
||||
break;
|
||||
|
||||
case eMidground:
|
||||
mMidgroundBucket.Add(Ptr, Transparent);
|
||||
break;
|
||||
|
||||
case eForeground:
|
||||
mForegroundBucket.Add(Ptr, Transparent);
|
||||
break;
|
||||
|
||||
case eUI:
|
||||
mUIBucket.Add(Ptr, Transparent);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void CRenderer::BeginFrame()
|
||||
|
||||
@@ -4,8 +4,9 @@
|
||||
#include "CCamera.h"
|
||||
#include "CGraphics.h"
|
||||
#include "CRenderBucket.h"
|
||||
#include "FRenderOptions.h"
|
||||
#include "EDepthGroup.h"
|
||||
#include "ERenderCommand.h"
|
||||
#include "FRenderOptions.h"
|
||||
#include "SRenderablePtr.h"
|
||||
#include "SViewInfo.h"
|
||||
#include "Core/OpenGL/CFramebuffer.h"
|
||||
@@ -33,8 +34,6 @@ private:
|
||||
bool mDrawGrid;
|
||||
CColor mClearColor;
|
||||
u32 mContextIndex;
|
||||
CRenderBucket mOpaqueBucket;
|
||||
CRenderBucket mTransparentBucket;
|
||||
bool mInitialized;
|
||||
u32 mViewportWidth, mViewportHeight;
|
||||
u32 mBloomWidth, mBloomHeight;
|
||||
@@ -44,6 +43,11 @@ private:
|
||||
CFramebuffer mBloomFramebuffers[3];
|
||||
GLint mDefaultFramebuffer;
|
||||
|
||||
CRenderBucket mBackgroundBucket;
|
||||
CRenderBucket mMidgroundBucket;
|
||||
CRenderBucket mForegroundBucket;
|
||||
CRenderBucket mUIBucket;
|
||||
|
||||
// Static Members
|
||||
static u32 sNumRenderers;
|
||||
|
||||
@@ -68,8 +72,7 @@ public:
|
||||
void RenderBuckets(const SViewInfo& rkViewInfo);
|
||||
void RenderBloom();
|
||||
void RenderSky(CModel *pSkyboxModel, const SViewInfo& rkViewInfo);
|
||||
void AddOpaqueMesh(IRenderable *pRenderable, int AssetID, const CAABox& rkAABox, ERenderCommand Command);
|
||||
void AddTransparentMesh(IRenderable *pRenderable, int AssetID, const CAABox& rkAABox, ERenderCommand Command);
|
||||
void AddMesh(IRenderable *pRenderable, int AssetID, const CAABox& rkAABox, bool Transparent, ERenderCommand Command, EDepthGroup DepthGroup = eMidground);
|
||||
void BeginFrame();
|
||||
void EndFrame();
|
||||
void ClearDepthBuffer();
|
||||
|
||||
13
src/Core/Render/EDepthGroup.h
Normal file
13
src/Core/Render/EDepthGroup.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef EDEPTHGROUP
|
||||
#define EDEPTHGROUP
|
||||
|
||||
enum EDepthGroup
|
||||
{
|
||||
eBackground,
|
||||
eMidground,
|
||||
eForeground,
|
||||
eUI
|
||||
};
|
||||
|
||||
#endif // EDEPTHGROUP
|
||||
|
||||
Reference in New Issue
Block a user