osg::OBJSceneFileType Class Reference
[File Input/Output]

OBJSceneFileType.

#include <OSGOBJSceneFileType.h>

Inheritance diagram for osg::OBJSceneFileType:
osg::SceneFileType

List of all members.

Classes

struct  Face
struct  Mesh
struct  TiePoint

Public Types

typedef std::vector
< FieldContainerPtr
FCPtrStore
Flags



enum  { OSG_READ_SUPPORTED = 1, OSG_WRITE_SUPPORTED = 2 }

Public Member Functions

Destructors



virtual ~OBJSceneFileType (void)
Get



virtual const Char8getName (void) const
Read



virtual NodePtr read (std::istream &is, const Char8 *fileNameOrExtension) const
Write



virtual bool write (const NodePtr &node, std::ostream &os, const Char8 *fileNameOrExtension) const
Set



void setOptions (const Char8 *options)
Get



virtual std::list< IDString > & suffixList (void)
bool doOverride (void)
UInt32 getOverridePriority (void)
UInt32 getFlags (void)
const Char8getOptions (void)
Read



virtual NodePtr readFile (const Char8 *fileName) const
Write



virtual bool writeFile (const NodePtr &node, const Char8 *fileName) const
Debug



void print (void)

Static Public Member Functions

Class Get



static OBJSceneFileTypethe (void)

Protected Member Functions

Constructors



 OBJSceneFileType (const Char8 *suffixArray[], UInt16 suffixByteCount, bool override, UInt32 overridePriority, UInt32 flags)
 OBJSceneFileType (const OBJSceneFileType &obj)

Protected Attributes

Member



std::list< IDString_suffixList
bool _override
UInt32 _overridePriority
UInt32 _flags
std::string _options

Static Protected Attributes

Member



static const Char8_suffixA [] = { "obj" }
static OBJSceneFileType _the

Private Types

enum  DataElem {
  UNKNOWN_DE = 0, VERTEX_DE, VERTEX_TEXTURECOORD_DE, VERTEX_NORMAL_DE,
  FACE_DE, LIB_MTL_DE, USE_MTL_DE, GROUP_DE,
  SMOOTHING_GROUP_DE, OBJECT_DE
}
enum  MaterialElem {
  UNKNOWN_ME = 0, NEW_MTL_ME, MTL_DIFFUSE_ME, MTL_AMBIENT_ME,
  MTL_SPECULAR_ME, MTL_SHININESS_ME, MTL_ILLUM_ME, MTL_TRANSPARENCY_ME,
  MTL_DISSOLVE_ME, MTL_MAP_KD_ME, MTL_MAP_KA_ME, MTL_MAP_KS_ME,
  MTL_REFL_ME
}
typedef SceneFileType Inherited

Private Member Functions

void initElemMap (void)
Int32 readMTL (const Char8 *fileName, std::map< std::string, SimpleTexturedMaterialPtr > &mtlMap) const
void write (const NodePtr &node, std::ostream &os, UInt32 &pIndex, UInt32 &nIndex, UInt32 &tIndex) const
void operator= (const OBJSceneFileType &source)
 prohibit default function (move to 'public' if needed)

Private Attributes

std::map< std::string, DataElem_dataElemMap
std::map< std::string,
MaterialElem
_mtlElemMap

Friends

class Mesh
class Face

Detailed Description

Definition at line 57 of file OSGOBJSceneFileType.h.


Member Typedef Documentation

Definition at line 126 of file OSGOBJSceneFileType.h.

typedef std::vector<FieldContainerPtr> osg::SceneFileType::FCPtrStore [inherited]

Reimplemented in osg::OSGSceneFileType, and osg::VRMLSceneFileType.

Definition at line 65 of file OSGSceneFileType.h.


Member Enumeration Documentation

Enumerator:
UNKNOWN_DE 
VERTEX_DE 
VERTEX_TEXTURECOORD_DE 
VERTEX_NORMAL_DE 
FACE_DE 
LIB_MTL_DE 
USE_MTL_DE 
GROUP_DE 
SMOOTHING_GROUP_DE 
OBJECT_DE 

Definition at line 128 of file OSGOBJSceneFileType.h.

00129     {
00130       UNKNOWN_DE = 0,
00131 
00132       VERTEX_DE, VERTEX_TEXTURECOORD_DE, VERTEX_NORMAL_DE,
00133       FACE_DE,
00134 
00135       LIB_MTL_DE,
00136       USE_MTL_DE,
00137 
00138       GROUP_DE, SMOOTHING_GROUP_DE, OBJECT_DE
00139     };

