Results 1 to 6 of 6

Thread: Lighting - Shader Langauge - Vector Magnitude

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Location
    Kingston, Ontario, Canada
    Posts
    29

    Lighting - Shader Langauge - Vector Magnitude

    I am playing with Lesson 12 (Ambient And Point Lighting) from the WebGL Tutorials provided at:

    http://learningwebgl.com/blog/?p=1359

    I adapted the example to my own use but I noticed that the lighting does not drop off over distance. This is not a problem in a crowded scene where the light is likely to end up hitting something before traveling any significant distance but in a more open scene it is a problem. Think of a candle in a large room. The candle is bright and will illuminate objects near it but only a short distance away and objects are almost not illuminated at all.

    For the lesson it seems that the calculation to obtain the light on any particular fragment is done (on the shader) using:

    Code :
    vec3 lightDirection = normalize(uPointLightingLocation - mvPosition.xyz);
    vec3 transformedNormal = uNMatrix * aVertexNormal;
    float directionalLightWeighting = max(dot(transformedNormal, lightDirection), 0.0);
    vLightWeighting = uAmbientColor + uPointLightingColor * directionalLightWeighting;

    So it basically adds the ambient component plus the point light contribution which is based on the point light and its angle to the fragment. :P

    To me it seems that if I multiply the point light contribution by some fractional factor based on the distance between the point light and the fragment then I can make the light intensity drop off the further it is from the point light source.

    The distance, if I understand things correctly, should be the magnitude of uPointLightingLocation - mvPosition.xyz.

    However, I am not sure what the shader language command for getting the magnitude of a vector is.

    If anyone knows this, please let me know. It would be very helpful.
    "We often compare ourselves to the U.S.
    and often they come out the best,
    but they only have the right to bear arms
    while we have the right to bare breasts"
    Song "Busting The Breast" by Bowser & Blue

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    12

    Re: Lighting - Shader Langauge - Vector Magnitude

    Looking at the WebGL Quick Reference Card
    http://www.khronos.org/files/webgl/webg ... rd-1_0.pdf

    You want length()

    as in

    float distanceToLight = length(uPointLightingLocation - mvPosition.xyz)

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Location
    Kingston, Ontario, Canada
    Posts
    29

    Re: Lighting - Shader Langauge - Vector Magnitude

    Thanks.

    I'll give that a try.

    I have familiarity with Javascript but not with the shader language.
    "We often compare ourselves to the U.S.
    and often they come out the best,
    but they only have the right to bear arms
    while we have the right to bare breasts"
    Song "Busting The Breast" by Bowser & Blue

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    12

    Re: Lighting - Shader Langauge - Vector Magnitude

    Doh! If I had looked just one line down the reference card I would have seen the distance function

    Code :
    float distanceToLight = distance(uPointLightingLocation, mvPosition.xyz);

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Location
    Kingston, Ontario, Canada
    Posts
    29

    Re: Lighting - Shader Langauge - Vector Magnitude

    Hmmm...using length seemed to have worked just fine.

    I have already adjusted my code and my theory worked great. Now the light source contribution drops off as the fragments are further away from the light source.

    Thanks.
    "We often compare ourselves to the U.S.
    and often they come out the best,
    but they only have the right to bear arms
    while we have the right to bare breasts"
    Song "Busting The Breast" by Bowser & Blue

  6. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    17

    Re: Lighting - Shader Langauge - Vector Magnitude

    Quote Originally Posted by LordAshes
    Hmmm...using length seemed to have worked just fine.

    I have already adjusted my code and my theory worked great. Now the light source contribution drops off as the fragments are further away from the light source.

    Thanks.
    Yes, distance() is pretty much the length of the difference between the two, but distance does allow for some optimizations by the GPU, thus using it *can* sometimes be faster. Now it might not be the case with this method, but generally, it is a good idea to try to use as much of the built in methods as possible.

Similar Threads

  1. Replies: 0
    Last Post: 01-31-2012, 10:29 AM
  2. Replies: 1
    Last Post: 10-01-2009, 05:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •