Skip to content

Commit

Permalink
[renderer] removed duration from line drawing since it wasn't used an…
Browse files Browse the repository at this point in the history
…d fixed an issue where the line vertex buffer wasn't zeroed out
  • Loading branch information
PanosK92 committed Dec 4, 2024
1 parent eddb712 commit 4616738
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 52 deletions.
4 changes: 2 additions & 2 deletions runtime/Game/Car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,11 @@ namespace Spartan

// draw fz force
Math::Vector3 fz_end = start + Math::Vector3(parameters.pacejka_fz[wheel_index] * wheel_forward_dir) * 0.2f;
Renderer::DrawDirectionalArrow(start, fz_end, arrow_size, Color(0.0f, 1.0f, 0.0f, 1.0f), 0.0, false);
Renderer::DrawDirectionalArrow(start, fz_end, arrow_size, Color(0.0f, 1.0f, 0.0f, 1.0f), false);

// draw fx force
Math::Vector3 fx_end = start + Math::Vector3(parameters.pacejka_fx[wheel_index] * wheel_right_dir) * 0.2f;
Renderer::DrawDirectionalArrow(start, fx_end, arrow_size, Color(1.0f, 0.0f, 0.0f, 1.0f), 0.0, false);
Renderer::DrawDirectionalArrow(start, fx_end, arrow_size, Color(1.0f, 0.0f, 0.0f, 1.0f), false);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion runtime/Physics/PhysicsDebugDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ namespace Spartan
reinterpret_cast<const Math::Vector3&>(to),
reinterpret_cast<const Color&>(color_from),
reinterpret_cast<const Color&>(color_to),
0.0f,
true
);
}
Expand Down
1 change: 0 additions & 1 deletion runtime/Rendering/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ namespace Spartan
// line rendering
shared_ptr<RHI_Buffer> Renderer::m_vertex_buffer_lines;
vector<RHI_Vertex_PosCol> Renderer::m_line_vertices;
vector<float> Renderer::m_lines_duration;
uint32_t Renderer::m_lines_index_depth_off;
uint32_t Renderer::m_lines_index_depth_on;

Expand Down
15 changes: 7 additions & 8 deletions runtime/Rendering/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ namespace Spartan
static void Tick();

// primitive rendering (useful for debugging)
static void DrawLine(const Math::Vector3& from, const Math::Vector3& to, const Color& color_from = Color::standard_renderer_lines, const Color& color_to = Color::standard_renderer_lines, const float duration = 0.0f, const bool depth = true);
static void DrawTriangle(const Math::Vector3& v0, const Math::Vector3& v1, const Math::Vector3& v2, const Color& color = Color::standard_renderer_lines, const float duration = 0.0f, const bool depth = true);
static void DrawBox(const Math::BoundingBox& box, const Color& color = Color::standard_renderer_lines, const float duration = 0.0f, const bool depth = true);
static void DrawCircle(const Math::Vector3& center, const Math::Vector3& axis, const float radius, uint32_t segment_count, const Color& color = Color::standard_renderer_lines, const float duration = 0.0f, const bool depth = true);
static void DrawSphere(const Math::Vector3& center, float radius, uint32_t segment_count, const Color& color = Color::standard_renderer_lines, const float duration = 0.0f, const bool depth = true);
static void DrawDirectionalArrow(const Math::Vector3& start, const Math::Vector3& end, float arrow_size, const Color& color = Color::standard_renderer_lines, const float duration = 0.0f, const bool depth = true);
static void DrawPlane(const Math::Plane& plane, const Color& color = Color::standard_renderer_lines, const float duration = 0.0f, const bool depth = true);
static void DrawLine(const Math::Vector3& from, const Math::Vector3& to, const Color& color_from = Color::standard_renderer_lines, const Color& color_to = Color::standard_renderer_lines, const bool depth = true);
static void DrawTriangle(const Math::Vector3& v0, const Math::Vector3& v1, const Math::Vector3& v2, const Color& color = Color::standard_renderer_lines, const bool depth = true);
static void DrawBox(const Math::BoundingBox& box, const Color& color = Color::standard_renderer_lines, const bool depth = true);
static void DrawCircle(const Math::Vector3& center, const Math::Vector3& axis, const float radius, uint32_t segment_count, const Color& color = Color::standard_renderer_lines, const bool depth = true);
static void DrawSphere(const Math::Vector3& center, float radius, uint32_t segment_count, const Color& color = Color::standard_renderer_lines, const bool depth = true);
static void DrawDirectionalArrow(const Math::Vector3& start, const Math::Vector3& end, float arrow_size, const Color& color = Color::standard_renderer_lines, const bool depth = true);
static void DrawPlane(const Math::Plane& plane, const Color& color = Color::standard_renderer_lines, const bool depth = true);
static void DrawString(const std::string& text, const Math::Vector2& position_screen_percentage);

