Updated and fixed DebugDrawer

Former-commit-id: b567c3a02174dbdb68f4e2d549d37c2b2b0a942b
This commit is contained in:
Lynix 2013-08-07 01:16:51 +02:00
parent 98246d0eee
commit c75887f600
1 changed files with 100 additions and 110 deletions

View File

@ -19,15 +19,13 @@
namespace namespace
{ {
static NzColor primaryColor; static NzColor s_primaryColor;
static NzColor secondaryColor; static NzColor s_secondaryColor;
static NzRenderStates renderStates; static NzRenderStates s_renderStates;
static const NzShaderProgram* program = nullptr; static const NzShaderProgram* s_program = nullptr;
static NzVertexBuffer* vertexBuffer = nullptr; static NzVertexBuffer s_vertexBuffer;
static NzVertexDeclaration* vertexDeclaration = nullptr; static bool s_initialized = false;
static bool depthBufferEnabled = true; static int s_colorLocation = -1;
static bool initialized = false;
static int colorLocation = -1;
} }
void NzDebugDrawer::Draw(const NzBoundingVolumef& volume) void NzDebugDrawer::Draw(const NzBoundingVolumef& volume)
@ -35,14 +33,14 @@ void NzDebugDrawer::Draw(const NzBoundingVolumef& volume)
if (!volume.IsFinite()) if (!volume.IsFinite())
return; return;
NzColor oldPrimaryColor = primaryColor; NzColor oldPrimaryColor = s_primaryColor;
Draw(volume.aabb); Draw(volume.aabb);
primaryColor = secondaryColor; s_primaryColor = s_secondaryColor;
Draw(volume.obb); Draw(volume.obb);
primaryColor = oldPrimaryColor; s_primaryColor = oldPrimaryColor;
} }
void NzDebugDrawer::Draw(const NzBoxi& box) void NzDebugDrawer::Draw(const NzBoxi& box)
@ -52,13 +50,13 @@ void NzDebugDrawer::Draw(const NzBoxi& box)
void NzDebugDrawer::Draw(const NzBoxf& box) void NzDebugDrawer::Draw(const NzBoxf& box)
{ {
if (!initialized) if (!s_initialized)
{ {
NazaraError("Debug drawer is not initialized"); NazaraError("Debug drawer is not s_initialized");
return; return;
} }
NzBufferMapper<NzVertexBuffer> mapper(vertexBuffer, nzBufferAccess_DiscardAndWrite, 0, 24); NzBufferMapper<NzVertexBuffer> mapper(s_vertexBuffer, nzBufferAccess_DiscardAndWrite, 0, 24);
NzVertexStruct_XYZ* vertex = reinterpret_cast<NzVertexStruct_XYZ*>(mapper.GetPointer()); NzVertexStruct_XYZ* vertex = reinterpret_cast<NzVertexStruct_XYZ*>(mapper.GetPointer());
NzVector3f max, min; NzVector3f max, min;
@ -127,11 +125,11 @@ void NzDebugDrawer::Draw(const NzBoxf& box)
mapper.Unmap(); mapper.Unmap();
NzRenderer::SetRenderStates(renderStates); NzRenderer::SetRenderStates(s_renderStates);
NzRenderer::SetShaderProgram(program); NzRenderer::SetShaderProgram(s_program);
NzRenderer::SetVertexBuffer(vertexBuffer); NzRenderer::SetVertexBuffer(&s_vertexBuffer);
program->SendColor(colorLocation, primaryColor); s_program->SendColor(s_colorLocation, s_primaryColor);
NzRenderer::DrawPrimitives(nzPrimitiveMode_LineList, 0, 24); NzRenderer::DrawPrimitives(nzPrimitiveMode_LineList, 0, 24);
} }
@ -143,13 +141,13 @@ void NzDebugDrawer::Draw(const NzBoxui& box)
void NzDebugDrawer::Draw(const NzFrustumf& frustum) void NzDebugDrawer::Draw(const NzFrustumf& frustum)
{ {
if (!initialized) if (!s_initialized)
{ {
NazaraError("Debug drawer is not initialized"); NazaraError("Debug drawer is not s_initialized");
return; return;
} }
NzBufferMapper<NzVertexBuffer> mapper(vertexBuffer, nzBufferAccess_DiscardAndWrite, 0, 24); NzBufferMapper<NzVertexBuffer> mapper(s_vertexBuffer, nzBufferAccess_DiscardAndWrite, 0, 24);
NzVertexStruct_XYZ* vertex = reinterpret_cast<NzVertexStruct_XYZ*>(mapper.GetPointer()); NzVertexStruct_XYZ* vertex = reinterpret_cast<NzVertexStruct_XYZ*>(mapper.GetPointer());
vertex->position.Set(frustum.GetCorner(nzCorner_NearLeftBottom)); vertex->position.Set(frustum.GetCorner(nzCorner_NearLeftBottom));
@ -214,24 +212,24 @@ void NzDebugDrawer::Draw(const NzFrustumf& frustum)
mapper.Unmap(); mapper.Unmap();
NzRenderer::SetRenderStates(renderStates); NzRenderer::SetRenderStates(s_renderStates);
NzRenderer::SetShaderProgram(program); NzRenderer::SetShaderProgram(s_program);
NzRenderer::SetVertexBuffer(vertexBuffer); NzRenderer::SetVertexBuffer(&s_vertexBuffer);
program->SendColor(colorLocation, primaryColor); s_program->SendColor(s_colorLocation, s_primaryColor);
NzRenderer::DrawPrimitives(nzPrimitiveMode_LineList, 0, 24); NzRenderer::DrawPrimitives(nzPrimitiveMode_LineList, 0, 24);
} }
void NzDebugDrawer::Draw(const NzOrientedBoxf& orientedBox) void NzDebugDrawer::Draw(const NzOrientedBoxf& orientedBox)
{ {
if (!initialized) if (!s_initialized)
{ {
NazaraError("Debug drawer is not initialized"); NazaraError("Debug drawer is not s_initialized");
return; return;
} }
NzBufferMapper<NzVertexBuffer> mapper(vertexBuffer, nzBufferAccess_DiscardAndWrite, 0, 24); NzBufferMapper<NzVertexBuffer> mapper(s_vertexBuffer, nzBufferAccess_DiscardAndWrite, 0, 24);
NzVertexStruct_XYZ* vertex = reinterpret_cast<NzVertexStruct_XYZ*>(mapper.GetPointer()); NzVertexStruct_XYZ* vertex = reinterpret_cast<NzVertexStruct_XYZ*>(mapper.GetPointer());
vertex->position.Set(orientedBox.GetCorner(nzCorner_NearLeftBottom)); vertex->position.Set(orientedBox.GetCorner(nzCorner_NearLeftBottom));
@ -296,31 +294,31 @@ void NzDebugDrawer::Draw(const NzOrientedBoxf& orientedBox)
mapper.Unmap(); mapper.Unmap();
NzRenderer::SetRenderStates(renderStates); NzRenderer::SetRenderStates(s_renderStates);
NzRenderer::SetShaderProgram(program); NzRenderer::SetShaderProgram(s_program);
NzRenderer::SetVertexBuffer(vertexBuffer); NzRenderer::SetVertexBuffer(&s_vertexBuffer);
program->SendColor(colorLocation, primaryColor); s_program->SendColor(s_colorLocation, s_primaryColor);
NzRenderer::DrawPrimitives(nzPrimitiveMode_LineList, 0, 24); NzRenderer::DrawPrimitives(nzPrimitiveMode_LineList, 0, 24);
} }
void NzDebugDrawer::Draw(const NzSkeleton* skeleton) void NzDebugDrawer::Draw(const NzSkeleton* skeleton)
{ {
if (!initialized) if (!s_initialized)
{ {
NazaraError("Debug drawer is not initialized"); NazaraError("Debug drawer is not s_initialized");
return; return;
} }
unsigned int jointCount = skeleton->GetJointCount(); unsigned int jointCount = skeleton->GetJointCount();
if (vertexBuffer->GetVertexCount() < jointCount*2) if (s_vertexBuffer.GetVertexCount() < jointCount*2)
{ {
NazaraError("Debug buffer not large enougth to draw object"); NazaraError("Debug buffer not large enougth to draw object");
return; return;
} }
NzBufferMapper<NzVertexBuffer> mapper(vertexBuffer, nzBufferAccess_DiscardAndWrite, 0, jointCount*2); NzBufferMapper<NzVertexBuffer> mapper(s_vertexBuffer, nzBufferAccess_DiscardAndWrite, 0, jointCount*2);
NzVertexStruct_XYZ* vertex = reinterpret_cast<NzVertexStruct_XYZ*>(mapper.GetPointer()); NzVertexStruct_XYZ* vertex = reinterpret_cast<NzVertexStruct_XYZ*>(mapper.GetPointer());
unsigned int vertexCount = 0; unsigned int vertexCount = 0;
@ -344,36 +342,36 @@ void NzDebugDrawer::Draw(const NzSkeleton* skeleton)
if (vertexCount > 0) if (vertexCount > 0)
{ {
NzRenderer::SetRenderStates(renderStates); NzRenderer::SetRenderStates(s_renderStates);
NzRenderer::SetShaderProgram(program); NzRenderer::SetShaderProgram(s_program);
NzRenderer::SetVertexBuffer(vertexBuffer); NzRenderer::SetVertexBuffer(&s_vertexBuffer);
program->SendColor(colorLocation, primaryColor); s_program->SendColor(s_colorLocation, s_primaryColor);
NzRenderer::DrawPrimitives(nzPrimitiveMode_LineList, 0, vertexCount); NzRenderer::DrawPrimitives(nzPrimitiveMode_LineList, 0, vertexCount);
program->SendColor(colorLocation, secondaryColor); s_program->SendColor(s_colorLocation, s_secondaryColor);
NzRenderer::DrawPrimitives(nzPrimitiveMode_PointList, 0, vertexCount); NzRenderer::DrawPrimitives(nzPrimitiveMode_PointList, 0, vertexCount);
} }
} }
void NzDebugDrawer::DrawBinormals(const NzStaticMesh* subMesh) void NzDebugDrawer::DrawBinormals(const NzStaticMesh* subMesh)
{ {
if (!initialized) if (!s_initialized)
{ {
NazaraError("Debug drawer is not initialized"); NazaraError("Debug drawer is not s_initialized");
return; return;
} }
unsigned int normalCount = subMesh->GetVertexCount(); unsigned int normalCount = subMesh->GetVertexCount();
unsigned int vertexCount = normalCount*2; unsigned int vertexCount = normalCount*2;
if (vertexBuffer->GetVertexCount() < vertexCount) if (s_vertexBuffer.GetVertexCount() < vertexCount)
{ {
NazaraError("Debug buffer not large enougth to draw object"); NazaraError("Debug buffer not large enougth to draw object");
return; return;
} }
NzBufferMapper<NzVertexBuffer> inputMapper(subMesh->GetVertexBuffer(), nzBufferAccess_ReadOnly); NzBufferMapper<NzVertexBuffer> inputMapper(subMesh->GetVertexBuffer(), nzBufferAccess_ReadOnly);
NzBufferMapper<NzVertexBuffer> outputMapper(vertexBuffer, nzBufferAccess_DiscardAndWrite, 0, vertexCount); NzBufferMapper<NzVertexBuffer> outputMapper(s_vertexBuffer, nzBufferAccess_DiscardAndWrite, 0, vertexCount);
NzMeshVertex* inputVertex = reinterpret_cast<NzMeshVertex*>(inputMapper.GetPointer()); NzMeshVertex* inputVertex = reinterpret_cast<NzMeshVertex*>(inputMapper.GetPointer());
NzVertexStruct_XYZ* outputVertex = reinterpret_cast<NzVertexStruct_XYZ*>(outputMapper.GetPointer()); NzVertexStruct_XYZ* outputVertex = reinterpret_cast<NzVertexStruct_XYZ*>(outputMapper.GetPointer());
@ -394,33 +392,33 @@ void NzDebugDrawer::DrawBinormals(const NzStaticMesh* subMesh)
if (vertexCount > 0) if (vertexCount > 0)
{ {
NzRenderer::SetRenderStates(renderStates); NzRenderer::SetRenderStates(s_renderStates);
NzRenderer::SetShaderProgram(program); NzRenderer::SetShaderProgram(s_program);
NzRenderer::SetVertexBuffer(vertexBuffer); NzRenderer::SetVertexBuffer(&s_vertexBuffer);
program->SendColor(colorLocation, primaryColor); s_program->SendColor(s_colorLocation, s_primaryColor);
NzRenderer::DrawPrimitives(nzPrimitiveMode_LineList, 0, vertexCount); NzRenderer::DrawPrimitives(nzPrimitiveMode_LineList, 0, vertexCount);
} }
} }
void NzDebugDrawer::DrawNormals(const NzStaticMesh* subMesh) void NzDebugDrawer::DrawNormals(const NzStaticMesh* subMesh)
{ {
if (!initialized) if (!s_initialized)
{ {
NazaraError("Debug drawer is not initialized"); NazaraError("Debug drawer is not s_initialized");
return; return;
} }
unsigned int normalCount = subMesh->GetVertexCount(); unsigned int normalCount = subMesh->GetVertexCount();
unsigned int vertexCount = normalCount*2; unsigned int vertexCount = normalCount*2;
if (vertexBuffer->GetVertexCount() < vertexCount) if (s_vertexBuffer.GetVertexCount() < vertexCount)
{ {
NazaraError("Debug buffer not large enougth to draw object"); NazaraError("Debug buffer not large enougth to draw object");
return; return;
} }
NzBufferMapper<NzVertexBuffer> inputMapper(subMesh->GetVertexBuffer(), nzBufferAccess_ReadOnly); NzBufferMapper<NzVertexBuffer> inputMapper(subMesh->GetVertexBuffer(), nzBufferAccess_ReadOnly);
NzBufferMapper<NzVertexBuffer> outputMapper(vertexBuffer, nzBufferAccess_DiscardAndWrite, 0, vertexCount); NzBufferMapper<NzVertexBuffer> outputMapper(s_vertexBuffer, nzBufferAccess_DiscardAndWrite, 0, vertexCount);
NzMeshVertex* inputVertex = reinterpret_cast<NzMeshVertex*>(inputMapper.GetPointer()); NzMeshVertex* inputVertex = reinterpret_cast<NzMeshVertex*>(inputMapper.GetPointer());
NzVertexStruct_XYZ* outputVertex = reinterpret_cast<NzVertexStruct_XYZ*>(outputMapper.GetPointer()); NzVertexStruct_XYZ* outputVertex = reinterpret_cast<NzVertexStruct_XYZ*>(outputMapper.GetPointer());
@ -441,33 +439,33 @@ void NzDebugDrawer::DrawNormals(const NzStaticMesh* subMesh)
if (vertexCount > 0) if (vertexCount > 0)
{ {
NzRenderer::SetRenderStates(renderStates); NzRenderer::SetRenderStates(s_renderStates);
NzRenderer::SetShaderProgram(program); NzRenderer::SetShaderProgram(s_program);
NzRenderer::SetVertexBuffer(vertexBuffer); NzRenderer::SetVertexBuffer(&s_vertexBuffer);
program->SendColor(colorLocation, primaryColor); s_program->SendColor(s_colorLocation, s_primaryColor);
NzRenderer::DrawPrimitives(nzPrimitiveMode_LineList, 0, vertexCount); NzRenderer::DrawPrimitives(nzPrimitiveMode_LineList, 0, vertexCount);
} }
} }
void NzDebugDrawer::DrawTangents(const NzStaticMesh* subMesh) void NzDebugDrawer::DrawTangents(const NzStaticMesh* subMesh)
{ {
if (!initialized) if (!s_initialized)
{ {
NazaraError("Debug drawer is not initialized"); NazaraError("Debug drawer is not s_initialized");
return; return;
} }
unsigned int tangentCount = subMesh->GetVertexCount(); unsigned int tangentCount = subMesh->GetVertexCount();
unsigned int vertexCount = tangentCount*2; unsigned int vertexCount = tangentCount*2;
if (vertexBuffer->GetVertexCount() < vertexCount) if (s_vertexBuffer.GetVertexCount() < vertexCount)
{ {
NazaraError("Debug buffer not large enougth to draw object"); NazaraError("Debug buffer not large enougth to draw object");
return; return;
} }
NzBufferMapper<NzVertexBuffer> inputMapper(subMesh->GetVertexBuffer(), nzBufferAccess_ReadOnly); NzBufferMapper<NzVertexBuffer> inputMapper(subMesh->GetVertexBuffer(), nzBufferAccess_ReadOnly);
NzBufferMapper<NzVertexBuffer> outputMapper(vertexBuffer, nzBufferAccess_DiscardAndWrite, 0, vertexCount); NzBufferMapper<NzVertexBuffer> outputMapper(s_vertexBuffer, nzBufferAccess_DiscardAndWrite, 0, vertexCount);
NzMeshVertex* inputVertex = reinterpret_cast<NzMeshVertex*>(inputMapper.GetPointer()); NzMeshVertex* inputVertex = reinterpret_cast<NzMeshVertex*>(inputMapper.GetPointer());
NzVertexStruct_XYZ* outputVertex = reinterpret_cast<NzVertexStruct_XYZ*>(outputMapper.GetPointer()); NzVertexStruct_XYZ* outputVertex = reinterpret_cast<NzVertexStruct_XYZ*>(outputMapper.GetPointer());
@ -488,45 +486,45 @@ void NzDebugDrawer::DrawTangents(const NzStaticMesh* subMesh)
if (vertexCount > 0) if (vertexCount > 0)
{ {
NzRenderer::SetRenderStates(renderStates); NzRenderer::SetRenderStates(s_renderStates);
NzRenderer::SetShaderProgram(program); NzRenderer::SetShaderProgram(s_program);
NzRenderer::SetVertexBuffer(vertexBuffer); NzRenderer::SetVertexBuffer(&s_vertexBuffer);
program->SendColor(colorLocation, primaryColor); s_program->SendColor(s_colorLocation, s_primaryColor);
NzRenderer::DrawPrimitives(nzPrimitiveMode_LineList, 0, vertexCount); NzRenderer::DrawPrimitives(nzPrimitiveMode_LineList, 0, vertexCount);
} }
} }
void NzDebugDrawer::EnableDepthBuffer(bool depthBuffer) void NzDebugDrawer::EnableDepthBuffer(bool depthBuffer)
{ {
renderStates.parameters[nzRendererParameter_DepthBuffer] = depthBuffer; s_renderStates.parameters[nzRendererParameter_DepthBuffer] = depthBuffer;
} }
float NzDebugDrawer::GetLineWidth() float NzDebugDrawer::GetLineWidth()
{ {
return renderStates.lineWidth; return s_renderStates.lineWidth;
} }
float NzDebugDrawer::GetPointSize() float NzDebugDrawer::GetPointSize()
{ {
return renderStates.pointSize; return s_renderStates.pointSize;
} }
NzColor NzDebugDrawer::GetPrimaryColor() NzColor NzDebugDrawer::GetPrimaryColor()
{ {
return primaryColor; return s_primaryColor;
} }
NzColor NzDebugDrawer::GetSecondaryColor() NzColor NzDebugDrawer::GetSecondaryColor()
{ {
return secondaryColor; return s_secondaryColor;
} }
bool NzDebugDrawer::Initialize() bool NzDebugDrawer::Initialize()
{ {
if (!initialized) if (!s_initialized)
{ {
// Program // s_program
{ {
NzShaderProgramManagerParams params; NzShaderProgramManagerParams params;
params.target = nzShaderTarget_Model; params.target = nzShaderTarget_Model;
@ -540,33 +538,36 @@ bool NzDebugDrawer::Initialize()
params.model.parallaxMapping = false; params.model.parallaxMapping = false;
params.model.specularMapping = false; params.model.specularMapping = false;
program = NzShaderProgramManager::Get(params); s_program = NzShaderProgramManager::Get(params);
if (!program) if (!s_program)
{ {
NazaraError("Failed to build debug program"); NazaraError("Failed to build debug s_program");
return false;
}
colorLocation = program->GetUniformLocation(nzShaderUniform_MaterialDiffuse);
}
// VertexBuffer (Nécessite la déclaration)
{
vertexBuffer = new NzVertexBuffer(NzVertexDeclaration::Get(nzVertexLayout_XYZ_Normal_UV_Tangent), 65365, nzBufferStorage_Hardware, nzBufferUsage_Dynamic);
if (!vertexBuffer->GetBuffer()->IsValid())
{
NazaraError("Failed to create buffer");
Uninitialize(); Uninitialize();
return false; return false;
} }
s_colorLocation = s_program->GetUniformLocation(nzShaderUniform_MaterialDiffuse);
} }
primaryColor = NzColor::Red; // s_vertexBuffer
renderStates.parameters[nzRendererParameter_DepthBuffer] = true; try
secondaryColor = NzColor::Green; {
s_vertexBuffer.Reset(NzVertexDeclaration::Get(nzVertexLayout_XYZ), 65365, nzBufferStorage_Hardware, nzBufferUsage_Dynamic);
}
catch (const std::exception& e)
{
NazaraError("Failed to create buffer: " + NzString(e.what()));
initialized = true; Uninitialize();
return false;
}
s_primaryColor = NzColor::Red;
s_renderStates.parameters[nzRendererParameter_DepthBuffer] = true;
s_secondaryColor = NzColor::Green;
s_initialized = true;
} }
return true; return true;
@ -574,42 +575,31 @@ bool NzDebugDrawer::Initialize()
bool NzDebugDrawer::IsDepthBufferEnabled() bool NzDebugDrawer::IsDepthBufferEnabled()
{ {
return depthBufferEnabled; return s_renderStates.parameters[nzRendererParameter_DepthBuffer];
} }
void NzDebugDrawer::SetLineWidth(float width) void NzDebugDrawer::SetLineWidth(float width)
{ {
renderStates.lineWidth = width; s_renderStates.lineWidth = width;
} }
void NzDebugDrawer::SetPointSize(float size) void NzDebugDrawer::SetPointSize(float size)
{ {
renderStates.pointSize = size; s_renderStates.pointSize = size;
} }
void NzDebugDrawer::SetPrimaryColor(const NzColor& color) void NzDebugDrawer::SetPrimaryColor(const NzColor& color)
{ {
primaryColor = color; s_primaryColor = color;
} }
void NzDebugDrawer::SetSecondaryColor(const NzColor& color) void NzDebugDrawer::SetSecondaryColor(const NzColor& color)
{ {
secondaryColor = color; s_secondaryColor = color;
} }
void NzDebugDrawer::Uninitialize() void NzDebugDrawer::Uninitialize()
{ {
if (vertexBuffer) s_vertexBuffer.Reset();
{ s_initialized = false;
delete vertexBuffer;
vertexBuffer = nullptr;
}
if (vertexDeclaration)
{
delete vertexDeclaration;
vertexDeclaration = nullptr;
}
initialized = false;
} }