diff options
Diffstat (limited to 'libraries/irrlicht-1.8/source/Irrlicht/CSTLMeshWriter.cpp')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/CSTLMeshWriter.cpp | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CSTLMeshWriter.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CSTLMeshWriter.cpp new file mode 100644 index 0000000..998a73f --- /dev/null +++ b/libraries/irrlicht-1.8/source/Irrlicht/CSTLMeshWriter.cpp | |||
@@ -0,0 +1,187 @@ | |||
1 | // Copyright (C) 2002-2012 Nikolaus Gebhardt | ||
2 | // This file is part of the "Irrlicht Engine". | ||
3 | // For conditions of distribution and use, see copyright notice in irrlicht.h | ||
4 | |||
5 | #include "IrrCompileConfig.h" | ||
6 | |||
7 | #ifdef _IRR_COMPILE_WITH_STL_WRITER_ | ||
8 | |||
9 | #include "CSTLMeshWriter.h" | ||
10 | #include "os.h" | ||
11 | #include "IMesh.h" | ||
12 | #include "IMeshBuffer.h" | ||
13 | #include "IAttributes.h" | ||
14 | #include "ISceneManager.h" | ||
15 | #include "IMeshCache.h" | ||
16 | #include "IWriteFile.h" | ||
17 | |||
18 | namespace irr | ||
19 | { | ||
20 | namespace scene | ||
21 | { | ||
22 | |||
23 | CSTLMeshWriter::CSTLMeshWriter(scene::ISceneManager* smgr) | ||
24 | : SceneManager(smgr) | ||
25 | { | ||
26 | #ifdef _DEBUG | ||
27 | setDebugName("CSTLMeshWriter"); | ||
28 | #endif | ||
29 | |||
30 | if (SceneManager) | ||
31 | SceneManager->grab(); | ||
32 | } | ||
33 | |||
34 | |||
35 | CSTLMeshWriter::~CSTLMeshWriter() | ||
36 | { | ||
37 | if (SceneManager) | ||
38 | SceneManager->drop(); | ||
39 | } | ||
40 | |||
41 | |||
42 | //! Returns the type of the mesh writer | ||
43 | EMESH_WRITER_TYPE CSTLMeshWriter::getType() const | ||
44 | { | ||
45 | return EMWT_STL; | ||
46 | } | ||
47 | |||
48 | |||
49 | //! writes a mesh | ||
50 | bool CSTLMeshWriter::writeMesh(io::IWriteFile* file, scene::IMesh* mesh, s32 flags) | ||
51 | { | ||
52 | if (!file) | ||
53 | return false; | ||
54 | |||
55 | os::Printer::log("Writing mesh", file->getFileName()); | ||
56 | |||
57 | if (flags & scene::EMWF_WRITE_COMPRESSED) | ||
58 | return writeMeshBinary(file, mesh, flags); | ||
59 | else | ||
60 | return writeMeshASCII(file, mesh, flags); | ||
61 | } | ||
62 | |||
63 | |||
64 | bool CSTLMeshWriter::writeMeshBinary(io::IWriteFile* file, scene::IMesh* mesh, s32 flags) | ||
65 | { | ||
66 | // write STL MESH header | ||
67 | |||
68 | file->write("binary ",7); | ||
69 | const core::stringc name(SceneManager->getMeshCache()->getMeshName(mesh)); | ||
70 | const s32 sizeleft = 73-name.size(); // 80 byte header | ||
71 | if (sizeleft<0) | ||
72 | file->write(name.c_str(),73); | ||
73 | else | ||
74 | { | ||
75 | char* buf = new char[80]; | ||
76 | memset(buf, 0, 80); | ||
77 | file->write(name.c_str(),name.size()); | ||
78 | file->write(buf,sizeleft); | ||
79 | delete [] buf; | ||
80 | } | ||
81 | u32 facenum = 0; | ||
82 | for (u32 j=0; j<mesh->getMeshBufferCount(); ++j) | ||
83 | facenum += mesh->getMeshBuffer(j)->getIndexCount()/3; | ||
84 | file->write(&facenum,4); | ||
85 | |||
86 | // write mesh buffers | ||
87 | |||
88 | for (u32 i=0; i<mesh->getMeshBufferCount(); ++i) | ||
89 | { | ||
90 | IMeshBuffer* buffer = mesh->getMeshBuffer(i); | ||
91 | if (buffer) | ||
92 | { | ||
93 | const u32 indexCount = buffer->getIndexCount(); | ||
94 | const u16 attributes = 0; | ||
95 | for (u32 j=0; j<indexCount; j+=3) | ||
96 | { | ||
97 | const core::vector3df& v1 = buffer->getPosition(buffer->getIndices()[j]); | ||
98 | const core::vector3df& v2 = buffer->getPosition(buffer->getIndices()[j+1]); | ||
99 | const core::vector3df& v3 = buffer->getPosition(buffer->getIndices()[j+2]); | ||
100 | const core::plane3df tmpplane(v1,v2,v3); | ||
101 | file->write(&tmpplane.Normal, 12); | ||
102 | file->write(&v1, 12); | ||
103 | file->write(&v2, 12); | ||
104 | file->write(&v3, 12); | ||
105 | file->write(&attributes, 2); | ||
106 | } | ||
107 | } | ||
108 | } | ||
109 | return true; | ||
110 | } | ||
111 | |||
112 | |||
113 | bool CSTLMeshWriter::writeMeshASCII(io::IWriteFile* file, scene::IMesh* mesh, s32 flags) | ||
114 | { | ||
115 | // write STL MESH header | ||
116 | |||
117 | file->write("solid ",6); | ||
118 | const core::stringc name(SceneManager->getMeshCache()->getMeshName(mesh)); | ||
119 | file->write(name.c_str(),name.size()); | ||
120 | file->write("\n\n",2); | ||
121 | |||
122 | // write mesh buffers | ||
123 | |||
124 | for (u32 i=0; i<mesh->getMeshBufferCount(); ++i) | ||
125 | { | ||
126 | IMeshBuffer* buffer = mesh->getMeshBuffer(i); | ||
127 | if (buffer) | ||
128 | { | ||
129 | const u32 indexCount = buffer->getIndexCount(); | ||
130 | for (u32 j=0; j<indexCount; j+=3) | ||
131 | { | ||
132 | writeFace(file, | ||
133 | buffer->getPosition(buffer->getIndices()[j]), | ||
134 | buffer->getPosition(buffer->getIndices()[j+1]), | ||
135 | buffer->getPosition(buffer->getIndices()[j+2])); | ||
136 | } | ||
137 | file->write("\n",1); | ||
138 | } | ||
139 | } | ||
140 | |||
141 | file->write("endsolid ",9); | ||
142 | file->write(name.c_str(),name.size()); | ||
143 | |||
144 | return true; | ||
145 | } | ||
146 | |||
147 | |||
148 | void CSTLMeshWriter::getVectorAsStringLine(const core::vector3df& v, core::stringc& s) const | ||
149 | { | ||
150 | s = core::stringc(v.X); | ||
151 | s += " "; | ||
152 | s += core::stringc(v.Y); | ||
153 | s += " "; | ||
154 | s += core::stringc(v.Z); | ||
155 | s += "\n"; | ||
156 | } | ||
157 | |||
158 | |||
159 | void CSTLMeshWriter::writeFace(io::IWriteFile* file, | ||
160 | const core::vector3df& v1, | ||
161 | const core::vector3df& v2, | ||
162 | const core::vector3df& v3) | ||
163 | { | ||
164 | core::stringc tmp; | ||
165 | file->write("facet normal ",13); | ||
166 | getVectorAsStringLine(core::plane3df(v1,v2,v3).Normal, tmp); | ||
167 | file->write(tmp.c_str(),tmp.size()); | ||
168 | file->write(" outer loop\n",13); | ||
169 | file->write(" vertex ",11); | ||
170 | getVectorAsStringLine(v1, tmp); | ||
171 | file->write(tmp.c_str(),tmp.size()); | ||
172 | file->write(" vertex ",11); | ||
173 | getVectorAsStringLine(v2, tmp); | ||
174 | file->write(tmp.c_str(),tmp.size()); | ||
175 | file->write(" vertex ",11); | ||
176 | getVectorAsStringLine(v3, tmp); | ||
177 | file->write(tmp.c_str(),tmp.size()); | ||
178 | file->write(" endloop\n",10); | ||
179 | file->write("endfacet\n",9); | ||
180 | } | ||
181 | |||
182 | |||
183 | } // end namespace | ||
184 | } // end namespace | ||
185 | |||
186 | #endif | ||
187 | |||