Skip to content

Commit

Permalink
refactor(engine): change gizmos structure to not have logic on resources
Browse files Browse the repository at this point in the history
  • Loading branch information
DiogoMendonc-a committed Oct 22, 2023
1 parent 690e45b commit d312956
Show file tree
Hide file tree
Showing 13 changed files with 374 additions and 275 deletions.
2 changes: 1 addition & 1 deletion core/include/cubos/core/gl/render_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ namespace cubos::core::gl
/// @param stencil Stencil value.
virtual void clearStencil(int stencil) = 0;

/// @brief Draws Lines.
/// @brief Draws lines.
/// @param offset Index of the first vertex to be drawn.
/// @param count Number of vertices that will be drawn.
virtual void drawLines(std::size_t offset, std::size_t count) = 0;
Expand Down
1 change: 1 addition & 0 deletions docs/pages/3_examples/2_engine/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ multiple plugins of the engine:
- @subpage examples-engine-hello-cubos - @copybrief examples-engine-hello-cubos
- @subpage examples-engine-settings - @copybrief examples-engine-settings
- @subpage examples-engine-renderer - @copybrief examples-engine-renderer
- @subpage examples-engine-gizmos - @copybrief examples-engine-gizmos
- @subpage examples-engine-scene - @copybrief examples-engine-scene
- @subpage examples-engine-input - @copybrief examples-engine-input
- @subpage examples-engine-assets - @copybrief examples-engine-assets
Expand Down
1 change: 1 addition & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ set(CUBOS_ENGINE_SOURCE

"src/cubos/engine/gizmos/plugin.cpp"
"src/cubos/engine/gizmos/gizmos.cpp"
"src/cubos/engine/gizmos/renderer.cpp"

"src/cubos/engine/scene/plugin.cpp"
"src/cubos/engine/scene/bridge.cpp"
Expand Down
55 changes: 32 additions & 23 deletions engine/include/cubos/engine/gizmos/gizmos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,25 @@
/// @ingroup gizmos-plugin

#pragma once

#include <vector>

#include <glm/glm.hpp>
#include <glm/vec3.hpp>

#include <cubos/core/gl/render_device.hpp>

#include <cubos/engine/cubos.hpp>

using cubos::core::gl::RenderDevice;
using cubos::core::gl::ShaderPipeline;
using cubos::engine::DeltaTime;

namespace cubos::engine
{
/// @brief Resource which draws gizmos.
class GizmosRenderer;

/// @brief Resource which queues commands for drawing gizmos, basic primitives useful for debugging and tools.
///
/// @ingroup gizmos-plugin
class Gizmos final
{
public:
/// @brief Initiates the Gizmos resource.
/// @param renderDevice Active render device.
void init(RenderDevice& renderDevice);

/// @brief Sets the color to be used when drawing any subsequent gizmos.
/// @param color Color to be used.
void color(const glm::vec3& color);
Expand All @@ -37,36 +32,50 @@ namespace cubos::engine
/// @param to The other end of the line to be drawn.
/// @param lifespan How long the line will be on screen for, in seconds. Defaults to 0, which means a single
/// frame.
void drawLine(const std::string& id, glm::vec3 from, glm::vec3 to, float lifespan = 0);
void drawLine(const std::string& id, glm::vec3 from, glm::vec3 to, float lifespan = 0.0F);

/// @brief Draws a filled box gizmo.
/// @param id Identifier of the gizmo.
/// @param corner One of the corners of the box to be drawn.
/// @param oppositeCorner The opposite corner of the box to be drawn.
/// @param lifespan How long the line will be on screen for, in seconds. Defaults to 0, which means a single
/// frame.
void drawBox(const std::string& id, glm::vec3 corner, glm::vec3 oppositeCorner, float lifespan = 0);
void drawBox(const std::string& id, glm::vec3 corner, glm::vec3 oppositeCorner, float lifespan = 0.0F);

/// @brief Draws a wireframe box gizmo.
/// @param id Identifier of the gizmo.
/// @param corner One of the corners of the box to be drawn.
/// @param oppositeCorner The opposite corner of the box to be drawn.
/// @param lifespan How long the line will be on screen for, in seconds. Defaults to 0, which means a single
/// frame.
void drawWireBox(const std::string& id, glm::vec3 corner, glm::vec3 oppositeCorner, float lifespan = 0);
void drawWireBox(const std::string& id, glm::vec3 corner, glm::vec3 oppositeCorner, float lifespan = 0.0F);

/// @brief Class that describes a type of gizmo
class Gizmo
{
public:
virtual ~Gizmo() = default;

Gizmo(const std::string& id, glm::vec3 color, float lifespan);

/// @brief Draws the gizmo to screen.
/// @param renderer Renderer.
virtual void draw(GizmosRenderer& renderer) = 0;

/// @brief Decreases the time the gizmo has left before it is destroyed.
/// @param delta Seconds since the last frame.
bool decreaseLifespan(float delta);

protected:
const std::string& mId; ///< Gizmo identifier
glm::vec3 mColor; ///< Color of the gizmo
float mLifespan; ///< Time in seconds the gizmo has left to live
};

/// @brief Draws all the gizmos that were called to be drawn.
/// @param deltaTime Resource holding the time since the previous frame.
void drawQueuedGizmos(DeltaTime deltaTime);
std::vector<std::shared_ptr<Gizmo>> gizmos; ///< Queued gizmos to be drawn.

private:
ShaderPipeline mPipeline; ///< Shader pipeline to be used when drawing gizmos
RenderDevice* mRenderDevice; ///< Active render device
glm::vec3 mColor; ///< Currently set color
class GizmoBase;
class LineGizmo;
class BoxGizmo;
std::vector<std::shared_ptr<GizmoBase>> mGizmosVector; ///< Queued gizmos to be drawn.
glm::vec3 mColor; ///< Currently set color
};

} // namespace cubos::engine
13 changes: 11 additions & 2 deletions engine/include/cubos/engine/gizmos/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ namespace cubos::engine
{
/// @defgroup gizmos-plugin Gizmos
/// @ingroup engine
/// @brief Allows gizmos to be drawn.
/// @brief Used to draw gizmos helpful for debugging and tools.
///
/// ## Resources
/// - @ref Gizmos - stores gizmos information.
/// - @ref Gizmos - used to queue gizmo draw commands.
///
/// ## Startup tags
/// - `cubos.gizmos.init` - the gizmos renderer is initialized, after `cubos.window.init`
///
/// ## Tags
/// - `cubos.gizmos.draw` - queued gizmos are rendered to the window, after `cubos.renderer.draw` and
/// before `cubos.window.render`.
///
/// ## Dependencies
/// - @ref window-plugin

/// @brief Plugin entry function.
/// @param cubos @b CUBOS. main class.
Expand Down
16 changes: 7 additions & 9 deletions engine/samples/gizmos/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
using cubos::core::ecs::Commands;
using cubos::core::ecs::Write;

using cubos::engine::Gizmos;

using namespace cubos::engine;

static void mockCamera(Write<ActiveCameras> camera, Commands cmds)
static void setCameraSystem(Write<ActiveCameras> camera, Commands cmds)
{
camera->entities[0] = cmds.create()
.add(Camera{.fovY = 60.0F, .zNear = 0.1F, .zFar = 100.0F})
Expand All @@ -24,13 +22,13 @@ static void mockCamera(Write<ActiveCameras> camera, Commands cmds)
static void drawSystem(Write<Gizmos> gizmos)
{
gizmos->color({0, 0, 1});
gizmos->drawLine("test line", {0, 0, 0}, {1, 1, 1}, 0);
gizmos->drawLine("test line", {0, 0, 0}, {1, 1, 1});

gizmos->color({0, 1, 1});
gizmos->drawBox("test box", {0.4, 0.4, 0}, {0.6, 0.6, 1}, 0);
gizmos->drawBox("test box", {0.4, 0.4, 0}, {0.6, 0.6, 1});

gizmos->color({1, 0, 1});
gizmos->drawWireBox("test box", {0.2, 0.7, 0}, {0.3, 0.1, 0}, 0);
gizmos->drawWireBox("test box", {0.2, 0.7, 0}, {0.3, 0.1, 0});
}
/// [System]

Expand All @@ -42,15 +40,15 @@ static void drawStartingLineSystem(Write<Gizmos> gizmos)
}
/// [Start Up System]

/// [Run]
int main(int argc, char** argv)
{
/// [Adding plugin]
Cubos cubos{argc, argv};
cubos.addPlugin(gizmosPlugin);
/// [Adding plugin]
cubos.addPlugin(rendererPlugin);
cubos.startupSystem(mockCamera);
cubos.startupSystem(setCameraSystem);
cubos.startupSystem(drawStartingLineSystem).tagged("sample.init").after("cubos.gizmos.init");
cubos.system(drawSystem);
cubos.run();
}
/// [Run]
19 changes: 19 additions & 0 deletions engine/samples/gizmos/page.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Gizmos {#examples-engine-gizmos}

@brief Using the @ref gizmos-plugin plugin.

This example shows the @ref gizmos-plugin plugin, which allows drawing simple primitives. These are not intended for use in the final product, only in developer tools and for debugging.

The plugin function is included from the @ref engine/gizmos/plugin.hpp header.

@snippet gizmos/main.cpp Adding plugin

To draw a gizmo, all you need to do is to get a reference to the @ref cubos::engine::Gizmos resource, and then call the draw function on it for the gizmo you want to draw. Additionally, you can also call the @ref cubos::engine::Gizmos::color function to set the color for future gizmos. So, for example if you want to draw a line in a given system, all you need to do is the following:

@snippet gizmos/main.cpp Start Up System

This code will draw a red line from the top-left of the screen to the bottom right, and it will stay there for 10 seconds.

In this other example, we draw a line, a box, and a wire box. Unlike the one in the previous example, this system is not a start-up system, so the draw functions get called every single frame. When this happensl, you should set the lifetime of a gizmo to 0, which means it will be drawn for a single frame only. This way we avoid drawing gizmos on top of identical ones that were already there, or in the case of moving gizmos, leaving a trail of old version behind them.

@snippet gizmos/main.cpp System
Loading

0 comments on commit d312956

Please sign in to comment.