@@ -82,7 +82,7 @@ class ResourceIOStream : public Assimp::IOStream
82
82
memcpy (buffer, pos_, to_read);
83
83
pos_ += to_read;
84
84
85
- return to_read;
85
+ return to_read / size ;
86
86
}
87
87
88
88
size_t Write (const void * buffer, size_t size, size_t count) override
@@ -262,6 +262,7 @@ std::vector<Ogre::MaterialPtr> AssimpLoader::loadMaterials(
262
262
Ogre::ColourValue (0 , 0 , 0 , 1.0 ));
263
263
264
264
265
+ SetScene (scene);
265
266
setLightColorsFromAssimp (resource_path, mat, ai_material, material_internals);
266
267
267
268
setBlending (mat, ai_material, material_internals);
@@ -274,6 +275,11 @@ std::vector<Ogre::MaterialPtr> AssimpLoader::loadMaterials(
274
275
return material_table_out;
275
276
}
276
277
278
+ void AssimpLoader::SetScene (const aiScene * ai_scene)
279
+ {
280
+ this ->ai_scene_ = ai_scene;
281
+ }
282
+
277
283
void AssimpLoader::setLightColorsFromAssimp (
278
284
const std::string & resource_path,
279
285
Ogre::MaterialPtr & mat,
@@ -290,11 +296,24 @@ void AssimpLoader::setLightColorsFromAssimp(
290
296
uint32_t uv_index;
291
297
ai_material->GetTexture (aiTextureType_DIFFUSE, 0 , &texture_name, &mapping, &uv_index);
292
298
299
+ std::string texture_path;
300
+ #ifdef ASSIMP_VERSION_5
301
+ const aiTexture * texture = this ->ai_scene_ ->GetEmbeddedTexture (texture_name.C_Str ());
302
+ if (texture == nullptr ) {
303
+ #endif
304
+ // It's not an embedded texture. We have to go find it.
293
305
// Assume textures are in paths relative to the mesh
294
306
QFileInfo resource_path_finfo (QString::fromStdString (resource_path));
295
307
QDir resource_path_qdir = resource_path_finfo.dir ();
296
- std::string texture_path = resource_path_qdir.path ().toStdString () + " /" + texture_name.data ;
308
+ texture_path = resource_path_qdir.path ().toStdString () + " /" + texture_name.data ;
297
309
loadTexture (texture_path);
310
+ #ifdef ASSIMP_VERSION_5
311
+ } else {
312
+ // it's an embedded texture, like in GLB / glTF
313
+ texture_path = resource_path + texture_name.data ;
314
+ loadEmbeddedTexture (texture, texture_path);
315
+ }
316
+ #endif
298
317
Ogre::TextureUnitState * tu = material_internals.pass_ ->createTextureUnitState ();
299
318
tu->setTextureName (texture_path);
300
319
} else if (propKey == " $clr.diffuse" ) {
@@ -340,6 +359,35 @@ void AssimpLoader::setLightColorsFromAssimp(
340
359
}
341
360
}
342
361
362
+ void AssimpLoader::loadEmbeddedTexture (
363
+ const aiTexture * texture, const std::string & resource_path)
364
+ {
365
+ if (texture == nullptr ) {
366
+ RVIZ_RENDERING_LOG_ERROR_STREAM (" null texture!" );
367
+ return ;
368
+ }
369
+
370
+ // use the format hint to try to load the image
371
+ std::string format_hint (
372
+ texture->achFormatHint ,
373
+ strnlen (texture->achFormatHint , sizeof (texture->achFormatHint )));
374
+
375
+ Ogre::DataStreamPtr stream (
376
+ new Ogre::MemoryDataStream (
377
+ (unsigned char *)texture->pcData , texture->mWidth ));
378
+
379
+ try {
380
+ Ogre::Image image;
381
+ image.load (stream, format_hint.c_str ());
382
+ Ogre::TextureManager::getSingleton ().loadImage (
383
+ resource_path, ROS_PACKAGE_NAME, image);
384
+ } catch (Ogre::Exception & e) {
385
+ RVIZ_RENDERING_LOG_ERROR_STREAM (
386
+ " Could not load texture [" << resource_path.c_str () <<
387
+ " ] with format hint [" << format_hint << " ]: " << e.what ());
388
+ }
389
+ }
390
+
343
391
void AssimpLoader::loadTexture (const std::string & resource_path)
344
392
{
345
393
if (!Ogre::TextureManager::getSingleton ().resourceExists (resource_path, ROS_PACKAGE_NAME)) {
0 commit comments