Debian Codegraph @

libs/picomodel/pm_ms3d.c

raw symbols SCIP
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 /* dependencies */
36 #include "picointernal.h"
37 #include "globaldefs.h"
38 #include "assert.h"
39
40 /* disable warnings */
41 #if GDEF_COMPILER_MSVC
42 #pragma warning( disable:4100 ) /* unref param */
43 #endif
44
45 /* remarks:
46 * - loader seems stable
47 * todo:
48 * - fix uv coordinate problem
49 * - check for buffer overflows ('bufptr' accesses)
50 */
51 /* uncomment when debugging this module */
52 #define DEBUG_PM_MS3D
53 #define DEBUG_PM_MS3D_EX
54
55 /* ms3d limits */
56 const int MS3D_MAX_VERTS = 8192;
57 const int MS3D_MAX_TRIS = 16384;
58 const int MS3D_MAX_GROUPS = 128;
59 const int MS3D_MAX_MATERIALS = 128;
60 const int MS3D_MAX_JOINTS = 128;
61 const int MS3D_MAX_KEYFRAMES = 216;
62
63 /* ms3d flags */
64 const int MS3D_SELECTED = 1;
65 const int MS3D_HIDDEN = 2;
66 const int MS3D_SELECTED2 = 4;
67 const int MS3D_DIRTY = 8;
68
69 /* this freaky loader needs byte alignment */
70 #pragma pack(push, 1)
71
72 /* ms3d header */
73 typedef struct SMsHeader
74 {
75 char magic[10];
76 int version;
77 }
78 TMsHeader;
79
80 /* ms3d vertex */
81 typedef struct SMsVertex
82 {
83 unsigned char flags; /* sel, sel2, or hidden */
84 float xyz[3];
85 char boneID; /* -1 means 'no bone' */
86 unsigned char refCount;
87 }
88 TMsVertex;
89
90 /* ms3d triangle */
91 typedef struct SMsTriangle
92 {
93 unsigned short flags; /* sel, sel2, or hidden */
94 unsigned short vertexIndices[3];
95 float vertexNormals[3][3];
96 float s[3];
97 float t[3];
98 unsigned char smoothingGroup; /* 1 - 32 */
99 unsigned char groupIndex;
100 }
101 TMsTriangle;
102
103 /* ms3d material */
104 typedef struct SMsMaterial
105 {
106 char name[32];
107 float ambient[4];
108 float diffuse[4];
109 float specular[4];
110 float emissive[4];
111 float shininess; /* range 0..128 */
112 float transparency; /* range 0..1 */
113 unsigned char mode;
114 char texture [128]; /* texture.bmp */
115 char alphamap[128]; /* alpha.bmp */
116 }
117 TMsMaterial;
118
119 // ms3d group (static part)
120 // followed by a variable size block (see below)
121 typedef struct SMsGroup
122 {
123 unsigned char flags; // sel, hidden
124 char name[32];
125 unsigned short numTriangles;
126 /*
127 unsigned short triangleIndices[ numTriangles ];
128 char materialIndex; // -1 means 'no material'
129 */
130 }
131 TMsGroup;
132
133 // ms3d joint
134 typedef struct SMsJoint
135 {
136 unsigned char flags;
137 char name[32];
138 char parentName[32];
139 float rotation[3];
140 float translation[3];
141 unsigned short numRotationKeyframes;
142 unsigned short numTranslationKeyframes;
143 }
144 TMsJoint;
145
146 // ms3d keyframe
147 typedef struct SMsKeyframe
148 {
149 float time;
150 float parameter[3];
151 }
152 TMsKeyframe;
153
154 /* restore previous data alignment */
155 #pragma pack(pop)
156
157 /* _ms3d_canload:
158 * validates a milkshape3d model file.
159 */
160 static int _ms3d_canload( PM_PARAMS_CANLOAD ){
161 const TMsHeader *hdr;
162
163
164 /* sanity check */
165 if ( (size_t) bufSize < sizeof( TMsHeader ) ) {
166 return PICO_PMV_ERROR_SIZE;
167 }
168
169 /* get ms3d header */
170 hdr = (const TMsHeader *)buffer;
171
172 /* check ms3d magic */
173 if ( strncmp( hdr->magic,"MS3D000000",10 ) != 0 ) {
174 return PICO_PMV_ERROR_IDENT;
175 }
176
177 /* check ms3d version */
178 if ( _pico_little_long( hdr->version ) < 3 ||
179 _pico_little_long( hdr->version ) > 4 ) {
180 _pico_printf( PICO_ERROR,"MS3D file ignored. Only MS3D 1.3 and 1.4 is supported." );
181 return PICO_PMV_ERROR_VERSION;
182 }
183 /* file seems to be a valid ms3d */
184 return PICO_PMV_OK;
185 }
186
187 static unsigned char *GetWord( unsigned char *bufptr, int *out ){
188 assert( bufptr != NULL );
189 *out = _pico_little_short( *(unsigned short *)bufptr );
190 return( bufptr + 2 );
191 }
192
193 /* _ms3d_load:
194 * loads a milkshape3d model file.
195 */
196 static picoModel_t *_ms3d_load( PM_PARAMS_LOAD ){
197 picoModel_t *model;
198 unsigned char *bufptr, *bufptr0;
199 int shaderRefs[ MS3D_MAX_GROUPS ];
200 int numGroups;
201 int numMaterials;
202 // unsigned char *ptrToGroups;
203 int numVerts;
204 unsigned char *ptrToVerts;
205 int numTris;
206 unsigned char *ptrToTris;
207 int i,k,m;
208
209 /* create new pico model */
210 model = PicoNewModel();
211 if ( model == NULL ) {
212 return NULL;
213 }
214
215 /* do model setup */
216 PicoSetModelFrameNum( model, frameNum );
217 PicoSetModelName( model, fileName );
218 PicoSetModelFileName( model, fileName );
219
220 bufptr0 = bufptr = (picoByte_t*) _pico_alloc( bufSize );
221
222 if ( !bufptr )
223 {
224 return NULL;
225 }
226
227 memcpy( bufptr, buffer, bufSize );
228 /* skip header */
229 bufptr += sizeof( TMsHeader );
230
231 /* get number of vertices */
232 bufptr = GetWord( bufptr,&numVerts );
233 ptrToVerts = bufptr;
234
235 #ifdef DEBUG_PM_MS3D
236 printf( "NumVertices: %d\n",numVerts );
237 #endif
238 /* swap verts */
239 for ( i = 0; i < numVerts; i++ )
240 {
241 TMsVertex *vertex;
242 vertex = (TMsVertex *)bufptr;
243 bufptr += sizeof( TMsVertex );
244
245 vertex->xyz[ 0 ] = _pico_little_float( vertex->xyz[ 0 ] );
246 vertex->xyz[ 1 ] = _pico_little_float( vertex->xyz[ 1 ] );
247 vertex->xyz[ 2 ] = _pico_little_float( vertex->xyz[ 2 ] );
248
249 #ifdef DEBUG_PM_MS3D_EX_
250 printf( "Vertex: x: %f y: %f z: %f\n",
251 msvd[i]->vertex[0],
252 msvd[i]->vertex[1],
253 msvd[i]->vertex[2] );
254 #endif
255 }
256 /* get number of triangles */
257 bufptr = GetWord( bufptr,&numTris );
258 ptrToTris = bufptr;
259
260 #ifdef DEBUG_PM_MS3D
261 printf( "NumTriangles: %d\n",numTris );
262 #endif
263 /* swap tris */
264 for ( i = 0; i < numTris; i++ )
265 {
266 TMsTriangle *triangle;
267 triangle = (TMsTriangle *)bufptr;
268 bufptr += sizeof( TMsTriangle );
269
270 triangle->flags = _pico_little_short( triangle->flags );
271
272 /* run through all tri verts */
273 for ( k = 0; k < 3; k++ )
274 {
275 /* swap tex coords */
276 triangle->s[ k ] = _pico_little_float( triangle->s[ k ] );
277 triangle->t[ k ] = _pico_little_float( triangle->t[ k ] );
278
279 /* swap fields */
280 triangle->vertexIndices[ k ] = _pico_little_short( triangle->vertexIndices[ k ] );
281 triangle->vertexNormals[ 0 ][ k ] = _pico_little_float( triangle->vertexNormals[ 0 ][ k ] );
282 triangle->vertexNormals[ 1 ][ k ] = _pico_little_float( triangle->vertexNormals[ 1 ][ k ] );
283 triangle->vertexNormals[ 2 ][ k ] = _pico_little_float( triangle->vertexNormals[ 2 ][ k ] );
284
285 /* check for out of range indices */
286 if ( triangle->vertexIndices[ k ] >= numVerts ) {
287 _pico_printf( PICO_ERROR,"Vertex %d index %d out of range (%d, max %d)",i,k,triangle->vertexIndices[k],numVerts - 1 );
288 PicoFreeModel( model );
289 _pico_free( bufptr0 );
290 return NULL; /* yuck */
291 }
292 }
293 }
294 /* get number of groups */
295 bufptr = GetWord( bufptr,&numGroups );
296 // ptrToGroups = bufptr;
297
298 #ifdef DEBUG_PM_MS3D
299 printf( "NumGroups: %d\n",numGroups );
300 #endif
301 /* run through all groups in model */
302 for ( i = 0; i < numGroups && i < MS3D_MAX_GROUPS; i++ )
303 {
304 picoSurface_t *surface;
305 TMsGroup *group;
306
307 group = (TMsGroup *)bufptr;
308 bufptr += sizeof( TMsGroup );
309
310 /* we ignore hidden groups */
311 if ( group->flags & MS3D_HIDDEN ) {
312 bufptr += ( group->numTriangles * 2 ) + 1;
313 continue;
314 }
315 /* forced null term of group name */
316 group->name[ 31 ] = '\0';
317
318 /* create new pico surface */
319 surface = PicoNewSurface( model );
320 if ( surface == NULL ) {
321 PicoFreeModel( model );
322 _pico_free( bufptr0 );
323 return NULL;
324 }
325 /* do surface setup */
326 PicoSetSurfaceType( surface,PICO_TRIANGLES );
327 PicoSetSurfaceName( surface,group->name );
328
329 /* process triangle indices */
330 for ( k = 0; k < group->numTriangles; k++ )
331 {
332 TMsTriangle *triangle;
333 unsigned int triangleIndex;
334
335 /* get triangle index */
336 bufptr = GetWord( bufptr,(int *)&triangleIndex );
337
338 /* get ptr to triangle data */
339 triangle = (TMsTriangle *)( ptrToTris + ( sizeof( TMsTriangle ) * triangleIndex ) );
340
341 /* run through triangle vertices */
342 for ( m = 0; m < 3; m++ )
343 {
344 TMsVertex *vertex;
345 unsigned int vertexIndex;
346 picoVec2_t texCoord;
347
348 /* get ptr to vertex data */
349 vertexIndex = triangle->vertexIndices[ m ];
350 vertex = (TMsVertex *)( ptrToVerts + ( sizeof( TMsVertex ) * vertexIndex ) );
351
352 /* store vertex origin */
353 PicoSetSurfaceXYZ( surface,vertexIndex,vertex->xyz );
354
355 /* store vertex color */
356 PicoSetSurfaceColor( surface, 0, vertexIndex, picoColor_white );
357
358 /* store vertex normal */
359 PicoSetSurfaceNormal( surface,vertexIndex,triangle->vertexNormals[ m ] );
360
361 /* store current face vertex index */
362 PicoSetSurfaceIndex( surface,( k * 3 + ( 2 - m ) ),(picoIndex_t)vertexIndex );
363
364 /* get texture vertex coord */
365 texCoord[ 0 ] = triangle->s[ m ];
366 texCoord[ 1 ] = -triangle->t[ m ]; /* flip t */
367
368 /* store texture vertex coord */
369 PicoSetSurfaceST( surface,0,vertexIndex,texCoord );
370 }
371 }
372 /* store material */
373 shaderRefs[ i ] = *bufptr++;
374
375 #ifdef DEBUG_PM_MS3D
376 printf( "Group %d: '%s' (%d tris)\n",i,group->name,group->numTriangles );
377 #endif
378 }
379 /* get number of materials */
380 bufptr = GetWord( bufptr,&numMaterials );
381
382 #ifdef DEBUG_PM_MS3D
383 printf( "NumMaterials: %d\n",numMaterials );
384 #endif
385 /* run through all materials in model */
386 for ( i = 0; i < numMaterials; i++ )
387 {
388 picoShader_t *shader;
389 picoColor_t ambient,diffuse,specular;
390 TMsMaterial *material;
391 int k;
392
393 material = (TMsMaterial *)bufptr;
394 bufptr += sizeof( TMsMaterial );
395
396 /* null term strings */
397 material->name [ 31 ] = '\0';
398 material->texture [ 127 ] = '\0';
399 material->alphamap[ 127 ] = '\0';
400
401 /* ltrim strings */
402 _pico_strltrim( material->name );
403 _pico_strltrim( material->texture );
404 _pico_strltrim( material->alphamap );
405
406 /* rtrim strings */
407 _pico_strrtrim( material->name );
408 _pico_strrtrim( material->texture );
409 _pico_strrtrim( material->alphamap );
410
411 /* create new pico shader */
412 shader = PicoNewShader( model );
413 if ( shader == NULL ) {
414 PicoFreeModel( model );
415 _pico_free( bufptr0 );
416 return NULL;
417 }
418 /* scale shader colors */
419 for ( k = 0; k < 4; k++ )
420 {
421 ambient [ k ] = (picoByte_t) ( material->ambient[ k ] * 255 );
422 diffuse [ k ] = (picoByte_t) ( material->diffuse[ k ] * 255 );
423 specular[ k ] = (picoByte_t) ( material->specular[ k ] * 255 );
424 }
425 /* set shader colors */
426 PicoSetShaderAmbientColor( shader,ambient );
427 PicoSetShaderDiffuseColor( shader,diffuse );
428 PicoSetShaderSpecularColor( shader,specular );
429
430 /* set shader transparency */
431 PicoSetShaderTransparency( shader,material->transparency );
432
433 /* set shader shininess (0..127) */
434 PicoSetShaderShininess( shader,material->shininess );
435
436 /* set shader name */
437 PicoSetShaderName( shader,material->name );
438
439 /* set shader texture map name */
440 PicoSetShaderMapName( shader,material->texture );
441
442 #ifdef DEBUG_PM_MS3D
443 printf( "Material %d: '%s' ('%s','%s')\n",i,material->name,material->texture,material->alphamap );
444 #endif
445 }
446 /* assign shaders to surfaces */
447 for ( i = 0; i < numGroups && i < MS3D_MAX_GROUPS; i++ )
448 {
449 picoSurface_t *surface;
450 picoShader_t *shader;
451
452 /* sanity check */
453 if ( shaderRefs[ i ] >= MS3D_MAX_MATERIALS ||
454 shaderRefs[ i ] < 0 ) {
455 continue;
456 }
457
458 /* get surface */
459 surface = PicoGetModelSurface( model,i );
460 if ( surface == NULL ) {
461 continue;
462 }
463
464 /* get shader */
465 shader = PicoGetModelShader( model,shaderRefs[ i ] );
466 if ( shader == NULL ) {
467 continue;
468 }
469
470 /* assign shader */
471 PicoSetSurfaceShader( surface,shader );
472
473 #ifdef DEBUG_PM_MS3D
474 printf( "Mapped: %d ('%s') to %d (%s)\n",
475 shaderRefs[i],shader->name,i,surface->name );
476 #endif
477 }
478 /* return allocated pico model */
479 _pico_free( bufptr0 );
480 return model;
481 // return NULL;
482 }
483
484 /* pico file format module definition */
485 const picoModule_t picoModuleMS3D =
486 {
487 "0.4-a", /* module version string */
488 "Milkshape 3D", /* module display name */
489 "seaw0lf", /* author's name */
490 "2002 seaw0lf", /* module copyright */
491 {
492 "ms3d",NULL,NULL,NULL /* default extensions to use */
493 },
494 _ms3d_canload, /* validation routine */
495 _ms3d_load, /* load routine */
496 NULL, /* save validation routine */
497 NULL /* save routine */
498 };