I have a volume dataset, that I can render(this was part of a homework) I have already implemented ray casting using front-to-back composition and I want to integrate phong-shading. For computing the gradient I use forward-difference-method.
I know I can estimate the gradient of a voxel texture coordinate with the forward difference method.
My question would be, since phong-shading returns a color and front-to-back raycasting also returns a color, when do I apply the shading to the color of the ray function? After I have finalized my new color for a specific ray or while computing the color from the ray hit? Maybe this piece of code helps to understand my problem. I have added comments on where I think I should implement phong shading.
void main(){
if (raycast(t0,t1)) {
float t = t0;
for(float t =t0; t<t1;t+=stepsize){
vec3 p = ray_o + ray_d * t;
vec3 coord = worldToVolumeCoord(p, min_box, max_box);
float s = texture(tex_vol, coord).r;
vec4 color = texture(tex_tf, s);
if(color.a > 0){
// Front to back , when to apply shading?????
C = C + (1-A) * color.a * vec3(color);
A = A + (1-A) * color.a;
}
// vec3 Gradient = getGradient(tex_vol, coord, stepsize)
// But how to apply Phong Shading?
}
// Apply phong here?
}
}