Enumerator:
UNKNOWN_ME 
NEW_MTL_ME 
MTL_DIFFUSE_ME 
MTL_AMBIENT_ME 
MTL_SPECULAR_ME 
MTL_SHININESS_ME 
MTL_ILLUM_ME 
MTL_TRANSPARENCY_ME 
MTL_DISSOLVE_ME 
MTL_MAP_KD_ME 
MTL_MAP_KA_ME 
MTL_MAP_KS_ME 
MTL_REFL_ME 

Definition at line 143 of file OSGOBJSceneFileType.h.

anonymous enum [inherited]
Enumerator:
OSG_READ_SUPPORTED 
OSG_WRITE_SUPPORTED 

Definition at line 78 of file OSGSceneFileType.h.

00079     {
00080         OSG_READ_SUPPORTED = 1,
00081         OSG_WRITE_SUPPORTED = 2
00082     };


Constructor & Destructor Documentation

OBJSceneFileType::~OBJSceneFileType ( void   )  [virtual]

Definition at line 754 of file OSGOBJSceneFileType.cpp.

00755 {
00756     return;
00757 }

OBJSceneFileType::OBJSceneFileType ( const Char8 suffixArray[],
UInt16  suffixByteCount,
bool  override,
UInt32  overridePriority,
UInt32  flags 
) [protected]

constructors & destructors

Definition at line 685 of file OSGOBJSceneFileType.cpp.

References initElemMap().

00689                                                   :
00690     SceneFileType(suffixArray,
00691                   suffixByteCount,
00692                   override,
00693                   overridePriority,
00694                   flags),
00695     _dataElemMap()
00696 
00697 {
00698     initElemMap();
00699 }

OBJSceneFileType::OBJSceneFileType ( const OBJSceneFileType obj  )  [protected]

constructors & destructors

Definition at line 722 of file OSGOBJSceneFileType.cpp.

References initElemMap().

00722                                                               :
00723     SceneFileType(obj)
00724 {
00725     initElemMap();
00726 }


Member Function Documentation

OBJSceneFileType & OBJSceneFileType::the ( void   )  [static]

Definition at line 749 of file OSGOBJSceneFileType.cpp.

References _the.

Referenced by read(), and readMTL().

00750 {
00751     return _the;
00752 }

const Char8 * OBJSceneFileType::getName ( void   )  const [virtual]

Implements osg::SceneFileType.

Definition at line 759 of file OSGOBJSceneFileType.cpp.

00760 {
00761     return "Wavefront Geometry";
00762 }

NodePtr OBJSceneFileType::read ( std::istream &  is,
const Char8 fileNameOrExtension 
) const [virtual]

Reimplemented from osg::SceneFileType.

Definition at line 142 of file OSGOBJSceneFileType.cpp.

References _dataElemMap, osg::beginEditCP(), osg::calcVertexNormals(), osg::GroupBase::create(), osg::Node::create(), osg::SimpleTexturedMaterialBase::create(), osg::GeometryBase::create(), osg::GeoProperty< GeoPropertyDesc >::create(), osg::createSharedIndex(), osg::endEditCP(), FACE_DE, FFATAL, FWARNING, GROUP_DE, INT_MAX, LIB_MTL_DE, osg::Geometry::MapNormal, osg::Geometry::MapPosition, osg::Geometry::MapTexCoords, osg::NullFC, OBJECT_DE, readMTL(), osg::setName(), SMOOTHING_GROUP_DE, the(), UNKNOWN_DE, USE_MTL_DE, VERTEX_DE, VERTEX_NORMAL_DE, and VERTEX_TEXTURECOORD_DE.

Referenced by readMTL().

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     // create the first mesh entry
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                         //is.ignore(INT_MAX, '\n');
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                         //is >> elem;
00227                         // operator>> doesn't work for filenames with a space.
00228                         elem = "";
00229                         std::getline (is, elem);
00230                         // remove white space.
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                         //is.ignore(INT_MAX, '\n');
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                         // support for new lines via backslash.
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                             // skip whitespace.
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             // create Geometry objects
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                 // create and check mesh index mask
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                 // consider this real-world example:
00368                 // [...]
00369                 // f 1603//1747 1679//1744 1678//1743
00370                 // s 1
00371                 // f 9/1/10 5/2/9 1680/3/1748 1681/4/174
00372                 // [...]
00373                 // Some faces contain texture coords and others do not.
00374                 // The old version did just skip this geometry.
00375                 // This version should continue if there's at least 
00376                 // the vertex index
00377                 // I've seen the change in the maskIndex only after a smooth group,
00378                 // so it's perhaps smarter to not ignore the smooth group further up in this code
00379                 if( !(indexMask & 1) )  {
00380                   // if there are vertex indices there's no reason to get in here
00381                                   FFATAL (( "IndexMask unmatch, can not create geo\n"));
00382                                   meshIndexMask = 0;
00383                                   break;
00384                 }else{
00385                   // consider the minimum similarities of mesh masks
00386                   meshIndexMask &= indexMask;
00387                 }
00388                             }
00389                     }
00390                 else
00391                 {
00392                     FWARNING (("Mesh with empty faceList\n"));
00393                 }
00394 
00395                 // fill the geo properties
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                 // just make sure that there are tex coords for ALL meshes
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                 // just make sure that there are normals for ALL vertices
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                         // add the lens entry
00485                         lensPtr->push_back(n);
00486 
00487                         // add the type entry
00488                         typePtr->push_back(GL_POLYGON);
00489 
00490                         // create the index values
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                     // check if we have normals
00507                     if ((meshIndexMask & 4) == 0)
00508                         calcVertexNormals(geoPtr);
00509 
00510                     // create and link the node
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 }

bool OBJSceneFileType::write ( const NodePtr node,
std::ostream &  os,
const Char8 fileNameOrExtension 
) const [virtual]

Reimplemented from osg::SceneFileType.

Definition at line 629 of file OSGOBJSceneFileType.cpp.

Referenced by write().

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 }

void OBJSceneFileType::initElemMap ( void   )  [private]

Definition at line 785 of file OSGOBJSceneFileType.cpp.

References _dataElemMap, _mtlElemMap, FACE_DE, GROUP_DE, LIB_MTL_DE, MTL_AMBIENT_ME, MTL_DIFFUSE_ME, MTL_DISSOLVE_ME, MTL_ILLUM_ME, MTL_MAP_KA_ME, MTL_MAP_KD_ME, MTL_MAP_KS_ME, MTL_REFL_ME, MTL_SHININESS_ME, MTL_SPECULAR_ME, MTL_TRANSPARENCY_ME, NEW_MTL_ME, OBJECT_DE, SMOOTHING_GROUP_DE, UNKNOWN_DE, UNKNOWN_ME, USE_MTL_DE, VERTEX_DE, VERTEX_NORMAL_DE, and VERTEX_TEXTURECOORD_DE.

Referenced by OBJSceneFileType().

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 }

Int32 OBJSceneFileType::readMTL ( const Char8 fileName,
std::map< std::string, SimpleTexturedMaterialPtr > &  mtlMap 
) const [private]

Definition at line 822 of file OSGOBJSceneFileType.cpp.

References _mtlElemMap, osg::beginEditCP(), osg::SimpleTexturedMaterialBase::create(), osg::endEditCP(), FFATAL, osg::PathHandler::findFile(), FWARNING, INT_MAX, MTL_AMBIENT_ME, MTL_DIFFUSE_ME, MTL_DISSOLVE_ME, MTL_ILLUM_ME, MTL_MAP_KA_ME, MTL_MAP_KD_ME, MTL_MAP_KS_ME, MTL_REFL_ME, MTL_SHININESS_ME, MTL_SPECULAR_ME, MTL_TRANSPARENCY_ME, NEW_MTL_ME, osg::NullFC, read(), osg::setName(), the(), and UNKNOWN_ME.

Referenced by read().

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                                 ; // TODO: What to do with illum ?!?
00917                                 in >> elem;
00918                                 // FFATAL (("obj mtl illum not handled yet\n"));
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 }

void OBJSceneFileType::write ( const NodePtr node,
std::ostream &  os,
UInt32 pIndex,
UInt32 nIndex,
UInt32 tIndex 
) const [private]

Definition at line 551 of file OSGOBJSceneFileType.cpp.

References osg::AttachmentContainerPtr::dcast(), osg::NodePtr::getCore(), osg::TriangleIterator::getNormalIndex(), osg::TriangleIterator::getPositionIndex(), osg::TriangleIterator::getTexCoordsIndex(), osg::TransformationMatrix< ValueTypeT >::mult(), osg::NullFC, and write().

