Game Development Reference
In-Depth Information
void CPolygon::DrawFlat(CCanvas &buf){
//This function will draw a triangle to the off screen buffer
//class CCanvas
//Only 24 bit supported a the moment
if (buf.GetBitDepth()!=24) return;
//The polygon is facing away from camera
if (!Facing()) return;
int count,min,max,mid,i;
int x[c],y[c];
double m1, d1, m2, d2, q, u, v;
count=1;
//Set the values for red, green and blue directly. No shading
red=col.red; green=col.green; blue=col.blue;
while(count<(numverts-1)){
x[0]=pts[0].sx; x[1]=pts[count].sx; x[2]=pts[count+1].sx;
y[0]=pts[0].sy; y[1]=pts[count].sy; y[2]=pts[count+1].sy;
//Sort points by height
max=(y[0]<y[1])?1:0;
max=(y[max]<y[2])?2:max;
min=(y[0]<y[1])?0:1;
min=(y[min]<y[2])?min:2;
mid=3-(max+min);
//x distance
q=(double)(x[max]-x[min]);
//Avoid division by zero
q=(q)?q:EPSILON;
m2=(double)(y[max]-y[min])/q;
d2=y[max]-m2*x[max];
//Now we know the highest. middle and lowesty positions
//Draw horizontal lines from highest to middle position
if (y[max]!=y[min]){
q=(double)(x[mid]-x[max]);
q=(q)?q:EPSILON;
m1=(double)(y[mid]-y[max])/q;
d1=(double)y[mid]-m1*x[mid];
for (i=y[max];i>y[mid];i-){
u=((double)i-d1)/m1; v=((double)i-d2)/m2;
HorzLine(buf, (int)u, (int)v, i);
}
}
//Reached the mid point