| 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/CSetOfTriangles.h> |
| 13 |
#include <mrpt/opengl/CTexturedPlane.h> |
| 14 |
#include <mrpt/serialization/CArchive.h> |
| 15 |
|
| 16 |
using namespace mrpt; |
| 17 |
using namespace mrpt::opengl; |
| 18 |
using namespace mrpt::poses; |
| 19 |
using namespace mrpt::math; |
| 20 |
using namespace std; |
| 21 |
|
| 22 |
IMPLEMENTS_SERIALIZABLE(CTexturedPlane, CRenderizable, mrpt::opengl) |
| 23 |
|
| 24 |
CTexturedPlane::CTexturedPlane(float x_min, float x_max, float y_min, float y_max) |
| 25 |
{ |
| 26 |
CRenderizableShaderTriangles::enableLight(false); |
| 27 |
CRenderizableShaderTexturedTriangles::enableLight(false); |
| 28 |
|
| 29 |
setPlaneCorners(x_min, x_max, y_min, y_max); |
| 30 |
} |
| 31 |
|
| 32 |
void CTexturedPlane::render(const RenderContext& rc) const |
| 33 |
{ |
| 34 |
const bool hasTexture = textureImageHasBeenAssigned() && !getTextureImage().isEmpty(); |
| 35 |
|
| 36 |
switch (rc.shader_id) |
| 37 |
{ |
| 38 |
case DefaultShaderID::TRIANGLES_NO_LIGHT: |
| 39 |
case DefaultShaderID::TRIANGLES_LIGHT: |
| 40 |
if (!hasTexture) CRenderizableShaderTriangles::render(rc); |
| 41 |
break; |
| 42 |
case DefaultShaderID::TEXTURED_TRIANGLES_NO_LIGHT: |
| 43 |
case DefaultShaderID::TEXTURED_TRIANGLES_LIGHT: |
| 44 |
if (hasTexture) CRenderizableShaderTexturedTriangles::render(rc); |
| 45 |
break; |
| 46 |
}; |
| 47 |
} |
| 48 |
|
| 49 |
void CTexturedPlane::renderUpdateBuffers() const |
| 50 |
{ |
| 51 |
const bool hasTexture = textureImageHasBeenAssigned() && !getTextureImage().isEmpty(); |
| 52 |
|
| 53 |
if (!hasTexture) |
| 54 |
CRenderizableShaderTriangles::renderUpdateBuffers(); |
| 55 |
else |
| 56 |
CRenderizableShaderTexturedTriangles::renderUpdateBuffers(); |
| 57 |
} |
| 58 |
|
| 59 |
void CTexturedPlane::onUpdateBuffers_TexturedTriangles() |
| 60 |
{ |
| 61 |
MRPT_START |
| 62 |
using P2f = mrpt::math::TPoint2Df; |
| 63 |
using P3f = mrpt::math::TPoint3Df; |
| 64 |
|
| 65 |
auto& tris = CRenderizableShaderTexturedTriangles::m_triangles; |
| 66 |
std::unique_lock<std::shared_mutex> writeLock( |
| 67 |
CRenderizableShaderTexturedTriangles::m_trianglesMtx.data); |
| 68 |
|
| 69 |
tris.clear(); |
| 70 |
|
| 71 |
{ |
| 72 |
TTriangle t; |
| 73 |
t.vertices[0].xyzrgba.pt = P3f(m_xMin, m_yMin, 0); |
| 74 |
t.vertices[1].xyzrgba.pt = P3f(m_xMax, m_yMin, 0); |
| 75 |
t.vertices[2].xyzrgba.pt = P3f(m_xMax, m_yMax, 0); |
| 76 |
|
| 77 |
t.vertices[0].uv = P2f(0, 0); |
| 78 |
t.vertices[1].uv = P2f(1, 0); |
| 79 |
t.vertices[2].uv = P2f(1, 1); |
| 80 |
|
| 81 |
t.computeNormals(); |
| 82 |
tris.emplace_back(t); |
| 83 |
} |
| 84 |
{ |
| 85 |
TTriangle t; |
| 86 |
t.vertices[0].xyzrgba.pt = P3f(m_xMin, m_yMin, 0); |
| 87 |
t.vertices[1].xyzrgba.pt = P3f(m_xMax, m_yMax, 0); |
| 88 |
t.vertices[2].xyzrgba.pt = P3f(m_xMin, m_yMax, 0); |
| 89 |
|
| 90 |
t.vertices[0].uv = P2f(0, 0); |
| 91 |
t.vertices[1].uv = P2f(1, 1); |
| 92 |
t.vertices[2].uv = P2f(0, 1); |
| 93 |
|
| 94 |
t.computeNormals(); |
| 95 |
tris.emplace_back(t); |
| 96 |
} |
| 97 |
|
| 98 |
MRPT_END |
| 99 |
} |
| 100 |
|
| 101 |
void CTexturedPlane::onUpdateBuffers_Triangles() |
| 102 |
{ |
| 103 |
MRPT_START |
| 104 |
using P3f = mrpt::math::TPoint3Df; |
| 105 |
|
| 106 |
std::unique_lock<std::shared_mutex> trisWriteLock( |
| 107 |
CRenderizableShaderTriangles::m_trianglesMtx.data); |
| 108 |
auto& tris = CRenderizableShaderTriangles::m_triangles; |
| 109 |
tris.clear(); |
| 110 |
|
| 111 |
const auto col = getColor_u8(); |
| 112 |
TTriangle t; |
| 113 |
for (int i = 0; i < 3; i++) |
| 114 |
{ |
| 115 |
t.vertices[i].xyzrgba.r = col.R; |
| 116 |
t.vertices[i].xyzrgba.g = col.G; |
| 117 |
t.vertices[i].xyzrgba.b = col.B; |
| 118 |
t.vertices[i].xyzrgba.a = col.A; |
| 119 |
} |
| 120 |
|
| 121 |
t.vertices[0].xyzrgba.pt = P3f(m_xMin, m_yMin, 0); |
| 122 |
t.vertices[1].xyzrgba.pt = P3f(m_xMax, m_yMin, 0); |
| 123 |
t.vertices[2].xyzrgba.pt = P3f(m_xMax, m_yMax, 0); |
| 124 |
|
| 125 |
t.computeNormals(); |
| 126 |
tris.emplace_back(t); |
| 127 |
|
| 128 |
t.vertices[0].xyzrgba.pt = P3f(m_xMin, m_yMin, 0); |
| 129 |
t.vertices[1].xyzrgba.pt = P3f(m_xMax, m_yMax, 0); |
| 130 |
t.vertices[2].xyzrgba.pt = P3f(m_xMin, m_yMax, 0); |
| 131 |
|
| 132 |
t.computeNormals(); |
| 133 |
tris.emplace_back(t); |
| 134 |
|
| 135 |
MRPT_END |
| 136 |
} |
| 137 |
|
| 138 |
uint8_t CTexturedPlane::serializeGetVersion() const { return 2; } |
| 139 |
void CTexturedPlane::serializeTo(mrpt::serialization::CArchive& out) const |
| 140 |
{ |
| 141 |
writeToStreamRender(out); |
| 142 |
|
| 143 |
out << m_xMin << m_xMax; |
| 144 |
out << m_yMin << m_yMax; |
| 145 |
|
| 146 |
writeToStreamTexturedObject(out); |
| 147 |
} |
| 148 |
|
| 149 |
void CTexturedPlane::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version) |
| 150 |
{ |
| 151 |
switch (version) |
| 152 |
{ |
| 153 |
case 0: |
| 154 |
case 1: |
| 155 |
THROW_EXCEPTION("Deserialization of old formats not supported."); |
| 156 |
break; |
| 157 |
case 2: |
| 158 |
{ |
| 159 |
readFromStreamRender(in); |
| 160 |
in >> m_xMin >> m_xMax; |
| 161 |
in >> m_yMin >> m_yMax; |
| 162 |
readFromStreamTexturedObject(in); |
| 163 |
} |
| 164 |
break; |
| 165 |
default: |
| 166 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 167 |
}; |
| 168 |
CRenderizable::notifyChange(); |
| 169 |
} |
| 170 |
|
| 171 |
bool CTexturedPlane::traceRay(const mrpt::poses::CPose3D& o, double& dist) const |
| 172 |
{ |
| 173 |
if (!polygonUpToDate) updatePoly(); |
| 174 |
return math::traceRay(tmpPoly, (o - getCPose()).asTPose(), dist); |
| 175 |
} |
| 176 |
|
| 177 |
void CTexturedPlane::updatePoly() const |
| 178 |
{ |
| 179 |
TPolygon3D poly(4); |
| 180 |
poly[0].x = poly[1].x = m_xMin; |
| 181 |
poly[2].x = poly[3].x = m_xMax; |
| 182 |
poly[0].y = poly[3].y = m_yMin; |
| 183 |
poly[1].y = poly[2].y = m_yMax; |
| 184 |
for (size_t i = 0; i < 4; i++) poly[i].z = 0; |
| 185 |
tmpPoly.resize(1); |
| 186 |
tmpPoly[0] = poly; |
| 187 |
polygonUpToDate = true; |
| 188 |
} |
| 189 |
|
| 190 |
auto CTexturedPlane::internalBoundingBoxLocal() const -> mrpt::math::TBoundingBoxf |
| 191 |
{ |
| 192 |
return mrpt::math::TBoundingBoxf::FromUnsortedPoints( |
| 193 |
{m_xMin, m_yMin, 0.f}, {m_xMax, m_yMax, 0.f}); |
| 194 |
} |