TTK
Loading...
Searching...
No Matches
ttkCinemaDarkroomFXAA.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
19attribute vec4 vertexMC;
20varying vec4 vPos;
21
22varying vec2 v_rgbNW;
23varying vec2 v_rgbNE;
24varying vec2 v_rgbSW;
25varying vec2 v_rgbSE;
26varying vec2 v_rgbM;
27
28void main() {
29 vPos = vertexMC/2. + vec4(0.5,0.5,0.5,0);
30
31 vec2 fragCoord = vPos.xy * cResolution;
32 vec2 inverseVP = 1.0 / cResolution.xy;
33 v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP;
34 v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP;
35 v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP;
36 v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP;
37 v_rgbM = vec2(fragCoord * inverseVP);
38
39 gl_Position = vertexMC;
40}
41
42 )");
43}
44
46 return std::string(R"(
47//VTK::System::Dec // always start with these lines in your FS
48//VTK::Output::Dec // always start with these lines in your FS
49
50varying vec4 vPos;
51
52varying vec2 v_rgbNW;
53varying vec2 v_rgbNE;
54varying vec2 v_rgbSW;
55varying vec2 v_rgbSE;
56varying vec2 v_rgbM;
57
58uniform sampler2D tex0;
59
60#ifndef FXAA_REDUCE_MIN
61 #define FXAA_REDUCE_MIN (1.0/ 128.0)
62#endif
63
64#ifndef FXAA_REDUCE_MUL
65 #define FXAA_REDUCE_MUL (1.0 / 8.0)
66#endif
67
68#ifndef FXAA_SPAN_MAX
69 #define FXAA_SPAN_MAX 8.0
70#endif
71
72vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution,
73 vec2 v_rgbNW, vec2 v_rgbNE,
74 vec2 v_rgbSW, vec2 v_rgbSE,
75 vec2 v_rgbM) {
76 vec4 color;
77 mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y);
78 vec3 rgbNW = texture2D(tex, v_rgbNW).xyz;
79 vec3 rgbNE = texture2D(tex, v_rgbNE).xyz;
80 vec3 rgbSW = texture2D(tex, v_rgbSW).xyz;
81 vec3 rgbSE = texture2D(tex, v_rgbSE).xyz;
82 vec4 texColor = texture2D(tex, v_rgbM);
83 vec3 rgbM = texColor.xyz;
84 vec3 luma = vec3(0.299, 0.587, 0.114);
85 float lumaNW = dot(rgbNW, luma);
86 float lumaNE = dot(rgbNE, luma);
87 float lumaSW = dot(rgbSW, luma);
88 float lumaSE = dot(rgbSE, luma);
89 float lumaM = dot(rgbM, luma);
90 float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
91 float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
92
93 mediump vec2 dir;
94 dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
95 dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
96
97 float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *
98 (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);
99
100 float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
101 dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
102 max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
103 dir * rcpDirMin)) * inverseVP;
104
105 vec3 rgbA = 0.5 * (
106 texture2D(tex, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz +
107 texture2D(tex, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz);
108
109 vec3 rgbB = rgbA * 0.5 + 0.25 * (
110 texture2D(tex, fragCoord * inverseVP + dir * -0.5).xyz +
111 texture2D(tex, fragCoord * inverseVP + dir * 0.5).xyz);
112
113 float lumaB = dot(rgbB, luma);
114 if ((lumaB < lumaMin) || (lumaB > lumaMax))
115 color = vec4(rgbA, texColor.a);
116 else
117 color = vec4(rgbB, texColor.a);
118
119 return color;
120}
121
122void main() {
123 vec2 fragCoord = vPos.xy * cResolution;
124 gl_FragColor = fxaa(tex0, fragCoord, cResolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM);
125}
126 )");
127}
128
130 vtkInformationVector **inputVector,
131 vtkInformationVector *outputVector) {
132
133 auto inputImage = vtkImageData::GetData(inputVector[0]);
134 auto outputImage = vtkImageData::GetData(outputVector);
135 outputImage->ShallowCopy(inputImage);
136
137 this->InitRenderer(outputImage);
138
139 if(!this->AddTexture(outputImage, 0, 0))
140 return 0;
141
142 this->Render(outputImage, "FXAA");
143
144 return 1;
145}
#define ttkNotUsed(x)
Mark function/method parameters that are not used in the function body at all.
Definition BaseClass.h:47
Fast Approximate Anti-Aliasing.
~ttkCinemaDarkroomFXAA() override
std::string GetFragmentShaderCode() override
std::string GetVertexShaderCode() override
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) 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)
void setDebugMsgPrefix(const std::string &prefix)
Definition Debug.h:364
vtkStandardNewMacro(ttkCinemaDarkroomFXAA)