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 | |
941f6cae |
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 |
8613985b |
32 | |
941f6cae |
33 | float Shine() const { return SpecularShininess.a(); } |
34 | float& ChangeShine() { return SpecularShininess.a(); } |
8613985b |
35 | |
67312b79 |
36 | //! Empty constructor. |
941f6cae |
37 | OpenGl_MaterialCommon() : Diffuse (1.0f), Emission (1.0f), SpecularShininess (1.0f, 1.0f, 1.0f, 0.0f), Ambient (1.0f) {} |
67312b79 |
38 | |
941f6cae |
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 | } |
67312b79 |
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 | |
941f6cae |
66 | //! Set material color. |
67 | void SetColor (const OpenGl_Vec3& theColor) |
68 | { |
69 | BaseColor.SetValues (theColor, BaseColor.a()); |
70 | } |
67312b79 |
71 | |
72 | }; |
73 | |
74 | //! OpenGL material definition |
75 | struct OpenGl_Material |
76 | { |
941f6cae |
77 | OpenGl_MaterialCommon Common[2]; |
78 | OpenGl_MaterialPBR Pbr[2]; |
67312b79 |
79 | |
8613985b |
80 | //! Set material color. |
941f6cae |
81 | void SetColor (const OpenGl_Vec3& theColor) |
8613985b |
82 | { |
941f6cae |
83 | Common[0].SetColor (theColor); |
84 | Common[1].SetColor (theColor); |
85 | Pbr[0].SetColor (theColor); |
86 | Pbr[1].SetColor (theColor); |
8613985b |
87 | } |
88 | |
89 | //! Initialize material |
ba00aab7 |
90 | void Init (const OpenGl_Context& theCtx, |
941f6cae |
91 | const Graphic3d_MaterialAspect& theFront, |
92 | const Quantity_Color& theFrontColor, |
93 | const Graphic3d_MaterialAspect& theBack, |
94 | const Quantity_Color& theBackColor); |
8613985b |
95 | |
8613985b |
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 | |
941f6cae |
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 | |
8613985b |
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 |