PDA

View Full Version : Polygon Facing



legomanww
11-01-2004, 07:03 AM
I am writing a little shader plugin, and I need to determine if the spot it is shading is facing the camera, or away. I need to be able to render both sides of the polygons, so I figured I could just turn on double sided, and then determine the direction the polygon is facing, then I can shade it appropriately.

I cannot figure out how I would determine wether or not the polygon is facing the camera or not.

I've got an idea that I would use the wNorm and the camera position coordinates it gives to determine if it's facing, but I don't know where to go from there.

thanks,
William

Lightwolf
11-01-2004, 07:06 AM
Hi,

just a quick answer (I'm on the run)... the dot roduct is defined as the angle between to vectors. That should get you further.

Cheers,
Mike

legomanww
11-01-2004, 08:15 PM
thanks,

I think I've got it, I looked up the dot product again in my calc book, I think I got it now, played around with it when I was bored at school, so it makes sense now.

legomanww
11-02-2004, 07:03 AM
Well, I tried out the dot product thing, I don't know for sure that I got it right, but what I got didn't work.

I realized that the dot produt is limited to 180 degrees, so if you come from behind, I still don't see how I can determine if it's hitting it from behind or in front.

Thanks,
William

Lightwolf
11-02-2004, 07:14 AM
I realized that the dot produt is limited to 180 degrees, so if you come from behind, I still don't see how I can determine if it's hitting it from behind or in front.

If you're coming from behind, then the angle > 90 . You do have to be careful with the direction of your vectors prior to the dot product as well.

Cheers,
Mike

legomanww
11-02-2004, 07:22 AM
Alright, I think I got confused with the angles, think I've got it now. When the dot product results in less than 0 it is more than 90 degrees right?

How do I determine the vector for the incoming ray though? Is it (wPos - raySource) , if that was normalized to be a length of 1, would that be the incoming vector?

Lightwolf
11-02-2004, 07:24 AM
How do I determine the vector for the incoming ray though? Is it (wPos - raySource) , if that was normalized to be a length of 1, would that be the incoming vector?
Yes. You wouldn't even have to normalize it. If you do, you get the incoming _direction_ vector.

Cheers,
Mike

legomanww
11-02-2004, 07:29 AM
Alright, Thanks.

I'll work on it some more when I get home.

Does this look correct so far?



N[0] = ( float ) sa->wNorm0[ 0 ];
N[1] = ( float ) sa->wNorm0[ 1 ];
N[2] = ( float ) sa->wNorm0[ 2 ];

CVec[0] = ( float ) ((sa->wPos[0] - sa->raySource[0]));
CVec[1] = ( float ) ((sa->wPos[0] - sa->raySource[0]));
CVec[1] = ( float ) ((sa->wPos[0] - sa->raySource[0]));

//normalize(N);
//normalize(CVec);

PolygonFacing = dot(N, CVec) / ((mag(N) * mag(CVec)));

if ( PolygonFacing >= 0 )
{
sa->color[ 0 ]= -PolygonFacing;
sa->color[ 1 ]= -PolygonFacing;
sa->color[ 2 ]= -PolygonFacing;

}
else
{

sa->color[ 0 ]= 0;
sa->color[ 1 ]= 1;
sa->color[ 2 ]= 0;
}

sa->luminous = 1;

Lightwolf
11-02-2004, 07:38 AM
Does this look correct so far?
Almost, except for a couple of typos I guess:


// These could be replaced by the macros from lwmath.h
N[0] = ( float ) sa->wNorm0[ 0 ];
N[1] = ( float ) sa->wNorm0[ 1 ];
N[2] = ( float ) sa->wNorm0[ 2 ];

CVec[0] = ( float ) ((sa->wPos[0] - sa->raySource[0]));
CVec[1] = ( float ) ((sa->wPos[1] - sa->raySource[1]));
CVec[2] = ( float ) ((sa->wPos[2] - sa->raySource[2]));

// both normalizes are probably optional, but better safe than sorry.
normalize(N);
normalize(CVec);

PolygonFacing = dot(N, CVec); // returns an agle in radians

if ( PolygonFacing > PI )
{
// faces away from raysource
sa->color[ 0 ]= -PolygonFacing;
sa->color[ 1 ]= -PolygonFacing;
sa->color[ 2 ]= -PolygonFacing;
// or VSET(sa->color, -PolygonFacing);
}
else
{
// faces toward raysource
sa->color[ 0 ]= 0.0f; // don't let the compiler complain ;)
sa->color[ 1 ]= 1.0f;
sa->color[ 2 ]= 0.0f;
}
sa->luminous = 1.0f;

I'd also let those positions be doubles, a bit of extra accuracy is always good here.

Cheers,
Mike

legomanww
11-02-2004, 05:37 PM
Haha, wow, I just saw the typos with the vectors, being all [0]. Guess I forgot to change them, and that would explain the results I got.

Thanks a lot Mike, I really appreciate it.

Lightwolf
11-03-2004, 01:02 AM
Haha, wow, I just saw the typos with the vectors, being all [0]. Guess I forgot to change them, and that would explain the results I got.

Thanks a lot Mike, I really appreciate it.
You're welcome.

You know, it seems that 90% of all bugs produced end up being stuff like that, you stare at it for hours, and the next day you go "duh, how stupid" :)

Cheers,
Mike

legomanww
11-06-2004, 10:02 AM
Success.

I belive that I got it working correctly, I will post some pictures of the result later today.

legomanww
11-06-2004, 06:04 PM
http://s87839584.onlinehome.us/glass2.jpg

http://s87839584.onlinehome.us/glass3.jpg

Lightwolf
11-08-2004, 03:00 AM
Wow, very neat. Congrats on getting it to work!

Cheers,
Mike