Pages

Thursday 24 June 2010

Lighting Volumes II

Some more progress on implementing lighting volumes. I'm now able to light objects using a volume texture built from the volume lighting samples mentioned in the previous post. The effect looks much better in motion so I'll post some videos soon. Until then, some screenshots...


BugBackToad model by SonK, which can be found at http://ompf.org/forum/viewtopic.php?f=7&t=752


As before, the static building geometry is lit by a lightmap. The toad creature is being lit by the lighting volume. A representation of which you can see in the last three screenshots. The light volume is 64x32x64 and was generated with 664 rays per sample in about 10 seconds.

Generating each of the samples turned out to be quite easy but it took me a while to convert those samples into a volume texture I could use. The sample colours are copied from a one dimensional array straight into the volume texture. In the end the trick was to create the initial sample positions, layer by layer, in the same order as the volume texture stores them. Obvious really :)

Converting the fragments world space position into a UVW coordinate that could be used to lookup the light volume took some head scratching but I got there in the end. This code assumes that the volumes transform represents the lowest corner of the box, not it's physical center.

// Matrix used to map lookup UVW from volume world space to volume texture space. // Where vW, vH and vL are the dimensions of the volume.
Matrix invVolumeDimensions = ((1/vW,0,0,0),
                              (0,1/vH,0,0),
                              (0,0,1/vL,0),
                              (0,0,0,1));

//Matrix used to map a world position to volume texture space.
Matrix invVolumeTransform = (inverse worldSpaceVolumeTransform) * invVolumeDimensions;

// Transform world position into light volume texture space
float3 volumeUVW = fragmentWorldPosition * invVolumeTransform;

// Sample light volume
float3 volColour = tex3D(volumeColourSampler,volumeUVW);


No comments:

Post a Comment