// vertex
varying vec4 cameraPos;
varying vec4 vertexPos;
varying mat4 texgen;
void main(void) {
vertexPos = gl_Vertex;
gl_Position = ftransform();
cameraPos = gl_ModelViewMatrixInverse*vec4(0,0,0,1);
texgen = mat4(gl_ObjectPlaneS[0],
gl_ObjectPlaneT[0],
gl_ObjectPlaneR[0],
gl_ObjectPlaneQ[0]);
}
// fragment
uniform sampler3D baseTexture;
varying vec4 cameraPos;
varying vec4 vertexPos;
varying mat4 texgen;
void main(void)
{
vec4 t0 = vertexPos;
vec4 te = cameraPos;
// after normalize
t0 = t0 * texgen;
te = te * texgen;
const float num_iterations = 512.0;
vec3 deltaTexCoord=(te-t0).xyz/float(num_iterations-1.0);
vec3 texcoord = t0.xyz;
vec4 fragColor = vec4(0.0, 0.0, 0.0, 0.0);
while(num_iterations>0.0)
{
vec4 color = texture3D(baseTexture, texcoord);
float r = color[3];
if (r>AlphaFuncValue)
{
fragColor.xyz = fragColor.xyz*(1.0-r)+color.xyz*r;
fragColor.w += r;
}
if (fragColor.w<color.w)
{
fragColor = color;
}
texcoord += deltaTexCoord;
--num_iterations;
}
if (fragColor.w>1.0) fragColor.w = 1.0;
if (fragColor.w<0.0) discard;
gl_FragColor = fragColor;
}