00556 {
00557     UInt32 i,pCount=0,nCount=0,tCount=0;
00558     GeometryPtr g = GeometryPtr::dcast(node->getCore());
00559     if(g != NullFC)
00560     {
00561         // HACK separate it in several geometry nodes.
00562         os << "g Geometry" << std::endl;
00563         os << "usemtl Geometry" << std::endl;
00564         Matrix mat = node->getToWorld();
00565         // write vertices
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         // write normals
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         // texture coords
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         // write indices
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 }

void osg::OBJSceneFileType::operator= ( const OBJSceneFileType source  )  [private]

Reimplemented from osg::SceneFileType.

void SceneFileType::setOptions ( const Char8 options  )  [inherited]

Definition at line 115 of file OSGSceneFileType.cpp.

References osg::SceneFileType::_options.

Referenced by osg::SceneFileHandler::setOptions().

00116 {
00117     _options = options;
00118 }

std::list< IDString > & SceneFileType::suffixList ( void   )  [virtual, inherited]
bool SceneFileType::doOverride ( void   )  [inherited]
UInt32 SceneFileType::getOverridePriority ( void   )  [inherited]
UInt32 SceneFileType::getFlags ( void   )  [inherited]

Definition at line 159 of file OSGSceneFileType.cpp.

References osg::SceneFileType::_flags.

Referenced by osg::SceneFileHandler::getSuffixList(), and osg::SceneFileHandler::print().

00160 {
00161     return _flags;
00162 }

const Char8 * SceneFileType::getOptions ( void   )  [inherited]

Definition at line 166 of file OSGSceneFileType.cpp.

References osg::SceneFileType::_options.

Referenced by osg::SceneFileHandler::getOptions().

00167 {
00168     return _options.c_str();
00169 }

NodePtr SceneFileType::readFile ( const Char8 fileName  )  const [virtual, inherited]

Definition at line 181 of file OSGSceneFileType.cpp.

References FWARNING, and osg::NullFC.

Referenced by osg::SceneFileHandler::read().

00182 {
00183     FWARNING (("FILE INTERFACE NOT IMPLEMENTED!\n"));
00184     return NullFC;
00185 }

bool SceneFileType::writeFile ( const NodePtr node,
const Char8 fileName 
) const [virtual, inherited]

Definition at line 197 of file OSGSceneFileType.cpp.

References FWARNING.

Referenced by osg::SceneFileHandler::write().

00199 {
00200     FWARNING (("FILE INTERFACE NOT IMPLEMENTED!\n"));
00201     return false;
00202 }

void SceneFileType::print ( void   )  [inherited]

Definition at line 121 of file OSGSceneFileType.cpp.

References osg::SceneFileType::_suffixList, osg::SceneFileType::getName(), osg::LOG_DEBUG, osg::osgLog(), and osg::Log::stream().

00122 {
00123     std::list<IDString>::iterator sI;
00124 
00125     osgLog() << getName();
00126 
00127     if (_suffixList.empty())
00128         osgLog() << "NONE";
00129     else
00130         for (sI = _suffixList.begin(); sI != _suffixList.end(); sI++)
00131             osgLog().stream(OSG::LOG_DEBUG) << sI->str() << " ";
00132 
00133     osgLog() << std::endl;
00134 }


Friends And Related Function Documentation

friend class Mesh [friend]

Definition at line 159 of file OSGOBJSceneFileType.h.

friend class Face [friend]

Definition at line 162 of file OSGOBJSceneFileType.h.


Member Data Documentation

const Char8 * OBJSceneFileType::_suffixA = { "obj" } [static, protected]

Definition at line 106 of file OSGOBJSceneFileType.h.

Definition at line 107 of file OSGOBJSceneFileType.h.

Referenced by the().

std::map<std::string, DataElem> osg::OBJSceneFileType::_dataElemMap [private]

Definition at line 141 of file OSGOBJSceneFileType.h.

Referenced by initElemMap(), and read().

std::map<std::string, MaterialElem> osg::OBJSceneFileType::_mtlElemMap [private]

Definition at line 155 of file OSGOBJSceneFileType.h.

Referenced by initElemMap(), and readMTL().

std::list<IDString> osg::SceneFileType::_suffixList [protected, inherited]
bool osg::SceneFileType::_override [protected, inherited]

Definition at line 154 of file OSGSceneFileType.h.

Referenced by osg::SceneFileType::doOverride().

Definition at line 155 of file OSGSceneFileType.h.

Referenced by osg::SceneFileType::getOverridePriority().

UInt32 osg::SceneFileType::_flags [protected, inherited]

Definition at line 156 of file OSGSceneFileType.h.

Referenced by osg::SceneFileType::getFlags().

std::string osg::SceneFileType::_options [protected, inherited]

The documentation for this class was generated from the following files:

Generated on 8 Feb 2010 for OpenSG by  doxygen 1.6.1