Big skeletal animation update
Added MeshInfos demo Added MD5Mesh/MD5Anim loader support Added Node class Fixed ResourceParams not being exported Added support for skeletal animation (Animation/Mesh/Joint/SkeletalMesh/Skeleton) Meshes are now only stored with VertexStruct_XYZ_Normal_UV_Tangent type Moved Sequence declaration to Sequence.hpp -Animation: Renamed Create to Create[Keyframe|Skeletal] -AxisAlignedBox: Added Contains method Added GetCorner method Added GetCube method Added Transform method -Cube/Rect: Added GetPosition method Added GetSize method (Almost) Fixed ExtendTo method Fixed GetCenter method -File: Added GetDirectory static function Added GetPath method Renamed GetDirectoryPath method to GetDirectory -Math module: Fixed constructor/methods taking a non-const array GetNormal/Normalize methods now takes an optionnal integer pointer (returning length) Made all classes default constructor trivial Inverse, MakeIdentity, MakeZero, Normalize, Set methods now returns reference to object -Matrix4: Modified methods to avoid copies Removed COW (Too much overhead) Removed Concatenate[Affine] static function -Mesh: Renamed Create to Create[Keyframe|Skeletal|Static] Renamed Skin to Material -MeshParams: No longer takes declaration argument Renamed loadAnimations to animated Storage default to BufferStorage_Hardware if supported and BufferStorage_Software otherwise -OpenGL: Added glGetBooleanv function Added glIsEnabled function -Quaternion: Added ComputeW method Added Conjugate method -Renderer: Added IsEnabled static function Fixed GetLineWidth function not being static Removed SetVertexDeclaration -RenderWindow: Made CopyTo[Image|Texture] method constant -Resource Fixed RemoveResourceListener crash -ResourceLoader: Loaders are now used in a LIFO context -Stream: Renamed GetLine method to ReadLine -String: Fixed Simplified -Utility module Added configuration define for strict resource parsing -VertexBuffer Now takes a VertexDeclaration pointer -VertexDeclaration No longer throw an error when getting a non-existing element Former-commit-id: f7358c1231d6af48b799d2f24f077a001e16785b
This commit is contained in:
411
src/Nazara/Renderer/DebugDrawer.cpp
Normal file
411
src/Nazara/Renderer/DebugDrawer.cpp
Normal file
@@ -0,0 +1,411 @@
|
||||
// Copyright (C) 2012 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Renderer module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Renderer/OpenGL.hpp>
|
||||
#include <Nazara/Renderer/DebugDrawer.hpp>
|
||||
#include <Nazara/Renderer/Renderer.hpp>
|
||||
#include <Nazara/Renderer/Shader.hpp>
|
||||
#include <Nazara/Utility/AxisAlignedBox.hpp>
|
||||
#include <Nazara/Utility/Skeleton.hpp>
|
||||
#include <Nazara/Utility/VertexBuffer.hpp>
|
||||
#include <Nazara/Utility/VertexDeclaration.hpp>
|
||||
#include <Nazara/Utility/VertexStruct.hpp>
|
||||
#include <Nazara/Renderer/Debug.hpp>
|
||||
|
||||
///TODO: Améliorer
|
||||
|
||||
namespace
|
||||
{
|
||||
static NzColor primaryColor = NzColor::Red;
|
||||
static NzColor secondaryColor = NzColor::Green;
|
||||
static NzShader* shader = nullptr;
|
||||
static NzVertexBuffer* vertexBuffer = nullptr;
|
||||
static NzVertexDeclaration* vertexDeclaration = nullptr;
|
||||
static bool depthTest = true;
|
||||
static bool initialized = false;
|
||||
static float lineWidth = 2.f;
|
||||
static float pointSize = 3.f;
|
||||
static int colorLocation = -1;
|
||||
}
|
||||
|
||||
void NzDebugDrawer::Draw(const NzAxisAlignedBox& aabb)
|
||||
{
|
||||
if (!aabb.IsFinite())
|
||||
return;
|
||||
|
||||
Draw(aabb.GetCube());
|
||||
}
|
||||
|
||||
void NzDebugDrawer::Draw(const NzCubei& cube)
|
||||
{
|
||||
Draw(NzCubef(cube));
|
||||
}
|
||||
|
||||
void NzDebugDrawer::Draw(const NzCubef& cube)
|
||||
{
|
||||
if (!initialized)
|
||||
{
|
||||
NazaraError("Debug drawer is not initialized");
|
||||
return;
|
||||
}
|
||||
|
||||
NzVertexStruct_XYZ* vertex = reinterpret_cast<NzVertexStruct_XYZ*>(vertexBuffer->Map(nzBufferAccess_DiscardAndWrite, 0, 24));
|
||||
if (!vertex)
|
||||
{
|
||||
NazaraError("Failed to map buffer");
|
||||
return;
|
||||
}
|
||||
|
||||
NzVector3f max, min;
|
||||
max = cube.GetPosition() + cube.GetSize();
|
||||
min = cube.GetPosition();
|
||||
|
||||
vertex->position.Set(min.x, min.y, min.z);
|
||||
vertex++;
|
||||
vertex->position.Set(max.x, min.y, min.z);
|
||||
vertex++;
|
||||
|
||||
vertex->position.Set(min.x, min.y, min.z);
|
||||
vertex++;
|
||||
vertex->position.Set(min.x, max.y, min.z);
|
||||
vertex++;
|
||||
|
||||
vertex->position.Set(min.x, min.y, min.z);
|
||||
vertex++;
|
||||
vertex->position.Set(min.x, min.y, max.z);
|
||||
vertex++;
|
||||
|
||||
vertex->position.Set(max.x, max.y, max.z);
|
||||
vertex++;
|
||||
vertex->position.Set(min.x, max.y, max.z);
|
||||
vertex++;
|
||||
|
||||
vertex->position.Set(max.x, max.y, max.z);
|
||||
vertex++;
|
||||
vertex->position.Set(max.x, min.y, max.z);
|
||||
vertex++;
|
||||
|
||||
vertex->position.Set(max.x, max.y, max.z);
|
||||
vertex++;
|
||||
vertex->position.Set(max.x, max.y, min.z);
|
||||
vertex++;
|
||||
|
||||
vertex->position.Set(min.x, min.y, max.z);
|
||||
vertex++;
|
||||
vertex->position.Set(max.x, min.y, max.z);
|
||||
vertex++;
|
||||
|
||||
vertex->position.Set(min.x, min.y, max.z);
|
||||
vertex++;
|
||||
vertex->position.Set(min.x, max.y, max.z);
|
||||
vertex++;
|
||||
|
||||
vertex->position.Set(min.x, max.y, min.z);
|
||||
vertex++;
|
||||
vertex->position.Set(max.x, max.y, min.z);
|
||||
vertex++;
|
||||
|
||||
vertex->position.Set(min.x, max.y, min.z);
|
||||
vertex++;
|
||||
vertex->position.Set(min.x, max.y, max.z);
|
||||
vertex++;
|
||||
|
||||
vertex->position.Set(max.x, min.y, min.z);
|
||||
vertex++;
|
||||
vertex->position.Set(max.x, max.y, min.z);
|
||||
vertex++;
|
||||
|
||||
vertex->position.Set(max.x, min.y, min.z);
|
||||
vertex++;
|
||||
vertex->position.Set(max.x, min.y, max.z);
|
||||
vertex++;
|
||||
|
||||
if (!vertexBuffer->Unmap())
|
||||
NazaraWarning("Failed to unmap buffer");
|
||||
|
||||
NzShader* oldShader = NzRenderer::GetShader();
|
||||
|
||||
if (!NzRenderer::SetShader(shader))
|
||||
{
|
||||
NazaraError("Failed to set debug shader");
|
||||
return;
|
||||
}
|
||||
|
||||
bool depthTestActive = NzRenderer::IsEnabled(nzRendererParameter_DepthTest);
|
||||
if (depthTestActive != depthTest)
|
||||
NzRenderer::Enable(nzRendererParameter_DepthTest, depthTest);
|
||||
|
||||
NzRenderer::SetVertexBuffer(vertexBuffer);
|
||||
|
||||
shader->SendColor(colorLocation, primaryColor);
|
||||
|
||||
NzRenderer::DrawPrimitives(nzPrimitiveType_LineList, 0, 24);
|
||||
|
||||
if (depthTestActive != depthTest)
|
||||
NzRenderer::Enable(nzRendererParameter_DepthTest, depthTestActive);
|
||||
|
||||
if (!NzRenderer::SetShader(oldShader))
|
||||
NazaraWarning("Failed to reset shader");
|
||||
}
|
||||
|
||||
void NzDebugDrawer::Draw(const NzCubeui& cube)
|
||||
{
|
||||
Draw(NzCubef(cube));
|
||||
}
|
||||
|
||||
void NzDebugDrawer::Draw(const NzSkeleton* skeleton)
|
||||
{
|
||||
if (!initialized)
|
||||
{
|
||||
NazaraError("Debug drawer is not initialized");
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned int jointCount = skeleton->GetJointCount();
|
||||
if (vertexBuffer->GetVertexCount() < jointCount*2)
|
||||
{
|
||||
NazaraError("Debug buffer not length enougth to draw object");
|
||||
return;
|
||||
}
|
||||
|
||||
NzVertexStruct_XYZ* vertex = reinterpret_cast<NzVertexStruct_XYZ*>(vertexBuffer->Map(nzBufferAccess_DiscardAndWrite, 0, jointCount*2));
|
||||
if (!vertex)
|
||||
{
|
||||
NazaraError("Failed to map buffer");
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned int vertexCount = 0;
|
||||
for (unsigned int i = 0; i < jointCount; ++i)
|
||||
{
|
||||
const NzNode* joint = skeleton->GetJoint(i);
|
||||
const NzNode* parent = joint->GetParent();
|
||||
if (parent)
|
||||
{
|
||||
vertex->position = joint->GetDerivedTranslation();
|
||||
vertex++;
|
||||
|
||||
vertex->position = parent->GetDerivedTranslation();
|
||||
vertex++;
|
||||
|
||||
vertexCount += 2;
|
||||
}
|
||||
}
|
||||
|
||||
if (!vertexBuffer->Unmap())
|
||||
NazaraWarning("Failed to unmap buffer");
|
||||
|
||||
if (vertexCount > 0)
|
||||
{
|
||||
NzShader* oldShader = NzRenderer::GetShader();
|
||||
|
||||
if (!NzRenderer::SetShader(shader))
|
||||
{
|
||||
NazaraError("Failed to set debug shader");
|
||||
return;
|
||||
}
|
||||
|
||||
bool depthTestActive = NzRenderer::IsEnabled(nzRendererParameter_DepthTest);
|
||||
if (depthTestActive != depthTest)
|
||||
NzRenderer::Enable(nzRendererParameter_DepthTest, depthTest);
|
||||
|
||||
NzRenderer::SetVertexBuffer(vertexBuffer);
|
||||
|
||||
shader->SendColor(colorLocation, primaryColor);
|
||||
NzRenderer::DrawPrimitives(nzPrimitiveType_LineList, 0, vertexCount);
|
||||
|
||||
float oldPointSize = NzRenderer::GetPointSize();
|
||||
NzRenderer::SetPointSize(pointSize);
|
||||
|
||||
shader->SendColor(colorLocation, secondaryColor);
|
||||
NzRenderer::DrawPrimitives(nzPrimitiveType_PointList, 0, vertexCount);
|
||||
|
||||
NzRenderer::SetPointSize(oldPointSize);
|
||||
|
||||
if (depthTestActive != depthTest)
|
||||
NzRenderer::Enable(nzRendererParameter_DepthTest, depthTestActive);
|
||||
|
||||
if (!NzRenderer::SetShader(oldShader))
|
||||
NazaraWarning("Failed to reset shader");
|
||||
}
|
||||
}
|
||||
|
||||
bool NzDebugDrawer::Initialize()
|
||||
{
|
||||
if (!initialized)
|
||||
{
|
||||
// Shader
|
||||
{
|
||||
const char* fragmentSource110 =
|
||||
"#version 110\n"
|
||||
"uniform vec3 color;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_FragColor = vec4(color, 1.0);\n"
|
||||
"}\n";
|
||||
|
||||
const char* fragmentSource140 =
|
||||
"#version 140\n"
|
||||
"uniform vec3 color;\n"
|
||||
"out vec4 RenderTarget0;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" RenderTarget0 = vec4(color, 1.0);\n"
|
||||
"}\n";
|
||||
|
||||
const char* vertexSource110 =
|
||||
"#version 110\n"
|
||||
"attribute vec3 Position;\n"
|
||||
"uniform mat4 WorldViewProjMatrix;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_Position = WorldViewProjMatrix * vec4(Position, 1.0);\n"
|
||||
"}\n";
|
||||
|
||||
const char* vertexSource140 =
|
||||
"#version 140\n"
|
||||
"in vec3 Position;\n"
|
||||
"uniform mat4 WorldViewProjMatrix;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_Position = WorldViewProjMatrix * vec4(Position, 1.0);\n"
|
||||
"}\n";
|
||||
|
||||
bool useGLSL140 = (NzOpenGL::GetVersion() >= 310);
|
||||
|
||||
shader = new NzShader(nzShaderLanguage_GLSL);
|
||||
if (!shader->Load(nzShaderType_Fragment, (useGLSL140) ? fragmentSource140 : fragmentSource110))
|
||||
{
|
||||
NazaraError("Failed to load fragment shader");
|
||||
Uninitialize();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!shader->Load(nzShaderType_Vertex, (useGLSL140) ? vertexSource140 : vertexSource110))
|
||||
{
|
||||
NazaraError("Failed to load vertex shader");
|
||||
Uninitialize();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!shader->Compile())
|
||||
{
|
||||
NazaraError("Failed to compile shader");
|
||||
Uninitialize();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
colorLocation = shader->GetUniformLocation("color");
|
||||
}
|
||||
|
||||
// VertexDeclaration
|
||||
{
|
||||
NzVertexElement element;
|
||||
element.offset = 0;
|
||||
element.type = nzElementType_Float3;
|
||||
element.usage = nzElementUsage_Position;
|
||||
|
||||
vertexDeclaration = new NzVertexDeclaration;
|
||||
if (!vertexDeclaration->Create(&element, 1))
|
||||
{
|
||||
NazaraError("Failed to create declaration");
|
||||
Uninitialize();
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// VertexBuffer (Nécessite la déclaration)
|
||||
{
|
||||
vertexBuffer = new NzVertexBuffer(vertexDeclaration, 256, nzBufferStorage_Hardware, nzBufferUsage_Dynamic);
|
||||
if (!vertexBuffer->GetBuffer()->IsValid())
|
||||
{
|
||||
NazaraError("Failed to create buffer");
|
||||
Uninitialize();
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NzDebugDrawer::GetDepthTest()
|
||||
{
|
||||
return depthTest;
|
||||
}
|
||||
|
||||
float NzDebugDrawer::GetLineWidth()
|
||||
{
|
||||
return lineWidth;
|
||||
}
|
||||
|
||||
float NzDebugDrawer::GetPointSize()
|
||||
{
|
||||
return pointSize;
|
||||
}
|
||||
|
||||
NzColor NzDebugDrawer::GetPrimaryColor()
|
||||
{
|
||||
return primaryColor;
|
||||
}
|
||||
|
||||
NzColor NzDebugDrawer::GetSecondaryColor()
|
||||
{
|
||||
return secondaryColor;
|
||||
}
|
||||
|
||||
void NzDebugDrawer::SetDepthTest(bool shouldTest)
|
||||
{
|
||||
depthTest = shouldTest;
|
||||
}
|
||||
|
||||
void NzDebugDrawer::SetLineWidth(float width)
|
||||
{
|
||||
lineWidth = width;
|
||||
}
|
||||
|
||||
void NzDebugDrawer::SetPointSize(float size)
|
||||
{
|
||||
pointSize = size;
|
||||
}
|
||||
|
||||
void NzDebugDrawer::SetPrimaryColor(const NzColor& color)
|
||||
{
|
||||
primaryColor = color;
|
||||
}
|
||||
|
||||
void NzDebugDrawer::SetSecondaryColor(const NzColor& color)
|
||||
{
|
||||
secondaryColor = color;
|
||||
}
|
||||
|
||||
void NzDebugDrawer::Uninitialize()
|
||||
{
|
||||
if (shader)
|
||||
{
|
||||
delete shader;
|
||||
shader = nullptr;
|
||||
}
|
||||
|
||||
if (vertexBuffer)
|
||||
{
|
||||
delete vertexBuffer;
|
||||
vertexBuffer = nullptr;
|
||||
}
|
||||
|
||||
if (vertexDeclaration)
|
||||
{
|
||||
delete vertexDeclaration;
|
||||
vertexDeclaration = nullptr;
|
||||
}
|
||||
|
||||
initialized = false;
|
||||
}
|
||||
Reference in New Issue
Block a user