SLA slices preview - wip 3

This commit is contained in:
Enrico Turri 2018-11-27 14:50:57 +01:00
parent 21d5acfdd7
commit e6369fe548
10 changed files with 122 additions and 25 deletions

View File

@ -8,10 +8,18 @@ varying vec2 intensity;
varying vec3 delta_box_min; varying vec3 delta_box_min;
varying vec3 delta_box_max; varying vec3 delta_box_max;
varying float world_z;
uniform vec4 uniform_color; uniform vec4 uniform_color;
// x = min z, y = max z;
uniform vec2 z_range;
void main() void main()
{ {
if ((world_z < z_range.x) || (z_range.y < world_z))
discard;
// if the fragment is outside the print volume -> use darker color // if the fragment is outside the print volume -> use darker color
vec3 color = (any(lessThan(delta_box_min, ZERO)) || any(greaterThan(delta_box_max, ZERO))) ? mix(uniform_color.rgb, ZERO, 0.3333) : uniform_color.rgb; vec3 color = (any(lessThan(delta_box_min, ZERO)) || any(greaterThan(delta_box_max, ZERO))) ? mix(uniform_color.rgb, ZERO, 0.3333) : uniform_color.rgb;
gl_FragColor = vec4(vec3(intensity.y, intensity.y, intensity.y) + color * intensity.x, uniform_color.a); gl_FragColor = vec4(vec3(intensity.y, intensity.y, intensity.y) + color * intensity.x, uniform_color.a);

View File

@ -34,6 +34,8 @@ varying vec2 intensity;
varying vec3 delta_box_min; varying vec3 delta_box_min;
varying vec3 delta_box_max; varying vec3 delta_box_max;
varying float world_z;
void main() void main()
{ {
// First transform the normal into camera space and normalize the result. // First transform the normal into camera space and normalize the result.
@ -67,4 +69,5 @@ void main()
} }
gl_Position = ftransform(); gl_Position = ftransform();
world_z = vec3(print_box.volume_world_matrix * gl_Vertex).z;
} }

View File

@ -942,6 +942,7 @@ void GLVolumeCollection::render_VBOs() const
GLint current_program_id; GLint current_program_id;
::glGetIntegerv(GL_CURRENT_PROGRAM, &current_program_id); ::glGetIntegerv(GL_CURRENT_PROGRAM, &current_program_id);
GLint color_id = (current_program_id > 0) ? glGetUniformLocation(current_program_id, "uniform_color") : -1; GLint color_id = (current_program_id > 0) ? glGetUniformLocation(current_program_id, "uniform_color") : -1;
GLint z_range_id = (current_program_id > 0) ? glGetUniformLocation(current_program_id, "z_range") : -1;
GLint print_box_min_id = (current_program_id > 0) ? glGetUniformLocation(current_program_id, "print_box.min") : -1; GLint print_box_min_id = (current_program_id > 0) ? glGetUniformLocation(current_program_id, "print_box.min") : -1;
GLint print_box_max_id = (current_program_id > 0) ? glGetUniformLocation(current_program_id, "print_box.max") : -1; GLint print_box_max_id = (current_program_id > 0) ? glGetUniformLocation(current_program_id, "print_box.max") : -1;
GLint print_box_detection_id = (current_program_id > 0) ? glGetUniformLocation(current_program_id, "print_box.volume_detection") : -1; GLint print_box_detection_id = (current_program_id > 0) ? glGetUniformLocation(current_program_id, "print_box.volume_detection") : -1;
@ -953,6 +954,9 @@ void GLVolumeCollection::render_VBOs() const
if (print_box_max_id != -1) if (print_box_max_id != -1)
::glUniform3fv(print_box_max_id, 1, (const GLfloat*)print_box_max); ::glUniform3fv(print_box_max_id, 1, (const GLfloat*)print_box_max);
if (z_range_id != -1)
::glUniform2fv(z_range_id, 1, (const GLfloat*)z_range);
for (GLVolume *volume : this->volumes) for (GLVolume *volume : this->volumes)
{ {
if (volume->layer_height_texture_data.can_use()) if (volume->layer_height_texture_data.can_use())

View File

@ -506,6 +506,9 @@ class GLVolumeCollection
float print_box_min[3]; float print_box_min[3];
float print_box_max[3]; float print_box_max[3];
// z range for clipping in shaders
float z_range[2];
public: public:
GLVolumePtrs volumes; GLVolumePtrs volumes;
@ -564,6 +567,8 @@ public:
print_box_max[0] = max_x; print_box_max[1] = max_y; print_box_max[2] = max_z; print_box_max[0] = max_x; print_box_max[1] = max_y; print_box_max[2] = max_z;
} }
void set_z_range(float min_z, float max_z) { z_range[0] = min_z; z_range[1] = max_z; }
// returns true if all the volumes are completely contained in the print volume // returns true if all the volumes are completely contained in the print volume
// returns the containment state in the given out_state, if non-null // returns the containment state in the given out_state, if non-null
bool check_outside_state(const DynamicPrintConfig* config, ModelInstance::EPrintVolumeState* out_state); bool check_outside_state(const DynamicPrintConfig* config, ModelInstance::EPrintVolumeState* out_state);

