4
#extension GL_ARB_draw_buffers : enable
6
// flag for depth test on/off
7
uniform bool depth_test;
9
// 2.0*size of current level / size of one level down
10
uniform vec2 level_ratio;
17
uniform float reconstruction_filter_size;
18
uniform float prefilter_size;
20
uniform sampler2D textureA;
21
uniform sampler2D textureB;
24
// tests if a point is inside an ellipse.
25
// Ellipse is centered at origin and point displaced by d.
26
// Radius is the half the ellipse's major axis.
27
// Minor axis is computed by normal direction.
28
float pointInEllipse(in vec2 d, in float radius, in vec3 normal){
29
float len = length(normal.xy);
36
// angle between normal and z direction
37
float angle = acos(normal.y);
41
float cos_angle = normal.y;
42
float sin_angle = sin(angle);
44
// rotate point to ellipse coordinate system
45
vec2 rotated_pos = vec2(d.x*cos_angle + d.y*sin_angle,
46
-d.x*sin_angle + d.y*cos_angle);
48
// major and minor axis
52
// include antialiasing filter
56
// inside ellipse test
57
float test = ((rotated_pos.x*rotated_pos.x)/(a*a)) + ((rotated_pos.y*rotated_pos.y)/(b*b));
59
if (test <= reconstruction_filter_size)
68
vec4 bufferA = vec4(0.0, 0.0, 0.0, 0.0);
69
vec4 bufferB = vec4(0.0, 0.0, 0.0, 0.0);
71
float valid_pixels = 0.0;
73
vec4 pixelA[4], pixelB[4];
75
vec2 center_coord = gl_TexCoord[0].st * level_ratio;
78
tex_coord[0].st = center_coord.st + offset.st;
80
tex_coord[1].s = center_coord.s - offset.s;
81
tex_coord[1].t = center_coord.t + offset.t;
83
tex_coord[2].s = center_coord.s + offset.s;
84
tex_coord[2].t = center_coord.t - offset.t;
86
tex_coord[3].st = center_coord.st - offset.st;
88
// Compute the front most pixel from lower level (minimum z
90
float dist_test = 0.0;
92
float zmax = -10000.0;
94
for (int i = 0; i < 4; ++i) {
95
pixelA[i] = texture2DLod (textureA, tex_coord[i].st, float(level-1)).xyzw;
96
if (pixelA[i].w > 0.0) {
97
pixelB[i] = texture2DLod (textureB, tex_coord[i].st, float(level-1)).xyzw;
98
dist_test = pointInEllipse(pixelB[i].zw, pixelA[i].w, pixelA[i].xyz);
100
if (dist_test != -10.0)
102
// test for minimum depth coordinate of valid ellipses
103
if (pixelB[i].x <= zmin) {
105
zmax = zmin + pixelB[i].y;
109
// if the ellipse does not reach the center ignore it in the averaging
115
float new_zmax = zmax;
117
// Gather pixels values
118
for (int i = 0; i < 4; ++i)
120
// Check if valid gather pixel or unspecified (or ellipse out of reach set above)
121
if (pixelA[i].w > 0.0)
124
//if (abs(pixelC[i].w - obj_id) < 0.1 )
126
// Depth test between valid in reach ellipses
127
if ((!depth_test) || (pixelB[i].x - pixelB[i].y <= zmax))
130
bufferA += pixelA[i] * w;
132
// Increment ellipse total path with distance from gather pixel to center
133
// bufferB.zw += (pixelB[i].zw + gather_pixel_desloc[i].xy) * w;
134
bufferB.zw += pixelB[i].zw * w;
136
// Take maximum depth range
137
new_zmax = max(pixelB[i].x + pixelB[i].y, new_zmax);
145
// average values if there are any valid ellipses
146
// otherwise the pixel will be writen as unspecified
148
if (valid_pixels > 0.0)
150
bufferA /= valid_pixels;
151
bufferA.xyz = normalize(bufferA.xyz);
153
bufferB.y = new_zmax - zmin;
154
bufferB.zw /= valid_pixels;
157
// first buffer = (n.x, n.y, n.z, radius)
158
gl_FragData[0] = bufferA;
159
// second buffer = (depth, max_depth, dx, dy)
160
gl_FragData[1] = bufferB;