Jump to content

  • Log In with Google      Sign In   
  • Create Account


Adding material into light calculation?


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
15 replies to this topic

#1 cozzie   Members   -  Reputation: 1468

Like
0Likes
Like

Posted 29 December 2012 - 08:24 AM

Hi,

 

Happy as I was in getting my first VS/PS working with ambient and diffuse lighting, I'm now trying to add the ambient and diffuse material into the calculation. Been trying a lot of variants but now yet with succes.

 

Here's the calculation I'm doing:

float4      MatAmb : MATERIALAMBIENT; 
float4      MatDiff: MATERIALDIFFUSE; 

float3 DiffLightDir <  string UIDirectional = "Light Direction"; > = {1.0, 0.0, 0.0};
float4 DiffLightColor = { 1.0f, 1.0f, 1.0f, 1.0f };
float  DiffLightIntensity = 0.0f;

float4      AmbientColor;
float       AmbientIntensity;

VS_OUTPUT VS_function(VS_INPUT input)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;

    float4 worldPosition = mul(input.Pos, World);
    Out.Pos = mul(worldPosition, ViewProj);

    float4 normal = mul(input.Normal, WorldInvTransp);
    float lightIntensity = dot(normal, DiffLightDir);

    Out.Color = saturate(DiffLightColor * DiffLightIntensity * lightIntensity * MatDiff); 
    Out.Normal = normal;
    Out.TexCoord = input.TexCoord;

    return Out;
}

/****************************************************/
/**          THE PIXELSHADER        PROGRAM        **/
/****************************************************/

float4 PS_function(VS_OUTPUT input): COLOR0
{
    float4 textureColor = tex2D(textureSampler, input.TexCoord);
    textureColor.a = 1;

    return saturate((input.Color + AmbientColor * AmbientIntensity) * textureColor);
}

 

What I thought was the solution for ambient, was:

 

return saturate((input.Color + AmbientColor * AmbientIntensity) * textureColor * MatAmb);

 

But the endresult is for each material 'black'/ dark, I'm 100% sure all vars are filled with valid values.

Can you give me some pointers or help me out?



Sponsor:

#2 cozzie   Members   -  Reputation: 1468

Like
0Likes
Like

Posted 29 December 2012 - 08:44 AM

I have tried this, giving a bit better result but not correct:

 

float amb = AmbientColor * AmbientIntensity;

float diff = input.Color;

return (amb * MatAmb + diff * MatDiff) * textureColor;

 

Now I get the material color taken into account, but for example red light also lights up a material which is green

(i.e. light: RGB = 1, 0, 0, material ambient = 0,1,0)



#3 lipsryme   Members   -  Reputation: 986

Like
0Likes
Like

Posted 29 December 2012 - 10:45 AM

I think what you want to do is something like this

 

// Incoming radiance (this would be the color and intensity of the light energy
// that is coming towards your surface/shaded pixel)
float3 Li = LightColor * LightIntensity;  

// This would correspond to your LightIntensity (the cosine factor)
float NdotL = saturate(dot(Normal, LightDirection)); 

// Your final diffuse light output (or composition) 
float3 Diffuse = NdotL * Li;

// Your ambient light term
float3 AmbientLight = AmbientLight;

// Now "add" the ambient light to your diffuse composition
Diffuse += AmbientColor * AmbientIntensity;

// And at last multiply the result of your light computation by your material's
// diffuse color (albedo) -> basically the regular texture color
float3 output = Diffuse * Albedo;

 

 


Edited by lipsryme, 29 December 2012 - 12:41 PM.


#4 cozzie   Members   -  Reputation: 1468

Like
0Likes
Like

Posted 29 December 2012 - 12:27 PM

Hi, thanks. I got it, with the difference that I have a different diffuse and ambient material.
Part of the lighting calculation (diffuse light) is done in te Vertex shader, the rest in the pixel shader.

The working result now is:
 

float4 PS_function(VS_OUTPUT input): COLOR0
{
    float4 textureColor = tex2D(textureSampler, input.TexCoord);
    textureColor.a = 1;

    return saturate((input.Color * MatDiff + AmbientColor * AmbientIntensity * MatAmb) * textureColor);
}

