| 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/opengl/CFrustum.h> |
| 13 |
#include <mrpt/serialization/CArchive.h> |
| 14 |
|
| 15 |
using namespace mrpt; |
| 16 |
using namespace mrpt::opengl; |
| 17 |
using namespace mrpt::math; |
| 18 |
using namespace std; |
| 19 |
|
| 20 |
IMPLEMENTS_SERIALIZABLE(CFrustum, CRenderizableShaderTriangles, mrpt::opengl) |
| 21 |
|
| 22 |
void CFrustum::render(const RenderContext& rc) const |
| 23 |
{ |
| 24 |
switch (rc.shader_id) |
| 25 |
{ |
| 26 |
case DefaultShaderID::TRIANGLES_LIGHT: |
| 27 |
if (m_draw_planes) CRenderizableShaderTriangles::render(rc); |
| 28 |
break; |
| 29 |
case DefaultShaderID::WIREFRAME: |
| 30 |
if (m_draw_lines) CRenderizableShaderWireFrame::render(rc); |
| 31 |
break; |
| 32 |
}; |
| 33 |
} |
| 34 |
void CFrustum::renderUpdateBuffers() const |
| 35 |
{ |
| 36 |
CRenderizableShaderTriangles::renderUpdateBuffers(); |
| 37 |
CRenderizableShaderWireFrame::renderUpdateBuffers(); |
| 38 |
} |
| 39 |
|
| 40 |
std::array<mrpt::math::TPoint3Df, 8> CFrustum::computeFrustumCorners() const |
| 41 |
{ |
| 42 |
std::array<mrpt::math::TPoint3Df, 8> pts; |
| 43 |
for (size_t j = 0; j < 2; j++) |
| 44 |
{ |
| 45 |
const float r = j == 0 ? m_min_distance : m_max_distance; |
| 46 |
for (size_t i = 0; i < 4; i++) pts[4 * j + i].x = r; |
| 47 |
pts[4 * j + 0].y = -r * tan(m_fov_horz_left); |
| 48 |
pts[4 * j + 1].y = -r * tan(m_fov_horz_left); |
| 49 |
pts[4 * j + 2].y = r * tan(m_fov_horz_right); |
| 50 |
pts[4 * j + 3].y = r * tan(m_fov_horz_right); |
| 51 |
pts[4 * j + 0].z = -r * tan(m_fov_vert_down); |
| 52 |
pts[4 * j + 1].z = r * tan(m_fov_vert_up); |
| 53 |
pts[4 * j + 2].z = -r * tan(m_fov_vert_down); |
| 54 |
pts[4 * j + 3].z = r * tan(m_fov_vert_up); |
| 55 |
} |
| 56 |
return pts; |
| 57 |
} |
| 58 |
|
| 59 |
void CFrustum::onUpdateBuffers_Wireframe() |
| 60 |
{ |
| 61 |
auto& vbd = CRenderizableShaderWireFrame::m_vertex_buffer_data; |
| 62 |
auto& cbd = CRenderizableShaderWireFrame::m_color_buffer_data; |
| 63 |
std::unique_lock<std::shared_mutex> wfWriteLock( |
| 64 |
CRenderizableShaderWireFrame::m_wireframeMtx.data); |
| 65 |
|
| 66 |
vbd.clear(); |
| 67 |
|
| 68 |
const std::array<mrpt::math::TPoint3Df, 8> pts = computeFrustumCorners(); |
| 69 |
|
| 70 |
const std::array<int, 16> draw_path = {0, 1, 3, 2, 0, 4, 6, 2, 3, 7, 6, 4, 5, 7, 5, 1}; |
| 71 |
|
| 72 |
// GL_LINE_STRIP: |
| 73 |
for (size_t idx = 0; idx < draw_path.size(); idx++) |
| 74 |
{ |
| 75 |
const unsigned int idx_next = (idx + 1) % draw_path.size(); |
| 76 |
vbd.emplace_back(pts[draw_path[idx]]); |
| 77 |
vbd.emplace_back(pts[draw_path[idx_next]]); |
| 78 |
} |
| 79 |
|
| 80 |
cbd.assign(vbd.size(), getColor_u8()); |
| 81 |
} |
| 82 |
void CFrustum::onUpdateBuffers_Triangles() |
| 83 |
{ |
| 84 |
std::unique_lock<std::shared_mutex> trisWriteLock( |
| 85 |
CRenderizableShaderTriangles::m_trianglesMtx.data); |
| 86 |
auto& tris = CRenderizableShaderTriangles::m_triangles; |
| 87 |
|
| 88 |
tris.clear(); |
| 89 |
|
| 90 |
const std::array<mrpt::math::TPoint3Df, 8> pts = computeFrustumCorners(); |
| 91 |
|
| 92 |
tris.emplace_back(pts[0], pts[2], pts[6]); |
| 93 |
tris.emplace_back(pts[6], pts[4], pts[0]); |
| 94 |
|
| 95 |
tris.emplace_back(pts[2], pts[3], pts[7]); |
| 96 |
tris.emplace_back(pts[7], pts[6], pts[2]); |
| 97 |
|
| 98 |
tris.emplace_back(pts[4], pts[6], pts[7]); |
| 99 |
tris.emplace_back(pts[7], pts[5], pts[4]); |
| 100 |
|
| 101 |
tris.emplace_back(pts[1], pts[5], pts[7]); |
| 102 |
tris.emplace_back(pts[7], pts[3], pts[1]); |
| 103 |
|
| 104 |
tris.emplace_back(pts[1], pts[5], pts[7]); |
| 105 |
tris.emplace_back(pts[7], pts[3], pts[1]); |
| 106 |
|
| 107 |
tris.emplace_back(pts[4], pts[5], pts[1]); |
| 108 |
tris.emplace_back(pts[1], pts[0], pts[4]); |
| 109 |
|
| 110 |
// All faces, all vertices, same color: |
| 111 |
for (auto& t : tris) t.setColor(m_planes_color); |
| 112 |
} |
| 113 |
|
| 114 |
// Ctors |
| 115 |
CFrustum::CFrustum() : |
| 116 |
m_fov_horz_left(mrpt::DEG2RAD(45.0f)), |
| 117 |
m_fov_horz_right(mrpt::DEG2RAD(45.0f)), |
| 118 |
m_fov_vert_down(mrpt::DEG2RAD(30.0f)), |
| 119 |
m_fov_vert_up(mrpt::DEG2RAD(30.0f)), |
| 120 |
m_planes_color(0xE0, 0x00, 0x00, 0x50) // RGBA |
| 121 |
{ |
| 122 |
keep_min(m_fov_horz_left, DEG2RAD(89.9f)); |
| 123 |
keep_max(m_fov_horz_left, 0); |
| 124 |
keep_min(m_fov_horz_right, DEG2RAD(89.9f)); |
| 125 |
keep_max(m_fov_horz_right, 0); |
| 126 |
keep_min(m_fov_vert_down, DEG2RAD(89.9f)); |
| 127 |
keep_max(m_fov_vert_down, 0); |
| 128 |
keep_min(m_fov_vert_up, DEG2RAD(89.9f)); |
| 129 |
keep_max(m_fov_vert_up, 0); |
| 130 |
} |
| 131 |
|
| 132 |
CFrustum::CFrustum( |
| 133 |
float near_distance, |
| 134 |
float far_distance, |
| 135 |
float horz_FOV_degrees, |
| 136 |
float vert_FOV_degrees, |
| 137 |
float lineWidth, |
| 138 |
bool draw_lines, |
| 139 |
bool draw_planes) : |
| 140 |
m_min_distance(near_distance), |
| 141 |
m_max_distance(far_distance), |
| 142 |
m_fov_horz_left(mrpt::DEG2RAD(.5f * horz_FOV_degrees)), |
| 143 |
m_fov_horz_right(mrpt::DEG2RAD(.5f * horz_FOV_degrees)), |
| 144 |
m_fov_vert_down(mrpt::DEG2RAD(.5f * vert_FOV_degrees)), |
| 145 |
m_fov_vert_up(mrpt::DEG2RAD(.5f * vert_FOV_degrees)), |
| 146 |
m_draw_lines(draw_lines), |
| 147 |
m_draw_planes(draw_planes), |
| 148 |
m_planes_color(0xE0, 0x00, 0x00, 0x50) // RGBA |
| 149 |
{ |
| 150 |
this->setLineWidth(lineWidth); |
| 151 |
} |
| 152 |
|
| 153 |
uint8_t CFrustum::serializeGetVersion() const { return 1; } |
| 154 |
void CFrustum::serializeTo(mrpt::serialization::CArchive& out) const |
| 155 |
{ |
| 156 |
writeToStreamRender(out); |
| 157 |
// version 0 |
| 158 |
out << m_min_distance << m_max_distance << m_fov_horz_left << m_fov_horz_right << m_fov_vert_down |
| 159 |
<< m_fov_vert_up << m_draw_lines << m_draw_planes << m_lineWidth << m_planes_color.R |
| 160 |
<< m_planes_color.G << m_planes_color.B << m_planes_color.A; |
| 161 |
CRenderizableShaderTriangles::params_serialize(out); // v1 |
| 162 |
} |
| 163 |
|
| 164 |
void CFrustum::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version) |
| 165 |
{ |
| 166 |
switch (version) |
| 167 |
{ |
| 168 |
case 0: |
| 169 |
case 1: |
| 170 |
readFromStreamRender(in); |
| 171 |
in >> m_min_distance >> m_max_distance >> m_fov_horz_left >> m_fov_horz_right >> |
| 172 |
m_fov_vert_down >> m_fov_vert_up >> m_draw_lines >> m_draw_planes >> m_lineWidth >> |
| 173 |
m_planes_color.R >> m_planes_color.G >> m_planes_color.B >> m_planes_color.A; |
| 174 |
|
| 175 |
if (version >= 1) CRenderizableShaderTriangles::params_deserialize(in); |
| 176 |
|
| 177 |
break; |
| 178 |
default: |
| 179 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 180 |
}; |
| 181 |
CRenderizable::notifyChange(); |
| 182 |
} |
| 183 |
|
| 184 |
bool CFrustum::traceRay( |
| 185 |
[[maybe_unused]] const mrpt::poses::CPose3D& o, [[maybe_unused]] double& dist) const |
| 186 |
{ |
| 187 |
THROW_EXCEPTION("TO DO"); |
| 188 |
} |
| 189 |
|
| 190 |
// setters: |
| 191 |
void CFrustum::setNearFarPlanes(const float near_distance, const float far_distance) |
| 192 |
{ |
| 193 |
m_min_distance = near_distance; |
| 194 |
m_max_distance = far_distance; |
| 195 |
CRenderizable::notifyChange(); |
| 196 |
} |
| 197 |
void CFrustum::setHorzFOV(const float fov_horz_degrees) |
| 198 |
{ |
| 199 |
m_fov_horz_right = m_fov_horz_left = 0.5f * mrpt::DEG2RAD(fov_horz_degrees); |
| 200 |
keep_min(m_fov_horz_left, DEG2RAD(89.9f)); |
| 201 |
keep_max(m_fov_horz_left, 0); |
| 202 |
keep_min(m_fov_horz_right, DEG2RAD(89.9f)); |
| 203 |
keep_max(m_fov_horz_right, 0); |
| 204 |
CRenderizable::notifyChange(); |
| 205 |
} |
| 206 |
void CFrustum::setVertFOV(const float fov_vert_degrees) |
| 207 |
{ |
| 208 |
m_fov_vert_down = m_fov_vert_up = 0.5f * mrpt::DEG2RAD(fov_vert_degrees); |
| 209 |
keep_min(m_fov_vert_down, DEG2RAD(89.9f)); |
| 210 |
keep_max(m_fov_vert_down, 0); |
| 211 |
keep_min(m_fov_vert_up, DEG2RAD(89.9f)); |
| 212 |
keep_max(m_fov_vert_up, 0); |
| 213 |
CRenderizable::notifyChange(); |
| 214 |
} |
| 215 |
void CFrustum::setHorzFOVAsymmetric( |
| 216 |
const float fov_horz_left_degrees, const float fov_horz_right_degrees) |
| 217 |
{ |
| 218 |
m_fov_horz_left = mrpt::DEG2RAD(fov_horz_left_degrees); |
| 219 |
m_fov_horz_right = mrpt::DEG2RAD(fov_horz_right_degrees); |
| 220 |
keep_min(m_fov_horz_left, DEG2RAD(89.9f)); |
| 221 |
keep_max(m_fov_horz_left, 0); |
| 222 |
keep_min(m_fov_horz_right, DEG2RAD(89.9f)); |
| 223 |
keep_max(m_fov_horz_right, 0); |
| 224 |
CRenderizable::notifyChange(); |
| 225 |
} |
| 226 |
void CFrustum::setVertFOVAsymmetric( |
| 227 |
const float fov_vert_down_degrees, const float fov_vert_up_degrees) |
| 228 |
{ |
| 229 |
m_fov_vert_down = mrpt::DEG2RAD(fov_vert_down_degrees); |
| 230 |
m_fov_vert_up = mrpt::DEG2RAD(fov_vert_up_degrees); |
| 231 |
keep_min(m_fov_vert_down, DEG2RAD(89.9f)); |
| 232 |
keep_max(m_fov_vert_down, 0); |
| 233 |
keep_min(m_fov_vert_up, DEG2RAD(89.9f)); |
| 234 |
keep_max(m_fov_vert_up, 0); |
| 235 |
CRenderizable::notifyChange(); |
| 236 |
} |
| 237 |
|
| 238 |
auto CFrustum::internalBoundingBoxLocal() const -> mrpt::math::TBoundingBoxf |
| 239 |
{ |
| 240 |
std::array<mrpt::math::TPoint3Df, 8> pts = computeFrustumCorners(); |
| 241 |
|
| 242 |
auto bb = mrpt::math::TBoundingBoxf::PlusMinusInfinity(); |
| 243 |
|
| 244 |
for (const auto& pt : pts) bb.updateWithPoint(pt); |
| 245 |
|
| 246 |
return bb; |
| 247 |
} |
| 248 |
|
| 249 |
CFrustum::CFrustum(const mrpt::img::TCamera& i, double focalScale) : |
| 250 |
CFrustum( |
| 251 |
i.fx() * focalScale * 0.1f, |
| 252 |
i.fx() * focalScale, |
| 253 |
2 * mrpt::RAD2DEG(std::atan2(i.ncols, 2 * i.fx())), |
| 254 |
2 * mrpt::RAD2DEG(std::atan2(i.nrows, 2 * i.fy())), |
| 255 |
1.0f, |
| 256 |
true, |
| 257 |
false) |
| 258 |
{ |
| 259 |
} |