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:
clipped:
thanx in advance.