GCodeViewer -> extrusion paths colored by extrusion role

This commit is contained in:
enricoturri1966 2020-04-16 15:09:04 +02:00
parent 61ab7bbebf
commit 75d1e8373d
5 changed files with 105 additions and 34 deletions

View File

@ -1,4 +1,4 @@
#include "../libslic3r.h" #include "libslic3r/libslic3r.h"
#include "GCodeProcessor.hpp" #include "GCodeProcessor.hpp"
#include <boost/log/trivial.hpp> #include <boost/log/trivial.hpp>

View File

@ -2,9 +2,9 @@
#define slic3r_GCodeProcessor_hpp_ #define slic3r_GCodeProcessor_hpp_
#if ENABLE_GCODE_VIEWER #if ENABLE_GCODE_VIEWER
#include "../GCodeReader.hpp" #include "libslic3r/GCodeReader.hpp"
#include "../Point.hpp" #include "libslic3r/Point.hpp"
#include "../ExtrusionEntity.hpp" #include "libslic3r/ExtrusionEntity.hpp"
#include <array> #include <array>
#include <vector> #include <vector>

View File

@ -43,8 +43,44 @@ void GCodeViewer::IBuffer::reset()
// release cpu memory // release cpu memory
data = std::vector<unsigned int>(); data = std::vector<unsigned int>();
data_size = 0; data_size = 0;
paths = std::vector<Path>();
} }
bool GCodeViewer::IBuffer::init_shader(const std::string& vertex_shader_src, const std::string& fragment_shader_src)
{
if (!shader.init(vertex_shader_src, fragment_shader_src))
{
BOOST_LOG_TRIVIAL(error) << "Unable to initialize toolpaths shader: please, check that the files " << vertex_shader_src << " and " << fragment_shader_src << " are available";
return false;
}
return true;
}
void GCodeViewer::IBuffer::add_path(GCodeProcessor::EMoveType type, ExtrusionRole role)
{
unsigned int id = static_cast<unsigned int>(data.size());
paths.push_back({ type, role, id, id });
}
const std::array<std::array<float, 4>, erCount> GCodeViewer::Default_Extrusion_Role_Colors {{
{ 0.00f, 0.00f, 0.00f, 1.0f }, // erNone
{ 1.00f, 1.00f, 0.40f, 1.0f }, // erPerimeter
{ 1.00f, 0.65f, 0.00f, 1.0f }, // erExternalPerimeter
{ 0.00f, 0.00f, 1.00f, 1.0f }, // erOverhangPerimeter
{ 0.69f, 0.19f, 0.16f, 1.0f }, // erInternalInfill
{ 0.84f, 0.20f, 0.84f, 1.0f }, // erSolidInfill
{ 1.00f, 0.10f, 0.10f, 1.0f }, // erTopSolidInfill
{ 0.60f, 0.60f, 1.00f, 1.0f }, // erBridgeInfill
{ 1.00f, 1.00f, 1.00f, 1.0f }, // erGapFill
{ 0.52f, 0.48f, 0.13f, 1.0f }, // erSkirt
{ 0.00f, 1.00f, 0.00f, 1.0f }, // erSupportMaterial
{ 0.00f, 0.50f, 0.00f, 1.0f }, // erSupportMaterialInterface
{ 0.70f, 0.89f, 0.67f, 1.0f }, // erWipeTower
{ 0.16f, 0.80f, 0.58f, 1.0f }, // erCustom
{ 0.00f, 0.00f, 0.00f, 1.0f } // erMixed
}};
void GCodeViewer::load(const GCodeProcessor::Result& gcode_result, const Print& print, bool initialized) void GCodeViewer::load(const GCodeProcessor::Result& gcode_result, const Print& print, bool initialized)
{ {
if (m_last_result_id == gcode_result.id) if (m_last_result_id == gcode_result.id)
@ -99,40 +135,41 @@ bool GCodeViewer::init_shaders()
for (unsigned char i = begin_id; i < end_id; ++i) for (unsigned char i = begin_id; i < end_id; ++i)
{ {
Shader& shader = m_buffers[i].shader; switch (buffer_type(i))
std::string vertex_shader_src;
std::string fragment_shader_src;
GCodeProcessor::EMoveType type = buffer_type(i);
switch (type)
{ {
case GCodeProcessor::EMoveType::Tool_change: case GCodeProcessor::EMoveType::Tool_change:
{ {
vertex_shader_src = "toolchanges.vs"; if (!m_buffers[i].init_shader("toolchanges.vs", "toolchanges.fs"))
fragment_shader_src = "toolchanges.fs"; return false;
break; break;
} }
case GCodeProcessor::EMoveType::Retract: case GCodeProcessor::EMoveType::Retract:
{ {
vertex_shader_src = "retractions.vs"; if (!m_buffers[i].init_shader("retractions.vs", "retractions.fs"))
fragment_shader_src = "retractions.fs"; return false;
break; break;
} }
case GCodeProcessor::EMoveType::Unretract: case GCodeProcessor::EMoveType::Unretract:
{ {
vertex_shader_src = "unretractions.vs"; if (!m_buffers[i].init_shader("unretractions.vs", "unretractions.fs"))
fragment_shader_src = "unretractions.fs"; return false;
break; break;
} }
case GCodeProcessor::EMoveType::Extrude: case GCodeProcessor::EMoveType::Extrude:
{ {
vertex_shader_src = "extrusions.vs"; if (!m_buffers[i].init_shader("extrusions.vs", "extrusions.fs"))
fragment_shader_src = "extrusions.fs"; return false;
break; break;
} }
case GCodeProcessor::EMoveType::Travel: case GCodeProcessor::EMoveType::Travel:
{ {
vertex_shader_src = "travels.vs"; if (!m_buffers[i].init_shader("travels.vs", "travels.fs"))
fragment_shader_src = "travels.fs"; return false;
break; break;
} }
default: default:
@ -140,12 +177,6 @@ bool GCodeViewer::init_shaders()
break; break;
} }
} }
if (!shader.init(vertex_shader_src, fragment_shader_src))
{
BOOST_LOG_TRIVIAL(error) << "Unable to initialize toolpaths shader: please, check that the files " << vertex_shader_src << " and " << fragment_shader_src << " are available";
return false;
}
} }
if (!m_shells.shader.init("shells.vs", "shells.fs")) if (!m_shells.shader.init("shells.vs", "shells.fs"))
@ -203,13 +234,20 @@ void GCodeViewer::load_toolpaths(const GCodeProcessor::Result& gcode_result)
case GCodeProcessor::EMoveType::Retract: case GCodeProcessor::EMoveType::Retract:
case GCodeProcessor::EMoveType::Unretract: case GCodeProcessor::EMoveType::Unretract:
{ {
buffer.add_path(curr.type, curr.extrusion_role);
buffer.data.push_back(static_cast<unsigned int>(i)); buffer.data.push_back(static_cast<unsigned int>(i));
break; break;
} }
case GCodeProcessor::EMoveType::Extrude: case GCodeProcessor::EMoveType::Extrude:
case GCodeProcessor::EMoveType::Travel: case GCodeProcessor::EMoveType::Travel:
{ {
buffer.data.push_back(static_cast<unsigned int>(i - 1)); if (prev.type != curr.type)
{
buffer.add_path(curr.type, curr.extrusion_role);
buffer.data.push_back(static_cast<unsigned int>(i - 1));
}
buffer.paths.back().last = static_cast<unsigned int>(buffer.data.size());
buffer.data.push_back(static_cast<unsigned int>(i)); buffer.data.push_back(static_cast<unsigned int>(i));
break; break;
} }
@ -387,16 +425,26 @@ void GCodeViewer::render_toolpaths() const
} }
case GCodeProcessor::EMoveType::Extrude: case GCodeProcessor::EMoveType::Extrude:
{ {
std::array<float, 4> color = { 1.0f, 0.0f, 0.0f, 1.0f }; for (const Path& path : buffer.paths)
set_color(current_program_id, color); {
glsafe(::glDrawElements(GL_LINES, (GLsizei)buffer.data_size, GL_UNSIGNED_INT, nullptr)); unsigned int color_id = static_cast<unsigned int>(path.role);
if (color_id >= erCount)
color_id = 0;
set_color(current_program_id, m_extrusion_role_colors[color_id]);
glsafe(::glDrawElements(GL_LINE_STRIP, GLsizei(path.last - path.first + 1), GL_UNSIGNED_INT, (const void*)(path.first * sizeof(GLuint))));
}
break; break;
} }
case GCodeProcessor::EMoveType::Travel: case GCodeProcessor::EMoveType::Travel:
{ {
std::array<float, 4> color = { 1.0f, 1.0f, 0.0f, 1.0f }; std::array<float, 4> color = { 1.0f, 1.0f, 0.0f, 1.0f };
set_color(current_program_id, color); set_color(current_program_id, color);
glsafe(::glDrawElements(GL_LINES, (GLsizei)buffer.data_size, GL_UNSIGNED_INT, nullptr)); for (const Path& path : buffer.paths)
{
glsafe(::glDrawElements(GL_LINE_STRIP, GLsizei(path.last - path.first + 1), GL_UNSIGNED_INT, (const void*)(path.first * sizeof(GLuint))));
}
break; break;
} }
} }

