1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2013 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _OpenGl_Material_Header
17 #define _OpenGl_Material_Header
19 #include <Graphic3d_MaterialAspect.hxx>
20 #include <OpenGl_Vec.hxx>
24 //! OpenGL material definition
25 struct OpenGl_MaterialCommon
28 OpenGl_Vec4 Ambient; //!< ambient reflection coefficient
29 OpenGl_Vec4 Diffuse; //!< diffuse reflection coefficient
30 OpenGl_Vec4 Specular; //!< glossy reflection coefficient
31 OpenGl_Vec4 Emission; //!< material emission
32 OpenGl_Vec4 Params; //!< extra packed parameters
34 float Shine() const { return Params.x(); }
35 float& ChangeShine() { return Params.x(); }
37 float Transparency() const { return Params.y(); }
38 float& ChangeTransparency() { return Params.y(); }
40 //! Empty constructor.
41 OpenGl_MaterialCommon() : Ambient (1.0f), Diffuse (1.0f), Specular (1.0f), Emission (1.0f), Params (1.0f, 0.0f, 0.0f, 0.0f) {}
43 //! Returns packed (serialized) representation of material properties
44 const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); }
45 static Standard_Integer NbOfVec4() { return 5; }
49 //! OpenGL material definition
50 struct OpenGl_MaterialPBR
53 OpenGl_Vec4 BaseColor; //!< base color of PBR material with alpha component
54 OpenGl_Vec4 EmissionIOR; //!< light intensity which is emitted by PBR material and index of refraction
55 OpenGl_Vec4 Params; //!< extra packed parameters
57 float Metallic() const { return Params.b(); }
58 float& ChangeMetallic() { return Params.b(); }
60 float Roughness() const { return Params.g(); }
61 float& ChangeRoughness() { return Params.g(); }
63 //! Empty constructor.
64 OpenGl_MaterialPBR() : BaseColor (1.0f), EmissionIOR (1.0f), Params (1.0f, 1.0f, 1.0f, 1.0f) {}
66 //! Returns packed (serialized) representation of material properties
67 const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); }
68 static Standard_Integer NbOfVec4() { return 3; }
72 //! OpenGL material definition
73 struct OpenGl_Material
75 OpenGl_MaterialCommon Common;
76 OpenGl_MaterialPBR Pbr;
78 //! Set material color.
79 void SetColor (const OpenGl_Vec4& theColor)
81 // apply the same formula as in Graphic3d_MaterialAspect::SetColor()
82 Common.Ambient.SetValues (theColor.rgb() * 0.25f, Common.Ambient.a());
83 Common.Diffuse.SetValues (theColor.rgb(), Common.Diffuse.a());
84 Pbr .BaseColor.SetValues (theColor.rgb(), Pbr.BaseColor.a());
87 //! Initialize material
88 void Init (const OpenGl_Context& theCtx,
89 const Graphic3d_MaterialAspect& theProp,
90 const Quantity_Color& theInteriorColor);
92 //! Check this material for equality with another material (without tolerance!).
93 bool IsEqual (const OpenGl_Material& theOther) const
95 return std::memcmp (this, &theOther, sizeof(OpenGl_Material)) == 0;
98 //! Check this material for equality with another material (without tolerance!).
99 bool operator== (const OpenGl_Material& theOther) { return IsEqual (theOther); }
100 bool operator== (const OpenGl_Material& theOther) const { return IsEqual (theOther); }
102 //! Check this material for non-equality with another material (without tolerance!).
103 bool operator!= (const OpenGl_Material& theOther) { return !IsEqual (theOther); }
104 bool operator!= (const OpenGl_Material& theOther) const { return !IsEqual (theOther); }
109 enum OpenGl_MaterialFlag
111 OpenGl_MaterialFlag_Front, //!< material for front faces
112 OpenGl_MaterialFlag_Back //!< material for back faces
115 #endif // _OpenGl_Material_Header