| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, Individual contributors, see AUTHORS file | |
| 6 |
| See: https://www.mrpt.org/Authors - All rights reserved. | |
| 7 |
| Released under BSD License. See: https://www.mrpt.org/License | |
| 8 |
+------------------------------------------------------------------------+ */ |
| 9 |
|
| 10 |
#include "opengl-precomp.h" // Precompiled header |
| 11 |
// |
| 12 |
#include <mrpt/containers/yaml.h> |
| 13 |
#include <mrpt/math/geometry.h> // crossProduct3D() |
| 14 |
#include <mrpt/math/ops_containers.h> // dotProduct() |
| 15 |
#include <mrpt/opengl/TLightParameters.h> |
| 16 |
#include <mrpt/opengl/TRenderMatrices.h> |
| 17 |
|
| 18 |
#include <Eigen/Dense> |
| 19 |
#include <cmath> // atan2 |
| 20 |
|
| 21 |
using namespace mrpt::opengl; |
| 22 |
|
| 23 |
mrpt::math::CMatrixFloat44 TRenderMatrices::OrthoProjectionMatrix( |
| 24 |
float left, float right, float bottom, float top, float znear, float zfar) |
| 25 |
{ |
| 26 |
ASSERT_GT_(zfar, znear); |
| 27 |
|
| 28 |
mrpt::math::CMatrixFloat44 p = mrpt::math::CMatrixFloat44::Identity(); |
| 29 |
|
| 30 |
p(0, 0) = 2.0f / (right - left); |
| 31 |
p(1, 1) = 2.0f / (top - bottom); |
| 32 |
p(2, 2) = -2.0f / (zfar - znear); |
| 33 |
p(0, 3) = -(right + left) / (right - left); |
| 34 |
p(1, 3) = -(top + bottom) / (top - bottom); |
| 35 |
p(2, 3) = -(zfar + znear) / (zfar - znear); |
| 36 |
|
| 37 |
return p; |
| 38 |
} |
| 39 |
|
| 40 |
void TRenderMatrices::computeOrthoProjectionMatrix( |
| 41 |
float left, float right, float bottom, float top, float znear, float zfar) |
| 42 |
{ |
| 43 |
m_last_z_near = znear; |
| 44 |
m_last_z_far = zfar; |
| 45 |
|
| 46 |
p_matrix = OrthoProjectionMatrix(left, right, bottom, top, znear, zfar); |
| 47 |
} |
| 48 |
|
| 49 |
void TRenderMatrices::computeNoProjectionMatrix(float znear, float zfar) |
| 50 |
{ |
| 51 |
ASSERT_GT_(zfar, znear); |
| 52 |
m_last_z_near = znear; |
| 53 |
m_last_z_far = zfar; |
| 54 |
|
| 55 |
p_matrix.setIdentity(); |
| 56 |
v_matrix.setIdentity(); |
| 57 |
} |
| 58 |
|
| 59 |
// Replacement for obsolete: gluPerspective() and glOrtho() |
| 60 |
void TRenderMatrices::computeProjectionMatrix(float znear, float zfar) |
| 61 |
{ |
| 62 |
ASSERT_GT_(FOV, .0f); |
| 63 |
ASSERT_GT_(zfar, znear); |
| 64 |
ASSERT_GT_(zfar, .0f); |
| 65 |
ASSERT_GE_(znear, .0f); |
| 66 |
|
| 67 |
m_last_z_near = znear; |
| 68 |
m_last_z_far = zfar; |
| 69 |
|
| 70 |
if (pinhole_model.has_value()) |
| 71 |
{ |
| 72 |
const auto& phm = pinhole_model.value(); |
| 73 |
|
| 74 |
// Equivalent to gluPerspective(), from pinhole camera intrinsic |
| 75 |
// parameters (cx,cy,fx,fy): |
| 76 |
ASSERT_EQUAL_(viewport_width, pinhole_model->ncols); |
| 77 |
ASSERT_EQUAL_(viewport_height, pinhole_model->nrows); |
| 78 |
|
| 79 |
const int W = pinhole_model->ncols, H = pinhole_model->nrows; |
| 80 |
|
| 81 |
// See: e.g. |
| 82 |
// http://ksimek.github.io/2013/06/03/calibrated_cameras_in_opengl/ |
| 83 |
mrpt::math::CMatrixFloat44 persp; |
| 84 |
persp.setZero(); |
| 85 |
|
| 86 |
persp(0, 0) = phm.fx(); |
| 87 |
persp(1, 1) = phm.fy(); |
| 88 |
|
| 89 |
persp(0, 2) = -phm.cx(); |
| 90 |
persp(1, 2) = -H + phm.cy(); |
| 91 |
persp(2, 2) = (zfar + znear); |
| 92 |
persp(3, 2) = -1.0f; |
| 93 |
persp(2, 3) = zfar * znear; |
| 94 |
|
| 95 |
// glOrtho(-W/2, W/2, -H/2, H/2, near, far); |
| 96 |
|
| 97 |
computeOrthoProjectionMatrix(0, W, 0 /*bottom*/, H /*top*/, znear, zfar); |
| 98 |
|
| 99 |
// glMultMatrix(persp); |
| 100 |
p_matrix.asEigen() *= persp.asEigen(); |
| 101 |
} |
| 102 |
else if (is_projective) |
| 103 |
{ |
| 104 |
// Was: gluPerspective() |
| 105 |
// Based on GLM's perspective (MIT license). |
| 106 |
|
| 107 |
const float aspect = viewport_width / (1.0f * viewport_height); |
| 108 |
ASSERT_GT_(std::abs(aspect - std::numeric_limits<float>::epsilon()), .0f); |
| 109 |
|
| 110 |
const float f = 1.0f / std::tan(mrpt::DEG2RAD(FOV) / 2.0f); |
| 111 |
p_matrix.setZero(); |
| 112 |
|
| 113 |
p_matrix(0, 0) = f / aspect; |
| 114 |
p_matrix(1, 1) = f; |
| 115 |
p_matrix(2, 2) = -(zfar + znear) / (zfar - znear); |
| 116 |
p_matrix(3, 2) = -1.0f; |
| 117 |
p_matrix(2, 3) = -(2.0f * zfar * znear) / (zfar - znear); |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
// Was: |
| 122 |
// glOrtho(-Ax, Ax, -Ay, Ay, -0.5 * m_clip_max, 0.5 * m_clip_max); |
| 123 |
|
| 124 |
const float ratio = viewport_width / (1.0f * viewport_height); |
| 125 |
float Ax = eyeDistance * 0.5f; |
| 126 |
float Ay = eyeDistance * 0.5f; |
| 127 |
|
| 128 |
if (ratio > 1) |
| 129 |
Ax *= ratio; |
| 130 |
else |
| 131 |
{ |
| 132 |
if (ratio != 0) Ay /= ratio; |
| 133 |
} |
| 134 |
|
| 135 |
const auto left = -.5f * Ax, right = .5f * Ax; |
| 136 |
const auto bottom = -.5f * Ay, top = .5f * Ay; |
| 137 |
computeOrthoProjectionMatrix(left, right, bottom, top, znear, zfar); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
namespace |
| 142 |
{ |
| 143 |
void azimuthElevationFromDirection( |
| 144 |
const mrpt::math::TVector3Df& v, float& elevation, float& azimuth) |
| 145 |
{ |
| 146 |
// Compute the elevation angle |
| 147 |
elevation = atan2(v.z, sqrt(v.x * v.x + v.y * v.y)); |
| 148 |
|
| 149 |
// Compute the azimuth angle |
| 150 |
if (v.x == 0 && v.y == 0) |
| 151 |
azimuth = 0; |
| 152 |
else |
| 153 |
azimuth = atan2(v.y, v.x); |
| 154 |
} |
| 155 |
} // namespace |
| 156 |
|
| 157 |
void TRenderMatrices::computeLightProjectionMatrix( |
| 158 |
float zmin, float zmax, const TLightParameters& lp) |
| 159 |
{ |
| 160 |
m_last_light_z_near = zmin; |
| 161 |
m_last_light_z_far = zmax; |
| 162 |
|
| 163 |
float dist = eyeDistance * lp.eyeDistance2lightShadowExtension; |
| 164 |
|
| 165 |
// Ensure dist is not too small: |
| 166 |
mrpt::keep_max(dist, zmax * lp.minimum_shadow_map_extension_ratio); |
| 167 |
|
| 168 |
light_p = OrthoProjectionMatrix(-dist, dist, -dist, dist, zmin, zmax); |
| 169 |
|
| 170 |
// "up" vector from elevation: |
| 171 |
|
| 172 |
float azim = 0, elevation = 0; |
| 173 |
azimuthElevationFromDirection(lp.direction, elevation, azim); |
| 174 |
|
| 175 |
const auto lightUp = mrpt::math::TVector3Df( |
| 176 |
-cos(azim) * sin(elevation), // x |
| 177 |
-sin(azim) * sin(elevation), // y |
| 178 |
cos(elevation) // z |
| 179 |
); |
| 180 |
|
| 181 |
light_v = LookAt(pointing - lp.direction * zmax * 0.5, pointing, lightUp); |
| 182 |
|
| 183 |
light_pv.asEigen() = light_p.asEigen() * light_v.asEigen(); |
| 184 |
|
| 185 |
// light_pmv is updated in RenderQueue |
| 186 |
} |
| 187 |
|
| 188 |
// Replacement for deprecated OpenGL gluLookAt(): |
| 189 |
mrpt::math::CMatrixFloat44 TRenderMatrices::LookAt( |
| 190 |
const mrpt::math::TVector3D& lookFrom, |
| 191 |
const mrpt::math::TVector3D& lookAt, |
| 192 |
const mrpt::math::TVector3D& up, |
| 193 |
mrpt::math::CMatrixFloat44* viewWithoutTranslation) |
| 194 |
{ |
| 195 |
using mrpt::math::TVector3D; |
| 196 |
|
| 197 |
// Note: Use double instead of float to avoid numerical innacuracies that |
| 198 |
// are really noticeable with the naked eye when elevation is close to 90 |
| 199 |
// deg (!) |
| 200 |
TVector3D forward = TVector3D(lookAt - lookFrom); |
| 201 |
|
| 202 |
const double fn = forward.norm(); |
| 203 |
ASSERT_(fn != 0); |
| 204 |
forward *= 1.0 / fn; |
| 205 |
|
| 206 |
// Side = forward x up |
| 207 |
TVector3D side = mrpt::math::crossProduct3D(forward, up); |
| 208 |
const double sn = side.norm(); |
| 209 |
ASSERT_(sn != 0); |
| 210 |
side *= 1.0 / sn; |
| 211 |
|
| 212 |
// Recompute up as: up = side x forward |
| 213 |
const TVector3D up2 = mrpt::math::crossProduct3D(side, forward); |
| 214 |
|
| 215 |
// s.x s.y s.z -dot(s, eye) |
| 216 |
// u.x u.y u.z -dot(u, eye) |
| 217 |
// -f.x -f.y -f.z dot(up, eye) |
| 218 |
// 0 0 0 1 |
| 219 |
|
| 220 |
mrpt::math::CMatrixFloat44 m(mrpt::math::UNINITIALIZED_MATRIX); |
| 221 |
// Axis X: |
| 222 |
m(0, 0) = d2f(side[0]); |
| 223 |
m(0, 1) = d2f(side[1]); |
| 224 |
m(0, 2) = d2f(side[2]); |
| 225 |
// Axis Y: |
| 226 |
m(1, 0) = d2f(up2[0]); |
| 227 |
m(1, 1) = d2f(up2[1]); |
| 228 |
m(1, 2) = d2f(up2[2]); |
| 229 |
// Axis Z: |
| 230 |
m(2, 0) = d2f(-forward[0]); |
| 231 |
m(2, 1) = d2f(-forward[1]); |
| 232 |
m(2, 2) = d2f(-forward[2]); |
| 233 |
// Last row: |
| 234 |
m(3, 0) = .0f; |
| 235 |
m(3, 1) = .0f; |
| 236 |
m(3, 2) = .0f; |
| 237 |
m(3, 3) = 1.f; |
| 238 |
|
| 239 |
if (viewWithoutTranslation) *viewWithoutTranslation = m; |
| 240 |
|
| 241 |
// Translation: |
| 242 |
m(0, 3) = d2f(-mrpt::math::dotProduct<3, double>(side, lookFrom)); |
| 243 |
m(1, 3) = d2f(-mrpt::math::dotProduct<3, double>(up2, lookFrom)); |
| 244 |
m(2, 3) = d2f(mrpt::math::dotProduct<3, double>(forward, lookFrom)); |
| 245 |
|
| 246 |
return m; |
| 247 |
} |
| 248 |
|
| 249 |
// Replacement for deprecated OpenGL gluLookAt(): |
| 250 |
void TRenderMatrices::computeViewMatrix() |
| 251 |
{ |
| 252 |
v_matrix = LookAt(eye, pointing, up, &v_matrix_no_translation); |
| 253 |
} |
| 254 |
|
| 255 |
void TRenderMatrices::projectPoint( |
| 256 |
float x, float y, float z, float& proj_u, float& proj_v, float& proj_z_depth) const |
| 257 |
{ |
| 258 |
const Eigen::Matrix<float, 4, 1, Eigen::ColMajor> proj = |
| 259 |
pmv_matrix.asEigen() * Eigen::Matrix<float, 4, 1, Eigen::ColMajor>(x, y, z, 1); |
| 260 |
proj_u = proj[3] ? proj[0] / proj[3] : 0; |
| 261 |
proj_v = proj[3] ? proj[1] / proj[3] : 0; |
| 262 |
proj_z_depth = proj[2]; |
| 263 |
} |
| 264 |
|
| 265 |
void TRenderMatrices::projectPointPixels( |
| 266 |
float x, float y, float z, float& proj_u_px, float& proj_v_px, float& proj_depth) const |
| 267 |
{ |
| 268 |
projectPoint(x, y, z, proj_u_px, proj_v_px, proj_depth); |
| 269 |
proj_u_px = (proj_u_px + 1.0f) * (viewport_width * 0.5f); |
| 270 |
proj_v_px = (proj_v_px + 1.0f) * (viewport_height * 0.5f); |
| 271 |
} |
| 272 |
|
| 273 |
void TRenderMatrices::saveToYaml(mrpt::containers::yaml& c) const |
| 274 |
{ |
| 275 |
c = mrpt::containers::yaml::Map(); |
| 276 |
|
| 277 |
MCP_SAVE(c, initialized); |
| 278 |
MCP_SAVE(c, viewport_width); |
| 279 |
MCP_SAVE(c, viewport_height); |
| 280 |
MCP_SAVE(c, FOV); |
| 281 |
MCP_SAVE_DEG(c, azimuth); |
| 282 |
MCP_SAVE_DEG(c, elev); |
| 283 |
MCP_SAVE(c, eyeDistance); |
| 284 |
MCP_SAVE(c, is_projective); |
| 285 |
|
| 286 |
c["eye"] = eye.asString(); |
| 287 |
c["pointing"] = pointing.asString(); |
| 288 |
c["up"] = up.asString(); |
| 289 |
|
| 290 |
c["p_matrix"] = mrpt::containers::yaml::FromMatrix(p_matrix); |
| 291 |
c["v_matrix"] = mrpt::containers::yaml::FromMatrix(v_matrix); |
| 292 |
c["m_matrix"] = mrpt::containers::yaml::FromMatrix(m_matrix); |
| 293 |
c["light_p_matrix"] = mrpt::containers::yaml::FromMatrix(light_p); |
| 294 |
c["light_v_matrix"] = mrpt::containers::yaml::FromMatrix(light_v); |
| 295 |
} |
| 296 |
|
| 297 |
void TRenderMatrices::print(std::ostream& o) const |
| 298 |
{ |
| 299 |
mrpt::containers::yaml c; |
| 300 |
saveToYaml(c); |
| 301 |
o << c; |
| 302 |
} |