diff options
Diffstat (limited to 'libraries/irrlicht-1.8/source/Irrlicht/CPLYMeshWriter.cpp')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/CPLYMeshWriter.cpp | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CPLYMeshWriter.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CPLYMeshWriter.cpp new file mode 100644 index 0000000..c9f05c8 --- /dev/null +++ b/libraries/irrlicht-1.8/source/Irrlicht/CPLYMeshWriter.cpp | |||
@@ -0,0 +1,183 @@ | |||
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_PLY_WRITER_ | ||
8 | |||
9 | #include "CPLYMeshWriter.h" | ||
10 | #include "os.h" | ||
11 | #include "IMesh.h" | ||
12 | #include "IMeshBuffer.h" | ||
13 | #include "IWriteFile.h" | ||
14 | |||
15 | namespace irr | ||
16 | { | ||
17 | namespace scene | ||
18 | { | ||
19 | |||
20 | CPLYMeshWriter::CPLYMeshWriter() | ||
21 | { | ||
22 | #ifdef _DEBUG | ||
23 | setDebugName("CPLYMeshWriter"); | ||
24 | #endif | ||
25 | } | ||
26 | |||
27 | |||
28 | //! Returns the type of the mesh writer | ||
29 | EMESH_WRITER_TYPE CPLYMeshWriter::getType() const | ||
30 | { | ||
31 | return EMWT_PLY; | ||
32 | } | ||
33 | |||
34 | //! writes a mesh | ||
35 | bool CPLYMeshWriter::writeMesh(io::IWriteFile* file, scene::IMesh* mesh, s32 flags) | ||
36 | { | ||
37 | if (!file || !mesh) | ||
38 | return false; | ||
39 | |||
40 | os::Printer::log("Writing mesh", file->getFileName()); | ||
41 | |||
42 | // write PLY header | ||
43 | core::stringc header = | ||
44 | "ply\n" | ||
45 | "format ascii 1.0\n" | ||
46 | "comment Irrlicht Engine "; | ||
47 | header += IRRLICHT_SDK_VERSION; | ||
48 | |||
49 | // get vertex and triangle counts | ||
50 | u32 VertexCount = 0; | ||
51 | u32 TriangleCount = 0; | ||
52 | |||
53 | for (u32 i=0; i < mesh->getMeshBufferCount(); ++i) | ||
54 | { | ||
55 | VertexCount += mesh->getMeshBuffer(i)->getVertexCount(); | ||
56 | TriangleCount += mesh->getMeshBuffer(i)->getIndexCount() / 3; | ||
57 | } | ||
58 | |||
59 | // vertex definition | ||
60 | header += "\nelement vertex "; | ||
61 | header += VertexCount; | ||
62 | |||
63 | header += "\n" | ||
64 | "property float x\n" | ||
65 | "property float y\n" | ||
66 | "property float z\n" | ||
67 | "property float nx\n" | ||
68 | "property float ny\n" | ||
69 | "property float nz\n"; | ||
70 | // todo: writer flags for extended (r,g,b,u,v) and non-standard (alpha,u1,uv,tx,ty,tz) properties | ||
71 | // "property uchar red\n" | ||
72 | // "property uchar green\n" | ||
73 | // "property uchar blue\n" | ||
74 | // "property uchar alpha\n" | ||
75 | // "property float u\n" | ||
76 | // "property float v\n"; | ||
77 | // "property float u1\n | ||
78 | // "property float v1\n" | ||
79 | // "property float tx\n" | ||
80 | // "property float ty\n" | ||
81 | // "property float tz\n" | ||
82 | |||
83 | // face definition | ||
84 | |||
85 | header += "element face "; | ||
86 | header += TriangleCount; | ||
87 | header += "\n" | ||
88 | "property list uchar int vertex_indices\n" | ||
89 | "end_header\n"; | ||
90 | |||
91 | // write header | ||
92 | file->write(header.c_str(), header.size()); | ||
93 | |||
94 | // write vertices | ||
95 | |||
96 | c8 outLine[1024]; | ||
97 | |||
98 | for (u32 i=0; i < mesh->getMeshBufferCount(); ++i) | ||
99 | { | ||
100 | scene::IMeshBuffer* mb = mesh->getMeshBuffer(i); | ||
101 | for (u32 j=0; j < mb->getVertexCount(); ++j) | ||
102 | { | ||
103 | const core::vector3df& pos = mb->getPosition(j); | ||
104 | const core::vector3df& n = mb->getNormal(j); | ||
105 | // const core::vector2df& tc = mb->getTCoords(j); | ||
106 | |||
107 | u8 *buf = (u8*)mb->getVertices(); | ||
108 | switch(mb->getVertexType()) | ||
109 | { | ||
110 | case video::EVT_STANDARD: | ||
111 | buf += sizeof(video::S3DVertex)*j; | ||
112 | break; | ||
113 | case video::EVT_2TCOORDS: | ||
114 | buf += sizeof(video::S3DVertex2TCoords)*j; | ||
115 | break; | ||
116 | case video::EVT_TANGENTS: | ||
117 | buf += sizeof(video::S3DVertexTangents)*j; | ||
118 | break; | ||
119 | } | ||
120 | // video::SColor &col = ( (video::S3DVertex*)buf )->Color; | ||
121 | |||
122 | // x y z nx ny nz red green blue alpha u v [u1 v1 | tx ty tz]\n | ||
123 | snprintf(outLine, 1024, | ||
124 | "%f %f %f %f %f %f\n",// %u %u %u %u %f %f\n", | ||
125 | pos.X, pos.Z, pos.Y, // Y and Z are flipped | ||
126 | n.X, n.Z, n.Y); | ||
127 | /*col.getRed(), col.getGreen(), col.getBlue(), col.getAlpha(), | ||
128 | tc.X, tc.Y);*/ | ||
129 | |||
130 | // write the line | ||
131 | file->write(outLine, strlen(outLine)); | ||
132 | } | ||
133 | } | ||
134 | |||
135 | // index of the first vertex in the current mesh buffer | ||
136 | u32 StartOffset = 0; | ||
137 | |||
138 | // write triangles | ||
139 | for (u32 i=0; i < mesh->getMeshBufferCount(); ++i) | ||
140 | { | ||
141 | scene::IMeshBuffer* mb = mesh->getMeshBuffer(i); | ||
142 | for (u32 j=0; j < mb->getIndexCount(); j+=3) | ||
143 | { | ||
144 | // y and z are flipped so triangles are reversed | ||
145 | u32 a=StartOffset, | ||
146 | b=StartOffset, | ||
147 | c=StartOffset; | ||
148 | |||
149 | switch(mb->getIndexType()) | ||
150 | { | ||
151 | case video::EIT_16BIT: | ||
152 | a += mb->getIndices()[j+0]; | ||
153 | c += mb->getIndices()[j+1]; | ||
154 | b += mb->getIndices()[j+2]; | ||
155 | break; | ||
156 | case video::EIT_32BIT: | ||
157 | a += ((u32*)mb->getIndices()) [j+0]; | ||
158 | c += ((u32*)mb->getIndices()) [j+0]; | ||
159 | b += ((u32*)mb->getIndices()) [j+0]; | ||
160 | break; | ||
161 | } | ||
162 | |||
163 | // count a b c\n | ||
164 | snprintf(outLine, 1024, "3 %u %u %u\n", a, b, c); | ||
165 | // write the line | ||
166 | file->write(outLine, strlen(outLine)); | ||
167 | } | ||
168 | |||
169 | // increment offset | ||
170 | StartOffset += mb->getVertexCount(); | ||
171 | } | ||
172 | |||
173 | // all done! | ||
174 | |||
175 | |||
176 | return true; | ||
177 | } | ||
178 | |||
179 | } // end namespace | ||
180 | } // end namespace | ||
181 | |||
182 | #endif // _IRR_COMPILE_WITH_PLY_WRITER_ | ||
183 | |||