@@ -85,37 +85,84 @@ bool CPLYMeshWriter::writeAsset(io::IWriteFile* _file, const SAssetWriteParams&
85
85
86
86
const asset::E_WRITER_FLAGS flags = _override->getAssetWritingFlags (ctx, mesh, 0u );
87
87
88
+ auto getConvertedMeshBufferWithNewIndexBuffer = [&]() -> core::smart_refctd_ptr<asset::ICPUMeshBuffer>
89
+ {
90
+ auto inputMeshBuffer = *meshbuffers.begin ();
91
+
92
+ const auto inputIndexType = inputMeshBuffer->getIndexType ();
93
+ const auto inputPrimitiveType = inputMeshBuffer->getPipeline ()->getPrimitiveAssemblyParams ().primitiveType ;
94
+
95
+ const void * inputIndices = inputMeshBuffer->getIndices ();
96
+ auto inputIndexCount = inputMeshBuffer->getIndexCount ();
97
+
98
+ if (inputMeshBuffer->getIndexBufferBinding ().buffer && inputPrimitiveType != asset::EPT_TRIANGLE_LIST)
99
+ {
100
+ auto copyMeshBuffer = core::smart_refctd_ptr_static_cast<asset::ICPUMeshBuffer>(inputMeshBuffer->clone ());
101
+
102
+ if (inputPrimitiveType == asset::EPT_TRIANGLE_FAN || inputPrimitiveType == asset::EPT_TRIANGLE_STRIP)
103
+ {
104
+ core::smart_refctd_ptr<ICPUBuffer> newIndexBuffer;
105
+
106
+ if (inputPrimitiveType == asset::EPT_TRIANGLE_FAN)
107
+ newIndexBuffer = IMeshManipulator::idxBufferFromTrianglesFanToTriangles (inputIndices, inputIndexCount, inputIndexType, inputIndexType);
108
+ else if (inputPrimitiveType == asset::EPT_TRIANGLE_STRIP)
109
+ newIndexBuffer = IMeshManipulator::idxBufferFromTriangleStripsToTriangles (inputIndices, inputIndexCount, inputIndexType, inputIndexType);
110
+
111
+ asset::SBufferBinding<asset::ICPUBuffer> indexBufferBinding;
112
+ indexBufferBinding.buffer = newIndexBuffer;
113
+ indexBufferBinding.offset = 0u ;
114
+
115
+ copyMeshBuffer->setIndexBufferBinding (std::move (indexBufferBinding));
116
+ return copyMeshBuffer;
117
+ }
118
+ }
119
+ else
120
+ return nullptr ;
121
+ };
122
+
123
+ const auto cpuConvertedMeshBuffer = getConvertedMeshBufferWithNewIndexBuffer ();
124
+ const asset::ICPUMeshBuffer* rawConvertedMeshBuffer = cpuConvertedMeshBuffer.get () ? cpuConvertedMeshBuffer.get () : *meshbuffers.begin ();
125
+ const auto primitiveType = rawConvertedMeshBuffer->getPipeline ()->getPrimitiveAssemblyParams ().primitiveType ;
126
+ const auto indexType = rawConvertedMeshBuffer->getIndexType ();
127
+ uint32_t faceCount = {};
128
+ size_t vertexCount = {};
129
+
130
+ void * indices = nullptr ;
131
+ {
132
+ auto indexCount = rawConvertedMeshBuffer->getIndexCount ();
133
+
134
+ indices = _NBL_ALIGNED_MALLOC (indexCount * (indexType == asset::EIT_16BIT ? 2u : 4u ), _NBL_SIMD_ALIGNMENT);
135
+ memcpy (indices, rawConvertedMeshBuffer->getIndices (), indexCount * (indexType == asset::EIT_16BIT ? 2u : 4u ));
136
+
137
+ IMeshManipulator::getPolyCount (faceCount, rawConvertedMeshBuffer);
138
+ vertexCount = IMeshManipulator::upperBoundVertexID (rawConvertedMeshBuffer);
139
+ }
140
+
88
141
// write PLY header
89
142
std::string header = " ply\n " ;
90
143
header += (flags & asset::EWF_BINARY) ? " format binary_little_endian 1.0" : " format ascii 1.0" ;
91
144
header += " \n comment IrrlichtBAW " ;
92
145
header += NABLA_SDK_VERSION;
93
146
94
- auto meshBuffer = *meshbuffers.begin ();
95
- // get vertex and triangle counts
96
- size_t vtxCount = IMeshManipulator::upperBoundVertexID (meshBuffer);
97
- size_t faceCount = meshBuffer->getIndexCount () / 3 ;
98
-
99
147
// vertex definition
100
148
header += " \n element vertex " ;
101
- header += std::to_string (vtxCount ) + ' \n ' ;
149
+ header += std::to_string (vertexCount ) + ' \n ' ;
102
150
103
151
bool vaidToWrite[4 ]{ 0 , 0 , 0 , 0 };
104
- auto mbPipeline = meshBuffer->getPipeline ();
105
152
106
- if (meshBuffer ->getAttribBoundBuffer (0 ).buffer )
153
+ if (rawConvertedMeshBuffer ->getAttribBoundBuffer (0 ).buffer )
107
154
{
108
- const asset::E_FORMAT t = meshBuffer ->getAttribFormat (0 );
155
+ const asset::E_FORMAT t = rawConvertedMeshBuffer ->getAttribFormat (0 );
109
156
std::string typeStr = getTypeString (t);
110
157
vaidToWrite[0 ] = true ;
111
158
header +=
112
159
" property " + typeStr + " x\n " +
113
160
" property " + typeStr + " y\n " +
114
161
" property " + typeStr + " z\n " ;
115
162
}
116
- if (meshBuffer ->getAttribBoundBuffer (1 ).buffer )
163
+ if (rawConvertedMeshBuffer ->getAttribBoundBuffer (1 ).buffer )
117
164
{
118
- const asset::E_FORMAT t = meshBuffer ->getAttribFormat (1 );
165
+ const asset::E_FORMAT t = rawConvertedMeshBuffer ->getAttribFormat (1 );
119
166
std::string typeStr = getTypeString (t);
120
167
vaidToWrite[1 ] = true ;
121
168
header +=
@@ -127,100 +174,61 @@ bool CPLYMeshWriter::writeAsset(io::IWriteFile* _file, const SAssetWriteParams&
127
174
header += " property " + typeStr + " alpha\n " ;
128
175
}
129
176
}
130
- if (meshBuffer ->getAttribBoundBuffer (2 ).buffer )
177
+ if (rawConvertedMeshBuffer ->getAttribBoundBuffer (2 ).buffer )
131
178
{
132
- const asset::E_FORMAT t = meshBuffer ->getAttribFormat (2 );
179
+ const asset::E_FORMAT t = rawConvertedMeshBuffer ->getAttribFormat (2 );
133
180
std::string typeStr = getTypeString (t);
134
181
vaidToWrite[2 ] = true ;
135
182
header +=
136
183
" property " + typeStr + " u\n " +
137
184
" property " + typeStr + " v\n " ;
138
185
}
139
- if (meshBuffer ->getAttribBoundBuffer (3 ).buffer )
186
+ if (rawConvertedMeshBuffer ->getAttribBoundBuffer (3 ).buffer )
140
187
{
141
- const asset::E_FORMAT t = meshBuffer ->getAttribFormat (3 );
188
+ const asset::E_FORMAT t = rawConvertedMeshBuffer ->getAttribFormat (3 );
142
189
std::string typeStr = getTypeString (t);
143
190
vaidToWrite[3 ] = true ;
144
191
header +=
145
192
" property " + typeStr + " nx\n " +
146
193
" property " + typeStr + " ny\n " +
147
194
" property " + typeStr + " nz\n " ;
148
- }
149
-
150
- void * indices = nullptr ;
151
- bool needToFreeIndices = false ;
152
-
153
- const void * ind = meshBuffer->getIndices ();
154
- 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
155
- // github comment
156
-
157
- const auto idxtype = meshBuffer->getIndexType ();
158
- const auto primitiveT = mbPipeline->getPrimitiveAssemblyParams ().primitiveType ;
159
-
160
- if (meshBuffer->getIndexBufferBinding ().buffer && primitiveT != asset::EPT_TRIANGLE_LIST)
161
- {
162
- if (primitiveT == asset::EPT_TRIANGLE_FAN || primitiveT == asset::EPT_TRIANGLE_STRIP)
163
- {
164
- core::smart_refctd_ptr<ICPUBuffer> buf;
165
- if (primitiveT == asset::EPT_TRIANGLE_FAN)
166
- {
167
- buf = IMeshManipulator::idxBufferFromTrianglesFanToTriangles (ind, idxCnt, idxtype, idxtype);
168
- }
169
- else if (primitiveT == asset::EPT_TRIANGLE_STRIP)
170
- {
171
- buf = IMeshManipulator::idxBufferFromTriangleStripsToTriangles (ind, idxCnt, idxtype, idxtype);
172
- }
173
- needToFreeIndices = true ;
174
- faceCount = buf->getSize () / (idxtype == asset::EIT_16BIT ? 2u : 4u ) / 3u ;
175
- indices = _NBL_ALIGNED_MALLOC (buf->getSize (),_NBL_SIMD_ALIGNMENT);
176
- memcpy (indices, buf->getPointer (), buf->getSize ());
177
- }
178
- }
179
- else {
180
- indices = _NBL_ALIGNED_MALLOC (idxCnt * (idxtype == asset::EIT_16BIT ? 2u : 4u ), _NBL_SIMD_ALIGNMENT);
181
- memcpy (indices, meshBuffer->getIndices (), idxCnt * (idxtype == asset::EIT_16BIT ? 2u : 4u ));
182
- }
195
+ }
183
196
184
197
asset::E_INDEX_TYPE idxT = asset::EIT_UNKNOWN;
185
198
bool forceFaces = false ;
186
- if (primitiveT == asset::EPT_POINT_LIST)
187
- {
199
+
200
+ if (primitiveType == asset::EPT_POINT_LIST)
188
201
faceCount = 0u ;
189
- }
190
- else if (indices && idxtype != asset::EIT_UNKNOWN)
202
+ else if (indices && indexType != asset::EIT_UNKNOWN)
191
203
{
192
204
header += " element face " ;
193
205
header += std::to_string (faceCount) + ' \n ' ;
194
- idxT = idxtype ;
206
+ idxT = indexType ;
195
207
const std::string idxTypeStr = idxT == asset::EIT_32BIT ? " uint32" : " uint16" ;
196
208
header += " property list uchar " + idxTypeStr + " vertex_indices\n " ;
197
209
}
198
- else if (primitiveT == asset::EPT_TRIANGLE_LIST)
210
+ else if (primitiveType == asset::EPT_TRIANGLE_LIST)
199
211
{
200
- faceCount = vtxCount THIS IS ABSOLUTELY 100 % WRONG / 3 ; // TODO: THIS IS WRONG
201
212
forceFaces = true ;
202
213
203
214
header += " element face " ;
204
215
header += std::to_string (faceCount) + ' \n ' ;
205
- idxT = vtxCount <= ((1u <<16 ) - 1 ) ? asset::EIT_16BIT : asset::EIT_32BIT;
216
+ idxT = vertexCount <= ((1u <<16 ) - 1 ) ? asset::EIT_16BIT : asset::EIT_32BIT;
206
217
const std::string idxTypeStr = idxT == asset::EIT_32BIT ? " uint32" : " uint16" ;
207
218
header += " property list uchar " + idxTypeStr + " vertex_indices\n " ;
208
219
}
209
220
else
210
- {
211
221
faceCount = 0u ;
212
- }
213
222
header += " end_header\n " ;
214
223
215
224
file->write (header.c_str (), header.size ());
216
225
217
226
if (flags & asset::EWF_BINARY)
218
- writeBinary (file, meshBuffer, vtxCount , faceCount, idxT, indices, forceFaces, vaidToWrite, _params);
227
+ writeBinary (file, rawConvertedMeshBuffer, vertexCount , faceCount, idxT, indices, forceFaces, vaidToWrite, _params);
219
228
else
220
- writeText (file, meshBuffer, vtxCount , faceCount, idxT, indices, forceFaces, vaidToWrite, _params);
229
+ writeText (file, rawConvertedMeshBuffer, vertexCount , faceCount, idxT, indices, forceFaces, vaidToWrite, _params);
221
230
222
- if (needToFreeIndices)
223
- _NBL_ALIGNED_FREE (const_cast <void *>(indices));
231
+ _NBL_ALIGNED_FREE (const_cast <void *>(indices));
224
232
225
233
return true ;
226
234
}
0 commit comments