Create CSTLWriter flipping dependency
MODIFY
include/irr/asset/IAssetWriter.h
include/irr/asset/IAssetWriter.h
@@ -24,30 +24,32 @@ enum E_WRITER_FLAGS : uint32_t
EWF_BINARY = 1u<<2u
};
+class IAssetWriter : public virtual core::IReferenceCounted
+{
+public:
+
//! Parameter flags for a loader
/**
These are extra flags that have an impact on extraordinary tasks while writing to file.
E_LOADER_PARAMETER_FLAGS::ELPF_NONE is default and means that there is nothing to perform.
- E_LOADER_PARAMETER_FLAGS::ELPF_RIGHT_HANDED_MESHES specifies that a mesh will be flipped,
- if current flag responsible for orientation is signed as left handed. If it isn't,
- no flipping will be performed, because there will be no need for that.
+ E_LOADER_PARAMETER_FLAGS::EWPF_MESH_IS_RIGHT_HANDED specifies that a mesh will be flipped,
+ if current E_LOADER_PARAMETER_FLAGS::EWPF_WRITE_RIGHT_HANDED responsible for orientation is signed as left handed.
+ If it isn't, no flipping will be performed, because there will be no need for that.
*/
enum E_WRITER_PARAMETER_FLAGS : uint64_t
{
EWPF_NONE = 0, //!< default value, it doesn't do anything
- EWPF_RIGHT_HANDED_MESHES = 0x1, //!< specifies that a mesh will be flipped in such a way that it'll look correctly in right-handed camera system
+ EWPF_MESH_IS_RIGHT_HANDED = 0x1, //!< specifies the incoming orientation of loaded mesh we want to write
+ EWPF_WRITE_RIGHT_HANDED = 0x2 //!< specifies the output handness orientation. There will be no flipping applied if incoming orientation matches output orientation - in case where EWPF_MESH_IS_RIGHT_HANDED and EWPF_WRITE_RIGHT_HANDED are both attached
};
-class IAssetWriter : public virtual core::IReferenceCounted
-{
-public:
struct SAssetWriteParams
{
- SAssetWriteParams(IAsset* _asset, const E_WRITER_FLAGS& _flags = EWF_NONE, const float& _compressionLevel = 0.f, const size_t& _encryptionKeyLen = 0, const uint8_t* _encryptionKey = nullptr, const void* _userData = nullptr, const E_WRITER_PARAMETER_FLAGS& _writerFlags = EWPF_NONE, const irr::asset::IAssetLoader::E_LOADER_PARAMETER_FLAGS& _loaderFlagsUsedForWriting = irr::asset::IAssetLoader::E_LOADER_PARAMETER_FLAGS::ELPF_NONE) :
+ SAssetWriteParams(IAsset* _asset, const E_WRITER_FLAGS& _flags = EWF_NONE, const float& _compressionLevel = 0.f, const size_t& _encryptionKeyLen = 0, const uint8_t* _encryptionKey = nullptr, const void* _userData = nullptr, const E_WRITER_PARAMETER_FLAGS& _writerFlags = EWPF_NONE) :
rootAsset(_asset), flags(_flags), compressionLevel(_compressionLevel),
encryptionKeyLen(_encryptionKeyLen), encryptionKey(_encryptionKey),
- userData(_userData), writerFlags(_writerFlags), loaderFlagsUsedForWriting(_loaderFlagsUsedForWriting)
+ userData(_userData), writerFlags(_writerFlags)
{
}
@@ -58,7 +60,6 @@ public:
const uint8_t* encryptionKey;
const void* userData;
const E_WRITER_PARAMETER_FLAGS writerFlags;
- const irr::asset::IAssetLoader::E_LOADER_PARAMETER_FLAGS loaderFlagsUsedForWriting;
};
//! Struct for keeping the state of the current write operation for safe threading

MODIFY
src/irr/asset/CSTLMeshWriter.cpp
src/irr/asset/CSTLMeshWriter.cpp
@@ -55,15 +55,15 @@ bool CSTLMeshWriter::writeAsset(io::IWriteFile* _file, const SAssetWriteParams&
const asset::E_WRITER_FLAGS flags = _override->getAssetWritingFlags(ctx, mesh, 0u);
if (flags & asset::EWF_BINARY)
- return writeMeshBinary(file, mesh);
+ return writeMeshBinary(file, mesh, _params);
else
- return writeMeshASCII(file, mesh);
+ return writeMeshASCII(file, mesh, _params);
}
namespace
{
template <class I>
-inline void writeFacesBinary(asset::ICPUMeshBuffer* buffer, const bool& noIndices, io::IWriteFile* file, asset::E_VERTEX_ATTRIBUTE_ID _colorVaid)
+inline void writeFacesBinary(asset::ICPUMeshBuffer* buffer, const bool& noIndices, io::IWriteFile* file, asset::E_VERTEX_ATTRIBUTE_ID _colorVaid, const irr::asset::IAssetWriter::SAssetWriteParams& _params)
{
bool hasColor = buffer->getMeshDataAndFormat()->getMappedBuffer(_colorVaid);
const asset::E_FORMAT colorType = buffer->getMeshDataAndFormat()->getAttribFormat(_colorVaid);
@@ -112,6 +112,30 @@ inline void writeFacesBinary(asset::ICPUMeshBuffer* buffer, const bool& noIndice
}
}
+ auto flipVectors = [&]()
+ {
+ for(uint8_t i = 0; i < 3; ++i)
+ v[i].X = -v[i].X;
+ };
+
+ if (_params.writerFlags & IAssetWriter::EWPF_WRITE_RIGHT_HANDED)
+ {
+ if (_params.writerFlags & IAssetWriter::EWPF_MESH_IS_RIGHT_HANDED)
+ {
+ // do nothing
+ }
+ else
+ flipVectors();
+ }
+ else
+ {
+ if (!(_params.writerFlags & IAssetWriter::EWPF_MESH_IS_RIGHT_HANDED))
+ {
+ // do nothing
+ }
+ else
+ flipVectors();
+ }
const core::plane3dSIMDf plane(v[0], v[1], v[2]);
file->write(&plane, 12);
@@ -123,7 +147,7 @@ inline void writeFacesBinary(asset::ICPUMeshBuffer* buffer, const bool& noIndice
}
}
-bool CSTLMeshWriter::writeMeshBinary(io::IWriteFile* file, const asset::ICPUMesh* mesh)
+bool CSTLMeshWriter::writeMeshBinary(io::IWriteFile* file, const asset::ICPUMesh* mesh, const SAssetWriteParams& _params)
{
// write STL MESH header
const char headerTxt[] = "Irrlicht-baw Engine";
@@ -150,42 +174,6 @@ bool CSTLMeshWriter::writeMeshBinary(io::IWriteFile* file, const asset::ICPUMesh
for (uint32_t i=0; i<mesh->getMeshBufferCount(); ++i)
{
asset::ICPUMeshBuffer* buffer = mesh->getMeshBuffer(i);
-
- /*
- pseudo code that has to be applied in binary and ASCII writing
-
- if(WRITER_RIGHTHANDED)
- {
- if(LOADER_RIGHTANDED)
- {
- // do nothing
- }
- else
- {
- if(buffer == bufferPos || buffer == bufferNorm)
- for(auto& it : buffer)
- {
- buffer.something.X = -buffer.something.X;
- }
- }
- }
- else
- {
- if(LOADER_LEFTHANDED)
- {
- // do nothing
- }
- else
- {
- if(buffer == bufferPos || buffer == bufferNorm)
- for(auto& it : buffer)
- {
- buffer.something.X = -buffer.something.X;
- }
- }
- }
- */
-
if (buffer&&buffer->getMeshDataAndFormat())
{
asset::E_INDEX_TYPE type = buffer->getIndexType();
@@ -193,15 +181,15 @@ bool CSTLMeshWriter::writeMeshBinary(io::IWriteFile* file, const asset::ICPUMesh
type = asset::EIT_UNKNOWN;
if (type== asset::EIT_16BIT)
{
- writeFacesBinary<uint16_t>(buffer, false, file, asset::EVAI_ATTR1);
+ writeFacesBinary<uint16_t>(buffer, false, file, asset::EVAI_ATTR1, _params);
}
else if (type== asset::EIT_32BIT)
{
- writeFacesBinary<uint32_t>(buffer, false, file, asset::EVAI_ATTR1);
+ writeFacesBinary<uint32_t>(buffer, false, file, asset::EVAI_ATTR1, _params);
}
else
{
- writeFacesBinary<uint16_t>(buffer, true, file, asset::EVAI_ATTR1); //template param doesn't matter if there's no indices
+ writeFacesBinary<uint16_t>(buffer, true, file, asset::EVAI_ATTR1, _params); //template param doesn't matter if there's no indices
}
}
}
@@ -209,7 +197,7 @@ bool CSTLMeshWriter::writeMeshBinary(io::IWriteFile* file, const asset::ICPUMesh
}
-bool CSTLMeshWriter::writeMeshASCII(io::IWriteFile* file, const asset::ICPUMesh* mesh)
+bool CSTLMeshWriter::writeMeshASCII(io::IWriteFile* file, const asset::ICPUMesh* mesh, const SAssetWriteParams& _params)
{
// write STL MESH header
const char headerTxt[] = "Irrlicht-baw Engine ";
@@ -239,7 +227,8 @@ bool CSTLMeshWriter::writeMeshASCII(io::IWriteFile* file, const asset::ICPUMesh*
writeFaceText(file,
buffer->getPosition(((uint16_t*)buffer->getIndices())[j]),
buffer->getPosition(((uint16_t*)buffer->getIndices())[j+1]),
- buffer->getPosition(((uint16_t*)buffer->getIndices())[j+2])
+ buffer->getPosition(((uint16_t*)buffer->getIndices())[j+2]),
+ _params
);
}
}
@@ -251,7 +240,8 @@ bool CSTLMeshWriter::writeMeshASCII(io::IWriteFile* file, const asset::ICPUMesh*
writeFaceText(file,
buffer->getPosition(((uint32_t*)buffer->getIndices())[j]),
buffer->getPosition(((uint32_t*)buffer->getIndices())[j+1]),
- buffer->getPosition(((uint32_t*)buffer->getIndices())[j+2])
+ buffer->getPosition(((uint32_t*)buffer->getIndices())[j+2]),
+ _params
);
}
}
@@ -262,8 +252,9 @@ bool CSTLMeshWriter::writeMeshASCII(io::IWriteFile* file, const asset::ICPUMesh*
{
writeFaceText(file,
buffer->getPosition(j),
- buffer->getPosition(j+1),
- buffer->getPosition(j+2)
+ buffer->getPosition(j+1ul),
+ buffer->getPosition(j+2ul),
+ _params
);
}
}
@@ -290,21 +281,54 @@ void CSTLMeshWriter::getVectorAsStringLine(const core::vectorSIMDf& v, core::str
void CSTLMeshWriter::writeFaceText(io::IWriteFile* file,
const core::vectorSIMDf& v1,
const core::vectorSIMDf& v2,
- const core::vectorSIMDf& v3)
+ const core::vectorSIMDf& v3,
+ const SAssetWriteParams& _params)
{
+ auto normal = core::plane3dSIMDf(v1, v2, v3).getNormal();
+ core::vectorSIMDf vertex1 = v1;
+ core::vectorSIMDf vertex2 = v2;
+ core::vectorSIMDf vertex3 = v3;
core::stringc tmp;
+
+ auto flipVectors = [&]()
+ {
+ vertex1.X = -vertex1.X;
+ vertex2.X = -vertex2.X;
+ vertex3.X = -vertex3.X;
+ normal = core::plane3dSIMDf(vertex1, vertex2, vertex3).getNormal();
+ };
+
+ if(_params.writerFlags & IAssetWriter::EWPF_WRITE_RIGHT_HANDED)
+ {
+ if(_params.writerFlags & IAssetWriter::EWPF_MESH_IS_RIGHT_HANDED)
+ {
+ // do nothing
+ }
+ else
+ flipVectors();
+ }
+ else
+ {
+ if (!(_params.writerFlags & IAssetWriter::EWPF_MESH_IS_RIGHT_HANDED))
+ {
+ // do nothing
+ }
+ else
+ flipVectors();
+ }
+
file->write("facet normal ",13);
- getVectorAsStringLine(core::plane3dSIMDf(v1, v2, v3).getNormal(), tmp);
+ getVectorAsStringLine(normal, tmp);
file->write(tmp.c_str(),tmp.size());
file->write(" outer loop\n",13);
file->write(" vertex ",11);
- getVectorAsStringLine(v1, tmp);
+ getVectorAsStringLine(vertex1, tmp);
file->write(tmp.c_str(),tmp.size());
file->write(" vertex ",11);
- getVectorAsStringLine(v2, tmp);
+ getVectorAsStringLine(vertex2, tmp);
file->write(tmp.c_str(),tmp.size());
file->write(" vertex ",11);
- getVectorAsStringLine(v3, tmp);
+ getVectorAsStringLine(vertex3, tmp);
file->write(tmp.c_str(),tmp.size());
file->write(" endloop\n",10);
file->write("endfacet\n",9);

MODIFY
src/irr/asset/CSTLMeshWriter.h
src/irr/asset/CSTLMeshWriter.h
@@ -38,17 +38,17 @@ namespace asset
protected:
// write binary format
- bool writeMeshBinary(io::IWriteFile* file, const asset::ICPUMesh* mesh);
+ bool writeMeshBinary(io::IWriteFile* file, const asset::ICPUMesh* mesh, const SAssetWriteParams& _params);
// write text format
- bool writeMeshASCII(io::IWriteFile* file, const asset::ICPUMesh* mesh);
+ bool writeMeshASCII(io::IWriteFile* file, const asset::ICPUMesh* mesh, const SAssetWriteParams& _params);
// create vector output with line end into string
void getVectorAsStringLine(const core::vectorSIMDf& v, core::stringc& s) const;
// write face information to file
void writeFaceText(io::IWriteFile* file, const core::vectorSIMDf& v1,
- const core::vectorSIMDf& v2, const core::vectorSIMDf& v3);
+ const core::vectorSIMDf& v2, const core::vectorSIMDf& v3, const SAssetWriteParams& _params);
};
} // end namespace

Create CSTLWriter flipping dependency