2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
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.
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.
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
24 #include "ifilesystem.h"
28 #include "bytestreamutils.h"
32 #include "md3normals.h"
34 // the maximum size of game reletive pathnames
35 const int MAX_QPATH = 64;
38 ========================================================================
40 .MD3 triangle model file format
42 ========================================================================
45 const unsigned char MD3_IDENT[4] = {'I', 'D', 'P', '3',};
46 const int MD3_VERSION = 15;
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
58 const float MD3_XYZ_SCALE = (1.f / 64);
60 typedef float float3[3];
62 void istream_read_float3(PointerInputStream &inputStream, float3 f)
64 f[0] = istream_read_float32_le(inputStream);
65 f[1] = istream_read_float32_le(inputStream);
66 f[2] = istream_read_float32_le(inputStream);
69 typedef struct md3Frame_s {
76 void istream_read_md3Frame(PointerInputStream &inputStream, md3Frame_t &frame)
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);
85 typedef struct md3Tag_s {
86 char name[MAX_QPATH]; // tag name
91 void istream_read_md3Shader(PointerInputStream &inputStream, md3Tag_t &tag)
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]);
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
113 char name[MAX_QPATH]; // polyset name
116 int numFrames; // all surfaces in a model should have the same
118 int numShaders; // all surfaces in a model should have the same
124 int ofsShaders; // offset from start of md3Surface_t
125 int ofsSt; // texture coords are common for all frames
126 int ofsXyzNormals; // numVerts * numFrames
128 int ofsEnd; // next surface follows
131 void istream_read_md3Surface(PointerInputStream &inputStream, md3Surface_t &surface)
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);
148 char name[MAX_QPATH];
149 int shaderIndex; // for in-game use
152 void istream_read_md3Shader(PointerInputStream &inputStream, md3Shader_t &shader)
154 inputStream.read(reinterpret_cast<unsigned char *>( shader.name ), MAX_QPATH);
155 shader.shaderIndex = istream_read_int32_le(inputStream);
162 void istream_read_md3Triangle(PointerInputStream &inputStream, md3Triangle_t &triangle)
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);
173 void istream_read_md3St(PointerInputStream &inputStream, md3St_t &st)
175 st.st[0] = istream_read_float32_le(inputStream);
176 st.st[1] = istream_read_float32_le(inputStream);
184 void istream_read_md3XyzNormal(PointerInputStream &inputStream, md3XyzNormal_t &xyz)
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);
196 char name[MAX_QPATH]; // model name
206 int ofsFrames; // offset for first frame
207 int ofsTags; // numFrames * numTags
208 int ofsSurfaces; // first surface, others follow
210 int ofsEnd; // end of file
213 void istream_read_md3Header(PointerInputStream &inputStream, md3Header_t &header)
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);
229 int MD3Surface_read(Surface &surface, unsigned char *buffer)
231 md3Surface_t md3Surface;
233 PointerInputStream inputStream(buffer);
234 istream_read_md3Surface(inputStream, md3Surface);
238 surface.vertices().reserve(md3Surface.numVerts);
240 PointerInputStream xyzNormalStream(buffer + md3Surface.ofsXyzNormals);
241 PointerInputStream stStream(buffer + md3Surface.ofsSt);
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);
249 istream_read_md3St(stStream, md3St);
251 surface.vertices().push_back(
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])
263 surface.indices().reserve(md3Surface.numTriangles * 3);
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]);
276 md3Shader_t md3Shader;
278 PointerInputStream inputStream(buffer + md3Surface.ofsShaders);
279 istream_read_md3Shader(inputStream, md3Shader);
281 surface.setShader(md3Shader.name);
284 surface.updateAABB();
286 return md3Surface.ofsEnd;
289 void MD3Model_read(Model &model, unsigned char *buffer)
291 md3Header_t md3Header;
293 PointerInputStream inputStream(buffer);
294 istream_read_md3Header(inputStream, md3Header);
297 unsigned char *surfacePosition = buffer + md3Header.ofsSurfaces;
299 for (int i = 0; i != md3Header.numSurfaces; ++i) {
300 surfacePosition += MD3Surface_read(model.newSurface(), surfacePosition);
306 scene::Node &MD3Model_new(unsigned char *buffer)
308 ModelNode *modelNode = new ModelNode();
309 MD3Model_read(modelNode->model(), buffer);
310 return modelNode->node();
313 scene::Node &MD3Model_default()
315 ModelNode *modelNode = new ModelNode();
316 Model_constructNull(modelNode->model());
317 return modelNode->node();
320 scene::Node &MD3Model_fromBuffer(unsigned char *buffer)
322 if (!ident_equal(buffer, MD3_IDENT)) {
323 globalErrorStream() << "MD3 read error: incorrect ident\n";
324 return MD3Model_default();
326 return MD3Model_new(buffer);
330 scene::Node &loadMD3Model(ArchiveFile &file)
332 ScopedArchiveBuffer buffer(file);
333 return MD3Model_fromBuffer(buffer.buffer);