| 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 |
// This file contains portions of code from Assimp's example: |
| 11 |
// "Sample_SimpleOpenGL.c" |
| 12 |
|
| 13 |
#include "opengl-precomp.h" // Precompiled header |
| 14 |
// |
| 15 |
#include <mrpt/opengl/CAssimpModel.h> |
| 16 |
|
| 17 |
#if MRPT_HAS_ASSIMP |
| 18 |
#if defined(MRPT_ASSIMP_VERSION_MAJOR) && MRPT_ASSIMP_VERSION_MAJOR < 3 |
| 19 |
#include <aiPostProcess.h> |
| 20 |
#include <aiScene.h> |
| 21 |
#include <assimp.h> |
| 22 |
#else |
| 23 |
#include <assimp/cimport.h> |
| 24 |
#include <assimp/postprocess.h> |
| 25 |
#include <assimp/scene.h> |
| 26 |
#include <assimp/types.h> |
| 27 |
|
| 28 |
#include <assimp/DefaultLogger.hpp> |
| 29 |
#include <assimp/Exporter.hpp> |
| 30 |
#include <assimp/Importer.hpp> |
| 31 |
#include <assimp/LogStream.hpp> |
| 32 |
#endif |
| 33 |
#endif |
| 34 |
|
| 35 |
#include <mrpt/core/lock_helper.h> |
| 36 |
#include <mrpt/core/round.h> |
| 37 |
#include <mrpt/opengl/opengl_api.h> |
| 38 |
#include <mrpt/serialization/CArchive.h> |
| 39 |
#include <mrpt/system/filesystem.h> |
| 40 |
|
| 41 |
// Universal include for all versions of OpenCV |
| 42 |
#if MRPT_HAS_OPENCV |
| 43 |
#include <mrpt/3rdparty/do_opencv_includes.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
#include <mutex> |
| 47 |
#include <optional> |
| 48 |
|
| 49 |
using namespace mrpt; |
| 50 |
using namespace mrpt::opengl; |
| 51 |
using namespace mrpt::math; |
| 52 |
using mrpt::img::CImage; |
| 53 |
|
| 54 |
IMPLEMENTS_SERIALIZABLE(CAssimpModel, CRenderizable, mrpt::opengl) |
| 55 |
|
| 56 |
namespace mrpt::opengl::internal |
| 57 |
{ |
| 58 |
class TexturesCache |
| 59 |
{ |
| 60 |
public: |
| 61 |
static TexturesCache& Instance() |
| 62 |
{ |
| 63 |
static TexturesCache i; |
| 64 |
return i; |
| 65 |
} |
| 66 |
|
| 67 |
struct CachedTexturesInfo |
| 68 |
{ |
| 69 |
bool load_ok = false; |
| 70 |
bool load_attempted = false; |
| 71 |
mrpt::img::CImage img_rgb; |
| 72 |
std::optional<mrpt::img::CImage> img_alpha; |
| 73 |
}; |
| 74 |
|
| 75 |
CachedTexturesInfo& get( |
| 76 |
const CAssimpModel::filepath_t& texturePath, |
| 77 |
bool verboseLoad, |
| 78 |
#if MRPT_HAS_ASSIMP |
| 79 |
const aiScene* scene |
| 80 |
#else |
| 81 |
const void* scene |
| 82 |
#endif |
| 83 |
) |
| 84 |
{ |
| 85 |
using namespace std::string_literals; |
| 86 |
|
| 87 |
auto lck = mrpt::lockHelper(gTextureCacheMtx); |
| 88 |
auto& entry = gTextureCache[texturePath]; |
| 89 |
if (entry.load_attempted) return entry; |
| 90 |
|
| 91 |
// Load images: |
| 92 |
// TGA is handled specially since it's not supported by OpenCV: |
| 93 |
if (mrpt::system::lowerCase(mrpt::system::extractFileExtension(texturePath)) == "tga"s) |
| 94 |
{ |
| 95 |
entry.img_alpha.emplace(); |
| 96 |
|
| 97 |
entry.load_ok = mrpt::img::CImage::loadTGA(texturePath, entry.img_rgb, *entry.img_alpha); |
| 98 |
} |
| 99 |
else |
| 100 |
{ |
| 101 |
entry.load_ok = entry.img_rgb.loadFromFile(texturePath); |
| 102 |
} |
| 103 |
|
| 104 |
if (entry.load_ok) |
| 105 |
{ |
| 106 |
if (verboseLoad) std::cout << "[CAssimpModel] Loaded texture: " << texturePath << "\n"; |
| 107 |
} |
| 108 |
#if MRPT_HAS_ASSIMP && MRPT_HAS_OPENCV |
| 109 |
else if (scene->HasTextures()) |
| 110 |
{ |
| 111 |
// Embedded texture? |
| 112 |
auto aiTex = scene->GetEmbeddedTexture(texturePath.c_str()); |
| 113 |
if (aiTex) |
| 114 |
{ |
| 115 |
const auto texW = aiTex->mWidth; |
| 116 |
const auto texH = aiTex->mHeight; |
| 117 |
|
| 118 |
if (texH == 0) |
| 119 |
{ |
| 120 |
// compressed format: |
| 121 |
// aiTex->achFormatHint; |
| 122 |
|
| 123 |
const cv::Mat data(texW, 1, CV_8UC1, aiTex->pcData); |
| 124 |
|
| 125 |
const cv::Mat im = cv::imdecode(data, cv::IMREAD_UNCHANGED); |
| 126 |
|
| 127 |
if (!im.empty()) |
| 128 |
{ |
| 129 |
// load ok: |
| 130 |
entry.load_ok = true; |
| 131 |
|
| 132 |
entry.img_rgb = mrpt::img::CImage(im, mrpt::img::DEEP_COPY); |
| 133 |
} |
| 134 |
} |
| 135 |
else |
| 136 |
{ |
| 137 |
// uncompressed format: |
| 138 |
THROW_EXCEPTION( |
| 139 |
"Support for uncompressed embedded textures not " |
| 140 |
"implemented yet for mrpt::opengl::CAssimpModel."); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
#endif |
| 145 |
|
| 146 |
if (!entry.load_ok) |
| 147 |
{ |
| 148 |
/* Error occurred */ |
| 149 |
const std::string sError = |
| 150 |
mrpt::format("[CAssimpModel] Couldn't load texture image: '%s'", texturePath.c_str()); |
| 151 |
std::cerr << sError << std::endl; |
| 152 |
} |
| 153 |
|
| 154 |
entry.load_attempted = true; |
| 155 |
return entry; |
| 156 |
} |
| 157 |
|
| 158 |
private: |
| 159 |
TexturesCache() = default; |
| 160 |
~TexturesCache() = default; |
| 161 |
|
| 162 |
std::map<CAssimpModel::filepath_t, CachedTexturesInfo> gTextureCache; |
| 163 |
std::mutex gTextureCacheMtx; |
| 164 |
}; |
| 165 |
|
| 166 |
struct RenderElements |
| 167 |
{ |
| 168 |
std::vector<mrpt::math::TPoint3Df>* lines_vbd = nullptr; |
| 169 |
std::vector<mrpt::img::TColor>* lines_cbd = nullptr; |
| 170 |
std::vector<mrpt::math::TPoint3Df>* pts_vbd = nullptr; |
| 171 |
std::vector<mrpt::img::TColor>* pts_cbd = nullptr; |
| 172 |
std::vector<mrpt::opengl::TTriangle>* tris = nullptr; |
| 173 |
|
| 174 |
// textures: |
| 175 |
const std::map<std::string, CAssimpModel::TInfoPerTexture>* ipt = nullptr; |
| 176 |
const std::vector<CSetOfTexturedTriangles::Ptr>* textObjs = nullptr; |
| 177 |
}; |
| 178 |
} // namespace mrpt::opengl::internal |
| 179 |
|
| 180 |
struct CAssimpModel::Impl |
| 181 |
{ |
| 182 |
#if (MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL) && MRPT_HAS_ASSIMP |
| 183 |
Impl() = default; |
| 184 |
~Impl() = default; |
| 185 |
|
| 186 |
Impl(const Impl& o) { *this = o; } |
| 187 |
Impl& operator=(const Impl&) |
| 188 |
{ |
| 189 |
THROW_EXCEPTION("Copying CAssimpModel objects via operator= not allowed."); |
| 190 |
return *this; |
| 191 |
} |
| 192 |
|
| 193 |
Assimp::Importer importer; |
| 194 |
Assimp::Exporter exporter; |
| 195 |
const aiScene* scene = nullptr; // Memory owned by "importer" |
| 196 |
#endif |
| 197 |
}; |
| 198 |
|
| 199 |
const aiScene* CAssimpModel::getAssimpScene() const |
| 200 |
{ |
| 201 |
#if (MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL) && MRPT_HAS_ASSIMP |
| 202 |
return m_assimp_scene->scene; |
| 203 |
#else |
| 204 |
return nullptr; |
| 205 |
#endif |
| 206 |
} |
| 207 |
|
| 208 |
#if (MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL) && MRPT_HAS_ASSIMP |
| 209 |
|
| 210 |
// Just return the diffuse color: |
| 211 |
namespace |
| 212 |
{ |
| 213 |
mrpt::img::TColor apply_material( |
| 214 |
const aiMaterial* mtl, const img::TColor& defaultColor, const bool ignoreMaterialColor); |
| 215 |
void get_bounding_box(const aiScene* sc, aiVector3D* min, aiVector3D* max); |
| 216 |
void get_bounding_box_for_node( |
| 217 |
const aiScene* sc, const aiNode* nd, aiVector3D* min, aiVector3D* max, aiMatrix4x4* trafo); |
| 218 |
} // namespace |
| 219 |
#endif // MRPT_HAS_OPENGL_GLUT && MRPT_HAS_ASSIMP |
| 220 |
|
| 221 |
void CAssimpModel::render(const RenderContext& rc) const |
| 222 |
{ |
| 223 |
switch (rc.shader_id) |
| 224 |
{ |
| 225 |
case DefaultShaderID::POINTS: |
| 226 |
CRenderizableShaderPoints::render(rc); |
| 227 |
break; |
| 228 |
case DefaultShaderID::TRIANGLES_LIGHT: |
| 229 |
CRenderizableShaderTriangles::render(rc); |
| 230 |
break; |
| 231 |
case DefaultShaderID::WIREFRAME: |
| 232 |
CRenderizableShaderWireFrame::render(rc); |
| 233 |
break; |
| 234 |
}; |
| 235 |
} |
| 236 |
void CAssimpModel::renderUpdateBuffers() const |
| 237 |
{ |
| 238 |
// onUpdateBuffers_all: already called upon loading of the model from file. |
| 239 |
|
| 240 |
CRenderizableShaderPoints::renderUpdateBuffers(); |
| 241 |
CRenderizableShaderTriangles::renderUpdateBuffers(); |
| 242 |
CRenderizableShaderWireFrame::renderUpdateBuffers(); |
| 243 |
} |
| 244 |
|
| 245 |
// special case for assimp: update all buffers within one run over the scene |
| 246 |
// structure. |
| 247 |
void CAssimpModel::onUpdateBuffers_all() |
| 248 |
{ |
| 249 |
auto& lines_vbd = CRenderizableShaderWireFrame::m_vertex_buffer_data; |
| 250 |
auto& lines_cbd = CRenderizableShaderWireFrame::m_color_buffer_data; |
| 251 |
std::unique_lock<std::shared_mutex> wfWriteLock( |
| 252 |
CRenderizableShaderWireFrame::m_wireframeMtx.data); |
| 253 |
|
| 254 |
lines_vbd.clear(); |
| 255 |
lines_cbd.clear(); |
| 256 |
|
| 257 |
auto& pts_vbd = CRenderizableShaderPoints::m_vertex_buffer_data; |
| 258 |
auto& pts_cbd = CRenderizableShaderPoints::m_color_buffer_data; |
| 259 |
std::unique_lock<std::shared_mutex> wfWriteLockPts(CRenderizableShaderPoints::m_pointsMtx.data); |
| 260 |
|
| 261 |
pts_vbd.clear(); |
| 262 |
pts_cbd.clear(); |
| 263 |
|
| 264 |
std::unique_lock<std::shared_mutex> trisWriteLock( |
| 265 |
CRenderizableShaderTriangles::m_trianglesMtx.data); |
| 266 |
auto& tris = CRenderizableShaderTriangles::m_triangles; |
| 267 |
|
| 268 |
tris.clear(); |
| 269 |
|
| 270 |
#if (MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL) && MRPT_HAS_ASSIMP |
| 271 |
if (!m_assimp_scene->scene) return; // No scene |
| 272 |
|
| 273 |
mrpt::opengl::internal::RenderElements re; |
| 274 |
re.lines_vbd = &lines_vbd; |
| 275 |
re.lines_cbd = &lines_cbd; |
| 276 |
re.pts_vbd = &pts_vbd; |
| 277 |
re.pts_cbd = &pts_cbd; |
| 278 |
re.tris = &tris; |
| 279 |
re.ipt = &m_textureIdMap; |
| 280 |
re.textObjs = &m_texturedObjects; |
| 281 |
|
| 282 |
ASSERT_(m_assimp_scene->scene); |
| 283 |
|
| 284 |
process_textures(m_assimp_scene->scene); |
| 285 |
|
| 286 |
// Model -> 3D primitives: |
| 287 |
const auto transf = mrpt::poses::CPose3D(); |
| 288 |
recursive_render(m_assimp_scene->scene, m_assimp_scene->scene->mRootNode, transf, re); |
| 289 |
|
| 290 |
// Handle split: |
| 291 |
if (m_split_triangles_rendering_bbox > .0f) |
| 292 |
{ |
| 293 |
const float voxel = m_split_triangles_rendering_bbox; |
| 294 |
|
| 295 |
ASSERT_EQUAL_(m_texturedObjects.size(), m_textureIdMap.size()); |
| 296 |
const std::vector<CSetOfTexturedTriangles::Ptr> origTexturedObjects = m_texturedObjects; |
| 297 |
m_texturedObjects.clear(); |
| 298 |
|
| 299 |
std::unordered_map<int64_t, std::map<size_t /*textId*/, CSetOfTexturedTriangles::Ptr>> |
| 300 |
newTextObjs; |
| 301 |
|
| 302 |
for (size_t textId = 0; textId < m_textureIdMap.size(); textId++) |
| 303 |
{ |
| 304 |
const auto& glTris = origTexturedObjects.at(textId); |
| 305 |
const std::vector<mrpt::opengl::TTriangle>& iTris = glTris->shaderTexturedTrianglesBuffer(); |
| 306 |
for (const auto& t : iTris) |
| 307 |
{ |
| 308 |
const auto midPt = 0.333f * (t.vertices[0].xyzrgba.pt + t.vertices[1].xyzrgba.pt + |
| 309 |
t.vertices[2].xyzrgba.pt); |
| 310 |
// hash for "voxels": |
| 311 |
const int64_t vxlCoord = mrpt::round(midPt.x / voxel) + // |
| 312 |
mrpt::round(midPt.y / voxel) * 10'000 + // |
| 313 |
mrpt::round(midPt.z / voxel) * 100'000'000; |
| 314 |
|
| 315 |
auto& trgObj = newTextObjs[vxlCoord][textId]; |
| 316 |
if (!trgObj) |
| 317 |
{ |
| 318 |
// Create |
| 319 |
trgObj = CSetOfTexturedTriangles::Create(); |
| 320 |
|
| 321 |
// Set representative point: the MOST FUNDAMENTAL property |
| 322 |
// to ensure proper z ordering for transparent rendering: |
| 323 |
trgObj->setLocalRepresentativePoint(midPt); |
| 324 |
|
| 325 |
// copy texture |
| 326 |
const auto& texRGB = glTris->getTextureImage(); |
| 327 |
const auto& texAlpha = glTris->getTextureAlphaImage(); |
| 328 |
if (!texAlpha.isEmpty()) |
| 329 |
trgObj->assignImage(texRGB, texAlpha); |
| 330 |
else |
| 331 |
trgObj->assignImage(texRGB); |
| 332 |
} |
| 333 |
// Add triangle: |
| 334 |
trgObj->insertTriangle(t); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
// replace list of objects: |
| 339 |
m_texturedObjects.clear(); |
| 340 |
for (const auto& m : newTextObjs) |
| 341 |
for (const auto& [k, v] : m.second) // |
| 342 |
m_texturedObjects.push_back(v); |
| 343 |
} // end: split into subobjects: |
| 344 |
|
| 345 |
#endif |
| 346 |
} |
| 347 |
|
| 348 |
// These ones: already done in onUpdateBuffers_all() |
| 349 |
void CAssimpModel::onUpdateBuffers_Wireframe() {} |
| 350 |
void CAssimpModel::onUpdateBuffers_Points() {} |
| 351 |
void CAssimpModel::onUpdateBuffers_Triangles() {} |
| 352 |
|
| 353 |
void CAssimpModel::enqueueForRenderRecursive( |
| 354 |
const mrpt::opengl::TRenderMatrices& state, |
| 355 |
RenderQueue& rq, |
| 356 |
bool wholeInView, |
| 357 |
bool is1stShadowMapPass) const |
| 358 |
{ |
| 359 |
// Enque rendering all textured meshes: |
| 360 |
mrpt::opengl::CListOpenGLObjects lst; |
| 361 |
for (const auto& o : m_texturedObjects) |
| 362 |
lst.emplace_back(std::dynamic_pointer_cast<mrpt::opengl::CRenderizable>(o)); |
| 363 |
|
| 364 |
mrpt::opengl::enqueueForRendering(lst, state, rq, wholeInView, is1stShadowMapPass); |
| 365 |
} |
| 366 |
|
| 367 |
uint8_t CAssimpModel::serializeGetVersion() const { return 3; } |
| 368 |
void CAssimpModel::serializeTo(mrpt::serialization::CArchive& out) const |
| 369 |
{ |
| 370 |
writeToStreamRender(out); |
| 371 |
#if (MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL) && MRPT_HAS_ASSIMP |
| 372 |
const bool empty = m_assimp_scene->scene == nullptr; |
| 373 |
out << empty; |
| 374 |
out << m_modelPath; // v2 |
| 375 |
out << m_split_triangles_rendering_bbox; // v3 |
| 376 |
if (!empty) |
| 377 |
{ |
| 378 |
#if 0 |
| 379 |
for (size_t i = 0; i < m_assimp_scene->exporter.GetExportFormatCount(); |
| 380 |
i++) |
| 381 |
{ |
| 382 |
const aiExportFormatDesc* d = |
| 383 |
m_assimp_scene->exporter.GetExportFormatDescription(i); |
| 384 |
std::cout << "format #" << i << ": " << d->description << " *." |
| 385 |
<< d->fileExtension << " id: '" << d->id << "'" |
| 386 |
<< std::endl; |
| 387 |
} |
| 388 |
#endif |
| 389 |
// new in v2: save as blob: |
| 390 |
const char* formatId = "assbin"; |
| 391 |
const auto blob = const_cast<Assimp::Exporter&>(m_assimp_scene->exporter) |
| 392 |
.ExportToBlob(m_assimp_scene->scene, formatId); |
| 393 |
ASSERT_(blob); |
| 394 |
|
| 395 |
out.WriteAs<uint32_t>(blob->size); |
| 396 |
ASSERT_(blob->size); |
| 397 |
out.WriteBuffer(blob->data, blob->size); |
| 398 |
} |
| 399 |
#else |
| 400 |
THROW_EXCEPTION("MRPT compiled without OpenGL and/or Assimp"); |
| 401 |
#endif |
| 402 |
} |
| 403 |
|
| 404 |
void CAssimpModel::serializeFrom(mrpt::serialization::CArchive& in, uint8_t version) |
| 405 |
{ |
| 406 |
switch (version) |
| 407 |
{ |
| 408 |
case 0: |
| 409 |
case 1: |
| 410 |
case 2: |
| 411 |
{ |
| 412 |
readFromStreamRender(in); |
| 413 |
clear(); |
| 414 |
|
| 415 |
if (version >= 2) // saved as blob |
| 416 |
{ |
| 417 |
const bool empty = in.ReadAs<bool>(); |
| 418 |
in >> m_modelPath; |
| 419 |
if (version >= 3) in >> m_split_triangles_rendering_bbox; |
| 420 |
|
| 421 |
if (!empty) |
| 422 |
{ |
| 423 |
const auto blobSize = in.ReadAs<uint32_t>(); |
| 424 |
ASSERT_(blobSize); |
| 425 |
std::vector<uint8_t> buf(blobSize); |
| 426 |
in.ReadBuffer(buf.data(), buf.size()); |
| 427 |
#if (MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL) && MRPT_HAS_ASSIMP |
| 428 |
m_assimp_scene->scene = |
| 429 |
m_assimp_scene->importer.ReadFileFromMemory(buf.data(), buf.size(), 0 /*flags*/); |
| 430 |
|
| 431 |
if (!m_assimp_scene->scene) |
| 432 |
{ |
| 433 |
THROW_EXCEPTION_FMT( |
| 434 |
"Error importing assimp blob data originally from " |
| 435 |
"file '%s': %s", |
| 436 |
m_modelPath.c_str(), m_assimp_scene->importer.GetErrorString()); |
| 437 |
} |
| 438 |
#else |
| 439 |
THROW_EXCEPTION("MRPT compiled without OpenGL and/or Assimp"); |
| 440 |
#endif |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
break; |
| 445 |
default: |
| 446 |
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(version); |
| 447 |
}; |
| 448 |
CRenderizable::notifyChange(); |
| 449 |
after_load_model(); |
| 450 |
} |
| 451 |
|
| 452 |
CAssimpModel::CAssimpModel() : m_assimp_scene(mrpt::make_impl<CAssimpModel::Impl>()) {} |
| 453 |
|
| 454 |
CAssimpModel::~CAssimpModel() { clear(); } |
| 455 |
|
| 456 |
void CAssimpModel::clear() |
| 457 |
{ |
| 458 |
CRenderizable::notifyChange(); |
| 459 |
|
| 460 |
#if (MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL) && MRPT_HAS_ASSIMP |
| 461 |
m_assimp_scene->importer.FreeScene(); |
| 462 |
#endif |
| 463 |
m_modelPath.clear(); |
| 464 |
m_textureIdMap.clear(); |
| 465 |
m_texturedObjects.clear(); |
| 466 |
} |
| 467 |
|
| 468 |
void CAssimpModel::loadScene(const std::string& filepath, int flags) |
| 469 |
{ |
| 470 |
#if (MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL) && MRPT_HAS_ASSIMP |
| 471 |
clear(); |
| 472 |
CRenderizable::notifyChange(); |
| 473 |
|
| 474 |
// Assimp flags: |
| 475 |
const std::vector<std::pair<uint32_t, unsigned int>> flagMap = { |
| 476 |
{ LoadFlags::RealTimeFast, aiProcessPreset_TargetRealtime_Fast}, |
| 477 |
{ LoadFlags::RealTimeQuality, aiProcessPreset_TargetRealtime_Quality}, |
| 478 |
{LoadFlags::RealTimeMaxQuality, aiProcessPreset_TargetRealtime_MaxQuality}, |
| 479 |
{ LoadFlags::FlipUVs, aiProcess_FlipUVs} |
| 480 |
}; |
| 481 |
|
| 482 |
unsigned int pFlags = 0; |
| 483 |
for (const auto& p : flagMap) |
| 484 |
if (flags & p.first) pFlags |= p.second; |
| 485 |
|
| 486 |
// Own flags: |
| 487 |
m_verboseLoad = !!(flags & LoadFlags::Verbose); |
| 488 |
m_ignoreMaterialColor = !!(flags & LoadFlags::IgnoreMaterialColor); |
| 489 |
|
| 490 |
m_assimp_scene->scene = m_assimp_scene->importer.ReadFile(filepath.c_str(), pFlags); |
| 491 |
|
| 492 |
if (!m_assimp_scene->scene) |
| 493 |
{ |
| 494 |
THROW_EXCEPTION_FMT( |
| 495 |
"Error importing '%s': %s", filepath.c_str(), m_assimp_scene->importer.GetErrorString()); |
| 496 |
} |
| 497 |
m_modelPath = filepath; |
| 498 |
|
| 499 |
after_load_model(); |
| 500 |
|
| 501 |
#else |
| 502 |
THROW_EXCEPTION("MRPT compiled without OpenGL and/or Assimp"); |
| 503 |
#endif |
| 504 |
} |
| 505 |
|
| 506 |
void CAssimpModel::after_load_model() |
| 507 |
{ |
| 508 |
#if (MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL) && MRPT_HAS_ASSIMP |
| 509 |
// Evaluate overall bbox: |
| 510 |
{ |
| 511 |
aiVector3D scene_min, scene_max; |
| 512 |
ASSERT_(m_assimp_scene->scene); |
| 513 |
get_bounding_box(m_assimp_scene->scene, &scene_min, &scene_max); |
| 514 |
m_bbox_min.x = scene_min.x; |
| 515 |
m_bbox_min.y = scene_min.y; |
| 516 |
m_bbox_min.z = scene_min.z; |
| 517 |
m_bbox_max.x = scene_max.x; |
| 518 |
m_bbox_max.y = scene_max.y; |
| 519 |
m_bbox_max.z = scene_max.z; |
| 520 |
} |
| 521 |
|
| 522 |
// Process all elements at once: |
| 523 |
// This populates the structures that will be attached to opengl |
| 524 |
// buffers |
| 525 |
const_cast<CAssimpModel&>(*this).onUpdateBuffers_all(); |
| 526 |
|
| 527 |
onAfterLoadScene(); |
| 528 |
#endif |
| 529 |
} |
| 530 |
|
| 531 |
auto CAssimpModel::internalBoundingBoxLocal() const -> mrpt::math::TBoundingBoxf |
| 532 |
{ |
| 533 |
return mrpt::math::TBoundingBoxf::FromUnsortedPoints(m_bbox_min, m_bbox_max); |
| 534 |
} |
| 535 |
|
| 536 |
void CAssimpModel::split_triangles_rendering_bbox(const float bbox_size) |
| 537 |
{ |
| 538 |
m_split_triangles_rendering_bbox = bbox_size; |
| 539 |
|
| 540 |
#if (MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL) && MRPT_HAS_ASSIMP |
| 541 |
CRenderizable::notifyChange(); |
| 542 |
if (m_assimp_scene->scene) after_load_model(); |
| 543 |
#endif |
| 544 |
} |
| 545 |
|
| 546 |
bool CAssimpModel::traceRay( |
| 547 |
[[maybe_unused]] const mrpt::poses::CPose3D& o, [[maybe_unused]] double& dist) const |
| 548 |
{ |
| 549 |
// TODO |
| 550 |
return false; |
| 551 |
} |
| 552 |
|
| 553 |
#if (MRPT_HAS_OPENGL_GLUT || MRPT_HAS_EGL) && MRPT_HAS_ASSIMP |
| 554 |
|
| 555 |
namespace |
| 556 |
{ |
| 557 |
void get_bounding_box_for_node( |
| 558 |
const aiScene* scene, const aiNode* nd, aiVector3D* min, aiVector3D* max, aiMatrix4x4* trafo) |
| 559 |
{ |
| 560 |
aiMatrix4x4 prev; |
| 561 |
unsigned int n = 0, t; |
| 562 |
|
| 563 |
prev = *trafo; |
| 564 |
aiMultiplyMatrix4(trafo, &nd->mTransformation); |
| 565 |
|
| 566 |
for (; n < nd->mNumMeshes; ++n) |
| 567 |
{ |
| 568 |
const aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]]; |
| 569 |
for (t = 0; t < mesh->mNumVertices; ++t) |
| 570 |
{ |
| 571 |
aiVector3D tmp = mesh->mVertices[t]; |
| 572 |
aiTransformVecByMatrix4(&tmp, trafo); |
| 573 |
|
| 574 |
min->x = std::min(min->x, tmp.x); |
| 575 |
min->y = std::min(min->y, tmp.y); |
| 576 |
min->z = std::min(min->z, tmp.z); |
| 577 |
|
| 578 |
max->x = std::max(max->x, tmp.x); |
| 579 |
max->y = std::max(max->y, tmp.y); |
| 580 |
max->z = std::max(max->z, tmp.z); |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
for (n = 0; n < nd->mNumChildren; ++n) |
| 585 |
{ |
| 586 |
get_bounding_box_for_node(scene, nd->mChildren[n], min, max, trafo); |
| 587 |
} |
| 588 |
*trafo = prev; |
| 589 |
} |
| 590 |
|
| 591 |
void get_bounding_box(const aiScene* scene, aiVector3D* min, aiVector3D* max) |
| 592 |
{ |
| 593 |
aiMatrix4x4 trafo; |
| 594 |
aiIdentityMatrix4(&trafo); |
| 595 |
|
| 596 |
min->x = min->y = min->z = 1e10f; |
| 597 |
max->x = max->y = max->z = -1e10f; |
| 598 |
get_bounding_box_for_node(scene, scene->mRootNode, min, max, &trafo); |
| 599 |
} |
| 600 |
|
| 601 |
mrpt::img::TColor color4_to_TColor(const aiColor4D& c) |
| 602 |
{ |
| 603 |
return mrpt::img::TColorf(c.r, c.g, c.b, c.a).asTColor(); |
| 604 |
} |
| 605 |
|
| 606 |
mrpt::img::TColor apply_material( |
| 607 |
const aiMaterial* mtl, const mrpt::img::TColor& defaultColor, const bool ignoreMaterialColor) |
| 608 |
{ |
| 609 |
aiColor4D diffuse; |
| 610 |
if (!ignoreMaterialColor && |
| 611 |
AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse)) |
| 612 |
{ // |
| 613 |
return color4_to_TColor(diffuse); |
| 614 |
} |
| 615 |
else |
| 616 |
{ |
| 617 |
return defaultColor; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
mrpt::math::CMatrixDouble44 aiMatrix_to_mrpt(const aiMatrix4x4& m) |
| 622 |
{ |
| 623 |
mrpt::math::CMatrixDouble44 M; |
| 624 |
M(0, 0) = m.a1; |
| 625 |
M(0, 1) = m.a2; |
| 626 |
M(0, 2) = m.a3; |
| 627 |
M(0, 3) = m.a4; |
| 628 |
|
| 629 |
M(1, 0) = m.b1; |
| 630 |
M(1, 1) = m.b2; |
| 631 |
M(1, 2) = m.b3; |
| 632 |
M(1, 3) = m.b4; |
| 633 |
|
| 634 |
M(2, 0) = m.c1; |
| 635 |
M(2, 1) = m.c2; |
| 636 |
M(2, 2) = m.c3; |
| 637 |
M(2, 3) = m.c4; |
| 638 |
|
| 639 |
M(3, 0) = m.d1; |
| 640 |
M(3, 1) = m.d2; |
| 641 |
M(3, 2) = m.d3; |
| 642 |
M(3, 3) = m.d4; |
| 643 |
return M; |
| 644 |
} |
| 645 |
|
| 646 |
mrpt::math::TPoint3Df to_mrpt(const aiVector3D& v) { return {v.x, v.y, v.z}; } |
| 647 |
} // namespace |
| 648 |
|
| 649 |
void CAssimpModel::recursive_render( |
| 650 |
const aiScene* sc, |
| 651 |
const aiNode* nd, |
| 652 |
const mrpt::poses::CPose3D& transf, |
| 653 |
mrpt::opengl::internal::RenderElements& re) |
| 654 |
{ |
| 655 |
const aiMatrix4x4& m = nd->mTransformation; |
| 656 |
|
| 657 |
// update transform |
| 658 |
const auto nodeTransf = mrpt::poses::CPose3D(aiMatrix_to_mrpt(m)); |
| 659 |
const mrpt::poses::CPose3D curTf = transf + nodeTransf; |
| 660 |
|
| 661 |
// draw all meshes assigned to this node |
| 662 |
for (unsigned int n = 0; n < nd->mNumMeshes; ++n) |
| 663 |
{ |
| 664 |
const struct aiMesh* mesh = sc->mMeshes[nd->mMeshes[n]]; |
| 665 |
|
| 666 |
mrpt::img::TColor color = |
| 667 |
apply_material(sc->mMaterials[mesh->mMaterialIndex], getColor_u8(), m_ignoreMaterialColor); |
| 668 |
|
| 669 |
for (unsigned int t = 0; t < mesh->mNumFaces; ++t) |
| 670 |
{ |
| 671 |
const struct aiFace* face = &mesh->mFaces[t]; |
| 672 |
|
| 673 |
switch (face->mNumIndices) |
| 674 |
{ |
| 675 |
case 1: |
| 676 |
// GL_POINTS ================ |
| 677 |
for (unsigned int i = 0; i < face->mNumIndices; i++) |
| 678 |
{ |
| 679 |
// get group index for current index |
| 680 |
int vertexIndex = face->mIndices[i]; |
| 681 |
if (mesh->mColors[0] != nullptr && !m_ignoreMaterialColor) |
| 682 |
color = color4_to_TColor(mesh->mColors[0][vertexIndex]); |
| 683 |
|
| 684 |
re.pts_vbd->emplace_back(curTf.composePoint(to_mrpt(mesh->mVertices[vertexIndex]))); |
| 685 |
re.pts_cbd->emplace_back(color); |
| 686 |
} |
| 687 |
break; |
| 688 |
|
| 689 |
case 2: |
| 690 |
// GL_LINES ================ |
| 691 |
for (unsigned int i = 0; i < face->mNumIndices; i++) |
| 692 |
{ |
| 693 |
// get group index for current index |
| 694 |
int vertexIndex = face->mIndices[i]; |
| 695 |
if (mesh->mColors[0] != nullptr && !m_ignoreMaterialColor) |
| 696 |
color = color4_to_TColor(mesh->mColors[0][vertexIndex]); |
| 697 |
|
| 698 |
re.lines_vbd->emplace_back(curTf.composePoint(to_mrpt(mesh->mVertices[vertexIndex]))); |
| 699 |
re.lines_cbd->emplace_back(color); |
| 700 |
} |
| 701 |
break; |
| 702 |
|
| 703 |
case 3: |
| 704 |
{ |
| 705 |
// GL_TRIANGLES ================ |
| 706 |
const unsigned int nTri = face->mNumIndices / 3; |
| 707 |
ASSERT_EQUAL_(face->mNumIndices % 3, 0); |
| 708 |
|
| 709 |
size_t textureIdIndex = std::string::npos; |
| 710 |
if (mesh->HasTextureCoords(0)) |
| 711 |
{ |
| 712 |
ASSERT_LT_(mesh->mMaterialIndex, sc->mNumMaterials); |
| 713 |
const int texIndex = 0; |
| 714 |
aiString path; // filename |
| 715 |
if (AI_SUCCESS == sc->mMaterials[mesh->mMaterialIndex]->GetTexture( |
| 716 |
aiTextureType_DIFFUSE, texIndex, &path)) |
| 717 |
{ |
| 718 |
auto itIpt = re.ipt->find(path.data); |
| 719 |
ASSERTMSG_( |
| 720 |
itIpt != re.ipt->end(), mrpt::format( |
| 721 |
"Inconsistent texture data structure for " |
| 722 |
"texture with path: '%s'", |
| 723 |
path.data)); |
| 724 |
|
| 725 |
textureIdIndex = itIpt->second.id_idx; |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
for (unsigned int iTri = 0; iTri < nTri; iTri++) |
| 730 |
{ |
| 731 |
mrpt::opengl::TTriangle tri; |
| 732 |
for (unsigned int v = 0; v < 3; v++) |
| 733 |
{ |
| 734 |
unsigned int i = iTri * 3 + v; |
| 735 |
// get group index for current index |
| 736 |
int vertexIndex = face->mIndices[i]; |
| 737 |
if (mesh->mColors[0] != nullptr && !m_ignoreMaterialColor) |
| 738 |
color = color4_to_TColor(mesh->mColors[0][vertexIndex]); |
| 739 |
|
| 740 |
tri.r(v) = color.R; |
| 741 |
tri.g(v) = color.G; |
| 742 |
tri.b(v) = color.B; |
| 743 |
tri.a(v) = color.A; |
| 744 |
|
| 745 |
// texture_coordinates_set=0 |
| 746 |
if (mesh->HasTextureCoords(0)) |
| 747 |
{ |
| 748 |
tri.vertices[v].uv.x = mesh->mTextureCoords[0][vertexIndex].x; |
| 749 |
tri.vertices[v].uv.y = mesh->mTextureCoords[0][vertexIndex].y; |
| 750 |
} |
| 751 |
|
| 752 |
if (mesh->mNormals) |
| 753 |
tri.vertices[v].normal = curTf.rotateVector(to_mrpt(mesh->mNormals[vertexIndex])); |
| 754 |
|
| 755 |
auto pt = curTf.composePoint(to_mrpt(mesh->mVertices[vertexIndex])); |
| 756 |
tri.x(v) = pt.x; |
| 757 |
tri.y(v) = pt.y; |
| 758 |
tri.z(v) = pt.z; |
| 759 |
} |
| 760 |
|
| 761 |
if (textureIdIndex == std::string::npos) |
| 762 |
{ |
| 763 |
// Append to default non-textured mesh: |
| 764 |
re.tris->emplace_back(std::move(tri)); |
| 765 |
} |
| 766 |
else |
| 767 |
{ |
| 768 |
// Append to its corresponding textured object: |
| 769 |
re.textObjs->at(textureIdIndex)->insertTriangle(tri); |
| 770 |
} |
| 771 |
} |
| 772 |
} |
| 773 |
break; |
| 774 |
default: |
| 775 |
// GL_POLYGON ================ |
| 776 |
THROW_EXCEPTION("ASSIMP polygons not implemented yet."); |
| 777 |
break; |
| 778 |
} |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
// draw all children |
| 783 |
for (unsigned int n = 0; n < nd->mNumChildren; ++n) |
| 784 |
recursive_render(sc, nd->mChildren[n], curTf, re); |
| 785 |
} |
| 786 |
|
| 787 |
void CAssimpModel::process_textures(const aiScene* scene) |
| 788 |
{ |
| 789 |
using namespace std::string_literals; |
| 790 |
|
| 791 |
m_textureIdMap.clear(); |
| 792 |
m_texturedObjects.clear(); |
| 793 |
|
| 794 |
/* getTexture Filenames and no. of Textures */ |
| 795 |
for (unsigned int m = 0; m < scene->mNumMaterials; m++) |
| 796 |
{ |
| 797 |
for (int texIndex = 0;; texIndex++) |
| 798 |
{ |
| 799 |
bool anyFound = false; |
| 800 |
const std::vector<aiTextureType> texTypes = { |
| 801 |
aiTextureType_DIFFUSE, aiTextureType_AMBIENT, aiTextureType_SPECULAR}; |
| 802 |
|
| 803 |
for (const auto texType : texTypes) |
| 804 |
{ |
| 805 |
aiString path; // filename |
| 806 |
aiReturn texFound = scene->mMaterials[m]->GetTexture(texType, texIndex, &path); |
| 807 |
if (texFound != AI_SUCCESS) break; |
| 808 |
|
| 809 |
CAssimpModel::TInfoPerTexture& ipt = m_textureIdMap[path.data]; |
| 810 |
ipt.id_idx = std::string::npos; // pending |
| 811 |
anyFound = true; |
| 812 |
} |
| 813 |
if (!anyFound) break; |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
const auto basepath = mrpt::system::extractFileDirectory(m_modelPath); |
| 818 |
|
| 819 |
for (auto& kv : m_textureIdMap) |
| 820 |
{ |
| 821 |
// save image ID |
| 822 |
std::string filename = kv.first; // get filename |
| 823 |
CAssimpModel::TInfoPerTexture& ipt = kv.second; |
| 824 |
// save texture id for filename in map: |
| 825 |
ipt.id_idx = m_texturedObjects.size(); |
| 826 |
|
| 827 |
// Create "children" textured objects: |
| 828 |
auto& texturedObj = m_texturedObjects.emplace_back(CSetOfTexturedTriangles::Create()); |
| 829 |
|
| 830 |
const std::string fileloc = mrpt::system::pathJoin({basepath, filename}); |
| 831 |
|
| 832 |
// Query textureCache: |
| 833 |
auto& cache = internal::TexturesCache::Instance(); |
| 834 |
auto& tc = cache.get(fileloc, m_verboseLoad, scene); |
| 835 |
|
| 836 |
if (tc.load_ok) |
| 837 |
{ |
| 838 |
if (tc.img_alpha.has_value()) |
| 839 |
texturedObj->assignImage(tc.img_rgb, *tc.img_alpha); |
| 840 |
else |
| 841 |
texturedObj->assignImage(tc.img_rgb); |
| 842 |
} |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
#endif // MRPT_HAS_OPENGL_GLUT && MRPT_HAS_ASSIMP |