Sign in to follow this  
kRogue

GLSL/Cg and sqrt/magnitude

Recommended Posts

Hi all, was just wondering: when doing lighitn one usually does something like: distance=magnitue(lightPos-fragPosition) now within a fragment shader, this seems to me like a bad idea, as then I am taking a sqrt for each light source.. so I though the alternatives: (a) within the vertex shader, make a varying variable for each light that is the distnace, actually just pack 4 of theom into one varing variable via vec4. Downside: the distance then gets lineraly interpolated with in a primitve... maybe it does not hurt the accuracy so badly though.. (b) apporximate the sqrt myself and use dot() ... now the little wheels start to turn in my head: considering that for garpahics just needs the sqrt to be "close enough to look good" so does that imply that there is a sqrt/magnitude function which is faster but not as accuarate (on tpyical hardware) or that the sqrt built into the hardware gives only an OK approximation (like just mabe 2 or so digits being accurate) morover, in Cg, one can constrol the accuracy of what happen in the fragment shaders variables with the types half and fixed. Is there any such thing for GLSL? Best Regards

Share this post


Link to post
Share on other sites
yes both a+b are commonly used for light attenuation

in fact heres my vertexshader
vec3 light_vec = ( light_pos.xyz - gl_Vertex.xyz );
float LD = dot( light_vec, light_vec ); // light distance squared

though my geometry is reasonably tesselated so this gives good enuf results

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this