1 // Created on: 1997-07-28
2 // Created by: Pierre CHALAMET
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <Graphic3d_TextureRoot.hxx>
18
19 #include <Graphic3d_GraphicDriver.hxx>
20 #include <Graphic3d_TextureParams.hxx>
21 #include <Image_AlienPixMap.hxx>
22 #include <Image_DDSParser.hxx>
23 #include <Image_SupportedFormats.hxx>
24 #include <OSD_Directory.hxx>
25 #include <OSD_Environment.hxx>
26 #include <OSD_File.hxx>
27 #include <OSD_OpenFile.hxx>
28 #include <OSD_Protection.hxx>
29 #include <Standard_Atomic.hxx>
30
31 IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_TextureRoot, Standard_Transient)
32
33 namespace
34 {
35 static volatile Standard_Integer THE_TEXTURE_COUNTER = 0;
36 }
37
38 // =======================================================================
39 // function : TexturesFolder
40 // purpose :
41 // =======================================================================
TexturesFolder()42 TCollection_AsciiString Graphic3d_TextureRoot::TexturesFolder()
43 {
44 static Standard_Boolean IsDefined = Standard_False;
45 static TCollection_AsciiString VarName;
46 if (!IsDefined)
47 {
48 IsDefined = Standard_True;
49 OSD_Environment aTexDirEnv ("CSF_MDTVTexturesDirectory");
50 VarName = aTexDirEnv.Value();
51 if (VarName.IsEmpty())
52 {
53 OSD_Environment aCasRootEnv ("CASROOT");
54 VarName = aCasRootEnv.Value();
55 if (!VarName.IsEmpty())
56 {
57 VarName += "/src/Textures";
58 }
59 }
60
61 if (VarName.IsEmpty())
62 {
63 #ifdef OCCT_DEBUG
64 std::cerr << "Both environment variables CSF_MDTVTexturesDirectory and CASROOT are undefined!\n"
65 << "At least one should be defined to use standard Textures.\n";
66 #endif
67 throw Standard_Failure("CSF_MDTVTexturesDirectory and CASROOT are undefined");
68 }
69
70 const OSD_Path aDirPath (VarName);
71 OSD_Directory aDir (aDirPath);
72 const TCollection_AsciiString aTexture = VarName + "/2d_MatraDatavision.rgb";
73 OSD_File aTextureFile (aTexture);
74 if (!aDir.Exists() || !aTextureFile.Exists())
75 {
76 #ifdef OCCT_DEBUG
77 std::cerr << " CSF_MDTVTexturesDirectory or CASROOT not correctly set\n";
78 std::cerr << " not all files are found in : "<< VarName.ToCString() << std::endl;
79 #endif
80 throw Standard_Failure("CSF_MDTVTexturesDirectory or CASROOT not correctly set");
81 }
82 }
83 return VarName;
84 }
85
86 // =======================================================================
87 // function : Graphic3d_TextureRoot
88 // purpose :
89 // =======================================================================
Graphic3d_TextureRoot(const TCollection_AsciiString & theFileName,const Graphic3d_TypeOfTexture theType)90 Graphic3d_TextureRoot::Graphic3d_TextureRoot (const TCollection_AsciiString& theFileName,
91 const Graphic3d_TypeOfTexture theType)
92 : myParams (new Graphic3d_TextureParams()),
93 myPath (theFileName),
94 myRevision (0),
95 myType (theType),
96 myIsColorMap (true),
97 myIsTopDown (true)
98 {
99 generateId();
100 }
101
102 // =======================================================================
103 // function : Graphic3d_TextureRoot
104 // purpose :
105 // =======================================================================
Graphic3d_TextureRoot(const Handle (Image_PixMap)& thePixMap,const Graphic3d_TypeOfTexture theType)106 Graphic3d_TextureRoot::Graphic3d_TextureRoot (const Handle(Image_PixMap)& thePixMap,
107 const Graphic3d_TypeOfTexture theType)
108 : myParams (new Graphic3d_TextureParams()),
109 myPixMap (thePixMap),
110 myRevision (0),
111 myType (theType),
112 myIsColorMap (true),
113 myIsTopDown (true)
114 {
115 generateId();
116 }
117
118 // =======================================================================
119 // function : ~Graphic3d_TextureRoot
120 // purpose :
121 // =======================================================================
~Graphic3d_TextureRoot()122 Graphic3d_TextureRoot::~Graphic3d_TextureRoot()
123 {
124 //
125 }
126
127 // =======================================================================
128 // function : generateId
129 // purpose :
130 // =======================================================================
generateId()131 void Graphic3d_TextureRoot::generateId()
132 {
133 myTexId = TCollection_AsciiString ("Graphic3d_TextureRoot_")
134 + TCollection_AsciiString (Standard_Atomic_Increment (&THE_TEXTURE_COUNTER));
135 }
136
137 // =======================================================================
138 // function : GetCompressedImage
139 // purpose :
140 // =======================================================================
Handle(Image_CompressedPixMap)141 Handle(Image_CompressedPixMap) Graphic3d_TextureRoot::GetCompressedImage (const Handle(Image_SupportedFormats)& theSupported)
142 {
143 if (!myPixMap.IsNull())
144 {
145 return Handle(Image_CompressedPixMap)();
146 }
147
148 // Case 2: texture source is specified as path
149 TCollection_AsciiString aFilePath;
150 myPath.SystemName (aFilePath);
151 if (aFilePath.IsEmpty())
152 {
153 return Handle(Image_CompressedPixMap)();
154 }
155
156 TCollection_AsciiString aFilePathLower = aFilePath;
157 aFilePathLower.LowerCase();
158 if (!aFilePathLower.EndsWith (".dds"))
159 {
160 // do not waste time on file system access in case of wrong file extension
161 return Handle(Image_CompressedPixMap)();
162 }
163
164 if (Handle(Image_CompressedPixMap) anImage = Image_DDSParser::Load (theSupported, aFilePath, 0))
165 {
166 myIsTopDown = anImage->IsTopDown();
167 return anImage;
168 }
169 return Handle(Image_CompressedPixMap)();
170 }
171
172 // =======================================================================
173 // function : GetImage
174 // purpose :
175 // =======================================================================
Handle(Image_PixMap)176 Handle(Image_PixMap) Graphic3d_TextureRoot::GetImage (const Handle(Image_SupportedFormats)& theSupported)
177 {
178 if (Handle(Image_PixMap) anOldImage = GetImage())
179 {
180 myIsTopDown = anOldImage->IsTopDown();
181 return anOldImage; // compatibility with old API
182 }
183
184 // Case 1: texture source is specified as pixmap
185 if (!myPixMap.IsNull())
186 {
187 myIsTopDown = myPixMap->IsTopDown();
188 return myPixMap;
189 }
190
191 // Case 2: texture source is specified as path
192 TCollection_AsciiString aFilePath;
193 myPath.SystemName (aFilePath);
194 if (aFilePath.IsEmpty())
195 {
196 return Handle(Image_PixMap)();
197 }
198
199 Handle(Image_AlienPixMap) anImage = new Image_AlienPixMap();
200 if (anImage->Load (aFilePath))
201 {
202 myIsTopDown = anImage->IsTopDown();
203 convertToCompatible (theSupported, anImage);
204 return anImage;
205 }
206
207 return Handle(Image_PixMap)();
208 }
209
210 // =======================================================================
211 // function : convertToCompatible
212 // purpose :
213 // =======================================================================
convertToCompatible(const Handle (Image_SupportedFormats)& theSupported,const Handle (Image_PixMap)& theImage)214 void Graphic3d_TextureRoot::convertToCompatible (const Handle(Image_SupportedFormats)& theSupported,
215 const Handle(Image_PixMap)& theImage)
216 {
217 if (theSupported.IsNull()
218 || theSupported->IsSupported (theImage->Format())
219 || theImage.IsNull())
220 {
221 return;
222 }
223
224 switch (theImage->Format())
225 {
226 // BGR formats are unsupported in OpenGL ES, only RGB
227 case Image_Format_BGR:
228 Image_PixMap::SwapRgbaBgra (*theImage);
229 theImage->SetFormat (Image_Format_RGB);
230 break;
231 case Image_Format_BGRA:
232 case Image_Format_BGR32:
233 Image_PixMap::SwapRgbaBgra (*theImage);
234 theImage->SetFormat (theImage->Format() == Image_Format_BGR32 ? Image_Format_RGB32 : Image_Format_RGBA);
235 break;
236 default:
237 break;
238 }
239 }
240
241 // =======================================================================
242 // function : IsDone
243 // purpose :
244 // =======================================================================
IsDone() const245 Standard_Boolean Graphic3d_TextureRoot::IsDone() const
246 {
247 // Case 1: texture source is specified as pixmap
248 if (!myPixMap.IsNull())
249 {
250 return !myPixMap->IsEmpty();
251 }
252
253 // Case 2: texture source is specified as path
254 OSD_File aTextureFile (myPath);
255 return aTextureFile.Exists();
256 }
257