Debian Codegraph @

radiantcore/model/picomodel/lib/pm_md3.c

raw
1 /* -----------------------------------------------------------------------------
2
3 PicoModel Library
4
5 Copyright (c) 2002, Randy Reddig & seaw0lf
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without modification,
9 are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this list
12 of conditions and the following disclaimer.
13
14 Redistributions in binary form must reproduce the above copyright notice, this
15 list of conditions and the following disclaimer in the documentation and/or
16 other materials provided with the distribution.
17
18 Neither the names of the copyright holders nor the names of its contributors may
19 be used to endorse or promote products derived from this software without
20 specific prior written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 ----------------------------------------------------------------------------- */
34
35
36
37 /* marker */
38 #define PM_MD3_C
39
40
41
42 /* dependencies */
43 #include "picointernal.h"
44
45
46
47 /* md3 model format */
48 static const char* MD3_MAGIC = "IDP3";
49 #define MD3_VERSION 15
50
51 /* md3 vertex scale */
52 #define MD3_SCALE (1.0f / 64.0f)
53
54 /* md3 model frame information */
55 typedef struct md3Frame_s
56 {
57 float bounds[ 2 ][ 3 ];
58 float localOrigin[ 3 ];
59 float radius;
60 char creator[ 16 ];
61 }
62 md3Frame_t;
63
64 /* md3 model tag information */
65 typedef struct md3Tag_s
66 {
67 char name[ 64 ];
68 float origin[ 3 ];
69 float axis[ 3 ][ 3 ];
70 }
71 md3Tag_t;
72
73 /* md3 surface md3 (one object mesh) */
74 typedef struct md3Surface_s
75 {
76 char magic[ 4 ];
77 char name[ 64 ]; /* polyset name */
78 int flags;
79 int numFrames; /* all model surfaces should have the same */
80 int numShaders; /* all model surfaces should have the same */
81 int numVerts;
82 int numTriangles;
83 int ofsTriangles;
84 int ofsShaders; /* offset from start of md3Surface_t */
85 int ofsSt; /* texture coords are common for all frames */
86 int ofsVertexes; /* numVerts * numFrames */
87 int ofsEnd; /* next surface follows */
88 }
89 md3Surface_t;
90
91 typedef struct md3Shader_s
92 {
93 char name[ 64 ];
94 int shaderIndex; /* for ingame use */
95 }
96 md3Shader_t;
97
98 typedef struct md3Triangle_s
99 {
100 int indexes[ 3 ];
101 }
102 md3Triangle_t;
103
104 typedef struct md3TexCoord_s
105 {
106 float st[ 2 ];
107 }
108 md3TexCoord_t;
109
110 typedef struct md3Vertex_s
111 {
112 short xyz[ 3 ];
113 short normal;
114 }
115 md3Vertex_t;
116
117
118 /* md3 model file md3 structure */
119 typedef struct md3_s
120 {
121 char magic[ 4 ]; /* MD3_MAGIC */
122 int version;
123 char name[ 64 ]; /* model name */
124 int flags;
125 int numFrames;
126 int numTags;
127 int numSurfaces;
128 int numSkins; /* number of skins for the mesh */
129 int ofsFrames; /* offset for first frame */
130 int ofsTags; /* numFrames * numTags */
131 int ofsSurfaces; /* first surface, others follow */
132 int ofsEnd; /* end of file */
133 }
134 md3_t;
135
136
137
138
139 /*
140 _md3_canload()
141 validates a quake3 arena md3 model file. btw, i use the
142 preceding underscore cause it's a static func referenced
143 by one structure only.
144 */
145
146 static int _md3_canload( PM_PARAMS_CANLOAD )
147 {
148 md3_t *md3;
149
150
151 /* to keep the compiler happy */
152 *fileName = *fileName;
153
154 /* sanity check */
155 if( bufSize < ( sizeof( *md3 ) * 2) )
156 return PICO_PMV_ERROR_SIZE;
157
158 /* set as md3 */
159 md3 = (md3_t*) buffer;
160
161 /* check md3 magic */
162 if (md3->magic[0] != MD3_MAGIC[0] || md3->magic[1] != MD3_MAGIC[1] ||
163 md3->magic[2] != MD3_MAGIC[2] || md3->magic[3] != MD3_MAGIC[3])
164 {
165 return PICO_PMV_ERROR_IDENT;
166 }
167
168 /* check md3 version */
169 if( _pico_little_long( md3->version ) != MD3_VERSION )
170 return PICO_PMV_ERROR_VERSION;
171
172 /* file seems to be a valid md3 */
173 return PICO_PMV_OK;
174 }
175
176
177
178 /*
179 _md3_load()
180 loads a quake3 arena md3 model file.
181 */
182
183 static picoModel_t *_md3_load( PM_PARAMS_LOAD )
184 {
185 int i, j;
186 picoByte_t *bb;
187 md3_t *md3;
188 md3Surface_t *surface;
189 md3Shader_t *shader;
190 md3TexCoord_t *texCoord;
191 md3Frame_t *frame;
192 md3Triangle_t *triangle;
193 md3Vertex_t *vertex;
194 double lat, lng;
195
196 picoModel_t *picoModel;
197 picoSurface_t *picoSurface;
198 picoShader_t *picoShader;
199 picoVec3_t xyz, normal;
200 picoVec2_t st;
201 picoColor_t color;
202
203
204 /* -------------------------------------------------
205 md3 loading
206 ------------------------------------------------- */
207
208
209 /* set as md3 */
210 bb = (picoByte_t*) buffer;
211 md3 = (md3_t*) buffer;
212
213 /* check ident and version */
214 if (md3->magic[0] != MD3_MAGIC[0] || md3->magic[1] != MD3_MAGIC[1] ||
215 md3->magic[2] != MD3_MAGIC[2] || md3->magic[3] != MD3_MAGIC[3] ||
216 _pico_little_long( md3->version ) != MD3_VERSION)
217 {
218 /* not an md3 file (todo: set error) */
219 return NULL;
220 }
221
222 /* swap md3; sea: swaps fixed */
223 md3->version = _pico_little_long( md3->version );
224 md3->numFrames = _pico_little_long( md3->numFrames );
225 md3->numTags = _pico_little_long( md3->numTags );
226 md3->numSurfaces = _pico_little_long( md3->numSurfaces );
227 md3->numSkins = _pico_little_long( md3->numSkins );
228 md3->ofsFrames = _pico_little_long( md3->ofsFrames );
229 md3->ofsTags = _pico_little_long( md3->ofsTags );
230 md3->ofsSurfaces = _pico_little_long( md3->ofsSurfaces );
231 md3->ofsEnd = _pico_little_long( md3->ofsEnd );
232
233 /* do frame check */
234 if( md3->numFrames < 1 )
235 {
236 _pico_printf( PICO_ERROR, "MD3 with 0 frames" );
237 return NULL;
238 }
239
240 if( frameNum < 0 || frameNum >= md3->numFrames )
241 {
242 _pico_printf( PICO_ERROR, "Invalid or out-of-range MD3 frame specified" );
243 return NULL;
244 }
245
246 /* swap frames */
247 frame = (md3Frame_t*) (bb + md3->ofsFrames );
248 for( i = 0; i < md3->numFrames; i++, frame++ )
249 {
250 frame->radius = _pico_little_float( frame->radius );
251 for( j = 0; j < 3; j++ )
252 {
253 frame->bounds[ 0 ][ j ] = _pico_little_float( frame->bounds[ 0 ][ j ] );
254 frame->bounds[ 1 ][ j ] = _pico_little_float( frame->bounds[ 1 ][ j ] );
255 frame->localOrigin[ j ] = _pico_little_float( frame->localOrigin[ j ] );
256 }
257 }
258
259 /* swap surfaces */
260 surface = (md3Surface_t*) (bb + md3->ofsSurfaces);
261 for( i = 0; i < md3->numSurfaces; i++ )
262 {
263 /* swap surface md3; sea: swaps fixed */
264 surface->flags = _pico_little_long( surface->flags );
265 surface->numFrames = _pico_little_long( surface->numFrames );
266 surface->numShaders = _pico_little_long( surface->numShaders );
267 surface->numTriangles = _pico_little_long( surface->numTriangles );
268 surface->ofsTriangles = _pico_little_long( surface->ofsTriangles );
269 surface->numVerts = _pico_little_long( surface->numVerts );
270 surface->ofsShaders = _pico_little_long( surface->ofsShaders );
271 surface->ofsSt = _pico_little_long( surface->ofsSt );
272 surface->ofsVertexes = _pico_little_long( surface->ofsVertexes );
273 surface->ofsEnd = _pico_little_long( surface->ofsEnd );
274
275 /* swap triangles */
276 triangle = (md3Triangle_t*) ((picoByte_t*) surface + surface->ofsTriangles);
277 for( j = 0; j < surface->numTriangles; j++, triangle++ )
278 {
279 /* sea: swaps fixed */
280 triangle->indexes[ 0 ] = _pico_little_long( triangle->indexes[ 0 ] );
281 triangle->indexes[ 1 ] = _pico_little_long( triangle->indexes[ 1 ] );
282 triangle->indexes[ 2 ] = _pico_little_long( triangle->indexes[ 2 ] );
283 }
284
285 /* swap st coords */
286 texCoord = (md3TexCoord_t*) ((picoByte_t*) surface + surface->ofsSt);
287 for( j = 0; j < surface->numVerts; j++, texCoord++ )
288 {
289 texCoord->st[ 0 ] = _pico_little_float( texCoord->st[ 0 ] );
290 texCoord->st[ 1 ] = _pico_little_float( texCoord->st[ 1 ] );
291 }
292
293 /* swap xyz/normals */
294 vertex = (md3Vertex_t*) ((picoByte_t*) surface + surface->ofsVertexes);
295 for( j = 0; j < (surface->numVerts * surface->numFrames); j++, vertex++)
296 {
297 vertex->xyz[ 0 ] = _pico_little_short( vertex->xyz[ 0 ] );
298 vertex->xyz[ 1 ] = _pico_little_short( vertex->xyz[ 1 ] );
299 vertex->xyz[ 2 ] = _pico_little_short( vertex->xyz[ 2 ] );
300 vertex->normal = _pico_little_short( vertex->normal );
301 }
302
303 /* get next surface */
304 surface = (md3Surface_t*) ((picoByte_t*) surface + surface->ofsEnd);
305 }
306
307 /* -------------------------------------------------
308 pico model creation
309 ------------------------------------------------- */
310
311 /* create new pico model */
312 picoModel = PicoNewModel();
313 if( picoModel == NULL )
314 {
315 _pico_printf( PICO_ERROR, "Unable to allocate a new model" );
316 return NULL;
317 }
318
319 /* do model setup */
320 PicoSetModelFrameNum( picoModel, frameNum );
321 PicoSetModelNumFrames( picoModel, md3->numFrames ); /* sea */
322 PicoSetModelName( picoModel, fileName );
323 PicoSetModelFileName( picoModel, fileName );
324
325 /* md3 surfaces become picomodel surfaces */
326 surface = (md3Surface_t*) (bb + md3->ofsSurfaces);
327
328 /* run through md3 surfaces */
329 for( i = 0; i < md3->numSurfaces; i++ )
330 {
331 /* allocate new pico surface */
332 picoSurface = PicoNewSurface( picoModel );
333 if( picoSurface == NULL )
334 {
335 _pico_printf( PICO_ERROR, "Unable to allocate a new model surface" );
336 PicoFreeModel( picoModel ); /* sea */
337 return NULL;
338 }
339
340 /* md3 model surfaces are all triangle meshes */
341 PicoSetSurfaceType( picoSurface, PICO_TRIANGLES );
342
343 /* set surface name */
344 PicoSetSurfaceName( picoSurface, surface->name );
345
346 /* create new pico shader -sea */
347 picoShader = PicoNewShader( picoModel );
348 if( picoShader == NULL )
349 {
350 _pico_printf( PICO_ERROR, "Unable to allocate a new model shader" );
351 PicoFreeModel( picoModel );
352 return NULL;
353 }
354
355 /* detox and set shader name */
356 shader = (md3Shader_t*) ((picoByte_t*) surface + surface->ofsShaders);
357 _pico_setfext( shader->name, "" );
358 _pico_unixify( shader->name );
359 PicoSetShaderName( picoShader, shader->name );
360
361 /* associate current surface with newly created shader */
362 PicoSetSurfaceShader( picoSurface, picoShader );
363
364 /* copy indexes */
365 triangle = (md3Triangle_t *) ((picoByte_t*) surface + surface->ofsTriangles);
366
367 for( j = 0; j < surface->numTriangles; j++, triangle++ )
368 {
369 PicoSetSurfaceIndex( picoSurface, (j * 3 + 0), (picoIndex_t) triangle->indexes[ 0 ] );
370 PicoSetSurfaceIndex( picoSurface, (j * 3 + 1), (picoIndex_t) triangle->indexes[ 1 ] );
371 PicoSetSurfaceIndex( picoSurface, (j * 3 + 2), (picoIndex_t) triangle->indexes[ 2 ] );
372 }
373
374 /* copy vertexes */
375 texCoord = (md3TexCoord_t*) ((picoByte_t *) surface + surface->ofsSt);
376 vertex = (md3Vertex_t*) ((picoByte_t*) surface + surface->ofsVertexes + surface->numVerts * frameNum * sizeof( md3Vertex_t ) );
377 _pico_set_color( color, 255, 255, 255, 255 );
378
379 for( j = 0; j < surface->numVerts; j++, texCoord++, vertex++ )
380 {
381 /* set vertex origin */
382 xyz[ 0 ] = MD3_SCALE * vertex->xyz[ 0 ];
383 xyz[ 1 ] = MD3_SCALE * vertex->xyz[ 1 ];
384 xyz[ 2 ] = MD3_SCALE * vertex->xyz[ 2 ];
385 PicoSetSurfaceXYZ( picoSurface, j, xyz );
386
387 /* decode lat/lng normal to 3 float normal */
388 lat = (float) ((vertex->normal >> 8) & 0xff);
389 lng = (float) (vertex->normal & 0xff);
390 lat *= PICO_PI / 128;
391 lng *= PICO_PI / 128;
392 normal[ 0 ] = (picoVec_t) cos( lat ) * (picoVec_t) sin( lng );
393 normal[ 1 ] = (picoVec_t) sin( lat ) * (picoVec_t) sin( lng );
394 normal[ 2 ] = (picoVec_t) cos( lng );
395 PicoSetSurfaceNormal( picoSurface, j, normal );
396
397 /* set st coords */
398 st[ 0 ] = texCoord->st[ 0 ];
399 st[ 1 ] = texCoord->st[ 1 ];
400 PicoSetSurfaceST( picoSurface, 0, j, st );
401
402 /* set color */
403 PicoSetSurfaceColor( picoSurface, 0, j, color );
404 }
405
406 /* get next surface */
407 surface = (md3Surface_t*) ((picoByte_t*) surface + surface->ofsEnd);
408 }
409
410 /* return the new pico model */
411 return picoModel;
412 }
413
414
415
416 /* pico file format module definition */
417 const picoModule_t picoModuleMD3 =
418 {
419 "1.3", /* module version string */
420 "Quake 3 Arena", /* module display name */
421 "Randy Reddig", /* author's name */
422 "2002 Randy Reddig", /* module copyright */
423 {
424 "md3", NULL, NULL, NULL /* default extensions to use */
425 },
426 _md3_canload, /* validation routine */
427 _md3_load, /* load routine */
428 NULL, /* save validation routine */
429 NULL /* save routine */
430 };