Debian Codegraph @

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