]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/md3model/md2.cpp
reformat code! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / plugins / md3model / md2.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 "md2.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 "mdlnormals.h"
33
34 typedef unsigned char byte;
35
36 /*
37    ========================================================================
38
39    .MD2 triangle model file format
40
41    ========================================================================
42  */
43 const int MD2_NUMVERTEXNORMALS = 162;
44
45 const unsigned char MD2_IDENT[4] = {'I', 'D', 'P', '2',};
46 const int MD2_VERSION = 8;
47
48 const int MD2_MAX_TRIANGLES = 4096;
49 const int MD2_MAX_VERTS = 2048;
50 const int MD2_MAX_FRAMES = 512;
51 const int MD2_MAX_MD2SKINS = 32;
52 const int MD2_MAX_SKINNAME = 64;
53
54 typedef struct {
55     short s;
56     short t;
57 } md2St_t;
58
59 void istream_read_md2St(PointerInputStream &inputStream, md2St_t &st)
60 {
61     st.s = istream_read_int16_le(inputStream);
62     st.t = istream_read_int16_le(inputStream);
63 }
64
65 typedef struct {
66     short index_xyz[3];
67     short index_st[3];
68 } md2Triangle_t;
69
70 void istream_read_md2Triangle(PointerInputStream &inputStream, md2Triangle_t &triangle)
71 {
72     triangle.index_xyz[0] = istream_read_int16_le(inputStream);
73     triangle.index_xyz[1] = istream_read_int16_le(inputStream);
74     triangle.index_xyz[2] = istream_read_int16_le(inputStream);
75     triangle.index_st[0] = istream_read_int16_le(inputStream);
76     triangle.index_st[1] = istream_read_int16_le(inputStream);
77     triangle.index_st[2] = istream_read_int16_le(inputStream);
78 }
79
80 typedef struct {
81     byte v[3];              // scaled byte to fit in frame mins/maxs
82     byte lightnormalindex;
83 } md2XyzNormal_t;
84
85 void istream_read_md2XyzNormal(PointerInputStream &inputStream, md2XyzNormal_t &xyz)
86 {
87     inputStream.read(xyz.v, 3);
88     inputStream.read(&xyz.lightnormalindex, 1);
89 }
90
91 const int MD2_XYZNORMAL_V0 = 0;
92 const int MD2_XYZNORMAL_V1 = 1;
93 const int MD2_XYZNORMAL_V2 = 2;
94 const int MD2_XYZNORMAL_LNI = 3;
95 const int MD2_XYZNORMAL_SIZE = 4;
96
97 typedef struct {
98     float scale[3];         // multiply byte verts by this
99     float translate[3];         // then add this
100     char name[16];          // frame name from grabbing
101     md2XyzNormal_t verts[1];    // variable sized
102 } md2Frame_t;
103
104 void istream_read_md2Frame(PointerInputStream &inputStream, md2Frame_t &frame)
105 {
106     frame.scale[0] = istream_read_float32_le(inputStream);
107     frame.scale[1] = istream_read_float32_le(inputStream);
108     frame.scale[2] = istream_read_float32_le(inputStream);
109     frame.translate[0] = istream_read_float32_le(inputStream);
110     frame.translate[1] = istream_read_float32_le(inputStream);
111     frame.translate[2] = istream_read_float32_le(inputStream);
112     inputStream.read(reinterpret_cast<unsigned char *>( frame.name ), 16);
113 }
114
115
116 // the glcmd format:
117 // a positive integer starts a tristrip command, followed by that many
118 // vertex structures.
119 // a negative integer starts a trifan command, followed by -x vertexes
120 // a zero indicates the end of the command list.
121 // a vertex consists of a floating point s, a floating point t,
122 // and an integer vertex index.
123
124
125 typedef struct {
126     unsigned char ident[4];
127     int version;
128
129     int skinwidth;
130     int skinheight;
131     int framesize;              // byte size of each frame
132
133     int num_skins;
134     int num_xyz;
135     int num_st;                 // greater than num_xyz for seams
136     int num_tris;
137     int num_glcmds;             // dwords in strip/fan command list
138     int num_frames;
139
140     int ofs_skins;              // each skin is a MAX_SKINNAME string
141     int ofs_st;                 // byte offset from start for md2St_t
142     int ofs_tris;               // offset for md2triangle_t
143     int ofs_frames;             // offset for first md2Frame_t
144     int ofs_glcmds;
145     int ofs_end;                // end of file
146
147 } md2Header_t;
148
149 void istream_read_md2Header(PointerInputStream &inputStream, md2Header_t &header)
150 {
151     inputStream.read(header.ident, 4);
152     header.version = istream_read_int32_le(inputStream);
153     header.skinwidth = istream_read_int32_le(inputStream);
154     header.skinheight = istream_read_int32_le(inputStream);
155     header.framesize = istream_read_int32_le(inputStream);
156     header.num_skins = istream_read_int32_le(inputStream);
157     header.num_xyz = istream_read_int32_le(inputStream);
158     header.num_st = istream_read_int32_le(inputStream);
159     header.num_tris = istream_read_int32_le(inputStream);
160     header.num_glcmds = istream_read_int32_le(inputStream);
161     header.num_frames = istream_read_int32_le(inputStream);
162     header.ofs_skins = istream_read_int32_le(inputStream);
163     header.ofs_st = istream_read_int32_le(inputStream);
164     header.ofs_tris = istream_read_int32_le(inputStream);
165     header.ofs_frames = istream_read_int32_le(inputStream);
166     header.ofs_glcmds = istream_read_int32_le(inputStream);
167     header.ofs_end = istream_read_int32_le(inputStream);
168 }
169
170
171 ArbitraryMeshVertex
172 MD2Vertex_construct(const md2Header_t *pHeader, const md2Frame_t *pFrame, const md2XyzNormal_t *xyz, const md2St_t *st)
173 {
174     return ArbitraryMeshVertex(
175             Vertex3f(
176                     xyz->v[0] * pFrame->scale[0] + pFrame->translate[0],
177                     xyz->v[1] * pFrame->scale[1] + pFrame->translate[1],
178                     xyz->v[2] * pFrame->scale[2] + pFrame->translate[2]
179             ),
180             Normal3f(
181                     g_mdl_normals[xyz->lightnormalindex][0],
182                     g_mdl_normals[xyz->lightnormalindex][1],
183                     g_mdl_normals[xyz->lightnormalindex][2]
184             ),
185             TexCoord2f(
186                     (float) st->s / pHeader->skinwidth,
187                     (float) st->t / pHeader->skinheight
188             )
189     );
190 }
191
192 void MD2Surface_read(Model &model, const byte *buffer, ArchiveFile &file)
193 {
194     Surface &surface = model.newSurface();
195     md2Header_t header;
196     {
197         PointerInputStream inputStream(buffer);
198         istream_read_md2Header(inputStream, header);
199     }
200
201     {
202
203         md2Frame_t frame;
204         PointerInputStream frameStream(buffer + header.ofs_frames);
205         istream_read_md2Frame(frameStream, frame);
206
207
208         surface.indices().reserve(header.num_tris * 3);
209
210         Array<md2XyzNormal_t> md2Xyz(header.num_xyz);
211         for (Array<md2XyzNormal_t>::iterator i = md2Xyz.begin(); i != md2Xyz.end(); ++i) {
212             istream_read_md2XyzNormal(frameStream, *i);
213         }
214
215         Array<md2St_t> md2St(header.num_st);
216         PointerInputStream stStream(buffer + header.ofs_st);
217         for (Array<md2St_t>::iterator i = md2St.begin(); i != md2St.end(); ++i) {
218             istream_read_md2St(stStream, *i);
219         }
220
221         UniqueVertexBuffer<ArbitraryMeshVertex> inserter(surface.vertices());
222         inserter.reserve(header.num_st);
223
224         PointerInputStream triangleStream(buffer + header.ofs_tris);
225         for (int i = 0; i < header.num_tris; ++i) {
226             md2Triangle_t triangle;
227             istream_read_md2Triangle(triangleStream, triangle);
228             surface.indices().insert(inserter.insert(
229                     MD2Vertex_construct(&header, &frame, &md2Xyz[triangle.index_xyz[0]],
230                                         &md2St[triangle.index_st[0]])));
231             surface.indices().insert(inserter.insert(
232                     MD2Vertex_construct(&header, &frame, &md2Xyz[triangle.index_xyz[1]],
233                                         &md2St[triangle.index_st[1]])));
234             surface.indices().insert(inserter.insert(
235                     MD2Vertex_construct(&header, &frame, &md2Xyz[triangle.index_xyz[2]],
236                                         &md2St[triangle.index_st[2]])));
237         }
238     }
239
240     char skinname[MD2_MAX_SKINNAME];
241     char skinnameRelative[MD2_MAX_SKINNAME];
242     char path[MD2_MAX_SKINNAME];
243     int i = MD2_MAX_SKINNAME;
244     PointerInputStream inputStream(buffer + header.ofs_skins);
245     inputStream.read(reinterpret_cast<byte *>( skinnameRelative ), MD2_MAX_SKINNAME);
246     // relative texture path - allows moving of models in game dir structure without changing the skinpath
247     // e.g. used in ufo:ai
248     if (skinnameRelative[0] == '.') {
249         strncpy(path, file.getName(), MD2_MAX_SKINNAME);
250         for (; i--;) {
251             // skip filename
252             if (path[i] == '/' || path[i] == '\\') {
253                 break;
254             }
255             path[i] = '\0';
256         }
257 //      globalErrorStream() << "modified skinname: " << path << " (path) and " << skinnameRelative << " (texture)" << "\n";
258         snprintf(skinname, MD2_MAX_SKINNAME, "%s%s", path, &skinnameRelative[1]);
259 //      globalErrorStream() << skinname << "\n";
260     } else {
261         strcpy(skinname, skinnameRelative);
262     }
263     surface.setShader(skinname);
264     surface.updateAABB();
265 }
266
267 void MD2Model_read(Model &model, const byte *buffer, ArchiveFile &file)
268 {
269     MD2Surface_read(model, buffer, file);
270     model.updateAABB();
271 }
272
273 scene::Node &MD2Model_new(const byte *buffer, ArchiveFile &file)
274 {
275     ModelNode *modelNode = new ModelNode();
276     MD2Model_read(modelNode->model(), buffer, file);
277     return modelNode->node();
278 }
279
280 scene::Node &MD2Model_default()
281 {
282     ModelNode *modelNode = new ModelNode();
283     Model_constructNull(modelNode->model());
284     return modelNode->node();
285 }
286
287 scene::Node &MD2Model_fromBuffer(unsigned char *buffer, ArchiveFile &file)
288 {
289     if (!ident_equal(buffer, MD2_IDENT)) {
290         globalErrorStream() << "MD2 read error: incorrect ident\n";
291         return MD2Model_default();
292     } else {
293         return MD2Model_new(buffer, file);
294     }
295 }
296
297 scene::Node &loadMD2Model(ArchiveFile &file)
298 {
299     ScopedArchiveBuffer buffer(file);
300     return MD2Model_fromBuffer(buffer.buffer, file);
301 }