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
|
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#include "IrrCompileConfig.h"
#ifdef _IRR_COMPILE_WITH_STL_WRITER_
#include "CSTLMeshWriter.h"
#include "os.h"
#include "IMesh.h"
#include "IMeshBuffer.h"
#include "IAttributes.h"
#include "ISceneManager.h"
#include "IMeshCache.h"
#include "IWriteFile.h"
namespace irr
{
namespace scene
{
CSTLMeshWriter::CSTLMeshWriter(scene::ISceneManager* smgr)
: SceneManager(smgr)
{
#ifdef _DEBUG
setDebugName("CSTLMeshWriter");
#endif
if (SceneManager)
SceneManager->grab();
}
CSTLMeshWriter::~CSTLMeshWriter()
{
if (SceneManager)
SceneManager->drop();
}
//! Returns the type of the mesh writer
EMESH_WRITER_TYPE CSTLMeshWriter::getType() const
{
return EMWT_STL;
}
//! writes a mesh
bool CSTLMeshWriter::writeMesh(io::IWriteFile* file, scene::IMesh* mesh, s32 flags)
{
if (!file)
return false;
os::Printer::log("Writing mesh", file->getFileName());
if (flags & scene::EMWF_WRITE_COMPRESSED)
return writeMeshBinary(file, mesh, flags);
else
return writeMeshASCII(file, mesh, flags);
}
bool CSTLMeshWriter::writeMeshBinary(io::IWriteFile* file, scene::IMesh* mesh, s32 flags)
{
// write STL MESH header
file->write("binary ",7);
const core::stringc name(SceneManager->getMeshCache()->getMeshName(mesh));
const s32 sizeleft = 73-name.size(); // 80 byte header
if (sizeleft<0)
file->write(name.c_str(),73);
else
{
char* buf = new char[80];
memset(buf, 0, 80);
file->write(name.c_str(),name.size());
file->write(buf,sizeleft);
delete [] buf;
}
u32 facenum = 0;
for (u32 j=0; j<mesh->getMeshBufferCount(); ++j)
facenum += mesh->getMeshBuffer(j)->getIndexCount()/3;
file->write(&facenum,4);
// write mesh buffers
for (u32 i=0; i<mesh->getMeshBufferCount(); ++i)
{
IMeshBuffer* buffer = mesh->getMeshBuffer(i);
if (buffer)
{
const u32 indexCount = buffer->getIndexCount();
const u16 attributes = 0;
for (u32 j=0; j<indexCount; j+=3)
{
const core::vector3df& v1 = buffer->getPosition(buffer->getIndices()[j]);
const core::vector3df& v2 = buffer->getPosition(buffer->getIndices()[j+1]);
const core::vector3df& v3 = buffer->getPosition(buffer->getIndices()[j+2]);
const core::plane3df tmpplane(v1,v2,v3);
file->write(&tmpplane.Normal, 12);
file->write(&v1, 12);
file->write(&v2, 12);
file->write(&v3, 12);
file->write(&attributes, 2);
}
}
}
return true;
}
bool CSTLMeshWriter::writeMeshASCII(io::IWriteFile* file, scene::IMesh* mesh, s32 flags)
{
// write STL MESH header
file->write("solid ",6);
const core::stringc name(SceneManager->getMeshCache()->getMeshName(mesh));
file->write(name.c_str(),name.size());
file->write("\n\n",2);
// write mesh buffers
for (u32 i=0; i<mesh->getMeshBufferCount(); ++i)
{
IMeshBuffer* buffer = mesh->getMeshBuffer(i);
if (buffer)
{
const u32 indexCount = buffer->getIndexCount();
for (u32 j=0; j<indexCount; j+=3)
{
writeFace(file,
buffer->getPosition(buffer->getIndices()[j]),
buffer->getPosition(buffer->getIndices()[j+1]),
buffer->getPosition(buffer->getIndices()[j+2]));
}
file->write("\n",1);
}
}
file->write("endsolid ",9);
file->write(name.c_str(),name.size());
return true;
}
void CSTLMeshWriter::getVectorAsStringLine(const core::vector3df& v, core::stringc& s) const
{
s = core::stringc(v.X);
s += " ";
s += core::stringc(v.Y);
s += " ";
s += core::stringc(v.Z);
s += "\n";
}
void CSTLMeshWriter::writeFace(io::IWriteFile* file,
const core::vector3df& v1,
const core::vector3df& v2,
const core::vector3df& v3)
{
core::stringc tmp;
file->write("facet normal ",13);
getVectorAsStringLine(core::plane3df(v1,v2,v3).Normal, tmp);
file->write(tmp.c_str(),tmp.size());
file->write(" outer loop\n",13);
file->write(" vertex ",11);
getVectorAsStringLine(v1, tmp);
file->write(tmp.c_str(),tmp.size());
file->write(" vertex ",11);
getVectorAsStringLine(v2, tmp);
file->write(tmp.c_str(),tmp.size());
file->write(" vertex ",11);
getVectorAsStringLine(v3, tmp);
file->write(tmp.c_str(),tmp.size());
file->write(" endloop\n",10);
file->write("endfacet\n",9);
}
} // end namespace
} // end namespace
#endif
|