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

#21 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 22 July 2013 - 08:07 PM

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



Sponsor:

#22 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 22 July 2013 - 08:18 PM

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!

 

spec.png

 

What's that?


Edited by Medo3337, 22 July 2013 - 08:20 PM.


#23 Anddos   Members   -  Reputation: 471

Like
0Likes
Like

Posted 22 July 2013 - 08:29 PM

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


:)

#24 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 22 July 2013 - 08:32 PM

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


#25 Anddos   Members   -  Reputation: 471

Like
0Likes
Like

Posted 22 July 2013 - 08:54 PM

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, 22 July 2013 - 09:17 PM.

:)

#26 Medo3337   Members   -  Reputation: 665

Like
-1Likes
Like

Posted 22 July 2013 - 09:06 PM

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



#27 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 22 July 2013 - 09:39 PM

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.



#28 Tarika   Members   -  Reputation: 481

Like
0Likes
Like

Posted 23 July 2013 - 06:21 AM

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, 23 July 2013 - 06:21 AM.

We are now on Tumblr, so please come and check out our site! 

 

http://xpod-games.com


#29 belfegor   Crossbones+   -  Reputation: 2362

Like
0Likes
Like

Posted 23 July 2013 - 07:18 AM

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



#30 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 23 July 2013 - 10:09 AM

@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, 23 July 2013 - 10:10 AM.


#31 Anddos   Members   -  Reputation: 471

Like
0Likes
Like

Posted 23 July 2013 - 10:12 AM

how are you creating the sun ?


:)

#32 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 23 July 2013 - 10:48 AM

@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, 23 July 2013 - 10:49 AM.


#33 belfegor   Crossbones+   -  Reputation: 2362

Like
0Likes
Like

Posted 23 July 2013 - 11:00 AM


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



#34 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 23 July 2013 - 11:33 AM

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

 

spec-max.png



#35 belfegor   Crossbones+   -  Reputation: 2362

Like
1Likes
Like

Posted 23 July 2013 - 11:59 AM

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


Edited by belfegor, 23 July 2013 - 12:04 PM.


#36 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 23 July 2013 - 12:47 PM

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


Edited by Medo3337, 23 July 2013 - 12:48 PM.


#37 belfegor   Crossbones+   -  Reputation: 2362

Like
0Likes
Like

Posted 23 July 2013 - 12:58 PM

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, 23 July 2013 - 01:02 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