1 // Copyright (c) 2017 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #ifndef _Graphic3d_LightSet_HeaderFile
15 #define _Graphic3d_LightSet_HeaderFile
17 #include <NCollection_IndexedDataMap.hxx>
18 #include <Graphic3d_CLight.hxx>
20 //! Class defining the set of light sources.
21 class Graphic3d_LightSet : public Standard_Transient
23 DEFINE_STANDARD_RTTIEXT(Graphic3d_LightSet, Standard_Transient)
26 //! Iteration filter flags.
29 IterationFilter_None = 0x0000, //!< no filter
30 IterationFilter_ExcludeAmbient = 0x0002, //!< exclude ambient light sources
31 IterationFilter_ExcludeDisabled = 0x0004, //!< exclude disabled light sources
32 IterationFilter_ExcludeDisabledAndAmbient = IterationFilter_ExcludeAmbient | IterationFilter_ExcludeDisabled,
35 //! Iterator through light sources.
39 //! Empty constructor.
40 Iterator() : myFilter (0) {}
42 //! Constructor with initialization.
43 Iterator (const Graphic3d_LightSet& theSet,
44 IterationFilter theFilter = IterationFilter_None)
45 : myIter (theSet.myLights),
51 //! Constructor with initialization.
52 Iterator (const Handle(Graphic3d_LightSet)& theSet,
53 IterationFilter theFilter = IterationFilter_None)
54 : myFilter (theFilter)
58 myIter = NCollection_IndexedDataMap<Handle(Graphic3d_CLight), Standard_Size>::Iterator (theSet->myLights);
63 //! Returns TRUE if iterator points to a valid item.
64 Standard_Boolean More() const { return myIter.More(); }
66 //! Returns current item.
67 const Handle(Graphic3d_CLight)& Value() const { return myIter.Key(); }
69 //! Moves to the next item.
78 //! Skip filtered items.
86 for (; myIter.More(); myIter.Next())
88 if ((myFilter & IterationFilter_ExcludeAmbient) != 0
89 && myIter.Key()->Type() == Graphic3d_TOLS_AMBIENT)
93 else if ((myFilter & IterationFilter_ExcludeDisabled) != 0
94 && !myIter.Key()->IsEnabled())
104 NCollection_IndexedDataMap<Handle(Graphic3d_CLight), Standard_Size>::Iterator myIter;
105 Standard_Integer myFilter;
110 //! Empty constructor.
111 Standard_EXPORT Graphic3d_LightSet();
113 //! Return lower light index.
114 Standard_Integer Lower() const { return 1; }
116 //! Return upper light index.
117 Standard_Integer Upper() const { return myLights.Extent(); }
119 //! Return TRUE if lights list is empty.
120 Standard_Boolean IsEmpty() const { return myLights.IsEmpty(); }
122 //! Return number of light sources.
123 Standard_Integer Extent() const { return myLights.Extent(); }
125 //! Return the light source for specified index within range [Lower(), Upper()].
126 const Handle(Graphic3d_CLight)& Value (Standard_Integer theIndex) const { return myLights.FindKey (theIndex); }
128 //! Return TRUE if light source is defined in this set.
129 Standard_Boolean Contains (const Handle(Graphic3d_CLight)& theLight) const { return myLights.Contains (theLight); }
131 //! Append new light source.
132 Standard_EXPORT Standard_Boolean Add (const Handle(Graphic3d_CLight)& theLight);
134 //! Remove light source.
135 Standard_EXPORT Standard_Boolean Remove (const Handle(Graphic3d_CLight)& theLight);
137 //! Returns total amount of lights of specified type.
138 Standard_Integer NbLightsOfType (Graphic3d_TypeOfLightSource theType) const { return myLightTypes[theType]; }
140 //! @name cached state of lights set updated by UpdateRevision()
143 //! Update light sources revision.
144 Standard_EXPORT Standard_Size UpdateRevision();
146 //! Return light sources revision.
147 //! @sa UpdateRevision()
148 Standard_Size Revision() const { return myRevision; }
150 //! Returns total amount of enabled lights EXCLUDING ambient.
151 //! @sa UpdateRevision()
152 Standard_Integer NbEnabled() const { return myNbEnabled; }
154 //! Returns total amount of enabled lights of specified type.
155 //! @sa UpdateRevision()
156 Standard_Integer NbEnabledLightsOfType (Graphic3d_TypeOfLightSource theType) const { return myLightTypesEnabled[theType]; }
158 //! Returns cumulative ambient color, which is computed as sum of all enabled ambient light sources.
159 //! Values are NOT clamped (can be greater than 1.0f) and alpha component is fixed to 1.0f.
160 //! @sa UpdateRevision()
161 const Graphic3d_Vec4& AmbientColor() const { return myAmbient; }
163 //! Returns a string defining a list of enabled light sources as concatenation of letters 'd' (Directional), 'p' (Point), 's' (Spot)
164 //! depending on the type of light source in the list.
166 //! @sa UpdateRevision()
167 const TCollection_AsciiString& KeyEnabledLong() const { return myKeyEnabledLong; }
169 //! Returns a string defining a list of enabled light sources as concatenation of letters 'd' (Directional), 'p' (Point), 's' (Spot)
170 //! depending on the type of light source in the list, specified only once.
172 //! @sa UpdateRevision()
173 const TCollection_AsciiString& KeyEnabledShort() const { return myKeyEnabledShort; }
176 NCollection_IndexedDataMap<Handle(Graphic3d_CLight), Standard_Size>
177 myLights; //!< list of light sources with their cached state (revision)
178 Graphic3d_Vec4 myAmbient; //!< cached value of cumulative ambient color
179 TCollection_AsciiString myKeyEnabledLong; //!< key identifying the list of enabled light sources by their type
180 TCollection_AsciiString myKeyEnabledShort; //!< key identifying the list of enabled light sources by the number of sources of each type
181 Standard_Integer myLightTypes [Graphic3d_TypeOfLightSource_NB]; //!< counters per each light source type defined in the list
182 Standard_Integer myLightTypesEnabled[Graphic3d_TypeOfLightSource_NB]; //!< counters per each light source type enabled in the list
183 Standard_Integer myNbEnabled; //!< number of enabled light sources, excluding ambient
184 Standard_Size myRevision; //!< current revision of light source set
185 Standard_Size myCacheRevision; //!< revision of cached state
188 DEFINE_STANDARD_HANDLE(Graphic3d_LightSet, Standard_Transient)
190 #endif // _Graphic3d_LightSet_HeaderFile