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 15, 2023
1 parent dc10813 commit 1c26087
Show file tree
Hide file tree
Showing 11 changed files with 369 additions and 243 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
53 changes: 52 additions & 1 deletion core/src/cubos/core/gl/ogl_render_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,8 +842,19 @@ class OGLVertexBuffer : public impl::VertexBuffer

void* map() override
{
GLenum glErr = glGetError();
if (glErr != 0)

Check warning on line 846 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L845-L846

Added lines #L845 - L846 were not covered by tests
{
LOG_GL_ERROR(glErr);

Check warning on line 848 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L848

Added line #L848 was not covered by tests
}
glBindBuffer(GL_ARRAY_BUFFER, this->id);
return glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
void* r = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
glErr = glGetError();
if (glErr != 0)

Check warning on line 853 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L851-L853

Added lines #L851 - L853 were not covered by tests
{
LOG_GL_ERROR(glErr);

Check warning on line 855 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L855

Added line #L855 was not covered by tests
}
return r;

Check warning on line 857 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L857

Added line #L857 was not covered by tests
}

void unmap() override
Expand Down Expand Up @@ -1451,17 +1462,32 @@ void OGLRenderDevice::setFramebuffer(Framebuffer fb)

RasterState OGLRenderDevice::createRasterState(const RasterStateDesc& desc)
{
GLenum glErr = glGetError();
if (glErr != 0)

Check warning on line 1466 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L1465-L1466

Added lines #L1465 - L1466 were not covered by tests
{
LOG_GL_ERROR(glErr);

Check warning on line 1468 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L1468

Added line #L1468 was not covered by tests
}
auto rs = std::make_shared<OGLRasterState>();
rs->cullEnabled = static_cast<GLboolean>(desc.cullEnabled);
rs->scissorEnabled = static_cast<GLboolean>(desc.scissorEnabled);
faceToGL(desc.cullFace, rs->cullFace);
windingToGL(desc.frontFace, rs->frontFace);
rasterModeToGL(desc.rasterMode, rs->polygonMode);
glErr = glGetError();
if (glErr != 0)

Check warning on line 1477 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L1476-L1477

Added lines #L1476 - L1477 were not covered by tests
{
LOG_GL_ERROR(glErr);

Check warning on line 1479 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L1479

Added line #L1479 was not covered by tests
}
return rs;
}

void OGLRenderDevice::setRasterState(RasterState rs)
{
GLenum glErr = glGetError();
if (glErr != 0)

Check warning on line 1487 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L1486-L1487

Added lines #L1486 - L1487 were not covered by tests
{
LOG_GL_ERROR(glErr);

Check warning on line 1489 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L1489

Added line #L1489 was not covered by tests
}
auto rsImpl = std::static_pointer_cast<OGLRasterState>(rs ? rs : mDefaultRS);

if (rsImpl->cullEnabled == 0U)
Expand All @@ -1485,6 +1511,11 @@ void OGLRenderDevice::setRasterState(RasterState rs)

glFrontFace(rsImpl->frontFace);
glPolygonMode(GL_FRONT_AND_BACK, rsImpl->polygonMode);
glErr = glGetError();
if (glErr != 0)

Check warning on line 1515 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L1514-L1515

Added lines #L1514 - L1515 were not covered by tests
{
LOG_GL_ERROR(glErr);

Check warning on line 1517 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L1517

Added line #L1517 was not covered by tests
}
}

DepthStencilState OGLRenderDevice::createDepthStencilState(const DepthStencilStateDesc& desc)
Expand Down Expand Up @@ -2449,13 +2480,33 @@ void OGLRenderDevice::drawTriangles(std::size_t offset, std::size_t count)

void OGLRenderDevice::drawLines(std::size_t offset, std::size_t count)

Check warning on line 2481 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L2481

Added line #L2481 was not covered by tests
{
GLenum glErr = glGetError();
if (glErr != 0)

Check warning on line 2484 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L2483-L2484

Added lines #L2483 - L2484 were not covered by tests
{
LOG_GL_ERROR(glErr);

Check warning on line 2486 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L2486

Added line #L2486 was not covered by tests
}
glDrawArrays(GL_LINES, static_cast<GLint>(offset), static_cast<GLsizei>(count));
glErr = glGetError();
if (glErr != 0)

Check warning on line 2490 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L2488-L2490

Added lines #L2488 - L2490 were not covered by tests
{
LOG_GL_ERROR(glErr);

Check warning on line 2492 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L2492

Added line #L2492 was not covered by tests
}
}

void OGLRenderDevice::drawTrianglesIndexed(std::size_t offset, std::size_t count)
{
GLenum glErr = glGetError();
if (glErr != 0)

Check warning on line 2499 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L2498-L2499

Added lines #L2498 - L2499 were not covered by tests
{
LOG_GL_ERROR(glErr);

Check warning on line 2501 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L2501

Added line #L2501 was not covered by tests
}
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(count), static_cast<GLenum>(mCurrentIndexFormat),
reinterpret_cast<const void*>(offset * mCurrentIndexSz));
glErr = glGetError();
if (glErr != 0)

Check warning on line 2506 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L2505-L2506

Added lines #L2505 - L2506 were not covered by tests
{
LOG_GL_ERROR(glErr);

Check warning on line 2508 in core/src/cubos/core/gl/ogl_render_device.cpp

View check run for this annotation

Codecov / codecov/patch

core/src/cubos/core/gl/ogl_render_device.cpp#L2508

Added line #L2508 was not covered by tests
}
}

void OGLRenderDevice::drawTrianglesInstanced(std::size_t offset, std::size_t count, std::size_t instanceCount)
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
82 changes: 62 additions & 20 deletions engine/include/cubos/engine/gizmos/gizmos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,42 @@

#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.
/// @brief Resource that holds the information needed to draw a gizmo
class GizmosRenderer final
{
public:
cubos::core::gl::ShaderPipeline pipeline; ///< Shader pipeline to be used when drawing gizmos
cubos::core::gl::RenderDevice* renderDevice; ///< Active render device

/// @brief Set of buffers and structs that hold te information needed to draw a specific type of mesh
struct Primitive
{
cubos::core::gl::VertexBuffer vb;
cubos::core::gl::VertexArray va;
cubos::core::gl::IndexBuffer ib;
cubos::core::gl::VertexArrayDesc vaDesc;
};

Primitive linePrimitive; ///< GL line information
Primitive boxPrimitive; ///< GL box information

/// @brief Sets up the GizmosRenderer to be used
/// @param renderDevice the current RenderDevice being used
void init(cubos::core::gl::RenderDevice* renderDevice);

private:
void initLinePrimitive();
void initBoxPrimitive();
};

/// @brief Resource which queues 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 Down Expand Up @@ -55,18 +75,40 @@ namespace cubos::engine
/// frame.
void drawWireBox(const std::string& id, glm::vec3 corner, glm::vec3 oppositeCorner, float lifespan = 0);

/// @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);
/// @brief Class that describes a type of gizmo
class GizmoBase
{
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

public:
GizmoBase(const std::string& id, glm::vec3 color, float lifespan)
: mId(id)
, mColor(color)
, mLifespan(lifespan)
{
}

/// @brief draws the gizmo to screen
/// @param renderer the GizmosRenderer in use
virtual void draw(GizmosRenderer renderer) = 0;

/// @brief decreases the time the gizmo has left before it is destroyed
/// @param delta the time in seconds since the last frame
bool decreaseLifespan(float delta)
{
mLifespan -= delta;
return mLifespan <= 0;
}

virtual ~GizmoBase() = default;
};

std::vector<std::shared_ptr<GizmoBase>> gizmosVector; ///< 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
} // namespace cubos::engine
2 changes: 1 addition & 1 deletion engine/include/cubos/engine/gizmos/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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.
Expand Down
12 changes: 6 additions & 6 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 Down Expand Up @@ -42,15 +40,17 @@ 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);
/// [Run]
cubos.startupSystem(drawStartingLineSystem).tagged("sample.init").after("cubos.gizmos.init");
cubos.system(drawSystem);
/// [Run]
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 1c26087

Please sign in to comment.