Directional Light

Started by
35 comments, last by belfegor 10 years, 8 months ago

how are you creating the sun ?

:)
Advertisement

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


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

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

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

material-editor-01-defau.jpg


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

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

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.

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.

This topic is closed to new replies.

Advertisement