View File

@ -3312,6 +3312,7 @@ GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas)
: m_canvas(canvas) : m_canvas(canvas)
, m_context(nullptr) , m_context(nullptr)
, m_toolbar(*this) , m_toolbar(*this)
, m_use_clipping_planes(false)
, m_config(nullptr) , m_config(nullptr)
, m_process(nullptr) , m_process(nullptr)
, m_model(nullptr) , m_model(nullptr)
@ -3755,6 +3756,12 @@ void GLCanvas3D::render()
_camera_tranform(); _camera_tranform();
if (m_use_clipping_planes)
{
::glClipPlane(GL_CLIP_PLANE0, (GLdouble*)m_clipping_planes[0].get_data());
::glClipPlane(GL_CLIP_PLANE1, (GLdouble*)m_clipping_planes[1].get_data());
}
GLfloat position_cam[4] = { 1.0f, 0.0f, 1.0f, 0.0f }; GLfloat position_cam[4] = { 1.0f, 0.0f, 1.0f, 0.0f };
::glLightfv(GL_LIGHT1, GL_POSITION, position_cam); ::glLightfv(GL_LIGHT1, GL_POSITION, position_cam);
GLfloat position_top[4] = { -0.5f, -0.5f, 1.0f, 0.0f }; GLfloat position_top[4] = { -0.5f, -0.5f, 1.0f, 0.0f };
@ -5694,6 +5701,11 @@ void GLCanvas3D::_render_objects() const
::glDisable(GL_CULL_FACE); ::glDisable(GL_CULL_FACE);
} }
if (m_use_clipping_planes)
m_volumes.set_z_range(m_clipping_planes[0].get_data()[3], m_clipping_planes[1].get_data()[3]);
else
m_volumes.set_z_range(-FLT_MAX, FLT_MAX);
m_shader.start_using(); m_shader.start_using();
m_volumes.render_VBOs(); m_volumes.render_VBOs();
m_shader.stop_using(); m_shader.stop_using();
@ -5703,6 +5715,12 @@ void GLCanvas3D::_render_objects() const
} }
else else
{ {
if (m_use_clipping_planes)
{
::glEnable(GL_CLIP_PLANE0);
::glEnable(GL_CLIP_PLANE1);
}
// do not cull backfaces to show broken geometry, if any // do not cull backfaces to show broken geometry, if any
if (m_picking_enabled) if (m_picking_enabled)
::glDisable(GL_CULL_FACE); ::glDisable(GL_CULL_FACE);
@ -5711,6 +5729,12 @@ void GLCanvas3D::_render_objects() const
if (m_picking_enabled) if (m_picking_enabled)
::glEnable(GL_CULL_FACE); ::glEnable(GL_CULL_FACE);
if (m_use_clipping_planes)
{
::glDisable(GL_CLIP_PLANE0);
::glDisable(GL_CLIP_PLANE1);
}
} }
::glDisable(GL_LIGHTING); ::glDisable(GL_LIGHTING);

View File

