TTK
Loading...
Searching...
No Matches
ttkCinemaDarkroomSSAO.cpp
Go to the documentation of this file.
2
3#include <vtkImageData.h>
4#include <vtkInformation.h>
5#include <vtkObjectFactory.h>
6
8
10 this->setDebugMsgPrefix("CinemaDarkroomSSAO");
11}
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
20uniform sampler2D tex0;
21varying vec4 vPos;
22
23float readDepth( const in vec2 coord ){
24 return texture2D( tex0, coord ).r;
25}
26
27vec3 computePos( const in vec2 coord ){
28 return vec3(
29 coord,
30 readDepth(coord)
31 );
32}
33
34vec3 computeNormal(){
35 vec2 pixelSize = 1./cResolution;
36
37 vec3 eps = 2.0*vec3( pixelSize.x, pixelSize.y, 0 );
38 float depthN = readDepth(vPos.xy + eps.zy);
39 float depthE = readDepth(vPos.xy + eps.xz);
40 float depthS = readDepth(vPos.xy - eps.zy);
41 float depthW = readDepth(vPos.xy - eps.xz);
42
43 float dzdx = (depthE - depthW) / 2.0;
44 float dzdy = (depthN - depthS) / 2.0;
45
46 return normalize(vec3(-dzdx, -dzdy, 1.0));
47}
48
49const vec2 poissonDisk[32] = vec2[](
50 vec2( -0.94201624, -0.39906216 ),
51 vec2( 0.94558609, -0.76890725 ),
52 vec2( -0.094184101, -0.92938870 ),
53 vec2( 0.34495938, 0.29387760 ),
54 vec2( -0.91588581, 0.45771432 ),
55 vec2( -0.81544232, -0.87912464 ),
56 vec2( -0.38277543, 0.27676845 ),
57 vec2( 0.97484398, 0.75648379 ),
58 vec2( 0.44323325, -0.97511554 ),
59 vec2( 0.53742981, -0.47373420 ),
60 vec2( -0.26496911, -0.41893023 ),
61 vec2( 0.79197514, 0.19090188 ),
62 vec2( -0.24188840, 0.99706507 ),
63 vec2( -0.81409955, 0.91437590 ),
64 vec2( 0.19984126, 0.78641367 ),
65 vec2( 0.14383161, -0.14100790 ),
66 vec2( -0.44201624, -0.29906216 ),
67 vec2( 0.94558609, -0.46890725 ),
68 vec2( -0.194184101, -0.42938870 ),
69 vec2( 0.24495938, 0.99387760 ),
70 vec2( -0.31588581, 0.45771432 ),
71 vec2( -0.81544232, -0.87912464 ),
72 vec2( -0.08277543, 0.87676845 ),
73 vec2( 0.57484398, 0.55648379 ),
74 vec2( 0.74323325, -0.27511554 ),
75 vec2( 0.44298431, -0.47373420 ),
76 vec2( -0.21196911, -0.22893023 ),
77 vec2( 0.79197514, 0.12020188 ),
78 vec2( -0.11184840, 0.99706507 ),
79 vec2( -0.4309955, 0.111437590 ),
80 vec2( 0.12344126, 0.78641367 ),
81 vec2( 0.2183161, -0.89100790 )
82);
83
84void main(){
85 vec3 pos = computePos(vPos.xy);
86 vec3 n = computeNormal();
87
88 float occlusion = 0.0;
89
90 vec2 aspect = vec2(cResolution.y/cResolution.x, 1) * cRadius;
91
92 for (int i = 0; i < 32; ++i){
93 // get sample
94 vec2 sampleTexCoord = vPos.xy + poissonDisk[i] * aspect;
95 vec3 samplePos = computePos(sampleTexCoord);
96 vec3 sampleDir = normalize(pos - samplePos);
97
98 // distance between SURFACE-POSITION and SAMPLE-POSITION
99 float VPdistSP = distance(pos, samplePos);
100
101 // angle between SURFACE-NORMAL and SAMPLE-DIRECTION (vector from SURFACE-POSITION to SAMPLE-POSITION)
102 float dotNS = max(dot(sampleDir, n), 0.0);
103
104 // occlusion factor
105 float a = 1.0 - smoothstep(cDiffArea, cDiffArea * 2.0, VPdistSP);
106
107 // aggregate
108 occlusion += a*dotNS;
109 }
110
111 float ao = 1.-occlusion/32.0;
112 gl_FragData[0] = vec4( ao,ao,ao, 1 );
113}
114 )");
115}
116
118 vtkInformationVector **inputVector,
119 vtkInformationVector *outputVector) {
120
121 auto inputImage = vtkImageData::GetData(inputVector[0]);
122 auto outputImage = vtkImageData::GetData(outputVector);
123 outputImage->ShallowCopy(inputImage);
124
125 this->InitRenderer(outputImage);
126
127 this->AddReplacement("cRadius", {this->Radius});
128 this->AddReplacement("cDiffArea", {this->DiffArea});
129
130 if(!this->AddTexture(outputImage, 0, 0))
131 return 0;
132
133 this->Render(outputImage, "SSAO");
134
135 return 1;
136}
#define ttkNotUsed(x)
Mark function/method parameters that are not used in the function body at all.
Definition: BaseClass.h:47
Screen Space Ambient Occlusion.
~ttkCinemaDarkroomSSAO() override
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) 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(ttkCinemaDarkroomSSAO)