Added proper debug line renderer!

This commit is contained in:
Antoine Pilote
2024-08-09 23:36:02 -04:00
parent 275aad82b9
commit f3b9b328b1
12 changed files with 234 additions and 7 deletions

View File

@@ -178,8 +178,7 @@ void GizmoDrawer::DrawAxis(Ref<Scene> scene, bool occluded)
void GizmoDrawer::DrawNavMesh(Ref<Scene> scene, bool occluded)
{
auto cam = Engine::GetCurrentScene()->m_EditorCamera;
auto& cam = Engine::GetCurrentScene()->m_EditorCamera;
auto navVolumesView = scene->m_Registry.view<TransformComponent, NavMeshVolumeComponent>();
for (auto e : navVolumesView)
{

View File

@@ -351,8 +351,9 @@ namespace Nuake
RenderCommand::DrawLines(0, 2);
}
void Renderer::DrawDebugLine(Vector3 start, Vector3 end, Vector3 color)
void Renderer::DrawLine(Vector3 start, Vector3 end, Vector3 color)
{
//m_DebugShader->Bind();
//m_DebugShader->SetUniform4f("u_Color", color.r, color.g, color.b, color.a);
}

View File

@@ -75,7 +75,7 @@ namespace Nuake
// Debug
static void DrawLine(Vector3 start, Vector3 end, Color color, Matrix4 transform = Matrix4());
static void DrawDebugLine(Vector3 start, Vector3 end, Vector3 color);
static void DrawLine(Vector3 start, Vector3 end, Vector3 color);
static void DrawCube(TransformComponent transform, glm::vec4 color);
static void DrawSphere(TransformComponent transform, glm::vec4 color);
static void DrawQuad(Matrix4 transform = Matrix4());

View File

@@ -43,12 +43,35 @@ namespace Nuake
mOutlineBuffer = CreateScope<FrameBuffer>(false, defaultResolution);
mOutlineBuffer->SetTexture(CreateRef<Texture>(defaultResolution, GL_RGB), GL_COLOR_ATTACHMENT0);
// Generate debug meshes
std::vector<Vertex> lineVertices
{
{ Vector3(0, 0, 0), Vector2(0, 0), Vector3(0, 0, 0) },
{ Vector3(1, 1, 1), Vector2(0, 0), Vector3(0, 0, 0) }
};
std::vector<uint32_t> lineIndices
{
0, 1
};
mLineMesh = CreateRef<Mesh>();
mLineMesh->AddSurface(lineVertices, lineIndices);
}
void SceneRenderer::Cleanup()
{
}
void SceneRenderer::Update(const Timestep time)
{
for (auto& line : mDebugLines)
{
line.Life -= time;
}
}
void SceneRenderer::BeginRenderScene(const Matrix4& projection, const Matrix4& view, const Vector3& camPos)
{
mProjection = projection;
@@ -84,6 +107,8 @@ namespace Nuake
mShadingBuffer->QueueResize(framebuffer.GetSize());
ShadingPass(scene);
DebugRendererPass(scene);
Ref<Texture> finalOutput = mShadingBuffer->GetTexture();
if (scene.GetEnvironment()->BloomEnabled)
{
@@ -342,6 +367,19 @@ namespace Nuake
RenderCommand::Enable(RendererEnum::DEPTH_TEST);
Renderer::EndDraw();
}
void SceneRenderer::DrawDebugLine(const Vector3& start, const Vector3& end, const Color& color, float life)
{
DebugLine debugLine = {
.Start = start,
.End = end,
.LineColor = color,
.Life = life,
.Width = 2.0f
};
mDebugLines.push_back(debugLine);
}
void SceneRenderer::ShadowPass(Scene& scene)
{
@@ -622,7 +660,7 @@ namespace Nuake
}
// Reset material on quadmesh
//Renderer::QuadMesh->SetMaterial(previousMaterial);
// Renderer::QuadMesh->SetMaterial(previousMaterial);
// Skinned mesh at the end because we switch shader
gBufferSkinnedMeshShader->Bind();
@@ -699,7 +737,6 @@ namespace Nuake
LightComponent light;
float distance;
};
std::vector<LightDistance> lightDistances;
auto view = scene.m_Registry.view<TransformComponent, LightComponent, ParentComponent>();
@@ -758,6 +795,46 @@ namespace Nuake
void SceneRenderer::PostProcessPass(const Scene& scene)
{
}
void SceneRenderer::DebugRendererPass(Scene& scene)
{
mShadingBuffer->Bind();
{
// Lines
mLineMesh->Bind();
Shader* shader = ShaderManager::GetShader("Resources/Shaders/debugLine.shader");
shader->Bind();
shader->SetUniformMat4f("u_Projection", mProjection);
shader->SetUniformMat4f("u_View", mView);
for (auto& l : mDebugLines)
{
shader->SetUniformVec4("u_Color", l.LineColor);
shader->SetUniformVec3("u_StartPos", l.Start);
shader->SetUniformVec3("u_EndPos", l.End);
glLineWidth(l.Width);
RenderCommand::DrawLines(0, 2);
}
std::erase_if(mDebugLines, [](const DebugLine& line)
{
return line.Life <= 0.0f;
});
shader->Unbind();
// Cubes
// Spheres
// Quads
}
mShadingBuffer->Unbind();
}
void SceneRenderer::SetSkeletonBoneTransformRecursive(Scene& scene, SkeletonNode& skeletonNode, Shader* shader)

