Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-18 08:29:45

0001 // Copyright (c) 1999-2014 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _Graphic3d_CLight_HeaderFile
0015 #define _Graphic3d_CLight_HeaderFile
0016 
0017 #include <gp_Dir.hxx>
0018 #include <Graphic3d_TypeOfLightSource.hxx>
0019 #include <Graphic3d_Vec.hxx>
0020 #include <NCollection_List.hxx>
0021 #include <TCollection_AsciiString.hxx>
0022 #include <Quantity_ColorRGBA.hxx>
0023 
0024 //! Generic light source definition.
0025 //! This class defines arbitrary light source - see Graphic3d_TypeOfLightSource enumeration.
0026 //! Some parameters are applicable only to particular light type;
0027 //! calling methods unrelated to current type will throw an exception.
0028 class Graphic3d_CLight : public Standard_Transient
0029 {
0030   DEFINE_STANDARD_RTTIEXT(Graphic3d_CLight, Standard_Transient)
0031 public:
0032   //! Empty constructor, which should be followed by light source properties configuration.
0033   Standard_EXPORT Graphic3d_CLight(Graphic3d_TypeOfLightSource theType);
0034 
0035   //! Copy parameters from another light source excluding source type.
0036   Standard_EXPORT void CopyFrom(const Handle(Graphic3d_CLight)& theLight);
0037 
0038   //! Returns the Type of the Light, cannot be changed after object construction.
0039   Graphic3d_TypeOfLightSource Type() const { return myType; }
0040 
0041   //! Returns light source name; empty string by default.
0042   const TCollection_AsciiString& Name() const { return myName; }
0043 
0044   //! Sets light source name.
0045   void SetName(const TCollection_AsciiString& theName) { myName = theName; }
0046 
0047   //! Returns the color of the light source; WHITE by default.
0048   const Quantity_Color& Color() const { return myColor.GetRGB(); }
0049 
0050   //! Defines the color of a light source by giving the basic color.
0051   Standard_EXPORT void SetColor(const Quantity_Color& theColor);
0052 
0053   //! Check that the light source is turned on; TRUE by default.
0054   //! This flag affects all occurrences of light sources, where it was registered and activated;
0055   //! so that it is possible defining an active light in View which is actually in disabled state.
0056   Standard_Boolean IsEnabled() const { return myIsEnabled; }
0057 
0058   //! Change enabled state of the light state.
0059   //! This call does not remove or deactivate light source in Views/Viewers;
0060   //! instead it turns it OFF so that it just have no effect.
0061   Standard_EXPORT void SetEnabled(Standard_Boolean theIsOn);
0062 
0063   //! Return TRUE if shadow casting is enabled; FALSE by default.
0064   //! Has no effect in Ray-Tracing rendering mode.
0065   Standard_Boolean ToCastShadows() const { return myToCastShadows; }
0066 
0067   //! Enable/disable shadow casting.
0068   Standard_EXPORT void SetCastShadows(Standard_Boolean theToCast);
0069 
0070   //! Returns true if the light is a headlight; FALSE by default.
0071   //! Headlight flag means that light position/direction are defined not in a World coordinate
0072   //! system, but relative to the camera orientation.
0073   Standard_Boolean IsHeadlight() const { return myIsHeadlight; }
0074 
0075   //! Alias for IsHeadlight().
0076   Standard_Boolean Headlight() const { return myIsHeadlight; }
0077 
0078   //! Setup headlight flag.
0079   Standard_EXPORT void SetHeadlight(Standard_Boolean theValue);
0080 
0081   //! @name positional/spot light properties
0082 public:
0083   //! Returns location of positional/spot light; (0, 0, 0) by default.
0084   const gp_Pnt& Position() const { return myPosition; }
0085 
0086   //! Setup location of positional/spot light.
0087   Standard_EXPORT void SetPosition(const gp_Pnt& thePosition);
0088 
0089   //! Returns location of positional/spot light.
0090   void Position(Standard_Real& theX, Standard_Real& theY, Standard_Real& theZ) const
0091   {
0092     theX = myPosition.X();
0093     theY = myPosition.Y();
0094     theZ = myPosition.Z();
0095   }
0096 
0097   //! Setup location of positional/spot light.
0098   void SetPosition(Standard_Real theX, Standard_Real theY, Standard_Real theZ)
0099   {
0100     SetPosition(gp_Pnt(theX, theY, theZ));
0101   }
0102 
0103   //! Returns constant attenuation factor of positional/spot light source; 1.0f by default.
0104   //! Distance attenuation factors of reducing positional/spot light intensity depending on the
0105   //! distance from its position:
0106   //! @code
0107   //!   float anAttenuation = 1.0 / (ConstAttenuation() + LinearAttenuation() * theDistance +
0108   //!   QuadraticAttenuation() * theDistance * theDistance);
0109   //! @endcode
0110   Standard_ShortReal ConstAttenuation() const { return myParams.x(); }
0111 
0112   //! Returns linear attenuation factor of positional/spot light source; 0.0 by default.
0113   //! Distance attenuation factors of reducing positional/spot light intensity depending on the
0114   //! distance from its position:
0115   //! @code
0116   //!   float anAttenuation = 1.0 / (ConstAttenuation() + LinearAttenuation() * theDistance +
0117   //!   QuadraticAttenuation() * theDistance * theDistance);
0118   //! @endcode
0119   Standard_ShortReal LinearAttenuation() const { return myParams.y(); }
0120 
0121   //! Returns the attenuation factors.
0122   void Attenuation(Standard_Real& theConstAttenuation, Standard_Real& theLinearAttenuation) const
0123   {
0124     theConstAttenuation  = ConstAttenuation();
0125     theLinearAttenuation = LinearAttenuation();
0126   }
0127 
0128   //! Defines the coefficients of attenuation; values should be >= 0.0 and their summ should not be
0129   //! equal to 0.
0130   Standard_EXPORT void SetAttenuation(Standard_ShortReal theConstAttenuation,
0131                                       Standard_ShortReal theLinearAttenuation);
0132 
0133   //! @name directional/spot light additional properties
0134 public:
0135   //! Returns direction of directional/spot light.
0136   gp_Dir Direction() const { return gp_Dir(myDirection.x(), myDirection.y(), myDirection.z()); }
0137 
0138   //! Sets direction of directional/spot light.
0139   Standard_EXPORT void SetDirection(const gp_Dir& theDir);
0140 
0141   //! Returns the theVx, theVy, theVz direction of the light source.
0142   void Direction(Standard_Real& theVx, Standard_Real& theVy, Standard_Real& theVz) const
0143   {
0144     theVx = myDirection.x();
0145     theVy = myDirection.y();
0146     theVz = myDirection.z();
0147   }
0148 
0149   //! Sets direction of directional/spot light.
0150   void SetDirection(Standard_Real theVx, Standard_Real theVy, Standard_Real theVz)
0151   {
0152     SetDirection(gp_Dir(theVx, theVy, theVz));
0153   }
0154 
0155   //! Returns location of positional/spot/directional light, which is the same as returned by
0156   //! Position().
0157   const gp_Pnt& DisplayPosition() const { return myPosition; }
0158 
0159   //! Setup location of positional/spot/directional light,
0160   //! which is the same as SetPosition() but allows directional light source
0161   //! (technically having no position, but this point can be used for displaying light source
0162   //! presentation).
0163   Standard_EXPORT void SetDisplayPosition(const gp_Pnt& thePosition);
0164 
0165   //! @name spotlight additional definition parameters
0166 public:
0167   //! Returns an angle in radians of the cone created by the spot; 30 degrees by default.
0168   Standard_ShortReal Angle() const { return myParams.z(); }
0169 
0170   //! Angle in radians of the cone created by the spot, should be within range (0.0, M_PI).
0171   Standard_EXPORT void SetAngle(Standard_ShortReal theAngle);
0172 
0173   //! Returns intensity distribution of the spot light, within [0.0, 1.0] range; 1.0 by default.
0174   //! This coefficient should be converted into spotlight exponent within [0.0, 128.0] range:
0175   //! @code
0176   //!   float aSpotExponent = Concentration() * 128.0;
0177   //!   anAttenuation *= pow (aCosA, aSpotExponent);"
0178   //! @endcode
0179   //! The concentration factor determines the dispersion of the light on the surface, the default
0180   //! value (1.0) corresponds to a minimum of dispersion.
0181   Standard_ShortReal Concentration() const { return myParams.w(); }
0182 
0183   //! Defines the coefficient of concentration; value should be within range [0.0, 1.0].
0184   Standard_EXPORT void SetConcentration(Standard_ShortReal theConcentration);
0185 
0186   //! @name Ray-Tracing / Path-Tracing light properties
0187 public:
0188   //! Returns the intensity of light source; 1.0 by default.
0189   Standard_ShortReal Intensity() const { return myIntensity; }
0190 
0191   //! Modifies the intensity of light source, which should be > 0.0.
0192   Standard_EXPORT void SetIntensity(Standard_ShortReal theValue);
0193 
0194   //! Returns the smoothness of light source (either smoothing angle for directional light or
0195   //! smoothing radius in case of positional light); 0.0 by default.
0196   Standard_ShortReal Smoothness() const { return mySmoothness; }
0197 
0198   //! Modifies the smoothing radius of positional/spot light; should be >= 0.0.
0199   Standard_EXPORT void SetSmoothRadius(Standard_ShortReal theValue);
0200 
0201   //! Modifies the smoothing angle (in radians) of directional light source; should be within range
0202   //! [0.0, M_PI/2].
0203   Standard_EXPORT void SetSmoothAngle(Standard_ShortReal theValue);
0204 
0205   //! Returns TRUE if maximum distance of point light source is defined.
0206   bool HasRange() const { return myDirection.w() != 0.0f; }
0207 
0208   //! Returns maximum distance on which point light source affects to objects and is considered
0209   //! during illumination calculations. 0.0 means disabling range considering at all without any
0210   //! distance limits. Has sense only for point light sources (positional and spot).
0211   Standard_ShortReal Range() const { return myDirection.w(); }
0212 
0213   //! Modifies maximum distance on which point light source affects to objects and is considered
0214   //! during illumination calculations. Positional and spot lights are only point light sources. 0.0
0215   //! means disabling range considering at all without any distance limits.
0216   Standard_EXPORT void SetRange(Standard_ShortReal theValue);
0217 
0218   //! @name low-level access methods
0219 public:
0220   //! @return light resource identifier string
0221   const TCollection_AsciiString& GetId() const { return myId; }
0222 
0223   //! Packed light parameters.
0224   const Graphic3d_Vec4& PackedParams() const { return myParams; }
0225 
0226   //! Returns the color of the light source with dummy Alpha component, which should be ignored.
0227   const Graphic3d_Vec4& PackedColor() const { return myColor; }
0228 
0229   //! Returns direction of directional/spot light and range for positional/spot light in alpha
0230   //! channel.
0231   const Graphic3d_Vec4& PackedDirectionRange() const { return myDirection; }
0232 
0233   //! Returns direction of directional/spot light.
0234   Graphic3d_Vec3 PackedDirection() const { return myDirection.xyz(); }
0235 
0236   //! @return modification counter
0237   Standard_Size Revision() const { return myRevision; }
0238 
0239   //! Dumps the content of me into the stream
0240   Standard_EXPORT void DumpJson(Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
0241 
0242 private:
0243   //! Access positional/spot light constant attenuation coefficient from packed vector.
0244   Standard_ShortReal& changeConstAttenuation() { return myParams.x(); }
0245 
0246   //! Access positional/spot light linear attenuation coefficient from packed vector.
0247   Standard_ShortReal& changeLinearAttenuation() { return myParams.y(); }
0248 
0249   //! Access spotlight angle parameter from packed vector.
0250   Standard_ShortReal& changeAngle() { return myParams.z(); }
0251 
0252   //! Access spotlight concentration parameter from packed vector.
0253   Standard_ShortReal& changeConcentration() { return myParams.w(); }
0254 
0255 private:
0256   //! Generate unique object id.
0257   void makeId();
0258 
0259   //! Update modification counter.
0260   void updateRevisionIf(bool theIsModified)
0261   {
0262     if (theIsModified)
0263     {
0264       ++myRevision;
0265     }
0266   }
0267 
0268 private:
0269   Graphic3d_CLight(const Graphic3d_CLight&);
0270   Graphic3d_CLight& operator=(const Graphic3d_CLight&);
0271 
0272 protected:
0273   TCollection_AsciiString myId;                      //!< resource id
0274   TCollection_AsciiString myName;                    //!< user given name
0275   gp_Pnt                  myPosition;                //!< light position
0276   Quantity_ColorRGBA      myColor;                   //!< light color
0277   Graphic3d_Vec4          myDirection;               //!< direction of directional/spot light
0278   Graphic3d_Vec4          myParams;                  //!< packed light parameters
0279                                                      // clang-format off
0280   Standard_ShortReal                mySmoothness;  //!< radius for point light or cone angle for directional light
0281                                                      // clang-format on
0282   Standard_ShortReal                myIntensity;     //!< intensity multiplier for light
0283   const Graphic3d_TypeOfLightSource myType;          //!< Graphic3d_TypeOfLightSource enumeration
0284   Standard_Size                     myRevision;      //!< modification counter
0285   Standard_Boolean                  myIsHeadlight;   //!< flag to mark head light
0286   Standard_Boolean                  myIsEnabled;     //!< enabled state
0287   Standard_Boolean                  myToCastShadows; //!< casting shadows is requested
0288 };
0289 
0290 DEFINE_STANDARD_HANDLE(Graphic3d_CLight, Standard_Transient)
0291 
0292 #endif // Graphic3d_CLight_HeaderFile