0032121: Draw Harness, ViewerTest - implement -reset option for vlight command
[occt.git] / src / Graphic3d / Graphic3d_LightSet.hxx
1 // Copyright (c) 2017 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #ifndef _Graphic3d_LightSet_HeaderFile
15 #define _Graphic3d_LightSet_HeaderFile
16
17 #include <NCollection_IndexedDataMap.hxx>
18 #include <Graphic3d_CLight.hxx>
19
20 //! Class defining the set of light sources.
21 class Graphic3d_LightSet : public Standard_Transient
22 {
23   DEFINE_STANDARD_RTTIEXT(Graphic3d_LightSet, Standard_Transient)
24 public:
25
26   //! Iteration filter flags.
27   enum IterationFilter
28   {
29     IterationFilter_None            = 0x0000, //!< no filter
30     IterationFilter_ExcludeAmbient  = 0x0002, //!< exclude ambient  light sources
31     IterationFilter_ExcludeDisabled = 0x0004, //!< exclude disabled light sources
32     IterationFilter_ExcludeNoShadow = 0x0008, //!< exclude light sources not casting shadow
33     IterationFilter_ExcludeDisabledAndAmbient = IterationFilter_ExcludeAmbient | IterationFilter_ExcludeDisabled,
34     IterationFilter_ActiveShadowCasters = IterationFilter_ExcludeDisabledAndAmbient | IterationFilter_ExcludeNoShadow,
35   };
36
37   //! Iterator through light sources.
38   class Iterator
39   {
40   public:
41     //! Empty constructor.
42     Iterator() : myFilter (0) {}
43
44     //! Constructor with initialization.
45     Iterator (const Graphic3d_LightSet& theSet,
46               IterationFilter theFilter = IterationFilter_None)
47     : myIter (theSet.myLights),
48       myFilter (theFilter)
49     {
50       skipFiltered();
51     }
52
53     //! Constructor with initialization.
54     Iterator (const Handle(Graphic3d_LightSet)& theSet,
55               IterationFilter theFilter = IterationFilter_None)
56     : myFilter (theFilter)
57     {
58       if (!theSet.IsNull())
59       {
60         myIter = NCollection_IndexedDataMap<Handle(Graphic3d_CLight), Standard_Size>::Iterator (theSet->myLights);
61         skipFiltered();
62       }
63     }
64
65     //! Returns TRUE if iterator points to a valid item.
66     Standard_Boolean More() const { return myIter.More(); }
67
68     //! Returns current item.
69     const Handle(Graphic3d_CLight)& Value() const { return myIter.Key(); }
70
71     //! Moves to the next item.
72     void Next()
73     {
74       myIter.Next();
75       skipFiltered();
76     }
77
78   protected:
79
80     //! Skip filtered items.
81     void skipFiltered()
82     {
83       if (myFilter == 0)
84       {
85         return;
86       }
87
88       for (; myIter.More(); myIter.Next())
89       {
90         if ((myFilter & IterationFilter_ExcludeAmbient) != 0
91          && myIter.Key()->Type() == Graphic3d_TypeOfLightSource_Ambient)
92         {
93           continue;
94         }
95         else if ((myFilter & IterationFilter_ExcludeDisabled) != 0
96               && !myIter.Key()->IsEnabled())
97         {
98           continue;
99         }
100         else if ((myFilter & IterationFilter_ExcludeNoShadow) != 0
101               && !myIter.Key()->ToCastShadows())
102         {
103           continue;
104         }
105
106         break;
107       }
108     }
109
110   protected:
111     NCollection_IndexedDataMap<Handle(Graphic3d_CLight), Standard_Size>::Iterator myIter;
112     Standard_Integer myFilter;
113   };
114
115 public:
116
117   //! Empty constructor.
118   Standard_EXPORT Graphic3d_LightSet();
119
120   //! Return lower light index.
121   Standard_Integer Lower() const { return 1; }
122
123   //! Return upper light index.
124   Standard_Integer Upper() const { return myLights.Extent(); }
125
126   //! Return TRUE if lights list is empty.
127   Standard_Boolean IsEmpty() const { return myLights.IsEmpty(); }
128
129   //! Return number of light sources.
130   Standard_Integer Extent() const { return myLights.Extent(); }
131
132   //! Return the light source for specified index within range [Lower(), Upper()].
133   const Handle(Graphic3d_CLight)& Value (Standard_Integer theIndex) const { return myLights.FindKey (theIndex); }
134
135   //! Return TRUE if light source is defined in this set.
136   Standard_Boolean Contains (const Handle(Graphic3d_CLight)& theLight) const { return myLights.Contains (theLight); }
137
138   //! Append new light source.
139   Standard_EXPORT Standard_Boolean Add (const Handle(Graphic3d_CLight)& theLight);
140
141   //! Remove light source.
142   Standard_EXPORT Standard_Boolean Remove (const Handle(Graphic3d_CLight)& theLight);
143
144   //! Returns total amount of lights of specified type.
145   Standard_Integer NbLightsOfType (Graphic3d_TypeOfLightSource theType) const { return myLightTypes[theType]; }
146
147 //! @name cached state of lights set updated by UpdateRevision()
148 public:
149
150   //! Update light sources revision.
151   Standard_EXPORT Standard_Size UpdateRevision();
152
153   //! Return light sources revision.
154   //! @sa UpdateRevision()
155   Standard_Size Revision() const { return myRevision; }
156
157   //! Returns total amount of enabled lights EXCLUDING ambient.
158   //! @sa UpdateRevision()
159   Standard_Integer NbEnabled() const { return myNbEnabled; }
160
161   //! Returns total amount of enabled lights of specified type.
162   //! @sa UpdateRevision()
163   Standard_Integer NbEnabledLightsOfType (Graphic3d_TypeOfLightSource theType) const { return myLightTypesEnabled[theType]; }
164
165   //! Returns total amount of enabled lights castings shadows.
166   //! @sa UpdateRevision()
167   Standard_Integer NbCastShadows() const { return myNbCastShadows; }
168
169   //! Returns cumulative ambient color, which is computed as sum of all enabled ambient light sources.
170   //! Values are NOT clamped (can be greater than 1.0f) and alpha component is fixed to 1.0f.
171   //! @sa UpdateRevision()
172   const Graphic3d_Vec4& AmbientColor() const { return myAmbient; }
173
174   //! Returns a string defining a list of enabled light sources as concatenation of letters 'd' (Directional), 'p' (Point), 's' (Spot)
175   //! depending on the type of light source in the list.
176   //! Example: "dppp".
177   //! @sa UpdateRevision()
178   const TCollection_AsciiString& KeyEnabledLong() const { return myKeyEnabledLong; }
179
180   //! Returns a string defining a list of enabled light sources as concatenation of letters 'd' (Directional), 'p' (Point), 's' (Spot)
181   //! depending on the type of light source in the list, specified only once.
182   //! Example: "dp".
183   //! @sa UpdateRevision()
184   const TCollection_AsciiString& KeyEnabledShort() const { return myKeyEnabledShort; }
185
186 protected:
187   NCollection_IndexedDataMap<Handle(Graphic3d_CLight), Standard_Size>
188                           myLights;                 //!< list of light sources with their cached state (revision)
189   Graphic3d_Vec4          myAmbient;                //!< cached value of cumulative ambient color
190   TCollection_AsciiString myKeyEnabledLong;         //!< key identifying the list of enabled light sources by their type
191   TCollection_AsciiString myKeyEnabledShort;        //!< key identifying the list of enabled light sources by the number of sources of each type
192   Standard_Integer        myLightTypes       [Graphic3d_TypeOfLightSource_NB]; //!< counters per each light source type defined in the list
193   Standard_Integer        myLightTypesEnabled[Graphic3d_TypeOfLightSource_NB]; //!< counters per each light source type enabled in the list
194   Standard_Integer        myNbEnabled;              //!< number of enabled light sources, excluding ambient
195   Standard_Integer        myNbCastShadows;          //!< number of enabled light sources casting shadows
196   Standard_Size           myRevision;               //!< current revision of light source set
197   Standard_Size           myCacheRevision;          //!< revision of cached state
198 };
199
200 DEFINE_STANDARD_HANDLE(Graphic3d_LightSet, Standard_Transient)
201
202 #endif // _Graphic3d_LightSet_HeaderFile