Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit a2b6329

Browse files
committed
Add IGeometry, QuadShape, Drawable hierarchy
1 parent c262937 commit a2b6329

File tree

5 files changed

+138
-22
lines changed

5 files changed

+138
-22
lines changed

App/Src/Main.cpp

+17-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <imgui.h>
22
#include <Tkge/Engine.hpp>
3+
#include <Tkge/Graphics/Drawable.hpp>
34
#include <Tkge/Graphics/Shader.hpp>
45
#include <klib/assert.hpp>
56
#include <kvf/time.hpp>
@@ -52,27 +53,19 @@ namespace
5253
const auto& renderDevice = engine.RenderDevice();
5354
if (!shader.Load(renderDevice.get_device(), vertexSpirV, fragmentSpirV)) { throw std::runtime_error{"Failed to load shaders"}; }
5455

55-
static constexpr auto Vertices = std::array{
56-
Tkge::Graphics::Vertex{.position = {-200.0f, -200.0f}},
57-
Tkge::Graphics::Vertex{.position = {200.0f, -200.0f}},
58-
Tkge::Graphics::Vertex{.position = {200.0f, 200.0f}},
59-
Tkge::Graphics::Vertex{.position = {-200.0f, 200.0f}},
60-
};
56+
auto quad = Tkge::Graphics::Quad{};
57+
quad.Create(glm::vec2{400.0f});
58+
quad.transform.position.x = -250.0f;
59+
quad.tint = kvf::magenta_v;
6160

62-
static constexpr auto Indices = std::array{
63-
0u, 1u, 2u, 2u, 3u, 0u,
64-
};
61+
auto instancedQuad = Tkge::Graphics::InstancedQuad{};
62+
instancedQuad.Create(glm::vec2{150.0f});
6563

66-
static constexpr auto Primitive = Tkge::Graphics::Primitive{
67-
.vertices = Vertices,
68-
.indices = Indices,
69-
};
70-
71-
auto instances = std::array<Tkge::Graphics::RenderInstance, 2>{};
72-
instances[0].transform.position.x = -250.0f;
73-
instances[0].tint = kvf::cyan_v;
74-
instances[1].transform.position.x = 250.0f;
75-
instances[1].tint = kvf::yellow_v;
64+
instancedQuad.instances.resize(2);
65+
instancedQuad.instances[0].transform.position = {250.0f, -100.0f};
66+
instancedQuad.instances[0].tint = kvf::cyan_v;
67+
instancedQuad.instances[1].transform.position = {250.0f, 100.0f};
68+
instancedQuad.instances[1].tint = kvf::yellow_v;
7669

7770
auto wireframe = false;
7871
auto lineWidth = 3.0f;
@@ -87,8 +80,9 @@ namespace
8780
const auto dt = deltaTime.tick();
8881
elapsed += dt;
8982

90-
instances[0].tint.w = kvf::Color::to_u8((0.5f * std::sin(elapsed.count())) + 0.5f);
91-
instances[1].tint.w = kvf::Color::to_u8((0.5f * std::sin(-elapsed.count())) + 0.5f);
83+
instancedQuad.instances[0].tint.w = kvf::Color::to_u8((0.5f * std::sin(elapsed.count())) + 0.5f);
84+
instancedQuad.instances[1].tint.w = kvf::Color::to_u8((0.5f * std::sin(-elapsed.count())) + 0.5f);
85+
quad.tint.w = kvf::Color::to_u8((0.5f * std::cos(elapsed.count())) + 0.5f);
9286

9387
if (ImGui::Begin("Misc"))
9488
{
@@ -102,7 +96,8 @@ namespace
10296
renderer.BindShader(shader);
10397
renderer.SetLineWidth(lineWidth);
10498
renderer.SetWireframe(wireframe);
105-
renderer.Draw(Primitive, instances);
99+
instancedQuad.Draw(renderer);
100+
quad.Draw(renderer);
106101
}
107102

