5ea41b2f4674e71a93bb162120a7e4ca52847de6
[occt.git] / src / Graphic3d / Graphic3d_TextureRoot.cxx
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 <OSD_Directory.hxx>
23 #include <OSD_Environment.hxx>
24 #include <OSD_File.hxx>
25 #include <OSD_Protection.hxx>
26 #include <Standard_Atomic.hxx>
27
28 IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_TextureRoot, Standard_Transient)
29
30 namespace
31 {
32   static volatile Standard_Integer THE_TEXTURE_COUNTER = 0;
33 }
34
35 // =======================================================================
36 // function : TexturesFolder
37 // purpose  :
38 // =======================================================================
39 TCollection_AsciiString Graphic3d_TextureRoot::TexturesFolder()
40 {
41   static Standard_Boolean IsDefined = Standard_False;
42   static TCollection_AsciiString VarName;
43   if (!IsDefined)
44   {
45     IsDefined = Standard_True;
46     OSD_Environment aTexDirEnv ("CSF_MDTVTexturesDirectory");
47     VarName = aTexDirEnv.Value();
48     if (VarName.IsEmpty())
49     {
50       OSD_Environment aCasRootEnv ("CASROOT");
51       VarName = aCasRootEnv.Value();
52       if (!VarName.IsEmpty())
53       {
54         VarName += "/src/Textures";
55       }
56     }
57
58     if (VarName.IsEmpty())
59     {
60 #ifdef OCCT_DEBUG
61       std::cerr << "Both environment variables CSF_MDTVTexturesDirectory and CASROOT are undefined!\n"
62                 << "At least one should be defined to use standard Textures.\n";
63 #endif
64       throw Standard_Failure("CSF_MDTVTexturesDirectory and CASROOT are undefined");
65     }
66
67     const OSD_Path aDirPath (VarName);
68     OSD_Directory aDir (aDirPath);
69     const TCollection_AsciiString aTexture = VarName + "/2d_MatraDatavision.rgb";
70     OSD_File aTextureFile (aTexture);
71     if (!aDir.Exists() || !aTextureFile.Exists())
72     {
73 #ifdef OCCT_DEBUG
74       std::cerr << " CSF_MDTVTexturesDirectory or CASROOT not correctly setted\n";
75       std::cerr << " not all files are found in : "<< VarName.ToCString() << std::endl;
76 #endif
77       throw Standard_Failure("CSF_MDTVTexturesDirectory or CASROOT not correctly setted");
78     }
79   }
80   return VarName;
81 }
82
83 // =======================================================================
84 // function : Graphic3d_TextureRoot
85 // purpose  :
86 // =======================================================================
87 Graphic3d_TextureRoot::Graphic3d_TextureRoot (const TCollection_AsciiString& theFileName,
88                                               const Graphic3d_TypeOfTexture  theType)
89 : myParams   (new Graphic3d_TextureParams()),
90   myPath     (theFileName),
91   myRevision (0),
92   myType     (theType)
93 {
94   generateId();
95 }
96
97 // =======================================================================
98 // function : Graphic3d_TextureRoot
99 // purpose  :
100 // =======================================================================
101 Graphic3d_TextureRoot::Graphic3d_TextureRoot (const Handle(Image_PixMap)&   thePixMap,
102                                               const Graphic3d_TypeOfTexture theType)
103 : myParams   (new Graphic3d_TextureParams()),
104   myPixMap   (thePixMap),
105   myRevision (0),
106   myType     (theType)
107 {
108   generateId();
109 }
110
111 // =======================================================================
112 // function : ~Graphic3d_TextureRoot
113 // purpose  :
114 // =======================================================================
115 Graphic3d_TextureRoot::~Graphic3d_TextureRoot()
116 {
117   //
118 }
119
120 // =======================================================================
121 // function : generateId
122 // purpose  :
123 // =======================================================================
124 void Graphic3d_TextureRoot::generateId()
125 {
126   myTexId = TCollection_AsciiString ("Graphic3d_TextureRoot_")
127           + TCollection_AsciiString (Standard_Atomic_Increment (&THE_TEXTURE_COUNTER));
128 }
129
130 // =======================================================================
131 // function : GetImage
132 // purpose  :
133 // =======================================================================
134 Handle(Image_PixMap) Graphic3d_TextureRoot::GetImage() const
135 {
136   // Case 1: texture source is specified as pixmap
137   if (!myPixMap.IsNull())
138   {
139     return myPixMap;
140   }
141
142   // Case 2: texture source is specified as path
143   TCollection_AsciiString aFilePath;
144   myPath.SystemName (aFilePath);
145   if (aFilePath.IsEmpty())
146   {
147     return Handle(Image_PixMap)();
148   }
149
150   Handle(Image_AlienPixMap) anImage = new Image_AlienPixMap();
151   if (!anImage->Load (aFilePath))
152   {
153     return Handle(Image_PixMap)();
154   }
155
156   return anImage;
157 }
158
159 // =======================================================================
160 // function : IsDone
161 // purpose  :
162 // =======================================================================
163 Standard_Boolean Graphic3d_TextureRoot::IsDone() const
164 {
165   // Case 1: texture source is specified as pixmap
166   if (!myPixMap.IsNull())
167   {
168     return !myPixMap->IsEmpty();
169   }
170
171   // Case 2: texture source is specified as path
172   OSD_File aTextureFile (myPath);
173   return aTextureFile.Exists();
174 }