Skip to content

Commit

Permalink
feat(engine): add arrow gizmo
Browse files Browse the repository at this point in the history
  • Loading branch information
DiogoMendonc-a committed Oct 31, 2023
1 parent 0b5ddfd commit 6558385
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
**/engine/collisions/ @luishfonseca
**/engine/input/ @luishfonseca
**/engine/physics/ @luishfonseca @fallenatlas
**/engine/gizmos/ @DiogoMendonc-a

**/tesseratos/ @DiogoMendonc-a @roby2014
13 changes: 13 additions & 0 deletions engine/include/cubos/engine/gizmos/gizmos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ namespace cubos::engine
glm::vec3 secondBaseCenter, float secondBaseRadius, float lifespan = 0.0F,
Space space = Space::World);

/// @brief Draws an arrow gizmo.
/// @param id Identifier of the gizmo.
/// @param origin Point from which the arrow starts.
/// @param direction Direction of the arrow.
/// @param girth Width of the cylinder part of the arrow.
/// @param width Width of the base of the cone at the tip of the arrow.
/// @param ratio Point of the arrow at which the cylinder ends and the cone begins.
/// @param lifespan How long the line will be on screen for, in seconds. Defaults to 0, which means a single
/// frame.
/// @param space Space to draw the gizmo in.
void drawArrow(const std::string& id, glm::vec3 origin, glm::vec3 direction, float girth, float width,
float ratio = 0.F, float lifespan = 0.0F, Space space = Space::World);

/// @brief Draws a wireframe box gizmo.
/// @param id Identifier of the gizmo.
/// @param corner One of the corners of the box to be drawn.
Expand Down
7 changes: 4 additions & 3 deletions engine/samples/gizmos/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static void drawSystem(Write<Gizmos> gizmos)

/// [Box]
gizmos->color({0.5F, 1, 1});
gizmos->drawWireBox("test box", {0.5F, 0.5F, 0.5F}, {-1, -1, -1}, 0, Gizmos::Space::World);
gizmos->drawWireBox("test box", {-5, -5, -5}, {-7, -7, -7}, 0, Gizmos::Space::World);
/// [Box]

///[Cut Cone]
Expand All @@ -74,8 +74,9 @@ static void drawSystem(Write<Gizmos> gizmos)
/// [Start Up System]
static void drawStartingLineSystem(Write<Gizmos> gizmos)
{
gizmos->color({1, 0, 0});
gizmos->drawLine("test line", {0, 1, 0}, {1, 0, 1}, 10, Gizmos::Space::Screen);
gizmos->color({1, 0, 1});
gizmos->drawArrow("test arrow", {0.6F, 0.6F, 0.0F}, {-0.1F, -0.1F, 0.0F}, 0.003F, 0.009F, 0.7F, 10.0F,
Gizmos::Space::Screen);
}
/// [Start Up System]

Expand Down
4 changes: 2 additions & 2 deletions engine/samples/gizmos/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ 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:
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 an arrow 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.
This code will draw an arrow poiting at the center of the screen, and it will stay there for 10 seconds.

In this other example, we draw lines, 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 happens, 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.

Expand Down
8 changes: 8 additions & 0 deletions engine/src/cubos/engine/gizmos/gizmos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,11 @@ void Gizmos::drawCutCone(const std::string& id, glm::vec3 firstBaseCenter, float
mColor, lifespan),
space);
}

void Gizmos::drawArrow(const std::string& id, glm::vec3 origin, glm::vec3 direction, float girth, float width,
float ratio, float lifespan, Space space)
{
auto p = direction * ratio;
drawCutCone(id, origin, girth, origin + p, girth, lifespan, space);
drawCutCone(id, origin + p, width, origin + direction, 0.0F, lifespan, space);
}
14 changes: 13 additions & 1 deletion engine/src/cubos/engine/gizmos/types/cut_cone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,19 @@ namespace cubos::engine
auto* verts = static_cast<glm::vec3*>(renderer.cutConePrimitive.vb->map());

glm::vec3 n = glm::normalize(mPointB - mPointA);
glm::vec3 p = {n[1], -n[0], n[2]};
glm::vec3 p;
if (n[0] != n[1])
{
p = {n[1], -n[0], n[2]};
}
else if (n[0] != n[2])
{
p = {-n[2], n[1], n[0]};
}
else
{
p = {n[0], -n[2], n[1]};
}

glm::vec3 pA = p * mRadiusA;

Expand Down

0 comments on commit 6558385

Please sign in to comment.