Sign in to follow this  
giugio

shader and camera

Recommended Posts

giugio    246
[code]
float4x4 Matrix;
float4 NFactor;
float4 Ambient;
float4 MatDiff;

float4 LightDiff1;
float4 LightDir1;

float4 LightDiff2;
float4 LightDir2;

struct VS_INPUT
{
float4 Pos : POSITION;
float4 Normal : NORMAL;
float2 Tex0 : TEXCOORD0;
};

struct VS_OUTPUT
{
float4 Pos : POSITION;
float4 Color : COLOR;
float2 Tex0 : TEXCOORD0;
};

////////////////////////////////////////

VS_OUTPUT VShade(VS_INPUT In)
{
VS_OUTPUT Out = (VS_OUTPUT) 0;

Out.Pos = mul(Matrix,In.Pos);
Out.Tex0 = In.Tex0;
float4 Normal = In.Normal*NFactor; // N or -N

// directional light 1

float4 Color1 = max(0,dot(Normal,-LightDir1)) *MatDiff*LightDiff1;

// directional light 2

float4 Color2 = max(0,dot(Normal,-LightDir2)) *MatDiff*LightDiff2;

// final color

Out.Color = Color1+Color2+Ambient;
Out.Color.a = MatDiff.a;

return Out;
}


[/code]

The shader is a front/back materials HLSL shader that i have converted to opengl cg , from:

[url="http://www.fairyengine.com/articles/hlsl2sided.htm#sample"]http://www.fairyengi...ided.htm#sample[/url]

the problem is that if i move the camera position ,the geometry is deformed in wrong manner, instead if i don't change camera position the geometry is correct.
How i can create a shader that support camera position changes?

Share this post


Link to post
Share on other sites
Juliean    7077
Did you make sure the worldviewprocection-matrix you supply to the shader is correct? I ran into a similar problem because I forget to supply the correct world matrix (you need to do it for every mesh/set of primitives).

Share this post


Link to post
Share on other sites
giugio    246
[quote name='The King2' timestamp='1302520165' post='4797047']
Did you make sure the worldviewprocection-matrix you supply to the shader is correct? I ran into a similar problem because I forget to supply the correct world matrix (you need to do it for every mesh/set of primitives).
[/quote]

may be that the normals must to be transformed for a matrix(WordMatrix)?

Share this post


Link to post
Share on other sites
Burnt_Fyr    1665
[quote name='giugio' timestamp='1302524514' post='4797078']
[quote name='The King2' timestamp='1302520165' post='4797047']
Did you make sure the worldviewprocection-matrix you supply to the shader is correct? I ran into a similar problem because I forget to supply the correct world matrix (you need to do it for every mesh/set of primitives).
[/quote]

may be that the normals must to be transformed for a matrix(WordMatrix)?
[/quote]

I vote for this. Your normals are local to object space, and may work if your camera is aligned with world frame, but if the model or camera is moved your normals do not represent this. Multiply the vertex normal by the world transform to bring it world space, or multiply your camera position by the inverse world matrix to bring the camera into object space. Either method will work(though there are pro's and con's to each) as long as position, normal, camera, and light are all in the same space.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this