| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#include "picointernal.h" |
| 37 |
#include "lwo/lwo2.h" |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#ifdef DEBUG_PM_LWO |
| 43 |
#include "time.h" |
| 44 |
#endif |
| 45 |
|
| 46 |
|
| 47 |
static const char *lwo_lwIDToStr( unsigned int lwID ){ |
| 48 |
static char lwIDStr[5]; |
| 49 |
|
| 50 |
if ( !lwID ) { |
| 51 |
return "n/a"; |
| 52 |
} |
| 53 |
|
| 54 |
lwIDStr[ 0 ] = (char)( ( lwID ) >> 24 ); |
| 55 |
lwIDStr[ 1 ] = (char)( ( lwID ) >> 16 ); |
| 56 |
lwIDStr[ 2 ] = (char)( ( lwID ) >> 8 ); |
| 57 |
lwIDStr[ 3 ] = (char)( ( lwID ) ); |
| 58 |
lwIDStr[ 4 ] = '\0'; |
| 59 |
|
| 60 |
return lwIDStr; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
static int _lwo_canload( PM_PARAMS_CANLOAD ){ |
| 70 |
picoMemStream_t *s; |
| 71 |
unsigned int failID = 0; |
| 72 |
int failpos = -1; |
| 73 |
int ret; |
| 74 |
|
| 75 |
|
| 76 |
s = _pico_new_memstream( (const picoByte_t *)buffer, bufSize ); |
| 77 |
if ( s == NULL ) { |
| 78 |
return PICO_PMV_ERROR_MEMORY; |
| 79 |
} |
| 80 |
|
| 81 |
ret = lwValidateObject( fileName, s, &failID, &failpos ); |
| 82 |
|
| 83 |
_pico_free_memstream( s ); |
| 84 |
|
| 85 |
return ret; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
static picoModel_t *_lwo_load( PM_PARAMS_LOAD ){ |
| 93 |
picoMemStream_t *s; |
| 94 |
unsigned int failID = 0; |
| 95 |
int failpos = -1; |
| 96 |
lwObject *obj; |
| 97 |
lwSurface *surface; |
| 98 |
lwLayer *layer; |
| 99 |
lwPoint *pt; |
| 100 |
lwPolygon *pol; |
| 101 |
lwPolVert *v; |
| 102 |
lwVMapPt *vm; |
| 103 |
char name[256]; |
| 104 |
name[255] = '\0'; |
| 105 |
int i, j, k, numverts; |
| 106 |
|
| 107 |
picoModel_t *picoModel; |
| 108 |
picoSurface_t *picoSurface; |
| 109 |
picoShader_t *picoShader; |
| 110 |
picoVec3_t xyz, normal; |
| 111 |
picoVec2_t st; |
| 112 |
picoColor_t color; |
| 113 |
|
| 114 |
int defaultSTAxis[ 2 ]; |
| 115 |
picoVec2_t defaultXYZtoSTScale; |
| 116 |
|
| 117 |
picoVertexCombinationHash_t **hashTable; |
| 118 |
picoVertexCombinationHash_t *vertexCombinationHash; |
| 119 |
|
| 120 |
#ifdef DEBUG_PM_LWO |
| 121 |
clock_t load_start, load_finish, convert_start, convert_finish; |
| 122 |
double load_elapsed, convert_elapsed; |
| 123 |
|
| 124 |
load_start = clock(); |
| 125 |
#endif |
| 126 |
|
| 127 |
|
| 128 |
if ( frameNum < 0 || frameNum >= 1 ) { |
| 129 |
_pico_printf( PICO_ERROR, "Invalid or out-of-range LWO frame specified" ); |
| 130 |
return NULL; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
s = _pico_new_memstream( (const picoByte_t *)buffer, bufSize ); |
| 135 |
if ( s == NULL ) { |
| 136 |
return NULL; |
| 137 |
} |
| 138 |
|
| 139 |
obj = lwGetObject( fileName, s, &failID, &failpos ); |
| 140 |
|
| 141 |
_pico_free_memstream( s ); |
| 142 |
|
| 143 |
if ( !obj ) { |
| 144 |
_pico_printf( PICO_ERROR, "Couldn't load LWO file, failed on ID '%s', position %d", lwo_lwIDToStr( failID ), failpos ); |
| 145 |
return NULL; |
| 146 |
} |
| 147 |
|
| 148 |
#ifdef DEBUG_PM_LWO |
| 149 |
convert_start = load_finish = clock(); |
| 150 |
load_elapsed = (double)( load_finish - load_start ) / CLOCKS_PER_SEC; |
| 151 |
#endif |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
picoModel = PicoNewModel(); |
| 159 |
if ( picoModel == NULL ) { |
| 160 |
_pico_printf( PICO_ERROR, "Unable to allocate a new model" ); |
| 161 |
return NULL; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
PicoSetModelFrameNum( picoModel, frameNum ); |
| 166 |
PicoSetModelNumFrames( picoModel, 1 ); |
| 167 |
PicoSetModelName( picoModel, fileName ); |
| 168 |
PicoSetModelFileName( picoModel, fileName ); |
| 169 |
|
| 170 |
|
| 171 |
layer = &obj->layer[0]; |
| 172 |
|
| 173 |
|
| 174 |
if ( obj->nlayers > 1 ) { |
| 175 |
_pico_printf( PICO_WARNING, "LWO loader discards any geometry data not in Layer 1 (%d layers found)", obj->nlayers ); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
normal[ 0 ] = normal[ 1 ] = normal[ 2 ] = 0.f; |
| 180 |
|
| 181 |
|
| 182 |
st[ 0 ] = st[ 1 ] = 0.f; |
| 183 |
defaultSTAxis[ 0 ] = 0; |
| 184 |
defaultSTAxis[ 1 ] = 1; |
| 185 |
for ( i = 0; i < 3; i++ ) |
| 186 |
{ |
| 187 |
float min = layer->bbox[ i ]; |
| 188 |
float max = layer->bbox[ i + 3 ]; |
| 189 |
float size = max - min; |
| 190 |
|
| 191 |
if ( size > st[ 0 ] ) { |
| 192 |
defaultSTAxis[ 1 ] = defaultSTAxis[ 0 ]; |
| 193 |
defaultSTAxis[ 0 ] = i; |
| 194 |
|
| 195 |
st[ 1 ] = st[ 0 ]; |
| 196 |
st[ 0 ] = size; |
| 197 |
} |
| 198 |
else if ( size > st[ 1 ] ) { |
| 199 |
defaultSTAxis[ 1 ] = i; |
| 200 |
st[ 1 ] = size; |
| 201 |
} |
| 202 |
} |
| 203 |
defaultXYZtoSTScale[ 0 ] = 4.f / st[ 0 ]; |
| 204 |
defaultXYZtoSTScale[ 1 ] = 4.f / st[ 1 ]; |
| 205 |
|
| 206 |
|
| 207 |
surface = obj->surf; |
| 208 |
while ( surface ) |
| 209 |
{ |
| 210 |
|
| 211 |
picoSurface = PicoNewSurface( picoModel ); |
| 212 |
if ( picoSurface == NULL ) { |
| 213 |
_pico_printf( PICO_ERROR, "Unable to allocate a new model surface" ); |
| 214 |
PicoFreeModel( picoModel ); |
| 215 |
lwFreeObject( obj ); |
| 216 |
return NULL; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
PicoSetSurfaceType( picoSurface, PICO_TRIANGLES ); |
| 221 |
|
| 222 |
|
| 223 |
PicoSetSurfaceName( picoSurface, surface->name ); |
| 224 |
|
| 225 |
|
| 226 |
picoShader = PicoNewShader( picoModel ); |
| 227 |
if ( picoShader == NULL ) { |
| 228 |
_pico_printf( PICO_ERROR, "Unable to allocate a new model shader" ); |
| 229 |
PicoFreeModel( picoModel ); |
| 230 |
lwFreeObject( obj ); |
| 231 |
return NULL; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
strncpy( name, surface->name, sizeof( name ) - 1 ); |
| 236 |
_pico_first_token( name ); |
| 237 |
_pico_setfext( name, NULL ); |
| 238 |
_pico_unixify( name ); |
| 239 |
PicoSetShaderName( picoShader, name ); |
| 240 |
|
| 241 |
|
| 242 |
PicoSetSurfaceShader( picoSurface, picoShader ); |
| 243 |
|
| 244 |
|
| 245 |
numverts = 0; |
| 246 |
|
| 247 |
hashTable = PicoNewVertexCombinationHashTable(); |
| 248 |
|
| 249 |
if ( hashTable == NULL ) { |
| 250 |
_pico_printf( PICO_ERROR, "Unable to allocate hash table" ); |
| 251 |
PicoFreeModel( picoModel ); |
| 252 |
lwFreeObject( obj ); |
| 253 |
return NULL; |
| 254 |
} |
| 255 |
|
| 256 |
for ( i = 0, pol = layer->polygon.pol; i < layer->polygon.count; i++, pol++ ) |
| 257 |
{ |
| 258 |
|
| 259 |
if ( pol->surf != surface ) { |
| 260 |
continue; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
if ( pol->type != ID_FACE ) { |
| 265 |
_pico_printf( PICO_WARNING, "LWO loader discarded a polygon because it's type != FACE (%s)", lwo_lwIDToStr( pol->type ) ); |
| 266 |
continue; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
if ( pol->nverts != 3 ) { |
| 271 |
_pico_printf( PICO_WARNING, "LWO loader discarded a polygon because it has != 3 verts (%d)", pol->nverts ); |
| 272 |
continue; |
| 273 |
} |
| 274 |
|
| 275 |
for ( j = 0, v = pol->v; j < 3; j++, v++ ) |
| 276 |
{ |
| 277 |
pt = &layer->point.pt[ v->index ]; |
| 278 |
|
| 279 |
|
| 280 |
xyz[ 0 ] = pt->pos[ 0 ]; |
| 281 |
xyz[ 1 ] = pt->pos[ 2 ]; |
| 282 |
xyz[ 2 ] = pt->pos[ 1 ]; |
| 283 |
|
| 284 |
|
| 285 |
#if 0 |
| 286 |
if ( surface->smooth <= 0 ) { |
| 287 |
|
| 288 |
normal[ 0 ] = v->norm[ 0 ]; |
| 289 |
normal[ 1 ] = v->norm[ 2 ]; |
| 290 |
normal[ 2 ] = v->norm[ 1 ]; |
| 291 |
} |
| 292 |
else |
| 293 |
#endif |
| 294 |
{ |
| 295 |
|
| 296 |
normal[ 0 ] = 0; |
| 297 |
normal[ 1 ] = 0; |
| 298 |
normal[ 2 ] = 0; |
| 299 |
} |
| 300 |
|
| 301 |
st[ 0 ] = xyz[ defaultSTAxis[ 0 ] ] * defaultXYZtoSTScale[ 0 ]; |
| 302 |
st[ 1 ] = xyz[ defaultSTAxis[ 1 ] ] * defaultXYZtoSTScale[ 1 ]; |
| 303 |
|
| 304 |
color[ 0 ] = (picoByte_t)( surface->color.rgb[ 0 ] * surface->diffuse.val * 0xFF ); |
| 305 |
color[ 1 ] = (picoByte_t)( surface->color.rgb[ 1 ] * surface->diffuse.val * 0xFF ); |
| 306 |
color[ 2 ] = (picoByte_t)( surface->color.rgb[ 2 ] * surface->diffuse.val * 0xFF ); |
| 307 |
color[ 3 ] = 0xFF; |
| 308 |
|
| 309 |
|
| 310 |
for ( k = 0, vm = pt->vm; k < pt->nvmaps; k++, vm++ ) |
| 311 |
{ |
| 312 |
if ( vm->vmap->type == LWID_( 'T','X','U','V' ) ) { |
| 313 |
|
| 314 |
st[ 0 ] = vm->vmap->val[ vm->index ][ 0 ]; |
| 315 |
st[ 1 ] = 1.f - vm->vmap->val[ vm->index ][ 1 ]; |
| 316 |
} |
| 317 |
else if ( vm->vmap->type == LWID_( 'R','G','B','A' ) ) { |
| 318 |
|
| 319 |
color[ 0 ] = (picoByte_t)( vm->vmap->val[ vm->index ][ 0 ] * surface->color.rgb[ 0 ] * surface->diffuse.val * 0xFF ); |
| 320 |
color[ 1 ] = (picoByte_t)( vm->vmap->val[ vm->index ][ 1 ] * surface->color.rgb[ 1 ] * surface->diffuse.val * 0xFF ); |
| 321 |
color[ 2 ] = (picoByte_t)( vm->vmap->val[ vm->index ][ 2 ] * surface->color.rgb[ 2 ] * surface->diffuse.val * 0xFF ); |
| 322 |
color[ 3 ] = (picoByte_t)( vm->vmap->val[ vm->index ][ 3 ] * 0xFF ); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
for ( k = 0, vm = v->vm; k < v->nvmaps; k++, vm++ ) |
| 328 |
{ |
| 329 |
if ( vm->vmap->type == LWID_( 'T','X','U','V' ) ) { |
| 330 |
|
| 331 |
st[ 0 ] = vm->vmap->val[ vm->index ][ 0 ]; |
| 332 |
st[ 1 ] = 1.f - vm->vmap->val[ vm->index ][ 1 ]; |
| 333 |
} |
| 334 |
else if ( vm->vmap->type == LWID_( 'R','G','B','A' ) ) { |
| 335 |
|
| 336 |
color[ 0 ] = (picoByte_t)( vm->vmap->val[ vm->index ][ 0 ] * surface->color.rgb[ 0 ] * surface->diffuse.val * 0xFF ); |
| 337 |
color[ 1 ] = (picoByte_t)( vm->vmap->val[ vm->index ][ 1 ] * surface->color.rgb[ 1 ] * surface->diffuse.val * 0xFF ); |
| 338 |
color[ 2 ] = (picoByte_t)( vm->vmap->val[ vm->index ][ 2 ] * surface->color.rgb[ 2 ] * surface->diffuse.val * 0xFF ); |
| 339 |
color[ 3 ] = (picoByte_t)( vm->vmap->val[ vm->index ][ 3 ] * 0xFF ); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
vertexCombinationHash = PicoFindVertexCombinationInHashTable( hashTable, xyz, normal, st, color ); |
| 345 |
|
| 346 |
if ( vertexCombinationHash ) { |
| 347 |
|
| 348 |
PicoSetSurfaceIndex( picoSurface, ( i * 3 + j ), vertexCombinationHash->index ); |
| 349 |
} |
| 350 |
else |
| 351 |
{ |
| 352 |
|
| 353 |
vertexCombinationHash = PicoAddVertexCombinationToHashTable( hashTable, xyz, normal, st, color, (picoIndex_t) numverts ); |
| 354 |
|
| 355 |
if ( vertexCombinationHash == NULL ) { |
| 356 |
_pico_printf( PICO_ERROR, "Unable to allocate hash bucket entry table" ); |
| 357 |
PicoFreeVertexCombinationHashTable( hashTable ); |
| 358 |
PicoFreeModel( picoModel ); |
| 359 |
lwFreeObject( obj ); |
| 360 |
return NULL; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
PicoSetSurfaceXYZ( picoSurface, numverts, xyz ); |
| 365 |
|
| 366 |
|
| 367 |
PicoSetSurfaceNormal( picoSurface, numverts, normal ); |
| 368 |
|
| 369 |
|
| 370 |
PicoSetSurfaceColor( picoSurface, 0, numverts, color ); |
| 371 |
|
| 372 |
|
| 373 |
PicoSetSurfaceST( picoSurface, 0, numverts, st ); |
| 374 |
|
| 375 |
|
| 376 |
PicoSetSurfaceIndex( picoSurface, ( i * 3 + j ), (picoIndex_t) numverts ); |
| 377 |
|
| 378 |
numverts++; |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
PicoFreeVertexCombinationHashTable( hashTable ); |
| 385 |
|
| 386 |
|
| 387 |
surface = surface->next; |
| 388 |
} |
| 389 |
|
| 390 |
#ifdef DEBUG_PM_LWO |
| 391 |
load_start = convert_finish = clock(); |
| 392 |
#endif |
| 393 |
|
| 394 |
lwFreeObject( obj ); |
| 395 |
|
| 396 |
#ifdef DEBUG_PM_LWO |
| 397 |
load_finish = clock(); |
| 398 |
load_elapsed += (double)( load_finish - load_start ) / CLOCKS_PER_SEC; |
| 399 |
convert_elapsed = (double)( convert_finish - convert_start ) / CLOCKS_PER_SEC; |
| 400 |
_pico_printf( PICO_NORMAL, "Loaded model in in %-.2f second(s) (loading: %-.2fs converting: %-.2fs)\n", load_elapsed + convert_elapsed, load_elapsed, convert_elapsed ); |
| 401 |
#endif |
| 402 |
|
| 403 |
|
| 404 |
return picoModel; |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
const picoModule_t picoModuleLWO = |
| 409 |
{ |
| 410 |
"1.0", |
| 411 |
"LightWave Object", |
| 412 |
"Arnout van Meer", |
| 413 |
"2003 Arnout van Meer, 2000 Ernie Wright", |
| 414 |
{ |
| 415 |
"lwo", NULL, NULL, NULL |
| 416 |
}, |
| 417 |
_lwo_canload, |
| 418 |
_lwo_load, |
| 419 |
NULL, |
| 420 |
NULL |
| 421 |
}; |