mirror of
https://git.dev.opencascade.org/repos/occt.git
synced 2025-04-03 17:56:21 +03:00
0027974: Visualization, ray tracing - Improve ray tracing engine
* Multiple importance sampling for path tracing * Improved light sources sampling (better handling several light sources) * Fixed issues in light source intersection (light distance is taken into account) * Add new TCL sample - OCCT Ball model for demonstrating physically-based materials * Fix potential issue on NVIDIA GPUs ("Error: Failed to upload light source buffer") * Path tracing materials reviewed; directional light source was smoother by default
This commit is contained in:
parent
0d0481c787
commit
6e728f3b5c
1131
data/occ/Ball.brep
Normal file
1131
data/occ/Ball.brep
Normal file
File diff suppressed because one or more lines are too long
78
samples/tcl/pathtrace_ball.tcl
Normal file
78
samples/tcl/pathtrace_ball.tcl
Normal file
@ -0,0 +1,78 @@
|
||||
# Script demonstrating Global illumination materials
|
||||
# using path tracing rendering engine in 3D view
|
||||
|
||||
#Category: Visualization
|
||||
#Title: Path tracing - Ball
|
||||
|
||||
set aBallPath [locate_data_file occ/Ball.brep]
|
||||
|
||||
pload MODELING VISUALIZATION
|
||||
|
||||
# Setup 3D viewer
|
||||
vclear
|
||||
vinit name=View1 w=512 h=512
|
||||
vglinfo
|
||||
vvbo 0
|
||||
vsetdispmode 1
|
||||
|
||||
# Setup view parameters
|
||||
vcamera -persp
|
||||
vviewparams -scale 18 -eye 44.49 -0.15 33.93 -at -14.20 -0.15 1.87 -up -0.48 0.00 0.88
|
||||
|
||||
# Load the model from disk
|
||||
puts "Importing shape..."
|
||||
restore $aBallPath ball
|
||||
|
||||
# Tessellate the model
|
||||
incmesh ball 0.01
|
||||
|
||||
# Display the model and assign material
|
||||
vdisplay -noupdate ball
|
||||
vsetmaterial -noupdate ball glass
|
||||
|
||||
# Create a sphere inside the model
|
||||
psphere s 8
|
||||
incmesh s 0.01
|
||||
vdisplay -noupdate s
|
||||
vsetlocation -noupdate s 0 0 13
|
||||
vsetmaterial -noupdate s plaster
|
||||
|
||||
# Create chessboard-style floor
|
||||
box tile 10 10 0.1
|
||||
eval compound [lrepeat 144 tile] tiles
|
||||
explode tiles
|
||||
for {set i 0} {$i < 12} {incr i} {
|
||||
for {set j 1} {$j <= 12} {incr j} {
|
||||
ttranslate tiles_[expr 12 * $i + $j] [expr $i * 10 - 90] [expr $j * 10 - 70] -0.15
|
||||
vdisplay -noupdate tiles_[expr 12 * $i + $j]
|
||||
|
||||
vsetmaterial -noupdate tiles_[expr 12 * $i + $j] plaster
|
||||
|
||||
if {($i + $j) % 2 == 0} {
|
||||
vbsdf tiles_[expr 12 * $i + $j] -kd 0.85
|
||||
} else {
|
||||
vbsdf tiles_[expr 12 * $i + $j] -kd 0.45
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Configure light sources
|
||||
vlight del 1
|
||||
vlight change 0 head 0
|
||||
vlight change 0 direction -0.25 -1 -1
|
||||
vlight change 0 sm 0.3
|
||||
vlight change 0 int 10
|
||||
|
||||
# Load environment map
|
||||
vtextureenv on 1
|
||||
|
||||
puts "Trying path tracing mode..."
|
||||
vrenderparams -ray -gi -rayDepth 10
|
||||
|
||||
# Start progressive refinement mode
|
||||
#vprogressive
|
||||
|
||||
puts "Make several path tracing iterations to refine the picture, please wait..."
|
||||
vfps 100
|
||||
puts "Done. To improve the image further, or after view manipulations, give command:"
|
||||
puts "vfps \[nb_iteratons\]"
|
@ -2,11 +2,12 @@
|
||||
# path tracing rendering engine in 3d view
|
||||
|
||||
#Category: Visualization
|
||||
#Title: Path tracing
|
||||
#Title: Path tracing - Cube
|
||||
|
||||
pload MODELING VISUALIZATION
|
||||
|
||||
# setup 3D viewer content
|
||||
vclear
|
||||
vinit name=View1 w=512 h=512
|
||||
vglinfo
|
||||
|
||||
@ -24,18 +25,18 @@ vcamera -persp
|
||||
# setup outer box
|
||||
box b 1 1 1
|
||||
explode b FACE
|
||||
vdisplay b_1 b_2 b_3 b_5 b_6
|
||||
vsetlocation b_1 1 0 0
|
||||
vsetlocation b_2 -1 0 0
|
||||
vsetlocation b_3 0 1 0
|
||||
vsetlocation b_5 0 0 1
|
||||
vsetlocation b_6 0 0 -1
|
||||
vdisplay -noupdate b_1 b_2 b_3 b_5 b_6
|
||||
vlocation -noupdate b_1 -setLocation 1 0 0
|
||||
vlocation -noupdate b_2 -setLocation -1 0 0
|
||||
vlocation -noupdate b_3 -setLocation 0 1 0
|
||||
vlocation -noupdate b_5 -setLocation 0 0 1
|
||||
vlocation -noupdate b_6 -setLocation 0 0 -1
|
||||
|
||||
vsetmaterial b_1 plastic
|
||||
vsetmaterial b_2 plastic
|
||||
vsetmaterial b_3 plastic
|
||||
vsetmaterial b_5 plastic
|
||||
vsetmaterial b_6 plastic
|
||||
vsetmaterial -noupdate b_1 plastic
|
||||
vsetmaterial -noupdate b_2 plastic
|
||||
vsetmaterial -noupdate b_3 plastic
|
||||
vsetmaterial -noupdate b_5 plastic
|
||||
vsetmaterial -noupdate b_6 plastic
|
||||
vbsdf b_1 -kd 1 0.3 0.3 -ks 0
|
||||
vbsdf b_2 -kd 0.3 0.5 1 -ks 0
|
||||
vbsdf b_3 -kd 1 -ks 0
|
||||
@ -47,36 +48,36 @@ vfit
|
||||
|
||||
# setup first inner sphere
|
||||
psphere s 0.2
|
||||
vdisplay s
|
||||
vsetlocation s 0.21 0.3 0.2
|
||||
vsetmaterial s glass
|
||||
vdisplay -noupdate s
|
||||
vlocation -noupdate s -setLocation 0.21 0.3 0.2
|
||||
vsetmaterial -noupdate s glass
|
||||
vbsdf s -absorpcolor 0.8 0.8 1.0
|
||||
vbsdf s -absorpcoeff 6
|
||||
|
||||
# setup first inner box
|
||||
box c 0.3 0.3 0.2
|
||||
vdisplay c
|
||||
vsetlocation c 0.55 0.3 0.0
|
||||
vlocrotate c 0 0 0 0 0 1 -30
|
||||
vsetmaterial c plastic
|
||||
vdisplay -noupdate c
|
||||
vlocation -noupdate c -setLocation 0.55 0.3 0.0
|
||||
vlocation -noupdate c -rotate 0 0 0 0 0 1 -30
|
||||
vsetmaterial -noupdate c plastic
|
||||
vbsdf c -kd 1.0 0.8 0.2 -ks 0.3 -n
|
||||
|
||||
# setup second inner box
|
||||
box g 0.15 0.15 0.3
|
||||
vdisplay g
|
||||
vsetlocation g 0.7 0.25 0.2
|
||||
vlocrotate g 0 0 0 0 0 1 10
|
||||
vsetmaterial g glass
|
||||
vdisplay -noupdate g
|
||||
vlocation -noupdate g -setLocation 0.7 0.25 0.2
|
||||
vlocation -noupdate g -rotate 0 0 0 0 0 1 10
|
||||
vsetmaterial -noupdate g glass
|
||||
vbsdf g -absorpcolor 0.8 1.0 0.8
|
||||
vbsdf g -absorpcoeff 6
|
||||
|
||||
# setup second inner sphere
|
||||
psphere r 0.1
|
||||
vdisplay r
|
||||
vsetmaterial r plastic
|
||||
vdisplay -noupdate r
|
||||
vsetmaterial -noupdate r plastic
|
||||
vbsdf r -kd 0.5 0.9 0.3 -ks 0.0 -kr 0.3 -n
|
||||
vbsdf r -fresnel Constant 1.0
|
||||
vsetlocation r 0.5 0.65 0.1
|
||||
vlocation r -setLocation 0.5 0.65 0.1
|
||||
|
||||
puts "Trying path tracing mode..."
|
||||
vrenderparams -ray -gi -rayDepth 8
|
@ -82,6 +82,12 @@ public:
|
||||
&& myFresnelData == theOther.myFresnelData;
|
||||
}
|
||||
|
||||
//! Returns type of Fresnel.
|
||||
Graphic3d_FresnelModel FresnelType() const
|
||||
{
|
||||
return myFresnelType;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
//! Creates new Fresnel reflectance factor.
|
||||
@ -164,9 +170,9 @@ public:
|
||||
public:
|
||||
|
||||
//! Creates uninitialized BSDF.
|
||||
Graphic3d_BSDF()
|
||||
Graphic3d_BSDF() : Roughness (1.f), AbsorptionCoeff (0.f)
|
||||
{
|
||||
Roughness = AbsorptionCoeff = 0.f;
|
||||
Fresnel = Graphic3d_Fresnel::CreateConstant (1.f);
|
||||
}
|
||||
|
||||
//! Normalizes BSDF components.
|
||||
|
@ -66,7 +66,7 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
mySpecularColor.SetValues (1.0, 1.0, 1.0, Quantity_TOC_RGB);
|
||||
myMaterialName = theName;
|
||||
|
||||
myBSDF = Graphic3d_BSDF::CreateDiffuse (Graphic3d_Vec3 (0.2f, 0.2f, 0.2f));
|
||||
myBSDF = Graphic3d_BSDF::CreateDiffuse (Graphic3d_Vec3 (0.0f));
|
||||
|
||||
Standard_Integer index = Standard_Integer (theName);
|
||||
if (index < NumberOfMaterials())
|
||||
@ -87,7 +87,7 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
static_cast<Standard_ShortReal> (myDiffuseColor.Blue()));
|
||||
myBSDF.Ks = Graphic3d_Vec3 (0.00784314f, 0.00784314f, 0.00784314f);
|
||||
myBSDF.Normalize();
|
||||
myBSDF.Roughness = 32;
|
||||
myBSDF.Roughness = 0.25f;
|
||||
break;
|
||||
case Graphic3d_NOM_SHINY_PLASTIC:
|
||||
myShininess = Standard_ShortReal (1.0);
|
||||
@ -98,9 +98,9 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
myBSDF.Kd = Graphic3d_Vec3 (static_cast<Standard_ShortReal> (myDiffuseColor.Red()),
|
||||
static_cast<Standard_ShortReal> (myDiffuseColor.Green()),
|
||||
static_cast<Standard_ShortReal> (myDiffuseColor.Blue()));
|
||||
myBSDF.Ks = Graphic3d_Vec3 (0.0156863f, 0.0156863f, 0.0156863f);
|
||||
myBSDF.Ks = Graphic3d_Vec3 (0.145f, 0.145f, 0.145f);
|
||||
myBSDF.Normalize();
|
||||
myBSDF.Roughness = 64.f;
|
||||
myBSDF.Roughness = 0.17f;
|
||||
break;
|
||||
case Graphic3d_NOM_SATIN :
|
||||
myShininess = Standard_ShortReal (0.09375);
|
||||
@ -108,12 +108,10 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
myDiffuseCoef = Standard_ShortReal (0.4);
|
||||
mySpecularCoef = Standard_ShortReal (0.44);
|
||||
|
||||
myBSDF.Kd = Graphic3d_Vec3 (static_cast<Standard_ShortReal> (myDiffuseColor.Red()),
|
||||
static_cast<Standard_ShortReal> (myDiffuseColor.Green()),
|
||||
static_cast<Standard_ShortReal> (myDiffuseColor.Blue()));
|
||||
myBSDF.Ks = Graphic3d_Vec3 (0.0313726f, 0.0313726f, 0.0313726f);
|
||||
myBSDF.Roughness = 16.f;
|
||||
myBSDF.Normalize();
|
||||
myBSDF.Kd = Graphic3d_Vec3 (0.2f);
|
||||
myBSDF.Ks = Graphic3d_Vec3 (0.6f);
|
||||
myBSDF.Roughness = 0.6f;
|
||||
|
||||
break;
|
||||
case Graphic3d_NOM_NEON_GNC:
|
||||
myShininess = Standard_ShortReal (0.05);
|
||||
@ -124,11 +122,12 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
myEmissiveActivity = Standard_True;
|
||||
myAmbientActivity = Standard_False;
|
||||
|
||||
myBSDF.Kr = Graphic3d_Vec3 (0.207843f, 0.207843f, 0.207843f);
|
||||
myBSDF.Kd = Graphic3d_Vec3 (0.0f);
|
||||
myBSDF.Kr = Graphic3d_Vec3 (0.5f);
|
||||
myBSDF.Le = Graphic3d_Vec3 (static_cast<Standard_ShortReal> (myDiffuseColor.Red()),
|
||||
static_cast<Standard_ShortReal> (myDiffuseColor.Green()),
|
||||
static_cast<Standard_ShortReal> (myDiffuseColor.Blue()));
|
||||
myBSDF.Fresnel = Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.3f, 0.3f, 0.3f));
|
||||
myBSDF.Fresnel = Graphic3d_Fresnel::CreateDielectric (1.5f);
|
||||
break;
|
||||
case Graphic3d_NOM_METALIZED:
|
||||
myShininess = Standard_ShortReal (0.13);
|
||||
@ -143,7 +142,7 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
static_cast<Standard_ShortReal> (myDiffuseColor.Blue()));
|
||||
|
||||
myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
|
||||
Graphic3d_Fresnel::CreateSchlick (aColor), 1024.f);
|
||||
Graphic3d_Fresnel::CreateSchlick (aColor), 0.045f);
|
||||
}
|
||||
break;
|
||||
// Ascending Compatibility physical materials. The same definition is taken as in the next constructor.
|
||||
@ -156,7 +155,7 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
mySpecularCoef = 1.00f;
|
||||
|
||||
myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
|
||||
Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.58f, 0.42f, 0.20f)), 1024.f);
|
||||
Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.58f, 0.42f, 0.20f)), 0.045f);
|
||||
|
||||
// Color resulting from ambient
|
||||
myAmbientColor .SetValues (0.329f, 0.224f, 0.027f, Quantity_TOC_RGB);
|
||||
@ -174,7 +173,7 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
mySpecularCoef = 1.00f;
|
||||
|
||||
myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
|
||||
Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.65f, 0.35f, 0.15f)), 1024.f);
|
||||
Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.65f, 0.35f, 0.15f)), 0.045f);
|
||||
|
||||
// Color resulting from ambient
|
||||
myAmbientColor .SetValues (0.213f, 0.128f, 0.054f, Quantity_TOC_RGB);
|
||||
@ -192,7 +191,7 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
mySpecularCoef = 1.00f;
|
||||
|
||||
myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
|
||||
Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.955008f, 0.637427f, 0.538163f)), 1024.f);
|
||||
Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.955008f, 0.637427f, 0.538163f)), 0.045f);
|
||||
|
||||
// Color resulting from ambient
|
||||
myAmbientColor .SetValues (0.191f, 0.074f, 0.023f, Quantity_TOC_RGB);
|
||||
@ -210,7 +209,7 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
mySpecularCoef = 1.00f;
|
||||
|
||||
myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
|
||||
Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (1.000000f, 0.765557f, 0.336057f)), 1024.f);
|
||||
Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (1.000000f, 0.765557f, 0.336057f)), 0.045f);
|
||||
|
||||
// Color resulting from ambient
|
||||
myAmbientColor .SetValues (0.300f, 0.230f, 0.095f, Quantity_TOC_RGB);
|
||||
@ -228,7 +227,7 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
mySpecularCoef = 1.00f;
|
||||
|
||||
myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
|
||||
Graphic3d_Fresnel::CreateConductor (1.8800f, 3.4900f), 1024.f);
|
||||
Graphic3d_Fresnel::CreateConductor (1.8800f, 3.4900f), 0.045f);
|
||||
|
||||
// Color resulting from ambient
|
||||
myAmbientColor .SetValues (0.106f, 0.059f, 0.114f, Quantity_TOC_RGB);
|
||||
@ -262,7 +261,7 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
mySpecularCoef = 1.00f;
|
||||
|
||||
myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
|
||||
Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.971519f, 0.959915f, 0.915324f)), 1024.f);
|
||||
Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.971519f, 0.959915f, 0.915324f)), 0.045f);
|
||||
|
||||
// Color resulting from ambient
|
||||
myAmbientColor .SetValues (0.275f, 0.275f, 0.250f, Quantity_TOC_RGB);
|
||||
@ -280,7 +279,7 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
mySpecularCoef = 1.00f;
|
||||
|
||||
myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
|
||||
Graphic3d_Fresnel::CreateConductor (Graphic3d_Vec3 (2.90f, 2.80f, 2.53f), Graphic3d_Vec3 (3.08f, 2.90f, 2.74f)), 1024.f);
|
||||
Graphic3d_Fresnel::CreateConductor (Graphic3d_Vec3 (2.90f, 2.80f, 2.53f), Graphic3d_Vec3 (3.08f, 2.90f, 2.74f)), 0.045f);
|
||||
|
||||
// Color resulting from ambient
|
||||
myAmbientColor .SetValues (0.150f, 0.150f, 0.180f, Quantity_TOC_RGB);
|
||||
@ -318,7 +317,7 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
mySpecularCoef = 1.00f;
|
||||
|
||||
myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
|
||||
Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.549585f, 0.556114f, 0.554256f)), 1024.f);
|
||||
Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.549585f, 0.556114f, 0.554256f)), 0.045f);
|
||||
|
||||
// Color resulting from ambient
|
||||
myAmbientColor .SetValues (0.200f, 0.200f, 0.225f, Quantity_TOC_RGB);
|
||||
@ -336,7 +335,7 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
mySpecularCoef = 1.00f;
|
||||
|
||||
myBSDF = Graphic3d_BSDF::CreateMetallic (Graphic3d_Vec3 (0.985f, 0.985f, 0.985f),
|
||||
Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.913183f, 0.921494f, 0.924524f)), 1024.f);
|
||||
Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.913183f, 0.921494f, 0.924524f)), 0.045f);
|
||||
|
||||
// Color resulting from ambient
|
||||
myAmbientColor .SetValues (0.300f, 0.300f, 0.300f, Quantity_TOC_RGB);
|
||||
@ -366,9 +365,10 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
// Color resulting from specular
|
||||
myEmissiveColor.SetValues (0.0, 1.0, 0.46, Quantity_TOC_RGB);
|
||||
|
||||
myBSDF.Kr = Graphic3d_Vec3 (0.207843f, 0.207843f, 0.207843f);
|
||||
myBSDF.Kd = Graphic3d_Vec3 (0.0f);
|
||||
myBSDF.Kr = Graphic3d_Vec3 (0.5f);
|
||||
myBSDF.Le = Graphic3d_Vec3 (0.0f, 1.0f, 0.46f);
|
||||
myBSDF.Fresnel = Graphic3d_Fresnel::CreateSchlick (Graphic3d_Vec3 (0.3f, 0.3f, 0.3f));
|
||||
myBSDF.Fresnel = Graphic3d_Fresnel::CreateDielectric (1.5f);
|
||||
break;
|
||||
case Graphic3d_NOM_OBSIDIAN:
|
||||
myMaterialType = Graphic3d_MATERIAL_PHYSIC;
|
||||
@ -385,9 +385,9 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
// Color resulting from specular
|
||||
mySpecularColor.SetValues (0.333f, 0.329f, 0.346f, Quantity_TOC_RGB);
|
||||
|
||||
myBSDF.Kd = Graphic3d_Vec3 (0.0156863f, 0.f, 0.0155017f);
|
||||
myBSDF.Kd = Graphic3d_Vec3 (0.023f, 0.f, 0.023f);
|
||||
myBSDF.Ks = Graphic3d_Vec3 (0.0156863f, 0.0156863f, 0.0156863f);
|
||||
myBSDF.Roughness = 1024.f;
|
||||
myBSDF.Roughness = 0.1f;
|
||||
break;
|
||||
case Graphic3d_NOM_JADE:
|
||||
myMaterialType = Graphic3d_MATERIAL_PHYSIC;
|
||||
@ -407,7 +407,7 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
myBSDF.Fresnel = Graphic3d_Fresnel::CreateDielectric (1.5f);
|
||||
myBSDF.Kd = Graphic3d_Vec3 (0.208658f, 0.415686f, 0.218401f);
|
||||
myBSDF.Ks = Graphic3d_Vec3 (0.611765f, 0.611765f, 0.611765f);
|
||||
myBSDF.Roughness = 512.f;
|
||||
myBSDF.Roughness = 0.06f;
|
||||
break;
|
||||
case Graphic3d_NOM_CHARCOAL:
|
||||
myMaterialType = Graphic3d_MATERIAL_PHYSIC;
|
||||
@ -424,9 +424,9 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
// Color resulting from specular
|
||||
mySpecularColor.SetValues (0.000f, 0.000f, 0.000f, Quantity_TOC_RGB);
|
||||
|
||||
myBSDF.Kd = Graphic3d_Vec3 (0.0196078f, 0.0196078f, 0.0196078f);
|
||||
myBSDF.Ks = Graphic3d_Vec3 (0.0196078f, 0.0196078f, 0.0196078f);
|
||||
myBSDF.Roughness = 8;
|
||||
myBSDF.Kd = Graphic3d_Vec3 (0.02f, 0.02f, 0.02f);
|
||||
myBSDF.Ks = Graphic3d_Vec3 (0.1f, 0.1f, 0.1f);
|
||||
myBSDF.Roughness = 0.3f;
|
||||
break;
|
||||
case Graphic3d_NOM_WATER:
|
||||
myMaterialType = Graphic3d_MATERIAL_PHYSIC;
|
||||
@ -491,6 +491,29 @@ void Graphic3d_MaterialAspect::Init (const Graphic3d_NameOfMaterial theName)
|
||||
// Color resulting from specular
|
||||
mySpecularColor.SetValues (0.970f, 0.970f, 0.970f, Quantity_TOC_RGB);
|
||||
break;
|
||||
|
||||
case Graphic3d_NOM_TRANSPARENT:
|
||||
myMaterialType = Graphic3d_MATERIAL_PHYSIC;
|
||||
|
||||
myShininess = 0.90f;
|
||||
myAmbientCoef = 1.00f;
|
||||
myDiffuseCoef = 1.00f;
|
||||
mySpecularCoef = 1.00f;
|
||||
myRefractionIndex = 1.0f;
|
||||
|
||||
myBSDF.Kd = Graphic3d_Vec3 (0.1f);
|
||||
myBSDF.Kt = Graphic3d_Vec3 (0.9f);
|
||||
myBSDF.Fresnel = Graphic3d_Fresnel::CreateConstant (0.0f);
|
||||
myTransparencyCoef = 0.80f;
|
||||
|
||||
// Color resulting from ambient
|
||||
myAmbientColor.SetValues (0.550f, 0.550f, 0.550f, Quantity_TOC_RGB);
|
||||
// Color resulting from dispersed
|
||||
myDiffuseColor.SetValues (0.100f, 0.100f, 0.100f, Quantity_TOC_RGB);
|
||||
// Color resulting from specular
|
||||
mySpecularColor.SetValues (0.970f, 0.970f, 0.970f, Quantity_TOC_RGB);
|
||||
break;
|
||||
|
||||
case Graphic3d_NOM_UserDefined:
|
||||
myStringName = "UserDefined";
|
||||
break;
|
||||
@ -1035,7 +1058,8 @@ namespace
|
||||
{"Charcoal", Graphic3d_MATERIAL_PHYSIC},
|
||||
{"Water", Graphic3d_MATERIAL_PHYSIC},
|
||||
{"Glass", Graphic3d_MATERIAL_PHYSIC},
|
||||
{"Diamond", Graphic3d_MATERIAL_PHYSIC}
|
||||
{"Diamond", Graphic3d_MATERIAL_PHYSIC},
|
||||
{"Transparent", Graphic3d_MATERIAL_PHYSIC}
|
||||
|
||||
};
|
||||
}
|
||||
|
@ -43,6 +43,7 @@ Graphic3d_NOM_CHARCOAL,
|
||||
Graphic3d_NOM_WATER,
|
||||
Graphic3d_NOM_GLASS,
|
||||
Graphic3d_NOM_DIAMOND,
|
||||
Graphic3d_NOM_TRANSPARENT,
|
||||
Graphic3d_NOM_DEFAULT,
|
||||
Graphic3d_NOM_UserDefined
|
||||
};
|
||||
|
@ -109,9 +109,9 @@ OpenGl_RaytraceMaterial::OpenGl_RaytraceMaterial (const BVH_Vec4f& theAmbient,
|
||||
// function : OpenGl_LightSource
|
||||
// purpose : Creates new light source
|
||||
// =======================================================================
|
||||
OpenGl_RaytraceLight::OpenGl_RaytraceLight (const BVH_Vec4f& theDiffuse,
|
||||
OpenGl_RaytraceLight::OpenGl_RaytraceLight (const BVH_Vec4f& theEmission,
|
||||
const BVH_Vec4f& thePosition)
|
||||
: Diffuse (theDiffuse),
|
||||
: Emission (theEmission),
|
||||
Position (thePosition)
|
||||
{
|
||||
//
|
||||
@ -207,7 +207,7 @@ OpenGl_TriangleSet::OpenGl_TriangleSet (const Standard_Size theArrayID)
|
||||
myArrayID (theArrayID)
|
||||
{
|
||||
myBuilder = new BVH_BinnedBuilder<Standard_ShortReal, 3 /* dim */, 48 /* bins */>
|
||||
(5 /* leaf size */, 32 /* max height */, Standard_False, OSD_Parallel::NbLogicalProcessors() + 1 /* threads */);
|
||||
(4 /* leaf size */, 32 /* max height */, Standard_False, OSD_Parallel::NbLogicalProcessors() + 1 /* threads */);
|
||||
}
|
||||
|
||||
// =======================================================================
|
||||
|
@ -136,7 +136,7 @@ class OpenGl_RaytraceLight
|
||||
public:
|
||||
|
||||
//! Diffuse intensity (in terms of OpenGL).
|
||||
BVH_Vec4f Diffuse;
|
||||
BVH_Vec4f Emission;
|
||||
|
||||
//! Position of light source (in terms of OpenGL).
|
||||
BVH_Vec4f Position;
|
||||
@ -144,7 +144,10 @@ public:
|
||||
public:
|
||||
|
||||
//! Creates new light source.
|
||||
OpenGl_RaytraceLight (const BVH_Vec4f& theDiffuse,
|
||||
OpenGl_RaytraceLight() { }
|
||||
|
||||
//! Creates new light source.
|
||||
OpenGl_RaytraceLight (const BVH_Vec4f& theEmission,
|
||||
const BVH_Vec4f& thePosition);
|
||||
|
||||
//! Returns packed (serialized) representation of light source.
|
||||
|
@ -591,17 +591,17 @@ protected: //! @name data types related to ray-tracing
|
||||
//! Defines OpenGL texture samplers.
|
||||
enum ShaderSamplerNames
|
||||
{
|
||||
OpenGl_RT_SceneNodeInfoTexture = 0,
|
||||
OpenGl_RT_SceneMinPointTexture = 1,
|
||||
OpenGl_RT_SceneMaxPointTexture = 2,
|
||||
OpenGl_RT_SceneTransformTexture = 3,
|
||||
OpenGl_RT_EnvironmentMapTexture = 0,
|
||||
|
||||
OpenGl_RT_GeometryVertexTexture = 4,
|
||||
OpenGl_RT_GeometryNormalTexture = 5,
|
||||
OpenGl_RT_GeometryTexCrdTexture = 6,
|
||||
OpenGl_RT_GeometryTriangTexture = 7,
|
||||
OpenGl_RT_SceneNodeInfoTexture = 1,
|
||||
OpenGl_RT_SceneMinPointTexture = 2,
|
||||
OpenGl_RT_SceneMaxPointTexture = 3,
|
||||
OpenGl_RT_SceneTransformTexture = 4,
|
||||
|
||||
OpenGl_RT_EnvironmentMapTexture = 8,
|
||||
OpenGl_RT_GeometryVertexTexture = 5,
|
||||
OpenGl_RT_GeometryNormalTexture = 6,
|
||||
OpenGl_RT_GeometryTexCrdTexture = 7,
|
||||
OpenGl_RT_GeometryTriangTexture = 8,
|
||||
|
||||
OpenGl_RT_RaytraceMaterialTexture = 9,
|
||||
OpenGl_RT_RaytraceLightSrcTexture = 10,
|
||||
@ -747,9 +747,6 @@ protected: //! @name methods related to ray-tracing
|
||||
//! Updates 3D scene light sources for ray-tracing.
|
||||
Standard_Boolean updateRaytraceLightSources (const OpenGl_Mat4& theInvModelView, const Handle(OpenGl_Context)& theGlContext);
|
||||
|
||||
//! Updates environment map for ray-tracing.
|
||||
Standard_Boolean updateRaytraceEnvironmentMap (const Handle(OpenGl_Context)& theGlContext);
|
||||
|
||||
//! Checks to see if the OpenGL structure is modified.
|
||||
Standard_Boolean toUpdateStructure (const OpenGl_Structure* theStructure);
|
||||
|
||||
|
@ -42,6 +42,24 @@ namespace
|
||||
{
|
||||
static const OpenGl_Vec4 THE_WHITE_COLOR (1.0f, 1.0f, 1.0f, 1.0f);
|
||||
static const OpenGl_Vec4 THE_BLACK_COLOR (0.0f, 0.0f, 0.0f, 1.0f);
|
||||
|
||||
//! Operator returning TRUE for positional light sources.
|
||||
struct IsLightPositional
|
||||
{
|
||||
bool operator() (const OpenGl_Light& theLight)
|
||||
{
|
||||
return theLight.Type != Graphic3d_TOLS_DIRECTIONAL;
|
||||
}
|
||||
};
|
||||
|
||||
//! Operator returning TRUE for any non-ambient light sources.
|
||||
struct IsNotAmbient
|
||||
{
|
||||
bool operator() (const OpenGl_Light& theLight)
|
||||
{
|
||||
return theLight.Type != Graphic3d_TOLS_AMBIENT;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// =======================================================================
|
||||
@ -1402,10 +1420,20 @@ Standard_Boolean OpenGl_View::initRaytraceResources (const Handle(OpenGl_Context
|
||||
return safeFailBack ("Failed to load source into ray-tracing fragment shaders", theGlContext);
|
||||
}
|
||||
|
||||
TCollection_AsciiString aLog;
|
||||
|
||||
if (!myRaytraceShader->Compile (theGlContext)
|
||||
|| !myPostFSAAShader->Compile (theGlContext)
|
||||
|| !myOutImageShader->Compile (theGlContext))
|
||||
{
|
||||
#ifdef RAY_TRACE_PRINT_INFO
|
||||
myRaytraceShader->FetchInfoLog (theGlContext, aLog);
|
||||
|
||||
if (!aLog.IsEmpty())
|
||||
{
|
||||
std::cout << "Failed to compile ray-tracing shader: " << aLog << "\n";
|
||||
}
|
||||
#endif
|
||||
return safeFailBack ("Failed to compile ray-tracing fragment shaders", theGlContext);
|
||||
}
|
||||
|
||||
@ -1417,6 +1445,14 @@ Standard_Boolean OpenGl_View::initRaytraceResources (const Handle(OpenGl_Context
|
||||
|| !myPostFSAAProgram->Link (theGlContext)
|
||||
|| !myOutImageProgram->Link (theGlContext))
|
||||
{
|
||||
#ifdef RAY_TRACE_PRINT_INFO
|
||||
myRaytraceProgram->FetchInfoLog (theGlContext, aLog);
|
||||
|
||||
if (!aLog.IsEmpty())
|
||||
{
|
||||
std::cout << "Failed to compile ray-tracing shader: " << aLog << "\n";
|
||||
}
|
||||
#endif
|
||||
return safeFailBack ("Failed to initialize vertex attributes for ray-tracing program", theGlContext);
|
||||
}
|
||||
}
|
||||
@ -1424,7 +1460,7 @@ Standard_Boolean OpenGl_View::initRaytraceResources (const Handle(OpenGl_Context
|
||||
|
||||
if (myRaytraceInitStatus == OpenGl_RT_NONE)
|
||||
{
|
||||
myAccumFrames = 0; // reject accumulated frames
|
||||
myAccumFrames = 0; // accumulation should be restarted
|
||||
|
||||
if (!theGlContext->IsGlGreaterEqual (3, 1))
|
||||
{
|
||||
@ -2266,14 +2302,31 @@ Standard_Boolean OpenGl_View::uploadRaytraceData (const Handle(OpenGl_Context)&
|
||||
// =======================================================================
|
||||
Standard_Boolean OpenGl_View::updateRaytraceLightSources (const OpenGl_Mat4& theInvModelView, const Handle(OpenGl_Context)& theGlContext)
|
||||
{
|
||||
myRaytraceGeometry.Sources.clear();
|
||||
std::vector<OpenGl_Light> aLightSources;
|
||||
|
||||
myRaytraceGeometry.Ambient = BVH_Vec4f (0.0f, 0.0f, 0.0f, 0.0f);
|
||||
|
||||
OpenGl_ListOfLight::Iterator aLightIter (myShadingModel == Graphic3d_TOSM_NONE ? myNoShadingLight : myLights);
|
||||
for (; aLightIter.More(); aLightIter.Next())
|
||||
if (myShadingModel != Graphic3d_TOSM_NONE)
|
||||
{
|
||||
const OpenGl_Light& aLight = aLightIter.Value();
|
||||
aLightSources.assign (myLights.begin(), myLights.end());
|
||||
|
||||
// move positional light sources at the front of the list
|
||||
std::partition (aLightSources.begin(), aLightSources.end(), IsLightPositional());
|
||||
}
|
||||
|
||||
// get number of 'real' (not ambient) light sources
|
||||
const size_t aNbLights = std::count_if (aLightSources.begin(), aLightSources.end(), IsNotAmbient());
|
||||
|
||||
Standard_Boolean wasUpdated = myRaytraceGeometry.Sources.size () != aNbLights;
|
||||
|
||||
if (wasUpdated)
|
||||
{
|
||||
myRaytraceGeometry.Sources.resize (aNbLights);
|
||||
}
|
||||
|
||||
myRaytraceGeometry.Ambient = BVH_Vec4f (0.f, 0.f, 0.f, 0.f);
|
||||
|
||||
for (size_t aLightIdx = 0, aRealIdx = 0; aLightIdx < aLightSources.size(); ++aLightIdx)
|
||||
{
|
||||
const OpenGl_Light& aLight = aLightSources[aLightIdx];
|
||||
|
||||
if (aLight.Type == Graphic3d_TOLS_AMBIENT)
|
||||
{
|
||||
@ -2284,10 +2337,10 @@ Standard_Boolean OpenGl_View::updateRaytraceLightSources (const OpenGl_Mat4& the
|
||||
continue;
|
||||
}
|
||||
|
||||
BVH_Vec4f aDiffuse (aLight.Color.r() * aLight.Intensity,
|
||||
aLight.Color.g() * aLight.Intensity,
|
||||
aLight.Color.b() * aLight.Intensity,
|
||||
1.0f);
|
||||
BVH_Vec4f aEmission (aLight.Color.r() * aLight.Intensity,
|
||||
aLight.Color.g() * aLight.Intensity,
|
||||
aLight.Color.b() * aLight.Intensity,
|
||||
1.0f);
|
||||
|
||||
BVH_Vec4f aPosition (-aLight.Direction.x(),
|
||||
-aLight.Direction.y(),
|
||||
@ -2301,13 +2354,13 @@ Standard_Boolean OpenGl_View::updateRaytraceLightSources (const OpenGl_Mat4& the
|
||||
static_cast<float>(aLight.Position.z()),
|
||||
1.0f);
|
||||
|
||||
// store smoothing radius in w-component
|
||||
aDiffuse.w() = Max (aLight.Smoothness, 0.f);
|
||||
// store smoothing radius in W-component
|
||||
aEmission.w() = Max (aLight.Smoothness, 0.f);
|
||||
}
|
||||
else
|
||||
{
|
||||
// store cosine of smoothing angle in w-component
|
||||
aDiffuse.w() = cosf (Min (Max (aLight.Smoothness, 0.f), static_cast<Standard_ShortReal> (M_PI / 2.0)));
|
||||
// store cosine of smoothing angle in W-component
|
||||
aEmission.w() = cosf (Min (Max (aLight.Smoothness, 0.f), static_cast<Standard_ShortReal> (M_PI / 2.0)));
|
||||
}
|
||||
|
||||
if (aLight.IsHeadlight)
|
||||
@ -2315,23 +2368,26 @@ Standard_Boolean OpenGl_View::updateRaytraceLightSources (const OpenGl_Mat4& the
|
||||
aPosition = theInvModelView * aPosition;
|
||||
}
|
||||
|
||||
myRaytraceGeometry.Sources.push_back (OpenGl_RaytraceLight (aDiffuse, aPosition));
|
||||
for (int aK = 0; aK < 4; ++aK)
|
||||
{
|
||||
wasUpdated |= (aEmission[aK] != myRaytraceGeometry.Sources[aRealIdx].Emission[aK])
|
||||
|| (aPosition[aK] != myRaytraceGeometry.Sources[aRealIdx].Position[aK]);
|
||||
}
|
||||
|
||||
if (wasUpdated)
|
||||
{
|
||||
myRaytraceGeometry.Sources[aRealIdx] = OpenGl_RaytraceLight (aEmission, aPosition);
|
||||
}
|
||||
|
||||
++aRealIdx;
|
||||
}
|
||||
|
||||
if (myRaytraceLightSrcTexture.IsNull()) // create light source buffer
|
||||
if (myRaytraceLightSrcTexture.IsNull()) // create light source buffer
|
||||
{
|
||||
myRaytraceLightSrcTexture = new OpenGl_TextureBufferArb;
|
||||
|
||||
if (!myRaytraceLightSrcTexture->Create (theGlContext))
|
||||
{
|
||||
#ifdef RAY_TRACE_PRINT_INFO
|
||||
std::cout << "Error: Failed to create light source buffer" << std::endl;
|
||||
#endif
|
||||
return Standard_False;
|
||||
}
|
||||
}
|
||||
|
||||
if (myRaytraceGeometry.Sources.size() != 0)
|
||||
if (myRaytraceGeometry.Sources.size() != 0 && wasUpdated)
|
||||
{
|
||||
const GLfloat* aDataPtr = myRaytraceGeometry.Sources.front().Packed();
|
||||
if (!myRaytraceLightSrcTexture->Init (theGlContext, 4, GLsizei (myRaytraceGeometry.Sources.size() * 2), aDataPtr))
|
||||
@ -2341,56 +2397,13 @@ Standard_Boolean OpenGl_View::updateRaytraceLightSources (const OpenGl_Mat4& the
|
||||
#endif
|
||||
return Standard_False;
|
||||
}
|
||||
|
||||
myAccumFrames = 0; // accumulation should be restarted
|
||||
}
|
||||
|
||||
return Standard_True;
|
||||
}
|
||||
|
||||
// =======================================================================
|
||||
// function : updateRaytraceEnvironmentMap
|
||||
// purpose : Updates environment map for ray-tracing
|
||||
// =======================================================================
|
||||
Standard_Boolean OpenGl_View::updateRaytraceEnvironmentMap (const Handle(OpenGl_Context)& theGlContext)
|
||||
{
|
||||
Standard_Boolean aResult = Standard_True;
|
||||
|
||||
if (!myToUpdateEnvironmentMap)
|
||||
{
|
||||
return aResult;
|
||||
}
|
||||
|
||||
Handle(OpenGl_ShaderProgram) aPrograms[] = { myRaytraceProgram,
|
||||
myPostFSAAProgram };
|
||||
|
||||
for (Standard_Integer anIdx = 0; anIdx < 2; ++anIdx)
|
||||
{
|
||||
if (!aPrograms[anIdx].IsNull())
|
||||
{
|
||||
aResult &= theGlContext->BindProgram (aPrograms[anIdx]);
|
||||
|
||||
if (!myTextureEnv.IsNull())
|
||||
{
|
||||
myTextureEnv->Bind (theGlContext,
|
||||
GL_TEXTURE0 + OpenGl_RT_EnvironmentMapTexture);
|
||||
|
||||
aResult &= aPrograms[anIdx]->SetUniform (theGlContext,
|
||||
myUniformLocations[anIdx][OpenGl_RT_uSphereMapEnabled], 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
aResult &= aPrograms[anIdx]->SetUniform (theGlContext,
|
||||
myUniformLocations[anIdx][OpenGl_RT_uSphereMapEnabled], 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
myToUpdateEnvironmentMap = Standard_False;
|
||||
|
||||
theGlContext->BindProgram (NULL);
|
||||
|
||||
return aResult;
|
||||
}
|
||||
|
||||
// =======================================================================
|
||||
// function : setUniformState
|
||||
// purpose : Sets uniform state for the given ray-tracing shader program
|
||||
@ -2461,7 +2474,7 @@ Standard_Boolean OpenGl_View::setUniformState (const Standard_Integer the
|
||||
theProgram->SetUniform (theGlContext,
|
||||
myUniformLocations[theProgramId][OpenGl_RT_uLightAmbnt], myRaytraceGeometry.Ambient);
|
||||
|
||||
// Enable/disable run time rendering effects
|
||||
// Enable/disable run-time rendering effects
|
||||
theProgram->SetUniform (theGlContext,
|
||||
myUniformLocations[theProgramId][OpenGl_RT_uShadowsEnabled], myRenderParams.IsShadowEnabled ? 1 : 0);
|
||||
theProgram->SetUniform (theGlContext,
|
||||
@ -2506,6 +2519,11 @@ Standard_Boolean OpenGl_View::setUniformState (const Standard_Integer the
|
||||
myUniformLocations[theProgramId][OpenGl_RT_uBackColorBot], aBackColor);
|
||||
}
|
||||
|
||||
const Standard_Boolean toDisableEnvironmentMap = myTextureEnv.IsNull() || !myTextureEnv->IsValid();
|
||||
|
||||
theProgram->SetUniform (theGlContext,
|
||||
myUniformLocations[theProgramId][OpenGl_RT_uSphereMapEnabled], toDisableEnvironmentMap ? 0 : 1);
|
||||
|
||||
theProgram->SetUniform (theGlContext,
|
||||
myUniformLocations[theProgramId][OpenGl_RT_uSphereMapForBack], myRenderParams.UseEnvironmentMapBackground ? 1 : 0);
|
||||
|
||||
@ -2533,6 +2551,11 @@ void OpenGl_View::bindRaytraceTextures (const Handle(OpenGl_Context)& theGlConte
|
||||
#endif
|
||||
}
|
||||
|
||||
if (!myTextureEnv.IsNull() && myTextureEnv->IsValid())
|
||||
{
|
||||
myTextureEnv->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_EnvironmentMapTexture);
|
||||
}
|
||||
|
||||
mySceneMinPointTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMinPointTexture);
|
||||
mySceneMaxPointTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMaxPointTexture);
|
||||
mySceneNodeInfoTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneNodeInfoTexture);
|
||||
@ -2731,6 +2754,11 @@ Standard_Boolean OpenGl_View::runPathtrace (const Graphic3d_Camera::Projection
|
||||
{
|
||||
Standard_Boolean aResult = Standard_True;
|
||||
|
||||
if (myToUpdateEnvironmentMap) // check whether the map was changed
|
||||
{
|
||||
myAccumFrames = myToUpdateEnvironmentMap = 0;
|
||||
}
|
||||
|
||||
if (myRaytraceParameters.AdaptiveScreenSampling)
|
||||
{
|
||||
if (myAccumFrames == 0)
|
||||
@ -2875,11 +2903,6 @@ Standard_Boolean OpenGl_View::raytrace (const Standard_Integer theSizeX,
|
||||
return Standard_False;
|
||||
}
|
||||
|
||||
if (!updateRaytraceEnvironmentMap (theGlContext))
|
||||
{
|
||||
return Standard_False;
|
||||
}
|
||||
|
||||
OpenGl_Mat4 aLightSourceMatrix;
|
||||
|
||||
// Get inversed model-view matrix for transforming lights
|
||||
|
@ -1,3 +1,15 @@
|
||||
#ifdef _MSC_VER
|
||||
#define PATH_TRACING // just for editing in MS VS
|
||||
|
||||
#define in
|
||||
#define out
|
||||
#define inout
|
||||
|
||||
typedef struct { float x; float y; } vec2;
|
||||
typedef struct { float x; float y; float z; } vec3;
|
||||
typedef struct { float x; float y; float z; float w; } vec4;
|
||||
#endif
|
||||
|
||||
#ifdef PATH_TRACING
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////
|
||||
@ -42,13 +54,13 @@ struct SMaterial
|
||||
// Support subroutines
|
||||
|
||||
//=======================================================================
|
||||
// function : LocalSpace
|
||||
// function : buildLocalSpace
|
||||
// purpose : Generates local space for the given normal
|
||||
//=======================================================================
|
||||
SLocalSpace LocalSpace (in vec3 theNormal)
|
||||
SLocalSpace buildLocalSpace (in vec3 theNormal)
|
||||
{
|
||||
vec3 anAxisX = cross (vec3 (0.f, 1.f, 0.f), theNormal);
|
||||
vec3 anAxisY = cross (vec3 (1.f, 0.f, 0.f), theNormal);
|
||||
vec3 anAxisX = vec3 (theNormal.z, 0.f, -theNormal.x);
|
||||
vec3 anAxisY = vec3 (0.f, -theNormal.z, theNormal.y);
|
||||
|
||||
float aSqrLenX = dot (anAxisX, anAxisX);
|
||||
float aSqrLenY = dot (anAxisY, anAxisY);
|
||||
@ -98,19 +110,6 @@ float convolve (in vec3 theVector, in vec3 theFactor)
|
||||
return dot (theVector, theFactor) * (1.f / max (theFactor.x + theFactor.y + theFactor.z, 1e-15f));
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : sphericalDirection
|
||||
// purpose : Constructs vector from spherical coordinates
|
||||
//=======================================================================
|
||||
vec3 sphericalDirection (in float theCosTheta, in float thePhi)
|
||||
{
|
||||
float aSinTheta = sqrt (1.f - theCosTheta * theCosTheta);
|
||||
|
||||
return vec3 (aSinTheta * cos (thePhi),
|
||||
aSinTheta * sin (thePhi),
|
||||
theCosTheta);
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : fresnelSchlick
|
||||
// purpose : Computes the Fresnel reflection formula using
|
||||
@ -196,24 +195,24 @@ float fresnelConductor (in float theCosI, in float theEta, in float theK)
|
||||
// purpose : Computes the Fresnel reflection formula for general medium
|
||||
// in case of circularly polarized light.
|
||||
//=======================================================================
|
||||
vec3 fresnelMedia (in float theCosI, in vec3 theFresnelCoeffs)
|
||||
vec3 fresnelMedia (in float theCosI, in vec3 theFresnel)
|
||||
{
|
||||
if (theFresnelCoeffs.x > FRESNEL_SCHLICK)
|
||||
if (theFresnel.x > FRESNEL_SCHLICK)
|
||||
{
|
||||
return fresnelSchlick (abs (theCosI), theFresnelCoeffs);
|
||||
return fresnelSchlick (abs (theCosI), theFresnel);
|
||||
}
|
||||
|
||||
if (theFresnelCoeffs.x > FRESNEL_CONSTANT)
|
||||
if (theFresnel.x > FRESNEL_CONSTANT)
|
||||
{
|
||||
return vec3 (theFresnelCoeffs.z);
|
||||
return vec3 (theFresnel.z);
|
||||
}
|
||||
|
||||
if (theFresnelCoeffs.x > FRESNEL_CONDUCTOR)
|
||||
if (theFresnel.x > FRESNEL_CONDUCTOR)
|
||||
{
|
||||
return vec3 (fresnelConductor (abs (theCosI), theFresnelCoeffs.y, theFresnelCoeffs.z));
|
||||
return vec3 (fresnelConductor (abs (theCosI), theFresnel.y, theFresnel.z));
|
||||
}
|
||||
|
||||
return vec3 (fresnelDielectric (theCosI, theFresnelCoeffs.y));
|
||||
return vec3 (fresnelDielectric (theCosI, theFresnel.y));
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
@ -226,11 +225,11 @@ void transmitted (in float theIndex, in vec3 theIncident, out vec3 theTransmit)
|
||||
// Compute relative index of refraction
|
||||
float anEta = (theIncident.z > 0.f) ? 1.f / theIndex : theIndex;
|
||||
|
||||
// Handle total internal reflection for transmission
|
||||
// Handle total internal reflection (TIR)
|
||||
float aSinT2 = anEta * anEta * (1.f - theIncident.z * theIncident.z);
|
||||
|
||||
// Compute transmitted ray direction
|
||||
float aCosT = sqrt (1.f - min (aSinT2, 1.f)) * (theIncident.z > 0.f ? -1.f : 1.f);
|
||||
// Compute direction of transmitted ray
|
||||
float aCosT = sqrt (1.f - min (aSinT2, 1.f)) * sign (-theIncident.z);
|
||||
|
||||
theTransmit = normalize (vec3 (-anEta * theIncident.x,
|
||||
-anEta * theIncident.y,
|
||||
@ -242,145 +241,101 @@ void transmitted (in float theIndex, in vec3 theIncident, out vec3 theTransmit)
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//=======================================================================
|
||||
// function : handleLambertianReflection
|
||||
// function : HandleLambertianReflection
|
||||
// purpose : Handles Lambertian BRDF, with cos(N, PSI)
|
||||
//=======================================================================
|
||||
float handleLambertianReflection (in vec3 theInput, in vec3 theOutput)
|
||||
float HandleLambertianReflection (in vec3 theInput, in vec3 theOutput)
|
||||
{
|
||||
return max (0.f, theInput.z) * (1.f / M_PI);
|
||||
return (theInput.z <= 0.f || theOutput.z <= 0.f) ? 0.f : theInput.z * (1.f / M_PI);
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : handleBlinnReflection
|
||||
// function : SmithG1
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
float SmithG1 (in vec3 theDirection, in vec3 theM, in float theRoughness)
|
||||
{
|
||||
if (dot (theDirection, theM) * theDirection.z <= 0.f)
|
||||
{
|
||||
return 0.f;
|
||||
}
|
||||
|
||||
float aTanThetaM = sqrt (1.f - theDirection.z * theDirection.z) / theDirection.z;
|
||||
|
||||
if (aTanThetaM == 0.0f)
|
||||
{
|
||||
return 1.f;
|
||||
}
|
||||
|
||||
float aVal = 1.f / (theRoughness * aTanThetaM);
|
||||
|
||||
if (aVal >= 1.6f)
|
||||
{
|
||||
return 1.f;
|
||||
}
|
||||
|
||||
// Use fast and accurate rational approximation to the
|
||||
// shadowing-masking function (from Mitsuba renderer)
|
||||
float aSqr = aVal * aVal;
|
||||
|
||||
return (3.535f * aVal + 2.181f * aSqr) / (1.f + 2.276f * aVal + 2.577f * aSqr);
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : HandleBlinnReflection
|
||||
// purpose : Handles Blinn glossy BRDF, with cos(N, PSI)
|
||||
//=======================================================================
|
||||
vec3 handleBlinnReflection (in vec3 theInput, in vec3 theOutput, in vec3 theFresnelCoeffs, in float theExponent)
|
||||
vec3 HandleBlinnReflection (in vec3 theInput, in vec3 theOutput, in vec3 theFresnel, in float theRoughness)
|
||||
{
|
||||
vec3 aWeight = ZERO;
|
||||
// calculate the reflection half-vec
|
||||
vec3 aH = normalize (theInput + theOutput);
|
||||
|
||||
// Compute half-angle vector
|
||||
vec3 aHalf = theInput + theOutput;
|
||||
// roughness value -> Blinn exponent
|
||||
float aPower = max (2.f / (theRoughness * theRoughness) - 2.f, 0.f);
|
||||
|
||||
if (aHalf.z < 0.f)
|
||||
aHalf = -aHalf;
|
||||
// calculate microfacet distribution
|
||||
float aD = (aPower + 2.f) * (1.f / M_2_PI) * pow (aH.z, aPower);
|
||||
|
||||
float aLength = dot (aHalf, aHalf);
|
||||
// calculate shadow-masking function
|
||||
float aG = SmithG1 (theOutput, aH, theRoughness) * SmithG1 (theInput, aH, theRoughness);
|
||||
|
||||
if (aLength <= 0.f)
|
||||
return ZERO;
|
||||
|
||||
aHalf *= inversesqrt (aLength);
|
||||
|
||||
// Compute Fresnel reflectance
|
||||
float aCosDelta = dot (theOutput, aHalf);
|
||||
|
||||
vec3 aFresnel = fresnelMedia (aCosDelta, theFresnelCoeffs);
|
||||
|
||||
// Compute fraction of microfacets that reflect light
|
||||
float aCosThetaH = max (0.f, aHalf.z);
|
||||
|
||||
float aFraction = (theExponent + 2.f) * (M_PI / 2.f) * pow (aCosThetaH, theExponent);
|
||||
|
||||
// Compute geometry attenuation term (already includes cos)
|
||||
float aCosThetaI = max (0.f, theInput.z);
|
||||
float aCosThetaO = max (0.f, theOutput.z);
|
||||
|
||||
float aGeom = min (1.f, 2.f * aCosThetaH / max (0.f, aCosDelta) * min (aCosThetaO, aCosThetaI));
|
||||
|
||||
return aCosThetaO < 1.0e-3f ? ZERO :
|
||||
aFraction * aGeom / (4.f * aCosThetaO) * aFresnel;
|
||||
// return total amount of reflection
|
||||
return (theInput.z <= 0.f || theOutput.z <= 0.f) ? ZERO :
|
||||
aD * aG / (4.f * theOutput.z) * fresnelMedia (dot (theOutput, aH), theFresnel);
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : handleMaterial
|
||||
// function : HandleMaterial
|
||||
// purpose : Returns BSDF value for specified material, with cos(N, PSI)
|
||||
//=======================================================================
|
||||
vec3 handleMaterial (in SMaterial theMaterial, in vec3 theInput, in vec3 theOutput)
|
||||
vec3 HandleMaterial (in SMaterial theBSDF, in vec3 theInput, in vec3 theOutput)
|
||||
{
|
||||
return theMaterial.Kd.rgb * handleLambertianReflection (theInput, theOutput) +
|
||||
theMaterial.Ks.rgb * handleBlinnReflection (theInput, theOutput, theMaterial.Fresnel, theMaterial.Ks.w);
|
||||
return theBSDF.Kd.rgb * HandleLambertianReflection (theInput, theOutput) +
|
||||
theBSDF.Ks.rgb * HandleBlinnReflection (theInput, theOutput, theBSDF.Fresnel, theBSDF.Ks.w);
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : sampleLambertianReflection
|
||||
// function : SampleLambertianReflection
|
||||
// purpose : Samples Lambertian BRDF, W = BRDF * cos(N, PSI) / PDF(PSI)
|
||||
//=======================================================================
|
||||
void sampleLambertianReflection (in vec3 theOutput, out vec3 theInput)
|
||||
vec3 SampleLambertianReflection (in vec3 theOutput, out vec3 theInput, inout float thePDF)
|
||||
{
|
||||
float aKsi1 = RandFloat();
|
||||
float aKsi2 = RandFloat();
|
||||
|
||||
float aTemp = sqrt (aKsi2);
|
||||
|
||||
theInput = vec3 (aTemp * cos (2.f * M_PI * aKsi1),
|
||||
aTemp * sin (2.f * M_PI * aKsi1),
|
||||
theInput = vec3 (aTemp * cos (M_2_PI * aKsi1),
|
||||
aTemp * sin (M_2_PI * aKsi1),
|
||||
sqrt (1.f - aKsi2));
|
||||
|
||||
theInput.z = mix (-theInput.z, theInput.z, step (0.f, theOutput.z));
|
||||
}
|
||||
thePDF *= abs (theInput.z) * (1.f / M_PI);
|
||||
|
||||
// Types of bounces
|
||||
#define NON_SPECULAR_BOUNCE 0
|
||||
#define SPEC_REFLECT_BOUNCE 1
|
||||
#define SPEC_REFRACT_BOUNCE 2
|
||||
|
||||
#define IS_NON_SPEC_BOUNCE(theBounce) (theBounce == 0)
|
||||
#define IS_ANY_SPEC_BOUNCE(theBounce) (theBounce != 0)
|
||||
#define IS_REFL_SPEC_BOUNCE(theBounce) (theBounce == 1)
|
||||
#define IS_REFR_SPEC_BOUNCE(theBounce) (theBounce == 2)
|
||||
|
||||
//=======================================================================
|
||||
// function : sampleSpecularTransmission
|
||||
// purpose : Samples specular BTDF, W = BRDF * cos(N, PSI) / PDF(PSI)
|
||||
//=======================================================================
|
||||
vec3 sampleSpecularTransmission (in vec3 theOutput, out vec3 theInput,
|
||||
out int theBounce, in vec3 theWeight, in vec3 theFresnelCoeffs)
|
||||
{
|
||||
vec3 aFresnel = fresnelMedia (theOutput.z, theFresnelCoeffs);
|
||||
|
||||
float aProbability = convolve (aFresnel, theWeight);
|
||||
|
||||
// Check if transmission takes place
|
||||
theBounce = RandFloat() <= aProbability ?
|
||||
SPEC_REFLECT_BOUNCE : SPEC_REFRACT_BOUNCE;
|
||||
|
||||
// Sample input direction
|
||||
if (theBounce == SPEC_REFLECT_BOUNCE)
|
||||
{
|
||||
theInput = vec3 (-theOutput.x,
|
||||
-theOutput.y,
|
||||
theOutput.z);
|
||||
|
||||
theWeight = aFresnel * (1.f / aProbability);
|
||||
}
|
||||
else
|
||||
{
|
||||
transmitted (theFresnelCoeffs.y, theOutput, theInput);
|
||||
|
||||
theWeight = (UNIT - aFresnel) * (1.f / (1.f - aProbability));
|
||||
}
|
||||
|
||||
return theWeight;
|
||||
return (theInput.z <= 0.f || theOutput.z <= 0.f) ? ZERO : UNIT;
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : sampleSpecularReflection
|
||||
// purpose : Samples specular BRDF, W = BRDF * cos(N, PSI) / PDF(PSI)
|
||||
//=======================================================================
|
||||
vec3 sampleSpecularReflection (in vec3 theOutput, out vec3 theInput, in vec3 theFresnelCoeffs)
|
||||
{
|
||||
// Sample input direction
|
||||
theInput = vec3 (-theOutput.x,
|
||||
-theOutput.y,
|
||||
theOutput.z);
|
||||
|
||||
return fresnelMedia (theOutput.z, theFresnelCoeffs);
|
||||
}
|
||||
|
||||
#define MIN_COS 1.0e-20f
|
||||
|
||||
//=======================================================================
|
||||
// function : sampleBlinnReflection
|
||||
// function : SampleBlinnReflection
|
||||
// purpose : Samples Blinn BRDF, W = BRDF * cos(N, PSI) / PDF(PSI)
|
||||
// The BRDF is a product of three main terms, D, G, and F,
|
||||
// which is then divided by two cosine terms. Here we perform
|
||||
@ -389,155 +344,191 @@ vec3 sampleSpecularReflection (in vec3 theOutput, out vec3 theInput, in vec3 the
|
||||
// terms would be complex, and it is the D term that accounts
|
||||
// for most of the variation.
|
||||
//=======================================================================
|
||||
vec3 sampleBlinnReflection (in vec3 theOutput, out vec3 theInput, in vec3 theFresnelCoeffs, in float theExponent)
|
||||
vec3 SampleBlinnReflection (in vec3 theOutput, out vec3 theInput, in vec3 theFresnel, in float theRoughness, inout float thePDF)
|
||||
{
|
||||
vec3 aWeight = ZERO;
|
||||
|
||||
// Generate two random variables
|
||||
float aKsi1 = RandFloat();
|
||||
float aKsi2 = RandFloat();
|
||||
|
||||
// Compute sampled half-angle vector for Blinn distribution
|
||||
float aCosThetaH = pow (aKsi1, 1.f / (theExponent + 1.f));
|
||||
// roughness value --> Blinn exponent
|
||||
float aPower = max (2.f / (theRoughness * theRoughness) - 2.f, 0.f);
|
||||
|
||||
vec3 aHalf = sphericalDirection (aCosThetaH, aKsi2 * 2.f * M_PI);
|
||||
// normal from microface distribution
|
||||
float aCosThetaM = pow (aKsi1, 1.f / (aPower + 2.f));
|
||||
|
||||
if (aHalf.z < 0)
|
||||
{
|
||||
aHalf = -aHalf;
|
||||
}
|
||||
vec3 aM = vec3 (cos (M_2_PI * aKsi2),
|
||||
sin (M_2_PI * aKsi2),
|
||||
aCosThetaM);
|
||||
|
||||
// Compute incident direction by reflecting about half-vector
|
||||
float aCosDelta = dot (theOutput, aHalf);
|
||||
aM.xy *= sqrt (1.f - aCosThetaM * aCosThetaM);
|
||||
|
||||
vec3 anInput = 2.f * aCosDelta * aHalf - theOutput;
|
||||
// calculate PDF of sampled direction
|
||||
thePDF *= (aPower + 2.f) * (1.f / M_2_PI) * pow (aCosThetaM, aPower + 1.f);
|
||||
|
||||
if (theOutput.z * anInput.z <= 0.f)
|
||||
float aCosDelta = dot (theOutput, aM);
|
||||
|
||||
// pick input based on half direction
|
||||
theInput = -theOutput + 2.f * aCosDelta * aM;
|
||||
|
||||
if (theInput.z <= 0.f || theOutput.z <= 0.f)
|
||||
{
|
||||
return ZERO;
|
||||
}
|
||||
|
||||
theInput = anInput;
|
||||
// Jacobian of half-direction mapping
|
||||
thePDF /= 4.f * dot (theInput, aM);
|
||||
|
||||
// Compute Fresnel reflectance
|
||||
vec3 aFresnel = fresnelMedia (aCosDelta, theFresnelCoeffs);
|
||||
// compute shadow-masking coefficient
|
||||
float aG = SmithG1 (theOutput, aM, theRoughness) * SmithG1 (theInput, aM, theRoughness);
|
||||
|
||||
// Compute geometry attenuation term
|
||||
float aCosThetaI = max (MIN_COS, theInput.z);
|
||||
float aCosThetaO = max (MIN_COS, theOutput.z);
|
||||
|
||||
float aGeom = min (max (MIN_COS, aCosDelta), 2.f * aCosThetaH * min (aCosThetaO, aCosThetaI));
|
||||
|
||||
// Compute weight of the ray sample
|
||||
return aFresnel * ((theExponent + 2.f) / (theExponent + 1.f) * aGeom / aCosThetaO);
|
||||
return aG * aCosDelta / (theOutput.z * aM.z) * fresnelMedia (aCosDelta, theFresnel);
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : sampleMaterial
|
||||
// purpose : Samples specified composite material (BSDF)
|
||||
// function : SampleSpecularReflection
|
||||
// purpose : Samples specular BRDF, W = BRDF * cos(N, PSI) / PDF(PSI)
|
||||
//=======================================================================
|
||||
void sampleMaterial (in SMaterial theMaterial,
|
||||
in vec3 theOutput,
|
||||
out vec3 theInput,
|
||||
inout vec3 theWeight,
|
||||
inout int theBounce)
|
||||
vec3 SampleSpecularReflection (in vec3 theOutput, out vec3 theInput, in vec3 theFresnel)
|
||||
{
|
||||
// Compute the probability of ray reflection
|
||||
float aPd = convolve (theMaterial.Kd.rgb, theWeight);
|
||||
float aPs = convolve (theMaterial.Ks.rgb, theWeight);
|
||||
float aPr = convolve (theMaterial.Kr.rgb, theWeight);
|
||||
float aPt = convolve (theMaterial.Kt.rgb, theWeight);
|
||||
// Sample input direction
|
||||
theInput = vec3 (-theOutput.x,
|
||||
-theOutput.y,
|
||||
theOutput.z);
|
||||
|
||||
return fresnelMedia (theOutput.z, theFresnel);
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : SampleSpecularTransmission
|
||||
// purpose : Samples specular BTDF, W = BRDF * cos(N, PSI) / PDF(PSI)
|
||||
//=======================================================================
|
||||
vec3 SampleSpecularTransmission (in vec3 theOutput,
|
||||
out vec3 theInput, in vec3 theWeight, in vec3 theFresnel, inout bool theInside)
|
||||
{
|
||||
vec3 aFactor = fresnelMedia (theOutput.z, theFresnel);
|
||||
|
||||
float aReflection = convolve (aFactor, theWeight);
|
||||
|
||||
// sample specular BRDF/BTDF
|
||||
if (RandFloat() <= aReflection)
|
||||
{
|
||||
theInput = vec3 (-theOutput.x,
|
||||
-theOutput.y,
|
||||
theOutput.z);
|
||||
|
||||
theWeight = aFactor * (1.f / aReflection);
|
||||
}
|
||||
else
|
||||
{
|
||||
theInside = !theInside;
|
||||
|
||||
transmitted (theFresnel.y, theOutput, theInput);
|
||||
|
||||
theWeight = (UNIT - aFactor) * (1.f / (1.f - aReflection));
|
||||
}
|
||||
|
||||
return theWeight;
|
||||
}
|
||||
|
||||
#define FLT_EPSILON 1.0e-5F
|
||||
|
||||
//=======================================================================
|
||||
// function : BsdfPdf
|
||||
// purpose : Calculates BSDF of sampling input knowing output
|
||||
//=======================================================================
|
||||
float BsdfPdf (in SMaterial theBSDF,
|
||||
in vec3 theOutput,
|
||||
in vec3 theInput,
|
||||
in vec3 theWeight)
|
||||
{
|
||||
float aPd = convolve (theBSDF.Kd.rgb, theWeight);
|
||||
float aPs = convolve (theBSDF.Ks.rgb, theWeight);
|
||||
float aPr = convolve (theBSDF.Kr.rgb, theWeight);
|
||||
float aPt = convolve (theBSDF.Kt.rgb, theWeight);
|
||||
|
||||
float aReflection = aPd + aPs + aPr + aPt;
|
||||
|
||||
// Choose BSDF component to sample
|
||||
float aPDF = 0.f; // PDF of sampling input direction
|
||||
|
||||
if (theInput.z * theOutput.z > 0.f)
|
||||
{
|
||||
vec3 aHalf = normalize (theInput + theOutput);
|
||||
|
||||
// roughness value --> Blinn exponent
|
||||
float aPower = max (2.f / (theBSDF.Ks.w * theBSDF.Ks.w) - 2.f, 0.f);
|
||||
|
||||
aPDF = aPd * abs (theInput.z / M_PI) +
|
||||
aPs * (aPower + 2.f) * (1.f / M_2_PI) * pow (aHalf.z, aPower + 1.f) / (4.f * dot (theInput, aHalf));
|
||||
}
|
||||
|
||||
return aPDF / aReflection;
|
||||
}
|
||||
|
||||
//! Tool macro to handle sampling of particular BxDF
|
||||
#define PICK_BXDF(p, k) aPDF = p / aReflection; theWeight *= k / aPDF;
|
||||
|
||||
//=======================================================================
|
||||
// function : SampleBsdf
|
||||
// purpose : Samples specified composite material (BSDF)
|
||||
//=======================================================================
|
||||
float SampleBsdf (in SMaterial theBSDF,
|
||||
in vec3 theOutput,
|
||||
out vec3 theInput,
|
||||
inout vec3 theWeight,
|
||||
inout bool theInside)
|
||||
{
|
||||
// compute probability of each reflection type (BxDF)
|
||||
float aPd = convolve (theBSDF.Kd.rgb, theWeight);
|
||||
float aPs = convolve (theBSDF.Ks.rgb, theWeight);
|
||||
float aPr = convolve (theBSDF.Kr.rgb, theWeight);
|
||||
float aPt = convolve (theBSDF.Kt.rgb, theWeight);
|
||||
|
||||
float aReflection = aPd + aPs + aPr + aPt;
|
||||
|
||||
// choose BxDF component to sample
|
||||
float aKsi = aReflection * RandFloat();
|
||||
|
||||
theBounce = NON_SPECULAR_BOUNCE;
|
||||
// BxDF's PDF of sampled direction
|
||||
float aPDF = 0.f;
|
||||
|
||||
if (aKsi < aPd) // diffuse reflection
|
||||
{
|
||||
sampleLambertianReflection (theOutput, theInput);
|
||||
PICK_BXDF (aPd, theBSDF.Kd.rgb);
|
||||
|
||||
theWeight *= theMaterial.Kd.rgb * (aReflection / aPd);
|
||||
theWeight *= SampleLambertianReflection (theOutput, theInput, aPDF);
|
||||
}
|
||||
else if (aKsi < aPd + aPs) // glossy reflection
|
||||
else if (aKsi < aPd + aPs) // glossy reflection
|
||||
{
|
||||
theWeight *= theMaterial.Ks.rgb * (aReflection / aPs) *
|
||||
sampleBlinnReflection (theOutput, theInput, theMaterial.Fresnel, theMaterial.Ks.w);
|
||||
}
|
||||
else if (aKsi < aPd + aPs + aPr) // specular reflection
|
||||
{
|
||||
theWeight *= theMaterial.Kr.rgb * (aReflection / aPr) *
|
||||
sampleSpecularReflection (theOutput, theInput, theMaterial.Fresnel);
|
||||
PICK_BXDF (aPs, theBSDF.Ks.rgb);
|
||||
|
||||
theBounce = SPEC_REFLECT_BOUNCE; // specular bounce
|
||||
theWeight *= SampleBlinnReflection (theOutput, theInput, theBSDF.Fresnel, theBSDF.Ks.w, aPDF);
|
||||
}
|
||||
else // specular transmission
|
||||
else if (aKsi < aPd + aPs + aPr) // specular reflection
|
||||
{
|
||||
theWeight *= theMaterial.Kt.rgb * (aReflection / aPt) *
|
||||
sampleSpecularTransmission (theOutput, theInput, theBounce, theWeight, theMaterial.Fresnel);
|
||||
PICK_BXDF (aPr, theBSDF.Kr.rgb);
|
||||
|
||||
aPDF = MAXFLOAT;
|
||||
|
||||
theWeight *= SampleSpecularReflection (theOutput, theInput, theBSDF.Fresnel);
|
||||
}
|
||||
else if (aKsi < aReflection) // specular transmission
|
||||
{
|
||||
PICK_BXDF (aPt, theBSDF.Kt.rgb);
|
||||
|
||||
aPDF = MAXFLOAT;
|
||||
|
||||
theWeight *= SampleSpecularTransmission (theOutput, theInput, theWeight, theBSDF.Fresnel, theInside);
|
||||
}
|
||||
|
||||
// path termination for extra small weights
|
||||
theWeight = mix (theWeight, ZERO, float (aReflection < 1e-3f));
|
||||
theWeight = mix (ZERO, theWeight, step (FLT_EPSILON, aReflection));
|
||||
|
||||
return aPDF;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Handlers and samplers for light sources
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//=======================================================================
|
||||
// function : handlePointLight
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
float handlePointLight (in vec3 theInput, in vec3 theToLight, in float theRadius, in float theDistance)
|
||||
{
|
||||
float aDistance = dot (theToLight, theToLight);
|
||||
|
||||
float aCosMax = inversesqrt (1.f + theRadius * theRadius / aDistance);
|
||||
|
||||
return float (aDistance < theDistance * theDistance) *
|
||||
step (aCosMax, dot (theToLight, theInput) * inversesqrt (aDistance));
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : handleDirectLight
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
float handleDirectLight (in vec3 theInput, in vec3 theToLight, in float theCosMax)
|
||||
{
|
||||
return step (theCosMax, dot (theInput, theToLight));
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : sampleLight
|
||||
// purpose : General sampling function for directional and point lights
|
||||
//=======================================================================
|
||||
vec3 sampleLight (in vec3 theToLight, inout float theDistance, in bool isInfinite, in float theSmoothness, inout float thePDF)
|
||||
{
|
||||
SLocalSpace aSpace = LocalSpace (theToLight * (1.f / theDistance));
|
||||
|
||||
// for point lights smoothness defines radius
|
||||
float aCosMax = isInfinite ? theSmoothness :
|
||||
inversesqrt (1.f + theSmoothness * theSmoothness / (theDistance * theDistance));
|
||||
|
||||
float aKsi1 = RandFloat();
|
||||
float aKsi2 = RandFloat();
|
||||
|
||||
float aTmp = 1.f - aKsi2 * (1.f - aCosMax);
|
||||
|
||||
vec3 anInput = vec3 (cos (2.f * M_PI * aKsi1),
|
||||
sin (2.f * M_PI * aKsi1),
|
||||
aTmp);
|
||||
|
||||
anInput.xy *= sqrt (1.f - aTmp * aTmp);
|
||||
|
||||
thePDF *= (aCosMax < 1.f) ? 1.f / (2.f * M_PI) / (1.f - aCosMax) : 1.f;
|
||||
|
||||
return normalize (fromLocalSpace (anInput, aSpace));
|
||||
}
|
||||
|
||||
// =======================================================================
|
||||
// function : Latlong
|
||||
// purpose : Converts world direction to environment texture coordinates
|
||||
@ -552,49 +543,128 @@ vec2 Latlong (in vec3 thePoint)
|
||||
aPsi * 0.3183098f);
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : SampleLight
|
||||
// purpose : General sampling function for directional and point lights
|
||||
//=======================================================================
|
||||
vec3 SampleLight (in vec3 theToLight, inout float theDistance, in bool isInfinite, in float theSmoothness, inout float thePDF)
|
||||
{
|
||||
SLocalSpace aSpace = buildLocalSpace (theToLight * (1.f / theDistance));
|
||||
|
||||
// for point lights smoothness defines radius
|
||||
float aCosMax = isInfinite ? theSmoothness :
|
||||
inversesqrt (1.f + theSmoothness * theSmoothness / (theDistance * theDistance));
|
||||
|
||||
float aKsi1 = RandFloat();
|
||||
float aKsi2 = RandFloat();
|
||||
|
||||
float aTmp = 1.f - aKsi2 * (1.f - aCosMax);
|
||||
|
||||
vec3 anInput = vec3 (cos (M_2_PI * aKsi1),
|
||||
sin (M_2_PI * aKsi1),
|
||||
aTmp);
|
||||
|
||||
anInput.xy *= sqrt (1.f - aTmp * aTmp);
|
||||
|
||||
thePDF = (aCosMax < 1.f) ? (thePDF / M_2_PI) / (1.f - aCosMax) : MAXFLOAT;
|
||||
|
||||
return normalize (fromLocalSpace (anInput, aSpace));
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : HandlePointLight
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
float HandlePointLight (in vec3 theInput, in vec3 theToLight, in float theRadius, in float theDistance, inout float thePDF)
|
||||
{
|
||||
float aCosMax = inversesqrt (1.f + theRadius * theRadius / (theDistance * theDistance));
|
||||
|
||||
float aVisibility = step (aCosMax, dot (theInput, theToLight));
|
||||
|
||||
thePDF *= step (-1.f, -aCosMax) * aVisibility * (1.f / M_2_PI) / (1.f - aCosMax);
|
||||
|
||||
return aVisibility;
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : HandleDistantLight
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
float HandleDistantLight (in vec3 theInput, in vec3 theToLight, in float theCosMax, inout float thePDF)
|
||||
{
|
||||
float aVisibility = step (theCosMax, dot (theInput, theToLight));
|
||||
|
||||
thePDF *= step (-1.f, -theCosMax) * aVisibility * (1.f / M_2_PI) / (1.f - theCosMax);
|
||||
|
||||
return aVisibility;
|
||||
}
|
||||
|
||||
// =======================================================================
|
||||
// function: intersectLight
|
||||
// function: IntersectLight
|
||||
// purpose : Checks intersections with light sources
|
||||
// =======================================================================
|
||||
vec3 intersectLight (in SRay theRay, in bool isViewRay, in int theBounce, in float theDistance)
|
||||
vec3 IntersectLight (in SRay theRay, in int theDepth, in float theHitDistance, out float thePDF)
|
||||
{
|
||||
vec3 aRadiance = ZERO;
|
||||
vec3 aTotalRadiance = ZERO;
|
||||
|
||||
if ((isViewRay || IS_REFR_SPEC_BOUNCE(theBounce)) && uSphereMapForBack == 0)
|
||||
{
|
||||
aRadiance = BackgroundColor().xyz;
|
||||
}
|
||||
else
|
||||
{
|
||||
aRadiance = FetchEnvironment (Latlong (theRay.Direct)).xyz;
|
||||
}
|
||||
thePDF = 0.f; // PDF of sampling light sources
|
||||
|
||||
// Apply gamma correction (gamma is 2)
|
||||
aRadiance = aRadiance * aRadiance * float (theDistance == MAXFLOAT);
|
||||
|
||||
for (int aLightIdx = 0; aLightIdx < uLightCount && (isViewRay || IS_ANY_SPEC_BOUNCE(theBounce)); ++aLightIdx)
|
||||
for (int aLightIdx = 0; aLightIdx < uLightCount; ++aLightIdx)
|
||||
{
|
||||
vec4 aLight = texelFetch (
|
||||
uRaytraceLightSrcTexture, LIGHT_POS (aLightIdx));
|
||||
vec4 aParam = texelFetch (
|
||||
uRaytraceLightSrcTexture, LIGHT_PWR (aLightIdx));
|
||||
|
||||
// W component: 0 for infinite light and 1 for point light
|
||||
aLight.xyz -= mix (ZERO, theRay.Origin, aLight.w);
|
||||
|
||||
float aPDF = 1.f / uLightCount;
|
||||
|
||||
if (aLight.w != 0.f) // point light source
|
||||
{
|
||||
aRadiance += aParam.rgb * handlePointLight (
|
||||
theRay.Direct, aLight.xyz - theRay.Origin, aParam.w /* radius */, theDistance);
|
||||
float aCenterDst = length (aLight.xyz);
|
||||
|
||||
if (aCenterDst < theHitDistance)
|
||||
{
|
||||
float aVisibility = HandlePointLight (
|
||||
theRay.Direct, normalize (aLight.xyz), aParam.w /* radius */, aCenterDst, aPDF);
|
||||
|
||||
if (aVisibility > 0.f)
|
||||
{
|
||||
theHitDistance = aCenterDst;
|
||||
aTotalRadiance = aParam.rgb;
|
||||
|
||||
thePDF = aPDF;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (theDistance == MAXFLOAT) // directional light source
|
||||
else if (theHitDistance == MAXFLOAT) // directional light source
|
||||
{
|
||||
aRadiance += aParam.rgb * handleDirectLight (theRay.Direct, aLight.xyz, aParam.w /* angle cosine */);
|
||||
aTotalRadiance += aParam.rgb * HandleDistantLight (
|
||||
theRay.Direct, aLight.xyz, aParam.w /* angle cosine */, aPDF);
|
||||
|
||||
thePDF += aPDF;
|
||||
}
|
||||
}
|
||||
|
||||
return aRadiance;
|
||||
if (thePDF == 0.f && theHitDistance == MAXFLOAT) // light source not found
|
||||
{
|
||||
if (theDepth + uSphereMapForBack == 0) // view ray and map is hidden
|
||||
{
|
||||
aTotalRadiance = pow (BackgroundColor().rgb, vec3 (2.f));
|
||||
}
|
||||
else
|
||||
{
|
||||
aTotalRadiance = pow (FetchEnvironment (Latlong (theRay.Direct)).rgb, vec3 (2.f));
|
||||
}
|
||||
}
|
||||
|
||||
return aTotalRadiance;
|
||||
}
|
||||
|
||||
#define MIN_THROUGHPUT vec3 (0.02f)
|
||||
#define MIN_CONTRIBUTION vec3 (0.01f)
|
||||
#define MIN_THROUGHPUT vec3 (1.0e-3f)
|
||||
#define MIN_CONTRIBUTION vec3 (1.0e-2f)
|
||||
|
||||
#define MATERIAL_KD(index) (18 * index + 11)
|
||||
#define MATERIAL_KR(index) (18 * index + 12)
|
||||
@ -618,29 +688,32 @@ vec4 PathTrace (in SRay theRay, in vec3 theInverse)
|
||||
vec3 aRadiance = ZERO;
|
||||
vec3 aThroughput = UNIT;
|
||||
|
||||
int aBounce = 0; // type of previous hit point
|
||||
int aTrsfId = 0; // offset of object transform
|
||||
int aTransfID = 0; // ID of object transformation
|
||||
bool aInMedium = false; // is the ray inside an object
|
||||
|
||||
bool isInMedium = false;
|
||||
float aExpPDF = 1.f;
|
||||
float aImpPDF = 1.f;
|
||||
|
||||
for (int aDepth = 0; aDepth < NB_BOUNCES; ++aDepth)
|
||||
{
|
||||
SIntersect aHit = SIntersect (MAXFLOAT, vec2 (ZERO), ZERO);
|
||||
|
||||
ivec4 aTriIndex = SceneNearestHit (theRay, theInverse, aHit, aTrsfId);
|
||||
ivec4 aTriIndex = SceneNearestHit (theRay, theInverse, aHit, aTransfID);
|
||||
|
||||
// check implicit path
|
||||
vec3 aLe = intersectLight (theRay,
|
||||
aDepth == 0 /* is view ray */, aBounce, aHit.Time);
|
||||
vec3 aLe = IntersectLight (theRay, aDepth, aHit.Time, aExpPDF);
|
||||
|
||||
if (any (greaterThan (aLe, ZERO)) || aTriIndex.x == -1)
|
||||
{
|
||||
aRadiance += aThroughput * aLe; break; // terminate path
|
||||
float aMIS = (aDepth == 0 || aImpPDF == MAXFLOAT) ? 1.f :
|
||||
aImpPDF * aImpPDF / (aExpPDF * aExpPDF + aImpPDF * aImpPDF);
|
||||
|
||||
aRadiance += aThroughput * aLe * aMIS; break; // terminate path
|
||||
}
|
||||
|
||||
vec3 aInvTransf0 = texelFetch (uSceneTransformTexture, aTrsfId + 0).xyz;
|
||||
vec3 aInvTransf1 = texelFetch (uSceneTransformTexture, aTrsfId + 1).xyz;
|
||||
vec3 aInvTransf2 = texelFetch (uSceneTransformTexture, aTrsfId + 2).xyz;
|
||||
vec3 aInvTransf0 = texelFetch (uSceneTransformTexture, aTransfID + 0).xyz;
|
||||
vec3 aInvTransf1 = texelFetch (uSceneTransformTexture, aTransfID + 1).xyz;
|
||||
vec3 aInvTransf2 = texelFetch (uSceneTransformTexture, aTransfID + 2).xyz;
|
||||
|
||||
// compute geometrical normal
|
||||
aHit.Normal = normalize (vec3 (dot (aInvTransf0, aHit.Normal),
|
||||
@ -649,7 +722,7 @@ vec4 PathTrace (in SRay theRay, in vec3 theInverse)
|
||||
|
||||
theRay.Origin += theRay.Direct * aHit.Time; // get new intersection point
|
||||
|
||||
// Evaluate depth on first hit
|
||||
// evaluate depth on first hit
|
||||
if (aDepth == 0)
|
||||
{
|
||||
vec4 aNDCPoint = uViewMat * vec4 (theRay.Origin, 1.f);
|
||||
@ -694,7 +767,7 @@ vec4 PathTrace (in SRay theRay, in vec3 theInverse)
|
||||
dot (aInvTransf1, aNormal),
|
||||
dot (aInvTransf2, aNormal)));
|
||||
|
||||
SLocalSpace aSpace = LocalSpace (aNormal);
|
||||
SLocalSpace aSpace = buildLocalSpace (aNormal);
|
||||
|
||||
// account for self-emission (not stored in the material)
|
||||
aRadiance += aThroughput * texelFetch (
|
||||
@ -702,6 +775,8 @@ vec4 PathTrace (in SRay theRay, in vec3 theInverse)
|
||||
|
||||
if (uLightCount > 0 && convolve (aMaterial.Kd.rgb + aMaterial.Ks.rgb, aThroughput) > 0.f)
|
||||
{
|
||||
aExpPDF = 1.f / uLightCount;
|
||||
|
||||
int aLightIdx = min (int (floor (RandFloat() * uLightCount)), uLightCount - 1);
|
||||
|
||||
vec4 aLight = texelFetch (
|
||||
@ -712,15 +787,21 @@ vec4 PathTrace (in SRay theRay, in vec3 theInverse)
|
||||
// 'w' component is 0 for infinite light and 1 for point light
|
||||
aLight.xyz -= mix (ZERO, theRay.Origin, aLight.w);
|
||||
|
||||
float aPDF = 1.f / uLightCount, aDistance = length (aLight.xyz);
|
||||
float aDistance = length (aLight.xyz);
|
||||
|
||||
aLight.xyz = sampleLight (aLight.xyz, aDistance,
|
||||
aLight.w == 0.f /* is infinite */, aParam.w /* max cos or radius */, aPDF);
|
||||
aLight.xyz = SampleLight (aLight.xyz, aDistance,
|
||||
aLight.w == 0.f /* is infinite */, aParam.w /* max cos or radius */, aExpPDF);
|
||||
|
||||
vec3 aContrib = (1.f / aPDF) * aParam.rgb /* Le */ * handleMaterial (
|
||||
aImpPDF = BsdfPdf (aMaterial,
|
||||
toLocalSpace (-theRay.Direct, aSpace), toLocalSpace (aLight.xyz, aSpace), aThroughput);
|
||||
|
||||
// MIS weight including division by explicit PDF
|
||||
float aMIS = (aExpPDF == MAXFLOAT) ? 1.f : aExpPDF / (aExpPDF * aExpPDF + aImpPDF * aImpPDF);
|
||||
|
||||
vec3 aContrib = aMIS * aParam.rgb /* Le */ * HandleMaterial (
|
||||
aMaterial, toLocalSpace (aLight.xyz, aSpace), toLocalSpace (-theRay.Direct, aSpace));
|
||||
|
||||
if (any (greaterThan (aContrib, MIN_CONTRIBUTION))) // first check if light source is important
|
||||
if (any (greaterThan (aContrib, MIN_CONTRIBUTION))) // check if light source is important
|
||||
{
|
||||
SRay aShadow = SRay (theRay.Origin + aLight.xyz * uSceneEpsilon, aLight.xyz);
|
||||
|
||||
@ -734,32 +815,29 @@ vec4 PathTrace (in SRay theRay, in vec3 theInverse)
|
||||
}
|
||||
}
|
||||
|
||||
vec3 anInput;
|
||||
|
||||
sampleMaterial (aMaterial,
|
||||
toLocalSpace (-theRay.Direct, aSpace), anInput, aThroughput, aBounce);
|
||||
|
||||
if (isInMedium)
|
||||
if (aInMedium) // handle attenuation
|
||||
{
|
||||
aThroughput *= exp (-aHit.Time *
|
||||
aMaterial.Absorption.w * (UNIT - aMaterial.Absorption.rgb));
|
||||
}
|
||||
|
||||
isInMedium = IS_REFR_SPEC_BOUNCE(aBounce) ? !isInMedium : isInMedium;
|
||||
vec3 anInput = UNIT; // sampled input direction
|
||||
|
||||
#ifndef RUSSIAN_ROULETTE
|
||||
if (all (lessThan (aThroughput, MIN_THROUGHPUT)))
|
||||
{
|
||||
aDepth = INVALID_BOUNCES; // terminate path
|
||||
}
|
||||
#else
|
||||
float aSurvive = aDepth < 3 ? 1.f : min (dot (LUMA, aThroughput), 0.95f);
|
||||
aImpPDF = SampleBsdf (aMaterial,
|
||||
toLocalSpace (-theRay.Direct, aSpace), anInput, aThroughput, aInMedium);
|
||||
|
||||
if (RandFloat() > aSurvive)
|
||||
float aSurvive = 1.f;
|
||||
|
||||
#ifdef RUSSIAN_ROULETTE
|
||||
aSurvive = aDepth < 3 ? 1.f : min (dot (LUMA, aThroughput), 0.95f);
|
||||
#endif
|
||||
|
||||
if (RandFloat() > aSurvive || all (lessThanEqual (aThroughput, MIN_THROUGHPUT)))
|
||||
{
|
||||
aDepth = INVALID_BOUNCES; // terminate path
|
||||
}
|
||||
|
||||
#ifdef RUSSIAN_ROULETTE
|
||||
aThroughput /= aSurvive;
|
||||
#endif
|
||||
|
||||
|
@ -143,7 +143,9 @@ struct SIntersect
|
||||
#define AXIS_Y vec3 (0.0f, 1.0f, 0.0f)
|
||||
#define AXIS_Z vec3 (0.0f, 0.0f, 1.0f)
|
||||
|
||||
#define M_PI 3.14159265f
|
||||
#define M_PI 3.141592653f
|
||||
#define M_2_PI 6.283185307f
|
||||
#define M_PI_2 1.570796327f
|
||||
|
||||
#define LUMA vec3 (0.2126f, 0.7152f, 0.0722f)
|
||||
|
||||
@ -835,8 +837,8 @@ vec2 SmoothUV (in vec2 theUV, in ivec4 theTriangle)
|
||||
// =======================================================================
|
||||
vec4 FetchEnvironment (in vec2 theTexCoord)
|
||||
{
|
||||
return mix (vec4 (0.0f, 0.0f, 0.0f, 1.0f),
|
||||
textureLod (uEnvironmentMapTexture, theTexCoord, 0.0f), float (uSphereMapEnabled));
|
||||
return uSphereMapEnabled == 0 ?
|
||||
vec4 (0.f, 0.f, 0.f, 1.f) : textureLod (uEnvironmentMapTexture, theTexCoord, 0.f);
|
||||
}
|
||||
|
||||
// =======================================================================
|
||||
@ -1020,8 +1022,8 @@ vec4 Radiance (in SRay theRay, in vec3 theInverse)
|
||||
|
||||
if (aVisibility > 0.0f)
|
||||
{
|
||||
vec3 aIntensity = vec3 (texelFetch (
|
||||
uRaytraceLightSrcTexture, LIGHT_PWR (aLightIdx)));
|
||||
vec3 aIntensity = min (UNIT, vec3 (texelFetch (
|
||||
uRaytraceLightSrcTexture, LIGHT_PWR (aLightIdx))));
|
||||
|
||||
float aRdotV = dot (reflect (aLight.xyz, aSidedNormal), theRay.Direct);
|
||||
|
||||
|
@ -18,7 +18,7 @@ uniform int uBlockedRngEnabled;
|
||||
|
||||
//! Maximum radiance that can be added to the pixel. Decreases noise
|
||||
//! level, but introduces some bias.
|
||||
#define MAX_RADIANCE vec3 (25.f)
|
||||
#define MAX_RADIANCE vec3 (50.f)
|
||||
|
||||
// =======================================================================
|
||||
// function : main
|
||||
|
@ -1,6 +1,18 @@
|
||||
// This file has been automatically generated from resource file src/Shaders/PathtraceBase.fs
|
||||
|
||||
static const char Shaders_PathtraceBase_fs[] =
|
||||
"#ifdef _MSC_VER\n"
|
||||
" #define PATH_TRACING // just for editing in MS VS\n"
|
||||
"\n"
|
||||
" #define in\n"
|
||||
" #define out\n"
|
||||
" #define inout\n"
|
||||
"\n"
|
||||
" typedef struct { float x; float y; } vec2;\n"
|
||||
" typedef struct { float x; float y; float z; } vec3;\n"
|
||||
" typedef struct { float x; float y; float z; float w; } vec4;\n"
|
||||
"#endif\n"
|
||||
"\n"
|
||||
"#ifdef PATH_TRACING\n"
|
||||
"\n"
|
||||
"///////////////////////////////////////////////////////////////////////////////////////\n"
|
||||
@ -45,13 +57,13 @@ static const char Shaders_PathtraceBase_fs[] =
|
||||
"// Support subroutines\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : LocalSpace\n"
|
||||
"// function : buildLocalSpace\n"
|
||||
"// purpose : Generates local space for the given normal\n"
|
||||
"//=======================================================================\n"
|
||||
"SLocalSpace LocalSpace (in vec3 theNormal)\n"
|
||||
"SLocalSpace buildLocalSpace (in vec3 theNormal)\n"
|
||||
"{\n"
|
||||
" vec3 anAxisX = cross (vec3 (0.f, 1.f, 0.f), theNormal);\n"
|
||||
" vec3 anAxisY = cross (vec3 (1.f, 0.f, 0.f), theNormal);\n"
|
||||
" vec3 anAxisX = vec3 (theNormal.z, 0.f, -theNormal.x);\n"
|
||||
" vec3 anAxisY = vec3 (0.f, -theNormal.z, theNormal.y);\n"
|
||||
"\n"
|
||||
" float aSqrLenX = dot (anAxisX, anAxisX);\n"
|
||||
" float aSqrLenY = dot (anAxisY, anAxisY);\n"
|
||||
@ -102,19 +114,6 @@ static const char Shaders_PathtraceBase_fs[] =
|
||||
"}\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : sphericalDirection\n"
|
||||
"// purpose : Constructs vector from spherical coordinates\n"
|
||||
"//=======================================================================\n"
|
||||
"vec3 sphericalDirection (in float theCosTheta, in float thePhi)\n"
|
||||
"{\n"
|
||||
" float aSinTheta = sqrt (1.f - theCosTheta * theCosTheta);\n"
|
||||
"\n"
|
||||
" return vec3 (aSinTheta * cos (thePhi),\n"
|
||||
" aSinTheta * sin (thePhi),\n"
|
||||
" theCosTheta);\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : fresnelSchlick\n"
|
||||
"// purpose : Computes the Fresnel reflection formula using\n"
|
||||
"// Schlick's approximation.\n"
|
||||
@ -199,24 +198,24 @@ static const char Shaders_PathtraceBase_fs[] =
|
||||
"// purpose : Computes the Fresnel reflection formula for general medium\n"
|
||||
"// in case of circularly polarized light.\n"
|
||||
"//=======================================================================\n"
|
||||
"vec3 fresnelMedia (in float theCosI, in vec3 theFresnelCoeffs)\n"
|
||||
"vec3 fresnelMedia (in float theCosI, in vec3 theFresnel)\n"
|
||||
"{\n"
|
||||
" if (theFresnelCoeffs.x > FRESNEL_SCHLICK)\n"
|
||||
" if (theFresnel.x > FRESNEL_SCHLICK)\n"
|
||||
" {\n"
|
||||
" return fresnelSchlick (abs (theCosI), theFresnelCoeffs);\n"
|
||||
" return fresnelSchlick (abs (theCosI), theFresnel);\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" if (theFresnelCoeffs.x > FRESNEL_CONSTANT)\n"
|
||||
" if (theFresnel.x > FRESNEL_CONSTANT)\n"
|
||||
" {\n"
|
||||
" return vec3 (theFresnelCoeffs.z);\n"
|
||||
" return vec3 (theFresnel.z);\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" if (theFresnelCoeffs.x > FRESNEL_CONDUCTOR)\n"
|
||||
" if (theFresnel.x > FRESNEL_CONDUCTOR)\n"
|
||||
" {\n"
|
||||
" return vec3 (fresnelConductor (abs (theCosI), theFresnelCoeffs.y, theFresnelCoeffs.z));\n"
|
||||
" return vec3 (fresnelConductor (abs (theCosI), theFresnel.y, theFresnel.z));\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" return vec3 (fresnelDielectric (theCosI, theFresnelCoeffs.y));\n"
|
||||
" return vec3 (fresnelDielectric (theCosI, theFresnel.y));\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
@ -229,11 +228,11 @@ static const char Shaders_PathtraceBase_fs[] =
|
||||
" // Compute relative index of refraction\n"
|
||||
" float anEta = (theIncident.z > 0.f) ? 1.f / theIndex : theIndex;\n"
|
||||
"\n"
|
||||
" // Handle total internal reflection for transmission\n"
|
||||
" // Handle total internal reflection (TIR)\n"
|
||||
" float aSinT2 = anEta * anEta * (1.f - theIncident.z * theIncident.z);\n"
|
||||
"\n"
|
||||
" // Compute transmitted ray direction\n"
|
||||
" float aCosT = sqrt (1.f - min (aSinT2, 1.f)) * (theIncident.z > 0.f ? -1.f : 1.f);\n"
|
||||
" // Compute direction of transmitted ray\n"
|
||||
" float aCosT = sqrt (1.f - min (aSinT2, 1.f)) * sign (-theIncident.z);\n"
|
||||
"\n"
|
||||
" theTransmit = normalize (vec3 (-anEta * theIncident.x,\n"
|
||||
" -anEta * theIncident.y,\n"
|
||||
@ -245,145 +244,101 @@ static const char Shaders_PathtraceBase_fs[] =
|
||||
"//////////////////////////////////////////////////////////////////////////////////////////////\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : handleLambertianReflection\n"
|
||||
"// function : HandleLambertianReflection\n"
|
||||
"// purpose : Handles Lambertian BRDF, with cos(N, PSI)\n"
|
||||
"//=======================================================================\n"
|
||||
"float handleLambertianReflection (in vec3 theInput, in vec3 theOutput)\n"
|
||||
"float HandleLambertianReflection (in vec3 theInput, in vec3 theOutput)\n"
|
||||
"{\n"
|
||||
" return max (0.f, theInput.z) * (1.f / M_PI);\n"
|
||||
" return (theInput.z <= 0.f || theOutput.z <= 0.f) ? 0.f : theInput.z * (1.f / M_PI);\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : handleBlinnReflection\n"
|
||||
"// function : SmithG1\n"
|
||||
"// purpose :\n"
|
||||
"//=======================================================================\n"
|
||||
"float SmithG1 (in vec3 theDirection, in vec3 theM, in float theRoughness)\n"
|
||||
"{\n"
|
||||
" if (dot (theDirection, theM) * theDirection.z <= 0.f)\n"
|
||||
" {\n"
|
||||
" return 0.f;\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" float aTanThetaM = sqrt (1.f - theDirection.z * theDirection.z) / theDirection.z;\n"
|
||||
"\n"
|
||||
" if (aTanThetaM == 0.0f)\n"
|
||||
" {\n"
|
||||
" return 1.f;\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" float aVal = 1.f / (theRoughness * aTanThetaM);\n"
|
||||
"\n"
|
||||
" if (aVal >= 1.6f)\n"
|
||||
" {\n"
|
||||
" return 1.f;\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" // Use fast and accurate rational approximation to the\n"
|
||||
" // shadowing-masking function (from Mitsuba renderer)\n"
|
||||
" float aSqr = aVal * aVal;\n"
|
||||
"\n"
|
||||
" return (3.535f * aVal + 2.181f * aSqr) / (1.f + 2.276f * aVal + 2.577f * aSqr);\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : HandleBlinnReflection\n"
|
||||
"// purpose : Handles Blinn glossy BRDF, with cos(N, PSI)\n"
|
||||
"//=======================================================================\n"
|
||||
"vec3 handleBlinnReflection (in vec3 theInput, in vec3 theOutput, in vec3 theFresnelCoeffs, in float theExponent)\n"
|
||||
"vec3 HandleBlinnReflection (in vec3 theInput, in vec3 theOutput, in vec3 theFresnel, in float theRoughness)\n"
|
||||
"{\n"
|
||||
" vec3 aWeight = ZERO;\n"
|
||||
" // calculate the reflection half-vec\n"
|
||||
" vec3 aH = normalize (theInput + theOutput);\n"
|
||||
"\n"
|
||||
" // Compute half-angle vector\n"
|
||||
" vec3 aHalf = theInput + theOutput;\n"
|
||||
" // roughness value -> Blinn exponent\n"
|
||||
" float aPower = max (2.f / (theRoughness * theRoughness) - 2.f, 0.f);\n"
|
||||
"\n"
|
||||
" if (aHalf.z < 0.f)\n"
|
||||
" aHalf = -aHalf;\n"
|
||||
" // calculate microfacet distribution\n"
|
||||
" float aD = (aPower + 2.f) * (1.f / M_2_PI) * pow (aH.z, aPower);\n"
|
||||
"\n"
|
||||
" float aLength = dot (aHalf, aHalf);\n"
|
||||
" // calculate shadow-masking function\n"
|
||||
" float aG = SmithG1 (theOutput, aH, theRoughness) * SmithG1 (theInput, aH, theRoughness);\n"
|
||||
"\n"
|
||||
" if (aLength <= 0.f)\n"
|
||||
" return ZERO;\n"
|
||||
"\n"
|
||||
" aHalf *= inversesqrt (aLength);\n"
|
||||
"\n"
|
||||
" // Compute Fresnel reflectance\n"
|
||||
" float aCosDelta = dot (theOutput, aHalf);\n"
|
||||
"\n"
|
||||
" vec3 aFresnel = fresnelMedia (aCosDelta, theFresnelCoeffs);\n"
|
||||
"\n"
|
||||
" // Compute fraction of microfacets that reflect light\n"
|
||||
" float aCosThetaH = max (0.f, aHalf.z);\n"
|
||||
"\n"
|
||||
" float aFraction = (theExponent + 2.f) * (M_PI / 2.f) * pow (aCosThetaH, theExponent);\n"
|
||||
"\n"
|
||||
" // Compute geometry attenuation term (already includes cos)\n"
|
||||
" float aCosThetaI = max (0.f, theInput.z);\n"
|
||||
" float aCosThetaO = max (0.f, theOutput.z);\n"
|
||||
"\n"
|
||||
" float aGeom = min (1.f, 2.f * aCosThetaH / max (0.f, aCosDelta) * min (aCosThetaO, aCosThetaI));\n"
|
||||
"\n"
|
||||
" return aCosThetaO < 1.0e-3f ? ZERO :\n"
|
||||
" aFraction * aGeom / (4.f * aCosThetaO) * aFresnel;\n"
|
||||
" // return total amount of reflection\n"
|
||||
" return (theInput.z <= 0.f || theOutput.z <= 0.f) ? ZERO :\n"
|
||||
" aD * aG / (4.f * theOutput.z) * fresnelMedia (dot (theOutput, aH), theFresnel);\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : handleMaterial\n"
|
||||
"// function : HandleMaterial\n"
|
||||
"// purpose : Returns BSDF value for specified material, with cos(N, PSI)\n"
|
||||
"//=======================================================================\n"
|
||||
"vec3 handleMaterial (in SMaterial theMaterial, in vec3 theInput, in vec3 theOutput)\n"
|
||||
"vec3 HandleMaterial (in SMaterial theBSDF, in vec3 theInput, in vec3 theOutput)\n"
|
||||
"{\n"
|
||||
" return theMaterial.Kd.rgb * handleLambertianReflection (theInput, theOutput) +\n"
|
||||
" theMaterial.Ks.rgb * handleBlinnReflection (theInput, theOutput, theMaterial.Fresnel, theMaterial.Ks.w);\n"
|
||||
" return theBSDF.Kd.rgb * HandleLambertianReflection (theInput, theOutput) +\n"
|
||||
" theBSDF.Ks.rgb * HandleBlinnReflection (theInput, theOutput, theBSDF.Fresnel, theBSDF.Ks.w);\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : sampleLambertianReflection\n"
|
||||
"// function : SampleLambertianReflection\n"
|
||||
"// purpose : Samples Lambertian BRDF, W = BRDF * cos(N, PSI) / PDF(PSI)\n"
|
||||
"//=======================================================================\n"
|
||||
"void sampleLambertianReflection (in vec3 theOutput, out vec3 theInput)\n"
|
||||
"vec3 SampleLambertianReflection (in vec3 theOutput, out vec3 theInput, inout float thePDF)\n"
|
||||
"{\n"
|
||||
" float aKsi1 = RandFloat();\n"
|
||||
" float aKsi2 = RandFloat();\n"
|
||||
"\n"
|
||||
" float aTemp = sqrt (aKsi2);\n"
|
||||
"\n"
|
||||
" theInput = vec3 (aTemp * cos (2.f * M_PI * aKsi1),\n"
|
||||
" aTemp * sin (2.f * M_PI * aKsi1),\n"
|
||||
" theInput = vec3 (aTemp * cos (M_2_PI * aKsi1),\n"
|
||||
" aTemp * sin (M_2_PI * aKsi1),\n"
|
||||
" sqrt (1.f - aKsi2));\n"
|
||||
"\n"
|
||||
" theInput.z = mix (-theInput.z, theInput.z, step (0.f, theOutput.z));\n"
|
||||
"}\n"
|
||||
" thePDF *= abs (theInput.z) * (1.f / M_PI);\n"
|
||||
"\n"
|
||||
"// Types of bounces\n"
|
||||
"#define NON_SPECULAR_BOUNCE 0\n"
|
||||
"#define SPEC_REFLECT_BOUNCE 1\n"
|
||||
"#define SPEC_REFRACT_BOUNCE 2\n"
|
||||
"\n"
|
||||
"#define IS_NON_SPEC_BOUNCE(theBounce) (theBounce == 0)\n"
|
||||
"#define IS_ANY_SPEC_BOUNCE(theBounce) (theBounce != 0)\n"
|
||||
"#define IS_REFL_SPEC_BOUNCE(theBounce) (theBounce == 1)\n"
|
||||
"#define IS_REFR_SPEC_BOUNCE(theBounce) (theBounce == 2)\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : sampleSpecularTransmission\n"
|
||||
"// purpose : Samples specular BTDF, W = BRDF * cos(N, PSI) / PDF(PSI)\n"
|
||||
"//=======================================================================\n"
|
||||
"vec3 sampleSpecularTransmission (in vec3 theOutput, out vec3 theInput,\n"
|
||||
" out int theBounce, in vec3 theWeight, in vec3 theFresnelCoeffs)\n"
|
||||
"{\n"
|
||||
" vec3 aFresnel = fresnelMedia (theOutput.z, theFresnelCoeffs);\n"
|
||||
"\n"
|
||||
" float aProbability = convolve (aFresnel, theWeight);\n"
|
||||
"\n"
|
||||
" // Check if transmission takes place\n"
|
||||
" theBounce = RandFloat() <= aProbability ?\n"
|
||||
" SPEC_REFLECT_BOUNCE : SPEC_REFRACT_BOUNCE;\n"
|
||||
"\n"
|
||||
" // Sample input direction\n"
|
||||
" if (theBounce == SPEC_REFLECT_BOUNCE)\n"
|
||||
" {\n"
|
||||
" theInput = vec3 (-theOutput.x,\n"
|
||||
" -theOutput.y,\n"
|
||||
" theOutput.z);\n"
|
||||
"\n"
|
||||
" theWeight = aFresnel * (1.f / aProbability);\n"
|
||||
" }\n"
|
||||
" else\n"
|
||||
" {\n"
|
||||
" transmitted (theFresnelCoeffs.y, theOutput, theInput);\n"
|
||||
"\n"
|
||||
" theWeight = (UNIT - aFresnel) * (1.f / (1.f - aProbability));\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" return theWeight;\n"
|
||||
" return (theInput.z <= 0.f || theOutput.z <= 0.f) ? ZERO : UNIT;\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : sampleSpecularReflection\n"
|
||||
"// purpose : Samples specular BRDF, W = BRDF * cos(N, PSI) / PDF(PSI)\n"
|
||||
"//=======================================================================\n"
|
||||
"vec3 sampleSpecularReflection (in vec3 theOutput, out vec3 theInput, in vec3 theFresnelCoeffs)\n"
|
||||
"{\n"
|
||||
" // Sample input direction\n"
|
||||
" theInput = vec3 (-theOutput.x,\n"
|
||||
" -theOutput.y,\n"
|
||||
" theOutput.z);\n"
|
||||
"\n"
|
||||
" return fresnelMedia (theOutput.z, theFresnelCoeffs);\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"#define MIN_COS 1.0e-20f\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : sampleBlinnReflection\n"
|
||||
"// function : SampleBlinnReflection\n"
|
||||
"// purpose : Samples Blinn BRDF, W = BRDF * cos(N, PSI) / PDF(PSI)\n"
|
||||
"// The BRDF is a product of three main terms, D, G, and F,\n"
|
||||
"// which is then divided by two cosine terms. Here we perform\n"
|
||||
@ -392,155 +347,191 @@ static const char Shaders_PathtraceBase_fs[] =
|
||||
"// terms would be complex, and it is the D term that accounts\n"
|
||||
"// for most of the variation.\n"
|
||||
"//=======================================================================\n"
|
||||
"vec3 sampleBlinnReflection (in vec3 theOutput, out vec3 theInput, in vec3 theFresnelCoeffs, in float theExponent)\n"
|
||||
"vec3 SampleBlinnReflection (in vec3 theOutput, out vec3 theInput, in vec3 theFresnel, in float theRoughness, inout float thePDF)\n"
|
||||
"{\n"
|
||||
" vec3 aWeight = ZERO;\n"
|
||||
"\n"
|
||||
" // Generate two random variables\n"
|
||||
" float aKsi1 = RandFloat();\n"
|
||||
" float aKsi2 = RandFloat();\n"
|
||||
"\n"
|
||||
" // Compute sampled half-angle vector for Blinn distribution\n"
|
||||
" float aCosThetaH = pow (aKsi1, 1.f / (theExponent + 1.f));\n"
|
||||
" // roughness value --> Blinn exponent\n"
|
||||
" float aPower = max (2.f / (theRoughness * theRoughness) - 2.f, 0.f);\n"
|
||||
"\n"
|
||||
" vec3 aHalf = sphericalDirection (aCosThetaH, aKsi2 * 2.f * M_PI);\n"
|
||||
" // normal from microface distribution\n"
|
||||
" float aCosThetaM = pow (aKsi1, 1.f / (aPower + 2.f));\n"
|
||||
"\n"
|
||||
" if (aHalf.z < 0)\n"
|
||||
" {\n"
|
||||
" aHalf = -aHalf;\n"
|
||||
" }\n"
|
||||
" vec3 aM = vec3 (cos (M_2_PI * aKsi2),\n"
|
||||
" sin (M_2_PI * aKsi2),\n"
|
||||
" aCosThetaM);\n"
|
||||
"\n"
|
||||
" // Compute incident direction by reflecting about half-vector\n"
|
||||
" float aCosDelta = dot (theOutput, aHalf);\n"
|
||||
" aM.xy *= sqrt (1.f - aCosThetaM * aCosThetaM);\n"
|
||||
"\n"
|
||||
" vec3 anInput = 2.f * aCosDelta * aHalf - theOutput;\n"
|
||||
" // calculate PDF of sampled direction\n"
|
||||
" thePDF *= (aPower + 2.f) * (1.f / M_2_PI) * pow (aCosThetaM, aPower + 1.f);\n"
|
||||
"\n"
|
||||
" if (theOutput.z * anInput.z <= 0.f)\n"
|
||||
" float aCosDelta = dot (theOutput, aM);\n"
|
||||
"\n"
|
||||
" // pick input based on half direction\n"
|
||||
" theInput = -theOutput + 2.f * aCosDelta * aM;\n"
|
||||
"\n"
|
||||
" if (theInput.z <= 0.f || theOutput.z <= 0.f)\n"
|
||||
" {\n"
|
||||
" return ZERO;\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" theInput = anInput;\n"
|
||||
" // Jacobian of half-direction mapping\n"
|
||||
" thePDF /= 4.f * dot (theInput, aM);\n"
|
||||
"\n"
|
||||
" // Compute Fresnel reflectance\n"
|
||||
" vec3 aFresnel = fresnelMedia (aCosDelta, theFresnelCoeffs);\n"
|
||||
" // compute shadow-masking coefficient\n"
|
||||
" float aG = SmithG1 (theOutput, aM, theRoughness) * SmithG1 (theInput, aM, theRoughness);\n"
|
||||
"\n"
|
||||
" // Compute geometry attenuation term\n"
|
||||
" float aCosThetaI = max (MIN_COS, theInput.z);\n"
|
||||
" float aCosThetaO = max (MIN_COS, theOutput.z);\n"
|
||||
"\n"
|
||||
" float aGeom = min (max (MIN_COS, aCosDelta), 2.f * aCosThetaH * min (aCosThetaO, aCosThetaI));\n"
|
||||
"\n"
|
||||
" // Compute weight of the ray sample\n"
|
||||
" return aFresnel * ((theExponent + 2.f) / (theExponent + 1.f) * aGeom / aCosThetaO);\n"
|
||||
" return aG * aCosDelta / (theOutput.z * aM.z) * fresnelMedia (aCosDelta, theFresnel);\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : sampleMaterial\n"
|
||||
"// purpose : Samples specified composite material (BSDF)\n"
|
||||
"// function : SampleSpecularReflection\n"
|
||||
"// purpose : Samples specular BRDF, W = BRDF * cos(N, PSI) / PDF(PSI)\n"
|
||||
"//=======================================================================\n"
|
||||
"void sampleMaterial (in SMaterial theMaterial,\n"
|
||||
" in vec3 theOutput,\n"
|
||||
" out vec3 theInput,\n"
|
||||
" inout vec3 theWeight,\n"
|
||||
" inout int theBounce)\n"
|
||||
"vec3 SampleSpecularReflection (in vec3 theOutput, out vec3 theInput, in vec3 theFresnel)\n"
|
||||
"{\n"
|
||||
" // Compute the probability of ray reflection\n"
|
||||
" float aPd = convolve (theMaterial.Kd.rgb, theWeight);\n"
|
||||
" float aPs = convolve (theMaterial.Ks.rgb, theWeight);\n"
|
||||
" float aPr = convolve (theMaterial.Kr.rgb, theWeight);\n"
|
||||
" float aPt = convolve (theMaterial.Kt.rgb, theWeight);\n"
|
||||
" // Sample input direction\n"
|
||||
" theInput = vec3 (-theOutput.x,\n"
|
||||
" -theOutput.y,\n"
|
||||
" theOutput.z);\n"
|
||||
"\n"
|
||||
" return fresnelMedia (theOutput.z, theFresnel);\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : SampleSpecularTransmission\n"
|
||||
"// purpose : Samples specular BTDF, W = BRDF * cos(N, PSI) / PDF(PSI)\n"
|
||||
"//=======================================================================\n"
|
||||
"vec3 SampleSpecularTransmission (in vec3 theOutput,\n"
|
||||
" out vec3 theInput, in vec3 theWeight, in vec3 theFresnel, inout bool theInside)\n"
|
||||
"{\n"
|
||||
" vec3 aFactor = fresnelMedia (theOutput.z, theFresnel);\n"
|
||||
"\n"
|
||||
" float aReflection = convolve (aFactor, theWeight);\n"
|
||||
"\n"
|
||||
" // sample specular BRDF/BTDF\n"
|
||||
" if (RandFloat() <= aReflection)\n"
|
||||
" {\n"
|
||||
" theInput = vec3 (-theOutput.x,\n"
|
||||
" -theOutput.y,\n"
|
||||
" theOutput.z);\n"
|
||||
"\n"
|
||||
" theWeight = aFactor * (1.f / aReflection);\n"
|
||||
" }\n"
|
||||
" else\n"
|
||||
" {\n"
|
||||
" theInside = !theInside;\n"
|
||||
"\n"
|
||||
" transmitted (theFresnel.y, theOutput, theInput);\n"
|
||||
"\n"
|
||||
" theWeight = (UNIT - aFactor) * (1.f / (1.f - aReflection));\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" return theWeight;\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"#define FLT_EPSILON 1.0e-5F\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : BsdfPdf\n"
|
||||
"// purpose : Calculates BSDF of sampling input knowing output\n"
|
||||
"//=======================================================================\n"
|
||||
"float BsdfPdf (in SMaterial theBSDF,\n"
|
||||
" in vec3 theOutput,\n"
|
||||
" in vec3 theInput,\n"
|
||||
" in vec3 theWeight)\n"
|
||||
"{\n"
|
||||
" float aPd = convolve (theBSDF.Kd.rgb, theWeight);\n"
|
||||
" float aPs = convolve (theBSDF.Ks.rgb, theWeight);\n"
|
||||
" float aPr = convolve (theBSDF.Kr.rgb, theWeight);\n"
|
||||
" float aPt = convolve (theBSDF.Kt.rgb, theWeight);\n"
|
||||
"\n"
|
||||
" float aReflection = aPd + aPs + aPr + aPt;\n"
|
||||
"\n"
|
||||
" // Choose BSDF component to sample\n"
|
||||
" float aPDF = 0.f; // PDF of sampling input direction\n"
|
||||
"\n"
|
||||
" if (theInput.z * theOutput.z > 0.f)\n"
|
||||
" {\n"
|
||||
" vec3 aHalf = normalize (theInput + theOutput);\n"
|
||||
"\n"
|
||||
" // roughness value --> Blinn exponent\n"
|
||||
" float aPower = max (2.f / (theBSDF.Ks.w * theBSDF.Ks.w) - 2.f, 0.f);\n"
|
||||
"\n"
|
||||
" aPDF = aPd * abs (theInput.z / M_PI) +\n"
|
||||
" aPs * (aPower + 2.f) * (1.f / M_2_PI) * pow (aHalf.z, aPower + 1.f) / (4.f * dot (theInput, aHalf));\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" return aPDF / aReflection;\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"//! Tool macro to handle sampling of particular BxDF\n"
|
||||
"#define PICK_BXDF(p, k) aPDF = p / aReflection; theWeight *= k / aPDF;\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : SampleBsdf\n"
|
||||
"// purpose : Samples specified composite material (BSDF)\n"
|
||||
"//=======================================================================\n"
|
||||
"float SampleBsdf (in SMaterial theBSDF,\n"
|
||||
" in vec3 theOutput,\n"
|
||||
" out vec3 theInput,\n"
|
||||
" inout vec3 theWeight,\n"
|
||||
" inout bool theInside)\n"
|
||||
"{\n"
|
||||
" // compute probability of each reflection type (BxDF)\n"
|
||||
" float aPd = convolve (theBSDF.Kd.rgb, theWeight);\n"
|
||||
" float aPs = convolve (theBSDF.Ks.rgb, theWeight);\n"
|
||||
" float aPr = convolve (theBSDF.Kr.rgb, theWeight);\n"
|
||||
" float aPt = convolve (theBSDF.Kt.rgb, theWeight);\n"
|
||||
"\n"
|
||||
" float aReflection = aPd + aPs + aPr + aPt;\n"
|
||||
"\n"
|
||||
" // choose BxDF component to sample\n"
|
||||
" float aKsi = aReflection * RandFloat();\n"
|
||||
"\n"
|
||||
" theBounce = NON_SPECULAR_BOUNCE;\n"
|
||||
" // BxDF's PDF of sampled direction\n"
|
||||
" float aPDF = 0.f;\n"
|
||||
"\n"
|
||||
" if (aKsi < aPd) // diffuse reflection\n"
|
||||
" {\n"
|
||||
" sampleLambertianReflection (theOutput, theInput);\n"
|
||||
" PICK_BXDF (aPd, theBSDF.Kd.rgb);\n"
|
||||
"\n"
|
||||
" theWeight *= theMaterial.Kd.rgb * (aReflection / aPd);\n"
|
||||
" theWeight *= SampleLambertianReflection (theOutput, theInput, aPDF);\n"
|
||||
" }\n"
|
||||
" else if (aKsi < aPd + aPs) // glossy reflection\n"
|
||||
" else if (aKsi < aPd + aPs) // glossy reflection\n"
|
||||
" {\n"
|
||||
" theWeight *= theMaterial.Ks.rgb * (aReflection / aPs) *\n"
|
||||
" sampleBlinnReflection (theOutput, theInput, theMaterial.Fresnel, theMaterial.Ks.w);\n"
|
||||
" }\n"
|
||||
" else if (aKsi < aPd + aPs + aPr) // specular reflection\n"
|
||||
" {\n"
|
||||
" theWeight *= theMaterial.Kr.rgb * (aReflection / aPr) *\n"
|
||||
" sampleSpecularReflection (theOutput, theInput, theMaterial.Fresnel);\n"
|
||||
" PICK_BXDF (aPs, theBSDF.Ks.rgb);\n"
|
||||
"\n"
|
||||
" theBounce = SPEC_REFLECT_BOUNCE; // specular bounce\n"
|
||||
" theWeight *= SampleBlinnReflection (theOutput, theInput, theBSDF.Fresnel, theBSDF.Ks.w, aPDF);\n"
|
||||
" }\n"
|
||||
" else // specular transmission\n"
|
||||
" else if (aKsi < aPd + aPs + aPr) // specular reflection\n"
|
||||
" {\n"
|
||||
" theWeight *= theMaterial.Kt.rgb * (aReflection / aPt) *\n"
|
||||
" sampleSpecularTransmission (theOutput, theInput, theBounce, theWeight, theMaterial.Fresnel);\n"
|
||||
" PICK_BXDF (aPr, theBSDF.Kr.rgb);\n"
|
||||
"\n"
|
||||
" aPDF = MAXFLOAT;\n"
|
||||
"\n"
|
||||
" theWeight *= SampleSpecularReflection (theOutput, theInput, theBSDF.Fresnel);\n"
|
||||
" }\n"
|
||||
" else if (aKsi < aReflection) // specular transmission\n"
|
||||
" {\n"
|
||||
" PICK_BXDF (aPt, theBSDF.Kt.rgb);\n"
|
||||
"\n"
|
||||
" aPDF = MAXFLOAT;\n"
|
||||
"\n"
|
||||
" theWeight *= SampleSpecularTransmission (theOutput, theInput, theWeight, theBSDF.Fresnel, theInside);\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" // path termination for extra small weights\n"
|
||||
" theWeight = mix (theWeight, ZERO, float (aReflection < 1e-3f));\n"
|
||||
" theWeight = mix (ZERO, theWeight, step (FLT_EPSILON, aReflection));\n"
|
||||
"\n"
|
||||
" return aPDF;\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"//////////////////////////////////////////////////////////////////////////////////////////////\n"
|
||||
"// Handlers and samplers for light sources\n"
|
||||
"//////////////////////////////////////////////////////////////////////////////////////////////\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : handlePointLight\n"
|
||||
"// purpose :\n"
|
||||
"//=======================================================================\n"
|
||||
"float handlePointLight (in vec3 theInput, in vec3 theToLight, in float theRadius, in float theDistance)\n"
|
||||
"{\n"
|
||||
" float aDistance = dot (theToLight, theToLight);\n"
|
||||
"\n"
|
||||
" float aCosMax = inversesqrt (1.f + theRadius * theRadius / aDistance);\n"
|
||||
"\n"
|
||||
" return float (aDistance < theDistance * theDistance) *\n"
|
||||
" step (aCosMax, dot (theToLight, theInput) * inversesqrt (aDistance));\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : handleDirectLight\n"
|
||||
"// purpose :\n"
|
||||
"//=======================================================================\n"
|
||||
"float handleDirectLight (in vec3 theInput, in vec3 theToLight, in float theCosMax)\n"
|
||||
"{\n"
|
||||
" return step (theCosMax, dot (theInput, theToLight));\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : sampleLight\n"
|
||||
"// purpose : General sampling function for directional and point lights\n"
|
||||
"//=======================================================================\n"
|
||||
"vec3 sampleLight (in vec3 theToLight, inout float theDistance, in bool isInfinite, in float theSmoothness, inout float thePDF)\n"
|
||||
"{\n"
|
||||
" SLocalSpace aSpace = LocalSpace (theToLight * (1.f / theDistance));\n"
|
||||
"\n"
|
||||
" // for point lights smoothness defines radius\n"
|
||||
" float aCosMax = isInfinite ? theSmoothness :\n"
|
||||
" inversesqrt (1.f + theSmoothness * theSmoothness / (theDistance * theDistance));\n"
|
||||
"\n"
|
||||
" float aKsi1 = RandFloat();\n"
|
||||
" float aKsi2 = RandFloat();\n"
|
||||
"\n"
|
||||
" float aTmp = 1.f - aKsi2 * (1.f - aCosMax);\n"
|
||||
"\n"
|
||||
" vec3 anInput = vec3 (cos (2.f * M_PI * aKsi1),\n"
|
||||
" sin (2.f * M_PI * aKsi1),\n"
|
||||
" aTmp);\n"
|
||||
"\n"
|
||||
" anInput.xy *= sqrt (1.f - aTmp * aTmp);\n"
|
||||
"\n"
|
||||
" thePDF *= (aCosMax < 1.f) ? 1.f / (2.f * M_PI) / (1.f - aCosMax) : 1.f;\n"
|
||||
"\n"
|
||||
" return normalize (fromLocalSpace (anInput, aSpace));\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"// =======================================================================\n"
|
||||
"// function : Latlong\n"
|
||||
"// purpose : Converts world direction to environment texture coordinates\n"
|
||||
@ -555,49 +546,128 @@ static const char Shaders_PathtraceBase_fs[] =
|
||||
" aPsi * 0.3183098f);\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : SampleLight\n"
|
||||
"// purpose : General sampling function for directional and point lights\n"
|
||||
"//=======================================================================\n"
|
||||
"vec3 SampleLight (in vec3 theToLight, inout float theDistance, in bool isInfinite, in float theSmoothness, inout float thePDF)\n"
|
||||
"{\n"
|
||||
" SLocalSpace aSpace = buildLocalSpace (theToLight * (1.f / theDistance));\n"
|
||||
"\n"
|
||||
" // for point lights smoothness defines radius\n"
|
||||
" float aCosMax = isInfinite ? theSmoothness :\n"
|
||||
" inversesqrt (1.f + theSmoothness * theSmoothness / (theDistance * theDistance));\n"
|
||||
"\n"
|
||||
" float aKsi1 = RandFloat();\n"
|
||||
" float aKsi2 = RandFloat();\n"
|
||||
"\n"
|
||||
" float aTmp = 1.f - aKsi2 * (1.f - aCosMax);\n"
|
||||
"\n"
|
||||
" vec3 anInput = vec3 (cos (M_2_PI * aKsi1),\n"
|
||||
" sin (M_2_PI * aKsi1),\n"
|
||||
" aTmp);\n"
|
||||
"\n"
|
||||
" anInput.xy *= sqrt (1.f - aTmp * aTmp);\n"
|
||||
"\n"
|
||||
" thePDF = (aCosMax < 1.f) ? (thePDF / M_2_PI) / (1.f - aCosMax) : MAXFLOAT;\n"
|
||||
"\n"
|
||||
" return normalize (fromLocalSpace (anInput, aSpace));\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : HandlePointLight\n"
|
||||
"// purpose :\n"
|
||||
"//=======================================================================\n"
|
||||
"float HandlePointLight (in vec3 theInput, in vec3 theToLight, in float theRadius, in float theDistance, inout float thePDF)\n"
|
||||
"{\n"
|
||||
" float aCosMax = inversesqrt (1.f + theRadius * theRadius / (theDistance * theDistance));\n"
|
||||
"\n"
|
||||
" float aVisibility = step (aCosMax, dot (theInput, theToLight));\n"
|
||||
"\n"
|
||||
" thePDF *= step (-1.f, -aCosMax) * aVisibility * (1.f / M_2_PI) / (1.f - aCosMax);\n"
|
||||
"\n"
|
||||
" return aVisibility;\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"//=======================================================================\n"
|
||||
"// function : HandleDistantLight\n"
|
||||
"// purpose :\n"
|
||||
"//=======================================================================\n"
|
||||
"float HandleDistantLight (in vec3 theInput, in vec3 theToLight, in float theCosMax, inout float thePDF)\n"
|
||||
"{\n"
|
||||
" float aVisibility = step (theCosMax, dot (theInput, theToLight));\n"
|
||||
"\n"
|
||||
" thePDF *= step (-1.f, -theCosMax) * aVisibility * (1.f / M_2_PI) / (1.f - theCosMax);\n"
|
||||
"\n"
|
||||
" return aVisibility;\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"// =======================================================================\n"
|
||||
"// function: intersectLight\n"
|
||||
"// function: IntersectLight\n"
|
||||
"// purpose : Checks intersections with light sources\n"
|
||||
"// =======================================================================\n"
|
||||
"vec3 intersectLight (in SRay theRay, in bool isViewRay, in int theBounce, in float theDistance)\n"
|
||||
"vec3 IntersectLight (in SRay theRay, in int theDepth, in float theHitDistance, out float thePDF)\n"
|
||||
"{\n"
|
||||
" vec3 aRadiance = ZERO;\n"
|
||||
" vec3 aTotalRadiance = ZERO;\n"
|
||||
"\n"
|
||||
" if ((isViewRay || IS_REFR_SPEC_BOUNCE(theBounce)) && uSphereMapForBack == 0)\n"
|
||||
" {\n"
|
||||
" aRadiance = BackgroundColor().xyz;\n"
|
||||
" }\n"
|
||||
" else\n"
|
||||
" {\n"
|
||||
" aRadiance = FetchEnvironment (Latlong (theRay.Direct)).xyz;\n"
|
||||
" }\n"
|
||||
" thePDF = 0.f; // PDF of sampling light sources\n"
|
||||
"\n"
|
||||
" // Apply gamma correction (gamma is 2)\n"
|
||||
" aRadiance = aRadiance * aRadiance * float (theDistance == MAXFLOAT);\n"
|
||||
"\n"
|
||||
" for (int aLightIdx = 0; aLightIdx < uLightCount && (isViewRay || IS_ANY_SPEC_BOUNCE(theBounce)); ++aLightIdx)\n"
|
||||
" for (int aLightIdx = 0; aLightIdx < uLightCount; ++aLightIdx)\n"
|
||||
" {\n"
|
||||
" vec4 aLight = texelFetch (\n"
|
||||
" uRaytraceLightSrcTexture, LIGHT_POS (aLightIdx));\n"
|
||||
" vec4 aParam = texelFetch (\n"
|
||||
" uRaytraceLightSrcTexture, LIGHT_PWR (aLightIdx));\n"
|
||||
"\n"
|
||||
" // W component: 0 for infinite light and 1 for point light\n"
|
||||
" aLight.xyz -= mix (ZERO, theRay.Origin, aLight.w);\n"
|
||||
"\n"
|
||||
" float aPDF = 1.f / uLightCount;\n"
|
||||
"\n"
|
||||
" if (aLight.w != 0.f) // point light source\n"
|
||||
" {\n"
|
||||
" aRadiance += aParam.rgb * handlePointLight (\n"
|
||||
" theRay.Direct, aLight.xyz - theRay.Origin, aParam.w /* radius */, theDistance);\n"
|
||||
" float aCenterDst = length (aLight.xyz);\n"
|
||||
"\n"
|
||||
" if (aCenterDst < theHitDistance)\n"
|
||||
" {\n"
|
||||
" float aVisibility = HandlePointLight (\n"
|
||||
" theRay.Direct, normalize (aLight.xyz), aParam.w /* radius */, aCenterDst, aPDF);\n"
|
||||
"\n"
|
||||
" if (aVisibility > 0.f)\n"
|
||||
" {\n"
|
||||
" theHitDistance = aCenterDst;\n"
|
||||
" aTotalRadiance = aParam.rgb;\n"
|
||||
"\n"
|
||||
" thePDF = aPDF;\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
" else if (theDistance == MAXFLOAT) // directional light source\n"
|
||||
" else if (theHitDistance == MAXFLOAT) // directional light source\n"
|
||||
" {\n"
|
||||
" aRadiance += aParam.rgb * handleDirectLight (theRay.Direct, aLight.xyz, aParam.w /* angle cosine */);\n"
|
||||
" aTotalRadiance += aParam.rgb * HandleDistantLight (\n"
|
||||
" theRay.Direct, aLight.xyz, aParam.w /* angle cosine */, aPDF);\n"
|
||||
"\n"
|
||||
" thePDF += aPDF;\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" return aRadiance;\n"
|
||||
" if (thePDF == 0.f && theHitDistance == MAXFLOAT) // light source not found\n"
|
||||
" {\n"
|
||||
" if (theDepth + uSphereMapForBack == 0) // view ray and map is hidden\n"
|
||||
" {\n"
|
||||
" aTotalRadiance = pow (BackgroundColor().rgb, vec3 (2.f));\n"
|
||||
" }\n"
|
||||
" else\n"
|
||||
" {\n"
|
||||
" aTotalRadiance = pow (FetchEnvironment (Latlong (theRay.Direct)).rgb, vec3 (2.f));\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
" \n"
|
||||
" return aTotalRadiance;\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"#define MIN_THROUGHPUT vec3 (0.02f)\n"
|
||||
"#define MIN_CONTRIBUTION vec3 (0.01f)\n"
|
||||
"#define MIN_THROUGHPUT vec3 (1.0e-3f)\n"
|
||||
"#define MIN_CONTRIBUTION vec3 (1.0e-2f)\n"
|
||||
"\n"
|
||||
"#define MATERIAL_KD(index) (18 * index + 11)\n"
|
||||
"#define MATERIAL_KR(index) (18 * index + 12)\n"
|
||||
@ -621,29 +691,32 @@ static const char Shaders_PathtraceBase_fs[] =
|
||||
" vec3 aRadiance = ZERO;\n"
|
||||
" vec3 aThroughput = UNIT;\n"
|
||||
"\n"
|
||||
" int aBounce = 0; // type of previous hit point\n"
|
||||
" int aTrsfId = 0; // offset of object transform\n"
|
||||
" int aTransfID = 0; // ID of object transformation\n"
|
||||
" bool aInMedium = false; // is the ray inside an object\n"
|
||||
"\n"
|
||||
" bool isInMedium = false;\n"
|
||||
" float aExpPDF = 1.f;\n"
|
||||
" float aImpPDF = 1.f;\n"
|
||||
"\n"
|
||||
" for (int aDepth = 0; aDepth < NB_BOUNCES; ++aDepth)\n"
|
||||
" {\n"
|
||||
" SIntersect aHit = SIntersect (MAXFLOAT, vec2 (ZERO), ZERO);\n"
|
||||
"\n"
|
||||
" ivec4 aTriIndex = SceneNearestHit (theRay, theInverse, aHit, aTrsfId);\n"
|
||||
" ivec4 aTriIndex = SceneNearestHit (theRay, theInverse, aHit, aTransfID);\n"
|
||||
"\n"
|
||||
" // check implicit path\n"
|
||||
" vec3 aLe = intersectLight (theRay,\n"
|
||||
" aDepth == 0 /* is view ray */, aBounce, aHit.Time);\n"
|
||||
" vec3 aLe = IntersectLight (theRay, aDepth, aHit.Time, aExpPDF);\n"
|
||||
"\n"
|
||||
" if (any (greaterThan (aLe, ZERO)) || aTriIndex.x == -1)\n"
|
||||
" {\n"
|
||||
" aRadiance += aThroughput * aLe; break; // terminate path\n"
|
||||
" float aMIS = (aDepth == 0 || aImpPDF == MAXFLOAT) ? 1.f :\n"
|
||||
" aImpPDF * aImpPDF / (aExpPDF * aExpPDF + aImpPDF * aImpPDF);\n"
|
||||
"\n"
|
||||
" aRadiance += aThroughput * aLe * aMIS; break; // terminate path\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" vec3 aInvTransf0 = texelFetch (uSceneTransformTexture, aTrsfId + 0).xyz;\n"
|
||||
" vec3 aInvTransf1 = texelFetch (uSceneTransformTexture, aTrsfId + 1).xyz;\n"
|
||||
" vec3 aInvTransf2 = texelFetch (uSceneTransformTexture, aTrsfId + 2).xyz;\n"
|
||||
" vec3 aInvTransf0 = texelFetch (uSceneTransformTexture, aTransfID + 0).xyz;\n"
|
||||
" vec3 aInvTransf1 = texelFetch (uSceneTransformTexture, aTransfID + 1).xyz;\n"
|
||||
" vec3 aInvTransf2 = texelFetch (uSceneTransformTexture, aTransfID + 2).xyz;\n"
|
||||
"\n"
|
||||
" // compute geometrical normal\n"
|
||||
" aHit.Normal = normalize (vec3 (dot (aInvTransf0, aHit.Normal),\n"
|
||||
@ -652,7 +725,7 @@ static const char Shaders_PathtraceBase_fs[] =
|
||||
"\n"
|
||||
" theRay.Origin += theRay.Direct * aHit.Time; // get new intersection point\n"
|
||||
"\n"
|
||||
" // Evaluate depth on first hit\n"
|
||||
" // evaluate depth on first hit\n"
|
||||
" if (aDepth == 0)\n"
|
||||
" {\n"
|
||||
" vec4 aNDCPoint = uViewMat * vec4 (theRay.Origin, 1.f);\n"
|
||||
@ -697,7 +770,7 @@ static const char Shaders_PathtraceBase_fs[] =
|
||||
" dot (aInvTransf1, aNormal),\n"
|
||||
" dot (aInvTransf2, aNormal)));\n"
|
||||
"\n"
|
||||
" SLocalSpace aSpace = LocalSpace (aNormal);\n"
|
||||
" SLocalSpace aSpace = buildLocalSpace (aNormal);\n"
|
||||
"\n"
|
||||
" // account for self-emission (not stored in the material)\n"
|
||||
" aRadiance += aThroughput * texelFetch (\n"
|
||||
@ -705,6 +778,8 @@ static const char Shaders_PathtraceBase_fs[] =
|
||||
"\n"
|
||||
" if (uLightCount > 0 && convolve (aMaterial.Kd.rgb + aMaterial.Ks.rgb, aThroughput) > 0.f)\n"
|
||||
" {\n"
|
||||
" aExpPDF = 1.f / uLightCount;\n"
|
||||
"\n"
|
||||
" int aLightIdx = min (int (floor (RandFloat() * uLightCount)), uLightCount - 1);\n"
|
||||
"\n"
|
||||
" vec4 aLight = texelFetch (\n"
|
||||
@ -715,15 +790,21 @@ static const char Shaders_PathtraceBase_fs[] =
|
||||
" // 'w' component is 0 for infinite light and 1 for point light\n"
|
||||
" aLight.xyz -= mix (ZERO, theRay.Origin, aLight.w);\n"
|
||||
"\n"
|
||||
" float aPDF = 1.f / uLightCount, aDistance = length (aLight.xyz);\n"
|
||||
" float aDistance = length (aLight.xyz);\n"
|
||||
"\n"
|
||||
" aLight.xyz = sampleLight (aLight.xyz, aDistance,\n"
|
||||
" aLight.w == 0.f /* is infinite */, aParam.w /* max cos or radius */, aPDF);\n"
|
||||
" aLight.xyz = SampleLight (aLight.xyz, aDistance,\n"
|
||||
" aLight.w == 0.f /* is infinite */, aParam.w /* max cos or radius */, aExpPDF);\n"
|
||||
"\n"
|
||||
" vec3 aContrib = (1.f / aPDF) * aParam.rgb /* Le */ * handleMaterial (\n"
|
||||
" aImpPDF = BsdfPdf (aMaterial,\n"
|
||||
" toLocalSpace (-theRay.Direct, aSpace), toLocalSpace (aLight.xyz, aSpace), aThroughput);\n"
|
||||
"\n"
|
||||
" // MIS weight including division by explicit PDF\n"
|
||||
" float aMIS = (aExpPDF == MAXFLOAT) ? 1.f : aExpPDF / (aExpPDF * aExpPDF + aImpPDF * aImpPDF);\n"
|
||||
"\n"
|
||||
" vec3 aContrib = aMIS * aParam.rgb /* Le */ * HandleMaterial (\n"
|
||||
" aMaterial, toLocalSpace (aLight.xyz, aSpace), toLocalSpace (-theRay.Direct, aSpace));\n"
|
||||
"\n"
|
||||
" if (any (greaterThan (aContrib, MIN_CONTRIBUTION))) // first check if light source is important\n"
|
||||
" if (any (greaterThan (aContrib, MIN_CONTRIBUTION))) // check if light source is important\n"
|
||||
" {\n"
|
||||
" SRay aShadow = SRay (theRay.Origin + aLight.xyz * uSceneEpsilon, aLight.xyz);\n"
|
||||
"\n"
|
||||
@ -737,32 +818,29 @@ static const char Shaders_PathtraceBase_fs[] =
|
||||
" }\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" vec3 anInput;\n"
|
||||
"\n"
|
||||
" sampleMaterial (aMaterial,\n"
|
||||
" toLocalSpace (-theRay.Direct, aSpace), anInput, aThroughput, aBounce);\n"
|
||||
"\n"
|
||||
" if (isInMedium)\n"
|
||||
" if (aInMedium) // handle attenuation\n"
|
||||
" {\n"
|
||||
" aThroughput *= exp (-aHit.Time *\n"
|
||||
" aMaterial.Absorption.w * (UNIT - aMaterial.Absorption.rgb));\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" isInMedium = IS_REFR_SPEC_BOUNCE(aBounce) ? !isInMedium : isInMedium;\n"
|
||||
" vec3 anInput = UNIT; // sampled input direction\n"
|
||||
"\n"
|
||||
"#ifndef RUSSIAN_ROULETTE\n"
|
||||
" if (all (lessThan (aThroughput, MIN_THROUGHPUT)))\n"
|
||||
" {\n"
|
||||
" aDepth = INVALID_BOUNCES; // terminate path\n"
|
||||
" }\n"
|
||||
"#else\n"
|
||||
" float aSurvive = aDepth < 3 ? 1.f : min (dot (LUMA, aThroughput), 0.95f);\n"
|
||||
" aImpPDF = SampleBsdf (aMaterial,\n"
|
||||
" toLocalSpace (-theRay.Direct, aSpace), anInput, aThroughput, aInMedium);\n"
|
||||
"\n"
|
||||
" if (RandFloat() > aSurvive)\n"
|
||||
" float aSurvive = 1.f;\n"
|
||||
"\n"
|
||||
"#ifdef RUSSIAN_ROULETTE\n"
|
||||
" aSurvive = aDepth < 3 ? 1.f : min (dot (LUMA, aThroughput), 0.95f);\n"
|
||||
"#endif\n"
|
||||
"\n"
|
||||
" if (RandFloat() > aSurvive || all (lessThanEqual (aThroughput, MIN_THROUGHPUT)))\n"
|
||||
" {\n"
|
||||
" aDepth = INVALID_BOUNCES; // terminate path\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
"#ifdef RUSSIAN_ROULETTE\n"
|
||||
" aThroughput /= aSurvive;\n"
|
||||
"#endif\n"
|
||||
"\n"
|
||||
|
@ -146,7 +146,9 @@ static const char Shaders_RaytraceBase_fs[] =
|
||||
"#define AXIS_Y vec3 (0.0f, 1.0f, 0.0f)\n"
|
||||
"#define AXIS_Z vec3 (0.0f, 0.0f, 1.0f)\n"
|
||||
"\n"
|
||||
"#define M_PI 3.14159265f\n"
|
||||
"#define M_PI 3.141592653f\n"
|
||||
"#define M_2_PI 6.283185307f\n"
|
||||
"#define M_PI_2 1.570796327f\n"
|
||||
"\n"
|
||||
"#define LUMA vec3 (0.2126f, 0.7152f, 0.0722f)\n"
|
||||
"\n"
|
||||
@ -838,8 +840,8 @@ static const char Shaders_RaytraceBase_fs[] =
|
||||
"// =======================================================================\n"
|
||||
"vec4 FetchEnvironment (in vec2 theTexCoord)\n"
|
||||
"{\n"
|
||||
" return mix (vec4 (0.0f, 0.0f, 0.0f, 1.0f),\n"
|
||||
" textureLod (uEnvironmentMapTexture, theTexCoord, 0.0f), float (uSphereMapEnabled));\n"
|
||||
" return uSphereMapEnabled == 0 ?\n"
|
||||
" vec4 (0.f, 0.f, 0.f, 1.f) : textureLod (uEnvironmentMapTexture, theTexCoord, 0.f);\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"// =======================================================================\n"
|
||||
@ -1023,8 +1025,8 @@ static const char Shaders_RaytraceBase_fs[] =
|
||||
"\n"
|
||||
" if (aVisibility > 0.0f)\n"
|
||||
" {\n"
|
||||
" vec3 aIntensity = vec3 (texelFetch (\n"
|
||||
" uRaytraceLightSrcTexture, LIGHT_PWR (aLightIdx)));\n"
|
||||
" vec3 aIntensity = min (UNIT, vec3 (texelFetch (\n"
|
||||
" uRaytraceLightSrcTexture, LIGHT_PWR (aLightIdx))));\n"
|
||||
"\n"
|
||||
" float aRdotV = dot (reflect (aLight.xyz, aSidedNormal), theRay.Direct);\n"
|
||||
"\n"
|
||||
|
@ -21,7 +21,7 @@ static const char Shaders_RaytraceRender_fs[] =
|
||||
"\n"
|
||||
"//! Maximum radiance that can be added to the pixel. Decreases noise\n"
|
||||
"//! level, but introduces some bias.\n"
|
||||
"#define MAX_RADIANCE vec3 (25.f)\n"
|
||||
"#define MAX_RADIANCE vec3 (50.f)\n"
|
||||
"\n"
|
||||
"// =======================================================================\n"
|
||||
"// function : main\n"
|
||||
|
@ -70,6 +70,8 @@ V3d_DirectionalLight::V3d_DirectionalLight (const Handle(V3d_Viewer)& theViewer,
|
||||
SetHeadlight (theIsHeadlight);
|
||||
SetTarget (0., 0., 0.);
|
||||
SetPosition (-aV.X(), -aV.Y(), -aV.Z());
|
||||
SetSmoothAngle (0.2);
|
||||
SetIntensity (20.0);
|
||||
}
|
||||
|
||||
// =======================================================================
|
||||
|
7
tests/v3d/raytrace/sample_ball
Normal file
7
tests/v3d/raytrace/sample_ball
Normal file
@ -0,0 +1,7 @@
|
||||
puts "============"
|
||||
puts "Visualization - Path Tracing, Ball sample"
|
||||
puts "============"
|
||||
puts ""
|
||||
|
||||
source $env(CSF_OCCTSamplesPath)/tcl/pathtrace_ball.tcl
|
||||
vdump $imagedir/${casename}_zoom.png
|
6
tests/v3d/raytrace/sample_cube
Normal file
6
tests/v3d/raytrace/sample_cube
Normal file
@ -0,0 +1,6 @@
|
||||
puts "============"
|
||||
puts "Visualization - Path Tracing, Cube sample"
|
||||
puts "============"
|
||||
puts ""
|
||||
|
||||
source $env(CSF_OCCTSamplesPath)/tcl/pathtrace_cube.tcl
|
Loading…
x
Reference in New Issue
Block a user