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 Diffuse; //!< diffuse RGB coefficients + alpha
29 OpenGl_Vec4 Emission; //!< material RGB emission
30 OpenGl_Vec4 SpecularShininess; //!< glossy RGB coefficients + shininess
31 OpenGl_Vec4 Ambient; //!< ambient RGB coefficients
33 float Shine() const { return SpecularShininess.a(); }
34 float& ChangeShine() { return SpecularShininess.a(); }
36 //! Empty constructor.
37 OpenGl_MaterialCommon() : Diffuse (1.0f), Emission (1.0f), SpecularShininess (1.0f, 1.0f, 1.0f, 0.0f), Ambient (1.0f) {}
39 //! Set material color.
40 void SetColor (const OpenGl_Vec3& theColor)
42 // apply the same formula as in Graphic3d_MaterialAspect::SetColor()
43 Ambient.SetValues (theColor * 0.25f, Ambient.a());
44 Diffuse.SetValues (theColor, Diffuse.a());
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 //! Set material color.
67 void SetColor (const OpenGl_Vec3& theColor)
69 BaseColor.SetValues (theColor, BaseColor.a());
74 //! OpenGL material definition
75 struct OpenGl_Material
77 OpenGl_MaterialCommon Common[2];
78 OpenGl_MaterialPBR Pbr[2];
80 //! Set material color.
81 void SetColor (const OpenGl_Vec3& theColor)
83 Common[0].SetColor (theColor);
84 Common[1].SetColor (theColor);
85 Pbr[0].SetColor (theColor);
86 Pbr[1].SetColor (theColor);
89 //! Initialize material
90 void Init (const OpenGl_Context& theCtx,
91 const Graphic3d_MaterialAspect& theFront,
92 const Quantity_Color& theFrontColor,
93 const Graphic3d_MaterialAspect& theBack,
94 const Quantity_Color& theBackColor);
96 //! Check this material for equality with another material (without tolerance!).
97 bool IsEqual (const OpenGl_Material& theOther) const
99 return std::memcmp (this, &theOther, sizeof(OpenGl_Material)) == 0;
102 //! Check this material for 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); }
106 //! Check this material for non-equality with another material (without tolerance!).
107 bool operator!= (const OpenGl_Material& theOther) { return !IsEqual (theOther); }
108 bool operator!= (const OpenGl_Material& theOther) const { return !IsEqual (theOther); }
110 //! Returns packed (serialized) representation of common material properties
111 const OpenGl_Vec4* PackedCommon() const { return reinterpret_cast<const OpenGl_Vec4*> (Common); }
112 static Standard_Integer NbOfVec4Common() { return 4 * 2; }
114 //! Returns packed (serialized) representation of PBR material properties
115 const OpenGl_Vec4* PackedPbr() const { return reinterpret_cast<const OpenGl_Vec4*> (Pbr); }
116 static Standard_Integer NbOfVec4Pbr() { return 3 * 2; }
120 //! Initialize material
121 void init (const OpenGl_Context& theCtx,
122 const Graphic3d_MaterialAspect& theMat,
123 const Quantity_Color& theColor,
124 const Standard_Integer theIndex);
129 enum OpenGl_MaterialFlag
131 OpenGl_MaterialFlag_Front, //!< material for front faces
132 OpenGl_MaterialFlag_Back //!< material for back faces
135 #endif // _OpenGl_Material_Header