1 /**
2  * @file
3  * @note This file must be identical in the quake and utils directories
4  * @brief Header for various formats like pak, and model formats as well as bsp format
5  * @note The .pk3 files are just zip files
6  * @note quake file formats: md2, md3, pk3, bsp
7  */
8 
9 /*
10 Copyright (C) 1997-2001 Id Software, Inc.
11 
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License
14 as published by the Free Software Foundation; either version 2
15 of the License, or (at your option) any later version.
16 
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 
21 See the GNU General Public License for more details.
22 
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 
27 */
28 
29 #pragma once
30 
31 #include "../shared/defines.h"
32 
33 #define IDMDXHEADER "UFOMDX"
34 #define MDX_VERSION		1
35 
36 /*========================================================================
37 .MD2 triangle model file format
38 ========================================================================*/
39 
40 #define IDALIASHEADER	(('2'<<24)+('P'<<16)+('D'<<8)+'I')
41 #define IDTAGHEADER		(('2'<<24)+('P'<<16)+('D'<<8)+'J')
42 #define MD2_ALIAS_VERSION	8
43 #define TAG_VERSION		1
44 
45 #define MD2_MAX_TRIANGLES	4096
46 #define MD2_MAX_VERTS		2048
47 #define MD2_MAX_FRAMES		1024
48 #define MD2_MAX_SKINS	32
49 #define MD2_MAX_SKINNAME	64
50 #define MD2_MAX_TAGNAME		64
51 
52 /**
53  * @brief These two shorts are used to map a vertex onto a skin
54  * The horizontal axis position is given by s, and the vertical axis position is given by t.
55  * The ranges for s and t are greater than or equal to 0 and less than skinWidth and skinHeight
56  */
57 typedef struct {
58 	uint16_t s; /**< (0 <= s < skinWidth) */
59 	uint16_t t; /**< (0 <= t < skinHeight) */
60 } dMD2Coord_t;
61 
62 typedef struct {
63 	uint16_t index_verts[3];	/**< these three shorts are indices into the array of vertices in each frames.
64 								 * In other words, the number of triangles in a md2 file is fixed, and each
65 								 * triangle is always made of the same three indices into each frame's array
66 								 * of vertices. So, in each frame, the triangles themselves stay intact, their
67 								 * vertices are just moved around
68 								 */
69 	uint16_t index_st[3];	/**< these three shorts are indices into the array of texture coordinates */
70 } dMD2Triangle_t;
71 
72 typedef struct {
73 	byte v[3];	/**< scaled byte to fit in frame mins/maxs
74 				 * The three bytes represent the x, y, and z coordinates of this vertex.
75 				 * This is not the "real" vertex coordinate. This is a scaled version of the
76 				 * coordinate, scaled so that each of the three numbers fit within one byte.
77 				 * To scale the vertex back to the "real" coordinate, you need to first
78 				 * multiply each of the bytes by their respective float scale in the dMD2Frame_t
79 				 * structure, and then add the respective float translation, also in the dMD2Frame_t
80 				 * structure. This will give you the vertex coordinate relative to the model's
81 				 * origin, which is at the origin, (0, 0, 0)
82 				 */
83 	byte unused;
84 } dMD2TriangleVertex_t;
85 
86 /**
87  * @brief is a variable sized structure, however all frame_t structures within the same file will
88  * have the same size (numVertices in the header)
89  */
90 typedef struct dAliasFrame_s {
91 	float scale[3];				/**< multiply byte verts by this */
92 	float translate[3];			/**< then add this */
93 	char name[16];				/**< frame name from grabbing */
94 	dMD2TriangleVertex_t verts[1];		/**< variable sized - an array of num_verts dMD2TriangleVertex_t structures.*/
95 } dMD2Frame_t;
96 
97 
98 /**
99  * @brief the glcmd format:
100  * a positive integer starts a tristrip command, followed by that many
101  * vertex structures.
102  * a negative integer starts a trifan command, followed by -x vertexes
103  * a zero indicates the end of the command list.
104  * a vertex consists of a floating point s, a floating point t,
105  * and an integer vertex index.
106  */
107 
108 /** @brief model file header structure - 68 bytes */
109 typedef struct {
110 	uint32_t ident;					/**< a "magic number" used to identify the file. The magic number is
111 									 * 844121161 in decimal (0x32504449 in hexadecimal). The magic number
112 									 * is equal to the int "IDP2" (id polygon 2), which is formed by
113 									 * ('I' + ('D' << 8) + ('P' << 16) + ('2' << 24))
114 									 */
115 	uint32_t version;				/**< version number of the file. Always 8 */
116 
117 	uint32_t skinwidth;				/**< width of the skin(s) in pixels */
118 	uint32_t skinheight;			/**< height of the skin(s) in pixels */
119 	uint32_t framesize;				/**< byte size of each frame */
120 
121 	uint32_t num_skins;				/**< Number of skins associated with this model */
122 	uint32_t num_verts;				/**< number of vertices */
123 	uint32_t num_st;				/**< number of texture coordinates - can be greater than num_verts */
124 	uint32_t num_tris;				/**< number of triangles in each frame. */
125 	uint32_t num_glcmds;			/**< dwords in strip/fan command list */
126 	uint32_t num_frames;			/**< number of frames for this model */
127 
128 	uint32_t ofs_skins;				/**< each skin is a MD2_MAX_SKINNAME string */
129 	uint32_t ofs_st;				/**< byte offset from start for stverts */
130 	uint32_t ofs_tris;				/**< offset for dtriangles */
131 	uint32_t ofs_frames;			/**< offset for first frame */
132 	uint32_t ofs_glcmds;			/**< offset to the gl command list */
133 	uint32_t ofs_end;				/**< end of file */
134 } dMD2Model_t;
135 
136 
137 /** @brief Tag file header structure - 32 byte
138  * A tag for a model must have the same amount of frames as the model. A tag is
139  * nothing more than a coordinate. It's used to e.g. determine the hand of a model when
140  * the walking animation is played. The renderer has to know where to place the weapon. */
141 typedef struct {
142 	uint32_t ident;					/**< 844121162 */
143 	uint32_t version;				/**< version of the tag file - @sa TAG_VERSION */
144 
145 	uint32_t num_tags;				/**< number of tags in this tag file */
146 	uint32_t num_frames;			/**< number of frames in this tag file - should be the same as in your
147 									 * exported model that uses this tag file */
148 
149 	uint32_t ofs_names;
150 	uint32_t ofs_tags;
151 	uint32_t ofs_end;
152 	uint32_t ofs_extractend;
153 } dMD2tag_t;
154 
155 /*========================================================================
156 .MD3 model file format
157 ========================================================================*/
158 
159 #define IDMD3HEADER		(('3'<<24)+('P'<<16)+('D'<<8)+'I')
160 
161 #define MD3_ALIAS_VERSION	15
162 #define MD3_ALIAS_MAX_LODS	4
163 
164 #define	MD3_MAX_TRIANGLES	8192	/* per mesh */
165 #define MD3_MAX_VERTS		4096	/* per mesh */
166 #define MD3_MAX_SHADERS		256	/* per mesh */
167 #define MD3_MAX_FRAMES		1024	/* per model */
168 #define	MD3_MAX_MESHES		32	/* per model */
169 #define MD3_MAX_TAGS		16	/* per frame */
170 #define MD3_MAX_PATH		64
171 
172 /** vertex scales */
173 #define	MD3_XYZ_SCALE		(1.0f/64.0f)
174 
175 typedef struct {
176 	float st[2];
177 } dmd3coord_t;
178 
179 typedef struct {
180 	int16_t point[3];
181 	int16_t norm;
182 } dmd3vertex_t;
183 
184 typedef struct {
185 	float mins[3];
186 	float maxs[3];
187 	float translate[3];
188 	float radius;
189 	char creator[16];
190 } dmd3frame_t;
191 
192 typedef struct {
193 	float origin[3];
194 	float axis[3][3];
195 } dorientation_t;
196 
197 typedef struct {
198 	char name[MD3_MAX_PATH];		/**< tag name */
199 	dorientation_t orient;
200 } dmd3tag_t;
201 
202 typedef struct {
203 	char name[MD3_MAX_PATH];
204 	uint32_t unused;					/**< shader */
205 } dmd3skin_t;
206 
207 typedef struct {
208 	char id[4];
209 
210 	char name[MD3_MAX_PATH];
211 
212 	uint32_t flags;
213 
214 	uint32_t num_frames;
215 	uint32_t num_skins;
216 	uint32_t num_verts;
217 	uint32_t num_tris;
218 
219 	uint32_t ofs_tris;
220 	uint32_t ofs_skins;
221 	uint32_t ofs_tcs;
222 	uint32_t ofs_verts;
223 
224 	uint32_t meshsize;
225 } dmd3mesh_t;
226 
227 typedef struct {
228 	uint32_t id;
229 	uint32_t version;
230 
231 	char filename[MD3_MAX_PATH];
232 
233 	uint32_t flags;
234 
235 	uint32_t num_frames;
236 	uint32_t num_tags;
237 	uint32_t num_meshes;
238 	uint32_t num_skins;
239 
240 	uint32_t ofs_frames;
241 	uint32_t ofs_tags;
242 	uint32_t ofs_meshes;
243 	uint32_t ofs_end;
244 } dmd3_t;
245 
246 /*==============================================================================
247 .BSP file format
248 ==============================================================================*/
249 
250 /** little-endian "IBSP" */
251 #define IDBSPHEADER	(('P'<<24)+('S'<<16)+('B'<<8)+'I')
252 
253 #define BSPVERSION	78
254 
255 /** @brief Directory of the different data blocks */
256 typedef struct {
257 	uint32_t fileofs;	/**< the offset of the data block (from the start of the file) */
258 	uint32_t filelen;	/**< the length of the data block */
259 } lump_t;
260 
261 /** @brief The BSP header definition with the data block directory */
262 typedef struct {
263 	uint32_t ident;		/**< the magic number of the BSP file @sa IDBSPHEADER */
264 	uint32_t version;	/**< the version of the BSP file @sa BSPVERSION */
265 	lump_t lumps[HEADER_LUMPS];	/**< the data directories */
266 } dBspHeader_t;
267 
268 
269 #define BSP_SwapHeader(header, name) { \
270 	unsigned int i; \
271 	(header)->ident = LittleLong((header)->ident); \
272 	(header)->version = LittleLong((header)->version); \
273 	for (i = 0; i < HEADER_LUMPS; i++) { \
274 		lump_t* l = &(header)->lumps[i]; \
275 		l->filelen = LittleLong(l->filelen); \
276 		l->fileofs = LittleLong(l->fileofs); \
277 		if (l->fileofs == (uint32_t) -1) \
278 			Sys_Error("Invalid bsp header found (lump overflow %i): '%s'", i, (name)); \
279 	} \
280 }
281