Six degrees of freedom camera - 1st installment

This commit is contained in:
Enrico Turri 2020-01-15 12:49:34 +01:00
parent 536514ff03
commit 62e60bcb43
8 changed files with 245 additions and 2 deletions

View File

@ -74,5 +74,7 @@
// Enable configurable paths export (fullpath or not) to 3mf and amf
#define ENABLE_CONFIGURABLE_PATHS_EXPORT_TO_3MF_AND_AMF (1 && ENABLE_2_2_0_BETA1)
// Enable 6 degrees of freedom camera
#define ENABLE_6DOF_CAMERA (1 && ENABLE_2_2_0_BETA1)
#endif // _technologies_h_

View File

@ -262,7 +262,11 @@ Point Bed3D::point_projection(const Point& point) const
return m_polygon.point_projection(point);
}
#if ENABLE_6DOF_CAMERA
void Bed3D::render(GLCanvas3D& canvas, bool bottom, float scale_factor, bool show_axes) const
#else
void Bed3D::render(GLCanvas3D& canvas, float theta, float scale_factor, bool show_axes) const
#endif // ENABLE_6DOF_CAMERA
{
m_scale_factor = scale_factor;
@ -273,6 +277,15 @@ void Bed3D::render(GLCanvas3D& canvas, float theta, float scale_factor, bool sho
switch (m_type)
{
#if ENABLE_6DOF_CAMERA
case MK2: { render_prusa(canvas, "mk2", bottom); break; }
case MK3: { render_prusa(canvas, "mk3", bottom); break; }
case SL1: { render_prusa(canvas, "sl1", bottom); break; }
case MINI: { render_prusa(canvas, "mini", bottom); break; }
case ENDER3: { render_prusa(canvas, "ender3", bottom); break; }
default:
case Custom: { render_custom(canvas, bottom); break; }
#else
case MK2: { render_prusa(canvas, "mk2", theta > 90.0f); break; }
case MK3: { render_prusa(canvas, "mk3", theta > 90.0f); break; }
case SL1: { render_prusa(canvas, "sl1", theta > 90.0f); break; }
@ -280,6 +293,7 @@ void Bed3D::render(GLCanvas3D& canvas, float theta, float scale_factor, bool sho
case ENDER3: { render_prusa(canvas, "ender3", theta > 90.0f); break; }
default:
case Custom: { render_custom(canvas, theta > 90.0f); break; }
#endif // ENABLE_6DOF_CAMERA
}
glsafe(::glDisable(GL_DEPTH_TEST));

View File

@ -110,7 +110,11 @@ public:
bool contains(const Point& point) const;
Point point_projection(const Point& point) const;
#if ENABLE_6DOF_CAMERA
void render(GLCanvas3D& canvas, bool bottom, float scale_factor, bool show_axes) const;
#else
void render(GLCanvas3D& canvas, float theta, float scale_factor, bool show_axes) const;
#endif // ENABLE_6DOF_CAMERA
private:
void calc_bounding_boxes() const;

View File

@ -34,18 +34,27 @@ double Camera::FrustrumZMargin = 10.0;
double Camera::MaxFovDeg = 60.0;
Camera::Camera()
#if ENABLE_6DOF_CAMERA
: requires_zoom_to_bed(false)
#else
: phi(45.0f)
, requires_zoom_to_bed(false)
, inverted_phi(false)
#endif // ENABLE_6DOF_CAMERA
, m_type(Perspective)
, m_target(Vec3d::Zero())
#if !ENABLE_6DOF_CAMERA
, m_theta(45.0f)
#endif // !ENABLE_6DOF_CAMERA
, m_zoom(1.0)
, m_distance(DefaultDistance)
, m_gui_scale(1.0)
, m_view_matrix(Transform3d::Identity())
, m_projection_matrix(Transform3d::Identity())
{
#if ENABLE_6DOF_CAMERA
set_default_orientation();
#endif // ENABLE_6DOF_CAMERA
}
std::string Camera::get_type_as_string() const
@ -91,6 +100,9 @@ void Camera::select_next_type()
void Camera::set_target(const Vec3d& target)
{
#if ENABLE_6DOF_CAMERA
translate_world(target - m_target);
#else
BoundingBoxf3 test_box = m_scene_box;
test_box.translate(-m_scene_box.center());
// We may let this factor be customizable
@ -101,8 +113,10 @@ void Camera::set_target(const Vec3d& target)
m_target(0) = clamp(test_box.min(0), test_box.max(0), target(0));
m_target(1) = clamp(test_box.min(1), test_box.max(1), target(1));
m_target(2) = clamp(test_box.min(2), test_box.max(2), target(2));
#endif // ENABLE_6DOF_CAMERA
}
#if !ENABLE_6DOF_CAMERA
void Camera::set_theta(float theta, bool apply_limit)
{
if (apply_limit)
@ -114,6 +128,7 @@ void Camera::set_theta(float theta, bool apply_limit)
m_theta += 360.0f;
}
}
#endif // !ENABLE_6DOF_CAMERA
void Camera::update_zoom(double delta_zoom)
{
@ -130,7 +145,25 @@ void Camera::set_zoom(double zoom)
// Don't allow to zoom too close to the scene.
m_zoom = std::min(zoom, 100.0);
}
#if ENABLE_6DOF_CAMERA
void Camera::select_view(const std::string& direction)
{
if (direction == "iso")
set_default_orientation();
else if (direction == "left")
m_view_matrix = look_at(m_target - m_distance * Vec3d::UnitX(), m_target, Vec3d::UnitZ());
else if (direction == "right")
m_view_matrix = look_at(m_target + m_distance * Vec3d::UnitX(), m_target, Vec3d::UnitZ());
else if (direction == "top")
m_view_matrix = look_at(m_target + m_distance * Vec3d::UnitZ(), m_target, Vec3d::UnitY());
else if (direction == "bottom")
m_view_matrix = look_at(m_target - m_distance * Vec3d::UnitZ(), m_target, -Vec3d::UnitY());
else if (direction == "front")
m_view_matrix = look_at(m_target - m_distance * Vec3d::UnitY(), m_target, Vec3d::UnitZ());
else if (direction == "rear")
m_view_matrix = look_at(m_target + m_distance * Vec3d::UnitY(), m_target, Vec3d::UnitZ());
}
#else
bool Camera::select_view(const std::string& direction)
{
const float* dir_vec = nullptr;
@ -159,6 +192,7 @@ bool Camera::select_view(const std::string& direction)
else
return false;
}
#endif // ENABLE_6DOF_CAMERA
double Camera::get_fov() const
{
@ -180,20 +214,26 @@ void Camera::apply_viewport(int x, int y, unsigned int w, unsigned int h) const
void Camera::apply_view_matrix() const
{
#if !ENABLE_6DOF_CAMERA
double theta_rad = Geometry::deg2rad(-(double)m_theta);
double phi_rad = Geometry::deg2rad((double)phi);
double sin_theta = ::sin(theta_rad);
Vec3d camera_pos = m_target + m_distance * Vec3d(sin_theta * ::sin(phi_rad), sin_theta * ::cos(phi_rad), ::cos(theta_rad));
#endif // !ENABLE_6DOF_CAMERA
glsafe(::glMatrixMode(GL_MODELVIEW));
glsafe(::glLoadIdentity());
#if ENABLE_6DOF_CAMERA
glsafe(::glMultMatrixd(m_view_matrix.data()));
#else
glsafe(::glRotatef(-m_theta, 1.0f, 0.0f, 0.0f)); // pitch
glsafe(::glRotatef(phi, 0.0f, 0.0f, 1.0f)); // yaw
glsafe(::glTranslated(-camera_pos(0), -camera_pos(1), -camera_pos(2)));
glsafe(::glGetDoublev(GL_MODELVIEW_MATRIX, m_view_matrix.data()));
#endif // ENABLE_6DOF_CAMERA
}
void Camera::apply_projection(const BoundingBoxf3& box, double near_z, double far_z) const
@ -300,7 +340,11 @@ void Camera::zoom_to_box(const BoundingBoxf3& box, int canvas_w, int canvas_h)
{
m_zoom = zoom;
// center view around box center
#if ENABLE_6DOF_CAMERA
set_target(box.center());
#else
m_target = box.center();
#endif // ENABLE_6DOF_CAMERA
}
}
@ -313,7 +357,11 @@ void Camera::zoom_to_volumes(const GLVolumePtrs& volumes, int canvas_w, int canv
{
m_zoom = zoom;
// center view around the calculated center
#if ENABLE_6DOF_CAMERA
set_target(center);
#else
m_target = center;
#endif // ENABLE_6DOF_CAMERA
}
}
#endif // ENABLE_THUMBNAIL_GENERATOR
@ -360,6 +408,43 @@ void Camera::debug_render() const
}
#endif // ENABLE_CAMERA_STATISTICS
#if ENABLE_6DOF_CAMERA
void Camera::translate_world(const Vec3d& displacement)
{
Vec3d new_target = validate_target(m_target + displacement);
Vec3d new_displacement = new_target - m_target;
if (!new_displacement.isApprox(Vec3d::Zero()))
{
m_target += new_displacement;
m_view_matrix.translate(-new_displacement);
}
}
void Camera::rotate_on_sphere(double delta_azimut_rad, double delta_zenit_rad, bool apply_limit)
{
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()));
translate_world(target);
}
void Camera::rotate_local_around_target(const Vec3d& rotation_rad)
{
Vec3d target = m_target;
translate_world(-target);
m_view_matrix.rotate(Eigen::AngleAxisd(rotation_rad(0), get_dir_right()));
m_view_matrix.rotate(Eigen::AngleAxisd(rotation_rad(1), get_dir_up()));
m_view_matrix.rotate(Eigen::AngleAxisd(rotation_rad(2), get_dir_forward()));
translate_world(target);
}
bool Camera::is_looking_downward() const
{
return get_dir_forward().dot(Vec3d::UnitZ()) < 0.0;
}
#endif // ENABLE_6DOF_CAMERA
std::pair<double, double> Camera::calc_tight_frustrum_zs_around(const BoundingBoxf3& box) const
{
std::pair<double, double> ret;
@ -540,6 +625,63 @@ void Camera::set_distance(double distance) const
apply_view_matrix();
}
#if ENABLE_6DOF_CAMERA
Transform3d Camera::look_at(const Vec3d& position, const Vec3d& target, const Vec3d& up) const
{
Vec3d unit_z = (position - target).normalized();
Vec3d unit_x = up.cross(unit_z).normalized();
Vec3d unit_y = unit_z.cross(unit_x).normalized();
Transform3d matrix;
matrix(0, 0) = unit_x(0);
matrix(0, 1) = unit_x(1);
matrix(0, 2) = unit_x(2);
matrix(0, 3) = -unit_x.dot(position);
matrix(1, 0) = unit_y(0);
matrix(1, 1) = unit_y(1);
matrix(1, 2) = unit_y(2);
matrix(1, 3) = -unit_y.dot(position);
matrix(2, 0) = unit_z(0);
matrix(2, 1) = unit_z(1);
matrix(2, 2) = unit_z(2);
matrix(2, 3) = -unit_z.dot(position);
matrix(3, 0) = 0.0;
matrix(3, 1) = 0.0;
matrix(3, 2) = 0.0;
matrix(3, 3) = 1.0;
return matrix;
}
void Camera::set_default_orientation()
{
double theta_rad = Geometry::deg2rad(-45.0);
double phi_rad = Geometry::deg2rad(45.0);
double sin_theta = ::sin(theta_rad);
Vec3d camera_pos = m_target + m_distance * Vec3d(sin_theta * ::sin(phi_rad), sin_theta * ::cos(phi_rad), ::cos(theta_rad));
m_view_matrix = Transform3d::Identity();
m_view_matrix.rotate(Eigen::AngleAxisd(theta_rad, Vec3d::UnitX())).rotate(Eigen::AngleAxisd(phi_rad, Vec3d::UnitZ())).translate(-camera_pos);
}
Vec3d Camera::validate_target(const Vec3d& target) const
{
BoundingBoxf3 test_box = m_scene_box;
test_box.translate(-m_scene_box.center());
// We may let this factor be customizable
static const double ScaleFactor = 1.5;
test_box.scale(ScaleFactor);
test_box.translate(m_scene_box.center());
return Vec3d(std::clamp(target(0), test_box.min(0), test_box.max(0)),
std::clamp(target(1), test_box.min(1), test_box.max(1)),
std::clamp(target(2), test_box.min(2), test_box.max(2)));
}
#endif // ENABLE_6DOF_CAMERA
} // GUI
} // Slic3r

