View Full Version : Blending in fragment shader

Kal Mar
10-04-2010, 12:50 PM
Hi guys

I rendering a volume, that I writing a shader for coloring the volume. The volume is a sampler3D and a transfer function is a sampler3D, but I don't success, because all a cube is colored, ex. gray, I dicovered that lack a composition

Any idea for implementing a blending in my shader ?

My initial fragment code is:

uniform sampler3D VOL;
uniform sampler1D TF;

void main(void){

float pos;
pos = texture3D(VOL, gl_TexCoord[0].xyz).r;
gl_FragColor = texture1D(TF, pos);


carsten neumann
10-04-2010, 04:37 PM
Blending is usually ordering dependent, i.e. you need to make sure that you render either from front to back or back to front. The usual way to get this for volume rendering is to just draw the front faces of a cube. In the fragment shader for each fragment reconstruct where the ray from the eye position enters and leaves the volume and manually (i.e. with a loop in the fragment shader) trace that ray through the volume.
Since you are moving along a view ray and basically sample the volume at various points you can easily do this from the furthest to the closest point and blend the values accordingly.

Kal Mar
10-04-2010, 05:49 PM
You are referring to raycasting algorithm, truth? what combinations of blend (source, dest) I can use ? any example ?