The only thing I don't understand is that splitting the formula up, doesn't give the correct result.
I tried it like this:

float amb = AmbientColor * AmbientIntensity * MatAmb
float diff = input.Color * MatDiff
return saturate(diff + amb) * textureColor

This doesn't work though, resulting pixels are black/ not as expected.
I tried to fund this by doing a pseudo formula:

return saturate((input.Color * MatDiff + AmbientColor * AmbientIntensity * MatAmb) * textureColor);
equals
result = ((A * B + C * D * E) * F)

amb = C * D * E
diff = A * B

result = (A + B) * F

When I fill in fictive numbers for A though F, I get the same result calculating it in one formula as when I do it in with the amb/diff step in between.
Strange? or am I missing something



#5 lipsryme   Members   -  Reputation: 986

Like
0Likes
Like

Posted 29 December 2012 - 12:48 PM

Am I correct in thinking MatDiff is just a diffuse color coming from the material you've defined ?
If it is then you should multiply this with the texture color before you combine it with the lighting (input.Color).

Basically MatDiff and textureColor is the same "variable", being the final diffuse color of your material(not surface/pixel).
Which is then multiplied by the lighting.

If that's no different then maybe try to remove the saturate and see what happens...

The only way for it to be black is if your ambient term equals zero and your cosine factor (N dot L) or the material's diffuse color equals zero. Try to check if they are > 0.0f if not then either there's something wrong or it being completely black is the correct outcome :)

Edited by lipsryme, 29 December 2012 - 01:05 PM.


#6 rdragon1   Crossbones+   -  Reputation: 1174

Like
0Likes
Like

Posted 29 December 2012 - 01:18 PM

The only thing I don't understand is that splitting the formula up, doesn't give the correct result.

I tried it like this:

float amb = AmbientColor * AmbientIntensity * MatAmb
float diff = input.Color * MatDiff
return saturate(diff + amb) * textureColor
 

 

I think you mean for those types to be float3



#7 cozzie   Members   -  Reputation: 1468

Like
0Likes
Like

Posted 29 December 2012 - 03:12 PM

They're all actually float4 vars.

@Lipsryme; I understand what you mean, tried this:

 

// return saturate((input.Color * MatDiff + AmbientColor * AmbientIntensity * MatAmb) * textureColor);

float amb = AmbientColor * AmbientIntensity * MatAmb * textureColor;

float diff = input.Color * MatDiff * textureColor;

return saturate(diff + amb);

 

This gives a nice black & white scene (looks really cool :)) but not the same result as the total formula.

Will play around to see if I can find it out.

 

By the way, wouldn't it in the end be faster performance wise, to have the 1 complete calculation? (instead of 2 and extra vars etc.)



#8 rdragon1   Crossbones+   -  Reputation: 1174

Like
0Likes
Like

Posted 29 December 2012 - 03:58 PM

They're all actually float4 vars.

 

Then can you paste your *actual* code? Because what you've written there only says 'float', which is not a float4.



#9 cozzie   Members   -  Reputation: 1468

Like
0Likes
Like

Posted 29 December 2012 - 04:30 PM

Sorry, here it is.

Where the not commented calculation of the output color in the PixelShader works perfect and the commented one doesn't give the correct result:

float3 DiffLightDir <  string UIDirectional = "Light Direction"; > = {1.0, 0.0, 0.0};
float4 DiffLightColor = { 1.0f, 1.0f, 1.0f, 1.0f };
float  DiffLightIntensity = 0.5f;

float4x4    World         : WORLD;
float4x4    WorldInvTransp: WORLDINVTRANSP;
float4x4    ViewProj      : VIEWPROJECTION;

float4      AmbientColor;
float       AmbientIntensity;

float4      MatAmb : MATERIALAMBIENT; 
float4      MatDiff: MATERIALDIFFUSE; 
            
texture     Tex0 < string name = "textures\\wdplanks.png"; >;

