Changeset 275 for GtkRadiant/trunk/libs/picomodel/picomodel.c
- Timestamp:
- 06/26/08 06:21:13 (5 months ago)
- Files:
-
- GtkRadiant/trunk/libs/picomodel/picomodel.c (modified) (66 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
GtkRadiant/trunk/libs/picomodel/picomodel.c
r183 r275 162 162 char *modelFileName, *remapFileName; 163 163 164 164 165 165 /* init */ 166 166 model = NULL; 167 167 168 168 /* make sure we've got a file name */ 169 169 if( fileName == NULL ) … … 172 172 return NULL; 173 173 } 174 174 175 175 /* load file data (buffer is allocated by host app) */ 176 176 _pico_load_file( fileName, &buffer, &bufSize ); … … 183 183 /* get ptr to list of supported modules */ 184 184 modules = PicoModuleList( NULL ); 185 185 186 186 /* run it through the various loader functions and try */ 187 187 /* to find a loader that fits the given file data */ … … 190 190 /* get module */ 191 191 pm = *modules; 192 192 193 193 /* sanity check */ 194 194 if( pm == NULL) … … 198 198 if( pm->canload == NULL || pm->load == NULL ) 199 199 continue; 200 200 201 201 /* see whether this module can load the model file or not */ 202 202 if( pm->canload( fileName, buffer, bufSize ) == PICO_PMV_OK ) … … 209 209 return NULL; 210 210 } 211 211 212 212 /* assign pointer to file format module */ 213 213 model->module = pm; 214 214 215 215 /* get model file name */ 216 216 modelFileName = PicoGetModelFileName( model ); 217 217 218 218 /* apply model remappings from <model>.remap */ 219 219 if( strlen( modelFileName ) ) … … 234 234 } 235 235 } 236 236 237 237 /* model was loaded, so break out of loop */ 238 238 break; 239 239 } 240 240 } 241 241 242 242 /* free memory used by file buffer */ 243 243 if( buffer) … … 262 262 { 263 263 picoModel_t *model; 264 264 265 265 /* allocate */ 266 266 model = _pico_alloc( sizeof(picoModel_t) ); … … 270 270 /* clear */ 271 271 memset( model,0,sizeof(picoModel_t) ); 272 272 273 273 /* model set up */ 274 274 _pico_zero_bounds( model->mins,model->maxs ); … … 291 291 { 292 292 int i; 293 293 294 294 295 295 /* sanity check */ 296 296 if( model == NULL ) 297 297 return; 298 298 299 299 /* free bits */ 300 300 if( model->name ) 301 301 _pico_free( model->name ); 302 302 303 303 /* free shaders */ 304 304 for( i = 0; i < model->numShaders; i++ ) 305 305 PicoFreeShader( model->shader[ i ] ); 306 306 free( model->shader ); 307 307 308 308 /* free surfaces */ 309 309 for( i = 0; i < model->numSurfaces; i++ ) 310 310 PicoFreeSurface( model->surface[ i ] ); 311 311 free( model->surface ); 312 312 313 313 /* free the model */ 314 314 _pico_free( model ); … … 328 328 if( model == NULL ) 329 329 return 0; 330 330 331 331 /* bare minimums */ 332 332 /* sea: null surface/shader fix (1s=>0s) */ … … 343 343 return 0; 344 344 } 345 345 346 346 /* set shader count to higher */ 347 347 if( numShaders > model->numShaders ) 348 348 model->numShaders = numShaders; 349 349 350 350 /* additional surfaces? */ 351 351 while( numSurfaces > model->maxSurfaces ) … … 355 355 return 0; 356 356 } 357 357 358 358 /* set shader count to higher */ 359 359 if( numSurfaces > model->numSurfaces ) 360 360 model->numSurfaces = numSurfaces; 361 361 362 362 /* return ok */ 363 363 return 1; … … 378 378 { 379 379 picoShader_t *shader; 380 380 381 381 382 382 /* allocate and clear */ … … 385 385 return NULL; 386 386 memset( shader, 0, sizeof(picoShader_t) ); 387 387 388 388 /* attach it to the model */ 389 389 if( model != NULL ) … … 424 424 if( shader == NULL ) 425 425 return; 426 426 427 427 /* free bits */ 428 428 if( shader->name ) … … 430 430 if( shader->mapName ) 431 431 _pico_free( shader->mapName ); 432 432 433 433 /* free the shader */ 434 434 _pico_free( shader ); … … 445 445 { 446 446 int i; 447 448 447 448 449 449 /* sanity checks */ 450 450 if( model == NULL || name == NULL ) /* sea: null name fix */ 451 451 return NULL; 452 452 453 453 /* walk list */ 454 454 for( i = 0; i < model->numShaders; i++ ) … … 468 468 return model->shader[ i ]; 469 469 } 470 470 471 471 /* named shader not found */ 472 472 return NULL; … … 488 488 picoSurface_t *surface; 489 489 char surfaceName[64]; 490 490 491 491 /* allocate and clear */ 492 492 surface = _pico_alloc( sizeof( *surface ) ); … … 494 494 return NULL; 495 495 memset( surface, 0, sizeof( *surface ) ); 496 496 497 497 /* attach it to the model */ 498 498 if( model != NULL ) … … 504 504 return NULL; 505 505 } 506 506 507 507 /* attach */ 508 508 model->surface[ model->numSurfaces - 1 ] = surface; 509 509 surface->model = model; 510 510 511 511 /* set default name */ 512 512 sprintf( surfaceName, "Unnamed_%d", model->numSurfaces ); 513 513 PicoSetSurfaceName( surface, surfaceName ); 514 514 } 515 515 516 516 /* return */ 517 517 return surface; … … 527 527 { 528 528 int i; 529 530 529 530 531 531 /* dummy check */ 532 532 if( surface == NULL ) 533 533 return; 534 534 535 535 /* free bits */ 536 536 _pico_free( surface->xyz ); … … 538 538 _pico_free( surface->index ); 539 539 _pico_free( surface->faceNormal ); 540 540 541 541 /* free arrays */ 542 542 for( i = 0; i < surface->numSTArrays; i++ ) … … 546 546 _pico_free( surface->color[ i ] ); 547 547 free( surface->color ); 548 548 549 549 /* free the surface */ 550 550 _pico_free( surface ); … … 562 562 { 563 563 int i; 564 565 564 565 566 566 /* dummy check */ 567 567 if( surface == NULL ) 568 568 return 0; 569 569 570 570 /* bare minimums */ 571 571 if( numVertexes < 1 ) … … 577 577 if( numIndexes < 1 ) 578 578 numIndexes = 1; 579 579 580 580 /* additional vertexes? */ 581 581 while( numVertexes > surface->maxVertexes ) /* fix */ … … 593 593 return 0; 594 594 } 595 595 596 596 /* set vertex count to higher */ 597 597 if( numVertexes > surface->numVertexes ) 598 598 surface->numVertexes = numVertexes; 599 599 600 600 /* additional st arrays? */ 601 601 while( numSTArrays > surface->maxSTArrays ) /* fix */ … … 611 611 } 612 612 } 613 613 614 614 /* additional color arrays? */ 615 615 while( numColorArrays > surface->maxColorArrays ) /* fix */ … … 625 625 } 626 626 } 627 627 628 628 /* additional indexes? */ 629 629 while( numIndexes > surface->maxIndexes ) /* fix */ … … 633 633 return 0; 634 634 } 635 635 636 636 /* set index count to higher */ 637 637 if( numIndexes > surface->numIndexes ) … … 666 666 if( model == NULL || name == NULL ) 667 667 return NULL; 668 668 669 669 /* walk list */ 670 670 for( i = 0; i < model->numSurfaces; i++ ) … … 1036 1036 if( num < 0 || num >= model->numShaders ) 1037 1037 return NULL; 1038 1038 1039 1039 /* return the shader */ 1040 1040 return model->shader[ num ]; … … 1061 1061 if( num < 0 || num >= model->numSurfaces ) 1062 1062 return NULL; 1063 1063 1064 1064 /* return the surface */ 1065 1065 return model->surface[ num ]; … … 1071 1071 { 1072 1072 int i, count; 1073 1074 1073 1074 1075 1075 if( model == NULL ) 1076 1076 return 0; 1077 1077 if( model->surface == NULL ) 1078 1078 return 0; 1079 1079 1080 1080 count = 0; 1081 1081 for( i = 0; i < model->numSurfaces; i++ ) 1082 1082 count += PicoGetSurfaceNumVertexes( model->surface[ i ] ); 1083 1083 1084 1084 return count; 1085 1085 } … … 1090 1090 { 1091 1091 int i, count; 1092 1093 1092 1093 1094 1094 if( model == NULL ) 1095 1095 return 0; 1096 1096 if( model->surface == NULL ) 1097 1097 return 0; 1098 1098 1099 1099 count = 0; 1100 1100 for( i = 0; i < model->numSurfaces; i++ ) 1101 1101 count += PicoGetSurfaceNumIndexes( model->surface[ i ] ); 1102 1102 1103 1103 return count; 1104 1104 } … … 1414 1414 if( (vertexCombinationHash->vcd.normal[ 0 ] != normal[ 0 ] || vertexCombinationHash->vcd.normal[ 1 ] != normal[ 1 ] || vertexCombinationHash->vcd.normal[ 2 ] != normal[ 2 ]) ) 1415 1415 continue; 1416 1416 1417 1417 /* check st */ 1418 1418 if( vertexCombinationHash->vcd.st[ 0 ] != st[ 0 ] || vertexCombinationHash->vcd.st[ 1 ] != st[ 1 ] ) … … 1430 1430 ( fabs(normal[ 2 ] - vertexCombinationHash->vcd.normal[ 2 ]) ) > HASH_NORMAL_EPSILON ) 1431 1431 continue; 1432 1432 1433 1433 /* check st */ 1434 1434 if( ( fabs(st[ 0 ] - vertexCombinationHash->vcd.st[ 0 ]) ) > HASH_ST_EPSILON || … … 1489 1489 { 1490 1490 int i, j; 1491 1492 1491 1492 1493 1493 /* dummy check */ 1494 1494 if( surface == NULL || surface->numVertexes <= 0 ) 1495 1495 return -1; 1496 1496 1497 1497 /* walk vertex list */ 1498 1498 for( i = 0; i < surface->numVertexes; i++ ) … … 1501 1501 if( xyz != NULL && (surface->xyz[ i ][ 0 ] != xyz[ 0 ] || surface->xyz[ i ][ 1 ] != xyz[ 1 ] || surface->xyz[ i ][ 2 ] != xyz[ 2 ]) ) 1502 1502 continue; 1503 1503 1504 1504 /* check normal */ 1505 1505 if( normal != NULL && (surface->normal[ i ][ 0 ] != normal[ 0 ] || surface->normal[ i ][ 1 ] != normal[ 1 ] || surface->normal[ i ][ 2 ] != normal[ 2 ]) ) 1506 1506 continue; 1507 1507 1508 1508 /* check st */ 1509 1509 if( numSTs > 0 && st != NULL ) … … 1517 1517 continue; 1518 1518 } 1519 1519 1520 1520 /* check color */ 1521 1521 if( numColors > 0 && color != NULL ) … … 1529 1529 continue; 1530 1530 } 1531 1531 1532 1532 /* vertex matches */ 1533 1533 return i; 1534 1534 } 1535 1535 1536 1536 /* nada */ 1537 1537 return -1; … … 1555 1555 picoVec3_t *normals, diff; 1556 1556 picoVec4_t plane; 1557 1558 1557 1558 1559 1559 /* dummy check */ 1560 1560 if( surface == NULL || surface->numVertexes == 0 ) 1561 1561 return; 1562 1562 1563 1563 /* fixme: handle other surface types */ 1564 1564 if( surface->type != PICO_TRIANGLES ) 1565 1565 return; 1566 1566 1567 1567 /* allocate normal storage */ 1568 1568 normals = _pico_alloc( surface->numVertexes * sizeof( *normals ) ); … … 1572 1572 return; 1573 1573 } 1574 1574 1575 1575 /* zero it out */ 1576 1576 memset( normals, 0, surface->numVertexes * sizeof( *normals ) ); 1577 1577 1578 1578 /* walk vertex list */ 1579 1579 for( i = 0; i < surface->numVertexes; i++ ) … … 1581 1581 /* zero out votes */ 1582 1582 numVotes = 0; 1583 1583 1584 1584 /* find all the triangles that reference this vertex */ 1585 1585 for( j = 0, faceIndex = 0; j < surface->numIndexes; j += 3, faceIndex++ ) … … 1589 1589 b = surface->index[ j + 1 ]; 1590 1590 c = surface->index[ j + 2 ]; 1591 1591 1592 1592 /* ignore degenerate triangles */ 1593 1593 if( a == b || b == c || c == a ) 1594 1594 continue; 1595 1595 1596 1596 /* ignore indexes out of range */ 1597 1597 if( a < 0 || a >= surface->numVertexes || … … 1599 1599 c < 0 || c >= surface->numVertexes ) 1600 1600 continue; 1601 1601 1602 1602 /* test triangle */ 1603 1603 if( a == i || b == i || c == i ) … … 1631 1631 break; 1632 1632 } 1633 1633 1634 1634 /* add a new vote? */ 1635 1635 if( k == numVotes && numVotes < MAX_NORMAL_VOTES ) … … 1640 1640 } 1641 1641 } 1642 1642 1643 1643 /* tally votes */ 1644 1644 if( numVotes > 0 ) … … 1648 1648 for( k = 0; k < numVotes; k++ ) 1649 1649 _pico_add_vec( normals[ i ], votes[ k ], normals[ i ] ); 1650 1650 1651 1651 /* normalize it */ 1652 1652 if( _pico_normalize_vec( normals[ i ] ) ) … … 1663 1663 } 1664 1664 } 1665 1665 1666 1666 /* free normal storage */ 1667 1667 _pico_free( normals ); … … 1690 1690 picoByte_t *remapBuffer; 1691 1691 int remapBufSize; 1692 1693 1692 1693 1694 1694 /* sanity checks */ 1695 1695 if( model == NULL || remapFile == NULL ) 1696 1696 return 0; 1697 1697 1698 1698 /* load remap file contents */ 1699 1699 _pico_load_file( remapFile,&remapBuffer,&remapBufSize ); 1700 1700 1701 1701 /* check result */ 1702 1702 if( remapBufSize == 0 ) … … 1704 1704 if( remapBufSize < 0 ) 1705 1705 return 0; /* load failed: error */ 1706 1706 1707 1707 /* create a new pico parser */ 1708 1708 p = _pico_new_parser( remapBuffer, remapBufSize ); … … 1712 1712 _prm_error_return; 1713 1713 } 1714 1714 1715 1715 /* doo teh parse */ 1716 1716 while( 1 ) … … 1726 1726 continue; 1727 1727 } 1728 1728 1729 1729 /* block for quick material shader name remapping */ 1730 1730 /* materials { "m" (=>|->|=) "s" } */ … … 1742 1742 picoShader_t *shader; 1743 1743 char *materialName; 1744 1745 1744 1745 1746 1746 /* get material name */ 1747 1747 if (_pico_parse( p,1 ) == NULL) break; … … 1808 1808 /* check square closing bracket */ 1809 1809 if (!_pico_parse_check( p,0,"]" )) 1810 _prm_error_return; 1810 _prm_error_return; 1811 1811 1812 1812 /* try to find material by name */ … … 1865 1865 color[ 1 ] = (picoByte_t)v[ 1 ]; 1866 1866 color[ 2 ] = (picoByte_t)v[ 2 ]; 1867 color[ 3 ] = 1; 1867 1868 1868 1869 /* set new ambient color */ … … 1882 1883 color[ 1 ] = (picoByte_t)v[ 1 ]; 1883 1884 color[ 2 ] = (picoByte_t)v[ 2 ]; 1885 color[ 3 ] = 1; 1884 1886 1885 1887 /* set new ambient color */ … … 1899 1901 color[ 1 ] = (picoByte_t)v[ 1 ]; 1900 1902 color[ 2 ] = (picoByte_t)v[ 2 ]; 1903 color[ 3 ] = 1; 1901 1904 1902 1905 /* set new ambient color */ … … 1909 1912 /* end 'materials[' */ 1910 1913 } 1911 1914 1912 1915 /* free both parser and file buffer */ 1913 1916 _pico_free_parser( p ); … … 1925 1928 */ 1926 1929 1927 void PicoAddTriangleToModel( picoModel_t *model, picoVec3_t** xyz, picoVec3_t** normals, 1930 void PicoAddTriangleToModel( picoModel_t *model, picoVec3_t** xyz, picoVec3_t** normals, 1928 1931 int numSTs, picoVec2_t **st, int numColors, picoColor_t **colors, 1929 1932 picoShader_t* shader ) … … 1938 1941 workSurface = model->surface[i]; 1939 1942 if ( workSurface->shader == shader ) 1940 { 1943 { 1941 1944 break; 1942 1945 } … … 1961 1964 1962 1965 /* add the triangle data to the surface */ 1963 for ( i = 0 ; i < 3 ; i++ ) 1966 for ( i = 0 ; i < 3 ; i++ ) 1964 1967 { 1965 1968 /* get the next free spot in the index array */ … … 1971 1974 /* the vertex wasn't found, so create a new vertex in the pool from the data we have */ 1972 1975 if ( vertDataIndex == -1 ) 1973 { 1976 { 1974 1977 /* find the next spot for a new vertex */ 1975 vertDataIndex = PicoGetSurfaceNumVertexes ( workSurface ); 1978 vertDataIndex = PicoGetSurfaceNumVertexes ( workSurface ); 1976 1979 1977 1980 /* assign the data to it */ 1978 1981 PicoSetSurfaceXYZ ( workSurface ,vertDataIndex , *xyz[i] ); 1979 PicoSetSurfaceNormal ( workSurface , vertDataIndex , *normals[i] ); 1982 PicoSetSurfaceNormal ( workSurface , vertDataIndex , *normals[i] ); 1980 1983 1981 1984 /* make sure to copy over all available ST's and colors for the vertex */ … … 1990 1993 } 1991 1994 1992 /* add this vertex to the triangle */ 1995 /* add this vertex to the triangle */ 1993 1996 PicoSetSurfaceIndex ( workSurface , newVertIndex , vertDataIndex ); 1994 } 1995 } 1997 } 1998 }
