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