Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-18 09:20:34

0001 // Created by: NW,JPB,CAL
0002 // Copyright (c) 1991-1999 Matra Datavision
0003 // Copyright (c) 1999-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _Graphic3d_MaterialAspect_HeaderFile
0017 #define _Graphic3d_MaterialAspect_HeaderFile
0018 
0019 #include <Graphic3d_BSDF.hxx>
0020 #include <Graphic3d_PBRMaterial.hxx>
0021 #include <Graphic3d_NameOfMaterial.hxx>
0022 #include <Graphic3d_TypeOfMaterial.hxx>
0023 #include <Graphic3d_TypeOfReflection.hxx>
0024 #include <TCollection_AsciiString.hxx>
0025 #include <Quantity_Color.hxx>
0026 
0027 //! This class allows the definition of the type of a surface.
0028 //! Aspect attributes of a 3d face.
0029 //! Keywords: Material, FillArea, Shininess, Ambient, Color, Diffuse,
0030 //! Specular, Transparency, Emissive, ReflectionMode,
0031 //! BackFace, FrontFace, Reflection, Absorption
0032 class Graphic3d_MaterialAspect
0033 {
0034 public:
0035   DEFINE_STANDARD_ALLOC
0036 
0037   //! Returns the number of predefined textures.
0038   static int NumberOfMaterials() { return Graphic3d_NameOfMaterial_DEFAULT; }
0039 
0040   //! Returns the name of the predefined material of specified rank within range [1,
0041   //! NumberOfMaterials()].
0042   Standard_EXPORT static const char* MaterialName(const int theRank);
0043 
0044   //! Returns the type of the predefined material of specified rank within range [1,
0045   //! NumberOfMaterials()].
0046   Standard_EXPORT static Graphic3d_TypeOfMaterial MaterialType(const int theRank);
0047 
0048   //! Finds the material for specified name.
0049   //! @param[in] theName   name to find
0050   //! @param[out] theMat   found material
0051   //! @return FALSE if name was unrecognized
0052   Standard_EXPORT static bool MaterialFromName(const char* const         theName,
0053                                                Graphic3d_NameOfMaterial& theMat);
0054 
0055   //! Returns the material for specified name or Graphic3d_NameOfMaterial_DEFAULT if name is
0056   //! unknown.
0057   static Graphic3d_NameOfMaterial MaterialFromName(const char* const theName)
0058   {
0059     Graphic3d_NameOfMaterial aMat = Graphic3d_NameOfMaterial_DEFAULT;
0060     MaterialFromName(theName, aMat);
0061     return aMat;
0062   }
0063 
0064 public:
0065   //! Creates a material from default values.
0066   Standard_EXPORT Graphic3d_MaterialAspect();
0067 
0068   //! Creates a generic material.
0069   Standard_EXPORT Graphic3d_MaterialAspect(const Graphic3d_NameOfMaterial theName);
0070 
0071   //! Returns the material name (within predefined enumeration).
0072   Graphic3d_NameOfMaterial Name() const { return myMaterialName; }
0073 
0074   //! Returns the material name within predefined enumeration which has been requested (before
0075   //! modifications).
0076   Graphic3d_NameOfMaterial RequestedName() const { return myRequestedMaterialName; }
0077 
0078   //! Returns the given name of this material. This might be:
0079   //! - given name set by method ::SetMaterialName()
0080   //! - standard name for a material within enumeration
0081   //! - "UserDefined" for non-standard material without name specified externally.
0082   const TCollection_AsciiString& StringName() const { return myStringName; }
0083 
0084   //! Returns the given name of this material. This might be:
0085   const char* MaterialName() const { return myStringName.ToCString(); }
0086 
0087   //! The current material become a "UserDefined" material.
0088   //! Set the name of the "UserDefined" material.
0089   void SetMaterialName(const TCollection_AsciiString& theName)
0090   {
0091     // if a component of a "standard" material change, the
0092     // result is no more standard (a blue gold is not a gold)
0093     myMaterialName = Graphic3d_NameOfMaterial_UserDefined;
0094     myStringName   = theName;
0095   }
0096 
0097   //! Resets the material with the original values according to
0098   //! the material name but leave the current color values untouched
0099   //! for the material of type ASPECT.
0100   void Reset() { init(myRequestedMaterialName); }
0101 
0102   //! Returns the diffuse color of the surface.
0103   //! WARNING! This method does NOT return color for Graphic3d_MATERIAL_ASPECT material (color is
0104   //! defined by Graphic3d_Aspects::InteriorColor()).
0105   const Quantity_Color& Color() const { return myColors[Graphic3d_TOR_DIFFUSE]; }
0106 
0107   //! Modifies the ambient and diffuse color of the surface.
0108   //! WARNING! Has no effect for Graphic3d_MATERIAL_ASPECT material (color should be set to
0109   //! Graphic3d_Aspects::SetInteriorColor()).
0110   Standard_EXPORT void SetColor(const Quantity_Color& theColor);
0111 
0112   //! Returns the transparency coefficient of the surface (1.0 - Alpha); 0.0 means opaque.
0113   float Transparency() const { return myTransparencyCoef; }
0114 
0115   //! Returns the alpha coefficient of the surface (1.0 - Transparency); 1.0 means opaque.
0116   float Alpha() const { return 1.0f - myTransparencyCoef; }
0117 
0118   //! Modifies the transparency coefficient of the surface, where 0 is opaque and 1 is fully
0119   //! transparent. Transparency is applicable to materials that have at least one of reflection
0120   //! modes (ambient, diffuse, specular or emissive) enabled. See also SetReflectionModeOn() and
0121   //! SetReflectionModeOff() methods.
0122   //!
0123   //! Warning: Raises MaterialDefinitionError if given value is a negative value or greater
0124   //! than 1.0.
0125   Standard_EXPORT void SetTransparency(const float theValue);
0126 
0127   //! Modifies the alpha coefficient of the surface, where 1.0 is opaque and 0.0 is fully
0128   //! transparent.
0129   void SetAlpha(float theValue) { SetTransparency(1.0f - theValue); }
0130 
0131   //! Returns the ambient color of the surface.
0132   const Quantity_Color& AmbientColor() const { return myColors[Graphic3d_TOR_AMBIENT]; }
0133 
0134   //! Modifies the ambient color of the surface.
0135   Standard_EXPORT void SetAmbientColor(const Quantity_Color& theColor);
0136 
0137   //! Returns the diffuse color of the surface.
0138   const Quantity_Color& DiffuseColor() const { return myColors[Graphic3d_TOR_DIFFUSE]; }
0139 
0140   //! Modifies the diffuse color of the surface.
0141   Standard_EXPORT void SetDiffuseColor(const Quantity_Color& theColor);
0142 
0143   //! Returns the specular color of the surface.
0144   const Quantity_Color& SpecularColor() const { return myColors[Graphic3d_TOR_SPECULAR]; }
0145 
0146   //! Modifies the specular color of the surface.
0147   Standard_EXPORT void SetSpecularColor(const Quantity_Color& theColor);
0148 
0149   //! Returns the emissive color of the surface.
0150   const Quantity_Color& EmissiveColor() const { return myColors[Graphic3d_TOR_EMISSION]; }
0151 
0152   //! Modifies the emissive color of the surface.
0153   Standard_EXPORT void SetEmissiveColor(const Quantity_Color& theColor);
0154 
0155   //! Returns the luminosity of the surface.
0156   float Shininess() const { return myShininess; }
0157 
0158   //! Modifies the luminosity of the surface.
0159   //! Warning: Raises MaterialDefinitionError if given value is a negative value or greater
0160   //! than 1.0.
0161   Standard_EXPORT void SetShininess(const float theValue);
0162 
0163   //! Increases or decreases the luminosity.
0164   //! @param theDelta a signed percentage
0165   Standard_EXPORT void IncreaseShine(const float theDelta);
0166 
0167   //! Returns the refraction index of the material
0168   float RefractionIndex() const { return myRefractionIndex; }
0169 
0170   //! Modifies the refraction index of the material.
0171   //! Warning: Raises MaterialDefinitionError if given value is a lesser than 1.0.
0172   Standard_EXPORT void SetRefractionIndex(const float theValue);
0173 
0174   //! Returns BSDF (bidirectional scattering distribution function).
0175   const Graphic3d_BSDF& BSDF() const { return myBSDF; }
0176 
0177   //! Modifies the BSDF (bidirectional scattering distribution function).
0178   void SetBSDF(const Graphic3d_BSDF& theBSDF) { myBSDF = theBSDF; }
0179 
0180   //! Returns physically based representation of material
0181   const Graphic3d_PBRMaterial& PBRMaterial() const { return myPBRMaterial; }
0182 
0183   //! Modifies the physically based representation of material
0184   void SetPBRMaterial(const Graphic3d_PBRMaterial& thePBRMaterial)
0185   {
0186     myPBRMaterial = thePBRMaterial;
0187   }
0188 
0189   //! Returns TRUE if the reflection mode is active, FALSE otherwise.
0190   bool ReflectionMode(const Graphic3d_TypeOfReflection theType) const
0191   {
0192     return !myColors[theType].IsEqual(Quantity_NOC_BLACK);
0193   }
0194 
0195   //! Returns material type.
0196   Graphic3d_TypeOfMaterial MaterialType() const { return myMaterialType; }
0197 
0198   //! Returns TRUE if type of this material is equal to specified type.
0199   bool MaterialType(const Graphic3d_TypeOfMaterial theType) const
0200   {
0201     return myMaterialType == theType;
0202   }
0203 
0204   //! Set material type.
0205   Standard_EXPORT void SetMaterialType(const Graphic3d_TypeOfMaterial theType);
0206 
0207   //! Returns TRUE if this material differs from specified one.
0208   bool IsDifferent(const Graphic3d_MaterialAspect& theOther) const { return !IsEqual(theOther); }
0209 
0210   //! Returns TRUE if this material differs from specified one.
0211   bool operator!=(const Graphic3d_MaterialAspect& theOther) const { return IsDifferent(theOther); }
0212 
0213   //! Returns TRUE if this material is identical to specified one.
0214   bool IsEqual(const Graphic3d_MaterialAspect& theOther) const
0215   {
0216     return myTransparencyCoef == theOther.myTransparencyCoef
0217            && myRefractionIndex == theOther.myRefractionIndex && myBSDF == theOther.myBSDF
0218            && myPBRMaterial == theOther.myPBRMaterial && myShininess == theOther.myShininess
0219            && myColors[Graphic3d_TOR_AMBIENT] == theOther.myColors[Graphic3d_TOR_AMBIENT]
0220            && myColors[Graphic3d_TOR_DIFFUSE] == theOther.myColors[Graphic3d_TOR_DIFFUSE]
0221            && myColors[Graphic3d_TOR_SPECULAR] == theOther.myColors[Graphic3d_TOR_SPECULAR]
0222            && myColors[Graphic3d_TOR_EMISSION] == theOther.myColors[Graphic3d_TOR_EMISSION];
0223   }
0224 
0225   //! Returns TRUE if this material is identical to specified one.
0226   bool operator==(const Graphic3d_MaterialAspect& theOther) const { return IsEqual(theOther); }
0227 
0228   //! Dumps the content of me into the stream
0229   Standard_EXPORT void DumpJson(Standard_OStream& theOStream, int theDepth = -1) const;
0230 
0231 public:
0232   //! Deactivates the reflective properties of the surface with specified reflection type.
0233   Standard_DEPRECATED("Deprecated method, specific material component should be zerroed "
0234                       "instead")
0235   void SetReflectionModeOff(const Graphic3d_TypeOfReflection theType)
0236   {
0237     if (!ReflectionMode(theType))
0238     {
0239       return;
0240     }
0241 
0242     switch (theType)
0243     {
0244       case Graphic3d_TOR_AMBIENT:
0245         SetAmbientColor(Quantity_NOC_BLACK);
0246         break;
0247       case Graphic3d_TOR_DIFFUSE:
0248         SetDiffuseColor(Quantity_NOC_BLACK);
0249         break;
0250       case Graphic3d_TOR_SPECULAR:
0251         SetSpecularColor(Quantity_NOC_BLACK);
0252         break;
0253       case Graphic3d_TOR_EMISSION:
0254         SetEmissiveColor(Quantity_NOC_BLACK);
0255         break;
0256     }
0257   }
0258 
0259 private:
0260   //! Initialize the standard material.
0261   Standard_EXPORT void init(const Graphic3d_NameOfMaterial theName);
0262 
0263   //! Mark material as user defined.
0264   void setUserMaterial()
0265   {
0266     // if a component of a "standard" material change, the
0267     // result is no more standard (a blue gold is not a gold)
0268     if (myMaterialName != Graphic3d_NameOfMaterial_UserDefined)
0269     {
0270       myMaterialName = Graphic3d_NameOfMaterial_UserDefined;
0271       myStringName   = "UserDefined";
0272     }
0273   }
0274 
0275 private:
0276   Graphic3d_BSDF          myBSDF;
0277   Graphic3d_PBRMaterial   myPBRMaterial;
0278   TCollection_AsciiString myStringName;
0279   Quantity_Color          myColors[Graphic3d_TypeOfReflection_NB];
0280   float                   myTransparencyCoef;
0281   float                   myRefractionIndex;
0282   float                   myShininess;
0283 
0284   Graphic3d_TypeOfMaterial myMaterialType;
0285   Graphic3d_NameOfMaterial myMaterialName;
0286   Graphic3d_NameOfMaterial myRequestedMaterialName;
0287 };
0288 
0289 #endif // _Graphic3d_MaterialAspect_HeaderFile