Jump to content
  • Advertisement
Sign in to follow this  
Psilobe

HLSL Diffuse shader

This topic is 2554 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to create a basic diffuse shader but the something goes wrong, instead of my little teapot getting diffuse it gets pitch black.
The shader compiles without error. I've compared my code to multiple example diffuse shaders but cant figure out what's wrong. I'm writing the shader in rendermonkey.


float4x4 view_proj_matrix;
float4 Light1_Position;
float4x4 inv_view_matrix;

struct VS_INPUT
{
float4 Position : POSITION0;
float4 Normal: NORMAL0;
};

struct VS_OUTPUT
{
float4 Pos: POSITION;
float4 Nor: TEXCOORD0;
float3 LightVector: TEXCOORD1;
};

VS_OUTPUT vs_main(VS_INPUT Input)
{
VS_OUTPUT Out;

Out.Pos = mul(view_proj_matrix, Input.Position);
Out.Nor = normalize(Input.Normal);
Out.LightVector = normalize(Light1_Position-Input.Position);

return Out;
}


float4 Light1_Color;

struct VS_OUTPUT
{
float4 Pos: POSITION;
float4 Nor: TEXCOORD0;
float3 LightVector: TEXCOORD1;
};

float4 ps_main(VS_OUTPUT Input) : COLOR
{

float4 diffuse = dot(Input.Nor,Input.LightVector);
return (Light1_Color*diffuse);

}


Share this post


Link to post
Share on other sites
Advertisement

You need to also transform the input normal by the (3x3) of the view transformation.


No, he doesn't. He could try that, though. That is only needed when he would do the other stuff in view-space as well. But since neither "LightVector" nor "Position" look like viewspace I don't think that he has to.

I think the issue is that you "Input.Nor" is a float4, while "Input.LightVector" is a float3.
Also you seem to be doing alphablending because the results of the dot() go directly into the float4 you output.

I suggest to change the shader to the following:

float4 ps_main(VS_OUTPUT Input) : COLOR
{

float3 diffuse = dot(Input.Nor.xyz, Input.LightVector);
return (Light1_Color.rgb * diffuse, 1);

}


[font="Arial"]You also don't really need to have your normals as a float4. Float3 is sufficient. (Remember that you can use "[/font][font="Arial"].xyz[/font]"[font="Arial"] after the normalize() to get only the first 3 values)
Same goes for "Light1_Color". You don't need the Alpha-Channel there.
[/font]

Share this post


Link to post
Share on other sites
Ok so now the model is white all around. It should be more like a beige color if done correctly. I've tried switching things around to test but no matter what I do it stays white. I believe I'm doing something else wrong and missing something basic for shading.

But thanks for the help so far. :)




float4 Light1_Color;

struct VS_OUTPUT
{
float4 Pos: POSITION;
float4 Nor: TEXCOORD0;
float3 LightVector: TEXCOORD1;
};

float4 ps_main(VS_OUTPUT Input) : COLOR0
{

float3 diffuse = clamp(dot(Input.Nor.xyz,Input.LightVector),0,1);
return (Light1_Color.xyz*diffuse,1);

}

Share this post


Link to post
Share on other sites
So, for the next steps I would suggest checking your normals and the LightVector.

At first you should try to modify your Dot() so that it uses a hardcoded and know light-vector:
dot(Input.Nor.xyz,float3(1,1,1))If that looks good you know that the problem is you LightVector. If not you should check the normals.
For them you can simply do "return float4(Input.Nor.xyz, 1);" as first line in your shader. x is then red, y is green, z is blue. If that looks somewhat right it's okay (Whole mesh has to be colorful :) ).

PS: I noticed you added that Clamp(). Just a little hint: In HLSL "saturate(x)" is the same as "clamp(x, 0, 1)".

Share this post


Link to post
Share on other sites
Thanks for the tip about saturate, anyway I tried both testing the normal and light vector and it seems to be the normal that's wrong.When trying [color=#1C2837][size=2]"return float4(Input.Nor.xyz, 1); it was still white.

Share this post


Link to post
Share on other sites
Then the Normals of your mesh don't seem to be passed to the shader rightly. Doublecheck the inputs, does the mesh actually have normals?

Don't know how Rendermonky handles this stuff, though.

Share this post


Link to post
Share on other sites
Thanks for the help,, I think it was that I lacked an entry for the normal in the stream map. Will have to try tomorrow but that was probably the error.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!