The following code is a simple diffuse-only volume rendering with head-light
written in Diderot.
It uses an opacity function that varies linearly between two values.
This example illustrates the use of probing both a field and its gradient.
This version uses the ASCII syntax for the convolution and differentiation operator.
A Unicode version can be found here.
input string dataFile;
input real stepSz = 0.1;
input vec3 eye;
input vec3 orig;
input vec3 cVec;
input vec3 rVec;
input real valOpacMin;
input real valOpacMax;
image(3)[] img = load (dataFile);
field#1(3)[] F = img (*) bspln3;
strand RayCast (int row, int col)
{
vec3 pos = orig + real(row)*rVec + real(col)*cVec;
vec3 dir = (pos - eye)/|pos - eye|;
real t = 0.0;
real transp = 1.0;
real gray = 0.0;
output vec4 rgba = [0.0, 0.0, 0.0, 0.0];
update {
pos = pos + stepSz*dir;
if (inside (pos,F)) {
real val = F(pos);
vec3 grad = D F(pos);
vec3 norm = -grad / |grad|;
if (val > valOpacMin) {
real opac =
1.0 if (val > valOpacMax)
else (val - valOpacMin)/(valOpacMax - valOpacMin);
gray = gray + transp*opac*max(0.0, dot(-dir,norm));
transp = transp*(1.0 - opac);
}
}
if (transp < 0.01) {
transp = 0.0;
stabilize;
}
if (t > 40.0) {
stabilize;
}
t = t + stepSz;
}
stabilize {
rgba = [gray, gray, gray, 1.0-transp];
}
}
initially [ RayCast(r, c) | r in 0..199, c in 0..199 ];