/
Lwo2Exporter.cpp
291 lines (229 loc) · 7.59 KB
/
Lwo2Exporter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "Lwo2Exporter.h"
#include <vector>
#include "itextstream.h"
#include "imodelsurface.h"
#include "imap.h"
#include "bytestreamutils.h"
namespace model
{
namespace
{
struct Chunk;
// Represents a Chunk in the LWO2 file, accepting content
// through the public stream member.
// A Chunk can have contents and/or 1 or more subchunks.
struct Chunk
{
public:
typedef std::shared_ptr<Chunk> Ptr;
enum class Type
{
Chunk,
SubChunk
};
private:
Type _chunkType;
// The number of bytes used for subchunks
unsigned int _childChunkSizeBytes;
public:
std::string identifier; // the 4-byte ID
// The contents of this chunk (don't stream subchunk contents
// into here, append them as subChunks instead)
std::string contents;
// Child chunks
std::vector<Chunk::Ptr> subChunks;
public:
//boost::iostreams::stream<Device> stream;
std::stringstream stream;
Chunk(const std::string& identifier_, Type type) :
_chunkType(type),
identifier(identifier_),
stream(std::ios_base::in | std::ios_base::out | std::ios_base::binary)
{
// FORM sub-chunks are normal chunks and have 4 bytes size info
// whereas subchunks of e.g. CLIP use 2 bytes of size info
_childChunkSizeBytes = _chunkType == Type::Chunk ? 4 : 2;
}
// Copy ctor
Chunk(const Chunk& other) :
_chunkType(other._chunkType),
_childChunkSizeBytes(other._childChunkSizeBytes),
identifier(other.identifier),
stream(std::ios_base::in | std::ios_base::out | std::ios_base::binary),
subChunks(other.subChunks)
{}
// Returns the size of this Chunk's content
// excluding this Chunk's ID (4 bytes) and Size info (4 bytes)
unsigned int getContentSize() const
{
unsigned int totalSize = 0;
// Start with the size of the contents
// (don't use seek as we don't know if the client still wants to write stuff)
totalSize += static_cast<unsigned int>(stream.str().length());
if (!subChunks.empty())
{
// Sum up the size of the subchunks
for (const Chunk::Ptr& chunk : subChunks)
{
totalSize += 4; // ID (4 bytes)
totalSize += _childChunkSizeBytes; // Subchunk Size Info (can be 4 or 2 bytes)
// While the child chunk size itself doesn't include padding, we need to respect
// it when calculating the size of this parent chunk
unsigned int childChunkSize = chunk->getContentSize();
totalSize += childChunkSize + (childChunkSize % 2); // add 1 padding byte if odd
}
}
// Chunk size can be odd, padding must be handled by client code
return totalSize;
}
// Adds the specified empty Chunk and returns its reference
Chunk::Ptr addChunk(const std::string& identifier_, Type type)
{
subChunks.push_back(std::make_shared<Chunk>(identifier_, type));
return subChunks.back();
}
void flushBuffer()
{
stream.flush();
for (const Chunk::Ptr& chunk : subChunks)
{
chunk->flushBuffer();
}
}
void writeToStream(std::ostream& output)
{
// Flush all buffers before writing to the output stream
flushBuffer();
output.write(identifier.c_str(), identifier.length());
if (_chunkType == Type::Chunk)
{
stream::writeBigEndian<uint32_t>(output, getContentSize());
}
else
{
stream::writeBigEndian<uint16_t>(output, getContentSize());
}
// Write the direct contents of this chunk
stream.seekg(0, std::stringstream::beg);
output << stream.rdbuf();
// Write all subchunks
for (const Chunk::Ptr& chunk : subChunks)
{
chunk->writeToStream(output);
// Add the padding byte after the chunk
if (chunk->getContentSize() % 2 == 1)
{
output.write("\0", 1);
}
}
}
};
}
Lwo2Exporter::Lwo2Exporter()
{}
IModelExporterPtr Lwo2Exporter::clone()
{
return std::make_shared<Lwo2Exporter>();
}
const std::string& Lwo2Exporter::getExtension() const
{
static std::string _extension("LWO");
return _extension;
}
// Adds the given Surface to the exporter's queue
void Lwo2Exporter::addSurface(const IModelSurface& incoming)
{
_surfaces.push_back(Surface());
Surface& surface = _surfaces.back();
surface.materialName = incoming.getDefaultMaterial();
// Pull in all the triangles of that mesh
for (int i = 0; i < incoming.getNumTriangles(); ++i)
{
ModelPolygon poly = incoming.getPolygon(i);
unsigned int indexStart = static_cast<unsigned int>(surface.vertices.size());
surface.vertices.push_back(poly.a);
surface.vertices.push_back(poly.b);
surface.vertices.push_back(poly.c);
surface.indices.push_back(indexStart);
surface.indices.push_back(indexStart + 1);
surface.indices.push_back(indexStart + 2);
}
}
// Export the model file to the given stream
void Lwo2Exporter::exportToStream(std::ostream& stream)
{
Chunk fileChunk("FORM", Chunk::Type::Chunk);
// The data of the FORM file contains just the LWO2 id and the collection of chunks
fileChunk.stream.write("LWO2", 4);
// Assemble the list of regular Chunks, these all use 4 bytes for size info
// TAGS
Chunk::Ptr tags = fileChunk.addChunk("TAGS", Chunk::Type::Chunk);
// Export all material names as tags
if (!_surfaces.empty())
{
for (const Surface& surface : _surfaces)
{
// Include the nul character at the end
tags->stream.write(surface.materialName.c_str(), surface.materialName.length() + 1);
}
}
else
{
tags->stream.write("\0", 1); // empty string as first tag name
}
// Create a single layer for the geometry
Chunk::Ptr layr = fileChunk.addChunk("LAYR", Chunk::Type::Chunk);
// LAYR{ number[U2], flags[U2], pivot[VEC12], name[S0], parent[U2] ? }
stream::writeBigEndian<uint16_t>(layr->stream, 0); // number[U2]
stream::writeBigEndian<uint16_t>(layr->stream, 0); // flags[U2]
// pivot[VEC12]
stream::writeBigEndian<float>(layr->stream, 0);
stream::writeBigEndian<float>(layr->stream, 0);
stream::writeBigEndian<float>(layr->stream, 0);
layr->stream.write("\0", 1); // name[S0]
// no parent index
// Create the chunks for PNTS, POLS, PTAG
Chunk::Ptr pnts = fileChunk.addChunk("PNTS", Chunk::Type::Chunk);
Chunk::Ptr pols = fileChunk.addChunk("POLS", Chunk::Type::Chunk);
Chunk::Ptr ptag = fileChunk.addChunk("PTAG", Chunk::Type::Chunk);
// We only ever export FACE polygons
pols->stream.write("FACE", 4);
ptag->stream.write("SURF", 4); // we tag the surfaces
// Load all vertex coordinates into this chunk
for (std::size_t surfNum = 0; surfNum < _surfaces.size(); ++surfNum)
{
Surface& surface = _surfaces[surfNum];
for (const ArbitraryMeshVertex& vertex : surface.vertices)
{
stream::writeBigEndian<float>(pnts->stream, static_cast<float>(vertex.vertex.x()));
stream::writeBigEndian<float>(pnts->stream, static_cast<float>(vertex.vertex.y()));
stream::writeBigEndian<float>(pnts->stream, static_cast<float>(vertex.vertex.z()));
}
int16_t numVerts = 3; // we export triangles
for (std::size_t i = 0; i + 2 < surface.indices.size(); i += 3)
{
std::size_t polyNum = i / 3;
stream::writeBigEndian<uint16_t>(pols->stream, numVerts);
// Fixme: Index type is VX, handle cases larger than FF00
stream::writeBigEndian<uint16_t>(pols->stream, surface.indices[i+0]);
stream::writeBigEndian<uint16_t>(pols->stream, surface.indices[i+1]);
stream::writeBigEndian<uint16_t>(pols->stream, surface.indices[i+2]);
// Fixme: Index type is VX, handle cases larger than FF00
stream::writeBigEndian<uint16_t>(ptag->stream, polyNum);
stream::writeBigEndian<uint16_t>(ptag->stream, surfNum);
}
// Write the SURF chunk for the surface
Chunk::Ptr surf = fileChunk.addChunk("SURF", Chunk::Type::Chunk);
if (!surface.materialName.empty())
{
surf->stream.write(surface.materialName.c_str(), surface.materialName.length() + 1);
}
else
{
surf->stream.write("\0", 1);
}
surf->stream.write("\0", 1); // empty parent name
}
fileChunk.writeToStream(stream);
}
}