Jump to content

  • Log In with Google      Sign In   
  • Create Account


Directional Light


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
36 replies to this topic

#1 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 18 July 2013 - 12:59 PM

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

 



Sponsor:

#2 menohack   Members   -  Reputation: 216

Like
1Likes
Like

Posted 18 July 2013 - 03:02 PM

Are you transforming cameraPos?

 

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



#3 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 18 July 2013 - 04:10 PM

@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, 18 July 2013 - 11:12 PM.


#4 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 19 July 2013 - 12:14 AM

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.



#5 NightCreature83   Crossbones+   -  Reputation: 2739

Like
1Likes
Like

Posted 19 July 2013 - 02:07 AM

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, Mad Max

#6 BornToCode   Members   -  Reputation: 920

Like
0Likes
Like

Posted 19 July 2013 - 09:15 AM

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, 19 July 2013 - 09:16 AM.


#7 BigGiantHead   Members   -  Reputation: 182

Like
0Likes
Like

Posted 19 July 2013 - 11:03 AM

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


#8 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 20 July 2013 - 01:15 AM

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

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

 



#9 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 20 July 2013 - 12:01 PM

Any idea what's going on?



#10 LancerSolurus   Members   -  Reputation: 578

Like
0Likes
Like

Posted 20 July 2013 - 04:39 PM

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


#11 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 20 July 2013 - 08:48 PM

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


#12 belfegor   Crossbones+   -  Reputation: 2558

Like
0Likes
Like

Posted 21 July 2013 - 03:04 AM

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


#13 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 21 July 2013 - 01:00 PM

@belfegor: That won't really help, I still have the same problem, you might want to check my shader code to see yourself what's going on :)



#14 belfegor   Crossbones+   -  Reputation: 2558

Like
1Likes
Like

Posted 21 July 2013 - 01:20 PM

Copy/paste your cpp and whole shader again so i can see everything involved, try not to remove/edit something out.


Edited by belfegor, 21 July 2013 - 01:21 PM.


#15 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 21 July 2013 - 01:42 PM

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


#16 belfegor   Crossbones+   -  Reputation: 2558

Like
0Likes
Like

Posted 21 July 2013 - 02:26 PM

I have just plugged your code and shader in some of my project, changed only light specular to pure white:

test2013-07-2122-21-01.jpg

 

try D3DXCreateTeapot instead of your model for test.



#17 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 21 July 2013 - 06:37 PM

@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.



#18 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 22 July 2013 - 06:04 AM

@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.



#19 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 22 July 2013 - 03:29 PM

Up



#20 Anddos   Members   -  Reputation: 485

Like
0Likes
Like

Posted 22 July 2013 - 08:05 PM

@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, 22 July 2013 - 08:06 PM.

:)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS