Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,6 @@ target_link_libraries(txt OpenGL::GL glfw Freetype::Freetype)
target_compile_definitions(txt PUBLIC GLAD_OPTION_MX=1)

add_executable(maketicks maketicks.cpp)

add_executable(coordarrows coordarrows.cpp)
target_link_libraries(coordarrows OpenGL::GL glfw Freetype::Freetype)
55 changes: 55 additions & 0 deletions examples/coordarrows.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Visualize a CoordArrows VisualModel
*/
#include <iostream>
#include <sm/vec>
#include <mplot/Visual.h>
#include <mplot/CoordArrows.h> // This is the only VisualModel derived class not called SomethingVisual!

int main()
{
mplot::Visual v(1024, 768, "Coordinate Arrows");
v.showCoordArrows (true); // Shows the 'scene' coordarrows, that you get in every mplot::Visual
v.lightingEffects(true);

// An extra CoordArrows model, with defaults
sm::vec<float> offset = {};
auto cavm = std::make_unique<mplot::CoordArrows<>> (offset);
v.bindmodel (cavm);
cavm->finalize();
v.addVisualModel (cavm);

sm::vec<float> ux = {1,0,0};
sm::vec<float> uy = {0,1,0};
sm::vec<float> uz = {0,0,1};

// An CoordArrows model, with non-defaults
offset[0] += 1.5f;
cavm = std::make_unique<mplot::CoordArrows<>> (offset);
v.bindmodel (cavm);
cavm->init (ux, uy, -uz);

cavm->lengths = sm::vec<float>{1,1,1}; // this is the default already
cavm->thickness = 0.9f;
cavm->em = 0.08f;
// or: cavn->init (sm::vec<float>{1,1,1}, 0.9f, 0.08f);

cavm->z_label = "-Z";

cavm->finalize();
v.addVisualModel (cavm);

// One more, for good measure
offset[0] += 1.5f;
cavm = std::make_unique<mplot::CoordArrows<>> (offset);
v.bindmodel (cavm);
cavm->init (ux, uz, uy);
cavm->init (sm::vec<float>{1,1,0.5}, 1.5f, 0.13f);
cavm->x_label = "X'";
cavm->y_label = "Y'";
cavm->z_label = "Z'";
cavm->finalize();
v.addVisualModel (cavm);

v.keepOpen();
}
55 changes: 34 additions & 21 deletions mplot/CoordArrows.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ namespace mplot {
this->em = _em;
}

//! You can call this AS well as the first init overload to set the axis vectors
void init (const sm::vec<float, 3> _x, const sm::vec<float, 3> _y, const sm::vec<float, 3> _z)
{
this->x_axis = _x;
this->y_axis = _y;
this->z_axis = _z;
}

//! Make sure coord arrow colours are ok on the given background colour. Call this *after* finalize.
void setColourForBackground (const std::array<float, 4>& bgcolour)
{
Expand Down Expand Up @@ -62,19 +70,18 @@ namespace mplot {
mplot::TextFeatures tfca(this->em, 48, false, mplot::colour::black, mplot::VisualFont::DVSansItalic);

// These texts are black by default
sm::vec<float> toffset = this->mv_offset;
toffset[0] += this->lengths[0] + this->em;
sm::vec<float> toffset = {};
toffset = this->mv_offset + this->x_axis * this->lengths[0];
toffset[0] += this->em;
auto vtm1 = this->makeVisualTextModel (tfca);
vtm1->setupText (this->x_label, toffset);
this->texts.push_back (std::move(vtm1));
toffset = this->mv_offset;
toffset[1] += this->lengths[1];
toffset = this->mv_offset + this->y_axis * this->lengths[1];
toffset[0] += this->em;
auto vtm2 = this->makeVisualTextModel (tfca);
vtm2->setupText (this->y_label, toffset);
this->texts.push_back (std::move(vtm2));
toffset = this->mv_offset;
toffset[2] += this->lengths[2];
toffset = this->mv_offset + this->z_axis * this->lengths[2];
toffset[0] += this->em;
auto vtm3 = this->makeVisualTextModel (tfca);
vtm3->setupText (this->z_label, toffset);
Expand All @@ -95,32 +102,38 @@ namespace mplot {

// Draw four spheres to make up the coord frame, with centre at 0,0,0
// (mv_offset is applied in translation matrices)
sm::vec<float, 3> reloffset = {0,0,0};
sm::vec<float, 3> zerocoord = {0,0,0};
this->computeSphere (zerocoord, centresphere_col, this->thickness*this->lengths[0]/20.0);
sm::vec<float, 3> reloffset = {};
static constexpr sm::vec<float, 3> zerocoord = { 0.0f, 0.0f, 0.0f };
this->computeSphere (zerocoord, centresphere_col, this->thickness * this->lengths[0] / 20.0f);


// x
reloffset[0] += this->lengths[0];
this->computeSphere (reloffset, x_axis_col, this->thickness*this->lengths[0]/40.0);
this->computeTube (zerocoord, reloffset, x_axis_col, x_axis_col, this->thickness*this->lengths[0]/80.0);
reloffset = this->x_axis * this->lengths[0];
this->computeSphere (reloffset, x_axis_col, this->thickness * this->lengths[0] / 40.0f);
this->computeTube (zerocoord, reloffset, x_axis_col, x_axis_col, this->thickness * this->lengths[0] / 80.0f);

// y
reloffset[0] -= this->lengths[0];
reloffset[1] += this->lengths[1];
this->computeSphere (reloffset, y_axis_col, this->thickness*this->lengths[0]/40.0);
this->computeTube (zerocoord, reloffset, y_axis_col, y_axis_col, this->thickness*this->lengths[0]/80.0);
reloffset = this->y_axis * this->lengths[1];
this->computeSphere (reloffset, y_axis_col, this->thickness * this->lengths[0] / 40.0f);
this->computeTube (zerocoord, reloffset, y_axis_col, y_axis_col, this->thickness * this->lengths[0] / 80.0f);

// z
reloffset[1] -= this->lengths[1];
reloffset[2] += this->lengths[2];
this->computeSphere (reloffset, z_axis_col, this->thickness*this->lengths[0]/40.0);
this->computeTube (zerocoord, reloffset, z_axis_col, z_axis_col, this->thickness*this->lengths[0]/80.0);
reloffset = this->z_axis * this->lengths[2];
this->computeSphere (reloffset, z_axis_col, this->thickness * this->lengths[0] / 40.0f);
this->computeTube (zerocoord, reloffset, z_axis_col, z_axis_col, this->thickness * this->lengths[0] / 80.0f);

this->initAxisLabels();
}

//! The lengths of the x, y and z arrows.
//! Length multipliers that can be applied to ux, uy and uz
sm::vec<float, 3> lengths = { 1.0f, 1.0f, 1.0f };

//! The axes for the coordinate arrows. A simple right handed coordinate system aligned with
//! the 'real' world coordinate system by default.
sm::vec<float, 3> x_axis = { 1.0f, 0.0f, 0.0f };
sm::vec<float, 3> y_axis = { 0.0f, 1.0f, 0.0f };
sm::vec<float, 3> z_axis = { 0.0f, 0.0f, 1.0f };

//! A thickness scaling factor, to apply to the arrows.
float thickness = 1.0f;
//! m size for text labels
Expand Down