OSDN Git Service

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