Fix flags, make CPLYWriter work
MODIFY
src/irr/asset/CPLYMeshWriter.cpp
src/irr/asset/CPLYMeshWriter.cpp
@@ -208,9 +208,9 @@ bool CPLYMeshWriter::writeAsset(io::IWriteFile* _file, const SAssetWriteParams&
file->write(header.c_str(), header.size());
if (flags & asset::EWF_BINARY)
- writeBinary(file, mesh->getMeshBuffer(0), vtxCount, faceCount, idxT, indices, forceFaces, vaidToWrite);
+ writeBinary(file, mesh->getMeshBuffer(0), vtxCount, faceCount, idxT, indices, forceFaces, vaidToWrite, _params);
else
- writeText(file, mesh->getMeshBuffer(0), vtxCount, faceCount, idxT, indices, forceFaces, vaidToWrite);
+ writeText(file, mesh->getMeshBuffer(0), vtxCount, faceCount, idxT, indices, forceFaces, vaidToWrite, _params);
if (needToFreeIndices)
_IRR_ALIGNED_FREE(indices);
@@ -218,10 +218,12 @@ bool CPLYMeshWriter::writeAsset(io::IWriteFile* _file, const SAssetWriteParams&
return true;
}
-void CPLYMeshWriter::writeBinary(io::IWriteFile* _file, asset::ICPUMeshBuffer* _mbuf, size_t _vtxCount, size_t _fcCount, asset::E_INDEX_TYPE _idxType, void* const _indices, bool _forceFaces, const bool _vaidToWrite[4]) const
+void CPLYMeshWriter::writeBinary(io::IWriteFile* _file, asset::ICPUMeshBuffer* _mbuf, size_t _vtxCount, size_t _fcCount, asset::E_INDEX_TYPE _idxType, void* const _indices, bool _forceFaces, const bool _vaidToWrite[4], const SAssetWriteParams& _params) const
{
const size_t colCpa = asset::getFormatChannelCount(_mbuf->getMeshDataAndFormat()->getAttribFormat(asset::EVAI_ATTR1));
+ bool flipVectors = (!(_params.writerFlags & IAssetWriter::EWPF_MESH_IS_RIGHT_HANDED)) ? true : false;
+
asset::ICPUMeshBuffer* mbCopy = createCopyMBuffNormalizedReplacedWithTrueInt(_mbuf);
for (size_t i = 0u; i < _vtxCount; ++i)
{
@@ -229,7 +231,7 @@ void CPLYMeshWriter::writeBinary(io::IWriteFile* _file, asset::ICPUMeshBuffer* _
uint32_t ui[4];
if (_vaidToWrite[asset::EVAI_ATTR0])
{
- writeAttribBinary(_file, mbCopy, asset::EVAI_ATTR0, i, 3u);
+ writeAttribBinary(_file, mbCopy, asset::EVAI_ATTR0, i, 3u, flipVectors);
/*_mbuf->getAttribute(f, EVAI_ATTR0, i);
_file->write(f.pointer, 3*sizeof(float));*/
}
@@ -243,7 +245,7 @@ void CPLYMeshWriter::writeBinary(io::IWriteFile* _file, asset::ICPUMeshBuffer* _
}
if (_vaidToWrite[asset::EVAI_ATTR3])
{
- writeAttribBinary(_file, mbCopy, asset::EVAI_ATTR3, i, 3u);
+ writeAttribBinary(_file, mbCopy, asset::EVAI_ATTR3, i, 3u, flipVectors);
}
}
mbCopy->drop();
@@ -290,12 +292,17 @@ void CPLYMeshWriter::writeBinary(io::IWriteFile* _file, asset::ICPUMeshBuffer* _
_IRR_ALIGNED_FREE(indices);
}
-void CPLYMeshWriter::writeText(io::IWriteFile* _file, asset::ICPUMeshBuffer* _mbuf, size_t _vtxCount, size_t _fcCount, asset::E_INDEX_TYPE _idxType, void* const _indices, bool _forceFaces, const bool _vaidToWrite[4]) const
+void CPLYMeshWriter::writeText(io::IWriteFile* _file, asset::ICPUMeshBuffer* _mbuf, size_t _vtxCount, size_t _fcCount, asset::E_INDEX_TYPE _idxType, void* const _indices, bool _forceFaces, const bool _vaidToWrite[4], const SAssetWriteParams& _params) const
{
asset::ICPUMeshBuffer* mbCopy = createCopyMBuffNormalizedReplacedWithTrueInt(_mbuf);
- auto writefunc = [&_file,&mbCopy,this](asset::E_VERTEX_ATTRIBUTE_ID _vaid, size_t _ix, size_t _cpa)
+ auto writefunc = [&_file,&mbCopy, &_params, this](asset::E_VERTEX_ATTRIBUTE_ID _vaid, size_t _ix, size_t _cpa)
{
+ bool flipVerteciesAndNormals = false;
+ if (!(_params.writerFlags & IAssetWriter::EWPF_MESH_IS_RIGHT_HANDED))
+ if(_vaid == asset::E_VERTEX_ATTRIBUTE_ID::EVAI_ATTR0 || _vaid == asset::E_VERTEX_ATTRIBUTE_ID::EVAI_ATTR3)
+ flipVerteciesAndNormals = true;
+
uint32_t ui[4];
core::vectorSIMDf f;
const asset::E_FORMAT t = mbCopy->getMeshDataAndFormat()->getAttribFormat(_vaid);
@@ -303,18 +310,18 @@ void CPLYMeshWriter::writeText(io::IWriteFile* _file, asset::ICPUMeshBuffer* _mb
{
mbCopy->getAttribute(ui, _vaid, _ix);
if (!asset::isSignedFormat(t))
- writeVectorAsText(_file, ui, _cpa);
+ writeVectorAsText(_file, ui, _cpa, flipVerteciesAndNormals);
else
{
int32_t ii[4];
memcpy(ii, ui, 4*4);
- writeVectorAsText(_file, ii, _cpa);
+ writeVectorAsText(_file, ii, _cpa, flipVerteciesAndNormals);
}
}
else
{
mbCopy->getAttribute(f, _vaid, _ix);
- writeVectorAsText(_file, f.pointer, _cpa);
+ writeVectorAsText(_file, f.pointer, _cpa, flipVerteciesAndNormals);
}
};
@@ -388,14 +395,17 @@ void CPLYMeshWriter::writeText(io::IWriteFile* _file, asset::ICPUMeshBuffer* _mb
_IRR_ALIGNED_FREE(indices);
}
-void CPLYMeshWriter::writeAttribBinary(io::IWriteFile* _file, asset::ICPUMeshBuffer* _mbuf, asset::E_VERTEX_ATTRIBUTE_ID _vaid, size_t _ix, size_t _cpa) const
+void CPLYMeshWriter::writeAttribBinary(io::IWriteFile* _file, asset::ICPUMeshBuffer* _mbuf, asset::E_VERTEX_ATTRIBUTE_ID _vaid, size_t _ix, size_t _cpa, bool flipVectors) const
{
uint32_t ui[4];
core::vectorSIMDf f;
asset::E_FORMAT t = _mbuf->getMeshDataAndFormat()->getAttribFormat(_vaid);
if (asset::isScaledFormat(t) || asset::isIntegerFormat(t))
{
+ constexpr size_t xID = 0u;
_mbuf->getAttribute(ui, _vaid, _ix);
+ if (flipVectors)
+ ui[xID] = -ui[xID];
const uint32_t bytesPerCh = asset::getTexelOrBlockBytesize(t)/asset::getFormatChannelCount(t);
if (bytesPerCh == 1u || t == asset::EF_A2B10G10R10_UINT_PACK32 || t == asset::EF_A2B10G10R10_SINT_PACK32 || t == asset::EF_A2B10G10R10_SSCALED_PACK32 || t == asset::EF_A2B10G10R10_USCALED_PACK32)

MODIFY
src/irr/asset/CPLYMeshWriter.h
src/irr/asset/CPLYMeshWriter.h
@@ -36,10 +36,10 @@ namespace asset
virtual bool writeAsset(io::IWriteFile* _file, const SAssetWriteParams& _params, IAssetWriterOverride* _override = nullptr) override;
private:
- void writeBinary(io::IWriteFile* _file, asset::ICPUMeshBuffer* _mbuf, size_t _vtxCount, size_t _fcCount, asset::E_INDEX_TYPE _idxType, void* const _indices, bool _forceFaces, const bool _vaidToWrite[4]) const;
- void writeText(io::IWriteFile* _file, asset::ICPUMeshBuffer* _mbuf, size_t _vtxCount, size_t _fcCount, asset::E_INDEX_TYPE _idxType, void* const _indices, bool _forceFaces, const bool _vaidToWrite[4]) const;
+ void writeBinary(io::IWriteFile* _file, asset::ICPUMeshBuffer* _mbuf, size_t _vtxCount, size_t _fcCount, asset::E_INDEX_TYPE _idxType, void* const _indices, bool _forceFaces, const bool _vaidToWrite[4], const SAssetWriteParams& _params) const;
+ void writeText(io::IWriteFile* _file, asset::ICPUMeshBuffer* _mbuf, size_t _vtxCount, size_t _fcCount, asset::E_INDEX_TYPE _idxType, void* const _indices, bool _forceFaces, const bool _vaidToWrite[4], const SAssetWriteParams& _params) const;
- void writeAttribBinary(io::IWriteFile* _file, asset::ICPUMeshBuffer* _mbuf, asset::E_VERTEX_ATTRIBUTE_ID _vaid, size_t _ix, size_t _cpa) const;
+ void writeAttribBinary(io::IWriteFile* _file, asset::ICPUMeshBuffer* _mbuf, asset::E_VERTEX_ATTRIBUTE_ID _vaid, size_t _ix, size_t _cpa, bool flipVectors = false) const;
//! Creates new mesh buffer with the same attribute buffers mapped but with normalized types changed to corresponding true integer types.
static asset::ICPUMeshBuffer* createCopyMBuffNormalizedReplacedWithTrueInt(const asset::ICPUMeshBuffer* _mbuf);
@@ -47,12 +47,21 @@ namespace asset
static std::string getTypeString(asset::E_FORMAT _t);
template<typename T>
- void writeVectorAsText(io::IWriteFile* _file, const T* _vec, size_t _elementsToWrite) const
+ void writeVectorAsText(io::IWriteFile* _file, const T* _vec, size_t _elementsToWrite, bool flipVectors = false) const
{
+ constexpr size_t xID = 0u;
std::stringstream ss;
ss << std::fixed;
+ bool currentFlipOnVariable = false;
for (size_t i = 0u; i < _elementsToWrite; ++i)
- ss << std::setprecision(6) << _vec[i] << " ";
+ {
+ if (flipVectors && i == xID)
+ currentFlipOnVariable = true;
+ else
+ currentFlipOnVariable = false;
+
+ ss << std::setprecision(6) << _vec[i] * (currentFlipOnVariable ? -1 : 1) << " ";
+ }
auto str = ss.str();
_file->write(str.c_str(), str.size());
}

MODIFY
src/irr/asset/CSTLMeshWriter.cpp
src/irr/asset/CSTLMeshWriter.cpp
@@ -125,24 +125,8 @@ inline void writeFacesBinary(asset::ICPUMeshBuffer* buffer, const bool& noIndice
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(&normal, 12);
file->write(&vertex1, 12);
@@ -293,7 +277,7 @@ void CSTLMeshWriter::writeFaceText(io::IWriteFile* file,
core::vectorSIMDf vertex1 = v3;
core::vectorSIMDf vertex2 = v2;
core::vectorSIMDf vertex3 = v1;
- core::vector3df_SIMD normal = core::plane3dSIMDf(vertex1, vertex2, vertex3).getNormal();
+ core::vectorSIMDf normal = core::plane3dSIMDf(vertex1, vertex2, vertex3).getNormal();
core::stringc tmp;
auto flipVectors = [&]()
@@ -304,24 +288,8 @@ void CSTLMeshWriter::writeFaceText(io::IWriteFile* file,
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(normal, tmp);

Fix flags, make CPLYWriter work