-
Notifications
You must be signed in to change notification settings - Fork 8
/
raycastDiffuse.frag
97 lines (73 loc) · 2.26 KB
/
raycastDiffuse.frag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#version 330 core
precision highp float;
out vec4 FragColor;
in vec2 Texcoord;
//uniforms
uniform float step_length;
//texture samplers
uniform sampler2D front_face;
uniform sampler2D back_face;
//Actual Volume data
uniform sampler3D volumeTex;
uniform sampler1D transferFuncTex;
uniform sampler2D noiseTex;
uniform sampler3D gradientTex;
// A very simple color transfer function
/*
Simple transfer functions interpolates between a couple of colours denoting minimum and maximum intensity.
Uses exponential decay for the opacity
*/
vec4 color_transfer(float intensity)
{
vec3 high = vec3(1.0, 1.0, 1.0);
vec3 low = vec3(0.0, 0.0, 0.0);
float alpha = (exp(intensity) - 1.0) / (exp(1.0) - 1.0);
return vec4(intensity * high + (1.0 - intensity) * low, alpha);
}
void main()
{
vec3 L = vec3(0, 1, 1);
vec3 ray_start = texture(front_face, Texcoord).rgb;
vec3 ray_stop = texture(back_face, Texcoord).rgb;
if (ray_start == ray_stop)
{
discard;
return;
}
vec3 ray = ray_stop - ray_start;
float ray_length = length(ray);
vec3 step_vector = step_length * ray / ray_length;
//32 X 32 Size of noise Texture
float random = texture(noiseTex, gl_FragCoord.xy / vec2(32, 32)).x;
ray_start += step_vector * random;
vec3 position = ray_start;
vec4 color = vec4(0.0);
// Stop when the end of the volume is reached or early ray termination (when alpha gets high enough)
while (ray_length > 0)
{
float isoValue = texture(volumeTex, position).r;
vec3 normal = texture(gradientTex, position).xyz;
//Convert the sampled intensity value to color
//Use Transfer Function
//vec4 c = color_transfer(intensity);
//vec4 c = vec4(intensity, intensity, intensity, intensity);
//c.a *= 0.5f;
vec4 c = texture(transferFuncTex, isoValue);
//c.a *= 0.5f;
vec4 dst = color;
vec4 src = c;
float s = dot(normal, L);
//diffuse shading + fake ambient lighting
src.rgb = s * src.rgb + .1f * src.rgb;
//Alpha-blending (Front to back)
dst.rgb = dst.rgb + (1 - dst.a) * src.a * src.rgb;
dst.a = dst.a + (1 - dst.a) * src.a;
//break from the loop when alpha gets high enough
if(dst.a >= 0.99f)
break;
ray_length -= step_length;
position += step_vector;
color = dst;
}
FragColor = color;
}