# Light, Attenuation

This topic is 4884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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.

##### Share on other sites
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).

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633728
• Total Posts
3013575
×