• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Medo Mex

Directional Light

36 posts in this topic

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

 

0

Share this post


Link to post
Share on other sites

Are you transforming cameraPos?

 

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

1

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
0

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.

0

Share this post


Link to post
Share on other sites

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.

1

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
0

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

 

0

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.

0

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

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

Share this post


Link to post
Share on other sites

@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 :)

0

Share this post


Link to post
Share on other sites

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

Edited by belfegor
1

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

Share this post


Link to post
Share on other sites

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.

0

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.

0

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.

0

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
0

Share this post


Link to post
Share on other sites

@Anddos: I tried that:

mesh->CloneMeshFVF(D3DXMESH_32BIT | D3DXMESH_MANAGED, staticMesh->GetFVF() | D3DFVF_NORMAL, m_d3dDevice, &mesh);

Still the same problem, I don't see the specular light.

0

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
0

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

0

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
0

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
0

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  
Followers 0