Skip to content

Commit 63d3613

Browse files
committed
Fix LimonModel support
It was caused by changes to the model structure that were not reflected. The changes are not backwards compatible.
1 parent 11477be commit 63d3613

File tree

5 files changed

+94
-25
lines changed

5 files changed

+94
-25
lines changed

src/Assets/AssetManager.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ class AssetManager {
230230

231231
explicit AssetManager(GraphicsInterface* graphicsWrapper, ALHelper *alHelper) : graphicsWrapper(graphicsWrapper), alHelper(alHelper) {
232232
nextMaterialIndex.store(1);
233+
nextAssetIndex.store(0);
233234
loadAssetList();
234235
size_t num_threads = std::thread::hardware_concurrency();
235236
for (size_t i = 0; i < num_threads; ++i) {
@@ -245,7 +246,7 @@ class AssetManager {
245246
template<class T>
246247
std::vector<std::shared_ptr<T>>parallelLoadAssetList(const std::vector<std::vector<std::string>> filesList) {
247248
std::vector<std::shared_ptr<T>> loadedAssets;
248-
std::unordered_set<int> startedAssetIds;
249+
std::unordered_set<uint32_t> startedAssetIds;
249250
for(const auto &files : filesList) {
250251
if (assets.count(files) == 0) {
251252
uint32_t nextAssetIndexLocal = getNextAssetIndex();
@@ -258,6 +259,10 @@ class AssetManager {
258259
std::ifstream is(files[0], std::ios::binary);
259260
cereal::BinaryInputArchive archive(is);
260261
assets[files] = std::make_pair(std::make_shared<T>(this, nextAssetIndexLocal, files, archive), 0);
262+
assets[files].first->setLoadState(Asset::LoadState::CPU_LOAD_DONE);
263+
264+
startedAssetIds.insert(nextAssetIndexLocal);
265+
assetLoadGPUQueue.pushBack(assets[files].first, true);
261266
#else
262267
std::cerr << "Limon compiled without limonmodel support. Please acquire a release version. Exiting..." << std::endl;
263268
std::cerr << "Compile should define \"CEREAL_SUPPORT\"." << std::endl;
@@ -315,6 +320,10 @@ class AssetManager {
315320
std::ifstream is(files[0], std::ios::binary);
316321
cereal::BinaryInputArchive archive(is);
317322
assets[files] = std::make_pair(std::make_shared<T>(this, nextAssetIndexLocal, files, archive), 0);
323+
assets[files].first->setLoadState(Asset::LoadState::CPU_LOAD_DONE);
324+
325+
assetLoadGPUQueue.pushBack(assets[files].first, true);
326+
318327
#else
319328
std::cerr << "Limon compiled without limonmodel support. Please acquire a release version. Exiting..." << std::endl;
320329
std::cerr << "Compile should define \"CEREAL_SUPPORT\"." << std::endl;
@@ -360,6 +369,8 @@ class AssetManager {
360369
std::ifstream is(files[0], std::ios::binary);
361370
cereal::BinaryInputArchive archive(is);
362371
assets[files] = std::make_pair(std::make_shared<T>(this, getNextAssetIndex(), files, archive), 0);
372+
assets[files].first->loadGPUPart();
373+
assets[files].first->setLoadState(Asset::LoadState::DONE);
363374
#else
364375
std::cerr << "Limon compiled without limonmodel support. Please acquire a release version. Exiting..." << std::endl;
365376
std::cerr << "Compile should define \"CEREAL_SUPPORT\"." << std::endl;

src/Assets/MeshAsset.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class MeshAsset {
3232

3333
std::vector<glm::vec3> vertices;
3434
std::vector<glm::vec3> normals;
35-
std::vector<glm::mediump_uvec3> faces;
35+
std::vector<glm::mediump_uvec3> faces; //Possible reason for non portable data
3636
std::vector<glm::vec2> textureCoordinates;
3737
std::string name;
3838

@@ -60,15 +60,14 @@ class MeshAsset {
6060
bool setTriangles(const aiMesh *currentMesh);
6161

6262
void normalizeTextureCoordinates(glm::vec2 &textureCoordinates) const;
63-
void buildBulletMesh();
6463
#ifdef CEREAL_SUPPORT
6564
friend class cereal::access;
6665
#endif
6766
MeshAsset(){}
6867
public:
6968
MeshAsset(const aiMesh *currentMesh, std::string name, std::shared_ptr<const BoneNode> meshSkeleton,
7069
const glm::mat4 &parentTransform, const bool isPartOfAnimated);
71-
70+
void buildBulletMesh();
7271
/**
7372
* This method sets GPU side of the deserialization, and uses AssetManager to access GPU with getGraphicsWrapper
7473
*
@@ -123,7 +122,7 @@ class MeshAsset {
123122
#ifdef CEREAL_SUPPORT
124123
template<class Archive>
125124
void serialize(Archive & archive){
126-
archive( vertices, normals, textureCoordinates, faces, vertexCount, triangleCount, skeleton, bones, boneIDs, boneWeights, boneAttachedMeshes, boneIdMap, name, isPartOfAnimated, parentTransform);
125+
archive( vertices, normals, textureCoordinates, faces, vertexCount, triangleCount, offsets, skeleton, bones, boneIDs, boneWeights, boneAttachedMeshes, boneIdMap, name, isPartOfAnimated, parentTransform);
127126
}
128127
#endif
129128
};

src/Assets/ModelAsset.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,36 @@ class ModelAsset : public Asset {
225225
index++;
226226
embeddedTexture = assetManager->getEmbeddedTextures(name, index);
227227
}
228-
ar(assetID, boneIDCounter, boneIDCounterPerMesh, textures, hasAnimation, rootNode, boundingBoxMax, boundingBoxMin, centerOffset, boneInformationMap, simplifiedMeshes, meshes, animations, animationSections, customizationAfterSave, materialMap, transparentMaterialUsed);
228+
ar(name, boneIDCounter, boneIDCounterPerMesh, textures, hasAnimation, rootNode, boundingBoxMax, boundingBoxMin, centerOffset, boneInformationMap, simplifiedMeshes, meshes, animations, animationSections, customizationAfterSave, materialMap, meshMaterialMap, transparentMaterialUsed);
229229
}
230230

231231
template<class Archive>
232232
void load( Archive & ar ) {
233+
std::map<std::shared_ptr<MeshAsset>,std::shared_ptr<Material>> tempMeshMaterialMap;
233234
temporaryEmbeddedTextures = std::make_unique<std::vector<std::shared_ptr<const AssetManager::EmbeddedTexture>>>();
234-
ar(assetID, boneIDCounter, boneIDCounterPerMesh, *temporaryEmbeddedTextures, hasAnimation, rootNode, boundingBoxMax, boundingBoxMin, centerOffset, boneInformationMap, simplifiedMeshes, meshes, animations, animationSections, customizationAfterSave, materialMap, transparentMaterialUsed);
235+
ar(name,boneIDCounter, boneIDCounterPerMesh, *temporaryEmbeddedTextures, hasAnimation, rootNode, boundingBoxMax, boundingBoxMin, centerOffset, boneInformationMap, simplifiedMeshes, meshes, animations, animationSections, customizationAfterSave, materialMap, tempMeshMaterialMap, transparentMaterialUsed);
235236
//now update embedded textures to assetManager
237+
for (size_t i = 0; i < meshes.size(); ++i) {
238+
meshes[i]->buildBulletMesh();
239+
}
240+
241+
for (const auto& tempMeshMaterialPair:tempMeshMaterialMap) {
242+
meshMaterialMap[tempMeshMaterialPair.first] = tempMeshMaterialPair.second;
243+
}
244+
for (auto& materialPair:materialMap) {
245+
std::map<const std::shared_ptr<const MeshAsset>,std::shared_ptr<Material>>::iterator meshMaterialToUpdateIt;
246+
for (std::map<const std::shared_ptr<const MeshAsset>,std::shared_ptr<Material>>::iterator it = meshMaterialMap.begin(); it != meshMaterialMap.end(); ++it) {
247+
if(it->second == materialPair.second) {
248+
meshMaterialToUpdateIt = it;
249+
break;
250+
}
251+
}
252+
materialPair.second = assetManager->registerMaterial(materialPair.second);
253+
if (meshMaterialToUpdateIt != meshMaterialMap.end()) {
254+
meshMaterialMap[meshMaterialToUpdateIt->first] = materialPair.second;
255+
}
256+
materialPair.second->afterLoad(assetManager);
257+
}
236258
buildPhysicsMeshes();
237259
}
238260
#endif

src/Assets/TextureAsset.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class TextureAsset : public Asset {
4141
assert(false && "TextureAsset doesn't support Cereal Loading");
4242
}
4343
#endif
44-
~TextureAsset();
44+
~TextureAsset() override;
4545

4646
uint32_t getID() const {
4747
return texture->getTextureID();

src/Material.h

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class Material {
4949
*
5050
* vector has vector<string>, because embedded textures are saved with pairs, first element is the index, second element is the model file itself.
5151
*/
52+
53+
//FIXME
54+
//std::vector<std::vector<std::string>>* textureNames = nullptr;
55+
std::vector<std::string> ambientTextureNames, diffuseTextureNames, specularTextureNames, normalTextureNames, opacityTextureNames;
56+
5257
friend struct std::hash<Material>;
5358
std::shared_ptr<TextureAsset> ambientTexture = nullptr;
5459
std::shared_ptr<TextureAsset> diffuseTexture = nullptr;
@@ -334,25 +339,57 @@ class Material {
334339

335340
template<class Archive>
336341
void load(Archive & archive) {
337-
std::cerr << "LimonModel Material load needs reimplementing" << std::endl;
338-
getchar();
339-
/*
340-
* Old implementation below
341-
*
342-
textureNameListList = std::make_unique<std::vector<std::vector<std::string>>>();
343-
for (int i = 0; i < 5; ++i) {
344-
textureNameListList->push_back(std::vector<std::string>());
345-
}
346342

347-
std::vector<std::string> &ambientTextureNames = textureNameListList->at(0);
348-
std::vector<std::string> &diffuseTextureNames = textureNameListList->at(1);
349-
std::vector<std::string> &specularTextureNames = textureNameListList->at(2);
350-
std::vector<std::string> &normalTextureNames = textureNameListList->at(3);
351-
std::vector<std::string> &opacityTextureNames = textureNameListList->at(4);
352-
*/
353343
archive(name, specularExponent, maps, ambientColor, diffuseColor, specularColor, isAmbientMap, isDiffuseMap, isSpecularMap, isNormalMap, isOpacityMap, refractionIndex
354-
//,ambientTextureNames, diffuseTextureNames, specularTextureNames, normalTextureNames, opacityTextureNames);
355-
);
344+
,ambientTextureNames, diffuseTextureNames, specularTextureNames, normalTextureNames, opacityTextureNames);
345+
}
346+
347+
void afterLoad(AssetManager* assetManager) {
348+
this->assetManager = assetManager;
349+
if(!ambientTextureNames.empty()) {
350+
if (ambientTextureNames.size() > 1) {
351+
this->setAmbientTexture(ambientTextureNames[0], &ambientTextureNames[1]);
352+
} else {
353+
this->setAmbientTexture(ambientTextureNames[0]);
354+
}
355+
ambientTextureNames.clear();
356+
}
357+
358+
if(!diffuseTextureNames.empty()) {
359+
if (diffuseTextureNames.size() > 1) {
360+
this->setDiffuseTexture(diffuseTextureNames[0], &diffuseTextureNames[1]);
361+
} else {
362+
this->setDiffuseTexture(diffuseTextureNames[0]);
363+
}
364+
diffuseTextureNames.clear();
365+
}
366+
367+
if(!specularTextureNames.empty()) {
368+
if (specularTextureNames.size() > 1) {
369+
this->setSpecularTexture(specularTextureNames[0], &specularTextureNames[1]);
370+
} else {
371+
this->setSpecularTexture(specularTextureNames[0]);
372+
}
373+
specularTextureNames.clear();
374+
}
375+
376+
if(!normalTextureNames.empty()) {
377+
if (normalTextureNames.size() > 1) {
378+
this->setNormalTexture(normalTextureNames[0], &normalTextureNames[1]);
379+
} else {
380+
this->setNormalTexture(normalTextureNames[0]);
381+
}
382+
normalTextureNames.clear();
383+
}
384+
385+
if(!opacityTextureNames.empty()) {
386+
if (opacityTextureNames.size() > 1) {
387+
this->setOpacityTexture(opacityTextureNames[0], &opacityTextureNames[1]);
388+
} else {
389+
this->setOpacityTexture(opacityTextureNames[0]);
390+
}
391+
opacityTextureNames.clear();
392+
}
356393
}
357394
#endif
358395
};

0 commit comments

Comments
 (0)