Sign in to follow this  
staticVoid2

clipping help

Recommended Posts

staticVoid2    381
I've recently had to adjust my renderer to support polygons with x amount of vertices but the problem is when I try and clip a polygon (using the sutherland hodgman algorithm) with more than three vertices the textures start to dance. I have a polygon structure that holds an array of vertices and the number of vertices as an unsigned int, I basically pass two pointers to two of these structures into the function and it output's the result to the second structure,
int clip(const Polygon *p1, Polygon *p2, const Plane &plane)
{
   Vertex P = p1->vertices[p1->nVertices-1]; // previous point
   Vertex C;                               // current point
   int tracker = 0;

   for(int a = 0; a < p1->nVertices; a++) {
      C = p1->vertices[a];
         if(dotProduct(C, plane.normal) > plane.dist) { // current point inside
            if(dotProduct(P, plane.normal) > plane.dist) { // both points inside
               p2->vertices[tracker++] = C;
            } else { // current point inside but previous not
               linePlaneIntersection(P, C, plane, p2->vertices[tracker++]);
               p2->vertices[tracker++] = C;
            }
         } else
         if(dotProduct(P, plane.normal) > plane.dist) { // previous point inside but current not
            linePlaneIntersection(P, C, plane, p2->vertices[tracker++]);
         }
      P = C;
   }

   p2->nVertices = tracker;

   return tracker;
}

to draw the polygon I use:

void drawPolygon(Polygon p) {

   Vertex vBuff[3];

   vBuff[0] = p.vertices[0];

   for(int a = 1; a < p.nVertices-1; a++) {
      vBuff[1] = p.vertices[a];
      vBuff[2] = p.vertices[a+1];
      drawTriangle(vBuff);
   }
}


I have a .x file that has 1 face with 4 vertex indices(0, 1, 2, 3), when I use this the texture seems to warp, but if i change it to 2 faces with indices: (0, 1, 2) and (0, 2, 3) it works perfectly. Here's two screen shots: not clipped: Not Clipped clipped: Clipped thanx in advance.

Share this post


Link to post
Share on other sites
staticVoid2    381
I'm sure it's somthing to do with the drawing order, would it matter what vertex you start at? eg.

polygon ABCDEF

would

ABC
ACD
ADE
AEF

be any different from

BCD
BDE
BEF
BFA

???

[Edited by - staticVoid2 on March 23, 2008 1:51:31 PM]

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