Sign in to follow this  
Medo Mex

Directional Light

Recommended Posts

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);
}

 

Share this post


Link to post
Share on other sites

@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);
Edited by Medo3337

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by BornToCode

Share this post


Link to post
Share on other sites

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;
}

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

@LancerSolurus:

 

1) I'm setting the light direction to (1, 0, 1), it's a directional light (no position).

2) Not sure what you mean exactly

 

Here is the entire effect file:

float4x4 World;
float4x4 View;
float4x4 Projection;

// Light
float3 lightDirection;  // Directional light
float4 lightDiffuse;
float4 lightAmbient;
float4 lightSpecular;

// Material
float4 materialDiffuse;
float4 materialAmbient;
float4 materialSpecular;
float materialPower;

float4 globalAmbient;

// Camera position
float3 cameraPos;

// Texture
texture colorMapTexture;
sampler2D colorMap = sampler_state
{
    Texture = <colorMapTexture>;
    MagFilter = Linear;
    MinFilter = Anisotropic;
    MipFilter = Linear;
    MaxAnisotropy = 16;
};

struct VS_INPUT
{
   float3 Pos : POSITION;
   float2 UV : TEXCOORD0;
   float3 Normal : NORMAL;
};

struct VS_OUTPUT
{
   float4 Pos : POSITION;
   float3 worldPos : TEXCOORD1;
   float2 UV : TEXCOORD0;
   float3 Normal : NORMAL;
};

//-----------------------------------------------------------------------
// Vertex shader function
//-----------------------------------------------------------------------
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).xyz;
    Out.worldPos = mul(float4(IN.Pos, 1.0f), World).xyz;
    return Out;
}

//-----------------------------------------------------------------------
// Pixel shader function
//-----------------------------------------------------------------------
float4 PS( VS_OUTPUT IN ) : COLOR
{
    float3 viewDir = normalize(cameraPos - IN.worldPos);
    float3 lightDir = normalize(-lightDirection);
    float3 halfVector = normalize(lightDir + 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);
}

//-----------------------------------------------------------------------------
// Techniques.
//-----------------------------------------------------------------------------
technique DirectionalLight
{
    pass
    {
        AlphaBlendEnable = false;
        ZEnable = true;
        VertexShader = compile vs_3_0 VS();
        PixelShader = compile ps_3_0 PS();
    }
}

Share this post


Link to post
Share on other sites

nDotL and nDotH are saturated (clamped to 0 - 1 range) so i see no reason to check anything as it will return valid number, just make sure that your "materialPower" > 0

//float power = (nDotL <= 0.0f) ? 0.0f : pow(nDotH, materialPower);
float power = pow(nDotH, materialPower);

Share this post


Link to post
Share on other sites

@belfegor:

 

C++:

D3DXHANDLE hTechnique;
hTechnique = pEffect->GetTechniqueByName("DirectionalLighting");
if (FAILED(pEffect->SetTechnique(hTechnique)))
    return;

D3DXMATRIX view = camera->viewMatrix();
D3DXMATRIX proj = camera->projectionMatrix();

pEffect->SetMatrix("View", &view);
pEffect->SetMatrix("Projection", &proj);

// Light
float lightDiffuse[4] = {0.9f, 0.9f, 0.9f, 1.0f};
float lightSpecular[4] = {1.0f, 0.0f, 0.0f, 0.0f};
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 = 20.0f;

pEffect->SetValue("materialDiffuse", &materialDiffuse, sizeof(materialDiffuse));
pEffect->SetValue("materialAmbient", &materialAmbient, sizeof(materialAmbient));
pEffect->SetValue("materialSpecular", &materialSpecular, sizeof(materialSpecular));
pEffect->SetValue("materialPower", &materialPower, sizeof(materialPower));

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

float lightDirection[3] = {1.0f, 0.0f, 1.0f};
pEffect->SetValue("lightDirection", &lightDirection, sizeof(lightDirection));

float cameraPos[3] = {camera->position().x, camera->position().y, camera->position().z};
pEffect->SetValue("cameraPos", cameraPos, sizeof(cameraPos));

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

UINT passes = 0;
pEffect->Begin(&passes, 0);
pEffect->BeginPass(0);

