Results 1 to 14 of 14

Thread: Polygon Facing

  1. #1
    Newbie Member legomanww's Avatar
    Join Date
    Feb 2003
    Location
    Arizona, USA
    Posts
    78

    Polygon Facing

    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
    Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.
    -Cory Doctorow

  2. #2
    obfuscated SDK hacker Lightwolf's Avatar
    Join Date
    Feb 2003
    Location
    Stuttgart, Germany
    Posts
    13,613
    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

  3. #3
    Newbie Member legomanww's Avatar
    Join Date
    Feb 2003
    Location
    Arizona, USA
    Posts
    78
    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.
    Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.
    -Cory Doctorow

  4. #4
    Newbie Member legomanww's Avatar
    Join Date
    Feb 2003
    Location
    Arizona, USA
    Posts
    78
    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
    Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.
    -Cory Doctorow

  5. #5
    obfuscated SDK hacker Lightwolf's Avatar
    Join Date
    Feb 2003
    Location
    Stuttgart, Germany
    Posts
    13,613
    Quote Originally Posted by legomanww
    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

  6. #6
    Newbie Member legomanww's Avatar
    Join Date
    Feb 2003
    Location
    Arizona, USA
    Posts
    78
    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?
    Last edited by legomanww; 11-02-2004 at 07:26 AM.
    Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.
    -Cory Doctorow

  7. #7
    obfuscated SDK hacker Lightwolf's Avatar
    Join Date
    Feb 2003
    Location
    Stuttgart, Germany
    Posts
    13,613
    Quote Originally Posted by legomanww
    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

  8. #8
    Newbie Member legomanww's Avatar
    Join Date
    Feb 2003
    Location
    Arizona, USA
    Posts
    78
    Alright, Thanks.

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

    Does this look correct so far?

    Code:
        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;
    Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.
    -Cory Doctorow

  9. #9
    obfuscated SDK hacker Lightwolf's Avatar
    Join Date
    Feb 2003
    Location
    Stuttgart, Germany
    Posts
    13,613
    Quote Originally Posted by legomanww
    Does this look correct so far?
    Almost, except for a couple of typos I guess:
    Code:
    // 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
    Last edited by Lightwolf; 11-02-2004 at 07:40 AM.

  10. #10
    Newbie Member legomanww's Avatar
    Join Date
    Feb 2003
    Location
    Arizona, USA
    Posts
    78
    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.
    Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.
    -Cory Doctorow

  11. #11
    obfuscated SDK hacker Lightwolf's Avatar
    Join Date
    Feb 2003
    Location
    Stuttgart, Germany
    Posts
    13,613
    Quote Originally Posted by legomanww
    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

  12. #12
    Newbie Member legomanww's Avatar
    Join Date
    Feb 2003
    Location
    Arizona, USA
    Posts
    78
    Success.

    I belive that I got it working correctly, I will post some pictures of the result later today.
    Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.
    -Cory Doctorow

  13. #13
    Newbie Member legomanww's Avatar
    Join Date
    Feb 2003
    Location
    Arizona, USA
    Posts
    78


    Last edited by legomanww; 11-06-2004 at 06:53 PM.
    Engineers are all basically high-functioning autistics who have no idea how normal people do stuff.
    -Cory Doctorow

  14. #14
    obfuscated SDK hacker Lightwolf's Avatar
    Join Date
    Feb 2003
    Location
    Stuttgart, Germany
    Posts
    13,613
    Wow, very neat. Congrats on getting it to work!

    Cheers,
    Mike

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •