Skip to main content
GameDev.net gamedev.net
🔒 Locked

Light, Attenuation

Started by Hardguy Aug 1, 2005 at 12:12 AM 1 replies 600+ views
Original Post
Hardguy
Hardguy
So what is the best way for calculating light attentuation using the least textures units and gpu instructions? I have read some about it and this is what I have come up with so far: as a forumla A = d^2 / R^2 is used, A being attenuation, d distance to light and R radius of light (where A is 0). This can be formulated as: A = l.x^2 / R^2 + l.y^2 / R^2 + l.z^2 / R^2 l being light pos set to normal/tanget space. And if you set R' to 1 and l' to l/R you get A = l'.x^2 + l'.y^2 + l'.z^2 In the fragment shader you can now use the function: A = 1- ( (max(l'.x^2,1) + max(l'.y^2,1) + max(l'.z^2,1)) or use a 3d texture or a 2d and a 1d and end up with: A = tex2d(l'.x, l'.y) - tex1d(l'z) or A = tex3d(l'.x, l'.y, l'z) Using the 3d texture it can be done using only one texture unit and very few instructions. However a 3d texture is quite mem consuming and might have to have to low resolution. Using a 2D and 1D texture the resolution can be inscreased but one more texture unit are used. Are there any other better ways of doing this? I am trying to make this fit at least Gf3 and hopefully Gf2.
MickeyMouse
MickeyMouse
I'm using this method (HLSL like code):

half getAttenuation(half3 lightVec)
{
return saturate(1 - dot(lightVec, lightVec) * g_OneOverSqrLightRadius);
}

where:

lightVec is vector from fragment to light.
g_OneOverSqrLightRadius is precalculated (1 / (light radius) ^ 2).
Maciej Sawitus
my blog | my games
ZQJ
ZQJ
GF2 doesn't have pixel shaders. However it did have register combiners (I think it was called something like that...) which could be accessed as an OpenGL extension and do per-pixel lighting (or radial fogging). I really know very little about pixel shaders but I'd be surprised if they weren't quite efficient at doing 1/R^2 calculations. Sadly nVidia seem to have taken down most of their examples and info for older cards (obviously they don't want anybody to be able to play anything new on a GF2).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.