View File

@ -7,14 +7,14 @@
#include "3DScene.hpp" #include "3DScene.hpp"
#include "libslic3r/GCode/GCodeProcessor.hpp" #include "libslic3r/GCode/GCodeProcessor.hpp"
#include <vector>
namespace Slic3r { namespace Slic3r {
class Print; class Print;
namespace GUI { namespace GUI {
class GCodeViewer class GCodeViewer
{ {
static const std::array<std::array<float, 4>, erCount> Default_Extrusion_Role_Colors;
// buffer containing vertices data // buffer containing vertices data
struct VBuffer struct VBuffer
{ {
@ -29,16 +29,30 @@ class GCodeViewer
static size_t vertex_size_bytes() { return vertex_size() * sizeof(float); } static size_t vertex_size_bytes() { return vertex_size() * sizeof(float); }
}; };
// buffer containing indices data struct Path
{
GCodeProcessor::EMoveType type{ GCodeProcessor::EMoveType::Noop };
ExtrusionRole role{ erNone };
unsigned int first{ 0 };
unsigned int last{ 0 };
bool matches(GCodeProcessor::EMoveType type, ExtrusionRole role) const { return this->type == type && this->role == role; }
};
// buffer containing indices data and shader for a specific toolpath type
struct IBuffer struct IBuffer
{ {
unsigned int ibo_id{ 0 }; unsigned int ibo_id{ 0 };
Shader shader; Shader shader;
std::vector<unsigned int> data; std::vector<unsigned int> data;
size_t data_size{ 0 }; size_t data_size{ 0 };
std::vector<Path> paths;
bool visible{ false }; bool visible{ false };
void reset(); void reset();
bool init_shader(const std::string& vertex_shader_src, const std::string& fragment_shader_src);
void add_path(GCodeProcessor::EMoveType type, ExtrusionRole role);
}; };
struct Shells struct Shells
@ -55,11 +69,17 @@ class GCodeViewer
std::vector<double> m_layers_zs; std::vector<double> m_layers_zs;
Shells m_shells; Shells m_shells;
std::array<std::array<float, 4>, erCount> m_extrusion_role_colors;
public: public:
GCodeViewer() = default; GCodeViewer() = default;
~GCodeViewer() { reset(); } ~GCodeViewer() { reset(); }
bool init() { set_toolpath_visible(GCodeProcessor::EMoveType::Extrude, true); return init_shaders(); } bool init() {
m_extrusion_role_colors = Default_Extrusion_Role_Colors;
set_toolpath_visible(GCodeProcessor::EMoveType::Extrude, true);
return init_shaders();
}
void load(const GCodeProcessor::Result& gcode_result, const Print& print, bool initialized); void load(const GCodeProcessor::Result& gcode_result, const Print& print, bool initialized);
void reset(); void reset();
void render() const; void render() const;

View File

@ -2140,6 +2140,9 @@ void GLCanvas3D::render()
// we need to set the mouse's scene position here because the depth buffer // we need to set the mouse's scene position here because the depth buffer
// could be invalidated by the following gizmo render methods // could be invalidated by the following gizmo render methods
// this position is used later into on_mouse() to drag the objects // this position is used later into on_mouse() to drag the objects
#if ENABLE_GCODE_VIEWER
if (m_picking_enabled)
#endif // ENABLE_GCODE_VIEWER
m_mouse.scene_position = _mouse_to_3d(m_mouse.position.cast<coord_t>()); m_mouse.scene_position = _mouse_to_3d(m_mouse.position.cast<coord_t>());
_render_current_gizmo(); _render_current_gizmo();