Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:34

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