1 // Author: Kirill Gavrilov
2 // Copyright (c) 2017-2019 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <RWObj_MtlReader.hxx>
17 #include <RWObj_Tools.hxx>
19 #include <Message.hxx>
20 #include <Message_Messenger.hxx>
21 #include <OSD_File.hxx>
22 #include <OSD_OpenFile.hxx>
23 #include <OSD_Path.hxx>
27 //! Try to find a new location of the file relative to specified folder from absolute path.
28 //! @param theAbsolutePath original absolute file path
29 //! @param theNewFoler the new folder to look for the file
30 //! @param theRelativePath result file path relative to theNewFoler
31 //! @return true if relative file has been found
32 static bool findRelativePath (const TCollection_AsciiString& theAbsolutePath,
33 const TCollection_AsciiString& theNewFoler,
34 TCollection_AsciiString& theRelativePath)
36 TCollection_AsciiString aNewFoler = (theNewFoler.EndsWith ("\\") || theNewFoler.EndsWith ("/"))
38 : (theNewFoler + "/");
40 TCollection_AsciiString aRelPath;
41 TCollection_AsciiString aPath = theAbsolutePath;
44 TCollection_AsciiString aFolder, aFileName;
45 OSD_Path::FolderAndFileFromPath (aPath, aFolder, aFileName);
47 || aFileName.IsEmpty())
52 if (aRelPath.IsEmpty())
58 aRelPath = aFileName + "/" + aRelPath;
61 if (OSD_File (aNewFoler + aRelPath).Exists())
63 theRelativePath = aRelPath;
71 // =======================================================================
72 // function : RWObj_MtlReader
74 // =======================================================================
75 RWObj_MtlReader::RWObj_MtlReader (NCollection_DataMap<TCollection_AsciiString, RWObj_Material>& theMaterials)
77 myMaterials (&theMaterials),
83 // =======================================================================
84 // function : ~RWObj_MtlReader
86 // =======================================================================
87 RWObj_MtlReader::~RWObj_MtlReader()
95 // =======================================================================
98 // =======================================================================
99 bool RWObj_MtlReader::Read (const TCollection_AsciiString& theFolder,
100 const TCollection_AsciiString& theFile)
102 myPath = theFolder + theFile;
103 myFile = OSD_OpenFile (myPath.ToCString(), "rb");
106 Message::DefaultMessenger()->Send (TCollection_AsciiString ("OBJ material file '") + myPath + "' is not found!", Message_Warning);
107 return Standard_False;
110 char aLine[256] = {};
111 TCollection_AsciiString aMatName;
113 const Standard_Integer aNbMatOld = myMaterials->Extent();
114 bool hasAspect = false;
115 for (; ::feof (myFile) == 0 && ::fgets (aLine, 255, myFile) != NULL; )
119 const char* aPos = aLine;
122 for (; IsSpace(*aPos);)
134 if (::memcmp (aPos, "newmtl", 6) == 0)
137 if (!aMatName.IsEmpty())
141 aMat.Name = aMatName;
145 // reset incomplete material definition
146 aMat = RWObj_Material();
148 myMaterials->Bind (aMatName, aMat);
152 aMatName = TCollection_AsciiString(aPos);
153 aMat = RWObj_Material();
154 if (!RWObj_Tools::ReadName (aPos, aMatName))
156 Message::DefaultMessenger()->Send (TCollection_AsciiString("Empty OBJ material at line ") + myNbLines + " in file " + myPath, Message_Warning);
159 else if (::memcmp (aPos, "Ka", 2) == 0
160 && IsSpace (aPos[2]))
164 Graphic3d_Vec3 aColor;
165 RWObj_Tools::ReadVec3 (aPos, aNext, aColor);
167 if (validateColor (aColor))
169 aMat.AmbientColor = Quantity_Color (aColor.r(), aColor.g(), aColor.b(), Quantity_TOC_RGB);
173 else if (::memcmp (aPos, "Kd", 2) == 0
174 && IsSpace (aPos[2]))
178 Graphic3d_Vec3 aColor;
179 RWObj_Tools::ReadVec3 (aPos, aNext, aColor);
181 if (validateColor (aColor))
183 aMat.DiffuseColor = Quantity_Color (aColor.r(), aColor.g(), aColor.b(), Quantity_TOC_RGB);
187 else if (::memcmp (aPos, "Ks", 2) == 0
188 && IsSpace (aPos[2]))
192 Graphic3d_Vec3 aColor;
193 RWObj_Tools::ReadVec3 (aPos, aNext, aColor);
195 if (validateColor (aColor))
197 aMat.SpecularColor = Quantity_Color (aColor.r(), aColor.g(), aColor.b(), Quantity_TOC_RGB);
200 else if (::memcmp (aPos, "Ns", 2) == 0
201 && IsSpace (aPos[2]))
205 double aSpecular = Strtod (aPos, &aNext);
207 if (aSpecular >= 0.0)
209 aMat.Shininess = (float )Min (aSpecular / 1000.0, 1.0);
212 else if (::memcmp (aPos, "Tr", 2) == 0
213 && IsSpace (aPos[2]))
217 double aTransp = Strtod (aPos, &aNext);
219 if (validateScalar (aTransp)
222 aMat.Transparency = (float )aTransp;
225 else if (*aPos == 'd' && IsSpace (aPos[1]))
230 double anAlpha = Strtod (aPos, &aNext);
232 if (validateScalar (anAlpha)
235 aMat.Transparency = float(1.0 - anAlpha);
238 else if (::memcmp (aPos, "map_Kd", 6) == 0
239 && IsSpace (aPos[6]))
242 if (RWObj_Tools::ReadName (aPos, aMat.DiffuseTexture))
244 processTexturePath (aMat.DiffuseTexture, theFolder);
247 else if (::memcmp (aPos, "map_Ks", 6) == 0
248 && IsSpace (aPos[6]))
251 if (RWObj_Tools::ReadName (aPos, aMat.SpecularTexture))
253 processTexturePath (aMat.SpecularTexture, theFolder);
256 else if (::memcmp (aPos, "map_Bump", 8) == 0
257 && IsSpace (aPos[8]))
260 if (RWObj_Tools::ReadName (aPos, aMat.BumpTexture))
262 processTexturePath (aMat.BumpTexture, theFolder);
265 /*else if (::memcmp (aPos, "illum", 5) == 0)
269 const int aModel = strtol (aPos, &aNext, 10);
271 if (aModel < 0 || aModel > 10)
278 if (!aMatName.IsEmpty())
282 aMat.Name = aMatName;
286 // reset incomplete material definition
287 aMat = RWObj_Material();
289 myMaterials->Bind (aMatName, aMat);
292 return myMaterials->Extent() != aNbMatOld;
295 // =======================================================================
296 // function : processTexturePath
298 // =======================================================================
299 void RWObj_MtlReader::processTexturePath (TCollection_AsciiString& theTexturePath,
300 const TCollection_AsciiString& theFolder)
302 if (OSD_Path::IsAbsolutePath (theTexturePath.ToCString()))
304 Message::DefaultMessenger()->Send (TCollection_AsciiString("OBJ file specifies absolute path to the texture image file which may be inaccessible on another device\n")
305 + theTexturePath, Message_Warning);
306 if (!OSD_File (theTexturePath).Exists())
308 // workaround absolute filenames - try to find the same file at the OBJ file location
309 TCollection_AsciiString aRelativePath;
310 if (findRelativePath (theTexturePath, theFolder, aRelativePath))
312 theTexturePath = theFolder + aRelativePath;
318 theTexturePath = theFolder + theTexturePath;
322 // =======================================================================
323 // function : validateScalar
325 // =======================================================================
326 bool RWObj_MtlReader::validateScalar (const Standard_Real theValue)
331 Message::DefaultMessenger()->Send (TCollection_AsciiString("Invalid scalar in OBJ material at line ") + myNbLines + " in file " + myPath, Message_Warning);
337 // =======================================================================
338 // function : validateColor
340 // =======================================================================
341 bool RWObj_MtlReader::validateColor (const Graphic3d_Vec3& theVec)
343 if (theVec.r() < 0.0f || theVec.r() > 1.0f
344 || theVec.g() < 0.0f || theVec.g() > 1.0f
345 || theVec.b() < 0.0f || theVec.b() > 1.0f)
347 Message::DefaultMessenger()->Send (TCollection_AsciiString("Invalid color in OBJ material at line ") + myNbLines + " in file " + myPath, Message_Warning);