View File

@@ -12,10 +12,37 @@ namespace Nuake
{
class SceneRenderer
{
struct DebugSphere
{
Vector3 Position;
float Radius;
Color SphereColor;
float Life;
};
struct DebugQuad
{
Vector3 Position;
Vector3 Size;
Color SphereColor;
float Life;
};
struct DebugLine
{
Vector3 Start;
Vector3 End;
Color LineColor;
float Life;
float Width;
};
public:
void Init();
void Cleanup();
void Update(const Timestep time);
void BeginRenderScene(const Matrix4& projection, const Matrix4& view, const Vector3& camPos);
void RenderScene(Scene& scene, FrameBuffer& framebuffer);
@@ -24,12 +51,13 @@ namespace Nuake
return *mGBuffer;
}
void DrawDebugLine(const Vector3& start, const Vector3& end, const Color& color = Color(1, 0, 0, 1), float life = 0.0f);
uint32_t mOutlineEntityID = 0;
private:
Matrix4 mProjection, mView;
Vector3 mCamPos;
Scope<FrameBuffer> mGBuffer;
Scope<FrameBuffer> mShadingBuffer;
Scope<FrameBuffer> mToneMapBuffer;
@@ -38,10 +66,15 @@ namespace Nuake
Scope<FrameBuffer> mDOFBuffer;
Scope<FrameBuffer> mOutlineBuffer;
Ref<Mesh> mLineMesh;
std::vector<DebugLine> mDebugLines;
void ShadowPass(Scene& scene);
void GBufferPass(Scene& scene);
void ShadingPass(Scene& scene);
void PostProcessPass(const Scene& scene);
void DebugRendererPass(Scene& scene);
void SetSkeletonBoneTransformRecursive(Scene& scene, SkeletonNode& skeletonNode, Shader* shader);
};

View File

@@ -41,6 +41,7 @@ namespace Nuake
LoadEmbeddedShader(Resources_Shaders_ui_shader);
LoadEmbeddedShader(Resources_Shaders_vignette_shader);
LoadEmbeddedShader(Resources_Shaders_volumetric_shader);
LoadEmbeddedShader(Resources_Shaders_debugLine_shader);
}
Shader* ShaderManager::GetShader(const std::string& path)

View File

