| 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 |
/* marker */ |
| 36 |
#define PM_LWO_C |
| 37 |
|
| 38 |
/* dependencies */ |
| 39 |
#include "picointernal.h" |
| 40 |
#include "lwo/lwo2.h" |
| 41 |
|
| 42 |
/* uncomment when debugging this module */ |
| 43 |
/*#define DEBUG_PM_LWO*/ |
| 44 |
|
| 45 |
#ifdef DEBUG_PM_LWO |
| 46 |
#include "time.h" |
| 47 |
#endif |
| 48 |
|
| 49 |
/* helper functions */ |
| 50 |
static const char *lwo_lwIDToStr( unsigned int lwID ) |
| 51 |
{ |
| 52 |
static char lwIDStr[5]; |
| 53 |
|
| 54 |
if (!lwID) |
| 55 |
{ |
| 56 |
return "n/a"; |
| 57 |
} |
| 58 |
|
| 59 |
lwIDStr[ 0 ] = (char)((lwID) >> 24); |
| 60 |
lwIDStr[ 1 ] = (char)((lwID) >> 16); |
| 61 |
lwIDStr[ 2 ] = (char)((lwID) >> 8); |
| 62 |
lwIDStr[ 3 ] = (char)((lwID)); |
| 63 |
lwIDStr[ 4 ] = '\0'; |
| 64 |
|
| 65 |
return lwIDStr; |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
_lwo_canload() |
| 70 |
validates a LightWave Object model file. btw, i use the |
| 71 |
preceding underscore cause it's a static func referenced |
| 72 |
by one structure only. |
| 73 |
*/ |
| 74 |
static int _lwo_canload( PM_PARAMS_CANLOAD ) |
| 75 |
{ |
| 76 |
picoMemStream_t *s; |
| 77 |
unsigned int failID = 0; |
| 78 |
int failpos = -1; |
| 79 |
int ret; |
| 80 |
|
| 81 |
/* create a new pico memorystream */ |
| 82 |
s = _pico_new_memstream( (picoByte_t *)buffer, bufSize ); |
| 83 |
if (s == NULL) |
| 84 |
{ |
| 85 |
return PICO_PMV_ERROR_MEMORY; |
| 86 |
} |
| 87 |
|
| 88 |
ret = lwValidateObject( fileName, s, &failID, &failpos ); |
| 89 |
|
| 90 |
_pico_free_memstream( s ); |
| 91 |
|
| 92 |
return ret; |
| 93 |
} |
| 94 |
|
| 95 |
/* |
| 96 |
_lwo_load() |
| 97 |
loads a LightWave Object model file. |
| 98 |
*/ |
| 99 |
static picoModel_t *_lwo_load( PM_PARAMS_LOAD ) |
| 100 |
{ |
| 101 |
picoMemStream_t *s; |
| 102 |
unsigned int failID = 0; |
| 103 |
int failpos = -1; |
| 104 |
lwObject *obj; |
| 105 |
lwSurface *surface; |
| 106 |
lwLayer *layer; |
| 107 |
lwPoint *pt; |
| 108 |
lwPolygon *pol; |
| 109 |
lwPolVert *v; |
| 110 |
lwVMapPt *vm; |
| 111 |
char name[ 256 ]; |
| 112 |
int i, j, k, numverts; |
| 113 |
|
| 114 |
picoModel_t *picoModel; |
| 115 |
picoSurface_t *picoSurface; |
| 116 |
picoShader_t *picoShader; |
| 117 |
picoVec3_t xyz, normal; |
| 118 |
picoVec2_t st; |
| 119 |
picoColor_t color; |
| 120 |
|
| 121 |
int defaultSTAxis[ 2 ]; |
| 122 |
picoVec2_t defaultXYZtoSTScale; |
| 123 |
|
| 124 |
picoVertexCombinationHash_t **hashTable; |
| 125 |
picoVertexCombinationHash_t *vertexCombinationHash; |
| 126 |
|
| 127 |
int surfacePolyCount; |
| 128 |
|
| 129 |
#ifdef DEBUG_PM_LWO |
| 130 |
clock_t load_start, load_finish, convert_start, convert_finish; |
| 131 |
double load_elapsed, convert_elapsed; |
| 132 |
|
| 133 |
load_start = clock(); |
| 134 |
#endif |
| 135 |
|
| 136 |
/* do frame check */ |
| 137 |
if( frameNum < 0 || frameNum >= 1 ) |
| 138 |
{ |
| 139 |
_pico_printf( PICO_ERROR, "Invalid or out-of-range LWO frame specified" ); |
| 140 |
return NULL; |
| 141 |
} |
| 142 |
|
| 143 |
/* create a new pico memorystream */ |
| 144 |
s = _pico_new_memstream( (picoByte_t *)buffer, bufSize ); |
| 145 |
if (s == NULL) |
| 146 |
{ |
| 147 |
return NULL; |
| 148 |
} |
| 149 |
|
| 150 |
obj = lwGetObject( fileName, s, &failID, &failpos ); |
| 151 |
|
| 152 |
_pico_free_memstream( s ); |
| 153 |
|
| 154 |
if( !obj ) { |
| 155 |
_pico_printf( PICO_ERROR, "Couldn't load LWO file, failed on ID '%s', position %d", lwo_lwIDToStr( failID ), failpos ); |
| 156 |
return NULL; |
| 157 |
} |
| 158 |
|
| 159 |
#ifdef DEBUG_PM_LWO |
| 160 |
convert_start = load_finish = clock(); |
| 161 |
load_elapsed = (double)(load_finish - load_start) / CLOCKS_PER_SEC; |
| 162 |
#endif |
| 163 |
|
| 164 |
/* ------------------------------------------------- |
| 165 |
pico model creation |
| 166 |
------------------------------------------------- */ |
| 167 |
|
| 168 |
/* create a new pico model */ |
| 169 |
picoModel = PicoNewModel(); |
| 170 |
if (picoModel == NULL) |
| 171 |
{ |
| 172 |
_pico_printf( PICO_ERROR, "Unable to allocate a new model" ); |
| 173 |
return NULL; |
| 174 |
} |
| 175 |
|
| 176 |
/* do model setup */ |
| 177 |
PicoSetModelFrameNum( picoModel, frameNum ); |
| 178 |
PicoSetModelNumFrames( picoModel, 1 ); |
| 179 |
PicoSetModelName( picoModel, fileName ); |
| 180 |
PicoSetModelFileName( picoModel, fileName ); |
| 181 |
|
| 182 |
/* create all polygons from layer[ 0 ] that belong to this surface */ |
| 183 |
layer = &obj->layer[0]; |
| 184 |
|
| 185 |
/* warn the user that other layers are discarded */ |
| 186 |
if (obj->nlayers > 1) |
| 187 |
{ |
| 188 |
_pico_printf( PICO_WARNING, "LWO loader discards any geometry data not in Layer 1 (%d layers found)", obj->nlayers ); |
| 189 |
} |
| 190 |
|
| 191 |
/* initialize dummy normal */ |
| 192 |
normal[ 0 ] = normal[ 1 ] = normal[ 2 ] = 0.f; |
| 193 |
|
| 194 |
/* setup default st map */ |
| 195 |
st[ 0 ] = st[ 1 ] = 0.f; /* st[0] holds max, st[1] holds max par one */ |
| 196 |
defaultSTAxis[ 0 ] = 0; |
| 197 |
defaultSTAxis[ 1 ] = 1; |
| 198 |
for( i = 0; i < 3; i++ ) |
| 199 |
{ |
| 200 |
float min = layer->bbox[ i ]; |
| 201 |
float max = layer->bbox[ i + 3 ]; |
| 202 |
float size = max - min; |
| 203 |
|
| 204 |
if (size > st[ 0 ]) |
| 205 |
{ |
| 206 |
defaultSTAxis[ 1 ] = defaultSTAxis[ 0 ]; |
| 207 |
defaultSTAxis[ 0 ] = i; |
| 208 |
|
| 209 |
st[ 1 ] = st[ 0 ]; |
| 210 |
st[ 0 ] = size; |
| 211 |
} |
| 212 |
else if (size > st[ 1 ]) |
| 213 |
{ |
| 214 |
defaultSTAxis[ 1 ] = i; |
| 215 |
st[ 1 ] = size; |
| 216 |
} |
| 217 |
} |
| 218 |
defaultXYZtoSTScale[ 0 ] = 4.f / st[ 0 ]; |
| 219 |
defaultXYZtoSTScale[ 1 ] = 4.f / st[ 1 ]; |
| 220 |
|
| 221 |
/* LWO surfaces become pico surfaces */ |
| 222 |
surface = obj->surf; |
| 223 |
while (surface) |
| 224 |
{ |
| 225 |
/* allocate new pico surface */ |
| 226 |
picoSurface = PicoNewSurface( picoModel ); |
| 227 |
if (picoSurface == NULL) |
| 228 |
{ |
| 229 |
_pico_printf( PICO_ERROR, "Unable to allocate a new model surface" ); |
| 230 |
PicoFreeModel( picoModel ); |
| 231 |
lwFreeObject( obj ); |
| 232 |
return NULL; |
| 233 |
} |
| 234 |
|
| 235 |
/* LWO model surfaces are all triangle meshes */ |
| 236 |
PicoSetSurfaceType( picoSurface, PICO_TRIANGLES ); |
| 237 |
|
| 238 |
/* set surface name */ |
| 239 |
PicoSetSurfaceName( picoSurface, surface->name ); |
| 240 |
|
| 241 |
/* create new pico shader */ |
| 242 |
picoShader = PicoNewShader( picoModel ); |
| 243 |
if (picoShader == NULL) |
| 244 |
{ |
| 245 |
_pico_printf( PICO_ERROR, "Unable to allocate a new model shader" ); |
| 246 |
PicoFreeModel( picoModel ); |
| 247 |
lwFreeObject( obj ); |
| 248 |
return NULL; |
| 249 |
} |
| 250 |
|
| 251 |
/* detox and set shader name */ |
| 252 |
memset(name, 0, sizeof(name)); |
| 253 |
strncpy( name, surface->name, sizeof(name) - 1 ); |
| 254 |
_pico_first_token( name ); |
| 255 |
_pico_setfext( name, "" ); |
| 256 |
_pico_unixify( name ); |
| 257 |
PicoSetShaderName( picoShader, name ); |
| 258 |
|
| 259 |
/* associate current surface with newly created shader */ |
| 260 |
PicoSetSurfaceShader( picoSurface, picoShader ); |
| 261 |
|
| 262 |
/* copy indices and vertex data */ |
| 263 |
numverts = 0; |
| 264 |
|
| 265 |
hashTable = PicoNewVertexCombinationHashTable(); |
| 266 |
|
| 267 |
if (hashTable == NULL) |
| 268 |
{ |
| 269 |
_pico_printf( PICO_ERROR, "Unable to allocate hash table" ); |
| 270 |
PicoFreeModel( picoModel ); |
| 271 |
lwFreeObject( obj ); |
| 272 |
return NULL; |
| 273 |
} |
| 274 |
|
| 275 |
/* Local polygon index for this surface. We don't want to use i in the loop |
| 276 |
* since that is the global poly index for the entire layer */ |
| 277 |
surfacePolyCount = 0; |
| 278 |
|
| 279 |
for( i = 0, pol = layer->polygon.pol; i < layer->polygon.count; i++, pol++ ) |
| 280 |
{ |
| 281 |
/* does this polygon belong to this surface? */ |
| 282 |
if (pol->surf != surface) |
| 283 |
continue; |
| 284 |
|
| 285 |
/* we only support polygons of the FACE type */ |
| 286 |
if (pol->type != ID_FACE) |
| 287 |
{ |
| 288 |
_pico_printf( PICO_WARNING, "LWO loader discarded a polygon because it's type != FACE (%s)", lwo_lwIDToStr( pol->type ) ); |
| 289 |
continue; |
| 290 |
} |
| 291 |
|
| 292 |
/* NOTE: LWO has support for non-convex polygons, do we want to store them as well? */ |
| 293 |
if (pol->nverts != 3) |
| 294 |
{ |
| 295 |
_pico_printf( PICO_WARNING, "LWO loader discarded a polygon because it has != 3 verts (%d)", pol->nverts ); |
| 296 |
continue; |
| 297 |
} |
| 298 |
|
| 299 |
for( j = 0, v = pol->v; j < 3; j++, v++ ) |
| 300 |
{ |
| 301 |
pt = &layer->point.pt[ v->index ]; |
| 302 |
|
| 303 |
/* setup data */ |
| 304 |
xyz[ 0 ] = pt->pos[ 0 ]; |
| 305 |
xyz[ 1 ] = pt->pos[ 2 ]; |
| 306 |
xyz[ 2 ] = pt->pos[ 1 ]; |
| 307 |
|
| 308 |
/* doom3 lwo data doesn't seem to have smoothing-angle information */ |
| 309 |
#if 0 |
| 310 |
if(surface->smooth <= 0) |
| 311 |
{ |
| 312 |
/* use face normals */ |
| 313 |
normal[ 0 ] = v->norm[ 0 ]; |
| 314 |
normal[ 1 ] = v->norm[ 2 ]; |
| 315 |
normal[ 2 ] = v->norm[ 1 ]; |
| 316 |
} |
| 317 |
else |
| 318 |
#endif |
| 319 |
{ |
| 320 |
/* smooth normals later */ |
| 321 |
normal[ 0 ] = 0; |
| 322 |
normal[ 1 ] = 0; |
| 323 |
normal[ 2 ] = 0; |
| 324 |
} |
| 325 |
|
| 326 |
st[ 0 ] = xyz[ defaultSTAxis[ 0 ] ] * defaultXYZtoSTScale[ 0 ]; |
| 327 |
st[ 1 ] = xyz[ defaultSTAxis[ 1 ] ] * defaultXYZtoSTScale[ 1 ]; |
| 328 |
|
| 329 |
color[ 0 ] = (picoByte_t)(surface->color.rgb[ 0 ] * surface->diffuse.val * 0xFF); |
| 330 |
color[ 1 ] = (picoByte_t)(surface->color.rgb[ 1 ] * surface->diffuse.val * 0xFF); |
| 331 |
color[ 2 ] = (picoByte_t)(surface->color.rgb[ 2 ] * surface->diffuse.val * 0xFF); |
| 332 |
color[ 3 ] = 0xFF; |
| 333 |
|
| 334 |
/* set from points */ |
| 335 |
for( k = 0, vm = pt->vm; k < pt->nvmaps; k++, vm++ ) |
| 336 |
{ |
| 337 |
if (vm->vmap->type == LWID_('T','X','U','V')) |
| 338 |
{ |
| 339 |
/* set st coords */ |
| 340 |
st[ 0 ] = vm->vmap->val[ vm->index ][ 0 ]; |
| 341 |
st[ 1 ] = 1.f - vm->vmap->val[ vm->index ][ 1 ]; |
| 342 |
} |
| 343 |
else if (vm->vmap->type == LWID_('R','G','B','A')) |
| 344 |
{ |
| 345 |
/* set rgba */ |
| 346 |
color[ 0 ] = (picoByte_t)(vm->vmap->val[ vm->index ][ 0 ] * surface->color.rgb[ 0 ] * surface->diffuse.val * 0xFF); |
| 347 |
color[ 1 ] = (picoByte_t)(vm->vmap->val[ vm->index ][ 1 ] * surface->color.rgb[ 1 ] * surface->diffuse.val * 0xFF); |
| 348 |
color[ 2 ] = (picoByte_t)(vm->vmap->val[ vm->index ][ 2 ] * surface->color.rgb[ 2 ] * surface->diffuse.val * 0xFF); |
| 349 |
color[ 3 ] = (picoByte_t)(vm->vmap->val[ vm->index ][ 3 ] * 0xFF); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
/* override with polygon data */ |
| 354 |
for( k = 0, vm = v->vm; k < v->nvmaps; k++, vm++ ) |
| 355 |
{ |
| 356 |
if (vm->vmap->type == LWID_('T','X','U','V')) |
| 357 |
{ |
| 358 |
/* set st coords */ |
| 359 |
st[ 0 ] = vm->vmap->val[ vm->index ][ 0 ]; |
| 360 |
st[ 1 ] = 1.f - vm->vmap->val[ vm->index ][ 1 ]; |
| 361 |
} |
| 362 |
else if (vm->vmap->type == LWID_('R','G','B','A')) |
| 363 |
{ |
| 364 |
/* set rgba */ |
| 365 |
color[ 0 ] = (picoByte_t)(vm->vmap->val[ vm->index ][ 0 ] * surface->color.rgb[ 0 ] * surface->diffuse.val * 0xFF); |
| 366 |
color[ 1 ] = (picoByte_t)(vm->vmap->val[ vm->index ][ 1 ] * surface->color.rgb[ 1 ] * surface->diffuse.val * 0xFF); |
| 367 |
color[ 2 ] = (picoByte_t)(vm->vmap->val[ vm->index ][ 2 ] * surface->color.rgb[ 2 ] * surface->diffuse.val * 0xFF); |
| 368 |
color[ 3 ] = (picoByte_t)(vm->vmap->val[ vm->index ][ 3 ] * 0xFF); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
/* find vertex in this surface and if we can't find it there create it */ |
| 373 |
vertexCombinationHash = PicoFindVertexCombinationInHashTable( hashTable, xyz, normal, st, color ); |
| 374 |
|
| 375 |
if (vertexCombinationHash) |
| 376 |
{ |
| 377 |
/* found an existing one */ |
| 378 |
PicoSetSurfaceIndex( picoSurface, (surfacePolyCount * 3 + j ), vertexCombinationHash->index ); |
| 379 |
} |
| 380 |
else |
| 381 |
{ |
| 382 |
/* it is a new one */ |
| 383 |
vertexCombinationHash = PicoAddVertexCombinationToHashTable( hashTable, xyz, normal, st, color, (picoIndex_t) numverts ); |
| 384 |
|
| 385 |
if (vertexCombinationHash == NULL) |
| 386 |
{ |
| 387 |
_pico_printf( PICO_ERROR, "Unable to allocate hash bucket entry table" ); |
| 388 |
PicoFreeVertexCombinationHashTable( hashTable ); |
| 389 |
PicoFreeModel( picoModel ); |
| 390 |
lwFreeObject( obj ); |
| 391 |
return NULL; |
| 392 |
} |
| 393 |
|
| 394 |
/* add the vertex to this surface */ |
| 395 |
PicoSetSurfaceXYZ( picoSurface, numverts, xyz ); |
| 396 |
|
| 397 |
/* set dummy normal */ |
| 398 |
PicoSetSurfaceNormal( picoSurface, numverts, normal ); |
| 399 |
|
| 400 |
/* set color */ |
| 401 |
PicoSetSurfaceColor( picoSurface, 0, numverts, color ); |
| 402 |
|
| 403 |
/* set st coords */ |
| 404 |
PicoSetSurfaceST( picoSurface, 0, numverts, st ); |
| 405 |
|
| 406 |
/* set index */ |
| 407 |
PicoSetSurfaceIndex( picoSurface, (surfacePolyCount * 3 + j ), (picoIndex_t) numverts ); |
| 408 |
|
| 409 |
numverts++; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
/* We haven't discarded this polygon, so bump the surface polycount */ |
| 414 |
surfacePolyCount++; |
| 415 |
} |
| 416 |
|
| 417 |
/* free the hashtable */ |
| 418 |
PicoFreeVertexCombinationHashTable( hashTable ); |
| 419 |
|
| 420 |
/* get next surface */ |
| 421 |
surface = surface->next; |
| 422 |
} |
| 423 |
|
| 424 |
#ifdef DEBUG_PM_LWO |
| 425 |
load_start = convert_finish = clock(); |
| 426 |
#endif |
| 427 |
|
| 428 |
lwFreeObject( obj ); |
| 429 |
|
| 430 |
#ifdef DEBUG_PM_LWO |
| 431 |
load_finish = clock(); |
| 432 |
load_elapsed += (double)(load_finish - load_start) / CLOCKS_PER_SEC; |
| 433 |
convert_elapsed = (double)(convert_finish - convert_start) / CLOCKS_PER_SEC; |
| 434 |
_pico_printf( PICO_NORMAL, "Loaded model in in %-.2f second(s) (loading: %-.2fs converting: %-.2fs)\n", load_elapsed + convert_elapsed, load_elapsed, convert_elapsed ); |
| 435 |
#endif |
| 436 |
|
| 437 |
/* return the new pico model */ |
| 438 |
return picoModel; |
| 439 |
} |
| 440 |
|
| 441 |
/* pico file format module definition */ |
| 442 |
const picoModule_t picoModuleLWO = |
| 443 |
{ |
| 444 |
"1.0", /* module version string */ |
| 445 |
"LightWave Object", /* module display name */ |
| 446 |
"Arnout van Meer", /* author's name */ |
| 447 |
"2003 Arnout van Meer, 2000 Ernie Wright", /* module copyright */ |
| 448 |
{ |
| 449 |
"lwo", NULL, NULL, NULL /* default extensions to use */ |
| 450 |
}, |
| 451 |
_lwo_canload, /* validation routine */ |
| 452 |
_lwo_load, /* load routine */ |
| 453 |
NULL, /* save validation routine */ |
| 454 |
NULL /* save routine */ |
| 455 |
}; |