OSDN Git Service

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