diff options
Diffstat (limited to 'libraries/irrlicht-1.8/source/Irrlicht/COBJMeshWriter.cpp')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/COBJMeshWriter.cpp | 243 |
1 files changed, 243 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/COBJMeshWriter.cpp b/libraries/irrlicht-1.8/source/Irrlicht/COBJMeshWriter.cpp new file mode 100644 index 0000000..3555324 --- /dev/null +++ b/libraries/irrlicht-1.8/source/Irrlicht/COBJMeshWriter.cpp | |||
@@ -0,0 +1,243 @@ | |||
1 | // Copyright (C) 2008-2012 Christian Stehno | ||
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_OBJ_WRITER_ | ||
8 | |||
9 | #include "COBJMeshWriter.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 | #include "IFileSystem.h" | ||
18 | #include "ITexture.h" | ||
19 | |||
20 | namespace irr | ||
21 | { | ||
22 | namespace scene | ||
23 | { | ||
24 | |||
25 | COBJMeshWriter::COBJMeshWriter(scene::ISceneManager* smgr, io::IFileSystem* fs) | ||
26 | : SceneManager(smgr), FileSystem(fs) | ||
27 | { | ||
28 | #ifdef _DEBUG | ||
29 | setDebugName("COBJMeshWriter"); | ||
30 | #endif | ||
31 | |||
32 | if (SceneManager) | ||
33 | SceneManager->grab(); | ||
34 | |||
35 | if (FileSystem) | ||
36 | FileSystem->grab(); | ||
37 | } | ||
38 | |||
39 | |||
40 | COBJMeshWriter::~COBJMeshWriter() | ||
41 | { | ||
42 | if (SceneManager) | ||
43 | SceneManager->drop(); | ||
44 | |||
45 | if (FileSystem) | ||
46 | FileSystem->drop(); | ||
47 | } | ||
48 | |||
49 | |||
50 | //! Returns the type of the mesh writer | ||
51 | EMESH_WRITER_TYPE COBJMeshWriter::getType() const | ||
52 | { | ||
53 | return EMWT_OBJ; | ||
54 | } | ||
55 | |||
56 | |||
57 | //! writes a mesh | ||
58 | bool COBJMeshWriter::writeMesh(io::IWriteFile* file, scene::IMesh* mesh, s32 flags) | ||
59 | { | ||
60 | if (!file) | ||
61 | return false; | ||
62 | |||
63 | os::Printer::log("Writing mesh", file->getFileName()); | ||
64 | |||
65 | // write OBJ MESH header | ||
66 | |||
67 | const core::stringc name(FileSystem->getFileBasename(SceneManager->getMeshCache()->getMeshName(mesh), false)+".mtl"); | ||
68 | file->write("# exported by Irrlicht\n",23); | ||
69 | file->write("mtllib ",7); | ||
70 | file->write(name.c_str(),name.size()); | ||
71 | file->write("\n\n",2); | ||
72 | |||
73 | // write mesh buffers | ||
74 | |||
75 | core::array<video::SMaterial*> mat; | ||
76 | |||
77 | u32 allVertexCount=1; // count vertices over the whole file | ||
78 | for (u32 i=0; i<mesh->getMeshBufferCount(); ++i) | ||
79 | { | ||
80 | core::stringc num(i+1); | ||
81 | IMeshBuffer* buffer = mesh->getMeshBuffer(i); | ||
82 | if (buffer && buffer->getVertexCount()) | ||
83 | { | ||
84 | file->write("g grp", 5); | ||
85 | file->write(num.c_str(), num.size()); | ||
86 | file->write("\n",1); | ||
87 | |||
88 | u32 j; | ||
89 | const u32 vertexCount = buffer->getVertexCount(); | ||
90 | for (j=0; j<vertexCount; ++j) | ||
91 | { | ||
92 | file->write("v ",2); | ||
93 | getVectorAsStringLine(buffer->getPosition(j), num); | ||
94 | file->write(num.c_str(), num.size()); | ||
95 | } | ||
96 | |||
97 | for (j=0; j<vertexCount; ++j) | ||
98 | { | ||
99 | file->write("vt ",3); | ||
100 | getVectorAsStringLine(buffer->getTCoords(j), num); | ||
101 | file->write(num.c_str(), num.size()); | ||
102 | } | ||
103 | |||
104 | for (j=0; j<vertexCount; ++j) | ||
105 | { | ||
106 | file->write("vn ",3); | ||
107 | getVectorAsStringLine(buffer->getNormal(j), num); | ||
108 | file->write(num.c_str(), num.size()); | ||
109 | } | ||
110 | |||
111 | file->write("usemtl mat",10); | ||
112 | num = ""; | ||
113 | for (j=0; j<mat.size(); ++j) | ||
114 | { | ||
115 | if (*mat[j]==buffer->getMaterial()) | ||
116 | { | ||
117 | num = core::stringc(j); | ||
118 | break; | ||
119 | } | ||
120 | } | ||
121 | if (num == "") | ||
122 | { | ||
123 | num = core::stringc(mat.size()); | ||
124 | mat.push_back(&buffer->getMaterial()); | ||
125 | } | ||
126 | file->write(num.c_str(), num.size()); | ||
127 | file->write("\n",1); | ||
128 | |||
129 | const u32 indexCount = buffer->getIndexCount(); | ||
130 | for (j=0; j<indexCount; j+=3) | ||
131 | { | ||
132 | file->write("f ",2); | ||
133 | num = core::stringc(buffer->getIndices()[j+2]+allVertexCount); | ||
134 | file->write(num.c_str(), num.size()); | ||
135 | file->write("/",1); | ||
136 | file->write(num.c_str(), num.size()); | ||
137 | file->write("/",1); | ||
138 | file->write(num.c_str(), num.size()); | ||
139 | file->write(" ",1); | ||
140 | |||
141 | num = core::stringc(buffer->getIndices()[j+1]+allVertexCount); | ||
142 | file->write(num.c_str(), num.size()); | ||
143 | file->write("/",1); | ||
144 | file->write(num.c_str(), num.size()); | ||
145 | file->write("/",1); | ||
146 | file->write(num.c_str(), num.size()); | ||
147 | file->write(" ",1); | ||
148 | |||
149 | num = core::stringc(buffer->getIndices()[j+0]+allVertexCount); | ||
150 | file->write(num.c_str(), num.size()); | ||
151 | file->write("/",1); | ||
152 | file->write(num.c_str(), num.size()); | ||
153 | file->write("/",1); | ||
154 | file->write(num.c_str(), num.size()); | ||
155 | file->write(" ",1); | ||
156 | |||
157 | file->write("\n",1); | ||
158 | } | ||
159 | file->write("\n",1); | ||
160 | allVertexCount += vertexCount; | ||
161 | } | ||
162 | } | ||
163 | |||
164 | if (mat.size() == 0) | ||
165 | return true; | ||
166 | |||
167 | file = FileSystem->createAndWriteFile( name ); | ||
168 | if (file) | ||
169 | { | ||
170 | os::Printer::log("Writing material", file->getFileName()); | ||
171 | |||
172 | file->write("# exported by Irrlicht\n\n",24); | ||
173 | for (u32 i=0; i<mat.size(); ++i) | ||
174 | { | ||
175 | core::stringc num(i); | ||
176 | file->write("newmtl mat",10); | ||
177 | file->write(num.c_str(),num.size()); | ||
178 | file->write("\n",1); | ||
179 | |||
180 | getColorAsStringLine(mat[i]->AmbientColor, "Ka", num); | ||
181 | file->write(num.c_str(),num.size()); | ||
182 | getColorAsStringLine(mat[i]->DiffuseColor, "Kd", num); | ||
183 | file->write(num.c_str(),num.size()); | ||
184 | getColorAsStringLine(mat[i]->SpecularColor, "Ks", num); | ||
185 | file->write(num.c_str(),num.size()); | ||
186 | getColorAsStringLine(mat[i]->EmissiveColor, "Ke", num); | ||
187 | file->write(num.c_str(),num.size()); | ||
188 | num = core::stringc((double)(mat[i]->Shininess/0.128f)); | ||
189 | file->write("Ns ", 3); | ||
190 | file->write(num.c_str(),num.size()); | ||
191 | file->write("\n", 1); | ||
192 | if (mat[i]->getTexture(0)) | ||
193 | { | ||
194 | file->write("map_Kd ", 7); | ||
195 | file->write(mat[i]->getTexture(0)->getName().getPath().c_str(), mat[i]->getTexture(0)->getName().getPath().size()); | ||
196 | file->write("\n",1); | ||
197 | } | ||
198 | file->write("\n",1); | ||
199 | } | ||
200 | file->drop(); | ||
201 | } | ||
202 | return true; | ||
203 | } | ||
204 | |||
205 | |||
206 | void COBJMeshWriter::getVectorAsStringLine(const core::vector3df& v, core::stringc& s) const | ||
207 | { | ||
208 | s = core::stringc(-v.X); | ||
209 | s += " "; | ||
210 | s += core::stringc(v.Y); | ||
211 | s += " "; | ||
212 | s += core::stringc(v.Z); | ||
213 | s += "\n"; | ||
214 | } | ||
215 | |||
216 | |||
217 | void COBJMeshWriter::getVectorAsStringLine(const core::vector2df& v, core::stringc& s) const | ||
218 | { | ||
219 | s = core::stringc(v.X); | ||
220 | s += " "; | ||
221 | s += core::stringc(-v.Y); | ||
222 | s += "\n"; | ||
223 | } | ||
224 | |||
225 | |||
226 | void COBJMeshWriter::getColorAsStringLine(const video::SColor& color, const c8* const prefix, core::stringc& s) const | ||
227 | { | ||
228 | s = prefix; | ||
229 | s += " "; | ||
230 | s += core::stringc((double)(color.getRed()/255.f)); | ||
231 | s += " "; | ||
232 | s += core::stringc((double)(color.getGreen()/255.f)); | ||
233 | s += " "; | ||
234 | s += core::stringc((double)(color.getBlue()/255.f)); | ||
235 | s += "\n"; | ||
236 | } | ||
237 | |||
238 | |||
239 | } // end namespace | ||
240 | } // end namespace | ||
241 | |||
242 | #endif | ||
243 | |||