// options
Expand Down Expand Up @@ -198,7 +198,6 @@ namespace Spartan
static Pcb_Pass m_pcb_pass_cpu;
static std::shared_ptr<RHI_Buffer> m_vertex_buffer_lines;
static std::vector<RHI_Vertex_PosCol> m_line_vertices;
static std::vector<float> m_lines_duration;
static uint32_t m_lines_index_depth_off;
static uint32_t m_lines_index_depth_on;
static uint32_t m_resource_index;
Expand Down
11 changes: 8 additions & 3 deletions runtime/Rendering/Renderer_Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2119,7 +2119,7 @@ namespace Spartan
static RHI_PipelineState pso;
pso.shaders[RHI_Shader_Type::Vertex] = shader_v;
pso.shaders[RHI_Shader_Type::Pixel] = shader_p;
pso.rasterizer_state = GetRasterizerState(Renderer_RasterizerState::Wireframe);
pso.rasterizer_state = GetRasterizerState(Renderer_RasterizerState::Solid);
pso.render_target_color_textures[0] = tex_out;
pso.clear_color[0] = rhi_color_load;
pso.render_target_depth_texture = GetRenderTarget(Renderer_RenderTarget::gbuffer_depth_output);
Expand All @@ -2145,9 +2145,14 @@ namespace Spartan

if (vertex_count != 0)
{
// update vertex buffer
RHI_Vertex_PosCol* buffer = static_cast<RHI_Vertex_PosCol*>(m_vertex_buffer_lines->GetMappedData());
copy(m_line_vertices.begin(), m_line_vertices.end(), buffer);

// update vertex buffer
{
// zero out the entire buffer first - this is because the buffer grows but doesn't shrink back (so it can hold previous data)
memset(buffer, 0, m_vertex_buffer_lines->GetObjectSize());
copy(m_line_vertices.begin(), m_line_vertices.end(), buffer);
}

// depth off
if (draw_lines_depth_off)
Expand Down
68 changes: 31 additions & 37 deletions runtime/Rendering/Renderer_Primitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ namespace Spartan
namespace
{
vector<RHI_Vertex_PosCol> m_line_vertices;
vector<float> m_lines_duration;
uint32_t m_lines_index_depth_off = 0;
uint32_t m_lines_index_depth_on = 0;
}

void Renderer::DrawLine(const Vector3& from, const Vector3& to, const Color& color_from, const Color& color_to, const float duration /*= 0.0f*/, const bool depth /*= true*/)
void Renderer::DrawLine(const Vector3& from, const Vector3& to, const Color& color_from, const Color& color_to, const bool depth /*= true*/)
{
// get vertex index
uint32_t& index = depth ? m_lines_index_depth_on : m_lines_index_depth_off;
Expand All @@ -64,9 +63,6 @@ namespace Spartan

m_line_vertices.reserve(new_vertex_count);
m_line_vertices.resize(new_vertex_count);

m_lines_duration.reserve(new_vertex_count);
m_lines_duration.resize(new_vertex_count);
}

// write lines
Expand All @@ -80,7 +76,6 @@ namespace Spartan
line_start.col[1] = color_from.g;
line_start.col[2] = color_from.b;
line_start.col[3] = 1.0f;
m_lines_duration[index] = duration;

index++;
RHI_Vertex_PosCol& line_end = m_line_vertices[index];
Expand All @@ -91,37 +86,36 @@ namespace Spartan
line_end.col[1] = color_to.g;
line_end.col[2] = color_to.b;
line_end.col[3] = 1.0f;
m_lines_duration[index] = duration;
}
}

