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