1 /***************************************************************************
2  * Brutal Chess
3  * http://brutalchess.sf.net
4  *
5  * File : md3model.h
6  * Authors : Mike Cook, Joe Flint, Neil Pankey
7  **************************************************************************/
8 
9 #ifndef MD3MODEL_H
10 #define MD3MODEL_H
11 
12 #include "texture.h"
13 #include "vector.h"
14 
15 #include <map>
16 #include <string>
17 #include <vector>
18 
19 /**
20  * Represents and loads an MD3 Model.
21  */
22 class MD3Model {
23 
24  public:
25 
26     /**
27      * Construct a useless empty MD3Model.
28      */
MD3Model()29     MD3Model() :
30         m_frame1(0), m_frame2(0), m_blend(0.0) {}
31 
32     /**
33      * Loads and initializes the MD3Model from file.
34      * @param filename - The file to load the model from.
35      * @param skin - The skin to use.
36      */
37     MD3Model(const std::string& filename, const std::string& skin = "default")
38 		: m_frame1(0), m_frame2(0), m_blend(0.0)
39 		{ load(filename, skin); }
40 
41     /**
42      * Loads the model from the specified file.
43      * @param filename - The file to load the model from.
44      * @param skin - The skin to use.
45      */
46     bool load(const std::string& filename, const std::string& skin = "default");
47 
48     /**
49      * Loads the textures for the model.
50      */
51     bool loadGL();
52 
53     /**
54      * Sets the current frame of the animation.
55      * @param frame1 - The frame to set the upper-body of the model to.
56      * @param frame2 - The frame to set the lower-body of the model to.
57      * @param blend - Alpha of the vertex to blend.
58      */
59     void setFrame(int frame1, int frame2 = 0, double blend = 1.0);
60 
61     /**
62      * Links the head, body, and legs of the model together.
63      */
64 	void link(MD3Model * m);
65 
66     /**
67      * Draws the MD3Model.
68      */
69     void draw();
70 
71     // For debugging.
72     void printInfo() const;
73     void printHeader() const;
74     void printFrames() const;
75     void printMeshes() const;
76 
77     friend class Q3CharModel;
78 
79  private:
80 
81 	int m_frame1, m_frame2;
82 	double m_blend;
83 
84 	std::string                 m_tagname;
85 	std::vector<MD3Model*>      m_links;
86 
87 	std::string     m_filename;
88 	std::string     m_skin;
89 
90 	struct md3Header_t {
91 		char 	ID[4];
92 		int 	version;
93 		char 	name[68];
94 		int 	frame_num;
95 		int		tag_num;
96 		int		mesh_num;
97 		int		max_skin_num;
98 		int		frame_start;
99 		int		tag_start;
100 		int		mesh_start;
101 		int		file_size;
102 	};
103 	md3Header_t	m_header;
104 
105 	struct md3Tag_t {
106 	   	char	name[64];   // Name of the tag
107 		float	origin[3];  // Relative Position of Tag
108 		float	axis[3][3]; // The direction the tag
109                             // is facing relative to the
110     };                      // rest of the model.
111 
112 	struct md3FrameHeader_t {
113 		float	min_bound[3];
114 		float	max_bound[3];
115 		float	origin[3];
116 		float	bound_radius;
117 		char	name[16];
118 	};
119 
120 	struct md3Frame_t {
121 		md3FrameHeader_t        header;
122 		std::vector<md3Tag_t>   tags;
123 	};
124     std::vector<md3Frame_t>     m_frames;
125 
126 	struct md3MeshHeader_t {
127 		char	id[4];
128 		char	name[68];
129 		int	frame_num;
130 		int	skin_num;
131 		int	vertex_num;
132 		int	triangle_num;
133 		int	triangle_start;
134 		int	skin_start;
135 		int	texcoord_start;
136 		int	vertex_start;
137 		int	mesh_size;
138 	};
139 
140 	struct md3Skin_t {
141 		char name[68];
142 	};
143 
144 	struct md3Triangle_t {
145 		int vertices[3];
146 	};
147 
148 	struct md3TexCoord_t {
149 		float coords[2];
150 	};
151 
152 	struct md3Vertex_t {
153 		signed short coords[3];
154 		unsigned char normals[2];
155 	};
156 
157 	struct md3Normal_t {
158 		float x,y,z;
159 	};
160 
161 	struct md3Mesh_t {
162 		md3MeshHeader_t	            header;
163 		std::vector<md3Skin_t>	    skins;
164 		std::vector<md3Triangle_t>  triangles;
165 		std::vector<md3TexCoord_t>  texcoords;
166 		std::vector<md3Vertex_t>    vertices;
167 		std::vector<md3Normal_t>    normals;
168 		std::string                 tex;
169 	};
170 	std::vector<md3Mesh_t> m_meshes;
171 
172 	std::map<std::string, Texture> m_textures;
173 
174 	md3Vertex_t blendVertex(md3Vertex_t & v1, md3Vertex_t & v2, double b);
175 };
176 
177 #endif
178 
179 // End of file md3model.h
180 
181