OSDN Git Service

fix io.write.
[meshio/meshio.git] / include / pmd.h
1 /**
2  * PMD\8c`\8e®
3  * MMD\82Ì\83\82\83f\83\8b\83f\81[\83^\81B\83o\83C\83i\83\8a\8c`\8e®\82Å\82Ð\82Æ\82Â\82Ì\92¸\93_\82ª\8dÅ\91å\82Q\82Â\82Ü\82Å\82Ì\83{\81[\83\93\82Ì
4  * \83E\83F\83C\83g\82ð\95Û\8e\9d\82·\82é\92P\88ê\82Ì\92¸\93_\94z\97ñ\82Æ\92¸\93_\83C\83\93\83f\83b\83N\83X\94z\97ñ\82ð\82à\82Â\81B\96Ê\82Í
5  * \8eO\8ap\8c`\82Ì\82Ý\81B
6  * \82Ü\82½\95\\8fî\83\82\81[\83t\83B\83\93\83O\8fî\95ñ\81A\95¨\97\9d\89\89\8eZ\8cü\82¯\82Ì\8d\84\91Ì\8fî\95ñ\82Æ\8dS\91©\8fî\95ñ\82È\82Ç\82ð\82à\82Â\81B
7  *
8  * \8dÀ\95W\8cn
9  * \8d\8eè Y-UP
10  *
11  * \95\
12  * clock wise ?
13  *
14  * UV\8c´\93_
15  * left top ?
16  *
17  * \96@\90ü
18  * \92¸\93_\96@\90ü\82ª\8ai\94[\8dÏ\82Ý\81B
19  *
20  * \83\81\83b\83V\83\85
21  * \8dÅ\91å\92¸\93_\90\94
22  * \8dÅ\91å\8eO\8ap\8c`\90\94
23  *
24  * \97 \96Ê\82Ì\88µ\82¢
25  * \83I\83\8a\83W\83i\83\8b\82Å\82Í\83o\83b\83N\83J\83\8a\83\93\83O\82ð\82µ\82Ä\82¢\82È\82¢\82Ì\82Å\82â\82é\8fê\8d\87\82Í\83\82\83f\83\8b\8cÂ\95Ê\82É
26  * \91Î\89\9e\82ª\95K\97v\81B
27  *
28  * \8eQ\8dl\83T\83C\83g
29  * http://blog.goo.ne.jp/torisu_tetosuki/e/209ad341d3ece2b1b4df24abf619d6e4
30  */
31
32 #ifndef MESH_IO_PMD_H_INCLUDED
33 #define MESH_IO_PMD_H_INCLUDED 
34
35 #include <ostream>
36 #include <vector>
37 #include "la.h"
38 #include "text.h"
39
40 namespace meshio {
41 namespace pmd {
42
43 typedef ::meshio::la::Vector2 Vector2;
44 typedef ::meshio::la::Vector3 Vector3;
45 typedef ::meshio::la::Vector4 Vector4;
46 typedef ::meshio::color::fRGBA fRGBA;
47 typedef ::meshio::color::fRGB fRGB;
48
49 ////////////////////////////////////////////////////////////
50 //! \92¸\93_
51 ////////////////////////////////////////////////////////////
52 struct Vertex
53 {
54         //! \8dÀ\95W
55         Vector3 pos;
56         //! \96@\90ü\83x\83N\83g\83\8b
57         Vector3 normal;
58         //! \83e\83N\83X\83`\83\83UV
59         Vector2 uv;
60         //! \83u\83\8c\83\93\83f\83B\83\93\83O\83{\81[\83\931
61         unsigned short bone0;
62         //! \83u\83\8c\83\93\83f\83B\83\93\83O\83{\81[\83\932
63         unsigned short bone1;
64         //! \83E\83F\83C\83g[0 - 100]
65         unsigned char weight0;
66         //! \94ñ\83G\83b\83W
67         unsigned char edge_flag;
68 };
69 inline std::ostream &operator<<(std::ostream &os, const Vertex &rhs)
70 {
71         os
72                 << "[Vertex"
73                 << " pos:" << rhs.pos
74                 << " normal:" << rhs.normal
75                 << " uv:" << rhs.uv
76                 << " bone0:" << rhs.bone0
77                 << " bone1:" << rhs.bone1
78                 << " weight0:" << (int)rhs.weight0
79                 << " edge_flag:" << (int)rhs.edge_flag
80                 << "]"
81                 ;
82         return os;
83 }
84
85 ////////////////////////////////////////////////////////////
86 //! \8dÞ\8e¿
87 ////////////////////////////////////////////////////////////
88 struct Material
89 {
90         //! Diffuse
91         fRGBA diffuse;
92         //! Shinness
93         float shinness;
94         //! Specular
95         fRGB specular;
96         //! Ambient
97         fRGB ambient;
98         //! \83g\83D\81[\83\93\83e\83N\83X\83`\83\83
99         unsigned char toon_index;
100         //! \97Ö\8as/\89e
101         unsigned char flag;
102         //! \96Ê\92¸\93_\90\94
103         unsigned int vertex_count;
104         //! \83e\83N\83X\83`\83\83
105         char texture[20];
106
107         std::wstring getTexture()const;
108 };
109 inline std::ostream &operator<<(std::ostream &os,
110                 const Material &rhs)
111 {
112         os
113                 << "[Material"
114                 << " diffuse:" << rhs.diffuse
115                 << " toon_index:" << (int)rhs.toon_index
116                 << " flag:" << (int)rhs.flag
117                 << " vertex_count:" << rhs.vertex_count
118                 << " texture:" << rhs.texture
119                 << "]"
120                 ;
121         return os;
122 }
123
124 ////////////////////////////////////////////////////////////
125 //! \83{\81[\83\93 
126 ////////////////////////////////////////////////////////////
127 //! \83{\81[\83\93\82Ì\8eí\97Þ
128 enum BONE_TYPE
129 {
130         // \89ñ\93]
131         BONE_ROTATE=0,
132         // \89ñ\93]\82Æ\88Ú\93®
133         BONE_ROTATE_MOVE,
134         // IK
135         BONE_IK,
136         // \95s\96¾
137         BONE_UNKNOWN,
138         // IK\89e\8b¿\89º
139         BONE_IK_INFLUENCED,
140         // \89ñ\93]\89e\8b¿\89º
141         BONE_ROTATE_INFLUENCED,
142         // IK\90Ú\91±\90æ
143         BONE_IK_CONNECT,
144         // \94ñ\95\\8e¦
145         BONE_INVISIBLE,
146         // \94P\82è
147         BONE_TWIST,
148         // \89ñ\93]\98A\93®
149         BONE_REVOLVE,
150 };
151 struct Bone
152 {
153         //! \96¼\91O
154         char name[20];
155         //! \90e\83{\81[\83\93
156         unsigned short parent_index;
157         //! \8eq\83{\81[\83\93
158         unsigned short tail_index;
159         //! \83{\81[\83\93\8eí\97Þ
160         BONE_TYPE type;
161         //! \89e\8b¿IK\83{\81[\83\93
162         unsigned short ik_index;
163         // \83{\81[\83\93\8dÀ\95W
164         Vector3 pos;
165         //! \89p\8cê\96¼
166         char english_name[20];
167         //! \83{\81[\83\93\8aK\91w\8d\\92z\97p
168         Bone* parent;
169         Vector3 tail;
170         std::vector<Bone*> children;
171         unsigned short index;
172
173         Bone()
174                 : parent(0)
175                 {}
176         std::wstring getName()const;
177 };
178 inline std::ostream &operator<<(std::ostream &os,
179                 const Bone &rhs)
180 {
181         os
182                 << "[Bone "
183                 << '"' << rhs.name << '"'
184                 << "]"
185                 ;
186         return os;
187 }
188
189 ////////////////////////////////////////////////////////////
190 //! IK
191 ////////////////////////////////////////////////////////////
192 struct IK
193 {
194         //! IK(IK\83^\81[\83Q\83b\83g)
195         unsigned short index;
196         //! Target(\83G\83t\83F\83N\83^\81[)
197         unsigned short target;
198         //! \83G\83t\83F\83N\83^\82É\98A\93®\82·\82é\83{\81[\83\93\90\94
199         unsigned char length;
200         //! IK\92l1\81BCCD-IK\8e\8e\8ds\89ñ\90\94
201         unsigned short iterations;
202         //! IK\92l2\81BCCD-IK\8e\8e\8ds\88ê\89ñ\95Ó\82è\82Ì\89e\8b¿\93x
203         float weight;
204         //! \83G\83t\83F\83N\83^\82É\98A\93®\82·\82é\83{\81[\83\93(\8aî\96{\93I\82É\90e\83{\81[\83\93\82É\91k\82é)
205         std::vector<unsigned short> children;
206 };
207 inline std::ostream &operator<<(std::ostream &os, const IK &rhs)
208 {
209         os
210                 << "[IK "
211                 << "]"
212                 ;
213         return os;
214 }
215
216 ////////////////////////////////////////////////////////////
217 //! \95\\8fî
218 ////////////////////////////////////////////////////////////
219 //! \95\\8fî\82Ì\8eí\97Þ
220 enum MORPH_TYPE
221 {
222         //! \83x\81[\83X\95\\8fî
223         MORPH_BASE=0,
224         //! \82Ü\82ä
225         MORPH_MAYU,
226         //! \96Ú
227         MORPH_ME,
228         //! \83\8a\83b\83v
229         MORPH_LIP,
230         //! \82»\82Ì\91¼
231         MORPH_OTHER,
232 };
233 struct Morph
234 {
235         //! \95\\8fî\96¼
236         char name[20];
237         //! \8eg\97p\82·\82é\92¸\93_\90\94
238         unsigned int vertex_count;
239         //! \95ª\97Þ
240         unsigned char type;
241         //! \92¸\93_Index
242         std::vector<unsigned int> indices;
243         //! \88Ú\93®\97Ê
244         std::vector<Vector3> pos_list;
245         //! \89p\8cê\96¼
246         char english_name[20];
247
248         void append(int index, float x, float y, float z);
249         std::wstring getName()const;
250 };
251 inline std::ostream &operator<<(std::ostream &os, const Morph &rhs)
252 {
253         os
254                 << "[Morph "
255                 << '"' << rhs.name << '"'
256                 << "]"
257                 ;
258         return os;
259 }
260
261 ////////////////////////////////////////////////////////////
262 //! \83{\81[\83\93\95\\8e¦\98g
263 ////////////////////////////////////////////////////////////
264 struct BoneDisplayName
265 {
266         char name[50];
267         char english_name[50];
268
269         void setName(const char *name);
270         void setEnglishName(const char *english);
271 };
272
273 ////////////////////////////////////////////////////////////
274 //! \83g\83D\81[\83\93\83e\83N\83X\83`\83\83
275 ////////////////////////////////////////////////////////////
276 struct ToonTexture
277 {
278         char name[100];
279 };
280
281 ////////////////////////////////////////////////////////////
282 //! \8d\84\91Ì
283 ////////////////////////////////////////////////////////////
284 //! \8c`\8fó
285 enum SHAPE_TYPE
286 {
287         //! \8b\85
288         SHAPE_SPHERE=0,
289         //! \94 
290         SHAPE_BOX,
291         //! \83J\83v\83Z\83\8b
292         SHAPE_CAPSULE,
293 };
294 //! \8d\84\91Ì\83^\83C\83v
295 enum PROCESS_TYPE
296 {
297         //! \83{\81[\83\93\82Æ\93¯\82\93®\82«
298         RIGIDBODY_KINEMATICS=0,
299         //! \95¨\97\9d\89\89\8eZ
300         RIGIDBODY_PHYSICS,
301         //! \95¨\97\9d\89\89\8eZ\8c\8b\89Ê\82ð\83{\81[\83\93\82É\94½\89f\82·\82é
302         RIGIDBODY_PHYSICS_WITH_BONE,
303 };
304
305 struct RigidBody
306 {
307         //! \8d\84\91Ì\96¼
308         char name[20];
309         //! \8aÖ\98A\83{\81[\83\93(\83{\81[\83\93\92Ç\8f]\82Æ\83{\81[\83\93\88Ê\92u\8d\87\82í\82¹\82Å\95K\97v)
310         unsigned short boneIndex;
311         //! \83O\83\8b\81[\83v
312         unsigned char group;
313         //! \94ñ\8fÕ\93Ë\83O\83\8b\81[\83v
314         unsigned short target;
315         //! \8c`\8fó
316         SHAPE_TYPE shapeType;
317         //! \83T\83C\83Y
318         float w;
319         float h;
320         float d;
321         //! \8ep\90¨
322         Vector3 position;
323         Vector3 rotation;
324         //! \8e¿\97Ê
325         float weight;
326         //! \95¨\97\9d\89\89\8eZ\83p\83\89\83\81\81[\83^(bullet)
327         float linearDamping;
328         float angularDamping;
329         float restitution;
330         float friction;
331         //! \8d\84\91Ì\83^\83C\83v
332         PROCESS_TYPE processType;
333 };
334
335 //! Joint(\95¨\97\9d\89\89\8eZ\82Å\82ÌJoint\82ÆConstraint\82Í\93¯\82\88Ó\96¡)
336 struct Constraint
337 {
338         //! Joint\96¼
339         char name[20];
340         //! \90Ú\91±\8d\84\91ÌA
341         unsigned int rigidA;
342         //! \90Ú\91±\8d\84\91ÌB
343         unsigned int rigidB;
344         //! \88Ê\92u
345         Vector3 pos;
346         //! \89ñ\93]
347         Vector3 rot;
348         //! \88Ú\93®\90§\8cÀ
349         Vector3 constraintPosMin;
350         Vector3 constraintPosMax;
351         //! \89ñ\93]\90§\8cÀ
352         Vector3 constraintRotMin;
353         Vector3 constraintRotMax;
354         //! \82Î\82Ë
355         Vector3 springPos;
356         Vector3 springRot;
357 };
358
359
360 ///////////////////////////////////////////////////////////////////////////////
361 // IO
362 ///////////////////////////////////////////////////////////////////////////////
363 struct IO
364 {
365         float version;
366         char name[20];
367         char comment[256];
368         std::vector<Vertex> vertices;
369         std::vector<unsigned short> indices;
370         std::vector<Material*> materials;
371         std::vector<Bone> bones;
372         std::vector<IK> ik_list;
373         std::vector<Morph> morph_list;
374         std::vector<unsigned short> face_list;
375         std::vector<BoneDisplayName> bone_display_name_list;
376         std::vector<std::pair<unsigned short, unsigned char> > bone_display_list;
377         ToonTexture toon_textures[10];
378         std::vector<RigidBody> rigidbodies;
379         std::vector<Constraint> constraints;
380
381         char english_name[20];
382         char english_comment[256];
383
384         IO();
385         ~IO();
386         bool read(binary::IReader &reader);
387         //bool read(const char *path);
388         bool read(const wchar_t *path);
389         bool write(binary::IWriter &writer);
390         bool write(const char *path);
391         bool write(const wchar_t *path);
392
393         const Vector2* getUV(int index)const;
394         std::wstring getName()const;
395         std::wstring getEnglishName()const;
396 };
397 inline std::ostream &operator<<(std::ostream &os, const IO &rhs)
398 {
399         os
400                 << "<PMD " << rhs.name << std::endl
401                 << rhs.comment << std::endl
402                 << "[vertices] " << rhs.vertices.size() << std::endl
403                 << "[indices] " << rhs.indices.size() << std::endl
404                 << "[materials] " << rhs.materials.size() << std::endl
405                 ;
406         /*
407         std::copy(rhs.materials.begin(), rhs.materials.end(), 
408                         std::ostream_iterator<Material>(os, ""));
409
410         os
411                 << "[bones] " << rhs.bones.size() << std::endl
412                 ;
413         std::copy(rhs.bones.begin(), rhs.bones.end(), 
414                         std::ostream_iterator<Bone>(os, ""));
415
416         os
417                 << "[ik] " << rhs.ik_list.size() << std::endl
418                 ;
419         std::copy(rhs.ik_list.begin(), rhs.ik_list.end(), 
420                         std::ostream_iterator<IK>(os, ""));
421
422         os
423                 << "[morph] " << rhs.morph_list.size() << std::endl
424                 ;
425         std::copy(rhs.morph_list.begin(), rhs.morph_list.end(), 
426                         std::ostream_iterator<Morph>(os, ""));
427                         */
428
429         os
430                 << ">" << std::endl
431                 ;
432         return os;
433 }
434
435
436 } // namespace pmd
437 } // namespace meshio
438
439 #endif // MESH_IO_PMD_H_INCLUDED