@@ -268523,6 +268523,55 @@ const std::string Resources_Shaders_copy_shader_path = R"(Resources/Shaders/copy
};
unsigned int Resources_Shaders_copy_shader_len = 403;
// Data for file: Resources_Shaders_debugLine_shader_path
const std::string Resources_Shaders_debugLine_shader_path = R"(Resources/Shaders/debugLine.shader)";
unsigned char Resources_Shaders_debugLine_shader[] = {
0x23, 0x73, 0x68, 0x61, 0x64, 0x65, 0x72, 0x20, 0x76, 0x65, 0x72, 0x74,
0x65, 0x78, 0x0d, 0x0a, 0x23, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
0x20, 0x34, 0x34, 0x30, 0x20, 0x63, 0x6f, 0x72, 0x65, 0x0d, 0x0a, 0x0d,
0x0a, 0x6c, 0x61, 0x79, 0x6f, 0x75, 0x74, 0x28, 0x6c, 0x6f, 0x63, 0x61,
0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x30, 0x29, 0x20, 0x69, 0x6e,
0x20, 0x76, 0x65, 0x63, 0x33, 0x20, 0x56, 0x65, 0x72, 0x74, 0x65, 0x78,
0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0d, 0x0a, 0x0d,
0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x76, 0x65, 0x63,
0x33, 0x20, 0x75, 0x5f, 0x53, 0x74, 0x61, 0x72, 0x74, 0x50, 0x6f, 0x73,
0x3b, 0x0d, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x76,
0x65, 0x63, 0x33, 0x20, 0x75, 0x5f, 0x45, 0x6e, 0x64, 0x50, 0x6f, 0x73,
0x3b, 0x0d, 0x0a, 0x0d, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d,
0x20, 0x6d, 0x61, 0x74, 0x34, 0x20, 0x75, 0x5f, 0x50, 0x72, 0x6f, 0x6a,
0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0d, 0x0a, 0x75, 0x6e, 0x69,
0x66, 0x6f, 0x72, 0x6d, 0x20, 0x6d, 0x61, 0x74, 0x34, 0x20, 0x75, 0x5f,
0x56, 0x69, 0x65, 0x77, 0x3b, 0x0d, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f,
0x72, 0x6d, 0x20, 0x6d, 0x61, 0x74, 0x34, 0x20, 0x75, 0x5f, 0x4d, 0x6f,
0x64, 0x65, 0x6c, 0x3b, 0x0d, 0x0a, 0x0d, 0x0a, 0x76, 0x6f, 0x69, 0x64,
0x20, 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x29, 0x0d, 0x0a, 0x7b, 0x0d, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x76, 0x65, 0x63, 0x33, 0x20, 0x70, 0x6f, 0x73,
0x20, 0x3d, 0x20, 0x6d, 0x69, 0x78, 0x28, 0x75, 0x5f, 0x53, 0x74, 0x61,
0x72, 0x74, 0x50, 0x6f, 0x73, 0x2c, 0x20, 0x75, 0x5f, 0x45, 0x6e, 0x64,
0x50, 0x6f, 0x73, 0x2c, 0x20, 0x56, 0x65, 0x72, 0x74, 0x65, 0x78, 0x50,
0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x78, 0x29, 0x3b, 0x0d,
0x0a, 0x20, 0x20, 0x20, 0x20, 0x67, 0x6c, 0x5f, 0x50, 0x6f, 0x73, 0x69,
0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x75, 0x5f, 0x50, 0x72, 0x6f,
0x6a, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x2a, 0x20, 0x75, 0x5f,
0x56, 0x69, 0x65, 0x77, 0x20, 0x2a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x28,
0x70, 0x6f, 0x73, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0d,
0x0a, 0x7d, 0x0d, 0x0a, 0x0d, 0x0a, 0x23, 0x73, 0x68, 0x61, 0x64, 0x65,
0x72, 0x20, 0x66, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x0d, 0x0a,
0x23, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x34, 0x34, 0x30,
0x20, 0x63, 0x6f, 0x72, 0x65, 0x0d, 0x0a, 0x0d, 0x0a, 0x6c, 0x61, 0x79,
0x6f, 0x75, 0x74, 0x28, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e,
0x20, 0x3d, 0x20, 0x30, 0x29, 0x20, 0x6f, 0x75, 0x74, 0x20, 0x76, 0x65,
0x63, 0x34, 0x20, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72,
0x3b, 0x0d, 0x0a, 0x0d, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d,
0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x75, 0x5f, 0x43, 0x6f, 0x6c, 0x6f,
0x72, 0x3b, 0x0d, 0x0a, 0x0d, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x6d,
0x61, 0x69, 0x6e, 0x28, 0x29, 0x0d, 0x0a, 0x7b, 0x0d, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20,
0x3d, 0x20, 0x75, 0x5f, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0d, 0x0a,
0x7d
};
unsigned int Resources_Shaders_debugLine_shader_len = 505;
// Data for file: Resources_Shaders_deferred_shader_path
const std::string Resources_Shaders_deferred_shader_path = R"(Resources/Shaders/deferred.shader)";
unsigned char Resources_Shaders_deferred_shader[] = {

View File

@@ -150,6 +150,9 @@ extern unsigned char Resources_Shaders_combine_shader[];
extern const std::string Resources_Shaders_copy_shader_path;
extern unsigned int Resources_Shaders_copy_shader_len;
extern unsigned char Resources_Shaders_copy_shader[];
extern const std::string Resources_Shaders_debugLine_shader_path;
extern unsigned int Resources_Shaders_debugLine_shader_len;
extern unsigned char Resources_Shaders_debugLine_shader[];
extern const std::string Resources_Shaders_deferred_shader_path;
extern unsigned int Resources_Shaders_deferred_shader_len;
extern unsigned char Resources_Shaders_deferred_shader[];

View File

@@ -235,6 +235,8 @@ namespace Nuake
{
system->Update(ts);
}
m_SceneRenderer->Update(ts);
}
void Scene::FixedUpdate(Timestep ts)

