| 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 |
|
|---|
| 37 |
|
|---|
| 38 |
#define PM_OBJ_C |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
#include "picointernal.h" |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
#ifdef _WIN32 |
|---|
| 45 |
#pragma warning( disable:4100 ) |
|---|
| 46 |
#endif |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
typedef struct SObjVertexData |
|---|
| 60 |
{ |
|---|
| 61 |
picoVec3_t v; |
|---|
| 62 |
picoVec2_t vt; |
|---|
| 63 |
picoVec3_t vn; |
|---|
| 64 |
} |
|---|
| 65 |
TObjVertexData; |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
static int _obj_canload( PM_PARAMS_CANLOAD ) |
|---|
| 71 |
{ |
|---|
| 72 |
picoParser_t *p; |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
if (bufSize < 30) |
|---|
| 76 |
return PICO_PMV_ERROR_SIZE; |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
if (_pico_stristr(fileName,".obj") != NULL || |
|---|
| 81 |
_pico_stristr(fileName,".wf" ) != NULL) |
|---|
| 82 |
{ |
|---|
| 83 |
return PICO_PMV_OK; |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
p = _pico_new_parser( (picoByte_t *)buffer,bufSize ); |
|---|
| 91 |
if (p == NULL) |
|---|
| 92 |
return PICO_PMV_ERROR_MEMORY; |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
while( 1 ) |
|---|
| 96 |
{ |
|---|
| 97 |
|
|---|
| 98 |
if (_pico_parse_first( p ) == NULL) |
|---|
| 99 |
break; |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
if (p->curLine > 80) |
|---|
| 103 |
break; |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
if (p->token == NULL || !strlen( p->token )) |
|---|
| 107 |
continue; |
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
if (!_pico_stricmp(p->token,"usemtl") || |
|---|
| 111 |
!_pico_stricmp(p->token,"mtllib") || |
|---|
| 112 |
!_pico_stricmp(p->token,"g") || |
|---|
| 113 |
!_pico_stricmp(p->token,"v")) |
|---|
| 114 |
{ |
|---|
| 115 |
|
|---|
| 116 |
_pico_free_parser( p ); |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
return PICO_PMV_OK; |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
_pico_parse_skip_rest( p ); |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
_pico_free_parser( p ); |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
return PICO_PMV_ERROR; |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
#define SIZE_OBJ_STEP 4096 |
|---|
| 137 |
|
|---|
| 138 |
static TObjVertexData *SizeObjVertexData( |
|---|
| 139 |
TObjVertexData *vertexData, int reqEntries, |
|---|
| 140 |
int *entries, int *allocated) |
|---|
| 141 |
{ |
|---|
| 142 |
int newAllocated; |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
if (reqEntries < 1) |
|---|
| 146 |
return NULL; |
|---|
| 147 |
if (entries == NULL || allocated == NULL) |
|---|
| 148 |
return NULL; |
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
if (vertexData && (reqEntries < *allocated)) |
|---|
| 152 |
{ |
|---|
| 153 |
*entries = reqEntries; |
|---|
| 154 |
return vertexData; |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
if (vertexData == NULL) |
|---|
| 158 |
{ |
|---|
| 159 |
|
|---|
| 160 |
newAllocated = (reqEntries > SIZE_OBJ_STEP) ? |
|---|
| 161 |
reqEntries : SIZE_OBJ_STEP; |
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
#ifdef DEBUG_PM_OBJ_EX |
|---|
| 165 |
printf("SizeObjVertexData: allocate (%d entries)\n", |
|---|
| 166 |
newAllocated); |
|---|
| 167 |
#endif |
|---|
| 168 |
|
|---|
| 169 |
vertexData = (TObjVertexData *) |
|---|
| 170 |
_pico_alloc( sizeof(TObjVertexData) * newAllocated ); |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
if (vertexData == NULL) |
|---|
| 174 |
return NULL; |
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
*allocated = newAllocated; |
|---|
| 178 |
*entries = reqEntries; |
|---|
| 179 |
return vertexData; |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
if (reqEntries == *allocated) |
|---|
| 183 |
{ |
|---|
| 184 |
newAllocated = (*allocated + SIZE_OBJ_STEP); |
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
#ifdef DEBUG_PM_OBJ_EX |
|---|
| 188 |
printf("SizeObjVertexData: reallocate (%d entries)\n", |
|---|
| 189 |
newAllocated); |
|---|
| 190 |
#endif |
|---|
| 191 |
|
|---|
| 192 |
vertexData = (TObjVertexData *) |
|---|
| 193 |
_pico_realloc( (void *)&vertexData, |
|---|
| 194 |
sizeof(TObjVertexData) * (*allocated), |
|---|
| 195 |
sizeof(TObjVertexData) * (newAllocated)); |
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
if (vertexData == NULL) |
|---|
| 199 |
return NULL; |
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
*allocated = newAllocated; |
|---|
| 203 |
*entries = reqEntries; |
|---|
| 204 |
return vertexData; |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
return NULL; |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
static void FreeObjVertexData( TObjVertexData *vertexData ) |
|---|
| 211 |
{ |
|---|
| 212 |
if (vertexData != NULL) |
|---|
| 213 |
{ |
|---|
| 214 |
free( (TObjVertexData *)vertexData ); |
|---|
| 215 |
} |
|---|
| 216 |
} |
|---|
| 217 |
|
|---|
| 218 |
#if 0 |
|---|
| 219 |
static int _obj_mtl_load( picoModel_t *model ) |
|---|
| 220 |
{ |
|---|
| 221 |
|
|---|
| 222 |
picoParser_t *p; |
|---|
| 223 |
picoByte_t *mtlBuffer; |
|---|
| 224 |
int mtlBufSize; |
|---|
| 225 |
char *fileName; |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
if( model == NULL || model->fileName == NULL ) |
|---|
| 229 |
return 0; |
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
if (!strlen( model->fileName )) |
|---|
| 233 |
return 0; |
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
#define _obj_mtl_error_return \ |
|---|
| 237 |
{ \ |
|---|
| 238 |
_pico_free_parser( p ); \ |
|---|
| 239 |
_pico_free_file( mtlBuffer ); \ |
|---|
| 240 |
_pico_free( fileName ); \ |
|---|
| 241 |
return 0; \ |
|---|
| 242 |
} |
|---|
| 243 |
|
|---|
| 244 |
fileName = _pico_clone_alloc( model->fileName,-1 ); |
|---|
| 245 |
if (fileName == NULL) |
|---|
| 246 |
return 0; |
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
_pico_setfext( fileName, "mtl" ); |
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
_pico_load_file( fileName,&mtlBuffer,&mtlBufSize ); |
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
if (mtlBufSize == 0) return 1; |
|---|
| 256 |
if (mtlBufSize < 0) return 0; |
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
p = _pico_new_parser( mtlBuffer, mtlBufSize ); |
|---|
| 260 |
if (p == NULL) |
|---|
| 261 |
_obj_mtl_error_return; |
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
while( 1 ) |
|---|
| 265 |
{ |
|---|
| 266 |
|
|---|
| 267 |
if (_pico_parse( p,1 ) == NULL) |
|---|
| 268 |
break; |
|---|
| 269 |
#if 0 |
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
if (p->token == NULL || !strlen( p->token )) |
|---|
| 273 |
continue; |
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
if (p->token[0] == '#') |
|---|
| 277 |
{ |
|---|
| 278 |
_pico_parse_skip_rest( p ); |
|---|
| 279 |
continue; |
|---|
| 280 |
} |
|---|
| 281 |
|
|---|
| 282 |
if (!_pico_stricmp(p->token,"newmtl")) |
|---|
| 283 |
{ |
|---|
| 284 |
picoShader_t *shader; |
|---|
| 285 |
char *name; |
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
name = _pico_parse( p,0 ); |
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
if (name == NULL || !strlen(name)) |
|---|
| 292 |
{ |
|---|
| 293 |
_pico_printf( PICO_ERROR,"Missing material name in MTL, line %d.",p->curLine); |
|---|
| 294 |
_obj_mtl_error_return; |
|---|
| 295 |
} |
|---|
| 296 |
|
|---|
| 297 |
shader = PicoNewShader( model ); |
|---|
| 298 |
if (shader == NULL) |
|---|
| 299 |
_obj_mtl_error_return; |
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
PicoSetShaderName( shader,name ); |
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
curShader = shader; |
|---|
| 306 |
} |
|---|
| 307 |
|
|---|
| 308 |
else if (!_pico_stricmp(p->token,"map_kd")) |
|---|
| 309 |
{ |
|---|
| 310 |
char *mapName; |
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
if (curShader == NULL) |
|---|
| 314 |
_obj_mtl_error_return; |
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
mapName = _pico_parse( p,0 ); |
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
if (mapName == NULL || !strlen(mapName)) |
|---|
| 321 |
{ |
|---|
| 322 |
_pico_printf( PICO_ERROR,"Missing material map name in MTL, line %d.",p->curLine); |
|---|
| 323 |
_obj_mtl_error_return; |
|---|
| 324 |
} |
|---|
| 325 |
|
|---|
| 326 |
PicoSetShaderMapName( shader,mapName ); |
|---|
| 327 |
} |
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
else if (!_pico_stricmp(p->token,"d")) |
|---|
| 331 |
{ |
|---|
| 332 |
picoByte_t *diffuse; |
|---|
| 333 |
float value; |
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 |
if (!_pico_parse_float( p,&value )) |
|---|
| 338 |
_obj_mtl_error_return; |
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
PicoSetShaderTransparency( curShader,value ); |
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
diffuse = PicoGetShaderDiffuseColor( curShader ); |
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 |
diffuse[ 3 ] = (picoByte_t)( value * 255.0 ); |
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
PicoSetShaderDiffuseColor( curShader,diffuse ); |
|---|
| 351 |
} |
|---|
| 352 |
|
|---|
| 353 |
else if (!_pico_stricmp(p->token,"ns")) |
|---|
| 354 |
{ |
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
|
|---|
| 363 |
float value; |
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
if (curShader == NULL) |
|---|
| 367 |
_obj_mtl_error_return; |
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 |
if (!_pico_parse_float( p,&value )) |
|---|
| 371 |
_obj_mtl_error_return; |
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
if (value > 1000) |
|---|
| 378 |
value = 128.0 * (value / 2048.0); |
|---|
| 379 |
|
|---|
| 380 |
else if (value > 200) |
|---|
| 381 |
value = 128.0 * (value / 1000.0); |
|---|
| 382 |
|
|---|
| 383 |
else if (value > 100) |
|---|
| 384 |
value = 128.0 * (value / 200.0); |
|---|
| 385 |
|
|---|
| 386 |
else if (value > 1) |
|---|
| 387 |
value = 128.0 * (value / 100.0); |
|---|
| 388 |
|
|---|
| 389 |
else { |
|---|
| 390 |
value *= 128.0; |
|---|
| 391 |
} |
|---|
| 392 |
|
|---|
| 393 |
if (value < 0.0) value = 0.0; |
|---|
| 394 |
|
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 |
PicoSetShaderShininess( curShader,value ); |
|---|
| 398 |
} |
|---|
| 399 |
|
|---|
| 400 |
else if (!_pico_stricmp(p->token,"ka")) |
|---|
| 401 |
{ |
|---|
| 402 |
picoColor_t color; |
|---|
| 403 |
picoVec3_t v; |
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
if (curShader == NULL) |
|---|
| 407 |
_obj_mtl_error_return; |
|---|
| 408 |
|
|---|
| 409 |
|
|---|
| 410 |
if (!_pico_parse_vec( p,v )) |
|---|
| 411 |
_obj_mtl_error_return; |
|---|
| 412 |
|
|---|
| 413 |
|
|---|
| 414 |
color[ 0 ] = (picoByte_t)( v[ 0 ] * 255 ); |
|---|
| 415 |
color[ 1 ] = (picoByte_t)( v[ 1 ] * 255 ); |
|---|
| 416 |
color[ 2 ] = (picoByte_t)( v[ 2 ] * 255 ); |
|---|
| 417 |
color[ 3 ] = (picoByte_t)( 255 ); |
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 |
PicoSetShaderAmbientColor( curShader,color ); |
|---|
| 421 |
} |
|---|
| 422 |
|
|---|
| 423 |
else if (!_pico_stricmp(p->token,"kd")) |
|---|
| 424 |
{ |
|---|
| 425 |
picoColor_t color; |
|---|
| 426 |
picoVec3_t v; |
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 |
if (curShader == NULL) |
|---|
| 430 |
_obj_mtl_error_return; |
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 |
if (!_pico_parse_vec( p,v )) |
|---|
| 434 |
_obj_mtl_error_return; |
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
color[ 0 ] = (picoByte_t)( v[ 0 ] * 255 ); |
|---|
| 438 |
color[ 1 ] = (picoByte_t)( v[ 1 ] * 255 ); |
|---|
| 439 |
color[ 2 ] = (picoByte_t)( v[ 2 ] * 255 ); |
|---|
| 440 |
color[ 3 ] = (picoByte_t)( 255 ); |
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 |
PicoSetShaderDiffuseColor( curShader,color ); |
|---|
| 444 |
} |
|---|
| 445 |
|
|---|
| 446 |
else if (!_pico_stricmp(p->token,"ks")) |
|---|
| 447 |
{ |
|---|
| 448 |
picoColor_t color; |
|---|
| 449 |
picoVec3_t v; |
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 |
if (curShader == NULL) |
|---|
| 453 |
_obj_mtl_error_return; |
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 |
if (!_pico_parse_vec( p,v )) |
|---|
| 457 |
_obj_mtl_error_return; |
|---|
| 458 |
|
|---|
| 459 |
|
|---|
| 460 |
color[ 0 ] = (picoByte_t)( v[ 0 ] * 255 ); |
|---|
| 461 |
color[ 1 ] = (picoByte_t)( v[ 1 ] * 255 ); |
|---|
| 462 |
color[ 2 ] = (picoByte_t)( v[ 2 ] * 255 ); |
|---|
| 463 |
color[ 3 ] = (picoByte_t)( 255 ); |
|---|
| 464 |
|
|---|
| 465 |
|
|---|
| 466 |
PicoSetShaderSpecularColor( curShader,color ); |
|---|
| 467 |
} |
|---|
| 468 |
#endif |
|---|
| 469 |
|
|---|
| 470 |
_pico_parse_skip_rest( p ); |
|---|
| 471 |
} |
|---|
| 472 |
|
|---|
| 473 |
|
|---|
| 474 |
_pico_free_parser( p ); |
|---|
| 475 |
_pico_free_file( mtlBuffer ); |
|---|
| 476 |
_pico_free( fileName ); |
|---|
| 477 |
|
|---|
| 478 |
|
|---|
| 479 |
return 1; |
|---|
| 480 |
} |
|---|
| 481 |
#endif |
|---|
| 482 |
|
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 |
static picoModel_t *_obj_load( PM_PARAMS_LOAD ) |
|---|
| 487 |
{ |
|---|
| 488 |
TObjVertexData *vertexData = NULL; |
|---|
| 489 |
picoModel_t *model; |
|---|
| 490 |
picoSurface_t *curSurface = NULL; |
|---|
| 491 |
picoParser_t *p; |
|---|
| 492 |
int allocated; |
|---|
| 493 |
int entries; |
|---|
| 494 |
int numVerts = 0; |
|---|
| 495 |
int numNormals = 0; |
|---|
| 496 |
int numUVs = 0; |
|---|
| 497 |
int curVertex = 0; |
|---|
| 498 |
int curFace = 0; |
|---|
| 499 |
|
|---|
| 500 |
|
|---|
| 501 |
#define _obj_error_return(m) \ |
|---|
| 502 |
{ \ |
|---|
| 503 |
_pico_printf( PICO_ERROR,"%s in OBJ, line %d.",m,p->curLine); \ |
|---|
| 504 |
_pico_free_parser( p ); \ |
|---|
| 505 |
FreeObjVertexData( vertexData ); \ |
|---|
| 506 |
PicoFreeModel( model ); \ |
|---|
| 507 |
return NULL; \ |
|---|
| 508 |
} |
|---|
| 509 |
|
|---|
| 510 |
p = _pico_new_parser( (picoByte_t *)buffer,bufSize ); |
|---|
| 511 |
if (p == NULL) return NULL; |
|---|
| 512 |
|
|---|
| 513 |
|
|---|
| 514 |
model = PicoNewModel(); |
|---|
| 515 |
if (model == NULL) |
|---|
| 516 |
{ |
|---|
| 517 |
_pico_free_parser( p ); |
|---|
| 518 |
return NULL; |
|---|
| 519 |
} |
|---|
| 520 |
|
|---|
| 521 |
PicoSetModelFrameNum( model,frameNum ); |
|---|
| 522 |
PicoSetModelName( model,fileName ); |
|---|
| 523 |
PicoSetModelFileName( model,fileName ); |
|---|
| 524 |
|
|---|
| 525 |
|
|---|
| 526 |
#if 0 |
|---|
| 527 |
_obj_mtl_load( model ); |
|---|
| 528 |
#endif |
|---|
| 529 |
|
|---|
| 530 |
|
|---|
| 531 |
while( 1 ) |
|---|
| 532 |
{ |
|---|
| 533 |
|
|---|
| 534 |
if (_pico_parse_first( p ) == NULL) |
|---|
| 535 |
break; |
|---|
| 536 |
|
|---|
| 537 |
|
|---|
| 538 |
if (p->token == NULL || !strlen( p->token )) |
|---|
| 539 |
continue; |
|---|
| 540 |
|
|---|
| 541 |
|
|---|
| 542 |
if (p->token[0] == '#') |
|---|
| 543 |
{ |
|---|
| 544 |
_pico_parse_skip_rest( p ); |
|---|
| 545 |
continue; |
|---|
| 546 |
} |
|---|
| 547 |
|
|---|
| 548 |
if (!_pico_stricmp(p->token,"v")) |
|---|
| 549 |
{ |
|---|
| 550 |
TObjVertexData *data; |
|---|
| 551 |
picoVec3_t v; |
|---|
| 552 |
|
|---|
| 553 |
vertexData = SizeObjVertexData( vertexData,numVerts+1,&entries,&allocated ); |
|---|
| 554 |
if (vertexData == NULL) |
|---|
| 555 |
_obj_error_return("Realloc of vertex data failed (1)"); |
|---|
| 556 |
|
|---|
| 557 |
data = &vertexData[ numVerts++ ]; |
|---|
| 558 |
|
|---|
| 559 |
|
|---|
| 560 |
if (!_pico_parse_vec( p,v )) |
|---|
| 561 |
_obj_error_return("Vertex parse error"); |
|---|
| 562 |
|
|---|
| 563 |
_pico_copy_vec( v,data->v ); |
|---|
| 564 |
|
|---|
| 565 |
#ifdef DEBUG_PM_OBJ_EX |
|---|
| 566 |
printf("Vertex: x: %f y: %f z: %f\n",v[0],v[1],v[2]); |
|---|
| 567 |
#endif |
|---|
| 568 |
} |
|---|
| 569 |
|
|---|
| 570 |
else if (!_pico_stricmp(p->token,"vt")) |
|---|
| 571 |
{ |
|---|
| 572 |
TObjVertexData *data; |
|---|
| 573 |
picoVec2_t coord; |
|---|
| 574 |
|
|---|
| 575 |
vertexData = SizeObjVertexData( vertexData,numUVs+1,&entries,&allocated ); |
|---|
| 576 |
if (vertexData == NULL) |
|---|
| 577 |
_obj_error_return("Realloc of vertex data failed (2)"); |
|---|
| 578 |
|
|---|
| 579 |
data = &vertexData[ numUVs++ ]; |
|---|
| 580 |
|
|---|
| 581 |
|
|---|
| 582 |
if (!_pico_parse_vec2( p,coord )) |
|---|
| 583 |
_obj_error_return("UV coord parse error"); |
|---|
| 584 |
|
|---|
| 585 |
_pico_copy_vec2( coord,data->vt ); |
|---|
| 586 |
|
|---|
| 587 |
#ifdef DEBUG_PM_OBJ_EX |
|---|
| 588 |
printf("TexCoord: u: %f v: %f\n",coord[0],coord[1]); |
|---|
| 589 |
#endif |
|---|
| 590 |
} |
|---|
| 591 |
|
|---|
| 592 |
else if (!_pico_stricmp(p->token,"vn")) |
|---|
| 593 |
{ |
|---|
| 594 |
TObjVertexData *data; |
|---|
| 595 |
picoVec3_t n; |
|---|
| 596 |
|
|---|
| 597 |
vertexData = SizeObjVertexData( vertexData,numNormals+1,&entries,&allocated ); |
|---|
| 598 |
if (vertexData == NULL) |
|---|
| 599 |
_obj_error_return("Realloc of vertex data failed (3)"); |
|---|
| 600 |
|
|---|
| 601 |
data = &vertexData[ numNormals++ ]; |
|---|
| 602 |
|
|---|
| 603 |
|
|---|
| 604 |
if (!_pico_parse_vec( p,n )) |
|---|
| 605 |
_obj_error_return("Vertex normal parse error"); |
|---|
| 606 |
|
|---|
| 607 |
_pico_copy_vec( n,data->vn ); |
|---|
| 608 |
|
|---|
| 609 |
#ifdef DEBUG_PM_OBJ_EX |
|---|
| 610 |
printf("Normal: x: %f y: %f z: %f\n",n[0],n[1],n[2]); |
|---|
| 611 |
#endif |
|---|
| 612 |
} |
|---|
| 613 |
|
|---|
| 614 |
else if (!_pico_stricmp(p->token,"g")) |
|---|
| 615 |
{ |
|---|
| 616 |
picoSurface_t *newSurface; |
|---|
| 617 |
char *groupName; |
|---|
| 618 |
|
|---|
| 619 |
|
|---|
| 620 |
groupName = _pico_parse( p,0 ); |
|---|
| 621 |
if (groupName == NULL || !strlen(groupName)) |
|---|
| 622 |
{ |
|---|
| 623 |
|
|---|
| 624 |
|
|---|
| 625 |
#if 1 |
|---|
| 626 |
strcpy( p->token,"default" ); |
|---|
| 627 |
groupName = p->token; |
|---|
| 628 |
#else |
|---|
| 629 |
_obj_error_return("Invalid or missing group name"); |
|---|
| 630 |
#endif |
|---|
| 631 |
} |
|---|
| 632 |
|
|---|
| 633 |
newSurface = PicoNewSurface( model ); |
|---|
| 634 |
if (newSurface == NULL) |
|---|
| 635 |
_obj_error_return("Error allocating surface"); |
|---|
| 636 |
|
|---|
| 637 |
|
|---|
| 638 |
curFace = 0; |
|---|
| 639 |
|
|---|
| 640 |
|
|---|
| 641 |
curSurface = newSurface; |
|---|
| 642 |
|
|---|
| 643 |
|
|---|
| 644 |
PicoSetSurfaceType( newSurface,PICO_TRIANGLES ); |
|---|
| 645 |
|
|---|
| 646 |
|
|---|
| 647 |
PicoSetSurfaceName( newSurface,groupName ); |
|---|
| 648 |
|
|---|
| 649 |
#ifdef DEBUG_PM_OBJ_EX |
|---|
| 650 |
printf("Group: '%s'\n",groupName); |
|---|
| 651 |
#endif |
|---|
| 652 |
} |
|---|
| 653 |
|
|---|
| 654 |
else if (!_pico_stricmp(p->token,"f")) |
|---|
| 655 |
{ |
|---|
| 656 |
|
|---|
| 657 |
|
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 |
|
|---|
| 661 |
|
|---|
| 662 |
|
|---|
| 663 |
picoVec3_t verts [ 4 ]; |
|---|
| 664 |
picoVec3_t normals[ 4 ]; |
|---|
| 665 |
picoVec2_t coords [ 4 ]; |
|---|
| 666 |
|
|---|
| 667 |
int iv [ 4 ], has_v; |
|---|
| 668 |
int ivt[ 4 ], has_vt = 0; |
|---|
| 669 |
int ivn[ 4 ], has_vn = 0; |
|---|
| 670 |
int have_quad = 0; |
|---|
| 671 |
int slashcount; |
|---|
| 672 |
int doubleslash; |
|---|
| 673 |
int i; |
|---|
| 674 |
|
|---|
| 675 |
|
|---|
| 676 |
if (curSurface == NULL) |
|---|
| 677 |
_obj_error_return("No group defined for faces"); |
|---|
| 678 |
|
|---|
| 679 |
#ifdef DEBUG_PM_OBJ_EX |
|---|
| 680 |
printf("Face: "); |
|---|
| 681 |
#endif |
|---|
| 682 |
|
|---|
| 683 |
|
|---|
| 684 |
|
|---|
| 685 |
|
|---|
| 686 |
for (i=0; i<4; i++) |
|---|
| 687 |
{ |
|---|
| 688 |
char *str; |
|---|
| 689 |
|
|---|
| 690 |
|
|---|
| 691 |
|
|---|
| 692 |
str = _pico_parse( p,0 ); |
|---|
| 693 |
if (str == NULL) |
|---|
| 694 |
{ |
|---|
| 695 |
|
|---|
| 696 |
if (i == 3) break; |
|---|
| 697 |
|
|---|
| 698 |
|
|---|
| 699 |
_obj_error_return("Face parse error"); |
|---|
| 700 |
} |
|---|
| 701 |
|
|---|
| 702 |
|
|---|
| 703 |
if (i == 3) |
|---|
| 704 |
have_quad = 1; |
|---|
| 705 |
|
|---|
| 706 |
|
|---|
| 707 |
if (i == 0) |
|---|
| 708 |
{ |
|---|
| 709 |
slashcount = _pico_strchcount( str,'/' ); |
|---|
| 710 |
doubleslash = strstr(str,"//") != NULL; |
|---|
| 711 |
} |
|---|
| 712 |
|
|---|
| 713 |
if (doubleslash && (slashcount == 2)) |
|---|
| 714 |
{ |
|---|
| 715 |
has_v = has_vn = 1; |
|---|
| 716 |
sscanf( str,"%d//%d",&iv[ i ],&ivn[ i ] ); |
|---|
| 717 |
} |
|---|
| 718 |
|
|---|
| 719 |
else if (!doubleslash && (slashcount == 2)) |
|---|
| 720 |
{ |
|---|
| 721 |
has_v = has_vt = has_vn = 1; |
|---|
| 722 |
sscanf( str,"%d/%d/%d",&iv[ i ],&ivt[ i ],&ivn[ i ] ); |
|---|
| 723 |
} |
|---|
| 724 |
|
|---|
| 725 |
else if (!doubleslash && (slashcount == 1)) |
|---|
| 726 |
{ |
|---|
| 727 |
has_v = has_vt = 1; |
|---|
| 728 |
sscanf( str,"%d/%d",&iv[ i ],&ivt[ i ] ); |
|---|
| 729 |
} |
|---|
| 730 |
|
|---|
| 731 |
|
|---|
| 732 |
else { |
|---|
| 733 |
|
|---|
| 734 |
has_v = 1; |
|---|
| 735 |
iv[ i ] = atoi( str ); |
|---|
| 736 |
|
|---|
| 737 |
|
|---|
| 738 |
if (iv[ i ] == 0) |
|---|
| 739 |
_obj_error_return("Invalid face format"); |
|---|
| 740 |
} |
|---|
| 741 |
|
|---|
| 742 |
|
|---|
| 743 |
|
|---|
| 744 |
|
|---|
| 745 |
if (iv [ i ] < 0) iv [ i ] = (numVerts - iv [ i ]); |
|---|
| 746 |
if (ivt[ i ] < 0) ivt[ i ] = (numUVs - ivt[ i ]); |
|---|
| 747 |
if (ivn[ i ] < 0) ivn[ i ] = (numNormals - ivn[ i ]); |
|---|
| 748 |
|
|---|
| 749 |
|
|---|
| 750 |
|
|---|
| 751 |
|
|---|
| 752 |
|
|---|
| 753 |
|
|---|
| 754 |
|
|---|
| 755 |
|
|---|
| 756 |
if (has_v) |
|---|
| 757 |
{ |
|---|
| 758 |
|
|---|
| 759 |
if (iv[ i ] < 1 || iv[ i ] > numVerts) |
|---|
| 760 |
_obj_error_return("Vertex index out of range"); |
|---|
| 761 |
|
|---|
| 762 |
|
|---|
| 763 |
verts[ i ][ 0 ] = vertexData[ iv[ i ] - 1 ].v[ 0 ]; |
|---|
| 764 |
verts[ i ][ 1 ] = vertexData[ iv[ i ] - 1 ].v[ 1 ]; |
|---|
| 765 |
verts[ i ][ 2 ] = vertexData[ iv[ i ] - 1 ].v[ 2 ]; |
|---|
| 766 |
} |
|---|
| 767 |
|
|---|
| 768 |
if (has_vn) |
|---|
| 769 |
{ |
|---|
| 770 |
|
|---|
| 771 |
if (ivn[ i ] < 1 || ivn[ i ] > numNormals) |
|---|
| 772 |
_obj_error_return("Normal index out of range"); |
|---|
| 773 |
|
|---|
| 774 |
|
|---|
| 775 |
normals[ i ][ 0 ] = vertexData[ ivn[ i ] - 1 ].vn[ 0 ]; |
|---|
| 776 |
normals[ i ][ 1 ] = vertexData[ ivn[ i ] - 1 ].vn[ 1 ]; |
|---|
| 777 |
normals[ i ][ 2 ] = vertexData[ ivn[ i ] - 1 ].vn[ 2 ]; |
|---|
| 778 |
} |
|---|
| 779 |
|
|---|
| 780 |
if (has_vt) |
|---|
| 781 |
{ |
|---|
| 782 |
|
|---|
| 783 |
if (ivt[ i ] < 1 || ivt[ i ] > numUVs) |
|---|
| 784 |
_obj_error_return("UV coord index out of range"); |
|---|
| 785 |
|
|---|
| 786 |
|
|---|
| 787 |
coords[ i ][ 0 ] = vertexData[ ivt[ i ] - 1 ].vt[ 0 ]; |
|---|
| 788 |
coords[ i ][ 1 ] = vertexData[ ivt[ i ] - 1 ].vt[ 1 ]; |
|---|
| 789 |
coords[ i ][ 1 ] = -coords[ i ][ 1 ]; |
|---|
| 790 |
} |
|---|
| 791 |
#ifdef DEBUG_PM_OBJ_EX |
|---|
| 792 |
printf("(%4d",iv[ i ]); |
|---|
| 793 |
if (has_vt) printf(" %4d",ivt[ i ]); |
|---|
| 794 |
if (has_vn) printf(" %4d",ivn[ i ]); |
|---|
| 795 |
printf(") "); |
|---|
| 796 |
#endif |
|---|
| 797 |
} |
|---|
| 798 |
#ifdef DEBUG_PM_OBJ_EX |
|---|
| 799 |
printf("\n"); |
|---|
| 800 |
#endif |
|---|
| 801 |
|
|---|
| 802 |
|
|---|
| 803 |
|
|---|
| 804 |
if (has_v) |
|---|
| 805 |
{ |
|---|
| 806 |
int max = 3; |
|---|
| 807 |
if (have_quad) max = 4; |
|---|
| 808 |
|
|---|
| 809 |
|
|---|
| 810 |
for (i=0; i<max; i++) |
|---|
| 811 |
{ |
|---|
| 812 |
PicoSetSurfaceXYZ ( curSurface, (curVertex + i), verts [ i ] ); |
|---|
| 813 |
PicoSetSurfaceST ( curSurface,0,(curVertex + i), coords [ i ] ); |
|---|
| 814 |
PicoSetSurfaceNormal( curSurface, (curVertex + i), normals[ i ] ); |
|---|
| 815 |
} |
|---|
| 816 |
|
|---|
| 817 |
PicoSetSurfaceIndex( curSurface,(curFace * 3 + 2),(picoIndex_t)( curVertex + 0 ) ); |
|---|
| 818 |
PicoSetSurfaceIndex( curSurface,(curFace * 3 + 1),(picoIndex_t)( curVertex + 1 ) ); |
|---|
| 819 |
PicoSetSurfaceIndex( curSurface,(curFace * 3 + 0),(picoIndex_t)( curVertex + 2 ) ); |
|---|
| 820 |
curFace++; |
|---|
| 821 |
|
|---|
| 822 |
|
|---|
| 823 |
if (have_quad) |
|---|
| 824 |
{ |
|---|
| 825 |
|
|---|
| 826 |
PicoSetSurfaceIndex( curSurface,(curFace * 3 + 2),(picoIndex_t)( curVertex + 0 ) ); |
|---|
| 827 |
PicoSetSurfaceIndex( curSurface,(curFace * 3 + 1),(picoIndex_t)( curVertex + 2 ) ); |
|---|
| 828 |
PicoSetSurfaceIndex( curSurface,(curFace * 3 + 0),(picoIndex_t)( curVertex + 3 ) ); |
|---|
| 829 |
curFace++; |
|---|
| 830 |
} |
|---|
| 831 |
|
|---|
| 832 |
curVertex += max; |
|---|
| 833 |
} |
|---|
| 834 |
} |
|---|
| 835 |
|
|---|
| 836 |
_pico_parse_skip_rest( p ); |
|---|
| 837 |
} |
|---|
| 838 |
|
|---|
| 839 |
FreeObjVertexData( vertexData ); |
|---|
| 840 |
|
|---|
| 841 |
|
|---|
| 842 |
return model; |
|---|
| 843 |
|
|---|
| 844 |
} |
|---|
| 845 |
|
|---|
| 846 |
|
|---|
| 847 |
const picoModule_t picoModuleOBJ = |
|---|
| 848 |
{ |
|---|
| 849 |
"0.6-b", |
|---|
| 850 |
"Wavefront ASCII", |
|---|
| 851 |
"seaw0lf", |
|---|
| 852 |
"2002 seaw0lf", |
|---|
| 853 |
{ |
|---|
| 854 |
"obj",NULL,NULL,NULL |
|---|
| 855 |
}, |
|---|
| 856 |
_obj_canload, |
|---|
| 857 |
_obj_load, |
|---|
| 858 |
NULL, |
|---|
| 859 |
NULL |
|---|
| 860 |
}; |
|---|