Skip to content

Commit f670252

Browse files
committed
STL binary data writer
1 parent c3140ea commit f670252

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/nbl/asset/interchange/CSTLMeshWriter.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,55 @@ namespace
6666
template <class I>
6767
inline void writeFacesBinary(const asset::ICPUPolygonGeometry* geom, const bool& noIndices, system::IFile* file, uint32_t _colorVaid, IAssetWriter::SAssetWriteContext* context, size_t* fileOffset)
6868
{
69+
using normal_t = hlsl::float32_t3;
70+
using vertex_t = hlsl::float32_t3;
71+
using index_t = uint32_t;
72+
73+
auto& posView = geom->getPositionView();
74+
auto& normalView = geom->getNormalView();
75+
auto& idxView = geom->getIndexView();
76+
77+
const auto vertexCount = posView.getElementCount();
78+
const auto idxCount = idxView.getElementCount();
79+
80+
// TODO: check if I can actually assume following types, if not, handle that
81+
const uint32_t* idxBufPtr = reinterpret_cast<const uint32_t*>(idxView.getPointer());
82+
const hlsl::float32_t3* vtxBufPtr = reinterpret_cast<const hlsl::float32_t3*>(posView.getPointer());
83+
84+
static auto calculateNormal = [](const hlsl::float32_t3& v1, const hlsl::float32_t3 v2, const hlsl::float32_t3 v3)
85+
{
86+
return hlsl::normalize(hlsl::cross(v2 - v1, v3 - v1));
87+
};
88+
89+
for (size_t i = 0; i < idxCount; i+=3)
90+
{
91+
index_t idx[3] = {};
92+
for (size_t j = 0; j < 3; j++)
93+
idx[i] = *(idxBufPtr + j + i);
94+
95+
vertex_t pos[3] = {};
96+
for (size_t j = 0; j < 3; j++)
97+
pos[j] = *(vtxBufPtr + idx[j]);
98+
99+
// TODO: vertex color
100+
// TODO: I could get the normal from normalView, but I need to think how can I do that well
101+
102+
normal_t n = calculateNormal(pos[1], pos[2], pos[3]);
103+
104+
// success variable can be reused, no need to scope it
105+
system::IFile::success_t success{};
106+
107+
// write normal
108+
file->write(success, &normal, *fileOffset, 12);
109+
*fileOffset += success.getBytesProcessed();
110+
111+
// write positions
112+
for (size_t j = 0; j < 3; j++)
113+
{
114+
file->write(success, &pos[i], *fileOffset, 12);
115+
*fileOffset += success.getBytesProcessed();
116+
}
117+
}
69118
#if 0
70119
auto& inputParams = buffer->getPipeline()->getCachedCreationParams().vertexInput;
71120
bool hasColor = inputParams.enabledAttribFlags & core::createBitmask({ COLOR_ATTRIBUTE });

0 commit comments

Comments
 (0)