void Renderer::DrawTriangle(const Vector3& v0, const Vector3& v1, const Vector3& v2, const Color& color /*= DEBUG_COLOR*/, const float duration /*= 0.0f*/, bool depth /*= true*/)
void Renderer::DrawTriangle(const Vector3& v0, const Vector3& v1, const Vector3& v2, const Color& color /*= DEBUG_COLOR*/, bool depth /*= true*/)
{
DrawLine(v0, v1, color, color, duration, depth);
DrawLine(v1, v2, color, color, duration, depth);
DrawLine(v2, v0, color, color, duration, depth);
DrawLine(v0, v1, color, color, depth);
DrawLine(v1, v2, color, color, depth);
DrawLine(v2, v0, color, color, depth);
}

void Renderer::DrawBox(const BoundingBox& box, const Color& color, const float duration /*= 0.0f*/, const bool depth /*= true*/)
void Renderer::DrawBox(const BoundingBox& box, const Color& color, const bool depth /*= true*/)
{
const Vector3& min = box.GetMin();
const Vector3& max = box.GetMax();

DrawLine(Vector3(min.x, min.y, min.z), Vector3(max.x, min.y, min.z), color, color, duration, depth);
DrawLine(Vector3(max.x, min.y, min.z), Vector3(max.x, max.y, min.z), color, color, duration, depth);
DrawLine(Vector3(max.x, max.y, min.z), Vector3(min.x, max.y, min.z), color, color, duration, depth);
DrawLine(Vector3(min.x, max.y, min.z), Vector3(min.x, min.y, min.z), color, color, duration, depth);
DrawLine(Vector3(min.x, min.y, min.z), Vector3(min.x, min.y, max.z), color, color, duration, depth);
DrawLine(Vector3(max.x, min.y, min.z), Vector3(max.x, min.y, max.z), color, color, duration, depth);
DrawLine(Vector3(max.x, max.y, min.z), Vector3(max.x, max.y, max.z), color, color, duration, depth);
DrawLine(Vector3(min.x, max.y, min.z), Vector3(min.x, max.y, max.z), color, color, duration, depth);
DrawLine(Vector3(min.x, min.y, max.z), Vector3(max.x, min.y, max.z), color, color, duration, depth);
DrawLine(Vector3(max.x, min.y, max.z), Vector3(max.x, max.y, max.z), color, color, duration, depth);
DrawLine(Vector3(max.x, max.y, max.z), Vector3(min.x, max.y, max.z), color, color, duration, depth);
DrawLine(Vector3(min.x, max.y, max.z), Vector3(min.x, min.y, max.z), color, color, duration, depth);
DrawLine(Vector3(min.x, min.y, min.z), Vector3(max.x, min.y, min.z), color, color, depth);
DrawLine(Vector3(max.x, min.y, min.z), Vector3(max.x, max.y, min.z), color, color, depth);
DrawLine(Vector3(max.x, max.y, min.z), Vector3(min.x, max.y, min.z), color, color, depth);
DrawLine(Vector3(min.x, max.y, min.z), Vector3(min.x, min.y, min.z), color, color, depth);
DrawLine(Vector3(min.x, min.y, min.z), Vector3(min.x, min.y, max.z), color, color, depth);
DrawLine(Vector3(max.x, min.y, min.z), Vector3(max.x, min.y, max.z), color, color, depth);
DrawLine(Vector3(max.x, max.y, min.z), Vector3(max.x, max.y, max.z), color, color, depth);
DrawLine(Vector3(min.x, max.y, min.z), Vector3(min.x, max.y, max.z), color, color, depth);
DrawLine(Vector3(min.x, min.y, max.z), Vector3(max.x, min.y, max.z), color, color, depth);
DrawLine(Vector3(max.x, min.y, max.z), Vector3(max.x, max.y, max.z), color, color, depth);
DrawLine(Vector3(max.x, max.y, max.z), Vector3(min.x, max.y, max.z), color, color, depth);
DrawLine(Vector3(min.x, max.y, max.z), Vector3(min.x, min.y, max.z), color, color, depth);
}

void Renderer::DrawCircle(const Vector3& center, const Vector3& axis, const float radius, uint32_t segment_count, const Color& color /*= DEBUG_COLOR*/, const float duration /*= 0.0f*/, const bool depth /*= true*/)
void Renderer::DrawCircle(const Vector3& center, const Vector3& axis, const float radius, uint32_t segment_count, const Color& color /*= DEBUG_COLOR*/, const bool depth /*= true*/)
{
if (radius <= 0.0f)
return;
Expand Down Expand Up @@ -155,11 +149,11 @@ namespace Spartan
// Draw
for (uint32_t i = 0; i <= segment_count - 1; i++)
{
DrawLine(points[i], points[i + 1], color, color, duration, depth);
DrawLine(points[i], points[i + 1], color, color, depth);
}
}

