OSDN Git Service

modify for Linux.
[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 #include <algorithm>
22
23 namespace meshio {
24 namespace vmd {
25
26 typedef ::meshio::la::Vector2 Vector2;
27 typedef ::meshio::la::Vector3 Vector3;
28 typedef ::meshio::la::Quaternion Quaternion;
29 typedef ::meshio::color::fRGB fRGB;
30
31 ////////////////////////////////////////////////////////////
32 //! \83\82\81[\83V\83\87\83\93
33 ////////////////////////////////////////////////////////////
34 struct BoneKey
35 {
36         Vector3 pos;
37         Quaternion q;
38         char interpolationX[16];
39         char interpolationY[16];
40         char interpolationZ[16];
41         char interpolationRot[16];
42 };
43 inline std::ostream& operator<<(std::ostream &os, const BoneKey &rhs)
44 {
45         return os
46                 << "<BoneKey: " << rhs.pos << rhs.q << ">" 
47                 ;
48 }
49
50 ////////////////////////////////////////////////////////////
51 //! \95\\8fî
52 ////////////////////////////////////////////////////////////
53 struct MorphKey
54 {
55         float weight;
56 };
57 inline std::ostream& operator<<(std::ostream &os, const MorphKey &rhs)
58 {
59         return os
60                 << "<MorphKey: " << rhs.weight << ">" 
61                 ;
62 }
63
64
65 ////////////////////////////////////////////////////////////
66 //! \83J\83\81\83\89
67 ////////////////////////////////////////////////////////////
68 struct CameraKey
69 {
70         //! \89½\82Ì\8b\97\97£\81H
71         float length;
72         //! \88Ê\92u
73         Vector3 pos;
74         //! \83I\83C\83\89\81[\8ap
75         Vector3 euler;
76         //! \95â\8aÔ
77         char interpolation[24];
78         //! \8e\8b\96ì\8ap
79         unsigned short viewAngle;
80         //! \89\93\8bß\8a´ 0\82ªon
81         unsigned char perspective;
82 };
83 inline std::ostream& operator<<(std::ostream &os, const CameraKey &rhs)
84 {
85         return os
86                 << "<CameraKey: " << rhs.length 
87                 << rhs.pos << rhs.euler << ">" 
88                 ;
89 }
90
91
92 ////////////////////////////////////////////////////////////
93 //! \8cõ\8c¹
94 ////////////////////////////////////////////////////////////
95 struct LightKey
96 {
97         fRGB color;
98         Vector3 pos;
99 };
100 inline std::ostream& operator<<(std::ostream &os, const LightKey &rhs)
101 {
102         return os
103                 << "<LightKey: " << rhs.color << rhs.pos << ">" 
104                 ;
105 }
106
107
108 ////////////////////////////////////////////////////////////
109 //! \83Z\83\8b\83t\83V\83\83\83h\83E
110 ////////////////////////////////////////////////////////////
111 struct SelfShadowKey
112 {
113 };
114
115 ////////////////////////////////////////////////////////////
116 //! KeyFrame
117 ////////////////////////////////////////////////////////////
118 template<typename T>
119 struct KeyFrame
120 {
121         typedef T VALUE_TYPE;
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
132 ////////////////////////////////////////////////////////////
133 //! 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
134 //! \93Ç\82Ý\8d\9e\82ñ\82¾\8cã\82Å\83t\83\8c\81[\83\80\94Ô\8d\86\82Å\83\\81[\83g\82·\82é\81B
135 ////////////////////////////////////////////////////////////
136 template<typename T>
137 struct KeyFrameList
138 {
139         typedef T KEYFRAME_TYPE;
140
141         std::vector<KEYFRAME_TYPE> list;
142         void sort(){ std::sort(list.begin(), list.end()); }
143     KEYFRAME_TYPE& push(unsigned int frame)
144         {
145                 list.push_back(KEYFRAME_TYPE());
146                 KEYFRAME_TYPE &keyFrame=list.back();
147                 keyFrame.frame=frame;
148                 return keyFrame;
149         }
150         unsigned int getFrame(int index)
151         {
152                 return list[index].frame;
153         }
154         typename KEYFRAME_TYPE::VALUE_TYPE* getKey(int index)
155         {
156                 return &list[index].key;
157         }
158 };
159
160 ////////////////////////////////////////////////////////////
161 //! IO
162 ////////////////////////////////////////////////////////////
163 typedef KeyFrame<BoneKey> BoneKeyFrame;
164 typedef KeyFrameList<BoneKeyFrame> BoneKeyFrameList;
165
166 typedef KeyFrame<MorphKey> MorphKeyFrame;
167 typedef KeyFrameList<MorphKeyFrame> MorphKeyFrameList;
168
169 struct IO
170 {
171         std::string version;
172         char name[20];
173
174         //! \83\82\81[\83V\83\87\83\93
175         typedef std::map<std::wstring, BoneKeyFrameList*> BoneMap;
176         BoneMap boneMap;
177         std::vector<std::wstring> boneKeys;
178
179         //! \95\\8fî
180         typedef std::map<std::wstring, MorphKeyFrameList*> MorphMap;
181         MorphMap morphMap;
182         std::vector<std::wstring> morphKeys;
183
184         IO();
185         ~IO();
186         bool read(binary::IReader &reader);
187         bool read(const wchar_t *path);
188         bool read(const char *path);
189         bool write(std::ostream &os);
190
191         BoneKeyFrameList* getBoneKeyFrameList(const std::wstring &name);
192         MorphKeyFrameList* getMorphKeyFrameList(const std::wstring &name);
193 };
194 inline std::ostream& operator<<(std::ostream &os, const IO &rhs)
195 {
196         os
197                 << "<VMD " << rhs.name << std::endl
198                 << "[bones] " << rhs.boneMap.size() << std::endl
199                 << "[morphs] " << rhs.morphMap.size() << std::endl
200                 << ">"
201                 ;
202         return os;
203 }
204
205 } // namespace vmd
206 } // namespace meshio
207
208 #endif // MESH_IO_VMD_H_INCLUDED