Tech ENABLE_LEGACY_OPENGL_REMOVAL - Calculation of camera projection matrix

This commit is contained in:
enricoturri1966 2022-03-22 15:20:51 +01:00
parent 52cc68839a
commit a0630420d9
3 changed files with 53 additions and 11 deletions

View File

@ -109,12 +109,14 @@ void Camera::apply_viewport(int x, int y, unsigned int w, unsigned int h)
glsafe(::glGetIntegerv(GL_VIEWPORT, m_viewport.data()));
}
#if !ENABLE_LEGACY_OPENGL_REMOVAL
void Camera::apply_view_matrix()
{
glsafe(::glMatrixMode(GL_MODELVIEW));
glsafe(::glLoadIdentity());
glsafe(::glMultMatrixd(m_view_matrix.data()));
}
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
void Camera::apply_projection(const BoundingBoxf3& box, double near_z, double far_z)
{
@ -123,9 +125,11 @@ void Camera::apply_projection(const BoundingBoxf3& box, double near_z, double fa
const double old_distance = m_distance;
m_frustrum_zs = calc_tight_frustrum_zs_around(box);
#if !ENABLE_LEGACY_OPENGL_REMOVAL
if (m_distance != old_distance)
// the camera has been moved re-apply view matrix
apply_view_matrix();
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
if (near_z > 0.0)
m_frustrum_zs.first = std::max(std::min(m_frustrum_zs.first, near_z), FrustrumMinNearZ);
@ -159,6 +163,35 @@ void Camera::apply_projection(const BoundingBoxf3& box, double near_z, double fa
}
}
#if ENABLE_LEGACY_OPENGL_REMOVAL
switch (m_type)
{
default:
case EType::Ortho:
{
const double dz = m_frustrum_zs.second - m_frustrum_zs.first;
const double zz = m_frustrum_zs.first + m_frustrum_zs.second;
m_projection_matrix.matrix() << 1.0 / w, 0.0, 0.0, 0.0,
0.0, 1.0 / h, 0.0, 0.0,
0.0, 0.0, -2.0 / dz, -zz / dz,
0.0, 0.0, 0.0, 1.0;
break;
}
case EType::Perspective:
{
const double n = m_frustrum_zs.first;
const double f = m_frustrum_zs.second;
const double dz = f - n;
const double zz = n + f;
const double fn = n * f;
m_projection_matrix.matrix() << n / w, 0.0, 0.0, 0.0,
0.0, n / h, 0.0, 0.0,
0.0, 0.0, -zz / dz, -2.0 * fn / dz,
0.0, 0.0, -1.0, 0.0;
break;
}
}
#else
glsafe(::glMatrixMode(GL_PROJECTION));
glsafe(::glLoadIdentity());
@ -179,6 +212,7 @@ void Camera::apply_projection(const BoundingBoxf3& box, double near_z, double fa
glsafe(::glGetDoublev(GL_PROJECTION_MATRIX, m_projection_matrix.data()));
glsafe(::glMatrixMode(GL_MODELVIEW));
#endif // ENABLE_LEGACY_OPENGL_REMOVAL
}
void Camera::zoom_to_box(const BoundingBoxf3& box, double margin_factor)
@ -298,8 +332,8 @@ std::pair<double, double> Camera::calc_tight_frustrum_zs_around(const BoundingBo
// box in eye space
const BoundingBoxf3 eye_box = box.transformed(m_view_matrix);
near_z = -eye_box.max(2);
far_z = -eye_box.min(2);
near_z = -eye_box.max.z();
far_z = -eye_box.min.z();
// apply margin
near_z -= FrustrumZMargin;
@ -465,19 +499,19 @@ void Camera::look_at(const Vec3d& position, const Vec3d& target, const Vec3d& up
m_distance = (position - target).norm();
const Vec3d new_position = m_target + m_distance * unit_z;
m_view_matrix(0, 0) = unit_x(0);
m_view_matrix(0, 1) = unit_x(1);
m_view_matrix(0, 2) = unit_x(2);
m_view_matrix(0, 0) = unit_x.x();
m_view_matrix(0, 1) = unit_x.y();
m_view_matrix(0, 2) = unit_x.z();
m_view_matrix(0, 3) = -unit_x.dot(new_position);
m_view_matrix(1, 0) = unit_y(0);
m_view_matrix(1, 1) = unit_y(1);
m_view_matrix(1, 2) = unit_y(2);
m_view_matrix(1, 0) = unit_y.x();
m_view_matrix(1, 1) = unit_y.y();
m_view_matrix(1, 2) = unit_y.z();
m_view_matrix(1, 3) = -unit_y.dot(new_position);
m_view_matrix(2, 0) = unit_z(0);
m_view_matrix(2, 1) = unit_z(1);
m_view_matrix(2, 2) = unit_z(2);
m_view_matrix(2, 0) = unit_z.x();
m_view_matrix(2, 1) = unit_z.y();
m_view_matrix(2, 2) = unit_z.z();
m_view_matrix(2, 3) = -unit_z.dot(new_position);
m_view_matrix(3, 0) = 0.0;

View File

@ -92,7 +92,9 @@ public:
double get_fov() const;
void apply_viewport(int x, int y, unsigned int w, unsigned int h);
#if !ENABLE_LEGACY_OPENGL_REMOVAL
void apply_view_matrix();
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
// Calculates and applies the projection matrix tighting the frustrum z range around the given box.
// If larger z span is needed, pass the desired values of near and far z (negative values are ignored)
void apply_projection(const BoundingBoxf3& box, double near_z = -1.0, double far_z = -1.0);

View File

@ -1662,13 +1662,17 @@ void GLCanvas3D::render()
camera.requires_zoom_to_bed = false;
}
#if !ENABLE_LEGACY_OPENGL_REMOVAL
camera.apply_view_matrix();
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
camera.apply_projection(_max_bounding_box(true, true));
#if !ENABLE_LEGACY_OPENGL_REMOVAL
GLfloat position_cam[4] = { 1.0f, 0.0f, 1.0f, 0.0f };
glsafe(::glLightfv(GL_LIGHT1, GL_POSITION, position_cam));
GLfloat position_top[4] = { -0.5f, -0.5f, 1.0f, 0.0f };
glsafe(::glLightfv(GL_LIGHT0, GL_POSITION, position_top));
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
wxGetApp().imgui()->new_frame();
@ -4561,7 +4565,9 @@ void GLCanvas3D::_render_thumbnail_internal(ThumbnailData& thumbnail_data, const
camera.set_scene_box(scene_bounding_box());
camera.apply_viewport(0, 0, thumbnail_data.width, thumbnail_data.height);
camera.zoom_to_box(volumes_box);
#if !ENABLE_LEGACY_OPENGL_REMOVAL
camera.apply_view_matrix();
#endif // !ENABLE_LEGACY_OPENGL_REMOVAL
#if ENABLE_GL_SHADERS_ATTRIBUTES
const Transform3d& view_matrix = camera.get_view_matrix();