992ed6b3 |
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_ExcludeDisabledAndAmbient = IterationFilter_ExcludeAmbient | IterationFilter_ExcludeDisabled, |
33 | }; |
34 | |
35 | //! Iterator through light sources. |
36 | class Iterator |
37 | { |
38 | public: |
39 | //! Empty constructor. |
40 | Iterator() : myFilter (0) {} |
41 | |
42 | //! Constructor with initialization. |
43 | Iterator (const Graphic3d_LightSet& theSet, |
44 | IterationFilter theFilter = IterationFilter_None) |
45 | : myIter (theSet.myLights), |
46 | myFilter (theFilter) |
47 | { |
48 | skipFiltered(); |
49 | } |
50 | |
51 | //! Constructor with initialization. |
52 | Iterator (const Handle(Graphic3d_LightSet)& theSet, |
53 | IterationFilter theFilter = IterationFilter_None) |
54 | : myFilter (theFilter) |
55 | { |
56 | if (!theSet.IsNull()) |
57 | { |
58 | myIter = NCollection_IndexedDataMap<Handle(Graphic3d_CLight), Standard_Size>::Iterator (theSet->myLights); |
59 | skipFiltered(); |
60 | } |
61 | } |
62 | |
63 | //! Returns TRUE if iterator points to a valid item. |
64 | Standard_Boolean More() const { return myIter.More(); } |
65 | |
66 | //! Returns current item. |
67 | const Handle(Graphic3d_CLight)& Value() const { return myIter.Key(); } |
68 | |
69 | //! Moves to the next item. |
70 | void Next() |
71 | { |
72 | myIter.Next(); |
73 | skipFiltered(); |
74 | } |
75 | |
76 | protected: |
77 | |
78 | //! Skip filtered items. |
79 | void skipFiltered() |
80 | { |
81 | if (myFilter == 0) |
82 | { |
83 | return; |
84 | } |
85 | |
86 | for (; myIter.More(); myIter.Next()) |
87 | { |
88 | if ((myFilter & IterationFilter_ExcludeAmbient) != 0 |
89 | && myIter.Key()->Type() == Graphic3d_TOLS_AMBIENT) |
90 | { |
91 | continue; |
92 | } |
93 | else if ((myFilter & IterationFilter_ExcludeDisabled) != 0 |
94 | && !myIter.Key()->IsEnabled()) |
95 | { |
96 | continue; |
97 | } |
98 | |
99 | break; |
100 | } |
101 | } |
102 | |
103 | protected: |
104 | NCollection_IndexedDataMap<Handle(Graphic3d_CLight), Standard_Size>::Iterator myIter; |
105 | Standard_Integer myFilter; |
106 | }; |
107 | |
108 | public: |
109 | |
110 | //! Empty constructor. |
111 | Standard_EXPORT Graphic3d_LightSet(); |
112 | |
113 | //! Return lower light index. |
114 | Standard_Integer Lower() const { return 1; } |
115 | |
116 | //! Return upper light index. |
117 | Standard_Integer Upper() const { return myLights.Extent(); } |
118 | |
119 | //! Return TRUE if lights list is empty. |
120 | Standard_Boolean IsEmpty() const { return myLights.IsEmpty(); } |
121 | |
122 | //! Return number of light sources. |
123 | Standard_Integer Extent() const { return myLights.Extent(); } |
124 | |
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); } |
127 | |
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); } |
130 | |
131 | //! Append new light source. |
132 | Standard_EXPORT Standard_Boolean Add (const Handle(Graphic3d_CLight)& theLight); |
133 | |
134 | //! Remove light source. |
135 | Standard_EXPORT Standard_Boolean Remove (const Handle(Graphic3d_CLight)& theLight); |
136 | |
137 | //! Returns total amount of lights of specified type. |
138 | Standard_Integer NbLightsOfType (Graphic3d_TypeOfLightSource theType) const { return myLightTypes[theType]; } |
139 | |
140 | //! @name cached state of lights set updated by UpdateRevision() |
141 | public: |
142 | |
143 | //! Update light sources revision. |
144 | Standard_EXPORT Standard_Size UpdateRevision(); |
145 | |
146 | //! Return light sources revision. |
147 | //! @sa UpdateRevision() |
148 | Standard_Size Revision() const { return myRevision; } |
149 | |
150 | //! Returns total amount of enabled lights EXCLUDING ambient. |
151 | //! @sa UpdateRevision() |
152 | Standard_Integer NbEnabled() const { return myNbEnabled; } |
153 | |
154 | //! Returns total amount of enabled lights of specified type. |
155 | //! @sa UpdateRevision() |
156 | Standard_Integer NbEnabledLightsOfType (Graphic3d_TypeOfLightSource theType) const { return myLightTypesEnabled[theType]; } |
157 | |
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; } |
162 | |
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. |
165 | //! Example: "dppp". |
166 | //! @sa UpdateRevision() |
167 | const TCollection_AsciiString& KeyEnabledLong() const { return myKeyEnabledLong; } |
168 | |
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. |
171 | //! Example: "dp". |
172 | //! @sa UpdateRevision() |
173 | const TCollection_AsciiString& KeyEnabledShort() const { return myKeyEnabledShort; } |
174 | |
175 | protected: |
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 |
186 | }; |
187 | |
188 | DEFINE_STANDARD_HANDLE(Graphic3d_LightSet, Standard_Transient) |
189 | |
190 | #endif // _Graphic3d_LightSet_HeaderFile |