TTK
Loading...
Searching...
No Matches
ttkCinemaDarkroomSSDoF.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
20varying vec4 vPos;
21
22uniform sampler2D tex0;
23uniform sampler2D tex1;
24
25float readDepth( const in vec2 coord ){
26 return texture2D( tex1, coord ).r;
27}
28
29float computeCircleOfConfusion(
30 const in vec2 coord
31){
32 float s2 = readDepth(coord);
33 float c = cAperture * abs(s2-cFocalDepth);
34 return clamp(c, 0.0, cMaxBlur);
35}
36
37const vec2 poissonDisk[32] = vec2[](
38 vec2( -0.94201624, -0.39906216 ),
39 vec2( 0.94558609, -0.76890725 ),
40 vec2( -0.094184101, -0.92938870 ),
41 vec2( 0.34495938, 0.29387760 ),
42 vec2( -0.91588581, 0.45771432 ),
43 vec2( -0.81544232, -0.87912464 ),
44 vec2( -0.38277543, 0.27676845 ),
45 vec2( 0.97484398, 0.75648379 ),
46 vec2( 0.44323325, -0.97511554 ),
47 vec2( 0.53742981, -0.47373420 ),
48 vec2( -0.26496911, -0.41893023 ),
49 vec2( 0.79197514, 0.19090188 ),
50 vec2( -0.24188840, 0.99706507 ),
51 vec2( -0.81409955, 0.91437590 ),
52 vec2( 0.19984126, 0.78641367 ),
53 vec2( 0.14383161, -0.14100790 ),
54 vec2( -0.44201624, -0.29906216 ),
55 vec2( 0.94558609, -0.46890725 ),
56 vec2( -0.194184101, -0.42938870 ),
57 vec2( 0.24495938, 0.99387760 ),
58 vec2( -0.31588581, 0.45771432 ),
59 vec2( -0.81544232, -0.87912464 ),
60 vec2( -0.08277543, 0.87676845 ),
61 vec2( 0.57484398, 0.55648379 ),
62 vec2( 0.74323325, -0.27511554 ),
63 vec2( 0.44298431, -0.47373420 ),
64 vec2( -0.21196911, -0.22893023 ),
65 vec2( 0.79197514, 0.12020188 ),
66 vec2( -0.11184840, 0.99706507 ),
67 vec2( -0.4309955, 0.111437590 ),
68 vec2( 0.12344126, 0.78641367 ),
69 vec2( 0.2183161, -0.89100790 )
70);
71
72vec4 SSDoF(
73 const in vec2 coord
74){
75 float bleedingBias = 0.02;
76 float bleedingMult = 30.0;
77
78 float centerDepth = readDepth(coord);
79 float centerCoC = computeCircleOfConfusion(coord);
80
81 vec4 color = vec4(0);
82 float totalWeight = 0.0;
83
84 vec2 adjustedRadius = vec2(
85 cResolution[1]/cResolution[0],
86 1.0
87 )*cRadius;
88
89 for(int i=0; i<32; i++){
90 vec2 offset = poissonDisk[i] * adjustedRadius;
91
92 vec2 sampleCoords = coord + offset * centerCoC;
93 float sampleCoC = computeCircleOfConfusion(sampleCoords);
94
95 vec4 samplePixel = texture2D(tex0, sampleCoords);
96 float sampleDepth = readDepth(sampleCoords);
97
98 float weight = sampleDepth < centerDepth ? sampleCoC * bleedingMult : 1.0;
99 weight = (centerCoC > sampleCoC + bleedingBias) ? weight : 1.0;
100 weight = clamp(weight,0.0,1.0);
101
102 color += samplePixel*weight;
103 totalWeight += weight;
104 }
105
106 return color / totalWeight;
107}
108
109void main() {
110 gl_FragColor = SSDoF(vPos.xy);
111}
112 )");
113}
114
116 vtkInformationVector **inputVector,
117 vtkInformationVector *outputVector) {
118
119 auto inputImage = vtkImageData::GetData(inputVector[0]);
120 auto outputImage = vtkImageData::GetData(outputVector);
121 outputImage->ShallowCopy(inputImage);
122
123 this->InitRenderer(outputImage);
124
125 this->AddReplacement("cRadius", {this->Radius});
126 this->AddReplacement("cMaxBlur", {this->MaxBlur});
127 this->AddReplacement("cAperture", {this->Aperture});
128 this->AddReplacement("cFocalDepth", {this->FocalDepth});
129
130 if(!this->AddTexture(outputImage, 0, 0))
131 return 0;
132 if(!this->AddTexture(outputImage, 1, 1))
133 return 0;
134
135 this->Render(outputImage, "SSDoF");
136
137 return 1;
138}
#define ttkNotUsed(x)
Mark function/method parameters that are not used in the function body at all.
Definition BaseClass.h:47
Screen Space Depth of Field.
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
~ttkCinemaDarkroomSSDoF() 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(ttkCinemaDarkroomSSDoF)