volume rendering shader
https://stackoverflow.com/questions/9482572/volume-rendering-using-glsl-with-ray-casting-algorithm
vertex shader
void main()
{
gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
//gl_FrontColor = gl_Color;
gl_TexCoord[2] = gl_Position;
gl_TexCoord[0] = gl_MultiTexCoord1;
gl_TexCoord[1] = gl_Color;
}
fragment shader
uniform sampler2D tex;
uniform sampler3D volume_tex;
uniform float stepsize;
void main()
{
vec2 texc = ((gl_TexCoord[2].xy/gl_TexCoord[2].w) + 1) / 2;
vec4 start = gl_TexCoord[0];
vec4 back_position = texture2D(tex, texc);
vec3 dir = vec3(0.0);
dir.x = back_position.x - start.x;
dir.y = back_position.y - start.y;
dir.z = back_position.z - start.z;
float len = length(dir.xyz); // the length from front to back is calculated and used to terminate the ray
vec3 norm_dir = normalize(dir);
float delta = stepsize;
vec3 delta_dir = norm_dir * delta;
float delta_dir_len = length(delta_dir);
vec3 vect = start.xyz;
vec4 col_acc = vec4(0,0,0,0); // The dest color
float alpha_acc = 0.0; // The dest alpha for blending
float length_acc = 0.0;
vec4 color_sample; // The src color
float alpha_sample; // The src alpha
for(int i = 0; i < 450; i++)
{
color_sample = texture3D(volume_tex,vect);
// why multiply the stepsize?
alpha_sample = color_sample.a*stepsize;
// why multply 3?
col_acc += (1.0 - alpha_acc) * color_sample * alpha_sample*3 ;
alpha_acc += alpha_sample;
vect += delta_dir;
length_acc += delta_dir_len;
if(length_acc >= len || alpha_acc > 1.0)
break; // terminate if opacity > 1 or the ray is outside the volume
}
gl_FragColor = col_acc;
}
https://www.cnblogs.com/bitzhuwei/archive/2016/05/31/5544538.html
1 #version 400
2
3 in vec3 EntryPoint;
4 in vec4 ExitPointCoord;
5
6 uniform sampler2D ExitPoints;
7 uniform sampler3D VolumeTex;
8 uniform sampler1D TransferFunc;
9 uniform float StepSize = 0.001f;
10 uniform vec2 ScreenSize;
11 uniform vec4 backgroundColor = vec4(0, 0, 0, 0);// value in glClearColor(value);
12 layout (location = 0) out vec4 FragColor;
13
14 void main()
15 {
16 // ExitPointCoord is normalized device coordinate
17 vec3 exitPoint = texture(ExitPoints, gl_FragCoord.st / ScreenSize).xyz;
18 // that will actually give you clip-space coordinates rather than
19 // normalised device coordinates, since you're not performing the perspective
20 // division which happens during the rasterisation process (between the vertex
21 // shader and fragment shader
22 // vec2 exitFragCoord = (ExitPointCoord.xy / ExitPointCoord.w + 1.0)/2.0;
23 // vec3 exitPoint = texture(ExitPoints, exitFragCoord).xyz;
24
25 //background need no raycasting
26 if (EntryPoint == exitPoint) { discard; }
27
28 vec3 direction = exitPoint - EntryPoint;
29 float directionLength = length(direction); // the length from front to back is calculated and used to terminate the ray
30 vec3 deltaDirection = direction * (StepSize / directionLength);
31
32 vec3 voxelCoord = EntryPoint;
33 vec3 colorAccumulator = vec3(0.0); // The dest color
34 float alphaAccumulator = 0.0f;
35 float lengthAccumulator = 0.0;
36 float intensity;
37 vec4 colorSample; // The src color
38
39 for(int i = 0; i < 1600; i++)
40 {
41 // get scaler value in the volume data
42 intensity = texture(VolumeTex, voxelCoord).x;
43 // get mapped color from 1-D texture
44 colorSample = texture(TransferFunc, intensity);
45 // modulate the value of colorSample.a
46 // front-to-back integration
47 if (colorSample.a > 0.0) {
48 // accomodate for variable sampling rates (base interval defined by mod_compositing.frag)
49 colorSample.a = 1.0 - pow(1.0 - colorSample.a, StepSize * 200.0f);
50 colorAccumulator += (1.0 - alphaAccumulator) * colorSample.rgb * colorSample.a;
51 alphaAccumulator += (1.0 - alphaAccumulator) * colorSample.a;
52 }
53 voxelCoord += deltaDirection;
54 lengthAccumulator += StepSize;
55 if (lengthAccumulator >= directionLength)
56 {
57 colorAccumulator = colorAccumulator * alphaAccumulator
58 + (1 - alphaAccumulator) * backgroundColor.rgb;
59 break; // terminate if opacity > 1 or the ray is outside the volume
60 }
61 else if (alphaAccumulator > 1.0)
62 {
63 alphaAccumulator = 1.0;
64 break;
65 }
66 }
67 FragColor = vec4(colorAccumulator, alphaAccumulator);
68 }
raycast.frag