Diffuse and specular per-pixel lighting (no bumpmapping)

Problems building or running the engine, queries about how to use features etc.
User avatar
DWORD
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 1365
Joined: Tue Sep 07, 2004 12:43 pm
Location: Aalborg, Denmark

Diffuse and specular per-pixel lighting (no bumpmapping)

Post by DWORD »

I know this question has been asked many times before, but I've searched and tried hard for hours without result. I never worked with shaders, and just want to do simple diffuse and specular lighting per pixel using Cg for a start.

The answer in other posts has been to take a look at the Dot3 bumpmapping demo. I did this, and I must admit it's a bit complicated to me. If I understand it correctly, it's necessary to supply tangent vectors through some texture coordinates. I don't have this, because my model is generated in real-time; I only have positions and normals.

Would anyone be so kind to provide a minimal example of how to do diffuse and specular lighting given only vertex positions and normals--if at all possible. I'd very grateful.

Edit: Maybe I should note, that my normals are not normalized to save CPU cycles, as that is my bottleneck.
rincewind
Kobold
Posts: 33
Joined: Tue Jan 04, 2005 5:57 pm

Post by rincewind »

If you don't do normalmapping, you don't need the tangent.
Basicly, what you have to do is similar to what the Dot3-Demo does, just skip the rotation parts.
Also you just pass through your normal from the vertex to the pixel shader in a texcoord and use a cubemap to normalize (just like it's done with the lightVector and halfAngle). Calculation are then the same as in the Dot3-Demo except that you use the passes through normal instead of the one you get from the normalmap.

Hope that helped,

Rincewind
User avatar
DWORD
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 1365
Joined: Tue Sep 07, 2004 12:43 pm
Location: Aalborg, Denmark

Post by DWORD »

Thanks, that clarified it a bit. :) I found out, that in fact I had something working a little for diffuse lighting, just my model normals are not optimal, so I couldn't really see the effect before using e.g. the sphere.mesh. My current script looks like this, and I bet it's not very optimal: :P

Code: Select all

// Expand a range-compressed vector
float3 expand(float3 v)
{
    return (v - 0.5) * 2;
}

void main_vp(float4 position        : POSITION,
             float3 normal          : NORMAL,

             out float4 oPosition   : POSITION,
             out float3 oNormal     : TEXCOORD0,
             out float3 oLight      : TEXCOORD1,

             uniform float3	lightPosition,      // object space
             //uniform float3 eyePosition,        // object space
             uniform float4x4 worldViewProj)
{
    oPosition = mul(worldViewProj, position);

    oNormal = normal;
    oLight = lightPosition - position.xyz;
}

void main_fp(float3 normal          : TEXCOORD0,
             float3 light           : TEXCOORD1,

             out float3 oColor      : COLOR,

             uniform samplerCUBE normalCubeMap)
{
    // retrieve normalised light vector, expand from range-compressed
    float3 lightVec = expand(texCUBE(normalCubeMap, light).xyz);

    float3 normalVec = expand(texCUBE(normalCubeMap, normal).xyz);

    oColor = dot(normalVec, lightVec);
}
I don't really understand the texCUBE-thing. Right now I have two cubemaps in the material script, for tex_coord_set 0 and 1. Is that necessary. :? Please excuse my ignorance.

Edit: I will try your approach...

Edit: Think I got the texCUBE-thing... It says in Example_BumpMapping.cg:

Code: Select all

uniform samplerCUBE normalCubeMap2) // we need this second binding to be compatible with ps_1_1, ps_2_0 could reuse the other