| 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/CRenderizableShaderTexturedTriangles.h> |
| 13 |
#include <mrpt/opengl/TLightParameters.h> |
| 14 |
#include <mrpt/opengl/opengl_api.h> |
| 15 |
#include <mrpt/serialization/CArchive.h> |
| 16 |
#include <mrpt/system/CTimeLogger.h> |
| 17 |
|
| 18 |
#include <iostream> |
| 19 |
#include <memory> // std::align |
| 20 |
#include <thread> |
| 21 |
|
| 22 |
using namespace mrpt; |
| 23 |
using namespace mrpt::opengl; |
| 24 |
using namespace mrpt::poses; |
| 25 |
using namespace mrpt::math; |
| 26 |
using namespace std; |
| 27 |
using mrpt::img::CImage; |
| 28 |
|
| 29 |
IMPLEMENTS_VIRTUAL_SERIALIZABLE(CRenderizableShaderTexturedTriangles, CRenderizable, mrpt::opengl) |
| 30 |
|
| 31 |
CRenderizableShaderTexturedTriangles::CRenderizableShaderTexturedTriangles() |
| 32 |
{ // Initialize GlState |
| 33 |
auto gh = gls(); |
| 34 |
} |
| 35 |
|
| 36 |
void CRenderizableShaderTexturedTriangles::renderUpdateBuffers() const |
| 37 |
{ |
| 38 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 39 |
// Generate vertices & colors into m_triangles |
| 40 |
const_cast<CRenderizableShaderTexturedTriangles&>(*this).onUpdateBuffers_TexturedTriangles(); |
| 41 |
|
| 42 |
std::shared_lock<std::shared_mutex> readLock(m_trianglesMtx.data); |
| 43 |
auto gh = gls(); |
| 44 |
|
| 45 |
const auto n = m_triangles.size(); |
| 46 |
|
| 47 |
// Define OpenGL buffers: |
| 48 |
gh.state.vbo->createOnce(); |
| 49 |
gh.state.vbo->bind(); |
| 50 |
gh.state.vbo->allocate(m_triangles.data(), sizeof(m_triangles[0]) * n); |
| 51 |
|
| 52 |
// VAO: required to use glEnableVertexAttribArray() |
| 53 |
gh.state.vao->createOnce(); |
| 54 |
|
| 55 |
#endif |
| 56 |
} |
| 57 |
|
| 58 |
void CRenderizableShaderTexturedTriangles::render(const RenderContext& rc) const |
| 59 |
{ |
| 60 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 61 |
|
| 62 |
// This will load and/or select our texture, only once: |
| 63 |
initializeTextures(); |
| 64 |
ASSERT_(m_glTexture.initialized()); |
| 65 |
|
| 66 |
std::shared_lock<std::shared_mutex> readLock(m_trianglesMtx.data); |
| 67 |
auto gh = gls(); |
| 68 |
|
| 69 |
// Set the texture uniform: |
| 70 |
const Program& s = *rc.shader; |
| 71 |
const auto texUnit = m_glTexture.textureUnit(); |
| 72 |
if (s.hasUniform("textureSampler") && (!rc.activeTextureUnit || *rc.activeTextureUnit != texUnit)) |
| 73 |
{ |
| 74 |
rc.activeTextureUnit = texUnit; // buffer it |
| 75 |
// bound to GL_TEXTURE0 + "i": |
| 76 |
s.setInt("textureSampler", texUnit); |
| 77 |
} |
| 78 |
|
| 79 |
// Lights: |
| 80 |
if (m_enableLight && rc.lights && rc.shader->hasUniform("light_color") && |
| 81 |
(!rc.activeLights || rc.activeLights.value() != rc.lights)) |
| 82 |
{ |
| 83 |
// buffered pointer, to prevent re-setting the opengl state with the |
| 84 |
// same values, a performance killer: |
| 85 |
rc.activeLights = rc.lights; |
| 86 |
|
| 87 |
const auto& l = rc.lights; |
| 88 |
|
| 89 |
s.setFloat3("light_color", l->color.R, l->color.G, l->color.B); |
| 90 |
s.setFloat3("light_direction", l->direction.x, l->direction.y, l->direction.z); |
| 91 |
s.setFloat("light_ambient", l->ambient); |
| 92 |
s.setFloat("light_diffuse", l->diffuse); |
| 93 |
if (rc.shader->hasUniform("light_specular")) s.setFloat("light_specular", l->specular); |
| 94 |
|
| 95 |
if (rc.shader->hasUniform("light_zmax")) |
| 96 |
s.setFloat("light_zmax", rc.state->getLastLightClipZFar()); |
| 97 |
if (rc.shader->hasUniform("camera_far_plane")) |
| 98 |
s.setFloat("camera_far_plane", rc.state->getLastClipZFar()); |
| 99 |
|
| 100 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 101 |
} |
| 102 |
|
| 103 |
// Set the texture uniform: |
| 104 |
if (rc.shader->hasUniform("shadowMap")) |
| 105 |
{ |
| 106 |
// bound to GL_TEXTURE0 + "i": |
| 107 |
s.setInt("shadowMap", SHADOW_MAP_TEXTURE_UNIT); |
| 108 |
|
| 109 |
// Shadow params: |
| 110 |
if (const auto& l = rc.lights; l) |
| 111 |
{ |
| 112 |
rc.shader->setFloat("shadow_bias", l->shadow_bias); |
| 113 |
rc.shader->setFloat("shadow_bias_cam2frag", l->shadow_bias_cam2frag); |
| 114 |
rc.shader->setFloat("shadow_bias_normal", l->shadow_bias_normal); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
// Set up the vertex array: |
| 119 |
std::optional<GLuint> attr_position; |
| 120 |
if (rc.shader->hasAttribute("position")) |
| 121 |
{ |
| 122 |
attr_position = rc.shader->attributeId("position"); |
| 123 |
gh.state.vao->bind(); |
| 124 |
glEnableVertexAttribArray(*attr_position); |
| 125 |
gh.state.vbo->bind(); |
| 126 |
glVertexAttribPointer( |
| 127 |
*attr_position, /* attribute */ |
| 128 |
3, /* size */ |
| 129 |
GL_FLOAT, /* type */ |
| 130 |
GL_FALSE, /* normalized? */ |
| 131 |
sizeof(TTriangle::Vertex), /* stride */ |
| 132 |
BUFFER_OFFSET(offsetof(TTriangle::Vertex, xyzrgba.pt.x))); |
| 133 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 134 |
} |
| 135 |
|
| 136 |
// Set up the normals array: |
| 137 |
std::optional<GLuint> attr_normals; |
| 138 |
if (rc.shader->hasAttribute("vertexNormal")) |
| 139 |
{ |
| 140 |
attr_normals = rc.shader->attributeId("vertexNormal"); |
| 141 |
glEnableVertexAttribArray(*attr_normals); |
| 142 |
gh.state.vbo->bind(); |
| 143 |
glVertexAttribPointer( |
| 144 |
*attr_normals, /* attribute */ |
| 145 |
3, /* size */ |
| 146 |
GL_FLOAT, /* type */ |
| 147 |
GL_FALSE, /* normalized? */ |
| 148 |
sizeof(TTriangle::Vertex), /* stride */ |
| 149 |
BUFFER_OFFSET(offsetof(TTriangle::Vertex, normal.x))); |
| 150 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 151 |
} |
| 152 |
|
| 153 |
// Set up the UV array: |
| 154 |
std::optional<GLuint> attr_uv; |
| 155 |
if (rc.shader->hasAttribute("vertexUV")) |
| 156 |
{ |
| 157 |
attr_uv = rc.shader->attributeId("vertexUV"); |
| 158 |
glEnableVertexAttribArray(*attr_uv); |
| 159 |
gh.state.vbo->bind(); |
| 160 |
glVertexAttribPointer( |
| 161 |
*attr_uv, /* attribute */ |
| 162 |
2, /* size */ |
| 163 |
GL_FLOAT, /* type */ |
| 164 |
GL_FALSE, /* normalized? */ |
| 165 |
sizeof(TTriangle::Vertex), /* stride */ |
| 166 |
BUFFER_OFFSET(offsetof(TTriangle::Vertex, uv.x))); |
| 167 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 168 |
} |
| 169 |
|
| 170 |
if (m_cullface == TCullFace::NONE && |
| 171 |
(!rc.activeCullFace || *rc.activeCullFace != TCullFace::NONE)) |
| 172 |
{ |
| 173 |
rc.activeCullFace = TCullFace::NONE; |
| 174 |
glDisable(GL_CULL_FACE); |
| 175 |
} |
| 176 |
if (m_cullface != TCullFace::NONE && (!rc.activeCullFace || *rc.activeCullFace != m_cullface)) |
| 177 |
{ |
| 178 |
glEnable(GL_CULL_FACE); |
| 179 |
glCullFace(m_cullface == TCullFace::FRONT ? GL_FRONT : GL_BACK); |
| 180 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 181 |
rc.activeCullFace = m_cullface; |
| 182 |
} |
| 183 |
|
| 184 |
// Draw: |
| 185 |
glDrawArrays(GL_TRIANGLES, 0, 3 * m_triangles.size()); |
| 186 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 187 |
|
| 188 |
if (attr_position) glDisableVertexAttribArray(*attr_position); |
| 189 |
if (attr_uv) glDisableVertexAttribArray(*attr_uv); |
| 190 |
if (attr_normals) glDisableVertexAttribArray(*attr_normals); |
| 191 |
|
| 192 |
#endif |
| 193 |
} |
| 194 |
|
| 195 |
void CRenderizableShaderTexturedTriangles::assignImage(const CImage& img, const CImage& imgAlpha) |
| 196 |
{ |
| 197 |
MRPT_START |
| 198 |
|
| 199 |
CRenderizable::notifyChange(); |
| 200 |
|
| 201 |
m_glTexture.unloadTexture(); |
| 202 |
|
| 203 |
// Make a copy: |
| 204 |
m_textureImage = img; |
| 205 |
m_textureImageAlpha = imgAlpha; |
| 206 |
m_textureImageAssigned = true; |
| 207 |
|
| 208 |
m_enableTransparency = true; |
| 209 |
|
| 210 |
MRPT_END |
| 211 |
} |
| 212 |
|
| 213 |
void CRenderizableShaderTexturedTriangles::assignImage(const CImage& img) |
| 214 |
{ |
| 215 |
MRPT_START |
| 216 |
|
| 217 |
CRenderizable::notifyChange(); |
| 218 |
|
| 219 |
m_glTexture.unloadTexture(); |
| 220 |
|
| 221 |
// Make a shallow copy: |
| 222 |
m_textureImage = img; |
| 223 |
m_textureImageAssigned = true; |
| 224 |
|
| 225 |
m_enableTransparency = false; |
| 226 |
|
| 227 |
MRPT_END |
| 228 |
} |
| 229 |
|
| 230 |
void CRenderizableShaderTexturedTriangles::assignImage(CImage&& img, CImage&& imgAlpha) |
| 231 |
{ |
| 232 |
MRPT_START |
| 233 |
|
| 234 |
CRenderizable::notifyChange(); |
| 235 |
|
| 236 |
m_glTexture.unloadTexture(); |
| 237 |
|
| 238 |
m_textureImage = std::move(img); |
| 239 |
m_textureImageAlpha = std::move(imgAlpha); |
| 240 |
m_textureImageAssigned = true; |
| 241 |
|
| 242 |
m_enableTransparency = true; |
| 243 |
|
| 244 |
MRPT_END |
| 245 |
} |
| 246 |
|
| 247 |
void CRenderizableShaderTexturedTriangles::assignImage(CImage&& img) |
| 248 |
{ |
| 249 |
MRPT_START |
| 250 |
|
| 251 |
CRenderizable::notifyChange(); |
| 252 |
|
| 253 |
m_glTexture.unloadTexture(); |
| 254 |
|
| 255 |
m_textureImage = std::move(img); |
| 256 |
m_textureImageAssigned = true; |
| 257 |
|
| 258 |
m_enableTransparency = false; |
| 259 |
|
| 260 |
MRPT_END |
| 261 |
} |
| 262 |
|
| 263 |
void CRenderizableShaderTexturedTriangles::initializeTextures() const |
| 264 |
{ |
| 265 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 266 |
|
| 267 |
// Note: if we are rendering and the user assigned us no texture image, |
| 268 |
// let's create a dummy one with the uniform CRenderizable's color: |
| 269 |
if (!textureImageHasBeenAssigned() || m_textureImage.isEmpty()) |
| 270 |
{ |
| 271 |
mrpt::img::CImage im_rgb(4, 4, mrpt::img::CH_RGB), im_a(4, 4, mrpt::img::CH_GRAY); |
| 272 |
const auto col = getColor_u8(); |
| 273 |
im_rgb.filledRectangle(0, 0, 3, 3, col); |
| 274 |
im_a.filledRectangle(0, 0, 3, 3, mrpt::img::TColor(col.A, col.A, col.A, col.A)); |
| 275 |
const_cast<CRenderizableShaderTexturedTriangles*>(this)->assignImage( |
| 276 |
std::move(im_rgb), std::move(im_a)); |
| 277 |
} |
| 278 |
|
| 279 |
if (m_glTexture.initialized()) |
| 280 |
{ |
| 281 |
m_glTexture.bindAsTexture2D(); // activate it: |
| 282 |
return; |
| 283 |
} |
| 284 |
|
| 285 |
// Reserve the new one -------------------------- |
| 286 |
mrpt::opengl::Texture::Options opts; |
| 287 |
opts.enableTransparency = m_enableTransparency; |
| 288 |
opts.magnifyLinearFilter = m_textureInterpolate; |
| 289 |
opts.generateMipMaps = m_textureUseMipMaps; |
| 290 |
|
| 291 |
m_glTexture.assignImage2D(m_textureImage, m_textureImageAlpha, opts); |
| 292 |
|
| 293 |
#endif |
| 294 |
} |
| 295 |
|
| 296 |
CRenderizableShaderTexturedTriangles::~CRenderizableShaderTexturedTriangles() |
| 297 |
{ |
| 298 |
try |
| 299 |
{ |
| 300 |
m_glTexture.unloadTexture(); |
| 301 |
} |
| 302 |
catch (const std::exception& e) |
| 303 |
{ |
| 304 |
std::cerr << "[~CRenderizableShaderTexturedTriangles] Ignoring exception: " |
| 305 |
<< mrpt::exception_to_str(e); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
void CRenderizableShaderTexturedTriangles::writeToStreamTexturedObject( |
| 310 |
mrpt::serialization::CArchive& out) const |
| 311 |
{ |
| 312 |
uint8_t ver = 3; |
| 313 |
|
| 314 |
out << ver; |
| 315 |
out << m_enableTransparency << m_textureInterpolate << m_textureUseMipMaps; |
| 316 |
out << m_textureImage; |
| 317 |
if (m_enableTransparency) out << m_textureImageAlpha; |
| 318 |
out << m_textureImageAssigned; |
| 319 |
out << m_enableLight << static_cast<uint8_t>(m_cullface); // v2 |
| 320 |
} |
| 321 |
|
| 322 |
void CRenderizableShaderTexturedTriangles::readFromStreamTexturedObject( |
| 323 |
mrpt::serialization::CArchive& in) |
| 324 |
{ |
| 325 |
uint8_t version; |
| 326 |
in >> version; |
| 327 |
|
| 328 |
switch (version) |
| 329 |
{ |
| 330 |
case 0: |
| 331 |
case 1: |
| 332 |
case 2: |
| 333 |
case 3: |
| 334 |
{ |
| 335 |
in >> m_enableTransparency >> m_textureInterpolate; |
| 336 |
if (version >= 3) |
| 337 |
{ |
| 338 |
in >> m_textureUseMipMaps; |
| 339 |
} |
| 340 |
else |
| 341 |
{ |
| 342 |
m_textureUseMipMaps = true; |
| 343 |
} |
| 344 |
in >> m_textureImage; |
| 345 |
if (m_enableTransparency) |
| 346 |
{ |
| 347 |
in >> m_textureImageAlpha; |
| 348 |
assignImage(m_textureImage, m_textureImageAlpha); |
| 349 |
} |
| 350 |
else |
| 351 |
{ |
| 352 |
assignImage(m_textureImage); |
| 353 |
} |
| 354 |
if (version >= 1) |
| 355 |
in >> m_textureImageAssigned; |
| 356 |
else |
| 357 |
m_textureImageAssigned = true; |
| 358 |
|
| 359 |
if (version >= 2) |
| 360 |
{ |
| 361 |
in >> m_enableLight; |
| 362 |
m_cullface = static_cast<TCullFace>(in.ReadAs<uint8_t>()); |
| 363 |
} |
| 364 |
} |
| 365 |
break; |
| 366 |
default: |
| 367 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 368 |
}; |
| 369 |
|
| 370 |
CRenderizable::notifyChange(); |
| 371 |
} |
| 372 |
|
| 373 |
const mrpt::math::TBoundingBoxf CRenderizableShaderTexturedTriangles::trianglesBoundingBox() const |
| 374 |
{ |
| 375 |
mrpt::math::TBoundingBoxf bb; |
| 376 |
|
| 377 |
std::shared_lock<std::shared_mutex> readLock(m_trianglesMtx.data); |
| 378 |
|
| 379 |
if (m_triangles.empty()) return bb; |
| 380 |
|
| 381 |
bb = mrpt::math::TBoundingBoxf::PlusMinusInfinity(); |
| 382 |
|
| 383 |
for (const auto& t : m_triangles) |
| 384 |
for (int i = 0; i < 3; i++) bb.updateWithPoint(t.vertices[i].xyzrgba.pt); |
| 385 |
|
| 386 |
return bb; |
| 387 |
} |