@ -556,6 +556,31 @@ public:
#endif // ENABLE_ENSURE_ON_BED_WHILE_SCALING #endif // ENABLE_ENSURE_ON_BED_WHILE_SCALING
}; };
class ClippingPlane
{
double m_data[4];
public:
ClippingPlane()
{
m_data[0] = 0.0;
m_data[1] = 0.0;
m_data[2] = 1.0;
m_data[3] = 0.0;
}
ClippingPlane(const Vec3d& direction, double offset)
{
Vec3d norm_dir = direction.normalized();
m_data[0] = norm_dir(0);
m_data[1] = norm_dir(1);
m_data[2] = norm_dir(2);
m_data[3] = offset;
}
const double* get_data() const { return m_data; }
};
private: private:
class Gizmos class Gizmos
{ {
@ -703,6 +728,8 @@ private:
Mouse m_mouse; Mouse m_mouse;
mutable Gizmos m_gizmos; mutable Gizmos m_gizmos;
mutable GLToolbar m_toolbar; mutable GLToolbar m_toolbar;
ClippingPlane m_clipping_planes[2];
bool m_use_clipping_planes;
mutable GLVolumeCollection m_volumes; mutable GLVolumeCollection m_volumes;
Selection m_selection; Selection m_selection;
@ -778,6 +805,13 @@ public:
void set_axes_length(float length); void set_axes_length(float length);
void set_clipping_plane(unsigned int id, const ClippingPlane& plane)
{
if (id < 2)
m_clipping_planes[id] = plane;
}
void set_use_clipping_planes(bool use) { m_use_clipping_planes = use; }
void set_color_by(const std::string& value); void set_color_by(const std::string& value);
float get_camera_zoom() const; float get_camera_zoom() const;

View File

@ -391,11 +391,7 @@ void Preview::create_double_slider()
// sizer, m_canvas_widget // sizer, m_canvas_widget
m_canvas_widget->Bind(wxEVT_KEY_DOWN, &Preview::update_double_slider_from_canvas, this); m_canvas_widget->Bind(wxEVT_KEY_DOWN, &Preview::update_double_slider_from_canvas, this);
m_slider->Bind(wxEVT_SCROLL_CHANGED, [this](wxEvent& event) { m_slider->Bind(wxEVT_SCROLL_CHANGED, &Preview::on_sliders_scroll_changed, this);
m_canvas->set_toolpaths_range(m_slider->GetLowerValueD() - 1e-6, m_slider->GetHigherValueD() + 1e-6);
if (IsShown())
m_canvas_widget->Refresh();
});
Bind(wxCUSTOMEVT_TICKSCHANGED, [this](wxEvent&) { Bind(wxCUSTOMEVT_TICKSCHANGED, [this](wxEvent&) {
auto& config = wxGetApp().preset_bundle->project_config; auto& config = wxGetApp().preset_bundle->project_config;
@ -409,10 +405,10 @@ void Preview::update_double_slider(const std::vector<double>& layers_z, bool for
std::vector<std::pair<int, double>> values; std::vector<std::pair<int, double>> values;
fill_slider_values(values, layers_z); fill_slider_values(values, layers_z);
const double z_low = m_slider->GetLowerValueD();
const double z_high = m_slider->GetHigherValueD();
m_slider->SetMaxValue(layers_z.size() - 1); m_slider->SetMaxValue(layers_z.size() - 1);
m_slider->SetSliderValues(values); m_slider->SetSliderValues(values);
const double z_low = m_slider->GetLowerValueD();
const double z_high = m_slider->GetHigherValueD();
const auto& config = wxGetApp().preset_bundle->project_config; const auto& config = wxGetApp().preset_bundle->project_config;
const std::vector<double> &ticks_from_config = (config.option<ConfigOptionFloats>("colorprint_heights"))->values; const std::vector<double> &ticks_from_config = (config.option<ConfigOptionFloats>("colorprint_heights"))->values;
@ -425,19 +421,10 @@ void Preview::update_double_slider(const std::vector<double>& layers_z, bool for
void Preview::fill_slider_values(std::vector<std::pair<int, double>> &values, void Preview::fill_slider_values(std::vector<std::pair<int, double>> &values,
const std::vector<double> &layers_z) const std::vector<double> &layers_z)
{ {
std::vector<double> layers_all_z = m_canvas->get_current_print_zs(false); values.clear();
if (layers_all_z.size() == layers_z.size()) for (int i = 0; i < layers_z.size(); ++i)
for (int i = 0; i < layers_z.size(); i++) {
values.push_back(std::pair<int, double>(i + 1, layers_z[i])); values.push_back(std::pair<int, double>(i + 1, layers_z[i]));
else if (layers_all_z.size() > layers_z.size()) {
int cur_id = 0;
for (int i = 0; i < layers_z.size(); i++)
for (int j = cur_id; j < layers_all_z.size(); j++)
if (layers_z[i] - 1e-6 < layers_all_z[j] && layers_all_z[j] < layers_z[i] + 1e-6) {
values.push_back(std::pair<int, double>(j + 1, layers_z[i]));
cur_id = j;
break;
}
} }
// All ticks that would end up outside the slider range should be erased. // All ticks that would end up outside the slider range should be erased.
@ -626,12 +613,13 @@ void Preview::load_print_as_sla()
std::set<float> zs; std::set<float> zs;
for (const SLAPrintObject* obj : print->objects()) for (const SLAPrintObject* obj : print->objects())
{ {
double shift_z = obj->get_current_elevation();
if (obj->is_step_done(slaposIndexSlices)) if (obj->is_step_done(slaposIndexSlices))
{ {
const SLAPrintObject::SliceIndex& index = obj->get_slice_index(); const SLAPrintObject::SliceIndex& index = obj->get_slice_index();
for (const SLAPrintObject::SliceIndex::value_type& id : index) for (const SLAPrintObject::SliceIndex::value_type& id : index)
{ {
zs.insert(id.second.scale_back(id.first)); zs.insert(shift_z + id.second.scale_back(id.first));
} }
} }
} }
@ -645,7 +633,6 @@ void Preview::load_print_as_sla()
if (IsShown()) if (IsShown())
{ {
std::cout << "Preview::load_print_as_sla()" << std::endl;
m_canvas->load_sla_preview(); m_canvas->load_sla_preview();
show_hide_ui_elements("none"); show_hide_ui_elements("none");
@ -660,5 +647,26 @@ void Preview::load_print_as_sla()
} }
} }
void Preview::on_sliders_scroll_changed(wxEvent& event)
{
if (IsShown())
{
PrinterTechnology tech = m_process->current_printer_technology();
if (tech == ptFFF)
{
m_canvas->set_toolpaths_range(m_slider->GetLowerValueD() - 1e-6, m_slider->GetHigherValueD() + 1e-6);
m_canvas_widget->Refresh();
m_canvas->set_use_clipping_planes(false);
}
else if (tech == ptSLA)
{
m_canvas->set_clipping_plane(0, GLCanvas3D::ClippingPlane(Vec3d(0.0, 0.0, 1.0), m_slider->GetLowerValueD() - 1e-6));
m_canvas->set_clipping_plane(1, GLCanvas3D::ClippingPlane(Vec3d(0.0, 0.0, -1.0), m_slider->GetHigherValueD() + 1e-6));
m_canvas->set_use_clipping_planes(true);
m_canvas_widget->Refresh();
}
}
}
} // namespace GUI } // namespace GUI
} // namespace Slic3r } // namespace Slic3r

View File

@ -110,6 +110,9 @@ private:
void load_print_as_fff(); void load_print_as_fff();
void load_print_as_sla(); void load_print_as_sla();
void on_sliders_scroll_changed(wxEvent& event);
}; };
} // namespace GUI } // namespace GUI

View File

@ -1889,6 +1889,7 @@ void Plater::priv::update_sla_scene()
this->schedule_background_process(); this->schedule_background_process();
this->canvas3D->reload_scene(true); this->canvas3D->reload_scene(true);
delayed_scene_refresh = false; delayed_scene_refresh = false;
this->preview->reload_print();
} }
void Plater::priv::reload_from_disk() void Plater::priv::reload_from_disk()

View File

@ -1854,6 +1854,8 @@ void PrusaDoubleSlider::correct_higher_value()
void PrusaDoubleSlider::OnMotion(wxMouseEvent& event) void PrusaDoubleSlider::OnMotion(wxMouseEvent& event)
{ {
bool action = false;
const wxClientDC dc(this); const wxClientDC dc(this);
const wxPoint pos = event.GetLogicalPosition(dc); const wxPoint pos = event.GetLogicalPosition(dc);
m_is_one_layer_icon_focesed = is_point_in_rect(pos, m_rect_one_layer_icon); m_is_one_layer_icon_focesed = is_point_in_rect(pos, m_rect_one_layer_icon);
@ -1864,19 +1866,24 @@ void PrusaDoubleSlider::OnMotion(wxMouseEvent& event)
if (m_selection == ssLower) { if (m_selection == ssLower) {
m_lower_value = get_value_from_position(pos.x, pos.y); m_lower_value = get_value_from_position(pos.x, pos.y);
correct_lower_value(); correct_lower_value();
action = true;
} }
else if (m_selection == ssHigher) { else if (m_selection == ssHigher) {
m_higher_value = get_value_from_position(pos.x, pos.y); m_higher_value = get_value_from_position(pos.x, pos.y);
correct_higher_value(); correct_higher_value();
action = true;
} }
} }
Refresh(); Refresh();
Update(); Update();
event.Skip(); event.Skip();
wxCommandEvent e(wxEVT_SCROLL_CHANGED); if (action)
e.SetEventObject(this); {
ProcessWindowEvent(e); wxCommandEvent e(wxEVT_SCROLL_CHANGED);
e.SetEventObject(this);
ProcessWindowEvent(e);
}
} }
void PrusaDoubleSlider::OnLeftUp(wxMouseEvent& event) void PrusaDoubleSlider::OnLeftUp(wxMouseEvent& event)