determinate miplevel with fwidth
What exactly does the GLSL function fwidth(p) do?
I know that it is implemented开发者_运维问答 as:
fwidth(p) = abs(dfdx(p)) + abs(dfdy(p))
but i am not sure if i got it yet.
I am doing some basic raycasting here and try to calculate the miplevel that is required. To find the miplevel is call fwidth on the coordinate where the ray hits the volume (in texture space).
// get 'derivate width' of sampling position
vec3 width = fwidth(current_position_in_texture_space * size_of_texture);
// get radius around current position in voxels
float range = length(width);
// calculate factor for interpolation (see below)
float factor = range / distance_from_camera_to_current_position;
In my understanding GLSL will syncronize all threads and calculate the derivates with the top and right neighbour threads.
During the traversal i interpolate range linear:
// during traversal
float new_range = distance_from_camera_to_current_position * factor;
// calculate mip level from voxel range
float level = log2(new_range);
// get new sampling stepsize
float stepsize = new_range * 0.5;
If this is correct level should be the mip level that is required to sample the current position. But currently the volume is sampling step size is to big ... altough it reduces if the distance to the camera is reduced.
There are many things wrong with what you are doing.
Why not let the gpu select the right miplevel itself? Just sample on the stepping position.
Your scaling is probably wrong.
Let the driver select the right level for you, seriously.
Think about how trilinear sampling works. Then think about how it works for volumes.
fwidth is not the right way to determine a miplevel, even if you want the nearest one.
Did I say let the GPU do the miplevel selection?
fwidth is useful for stepping faster or slower
精彩评论