View File

@ -30,21 +30,29 @@ struct Camera
Num_types
};
#if !ENABLE_6DOF_CAMERA
float phi;
bool requires_zoom_to_bed;
bool inverted_phi;
#endif // !ENABLE_6DOF_CAMERA
bool requires_zoom_to_bed;
private:
EType m_type;
Vec3d m_target;
#if !ENABLE_6DOF_CAMERA
float m_theta;
#endif // !ENABLE_6DOF_CAMERA
double m_zoom;
// Distance between camera position and camera target measured along the camera Z axis
mutable double m_distance;
mutable double m_gui_scale;
mutable std::array<int, 4> 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<double, double> m_frustrum_zs;
@ -66,8 +74,10 @@ public:
double get_distance() const { return m_distance; }
double get_gui_scale() const { return m_gui_scale; }
#if !ENABLE_6DOF_CAMERA
float get_theta() const { return m_theta; }
void set_theta(float theta, bool apply_limit);
#endif // !ENABLE_6DOF_CAMERA
double get_zoom() const { return m_zoom; }
void update_zoom(double delta_zoom);
@ -76,7 +86,11 @@ public:
const BoundingBoxf3& get_scene_box() const { return m_scene_box; }
void set_scene_box(const BoundingBoxf3& box) { m_scene_box = box; }
#if ENABLE_6DOF_CAMERA
void select_view(const std::string& direction);
#else
bool select_view(const std::string& direction);
#endif // ENABLE_6DOF_CAMERA
const std::array<int, 4>& get_viewport() const { return m_viewport; }
const Transform3d& get_view_matrix() const { return m_view_matrix; }
@ -110,6 +124,21 @@ public:
void debug_render() const;
#endif // ENABLE_CAMERA_STATISTICS
#if ENABLE_6DOF_CAMERA
// translate the camera in world space
void translate_world(const Vec3d& displacement);
// rotate the camera on a sphere having center == m_target and radius == m_distance
// using the given variations of spherical coordinates
void rotate_on_sphere(double delta_azimut_rad, double delta_zenit_rad, bool apply_limit);
// rotate the camera around three axes parallel to the camera local axes and passing through m_target
void rotate_local_around_target(const Vec3d& rotation_rad);
// returns true if the camera z axis (forward) is pointing in the negative direction of the world z axis
bool is_looking_downward() const;
#endif // ENABLE_6DOF_CAMERA
private:
// returns tight values for nearZ and farZ plane around the given bounding box
// the camera MUST be outside of the bounding box in eye coordinate of the given box
@ -121,6 +150,12 @@ private:
double calc_zoom_to_bounding_box_factor(const BoundingBoxf3& box, int canvas_w, int canvas_h) const;
#endif // ENABLE_THUMBNAIL_GENERATOR
void set_distance(double distance) const;
#if ENABLE_6DOF_CAMERA
Transform3d look_at(const Vec3d& position, const Vec3d& target, const Vec3d& up) const;
void set_default_orientation();
Vec3d validate_target(const Vec3d& target) const;
#endif // ENABLE_6DOF_CAMERA
};
} // GUI

