rendered paste bodyprocedure TriangleFLATZ(const theX1,theY1,theZ1,theX2,theY2,theZ2,theX3,theY3,theZ3:single;color:dword;where:gfxImage);
var
X1,X2,X3,Y1,Y2,Y3,Z1,Z2,Z3:longint;
DX12,DX23,DX31,DY12,DY23,DY31:longint;
FDX12,FDX23,FDX31,FDY12,FDY23,FDY31:longint;
minx,maxx,miny,maxy:longint;
C1,C2,C3:longint;
CX1,CX2,CX3,CY1,CY2,CY3:longint;
x,y:longint;
a,b,c,D:longint;
Z:longint;
initialZ:Longint;
dx,dy:Longint;
tempZ:Longint;
begin
// 28.4 fixed-point coordinates
Y1:= round(16.0 * theY1);
Y2:= round(16.0 * theY2);
Y3:= round(16.0 * theY3);
X1:= round(16.0 * theX1);
X2:= round(16.0 * theX2);
X3:= round(16.0 * theX3);
Z1:= round(16.0 * theZ1);
Z2:= round(16.0 * theZ2);
Z3:= round(16.0 * theZ3);
// the plane equation for this triangle: ax + by + cz = d
a:= ((y2 - y1)*(z3 - z1) shr 4) - ((y3 - y1)*(z2 - z1) shr 4);
b:= ((x2 - x1)*(z3 - z1) shr 4) - ((x3 - x1)*(z2 - z1) shr 4);
c:= ((x2 - x1)*(y3 - y1) shr 4) - ((x3 - x1)*(y2 - y1) shr 4);
D:= (-x1*a) shr 4 + (y1*b) shr 4 - (z1*c) shr 4;
// Deltas
DX12:= X1 - X2;
DX23:= X2 - X3;
DX31:= X3 - X1;
DY12:= Y1 - Y2;
DY23:= Y2 - Y3;
DY31:= Y3 - Y1;
// Fixed-point deltas
FDX12:= DX12 shl 4;
FDX23:= DX23 shl 4;
FDX31:= DX31 shl 4;
FDY12:= DY12 shl 4;
FDY23:= DY23 shl 4;
FDY31:= DY31 shl 4;
// Bounding rectangle
minx:= round(min(x1,min(x2, x3))) shr 4;
maxx:= round(max(x1,max(x2, x3))) shr 4;
miny:= round(min(y1,min(y2, y3))) shr 4;
maxy:= round(max(y1,max(y2, y3))) shr 4;
// Half-edge constants
C1:= DY12 * X1 - DX12 * Y1;
C2:= DY23 * X2 - DX23 * Y2;
C3:= DY31 * X3 - DX31 * Y3;
// Correct for fill convention
if (DY12 < 0) or ((DY12 = 0) and (DX12 > 0)) then inc(C1);
if (DY23 < 0) or ((DY23 = 0) and (DX23 > 0)) then inc(C2);
if (DY31 < 0) or ((DY31 = 0) and (DX31 > 0)) then inc(C3);
CY1:= C1 + DX12 * (miny shl 4) - DY12 * (minx shl 4);
CY2:= C2 + DX23 * (miny shl 4) - DY23 * (minx shl 4);
CY3:= C3 + DX31 * (miny shl 4) - DY31 * (minx shl 4);
if (c shr 4) = 0 then exit;
initialZ:= (((A * -(minx)) shr 4 + ((B * -(miny)) shr 4 -D)) div (C shr 4));
dx:= ((-A) div (C shr 4));
dy:= ((-B) div (C shr 4));
for y:= miny to maxy do begin
CX1:=CY1;
CX2:=CY2;
CX3:=CY3;
Z:=initialZ;
for x:=minx to maxx do begin
if (CX1 > 0) and (CX2 > 0) and (CX3 > 0) then begin
Z:=Z+dx;
tempZ:=z-(zAddValue shl 4);
if tempZ > getL(zBuffer+(y shl shiftconst + x) shl 2) then begin
PutPixel32(x, y, color, where);
putL(zBuffer+(y shl shiftconst + x) shl 2, tempZ)
end;
end;
CX1:=CX1-FDY12;
CX2:=CX2-FDY23;
CX3:=CX3-FDY31;
end;
initialZ:=initialZ+dy;
CY1:=CY1+FDX12;
CY2:=CY2+FDX23;
CY3:=CY3+FDX31;
end;
end;