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