OSDN Git Service

cd317a6ba44adb02dbb516a5bdd1e453e4ffa296
[meshio/meshio.git] / include / vmd.h
1 /**
2  * VMD\8c`\8e®
3  * MMD\82Ì\83\82\81[\83V\83\87\83\93\83f\81[\83^\81B
4  * \83o\83C\83i\83\8a\8c`\8e®\82Å\83{\81[\83\93\83\82\81[\83V\83\87\83\93\81A\95\\8fî\83\82\81[\83t\83B\83\93\83O\81A\8cõ\8c¹\81A\89e\82Ì\83L\81[\83t\83\8c\81[\83\80\82ª
5  * \8bL\98^\82³\82ê\82é\81B
6  *
7  * \8eQ\8dl\83T\83C\83g
8  * http://blog.goo.ne.jp/torisu_tetosuki/e/bc9f1c4d597341b394bd02b64597499d
9  * http://atupdate.web.fc2.com/vmd_format.htm
10  */
11
12 #ifndef MESH_IO_VMD_H_INCLUDED
13 #define MESH_IO_VMD_H_INCLUDED 
14
15 #include <ostream>
16 #include <vector>
17 #include <map>
18 #include "la.h"
19 #include "color.h"
20 #include "binary.h"
21
22 namespace meshio {
23 namespace vmd {
24
25 typedef la::Vector2 Vector2;
26 typedef la::Vector3 Vector3;
27 typedef la::Quaternion Quaternion;
28 typedef color::fRGB fRGB;
29
30 ////////////////////////////////////////////////////////////
31 //! \83\82\81[\83V\83\87\83\93
32 ////////////////////////////////////////////////////////////
33 struct BoneKey
34 {
35         Vector3 pos;
36         Quaternion q;
37         char interpolationX[16];
38         char interpolationY[16];
39         char interpolationZ[16];
40         char interpolationRot[16];
41 };
42 inline std::ostream& operator<<(std::ostream &os, const BoneKey &rhs)
43 {
44         return os
45                 << "<BoneKey: " << rhs.pos << rhs.q << ">" 
46                 ;
47 }
48
49 ////////////////////////////////////////////////////////////
50 //! \95\\8fî
51 ////////////////////////////////////////////////////////////
52 struct MorphKey
53 {
54         float weight;
55 };
56 inline std::ostream& operator<<(std::ostream &os, const MorphKey &rhs)
57 {
58         return os
59                 << "<MorphKey: " << rhs.weight << ">" 
60                 ;
61 }
62
63
64 ////////////////////////////////////////////////////////////
65 //! \83J\83\81\83\89
66 ////////////////////////////////////////////////////////////
67 struct CameraKey
68 {
69         //! \89½\82Ì\8b\97\97£\81H
70         float length;
71         //! \88Ê\92u
72         Vector3 pos;
73         //! \83I\83C\83\89\81[\8ap
74         Vector3 euler;
75         //! \95â\8aÔ
76         char interpolation[24];
77         //! \8e\8b\96ì\8ap
78         unsigned short viewAngle;
79         //! \89\93\8bß\8a´ 0\82ªon
80         unsigned char perspective;
81 };
82 inline std::ostream& operator<<(std::ostream &os, const CameraKey &rhs)
83 {
84         return os
85                 << "<CameraKey: " << rhs.length 
86                 << rhs.pos << rhs.euler << ">" 
87                 ;
88 }
89
90
91 ////////////////////////////////////////////////////////////
92 //! \8cõ\8c¹
93 ////////////////////////////////////////////////////////////
94 struct LightKey
95 {
96         fRGB color;
97         Vector3 pos;
98 };
99 inline std::ostream& operator<<(std::ostream &os, const LightKey &rhs)
100 {
101         return os
102                 << "<LightKey: " << rhs.color << rhs.pos << ">" 
103                 ;
104 }
105
106
107 ////////////////////////////////////////////////////////////
108 //! \83Z\83\8b\83t\83V\83\83\83h\83E
109 ////////////////////////////////////////////////////////////
110 struct SelfShadowKey
111 {
112 };
113
114 ////////////////////////////////////////////////////////////
115 //! 1\83`\83\83\83\93\83l\83\8b\95ª\82Ì\83L\81[\83t\83\8c\81[\83\80\82Ì\83\8a\83X\83g\81B
116 //! \93Ç\82Ý\8d\9e\82ñ\82¾\8cã\82Å\83t\83\8c\81[\83\80\94Ô\8d\86\82Å\83\\81[\83g\82·\82é\81B
117 ////////////////////////////////////////////////////////////
118 template<typename T>
119 struct KeyFrameList
120 {
121         struct KeyFrame
122         {
123                 //! \83t\83\8c\81[\83\80\94Ô\8d\86
124                 unsigned int frame;
125                 //! \83L\81[
126                 T key;
127
128                 //! \83t\83\8c\81[\83\80\94Ô\8d\86\82Å\83L\81[\82ð\83\\81[\83g\82·\82é
129                 bool operator<(const KeyFrame &rhs)const{ return frame<rhs.frame; }
130         };
131         std::vector<KeyFrame> list;
132         void sort(){ std::sort(list.begin(), list.end()); }
133         KeyFrame& push(unsigned int frame)
134         {
135                 list.push_back(KeyFrame());
136                 KeyFrame &keyFrame=list.back();
137                 keyFrame.frame=frame;
138                 return keyFrame;
139         }
140 };
141
142 ////////////////////////////////////////////////////////////
143 //! IO
144 ////////////////////////////////////////////////////////////
145 struct IO
146 {
147         std::string version;
148         char name[20];
149
150         //! \83\82\81[\83V\83\87\83\93
151         typedef std::map<std::string, KeyFrameList<BoneKey> > BoneMap;
152         BoneMap boneMap;
153         //! \95\\8fî
154         typedef std::map<std::string, KeyFrameList<MorphKey> > MorphMap;
155         MorphMap morphMap;
156         //! \83J\83\81\83\89\83\82\81[\83V\83\87\83\93
157         typedef std::map<std::string, KeyFrameList<CameraKey> > CameraMap;
158         CameraMap cameraKey;
159         //! \8fÆ\96¾
160         typedef std::map<std::string, KeyFrameList<LightKey> > LightMap;
161         LightMap lightMap;
162         //! \83Z\83\8b\83t\83V\83\83\83h\81[
163         typedef std::map<std::string, KeyFrameList<SelfShadowKey> > SelfShadowMap;
164         SelfShadowMap selfShadowMap;
165
166         IO();
167         bool read(binary::IReader &reader);
168         bool read(const char *path);
169         bool write(std::ostream &os);
170 };
171 inline std::ostream& operator<<(std::ostream &os, const IO &rhs)
172 {
173         os
174                 << "<VMD " << rhs.name << std::endl
175                 << "[bones] " << rhs.boneMap.size() << std::endl
176                 << "[morphs] " << rhs.morphMap.size() << std::endl
177                 << ">"
178                 ;
179         return os;
180 }
181
182 } // namespace vmd
183 } // namespace meshio
184
185 #endif // MESH_IO_VMD_H_INCLUDED