2019-03-06 14:46:19 +00:00
|
|
|
#include "libslic3r/libslic3r.h"
|
|
|
|
|
|
|
|
#include "Camera.hpp"
|
2019-04-01 08:00:10 +00:00
|
|
|
#include "3DScene.hpp"
|
2019-05-20 07:39:57 +00:00
|
|
|
#if ENABLE_CAMERA_STATISTICS
|
|
|
|
#include "GUI_App.hpp"
|
|
|
|
#endif // ENABLE_CAMERA_STATISTICS
|
2019-04-01 08:00:10 +00:00
|
|
|
|
|
|
|
#include <GL/glew.h>
|
2019-03-06 14:46:19 +00:00
|
|
|
|
|
|
|
static const float GIMBALL_LOCK_THETA_MAX = 180.0f;
|
|
|
|
|
2019-04-01 08:00:10 +00:00
|
|
|
// 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 };
|
|
|
|
static const float VIEW_RIGHT[2] = { -90.0f, 90.0f };
|
|
|
|
static const float VIEW_TOP[2] = { 0.0f, 0.0f };
|
|
|
|
static const float VIEW_BOTTOM[2] = { 0.0f, 180.0f };
|
|
|
|
static const float VIEW_FRONT[2] = { 0.0f, 90.0f };
|
|
|
|
static const float VIEW_REAR[2] = { 180.0f, 90.0f };
|
|
|
|
|
2019-03-06 14:46:19 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
namespace GUI {
|
|
|
|
|
2019-06-13 07:12:44 +00:00
|
|
|
const float Camera::DefaultDistance = 1000.0f;
|
2019-06-14 08:38:09 +00:00
|
|
|
double Camera::FrustrumMinZSize = 50.0;
|
|
|
|
double Camera::FrustrumZMargin = 10.0;
|
2019-06-13 07:12:44 +00:00
|
|
|
|
2019-03-06 14:46:19 +00:00
|
|
|
Camera::Camera()
|
|
|
|
: type(Ortho)
|
|
|
|
, zoom(1.0f)
|
|
|
|
, phi(45.0f)
|
2019-06-13 07:12:44 +00:00
|
|
|
, distance(DefaultDistance)
|
2019-03-06 14:46:19 +00:00
|
|
|
, requires_zoom_to_bed(false)
|
2019-04-30 13:09:25 +00:00
|
|
|
, inverted_phi(false)
|
2019-03-06 14:46:19 +00:00
|
|
|
, m_theta(45.0f)
|
|
|
|
, m_target(Vec3d::Zero())
|
2019-05-16 13:54:11 +00:00
|
|
|
, m_view_matrix(Transform3d::Identity())
|
|
|
|
, m_projection_matrix(Transform3d::Identity())
|
2019-03-06 14:46:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Camera::get_type_as_string() const
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case Unknown:
|
|
|
|
return "unknown";
|
|
|
|
// case Perspective:
|
|
|
|
// return "perspective";
|
|
|
|
case Ortho:
|
2019-06-13 08:24:19 +00:00
|
|
|
return "orthographic";
|
2019-03-06 14:46:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::set_target(const Vec3d& target)
|
|
|
|
{
|
|
|
|
m_target = target;
|
|
|
|
m_target(0) = clamp(m_scene_box.min(0), m_scene_box.max(0), m_target(0));
|
|
|
|
m_target(1) = clamp(m_scene_box.min(1), m_scene_box.max(1), m_target(1));
|
|
|
|
m_target(2) = clamp(m_scene_box.min(2), m_scene_box.max(2), m_target(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 08:00:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::apply_viewport(int x, int y, unsigned int w, unsigned int h) const
|
|
|
|
{
|
|
|
|
glsafe(::glViewport(0, 0, w, h));
|
|
|
|
glsafe(::glGetIntegerv(GL_VIEWPORT, m_viewport.data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::apply_view_matrix() const
|
|
|
|
{
|
2019-06-13 07:12:44 +00:00
|
|
|
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 + (double)distance * Vec3d(sin_theta * ::sin(phi_rad), sin_theta * ::cos(phi_rad), ::cos(theta_rad));
|
|
|
|
|
2019-04-01 08:00:10 +00:00
|
|
|
glsafe(::glMatrixMode(GL_MODELVIEW));
|
|
|
|
glsafe(::glLoadIdentity());
|
|
|
|
|
|
|
|
glsafe(::glRotatef(-m_theta, 1.0f, 0.0f, 0.0f)); // pitch
|
2019-05-16 13:54:11 +00:00
|
|
|
glsafe(::glRotatef(phi, 0.0f, 0.0f, 1.0f)); // yaw
|
2019-06-13 07:12:44 +00:00
|
|
|
|
|
|
|
glsafe(::glTranslated(-camera_pos(0), -camera_pos(1), -camera_pos(2)));
|
2019-04-01 08:00:10 +00:00
|
|
|
|
|
|
|
glsafe(::glGetDoublev(GL_MODELVIEW_MATRIX, m_view_matrix.data()));
|
|
|
|
}
|
|
|
|
|
2019-05-16 13:54:11 +00:00
|
|
|
void Camera::apply_projection(const BoundingBoxf3& box) const
|
|
|
|
{
|
2019-06-14 08:38:09 +00:00
|
|
|
m_frustrum_zs = calc_tight_frustrum_zs_around(box);
|
|
|
|
|
2019-05-16 13:54:11 +00:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case Ortho:
|
|
|
|
{
|
|
|
|
double w2 = (double)m_viewport[2];
|
|
|
|
double h2 = (double)m_viewport[3];
|
|
|
|
double two_zoom = 2.0 * zoom;
|
|
|
|
if (two_zoom != 0.0)
|
|
|
|
{
|
|
|
|
double inv_two_zoom = 1.0 / two_zoom;
|
|
|
|
w2 *= inv_two_zoom;
|
|
|
|
h2 *= inv_two_zoom;
|
|
|
|
}
|
|
|
|
|
2019-06-14 08:38:09 +00:00
|
|
|
apply_ortho_projection(-w2, w2, -h2, h2, m_frustrum_zs.first, m_frustrum_zs.second);
|
2019-05-16 13:54:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// case Perspective:
|
|
|
|
// {
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 07:39:57 +00:00
|
|
|
#if ENABLE_CAMERA_STATISTICS
|
|
|
|
void Camera::debug_render() const
|
|
|
|
{
|
|
|
|
ImGuiWrapper& imgui = *wxGetApp().imgui();
|
|
|
|
imgui.set_next_window_bg_alpha(0.5f);
|
|
|
|
imgui.begin(std::string("Camera statistics"), ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse);
|
|
|
|
|
2019-06-13 08:24:19 +00:00
|
|
|
std::string type = get_type_as_string();
|
2019-05-20 07:39:57 +00:00
|
|
|
Vec3f position = get_position().cast<float>();
|
|
|
|
Vec3f target = m_target.cast<float>();
|
|
|
|
Vec3f forward = get_dir_forward().cast<float>();
|
|
|
|
Vec3f right = get_dir_right().cast<float>();
|
|
|
|
Vec3f up = get_dir_up().cast<float>();
|
2019-06-14 08:38:09 +00:00
|
|
|
float nearZ = (float)m_frustrum_zs.first;
|
|
|
|
float farZ = (float)m_frustrum_zs.second;
|
2019-05-20 07:39:57 +00:00
|
|
|
|
2019-06-13 08:24:19 +00:00
|
|
|
ImGui::InputText("Type", const_cast<char*>(type.data()), type.length(), ImGuiInputTextFlags_ReadOnly);
|
|
|
|
ImGui::Separator();
|
2019-05-20 07:39:57 +00:00
|
|
|
ImGui::InputFloat3("Position", position.data(), "%.6f", ImGuiInputTextFlags_ReadOnly);
|
|
|
|
ImGui::InputFloat3("Target", target.data(), "%.6f", ImGuiInputTextFlags_ReadOnly);
|
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputFloat3("Forward", forward.data(), "%.6f", ImGuiInputTextFlags_ReadOnly);
|
|
|
|
ImGui::InputFloat3("Right", right.data(), "%.6f", ImGuiInputTextFlags_ReadOnly);
|
|
|
|
ImGui::InputFloat3("Up", up.data(), "%.6f", ImGuiInputTextFlags_ReadOnly);
|
2019-06-14 08:38:09 +00:00
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputFloat("Near Z", &nearZ, 0.0f, 0.0f, "%.6f", ImGuiInputTextFlags_ReadOnly);
|
|
|
|
ImGui::InputFloat("Far Z", &farZ, 0.0f, 0.0f, "%.6f", ImGuiInputTextFlags_ReadOnly);
|
2019-05-20 07:39:57 +00:00
|
|
|
imgui.end();
|
|
|
|
}
|
|
|
|
#endif // ENABLE_CAMERA_STATISTICS
|
|
|
|
|
2019-05-16 13:54:11 +00:00
|
|
|
void Camera::apply_ortho_projection(double x_min, double x_max, double y_min, double y_max, double z_min, double z_max) const
|
2019-04-01 08:00:10 +00:00
|
|
|
{
|
|
|
|
glsafe(::glMatrixMode(GL_PROJECTION));
|
|
|
|
glsafe(::glLoadIdentity());
|
|
|
|
|
|
|
|
glsafe(::glOrtho(x_min, x_max, y_min, y_max, z_min, z_max));
|
|
|
|
glsafe(::glGetDoublev(GL_PROJECTION_MATRIX, m_projection_matrix.data()));
|
|
|
|
|
|
|
|
glsafe(::glMatrixMode(GL_MODELVIEW));
|
|
|
|
}
|
|
|
|
|
2019-06-14 08:38:09 +00:00
|
|
|
std::pair<double, double> Camera::calc_tight_frustrum_zs_around(const BoundingBoxf3& box) const
|
|
|
|
{
|
|
|
|
std::pair<double, double> ret = std::make_pair(DBL_MAX, -DBL_MAX);
|
|
|
|
|
|
|
|
Vec3d bb_min = box.min;
|
|
|
|
Vec3d bb_max = box.max;
|
|
|
|
|
|
|
|
// bbox vertices in world space
|
|
|
|
std::vector<Vec3d> vertices;
|
|
|
|
vertices.reserve(8);
|
|
|
|
vertices.push_back(bb_min);
|
|
|
|
vertices.emplace_back(bb_max(0), bb_min(1), bb_min(2));
|
|
|
|
vertices.emplace_back(bb_max(0), bb_max(1), bb_min(2));
|
|
|
|
vertices.emplace_back(bb_min(0), bb_max(1), bb_min(2));
|
|
|
|
vertices.emplace_back(bb_min(0), bb_min(1), bb_max(2));
|
|
|
|
vertices.emplace_back(bb_max(0), bb_min(1), bb_max(2));
|
|
|
|
vertices.push_back(bb_max);
|
|
|
|
vertices.emplace_back(bb_min(0), bb_max(1), bb_max(2));
|
|
|
|
|
|
|
|
// set the Z range in eye coordinates (only negative Zs are in front of the camera)
|
|
|
|
for (const Vec3d& v : vertices)
|
|
|
|
{
|
|
|
|
// ensure non-negative values
|
|
|
|
double z = std::max(-(m_view_matrix * v)(2), 0.0);
|
|
|
|
ret.first = std::min(ret.first, z);
|
|
|
|
ret.second = std::max(ret.second, z);
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply margin
|
|
|
|
ret.first -= FrustrumZMargin;
|
|
|
|
ret.second += FrustrumZMargin;
|
|
|
|
|
|
|
|
// ensure min size
|
|
|
|
if (ret.second - ret.first < FrustrumMinZSize)
|
|
|
|
ret.second = ret.first + FrustrumMinZSize;
|
|
|
|
|
|
|
|
assert(ret.first > 0.0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-06 14:46:19 +00:00
|
|
|
} // GUI
|
|
|
|
} // Slic3r
|
|
|
|
|