108103
engine.Present();
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
#include <Tkge/Graphics/Renderer.hpp>
3+
#include <Tkge/Graphics/Shape.hpp>
4+
#include <concepts>
5+
6+
namespace Tkge::Graphics
7+
{
8+
class IDrawable : public klib::Polymorphic
9+
{
10+
public:
11+
[[nodiscard]] virtual Primitive GetPrimitive() const = 0;
12+
[[nodiscard]] virtual std::span<const RenderInstance> GetInstances() const = 0;
13+
14+
virtual void Draw(Renderer& renderer) const { renderer.Draw(GetPrimitive(), GetInstances()); }
15+
};
16+
17+
template <std::derived_from<IGeometry> TGeometry>
18+
class BasicDrawable : public TGeometry, public IDrawable
19+
{
20+
public:
21+
[[nodiscard]] Primitive GetPrimitive() const final
22+
{
23+
return Primitive{
24+
.vertices = this->GetVertices(),
25+
.indices = this->GetIndices(),
26+
.topology = this->GetTopology(),
27+
};
28+
}
29+
};
30+
31+
template <std::derived_from<IGeometry> TGeometry>
32+
class Drawable : public BasicDrawable<TGeometry>, public RenderInstance
33+
{
34+
public:
35+
[[nodiscard]] std::span<const RenderInstance> GetInstances() const final { return {static_cast<const RenderInstance*>(this), 1}; }
36+
};
37+
38+
template <std::derived_from<IGeometry> TGeometry>
39+
class InstancedDrawable : public BasicDrawable<TGeometry>
40+
{
41+
public:
42+
[[nodiscard]] std::span<const RenderInstance> GetInstances() const final { return instances; }
43+
44+
std::vector<RenderInstance> instances{};
45+
};
46+
47+
using Quad = Drawable<QuadShape>;
48+
using InstancedQuad = InstancedDrawable<QuadShape>;
49+
50+
} // namespace Tkge::Graphics
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
#include <Tkge/Graphics/Vertex.hpp>
3+
#include <klib/polymorphic.hpp>
4+
#include <cstdint>
5+
#include <span>
6+
7+
namespace Tkge::Graphics
8+
{
9+
class IGeometry : public klib::Polymorphic
10+
{
11+
public:
12+
[[nodiscard]] virtual std::span<const Vertex> GetVertices() const = 0;
13+
[[nodiscard]] virtual std::span<const std::uint32_t> GetIndices() const = 0;
14+
[[nodiscard]] virtual vk::PrimitiveTopology GetTopology() const = 0;
15+
};
16+
} // namespace Tkge::Graphics

Lib/Include/Tkge/Graphics/Shape.hpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
#include <Tkge/Graphics/IGeometry.hpp>
3+
#include <kvf/color.hpp>
4+
#include <kvf/rect.hpp>
5+
#include <array>
6+
7+
namespace Tkge::Graphics
8+
{
9+
class QuadShape : public IGeometry
10+
{
11+
public:
12+
static constexpr auto Indices = std::array{0u, 1u, 2u, 2u, 3u, 0u};
13+
static constexpr std::size_t VertexCount{6};
14+
15+
[[nodiscard]] std::span<const Vertex> GetVertices() const final { return m_vertices; }
16+
[[nodiscard]] std::span<const std::uint32_t> GetIndices() const final { return Indices; }
17+
[[nodiscard]] vk::PrimitiveTopology GetTopology() const final { return vk::PrimitiveTopology::eTriangleList; }
18+
19+
void Create(const kvf::Rect<>& rect, const kvf::UvRect& uv = kvf::uv_rect_v, kvf::Color colour = kvf::white_v);
20+
void Create(const glm::vec2 size) { Create(kvf::Rect<>::from_size(size)); }
21+
22+
[[nodiscard]] kvf::Rect<> GetRect() const;
23+
[[nodiscard]] kvf::UvRect GetUv() const;
24+
[[nodiscard]] glm::vec2 GetSize() const { return GetRect().size(); }
25+
[[nodiscard]] kvf::Color GetColour() const { return m_vertices.front().colour; }
26+
27+
private:
28+
std::array<Vertex, VertexCount> m_vertices{};
29+
};
30+
} // namespace Tkge::Graphics

Lib/Src/Graphics/Shape.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <Tkge/Graphics/Shape.hpp>
2+
3+
namespace Tkge::Graphics
4+
{
5+
namespace
6+
{
7+
constexpr std::size_t LeftBottom{0};
8+
constexpr std::size_t RightBottom{1};
9+
constexpr std::size_t RightTop{2};
10+
constexpr std::size_t LeftTop{3};
11+
} // namespace
12+
13+
void QuadShape::Create(const kvf::Rect<>& rect, const kvf::UvRect& uv, const kvf::Color colour)
14+
{
15+
m_vertices[LeftBottom] = Vertex{.position = rect.bottom_left(), .colour = colour.to_linear(), .uv = uv.bottom_left()};
16+
m_vertices[RightBottom] = Vertex{.position = rect.bottom_right(), .colour = colour.to_linear(), .uv = uv.bottom_right()};
17+
m_vertices[RightTop] = Vertex{.position = rect.top_right(), .colour = colour.to_linear(), .uv = uv.top_right()};
18+
m_vertices[LeftTop] = Vertex{.position = rect.top_left(), .colour = colour.to_linear(), .uv = uv.top_left()};
19+
}
20+
21+
kvf::Rect<> QuadShape::GetRect() const { return kvf::Rect<>{.lt = m_vertices[LeftTop].position, .rb = m_vertices[RightBottom].position}; }
22+
23+
[[nodiscard]] kvf::UvRect QuadShape::GetUv() const { return {.lt = m_vertices[LeftTop].uv, .rb = m_vertices[RightBottom].uv}; }
24+
25+
} // namespace Tkge::Graphics

0 commit comments

Comments
 (0)