From 6b954fd7af7cbf7996efb2e35806d16acc7a2d5e Mon Sep 17 00:00:00 2001 From: Hleb Valoshka <375gnu@gmail.com> Date: Wed, 20 Nov 2019 19:21:05 +0300 Subject: [PATCH] Replace gluProject with own implementation --- src/celengine/render.cpp | 34 +++++++++++++--------------------- src/celengine/render.h | 4 ++-- src/celmath/geomutil.h | 25 +++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/celengine/render.cpp b/src/celengine/render.cpp index 5e38d579..ce04b611 100644 --- a/src/celengine/render.cpp +++ b/src/celengine/render.cpp @@ -1298,30 +1298,22 @@ void Renderer::addAnnotation(vector& annotations, float size, bool special) { - double winX, winY, winZ; GLint view[4] = { 0, 0, windowWidth, windowHeight }; - float depth = (float) (pos.x() * modelMatrix[2] + - pos.y() * modelMatrix[6] + - pos.z() * modelMatrix[10]); - if (gluProject(pos.x(), pos.y(), pos.z(), - modelMatrix, - projMatrix, - view, - &winX, &winY, &winZ) != GL_FALSE) + Vector3d win; + Vector3d posd = pos.cast(); + if (Project(posd, modelMatrix, projMatrix, view, win)) { + double depth = pos.x() * modelMatrix(2, 0) + + pos.y() * modelMatrix(2, 1) + + pos.z() * modelMatrix(2, 2); + win.z() = -depth; + Annotation a; - if (special) - { - if (markerRep == nullptr) - a.labelText = labelText; - } - else - { - a.labelText = labelText; - } + if (!special || markerRep == nullptr) + a.labelText = labelText; a.markerRep = markerRep; a.color = color; - a.position = Vector3f((float) winX, (float) winY, -depth); + a.position = win.cast(); a.halign = halign; a.valign = valign; a.size = size; @@ -2522,8 +2514,8 @@ void Renderer::draw(const Observer& observer, // Get the model matrix *before* translation. We'll use this for // positioning star and planet labels. - glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix); - glGetDoublev(GL_PROJECTION_MATRIX, projMatrix); + glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix.data()); + glGetDoublev(GL_PROJECTION_MATRIX, projMatrix.data()); clearSortedAnnotations(); diff --git a/src/celengine/render.h b/src/celengine/render.h index a637cda7..41464341 100644 --- a/src/celengine/render.h +++ b/src/celengine/render.h @@ -719,8 +719,8 @@ class Renderer std::vector lightSourceList; - double modelMatrix[16]; - double projMatrix[16]; + Eigen::Matrix4d modelMatrix; + Eigen::Matrix4d projMatrix; bool useCompressedTextures{ false }; unsigned int textureResolution; diff --git a/src/celmath/geomutil.h b/src/celmath/geomutil.h index 2e0fa6a8..0aa51936 100644 --- a/src/celmath/geomutil.h +++ b/src/celmath/geomutil.h @@ -62,6 +62,31 @@ LookAt(const Eigen::Matrix& from, const Eigen::Matrix& to, con return Eigen::Quaternion(m).conjugate(); } +/*! Project to screen space + */ +template bool +Project(const Eigen::Matrix& from, + const Eigen::Matrix& modelViewMatrix, + const Eigen::Matrix& projMatrix, + const int viewport[4], + Eigen::Matrix& to) +{ + Eigen::Matrix in(from.x(), from.y(), from.z(), T(1.0)); + Eigen::Matrix out = projMatrix * modelViewMatrix * in; + if (out.w() == T(0.0)) + return false; + + out = out.array() / out.w(); + // Map x, y and z to range 0-1 + out = T(0.5) + out.array() * T(0.5); + // Map x,y to viewport + out.x() = viewport[0] + out.x() * viewport[2]; + out.y() = viewport[1] + out.y() * viewport[3]; + + to = { out.x(), out.y(), out.z() }; + return true; +} + }; // namespace celmath #endif // _CELMATH_GEOMUTIL_H_