Thread: GLSL lighting (again)

  1. #1
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071

    GLSL lighting (again)

    Ok. I'm back at the lighting with GLSL again. This time however, rather than trying to do normal mapping, I'm going to start with regular diffuse per-pixel lighting. So far, I have a shader that does this, and mimics the OGL fixed-function pipe lighting. The problem is, it mimics the OGL default light (as in when you just enable lighting and a light, and set no position, color, etc. values). How do I give the GLSL light a set position (so it dosn't move as the camera moves). I know I have to pass the shader the OGL modelview matrix, but I don't know what to do with it from there.

    As always, help is appreciated.
    -psychopath
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I dont know how to do this in GL but in DX you would simply pass the light position as is without running it through the pipeline. You don't want to transform the light position unless it is a moving light. But if you use directional lights it won't move anyways and if it does it won't matter because directional lights are simulating diffuse lighting such as when the sun is shining at high noon. The light is everywhere and coming from a certain direction which produces the light and dark shades on your model. Point lights can move and radiate outward and have a falloff property as well as other properties. Spot lights have direction, falloff, cones, and position.

    So pass the light position to your shader and use it as is. It does not get transformed.

  3. #3
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    >>I dont know how to do this in GL but in DX you would simply pass the light position as is without running it through the pipeline. You don't want to transform the light position unless it is a moving light.<<
    That's what I'm doing (at least I think that's what i'm doing).

    I should probably rephrase my problem. Suppose you a wall. There is a light on on side of the wall. This means that the side facing the light should be illuminated. If you look at the other side, it should be dark, because it faces away from the light. However, when I look at the side that should be dark, it lights up, and the other side goes dark.

    I have plenty of working lighting shaders, but they all have this problem, and I don't know what it is, or how to fix it.

    -psychopath
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  4. #4
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Ok, I have it working. However, It only works properly if I set the lights position with built in OGL functions. For example, I would first call glLightModelfv(GL_LIGHT0, GL_POSITION, pos); and then use gl_LightModel[0].position.xyz as the position variable in the shader. What I'm wondering now, is how to render multitple lights using this method.

    I'm thinking somthing similar to:
    Code:
    for(int i=0; i<numberoflights; i++)
    {
         glLightModelfv(GL_LIGHT0, GL_POSITION, light[i].pos);
         funcCallLightShader();
    }
    Or would this be too slow and/or inefficient?

    -psychopath
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Ok you are suffering from back lighting problems. In your shader do not change the color of the pixel in question if the dot product is less than 0. So if the light is facing the normal it will alter the color, if not it returns the pixel color unaltered.

  6. #6
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Ok, I'll give that a try later tonight, when I get home, but i'm still not entirly clear on how what your saying creates a problem .

    -psychopath

    [EDIT]Nevermind, after re-reading what you said, I get it now. I'll try the fix when I get home.[/EDIT]
    Last edited by psychopath; 12-19-2005 at 08:29 AM.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  7. #7
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Still dosen't work

    In my fragment shader:
    Code:
    // write Total Color:
    if(dot(N,L)>=0.0)
    {gl_FragColor = texture2D(baseMap, varTexCoord) * (Iamb + Idiff + Ispec);}
    else if(dot(N,L)<=0.0)
    {gl_FragColor = gl_FragColor;}
    -psychopath
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Eliminating lighting "over-bright"
    By psychopath in forum Game Programming
    Replies: 1
    Last Post: 05-31-2006, 06:52 PM
  2. Replies: 6
    Last Post: 11-12-2005, 11:57 AM
  3. My attempt at lighting math
    By psychopath in forum Game Programming
    Replies: 11
    Last Post: 03-30-2005, 12:35 AM
  4. Lighting a Direct3D 9 mesh sphere
    By bennyandthejets in forum Game Programming
    Replies: 12
    Last Post: 02-14-2005, 01:19 AM
  5. Per-pixel dot3 lighting
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 01-20-2005, 08:05 PM