View File

@ -1792,8 +1792,14 @@ void GLCanvas3D::zoom_to_selection()
void GLCanvas3D::select_view(const std::string& direction)
{
#if ENABLE_6DOF_CAMERA
m_camera.select_view(direction);
if (m_canvas != nullptr)
m_canvas->Refresh();
#else
if (m_camera.select_view(direction) && (m_canvas != nullptr))
m_canvas->Refresh();
#endif // ENABLE_6DOF_CAMERA
}
void GLCanvas3D::update_volumes_colors_by_extruder()
@ -1850,10 +1856,12 @@ void GLCanvas3D::render()
GLfloat position_top[4] = { -0.5f, -0.5f, 1.0f, 0.0f };
glsafe(::glLightfv(GL_LIGHT0, GL_POSITION, position_top));
#if !ENABLE_6DOF_CAMERA
float theta = m_camera.get_theta();
if (theta > 180.f)
// absolute value of the rotation
theta = 360.f - theta;
#endif // !ENABLE_6DOF_CAMERA
wxGetApp().imgui()->new_frame();
@ -1878,7 +1886,11 @@ void GLCanvas3D::render()
_render_objects();
_render_sla_slices();
_render_selection();
#if ENABLE_6DOF_CAMERA
_render_bed(!m_camera.is_looking_downward(), true);
#else
_render_bed(theta, true);
#endif // ENABLE_6DOF_CAMERA
#if ENABLE_RENDER_SELECTION_CENTER
_render_selection_center();
@ -3213,7 +3225,11 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
// we do not want to translate objects if the user just clicked on an object while pressing shift to remove it from the selection and then drag
if (m_selection.contains_volume(get_first_hover_volume_idx()))
{
#if ENABLE_6DOF_CAMERA
if (std::abs(m_camera.get_dir_forward()(2)) < EPSILON)
#else
if (m_camera.get_theta() == 90.0f)
#endif // ENABLE_6DOF_CAMERA
{
// side view -> move selected volumes orthogonally to camera view direction
Linef3 ray = mouse_ray(pos);
@ -3273,9 +3289,15 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
if (m_hover_volume_idxs.empty() && m_mouse.is_start_position_3D_defined())
{
const Vec3d& orig = m_mouse.drag.start_position_3D;
#if ENABLE_6DOF_CAMERA
m_camera.rotate_on_sphere(Geometry::deg2rad((pos(0) - orig(0))* (double)TRACKBALLSIZE),
Geometry::deg2rad((pos(1) - orig(1))* (double)TRACKBALLSIZE),
wxGetApp().preset_bundle->printers.get_edited_preset().printer_technology() != ptSLA);
#else
float sign = m_camera.inverted_phi ? -1.0f : 1.0f;
m_camera.phi += sign * ((float)pos(0) - (float)orig(0)) * TRACKBALLSIZE;
m_camera.set_theta(m_camera.get_theta() - ((float)pos(1) - (float)orig(1)) * TRACKBALLSIZE, wxGetApp().preset_bundle->printers.get_edited_preset().printer_technology() != ptSLA);
#endif // ENABLE_6DOF_CAMERA
m_dirty = true;
}
m_mouse.drag.start_position_3D = Vec3d((double)pos(0), (double)pos(1), 0.0);
@ -3325,9 +3347,11 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt)
if (!evt.ShiftDown() && m_picking_enabled)
deselect_all();
}
#if !ENABLE_6DOF_CAMERA
else if (evt.LeftUp() && m_mouse.dragging)
// Flips X mouse deltas if bed is upside down
m_camera.inverted_phi = (m_camera.get_dir_up()(2) < 0.0);
#endif // !ENABLE_6DOF_CAMERA
else if (evt.RightUp())
{
m_mouse.position = pos.cast<double>();
@ -3932,6 +3956,9 @@ void GLCanvas3D::_render_thumbnail_internal(ThumbnailData& thumbnail_data, bool
Camera camera;
camera.set_type(Camera::Ortho);
#if ENABLE_6DOF_CAMERA
camera.set_scene_box(scene_bounding_box());
#endif // ENABLE_6DOF_CAMERA
camera.zoom_to_volumes(visible_volumes, thumbnail_data.width, thumbnail_data.height);
camera.apply_viewport(0, 0, thumbnail_data.width, thumbnail_data.height);
camera.apply_view_matrix();
@ -3982,7 +4009,11 @@ void GLCanvas3D::_render_thumbnail_internal(ThumbnailData& thumbnail_data, bool
glsafe(::glDisable(GL_DEPTH_TEST));
if (show_bed)
#if ENABLE_6DOF_CAMERA
_render_bed(!camera.is_looking_downward(), false);
#else
_render_bed(camera.get_theta(), false);
#endif // ENABLE_6DOF_CAMERA
if (transparent_background)
glsafe(::glClearColor(1.0f, 1.0f, 1.0f, 1.0f));

View File

@ -168,12 +168,17 @@ bool Mouse3DController::State::apply(Camera& camera)
if (has_rotation())
{
#if ENABLE_6DOF_CAMERA
Vec3d rotation = (m_rotation_params.scale * m_rotation.queue.front()).cast<double>();
camera.rotate_local_around_target(Vec3d(Geometry::deg2rad(rotation(0)), Geometry::deg2rad(-rotation(2)), Geometry::deg2rad(-rotation(1))));
#else
const Vec3f& rotation = m_rotation.queue.front();
float theta = m_rotation_params.scale * rotation(0);
float phi = m_rotation_params.scale * rotation(2);
float sign = camera.inverted_phi ? -1.0f : 1.0f;
camera.phi += sign * phi;
camera.set_theta(camera.get_theta() + theta, wxGetApp().preset_bundle->printers.get_edited_preset().printer_technology() != ptSLA);
#endif // ENABLE_6DOF_CAMERA
m_rotation.queue.pop();
ret = true;
}
@ -877,9 +882,15 @@ bool Mouse3DController::handle_packet_translation(const DataPacket& packet)
bool Mouse3DController::handle_packet_rotation(const DataPacket& packet, unsigned int first_byte)
{
double deadzone = (double)m_state.get_rotation_deadzone();
#if ENABLE_6DOF_CAMERA
Vec3f rotation((float)convert_input(packet[first_byte + 0], packet[first_byte + 1], deadzone),
(float)convert_input(packet[first_byte + 2], packet[first_byte + 3], deadzone),
(float)convert_input(packet[first_byte + 4], packet[first_byte + 5], deadzone));
#else
Vec3f rotation(-(float)convert_input(packet[first_byte + 0], packet[first_byte + 1], deadzone),
(float)convert_input(packet[first_byte + 2], packet[first_byte + 3], deadzone),
-(float)convert_input(packet[first_byte + 4], packet[first_byte + 5], deadzone));
#endif // ENABLE_6DOF_CAMERA
if (!rotation.isApprox(Vec3f::Zero()))
{

View File

@ -2004,7 +2004,11 @@ void Selection::render_sidebar_layers_hints(const std::string& sidebar_field) co
const float max_y = box.max(1) + Margin;
// view dependend order of rendering to keep correct transparency
#if ENABLE_6DOF_CAMERA
bool camera_on_top = wxGetApp().plater()->get_camera().is_looking_downward();
#else
bool camera_on_top = wxGetApp().plater()->get_camera().get_theta() <= 90.0f;
#endif // ENABLE_6DOF_CAMERA
float z1 = camera_on_top ? min_z : max_z;
float z2 = camera_on_top ? max_z : min_z;