Flip CXMeshFileLoader
MODIFY
src/irr/asset/CXMeshFileLoader.cpp
src/irr/asset/CXMeshFileLoader.cpp
@@ -88,7 +88,7 @@ asset::SAssetBundle CXMeshFileLoader::loadAsset(io::IReadFile* _file, const asse
ctx.AnimatedMesh = new asset::CCPUSkinnedMesh();
asset::ICPUMesh* retVal = nullptr;
- if (load(ctx, _file))
+ if (load(ctx, _file, _params))
{
ctx.AnimatedMesh->finalize();
if (ctx.AnimatedMesh->isStatic())
@@ -225,12 +225,12 @@ core::matrix4x3 getGlobalMatrix_evil(asset::ICPUSkinnedMesh::SJoint* joint)
}
-bool CXMeshFileLoader::load(SContext& _ctx, io::IReadFile* file)
+bool CXMeshFileLoader::load(SContext& _ctx, io::IReadFile* file, const asset::IAssetLoader::SAssetLoadParams& _params)
{
if (!readFileIntoMemory(_ctx, file))
return false;
- if (!parseFile(_ctx))
+ if (!parseFile(_ctx, _params))
return false;
for (uint32_t n=0; n<_ctx.Meshes.size(); ++n)
@@ -690,9 +690,9 @@ bool CXMeshFileLoader::readFileIntoMemory(SContext& _ctx, io::IReadFile* file)
//! Parses the file
-bool CXMeshFileLoader::parseFile(SContext& _ctx)
+bool CXMeshFileLoader::parseFile(SContext& _ctx, const asset::IAssetLoader::SAssetLoadParams& _params)
{
- while(parseDataObject(_ctx))
+ while(parseDataObject(_ctx, _params))
{
// loop
}
@@ -702,7 +702,7 @@ bool CXMeshFileLoader::parseFile(SContext& _ctx)
//! Parses the next Data object in the file
-bool CXMeshFileLoader::parseDataObject(SContext& _ctx)
+bool CXMeshFileLoader::parseDataObject(SContext& _ctx, const asset::IAssetLoader::SAssetLoadParams& _params)
{
std::string objectName = getNextToken(_ctx);
@@ -719,7 +719,7 @@ bool CXMeshFileLoader::parseDataObject(SContext& _ctx)
else
if (objectName == "Frame")
{
- return parseDataObjectFrame( _ctx, 0 );
+ return parseDataObjectFrame( _ctx, 0, _params);
}
else
if (objectName == "Mesh")
@@ -732,7 +732,7 @@ bool CXMeshFileLoader::parseDataObject(SContext& _ctx)
//mesh->Buffer=AnimatedMesh->addMeshBuffer();
_ctx.Meshes.push_back(mesh);
- return parseDataObjectMesh(_ctx, *mesh);
+ return parseDataObjectMesh(_ctx, *mesh, _params);
}
else
if (objectName == "AnimationSet")
@@ -796,7 +796,7 @@ bool CXMeshFileLoader::parseDataObjectTemplate(SContext& _ctx)
}
-bool CXMeshFileLoader::parseDataObjectFrame(SContext& _ctx, asset::ICPUSkinnedMesh::SJoint *Parent)
+bool CXMeshFileLoader::parseDataObjectFrame(SContext& _ctx, asset::ICPUSkinnedMesh::SJoint *Parent, const asset::IAssetLoader::SAssetLoadParams& _params)
{
#ifdef _XREADER_DEBUG
os::Printer::log("CXFileReader: Reading frame", ELL_DEBUG);
@@ -878,7 +878,7 @@ bool CXMeshFileLoader::parseDataObjectFrame(SContext& _ctx, asset::ICPUSkinnedMe
if (objectName == "Frame")
{
- if (!parseDataObjectFrame(_ctx, joint))
+ if (!parseDataObjectFrame(_ctx, joint, _params))
return false;
}
else
@@ -905,7 +905,7 @@ bool CXMeshFileLoader::parseDataObjectFrame(SContext& _ctx, asset::ICPUSkinnedMe
_ctx.Meshes.push_back(mesh);
- if (!parseDataObjectMesh(_ctx, *mesh))
+ if (!parseDataObjectMesh(_ctx, *mesh, _params))
return false;
}
else
@@ -952,7 +952,7 @@ bool CXMeshFileLoader::parseDataObjectTransformationMatrix(SContext& _ctx, core:
}
-bool CXMeshFileLoader::parseDataObjectMesh(SContext& _ctx, SXMesh &mesh)
+bool CXMeshFileLoader::parseDataObjectMesh(SContext& _ctx, SXMesh &mesh, const asset::IAssetLoader::SAssetLoadParams& _params)
{
std::string name;
@@ -970,6 +970,14 @@ bool CXMeshFileLoader::parseDataObjectMesh(SContext& _ctx, SXMesh &mesh)
os::Printer::log("CXFileReader: Reading mesh", name, ELL_DEBUG);
#endif
+ auto performActionBasedOnOrientationSystem = [&](auto performOnRightHanded, auto performOnLeftHanded = [&](void) {})
+ {
+ if (_params.loaderFlags & E_LOADER_PARAMETER_FLAGS::ELPF_RIGHT_HANDED_MESHES)
+ performOnRightHanded();
+ else
+ performOnLeftHanded();
+ };
+
// read vertex count
const uint32_t nVertices = readInt(_ctx);
@@ -978,6 +986,7 @@ bool CXMeshFileLoader::parseDataObjectMesh(SContext& _ctx, SXMesh &mesh)
for (uint32_t n=0; n<nVertices; ++n)
{
readVector3(_ctx, mesh.Vertices[n].Pos);
+ performActionBasedOnOrientationSystem([&]() {mesh.Vertices[n].Pos.X = -mesh.Vertices[n].Pos.X; }, [&]() {});
}
if (!checkForTwoFollowingSemicolons(_ctx))
@@ -1018,19 +1027,48 @@ bool CXMeshFileLoader::parseDataObjectMesh(SContext& _ctx, SXMesh &mesh)
polygonfaces[f] = readInt(_ctx);
for (uint32_t jk=0; jk<triangles; ++jk)
+ {
+ performActionBasedOnOrientationSystem
+ (
+ [&]()
+ {
+ mesh.Indices[currentIndex++] = polygonfaces[jk + 2];
+ mesh.Indices[currentIndex++] = polygonfaces[jk + 1];
+ mesh.Indices[currentIndex++] = polygonfaces[0];
+ },
+ [&]()
{
mesh.Indices[currentIndex++] = polygonfaces[0];
mesh.Indices[currentIndex++] = polygonfaces[jk + 1];
mesh.Indices[currentIndex++] = polygonfaces[jk + 2];
}
+ );
+ }
// TODO: change face indices in material list
}
else
{
- mesh.Indices[currentIndex++] = readInt(_ctx);
- mesh.Indices[currentIndex++] = readInt(_ctx);
- mesh.Indices[currentIndex++] = readInt(_ctx);
+ std::array<uint32_t, 3> indicies;
+ for (auto& it : indicies)
+ it = readInt(_ctx);
+
+ performActionBasedOnOrientationSystem
+ (
+ [&]()
+ {
+ mesh.Indices[currentIndex++] = indicies[2];
+ mesh.Indices[currentIndex++] = indicies[1];
+ mesh.Indices[currentIndex++] = indicies[0];
+ },
+ [&]()
+ {
+ mesh.Indices[currentIndex++] = indicies[0];
+ mesh.Indices[currentIndex++] = indicies[1];
+ mesh.Indices[currentIndex++] = indicies[2];
+ }
+ );
+
mesh.IndexCountPerFace[k] = 3;
}
}
@@ -1065,7 +1103,7 @@ bool CXMeshFileLoader::parseDataObjectMesh(SContext& _ctx, SXMesh &mesh)
if (objectName == "MeshNormals")
{
- if (!parseDataObjectMeshNormals(_ctx, mesh))
+ if (!parseDataObjectMeshNormals(_ctx, mesh, performActionBasedOnOrientationSystem))
return false;
}
else
@@ -1437,7 +1475,7 @@ bool CXMeshFileLoader::parseDataObjectSkinMeshHeader(SContext& _ctx, SXMesh& mes
}
-bool CXMeshFileLoader::parseDataObjectMeshNormals(SContext& _ctx, SXMesh &mesh)
+bool CXMeshFileLoader::parseDataObjectMeshNormals(SContext& _ctx, SXMesh &mesh, std::function<void(std::function<void()>, std::function<void()>)> performActionBasedOnOrientationSystem)
{
#ifdef _XREADER_DEBUG
os::Printer::log("CXFileReader: reading mesh normals", ELL_DEBUG);
@@ -1489,11 +1527,23 @@ bool CXMeshFileLoader::parseDataObjectMeshNormals(SContext& _ctx, SXMesh &mesh)
if (indexcount == 3)
{
// default, only one triangle in this face
- for (uint32_t h=0; h<3; ++h)
+ std::array<uint32_t, 3> tmpNormals;
+ for(auto& it : tmpNormals)
+ it = readInt(_ctx);
+
+ performActionBasedOnOrientationSystem
+ (
+ [&]()
+ {
+ for(uint32_t it = 3; it > 0; --it)
+ mesh.Vertices[mesh.Indices[normalidx++]].Normal.set(normals[tmpNormals[it - 1]]);
+ },
+ [&]()
{
- const uint32_t normalnum = readInt(_ctx);
- mesh.Vertices[mesh.Indices[normalidx++]].Normal.set(normals[normalnum]);
+ for(auto& it : tmpNormals)
+ mesh.Vertices[mesh.Indices[normalidx++]].Normal.set(normals[it]);
}
+ );
}
else
{
@@ -1503,11 +1553,23 @@ bool CXMeshFileLoader::parseDataObjectMeshNormals(SContext& _ctx, SXMesh &mesh)
polygonfaces[h] = readInt(_ctx);
for (uint32_t jk=0; jk<triangles; ++jk)
+ {
+ performActionBasedOnOrientationSystem
+ (
+ [&]()
+ {
+ mesh.Vertices[mesh.Indices[normalidx++]].Normal.set(normals[polygonfaces[jk + 2]]);
+ mesh.Vertices[mesh.Indices[normalidx++]].Normal.set(normals[polygonfaces[jk + 1]]);
+ mesh.Vertices[mesh.Indices[normalidx++]].Normal.set(normals[polygonfaces[0]]);
+ },
+ [&]()
{
mesh.Vertices[mesh.Indices[normalidx++]].Normal.set(normals[polygonfaces[0]]);
mesh.Vertices[mesh.Indices[normalidx++]].Normal.set(normals[polygonfaces[jk + 1]]);
mesh.Vertices[mesh.Indices[normalidx++]].Normal.set(normals[polygonfaces[jk + 2]]);
}
+ );
+ }
}
}

MODIFY
src/irr/asset/CXMeshFileLoader.h
src/irr/asset/CXMeshFileLoader.h
@@ -163,27 +163,27 @@ private:
asset::IAssetLoader::IAssetLoaderOverride* loaderOverride;
};
- bool load(SContext& _ctx, io::IReadFile* file);
+ bool load(SContext& _ctx, io::IReadFile* file, const asset::IAssetLoader::SAssetLoadParams& _params);
bool readFileIntoMemory(SContext& _ctx, io::IReadFile* file);
- bool parseFile(SContext& _ctx);
+ bool parseFile(SContext& _ctx, const asset::IAssetLoader::SAssetLoadParams& _params);
- bool parseDataObject(SContext& _ctx);
+ bool parseDataObject(SContext& _ctx, const asset::IAssetLoader::SAssetLoadParams& _params);
bool parseDataObjectTemplate(SContext& _ctx);
- bool parseDataObjectFrame(SContext& _ctx, asset::ICPUSkinnedMesh::SJoint *parent);
+ bool parseDataObjectFrame(SContext& _ctx, asset::ICPUSkinnedMesh::SJoint *parent, const asset::IAssetLoader::SAssetLoadParams& _params);
bool parseDataObjectTransformationMatrix(SContext& _ctx, core::matrix4x3 &mat);
- bool parseDataObjectMesh(SContext& _ctx, SXMesh &mesh);
+ bool parseDataObjectMesh(SContext& _ctx, SXMesh &mesh, const asset::IAssetLoader::SAssetLoadParams& _params);
bool parseDataObjectSkinWeights(SContext& _ctx, SXMesh &mesh);
bool parseDataObjectSkinMeshHeader(SContext& _ctx, SXMesh &mesh);
- bool parseDataObjectMeshNormals(SContext& _ctx, SXMesh &mesh);
+ bool parseDataObjectMeshNormals(SContext& _ctx, SXMesh &mesh, std::function<void(std::function<void()>, std::function<void()>)> performActionBasedOnOrientationSystem);
bool parseDataObjectMeshTextureCoords(SContext& _ctx, SXMesh &mesh);

Flip CXMeshFileLoader