So if I understood correct to get the model into lightspace I have to take the the cube grids transform matrix and multiply it in the shader with the vertex to get it in lightspace. Then rescale it to 0-1 range to sample it, and then lookup with the transformed vertex pos.
Though I’m abit concerned with the rescaling. What I had in mind was having the pivot point at the left bottom back corner (so the grid vertices are always in the positive axises), and then divide the transformed vertex in the shader with the scale X,Y,Z values of the cube grid. Is there a better way to do it? :o
That’s correct, the point being shaded needs to be mapped to the 0-1 space of the volume texture. If the light volumes are perfectly aligned to the world then you can get just reposition and scale the world coordinate and use that as the look up. If the light volume is rotated then you need to map the world position with a matrix transform. I calculate the required transform matrix for each volume once and then send that to the shader to save on instructions.
Matrix volWorldTransform; // Volume world transform with pivot at the point representing 0,0,0 in volume texture
Matrix volUVWTransform =
1 / volWidth, 0, 0, 0,
0, 1/ volHeight, 0, 0,
0, 0, 1 / volLength, 0,
0, 0, 0, 1;
Matrix worldToVolumeTransform = (inverse volWorldTransform) * volUVWTransform;
worldToVolumeTransform is then sent to the shader and used to transform the world position into the volumes UVW space.
float3 volumeUVW = mul(input.worldPos worldToVolumeTransform);
float3 color = tex3D(sampler_volCol,volumeUVW);
Also how do you handle which volume textures to use at rendering? I guess you just do a AABB colission test with the mesh and then pick the correct volume, which then hooks the shader with the correct texture during the rendering pipeline, am I right? :o In deferred rendering this problem doesn’t exist since you render all of the volumes into the light buffer anyway ;p But I was thinking of a forward method to try, and then perhaps switch to deferred ^^
I’m not sure as to the best way to go about this in a forward renderer but it’s pretty easy with a deferred renderer. It’s just a case of using texKill on the result of volumeUVW and rejecting any coordinates that fall outside the volume.
-Stef