Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:03:47

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