Removed unnecessary const_cast calls inside Camera.

This commit is contained in:
Vojtech Bubnik 2021-06-02 12:19:55 +02:00
parent a1283277d3
commit 2d9953069a
2 changed files with 17 additions and 18 deletions

View file

@ -116,23 +116,22 @@ void Camera::apply_view_matrix() const
glsafe(::glMultMatrixd(m_view_matrix.data())); glsafe(::glMultMatrixd(m_view_matrix.data()));
} }
void Camera::apply_projection(const BoundingBoxf3& box, double near_z, double far_z) const void Camera::apply_projection(const BoundingBoxf3& box, double near_z, double far_z)
{ {
double w = 0.0; double w = 0.0;
double h = 0.0; double h = 0.0;
const double old_distance = m_distance; const double old_distance = m_distance;
std::pair<double, double>* frustrum_zs = const_cast<std::pair<double, double>*>(&m_frustrum_zs); m_frustrum_zs = calc_tight_frustrum_zs_around(box);
*frustrum_zs = calc_tight_frustrum_zs_around(box);
if (m_distance != old_distance) if (m_distance != old_distance)
// the camera has been moved re-apply view matrix // the camera has been moved re-apply view matrix
apply_view_matrix(); apply_view_matrix();
if (near_z > 0.0) if (near_z > 0.0)
frustrum_zs->first = std::max(std::min(frustrum_zs->first, near_z), FrustrumMinNearZ); m_frustrum_zs.first = std::max(std::min(m_frustrum_zs.first, near_z), FrustrumMinNearZ);
if (far_z > 0.0) if (far_z > 0.0)
frustrum_zs->second = std::max(frustrum_zs->second, far_z); m_frustrum_zs.second = std::max(m_frustrum_zs.second, far_z);
w = 0.5 * (double)m_viewport[2]; w = 0.5 * (double)m_viewport[2];
h = 0.5 * (double)m_viewport[3]; h = 0.5 * (double)m_viewport[3];
@ -146,16 +145,16 @@ void Camera::apply_projection(const BoundingBoxf3& box, double near_z, double fa
default: default:
case Ortho: case Ortho:
{ {
*const_cast<double*>(&m_gui_scale) = 1.0; m_gui_scale = 1.0;
break; break;
} }
case Perspective: case Perspective:
{ {
// scale near plane to keep w and h constant on the plane at z = m_distance // scale near plane to keep w and h constant on the plane at z = m_distance
const double scale = frustrum_zs->first / m_distance; const double scale = m_frustrum_zs.first / m_distance;
w *= scale; w *= scale;
h *= scale; h *= scale;
*const_cast<double*>(&m_gui_scale) = scale; m_gui_scale = scale;
break; break;
} }
} }
@ -168,17 +167,17 @@ void Camera::apply_projection(const BoundingBoxf3& box, double near_z, double fa
default: default:
case Ortho: case Ortho:
{ {
glsafe(::glOrtho(-w, w, -h, h, frustrum_zs->first, frustrum_zs->second)); glsafe(::glOrtho(-w, w, -h, h, m_frustrum_zs.first, m_frustrum_zs.second));
break; break;
} }
case Perspective: case Perspective:
{ {
glsafe(::glFrustum(-w, w, -h, h, frustrum_zs->first, frustrum_zs->second)); glsafe(::glFrustum(-w, w, -h, h, m_frustrum_zs.first, m_frustrum_zs.second));
break; break;
} }
} }
glsafe(::glGetDoublev(GL_PROJECTION_MATRIX, const_cast<Transform3d*>(&m_projection_matrix)->data())); glsafe(::glGetDoublev(GL_PROJECTION_MATRIX, m_projection_matrix.data()));
glsafe(::glMatrixMode(GL_MODELVIEW)); glsafe(::glMatrixMode(GL_MODELVIEW));
} }
@ -292,7 +291,7 @@ void Camera::rotate_local_around_target(const Vec3d& rotation_rad)
} }
} }
std::pair<double, double> Camera::calc_tight_frustrum_zs_around(const BoundingBoxf3& box) const std::pair<double, double> Camera::calc_tight_frustrum_zs_around(const BoundingBoxf3& box)
{ {
std::pair<double, double> ret; std::pair<double, double> ret;
auto& [near_z, far_z] = ret; auto& [near_z, far_z] = ret;
@ -448,11 +447,11 @@ double Camera::calc_zoom_to_volumes_factor(const GLVolumePtrs& volumes, Vec3d& c
return std::min((double)m_viewport[2] / dx, (double)m_viewport[3] / dy); return std::min((double)m_viewport[2] / dx, (double)m_viewport[3] / dy);
} }
void Camera::set_distance(double distance) const void Camera::set_distance(double distance)
{ {
if (m_distance != distance) { if (m_distance != distance) {
const_cast<Transform3d*>(&m_view_matrix)->translate((distance - m_distance) * get_dir_forward()); m_view_matrix.translate((distance - m_distance) * get_dir_forward());
*const_cast<double*>(&m_distance) = distance; m_distance = distance;
} }
} }

View file

@ -95,7 +95,7 @@ public:
void apply_view_matrix() const; void apply_view_matrix() const;
// Calculates and applies the projection matrix tighting the frustrum z range around the given box. // 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) // 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) const; void apply_projection(const BoundingBoxf3& box, double near_z = -1.0, double far_z = -1.0);
void zoom_to_box(const BoundingBoxf3& box, double margin_factor = DefaultZoomToBoxMarginFactor); void zoom_to_box(const BoundingBoxf3& box, double margin_factor = DefaultZoomToBoxMarginFactor);
void zoom_to_volumes(const GLVolumePtrs& volumes, double margin_factor = DefaultZoomToVolumesMarginFactor); void zoom_to_volumes(const GLVolumePtrs& volumes, double margin_factor = DefaultZoomToVolumesMarginFactor);
@ -132,10 +132,10 @@ public:
private: private:
// returns tight values for nearZ and farZ plane around the given bounding box // 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 // the camera MUST be outside of the bounding box in eye coordinate of the given box
std::pair<double, double> calc_tight_frustrum_zs_around(const BoundingBoxf3& box) const; std::pair<double, double> calc_tight_frustrum_zs_around(const BoundingBoxf3& box);
double calc_zoom_to_bounding_box_factor(const BoundingBoxf3& box, double margin_factor = DefaultZoomToBoxMarginFactor) const; 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; double calc_zoom_to_volumes_factor(const GLVolumePtrs& volumes, Vec3d& center, double margin_factor = DefaultZoomToVolumesMarginFactor) const;
void set_distance(double distance) const; void set_distance(double distance);
void set_default_orientation(); void set_default_orientation();
Vec3d validate_target(const Vec3d& target) const; Vec3d validate_target(const Vec3d& target) const;