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
11 changes: 11 additions & 0 deletions examples/breadcrumbs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ int main()

sm::vvec<std::array<float, 3>> col = { mplot::colour::blue, mplot::colour::springgreen };
sm::vvec<float> alph = { 0.5f, 1.0f };
// A scaling vector to make sequential instances have a different size
sm::vvec<float> scl = { 1.0f, 1.2f };

while (!v.readyToFinish()) {
Expand All @@ -103,6 +104,16 @@ int main()
// Place data in SSBO. first call of set_data must occur after first call to v.render()
isvp->set_instance_data (points, col, alph, scl);

// As well as scl, we have an applied-to-all instances scale (iscl) that is passed to the
// s_matrix in the instance shader
float iscl = 1.5f + 0.5f * std::sin (sm::mathconst<float>::two_pi * (static_cast<float>(i % 360) / 360.0f));
isvp->set_instance_scale (iscl);

// Can set scale of the black spheres based on distance to rotation centre. As they get
// further away, they get larger so you can still see them.
float iscl2 = 0.6f * std::log (2.0f + v.get_d_to_rotation_centre());
isvp2->set_instance_scale (iscl2);

v.render();
v.waitevents (0.018);

Expand Down
3 changes: 3 additions & 0 deletions mplot/VisualBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,9 @@ namespace mplot

public:

//! Getter for d_to_rotation_centre
float get_d_to_rotation_centre() const { return this->d_to_rotation_centre; }

/*
* Generic callback handlers
*/
Expand Down
3 changes: 2 additions & 1 deletion mplot/VisualDefaultShaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace mplot
"uniform mat4 p_matrix;\n"
"uniform float alpha;\n";
const char* defaultVtxShader_instance_uniforms =
"uniform mat4 s_matrix;\n" // scaling matrix
"uniform int instance_count = 0;\n"
"uniform int instance_start = -1;\n"
"uniform int instparam_count = 0;\n";
Expand Down Expand Up @@ -56,7 +57,7 @@ namespace mplot
" float s = iparam[ippos_i + idx * 5 + 4];\n"
" vec3 p_three = vec3(position) * s;\n"
" vec4 p_scaled = vec4(p_three, 1.0);\n"
" gl_Position = (p_matrix * v_matrix * m_matrix * (p_scaled + iposv));\n"
" gl_Position = (p_matrix * v_matrix * m_matrix * ((s_matrix * p_scaled) + iposv));\n"
" vertex.color = vec4(iparam[ippos_i + idx * 5], iparam[ippos_i + idx * 5 + 1], iparam[ippos_i + idx * 5 + 2], iparam[ippos_i + idx * 5 + 3]);\n"
" vertex.fragpos = vec3(m_matrix * p_scaled);\n"
" } else {\n"
Expand Down
14 changes: 14 additions & 0 deletions mplot/VisualModelBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,9 @@ namespace mplot
std::function<void(const unsigned int, const std::array<float, 3>&, const float, const float)> insert_instparam_data;
std::function<void(mplot::VisualBase<glver>*)> instanced_needs_update;

//! Set the scaling matrix for all instances
virtual void set_instance_scale (const float scl) = 0;

//! Set up the instance positions (with default params for colour, rotn, scale)
virtual void set_instance_data (const sm::vvec<sm::vec<float, 3>>& position) = 0;

Expand Down Expand Up @@ -908,6 +911,17 @@ namespace mplot
*/
sm::mat<float, 4> scenematrix = {};

/*!
* Instance scaling. This matrix can be used to control the scale of all instances of an
* instanced VisualModel
*/
sm::mat<float, 4> instscale = {};

/*!
* The colour for all instances. This is used if you don't set colour in your instance params.
*/
std::array<float, 3> instcolour = mplot::colour::yellow;

//! Contains the positions within the vbo array of the different vertex buffer objects
enum VBOPos { posnVBO, normVBO, colVBO, idxVBO, numVBO };

Expand Down
12 changes: 11 additions & 1 deletion mplot/VisualModelImplMX.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,15 @@ namespace mplot
model->releaseContext = &mplot::VisualBase<glver>::release_context;
}

void set_instance_scale (const float scl) final
{
this->instscale.set_identity();
this->instscale.scale (scl);
}

void set_instance_data (const sm::vvec<sm::vec<float, 3>>& position) final
{
sm::vvec<std::array<float, 3>> c = { mplot::colour::crimson };
sm::vvec<std::array<float, 3>> c = { this->instcolour };
sm::vvec<float> a = { 1.0f };
sm::vvec<float> s = { 1.0f };
this->set_instance_data (position, c, a, s);
Expand Down Expand Up @@ -301,6 +307,10 @@ namespace mplot
GLint loc_m = _glfn->GetUniformLocation (this->get_gprog(this->parentVis), static_cast<const GLchar*>("m_matrix"));
if (loc_m != -1) { _glfn->UniformMatrix4fv (loc_m, 1, GL_FALSE, this->viewmatrix.arr.data()); }

// the instance scaling matrix (applied to all instances)
GLint loc_s = _glfn->GetUniformLocation (this->get_gprog(this->parentVis), static_cast<const GLchar*>("s_matrix"));
if (loc_s != -1) { _glfn->UniformMatrix4fv (loc_s, 1, GL_FALSE, this->instscale.arr.data()); }

if constexpr (debug_render) {
std::cout << "VisualModel::render: scenematrix:\n" << this->scenematrix << std::endl;
std::cout << "VisualModel::render: model viewmatrix:\n" << this->viewmatrix << std::endl;
Expand Down
8 changes: 7 additions & 1 deletion mplot/VisualModelImplNoMX.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,15 @@ namespace mplot
model->releaseContext = &mplot::VisualBase<glver>::release_context;
}

void set_instance_scale (const float scl) final
{
this->instscale.set_identity();
this->instscale.scale (scl);
}

void set_instance_data (const sm::vvec<sm::vec<float, 3>>& position) final
{
sm::vvec<std::array<float, 3>> c = { mplot::colour::crimson };
sm::vvec<std::array<float, 3>> c = { this->instcolour };
sm::vvec<float> a = { 1.0f };
sm::vvec<float> s = { 1.0f };
this->set_instance_data (position, c, a, s);
Expand Down