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
7 changes: 4 additions & 3 deletions examples/cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ int main()
// Parameters of the model
sm::vec<float, 3> offset = { 1, -0.5, -0.5 }; // a within-scene offset

sm::vec<float, 3> e1 = { 1, 0, 0 };
sm::vec<float, 3> e2 = { 0, 1, 0 };
sm::vec<float, 3> e3 = { 0, 0, 1 };
sm::vec<float, 3> e1 = { 1, 0, 0 };
sm::vec<float, 3> e2 = { 0, 1, 0 };
sm::vec<float, 3> e3 = { 0, 0, 1 };

sm::vec<float, 3> colour1 = { 0.35, 0.76, 0.98 }; // RGB colour triplet

auto rv = std::make_unique<mplot::RhomboVisual<>> (offset, e1, e2, e3, colour1);
v.bindmodel (rv);
rv->name = "Cube.002";
rv->facecm = mplot::ColourMapType::Rainbow; // Try Rainbow, Batlow, Tofino
rv->finalize();
v.addVisualModel (rv);

Expand Down
87 changes: 87 additions & 0 deletions mplot/RhomboVisual.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <array>
#include <sm/vec>
#include <mplot/VisualModel.h>
#include <mplot/ColourMap.h>

namespace mplot {

Expand All @@ -20,6 +21,15 @@ namespace mplot {

//! Initialize vertex buffer objects and vertex array object.
void initializeVertices() override
{
if (this->facecm == mplot::ColourMapType::Fixed) {
this->vertices_singlecolour();
} else {
this->vertices_multicolour();
}
}

void vertices_singlecolour()
{
// Compute the face normals
sm::vec<float> _n1 = this->edge1.cross (this->edge2);
Expand Down Expand Up @@ -86,11 +96,88 @@ namespace mplot {
}
}

// Allowing a colour for each face. May be useful for debugging
void vertices_multicolour()
{
// Compute the face normals
sm::vec<float> _n1 = this->edge1.cross (this->edge2);
_n1.renormalize();
sm::vec<float> _n2 = this->edge2.cross (this->edge3);
_n2.renormalize();
sm::vec<float> _n3 = this->edge1.cross (this->edge3);
_n3.renormalize();

// First corner of rhombohedron is at model-frame's origin
sm::vec<float> o = {0,0,0};

// Push positions and normals for 24 vertices to make up the rhombohedron; *6* for each face.
// Front face needs 6 vertices
this->vertex_push (o, this->vertexPositions);
this->vertex_push (o + this->edge1, this->vertexPositions);
this->vertex_push (o + this->edge3, this->vertexPositions);
this->vertex_push (o + this->edge3, this->vertexPositions); // extra
this->vertex_push (o + this->edge1, this->vertexPositions); // extra
this->vertex_push (o + this->edge1 + this->edge3, this->vertexPositions);
for (unsigned short i = 0U; i < 6U; ++i) { this->vertex_push (_n3, this->vertexNormals); }
// Top face
this->vertex_push (o + this->edge3, this->vertexPositions);
this->vertex_push (o + this->edge1 + this->edge3, this->vertexPositions);
this->vertex_push (o + this->edge2 + this->edge3, this->vertexPositions);
this->vertex_push (o + this->edge2 + this->edge3, this->vertexPositions); // extra
this->vertex_push (o + this->edge1 + this->edge3, this->vertexPositions); // extra
this->vertex_push (o + this->edge2 + this->edge1 + this->edge3, this->vertexPositions);
for (unsigned short i = 0U; i < 6U; ++i) { this->vertex_push (_n1, this->vertexNormals); }
// Back face
this->vertex_push (o + this->edge2 + this->edge3, this->vertexPositions);
this->vertex_push (o + this->edge2 + this->edge1 + this->edge3, this->vertexPositions);
this->vertex_push (o + this->edge2, this->vertexPositions);
this->vertex_push (o + this->edge2, this->vertexPositions); // extra
this->vertex_push (o + this->edge2 + this->edge1 + this->edge3, this->vertexPositions); // extra
this->vertex_push (o + this->edge2 + this->edge1, this->vertexPositions);
for (unsigned short i = 0U; i < 6U; ++i) { this->vertex_push (-_n3, this->vertexNormals); }
// Bottom face
this->vertex_push (o + this->edge2, this->vertexPositions);
this->vertex_push (o + this->edge2 + this->edge1, this->vertexPositions);
this->vertex_push (o, this->vertexPositions);
this->vertex_push (o, this->vertexPositions); // extra
this->vertex_push (o + this->edge2 + this->edge1, this->vertexPositions); // extra
this->vertex_push (o + this->edge1, this->vertexPositions);
for (unsigned short i = 0U; i < 6U; ++i) { this->vertex_push (-_n1, this->vertexNormals); }
// Left face
this->vertex_push (o + this->edge2, this->vertexPositions);
this->vertex_push (o, this->vertexPositions);
this->vertex_push (o + this->edge2 + this->edge3, this->vertexPositions);
this->vertex_push (o + this->edge2 + this->edge3, this->vertexPositions); // extra
this->vertex_push (o, this->vertexPositions); // extra
this->vertex_push (o + this->edge3, this->vertexPositions);
for (unsigned short i = 0U; i < 6U; ++i) { this->vertex_push (-_n2, this->vertexNormals); }
// Right face
this->vertex_push (o + this->edge1, this->vertexPositions);
this->vertex_push (o + this->edge1 + this->edge2, this->vertexPositions);
this->vertex_push (o + this->edge1 + this->edge3, this->vertexPositions);
this->vertex_push (o + this->edge1 + this->edge3, this->vertexPositions); // extra
this->vertex_push (o + this->edge1 + this->edge2, this->vertexPositions); // extra
this->vertex_push (o + this->edge1 + this->edge2 + this->edge3, this->vertexPositions);
for (unsigned short i = 0U; i < 6U; ++i) { this->vertex_push (_n2, this->vertexNormals); }

// Vertex colours are NOT all the same
mplot::ColourMap<float> cm (this->facecm);
for (unsigned short i = 0U; i < 36U; i += 3) {
this->vertex_push (cm.convert(static_cast<float>(i) / 35.0f), this->vertexColors);
this->vertex_push (cm.convert(static_cast<float>(i) / 35.0f), this->vertexColors);
this->vertex_push (cm.convert(static_cast<float>(i) / 35.0f), this->vertexColors);
}

// Indices for 6 faces.
for (unsigned short i = 0U; i < 36U; ++i) { this->indices.push_back (this->idx++); }
}

//! Three vectors define the Rhombohedron and we use a single colour
sm::vec<float, 3> edge1 = {0.0f, 0.0f, 0.0f};
sm::vec<float, 3> edge2 = {0.0f, 0.0f, 0.0f};
sm::vec<float, 3> edge3 = {0.0f, 0.0f, 0.0f};
std::array<float, 3> col = {0.0f, 0.0f, 1.0f};
mplot::ColourMapType facecm = mplot::ColourMapType::Fixed; // if so, use col
};

} // namespace mplot