8613985b |
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 | |
ba00aab7 |
22 | class OpenGl_Context; |
23 | |
8613985b |
24 | //! OpenGL material definition |
67312b79 |
25 | struct OpenGl_MaterialCommon |
8613985b |
26 | { |
27 | |
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 |
33 | |
34 | float Shine() const { return Params.x(); } |
35 | float& ChangeShine() { return Params.x(); } |
36 | |
37 | float Transparency() const { return Params.y(); } |
38 | float& ChangeTransparency() { return Params.y(); } |
39 | |
67312b79 |
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) {} |
42 | |
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; } |
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 | //! 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; } |
69 | |
70 | }; |
71 | |
72 | //! OpenGL material definition |
73 | struct OpenGl_Material |
74 | { |
75 | OpenGl_MaterialCommon Common; |
76 | OpenGl_MaterialPBR Pbr; |
77 | |
8613985b |
78 | //! Set material color. |
79 | void SetColor (const OpenGl_Vec4& theColor) |
80 | { |
81 | // apply the same formula as in Graphic3d_MaterialAspect::SetColor() |
67312b79 |
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()); |
8613985b |
85 | } |
86 | |
87 | //! Initialize material |
ba00aab7 |
88 | void Init (const OpenGl_Context& theCtx, |
89 | const Graphic3d_MaterialAspect& theProp, |
90 | const Quantity_Color& theInteriorColor); |
8613985b |
91 | |
8613985b |
92 | //! Check this material for equality with another material (without tolerance!). |
93 | bool IsEqual (const OpenGl_Material& theOther) const |
94 | { |
95 | return std::memcmp (this, &theOther, sizeof(OpenGl_Material)) == 0; |
96 | } |
97 | |
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); } |
101 | |
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); } |
105 | |
106 | }; |
107 | |
108 | //! Material flag |
109 | enum OpenGl_MaterialFlag |
110 | { |
111 | OpenGl_MaterialFlag_Front, //!< material for front faces |
112 | OpenGl_MaterialFlag_Back //!< material for back faces |
113 | }; |
114 | |
115 | #endif // _OpenGl_Material_Header |