# Directional Light

## Recommended Posts

Medo Mex    891

@Anddos: I have been looking at the sample for a very long time, I still didn't get it to work as expected.

You might want to look at the shader code that I recently posted to make sure that there is no bugs.

##### Share on other sites
Medo Mex    891

When I change the line:

float3 viewDir =  cameraPos - worldPos;

To:

float3 viewDir =  worldPos - cameraPos;

I get better results, however, when I set a small number for the material power (shininess) I get very high specular light and when I set a large number for the material power (shininess) I get smaller specular light.

##### Share on other sites
AmzBee    485

Daft question, but why are you multiplying the normal by the world matrix in your vertex shader?

Out.Normal = mul(IN.Normal, (float3x3)World);


Aimee

Edited by AmzBee

##### Share on other sites
belfegor    2834

@AmzBee

Daft question, but why are you multiplying the normal by the world matrix in your vertex shader?

They are directional vectors and are dependent on object orientation.

@Medo

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

specular highlight wiki

Specular reflection is visible only where the surface normal is oriented precisely halfway between the direction of incoming light and the direction of the viewer; this is called the half-angle direction because it bisects (divides into halves) the angle between the incoming light and the viewer

I get better results, however, when I set a small number for the material power (shininess) I get very high specular light and when I set a large number for the material power (shininess) I get smaller specular light.

That is to be expected.

##### Share on other sites
Medo Mex    891

@belfegor:

That is to be expected.

How do I fix that? I'm expecting If:

Shininess = 0 then no specular light

Shininess = 5 then a little specular

Shininess = 80 then high specular

What I'm getting now? 1 = High specular light, 80 = Less specular light, 200 = Tiny specular light

Edited by Medo3337

##### Share on other sites
Anddos    588

how are you creating the sun ?

##### Share on other sites
Medo Mex    891

@Anddos: I'm just creating a single directional light:

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, 1.0f, 1.0f, 1.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 = 1.0f; // *** Here I'm setting the value to 1.0f and getting high specular light, I'm expecting the specular light to be low since the value is low number ***

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

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

// Directional light
D3DXVECTOR3 lightDirection(-1.0f, 0.0f, -1.0f);
pEffect->SetValue("lightDirection", &lightDirection, sizeof(lightDirection));

pEffect->SetValue("cameraPos", &camera->position(), sizeof(camera->position()));
pEffect->SetTexture("colorMapTexture", texture);
Edited by Medo3337

##### Share on other sites
belfegor    2834

How do I fix that?

What is broken?

You could make variety of material types with different "Shininess" in combination with specular maps (watch this video about specular maps).

##### Share on other sites
Medo Mex    891

@belfegor: I think there is something confusing here, isn't "Shininess" same as specular level?

I want to control how much specular color will affect the mesh.

In 3Ds Max I control that by setting "Specular level" in the material window, how do I use this level in my light shader?

[attachment=16959:spec-max.png]

##### Share on other sites
belfegor    2834

Here is the hlsl output for 3ds max default standard material:

struct PixelInput {
float4 norm        : TEXCOORD0;
float4 worldpos    : TEXCOORD1;
float4 eyevector    : TEXCOORD2;
float4 tangent        : TEXCOORD3;
float4 binormal    : TEXCOORD4;
float4 objpos        : TEXCOORD5;
float4 tex0        : TEXCOORD6;
float4 tex1        : TEXCOORD7;
};

struct PixelOutput {
float4 c         : COLOR;
};

uniform float4 Diffuse = float4(0,588235,0,588235,0,588235,1,000000);
uniform float4 Specular = float4(0,900000,0,900000,0,900000,1,000000);
uniform float4 Emissive = float4(0,000000,0,000000,0,000000,1.0f);
uniform float SpecularLevel = 1,000000;
uniform float GlossLevel = 9,999999;
uniform float3 LightCol0;
uniform float3 LightDir0;
uniform float LightFallOff0;
uniform float LightHotSpot0;
uniform float Opacity = 1,000000;

PixelOutput fragmentEntry(PixelInput pi)
{
PixelOutput PO;
float3 N;
N = normalize(pi.norm.xyz);
float3 Eye;
Eye.xyz = normalize(pi.eyevector.xyz);
float4 nColor = float4(0.0,0.0,0.0,1.0);
float4 mDiffuse = Diffuse;
float4 mSpecular = Specular;
float4 SelfIllum = Emissive;
nColor = nColor + SelfIllum * mDiffuse;
float3  H;
float3 Light;
float f;
float fAtt;
float fDist;
/*  default light */
fAtt = 1.0;
nColor = nColor + float4(LightCol0,1.0) * mDiffuse * clamp(dot(N,LightDir0),0,1) * fAtt;
H = normalize(Eye+Light);
f = clamp(dot(N,H),0,1);
f = pow(f, GlossLevel);
f = f * SpecularLevel;
nColor = nColor + float4(LightCol0,1.0) * mSpecular * f * fAtt;
/* end of default light */
nColor.a = Opacity;
PO.c = nColor;
return PO;
}



Looks like they use Glossiness as "materialPower/Shinniness" and "Specular level" is just scalar for specular power (i would set it to 100 (1.0) to match your shader).

Edited by belfegor

##### Share on other sites
Medo Mex    891

@belfegor: Damn, I have been thinking that the specular shininess is the specular level!

I notice that it's set to 10 in 3Ds Max while in the shader it's "uniform float SpecularLevel = 1,000000;", why is that?

I will get the specular level from the .x file so I can pass the value to the shader, is the following correct?

float power = pow(nDotH, materialPower);
power *= specularLevel;

Remember that If I set specular level to 80 in 3Ds Max I want it to appear the same in my own program.

Edited by Medo3337

##### Share on other sites
belfegor    2834

I notice that it's set to 10 in 3Ds Max while in the shader it's "uniform float SpecularLevel = 1,000000;", why is that?

I would say because artists are accustomed on that way, looks like that is represented as percent in max and then converted before sent to shader.
There is other similar things too, for example most people are accustomed to degrees (because it is easier to visualise it) but some functions expect number in radians, in some scripting languages array indexing starts at 1 not 0 as in c++... etc

Remember that If I set specular level to 80 in 3Ds Max I want it to appear the same in my own program.

Use the same shader lightning formula and you will get same output.

I notice that it's set to 10 in 3Ds Max while in the shader it's "uniform float SpecularLevel = 1,000000;", why is that?

I noticed one thing so i will mention it to you. If you save material as FX from max, do not overwrite existing file as it will not update it, so rename it or delete old.

Edited by belfegor