Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-24 09:16:55

0001 // Created on: 2011-09-20
0002 // Created by: Sergey ZERCHANINOV
0003 // Copyright (c) 2011-2013 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 _OpenGl_Material_Header
0017 #define _OpenGl_Material_Header
0018 
0019 #include <Graphic3d_MaterialAspect.hxx>
0020 #include <OpenGl_Vec.hxx>
0021 
0022 class OpenGl_Context;
0023 
0024 //! OpenGL material definition
0025 struct OpenGl_MaterialCommon
0026 {
0027 
0028   NCollection_Vec4<float> Diffuse;           //!< diffuse RGB coefficients + alpha
0029   NCollection_Vec4<float> Emission;          //!< material RGB emission
0030   NCollection_Vec4<float> SpecularShininess; //!< glossy  RGB coefficients + shininess
0031   NCollection_Vec4<float> Ambient;           //!< ambient RGB coefficients
0032 
0033   float Shine() const { return SpecularShininess.a(); }
0034 
0035   float& ChangeShine() { return SpecularShininess.a(); }
0036 
0037   //! Empty constructor.
0038   OpenGl_MaterialCommon()
0039       : Diffuse(1.0f),
0040         Emission(1.0f),
0041         SpecularShininess(1.0f, 1.0f, 1.0f, 0.0f),
0042         Ambient(1.0f)
0043   {
0044   }
0045 
0046   //! Set material color.
0047   void SetColor(const NCollection_Vec3<float>& theColor)
0048   {
0049     // apply the same formula as in Graphic3d_MaterialAspect::SetColor()
0050     Ambient.SetValues(theColor * 0.25f, Ambient.a());
0051     Diffuse.SetValues(theColor, Diffuse.a());
0052   }
0053 };
0054 
0055 //! OpenGL material definition
0056 struct OpenGl_MaterialPBR
0057 {
0058 
0059   NCollection_Vec4<float> BaseColor; //!< base color of PBR material with alpha component
0060                                      // clang-format off
0061   NCollection_Vec4<float> EmissionIOR; //!< light intensity which is emitted by PBR material and index of refraction
0062                                      // clang-format on
0063   NCollection_Vec4<float> Params;    //!< extra packed parameters
0064 
0065   float Metallic() const { return Params.b(); }
0066 
0067   float& ChangeMetallic() { return Params.b(); }
0068 
0069   float Roughness() const { return Params.g(); }
0070 
0071   float& ChangeRoughness() { return Params.g(); }
0072 
0073   //! Empty constructor.
0074   OpenGl_MaterialPBR()
0075       : BaseColor(1.0f),
0076         EmissionIOR(1.0f),
0077         Params(1.0f, 1.0f, 1.0f, 1.0f)
0078   {
0079   }
0080 
0081   //! Set material color.
0082   void SetColor(const NCollection_Vec3<float>& theColor)
0083   {
0084     BaseColor.SetValues(theColor, BaseColor.a());
0085   }
0086 };
0087 
0088 //! OpenGL material definition
0089 struct OpenGl_Material
0090 {
0091   OpenGl_MaterialCommon Common[2];
0092   OpenGl_MaterialPBR    Pbr[2];
0093 
0094   //! Set material color.
0095   void SetColor(const NCollection_Vec3<float>& theColor)
0096   {
0097     Common[0].SetColor(theColor);
0098     Common[1].SetColor(theColor);
0099     Pbr[0].SetColor(theColor);
0100     Pbr[1].SetColor(theColor);
0101   }
0102 
0103   //! Initialize material
0104   void Init(const OpenGl_Context&           theCtx,
0105             const Graphic3d_MaterialAspect& theFront,
0106             const Quantity_Color&           theFrontColor,
0107             const Graphic3d_MaterialAspect& theBack,
0108             const Quantity_Color&           theBackColor);
0109 
0110   //! Check this material for equality with another material (without tolerance!).
0111   bool IsEqual(const OpenGl_Material& theOther) const
0112   {
0113     return std::memcmp(this, &theOther, sizeof(OpenGl_Material)) == 0;
0114   }
0115 
0116   //! Check this material for equality with another material (without tolerance!).
0117   bool operator==(const OpenGl_Material& theOther) { return IsEqual(theOther); }
0118 
0119   bool operator==(const OpenGl_Material& theOther) const { return IsEqual(theOther); }
0120 
0121   //! Check this material for non-equality with another material (without tolerance!).
0122   bool operator!=(const OpenGl_Material& theOther) { return !IsEqual(theOther); }
0123 
0124   bool operator!=(const OpenGl_Material& theOther) const { return !IsEqual(theOther); }
0125 
0126   //! Returns packed (serialized) representation of common material properties
0127   const NCollection_Vec4<float>* PackedCommon() const
0128   {
0129     return reinterpret_cast<const NCollection_Vec4<float>*>(Common);
0130   }
0131 
0132   static int NbOfVec4Common() { return 4 * 2; }
0133 
0134   //! Returns packed (serialized) representation of PBR material properties
0135   const NCollection_Vec4<float>* PackedPbr() const
0136   {
0137     return reinterpret_cast<const NCollection_Vec4<float>*>(Pbr);
0138   }
0139 
0140   static int NbOfVec4Pbr() { return 3 * 2; }
0141 
0142 private:
0143   //! Initialize material
0144   void init(const OpenGl_Context&           theCtx,
0145             const Graphic3d_MaterialAspect& theMat,
0146             const Quantity_Color&           theColor,
0147             const int                       theIndex);
0148 };
0149 
0150 //! Material flag
0151 enum OpenGl_MaterialFlag
0152 {
0153   OpenGl_MaterialFlag_Front, //!< material for front faces
0154   OpenGl_MaterialFlag_Back   //!< material for back  faces
0155 };
0156 
0157 #endif // _OpenGl_Material_Header