189f85a3 |
1 | // Created on: 2015-01-19 |
2 | // Created by: Denis BOGOLEPOV |
3 | // Copyright (c) 2014 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 | #include <Graphic3d_BSDF.hxx> |
17 | |
18 | #include <algorithm> |
19 | |
20 | // ======================================================================= |
21 | // function : Serialize |
22 | // purpose : |
23 | // ======================================================================= |
24 | Graphic3d_Vec4 Graphic3d_Fresnel::Serialize() const |
25 | { |
26 | Graphic3d_Vec4 aData = Graphic3d_Vec4 (myFresnelData, 0.f); |
27 | |
28 | if (myFresnelType != Graphic3d_FM_SCHLICK) |
29 | { |
30 | aData.x() = -static_cast<Standard_ShortReal> (myFresnelType); |
31 | } |
32 | |
33 | return aData; |
34 | } |
35 | |
36 | // ======================================================================= |
37 | // function : fresnelNormal |
38 | // purpose : |
39 | // ======================================================================= |
40 | inline Standard_ShortReal fresnelNormal (Standard_ShortReal theN, |
41 | Standard_ShortReal theK) |
42 | { |
43 | return ((theN - 1.f) * (theN - 1.f) + theK * theK) / |
44 | ((theN + 1.f) * (theN + 1.f) + theK * theK); |
45 | } |
46 | |
47 | // ======================================================================= |
48 | // function : CreateConductor |
49 | // purpose : |
50 | // ======================================================================= |
51 | Graphic3d_Fresnel Graphic3d_Fresnel::CreateConductor (const Graphic3d_Vec3& theRefractionIndex, |
52 | const Graphic3d_Vec3& theAbsorptionIndex) |
53 | { |
54 | return Graphic3d_Fresnel (Graphic3d_FM_SCHLICK, |
55 | Graphic3d_Vec3 (fresnelNormal (theRefractionIndex.x(), theAbsorptionIndex.x()), |
56 | fresnelNormal (theRefractionIndex.y(), theAbsorptionIndex.y()), |
57 | fresnelNormal (theRefractionIndex.z(), theAbsorptionIndex.z()))); |
58 | } |
59 | |
60 | // ======================================================================= |
61 | // function : Normalize |
62 | // purpose : |
63 | // ======================================================================= |
64 | void Graphic3d_BSDF::Normalize() |
65 | { |
66 | Standard_ShortReal aMax = std::max (Kd.x() + Ks.x() + Kr.x() + Kt.x(), |
67 | std::max (Kd.y() + Ks.y() + Kr.y() + Kt.y(), |
68 | Kd.z() + Ks.z() + Kr.z() + Kt.z())); |
69 | |
70 | if (aMax > 1.f) |
71 | { |
72 | Kd /= aMax; |
73 | Ks /= aMax; |
74 | Kr /= aMax; |
75 | Ks /= aMax; |
76 | } |
77 | } |
78 | |
79 | // ======================================================================= |
80 | // function : CreateDiffuse |
81 | // purpose : |
82 | // ======================================================================= |
83 | Graphic3d_BSDF Graphic3d_BSDF::CreateDiffuse (const Graphic3d_Vec3& theWeight) |
84 | { |
85 | Graphic3d_BSDF aBSDF; |
86 | |
87 | aBSDF.Kd = theWeight; |
88 | |
89 | return aBSDF; |
90 | } |
91 | |
92 | // ======================================================================= |
93 | // function : CreateMetallic |
94 | // purpose : |
95 | // ======================================================================= |
96 | Graphic3d_BSDF Graphic3d_BSDF::CreateMetallic (const Graphic3d_Vec3& theWeight, |
97 | const Graphic3d_Fresnel& theFresnel, |
98 | const Standard_ShortReal theRoughness) |
99 | { |
100 | Graphic3d_BSDF aBSDF; |
101 | |
102 | aBSDF.Roughness = theRoughness; |
103 | |
104 | // Selecting between specular and glossy |
105 | // BRDF depending on the given roughness |
106 | if (aBSDF.Roughness > 0.f) |
107 | { |
108 | aBSDF.Ks = theWeight; |
109 | } |
110 | else |
111 | { |
112 | aBSDF.Kr = theWeight; |
113 | } |
114 | |
115 | aBSDF.Fresnel = theFresnel; |
116 | |
117 | return aBSDF; |
118 | } |
119 | |
120 | // ======================================================================= |
121 | // function : CreateTransparent |
122 | // purpose : |
123 | // ======================================================================= |
124 | Graphic3d_BSDF Graphic3d_BSDF::CreateTransparent (const Graphic3d_Vec3& theWeight, |
125 | const Graphic3d_Vec3& theAbsorptionColor, |
126 | const Standard_ShortReal theAbsorptionCoeff) |
127 | { |
128 | Graphic3d_BSDF aBSDF; |
129 | |
130 | aBSDF.Kt = theWeight; |
131 | |
132 | aBSDF.AbsorptionColor = theAbsorptionColor; |
133 | aBSDF.AbsorptionCoeff = theAbsorptionCoeff; |
134 | |
135 | aBSDF.Fresnel = Graphic3d_Fresnel::CreateConstant (0.f); |
136 | |
137 | return aBSDF; |
138 | } |
139 | |
140 | // ======================================================================= |
141 | // function : CreateGlass |
142 | // purpose : |
143 | // ======================================================================= |
144 | Graphic3d_BSDF Graphic3d_BSDF::CreateGlass (const Graphic3d_Vec3& theWeight, |
145 | const Graphic3d_Vec3& theAbsorptionColor, |
146 | const Standard_ShortReal theAbsorptionCoeff, |
147 | const Standard_ShortReal theRefractionIndex) |
148 | { |
149 | Graphic3d_BSDF aBSDF; |
150 | |
151 | aBSDF.Kt = theWeight; |
152 | |
153 | aBSDF.AbsorptionColor = theAbsorptionColor; |
154 | aBSDF.AbsorptionCoeff = theAbsorptionCoeff; |
155 | |
156 | aBSDF.Fresnel = Graphic3d_Fresnel::CreateDielectric (theRefractionIndex); |
157 | |
158 | return aBSDF; |
159 | } |