Directional Light

Started by
35 comments, last by belfegor 10 years, 8 months ago
I'm trying to get directional light to work (for sun light), unfortunately I'm seeing static light on the mesh, what's wrong with the following code?

//-----------------------------------------------------------------------
// Pixel shader function (DIRECTIONAL LIGHT)
//-----------------------------------------------------------------------
float4 PS( VS_OUTPUT IN ) : COLOR
{
    float3 viewDir = cameraPos - IN.worldPos;
    float3 lightDir = -lightDirection;
    float3 halfVector = normalize(normalize(lightDir) + normalize(viewDir));

    float3 n = normalize(IN.Normal);
    float3 h = normalize(halfVector);
    float3 l = normalize(lightDir);
    
    float nDotL = saturate(dot(n, l));
    float nDotH = saturate(dot(n, h));
    float power = (nDotL == 0.0f) ? 0.0f : pow(nDotH, materialPower);
    
    float4 FinalDiffuse = materialDiffuse * lightDiffuse;
    float4 FinalSpecular = materialSpecular * lightSpecular;

    float4 color = (materialAmbient + globalAmbient) +
                   (FinalDiffuse * nDotL) + (FinalSpecular * power);
    return color * tex2D(colorMap, IN.UV);
}

Advertisement

Are you transforming cameraPos?

If you were using visual studio 2012 you could debug the shader and inspect values after it runs.

@menohack: I'm using Visual Studio 2010

Yes I'm updating cameraPos value each render.

Here is a part of the C++ code:


// Light
float lightDiffuse[4] = {1.0f, 1.0f, 1.0f, 0.01f};
float lightSpecular[4] = {0.5f, 0.5f, 0.5f, 0.5f};
pEffect->SetValue("lightDiffuse", &lightDiffuse, sizeof(lightDiffuse));
pEffect->SetValue("lightSpecular", &lightSpecular, sizeof(lightSpecular));

// Material
float materialDiffuse[4] = {1.0f, 1.0f, 1.0f, 1.0f};
float materialAmbient[4] = {0.15f, 0.15f, 0.15f, 1.0f};
float materialSpecular[4] = {1.0f, 1.0f, 1.0f, 1.0f};
float materialPower = 10.0f;

float gAmbient[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
pEffect->SetValue("globalAmbient", &gAmbient, sizeof(gAmbient));

D3DXVECTOR3 lightDirection(0.0f, 10.0f, 1.0f);
pEffect->SetValue("lightDirection", &lightDirection, sizeof(D3DXVECTOR3));
pEffect->SetValue("cameraPos", &getCameraPosition(), sizeof(D3DXVECTOR3));

pEffect->SetTexture("colorMapTexture", texture);

I got the directional light to work, but now, I don't see the specular light unless I set a very low number for specular power.

And when I set a very low number such as 1 or 2 I get high specular color.

As long as you aren't running on Win 8 you can use PIX from the the D3D SDK folder it also allows you to debug the shader. Or use the NVidia or AMD tools.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

How many vertices are in your mesh. If you have a model that is not dense enough when you apply specular to it, it will spread across the whole model which makes sense. An good example to test specular on is a sphere for example.

I'd recommend trying to implement bump mapping before implementing any specular lighting. It will show the specular light in much better detail.

Here is a tutorial for bumpmapping http://www.rastertek.com/dx10tut20.html.

Co-Founder of Tesseract Interactive.

Check out our project Excubitor on http://excubitorgame.com and http://www.indiedb.com/games/excubitor

Even the specular light sometimes look weird, here is a screenshot of the specular light (red light):

[attachment=16893:specular1.png]

Here is the Vertex Shader code:


VS_OUTPUT VS( VS_INPUT IN )
{
    VS_OUTPUT Out;
    Out = (VS_OUTPUT)0;
    float4x4 WVP = mul(World, mul(View, Projection));
    Out.Pos = mul(float4(IN.Pos, 1.0f), WVP);
    Out.UV = IN.UV;
    Out.Normal = mul(IN.Normal, (float3x3)World);
    Out.worldPos = mul(float4(IN.Pos, 1.0f), World).xyz;
    return Out;
}

Any idea what's going on?

Couple of things not mention, 1) where is the light, 2) did you multiply the specular using the dot product

I saw in the previous posting of your PS code that you did not do #2 properly, the specular reflection looks like it's on the wrong side of the wall. Without knowing #1 I can't be sure. Plus the way you did the specular power nDotL check, it should be nDotL <= 0, not == which would set the power to 0 only along the light/dark border.

******************************************************************************************
Youtube Channel

This topic is closed to new replies.

Advertisement