00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include <stdlib.h>
00045 #include <stdio.h>
00046
00047 #include "OSGConfig.h"
00048
00049 #include <OSGGL.h>
00050
00051 #include <string>
00052 #include <iostream>
00053 #include <fstream>
00054
00055 #include <vector>
00056
00057 #include <OSGLog.h>
00058
00059 #include <OSGNode.h>
00060 #include <OSGGeometry.h>
00061 #include <OSGGeoProperty.h>
00062 #include <OSGGeoFunctions.h>
00063 #include <OSGSimpleTexturedMaterial.h>
00064 #include <OSGImageFileHandler.h>
00065 #include <OSGPathHandler.h>
00066 #include <OSGGroup.h>
00067 #include <OSGSceneFileHandler.h>
00068 #include <OSGTriangleIterator.h>
00069 #include <OSGSimpleAttachments.h>
00070
00071 #include "OSGOBJSceneFileType.h"
00072
00073 #ifdef OSG_SGI_STL
00074
00075
00076 #ifndef INT_MAX
00077 #define INT_MAX std::numeric_limits<Int32>::max()
00078 #endif
00079 #else
00080 #include <limits.h>
00081 #endif
00082
00083 OSG_USING_NAMESPACE
00084
00085
00091 #if defined(OSG_WIN32_ICL) && !defined(OSG_CHECK_FIELDSETARG)
00092 #pragma warning (disable : 383)
00093 #endif
00094
00095
00096
00097
00098
00099 const Char8 *OBJSceneFileType::_suffixA[] = { "obj" };
00100
00101 OBJSceneFileType OBJSceneFileType::_the (_suffixA,
00102 sizeof(_suffixA),
00103 false,
00104 10,
00105 SceneFileType::OSG_READ_SUPPORTED|
00106 SceneFileType::OSG_WRITE_SUPPORTED);
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 NodePtr OBJSceneFileType::read(std::istream &is, const Char8 *) const
00143 {
00144 NodePtr rootPtr, nodePtr;
00145 std::string elem;
00146 std::map<std::string, DataElem>::const_iterator elemI;
00147 Vec3f vec3f;
00148 Vec2f vec2f;
00149 Real32 x,y,z;
00150 GeoPositionsPtr coordPtr = GeoPositions3f::create();
00151 GeoTexCoordsPtr texCoordPtr = GeoTexCoords2f::create();
00152 GeoNormalsPtr normalPtr = GeoNormals3f::create();
00153 GeometryPtr geoPtr;
00154 GeoIndicesPtr indexPtr;
00155 GeoPLengthsPtr lensPtr;
00156 GeoPTypesPtr typePtr;
00157 DataElem dataElem;
00158 Char8 strBuf[8192], *token, *nextToken;
00159 Int32 strBufSize = sizeof(strBuf)/sizeof(Char8);
00160 Int32 index, posIndex = 0, indexType;
00161 Int32 i,j,n,primCount[3];
00162 std::list<Mesh> meshList;
00163 std::map<std::string, SimpleTexturedMaterialPtr> mtlMap;
00164 std::map<std::string, SimpleTexturedMaterialPtr>::iterator mtlI;
00165 Mesh emptyMesh;
00166 Face emptyFace;
00167 TiePoint emptyTie;
00168 Int32 indexMask, meshIndexMask;
00169 std::list<Face>::iterator faceI;
00170 std::list<Mesh>::iterator meshI;
00171 bool isSingleIndex;
00172 std::string name;
00173
00174
00175 meshList.push_back(emptyMesh);
00176 meshI = meshList.begin();
00177
00178 if(is)
00179 {
00180 primCount[0] = 0;
00181 primCount[1] = 0;
00182 primCount[2] = 0;
00183
00184 beginEditCP(coordPtr);
00185 beginEditCP(texCoordPtr);
00186 beginEditCP(normalPtr);
00187
00188 for (is >> elem; is.eof() == false; is >> elem)
00189 if (elem[0] == '#' ||
00190 elem[0] == '$'
00191 )
00192 is.ignore(INT_MAX, '\n');
00193 else
00194 {
00195 SceneFileHandler::the().updateReadProgress();
00196 elemI = _dataElemMap.find(elem);
00197 dataElem = ((elemI == _dataElemMap.end()) ?
00198 UNKNOWN_DE : elemI->second );
00199 switch (dataElem)
00200 {
00201 case OBJECT_DE:
00202 case GROUP_DE:
00203 case SMOOTHING_GROUP_DE:
00204 is >> name;
00205
00206 break;
00207 case VERTEX_DE:
00208 primCount[0]++;
00209 is >> x >> y >> z;
00210 vec3f.setValues(x,y,z);
00211 coordPtr->push_back(vec3f);
00212 break;
00213 case VERTEX_TEXTURECOORD_DE:
00214 primCount[1]++;
00215 is >> x >> y;
00216 vec2f.setValues(x,y);
00217 texCoordPtr->push_back(vec2f);
00218 break;
00219 case VERTEX_NORMAL_DE:
00220 primCount[2]++;
00221 is >> x >> y >> z;
00222 vec3f.setValues(x,y,z);
00223 normalPtr->push_back(vec3f);
00224 break;
00225 case LIB_MTL_DE:
00226
00227
00228 elem = "";
00229 std::getline (is, elem);
00230
00231 while(!elem.empty() && isspace(elem[0]))
00232 elem.erase(0, 1);
00233 while(!elem.empty() && isspace(elem[elem.length()-1]))
00234 elem.erase(elem.length()-1, 1);
00235 readMTL ( elem.c_str(), mtlMap );
00236
00237 break;
00238 case USE_MTL_DE:
00239 is >> elem;
00240 if (meshI->faceList.empty() == false)
00241 {
00242 meshList.push_front(emptyMesh);
00243 meshI = meshList.begin();
00244 meshI->name = name;
00245 }
00246 mtlI = mtlMap.find(elem);
00247 if (mtlI == mtlMap.end())
00248 {
00249 FFATAL (("Unkown mtl '%s'\n", elem.c_str()));
00250 }
00251 else
00252 meshI->mtlPtr = mtlI->second;
00253 break;
00254 case FACE_DE:
00255 {
00256 meshI->faceList.push_front(emptyFace);
00257 faceI = meshI->faceList.begin();
00258
00259 Char8 *b = &strBuf[0];
00260 int bsize = strBufSize;
00261 do
00262 {
00263 is.get(b, bsize);
00264 is.ignore(INT_MAX, '\n');
00265 int l = strlen(b);
00266 bsize -= l;
00267 if(l == 0)
00268 break;
00269
00270 b = &b[l-1];
00271
00272
00273 while(isspace(*b))
00274 --b;
00275
00276 if(*b == '\\')
00277 *b++ = ' ';
00278 else
00279 break;
00280 }
00281 while(true);
00282
00283 token = strBuf;
00284 indexType = 0;
00285 while (token && *token)
00286 {
00287 for (; *token == '/'; token++)
00288 indexType++;
00289 for (; isspace(*token); token++)
00290 indexType = 0;
00291 index = strtol(token, &nextToken, 10);
00292 if (token == nextToken)
00293 break;
00294 if (indexType == 0)
00295 faceI->tieVec.push_back(emptyTie);
00296 if (index >= 0)
00297 index--;
00298 else
00299 index = primCount[indexType] + index;
00300 faceI->tieVec.back().index[indexType] = index;
00301 token = nextToken;
00302 }
00303 }
00304 break;
00305 case UNKNOWN_DE:
00306 default:
00307 FWARNING (( "Unkown obj data elem: %s\n",
00308 elem.c_str()));
00309 is.ignore(INT_MAX, '\n');
00310 break;
00311 }
00312 }
00313
00314 endEditCP(coordPtr);
00315 endEditCP(texCoordPtr);
00316 endEditCP(normalPtr);
00317
00318 #if 0
00319 std::cerr << "------------------------------------------------" << std::endl;
00320 i = 0;
00321 for (meshI = meshList.begin(); meshI != meshList.end(); meshI++) {
00322 std::cerr << "Mesh " << i << " faceCount :"
00323 << meshI->faceList.size() << std::endl;
00324 j = 0 ;
00325 for ( faceI = meshI->faceList.begin(); faceI != meshI->faceList.end();
00326 faceI++)
00327 std::cerr << "MESH " << i << "face: " << j++ << "tie num: "
00328 << faceI->tieVec.size() << std::endl;
00329 i++;
00330 }
00331 std::cerr << "------------------------------------------------" << std::endl;
00332 =======
00333 #endif
00334
00335 for (meshI = meshList.begin(); meshI != meshList.end(); meshI++)
00336 {
00337 geoPtr = Geometry::create();
00338 indexPtr = GeoIndicesUI32::create();
00339 lensPtr = GeoPLengthsUI32::create();
00340 typePtr = GeoPTypesUI8::create();
00341
00342
00343 meshIndexMask = 0;
00344 isSingleIndex = true;
00345 if ( meshI->faceList.empty() == false)
00346 for ( faceI = meshI->faceList.begin();
00347 faceI != meshI->faceList.end(); faceI++)
00348 {
00349 indexMask = 0;
00350 n = faceI->tieVec.size();
00351 for (i = 0; i < n; i++)
00352 for (j = 0; j < 3; j++)
00353 {
00354 if ((index = (faceI->tieVec[i].index[j])) >= 0) {
00355 indexMask |= (1 << j);
00356 if (j)
00357 isSingleIndex &= (posIndex == index);
00358 else
00359 posIndex = index;
00360 }
00361 }
00362 if (meshIndexMask == 0)
00363 meshIndexMask = indexMask;
00364 else
00365 if (meshIndexMask != indexMask)
00366 {
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379 if( !(indexMask & 1) ) {
00380
00381 FFATAL (( "IndexMask unmatch, can not create geo\n"));
00382 meshIndexMask = 0;
00383 break;
00384 }else{
00385
00386 meshIndexMask &= indexMask;
00387 }
00388 }
00389 }
00390 else
00391 {
00392 FWARNING (("Mesh with empty faceList\n"));
00393 }
00394
00395
00396 if (meshIndexMask)
00397 {
00398 beginEditCP ( geoPtr );
00399 {
00400 geoPtr->setPositions ( coordPtr );
00401 geoPtr->setIndices ( indexPtr );
00402 geoPtr->setLengths ( lensPtr );
00403 geoPtr->setTypes ( typePtr );
00404
00405 if ( (meshIndexMask & 2) && texCoordPtr->size() > 0 )
00406 {
00407
00408 geoPtr->setTexCoords ( texCoordPtr );
00409 }
00410 else
00411 {
00412 geoPtr->setTexCoords ( NullFC );
00413 meshIndexMask &= ~Geometry::MapTexCoords;
00414 }
00415
00416 if ( (meshIndexMask & 4) && normalPtr->size() > 0 )
00417 {
00418
00419 geoPtr->setNormals ( normalPtr );
00420 }
00421 else
00422 {
00423 geoPtr->setNormals ( NullFC );
00424 meshIndexMask &= ~Geometry::MapNormal;
00425 }
00426
00427 if (isSingleIndex)
00428 {
00429 indexType = 0;
00430 if (meshIndexMask & 1)
00431 indexType |= Geometry::MapPosition;
00432 if (meshIndexMask & 2)
00433 indexType |= Geometry::MapTexCoords;
00434 if (meshIndexMask & 4)
00435 indexType |= Geometry::MapNormal;
00436 geoPtr->editMFIndexMapping()->push_back(indexType);
00437 }
00438 else
00439 {
00440 if (meshIndexMask & 1)
00441 {
00442 indexType = Geometry::MapPosition;
00443 geoPtr->editMFIndexMapping()->push_back(
00444 indexType);
00445 }
00446 if (meshIndexMask & 2)
00447 {
00448 indexType = Geometry::MapTexCoords;
00449 geoPtr->editMFIndexMapping()->push_back(
00450 indexType);
00451 }
00452 if (meshIndexMask & 4)
00453 {
00454 indexType = Geometry::MapNormal;
00455 geoPtr->editMFIndexMapping()->push_back(
00456 indexType);
00457 }
00458 }
00459
00460 if (meshI->mtlPtr == NullFC)
00461 {
00462 meshI->mtlPtr = SimpleTexturedMaterial::create();
00463 beginEditCP( meshI->mtlPtr );
00464 {
00465 meshI->mtlPtr->setDiffuse( Color3f( .8, .8, .8 ) );
00466 meshI->mtlPtr->setSpecular( Color3f( 1, 1, 1 ) );
00467 meshI->mtlPtr->setShininess( 20 );
00468 }
00469 endEditCP( meshI->mtlPtr );
00470 }
00471 geoPtr->setMaterial ( meshI->mtlPtr );
00472 }
00473 endEditCP ( geoPtr );
00474
00475 beginEditCP(lensPtr);
00476 beginEditCP(typePtr);
00477 beginEditCP(indexPtr);
00478
00479 for ( faceI = meshI->faceList.begin();
00480 faceI != meshI->faceList.end(); faceI++)
00481 {
00482 n = faceI->tieVec.size();
00483
00484
00485 lensPtr->push_back(n);
00486
00487
00488 typePtr->push_back(GL_POLYGON);
00489
00490
00491 for (i = 0; i < n; i++)
00492 if (isSingleIndex)
00493 indexPtr->push_back( faceI->tieVec[i].index[0]);
00494 else
00495 for (j = 0; j < 3; j++)
00496 if ( meshIndexMask & (1 << j))
00497 indexPtr->push_back( faceI->tieVec[i].index[j]);
00498 }
00499
00500 endEditCP(indexPtr);
00501 endEditCP(typePtr);
00502 endEditCP(lensPtr);
00503
00504 createSharedIndex( geoPtr );
00505
00506
00507 if ((meshIndexMask & 4) == 0)
00508 calcVertexNormals(geoPtr);
00509
00510
00511 nodePtr = Node::create();
00512 if(!meshI->name.empty())
00513 OSG::setName(nodePtr, meshI->name);
00514 beginEditCP ( nodePtr );
00515 {
00516 nodePtr->setCore( geoPtr );
00517 }
00518 endEditCP ( nodePtr );
00519
00520 if (meshList.size() > 1)
00521 {
00522 if (rootPtr == NullFC)
00523 {
00524 rootPtr = Node::create();
00525 beginEditCP (rootPtr);
00526 {
00527 rootPtr->setCore ( Group::create() );
00528 rootPtr->addChild(nodePtr);
00529 }
00530 endEditCP (rootPtr);
00531 }
00532 else
00533 {
00534 beginEditCP(rootPtr);
00535 {
00536 rootPtr->addChild(nodePtr);
00537 }
00538 endEditCP (rootPtr);
00539 }
00540 }
00541 else
00542 rootPtr = nodePtr;
00543 }
00544 }
00545 }
00546
00547 SceneFileHandler::the().updateReadProgress(100);
00548 return rootPtr;
00549 }
00550
00551 void OBJSceneFileType::write(const NodePtr &node,
00552 std::ostream &os,
00553 UInt32 &pIndex,
00554 UInt32 &nIndex,
00555 UInt32 &tIndex) const
00556 {
00557 UInt32 i,pCount=0,nCount=0,tCount=0;
00558 GeometryPtr g = GeometryPtr::dcast(node->getCore());
00559 if(g != NullFC)
00560 {
00561
00562 os << "g Geometry" << std::endl;
00563 os << "usemtl Geometry" << std::endl;
00564 Matrix mat = node->getToWorld();
00565
00566 if(g->getPositions())
00567 {
00568 pCount = g->getPositions()->getSize();
00569 for(i=0 ; i< pCount ; ++i)
00570 {
00571 Pnt3f v = g->getPositions()->getValue(i);
00572 mat.mult(v, v);
00573 os << "v " << v[0] << " " << v[1] << " " << v[2] << std::endl;
00574 }
00575 }
00576
00577 if(g->getNormals())
00578 {
00579 nCount = g->getNormals()->getSize();
00580 for(i=0 ; i< nCount ; ++i)
00581 {
00582 Vec3f v = g->getNormals()->getValue(i);
00583 mat.mult(v, v);
00584 os << "vn " << v[0] << " " << v[1] << " " << v[2] << std::endl;
00585 }
00586 }
00587
00588 if(g->getTexCoords())
00589 {
00590 tCount = g->getTexCoords()->getSize();
00591 for(i=0 ; i< tCount ; ++i)
00592 {
00593 Vec2f v = g->getTexCoords()->getValue(i);
00594 os << "vt " << v[0] << " " << v[1] << std::endl;
00595 }
00596 }
00597
00598 TriangleIterator f;
00599 for(f=g->beginTriangles() ; f!=g->endTriangles() ; ++f)
00600 {
00601 os << "f";
00602 for(i=0 ; i<3 ; ++i)
00603 {
00604 os << " " << f.getPositionIndex(i) + pIndex;
00605 if(nCount || tCount)
00606 {
00607 os << "/";
00608 if(tCount)
00609 os << f.getTexCoordsIndex(i) + tIndex;
00610 if(nCount)
00611 os << "/" << f.getNormalIndex(i) + nIndex;
00612 }
00613 }
00614 os << std::endl;
00615 }
00616 pIndex += pCount;
00617 tIndex += tCount;
00618 nIndex += nCount;
00619 }
00620 for(MFNodePtr::const_iterator nI=node->getMFChildren()->begin();
00621 nI != node->getMFChildren()->end();
00622 ++nI)
00623 {
00624 write((*nI),os,pIndex,nIndex,tIndex);
00625 }
00626
00627 }
00628
00629 bool OBJSceneFileType::write(const NodePtr &node, std::ostream &os,
00630 const Char8 *fileNameOrExtension) const
00631 {
00632 UInt32 pIndex=1;
00633 UInt32 tIndex=1;
00634 UInt32 nIndex=1;
00635
00636 write(node,os,pIndex,tIndex,nIndex);
00637
00638 return true;
00639 }
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685 OBJSceneFileType::OBJSceneFileType(const Char8 *suffixArray[],
00686 UInt16 suffixByteCount,
00687 bool override,
00688 UInt32 overridePriority,
00689 UInt32 flags) :
00690 SceneFileType(suffixArray,
00691 suffixByteCount,
00692 override,
00693 overridePriority,
00694 flags),
00695 _dataElemMap()
00696
00697 {
00698 initElemMap();
00699 }
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722 OBJSceneFileType::OBJSceneFileType(const OBJSceneFileType &obj) :
00723 SceneFileType(obj)
00724 {
00725 initElemMap();
00726 }
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749 OBJSceneFileType &OBJSceneFileType::the(void)
00750 {
00751 return _the;
00752 }
00753
00754 OBJSceneFileType::~OBJSceneFileType(void)
00755 {
00756 return;
00757 }
00758
00759 const Char8 *OBJSceneFileType::getName(void) const
00760 {
00761 return "Wavefront Geometry";
00762 }
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785 void OBJSceneFileType::initElemMap(void)
00786 {
00787 if (_dataElemMap.empty())
00788 {
00789 _dataElemMap[""] = UNKNOWN_DE;
00790
00791 _dataElemMap["v"] = VERTEX_DE;
00792 _dataElemMap["vt"] = VERTEX_TEXTURECOORD_DE;
00793 _dataElemMap["vn"] = VERTEX_NORMAL_DE;
00794 _dataElemMap["f"] = FACE_DE;
00795 _dataElemMap["fo"] = FACE_DE;
00796 _dataElemMap["mtllib"] = LIB_MTL_DE;
00797 _dataElemMap["usemtl"] = USE_MTL_DE;
00798 _dataElemMap["g"] = GROUP_DE;
00799 _dataElemMap["s"] = SMOOTHING_GROUP_DE;
00800 _dataElemMap["o"] = OBJECT_DE;
00801 }
00802
00803 if (_mtlElemMap.empty())
00804 {
00805 _mtlElemMap[""] = UNKNOWN_ME;
00806
00807 _mtlElemMap["newmtl"] = NEW_MTL_ME;
00808 _mtlElemMap["Kd"] = MTL_DIFFUSE_ME;
00809 _mtlElemMap["Ka"] = MTL_AMBIENT_ME;
00810 _mtlElemMap["Ks"] = MTL_SPECULAR_ME;
00811 _mtlElemMap["Ns"] = MTL_SHININESS_ME;
00812 _mtlElemMap["Tr"] = MTL_TRANSPARENCY_ME;
00813 _mtlElemMap["d"] = MTL_DISSOLVE_ME;
00814 _mtlElemMap["map_Kd"] = MTL_MAP_KD_ME;
00815 _mtlElemMap["map_Ka"] = MTL_MAP_KA_ME;
00816 _mtlElemMap["map_Ks"] = MTL_MAP_KS_ME;
00817 _mtlElemMap["illum"] = MTL_ILLUM_ME;
00818 _mtlElemMap["refl"] = MTL_REFL_ME;
00819 }
00820 }
00821
00822 Int32 OBJSceneFileType::readMTL ( const Char8 *fileName,
00823 std::map<std::string, SimpleTexturedMaterialPtr> & mtlMap )
00824 const
00825 {
00826 if(fileName == NULL || strlen(fileName) == 0)
00827 return 0;
00828
00829 Int32 mtlCount = 0;
00830
00831 PathHandler *pathHandler = SceneFileHandler::the().getPathHandler();
00832 std::string fullFilePath;
00833 if(pathHandler != NULL)
00834 fullFilePath = pathHandler->findFile(fileName);
00835 else
00836 fullFilePath = fileName;
00837
00838 if(fullFilePath.empty())
00839 {
00840 FWARNING (("Couldn't open '%s'!\n", fileName));
00841 return 0;
00842 }
00843
00844 std::ifstream in(fullFilePath.c_str());
00845 SimpleTexturedMaterialPtr mtlPtr = NullFC;
00846 Real32 a,b,c;
00847 std::string elem;
00848 std::map<std::string, MaterialElem>::const_iterator elemI;
00849 MaterialElem mtlElem;
00850 std::map<std::string, OSG::ImagePtr> imageMap;
00851 std::map<std::string, OSG::ImagePtr>::iterator iI;
00852 ImagePtr image = NullFC;
00853 bool constDiffuse = false, constAmbient = false, constSpecular = false;
00854
00855 if (in)
00856 {
00857 for (in >> elem; in.eof() == false; in >> elem)
00858 {
00859 if (elem[0] == '#' || elem[0] == '$' )
00860 {
00861 in.ignore(INT_MAX, '\n');
00862 }
00863 else
00864 {
00865 elemI = _mtlElemMap.find(elem);
00866 mtlElem = ((elemI == _mtlElemMap.end()) ?
00867 UNKNOWN_ME : elemI->second);
00868 if (mtlElem == NEW_MTL_ME)
00869 {
00870 in >> elem;
00871 if (mtlPtr != NullFC)
00872 endEditCP(mtlPtr);
00873 mtlPtr = SimpleTexturedMaterial::create();
00874 OSG::setName(mtlPtr, elem.c_str());
00875 beginEditCP(mtlPtr);
00876 mtlPtr->setColorMaterial(GL_NONE);
00877 mtlPtr->setEnvMode(GL_MODULATE);
00878 mtlMap.insert(std::make_pair(elem, mtlPtr));
00879 mtlCount++;
00880 constDiffuse = false;
00881 constAmbient = false;
00882 constSpecular = false;
00883 }
00884 else
00885 {
00886 if (mtlPtr == NullFC)
00887 {
00888 FFATAL (( "Invalid Mtl token: %s, newmtl expected in %s\n",
00889 elem.c_str(), fileName ));
00890 in.ignore(INT_MAX, '\n');
00891 }
00892 else
00893 {
00894 switch (mtlElem)
00895 {
00896 case MTL_DIFFUSE_ME:
00897 in >> a >> b >> c;
00898 if (!constDiffuse)
00899 mtlPtr->setDiffuse( Color3f( a,b,c ));
00900 break;
00901 case MTL_AMBIENT_ME:
00902 in >> a >> b >> c;
00903 if (!constAmbient)
00904 mtlPtr->setAmbient( Color3f( a,b,c ));
00905 break;
00906 case MTL_SPECULAR_ME:
00907 in >> a >> b >> c;
00908 if (!constSpecular)
00909 mtlPtr->setSpecular( Color3f( a,b,c ));
00910 break;
00911 case MTL_SHININESS_ME:
00912 in >> a;
00913 mtlPtr->setShininess(a);
00914 break;
00915 case MTL_ILLUM_ME:
00916 ;
00917 in >> elem;
00918
00919 break;
00920 case MTL_REFL_ME:
00921 mtlPtr->setEnvMap(true);
00922 break;
00923 case MTL_TRANSPARENCY_ME:
00924 in >> a;
00925 mtlPtr->setTransparency(a);
00926 break;
00927 case MTL_DISSOLVE_ME:
00928 in >> a;
00929 mtlPtr->setTransparency(1.f - a);
00930 break;
00931 case MTL_MAP_KD_ME:
00932 case MTL_MAP_KA_ME:
00933 case MTL_MAP_KS_ME:
00934 image = NullFC;
00935 in >> elem;
00936 iI = imageMap.find(elem);
00937 if (iI == imageMap.end())
00938 {
00939 std::string fullElemPath;
00940 if(pathHandler != NULL)
00941 fullElemPath = pathHandler->findFile(elem.c_str());
00942 else
00943 fullElemPath = elem.c_str();
00944 image = OSG::ImageFileHandler::the().read(fullElemPath.c_str());
00945 if(image != NullFC)
00946 {
00947 beginEditCP(image,
00948 OSG::Image::ForceAlphaBinaryFieldMask);
00949 image->setForceAlphaBinary(
00950 image->calcIsAlphaBinary());
00951 endEditCP(image,
00952 OSG::Image::ForceAlphaBinaryFieldMask);
00953 imageMap[elem] = image;
00954 }
00955 }
00956 else
00957 {
00958 image = iI->second;
00959 }
00960 if (image != NullFC) {
00961 mtlPtr->setImage(image);
00962 switch (mtlElem) {
00963 case MTL_MAP_KD_ME:
00964 constDiffuse = true;
00965 mtlPtr->setDiffuse ( Color3f( 1, 1, 1) );
00966 break;
00967 case MTL_MAP_KA_ME:
00968 constAmbient = true;
00969 mtlPtr->setAmbient ( Color3f( 1, 1, 1) );
00970 break;
00971 case MTL_MAP_KS_ME:
00972 constSpecular = true;
00973 mtlPtr->setSpecular ( Color3f( 1, 1, 1) );
00974 break;
00975 default:
00976 break;
00977 }
00978 }
00979 else
00980 {
00981 FFATAL (( "Can not find %s texture file in mtl %s \n",
00982 elem.c_str(), fileName ));
00983 }
00984 break;
00985 default:
00986 FWARNING (( "Invalid %s entry in %s\n",
00987 elem.c_str(), fileName ));
00988 in.ignore(INT_MAX, '\n');
00989 }
00990 }
00991 }
00992 }
00993 }
00994 }
00995 else
00996 {
00997 FWARNING (("Couldn't open '%s'!\n", fileName));
00998 }
00999
01000 if (mtlPtr != NullFC)
01001 endEditCP(mtlPtr);
01002
01003 return mtlCount;
01004 }
01005
01006
01007
01008
01009
01010 #ifdef __sgi
01011 #pragma set woff 1174
01012 #endif
01013
01014 #ifdef OSG_LINUX_ICC
01015 #pragma warning( disable : 177 )
01016 #endif
01017
01018 namespace
01019 {
01020 static Char8 cvsid_cpp[] = "@(#)$Id: $";
01021 static Char8 cvsid_hpp[] = OSGOBJSCENEFILETYPE_HEADER_CVSID;
01022 }
01023