Visualisierung 2
Comparison of Hue Preserving Rendering to Alpha Composing
hue.frag
Go to the documentation of this file.
1 
12 #version 400
13 
14 out vec4 color;
15 
16 in vec3 position;
17 
18 uniform sampler3D volume;
19 uniform float regionSeperator;
20 uniform int depth;
21 uniform int width;
22 uniform int height;
23 uniform vec4 regionColor1;
24 uniform vec4 regionColor2;
25 uniform int orientation; // 0 top, 1 bottom, 2 left, 3 right, 4 front, 5 back
26 
27 // RGBtoHSV conversion from http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl
28 vec3 rgb2hsv(vec3 c)
29 {
30  vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
31  vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
32  vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
33 
34  float d = q.x - min(q.w, q.y);
35  float e = 1.0e-10;
36  return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
37 }
38 // HSVtoRGB conversion from http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl
39 vec3 hsv2rgb(vec3 c)
40 {
41  vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
42  vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
43  return vec3(c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y));
44 }
45 
46 // checks for hue equality based on threshold
47 bool equalHue(vec3 color1,vec3 color2){
48 
49  // convert to HSV to rotate Hue vector
50  vec3 c1HSV= rgb2hsv(color1);
51  vec3 c2HSV= rgb2hsv(color2);
52 
53  // constant treshold for equality check
54  return (abs(c1HSV.x-c2HSV.x)<0.05f)?true:false;
55 }
56 
57 // computes the opposite color in HSV by rotating the hue angle by 180 degrees
58 vec3 oppositeColor(vec3 color1,vec3 color2){
59 
60  // convert to HSV to rotate Hue vector
61  vec3 c1HSV= rgb2hsv(color1);
62  vec3 c2HSV= rgb2hsv(color2);
63 
64  // hue of color1 + 180 and saturation/value from color2
65  vec3 oppColor = vec3(mod(((c1HSV.x*360)+180), 360)/360, c2HSV.yz);
66 
67  // return RGB
68  return hsv2rgb(oppColor);
69 }
70 
71 // algorithm to calculate the new color from paper
72 vec3 huePreservingBlending(vec4 color1,vec4 color2){
73  vec3 colorNew;
74  if(equalHue(color1.rgb,color2.rgb)){
75  colorNew = color1.rgb+(1-color.a)*color2.a*color2.rgb;
76  }
77  else{
78  vec3 color2Prime = oppositeColor(color1.rgb,color2.rgb);
79  colorNew = color1.rgb+(1-color.a)*color2.a*color2Prime;
80  if(!equalHue(color1.rgb,colorNew)){
81  vec3 color1Prime = oppositeColor(color2.rgb,color1.rgb);
82  colorNew = color1Prime+(1-color.a)*color2.a*color2.rgb;
83  }
84  }
85  return colorNew;
86 }
87 
88 void main() {
89  // transform normalized device coordinates [-1,-1] to volume coordinate system [0,1]
90  vec3 volPosition = (position+1)/2;
91 
92  float alphaB; // back voxel
93  vec4 colorB;
94 
95  float x;
96  float y;
97  float z;
98 
99  // Front-to-Back compositing
100  for(float i=0;i<depth;i++)
101  {
102  switch (orientation) {
103  default:
104  case 0: // top
105  x = volPosition.x;
106  y = volPosition.y;
107  z = i/depth;
108  break;
109  case 1: // bottom
110  x = 1.0f - volPosition.x;
111  y = 1.0f - volPosition.y;
112  z = (depth - i - 1)/depth;
113  break;
114  case 2: // left
115  x = i/depth;
116  y = volPosition.x;
117  z = 1.0f - volPosition.y;
118  break;
119  case 3: // right
120  x = (depth - i - 1) / depth;
121  y = 1 - volPosition.x;
122  z = 1.0f - volPosition.y;
123  break;
124  case 4: // front
125  x = volPosition.x;
126  y = i/depth;
127  z = 1.0f - volPosition.y;
128  break;
129  case 5: // back
130  x = 1 - volPosition.x;
131  y = (depth - i - 1) / depth;
132  z = 1.0f - volPosition.y;
133  break;
134  }
135  colorB.a = texture(volume,vec3(x,y,z)).r;
136  colorB.rgb = (colorB.a<regionSeperator)?regionColor1.rgb:regionColor2.rgb;
137  color.rgb = huePreservingBlending(color,colorB);
138  color.r=min(1,color.r);
139  color.g=min(1,color.g);
140  color.b=min(1,color.b);
141  color.a=min(1,color.a + (1-color.a)*colorB.a*colorB.a);
142 
143  if(color.a==1){break;} // early ray termination
144  }
145 }
146 
147 
148 
bool equalHue(vec3 color1, vec3 color2)
Definition: hue.frag:47
uniform int depth
Definition: hue.frag:20
void main()
Definition: hue.frag:88
uniform vec4 regionColor1
Definition: hue.frag:23
out vec4 color
Definition: hue.frag:14
uniform int orientation
Definition: hue.frag:25
uniform int width
Definition: hue.frag:21
vec3 hsv2rgb(vec3 c)
Definition: hue.frag:39
vec3 huePreservingBlending(vec4 color1, vec4 color2)
Definition: hue.frag:72
uniform vec4 regionColor2
Definition: hue.frag:24
uniform int height
Definition: hue.frag:22
vec3 rgb2hsv(vec3 c)
Definition: hue.frag:28
uniform float regionSeperator
Definition: hue.frag:19
in vec3 position
Definition: hue.frag:16
uniform sampler3D volume
Definition: hue.frag:18
vec3 oppositeColor(vec3 color1, vec3 color2)
Definition: hue.frag:58