VS_OUTPUT VS_function(VS_INPUT input)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;

    float4 worldPosition = mul(input.Pos, World);
    Out.Pos = mul(worldPosition, ViewProj);

    float4 normal = mul(input.Normal, WorldInvTransp);
    float lightIntensity = dot(normal, DiffLightDir);

    Out.Color = saturate(DiffLightColor * DiffLightIntensity * lightIntensity); 
    Out.Normal = normal;
    Out.TexCoord = input.TexCoord;

    return Out;
}

float4 PS_function(VS_OUTPUT input): COLOR0
{
    float4 textureColor = tex2D(textureSampler, input.TexCoord);
    textureColor.a = 1;

    return saturate((input.Color * MatDiff + AmbientColor * AmbientIntensity * MatAmb) * textureColor);

    float amb = AmbientColor * AmbientIntensity * MatAmb;
    float diff = input.Color * MatDiff;

//    return saturate((diff + amb) * textureColor);
}

Input.color in de PX is the diffuse color component, as output of the VS.

The result when I uncomment the line is that the Material colors (MatAmb and MatDiff) are not visible in the result (just texture colours).



#10 rdragon1   Crossbones+   -  Reputation: 1174

Like
0Likes
Like

Posted 29 December 2012 - 05:43 PM

So the problem is exactly what I suggested. Your 'amb' and 'diffuse' variables should be float4's, but they're floats.



#11 cozzie   Members   -  Reputation: 1468

Like
0Likes
Like

Posted 29 December 2012 - 06:44 PM

Aah I get it, sorry, didn't see it right away.

Just changed it, and now it works :) thanks!

 

What would you think would be best performance wise, doing the separate calculations or doing it at once?

(like I did up till now)



#12 lipsryme   Members   -  Reputation: 986

Like
0Likes
Like

Posted 29 December 2012 - 07:08 PM

As far as I know, and someone correct me if I'm wrong but I don't see how it would create more operations for the hardware to do then doing it in a single line. And as far as the extra variable goes, it could be quite possible that the compiler sees this as redundant anyway and replaces it with the "single-line" approach. I'm not an expert on this though. Worst case scenario is having wasted a few bytes of graphics memory for those extra variables but having a much cleaner and readable code. But that's just me :)

#13 rdragon1   Crossbones+   -  Reputation: 1174

Like
0Likes
Like

Posted 29 December 2012 - 07:29 PM

Check the generated shader assembly output to find out if they're equivalent.



#14 cozzie   Members   -  Reputation: 1468

Like
0Likes
Like

Posted 30 December 2012 - 05:41 AM

Thanks,I'm trying it right away.

Do get some errors though when checking it using 'fxc.exe' from the DX sdk:

 

- working.fx(70,28): warning X3206: 'dot': implicit truncation of vector type

- working.fx(73,16): warning X3206: 'implicit truncation of vector type

- error X3501: 'main' entrypoint not found

 

compilation failed; no code producted

 

Could this have to do with DX10/DX9 compilation for the FX/ shader?

I'm using DX9 and read that in the SDK the compilations will be done based on syntax/ compilation of DX10 (and 11?).

 

If I open the same file in effectedit (also DX SDK) I don't get these errors.

Wíll try to figure out if there's a warning level or something in effectedit.

 

By the way strange that D3DXCreateEffectFromFile gives back a D3D_OK and the result is also as expected..

Any hints how to pick this up?



#15 cozzie   Members   -  Reputation: 1468

Like
0Likes
Like

Posted 30 December 2012 - 05:55 AM

Sorry, being stupid, forgot to give the /T parameter with fx_2_0 (since i use d3d9). Will keep you posted on the output comparison of the 2 styles of calculations

#16 cozzie   Members   -  Reputation: 1468

Like
1Likes
Like

Posted 30 December 2012 - 07:45 AM

Good news, I got the compilation comparison done :)

Here are the 2 results (TXT files):

www.sierracosworth.nl/gamedev/output1.txt

www.sierracosworth.nl/gamedev/output2.txt

 

When I compare the results, both outputs are 100% the same.

Good to know this, so I can tweak the shader to how I can read and work with it best, without affecting the result/ performance.

 

Thanks again for your help.






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