| 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/bimap.h> |
| 13 |
#include <mrpt/core/get_env.h> |
| 14 |
#include <mrpt/core/lock_helper.h> |
| 15 |
#include <mrpt/opengl/Texture.h> |
| 16 |
#include <mrpt/opengl/opengl_api.h> |
| 17 |
|
| 18 |
// Universal include for all versions of OpenCV |
| 19 |
#include <mrpt/3rdparty/do_opencv_includes.h> |
| 20 |
|
| 21 |
#include <iostream> |
| 22 |
#include <mutex> |
| 23 |
#include <set> |
| 24 |
|
| 25 |
using namespace mrpt::opengl; |
| 26 |
|
| 27 |
const bool MRPT_OPENGL_VERBOSE = mrpt::get_env<bool>("MRPT_OPENGL_VERBOSE", false); |
| 28 |
|
| 29 |
// Whether to profile memory allocations: |
| 30 |
// #define TEXTUREOBJ_PROFILE_MEM_ALLOC |
| 31 |
|
| 32 |
// Whether to use a memory pool for the texture buffer: |
| 33 |
#define TEXTUREOBJ_USE_MEMPOOL |
| 34 |
|
| 35 |
#ifdef TEXTUREOBJ_USE_MEMPOOL |
| 36 |
#include <mrpt/system/CGenericMemoryPool.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
void Texture::unloadTexture() |
| 40 |
{ |
| 41 |
m_tex.run_on_all( |
| 42 |
[](std::optional<texture_name_unit_t>& tnu) |
| 43 |
{ |
| 44 |
if (!tnu) return; |
| 45 |
releaseTextureName(tnu.value().name); |
| 46 |
tnu.reset(); |
| 47 |
}); |
| 48 |
} |
| 49 |
|
| 50 |
/** This class is a workaround to crashes and memory leaks caused by not |
| 51 |
* reserving and freeing opengl textures from the same thread. */ |
| 52 |
class TextureResourceHandler |
| 53 |
{ |
| 54 |
public: |
| 55 |
static TextureResourceHandler& Instance() |
| 56 |
{ |
| 57 |
static TextureResourceHandler o; |
| 58 |
return o; |
| 59 |
} |
| 60 |
|
| 61 |
/// Return textureName |
| 62 |
texture_name_t generateTextureID(const uint8_t* rgbDataForAssociation) |
| 63 |
{ |
| 64 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 65 |
auto lck = mrpt::lockHelper(m_texturesMtx); |
| 66 |
|
| 67 |
processDestroyQueue(); |
| 68 |
|
| 69 |
// Create one OpenGL texture |
| 70 |
GLuint textureID; |
| 71 |
glGenTextures(1, &textureID); |
| 72 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 73 |
m_textureReservedFrom[textureID] = std::this_thread::get_id(); |
| 74 |
|
| 75 |
if (rgbDataForAssociation) m_textureToRGBdata.insert(textureID, rgbDataForAssociation); |
| 76 |
|
| 77 |
if (MRPT_OPENGL_VERBOSE) |
| 78 |
std::cout << "[mrpt generateTextureID] textureName:" << textureID << std::endl; |
| 79 |
|
| 80 |
return textureID; |
| 81 |
#else |
| 82 |
THROW_EXCEPTION("This function needs OpenGL"); |
| 83 |
#endif |
| 84 |
} |
| 85 |
|
| 86 |
std::optional<texture_name_t> checkIfTextureAlreadyExists(const mrpt::img::CImage& rgb) |
| 87 |
{ |
| 88 |
#if (MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL) && MRPT_HAS_OPENCV |
| 89 |
auto lck = mrpt::lockHelper(m_texturesMtx); |
| 90 |
|
| 91 |
auto it = m_textureToRGBdata.getInverseMap().find(rgb.asCvMatRef().data); |
| 92 |
if (it != m_textureToRGBdata.getInverseMap().end()) |
| 93 |
return it->second; |
| 94 |
else |
| 95 |
return {}; |
| 96 |
#else |
| 97 |
return {}; |
| 98 |
#endif |
| 99 |
} |
| 100 |
|
| 101 |
void releaseTextureID(unsigned int texName) |
| 102 |
{ |
| 103 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 104 |
MRPT_START |
| 105 |
auto lck = mrpt::lockHelper(m_texturesMtx); |
| 106 |
|
| 107 |
if (MRPT_OPENGL_VERBOSE) |
| 108 |
std::cout << "[mrpt releaseTextureID] textureName: " << texName << std::endl; |
| 109 |
|
| 110 |
m_destroyQueue[m_textureReservedFrom.at(texName)].push_back(texName); |
| 111 |
processDestroyQueue(); |
| 112 |
MRPT_END |
| 113 |
#endif |
| 114 |
} |
| 115 |
|
| 116 |
private: |
| 117 |
TextureResourceHandler() |
| 118 |
{ |
| 119 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 120 |
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &m_maxTextureUnits); |
| 121 |
if (MRPT_OPENGL_VERBOSE) |
| 122 |
std::cout << "[mrpt TextureResourceHandler] maxTextureUnits:" << m_maxTextureUnits |
| 123 |
<< std::endl; |
| 124 |
#endif |
| 125 |
} |
| 126 |
|
| 127 |
void processDestroyQueue() |
| 128 |
{ |
| 129 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 130 |
if (auto itLst = m_destroyQueue.find(std::this_thread::get_id()); |
| 131 |
itLst != m_destroyQueue.end() && !itLst->second.empty()) |
| 132 |
{ |
| 133 |
auto& lst = itLst->second; |
| 134 |
|
| 135 |
// Delete in OpenGL: |
| 136 |
glDeleteTextures(lst.size(), lst.data()); |
| 137 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 138 |
|
| 139 |
// delete in rgb data container too: |
| 140 |
for (const auto id : lst) |
| 141 |
{ |
| 142 |
if (m_textureToRGBdata.hasKey(id)) m_textureToRGBdata.erase_by_key(id); |
| 143 |
} |
| 144 |
|
| 145 |
if (MRPT_OPENGL_VERBOSE) |
| 146 |
{ |
| 147 |
std::cout << "[mrpt processDestroyQueue] threadId=" << std::this_thread::get_id() |
| 148 |
<< " destroyed " << lst.size() << "\n"; |
| 149 |
} |
| 150 |
lst.clear(); |
| 151 |
m_destroyQueue.erase(itLst); |
| 152 |
} |
| 153 |
if (!m_destroyQueue.empty() && MRPT_OPENGL_VERBOSE) |
| 154 |
{ |
| 155 |
std::cout << "[mrpt processDestroyQueue] threadId=" << std::this_thread::get_id() |
| 156 |
<< ". Remaining at output: "; |
| 157 |
for (const auto& lst : m_destroyQueue) |
| 158 |
std::cout << "[" << lst.first << "]=" << lst.second.size() << " textures "; |
| 159 |
std::cout << "\n"; |
| 160 |
} |
| 161 |
#endif |
| 162 |
} |
| 163 |
|
| 164 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 165 |
std::mutex m_texturesMtx; |
| 166 |
std::map<GLuint, std::thread::id> m_textureReservedFrom; |
| 167 |
std::map<std::thread::id, std::vector<GLuint>> m_destroyQueue; |
| 168 |
mrpt::containers::bimap<GLuint, const uint8_t*> m_textureToRGBdata; |
| 169 |
GLint m_maxTextureUnits; |
| 170 |
#endif |
| 171 |
}; |
| 172 |
|
| 173 |
std::optional<texture_name_t> checkIfTextureAlreadyExists(const mrpt::img::CImage& rgb) |
| 174 |
{ |
| 175 |
return TextureResourceHandler::Instance().checkIfTextureAlreadyExists(rgb); |
| 176 |
} |
| 177 |
|
| 178 |
/// Returns: [texture name, texture unit] |
| 179 |
texture_name_t mrpt::opengl::getNewTextureNumber(const uint8_t* optionalRgbDataForAssociation) |
| 180 |
{ |
| 181 |
return TextureResourceHandler::Instance().generateTextureID(optionalRgbDataForAssociation); |
| 182 |
} |
| 183 |
|
| 184 |
void mrpt::opengl::releaseTextureName(const texture_name_t& t) |
| 185 |
{ |
| 186 |
TextureResourceHandler::Instance().releaseTextureID(t); |
| 187 |
} |
| 188 |
|
| 189 |
bool Texture::initialized() const |
| 190 |
{ |
| 191 |
// already assigned an ID? |
| 192 |
return m_tex.get().has_value(); |
| 193 |
} |
| 194 |
|
| 195 |
void Texture::assignImage2D(const mrpt::img::CImage& rgb, const Options& o, int textureUnit) |
| 196 |
{ |
| 197 |
try |
| 198 |
{ |
| 199 |
internalAssignImage_2D(&rgb, nullptr, o, textureUnit); |
| 200 |
} |
| 201 |
catch (std::exception& e) |
| 202 |
{ |
| 203 |
THROW_EXCEPTION_FMT("Error assigning texture: %s", e.what()); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
void Texture::assignImage2D( |
| 208 |
const mrpt::img::CImage& rgb, const mrpt::img::CImage& alpha, const Options& o, int textureUnit) |
| 209 |
{ |
| 210 |
try |
| 211 |
{ |
| 212 |
internalAssignImage_2D(&rgb, &alpha, o, textureUnit); |
| 213 |
} |
| 214 |
catch (std::exception& e) |
| 215 |
{ |
| 216 |
THROW_EXCEPTION_FMT("Error assigning texture: %s", e.what()); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
// Data types for memory pooling CRenderizableShaderTexturedTriangles: |
| 221 |
#ifdef TEXTUREOBJ_USE_MEMPOOL |
| 222 |
|
| 223 |
struct CRenderizableShaderTexturedTriangles_MemPoolParams |
| 224 |
{ |
| 225 |
/** size of the vector<unsigned char> */ |
| 226 |
size_t len = 0; |
| 227 |
|
| 228 |
inline bool isSuitable(const CRenderizableShaderTexturedTriangles_MemPoolParams& req) const |
| 229 |
{ |
| 230 |
return len == req.len; |
| 231 |
} |
| 232 |
}; |
| 233 |
struct CRenderizableShaderTexturedTriangles_MemPoolData |
| 234 |
{ |
| 235 |
std::vector<uint8_t> data; |
| 236 |
}; |
| 237 |
|
| 238 |
using TMyMemPool = mrpt::system::CGenericMemoryPool< |
| 239 |
CRenderizableShaderTexturedTriangles_MemPoolParams, |
| 240 |
CRenderizableShaderTexturedTriangles_MemPoolData>; |
| 241 |
#endif |
| 242 |
|
| 243 |
// Auxiliary function for loadTextureInOpenGL(): reserve memory and return |
| 244 |
// 16byte aligned starting point within it: |
| 245 |
namespace |
| 246 |
{ |
| 247 |
unsigned char* reserveDataBuffer(size_t len, std::vector<uint8_t>& data) |
| 248 |
{ |
| 249 |
#ifdef TEXTUREOBJ_USE_MEMPOOL |
| 250 |
TMyMemPool* pool = TMyMemPool::getInstance(); |
| 251 |
if (pool) |
| 252 |
{ |
| 253 |
CRenderizableShaderTexturedTriangles_MemPoolParams mem_params; |
| 254 |
mem_params.len = len; |
| 255 |
|
| 256 |
CRenderizableShaderTexturedTriangles_MemPoolData* mem_block = pool->request_memory(mem_params); |
| 257 |
if (mem_block) |
| 258 |
{ |
| 259 |
// Recover the memory block via a swap: |
| 260 |
data.swap(mem_block->data); |
| 261 |
delete mem_block; |
| 262 |
} |
| 263 |
} |
| 264 |
#endif |
| 265 |
data.resize(len); |
| 266 |
void* ptr = &data[0]; |
| 267 |
size_t space = len; |
| 268 |
return reinterpret_cast<unsigned char*>(std::align(16, 1 /*dummy size*/, ptr, space)); |
| 269 |
} |
| 270 |
} // namespace |
| 271 |
|
| 272 |
void Texture::internalAssignImage_2D( |
| 273 |
const mrpt::img::CImage* in_rgb, |
| 274 |
const mrpt::img::CImage* in_alpha, |
| 275 |
const Options& o, |
| 276 |
int textureUnit) |
| 277 |
{ |
| 278 |
#if (MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL) && MRPT_HAS_OPENCV |
| 279 |
unsigned char* dataAligned = nullptr; |
| 280 |
std::vector<uint8_t> data; |
| 281 |
|
| 282 |
#ifdef TEXTUREOBJ_PROFILE_MEM_ALLOC |
| 283 |
static mrpt::system::CTimeLogger tim; |
| 284 |
#endif |
| 285 |
|
| 286 |
ASSERT_(in_rgb); |
| 287 |
|
| 288 |
in_rgb->forceLoad(); // just in case they are lazy-load imgs |
| 289 |
if (in_alpha) in_alpha->forceLoad(); |
| 290 |
|
| 291 |
// Check if we already have this texture loaded in GPU and avoid creating |
| 292 |
// duplicated texture ID: |
| 293 |
const auto existingTextureId = checkIfTextureAlreadyExists(*in_rgb); |
| 294 |
if (existingTextureId.has_value()) |
| 295 |
{ |
| 296 |
get() = existingTextureId.value(); |
| 297 |
get()->unit = textureUnit; |
| 298 |
|
| 299 |
if (MRPT_OPENGL_VERBOSE) |
| 300 |
std::cout << "[mrpt internalAssignImage_2D] Reusing existing textureName:" << get()->name |
| 301 |
<< "\n"; |
| 302 |
|
| 303 |
return; |
| 304 |
} |
| 305 |
|
| 306 |
mrpt::img::CImage rgb; |
| 307 |
|
| 308 |
switch (in_rgb->getPixelDepth()) |
| 309 |
{ |
| 310 |
// Ideal case, nothing to do: |
| 311 |
case mrpt::img::PixelDepth::D8U: |
| 312 |
rgb = mrpt::img::CImage(*in_rgb, mrpt::img::SHALLOW_COPY); |
| 313 |
break; |
| 314 |
|
| 315 |
case mrpt::img::PixelDepth::D16U: |
| 316 |
{ |
| 317 |
double ratio; |
| 318 |
if (o.autoScale16to8bitConversion) |
| 319 |
{ |
| 320 |
// enhance brigthness: |
| 321 |
|
| 322 |
cv::Scalar meanColor = cv::mean(in_rgb->asCvMatRef()); |
| 323 |
const double avrVal = meanColor.val[0]; |
| 324 |
ratio = std::max<double>(1.0, 2 * avrVal); |
| 325 |
} |
| 326 |
else |
| 327 |
{ |
| 328 |
ratio = 65536.0; |
| 329 |
} |
| 330 |
|
| 331 |
rgb.resize(in_rgb->getWidth(), in_rgb->getHeight(), mrpt::img::CH_RGB); |
| 332 |
cv::convertScaleAbs(in_rgb->asCvMatRef(), rgb.asCvMatRef(), 255.0 / ratio); |
| 333 |
} |
| 334 |
break; |
| 335 |
|
| 336 |
default: |
| 337 |
THROW_EXCEPTION_FMT( |
| 338 |
"Unhandled pixel depth: PixelDepth=#%i", static_cast<int>(in_rgb->getPixelDepth())); |
| 339 |
}; |
| 340 |
|
| 341 |
// Shallow copy of the images, for the case we need to downsample them |
| 342 |
// below: |
| 343 |
mrpt::img::CImage alpha; |
| 344 |
if (in_alpha) alpha = mrpt::img::CImage(*in_alpha, mrpt::img::SHALLOW_COPY); |
| 345 |
|
| 346 |
// allocate texture names: |
| 347 |
get() = getNewTextureNumber(in_rgb->asCvMatRef().data); |
| 348 |
get()->unit = textureUnit; |
| 349 |
|
| 350 |
// activate the texture unit first before binding texture |
| 351 |
bindAsTexture2D(); |
| 352 |
|
| 353 |
// when texture area is small, linear interpolation: |
| 354 |
if (o.generateMipMaps) |
| 355 |
{ |
| 356 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR); |
| 357 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 358 |
} |
| 359 |
else |
| 360 |
{ |
| 361 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 362 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 363 |
} |
| 364 |
|
| 365 |
// when texture area is large: |
| 366 |
glTexParameterf( |
| 367 |
GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, o.magnifyLinearFilter ? GL_LINEAR : GL_NEAREST); |
| 368 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 369 |
|
| 370 |
const auto lmbdWrapMap = [](const Wrapping w) |
| 371 |
{ |
| 372 |
// clang-format off |
| 373 |
switch (w) |
| 374 |
{ |
| 375 |
case Wrapping::Repeat: return GL_REPEAT; |
| 376 |
case Wrapping::MirroredRepeat: return GL_MIRRORED_REPEAT; |
| 377 |
case Wrapping::ClampToEdge: return GL_CLAMP_TO_EDGE; |
| 378 |
case Wrapping::ClapToBorder: return GL_CLAMP_TO_BORDER; |
| 379 |
default: |
| 380 |
THROW_EXCEPTION_FMT("Invalid texture wrapping value: %i", static_cast<int>(w)); |
| 381 |
}; |
| 382 |
// clang-format on |
| 383 |
}; |
| 384 |
|
| 385 |
// if wrap is true, the texture wraps over at the edges (repeat) |
| 386 |
// ... false, the texture ends at the edges (clamp) |
| 387 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, lmbdWrapMap(o.wrappingModeS)); |
| 388 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 389 |
|
| 390 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, lmbdWrapMap(o.wrappingModeT)); |
| 391 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 392 |
|
| 393 |
// Ensure that the images do not overpass the maximum dimensions allowed |
| 394 |
// by OpenGL: |
| 395 |
// ------------------------------------------------------------------------------------ |
| 396 |
GLint texSize; |
| 397 |
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize); |
| 398 |
while (rgb.getHeight() > (unsigned int)texSize || rgb.getWidth() > (unsigned int)texSize) |
| 399 |
{ |
| 400 |
static bool warningEmitted = false; |
| 401 |
if (!warningEmitted) |
| 402 |
{ |
| 403 |
warningEmitted = true; |
| 404 |
std::cerr << "[mrpt::opengl::Texture] " |
| 405 |
"**PERFORMACE WARNING**:\n" |
| 406 |
<< " Downsampling texture image of size " << rgb.getWidth() << "x" |
| 407 |
<< rgb.getHeight() |
| 408 |
<< " since maximum allowed OpenGL texture size " |
| 409 |
"(GL_MAX_TEXTURE_SIZE) is " |
| 410 |
<< texSize << "\n"; |
| 411 |
} |
| 412 |
|
| 413 |
rgb = rgb.scaleHalf(mrpt::img::IMG_INTERP_LINEAR); |
| 414 |
if (in_alpha) alpha = alpha.scaleHalf(mrpt::img::IMG_INTERP_LINEAR); |
| 415 |
} |
| 416 |
|
| 417 |
const int width = rgb.getWidth(); |
| 418 |
const int height = rgb.getHeight(); |
| 419 |
|
| 420 |
#ifdef TEXTUREOBJ_PROFILE_MEM_ALLOC |
| 421 |
{ |
| 422 |
const std::string sSec = mrpt::format( |
| 423 |
"opengl_texture: load %ix%i %s %stransp", width, height, rgb->isColor() ? "RGB" : "BW", |
| 424 |
m_enableTransparency ? "" : "no "); |
| 425 |
tim.enter(sSec.c_str()); |
| 426 |
} |
| 427 |
#endif |
| 428 |
|
| 429 |
// GL_LUMINANCE and GL_LUMINANCE_ALPHA were removed in OpenGL 3.1 |
| 430 |
// Convert grayscale images into color: |
| 431 |
if (!rgb.isColor()) rgb = rgb.colorImage(); |
| 432 |
|
| 433 |
// ---------------------------------------------- |
| 434 |
// Color texture WITH alpha channel |
| 435 |
// ---------------------------------------------- |
| 436 |
if (o.enableTransparency) |
| 437 |
{ |
| 438 |
ASSERT_(!alpha.isColor()); |
| 439 |
ASSERT_EQUAL_(alpha.getWidth(), rgb.getWidth()); |
| 440 |
ASSERT_EQUAL_(alpha.getHeight(), rgb.getHeight()); |
| 441 |
|
| 442 |
#ifdef TEXTUREOBJ_PROFILE_MEM_ALLOC |
| 443 |
const std::string sSec = |
| 444 |
mrpt::format("opengl_texture_alloc %ix%i (color,trans)", width, height); |
| 445 |
tim.enter(sSec.c_str()); |
| 446 |
#endif |
| 447 |
|
| 448 |
dataAligned = reserveDataBuffer(height * width * 4 + 512, data); |
| 449 |
|
| 450 |
#ifdef TEXTUREOBJ_PROFILE_MEM_ALLOC |
| 451 |
tim.leave(sSec.c_str()); |
| 452 |
#endif |
| 453 |
|
| 454 |
for (int y = 0; y < height; y++) |
| 455 |
{ |
| 456 |
unsigned char* ptrSrcCol = rgb(0, y, 0); |
| 457 |
unsigned char* ptrSrcAlfa = alpha(0, y); |
| 458 |
unsigned char* ptr = dataAligned + y * width * 4; |
| 459 |
|
| 460 |
for (int x = 0; x < width; x++) |
| 461 |
{ |
| 462 |
*ptr++ = *ptrSrcCol++; |
| 463 |
*ptr++ = *ptrSrcCol++; |
| 464 |
*ptr++ = *ptrSrcCol++; |
| 465 |
*ptr++ = *ptrSrcAlfa++; |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
// Prepare image data types: |
| 470 |
const GLenum img_type = GL_UNSIGNED_BYTE; |
| 471 |
// Reverse RGB <-> BGR order? |
| 472 |
const bool is_RGB_order = (rgb.getChannelsOrder() == std::string("RGB")); |
| 473 |
const GLenum img_format = (is_RGB_order ? GL_RGBA : GL_BGRA); |
| 474 |
|
| 475 |
// Send image data to OpenGL: |
| 476 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 477 |
glPixelStorei(GL_UNPACK_ROW_LENGTH, width); |
| 478 |
glTexImage2D( |
| 479 |
GL_TEXTURE_2D, 0 /*level*/, GL_RGBA8 /* RGB components */, width, height, 0 /*border*/, |
| 480 |
img_format, img_type, dataAligned); |
| 481 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 482 |
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); // Reset |
| 483 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 484 |
|
| 485 |
} // End of color texture WITH trans. |
| 486 |
else |
| 487 |
{ |
| 488 |
// Color texture without transparency, |
| 489 |
// or with integrated RGBA alpha channel |
| 490 |
// -------------------------------------- |
| 491 |
// Prepare image data types: |
| 492 |
const GLenum img_type = GL_UNSIGNED_BYTE; |
| 493 |
const int nBytesPerPixel = rgb.channelCount(); |
| 494 |
// Reverse RGB <-> BGR order? |
| 495 |
const bool is_RGB_order = (rgb.getChannelsOrder() == std::string("RGB")); |
| 496 |
const GLenum img_format = [=]() |
| 497 |
{ |
| 498 |
switch (nBytesPerPixel) |
| 499 |
{ |
| 500 |
case 1: |
| 501 |
return GL_LUMINANCE; |
| 502 |
case 3: |
| 503 |
return (is_RGB_order ? GL_RGB : GL_BGR); |
| 504 |
case 4: |
| 505 |
return GL_BGRA; |
| 506 |
}; |
| 507 |
THROW_EXCEPTION("Invalid texture image channel count."); |
| 508 |
}(); |
| 509 |
|
| 510 |
// Send image data to OpenGL: |
| 511 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 512 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 513 |
glPixelStorei(GL_UNPACK_ROW_LENGTH, rgb.getRowStride() / nBytesPerPixel); |
| 514 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 515 |
glTexImage2D( |
| 516 |
GL_TEXTURE_2D, 0 /*level*/, nBytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8 /* RGB components */, |
| 517 |
width, height, 0 /*border*/, img_format, img_type, rgb.ptrLine<uint8_t>(0)); |
| 518 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 519 |
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); // Reset |
| 520 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 521 |
} // End of color texture WITHOUT trans. |
| 522 |
|
| 523 |
if (o.generateMipMaps) |
| 524 |
{ |
| 525 |
glGenerateMipmap(GL_TEXTURE_2D); |
| 526 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 527 |
} |
| 528 |
|
| 529 |
// Was: m_texture_is_loaded = true; |
| 530 |
// Now this situation is represented by the optional m_glTexture having |
| 531 |
// a valid value. |
| 532 |
|
| 533 |
#ifdef TEXTUREOBJ_PROFILE_MEM_ALLOC |
| 534 |
{ |
| 535 |
const std::string sSec = mrpt::format( |
| 536 |
"opengl_texture: load %ix%i %s %stransp", width, height, rgb->isColor() ? "RGB" : "BW", |
| 537 |
m_enableTransparency ? "" : "no "); |
| 538 |
tim.leave(sSec.c_str()); |
| 539 |
} |
| 540 |
#endif |
| 541 |
|
| 542 |
#ifdef TEXTUREOBJ_USE_MEMPOOL |
| 543 |
// Before freeing the buffer in "data", donate my memory to the pool: |
| 544 |
if (!data.empty()) |
| 545 |
{ |
| 546 |
TMyMemPool* pool = TMyMemPool::getInstance(); |
| 547 |
if (pool) |
| 548 |
{ |
| 549 |
CRenderizableShaderTexturedTriangles_MemPoolParams mem_params; |
| 550 |
mem_params.len = data.size(); |
| 551 |
|
| 552 |
auto* mem_block = new CRenderizableShaderTexturedTriangles_MemPoolData(); |
| 553 |
data.swap(mem_block->data); |
| 554 |
|
| 555 |
pool->dump_to_pool(mem_params, mem_block); |
| 556 |
} |
| 557 |
} |
| 558 |
#endif |
| 559 |
#endif |
| 560 |
} |
| 561 |
|
| 562 |
void Texture::bindAsTexture2D() |
| 563 |
{ |
| 564 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 565 |
glActiveTexture(GL_TEXTURE0 + get()->unit); |
| 566 |
glBindTexture(GL_TEXTURE_2D, get()->name); |
| 567 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 568 |
#endif |
| 569 |
} |
| 570 |
|
| 571 |
void Texture::bindAsCubeTexture() |
| 572 |
{ |
| 573 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 574 |
glActiveTexture(GL_TEXTURE0 + get()->unit); |
| 575 |
glBindTexture(GL_TEXTURE_CUBE_MAP, get()->name); |
| 576 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 577 |
#endif |
| 578 |
} |
| 579 |
|
| 580 |
void Texture::assignCubeImages(const std::array<mrpt::img::CImage, 6>& imgs, int textureUnit) |
| 581 |
{ |
| 582 |
#if MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL |
| 583 |
|
| 584 |
// just in case they are lazy-load imgs |
| 585 |
for (auto& im : imgs) |
| 586 |
{ |
| 587 |
im.forceLoad(); |
| 588 |
ASSERT_(im.getPixelDepth() == mrpt::img::PixelDepth::D8U); |
| 589 |
ASSERT_(im.isColor()); |
| 590 |
} |
| 591 |
|
| 592 |
// allocate texture "name" (ID): |
| 593 |
get() = getNewTextureNumber(nullptr); /* no cached img for cube textures */ |
| 594 |
|
| 595 |
// activate the texture unit first before binding texture |
| 596 |
bindAsCubeTexture(); |
| 597 |
|
| 598 |
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 599 |
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 600 |
|
| 601 |
// Special "wrap" for cube skybox textures: |
| 602 |
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 603 |
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 604 |
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); |
| 605 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 606 |
|
| 607 |
// Check max hardware-allowed texture size: |
| 608 |
{ |
| 609 |
GLint maxTexSize; |
| 610 |
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize); |
| 611 |
ASSERT_LE_(imgs[0].getHeight(), (unsigned int)maxTexSize); |
| 612 |
ASSERT_LE_(imgs[0].getWidth(), (unsigned int)maxTexSize); |
| 613 |
} |
| 614 |
|
| 615 |
for (int face = 0; face < 6; face++) |
| 616 |
{ |
| 617 |
const auto& rgb = imgs.at(face); |
| 618 |
|
| 619 |
const int width = rgb.getWidth(); |
| 620 |
const int height = rgb.getHeight(); |
| 621 |
|
| 622 |
// Format: color texture without transparency, or with integrated |
| 623 |
// RGBA alpha channel |
| 624 |
|
| 625 |
// Prepare image data types: |
| 626 |
const GLenum img_type = GL_UNSIGNED_BYTE; |
| 627 |
const int nBytesPerPixel = rgb.channelCount(); |
| 628 |
// Reverse RGB <-> BGR order? |
| 629 |
const bool is_RGB_order = (rgb.getChannelsOrder() == std::string("RGB")); |
| 630 |
const GLenum img_format = [=]() |
| 631 |
{ |
| 632 |
switch (nBytesPerPixel) |
| 633 |
{ |
| 634 |
case 1: |
| 635 |
return GL_LUMINANCE; |
| 636 |
case 3: |
| 637 |
return (is_RGB_order ? GL_RGB : GL_BGR); |
| 638 |
case 4: |
| 639 |
return GL_BGRA; |
| 640 |
}; |
| 641 |
THROW_EXCEPTION("Invalid texture image channel count."); |
| 642 |
}(); |
| 643 |
|
| 644 |
// Send image data to OpenGL: |
| 645 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 646 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 647 |
glPixelStorei(GL_UNPACK_ROW_LENGTH, rgb.getRowStride() / nBytesPerPixel); |
| 648 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 649 |
glTexImage2D( |
| 650 |
GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0 /*level*/, |
| 651 |
nBytesPerPixel == 3 ? GL_RGB8 : GL_RGBA8 /* RGB components */, width, height, 0 /*border*/, |
| 652 |
img_format, img_type, rgb.ptrLine<uint8_t>(0)); |
| 653 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 654 |
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); // Reset |
| 655 |
CHECK_OPENGL_ERROR_IN_DEBUG(); |
| 656 |
|
| 657 |
} // end for each "face" |
| 658 |
|
| 659 |
#endif |
| 660 |
} |