Thread: Per-pixel dot3 lighting

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    Per-pixel dot3 lighting

    I'm wondering if anyone here has attempted to code any vertex/pixel shaders in HLSL or in pure assembly.

    My goal is to write a simple per-pixel dot3 shader which would calculate the amount of light falling not just on a surface, but at each pixel. I'm not so concerned with phong lighting as of right now nor specular highlights - just getting the cosine lighting working right now would be great.

    I'm using the Direct3D equation for lighting to arrive at the final pixel color based on diffuse, ambient, and texel color.

    My question is in my vertex shader should I just pass on the texture coordinates and the vertex coordinates since the pixel shader really does not need them?

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    I'll admit I haven't done anything with shaders, but nobody else has replied so maybe just discussing it will help you.

    The thing that doesn't make sense is that you need to get the texel color (the color from the texture) which you say you need when computing the lighting terms, but, how can you do this without texture coordinates? It's my understanding that you need the texture coordinates to do the texture lookup to get the pertinent color (I don't know at what stage the texture lookup occurs though).

    If you've already got the texel color then tossing the extra data would seem to be a good idea.

    I know my response doesn't really help but...well...

    edit:
    so, are you going to use hlsl or asm? You can use CG as well can't you?
    See you in 13

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The texel color will come from the texture coordinates which is one of the required inputs. The problem I'm seeing is that most of the books I'm looking at like Shader X2: Tips and Tricks and Shader X2: Introductions and Tutorials are all showing that I must pass in a vector called tangent.

    From this tangent vector then they transform it into b and n vectors which then they cross. Then they take the result of b x n or the binormal vector and do a 4x4 multiply to transform into tangent space.

    But in all of my 3D math books not one of them mentions tangent space so I'm not sure what this really is. So I do not know what to pass along to my vertex shader to represent the tangent vector. It's not the texture coords because that is being passed as well in another register.

    My understanding of tangent space is limited to the the following: moving around on a texture in 3 space is known as moving through tangent space - regardless of the orientation of the texture, triangle, etc, you are still moving along tangent space base vectors as you move in 3 space to access every texel of the 2D texture that has been applied to the object.

    But all I want to do is dot3 lighting which to my knowledge does not require tangent space. It just requires the direction to the light and the normal to the plane to be lit. So, in theory, my vertex shader should transform the vertices into clip space and pass along the texture information untouched - after all the u,v coords will not change at the vertices. Supposedly then when I access the texture in the pixel shader it will be at the texel level which allows me to do per-pixel dot3 lighting. So then its a simple dp3 operation in asm and the output texel will be a combination of the lighting properties that have been passed along from the code to the vertex shader to the pixel shader.

    But tangent space is killing me here. Perhaps I need to order yet another book on 3D mathematics that discusses this in greater detail.
    Last edited by VirtualAce; 01-20-2005 at 12:47 AM.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    I'd have to personally read up on the methods for dot3 lighting to offer any reasonably good input from here on, which I doubt I'll have time for.
    See you in 13

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Dot3 lighting is something you already know. It's just standard cosine lighting. But with pixel shaders I can do this per-pixel instead of per-vertex and then linear interpolating across the triangle. With pixel shaders you don't 'guess' what color should be at point x,y you compute which color should be there.

    Something graphics programmers have wanted to do since the dawn of 3D. Compute lighting properties at the pixel level instead of the vertex level.

    Code:
    ..
    D3DXVector3 LightPosition;
    D3DXVector3 VertexPosition;
    
    D3DXVector3 ToLight=LightPosition-VertexPosition;
    
    D3DXVec3Normalize(&ToLight,&ToLight);
    
    
    float dot=D3DXVec3Dot(&ToLight,&Normal);
    
    Illumination=FinalColor*dot;
    ..

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    Oh, wow, that IS extremely straight forward (goes to show how little I knew about dot3 lighting).

    So, I see your conundrum about the tangent space thing. Your understanding of tangent space is correct though.

    So:final color is the color of the pixel. Vertex position is either a) the position of one of the three vertices of the triangle or b) the equivalent position of the texel in 3D space...this would be the position of the texel as you interpolate across the polygon when you rasterize it.

    If it's b, you would need tangent space vectors to find the interpolated position, in order to get the vector from the light position to the vertex position. But, I'm not totally sure how to use the tangent space vectors to do this on a triangle.

    (well, I DO have an idea but not sure if it's *the* way of doing it).

    Tell me what you think.
    Last edited by Darkness; 01-21-2005 at 09:56 AM.
    See you in 13

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing pixel colour
    By redruby147 in forum C Programming
    Replies: 11
    Last Post: 06-09-2009, 05:28 AM
  2. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  3. Eliminating lighting "over-bright"
    By psychopath in forum Game Programming
    Replies: 1
    Last Post: 05-31-2006, 06:52 PM
  4. Lighting in OpenGL
    By MathFan in forum Game Programming
    Replies: 5
    Last Post: 06-28-2004, 12:56 PM
  5. Replies: 5
    Last Post: 02-26-2003, 07:53 PM