home *** CD-ROM | disk | FTP | other *** search
/ The Equalizer BBS / equalizer-bbs-collection_2004.zip / equalizer-bbs-collection / DEMOSCENE-STUFF / PHRO.ZIP / POLYGONS.PAS < prev    next >
Pascal/Delphi Source File  |  1995-12-30  |  37KB  |  1,210 lines

  1. {   Polygon Fills Source file                  }
  2. {   PHRO!                                      }
  3. {   Phred/OTM                                  }
  4. {   achalfin@uceng.uc.edu                      }
  5. {   DO NOT DISTRIBUTE THIS SOURCE FILE         }
  6. Unit Polygons;
  7. {$G+}
  8.  
  9. Interface
  10.  
  11. Const
  12.   MinX = 0;
  13.   MinY = 0;
  14.   MaxX = 319;
  15.   MaxY = 199;
  16.  
  17. Procedure GouraudClipPolygon(x1, y1, x2, y2, x3, y3, C1, C2, C3, PgSeg : Integer);
  18. Procedure PhongClipPolygon(x1, y1, x2, y2, x3, y3, t1, p1, t2, p2, t3, p3, PgSeg : Integer; EMap : Pointer);
  19.  
  20. Implementation
  21.  
  22. Var
  23.   EnvMap : Pointer;
  24.  
  25.  
  26. Procedure GouraudPoly(X1, Y1, X2, Y2, X3, Y3, C1, C2, C3, PgSeg : Integer);
  27.  
  28. Var
  29.   xTop, yTop : Integer;
  30.   xBot, yBot : Integer;
  31.   xMid, yMid : Integer;
  32.   cTop, cBot, cMid : Integer;
  33.   Count : Integer;
  34.   xVal1, xStep1 : Integer;
  35.   xVal2, xStep2 : Integer;
  36.   cVal1, cStep1 : Integer;
  37.   DColor : Integer;
  38.   crMid : Integer;
  39.   xrMid : Integer;
  40.   hCount, cVal : Integer;
  41.  
  42. Begin
  43.   Asm
  44.     Mov  ax,Y1
  45.     Mov  bx,X1
  46.     Mov  cx,Y2
  47.     Mov  dx,X2
  48.     Mov  si,C1
  49.     Mov  di,C2
  50.  
  51.     Mov  xTop,bx
  52.     Mov  yTop,ax
  53.     Mov  xBot,dx
  54.     Mov  yBot,cx
  55.     Mov  cTop,si
  56.     Mov  cBot,di
  57.     Cmp  ax,cx
  58.     Jle @Skip2Top
  59.       Mov  xTop,dx
  60.       Mov  yTop,cx
  61.       Mov  xBot,bx
  62.       Mov  yBot,ax
  63.       Mov  cTop,di
  64.       Mov  cBot,si
  65.    @Skip2Top:
  66.     Mov  ax,Y3
  67.     Mov  bx,X3
  68.     Mov  cx,C3
  69.     Cmp  ax,yTop
  70.     Jge @CheckY3Bottom
  71.       Mov  dx,yTop
  72.       Mov  yMid,dx
  73.       Mov  dx,xTop
  74.       Mov  xMid,dx
  75.       Mov  dx,cTop
  76.       Mov  cMid,dx
  77.       Mov  yTop,ax
  78.       Mov  xTop,bx
  79.       Mov  cTop,cx
  80.       Jmp @Fini
  81.    @CheckY3Bottom:
  82.       Cmp ax,yBot
  83.       Jle @Y3Mid
  84.         Mov  dx,yBot
  85.         Mov  yMid,dx
  86.         Mov  dx,xBot
  87.         Mov  xMid,dx
  88.         Mov  dx,cBot
  89.         Mov  cMid,dx
  90.         Mov  yBot,ax
  91.         Mov  xBot,bx
  92.         Mov  cBot,cx
  93.         Jmp @Fini
  94.      @Y3Mid:
  95.         Mov  yMid,ax
  96.         Mov  xMid,bx
  97.         Mov  cMid,cx
  98.    @Fini:
  99.   End;
  100.  
  101.   If (yBot-yTop) = 0
  102.     Then Begin
  103.       xrMid := xMid;
  104.       crMid := cMid;
  105.     End
  106.     Else Begin
  107.       xrMid := (yMid-yTop)*(xBot-xTop) Div (yBot-yTop) + xTop;
  108.       crMid := (yMid-yTop)*(cBot-cTop) Div (yBot-yTop) + cTop;
  109.     End;
  110.  
  111.   Asm
  112.     Mov  es,PgSeg
  113.   End;
  114.  
  115.   If xMid < XrMid
  116.     Then Begin
  117.       DColor := (crMid-cMid) Shl 8 Div (xrMid-xMid+1);
  118.  
  119.       cVal1 := cTop Shl 8;
  120.       xVal1 := xTop Shl 6;
  121.       xVal2 := xVal1;
  122.       xStep1 := (xMid-xTop) Shl 6 Div (yMid-yTop+1);
  123.       cStep1 := (cMid-cTop) Shl 8 Div (yMid-yTop+1);
  124.       xStep2 := (xBot-xTop) Shl 6 Div (yBot-yTop+1);
  125.       For Count := yTop to yMid do
  126.         Begin
  127.           Asm
  128.             Mov  bx,Count
  129.             Mov  dx,bx
  130.             Shl  bx,6
  131.             Add  bh,dl
  132.             Mov  di,bx
  133.  
  134.             Mov  ax,Word Ptr [xVal1]
  135.             Shr  ax,6
  136.             Mov  cx,Word Ptr [xVal2]
  137.             Shr  cx,6
  138.  
  139.             Add  di,ax
  140.  
  141.             Sub  cx,ax
  142.             Inc  cx
  143.             Js   @Exit
  144.             Mov  bx,dColor
  145.             Mov  ax,cVal1
  146.             Shr  cx,1
  147.             Jnc @SkipSingle
  148.             Mov  es:[di],ah
  149.             Add  ax,bx
  150.             Inc  di
  151.            @SkipSingle:
  152.             Jcxz @Exit
  153.            @Looper:
  154.             Mov  dl,ah
  155.             Add  ax,bx
  156.             Mov  dh,ah
  157.             Add  ax,bx
  158.             Mov  es:[di],dx
  159.             Add  di,2
  160.             Dec  cx
  161.             Jnz  @Looper
  162.            @Exit:
  163.           End;
  164.           Inc(cVal1, cStep1);
  165.           Inc(xVal1, xStep1);
  166.           Inc(xVal2, xStep2);
  167.         End;
  168.  
  169.       xVal2 := xVal2 - xStep2;
  170.  
  171.       cVal1 := cMid Shl 8;
  172.       xVal1 := xMid Shl 6;
  173.       xStep1 := (xBot-xMid) Shl 6 Div (yBot-yMid+1);
  174.       cStep1 := (cBot-cMid) Shl 8 Div (yBot-yMid+1);
  175.       For Count := yMid to yBot do
  176.         Begin
  177.           Asm
  178.             Mov  bx,Count
  179.             Mov  dx,bx
  180.             Shl  bx,6
  181.             Add  bh,dl
  182.             Mov  di,bx
  183.  
  184.             Mov  ax,Word Ptr [xVal1]
  185.             Shr  ax,6
  186.             Mov  cx,Word Ptr [xVal2]
  187.             Shr  cx,6
  188.             Add  di,ax
  189.  
  190.             Sub  cx,ax
  191.             Inc  cx
  192.             Js   @Exit
  193.             Jcxz @Exit
  194.             Mov  bx,dColor
  195.             Mov  ax,cVal1
  196.             Shr  cx,1
  197.             Jnc @SkipSingle
  198.             Mov  es:[di],ah
  199.             Add  ax,bx
  200.             Inc  di
  201.            @SkipSingle:
  202.             Jcxz @Exit
  203.            @Looper:
  204.             Mov  dl,ah
  205.             Add  ax,bx
  206.             Mov  dh,ah
  207.             Add  ax,bx
  208.             Mov  es:[di],dx
  209.             Add  di,2
  210.             Dec  cx
  211.             Jnz  @Looper
  212.            @Exit:
  213.           End;
  214.           Inc(cVal1, cStep1);
  215.           Inc(xVal1, xStep1);
  216.           Inc(xVal2, xStep2);
  217.         End;
  218.     End
  219.     Else Begin
  220.       DColor := (cMid-crMid) Shl 8 Div (xMid-xrMid+1);
  221.  
  222.       cVal1 := cTop Shl 8;
  223.       xVal1 := (xTop) Shl 6;
  224.       xVal2 := xVal1;
  225.       xStep1 := (xBot-xTop) Shl 6 Div (yBot-yTop+1);
  226.       cStep1 := (cBot-cTop) Shl 8 Div (yBot-yTop+1);
  227.       xStep2 := (xMid-xTop) Shl 6 Div (yMid-yTop+1);
  228.  
  229.       For Count := yTop to yMid do
  230.         Begin
  231.           Asm
  232.             Mov  bx,Count
  233.             Mov  dx,bx
  234.             Shl  bx,6
  235.             Add  bh,dl
  236.             Mov  di,bx
  237.  
  238.             Mov  ax,Word Ptr [xVal1]
  239.             Shr  ax,6
  240.             Mov  cx,Word Ptr [xVal2]
  241.             Shr  cx,6
  242.             Add  di,ax
  243.  
  244.             Sub  cx,ax
  245.             Inc  cx
  246.             Js   @Exit
  247.             Jcxz @Exit
  248.             Mov  bx,dColor
  249.             Mov  ax,cVal1
  250.             Shr  cx,1
  251.             Jnc @SkipSingle
  252.             Mov  es:[di],ah
  253.             Add  ax,bx
  254.             Inc  di
  255.            @SkipSingle:
  256.             Jcxz @Exit
  257.            @Looper:
  258.             Mov  dl,ah
  259.             Add  ax,bx
  260.             Mov  dh,ah
  261.             Add  ax,bx
  262.             Mov  es:[di],dx
  263.             Add  di,2
  264.             Dec  cx
  265.             Jnz  @Looper
  266.            @Exit:
  267.           End;
  268.           Inc(cVal1, cStep1);
  269.           Inc(xVal1, xStep1);
  270.           Inc(xVal2, xStep2);
  271.         End;
  272.  
  273.       xVal1 := xVal1 - xStep1;
  274.       cVal1 := cVal1 - cStep1;
  275.       xVal2 := (xMid) Shl 6;
  276.       xStep2 := (xBot-xMid) Shl 6 Div (yBot-yMid+1);
  277.       For Count := yMid to yBot do
  278.         Begin
  279.           Asm
  280.             Mov  bx,Count
  281.             Mov  dx,bx
  282.             Shl  bx,6
  283.             Add  bh,dl
  284.             Mov  di,bx
  285.  
  286.             Mov  ax,Word Ptr [xVal1]
  287.             Shr  ax,6
  288.             Mov  cx,Word Ptr [xVal2]
  289.             Shr  cx,6
  290.  
  291.             Add  di,ax
  292.  
  293.             Sub  cx,ax
  294.             Inc  cx
  295.             Js   @Exit
  296.             Jcxz @Exit
  297.             Mov  bx,dColor
  298.             Mov  ax,cVal1
  299.             Shr  cx,1
  300.             Jnc @SkipSingle
  301.             Mov  es:[di],ah
  302.             Add  ax,bx
  303.             Inc  di
  304.            @SkipSingle:
  305.             Jcxz @Exit
  306.            @Looper:
  307.             Mov  dl,ah
  308.             Add  ax,bx
  309.             Mov  dh,ah
  310.             Add  ax,bx
  311.             Mov  es:[di],dx
  312.             Add  di,2
  313.             Dec  cx
  314.             Jnz  @Looper
  315.            @Exit:
  316.           End;
  317.           Inc(cVal1, cStep1);
  318.           Inc(xVal1, xStep1);
  319.           Inc(xVal2, xStep2);
  320.         End;
  321.     End;
  322.  
  323. End;
  324.  
  325. Procedure GouraudClipPolygon(x1, y1, x2, y2, x3, y3, C1, C2, C3, PgSeg : Integer);
  326.  
  327. Type
  328.   tFlatClip = Record
  329.     x, y, x1, y1 : Integer;
  330.     c, c1 : Integer;
  331.   End;
  332.  
  333. Var
  334.   FlatList : Array[0..7] of tFlatClip;
  335.   NumVert : Integer;
  336.   Count : Integer;
  337.   ClipVert : Integer;
  338.   V1, V2 : Integer;
  339.  
  340. Begin
  341.   If (((X1 >= MinX) and (X1 <= MaxX)) and ((Y1 >= MinY) and (Y1 <= MaxY)) and
  342.       ((X2 >= MinX) and (X2 <= MaxX)) and ((Y2 >= MinY) and (Y2 <= MaxY))) and
  343.       ((X3 >= MinX) and (X3 <= MaxX)) and ((Y3 >= MinY) and (Y3 <= MaxY))
  344.       Then Begin
  345.         GouraudPoly(X1, Y1, X2, Y2, X3, Y3, C1, C2, C3, PgSeg);
  346.         Exit;
  347.       End;
  348.   FlatList[0].x := X1;
  349.   FlatList[0].y := Y1;
  350.   FlatList[0].c := C1;
  351.   FlatList[1].x := X2;
  352.   FlatList[1].y := Y2;
  353.   FlatList[1].c := C2;
  354.   FlatList[2].x := X3;
  355.   FlatList[2].y := Y3;
  356.   FlatList[2].c := C3;
  357.   NumVert := 3;
  358.   ClipVert := 0;
  359.   { Clip against left side }
  360.   V1 := NumVert - 1;
  361.   For Count := 0 to (NumVert-1) do
  362.     Begin
  363.       If (FlatList[V1].x >= MinX) and (FlatList[Count].x >= MinX)
  364.         Then Begin
  365.           FlatList[ClipVert].x1 := FlatList[Count].x;
  366.           FlatList[ClipVert].y1 := FlatList[Count].y;
  367.           FlatList[ClipVert].c1 := FlatList[Count].c;
  368.           ClipVert := ClipVert + 1;
  369.         End;
  370.       If (FlatList[V1].x >= MinX) and (FlatList[Count].x < MinX)
  371.         Then Begin
  372.           FlatList[ClipVert].x1 := MinX;
  373.           FlatList[ClipVert].y1 := (FlatList[Count].y-FlatList[V1].y)*(MinX-FlatList[v1].x)
  374.                                     Div (FlatList[Count].x-FlatList[V1].x) + FlatList[V1].y;
  375.           FlatList[ClipVert].c1 := (FlatList[Count].c-FlatList[V1].c)*(MinX-FlatList[v1].x)
  376.                                     Div (FlatList[Count].x-FlatList[V1].x) + FlatList[V1].c;
  377.           ClipVert := ClipVert + 1;
  378.         End;
  379.       If (FlatList[V1].x < MinX) and (FlatList[Count].x >= MinX)
  380.         Then Begin
  381.           FlatList[ClipVert].x1 := MinX;
  382.           FlatList[ClipVert].y1 := (FlatList[Count].y-FlatList[V1].y)*(MinX-FlatList[V1].x)
  383.                                     Div (FlatList[Count].x-FlatList[V1].x) + FlatList[v1].y;
  384.           FlatList[ClipVert].c1 := (FlatList[Count].c-FlatList[V1].c)*(MinX-FlatList[V1].x)
  385.                                     Div (FlatList[Count].x-FlatList[V1].x) + FlatList[v1].c;
  386.           ClipVert := ClipVert + 1;
  387.           FlatList[ClipVert].x1 := FlatList[Count].x;
  388.           FlatList[ClipVert].y1 := FlatList[Count].y;
  389.           FlatList[ClipVert].c1 := FlatList[Count].c;
  390.           ClipVert := ClipVert + 1;
  391.         End;
  392.       V1 := Count;
  393.     End;
  394.   NumVert := ClipVert;
  395.   ClipVert := 0;
  396.   { Clip against Right side }
  397.   V1 := NumVert - 1;
  398.   For Count := 0 to (NumVert-1) do
  399.     Begin
  400.       If (FlatList[v1].x1 <= MaxX) and (FlatList[Count].x1 <= MaxX)
  401.         Then Begin
  402.           FlatList[ClipVert].x := FlatList[Count].x1;
  403.           FlatList[ClipVert].y := FlatList[Count].y1;
  404.           FlatList[ClipVert].c := FlatList[Count].c1;
  405.           ClipVert := ClipVert + 1;
  406.         End;
  407.       If (FlatList[V1].x1 <= MaxX) and (FlatList[Count].x1 > MaxX)
  408.         Then Begin
  409.           FlatList[ClipVert].x := MaxX;
  410.           FlatList[ClipVert].y := (FlatList[Count].y1-FlatList[V1].y1)*(MaxX-FlatList[v1].x1)
  411.                                    Div (FlatList[Count].x1-FlatList[v1].x1) + FlatList[v1].y1;
  412.           FlatList[ClipVert].c := (FlatList[Count].c1-FlatList[V1].c1)*(MaxX-FlatList[v1].x1)
  413.                                    Div (FlatList[Count].x1-FlatList[v1].x1) + FlatList[v1].c1;
  414.           ClipVert := ClipVert + 1;
  415.         End;
  416.       If (FlatList[v1].x1 > MaxX) and (FlatList[Count].x1 <= MaxX)
  417.         Then Begin
  418.           FlatList[ClipVert].x := MaxX;
  419.           FlatList[ClipVert].y := (FlatList[Count].y1-FlatList[V1].y1)*(MaxX-FlatList[v1].x1)
  420.                                    Div (FlatList[Count].x1-FlatList[v1].x1) + FlatList[v1].y1;
  421.           FlatList[ClipVert].c := (FlatList[Count].c1-FlatList[V1].c1)*(MaxX-FlatList[v1].x1)
  422.                                    Div (FlatList[Count].x1-FlatList[v1].x1) + FlatList[v1].c1;
  423.           ClipVert := ClipVert + 1;
  424.           FlatList[ClipVert].x := FlatList[Count].x1;
  425.           FlatList[ClipVert].y := FlatList[Count].y1;
  426.           FlatList[ClipVert].c := FlatList[Count].c1;
  427.           ClipVert := ClipVert + 1;
  428.         End;
  429.       v1 := Count;
  430.     End;
  431.   NumVert := ClipVert;
  432.   ClipVert := 0;
  433.   { Clip against top edge }
  434.   V1 := NumVert - 1;
  435.   For Count := 0 to (NumVert-1) do
  436.     Begin
  437.       If (FlatList[V1].y >= MinY) and (FlatList[Count].y >= MinY)
  438.         Then Begin
  439.           FlatList[ClipVert].x1 := FlatList[Count].x;
  440.           FlatList[ClipVert].y1 := FlatList[Count].y;
  441.           FlatList[ClipVert].c1 := FlatList[Count].c;
  442.           ClipVert := ClipVert + 1;
  443.         End;
  444.       If (FlatList[V1].y >= MinY) and (FlatList[Count].y < MinY)
  445.         Then Begin
  446.           FlatList[ClipVert].x1 := (MinY-FlatList[V1].y)*(FlatList[Count].x-FlatList[V1].x)
  447.                                            Div (FlatList[Count].y-FlatList[V1].y) + FlatList[V1].x;
  448.           FlatList[ClipVert].c1 := (MinY-FlatList[V1].y)*(FlatList[Count].c-FlatList[V1].c)
  449.                                            Div (FlatList[Count].y-FlatList[V1].y) + FlatList[V1].c;
  450.           FlatList[ClipVert].y1 := MinY;
  451.           ClipVert := ClipVert + 1;
  452.         End;
  453.       If (FlatList[V1].y < MinY) and (FlatList[Count].y >= MinY)
  454.         Then Begin
  455.           FlatList[ClipVert].x1 := (MinY-FlatList[V1].y)*(FlatList[Count].x-FlatList[V1].x)
  456.                                     Div (FlatList[Count].y-FlatList[V1].y) + FlatList[V1].x;
  457.           FlatList[ClipVert].c1 := (MinY-FlatList[V1].y)*(FlatList[Count].c-FlatList[V1].c)
  458.                                     Div (FlatList[Count].y-FlatList[V1].y) + FlatList[V1].c;
  459.           FlatList[ClipVert].y1 := MinY;
  460.           ClipVert := ClipVert + 1;
  461.           FlatList[ClipVert].x1 := FlatList[Count].x;
  462.           FlatList[ClipVert].y1 := FlatList[Count].y;
  463.           FlatList[ClipVert].c1 := FlatList[Count].c;
  464.           ClipVert := ClipVert + 1;
  465.         End;
  466.       V1 := Count;
  467.     End;
  468.   NumVert := ClipVert;
  469.   ClipVert := 0;
  470.   V1 := NumVert - 1;
  471.   For Count := 0 to (NumVert-1) do
  472.     Begin
  473.       If (FlatList[V1].y1 <= MaxY) and (FlatList[Count].y1 <= MaxY)
  474.         Then Begin
  475.           FlatList[ClipVert].x := FlatList[Count].x1;
  476.           FlatList[ClipVert].y := FlatList[Count].y1;
  477.           FlatList[ClipVert].c := FlatList[Count].c1;
  478.           ClipVert := ClipVert + 1;
  479.         End;
  480.       If (FlatList[V1].y1 <= MaxY) and (FlatList[Count].y1 > MaxY)
  481.         Then Begin
  482.           FlatList[ClipVert].x := (MaxY-FlatList[V1].y1)*(FlatList[Count].x1-FlatList[V1].x1)
  483.                                     Div (FlatList[Count].y1-FlatList[v1].y1) + FlatList[V1].x1;
  484.           FlatList[ClipVert].c := (MaxY-FlatList[V1].y1)*(FlatList[Count].c1-FlatList[V1].c1)
  485.                                     Div (FlatList[Count].y1-FlatList[v1].y1) + FlatList[V1].c1;
  486.           FlatList[ClipVert].y := MaxY;
  487.           ClipVert := ClipVert + 1;
  488.         End;
  489.       If (FlatList[V1].y1 > MaxY) and (FlatList[Count].y1 <= MaxY)
  490.         Then Begin
  491.           FlatList[ClipVert].x := (MaxY-FlatList[V1].y1)*(FlatList[Count].x1-FlatList[V1].x1)
  492.                                     Div (FlatList[Count].y1-FlatList[v1].y1) + FlatList[V1].x1;
  493.           FlatList[ClipVert].c := (MaxY-FlatList[V1].y1)*(FlatList[Count].c1-FlatList[V1].c1)
  494.                                     Div (FlatList[Count].y1-FlatList[v1].y1) + FlatList[V1].c1;
  495.           FlatList[ClipVert].y := MaxY;
  496.           ClipVert := ClipVert + 1;
  497.           FlatList[ClipVert].x := FlatList[Count].x1;
  498.           FlatList[ClipVert].y := FlatList[Count].y1;
  499.           FlatList[ClipVert].c := FlatList[Count].c1;
  500.           ClipVert := ClipVert + 1;
  501.         End;
  502.       V1 := Count;
  503.     End;
  504.   NumVert := ClipVert;
  505.  
  506.   V1 := 1;
  507.   V2 := 2;
  508.  
  509.   { Now draw the polygons }
  510.   For Count := 0 to (NumVert - 3) do
  511.     Begin
  512.       GouraudPoly(FlatList[0].x, FlatList[0].y,
  513.                   FlatList[V1].x, FlatList[V1].y,
  514.                   FlatList[V2].x, FlatList[V2].y,
  515.                   FlatList[0].c, FlatList[V1].c,
  516.                   FlatList[V2].c, PgSeg);
  517.       V1 := V2;
  518.       V2 := V2 + 1;
  519.     End;
  520. End;
  521.  
  522.  
  523. Procedure PhongPoly(X1, Y1, X2, Y2, X3, Y3, p1, t1, p2, t2, p3, t3, PgSeg : Integer);
  524.  
  525. Var
  526.   xTop, yTop, pTop, tTop : Integer;
  527.   xMid, yMid, pMid, tMid : Integer;
  528.   xBot, yBot, pBot, tBot : Integer;
  529.   xVal1, xStep1, xVal2, xStep2 : Integer;
  530.   tVal1, tStep1 : Integer;
  531.   pVal1, pStep1 : Integer;
  532.   Count : Integer;
  533.   xrMid, prMid, trMid : Integer;
  534.   pVal, tVal : Integer;
  535.   HCount : Integer;
  536.   DPhi, DTheta : Integer;
  537.   Segment : Word;
  538.   yDiver : Integer;
  539.  
  540. Begin
  541.   If Y1 < Y2
  542.     Then Begin
  543.       yTop := Y1;
  544.       xTop := X1;
  545.       pTop := p1;
  546.       tTop := t1;
  547.       yBot := Y2;
  548.       xBot := X2;
  549.       pBot := p2;
  550.       tBot := t2;
  551.     End
  552.     Else Begin
  553.       yTop := Y2;
  554.       xTop := X2;
  555.       pTop := p2;
  556.       tTop := t2;
  557.       yBot := Y1;
  558.       xBot := X1;
  559.       pBot := p1;
  560.       tBot := t1;
  561.     End;
  562.   If Y3 < yTop
  563.     Then Begin
  564.       yMid := yTop;
  565.       xMid := xTop;
  566.       tMid := tTop;
  567.       pMid := pTop;
  568.       xTop := X3;
  569.       yTop := Y3;
  570.       pTop := p3;
  571.       tTop := t3;
  572.     End
  573.     Else Begin
  574.       If Y3 > yBot
  575.         Then Begin
  576.           yMid := yBot;
  577.           xMid := xBot;
  578.           pMid := pBot;
  579.           tMid := tBot;
  580.           xBot := X3;
  581.           yBot := Y3;
  582.           pBot := p3;
  583.           tBot := t3;
  584.         End
  585.         Else Begin
  586.           yMid := Y3;
  587.           xMid := X3;
  588.           pMid := p3;
  589.           tMid := t3;
  590.         End;
  591.     End;
  592.  
  593.   If yBot-yTop = 0 Then Exit;
  594.  
  595.   Segment := PgSeg;
  596.  
  597.   yDiver := (yMid-yTop) Shl 8 Div (yBot-yTop);
  598.   Asm
  599.     Mov  ax,xBot
  600.     Sub  ax,xTop
  601.     Mov  bx,yDiver
  602.     IMul bx
  603.     Sar  ax,8
  604.     Add  ax,xTop
  605.     Mov  xrMid,ax
  606.  
  607.     Mov  ax,pBot
  608.     Sub  ax,pTop
  609.     IMul bx
  610.     Sar  ax,8
  611.     Add  ax,pTop
  612.     Mov  prMid,ax
  613.  
  614.     Mov  ax,tBot
  615.     Sub  ax,tTop
  616.     IMul bx
  617.     Sar  ax,8
  618.     Add  ax,tTop
  619.     Mov  trMid,ax
  620.   End;
  621.  
  622. {  xrMid := (yMid-yTop)*(xBot-xTop) Div (yBot-yTop) + xTop;
  623.   prMid := (yMid-yTop)*(pBot-pTop) Div (yBot-yTop) + pTop;
  624.   trMid := (yMid-yTop)*(tBot-tTop) Div (yBot-yTop) + tTop;
  625. }
  626.  
  627.   If xMid < xrMid
  628.     Then Begin    { 2 edges are on the left side }
  629.       DPhi := (prMid-pMid) Shl 8 Div (xrMid-xMid+1);
  630.       DTheta := (trMid-tMid) Shl 8 Div (xrMid-xMid+1);
  631.  
  632.       xVal1 := xTop Shl 6;
  633.       xVal2 := xVal1;
  634.       tVal1 := tTop Shl 8;
  635.       pVal1 := pTop Shl 8;
  636.  
  637.       xStep1 := (xMid-xTop) Shl 6 Div (yMid-yTop+1);
  638.       tStep1 := (tMid-tTop) Shl 8 Div (yMid-yTop+1);
  639.       pStep1 := (pMid-pTop) Shl 8 Div (yMid-yTop+1);
  640.       xStep2 := (xBot-xTop) Shl 6 Div (yBot-yTop+1);
  641.  
  642.       For Count := yTop to yMid do
  643.         Begin
  644.           { ***** HLine *****}
  645.           Asm
  646.             Mov  ax,Segment
  647.             Mov  es,ax
  648.  
  649.             Mov  ax,xVal1
  650.             Shr  ax,6
  651.             Mov  cx,xVal2
  652.             Shr  cx,6
  653.             Sub  cx,ax
  654.             Inc  cx
  655.             Js   @Exit
  656.             Jcxz @Exit
  657.             
  658.  
  659.             Mov  bx,Count
  660.             Mov  dx,bx
  661.             Shl  bx,6
  662.             Add  bh,dl
  663.             Add  bx,ax
  664.             Mov  di,bx
  665.  
  666.             Mov  ax,pVal1
  667.             Mov  si,dPhi
  668.             Mov  dx,tVal1
  669.             
  670.             Push bp
  671.             Mov  bp,dTheta
  672.             Push ds
  673.             Lds  bx,EnvMap
  674.  
  675.             Shr  cx,1
  676.             Jnc @SkipSingle
  677.              Mov  bh,ah
  678.              Mov  bl,dh
  679.              Mov  ch,ds:[bx]
  680.              Mov  es:[di],ch
  681.              Add  ax,si
  682.              Add  dx,bp
  683.              Inc  di
  684.            @SkipSingle:
  685.  
  686.             Xor  ch,ch
  687.             Jcxz @SkipLoop
  688.            @Looper:
  689.             Mov  bh,ah
  690.             Mov  bl,dh
  691.             Mov  ch,ds:[bx]
  692.             Mov  es:[di],ch
  693.             Add  ax,si
  694.             Add  dx,bp
  695.             Inc  di
  696.             Mov  bh,ah
  697.             Mov  bl,dh
  698.             Mov  ch,ds:[bx]
  699.             Mov  es:[di],ch
  700.             Add  ax,si
  701.             Add  dx,bp
  702.             Inc  di
  703.             Dec  cl
  704.             Jnz  @Looper
  705.            @SkipLoop:
  706.  
  707.             Pop  ds
  708.             Pop  bp
  709.            @Exit:
  710.            
  711.           End;
  712.           { ***** End HLine ***** }
  713.           xVal1 := xVal1 + xStep1;
  714.           xVal2 := xVal2 + xStep2;
  715.           pVal1 := pVal1 + pStep1;
  716.           tVal1 := tVal1 + tStep1;
  717.         End;
  718.  
  719.  
  720.       xVal2 := xVal2 - xStep2;
  721.       tVal1 := tMid Shl 8;
  722.       pVal1 := pMid Shl 8;
  723.       xVal1 := xMid Shl 6;
  724.       tStep1 := (tBot-tMid) Shl 8 Div (yBot-yMid+1);
  725.       pStep1 := (pBot-pMid) Shl 8 Div (yBot-yMid+1);
  726.       xStep1 := (xBot-xMid) Shl 6 Div (yBot-yMid+1);
  727.  
  728.       For Count := yMid to yBot do
  729.         Begin
  730.           { ***** HLine *****}
  731.           Asm
  732.             Mov  ax,Segment
  733.             Mov  es,ax
  734.  
  735.             Mov  ax,xVal1
  736.             Shr  ax,6
  737.             Mov  cx,xVal2
  738.             Shr  cx,6
  739.             Sub  cx,ax
  740.             Inc  cx
  741.             Js   @Exit
  742.             Jcxz @Exit
  743.             
  744.  
  745.             Mov  bx,Count
  746.             Mov  dx,bx
  747.             Shl  bx,6
  748.             Add  bh,dl
  749.             Add  bx,ax
  750.             Mov  di,bx
  751.  
  752.             Mov  ax,pVal1
  753.             Mov  si,dPhi
  754.             Mov  dx,tVal1
  755.             
  756.             Push bp
  757.             Mov  bp,dTheta
  758.             Push ds
  759.             Lds  bx,EnvMap
  760.  
  761.             Shr  cx,1
  762.             Jnc @SkipSingle
  763.              Mov  bh,ah
  764.              Mov  bl,dh
  765.              Mov  ch,ds:[bx]
  766.              Mov  es:[di],ch
  767.              Add  ax,si
  768.              Add  dx,bp
  769.              Inc  di
  770.            @SkipSingle:
  771.  
  772.             Xor  ch,ch
  773.             Jcxz @SkipLoop
  774.            @Looper:
  775.             Mov  bh,ah
  776.             Mov  bl,dh
  777.             Mov  ch,ds:[bx]
  778.             Mov  es:[di],ch
  779.             Add  ax,si
  780.             Add  dx,bp
  781.             Inc  di
  782.             Mov  bh,ah
  783.             Mov  bl,dh
  784.             Mov  ch,ds:[bx]
  785.             Mov  es:[di],ch
  786.             Add  ax,si
  787.             Add  dx,bp
  788.             Inc  di
  789.             Dec  cl
  790.             Jnz  @Looper
  791.            @SkipLoop:
  792.  
  793.             Pop  ds
  794.             Pop  bp
  795.            @Exit:
  796.            
  797.           End;
  798.           { ***** End HLine ***** }
  799.           xVal1 := xVal1 + xStep1;
  800.           xVal2 := xVal2 + xStep2;
  801.           pVal1 := pVal1 + pStep1;
  802.           tVal1 := tVal1 + tStep1;
  803.         End;
  804.  
  805.  
  806.     End
  807.     Else Begin  { Long Edge on the left side }
  808.       DPhi := (pMid-prMid) Shl 8 Div (xMid-xrMid+1);
  809.       DTheta := (tMid-trMid) Shl 8 Div (xMid-xrMid+1);
  810.  
  811.       xVal1 := xTop Shl 6;
  812.       tVal1 := tTop Shl 8;
  813.       pVal1 := pTop Shl 8;
  814.       xVal2 := xVal1;
  815.       xStep1 := (xBot-xTop) Shl 6 Div (yBot-yTop+1);
  816.       tStep1 := (tBot-tTop) Shl 8 Div (yBot-yTop+1);
  817.       pStep1 := (pBot-pTop) Shl 8 Div (yBot-yTop+1);
  818.       xStep2 := (xMid-xTop) Shl 6 Div (yMid-yTop+1);
  819.       For Count := yTop to yMid do
  820.         Begin
  821.           { ***** HLine *****}
  822.           Asm
  823.             Mov  ax,Segment
  824.             Mov  es,ax
  825.  
  826.             Mov  ax,xVal1
  827.             Shr  ax,6
  828.             Mov  cx,xVal2
  829.             Shr  cx,6
  830.             Sub  cx,ax
  831.             Inc  cx
  832.             Js   @Exit
  833.             Jcxz @Exit
  834.             
  835.  
  836.             Mov  bx,Count
  837.             Mov  dx,bx
  838.             Shl  bx,6
  839.             Add  bh,dl
  840.             Add  bx,ax
  841.             Mov  di,bx
  842.  
  843.             Mov  ax,pVal1
  844.             Mov  si,dPhi
  845.             Mov  dx,tVal1
  846.             
  847.             Push bp
  848.             Mov  bp,dTheta
  849.             Push ds
  850.             Lds  bx,EnvMap
  851.  
  852.             Shr  cx,1
  853.             Jnc @SkipSingle
  854.              Mov  bh,ah
  855.              Mov  bl,dh
  856.              Mov  ch,ds:[bx]
  857.              Mov  es:[di],ch
  858.              Add  ax,si
  859.              Add  dx,bp
  860.              Inc  di
  861.            @SkipSingle:
  862.  
  863.             Xor  ch,ch
  864.             Jcxz @SkipLoop
  865.            @Looper:
  866.             Mov  bh,ah
  867.             Mov  bl,dh
  868.             Mov  ch,ds:[bx]
  869.             Mov  es:[di],ch
  870.             Add  ax,si
  871.             Add  dx,bp
  872.             Inc  di
  873.             Mov  bh,ah
  874.             Mov  bl,dh
  875.             Mov  ch,ds:[bx]
  876.             Mov  es:[di],ch
  877.             Add  ax,si
  878.             Add  dx,bp
  879.             Inc  di
  880.             Dec  cl
  881.             Jnz  @Looper
  882.            @SkipLoop:
  883.  
  884.             Pop  ds
  885.             Pop  bp
  886.            @Exit:
  887.            
  888.           End;
  889.           { ***** End HLine ***** }
  890.           xVal1 := xVal1 + xStep1;
  891.           xVal2 := xVal2 + xStep2;
  892.           pVal1 := pVal1 + pStep1;
  893.           tVal1 := tVal1 + tStep1;
  894.         End;
  895.  
  896.       tVal1 := tVal1 - tStep1;
  897.       pVal1 := pVal1 - pStep1;
  898.       xVal1 := xVal1 - xStep1;
  899.       xStep2 := (xBot-xMid) Shl 6 Div (yBot-yMid+1);
  900.       For Count := yMid to yBot do
  901.         Begin
  902.           { ***** HLine *****}
  903.           Asm
  904.             Mov  ax,Segment
  905.             Mov  es,ax
  906.  
  907.             Mov  ax,xVal1
  908.             Shr  ax,6
  909.             Mov  cx,xVal2
  910.             Shr  cx,6
  911.             Sub  cx,ax
  912.             Inc  cx
  913.             Js   @Exit
  914.             Jcxz @Exit
  915.             
  916.  
  917.             Mov  bx,Count
  918.             Mov  dx,bx
  919.             Shl  bx,6
  920.             Add  bh,dl
  921.             Add  bx,ax
  922.             Mov  di,bx
  923.  
  924.             Mov  ax,pVal1
  925.             Mov  si,dPhi
  926.             Mov  dx,tVal1
  927.             
  928.             Push bp
  929.             Mov  bp,dTheta
  930.             Push ds
  931.             Lds  bx,EnvMap
  932.  
  933.             Shr  cx,1
  934.             Jnc @SkipSingle
  935.              Mov  bh,ah
  936.              Mov  bl,dh
  937.              Mov  ch,ds:[bx]
  938.              Mov  es:[di],ch
  939.              Add  ax,si
  940.              Add  dx,bp
  941.              Inc  di
  942.            @SkipSingle:
  943.  
  944.             Xor  ch,ch
  945.             Jcxz @SkipLoop
  946.            @Looper:
  947.             Mov  bh,ah
  948.             Mov  bl,dh
  949.             Mov  ch,ds:[bx]
  950.             Mov  es:[di],ch
  951.             Add  ax,si
  952.             Add  dx,bp
  953.             Inc  di
  954.             Mov  bh,ah
  955.             Mov  bl,dh
  956.             Mov  ch,ds:[bx]
  957.             Mov  es:[di],ch
  958.             Add  ax,si
  959.             Add  dx,bp
  960.             Inc  di
  961.             Dec  cl
  962.             Jnz  @Looper
  963.            @SkipLoop:
  964.  
  965.             Pop  ds
  966.             Pop  bp
  967.            @Exit:
  968.            
  969.           End;
  970.           { ***** End HLine ***** }
  971.           xVal1 := xVal1 + xStep1;
  972.           xVal2 := xVal2 + xStep2;
  973.           pVal1 := pVal1 + pStep1;
  974.           tVal1 := tVal1 + tStep1;
  975.         End;
  976.     End;
  977.  
  978. End;
  979.  
  980.  
  981.  
  982.  
  983.  
  984. Procedure PhongClipPolygon(x1, y1, x2, y2, x3, y3, t1, p1, t2, p2, t3, p3, PgSeg : Integer; EMap : Pointer);
  985.  
  986. Type
  987.   tFlatClip = Record
  988.     x, y, x1, y1 : Integer;
  989.     t, p, t1, p1 : Integer;
  990.   End;
  991.  
  992. Var
  993.   FlatList : Array[0..7] of tFlatClip;
  994.   NumVert : Integer;
  995.   Count : Integer;
  996.   ClipVert : Integer;
  997.   V1, V2 : Integer;
  998.  
  999. Begin
  1000.   EnvMap := EMap;
  1001.   If (((X1 >= MinX) and (X1 <= MaxX)) and ((Y1 >= MinY) and (Y1 <= MaxY)) and
  1002.       ((X2 >= MinX) and (X2 <= MaxX)) and ((Y2 >= MinY) and (Y2 <= MaxY))) and
  1003.       ((X3 >= MinX) and (X3 <= MaxX)) and ((Y3 >= MinY) and (Y3 <= MaxY))
  1004.       Then Begin
  1005.         PhongPoly(X1, Y1, X2, Y2, X3, Y3, p1, t1, p2, t2, p3, t3, PgSeg);
  1006.         Exit;
  1007.       End;
  1008.   FlatList[0].x := X1;
  1009.   FlatList[0].y := Y1;
  1010.   FlatList[0].t := t1;
  1011.   FlatList[0].p := p1;
  1012.   FlatList[1].x := X2;
  1013.   FlatList[1].y := Y2;
  1014.   FlatList[1].t := t2;
  1015.   FlatList[1].p := p2;
  1016.   FlatList[2].x := X3;
  1017.   FlatList[2].y := Y3;
  1018.   FlatList[2].t := t3;
  1019.   FlatList[2].p := p3;
  1020.   NumVert := 3;
  1021.   ClipVert := 0;
  1022.   { Clip against left side }
  1023.   V1 := NumVert - 1;
  1024.   For Count := 0 to (NumVert-1) do
  1025.     Begin
  1026.       If (FlatList[V1].x >= MinX) and (FlatList[Count].x >= MinX)
  1027.         Then Begin
  1028.           FlatList[ClipVert].x1 := FlatList[Count].x;
  1029.           FlatList[ClipVert].y1 := FlatList[Count].y;
  1030.           FlatList[ClipVert].t1 := FlatList[Count].t;
  1031.           FlatList[ClipVert].p1 := FlatList[Count].p;
  1032.           ClipVert := ClipVert + 1;
  1033.         End;
  1034.       If (FlatList[V1].x >= MinX) and (FlatList[Count].x < MinX)
  1035.         Then Begin
  1036.           FlatList[ClipVert].x1 := MinX;
  1037.           FlatList[ClipVert].y1 := (FlatList[Count].y-FlatList[V1].y)*(MinX-FlatList[v1].x)
  1038.                                     Div (FlatList[Count].x-FlatList[V1].x) + FlatList[V1].y;
  1039.           FlatList[ClipVert].t1 := (FlatList[Count].t-FlatList[V1].t)*(MinX-FlatList[v1].x)
  1040.                                     Div (FlatList[Count].x-FlatList[V1].x) + FlatList[V1].t;
  1041.           FlatList[ClipVert].p1 := (FlatList[Count].p-FlatList[V1].p)*(MinX-FlatList[v1].x)
  1042.                                     Div (FlatList[Count].x-FlatList[V1].x) + FlatList[V1].p;
  1043.           ClipVert := ClipVert + 1;
  1044.         End;
  1045.       If (FlatList[V1].x < MinX) and (FlatList[Count].x >= MinX)
  1046.         Then Begin
  1047.           FlatList[ClipVert].x1 := MinX;
  1048.           FlatList[ClipVert].y1 := (FlatList[Count].y-FlatList[V1].y)*(MinX-FlatList[V1].x)
  1049.                                     Div (FlatList[Count].x-FlatList[V1].x) + FlatList[v1].y;
  1050.           FlatList[ClipVert].t1 := (FlatList[Count].t-FlatList[V1].t)*(MinX-FlatList[V1].x)
  1051.                                     Div (FlatList[Count].x-FlatList[V1].x) + FlatList[v1].t;
  1052.           FlatList[ClipVert].p1 := (FlatList[Count].p-FlatList[V1].p)*(MinX-FlatList[V1].x)
  1053.                                     Div (FlatList[Count].x-FlatList[V1].x) + FlatList[v1].p;
  1054.           ClipVert := ClipVert + 1;
  1055.           FlatList[ClipVert].x1 := FlatList[Count].x;
  1056.           FlatList[ClipVert].y1 := FlatList[Count].y;
  1057.           FlatList[ClipVert].t1 := FlatList[Count].t;
  1058.           FlatList[ClipVert].p1 := FlatList[Count].p;
  1059.           ClipVert := ClipVert + 1;
  1060.         End;
  1061.       V1 := Count;
  1062.     End;
  1063.   NumVert := ClipVert;
  1064.   ClipVert := 0;
  1065.   { Clip against Right side }
  1066.   V1 := NumVert - 1;
  1067.   For Count := 0 to (NumVert-1) do
  1068.     Begin
  1069.       If (FlatList[v1].x1 <= MaxX) and (FlatList[Count].x1 <= MaxX)
  1070.         Then Begin
  1071.           FlatList[ClipVert].x := FlatList[Count].x1;
  1072.           FlatList[ClipVert].y := FlatList[Count].y1;
  1073.           FlatList[ClipVert].t := FlatList[Count].t1;
  1074.           FlatList[ClipVert].p := FlatList[Count].p1;
  1075.           ClipVert := ClipVert + 1;
  1076.         End;
  1077.       If (FlatList[V1].x1 <= MaxX) and (FlatList[Count].x1 > MaxX)
  1078.         Then Begin
  1079.           FlatList[ClipVert].x := MaxX;
  1080.           FlatList[ClipVert].y := (FlatList[Count].y1-FlatList[V1].y1)*(MaxX-FlatList[v1].x1)
  1081.                                    Div (FlatList[Count].x1-FlatList[v1].x1) + FlatList[v1].y1;
  1082.           FlatList[ClipVert].t := (FlatList[Count].t1-FlatList[V1].t1)*(MaxX-FlatList[v1].x1)
  1083.                                    Div (FlatList[Count].x1-FlatList[v1].x1) + FlatList[v1].t1;
  1084.           FlatList[ClipVert].p := (FlatList[Count].p1-FlatList[V1].p1)*(MaxX-FlatList[v1].x1)
  1085.                                    Div (FlatList[Count].x1-FlatList[v1].x1) + FlatList[v1].p1;
  1086.           ClipVert := ClipVert + 1;
  1087.         End;
  1088.       If (FlatList[v1].x1 > MaxX) and (FlatList[Count].x1 <= MaxX)
  1089.         Then Begin
  1090.           FlatList[ClipVert].x := MaxX;
  1091.           FlatList[ClipVert].y := (FlatList[Count].y1-FlatList[V1].y1)*(MaxX-FlatList[v1].x1)
  1092.                                    Div (FlatList[Count].x1-FlatList[v1].x1) + FlatList[v1].y1;
  1093.           FlatList[ClipVert].t := (FlatList[Count].t1-FlatList[V1].t1)*(MaxX-FlatList[v1].x1)
  1094.                                    Div (FlatList[Count].x1-FlatList[v1].x1) + FlatList[v1].t1;
  1095.           FlatList[ClipVert].p := (FlatList[Count].p1-FlatList[V1].p1)*(MaxX-FlatList[v1].x1)
  1096.                                    Div (FlatList[Count].x1-FlatList[v1].x1) + FlatList[v1].p1;
  1097.           ClipVert := ClipVert + 1;
  1098.           FlatList[ClipVert].x := FlatList[Count].x1;
  1099.           FlatList[ClipVert].y := FlatList[Count].y1;
  1100.           FlatList[ClipVert].t := FlatList[Count].t1;
  1101.           FlatList[ClipVert].p := FlatList[Count].p1;
  1102.           ClipVert := ClipVert + 1;
  1103.         End;
  1104.       v1 := Count;
  1105.     End;
  1106.   NumVert := ClipVert;
  1107.   ClipVert := 0;
  1108.   { Clip against top edge }
  1109.   V1 := NumVert - 1;
  1110.   For Count := 0 to (NumVert-1) do
  1111.     Begin
  1112.       If (FlatList[V1].y >= MinY) and (FlatList[Count].y >= MinY)
  1113.         Then Begin
  1114.           FlatList[ClipVert].x1 := FlatList[Count].x;
  1115.           FlatList[ClipVert].y1 := FlatList[Count].y;
  1116.           FlatList[ClipVert].t1 := FlatList[Count].t;
  1117.           FlatList[ClipVert].p1 := FlatList[Count].p;
  1118.           ClipVert := ClipVert + 1;
  1119.         End;
  1120.       If (FlatList[V1].y >= MinY) and (FlatList[Count].y < MinY)
  1121.         Then Begin
  1122.           FlatList[ClipVert].x1 := (MinY-FlatList[V1].y)*(FlatList[Count].x-FlatList[V1].x)
  1123.                                            Div (FlatList[Count].y-FlatList[V1].y) + FlatList[V1].x;
  1124.           FlatList[ClipVert].t1 := (MinY-FlatList[V1].y)*(FlatList[Count].t-FlatList[V1].t)
  1125.                                            Div (FlatList[Count].y-FlatList[V1].y) + FlatList[V1].t;
  1126.           FlatList[ClipVert].p1 := (MinY-FlatList[V1].y)*(FlatList[Count].p-FlatList[V1].p)
  1127.                                            Div (FlatList[Count].y-FlatList[V1].y) + FlatList[V1].p;
  1128.           FlatList[ClipVert].y1 := MinY;
  1129.           ClipVert := ClipVert + 1;
  1130.         End;
  1131.       If (FlatList[V1].y < MinY) and (FlatList[Count].y >= MinY)
  1132.         Then Begin
  1133.           FlatList[ClipVert].x1 := (MinY-FlatList[V1].y)*(FlatList[Count].x-FlatList[V1].x)
  1134.                                     Div (FlatList[Count].y-FlatList[V1].y) + FlatList[V1].x;
  1135.           FlatList[ClipVert].t1 := (MinY-FlatList[V1].y)*(FlatList[Count].t-FlatList[V1].t)
  1136.                                     Div (FlatList[Count].y-FlatList[V1].y) + FlatList[V1].t;
  1137.           FlatList[ClipVert].p1 := (MinY-FlatList[V1].y)*(FlatList[Count].p-FlatList[V1].p)
  1138.                                     Div (FlatList[Count].y-FlatList[V1].y) + FlatList[V1].p;
  1139.           FlatList[ClipVert].y1 := MinY;
  1140.           ClipVert := ClipVert + 1;
  1141.           FlatList[ClipVert].x1 := FlatList[Count].x;
  1142.           FlatList[ClipVert].y1 := FlatList[Count].y;
  1143.           FlatList[ClipVert].t1 := FlatList[Count].t;
  1144.           FlatList[ClipVert].p1 := FlatList[Count].p;
  1145.           ClipVert := ClipVert + 1;
  1146.         End;
  1147.       V1 := Count;
  1148.     End;
  1149.   NumVert := ClipVert;
  1150.   ClipVert := 0;
  1151.   V1 := NumVert - 1;
  1152.   For Count := 0 to (NumVert-1) do
  1153.     Begin
  1154.       If (FlatList[V1].y1 <= MaxY) and (FlatList[Count].y1 <= MaxY)
  1155.         Then Begin
  1156.           FlatList[ClipVert].x := FlatList[Count].x1;
  1157.           FlatList[ClipVert].y := FlatList[Count].y1;
  1158.           FlatList[ClipVert].t := FlatList[Count].t1;
  1159.           FlatList[ClipVert].p := FlatList[Count].p1;
  1160.           ClipVert := ClipVert + 1;
  1161.         End;
  1162.       If (FlatList[V1].y1 <= MaxY) and (FlatList[Count].y1 > MaxY)
  1163.         Then Begin
  1164.           FlatList[ClipVert].x := (MaxY-FlatList[V1].y1)*(FlatList[Count].x1-FlatList[V1].x1)
  1165.                                     Div (FlatList[Count].y1-FlatList[v1].y1) + FlatList[V1].x1;
  1166.           FlatList[ClipVert].t := (MaxY-FlatList[V1].y1)*(FlatList[Count].t1-FlatList[V1].t1)
  1167.                                     Div (FlatList[Count].y1-FlatList[v1].y1) + FlatList[V1].t1;
  1168.           FlatList[ClipVert].p := (MaxY-FlatList[V1].y1)*(FlatList[Count].p1-FlatList[V1].p1)
  1169.                                     Div (FlatList[Count].y1-FlatList[v1].y1) + FlatList[V1].p1;
  1170.           FlatList[ClipVert].y := MaxY;
  1171.           ClipVert := ClipVert + 1;
  1172.         End;
  1173.       If (FlatList[V1].y1 > MaxY) and (FlatList[Count].y1 <= MaxY)
  1174.         Then Begin
  1175.           FlatList[ClipVert].x := (MaxY-FlatList[V1].y1)*(FlatList[Count].x1-FlatList[V1].x1)
  1176.                                     Div (FlatList[Count].y1-FlatList[v1].y1) + FlatList[V1].x1;
  1177.           FlatList[ClipVert].t := (MaxY-FlatList[V1].y1)*(FlatList[Count].t1-FlatList[V1].t1)
  1178.                                     Div (FlatList[Count].y1-FlatList[v1].y1) + FlatList[V1].t1;
  1179.           FlatList[ClipVert].p := (MaxY-FlatList[V1].y1)*(FlatList[Count].p1-FlatList[V1].p1)
  1180.                                     Div (FlatList[Count].y1-FlatList[v1].y1) + FlatList[V1].p1;
  1181.           FlatList[ClipVert].y := MaxY;
  1182.           ClipVert := ClipVert + 1;
  1183.           FlatList[ClipVert].x := FlatList[Count].x1;
  1184.           FlatList[ClipVert].y := FlatList[Count].y1;
  1185.           FlatList[ClipVert].t := FlatList[Count].t1;
  1186.           FlatList[ClipVert].p := FlatList[Count].p1;
  1187.           ClipVert := ClipVert + 1;
  1188.         End;
  1189.       V1 := Count;
  1190.     End;
  1191.   NumVert := ClipVert;
  1192.  
  1193.   V1 := 1;
  1194.   V2 := 2;
  1195.  
  1196.   { Now draw the polygons }
  1197.   For Count := 0 to (NumVert - 3) do
  1198.     Begin
  1199.       PhongPoly(FlatList[0].x, FlatList[0].y,
  1200.                 FlatList[V1].x, FlatList[V1].y,
  1201.                 FlatList[V2].x, FlatList[V2].y,
  1202.                 FlatList[0].p, FlatList[0].t,
  1203.                 FlatList[V1].p, FlatList[V1].t,
  1204.                 FlatList[V2].p, FlatList[V2].t, PgSeg);
  1205.       V1 := V2;
  1206.       V2 := V2 + 1;
  1207.     End;
  1208. End;
  1209.  
  1210. End.