ENABLE_6DOF_CAMERA set as default

This commit is contained in:
Enrico Turri 2020-02-13 12:13:54 +01:00
parent 904263d231
commit 847efec2ed
10 changed files with 63 additions and 396 deletions

View file

@ -7,17 +7,11 @@
#include "GUI_App.hpp"
#include "AppConfig.hpp"
#if ENABLE_CAMERA_STATISTICS
#if ENABLE_6DOF_CAMERA
#include "Mouse3DController.hpp"
#endif // ENABLE_6DOF_CAMERA
#endif // ENABLE_CAMERA_STATISTICS
#include <GL/glew.h>
#if !ENABLE_6DOF_CAMERA
static const float GIMBALL_LOCK_THETA_MAX = 180.0f;
#endif // !ENABLE_6DOF_CAMERA
// phi / theta angles to orient the camera.
static const float VIEW_DEFAULT[2] = { 45.0f, 45.0f };
static const float VIEW_LEFT[2] = { 90.0f, 90.0f };
@ -41,29 +35,17 @@ 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_zenit(45.0f)
#else
, 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
@ -103,36 +85,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
static const double ScaleFactor = 1.5;
test_box.scale(ScaleFactor);
test_box.translate(m_scene_box.center());
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)
m_theta = clamp(0.0f, GIMBALL_LOCK_THETA_MAX, theta);
else
{
m_theta = fmod(theta, 360.0f);
if (m_theta < 0.0f)
m_theta += 360.0f;
}
}
#endif // !ENABLE_6DOF_CAMERA
void Camera::update_zoom(double delta_zoom)
{
set_zoom(m_zoom / (1.0 - std::max(std::min(delta_zoom, 4.0), -4.0) * 0.1));
@ -149,7 +104,6 @@ void Camera::set_zoom(double zoom)
m_zoom = std::min(zoom, max_zoom());
}
#if ENABLE_6DOF_CAMERA
void Camera::select_view(const std::string& direction)
{
if (direction == "iso")
@ -167,36 +121,6 @@ void Camera::select_view(const std::string& direction)
else if (direction == "rear")
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;
if (direction == "iso")
dir_vec = VIEW_DEFAULT;
else if (direction == "left")
dir_vec = VIEW_LEFT;
else if (direction == "right")
dir_vec = VIEW_RIGHT;
else if (direction == "top")
dir_vec = VIEW_TOP;
else if (direction == "bottom")
dir_vec = VIEW_BOTTOM;
else if (direction == "front")
dir_vec = VIEW_FRONT;
else if (direction == "rear")
dir_vec = VIEW_REAR;
if (dir_vec != nullptr)
{
phi = dir_vec[0];
set_theta(dir_vec[1], false);
return true;
}
else
return false;
}
#endif // ENABLE_6DOF_CAMERA
double Camera::get_fov() const
{
@ -218,102 +142,53 @@ 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
{
#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)
if (near_z > 0.0)
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);
w = 0.5 * (double)m_viewport[2];
h = 0.5 * (double)m_viewport[3];
double inv_zoom = get_inv_zoom();
w *= inv_zoom;
h *= inv_zoom;
switch (m_type)
{
m_frustrum_zs = calc_tight_frustrum_zs_around(box);
#endif // !ENABLE_6DOF_CAMERA
if (near_z > 0.0)
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);
w = 0.5 * (double)m_viewport[2];
h = 0.5 * (double)m_viewport[3];
double inv_zoom = get_inv_zoom();
w *= inv_zoom;
h *= inv_zoom;
switch (m_type)
{
default:
case Ortho:
{
m_gui_scale = 1.0;
break;
}
case Perspective:
{
// scale near plane to keep w and h constant on the plane at z = m_distance
double scale = m_frustrum_zs.first / m_distance;
w *= scale;
h *= scale;
m_gui_scale = scale;
break;
}
}
#if !ENABLE_6DOF_CAMERA
if (m_type == Perspective)
{
double fov_deg = Geometry::rad2deg(2.0 * std::atan(h / m_frustrum_zs.first));
// adjust camera distance to keep fov in a limited range
if (fov_deg > MaxFovDeg)
{
double delta_z = h / ::tan(0.5 * Geometry::deg2rad(MaxFovDeg)) - m_frustrum_zs.first;
if (delta_z > 0.001)
set_distance(m_distance + delta_z);
else
break;
}
else
break;
}
else
break;
default:
case Ortho:
{
m_gui_scale = 1.0;
break;
}
case Perspective:
{
// scale near plane to keep w and h constant on the plane at z = m_distance
double scale = m_frustrum_zs.first / m_distance;
w *= scale;
h *= scale;
m_gui_scale = scale;
break;
}
}
#endif // !ENABLE_6DOF_CAMERA
glsafe(::glMatrixMode(GL_PROJECTION));
glsafe(::glLoadIdentity());
@ -338,22 +213,14 @@ 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
@ -361,36 +228,20 @@ 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
}
}
#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;
// 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
@ -402,18 +253,15 @@ void Camera::debug_render() const
imgui.begin(std::string("Camera statistics"), ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse);
std::string type = get_type_as_string();
#if ENABLE_6DOF_CAMERA
if (wxGetApp().plater()->get_mouse3d_controller().is_running() || (wxGetApp().app_config->get("use_free_camera") == "1"))
type += "/free";
else
type += "/constrained";
#endif // ENABLE_6DOF_CAMERA
Vec3f position = get_position().cast<float>();
Vec3f target = m_target.cast<float>();
float distance = (float)get_distance();
#if ENABLE_6DOF_CAMERA
float zenit = (float)m_zenit;
#endif // ENABLE_6DOF_CAMERA
Vec3f forward = get_dir_forward().cast<float>();
Vec3f right = get_dir_right().cast<float>();
Vec3f up = get_dir_up().cast<float>();
@ -430,10 +278,8 @@ void Camera::debug_render() const
ImGui::InputFloat3("Position", position.data(), "%.6f", ImGuiInputTextFlags_ReadOnly);
ImGui::InputFloat3("Target", target.data(), "%.6f", ImGuiInputTextFlags_ReadOnly);
ImGui::InputFloat("Distance", &distance, 0.0f, 0.0f, "%.6f", ImGuiInputTextFlags_ReadOnly);
#if ENABLE_6DOF_CAMERA
ImGui::Separator();
ImGui::InputFloat("Zenit", &zenit, 0.0f, 0.0f, "%.6f", ImGuiInputTextFlags_ReadOnly);
#endif // ENABLE_6DOF_CAMERA
ImGui::Separator();
ImGui::InputFloat3("Forward", forward.data(), "%.6f", ImGuiInputTextFlags_ReadOnly);
ImGui::InputFloat3("Right", right.data(), "%.6f", ImGuiInputTextFlags_ReadOnly);
@ -453,7 +299,6 @@ 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);
@ -524,15 +369,10 @@ void Camera::rotate_local_around_pivot(const Vec3d& rotation_rad, const Vec3d& p
translate_world(center);
update_zenit();
}
#endif // ENABLE_6DOF_CAMERA
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, m_viewport[2], m_viewport[3]);
#endif // ENABLE_6DOF_CAMERA
}
std::pair<double, double> Camera::calc_tight_frustrum_zs_around(const BoundingBoxf3& box) const
@ -540,62 +380,45 @@ std::pair<double, double> Camera::calc_tight_frustrum_zs_around(const BoundingBo
std::pair<double, double> ret;
auto& [near_z, far_z] = ret;
#if !ENABLE_6DOF_CAMERA
while (true)
// box in eye space
BoundingBoxf3 eye_box = box.transformed(m_view_matrix);
near_z = -eye_box.max(2);
far_z = -eye_box.min(2);
// apply margin
near_z -= FrustrumZMargin;
far_z += FrustrumZMargin;
// ensure min size
if (far_z - near_z < FrustrumMinZRange)
{
#endif // !ENABLE_6DOF_CAMERA
// box in eye space
BoundingBoxf3 eye_box = box.transformed(m_view_matrix);
near_z = -eye_box.max(2);
far_z = -eye_box.min(2);
// apply margin
near_z -= FrustrumZMargin;
far_z += FrustrumZMargin;
// ensure min size
if (far_z - near_z < FrustrumMinZRange)
{
double mid_z = 0.5 * (near_z + far_z);
double half_size = 0.5 * FrustrumMinZRange;
near_z = mid_z - half_size;
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
set_distance(m_distance + FrustrumMinNearZ - near_z);
double mid_z = 0.5 * (near_z + far_z);
double half_size = 0.5 * FrustrumMinZRange;
near_z = mid_z - half_size;
far_z = mid_z + half_size;
}
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;
}
#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
{
@ -606,11 +429,6 @@ 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();
Vec3d forward = get_dir_forward();
@ -666,19 +484,11 @@ 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;
@ -686,11 +496,6 @@ 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();
Vec3d forward = get_dir_forward();
@ -741,29 +546,19 @@ 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
void Camera::look_at(const Vec3d& position, const Vec3d& target, const Vec3d& up)
{
Vec3d unit_z = (position - target).normalized();
@ -825,7 +620,6 @@ void Camera::update_zenit()
{
m_zenit = Geometry::rad2deg(0.5 * M_PI - std::acos(std::clamp(-get_dir_forward().dot(Vec3d::UnitZ()), -1.0, 1.0)));
}
#endif // ENABLE_6DOF_CAMERA
} // GUI
} // Slic3r