]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/md3model/md3.cpp
reformat code! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / plugins / md3model / md3.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #include "md3.h"
23
24 #include "ifilesystem.h"
25 #include "imodel.h"
26
27 #include "imagelib.h"
28 #include "bytestreamutils.h"
29
30 #include "model.h"
31 #include "ident.h"
32 #include "md3normals.h"
33
34 // the maximum size of game reletive pathnames
35 const int MAX_QPATH = 64;
36
37 /*
38    ========================================================================
39
40    .MD3 triangle model file format
41
42    ========================================================================
43  */
44
45 const unsigned char MD3_IDENT[4] = {'I', 'D', 'P', '3',};
46 const int MD3_VERSION = 15;
47
48 // limits
49 const int MD3_MAX_LODS = 4;
50 const int MD3_MAX_TRIANGLES = 8192;    // per surface
51 const int MD3_MAX_VERTS = 4096;    // per surface
52 const int MD3_MAX_SHADERS = 256;     // per surface
53 const int MD3_MAX_FRAMES = 1024;    // per model
54 const int MD3_MAX_SURFACES = 32;      // per model
55 const int MD3_MAX_TAGS = 16;      // per frame
56
57 // vertex scales
58 const float MD3_XYZ_SCALE = (1.f / 64);
59
60 typedef float float3[3];
61
62 void istream_read_float3(PointerInputStream &inputStream, float3 f)
63 {
64     f[0] = istream_read_float32_le(inputStream);
65     f[1] = istream_read_float32_le(inputStream);
66     f[2] = istream_read_float32_le(inputStream);
67 }
68
69 typedef struct md3Frame_s {
70     float3 bounds[2];
71     float3 localOrigin;
72     float radius;
73     char name[16];
74 } md3Frame_t;
75
76 void istream_read_md3Frame(PointerInputStream &inputStream, md3Frame_t &frame)
77 {
78     istream_read_float3(inputStream, frame.bounds[0]);
79     istream_read_float3(inputStream, frame.bounds[1]);
80     istream_read_float3(inputStream, frame.localOrigin);
81     frame.radius = istream_read_float32_le(inputStream);
82     inputStream.read(reinterpret_cast<unsigned char *>( frame.name ), 16);
83 }
84
85 typedef struct md3Tag_s {
86     char name[MAX_QPATH];           // tag name
87     float3 origin;
88     float3 axis[3];
89 } md3Tag_t;
90
91 void istream_read_md3Shader(PointerInputStream &inputStream, md3Tag_t &tag)
92 {
93     inputStream.read(reinterpret_cast<unsigned char *>( tag.name ), MAX_QPATH);
94     istream_read_float3(inputStream, tag.origin);
95     istream_read_float3(inputStream, tag.axis[0]);
96     istream_read_float3(inputStream, tag.axis[1]);
97     istream_read_float3(inputStream, tag.axis[2]);
98 }
99
100 /*
101 ** md3Surface_t
102 **
103 ** CHUNK                        SIZE
104 ** header                       sizeof( md3Surface_t )
105 ** shaders                      sizeof( md3Shader_t ) * numShaders
106 ** triangles[0]         sizeof( md3Triangle_t ) * numTriangles
107 ** st                           sizeof( md3St_t ) * numVerts
108 ** XyzNormals           sizeof( md3XyzNormal_t ) * numVerts * numFrames
109 */
110 typedef struct {
111     char ident[4];              //
112
113     char name[MAX_QPATH]; // polyset name
114
115     int flags;
116     int numFrames; // all surfaces in a model should have the same
117
118     int numShaders; // all surfaces in a model should have the same
119     int numVerts;
120
121     int numTriangles;
122     int ofsTriangles;
123
124     int ofsShaders; // offset from start of md3Surface_t
125     int ofsSt; // texture coords are common for all frames
126     int ofsXyzNormals; // numVerts * numFrames
127
128     int ofsEnd; // next surface follows
129 } md3Surface_t;
130
131 void istream_read_md3Surface(PointerInputStream &inputStream, md3Surface_t &surface)
132 {
133     inputStream.read(reinterpret_cast<unsigned char *>( surface.ident ), 4);
134     inputStream.read(reinterpret_cast<unsigned char *>( surface.name ), MAX_QPATH);
135     surface.flags = istream_read_int32_le(inputStream);
136     surface.numFrames = istream_read_int32_le(inputStream);
137     surface.numShaders = istream_read_int32_le(inputStream);
138     surface.numVerts = istream_read_int32_le(inputStream);
139     surface.numTriangles = istream_read_int32_le(inputStream);
140     surface.ofsTriangles = istream_read_int32_le(inputStream);
141     surface.ofsShaders = istream_read_int32_le(inputStream);
142     surface.ofsSt = istream_read_int32_le(inputStream);
143     surface.ofsXyzNormals = istream_read_int32_le(inputStream);
144     surface.ofsEnd = istream_read_int32_le(inputStream);
145 }
146
147 typedef struct {
148     char name[MAX_QPATH];
149     int shaderIndex;                // for in-game use
150 } md3Shader_t;
151
152 void istream_read_md3Shader(PointerInputStream &inputStream, md3Shader_t &shader)
153 {
154     inputStream.read(reinterpret_cast<unsigned char *>( shader.name ), MAX_QPATH);
155     shader.shaderIndex = istream_read_int32_le(inputStream);
156 }
157
158 typedef struct {
159     int indexes[3];
160 } md3Triangle_t;
161
162 void istream_read_md3Triangle(PointerInputStream &inputStream, md3Triangle_t &triangle)
163 {
164     triangle.indexes[0] = istream_read_int32_le(inputStream);
165     triangle.indexes[1] = istream_read_int32_le(inputStream);
166     triangle.indexes[2] = istream_read_int32_le(inputStream);
167 }
168
169 typedef struct {
170     float st[2];
171 } md3St_t;
172
173 void istream_read_md3St(PointerInputStream &inputStream, md3St_t &st)
174 {
175     st.st[0] = istream_read_float32_le(inputStream);
176     st.st[1] = istream_read_float32_le(inputStream);
177 }
178
179 typedef struct {
180     short xyz[3];
181     short normal;
182 } md3XyzNormal_t;
183
184 void istream_read_md3XyzNormal(PointerInputStream &inputStream, md3XyzNormal_t &xyz)
185 {
186     xyz.xyz[0] = istream_read_int16_le(inputStream);
187     xyz.xyz[1] = istream_read_int16_le(inputStream);
188     xyz.xyz[2] = istream_read_int16_le(inputStream);
189     xyz.normal = istream_read_int16_le(inputStream);
190 }
191
192 typedef struct {
193     char ident[4];
194     int version;
195
196     char name[MAX_QPATH];           // model name
197
198     int flags;
199
200     int numFrames;
201     int numTags;
202     int numSurfaces;
203
204     int numSkins;
205
206     int ofsFrames;                  // offset for first frame
207     int ofsTags;                    // numFrames * numTags
208     int ofsSurfaces;                // first surface, others follow
209
210     int ofsEnd;                     // end of file
211 } md3Header_t;
212
213 void istream_read_md3Header(PointerInputStream &inputStream, md3Header_t &header)
214 {
215     inputStream.read(reinterpret_cast<unsigned char *>( header.ident ), 4);
216     header.version = istream_read_int32_le(inputStream);
217     inputStream.read(reinterpret_cast<unsigned char *>( header.name ), MAX_QPATH);
218     header.flags = istream_read_int32_le(inputStream);
219     header.numFrames = istream_read_int32_le(inputStream);
220     header.numTags = istream_read_int32_le(inputStream);
221     header.numSurfaces = istream_read_int32_le(inputStream);
222     header.numSkins = istream_read_int32_le(inputStream);
223     header.ofsFrames = istream_read_int32_le(inputStream);
224     header.ofsTags = istream_read_int32_le(inputStream);
225     header.ofsSurfaces = istream_read_int32_le(inputStream);
226     header.ofsEnd = istream_read_int32_le(inputStream);
227 }
228
229 int MD3Surface_read(Surface &surface, unsigned char *buffer)
230 {
231     md3Surface_t md3Surface;
232     {
233         PointerInputStream inputStream(buffer);
234         istream_read_md3Surface(inputStream, md3Surface);
235     }
236
237     {
238         surface.vertices().reserve(md3Surface.numVerts);
239
240         PointerInputStream xyzNormalStream(buffer + md3Surface.ofsXyzNormals);
241         PointerInputStream stStream(buffer + md3Surface.ofsSt);
242
243         // read verts into vertex array - xyz, st, normal
244         for (int i = 0; i < md3Surface.numVerts; i++) {
245             md3XyzNormal_t md3Xyz;
246             istream_read_md3XyzNormal(xyzNormalStream, md3Xyz);
247
248             md3St_t md3St;
249             istream_read_md3St(stStream, md3St);
250
251             surface.vertices().push_back(
252                     ArbitraryMeshVertex(
253                             Vertex3f(md3Xyz.xyz[0] * MD3_XYZ_SCALE, md3Xyz.xyz[1] * MD3_XYZ_SCALE,
254                                      md3Xyz.xyz[2] * MD3_XYZ_SCALE),
255                             DecodeNormal(reinterpret_cast<byte *>( &md3Xyz.normal )),
256                             TexCoord2f(md3St.st[0], md3St.st[1])
257                     )
258             );
259         }
260     }
261
262     {
263         surface.indices().reserve(md3Surface.numTriangles * 3);
264
265         PointerInputStream inputStream(buffer + md3Surface.ofsTriangles);
266         for (int i = 0; i < md3Surface.numTriangles; i++) {
267             md3Triangle_t md3Triangle;
268             istream_read_md3Triangle(inputStream, md3Triangle);
269             surface.indices().insert(md3Triangle.indexes[0]);
270             surface.indices().insert(md3Triangle.indexes[1]);
271             surface.indices().insert(md3Triangle.indexes[2]);
272         }
273     }
274
275     {
276         md3Shader_t md3Shader;
277         {
278             PointerInputStream inputStream(buffer + md3Surface.ofsShaders);
279             istream_read_md3Shader(inputStream, md3Shader);
280         }
281         surface.setShader(md3Shader.name);
282     }
283
284     surface.updateAABB();
285
286     return md3Surface.ofsEnd;
287 }
288
289 void MD3Model_read(Model &model, unsigned char *buffer)
290 {
291     md3Header_t md3Header;
292     {
293         PointerInputStream inputStream(buffer);
294         istream_read_md3Header(inputStream, md3Header);
295     }
296
297     unsigned char *surfacePosition = buffer + md3Header.ofsSurfaces;
298
299     for (int i = 0; i != md3Header.numSurfaces; ++i) {
300         surfacePosition += MD3Surface_read(model.newSurface(), surfacePosition);
301     }
302
303     model.updateAABB();
304 }
305
306 scene::Node &MD3Model_new(unsigned char *buffer)
307 {
308     ModelNode *modelNode = new ModelNode();
309     MD3Model_read(modelNode->model(), buffer);
310     return modelNode->node();
311 }
312
313 scene::Node &MD3Model_default()
314 {
315     ModelNode *modelNode = new ModelNode();
316     Model_constructNull(modelNode->model());
317     return modelNode->node();
318 }
319
320 scene::Node &MD3Model_fromBuffer(unsigned char *buffer)
321 {
322     if (!ident_equal(buffer, MD3_IDENT)) {
323         globalErrorStream() << "MD3 read error: incorrect ident\n";
324         return MD3Model_default();
325     } else {
326         return MD3Model_new(buffer);
327     }
328 }
329
330 scene::Node &loadMD3Model(ArchiveFile &file)
331 {
332     ScopedArchiveBuffer buffer(file);
333     return MD3Model_fromBuffer(buffer.buffer);
334 }