Tech ENABLE_RAYCAST_PICKING set as default

This commit is contained in:
enricoturri1966 2022-12-05 10:24:30 +01:00
parent e04e8c55cf
commit 6f18f8f784
39 changed files with 30 additions and 1046 deletions

View file

@ -1346,13 +1346,10 @@ void GLCanvas3D::toggle_sla_auxiliaries_visibility(bool visible, const ModelObje
m_render_sla_auxiliaries = visible;
#if ENABLE_RAYCAST_PICKING
std::vector<std::shared_ptr<SceneRaycasterItem>>* raycasters = get_raycasters_for_picking(SceneRaycaster::EType::Volume);
#endif // ENABLE_RAYCAST_PICKING
for (GLVolume* vol : m_volumes.volumes) {
#if ENABLE_RAYCAST_PICKING
if ((mo == nullptr || m_model->objects[vol->composite_id.object_id] == mo)
if ((mo == nullptr || m_model->objects[vol->composite_id.object_id] == mo)
&& (instance_idx == -1 || vol->composite_id.instance_id == instance_idx)
&& vol->composite_id.volume_id < 0) {
vol->is_active = visible;
@ -1360,20 +1357,12 @@ void GLCanvas3D::toggle_sla_auxiliaries_visibility(bool visible, const ModelObje
if (it != raycasters->end())
(*it)->set_active(vol->is_active);
}
#else
if ((mo == nullptr || m_model->objects[vol->composite_id.object_id] == mo)
&& (instance_idx == -1 || vol->composite_id.instance_id == instance_idx)
&& vol->composite_id.volume_id < 0)
vol->is_active = visible;
#endif // ENABLE_RAYCAST_PICKING
}
}
void GLCanvas3D::toggle_model_objects_visibility(bool visible, const ModelObject* mo, int instance_idx, const ModelVolume* mv)
{
#if ENABLE_RAYCAST_PICKING
std::vector<std::shared_ptr<SceneRaycasterItem>>* raycasters = get_raycasters_for_picking(SceneRaycaster::EType::Volume);
#endif // ENABLE_RAYCAST_PICKING
for (GLVolume* vol : m_volumes.volumes) {
if (vol->is_wipe_tower)
vol->is_active = (visible && mo == nullptr);
@ -1404,11 +1393,10 @@ void GLCanvas3D::toggle_model_objects_visibility(bool visible, const ModelObject
}
}
}
#if ENABLE_RAYCAST_PICKING
auto it = std::find_if(raycasters->begin(), raycasters->end(), [vol](std::shared_ptr<SceneRaycasterItem> item) { return item->get_raycaster() == vol->mesh_raycaster.get(); });
if (it != raycasters->end())
(*it)->set_active(vol->is_active);
#endif // ENABLE_RAYCAST_PICKING
}
if (visible && !mo)
@ -1666,12 +1654,8 @@ void GLCanvas3D::render()
// and the viewport was set incorrectly, leading to tripping glAsserts further down
// the road (in apply_projection). That's why the minimum size is forced to 10.
Camera& camera = wxGetApp().plater()->get_camera();
#if ENABLE_RAYCAST_PICKING
camera.set_viewport(0, 0, std::max(10u, (unsigned int)cnv_size.get_width()), std::max(10u, (unsigned int)cnv_size.get_height()));
camera.apply_viewport();
#else
camera.apply_viewport(0, 0, std::max(10u, (unsigned int)cnv_size.get_width()), std::max(10u, (unsigned int)cnv_size.get_height()));
#endif // ENABLE_RAYCAST_PICKING
if (camera.requires_zoom_to_bed) {
zoom_to_bed();
@ -2266,9 +2250,7 @@ void GLCanvas3D::reload_scene(bool refresh_immediately, bool force_full_scene_re
#else
#if ENABLE_LEGACY_OPENGL_REMOVAL
volume.model.init_from(mesh);
#if ENABLE_RAYCAST_PICKING
volume.mesh_raycaster = std::make_unique<GUI::MeshRaycaster>(std::make_shared<TriangleMesh>(mesh));
#endif // ENABLE_RAYCAST_PICKING
#else
volume.indexed_vertex_array.load_mesh(mesh);
#endif // ENABLE_LEGACY_OPENGL_REMOVAL
@ -2284,13 +2266,9 @@ void GLCanvas3D::reload_scene(bool refresh_immediately, bool force_full_scene_re
#endif // ENABLE_LEGACY_OPENGL_REMOVAL
#else
#if ENABLE_LEGACY_OPENGL_REMOVAL
#if ENABLE_RAYCAST_PICKING
const TriangleMesh& new_mesh = m_model->objects[volume.object_idx()]->volumes[volume.volume_idx()]->mesh();
const TriangleMesh& new_mesh = m_model->objects[volume.object_idx()]->volumes[volume.volume_idx()]->mesh();
volume.model.init_from(new_mesh);
volume.mesh_raycaster = std::make_unique<GUI::MeshRaycaster>(std::make_shared<TriangleMesh>(new_mesh));
#else
volume.model.init_from(m_model->objects[volume.object_idx()]->volumes[volume.volume_idx()]->mesh());
#endif // ENABLE_RAYCAST_PICKING
#else
volume.indexed_vertex_array.load_mesh(m_model->objects[volume.object_idx()]->volumes[volume.volume_idx()]->mesh());
#endif // ENABLE_LEGACY_OPENGL_REMOVAL
@ -2447,7 +2425,6 @@ void GLCanvas3D::reload_scene(bool refresh_immediately, bool force_full_scene_re
manip->set_dirty();
}
#if ENABLE_RAYCAST_PICKING
// refresh volume raycasters for picking
m_scene_raycaster.remove_raycasters(SceneRaycaster::EType::Volume);
for (size_t i = 0; i < m_volumes.volumes.size(); ++i) {
@ -2464,7 +2441,6 @@ void GLCanvas3D::reload_scene(bool refresh_immediately, bool force_full_scene_re
m_scene_raycaster.remove_raycasters(SceneRaycaster::EType::Gizmo);
if (curr_gizmo != nullptr && !m_selection.is_empty())
curr_gizmo->register_raycasters_for_picking();
#endif // ENABLE_RAYCAST_PICKING
// and force this canvas to be redrawn.
m_dirty = true;
@ -3010,11 +2986,7 @@ void GLCanvas3D::on_key(wxKeyEvent& evt)
// set_cursor(Standard);
}
else if (keyCode == WXK_CONTROL) {
#if ENABLE_RAYCAST_PICKING
if (m_mouse.dragging && !m_moving) {
#else
if (m_mouse.dragging) {
#endif // ENABLE_RAYCAST_PICKING
if (m_mouse.dragging && !m_moving) {
// if the user releases CTRL while rotating the 3D scene
// prevent from moving the selected volume
m_mouse.drag.move_volume_idx = -1;
@ -3530,9 +3502,6 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
if (!evt.CmdDown())
m_mouse.drag.start_position_3D = m_mouse.scene_position;
m_sequential_print_clearance_first_displacement = true;
#if !ENABLE_RAYCAST_PICKING
m_moving = true;
#endif // !ENABLE_RAYCAST_PICKING
}
}
}
@ -3576,9 +3545,7 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
}
#if ENABLE_WORLD_COORDINATE
#if ENABLE_RAYCAST_PICKING
m_moving = true;
#endif // ENABLE_RAYCAST_PICKING
TransformationType trafo_type;
trafo_type.set_relative();
m_selection.translate(cur_pos - m_mouse.drag.start_position_3D, trafo_type);
@ -3609,9 +3576,7 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
// do not process the dragging if the left mouse was set down in another canvas
else if (evt.LeftIsDown()) {
// if dragging over blank area with left button, rotate
#if ENABLE_RAYCAST_PICKING
if (!m_moving) {
#endif // ENABLE_RAYCAST_PICKING
if ((any_gizmo_active || evt.CmdDown() || m_hover_volume_idxs.empty()) && m_mouse.is_start_position_3D_defined()) {
const Vec3d rot = (Vec3d(pos.x(), pos.y(), 0.0) - m_mouse.drag.start_position_3D) * (PI * TRACKBALLSIZE / 180.0);
if (wxGetApp().app_config->get("use_free_camera") == "1")
@ -3630,9 +3595,7 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
m_dirty = true;
}
m_mouse.drag.start_position_3D = Vec3d((double)pos.x(), (double)pos.y(), 0.0);
#if ENABLE_RAYCAST_PICKING
}
#endif // ENABLE_RAYCAST_PICKING
}
else if (evt.MiddleIsDown() || evt.RightIsDown()) {
// If dragging over blank area with right/middle button, pan.
@ -3657,9 +3620,7 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
}
}
else if (evt.LeftUp() || evt.MiddleUp() || evt.RightUp()) {
#if ENABLE_RAYCAST_PICKING
m_mouse.position = pos.cast<double>();
#endif // ENABLE_RAYCAST_PICKING
if (m_layers_editing.state != LayersEditing::Unknown) {
m_layers_editing.state = LayersEditing::Unknown;
@ -3685,9 +3646,6 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
deselect_all();
}
else if (evt.RightUp()) {
#if !ENABLE_RAYCAST_PICKING
m_mouse.position = pos.cast<double>();
#endif // !ENABLE_RAYCAST_PICKING
// forces a frame render to ensure that m_hover_volume_idxs is updated even when the user right clicks while
// the context menu is already shown
render();
@ -4678,12 +4636,8 @@ void GLCanvas3D::_render_thumbnail_internal(ThumbnailData& thumbnail_data, const
Camera camera;
camera.set_type(camera_type);
camera.set_scene_box(scene_bounding_box());
#if ENABLE_RAYCAST_PICKING
camera.set_viewport(0, 0, thumbnail_data.width, thumbnail_data.height);
camera.apply_viewport();
#else
camera.apply_viewport(0, 0, thumbnail_data.width, thumbnail_data.height);
#endif // ENABLE_RAYCAST_PICKING
camera.zoom_to_box(volumes_box);
#if ENABLE_LEGACY_OPENGL_REMOVAL
@ -4985,11 +4939,7 @@ void GLCanvas3D::_render_thumbnail_legacy(ThumbnailData& thumbnail_data, unsigne
#endif // ENABLE_THUMBNAIL_GENERATOR_DEBUG_OUTPUT
// restore the default framebuffer size to avoid flickering on the 3D scene
#if ENABLE_RAYCAST_PICKING
wxGetApp().plater()->get_camera().apply_viewport();
#else
wxGetApp().plater()->get_camera().apply_viewport(0, 0, cnv_size.get_width(), cnv_size.get_height());
#endif // ENABLE_RAYCAST_PICKING
}
bool GLCanvas3D::_init_toolbars()
@ -5456,7 +5406,6 @@ void GLCanvas3D::_refresh_if_shown_on_screen()
}
}
#if ENABLE_RAYCAST_PICKING
void GLCanvas3D::_picking_pass()
{
if (!m_picking_enabled || m_mouse.dragging || m_mouse.position == Vec2d(DBL_MAX, DBL_MAX) || m_gizmos.is_dragging()) {
@ -5599,82 +5548,6 @@ void GLCanvas3D::_picking_pass()
imgui.end();
#endif // ENABLE_RAYCAST_PICKING_DEBUG
}
#else
void GLCanvas3D::_picking_pass()
{
if (m_picking_enabled && !m_mouse.dragging && m_mouse.position != Vec2d(DBL_MAX, DBL_MAX) && !m_gizmos.is_dragging()) {
m_hover_volume_idxs.clear();
// Render the object for picking.
// FIXME This cannot possibly work in a multi - sampled context as the color gets mangled by the anti - aliasing.
// Better to use software ray - casting on a bounding - box hierarchy.
if (m_multisample_allowed)
// This flag is often ignored by NVIDIA drivers if rendering into a screen buffer.
glsafe(::glDisable(GL_MULTISAMPLE));
glsafe(::glDisable(GL_BLEND));
glsafe(::glEnable(GL_DEPTH_TEST));
glsafe(::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
#if !ENABLE_LEGACY_OPENGL_REMOVAL
m_camera_clipping_plane = m_gizmos.get_clipping_plane();
if (m_camera_clipping_plane.is_active()) {
::glClipPlane(GL_CLIP_PLANE0, (GLdouble*)m_camera_clipping_plane.get_data().data());
::glEnable(GL_CLIP_PLANE0);
}
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
_render_volumes_for_picking();
#if !ENABLE_LEGACY_OPENGL_REMOVAL
if (m_camera_clipping_plane.is_active())
::glDisable(GL_CLIP_PLANE0);
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
#if ENABLE_LEGACY_OPENGL_REMOVAL
const Camera& camera = wxGetApp().plater()->get_camera();
_render_bed_for_picking(camera.get_view_matrix(), camera.get_projection_matrix(), !camera.is_looking_downward());
#else
_render_bed_for_picking(!wxGetApp().plater()->get_camera().is_looking_downward());
#endif // ENABLE_LEGACY_OPENGL_REMOVAL
m_gizmos.render_current_gizmo_for_picking_pass();
if (m_multisample_allowed)
glsafe(::glEnable(GL_MULTISAMPLE));
int volume_id = -1;
int gizmo_id = -1;
std::array<GLubyte, 4> color = { 0, 0, 0, 0 };
const Size& cnv_size = get_canvas_size();
bool inside = 0 <= m_mouse.position(0) && m_mouse.position(0) < cnv_size.get_width() && 0 <= m_mouse.position(1) && m_mouse.position(1) < cnv_size.get_height();
if (inside) {
glsafe(::glReadPixels(m_mouse.position(0), cnv_size.get_height() - m_mouse.position.y() - 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, (void*)color.data()));
if (picking_checksum_alpha_channel(color[0], color[1], color[2]) == color[3]) {
// Only non-interpolated colors are valid, those have their lowest three bits zeroed.
// we reserve color = (0,0,0) for occluders (as the printbed)
// volumes' id are shifted by 1
// see: _render_volumes_for_picking()
unsigned int id = picking_encode(color[0], color[1], color[2]);
volume_id = id - 1;
// gizmos' id are instead properly encoded by the color
gizmo_id = id;
}
}
if (0 <= volume_id && volume_id < (int)m_volumes.volumes.size()) {
// do not add the volume id if any gizmo is active and CTRL is pressed
if (m_gizmos.get_current_type() == GLGizmosManager::EType::Undefined || !wxGetKeyState(WXK_CONTROL))
m_hover_volume_idxs.emplace_back(volume_id);
m_gizmos.set_hover_id(-1);
}
else
m_gizmos.set_hover_id(inside && (unsigned int)gizmo_id <= GLGizmoBase::BASE_ID ? ((int)GLGizmoBase::BASE_ID - gizmo_id) : -1);
_update_volumes_hover_state();
}
}
#endif // ENABLE_RAYCAST_PICKING
void GLCanvas3D::_rectangular_selection_picking_pass()
{
@ -5683,7 +5556,6 @@ void GLCanvas3D::_rectangular_selection_picking_pass()
std::set<int> idxs;
if (m_picking_enabled) {
#if ENABLE_RAYCAST_PICKING
const size_t width = std::max<size_t>(m_rectangle_selection.get_width(), 1);
const size_t height = std::max<size_t>(m_rectangle_selection.get_height(), 1);
@ -5737,7 +5609,6 @@ void GLCanvas3D::_rectangular_selection_picking_pass()
use_framebuffer = false;
}
}
#endif // ENABLE_RAYCAST_PICKING
if (m_multisample_allowed)
// This flag is often ignored by NVIDIA drivers if rendering into a screen buffer.
@ -5748,7 +5619,6 @@ void GLCanvas3D::_rectangular_selection_picking_pass()
glsafe(::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
#if ENABLE_RAYCAST_PICKING
const Camera& main_camera = wxGetApp().plater()->get_camera();
Camera framebuffer_camera;
framebuffer_camera.set_type(main_camera.get_type());
@ -5782,16 +5652,8 @@ void GLCanvas3D::_rectangular_selection_picking_pass()
}
_render_volumes_for_picking(*camera);
#else
_render_volumes_for_picking();
#endif // ENABLE_RAYCAST_PICKING
#if ENABLE_LEGACY_OPENGL_REMOVAL
#if ENABLE_RAYCAST_PICKING
_render_bed_for_picking(camera->get_view_matrix(), camera->get_projection_matrix(), !camera->is_looking_downward());
#else
const Camera& camera = wxGetApp().plater()->get_camera();
_render_bed_for_picking(camera.get_view_matrix(), camera.get_projection_matrix(), !camera.is_looking_downward());
#endif // ENABLE_RAYCAST_PICKING
#else
_render_bed_for_picking(!wxGetApp().plater()->get_camera().is_looking_downward());
#endif // ENABLE_LEGACY_OPENGL_REMOVAL
@ -5799,20 +5661,10 @@ void GLCanvas3D::_rectangular_selection_picking_pass()
if (m_multisample_allowed)
glsafe(::glEnable(GL_MULTISAMPLE));
#if ENABLE_RAYCAST_PICKING
const size_t px_count = width * height;
const size_t left = use_framebuffer ? 0 : (size_t)m_rectangle_selection.get_left();
const size_t top = use_framebuffer ? 0 : (size_t)get_canvas_size().get_height() - (size_t)m_rectangle_selection.get_top();
#else
int width = std::max((int)m_rectangle_selection.get_width(), 1);
int height = std::max((int)m_rectangle_selection.get_height(), 1);
int px_count = width * height;
int left = (int)m_rectangle_selection.get_left();
int top = get_canvas_size().get_height() - (int)m_rectangle_selection.get_top();
if (left >= 0 && top >= 0) {
#endif // ENABLE_RAYCAST_PICKING
#define USE_PARALLEL 1
#if USE_PARALLEL
struct Pixel
@ -5846,15 +5698,13 @@ void GLCanvas3D::_rectangular_selection_picking_pass()
std::vector<GLubyte> frame(4 * px_count);
glsafe(::glReadPixels(left, top, width, height, GL_RGBA, GL_UNSIGNED_BYTE, (void*)frame.data()));
for (int i = 0; i < px_count; ++i)
{
for (int i = 0; i < px_count; ++i) {
int px_id = 4 * i;
int volume_id = frame[px_id] + (frame[px_id + 1] << 8) + (frame[px_id + 2] << 16);
if (0 <= volume_id && volume_id < (int)m_volumes.volumes.size())
idxs.insert(volume_id);
}
#endif // USE_PARALLEL
#if ENABLE_RAYCAST_PICKING
if (camera != &main_camera)
main_camera.apply_viewport();
@ -5875,9 +5725,6 @@ void GLCanvas3D::_rectangular_selection_picking_pass()
if (render_tex != 0)
glsafe(::glDeleteTextures(1, &render_tex));
#else
}
#endif // ENABLE_RAYCAST_PICKING
}
m_hover_volume_idxs.assign(idxs.begin(), idxs.end());
@ -6280,11 +6127,7 @@ void GLCanvas3D::_render_overlays()
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
}
#if ENABLE_RAYCAST_PICKING
void GLCanvas3D::_render_volumes_for_picking(const Camera& camera) const
#else
void GLCanvas3D::_render_volumes_for_picking() const
#endif // ENABLE_RAYCAST_PICKING
{
#if ENABLE_LEGACY_OPENGL_REMOVAL
GLShaderProgram* shader = wxGetApp().get_shader("flat_clip");
@ -6300,11 +6143,7 @@ void GLCanvas3D::_render_volumes_for_picking() const
glsafe(::glEnableClientState(GL_NORMAL_ARRAY));
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
#if ENABLE_RAYCAST_PICKING
const Transform3d& view_matrix = camera.get_view_matrix();
#else
const Transform3d& view_matrix = wxGetApp().plater()->get_camera().get_view_matrix();
#endif // ENABLE_RAYCAST_PICKING
for (size_t type = 0; type < 2; ++ type) {
GLVolumeWithIdAndZList to_render = volumes_to_render(m_volumes.volumes, (type == 0) ? GLVolumeCollection::ERenderType::Opaque : GLVolumeCollection::ERenderType::Transparent, view_matrix);
for (const GLVolumeWithIdAndZ& volume : to_render)
@ -6316,12 +6155,7 @@ void GLCanvas3D::_render_volumes_for_picking() const
#if ENABLE_LEGACY_OPENGL_REMOVAL
volume.first->model.set_color(picking_decode(id));
shader->start_using();
#if ENABLE_RAYCAST_PICKING
shader->set_uniform("view_model_matrix", view_matrix * volume.first->world_matrix());
#else
const Camera& camera = wxGetApp().plater()->get_camera();
shader->set_uniform("view_model_matrix", camera.get_view_matrix() * volume.first->world_matrix());
#endif // ENABLE_RAYCAST_PICKING
shader->set_uniform("projection_matrix", camera.get_projection_matrix());
shader->set_uniform("volume_world_matrix", volume.first->world_matrix());
shader->set_uniform("z_range", m_volumes.get_z_range());
@ -6889,7 +6723,6 @@ Vec3d GLCanvas3D::_mouse_to_3d(const Point& mouse_pos, float* z)
if (m_canvas == nullptr)
return Vec3d(DBL_MAX, DBL_MAX, DBL_MAX);
#if ENABLE_RAYCAST_PICKING
if (z == nullptr) {
const SceneRaycaster::HitResult hit = m_scene_raycaster.hit(mouse_pos.cast<double>(), wxGetApp().plater()->get_camera(), nullptr);
return hit.is_valid() ? hit.position.cast<double>() : _mouse_to_bed_3d(mouse_pos);
@ -6901,23 +6734,6 @@ Vec3d GLCanvas3D::_mouse_to_3d(const Point& mouse_pos, float* z)
igl::unproject(Vec3d(mouse_pos.x(), viewport[3] - mouse_pos.y(), *z), camera.get_view_matrix().matrix(), camera.get_projection_matrix().matrix(), viewport, out);
return out;
}
#else
const Camera& camera = wxGetApp().plater()->get_camera();
const Matrix4d modelview = camera.get_view_matrix().matrix();
const Matrix4d projection = camera.get_projection_matrix().matrix();
const Vec4i viewport(camera.get_viewport().data());
const int y = viewport[3] - mouse_pos.y();
float mouse_z;
if (z == nullptr)
glsafe(::glReadPixels(mouse_pos.x(), y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, (void*)&mouse_z));
else
mouse_z = *z;
Vec3d out;
igl::unproject(Vec3d(mouse_pos.x(), y, mouse_z), modelview, projection, viewport, out);
return out;
#endif // ENABLE_RAYCAST_PICKING
}
Vec3d GLCanvas3D::_mouse_to_bed_3d(const Point& mouse_pos)