OSDN Git Service

840a675c47adbfa149269e2e84b31d0f8ae54d3a
[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 la::Vector2 Vector2;
44 typedef la::Vector3 Vector3;
45 typedef la::Vector4 Vector4;
46 typedef color::fRGBA fRGBA;
47 typedef 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 inline std::ostream &operator<<(std::ostream &os,
108                 const Material &rhs)
109 {
110         os
111                 << "[Material"
112                 << " diffuse:" << rhs.diffuse
113                 << " toon_index:" << (int)rhs.toon_index
114                 << " flag:" << (int)rhs.flag
115                 << " vertex_count:" << rhs.vertex_count
116                 << " texture:" << rhs.texture
117                 << "]"
118                 ;
119         return os;
120 }
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         char name[20];
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         Vector3 pos;
163 };
164 inline std::ostream &operator<<(std::ostream &os,
165                 const Bone &rhs)
166 {
167         os
168                 << "[Bone "
169                 << '"' << rhs.name << '"'
170                 << "]"
171                 ;
172         return os;
173 }
174
175 ////////////////////////////////////////////////////////////
176 //! IK
177 ////////////////////////////////////////////////////////////
178 struct IK
179 {
180         //! IK(IK\83^\81[\83Q\83b\83g)
181         unsigned short index;
182         //! Target(\83G\83t\83F\83N\83^\81[)
183         unsigned short target;
184         //! \83G\83t\83F\83N\83^\82É\98A\93®\82·\82é\83{\81[\83\93\90\94
185         unsigned char length;
186         //! IK\92l1\81BCCD-IK\8e\8e\8ds\89ñ\90\94
187         unsigned short iterations;
188         //! IK\92l2\81BCCD-IK\8e\8e\8ds\88ê\89ñ\95Ó\82è\82Ì\89e\8b¿\93x
189         float weight;
190         //! \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é)
191         std::vector<unsigned short> children;
192 };
193 inline std::ostream &operator<<(std::ostream &os, const IK &rhs)
194 {
195         os
196                 << "[IK "
197                 << "]"
198                 ;
199         return os;
200 }
201
202 ////////////////////////////////////////////////////////////
203 //! \95\\8fî
204 ////////////////////////////////////////////////////////////
205 //! \95\\8fî\82Ì\8eí\97Þ
206 enum MORPH_TYPE
207 {
208         //! \83x\81[\83X\95\\8fî
209         MORPH_BASE=0,
210         //! \82Ü\82ä
211         MORPH_MAYU,
212         //! \96Ú
213         MORPH_ME,
214         //! \83\8a\83b\83v
215         MORPH_LIP,
216         //! \82»\82Ì\91¼
217         MORPH_OTHER,
218 };
219 struct Morph
220 {
221         //! \95\\8fî\96¼
222         char name[20];
223         //! \8eg\97p\82·\82é\92¸\93_\90\94
224         unsigned int vertex_count;
225         //! \95ª\97Þ
226         unsigned char type;
227         //! \92¸\93_Index
228         std::vector<unsigned int> indices;
229         //! \88Ú\93®\97Ê
230         std::vector<Vector3> pos_list;
231 };
232 inline std::ostream &operator<<(std::ostream &os, const Morph &rhs)
233 {
234         os
235                 << "[Morph "
236                 << '"' << rhs.name << '"'
237                 << "]"
238                 ;
239         return os;
240 }
241
242 ////////////////////////////////////////////////////////////
243 //! \8d\84\91Ì
244 ////////////////////////////////////////////////////////////
245 //! \8c`\8fó
246 enum SHAPE_TYPE
247 {
248         //! \8b\85
249         SHAPE_SPHERE=0,
250         //! \94 
251         SHAPE_BOX,
252         //! \83J\83v\83Z\83\8b
253         SHAPE_CAPSULE,
254 };
255 //! \8d\84\91Ì\83^\83C\83v
256 enum PROCESS_TYPE
257 {
258         //! \83{\81[\83\93\82Æ\93¯\82\93®\82«
259         RIGIDBODY_KINEMATICS=0,
260         //! \95¨\97\9d\89\89\8eZ
261         RIGIDBODY_PHYSICS,
262         //! \95¨\97\9d\89\89\8eZ\8c\8b\89Ê\82ð\83{\81[\83\93\82É\94½\89f\82·\82é
263         RIGIDBODY_PHYSICS_WITH_BONE,
264 };
265
266 struct RigidBody
267 {
268         //! \8d\84\91Ì\96¼
269         char name[20];
270         //! \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)
271         unsigned short boneIndex;
272         //! \83O\83\8b\81[\83v
273         unsigned char group;
274         //! \94ñ\8fÕ\93Ë\83O\83\8b\81[\83v
275         unsigned short target;
276         //! \8c`\8fó
277         SHAPE_TYPE shapeType;
278         //! \83T\83C\83Y
279         float w;
280         float h;
281         float d;
282         //! \8ep\90¨
283         Vector3 position;
284         Vector3 rotation;
285         //! \8e¿\97Ê
286         float weight;
287         //! \95¨\97\9d\89\89\8eZ\83p\83\89\83\81\81[\83^(bullet)
288         float linearDamping;
289         float angularDamping;
290         float restitution;
291         float friction;
292         //! \8d\84\91Ì\83^\83C\83v
293         PROCESS_TYPE processType;
294 };
295
296 //! Joint(\95¨\97\9d\89\89\8eZ\82Å\82ÌJoint\82ÆConstraint\82Í\93¯\82\88Ó\96¡)
297 struct Constraint
298 {
299         //! Joint\96¼
300         char name[20];
301         //! \90Ú\91±\8d\84\91ÌA
302         unsigned int rigidA;
303         //! \90Ú\91±\8d\84\91ÌB
304         unsigned int rigidB;
305         //! \88Ê\92u
306         Vector3 pos;
307         //! \89ñ\93]
308         Vector3 rot;
309         //! \88Ú\93®\90§\8cÀ
310         Vector3 constraintPosMin;
311         Vector3 constraintPosMax;
312         //! \89ñ\93]\90§\8cÀ
313         Vector3 constraintRotMin;
314         Vector3 constraintRotMax;
315         //! \82Î\82Ë
316         Vector3 springPos;
317         Vector3 springRot;
318 };
319
320
321 ///////////////////////////////////////////////////////////////////////////////
322 // IO
323 ///////////////////////////////////////////////////////////////////////////////
324 struct IO
325 {
326         float version;
327         char name[20];
328         char comment[256];
329         std::vector<Vertex> vertices;
330         std::vector<unsigned short> indices;
331         std::vector<Material> materials;
332         std::vector<Bone> bones;
333         std::vector<IK> ik_list;
334         std::vector<Morph> morph_list;
335         std::vector<std::pair<unsigned short, unsigned char> > bone_list;
336         std::vector<std::string> bone_name_list;
337         std::vector<RigidBody> rigidbodies;
338         std::vector<Constraint> constraints;
339
340         IO();
341         bool read(binary::IReader &reader);
342         bool read(const char *path);
343         bool write(std::ostream &os);
344 };
345 inline std::ostream &operator<<(std::ostream &os, const IO &rhs)
346 {
347         os
348                 << "<PMD " << rhs.name << std::endl
349                 << rhs.comment << std::endl
350                 << "[vertices] " << rhs.vertices.size() << std::endl
351                 << "[indices] " << rhs.indices.size() << std::endl
352                 << "[materials] " << rhs.materials.size() << std::endl
353                 ;
354         /*
355         std::copy(rhs.materials.begin(), rhs.materials.end(), 
356                         std::ostream_iterator<Material>(os, ""));
357
358         os
359                 << "[bones] " << rhs.bones.size() << std::endl
360                 ;
361         std::copy(rhs.bones.begin(), rhs.bones.end(), 
362                         std::ostream_iterator<Bone>(os, ""));
363
364         os
365                 << "[ik] " << rhs.ik_list.size() << std::endl
366                 ;
367         std::copy(rhs.ik_list.begin(), rhs.ik_list.end(), 
368                         std::ostream_iterator<IK>(os, ""));
369
370         os
371                 << "[morph] " << rhs.morph_list.size() << std::endl
372                 ;
373         std::copy(rhs.morph_list.begin(), rhs.morph_list.end(), 
374                         std::ostream_iterator<Morph>(os, ""));
375                         */
376
377         os
378                 << ">" << std::endl
379                 ;
380         return os;
381 }
382
383
384 } // namespace pmd
385 } // namespace meshio
386
387 #endif // MESH_IO_PMD_H_INCLUDED