D3DXMATRIX mWorld = model->worldMatrix();
pEffect->SetMatrix("World", &mWorld);

model->render(dt);

pEffect->EndPass();
pEffect->End();

Shader:

float4x4 World;
float4x4 View;
float4x4 Projection;

// Light
float4 lightDiffuse;
float4 lightSpecular;
float3 lightDirection;  // Directional light

// Material
float4 materialDiffuse;
float4 materialAmbient;
float4 materialSpecular;
float materialPower;

float4 globalAmbient;

// Camera position
float3 cameraPos;

// Texture
texture colorMapTexture;
sampler2D colorMap = sampler_state
{
    Texture = <colorMapTexture>;
    MagFilter = Linear;
    MinFilter = Anisotropic;
    MipFilter = Linear;
    MaxAnisotropy = 16;
};

struct VS_INPUT
{
   float3 Pos : POSITION;
   float2 UV : TEXCOORD0;
   float3 Normal : NORMAL;
};

struct VS_OUTPUT
{
   float4 Pos : POSITION;
   float2 UV : TEXCOORD0;
   float3 Normal : NORMAL;
   float3 worldPos : TEXCOORD4;
};

//-----------------------------------------------------------------------
// Vertex shader function
//-----------------------------------------------------------------------
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).xyz;
    Out.worldPos = mul(float4(IN.Pos, 1.0f), World).xyz;
    return Out;
}

//-----------------------------------------------------------------------
// Pixel shader function
//-----------------------------------------------------------------------
float4 PS( VS_OUTPUT IN ) : COLOR
{
    float3 viewDir = normalize(cameraPos - IN.worldPos);

    float3 lightDir = normalize(-lightDirection);
    float3 halfVector = normalize(lightDir + 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 = 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);
}

//-----------------------------------------------------------------------------
// Techniques.
//-----------------------------------------------------------------------------
technique DirectionalLighting
{
    pass
    {
        AlphaBlendEnable = false;
        ZEnable = true;
        VertexShader = compile vs_3_0 VS();
        PixelShader = compile ps_3_0 PS();
    }
}

Share this post


Link to post
Share on other sites

@belfegor: Have you tried to scale and move the teapot?

 

It works much better with the teapot, but when I scale and move it sometimes I see weird specular (Like the one I posted in the screenshot).

 

Generally, the diffuse light works well with any mesh, but the specular disappear with my box mesh, sometimes it show up, but it looks weird.

 

I believe there is a problem with the shader code.

Share this post


Link to post
Share on other sites

@belfegor: Okay, after testing for sometime

 

I notice that the shapes I create manually works much better with the shader light (D3DXCreateTeapot, D3DXCreateBox, D3DXCreateSphere, etc...) All works very well with the light.

 

However, when I load a mesh using D3DXLoadMeshFromX(), the specular light get messed up.

Share this post


Link to post
Share on other sites

@belfegor: Okay, after testing for sometime

 

I notice that the shapes I create manually works much better with the shader light (D3DXCreateTeapot, D3DXCreateBox, D3DXCreateSphere, etc...) All works very well with the light.

 

However, when I load a mesh using D3DXLoadMeshFromX(), the specular light get messed up.

 

 

did you clone the mesh so it has NORMALS?

Edited by Anddos

Share this post


Link to post
Share on other sites

Well, I still don't see the specular light, however when I move the mesh and rotate it I begin to see the specular light (but in weird shape and just a tiny spot in random position on the mesh):

 

When the I set the material power to a smaller number for example (1.0) I get larger specular!

 

[attachment=16937:spec.png]

 

What's that?

Edited by Medo3337

Share this post


Link to post
Share on other sites

did you inverse the meshs worldspace to localspace?, you need to be working in localspace matrix in order to get specular lighting to show on the model...

Share this post


Link to post
Share on other sites

@Anddos: I'm doing the following:

// In Vertex Shader:
Out.worldPos = mul(float4(IN.Pos, 1.0f), World).xyz;

//In Pixel Shader:
float3 viewDir = normalize(cameraPos - IN.worldPos);
Edited by Medo3337

Share this post


Link to post
Share on other sites

I have attached a sample of some source code that shows how do specular lighting,there is a prebuilt exe to so you can test it before you make you're own changes.

Edited by Anddos

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this