Skip to content

Commit 855b55e

Browse files
fixed my bug in homogenize function (didn't set a new index count after changing primitive topology and index buffer)
@Anastazluk found a bug in your writer code
1 parent c2ec92a commit 855b55e

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

include/nbl/asset/IMeshManipulator.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ namespace asset
223223
@param _inIndexType Type of input index buffer data (32bit or 16bit).
224224
@param _outIndexType Type of output index buffer data (32bit or 16bit).
225225
*/
226-
static core::smart_refctd_ptr<ICPUBuffer> idxBufferFromLineStripsToLines(const void* _input, size_t _idxCount, E_INDEX_TYPE _inIndexType, E_INDEX_TYPE _outIndexType);
226+
static core::smart_refctd_ptr<ICPUBuffer> idxBufferFromLineStripsToLines(const void* _input, size_t& _idxCount, E_INDEX_TYPE _inIndexType, E_INDEX_TYPE _outIndexType);
227227

228228
//! Creates index buffer from input converting it to indices for triangle list primitives. Input is assumed to be indices for triangle strip.
229229
/**
@@ -232,7 +232,7 @@ namespace asset
232232
@param _inIndexType Type of input index buffer data (32bit or 16bit).
233233
@param _outIndexType Type of output index buffer data (32bit or 16bit).
234234
*/
235-
static core::smart_refctd_ptr<ICPUBuffer> idxBufferFromTriangleStripsToTriangles(const void* _input, size_t _idxCount, E_INDEX_TYPE _inIndexType, E_INDEX_TYPE _outIndexType);
235+
static core::smart_refctd_ptr<ICPUBuffer> idxBufferFromTriangleStripsToTriangles(const void* _input, size_t& _idxCount, E_INDEX_TYPE _inIndexType, E_INDEX_TYPE _outIndexType);
236236

237237
//! Creates index buffer from input converting it to indices for triangle list primitives. Input is assumed to be indices for triangle fan.
238238
/**
@@ -241,7 +241,7 @@ namespace asset
241241
@param _inIndexType Type of input index buffer data (32bit or 16bit).
242242
@param _outIndexType Type of output index buffer data (32bit or 16bit).
243243
*/
244-
static core::smart_refctd_ptr<ICPUBuffer> idxBufferFromTrianglesFanToTriangles(const void* _input, size_t _idxCount, E_INDEX_TYPE _inIndexType, E_INDEX_TYPE _outIndexType);
244+
static core::smart_refctd_ptr<ICPUBuffer> idxBufferFromTrianglesFanToTriangles(const void* _input, size_t& _idxCount, E_INDEX_TYPE _inIndexType, E_INDEX_TYPE _outIndexType);
245245

246246
//! Creates a 32bit index buffer for a mesh with primitive types changed to list types
247247
/**#
@@ -311,7 +311,7 @@ namespace asset
311311
ICPUMeshBuffer* cpumb = *it;
312312

313313
const auto indexType = cpumb->getIndexType();
314-
const auto indexCount = cpumb->getIndexCount();
314+
size_t indexCount = cpumb->getIndexCount();
315315

316316
auto& params = cpumb->getPipeline()->getPrimitiveAssemblyParams();
317317
core::smart_refctd_ptr<ICPUBuffer> newIndexBuffer;
@@ -355,7 +355,10 @@ namespace asset
355355
break;
356356
}
357357
if (newIndexBuffer)
358+
{
358359
cpumb->setIndexBufferBinding({0ull,std::move(newIndexBuffer)});
360+
cpumb->setIndexCount(indexCount);
361+
}
359362
cpumb->setIndexType(outIndexType);
360363
params.primitiveType = _newPrimitiveType;
361364
}

src/nbl/asset/CMeshManipulator.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -618,15 +618,17 @@ core::smart_refctd_ptr<ICPUMeshBuffer> IMeshManipulator::createOptimizedMeshBuff
618618
if (outbuffer->getPipeline()->getPrimitiveAssemblyParams().primitiveType == EPT_TRIANGLE_FAN)
619619
{
620620
outbuffer->getPipeline()->getPrimitiveAssemblyParams().primitiveType = EPT_TRIANGLE_LIST;
621-
auto newIb = idxBufferFromTrianglesFanToTriangles(outbuffer->getIndices(), outbuffer->getIndexCount(), canonicalMeshBufferIndexType, canonicalMeshBufferIndexType);
622-
outbuffer->setIndexCount(newIb->getSize() / sizeof(uint32_t));
621+
auto indexCount = outbuffer->getIndexCount();
622+
auto newIb = idxBufferFromTrianglesFanToTriangles(outbuffer->getIndices(), indexCount, canonicalMeshBufferIndexType, canonicalMeshBufferIndexType);
623+
outbuffer->setIndexCount(indexCount);
623624
outbuffer->setIndexBufferBinding({ 0u, std::move(newIb) });
624625
}
625626
else if (outbuffer->getPipeline()->getPrimitiveAssemblyParams().primitiveType == EPT_TRIANGLE_STRIP)
626627
{
627628
outbuffer->getPipeline()->getPrimitiveAssemblyParams().primitiveType = EPT_TRIANGLE_LIST;
628-
auto newIb = idxBufferFromTriangleStripsToTriangles(outbuffer->getIndices(), outbuffer->getIndexCount(), canonicalMeshBufferIndexType, canonicalMeshBufferIndexType);
629-
outbuffer->setIndexCount(newIb->getSize() / sizeof(uint32_t));
629+
auto indexCount = outbuffer->getIndexCount();
630+
auto newIb = idxBufferFromTriangleStripsToTriangles(outbuffer->getIndices(), indexCount, canonicalMeshBufferIndexType, canonicalMeshBufferIndexType);
631+
outbuffer->setIndexCount(indexCount);
630632
outbuffer->setIndexBufferBinding({ 0u, std::move(newIb) });
631633
}
632634
else if (outbuffer->getPipeline()->getPrimitiveAssemblyParams().primitiveType != EPT_TRIANGLE_LIST)
@@ -1535,7 +1537,7 @@ bool CMeshManipulator::calcMaxQuantizationError(const SAttribTypeChoice& _srcTyp
15351537
return true;
15361538
}
15371539

1538-
core::smart_refctd_ptr<ICPUBuffer> IMeshManipulator::idxBufferFromLineStripsToLines(const void* _input, size_t _idxCount, E_INDEX_TYPE _inIndexType, E_INDEX_TYPE _outIndexType)
1540+
core::smart_refctd_ptr<ICPUBuffer> IMeshManipulator::idxBufferFromLineStripsToLines(const void* _input, size_t& _idxCount, E_INDEX_TYPE _inIndexType, E_INDEX_TYPE _outIndexType)
15391541
{
15401542
if (_inIndexType == EIT_16BIT)
15411543
{
@@ -1554,7 +1556,7 @@ core::smart_refctd_ptr<ICPUBuffer> IMeshManipulator::idxBufferFromLineStripsToLi
15541556
return nullptr;
15551557
}
15561558

1557-
core::smart_refctd_ptr<ICPUBuffer> IMeshManipulator::idxBufferFromTriangleStripsToTriangles(const void* _input, size_t _idxCount, E_INDEX_TYPE _inIndexType, E_INDEX_TYPE _outIndexType)
1559+
core::smart_refctd_ptr<ICPUBuffer> IMeshManipulator::idxBufferFromTriangleStripsToTriangles(const void* _input, size_t& _idxCount, E_INDEX_TYPE _inIndexType, E_INDEX_TYPE _outIndexType)
15581560
{
15591561
if (_inIndexType == EIT_16BIT)
15601562
{
@@ -1573,7 +1575,7 @@ core::smart_refctd_ptr<ICPUBuffer> IMeshManipulator::idxBufferFromTriangleStrips
15731575
return nullptr;
15741576
}
15751577

1576-
core::smart_refctd_ptr<ICPUBuffer> IMeshManipulator::idxBufferFromTrianglesFanToTriangles(const void* _input, size_t _idxCount, E_INDEX_TYPE _inIndexType, E_INDEX_TYPE _outIndexType)
1578+
core::smart_refctd_ptr<ICPUBuffer> IMeshManipulator::idxBufferFromTrianglesFanToTriangles(const void* _input, size_t& _idxCount, E_INDEX_TYPE _inIndexType, E_INDEX_TYPE _outIndexType)
15771579
{
15781580
if (_inIndexType == EIT_16BIT)
15791581
{

src/nbl/asset/CMeshManipulator.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ class CMeshManipulator : public IMeshManipulator
8585
static bool calcMaxQuantizationError(const SAttribTypeChoice& _srcType, const SAttribTypeChoice& _dstType, const core::vector<core::vectorSIMDf>& _data, const SErrorMetric& _errMetric, CQuantNormalCache& _cache);
8686

8787
template<typename InType, typename OutType>
88-
static inline core::smart_refctd_ptr<ICPUBuffer> lineStripsToLines(const void* _input, size_t _idxCount)
88+
static inline core::smart_refctd_ptr<ICPUBuffer> lineStripsToLines(const void* _input, size_t& _idxCount)
8989
{
90-
const size_t outputSize = (_idxCount - 1) * 2;
90+
const size_t outputSize = _idxCount = (_idxCount - 1) * 2;
9191

9292
auto output = core::make_smart_refctd_ptr<ICPUBuffer>(sizeof(OutType)*outputSize);
9393
const auto* iptr = reinterpret_cast<const InType*>(_input);
@@ -101,9 +101,9 @@ class CMeshManipulator : public IMeshManipulator
101101
}
102102

103103
template<typename InType, typename OutType>
104-
static inline core::smart_refctd_ptr<ICPUBuffer> triangleStripsToTriangles(const void* _input, size_t _idxCount)
104+
static inline core::smart_refctd_ptr<ICPUBuffer> triangleStripsToTriangles(const void* _input, size_t& _idxCount)
105105
{
106-
const size_t outputSize = (_idxCount - 2) * 3;
106+
const size_t outputSize = _idxCount = (_idxCount - 2) * 3;
107107

108108
auto output = core::make_smart_refctd_ptr<ICPUBuffer>(sizeof(OutType)*outputSize);
109109
const auto* iptr = reinterpret_cast<const InType*>(_input);
@@ -123,9 +123,9 @@ class CMeshManipulator : public IMeshManipulator
123123
}
124124

125125
template<typename InType, typename OutType>
126-
static inline core::smart_refctd_ptr<ICPUBuffer> trianglesFanToTriangles(const void* _input, size_t _idxCount)
126+
static inline core::smart_refctd_ptr<ICPUBuffer> trianglesFanToTriangles(const void* _input, size_t& _idxCount)
127127
{
128-
const size_t outputSize = (_idxCount - 2) * 3;
128+
const size_t outputSize = _idxCount = (_idxCount - 2) * 3;
129129

130130
auto output = core::make_smart_refctd_ptr<ICPUBuffer>(sizeof(OutType)*outputSize);
131131
const auto* iptr = reinterpret_cast<const InType*>(_input);

src/nbl/asset/CPLYMeshWriter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ bool CPLYMeshWriter::writeAsset(io::IWriteFile* _file, const SAssetWriteParams&
147147
bool needToFreeIndices = false;
148148

149149
const void* ind = meshBuffer->getIndices();
150-
const auto idxCnt = meshBuffer->getIndexCount();
150+
auto idxCnt = meshBuffer->getIndexCount(); // when you convert triangle Fan or triangle strip to triangle list, the index count changes, and thats what you should derive your face count from
151151
const auto idxtype = meshBuffer->getIndexType();
152152
const auto primitiveT = mbPipeline->getPrimitiveAssemblyParams().primitiveType;
153153

@@ -191,7 +191,7 @@ bool CPLYMeshWriter::writeAsset(io::IWriteFile* _file, const SAssetWriteParams&
191191
}
192192
else if (primitiveT == asset::EPT_TRIANGLE_LIST)
193193
{
194-
faceCount = vtxCount / 3;
194+
faceCount = vtxCount / 3; // THIS IS WRONG
195195
forceFaces = true;
196196

197197
header += "element face ";

0 commit comments

Comments
 (0)