0027816: Visualization - provide an API for overriding clipping planes list
[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       Standard_Failure::Raise ("CSF_MDTVTexturesDirectory and CASROOT are undefined");
65       return VarName;
66     }
67
68     const OSD_Path aDirPath (VarName);
69     OSD_Directory aDir (aDirPath);
70     const TCollection_AsciiString aTexture = VarName + "/2d_MatraDatavision.rgb";
71     OSD_File aTextureFile (aTexture);
72     if (!aDir.Exists() || !aTextureFile.Exists())
73     {
74 #ifdef OCCT_DEBUG
75       std::cerr << " CSF_MDTVTexturesDirectory or CASROOT not correctly setted\n";
76       std::cerr << " not all files are found in : "<< VarName.ToCString() << std::endl;
77 #endif
78       Standard_Failure::Raise ("CSF_MDTVTexturesDirectory or CASROOT not correctly setted");
79       return VarName;
80     }
81   }
82   return VarName;
83 }
84
85 // =======================================================================
86 // function : Graphic3d_TextureRoot
87 // purpose  :
88 // =======================================================================
89 Graphic3d_TextureRoot::Graphic3d_TextureRoot (const TCollection_AsciiString& theFileName,
90                                               const Graphic3d_TypeOfTexture  theType)
91 : myParams   (new Graphic3d_TextureParams()),
92   myPath     (theFileName),
93   myRevision (0),
94   myType     (theType)
95 {
96   generateId();
97 }
98
99 // =======================================================================
100 // function : Graphic3d_TextureRoot
101 // purpose  :
102 // =======================================================================
103 Graphic3d_TextureRoot::Graphic3d_TextureRoot (const Handle(Image_PixMap)&   thePixMap,
104                                               const Graphic3d_TypeOfTexture theType)
105 : myParams   (new Graphic3d_TextureParams()),
106   myPixMap   (thePixMap),
107   myRevision (0),
108   myType     (theType)
109 {
110   generateId();
111 }
112
113 // =======================================================================
114 // function : ~Graphic3d_TextureRoot
115 // purpose  :
116 // =======================================================================
117 Graphic3d_TextureRoot::~Graphic3d_TextureRoot()
118 {
119   //
120 }
121
122 // =======================================================================
123 // function : generateId
124 // purpose  :
125 // =======================================================================
126 void Graphic3d_TextureRoot::generateId()
127 {
128   myTexId = TCollection_AsciiString ("Graphic3d_TextureRoot_")
129           + TCollection_AsciiString (Standard_Atomic_Increment (&THE_TEXTURE_COUNTER));
130 }
131
132 // =======================================================================
133 // function : GetImage
134 // purpose  :
135 // =======================================================================
136 Handle(Image_PixMap) Graphic3d_TextureRoot::GetImage() const
137 {
138   // Case 1: texture source is specified as pixmap
139   if (!myPixMap.IsNull())
140   {
141     return myPixMap;
142   }
143
144   // Case 2: texture source is specified as path
145   TCollection_AsciiString aFilePath;
146   myPath.SystemName (aFilePath);
147   if (aFilePath.IsEmpty())
148   {
149     return Handle(Image_PixMap)();
150   }
151
152   Handle(Image_AlienPixMap) anImage = new Image_AlienPixMap();
153   if (!anImage->Load (aFilePath))
154   {
155     return Handle(Image_PixMap)();
156   }
157
158   return anImage;
159 }
160
161 // =======================================================================
162 // function : IsDone
163 // purpose  :
164 // =======================================================================
165 Standard_Boolean Graphic3d_TextureRoot::IsDone() const
166 {
167   // Case 1: texture source is specified as pixmap
168   if (!myPixMap.IsNull())
169   {
170     return !myPixMap->IsEmpty();
171   }
172
173   // Case 2: texture source is specified as path
174   OSD_File aTextureFile (myPath);
175   return aTextureFile.Exists();
176 }