Replace gluPerspective with own implementation

pull/3/head
Hleb Valoshka 2019-11-20 20:34:54 +03:00
parent 6b954fd7af
commit bf2f4cb236
3 changed files with 45 additions and 17 deletions

View File

@ -2479,9 +2479,7 @@ void Renderer::draw(const Observer& observer,
pixelSize = calcPixelSize(fov, (float) windowHeight);
// Set up the projection we'll use for rendering stars.
gluPerspective(fov,
(float) windowWidth / (float) windowHeight,
NEAR_DIST, FAR_DIST);
glMatrix(Perspective(fov, getAspectRatio(), NEAR_DIST, FAR_DIST));
// Set the modelview matrix
glMatrixMode(GL_MODELVIEW);
@ -3337,11 +3335,9 @@ void Renderer::draw(const Observer& observer,
// Set up a perspective projection using the current interval's near and
// far clip planes.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov,
(float) windowWidth / (float) windowHeight,
nearPlaneDistance,
farPlaneDistance);
glLoadMatrix(Perspective(fov, getAspectRatio(),
nearPlaneDistance,
farPlaneDistance));
glMatrixMode(GL_MODELVIEW);
Frustum intervalFrustum(degToRad(fov),
@ -3494,10 +3490,7 @@ void Renderer::draw(const Observer& observer,
renderForegroundAnnotations(FontNormal);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov,
(float) windowWidth / (float) windowHeight,
NEAR_DIST, FAR_DIST);
glLoadMatrix(Perspective(fov, getAspectRatio(), NEAR_DIST, FAR_DIST));
glMatrixMode(GL_MODELVIEW);
if (!selectionVisible && (renderFlags & ShowMarkers))
@ -6747,11 +6740,8 @@ void DSORenderer::process(DeepSkyObject* const & dso,
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(fov,
(float) wWidth / (float) wHeight,
nearZ,
farZ);
float t = (float) wWidth / (float) wHeight;
glLoadMatrix(Perspective(fov, t, nearZ, farZ));
glMatrixMode(GL_MODELVIEW);
}

View File

@ -77,6 +77,16 @@ inline void glMatrix(const Eigen::Matrix4d& m)
glMultMatrixd(m.data());
}
inline void glLoadMatrix(const Eigen::Matrix4f& m)
{
glLoadMatrixf(m.data());
}
inline void glLoadMatrix(const Eigen::Matrix4d& m)
{
glLoadMatrixd(m.data());
}
inline void glScale(const Eigen::Vector3f& scale)
{
glScalef(scale.x(), scale.y(), scale.z());

View File

@ -87,6 +87,34 @@ Project(const Eigen::Matrix<T, 3, 1>& from,
return true;
}
/*! Return an perspective projection matrix
*/
template<class T> Eigen::Matrix<T, 4, 4>
Perspective(T fovy, T aspect, T nearZ, T farZ)
{
if (aspect == T(0.0))
return Eigen::Matrix<T, 4, 4>::Identity();
T deltaZ = farZ - nearZ;
if (deltaZ == T(0.0))
return Eigen::Matrix<T, 4, 4>::Identity();
T angle = degToRad(fovy / 2);
T sine = sin(angle);
if (sine == T(0.0))
return Eigen::Matrix<T, 4, 4>::Identity();
T ctg = cos(angle) / sine;
Eigen::Matrix<T, 4, 4> m = Eigen::Matrix<T, 4, 4>::Identity();
m(0, 0) = ctg / aspect;
m(1, 1) = ctg;
m(2, 2) = -(farZ + nearZ) / deltaZ;
m(2, 3) = T(-2.0) * nearZ * farZ / deltaZ;
m(3, 2) = T(-1.0);
m(3, 3) = T(0.0);
return m;
}
}; // namespace celmath
#endif // _CELMATH_GEOMUTIL_H_