View File

@@ -1,5 +1,8 @@
#include "EngineNetAPI.h"
#include <Coral/String.hpp>
#include <src/Core/Maths.h>
#include <Engine.h>
#include "src/Rendering/SceneRenderer.h"
namespace Nuake {
@@ -8,9 +11,19 @@ namespace Nuake {
Logger::Log(string, ".net", VERBOSE);
}
void DrawLine(float startX, float startY, float startZ, float endX, float endY, float endZ, float r, float g, float b, float a, float life)
{
const Vector3 start = { startX, startY, startZ };
const Vector3 end = { endX, endY, endZ };
const Vector4 color = { r, g, b, a };
Engine::GetCurrentScene()->m_SceneRenderer->DrawDebugLine(start, end, color, life);
}
void EngineNetAPI::RegisterMethods()
{
RegisterMethod("Engine.LoggerLogIcall", (void*)(&Log));
RegisterMethod("Debug.DrawLineIcall", &DrawLine);
}
}

View File

@@ -29,5 +29,25 @@ namespace Nuake.Net
}
}
public class Debug
{
internal static unsafe delegate*</* start */ float, float, float,
/* end */ float, float, float,
/* color */ float, float, float, float,
/* life */ float,
void> DrawLineIcall;
public Debug() { }
public static void DrawLine(Vector3 start, Vector3 end, Vector4 color, float life = 0.0f)
{
unsafe
{
DrawLineIcall(start.X, start.Y, start.Z, end.X, end.Y, end.Z, color.X, color.Y, color.Z, color.W, life);
}
}
}
}

View File

@@ -0,0 +1,29 @@
#shader vertex
#version 440 core
layout(location = 0) in vec3 VertexPosition;
uniform vec3 u_StartPos;
uniform vec3 u_EndPos;
uniform mat4 u_Projection;
uniform mat4 u_View;
uniform mat4 u_Model;
void main()
{
vec3 pos = mix(u_StartPos, u_EndPos, VertexPosition.x);
gl_Position = u_Projection * u_View * vec4(pos, 1.0f);
}
#shader fragment
#version 440 core
layout(location = 0) out vec4 FragColor;
uniform vec4 u_Color;
void main()
{
FragColor = u_Color;
}