0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / OpenGl / OpenGl_Material.hxx
1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2013 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _OpenGl_Material_Header
17 #define _OpenGl_Material_Header
18
19 #include <Graphic3d_MaterialAspect.hxx>
20 #include <OpenGl_Vec.hxx>
21
22 class OpenGl_Context;
23
24 //! OpenGL material definition
25 struct OpenGl_MaterialCommon
26 {
27
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
32
33   float  Shine() const { return SpecularShininess.a(); }
34   float& ChangeShine() { return SpecularShininess.a(); }
35
36   //! Empty constructor.
37   OpenGl_MaterialCommon() : Diffuse (1.0f), Emission (1.0f), SpecularShininess (1.0f, 1.0f, 1.0f, 0.0f), Ambient (1.0f) {}
38
39   //! Set material color.
40   void SetColor (const OpenGl_Vec3& theColor)
41   {
42     // apply the same formula as in Graphic3d_MaterialAspect::SetColor()
43     Ambient.SetValues (theColor * 0.25f, Ambient.a());
44     Diffuse.SetValues (theColor, Diffuse.a());
45   }
46
47 };
48
49 //! OpenGL material definition
50 struct OpenGl_MaterialPBR
51 {
52
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
56
57   float  Metallic()  const { return Params.b(); }
58   float& ChangeMetallic()  { return Params.b(); }
59
60   float  Roughness() const { return Params.g(); }
61   float& ChangeRoughness() { return Params.g(); }
62
63   //! Empty constructor.
64   OpenGl_MaterialPBR() : BaseColor (1.0f), EmissionIOR (1.0f), Params  (1.0f, 1.0f, 1.0f, 1.0f) {}
65
66   //! Set material color.
67   void SetColor (const OpenGl_Vec3& theColor)
68   {
69     BaseColor.SetValues (theColor, BaseColor.a());
70   }
71
72 };
73
74 //! OpenGL material definition
75 struct OpenGl_Material
76 {
77   OpenGl_MaterialCommon Common[2];
78   OpenGl_MaterialPBR    Pbr[2];
79
80   //! Set material color.
81   void SetColor (const OpenGl_Vec3& theColor)
82   {
83     Common[0].SetColor (theColor);
84     Common[1].SetColor (theColor);
85     Pbr[0].SetColor (theColor);
86     Pbr[1].SetColor (theColor);
87   }
88
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);
95
96   //! Check this material for equality with another material (without tolerance!).
97   bool IsEqual (const OpenGl_Material& theOther) const
98   {
99     return std::memcmp (this, &theOther, sizeof(OpenGl_Material)) == 0;
100   }
101
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); }
105
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); }
109
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; }
113
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; }
117
118 private:
119
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);
125
126 };
127
128 //! Material flag
129 enum OpenGl_MaterialFlag
130 {
131   OpenGl_MaterialFlag_Front, //!< material for front faces
132   OpenGl_MaterialFlag_Back   //!< material for back  faces
133 };
134
135 #endif // _OpenGl_Material_Header