Jump to content
  • Advertisement
Sign in to follow this  

GLSL/Cg and sqrt/magnitude

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

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!