1 /* -----------------------------------------------------------------------------
5 Copyright (c) 2002, Randy Reddig & seaw0lf
8 Redistribution and use in source and binary forms, with or without modification,
9 are permitted provided that the following conditions are met:
11 Redistributions of source code must retain the above copyright notice, this list
12 of conditions and the following disclaimer.
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.
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.
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.
33 ----------------------------------------------------------------------------- */
43 #include "picointernal.h"
47 /* md3 model format */
48 #define MD3_MAGIC "IDP3"
49 #define MD3_VERSION 15
51 /* md3 vertex scale */
52 #define MD3_SCALE ( 1.0f / 64.0f )
54 /* md3 model frame information */
55 typedef struct md3Frame_s
57 float bounds[ 2 ][ 3 ];
58 float localOrigin[ 3 ];
64 /* md3 model tag information */
65 typedef struct md3Tag_s
73 /* md3 surface md3 (one object mesh) */
74 typedef struct md3Surface_s
77 char name[ 64 ]; /* polyset name */
79 int numFrames; /* all model surfaces should have the same */
80 int numShaders; /* all model surfaces should have the same */
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 */
91 typedef struct md3Shader_s
94 int shaderIndex; /* for ingame use */
98 typedef struct md3Triangle_s
104 typedef struct md3TexCoord_s
110 typedef struct md3Vertex_s
118 /* md3 model file md3 structure */
121 char magic[ 4 ]; /* MD3_MAGIC */
123 char name[ 64 ]; /* model name */
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 */
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.
146 static int _md3_canload( PM_PARAMS_CANLOAD ){
151 if ( (size_t) bufSize < ( sizeof( *md3 ) * 2 ) ) {
152 return PICO_PMV_ERROR_SIZE;
156 md3 = (const md3_t*) buffer;
158 /* check md3 magic */
159 if ( *( (const int*) md3->magic ) != *( (const int*) MD3_MAGIC ) ) {
160 return PICO_PMV_ERROR_IDENT;
163 /* check md3 version */
164 if ( _pico_little_long( md3->version ) != MD3_VERSION ) {
165 return PICO_PMV_ERROR_VERSION;
168 /* file seems to be a valid md3 */
176 loads a quake3 arena md3 model file.
179 static picoModel_t *_md3_load( PM_PARAMS_LOAD ){
181 picoByte_t *bb, *bb0;
183 md3Surface_t *surface;
185 md3TexCoord_t *texCoord;
187 md3Triangle_t *triangle;
191 picoModel_t *picoModel;
192 picoSurface_t *picoSurface;
193 picoShader_t *picoShader;
194 picoVec3_t xyz, normal;
199 /* -------------------------------------------------
201 ------------------------------------------------- */
205 bb0 = bb = (picoByte_t*) _pico_alloc( bufSize );
206 memcpy( bb, buffer, bufSize );
209 /* check ident and version */
210 if ( *( (int*) md3->magic ) != *( (int*) MD3_MAGIC ) || _pico_little_long( md3->version ) != MD3_VERSION ) {
211 /* not an md3 file (todo: set error) */
216 /* swap md3; sea: swaps fixed */
217 md3->version = _pico_little_long( md3->version );
218 md3->numFrames = _pico_little_long( md3->numFrames );
219 md3->numTags = _pico_little_long( md3->numTags );
220 md3->numSurfaces = _pico_little_long( md3->numSurfaces );
221 md3->numSkins = _pico_little_long( md3->numSkins );
222 md3->ofsFrames = _pico_little_long( md3->ofsFrames );
223 md3->ofsTags = _pico_little_long( md3->ofsTags );
224 md3->ofsSurfaces = _pico_little_long( md3->ofsSurfaces );
225 md3->ofsEnd = _pico_little_long( md3->ofsEnd );
228 if ( md3->numFrames < 1 ) {
229 _pico_printf( PICO_ERROR, "MD3 with 0 frames" );
234 if ( frameNum < 0 || frameNum >= md3->numFrames ) {
235 _pico_printf( PICO_ERROR, "Invalid or out-of-range MD3 frame specified" );
241 frame = (md3Frame_t*) ( bb + md3->ofsFrames );
242 for ( i = 0; i < md3->numFrames; i++, frame++ )
244 frame->radius = _pico_little_float( frame->radius );
245 for ( j = 0; j < 3; j++ )
247 frame->bounds[ 0 ][ j ] = _pico_little_float( frame->bounds[ 0 ][ j ] );
248 frame->bounds[ 1 ][ j ] = _pico_little_float( frame->bounds[ 1 ][ j ] );
249 frame->localOrigin[ j ] = _pico_little_float( frame->localOrigin[ j ] );
254 surface = (md3Surface_t*) ( bb + md3->ofsSurfaces );
255 for ( i = 0; i < md3->numSurfaces; i++ )
257 /* swap surface md3; sea: swaps fixed */
258 surface->flags = _pico_little_long( surface->flags );
259 surface->numFrames = _pico_little_long( surface->numFrames );
260 surface->numShaders = _pico_little_long( surface->numShaders );
261 surface->numTriangles = _pico_little_long( surface->numTriangles );
262 surface->ofsTriangles = _pico_little_long( surface->ofsTriangles );
263 surface->numVerts = _pico_little_long( surface->numVerts );
264 surface->ofsShaders = _pico_little_long( surface->ofsShaders );
265 surface->ofsSt = _pico_little_long( surface->ofsSt );
266 surface->ofsVertexes = _pico_little_long( surface->ofsVertexes );
267 surface->ofsEnd = _pico_little_long( surface->ofsEnd );
270 triangle = (md3Triangle_t*) ( (picoByte_t*) surface + surface->ofsTriangles );
271 for ( j = 0; j < surface->numTriangles; j++, triangle++ )
273 /* sea: swaps fixed */
274 triangle->indexes[ 0 ] = _pico_little_long( triangle->indexes[ 0 ] );
275 triangle->indexes[ 1 ] = _pico_little_long( triangle->indexes[ 1 ] );
276 triangle->indexes[ 2 ] = _pico_little_long( triangle->indexes[ 2 ] );
280 texCoord = (md3TexCoord_t*) ( (picoByte_t*) surface + surface->ofsSt );
281 for ( j = 0; j < surface->numVerts; j++, texCoord++ )
283 texCoord->st[ 0 ] = _pico_little_float( texCoord->st[ 0 ] );
284 texCoord->st[ 1 ] = _pico_little_float( texCoord->st[ 1 ] );
287 /* swap xyz/normals */
288 vertex = (md3Vertex_t*) ( (picoByte_t*) surface + surface->ofsVertexes );
289 for ( j = 0; j < ( surface->numVerts * surface->numFrames ); j++, vertex++ )
291 vertex->xyz[ 0 ] = _pico_little_short( vertex->xyz[ 0 ] );
292 vertex->xyz[ 1 ] = _pico_little_short( vertex->xyz[ 1 ] );
293 vertex->xyz[ 2 ] = _pico_little_short( vertex->xyz[ 2 ] );
294 vertex->normal = _pico_little_short( vertex->normal );
297 /* get next surface */
298 surface = (md3Surface_t*) ( (picoByte_t*) surface + surface->ofsEnd );
301 /* -------------------------------------------------
303 ------------------------------------------------- */
305 /* create new pico model */
306 picoModel = PicoNewModel();
307 if ( picoModel == NULL ) {
308 _pico_printf( PICO_ERROR, "Unable to allocate a new model" );
314 PicoSetModelFrameNum( picoModel, frameNum );
315 PicoSetModelNumFrames( picoModel, md3->numFrames ); /* sea */
316 PicoSetModelName( picoModel, fileName );
317 PicoSetModelFileName( picoModel, fileName );
319 /* md3 surfaces become picomodel surfaces */
320 surface = (md3Surface_t*) ( bb + md3->ofsSurfaces );
322 /* run through md3 surfaces */
323 for ( i = 0; i < md3->numSurfaces; i++ )
325 /* allocate new pico surface */
326 picoSurface = PicoNewSurface( picoModel );
327 if ( picoSurface == NULL ) {
328 _pico_printf( PICO_ERROR, "Unable to allocate a new model surface" );
329 PicoFreeModel( picoModel ); /* sea */
334 /* md3 model surfaces are all triangle meshes */
335 PicoSetSurfaceType( picoSurface, PICO_TRIANGLES );
337 /* set surface name */
338 PicoSetSurfaceName( picoSurface, surface->name );
340 /* create new pico shader -sea */
341 picoShader = PicoNewShader( picoModel );
342 if ( picoShader == NULL ) {
343 _pico_printf( PICO_ERROR, "Unable to allocate a new model shader" );
344 PicoFreeModel( picoModel );
349 /* detox and set shader name */
350 shader = (md3Shader_t*) ( (picoByte_t*) surface + surface->ofsShaders );
351 _pico_setfext( shader->name, "" );
352 _pico_unixify( shader->name );
353 PicoSetShaderName( picoShader, shader->name );
355 /* associate current surface with newly created shader */
356 PicoSetSurfaceShader( picoSurface, picoShader );
359 triangle = (md3Triangle_t *) ( (picoByte_t*) surface + surface->ofsTriangles );
361 for ( j = 0; j < surface->numTriangles; j++, triangle++ )
363 PicoSetSurfaceIndex( picoSurface, ( j * 3 + 0 ), (picoIndex_t) triangle->indexes[ 0 ] );
364 PicoSetSurfaceIndex( picoSurface, ( j * 3 + 1 ), (picoIndex_t) triangle->indexes[ 1 ] );
365 PicoSetSurfaceIndex( picoSurface, ( j * 3 + 2 ), (picoIndex_t) triangle->indexes[ 2 ] );
369 texCoord = (md3TexCoord_t*) ( (picoByte_t *) surface + surface->ofsSt );
370 vertex = (md3Vertex_t*) ( (picoByte_t*) surface + surface->ofsVertexes + surface->numVerts * frameNum * sizeof( md3Vertex_t ) );
371 _pico_set_color( color, 255, 255, 255, 255 );
373 for ( j = 0; j < surface->numVerts; j++, texCoord++, vertex++ )
375 /* set vertex origin */
376 xyz[ 0 ] = MD3_SCALE * vertex->xyz[ 0 ];
377 xyz[ 1 ] = MD3_SCALE * vertex->xyz[ 1 ];
378 xyz[ 2 ] = MD3_SCALE * vertex->xyz[ 2 ];
379 PicoSetSurfaceXYZ( picoSurface, j, xyz );
381 /* decode lat/lng normal to 3 float normal */
382 lat = (float) ( ( vertex->normal >> 8 ) & 0xff );
383 lng = (float) ( vertex->normal & 0xff );
384 lat *= PICO_PI / 128;
385 lng *= PICO_PI / 128;
386 normal[ 0 ] = (picoVec_t) cos( lat ) * (picoVec_t) sin( lng );
387 normal[ 1 ] = (picoVec_t) sin( lat ) * (picoVec_t) sin( lng );
388 normal[ 2 ] = (picoVec_t) cos( lng );
389 PicoSetSurfaceNormal( picoSurface, j, normal );
392 st[ 0 ] = texCoord->st[ 0 ];
393 st[ 1 ] = texCoord->st[ 1 ];
394 PicoSetSurfaceST( picoSurface, 0, j, st );
397 PicoSetSurfaceColor( picoSurface, 0, j, color );
400 /* get next surface */
401 surface = (md3Surface_t*) ( (picoByte_t*) surface + surface->ofsEnd );
404 /* return the new pico model */
411 /* pico file format module definition */
412 const picoModule_t picoModuleMD3 =
414 "1.3", /* module version string */
415 "Quake 3 Arena", /* module display name */
416 "Randy Reddig", /* author's name */
417 "2002 Randy Reddig", /* module copyright */
419 "md3", NULL, NULL, NULL /* default extensions to use */
421 _md3_canload, /* validation routine */
422 _md3_load, /* load routine */
423 NULL, /* save validation routine */
424 NULL /* save routine */