Sign in to follow this  
Nameles

How to calculate correct normal for two-sided poly?

Recommended Posts

Nameles    122
Anyone knows how to do this? I found a sample app that does it on the NVidia site but the shader is all in assembly which I don’t really under stand. There is also no documentation with the sample so it’s not very useful to me. Can anyone explain what needs to be done to flip the normal for correct lighting?

Share this post


Link to post
Share on other sites
Nameles    122
Quote:
Original post by Promit
If it's a backfacing poly, then can't you just negate the normal and be done with it?


Is it really that simple?
That sample looked to be doing a lot more.
It looked more like a reflection of the normal but I wasn't sure do to the assembly language shader code.

I was looking at the Two-Sided Polygons sample.

Share this post


Link to post
Share on other sites
Nameles    122
Maybe if I negate both the light and the eye vectors

float3 N = mul(IN.normal, worldInverseTranspose); // normal
float3 E = normalize(worldEyePos - worldVertPos); // eye
float3 L = normalize(-lightDir.xyz); // light

if(0 > dot(N, L))
{
L = -L;
E = -E;
}

float3 H = normalize(E + L); // half angle



Would that work?

Share this post


Link to post
Share on other sites
Promit    13246
I believe something like this should work in HLSL:

float ndotv = dot( IN.normal, vertexToView );
float side = sign( ndotv );
IN.normal *= side;


Share this post


Link to post
Share on other sites
EvilDecl81    360
Quote:
Original post by paic
Wow, that's pretty clever. I wouldn't have think of that by myself althouhg it's so simple ^^


The VFace semantic is provided to tell you the side of the face for shader model 3.0. This is the correct way to determine the polygonside:

float4 main(...,float3 Normal : NORMAL, float S : VFace)
{
if(S < 0)
Normal = -Normal;

...

}

Unfortantly, due to hardware weirdness the only thing you know about VFace is the sign

Share this post


Link to post
Share on other sites
Nameles    122
Ok, seems that the solution Promit provided is the one I'll use.
Id love to use the vs_3_0 VFace but I don’t have that luxury.

I had the right idea but not the correct vector. It's obvious now that I should have used the vertex to eye vector and not the light vector for the dot... what was I thinking [rolleyes]

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