Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: fragment_program lighting problem with object/word space!

  1. #1
    Intern Contributor
    Join Date
    Jun 2004
    Location
    Greece
    Posts
    84

    fragment_program lighting problem with object/word space!

    Hello!
    I have this ARB_fragment_program:
    http://rafb.net/paste/results/pSdNk043.html

    When I have a world of polygons and some moving lights it works perfectly. But If I try to use rotatef or translatef or multMatrix then the lighting is not correct. Maybe need convert the ocject space matrix to word space matrix? How I will do that? Using A vertex Program maybe? And What I will write in the vertex program?

    thanks in advance!

  2. #2
    Senior Member OpenGL Pro
    Join Date
    May 2001
    Location
    The Round Table at Camelot
    Posts
    1,264

    Re: fragment_program lighting problem with object/word space!

    What space are your normal maps in? If they are tangent space normal maps (most likely) then you need a vertex program to transform your light vectors and such into tangent space before you do the lighting.

    -SirKnight
    -SirKnight

  3. #3
    Junior Member Regular Contributor
    Join Date
    Jan 2004
    Location
    Czech Republic, EU
    Posts
    190

    Re: fragment_program lighting problem with object/word space!

    I thing I have the same problem. If I use glRotatef, then glTranslate and then I render any geometry, it works properly. This is part of my vertex shader:

    Code :
    # get the light vector
    DP4 lightpos.x, mvi[0], state.light[0].position;
    DP4 lightpos.y, mvi[1], state.light[0].position;
    DP4 lightpos.z, mvi[2], state.light[0].position;
    DP4 lightpos.w, mvi[3], state.light[0].position;
    SUB lightvec, lightpos, vertex.position;
     
    # transform light vector into tangent space
    DP3 result.texcoord[1].x, lightvec, tangent;
    DP3 result.texcoord[1].y, lightvec, binormal;
    DP3 result.texcoord[1].z, lightvec, normal;
    MOV result.texcoord[1].w, 1.0;
    But if I use another glRotatef and another glTranslatef, it is not correct. How can I do this to work?
    (usually just hobbyist) OpenGL driver developer

  4. #4
    Junior Member Newbie
    Join Date
    Nov 2002
    Location
    UK
    Posts
    25

    Re: fragment_program lighting problem with object/word space!

    I think the problem might be that the light position provided by the state variables is in eye space. You will need to pass in the world space light position as a uniform variable and transform it to tangent space, or maybe transform the light position into object space outside the vertex program and then transform it to tangent space per vertex. For bump mapping to work you need the light direction in tangent space dotted with the normal. Haven't got time for a detailed explanation now, but I'm sure someone will be able to provide more details sooner than I can.

  5. #5
    Intern Contributor
    Join Date
    Jun 2004
    Location
    Greece
    Posts
    84

    Re: fragment_program lighting problem with object/word space!

    Originally posted by SirKnight:
    What space are your normal maps in? If they are tangent space normal maps (most likely) then you need a vertex program to transform your light vectors and such into tangent space before you do the lighting.

    -SirKnight
    Any Idea how I can do that? .. not an expert in vertex_programs

    thanks

  6. #6
    Junior Member Regular Contributor
    Join Date
    Jan 2004
    Location
    Czech Republic, EU
    Posts
    190

    Re: fragment_program lighting problem with object/word space!

    2Paulc:
    I thing I see the point.

    unreal:
    You don't need to know how vertex shaders work. I thing that you are calculating light vectors and sending it to card via texture coordinates of cube map. This calculation can be done on cpu (how you do that now) or in vertex shader on gpu. It isn't so important to do it in vertex shader, but it's better to save a little performance on cpu.

    Now how to calculate light vectors... If you have rotation matrix of object as M1, rotation matrix of camera as M2, both transposed, you can calculate light position in object space this way:
    LightPosInObjectSpace = (M1 * M2) * (LightPos-ObjPos)
    And now you can calculate light vectors and transform them to tangent space. This works only if you use glRotatef and then glTranslatef for you camera.

    Correct me, if there are any mistakes.
    (usually just hobbyist) OpenGL driver developer

Posting Permissions

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