TTK
Loading...
Searching...
No Matches
ttkCinemaDarkroomSSSAO.cpp
Go to the documentation of this file.
2
3#include <vtkImageData.h>
4#include <vtkInformation.h>
5#include <vtkObjectFactory.h>
6
8
12
14
16 return std::string(R"(
17//VTK::System::Dec // always start with these lines in your FS
18//VTK::Output::Dec // always start with these lines in your FS
19
20// -----------------------------------------------------------------------------
21// Custom SSAO implementation based on Blender's Viewport SSAO (https://github.com/blender/blender)
22// Roots can be traced back to Arkano22 (https://www.gamedev.net/forums/topic/550699-ssao-no-halo-artifacts/)
23// and Martins Upitis (martinsh) (http://devlog-martinsh.blogspot.com/search/label/SSAO)
24// -----------------------------------------------------------------------------
25
26uniform sampler2D tex0;
27
28varying vec4 vPos;
29
30#define DL 2.399963229728653 // PI * ( 3.0 - sqrt( 5.0 ) )
31#define EULER 2.718281828459045
32
33float readDepth( const in vec2 coord ){
34 return texture2D( tex0, coord ).r;
35}
36
37const float gDisplace = 0.5; // gauss bell center
38float compareDepths( const in float depth1, const in float depth2, inout int far ) {
39 float garea = 16.0; // gauss bell width
40 float diff = ( depth1 - depth2 ) * 100.0; // depth difference (0-100)
41
42 // reduce left bell width to avoid self-shadowing
43 if(diff<gDisplace){
44 garea = cDiffArea;
45 } else {
46 far = 1;
47 }
48
49 float dd = diff - gDisplace;
50 return pow( EULER, -2.0 * ( dd * dd ) / ( garea * garea ) );
51}
52
53float calcAO( float depth, float dw, float dh, vec2 uv ) {
54 vec2 vv = vec2( dw, dh );
55 vec2 coord1 = uv + vv;
56 vec2 coord2 = uv - vv;
57 float temp1 = 0.0;
58 float temp2 = 0.0;
59 int far = 0;
60
61 temp1 = compareDepths( depth, readDepth( coord1 ), far );
62 if ( far > 0 ) {
63 temp2 = compareDepths( readDepth( coord2 ), depth, far );
64 temp1 += ( 1.0 - temp1 ) * temp2;
65 }
66 return temp1;
67}
68
69void main() {
70 float depth = readDepth( vPos.xy );
71
72 const float samplesF = cSamples;
73 float occlusion = 0.0;
74
75 float dz = 1.0 / samplesF;
76 float l = 0.0;
77 float z = 1.0 - dz / 2.0;
78
79 float aspect = cResolution.y/cResolution.x;
80
81 for(int i=0; i<cSamples; i++){
82 float r = sqrt( 1.0 - z ) * cRadius;
83 float pw = cos( l ) * r;
84 float ph = sin( l ) * r;
85 occlusion += calcAO( depth, pw * aspect, ph, vPos.xy );
86 z = z - dz;
87 l = l + DL;
88 }
89
90 float ao = 1.-occlusion/samplesF;
91 gl_FragColor = vec4(ao,ao,ao, 1 );
92}
93 )");
94}
95
97 vtkInformationVector **inputVector,
98 vtkInformationVector *outputVector) {
99
100 auto inputImage = vtkImageData::GetData(inputVector[0]);
101 auto outputImage = vtkImageData::GetData(outputVector);
102 outputImage->ShallowCopy(inputImage);
103
104 int dim[3];
105 outputImage->GetDimensions(dim);
106
107 this->InitRenderer(outputImage);
108
109 this->AddReplacement("cSamples", {(double)this->Samples}, true);
110 this->AddReplacement("cRadius", {this->Radius});
111 this->AddReplacement("cDiffArea", {this->DiffArea});
112
113 if(!this->AddTexture(outputImage, 0, 0))
114 return 0;
115
116 this->Render(outputImage, "SSSAO");
117
118 return 1;
119}
#define ttkNotUsed(x)
Mark function/method parameters that are not used in the function body at all.
Definition BaseClass.h:47
Scalable Screen Space Ambient Occlusion.
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
~ttkCinemaDarkroomSSSAO() override
std::string GetFragmentShaderCode() override
Base Class for all CinemaDarkroom Shaders.
virtual int Render(vtkImageData *image, const std::string &name)
int InitRenderer(vtkImageData *outputImage)
int AddTexture(vtkImageData *image, int arrayIdx, int textureIdx)
int AddReplacement(const std::string &name, const std::vector< double > &values, const bool &isInt=false)
void setDebugMsgPrefix(const std::string &prefix)
Definition Debug.h:364
vtkStandardNewMacro(ttkCinemaDarkroomSSSAO)