1 // Copyright (c) 1999-2014 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_CLight_HeaderFile
15 #define _Graphic3d_CLight_HeaderFile
18 #include <Graphic3d_TypeOfLightSource.hxx>
19 #include <Graphic3d_Vec.hxx>
20 #include <NCollection_List.hxx>
21 #include <TCollection_AsciiString.hxx>
22 #include <Quantity_ColorRGBA.hxx>
24 //! Generic light source definition.
25 //! This class defines arbitrary light source - see Graphic3d_TypeOfLightSource enumeration.
26 //! Some parameters are applicable only to particular light type;
27 //! calling methods unrelated to current type will throw an exception.
28 class Graphic3d_CLight : public Standard_Transient
30 DEFINE_STANDARD_RTTIEXT(Graphic3d_CLight, Standard_Transient)
33 //! Empty constructor, which should be followed by light source properties configuration.
34 Standard_EXPORT Graphic3d_CLight (Graphic3d_TypeOfLightSource theType);
36 //! Returns the Type of the Light, cannot be changed after object construction.
37 Graphic3d_TypeOfLightSource Type() const { return myType; }
39 //! Returns light source name; empty string by default.
40 const TCollection_AsciiString& Name() const { return myName; }
42 //! Sets light source name.
43 void SetName (const TCollection_AsciiString& theName) { myName = theName; }
45 //! Returns the color of the light source; WHITE by default.
46 const Quantity_Color& Color() const { return myColor.GetRGB(); }
48 //! Defines the color of a light source by giving the basic color.
49 Standard_EXPORT void SetColor (const Quantity_Color& theColor);
51 //! Check that the light source is turned on; TRUE by default.
52 //! This flag affects all occurrences of light sources, where it was registered and activated;
53 //! so that it is possible defining an active light in View which is actually in disabled state.
54 Standard_Boolean IsEnabled() const { return myIsEnabled; }
56 //! Change enabled state of the light state.
57 //! This call does not remove or deactivate light source in Views/Viewers;
58 //! instead it turns it OFF so that it just have no effect.
59 Standard_EXPORT void SetEnabled (Standard_Boolean theIsOn);
61 //! Return TRUE if shadow casting is enabled; FALSE by default.
62 //! Has no effect in Ray-Tracing rendering mode.
63 Standard_Boolean ToCastShadows() const { return myToCastShadows; }
65 //! Enable/disable shadow casting.
66 Standard_EXPORT void SetCastShadows (Standard_Boolean theToCast);
68 //! Returns true if the light is a headlight; FALSE by default.
69 //! Headlight flag means that light position/direction are defined not in a World coordinate system, but relative to the camera orientation.
70 Standard_Boolean IsHeadlight() const { return myIsHeadlight; }
72 //! Alias for IsHeadlight().
73 Standard_Boolean Headlight() const { return myIsHeadlight; }
75 //! Setup headlight flag.
76 Standard_EXPORT void SetHeadlight (Standard_Boolean theValue);
78 //! @name positional/spot light properties
81 //! Returns location of positional/spot light; (0, 0, 0) by default.
82 const gp_Pnt& Position() const { return myPosition; }
84 //! Setup location of positional/spot light.
85 Standard_EXPORT void SetPosition (const gp_Pnt& thePosition);
87 //! Returns location of positional/spot light.
88 void Position (Standard_Real& theX,
90 Standard_Real& theZ) const
92 theX = myPosition.X();
93 theY = myPosition.Y();
94 theZ = myPosition.Z();
97 //! Setup location of positional/spot light.
98 void SetPosition (Standard_Real theX, Standard_Real theY, Standard_Real theZ) { SetPosition (gp_Pnt (theX, theY, theZ)); }
100 //! Returns constant attenuation factor of positional/spot light source; 1.0f by default.
101 //! Distance attenuation factors of reducing positional/spot light intensity depending on the distance from its position:
103 //! float anAttenuation = 1.0 / (ConstAttenuation() + LinearAttenuation() * theDistance + QuadraticAttenuation() * theDistance * theDistance);
105 Standard_ShortReal ConstAttenuation() const { return myParams.x(); }
107 //! Returns linear attenuation factor of positional/spot light source; 0.0 by default.
108 //! Distance attenuation factors of reducing positional/spot light intensity depending on the distance from its position:
110 //! float anAttenuation = 1.0 / (ConstAttenuation() + LinearAttenuation() * theDistance + QuadraticAttenuation() * theDistance * theDistance);
112 Standard_ShortReal LinearAttenuation() const { return myParams.y(); }
114 //! Returns the attenuation factors.
115 void Attenuation (Standard_Real& theConstAttenuation,
116 Standard_Real& theLinearAttenuation) const
118 theConstAttenuation = ConstAttenuation();
119 theLinearAttenuation = LinearAttenuation();
122 //! Defines the coefficients of attenuation; values should be >= 0.0 and their summ should not be equal to 0.
123 Standard_EXPORT void SetAttenuation (Standard_ShortReal theConstAttenuation,
124 Standard_ShortReal theLinearAttenuation);
126 //! @name directional/spot light additional properties
129 //! Returns direction of directional/spot light.
130 gp_Dir Direction() const { return gp_Dir (myDirection.x(), myDirection.y(), myDirection.z()); }
132 //! Sets direction of directional/spot light.
133 Standard_EXPORT void SetDirection (const gp_Dir& theDir);
135 //! Returns the theVx, theVy, theVz direction of the light source.
136 void Direction (Standard_Real& theVx,
137 Standard_Real& theVy,
138 Standard_Real& theVz) const
140 theVx = myDirection.x();
141 theVy = myDirection.y();
142 theVz = myDirection.z();
145 //! Sets direction of directional/spot light.
146 void SetDirection (Standard_Real theVx, Standard_Real theVy, Standard_Real theVz) { SetDirection (gp_Dir (theVx, theVy, theVz)); }
148 //! Returns location of positional/spot/directional light, which is the same as returned by Position().
149 const gp_Pnt& DisplayPosition() const { return myPosition; }
151 //! Setup location of positional/spot/directional light,
152 //! which is the same as SetPosition() but allows directional light source
153 //! (technically having no position, but this point can be used for displaying light source presentation).
154 Standard_EXPORT void SetDisplayPosition (const gp_Pnt& thePosition);
156 //! @name spotlight additional definition parameters
159 //! Returns an angle in radians of the cone created by the spot; 30 degrees by default.
160 Standard_ShortReal Angle() const { return myParams.z(); }
162 //! Angle in radians of the cone created by the spot, should be within range (0.0, M_PI).
163 Standard_EXPORT void SetAngle (Standard_ShortReal theAngle);
165 //! Returns intensity distribution of the spot light, within [0.0, 1.0] range; 1.0 by default.
166 //! This coefficient should be converted into spotlight exponent within [0.0, 128.0] range:
168 //! float aSpotExponent = Concentration() * 128.0;
169 //! anAttenuation *= pow (aCosA, aSpotExponent);"
171 //! The concentration factor determines the dispersion of the light on the surface, the default value (1.0) corresponds to a minimum of dispersion.
172 Standard_ShortReal Concentration() const { return myParams.w(); }
174 //! Defines the coefficient of concentration; value should be within range [0.0, 1.0].
175 Standard_EXPORT void SetConcentration (Standard_ShortReal theConcentration);
177 //! @name Ray-Tracing / Path-Tracing light properties
180 //! Returns the intensity of light source; 1.0 by default.
181 Standard_ShortReal Intensity() const { return myIntensity; }
183 //! Modifies the intensity of light source, which should be > 0.0.
184 Standard_EXPORT void SetIntensity (Standard_ShortReal theValue);
186 //! Returns the smoothness of light source (either smoothing angle for directional light or smoothing radius in case of positional light); 0.0 by default.
187 Standard_ShortReal Smoothness() const { return mySmoothness; }
189 //! Modifies the smoothing radius of positional/spot light; should be >= 0.0.
190 Standard_EXPORT void SetSmoothRadius (Standard_ShortReal theValue);
192 //! Modifies the smoothing angle (in radians) of directional light source; should be within range [0.0, M_PI/2].
193 Standard_EXPORT void SetSmoothAngle (Standard_ShortReal theValue);
195 //! Returns TRUE if maximum distance of point light source is defined.
196 bool HasRange() const { return myDirection.w() != 0.0f; }
198 //! Returns maximum distance on which point light source affects to objects and is considered during illumination calculations.
199 //! 0.0 means disabling range considering at all without any distance limits.
200 //! Has sense only for point light sources (positional and spot).
201 Standard_ShortReal Range() const { return myDirection.w(); }
203 //! Modifies maximum distance on which point light source affects to objects and is considered during illumination calculations.
204 //! Positional and spot lights are only point light sources.
205 //! 0.0 means disabling range considering at all without any distance limits.
206 Standard_EXPORT void SetRange (Standard_ShortReal theValue);
208 //! @name low-level access methods
211 //! @return light resource identifier string
212 const TCollection_AsciiString& GetId() const { return myId; }
214 //! Packed light parameters.
215 const Graphic3d_Vec4& PackedParams() const { return myParams; }
217 //! Returns the color of the light source with dummy Alpha component, which should be ignored.
218 const Graphic3d_Vec4& PackedColor() const { return myColor; }
220 //! Returns direction of directional/spot light and range for positional/spot light in alpha channel.
221 const Graphic3d_Vec4& PackedDirectionRange() const { return myDirection; }
223 //! Returns direction of directional/spot light.
224 Graphic3d_Vec3 PackedDirection() const { return myDirection.xyz(); }
226 //! @return modification counter
227 Standard_Size Revision() const { return myRevision; }
229 //! Dumps the content of me into the stream
230 Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
234 //! Access positional/spot light constant attenuation coefficient from packed vector.
235 Standard_ShortReal& changeConstAttenuation() { return myParams.x(); }
237 //! Access positional/spot light linear attenuation coefficient from packed vector.
238 Standard_ShortReal& changeLinearAttenuation() { return myParams.y(); }
240 //! Access spotlight angle parameter from packed vector.
241 Standard_ShortReal& changeAngle() { return myParams.z(); }
243 //! Access spotlight concentration parameter from packed vector.
244 Standard_ShortReal& changeConcentration() { return myParams.w(); }
248 //! Generate unique object id.
251 //! Update modification counter.
252 void updateRevisionIf (bool theIsModified)
262 Graphic3d_CLight (const Graphic3d_CLight& );
263 Graphic3d_CLight& operator= (const Graphic3d_CLight& );
267 TCollection_AsciiString myId; //!< resource id
268 TCollection_AsciiString myName; //!< user given name
269 gp_Pnt myPosition; //!< light position
270 Quantity_ColorRGBA myColor; //!< light color
271 Graphic3d_Vec4 myDirection; //!< direction of directional/spot light
272 Graphic3d_Vec4 myParams; //!< packed light parameters
273 Standard_ShortReal mySmoothness; //!< radius for point light or cone angle for directional light
274 Standard_ShortReal myIntensity; //!< intensity multiplier for light
275 const Graphic3d_TypeOfLightSource myType; //!< Graphic3d_TypeOfLightSource enumeration
276 Standard_Size myRevision; //!< modification counter
277 Standard_Boolean myIsHeadlight; //!< flag to mark head light
278 Standard_Boolean myIsEnabled; //!< enabled state
279 Standard_Boolean myToCastShadows;//!< casting shadows is requested
283 DEFINE_STANDARD_HANDLE(Graphic3d_CLight, Standard_Transient)
285 #endif // Graphic3d_CLight_HeaderFile