fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / OBJ / OSGOBJSceneFileType.cpp
bloba3e31fc7183b560c57323a4c640912f129d706a4
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * 2002/10/16: added transparency path. Thanks to Franck Sourdin *
33 * (sourdin@ai.cluny.ensam.fr) for it! *
34 * *
35 * *
36 * *
37 * *
38 \*---------------------------------------------------------------------------*/
40 //-------------------------------
41 // Includes
42 //-------------------------------
44 #include <cstdio>
45 #include <climits>
47 #include "OSGConfig.h"
49 #include "OSGGL.h"
51 #include <iostream>
52 #include <fstream>
53 #include <vector>
55 #include "OSGLog.h"
57 #include "OSGNode.h"
58 #include "OSGGeometry.h"
59 #include "OSGTypedGeoVectorProperty.h"
60 #include "OSGTypedGeoIntegralProperty.h"
61 #include "OSGGeoFunctions.h"
62 #include "OSGSimpleTexturedMaterial.h"
63 #include "OSGImageFileHandler.h"
64 #include "OSGPathHandler.h"
65 #include "OSGGroup.h"
66 #include "OSGSceneFileHandler.h"
67 #include "OSGTriangleIterator.h"
69 #include "OSGOBJSceneFileType.h"
71 OSG_USING_NAMESPACE
74 /*! \class OSG::OBJSceneFileType
75 \ingroup GrpSystemFileIO
79 #if defined(OSG_WIN32_ICL) && !defined(OSG_CHECK_FIELDSETARG)
80 #pragma warning (disable : 383)
81 #endif
83 /*****************************
84 * Types
85 *****************************/
86 // Static Class Varible implementations:
87 const Char8 *OBJSceneFileType::_suffixA[] = { "obj" };
89 OBJSceneFileType OBJSceneFileType::_the(_suffixA,
90 sizeof(_suffixA),
91 false,
92 10,
93 (SceneFileType::OSG_READ_SUPPORTED |
94 SceneFileType::OSG_WRITE_SUPPORTED));
96 /*****************************
97 * Classvariables
98 *****************************/
101 /********************************
102 * Class methodes
103 *******************************/
106 /*******************************
107 *public
108 *******************************/
110 //----------------------------
111 // Function name: read
112 //----------------------------
114 //Parameters:
115 //p: Scene &image, const char *fileName
116 //GlobalVars:
117 //g:
118 //Returns:
119 //r:bool
120 // Caution
121 //c:
122 //Assumations:
123 //a:
124 //Describtions:
125 //d: read the image from the given file
126 //SeeAlso:
127 //s:
129 //------------------------------
130 NodeTransitPtr OBJSceneFileType::read( std::istream &is,
131 const Char8 *,
132 Resolver ) const
134 NodeUnrecPtr rootPtr, nodePtr;
135 std::string elem;
136 std::map<std::string, DataElem>::const_iterator elemI;
137 Vec3f vec3r;
138 Pnt3f pnt3r;
139 Vec2f vec2r;
140 Real32 x,y,z;
141 GeoPnt3fPropertyUnrecPtr coordPtr = GeoPnt3fProperty::create();
142 GeoVec2fPropertyUnrecPtr texCoordPtr = GeoVec2fProperty::create();
143 GeoVec3fPropertyUnrecPtr normalPtr = GeoVec3fProperty::create();
144 GeometryUnrecPtr geoPtr;
145 GeoIntegralPropertyUnrecPtr posIndexPtr, texIndexPtr, normalIndexPtr;
146 GeoIntegralPropertyUnrecPtr lensPtr;
147 GeoIntegralPropertyUnrecPtr typePtr;
148 DataElem dataElem;
149 DataElem lastDataElem;
150 Char8 strBuf[8192], *token, *nextToken;
151 Int32 strBufSize = sizeof(strBuf)/sizeof(Char8);
152 Int32 index, posIndex = 0, indexType;
153 Int32 i,j,n,primCount[3];
154 std::list<Mesh> meshList;
155 std::map<std::string, SimpleTexturedMaterialUnrecPtr> mtlMap;
156 std::map<std::string, SimpleTexturedMaterialUnrecPtr>::iterator mtlI;
157 Mesh emptyMesh;
158 Face emptyFace;
159 TiePoint emptyTie;
160 Int32 indexMask, meshIndexMask;
161 std::list<Face>::iterator faceI;
162 std::list<Mesh>::iterator meshI;
163 bool isSingleIndex;
165 // create the first mesh entry
166 meshList.push_back(emptyMesh);
167 meshI = meshList.begin();
169 if(is)
171 primCount[0] = 0;
172 primCount[1] = 0;
173 primCount[2] = 0;
175 for(is >> elem; is.eof() == false; is >> elem)
177 if(elem[0] == '#' || elem[0] == '$')
179 is.ignore(INT_MAX, '\n');
181 else
183 SceneFileHandler::the()->updateReadProgress();
185 elemI = _dataElemMap.find(elem);
186 dataElem = ((elemI == _dataElemMap.end()) ?
187 UNKNOWN_DE : elemI->second );
188 switch (dataElem)
190 case OBJECT_DE:
191 case GROUP_DE:
192 case SMOOTHING_GROUP_DE:
193 is.ignore(INT_MAX, '\n');
194 break;
195 case VERTEX_DE:
196 primCount[0]++;
197 is >> x >> y >> z;
198 pnt3r.setValues(x,y,z);
199 coordPtr->addValue(pnt3r);
200 break;
201 case VERTEX_TEXTURECOORD_DE:
202 primCount[1]++;
203 is >> x >> y;
204 vec2r.setValues(x,y);
205 texCoordPtr->addValue(vec2r);
206 break;
207 case VERTEX_NORMAL_DE:
208 primCount[2]++;
209 is >> x >> y >> z;
210 vec3r.setValues(x,y,z);
211 normalPtr->addValue(vec3r);
212 break;
213 case LIB_MTL_DE:
214 is >> elem;
215 readMTL ( elem.c_str(), mtlMap );
216 is.ignore(INT_MAX, '\n');
217 break;
218 case USE_MTL_DE:
219 is >> elem;
220 if (meshI->faceList.empty() == false)
222 meshList.push_front(emptyMesh);
223 meshI = meshList.begin();
225 mtlI = mtlMap.find(elem);
226 if (mtlI == mtlMap.end())
228 FFATAL (("Unkown mtl %s\n", elem.c_str()));
230 else
232 meshI->mtlPtr = mtlI->second;
234 break;
235 case FACE_DE:
236 meshI->faceList.push_front(emptyFace);
237 faceI = meshI->faceList.begin();
238 is.get(strBuf,strBufSize);
239 token = strBuf;
240 indexType = 0;
241 while(token && *token)
243 // some tools use line continuation for long
244 // face definitions - these use a \ at the line
245 // end
246 if(*token == '\\')
248 is.ignore(1, '\n');
249 is.get(strBuf,strBufSize);
250 token = strBuf;
251 indexType = 0;
252 continue;
254 for (; *token == '/'; token++)
255 indexType++;
256 for (; isspace(*token); token++)
257 indexType = 0;
258 index = strtol(token, &nextToken, 10);
259 if (token == nextToken)
260 break;
261 if (indexType == 0)
262 faceI->tieVec.push_back(emptyTie);
263 if (index >= 0)
264 index--;
265 else
266 index = primCount[indexType] + index;
267 faceI->tieVec.back().index[indexType] = index;
268 token = nextToken;
270 break;
272 case UNKNOWN_DE:
273 default:
274 // don't warn about 3rd tex coord
275 if(lastDataElem != VERTEX_TEXTURECOORD_DE)
277 FWARNING (( "Unkown obj data elem: %s\n",
278 elem.c_str()));
280 is.ignore(INT_MAX, '\n');
281 break;
284 lastDataElem = dataElem;
288 #if 0
289 std::cerr << "------------------------------------------------" << std::endl;
290 i = 0;
291 for (meshI = meshList.begin(); meshI != meshList.end(); meshI++)
293 std::cerr << "Mesh " << i << " faceCount :"
294 << meshI->faceList.size() << std::endl;
295 j = 0 ;
296 for ( faceI = meshI->faceList.begin(); faceI != meshI->faceList.end();
297 faceI++)
298 std::cerr << "MESH " << i << "face: " << j++ << "tie num: "
299 << faceI->tieVec.size() << std::endl;
300 i++;
302 std::cerr << "------------------------------------------------" << std::endl;
303 #endif
305 // create Geometry objects
306 for (meshI = meshList.begin(); meshI != meshList.end(); meshI++)
308 geoPtr = Geometry::create();
309 posIndexPtr = NULL;
310 texIndexPtr = NULL;
311 normalIndexPtr = NULL;
312 lensPtr = GeoUInt32Property::create();
313 typePtr = GeoUInt8Property ::create();
315 // create and check mesh index mask
316 meshIndexMask = 0;
317 isSingleIndex = true;
318 if ( meshI->faceList.empty() == false)
320 for ( faceI = meshI->faceList.begin();
321 faceI != meshI->faceList.end(); faceI++)
323 indexMask = 0;
324 n = UInt32(faceI->tieVec.size());
325 for (i = 0; i < n; i++)
327 for (j = 0; j < 3; j++)
329 if ((index = (faceI->tieVec[i].index[j])) >= 0)
331 indexMask |= (1 << j);
332 if (j)
333 isSingleIndex &= (posIndex == index);
334 else
335 posIndex = index;
339 if (meshIndexMask == 0)
341 meshIndexMask = indexMask;
343 else if (meshIndexMask != indexMask)
345 // consider this real-world example:
346 // [...]
347 // f 1603//1747 1679//1744 1678//1743
348 // s 1
349 // f 9/1/10 5/2/9 1680/3/1748 1681/4/174
350 // [...]
351 // Some faces contain texture coords and others do not.
352 // The old version did just skip this geometry.
353 // This version should continue if there's at least
354 // the vertex index
355 // I've seen the change in the maskIndex only after a smooth group,
356 // so it's perhaps smarter to not ignore the smooth group further up in this code
357 if( !(indexMask & 1) )
359 // if there are vertex indices there's no reason to get in here
360 FFATAL (( "IndexMask unmatch, can not create geo\n"));
361 meshIndexMask = 0;
362 break;
364 else
366 // consider the minimum similarities of mesh masks
367 meshIndexMask &= indexMask;
372 else
374 FWARNING (("Mesh with empty faceList\n"));
377 // fill the geo properties
378 if (meshIndexMask)
380 geoPtr->setPositions ( coordPtr );
381 posIndexPtr = GeoUInt32Property::create();
382 if(!isSingleIndex)
383 geoPtr->setIndex(posIndexPtr, Geometry::PositionsIndex);
384 geoPtr->setLengths ( lensPtr );
385 geoPtr->setTypes ( typePtr );
387 if ( (meshIndexMask & 2) && texCoordPtr->size() > 0 )
389 geoPtr->setTexCoords ( texCoordPtr );
390 texIndexPtr = GeoUInt32Property::create();
391 if(!isSingleIndex)
392 geoPtr->setIndex(texIndexPtr, Geometry::TexCoordsIndex);
394 else
396 geoPtr->setTexCoords ( NULL );
399 if ( (meshIndexMask & 4) && normalPtr->size() > 0 )
401 geoPtr->setNormals ( normalPtr );
402 normalIndexPtr = GeoUInt32Property::create();
403 if(!isSingleIndex)
404 geoPtr->setIndex(normalIndexPtr, Geometry::NormalsIndex);
406 else
408 geoPtr->setNormals ( NULL );
411 if (meshI->mtlPtr == NULL)
413 meshI->mtlPtr = SimpleTexturedMaterial::create();
414 meshI->mtlPtr->setDiffuse( Color3f( .8f, .8f, .8f ) );
415 meshI->mtlPtr->setSpecular( Color3f( 1.f, 1.f, 1.f ) );
416 meshI->mtlPtr->setShininess( 20.f );
418 geoPtr->setMaterial ( meshI->mtlPtr );
420 for ( faceI = meshI->faceList.begin();
421 faceI != meshI->faceList.end(); faceI++)
423 n = UInt32(faceI->tieVec.size());
425 // add the lens entry
426 lensPtr->push_back(n);
428 // add the type entry
429 typePtr->push_back(GL_POLYGON);
431 // create the index values
432 for (i = 0; i < n; i++)
434 if (isSingleIndex)
436 posIndexPtr->push_back(faceI->tieVec[i].index[0]);
438 else
440 posIndexPtr->push_back(faceI->tieVec[i].index[0]);
441 if(texIndexPtr != NULL)
442 texIndexPtr->push_back(faceI->tieVec[i].index[1]);
443 if(normalIndexPtr != NULL)
444 normalIndexPtr->push_back(faceI->tieVec[i].index[2]);
449 if(isSingleIndex)
451 geoPtr->setIndex(posIndexPtr, Geometry::PositionsIndex);
452 geoPtr->setIndex(posIndexPtr, Geometry::NormalsIndex );
453 geoPtr->setIndex(posIndexPtr, Geometry::TexCoordsIndex);
456 // need to port the geometry functions ...
457 createSharedIndex( geoPtr );
459 // check if we have normals
460 // need to port the geometry functions ...
462 if(geoPtr->getNormals() == NULL)
463 calcVertexNormals(geoPtr);
465 // create and link the node
466 nodePtr = Node::create();
467 nodePtr->setCore( geoPtr );
469 if (meshList.size() > 1)
471 if (rootPtr == NULL)
473 rootPtr = Node::create();
475 GroupUnrecPtr tmpPtr = Group::create();
477 rootPtr->setCore ( tmpPtr );
478 rootPtr->addChild(nodePtr);
480 else
482 rootPtr->addChild(nodePtr);
485 else
487 rootPtr = nodePtr;
493 SceneFileHandler::the()->updateReadProgress(100);
495 commitChanges();
497 return NodeTransitPtr(rootPtr);
500 void OBJSceneFileType::write(Node * const node,
501 std::ostream &os,
502 UInt32 &pIndex,
503 UInt32 &nIndex,
504 UInt32 &tIndex) const
506 UInt32 i,pCount=0,nCount=0,tCount=0;
507 Geometry *g = dynamic_cast<Geometry *>(node->getCore());
508 if(g != NULL)
510 // HACK separate it in several geometry nodes.
511 os << "g Geometry" << std::endl;
512 os << "usemtl Geometry" << std::endl;
513 Matrix mat = node->getToWorld();
514 // write vertices
515 if(g->getPositions())
517 pCount = g->getPositions()->size32();
518 for(i=0 ; i< pCount ; ++i)
520 Pnt3f v;
521 g->getPositions()->getValue(v, i);
522 mat.mult(v, v);
523 os << "v " << v[0] << " " << v[1] << " " << v[2] << std::endl;
526 // write normals
527 if(g->getNormals())
529 nCount = g->getNormals()->size32();
530 for(i=0 ; i< nCount ; ++i)
532 Vec3f v;
533 g->getNormals()->getValue(v, i);
534 mat.mult(v, v);
535 os << "vn " << v[0] << " " << v[1] << " " << v[2] << std::endl;
538 // texture coords
539 if(g->getTexCoords())
541 tCount = g->getTexCoords()->size32();
542 for(i=0 ; i< tCount ; ++i)
544 Vec2f v;
545 g->getTexCoords()->getValue(v, i);
546 os << "vt " << v[0] << " " << v[1] << std::endl;
549 // write indices
550 TriangleIterator f;
551 for(f=g->beginTriangles() ; f!=g->endTriangles() ; ++f)
553 os << "f";
554 for(i=0 ; i<3 ; ++i)
556 os << " " << f.getPositionIndex(i) + pIndex;
557 if(nCount || tCount)
559 os << "/";
560 if(tCount)
561 os << f.getTexCoordsIndex(i) + tIndex;
562 if(nCount)
563 os << "/" << f.getNormalIndex(i) + nIndex;
566 os << std::endl;
568 pIndex += pCount;
569 tIndex += tCount;
570 nIndex += nCount;
572 for(MFUnrecChildNodePtr::const_iterator nI =node->getMFChildren()->begin();
573 nI!=node->getMFChildren()->end();
574 ++nI)
576 write((*nI),os,pIndex,nIndex,tIndex);
580 bool OBJSceneFileType::write(Node * const node, std::ostream &os,
581 const Char8 *fileNameOrExtension) const
583 UInt32 pIndex=1;
584 UInt32 tIndex=1;
585 UInt32 nIndex=1;
587 write(node,os,pIndex,tIndex,nIndex);
589 return true;
592 /******************************
593 *protected
594 ******************************/
597 /******************************
598 *private
599 ******************************/
602 /***************************
603 *instance methodes
604 ***************************/
607 /***************************
608 *public
609 ***************************/
612 /**constructors & destructors**/
615 //----------------------------
616 // Function name: OBJSceneFileType
617 //----------------------------
619 //Parameters:
620 //p: const char *suffixArray[], UInit16 suffixByteCount
621 //GlobalVars:
622 //g:
623 //Returns:
624 //r:
625 // Caution
626 //c:
627 //Assumations:
628 //a:
629 //Describtions:
630 //d: Default Constructor
631 //SeeAlso:
632 //s:
634 //------------------------------
636 OBJSceneFileType::OBJSceneFileType(const Char8 *suffixArray[],
637 UInt16 suffixByteCount,
638 bool override,
639 UInt32 overridePriority,
640 UInt32 flags) :
641 SceneFileType(suffixArray,
642 suffixByteCount,
643 override,
644 overridePriority,
645 flags),
646 _dataElemMap(),
647 _mtlElemMap ()
650 initElemMap();
653 //----------------------------
654 // Function name: OBJSceneFileType
655 //----------------------------
657 //Parameters:
658 //p: const OBJSceneFileType &obj
659 //GlobalVars:
660 //g:
661 //Returns:
662 //r:
663 // Caution
664 //c:
665 //Assumations:
666 //a:
667 //Describtions:
668 //d: Copy Constructor
669 //SeeAlso:
670 //s:
672 //------------------------------
674 OBJSceneFileType::OBJSceneFileType(const OBJSceneFileType &obj) :
675 SceneFileType(obj),
676 _dataElemMap ( ),
677 _mtlElemMap ( )
679 initElemMap();
682 //----------------------------
683 // Function name: ~OBJSceneFileType
684 //----------------------------
686 //Parameters:
687 //p: void
688 //GlobalVars:
689 //g:
690 //Returns:
691 //r:
692 // Caution
693 //c:
694 //Assumations:
695 //a:
696 //Describtions:
697 //d: Destructor
698 //SeeAlso:
699 //s:
701 //------------------------------
703 OBJSceneFileType &OBJSceneFileType::the(void)
705 return _the;
708 OBJSceneFileType::~OBJSceneFileType(void)
710 return;
713 const Char8 *OBJSceneFileType::getName(void) const
715 return "Wavefront Geometry";
719 /*------------access----------------*/
721 /*------------properies-------------*/
723 /*------------your Category---------*/
725 /*------------Operators-------------*/
729 /****************************
730 *protected
731 ****************************/
734 /****************************
735 *private
736 ****************************/
739 void OBJSceneFileType::initElemMap(void)
741 if (_dataElemMap.empty())
743 _dataElemMap[""] = UNKNOWN_DE;
745 _dataElemMap["v"] = VERTEX_DE;
746 _dataElemMap["vt"] = VERTEX_TEXTURECOORD_DE;
747 _dataElemMap["vn"] = VERTEX_NORMAL_DE;
748 _dataElemMap["f"] = FACE_DE;
749 _dataElemMap["fo"] = FACE_DE;
750 _dataElemMap["mtllib"] = LIB_MTL_DE;
751 _dataElemMap["usemtl"] = USE_MTL_DE;
752 _dataElemMap["g"] = GROUP_DE;
753 _dataElemMap["s"] = SMOOTHING_GROUP_DE;
754 _dataElemMap["o"] = OBJECT_DE;
757 if (_mtlElemMap.empty())
759 _mtlElemMap[""] = UNKNOWN_ME;
761 _mtlElemMap["newmtl"] = NEW_MTL_ME;
762 _mtlElemMap["Kd"] = MTL_DIFFUSE_ME;
763 _mtlElemMap["Ka"] = MTL_AMBIENT_ME;
764 _mtlElemMap["Ks"] = MTL_SPECULAR_ME;
765 _mtlElemMap["Ns"] = MTL_SHININESS_ME;
766 _mtlElemMap["Tr"] = MTL_TRANSPARENCY_ME;
767 _mtlElemMap["d"] = MTL_DISSOLVE_ME;
768 _mtlElemMap["map_Kd"] = MTL_MAP_KD_ME;
769 _mtlElemMap["map_Ka"] = MTL_MAP_KA_ME;
770 _mtlElemMap["map_Ks"] = MTL_MAP_KS_ME;
771 _mtlElemMap["illum"] = MTL_ILLUM_ME;
772 _mtlElemMap["refl"] = MTL_REFL_ME;
776 Int32 OBJSceneFileType::readMTL ( const Char8 *fileName,
777 std::map<std::string,
778 SimpleTexturedMaterialUnrecPtr> & mtlMap )
779 const
781 if(fileName == NULL || strlen(fileName) == 0)
782 return 0;
784 Int32 mtlCount = 0;
786 PathHandler *pathHandler = SceneFileHandler::the()->getPathHandler();
787 std::string fullFilePath;
789 if(pathHandler != NULL)
790 fullFilePath = pathHandler->findFile(fileName);
791 else
792 fullFilePath = fileName;
794 if(fullFilePath.empty())
796 FWARNING (("Couldn't open '%s'!\n", fileName));
797 return 0;
800 std::ifstream in(fullFilePath.c_str());
801 SimpleTexturedMaterialUnrecPtr mtlPtr = NULL;
802 Real32 a,b,c;
803 std::string elem;
804 std::map<std::string, MaterialElem>::const_iterator elemI;
805 MaterialElem mtlElem;
806 std::map<std::string, OSG::ImageUnrecPtr> imageMap;
807 std::map<std::string, OSG::ImageUnrecPtr>::iterator iI;
808 ImageUnrecPtr image = NULL;
809 bool constDiffuse = false, constAmbient = false, constSpecular = false;
811 if (in)
813 for (in >> elem; in.eof() == false; in >> elem)
815 if (elem[0] == '#' || elem[0] == '$' )
817 in.ignore(INT_MAX, '\n');
819 else
821 elemI = _mtlElemMap.find(elem);
822 mtlElem = ((elemI == _mtlElemMap.end()) ?
823 UNKNOWN_ME : elemI->second);
824 if (mtlElem == NEW_MTL_ME)
826 in >> elem;
827 mtlPtr = SimpleTexturedMaterial::create();
828 mtlPtr->setColorMaterial(GL_NONE);
829 mtlPtr->setEnvMode(GL_MODULATE);
830 mtlMap[elem] = mtlPtr;
831 mtlCount++;
832 constDiffuse = false;
833 constAmbient = false;
834 constSpecular = false;
836 else
838 if (mtlPtr == NULL)
840 FFATAL (( "Invalid Mtl token: %s, newmtl expected in %s\n",
841 elem.c_str(), fileName ));
842 in.ignore(INT_MAX, '\n');
844 else
846 switch (mtlElem)
848 case MTL_DIFFUSE_ME:
849 in >> a >> b >> c;
850 if (!constDiffuse)
851 mtlPtr->setDiffuse( Color3f( a,b,c ));
852 break;
853 case MTL_AMBIENT_ME:
854 in >> a >> b >> c;
855 if (!constAmbient)
856 mtlPtr->setAmbient( Color3f( a,b,c ));
857 break;
858 case MTL_SPECULAR_ME:
859 in >> a >> b >> c;
860 if (!constSpecular)
861 mtlPtr->setSpecular( Color3f( a,b,c ));
862 break;
863 case MTL_SHININESS_ME:
864 in >> a;
865 mtlPtr->setShininess(a);
866 break;
867 case MTL_ILLUM_ME:
868 ; // TODO: What to do with illum ?!?
869 in >> elem;
870 // FFATAL (("obj mtl illum not handled yet\n"));
871 break;
872 case MTL_REFL_ME:
873 mtlPtr->setEnvMap(true);
874 break;
875 case MTL_TRANSPARENCY_ME:
876 in >> a;
877 mtlPtr->setTransparency(a);
878 break;
879 case MTL_DISSOLVE_ME:
880 in >> a;
881 mtlPtr->setTransparency(1.f - a);
882 break;
883 case MTL_MAP_KD_ME:
884 case MTL_MAP_KA_ME:
885 case MTL_MAP_KS_ME:
886 image = NULL;
887 in >> elem;
888 iI = imageMap.find(elem);
889 if (iI == imageMap.end())
891 std::string fullElemPath;
892 if(pathHandler != NULL)
893 fullElemPath = pathHandler->findFile(elem.c_str());
894 else
895 fullElemPath = elem.c_str();
896 image = OSG::ImageFileHandler::the()->read(fullElemPath.c_str());
898 if(image != NULL)
900 image->setForceAlphaBinary(
901 image->calcIsAlphaBinary());
903 imageMap[elem] = image;
906 else
908 image = iI->second;
910 if (image != NULL)
912 mtlPtr->setImage(image);
913 switch (mtlElem)
915 case MTL_MAP_KD_ME:
916 constDiffuse = true;
917 mtlPtr->setDiffuse ( Color3f( 1.f, 1.f, 1.f) );
918 break;
919 case MTL_MAP_KA_ME:
920 constAmbient = true;
921 mtlPtr->setAmbient ( Color3f( 1.f, 1.f, 1.f) );
922 break;
923 case MTL_MAP_KS_ME:
924 constSpecular = true;
925 mtlPtr->setSpecular ( Color3f( 1.f, 1.f, 1.f) );
926 break;
927 default:
928 break;
931 else
933 FFATAL (( "Can not find %s texture file in mtl %s \n",
934 elem.c_str(), fileName ));
936 break;
937 default:
938 FWARNING (( "Invalid %s entry in %s\n",
939 elem.c_str(), fileName ));
940 in.ignore(INT_MAX, '\n');
947 return mtlCount;