OSDN Git Service

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