0027974: Visualization, ray tracing - Improve ray tracing engine
[occt.git] / src / Graphic3d / Graphic3d_BSDF.hxx
1 // Created on: 2015-01-15
2 // Created by: Danila ULYANOV
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 #ifndef _Graphic3d_BSDF_HeaderFile
17 #define _Graphic3d_BSDF_HeaderFile
18
19 #include <Graphic3d_Vec3.hxx>
20 #include <Graphic3d_Vec4.hxx>
21
22 //! Type of the Fresnel model.
23 enum Graphic3d_FresnelModel
24 {
25   Graphic3d_FM_SCHLICK    = 0,
26   Graphic3d_FM_CONSTANT   = 1,
27   Graphic3d_FM_CONDUCTOR  = 2,
28   Graphic3d_FM_DIELECTRIC = 3
29 };
30
31 //! Describes Fresnel reflectance parameters.
32 class Graphic3d_Fresnel
33 {
34 public:
35
36   //! Creates uninitialized Fresnel factor.
37   Graphic3d_Fresnel()
38   : myFresnelType (Graphic3d_FM_CONSTANT)
39   {
40     // ideal specular reflector
41     myFresnelData = Graphic3d_Vec3 (0.f, 1.f, 0.f);
42   }
43
44   //! Creates Schlick's approximation of Fresnel factor.
45   static Graphic3d_Fresnel CreateSchlick (const Graphic3d_Vec3& theSpecularColor)
46   {
47     return Graphic3d_Fresnel (Graphic3d_FM_SCHLICK, theSpecularColor);
48   }
49
50   //! Creates Fresnel factor for constant reflection.
51   static Graphic3d_Fresnel CreateConstant (const Standard_ShortReal theReflection)
52   {
53     return Graphic3d_Fresnel (Graphic3d_FM_CONSTANT, Graphic3d_Vec3 (0.f, 1.f, theReflection));
54   }
55
56   //! Creates Fresnel factor for physical-based dielectric model.
57   static Graphic3d_Fresnel CreateDielectric (Standard_ShortReal theRefractionIndex)
58   {
59     return Graphic3d_Fresnel (Graphic3d_FM_DIELECTRIC, Graphic3d_Vec3 (0.f, theRefractionIndex, 0.f));
60   }
61
62   //! Creates Fresnel factor for physical-based conductor model.
63   static Graphic3d_Fresnel CreateConductor (Standard_ShortReal theRefractionIndex,
64                                             Standard_ShortReal theAbsorptionIndex)
65   {
66     return Graphic3d_Fresnel (Graphic3d_FM_CONDUCTOR, Graphic3d_Vec3 (0.f, theRefractionIndex, theAbsorptionIndex));
67   }
68
69   //! Creates Fresnel factor for physical-based conductor model (spectral version).
70   Standard_EXPORT static Graphic3d_Fresnel CreateConductor (const Graphic3d_Vec3& theRefractionIndex,
71                                                             const Graphic3d_Vec3& theAbsorptionIndex);
72
73 public:
74
75   //! Returns serialized representation of Fresnel factor.
76   Standard_EXPORT Graphic3d_Vec4 Serialize() const;
77
78   //! Performs comparison of two objects describing Fresnel factor.
79   bool operator== (const Graphic3d_Fresnel& theOther) const
80   {
81     return myFresnelType == theOther.myFresnelType
82         && myFresnelData == theOther.myFresnelData;
83   }
84
85   //! Returns type of Fresnel.
86   Graphic3d_FresnelModel FresnelType() const
87   {
88     return myFresnelType;
89   }
90
91 protected:
92
93   //! Creates new Fresnel reflectance factor.
94   Graphic3d_Fresnel (Graphic3d_FresnelModel theType, const Graphic3d_Vec3& theData)
95   : myFresnelType (theType),
96     myFresnelData (theData)
97   {
98     //
99   }
100
101 private:
102
103   //! Type of Fresnel approximation.
104   Graphic3d_FresnelModel myFresnelType;
105
106   //! Serialized parameters of specific approximation.
107   Graphic3d_Vec3 myFresnelData;
108 };
109
110 //! Describes material's BSDF (Bidirectional Scattering Distribution Function) used
111 //! for physically-based rendering (in path tracing engine). BSDF is represented as
112 //! weighted mixture of basic BRDFs/BTDFs (Bidirectional Reflectance (Transmittance)
113 //! Distribution Functions).
114 class Graphic3d_BSDF
115 {
116 public:
117
118   //! Weight of the Lambertian BRDF.
119   Graphic3d_Vec3 Kd;
120
121   //! Weight of the reflection BRDF.
122   Graphic3d_Vec3 Kr;
123
124   //! Weight of the transmission BTDF.
125   Graphic3d_Vec3 Kt;
126
127   //! Weight of the Blinn's glossy BRDF.
128   Graphic3d_Vec3 Ks;
129
130   //! Self-emitted radiance.
131   Graphic3d_Vec3 Le;
132
133   //! Parameters of Fresnel reflectance.
134   Graphic3d_Fresnel Fresnel;
135
136   //! Roughness (exponent) of Blinn's BRDF.
137   Standard_ShortReal Roughness;
138
139   //! Absorption color of transparent media.
140   Graphic3d_Vec3 AbsorptionColor;
141
142   //! Absorption intensity of transparent media.
143   Standard_ShortReal AbsorptionCoeff;
144
145 public:
146
147   //! Creates BSDF describing diffuse (Lambertian) surface.
148   static Standard_EXPORT Graphic3d_BSDF CreateDiffuse (const Graphic3d_Vec3& theWeight);
149
150   //! Creates BSDF describing polished metallic-like surface.
151   static Standard_EXPORT Graphic3d_BSDF CreateMetallic (const Graphic3d_Vec3&    theWeight,
152                                                         const Graphic3d_Fresnel& theFresnel,
153                                                         const Standard_ShortReal theRoughness);
154
155   //! Creates BSDF describing transparent object.
156   //! Transparent BSDF models simple transparency without
157   //! refraction (the ray passes straight through the surface).
158   static Standard_EXPORT Graphic3d_BSDF CreateTransparent (const Graphic3d_Vec3&    theWeight,
159                                                            const Graphic3d_Vec3&    theAbsorptionColor,
160                                                            const Standard_ShortReal theAbsorptionCoeff);
161
162   //! Creates BSDF describing glass-like object.
163   //! Glass-like BSDF mixes refraction and reflection effects at
164   //! grazing angles using physically-based Fresnel dielectric model.
165   static Standard_EXPORT Graphic3d_BSDF CreateGlass (const Graphic3d_Vec3&    theWeight,
166                                                      const Graphic3d_Vec3&    theAbsorptionColor,
167                                                      const Standard_ShortReal theAbsorptionCoeff,
168                                                      const Standard_ShortReal theRefractionIndex);
169
170 public:
171
172   //! Creates uninitialized BSDF.
173   Graphic3d_BSDF() : Roughness (1.f), AbsorptionCoeff (0.f)
174   {
175     Fresnel = Graphic3d_Fresnel::CreateConstant (1.f);
176   }
177
178   //! Normalizes BSDF components.
179   Standard_EXPORT void Normalize();
180
181   //! Performs mixing of two BSDFs.
182   Graphic3d_BSDF& operator+ (const Graphic3d_BSDF& theOther)
183   {
184     Kd += theOther.Kd;
185     Kr += theOther.Kr;
186     Kt += theOther.Kt;
187     Ks += theOther.Ks;
188     Le += theOther.Le;
189
190     return *this;
191   }
192
193   //! Performs comparison of two BSDFs.
194   bool operator== (const Graphic3d_BSDF& theOther) const
195   {
196     return Kd              == theOther.Kd
197         && Kr              == theOther.Kr
198         && Kt              == theOther.Kt
199         && Ks              == theOther.Ks
200         && Le              == theOther.Le
201         && Fresnel         == theOther.Fresnel
202         && Roughness       == theOther.Roughness
203         && AbsorptionCoeff == theOther.AbsorptionCoeff
204         && AbsorptionColor == theOther.AbsorptionColor;
205   }
206
207 };
208
209 #endif // _Graphic3d_BSDF_HeaderFile