FIX: GLVolume: fix the crash under CLI when load object with lod_enabled

add log_enabled param when loading volumes
JIRA: no-jira

Change-Id: I77806622f6b9c5abc723adc389fbb7bd8dc414c5
(cherry picked from commit b2513b139547dfcce0f277fba9ec3d621f09bfa5)
This commit is contained in:
lane.wei 2024-08-09 15:48:09 +08:00 committed by Lane.Wei
parent 065dddb890
commit e0b5be5416
7 changed files with 37 additions and 24 deletions

View File

@ -4969,7 +4969,7 @@ int CLI::run(int argc, char **argv)
// continue;
for (int instance_idx = 0; instance_idx < (int)model_object.instances.size(); ++ instance_idx) {
const ModelInstance &model_instance = *model_object.instances[instance_idx];
glvolume_collection.load_object_volume(&model_object, obj_idx, volume_idx, instance_idx, "volume", true, false, true);
glvolume_collection.load_object_volume(&model_object, obj_idx, volume_idx, instance_idx, "volume", true, false, true, false);
//glvolume_collection.volumes.back()->geometry_id = key.geometry_id;
std::string color = filament_color?filament_color->get_at(volume_extruder_id - 1):"#00FF00FF";
@ -5843,7 +5843,7 @@ int CLI::run(int argc, char **argv)
// continue;
for (int instance_idx = 0; instance_idx < (int)model_object.instances.size(); ++ instance_idx) {
const ModelInstance &model_instance = *model_object.instances[instance_idx];
glvolume_collection.load_object_volume(&model_object, obj_idx, volume_idx, instance_idx, "volume", true, false, true);
glvolume_collection.load_object_volume(&model_object, obj_idx, volume_idx, instance_idx, "volume", true, false, true, false);
//glvolume_collection.volumes.back()->geometry_id = key.geometry_id;
std::string color = filament_color?filament_color->get_at(volume_extruder_id - 1):"#00FF00FF";

View File

@ -632,10 +632,6 @@ bool GLVolume::simplify_mesh(const TriangleMesh &mesh, GLIndexedVertexArray &va,
bool GLVolume::simplify_mesh(const indexed_triangle_set &_its, GLIndexedVertexArray &va, LOD_LEVEL lod) const
{
if (_its.indices.size() == 0 || _its.vertices.size() == 0) { return false; }
bool enable_lod = GUI::wxGetApp().app_config->get("enable_lod") == "true";
if (!enable_lod) {
return false;
}
auto its = std::make_unique<indexed_triangle_set>(_its);
auto m_state = std::make_unique<State>();
if (lod == LOD_LEVEL::MIDDLE) {
@ -1244,12 +1240,13 @@ std::vector<int> GLVolumeCollection::load_object(
int obj_idx,
const std::vector<int> &instance_idxs,
const std::string &color_by,
bool opengl_initialized)
bool opengl_initialized,
bool lod_enabled)
{
std::vector<int> volumes_idx;
for (int volume_idx = 0; volume_idx < int(model_object->volumes.size()); ++volume_idx)
for (int instance_idx : instance_idxs)
volumes_idx.emplace_back(this->GLVolumeCollection::load_object_volume(model_object, obj_idx, volume_idx, instance_idx, color_by, opengl_initialized));
volumes_idx.emplace_back(this->GLVolumeCollection::load_object_volume(model_object, obj_idx, volume_idx, instance_idx, color_by, opengl_initialized, false, lod_enabled));
return volumes_idx;
}
@ -1261,7 +1258,8 @@ int GLVolumeCollection::load_object_volume(
const std::string &color_by,
bool opengl_initialized,
bool in_assemble_view,
bool use_loaded_id)
bool use_loaded_id,
bool lod_enabled)
{
const ModelVolume *model_volume = model_object->volumes[volume_idx];
const int extruder_id = model_volume->extruder_id();
@ -1306,10 +1304,21 @@ int GLVolumeCollection::load_object_volume(
#if ENABLE_SMOOTH_NORMALS
v.indexed_vertex_array->load_mesh(mesh, true);
#else
if (v.indexed_vertex_array_middle == nullptr) { v.indexed_vertex_array_middle = std::make_shared<GLIndexedVertexArray>(); }
v.simplify_mesh(mesh, *v.indexed_vertex_array_middle, LOD_LEVEL::MIDDLE); // include finalize_geometry
if (v.indexed_vertex_array_small == nullptr) { v.indexed_vertex_array_small = std::make_shared<GLIndexedVertexArray>(); }
v.simplify_mesh(mesh, *v.indexed_vertex_array_small, LOD_LEVEL::SMALL);
if (lod_enabled) {
if (v.indexed_vertex_array_middle == nullptr)
{
v.indexed_vertex_array_middle = std::make_shared<GLIndexedVertexArray>();
}
v.simplify_mesh(mesh, *v.indexed_vertex_array_middle, LOD_LEVEL::MIDDLE); // include finalize_geometry
if (v.indexed_vertex_array_small == nullptr)
{
v.indexed_vertex_array_small = std::make_shared<GLIndexedVertexArray>();
}
v.simplify_mesh(mesh, *v.indexed_vertex_array_small, LOD_LEVEL::SMALL);
}
v.indexed_vertex_array->load_mesh(mesh);
#endif // ENABLE_SMOOTH_NORMALS
v.indexed_vertex_array->finalize_geometry(opengl_initialized);

View File

@ -673,7 +673,8 @@ public:
int obj_idx,
const std::vector<int> &instance_idxs,
const std::string &color_by,
bool opengl_initialized);
bool opengl_initialized,
bool lod_enabled);
int load_object_volume(
const ModelObject *model_object,
@ -683,7 +684,8 @@ public:
const std::string &color_by,
bool opengl_initialized,
bool in_assemble_view = false,
bool use_loaded_id = false);
bool use_loaded_id = false,
bool lod_enabled = true);
// Load SLA auxiliary GLVolumes (for support trees or pad).
void load_object_auxiliary(

View File

@ -3167,6 +3167,7 @@ void GCodeViewer::load_shells(const Print& print, bool initialized, bool force_p
// BBS: fix the issue that object_idx is not assigned as index of Model.objects array
int object_count = 0;
const ModelObjectPtrs& model_objs = wxGetApp().model().objects;
bool enable_lod = GUI::wxGetApp().app_config->get("enable_lod") == "true";
for (const PrintObject* obj : print.objects()) {
const ModelObject* model_obj = obj->model_object();
@ -3193,7 +3194,7 @@ void GCodeViewer::load_shells(const Print& print, bool initialized, bool force_p
instance_ids.resize(instance_index);
size_t current_volumes_count = m_shells.volumes.volumes.size();
m_shells.volumes.load_object(model_obj, object_idx, instance_ids, "object", initialized);
m_shells.volumes.load_object(model_obj, object_idx, instance_ids, "object", initialized, enable_lod);
// adjust shells' z if raft is present
const SlicingParameters& slicing_parameters = obj->slicing_parameters();

View File

@ -2253,22 +2253,22 @@ void GLCanvas3D::set_volumes_z_range(const std::array<double, 2>& range)
m_volumes.set_range(range[0] - 1e-6, range[1] + 1e-6);
}
std::vector<int> GLCanvas3D::load_object(const ModelObject& model_object, int obj_idx, std::vector<int> instance_idxs)
std::vector<int> GLCanvas3D::load_object(const ModelObject& model_object, int obj_idx, std::vector<int> instance_idxs, bool lod_enabled)
{
if (instance_idxs.empty()) {
for (unsigned int i = 0; i < model_object.instances.size(); ++i) {
instance_idxs.emplace_back(i);
}
}
return m_volumes.load_object(&model_object, obj_idx, instance_idxs, m_color_by, m_initialized);
return m_volumes.load_object(&model_object, obj_idx, instance_idxs, m_color_by, m_initialized, lod_enabled);
}
std::vector<int> GLCanvas3D::load_object(const Model& model, int obj_idx)
std::vector<int> GLCanvas3D::load_object(const Model& model, int obj_idx, bool lod_enabled)
{
if (0 <= obj_idx && obj_idx < (int)model.objects.size()) {
const ModelObject* model_object = model.objects[obj_idx];
if (model_object != nullptr)
return load_object(*model_object, obj_idx, std::vector<int>());
return load_object(*model_object, obj_idx, std::vector<int>(), lod_enabled);
}
return std::vector<int>();
@ -2556,6 +2556,7 @@ void GLCanvas3D::reload_scene(bool refresh_immediately, bool force_full_scene_re
}
}
m_volumes.volumes = std::move(glvolumes_new);
bool enable_lod = GUI::wxGetApp().app_config->get("enable_lod") == "true";
for (unsigned int obj_idx = 0; obj_idx < (unsigned int)m_model->objects.size(); ++ obj_idx) {
const ModelObject &model_object = *m_model->objects[obj_idx];
for (int volume_idx = 0; volume_idx < (int)model_object.volumes.size(); ++ volume_idx) {
@ -2576,7 +2577,7 @@ void GLCanvas3D::reload_scene(bool refresh_immediately, bool force_full_scene_re
// Note the index of the loaded volume, so that we can reload the main model GLVolume with the hollowed mesh
// later in this function.
it->volume_idx = m_volumes.volumes.size();
m_volumes.load_object_volume(&model_object, obj_idx, volume_idx, instance_idx, m_color_by, m_initialized, m_canvas_type == ECanvasType::CanvasAssembleView);
m_volumes.load_object_volume(&model_object, obj_idx, volume_idx, instance_idx, m_color_by, m_initialized, m_canvas_type == ECanvasType::CanvasAssembleView, false, enable_lod);
m_volumes.volumes.back()->geometry_id = key.geometry_id;
update_object_list = true;
} else {

View File

@ -911,8 +911,8 @@ public:
std::vector<CustomGCode::Item>& get_custom_gcode_per_print_z() { return m_gcode_viewer.get_custom_gcode_per_print_z(); }
size_t get_gcode_extruders_count() { return m_gcode_viewer.get_extruders_count(); }
std::vector<int> load_object(const ModelObject& model_object, int obj_idx, std::vector<int> instance_idxs);
std::vector<int> load_object(const Model& model, int obj_idx);
std::vector<int> load_object(const ModelObject& model_object, int obj_idx, std::vector<int> instance_idxs, bool lod_enabled);
std::vector<int> load_object(const Model& model, int obj_idx, bool lod_enabled);
void mirror_selection(Axis axis);

View File

@ -1125,7 +1125,7 @@ bool CalibUtils::process_and_store_3mf(Model *model, const DynamicPrintConfig &f
const ModelVolume &model_volume = *model_object.volumes[volume_idx];
for (int instance_idx = 0; instance_idx < (int)model_object.instances.size(); ++ instance_idx) {
const ModelInstance &model_instance = *model_object.instances[instance_idx];
glvolume_collection.load_object_volume(&model_object, obj_idx, volume_idx, instance_idx, "volume", true, false, true);
glvolume_collection.load_object_volume(&model_object, obj_idx, volume_idx, instance_idx, "volume", true, false, true, false);
glvolume_collection.volumes.back()->set_render_color( new_color[0], new_color[1], new_color[2], new_color[3]);
glvolume_collection.volumes.back()->set_color(new_color);
//glvolume_collection.volumes.back()->printable = model_instance.printable;