VMTK
castregister_with_planar_clipping.frag
1 #version 330
2 
10 in vec2 TexCoord;
11 uniform sampler2D backface_fbo;
12 uniform sampler2D frontface_fbo;
13 uniform sampler3D ref_volumetexture;
14 uniform sampler3D float_volumetexture;
15 uniform sampler1D ref_transferfunction;
16 uniform sampler1D float_transferfunction;
17 uniform int height;
18 uniform int width;
19 uniform int depth;
20 uniform int steps_mode;
21 uniform float noise_threshold;
22 uniform vec4 ref_phyDimensions;
23 uniform vec4 float_phyDimensions;
24 uniform mat4 inv_registration_matrix;
25 uniform float blending_factor;
27 out vec4 fColor;
41 void main(void)
42 {
43  vec3 start_position = texture(frontface_fbo, TexCoord).xyz;
44  vec3 end_position = texture(backface_fbo, TexCoord).xyz;
45 
46  if (start_position == end_position) // if two points are coincident
47  fColor = vec4(0.5, 0.5, 0.5,1.0); // color of pixel is black
48  else // compute the samples
49  {
50  vec4 color = vec4(0.0);
51  vec4 fusion = vec4(0.0);
52  float ref_density = 0.0;
53  float ref_eq_density = 0.0;
54  float float_density = 0.0;
55  float float_eq_density = 0.0;
56  float alpha = 0.0;
57  float accum_alpha = 0.0;
58  bool first=false;
59  vec4 inv = vec4(1. / float_phyDimensions.x, 1. / float_phyDimensions.y,
60  1. / float_phyDimensions.z, 1.0);
61 
62 
63  float length_of_ray_x = (end_position.x - start_position.x) * float(width);
64  float length_of_ray_y = (end_position.y - start_position.y) * float(height);
65  float length_of_ray_z = (end_position.z - start_position.z) * float(depth);
66  float length_of_ray = sqrt(length_of_ray_x * length_of_ray_x + length_of_ray_y * length_of_ray_y + length_of_ray_z * length_of_ray_z);
67  float steps;
68  float factor_for_tf_alpha;
69 
70  if (steps_mode == 0) // sample resolution is 2xlength
71  {
72  steps = length_of_ray * 2.0;
73  factor_for_tf_alpha = 2.0;
74  }
75  else
76  { // sample resolution is fixed on a pre-defined value
77  steps = float(steps_mode);
78  factor_for_tf_alpha = steps / length_of_ray;
79  }
80 
81  vec3 step = (end_position - start_position) / steps; // sample step
82 
83  for (float i=0.0; i < steps; i+=1.0)
84  {
85  vec3 current_position = start_position + i*step;
86 
87  ref_density = texture(ref_volumetexture, current_position).r;
88 
89  if (!first && ref_density >= noise_threshold) {
90  first = true;
91  }
92 
93 
94  if (first) {
95 
96  ref_eq_density = ref_density;
97 
98  vec4 ref_tf_color = texture(ref_transferfunction, ref_eq_density); // reference voxel color
99 
100  // get the correspondence in floating volume texture
101  vec4 pt = vec4(ref_phyDimensions.xyz * (current_position), 1.0);
102  vec4 corresp = inv * (inv_registration_matrix * pt);
103 
104  float_density = texture(float_volumetexture, corresp.xyz).r;
105 
106  float_eq_density = float_density;
107  vec4 float_tf_color = texture(float_transferfunction, float_eq_density); //float voxel color
108 
109  // blend two voxel colors
110  fusion.rgb = mix(float_tf_color.rgb, ref_tf_color.rgb, blending_factor); // (1-blending_factor)*float_tf_color+blending_factor*ref_tf_color
111 
112  // alpha of the fuse color
113  fusion.a = min(ref_tf_color.a,float_tf_color.a);
114 
115  if (alpha < 0.99) {
116  color.rgb = (1-alpha)*color.rgb + fusion.rgb;
117  color.a = mix(fusion.a, 1.0, color.a);
118  accum_alpha=alpha;
119  alpha = color.a;
120 
121  } else {
122  fColor = vec4(color.rgb, 1.0);
123  return;
124  }
125  }
126  }
127  if (accum_alpha == 0.0)
128  {
129  fColor = vec4(0.5,0.5,0.5, 1.0);
130  }
131  else
132  {
133  fColor = vec4(color.rgb, 1.0);
134  }
135  }
136 } // end main
out vec2 TexCoord
Definition: raytrace.vert:13
void main(void)
Main function.