Jump to content
  • Advertisement
Sign in to follow this  
giugio

shader and camera

This topic is 2599 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


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;
}




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

http://www.fairyengi...ided.htm#sample

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

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


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

Share this post


Link to post
Share on other sites

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


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
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!