void Renderer::DrawSphere(const Vector3& center, float radius, uint32_t segment_count, const Color& color /*= DEBUG_COLOR*/, const float duration /*= 0.0f*/, const bool depth /*= true*/)
void Renderer::DrawSphere(const Vector3& center, float radius, uint32_t segment_count, const Color& color /*= DEBUG_COLOR*/, const bool depth /*= true*/)
{
// Need at least 4 segments
segment_count = Helper::Max<uint32_t>(segment_count, 4);
Expand Down Expand Up @@ -191,8 +185,8 @@ namespace Spartan
Vertex2 = Vector3((CosX * SinY1), (SinX * SinY1), CosY1) * radius + center;
Vertex4 = Vector3((CosX * SinY2), (SinX * SinY2), CosY2) * radius + center;

DrawLine(Vertex1, Vertex2, color, color, duration, depth);
DrawLine(Vertex1, Vertex3, color, color, duration, depth);
DrawLine(Vertex1, Vertex2, color, color, depth);
DrawLine(Vertex1, Vertex3, color, color, depth);

Vertex1 = Vertex2;
Vertex3 = Vertex4;
Expand All @@ -204,11 +198,11 @@ namespace Spartan
}
}

void Renderer::DrawDirectionalArrow(const Vector3& start, const Vector3& end, float arrow_size, const Color& color /*= DEBUG_COLOR*/, const float duration /*= 0.0f*/, const bool depth /*= true*/)
void Renderer::DrawDirectionalArrow(const Vector3& start, const Vector3& end, float arrow_size, const Color& color /*= DEBUG_COLOR*/, const bool depth /*= true*/)
{
arrow_size = Helper::Max<float>(0.1f, arrow_size);

DrawLine(start, end, color, color, duration, depth);
DrawLine(start, end, color, color, depth);

Vector3 Dir = (end - start);
Dir.Normalize();
Expand All @@ -227,21 +221,21 @@ namespace Spartan
// since dir is x direction, my arrow will be pointing +y, -x and -y, -x
float arrow_sqrt = Helper::Sqrt(arrow_size);
Vector3 arrow_pos;
DrawLine(end, end + TM * Vector3(-arrow_sqrt, arrow_sqrt, 0), color, color, duration, depth);
DrawLine(end, end + TM * Vector3(-arrow_sqrt, -arrow_sqrt, 0), color, color, duration, depth);
DrawLine(end, end + TM * Vector3(-arrow_sqrt, arrow_sqrt, 0), color, color, depth);
DrawLine(end, end + TM * Vector3(-arrow_sqrt, -arrow_sqrt, 0), color, color, depth);
}

void Renderer::DrawPlane(const Math::Plane& plane, const Color& color /*= DEBUG_COLOR*/, const float duration /*= 0.0f*/, const bool depth /*= true*/)
void Renderer::DrawPlane(const Math::Plane& plane, const Color& color /*= DEBUG_COLOR*/, const bool depth /*= true*/)
{
// Arrow indicating normal
Vector3 plane_origin = plane.normal * plane.d;
DrawDirectionalArrow(plane_origin, plane_origin + plane.normal * 2.0f, 0.2f, color, duration, depth);
DrawDirectionalArrow(plane_origin, plane_origin + plane.normal * 2.0f, 0.2f, color, depth);

Vector3 U, V;
plane.normal.FindBestAxisVectors(U, V);
static const float scale = 10000.0f;
DrawLine(plane_origin - U * scale, plane_origin + U * scale, color, color, duration, depth);
DrawLine(plane_origin - V * scale, plane_origin + V * scale, color, color, duration, depth);
DrawLine(plane_origin - U * scale, plane_origin + U * scale, color, color, depth);
DrawLine(plane_origin - V * scale, plane_origin + V * scale, color, color, depth);
}

void Renderer::AddLinesToBeRendered()
Expand Down

0 comments on commit 4616738

Please sign in to comment.