~ubuntu-branches/ubuntu/lucid/meshlab/lucid

« back to all changes in this revision

Viewing changes to meshlab/src/meshlabplugins/render_splatpyramid/shaders/shader_analysis.frag

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-10-08 16:40:41 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20091008164041-0c2ealqv8b8uc20c
Tags: 1.2.2-1
* New upstream version
* Do not build filter_isoparametrization because liblevmar dependency
  is not (yet) in Debian
* Fix compilation with gcc-4.4, thanks to Jonathan Liu for the patch
  (closes: #539544)
* rules: Add compiler variables to the qmake call (for testing with new
  GCC versions)
* io_3ds.pro: Make LIBS and INCLUDEPATH point to Debian version of lib3ds
* io_epoch.pro: Make LIBS point to Debian version of libbz2
* control:
  - Move Homepage URL to the source package section
  - Update to standards-version 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Analysis step */
 
2
#version 120
 
3
 
 
4
#extension GL_ARB_draw_buffers : enable
 
5
 
 
6
// flag for depth test on/off
 
7
uniform bool depth_test;
 
8
 
 
9
// 2.0*size of current level / size of one level down
 
10
uniform vec2 level_ratio;
 
11
 
 
12
// current read level
 
13
uniform int level;
 
14
 
 
15
uniform vec2 offset;
 
16
 
 
17
uniform float reconstruction_filter_size;
 
18
uniform float prefilter_size;
 
19
 
 
20
uniform sampler2D textureA;
 
21
uniform sampler2D textureB;
 
22
 
 
23
 
 
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);
 
30
 
 
31
  if (len == 0.0)
 
32
    normal.y = 0.0;
 
33
  else
 
34
    normal.y /= len;
 
35
 
 
36
  // angle between normal and z direction
 
37
  float angle = acos(normal.y);
 
38
  if (normal.x > 0.0)
 
39
    angle *= -1.0;
 
40
 
 
41
  float cos_angle = normal.y;
 
42
  float sin_angle = sin(angle);
 
43
 
 
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);
 
47
 
 
48
  // major and minor axis
 
49
  float a = 1.0*radius;
 
50
  float b = a*normal.z;
 
51
 
 
52
  // include antialiasing filter
 
53
  a += prefilter_size;
 
54
  b += prefilter_size;
 
55
 
 
56
  // inside ellipse test
 
57
  float test = ((rotated_pos.x*rotated_pos.x)/(a*a)) + ((rotated_pos.y*rotated_pos.y)/(b*b));
 
58
 
 
59
  if (test <= reconstruction_filter_size)
 
60
    return test;
 
61
  else return -1.0;
 
62
}
 
63
 
 
64
void main (void) {
 
65
 
 
66
        vec2 tex_coord[4];
 
67
 
 
68
        vec4 bufferA = vec4(0.0, 0.0, 0.0, 0.0);
 
69
        vec4 bufferB = vec4(0.0, 0.0, 0.0, 0.0);
 
70
 
 
71
        float valid_pixels = 0.0;
 
72
 
 
73
        vec4 pixelA[4], pixelB[4];
 
74
 
 
75
        vec2 center_coord = gl_TexCoord[0].st * level_ratio;
 
76
 
 
77
        //up-right
 
78
        tex_coord[0].st = center_coord.st + offset.st;
 
79
        //up-left
 
80
        tex_coord[1].s = center_coord.s - offset.s;
 
81
        tex_coord[1].t = center_coord.t + offset.t;
 
82
        //down-right
 
83
        tex_coord[2].s = center_coord.s + offset.s;
 
84
        tex_coord[2].t = center_coord.t - offset.t;
 
85
        //down-left
 
86
        tex_coord[3].st = center_coord.st - offset.st;
 
87
 
 
88
        // Compute the front most pixel from lower level (minimum z
 
89
        // coordinate)
 
90
        float dist_test = 0.0;
 
91
        float zmin = 10000.0;
 
92
        float zmax = -10000.0;
 
93
        float obj_id = -1.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);
 
99
 
 
100
          if  (dist_test != -10.0)
 
101
                {
 
102
                  // test for minimum depth coordinate of valid ellipses
 
103
                  if (pixelB[i].x <= zmin) {
 
104
                        zmin = pixelB[i].x;
 
105
                        zmax = zmin + pixelB[i].y;
 
106
                  }
 
107
                }
 
108
      else {
 
109
                // if the ellipse does not reach the center ignore it in the averaging
 
110
                pixelA[i].w = -1.0;
 
111
      }
 
112
    }
 
113
  }
 
114
 
 
115
  float new_zmax = zmax;
 
116
 
 
117
  // Gather pixels values
 
118
  for (int i = 0; i < 4; ++i)
 
119
    {
 
120
      // Check if valid gather pixel or unspecified (or ellipse out of reach set above)
 
121
      if (pixelA[i].w > 0.0) 
 
122
                {
 
123
                
 
124
                  //if (abs(pixelC[i].w - obj_id) < 0.1 )
 
125
                  {
 
126
                        // Depth test between valid in reach ellipses
 
127
                        if ((!depth_test) || (pixelB[i].x - pixelB[i].y <= zmax))
 
128
                          {
 
129
                                float w = 1.0;
 
130
                                bufferA += pixelA[i] * w;
 
131
 
 
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;
 
135
              
 
136
                                // Take maximum depth range
 
137
                                new_zmax = max(pixelB[i].x + pixelB[i].y, new_zmax);
 
138
              
 
139
                                valid_pixels += w;
 
140
                          }
 
141
                  }
 
142
                }
 
143
    }
 
144
 
 
145
  // average values if there are any valid ellipses
 
146
  // otherwise the pixel will be writen as unspecified
 
147
  
 
148
  if (valid_pixels > 0.0)
 
149
    {
 
150
      bufferA /= valid_pixels;
 
151
      bufferA.xyz = normalize(bufferA.xyz);
 
152
      bufferB.x = zmin;
 
153
      bufferB.y = new_zmax - zmin;
 
154
      bufferB.zw /= valid_pixels;
 
155
    }
 
156
 
 
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;
 
161
}