diff --git a/src/slic3r/GUI/Camera.cpp b/src/slic3r/GUI/Camera.cpp index db0126d65..4eaa5a9e3 100644 --- a/src/slic3r/GUI/Camera.cpp +++ b/src/slic3r/GUI/Camera.cpp @@ -240,17 +240,27 @@ void Camera::apply_view_matrix() const void Camera::apply_projection(const BoundingBoxf3& box, double near_z, double far_z) const { +#if !ENABLE_6DOF_CAMERA set_distance(DefaultDistance); +#endif // !ENABLE_6DOF_CAMERA double w = 0.0; double h = 0.0; +#if ENABLE_6DOF_CAMERA + double old_distance = m_distance; + m_frustrum_zs = calc_tight_frustrum_zs_around(box); + if (m_distance != old_distance) + // the camera has been moved re-apply view matrix + apply_view_matrix(); +#else while (true) { m_frustrum_zs = calc_tight_frustrum_zs_around(box); +#endif // !ENABLE_6DOF_CAMERA if (near_z > 0.0) - m_frustrum_zs.first = std::min(m_frustrum_zs.first, near_z); + m_frustrum_zs.first = std::max(std::min(m_frustrum_zs.first, near_z), FrustrumMinNearZ); if (far_z > 0.0) m_frustrum_zs.second = std::max(m_frustrum_zs.second, far_z); @@ -281,6 +291,7 @@ void Camera::apply_projection(const BoundingBoxf3& box, double near_z, double fa } } +#if !ENABLE_6DOF_CAMERA if (m_type == Perspective) { double fov_deg = Geometry::rad2deg(2.0 * std::atan(h / m_frustrum_zs.first)); @@ -300,6 +311,7 @@ void Camera::apply_projection(const BoundingBoxf3& box, double near_z, double fa else break; } +#endif // !ENABLE_6DOF_CAMERA glsafe(::glMatrixMode(GL_PROJECTION)); glsafe(::glLoadIdentity()); @@ -324,14 +336,22 @@ void Camera::apply_projection(const BoundingBoxf3& box, double near_z, double fa } #if ENABLE_THUMBNAIL_GENERATOR +#if ENABLE_6DOF_CAMERA +void Camera::zoom_to_box(const BoundingBoxf3& box, double margin_factor) +#else void Camera::zoom_to_box(const BoundingBoxf3& box, int canvas_w, int canvas_h, double margin_factor) +#endif // ENABLE_6DOF_CAMERA #else void Camera::zoom_to_box(const BoundingBoxf3& box, int canvas_w, int canvas_h) #endif // ENABLE_THUMBNAIL_GENERATOR { // Calculate the zoom factor needed to adjust the view around the given box. #if ENABLE_THUMBNAIL_GENERATOR +#if ENABLE_6DOF_CAMERA + double zoom = calc_zoom_to_bounding_box_factor(box, margin_factor); +#else double zoom = calc_zoom_to_bounding_box_factor(box, canvas_w, canvas_h, margin_factor); +#endif // ENABLE_6DOF_CAMERA #else double zoom = calc_zoom_to_bounding_box_factor(box, canvas_w, canvas_h); #endif // ENABLE_THUMBNAIL_GENERATOR @@ -348,10 +368,18 @@ void Camera::zoom_to_box(const BoundingBoxf3& box, int canvas_w, int canvas_h) } #if ENABLE_THUMBNAIL_GENERATOR +#if ENABLE_6DOF_CAMERA +void Camera::zoom_to_volumes(const GLVolumePtrs& volumes, double margin_factor) +#else void Camera::zoom_to_volumes(const GLVolumePtrs& volumes, int canvas_w, int canvas_h, double margin_factor) +#endif // ENABLE_6DOF_CAMERA { Vec3d center; +#if ENABLE_6DOF_CAMERA + double zoom = calc_zoom_to_volumes_factor(volumes, center, margin_factor); +#else double zoom = calc_zoom_to_volumes_factor(volumes, canvas_w, canvas_h, center, margin_factor); +#endif // ENABLE_6DOF_CAMERA if (zoom > 0.0) { m_zoom = zoom; @@ -389,6 +417,7 @@ void Camera::debug_render() const float deltaZ = farZ - nearZ; float zoom = (float)m_zoom; float fov = (float)get_fov(); + std::arrayviewport = get_viewport(); float gui_scale = (float)get_gui_scale(); ImGui::InputText("Type", type.data(), type.length(), ImGuiInputTextFlags_ReadOnly); @@ -408,6 +437,8 @@ void Camera::debug_render() const ImGui::InputFloat("Zoom", &zoom, 0.0f, 0.0f, "%.6f", ImGuiInputTextFlags_ReadOnly); ImGui::InputFloat("Fov", &fov, 0.0f, 0.0f, "%.6f", ImGuiInputTextFlags_ReadOnly); ImGui::Separator(); + ImGui::InputInt4("Viewport", viewport.data(), ImGuiInputTextFlags_ReadOnly); + ImGui::Separator(); ImGui::InputFloat("GUI scale", &gui_scale, 0.0f, 0.0f, "%.6f", ImGuiInputTextFlags_ReadOnly); imgui.end(); } @@ -427,10 +458,31 @@ void Camera::translate_world(const Vec3d& displacement) void Camera::rotate_on_sphere(double delta_azimut_rad, double delta_zenit_rad) { + // FIXME -> The following is a HACK !!! + // When the value of the zenit rotation is large enough, the following call to rotate() shows + // numerical instability introducing some scaling into m_view_matrix (verified by checking + // that the camera space unit vectors are no more unit). + // See also https://dev.prusa3d.com/browse/SPE-1082 + // We split the zenit rotation into a set of smaller rotations which are then applied. + static const double MAX_ALLOWED = Geometry::deg2rad(0.1); + unsigned int zenit_steps_count = 1 + (unsigned int)(std::abs(delta_zenit_rad) / MAX_ALLOWED); + double zenit_step = delta_zenit_rad / (double)zenit_steps_count; + Vec3d target = m_target; translate_world(-target); - m_view_matrix.rotate(Eigen::AngleAxisd(delta_zenit_rad, get_dir_right())); - m_view_matrix.rotate(Eigen::AngleAxisd(delta_azimut_rad, Vec3d::UnitZ())); + + if (zenit_step != 0.0) + { + Vec3d right = get_dir_right(); + for (unsigned int i = 0; i < zenit_steps_count; ++i) + { + m_view_matrix.rotate(Eigen::AngleAxisd(zenit_step, right)); + } + } + + if (delta_azimut_rad != 0.0) + m_view_matrix.rotate(Eigen::AngleAxisd(delta_azimut_rad, Vec3d::UnitZ())); + translate_world(target); } @@ -453,7 +505,11 @@ void Camera::rotate_local_around_pivot(const Vec3d& rotation_rad, const Vec3d& p double Camera::min_zoom() const { +#if ENABLE_6DOF_CAMERA + return 0.7 * calc_zoom_to_bounding_box_factor(m_scene_box); +#else return 0.7 * calc_zoom_to_bounding_box_factor(m_scene_box, (int)m_viewport[2], (int)m_viewport[3]); +#endif // ENABLE_6DOF_CAMERA } #endif // ENABLE_6DOF_CAMERA @@ -462,8 +518,10 @@ std::pair Camera::calc_tight_frustrum_zs_around(const BoundingBo std::pair ret; auto& [near_z, far_z] = ret; +#if !ENABLE_6DOF_CAMERA while (true) { +#endif // !ENABLE_6DOF_CAMERA // box in eye space BoundingBoxf3 eye_box = box.transformed(m_view_matrix); near_z = -eye_box.max(2); @@ -482,18 +540,39 @@ std::pair Camera::calc_tight_frustrum_zs_around(const BoundingBo far_z = mid_z + half_size; } +#if ENABLE_6DOF_CAMERA + if (near_z < FrustrumMinNearZ) + { + float delta = FrustrumMinNearZ - near_z; + set_distance(m_distance + delta); + near_z += delta; + far_z += delta; + } + else if ((near_z > 2.0 * FrustrumMinNearZ) && (m_distance > DefaultDistance)) + { + float delta = m_distance - DefaultDistance; + set_distance(DefaultDistance); + near_z -= delta; + far_z -= delta; + } +#else if (near_z >= FrustrumMinNearZ) break; - // ensure min Near Z + // ensure min near z set_distance(m_distance + FrustrumMinNearZ - near_z); } +#endif // ENABLE_6DOF_CAMERA return ret; } #if ENABLE_THUMBNAIL_GENERATOR +#if ENABLE_6DOF_CAMERA +double Camera::calc_zoom_to_bounding_box_factor(const BoundingBoxf3& box, double margin_factor) const +#else double Camera::calc_zoom_to_bounding_box_factor(const BoundingBoxf3& box, int canvas_w, int canvas_h, double margin_factor) const +#endif // ENABLE_6DOF_CAMERA #else double Camera::calc_zoom_to_bounding_box_factor(const BoundingBoxf3& box, int canvas_w, int canvas_h) const #endif // ENABLE_THUMBNAIL_GENERATOR @@ -505,8 +584,10 @@ double Camera::calc_zoom_to_bounding_box_factor(const BoundingBoxf3& box, int ca // project the box vertices on a plane perpendicular to the camera forward axis // then calculates the vertices coordinate on this plane along the camera xy axes +#if !ENABLE_6DOF_CAMERA // ensure that the view matrix is updated apply_view_matrix(); +#endif // !ENABLE_6DOF_CAMERA Vec3d right = get_dir_right(); Vec3d up = get_dir_up(); @@ -563,11 +644,19 @@ double Camera::calc_zoom_to_bounding_box_factor(const BoundingBoxf3& box, int ca dx *= margin_factor; dy *= margin_factor; +#if ENABLE_6DOF_CAMERA + return std::min((double)m_viewport[2] / dx, (double)m_viewport[3] / dy); +#else return std::min((double)canvas_w / dx, (double)canvas_h / dy); +#endif // ENABLE_6DOF_CAMERA } #if ENABLE_THUMBNAIL_GENERATOR +#if ENABLE_6DOF_CAMERA +double Camera::calc_zoom_to_volumes_factor(const GLVolumePtrs& volumes, Vec3d& center, double margin_factor) const +#else double Camera::calc_zoom_to_volumes_factor(const GLVolumePtrs& volumes, int canvas_w, int canvas_h, Vec3d& center, double margin_factor) const +#endif // ENABLE_6DOF_CAMERA { if (volumes.empty()) return -1.0; @@ -575,8 +664,10 @@ double Camera::calc_zoom_to_volumes_factor(const GLVolumePtrs& volumes, int canv // project the volumes vertices on a plane perpendicular to the camera forward axis // then calculates the vertices coordinate on this plane along the camera xy axes +#if !ENABLE_6DOF_CAMERA // ensure that the view matrix is updated apply_view_matrix(); +#endif // !ENABLE_6DOF_CAMERA Vec3d right = get_dir_right(); Vec3d up = get_dir_up(); @@ -628,14 +719,26 @@ double Camera::calc_zoom_to_volumes_factor(const GLVolumePtrs& volumes, int canv if ((dx <= 0.0) || (dy <= 0.0)) return -1.0f; +#if ENABLE_6DOF_CAMERA + return std::min((double)m_viewport[2] / dx, (double)m_viewport[3] / dy); +#else return std::min((double)canvas_w / dx, (double)canvas_h / dy); +#endif // ENABLE_6DOF_CAMERA } #endif // ENABLE_THUMBNAIL_GENERATOR void Camera::set_distance(double distance) const { +#if ENABLE_6DOF_CAMERA + if (m_distance != distance) + { + m_view_matrix.translate((distance - m_distance) * get_dir_forward()); + m_distance = distance; + } +#else m_distance = distance; apply_view_matrix(); +#endif // ENABLE_6DOF_CAMERA } #if ENABLE_6DOF_CAMERA diff --git a/src/slic3r/GUI/Camera.hpp b/src/slic3r/GUI/Camera.hpp index 6a2bc2bf3..a1c42a0c6 100644 --- a/src/slic3r/GUI/Camera.hpp +++ b/src/slic3r/GUI/Camera.hpp @@ -48,11 +48,7 @@ private: mutable double m_gui_scale; mutable std::array m_viewport; -#if ENABLE_6DOF_CAMERA - Transform3d m_view_matrix; -#else mutable Transform3d m_view_matrix; -#endif // ENABLE_6DOF_CAMERA mutable Transform3d m_projection_matrix; mutable std::pair m_frustrum_zs; @@ -71,7 +67,11 @@ public: const Vec3d& get_target() const { return m_target; } void set_target(const Vec3d& target); +#if ENABLE_6DOF_CAMERA + double get_distance() const { return (get_position() - m_target).norm(); } +#else double get_distance() const { return m_distance; } +#endif // ENABLE_6DOF_CAMERA double get_gui_scale() const { return m_gui_scale; } #if !ENABLE_6DOF_CAMERA @@ -115,8 +115,13 @@ public: void apply_projection(const BoundingBoxf3& box, double near_z = -1.0, double far_z = -1.0) const; #if ENABLE_THUMBNAIL_GENERATOR +#if ENABLE_6DOF_CAMERA + void zoom_to_box(const BoundingBoxf3& box, double margin_factor = DefaultZoomToBoxMarginFactor); + void zoom_to_volumes(const GLVolumePtrs& volumes, double margin_factor = DefaultZoomToVolumesMarginFactor); +#else void zoom_to_box(const BoundingBoxf3& box, int canvas_w, int canvas_h, double margin_factor = DefaultZoomToBoxMarginFactor); void zoom_to_volumes(const GLVolumePtrs& volumes, int canvas_w, int canvas_h, double margin_factor = DefaultZoomToVolumesMarginFactor); +#endif // ENABLE_6DOF_CAMERA #else void zoom_to_box(const BoundingBoxf3& box, int canvas_w, int canvas_h); #endif // ENABLE_THUMBNAIL_GENERATOR @@ -151,8 +156,13 @@ private: // the camera MUST be outside of the bounding box in eye coordinate of the given box std::pair calc_tight_frustrum_zs_around(const BoundingBoxf3& box) const; #if ENABLE_THUMBNAIL_GENERATOR +#if ENABLE_6DOF_CAMERA + double calc_zoom_to_bounding_box_factor(const BoundingBoxf3& box, double margin_factor = DefaultZoomToBoxMarginFactor) const; + double calc_zoom_to_volumes_factor(const GLVolumePtrs& volumes, Vec3d& center, double margin_factor = DefaultZoomToVolumesMarginFactor) const; +#else double calc_zoom_to_bounding_box_factor(const BoundingBoxf3& box, int canvas_w, int canvas_h, double margin_factor = DefaultZoomToBoxMarginFactor) const; double calc_zoom_to_volumes_factor(const GLVolumePtrs& volumes, int canvas_w, int canvas_h, Vec3d& center, double margin_factor = DefaultZoomToVolumesMarginFactor) const; +#endif // ENABLE_6DOF_CAMERA #else double calc_zoom_to_bounding_box_factor(const BoundingBoxf3& box, int canvas_w, int canvas_h) const; #endif // ENABLE_THUMBNAIL_GENERATOR diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 8805dc92c..957b4f71d 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -1710,11 +1710,16 @@ void GLCanvas3D::render() } const Size& cnv_size = get_canvas_size(); +#if ENABLE_6DOF_CAMERA + m_camera.apply_viewport(0, 0, (unsigned int)cnv_size.get_width(), (unsigned int)cnv_size.get_height()); +#endif // ENABLE_6DOF_CAMERA if (m_camera.requires_zoom_to_bed) { zoom_to_bed(); +#if !ENABLE_6DOF_CAMERA _resize((unsigned int)cnv_size.get_width(), (unsigned int)cnv_size.get_height()); +#endif // !ENABLE_6DOF_CAMERA m_camera.requires_zoom_to_bed = false; } @@ -3814,8 +3819,13 @@ void GLCanvas3D::_render_thumbnail_internal(ThumbnailData& thumbnail_data, bool #if ENABLE_6DOF_CAMERA camera.set_scene_box(scene_bounding_box()); #endif // ENABLE_6DOF_CAMERA +#if ENABLE_6DOF_CAMERA + camera.apply_viewport(0, 0, thumbnail_data.width, thumbnail_data.height); + camera.zoom_to_volumes(visible_volumes); +#else camera.zoom_to_volumes(visible_volumes, thumbnail_data.width, thumbnail_data.height); camera.apply_viewport(0, 0, thumbnail_data.width, thumbnail_data.height); +#endif // ENABLE_6DOF_CAMERA camera.apply_view_matrix(); double near_z = -1.0; @@ -4406,8 +4416,10 @@ void GLCanvas3D::_resize(unsigned int w, unsigned int h) // ensures that this canvas is current _set_current(); +#if !ENABLE_6DOF_CAMERA // updates camera m_camera.apply_viewport(0, 0, w, h); +#endif // !ENABLE_6DOF_CAMERA } BoundingBoxf3 GLCanvas3D::_max_bounding_box(bool include_gizmos, bool include_bed_model) const @@ -4431,8 +4443,12 @@ BoundingBoxf3 GLCanvas3D::_max_bounding_box(bool include_gizmos, bool include_be #if ENABLE_THUMBNAIL_GENERATOR void GLCanvas3D::_zoom_to_box(const BoundingBoxf3& box, double margin_factor) { +#if ENABLE_6DOF_CAMERA + m_camera.zoom_to_box(box, margin_factor); +#else const Size& cnv_size = get_canvas_size(); m_camera.zoom_to_box(box, cnv_size.get_width(), cnv_size.get_height(), margin_factor); +#endif // ENABLE_6DOF_CAMERA m_dirty = true; } #else