home *** CD-ROM | disk | FTP | other *** search
/ Sound, Music & MIDI Collection 2 / SMMVOL2.bin / PROG / BWSB120B.ZIP / TTP / POLY.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-12-16  |  6.4 KB  |  279 lines

  1. ; A Polygon fill routine.
  2. ; Alex Chalfin   10/25/94
  3. ; Internet: achalfin@uceng.uc.edu
  4. ;
  5. ; Pascal Declaration:
  6. ;
  7. ; Procedure DrawPoly(Var VertexList;NumVert:Word;Color:Byte;PageAddr:Word);
  8. ;
  9.  
  10. .Model Large,Pascal
  11. .Data
  12.  
  13. MinY       dw ?   ; Starting Y value (Minimum)
  14. Edges      dw ?   ; Number of edges in the polygon
  15. StartVert1 dw ?   ; Start vertex index for line1
  16. StartVert2 dw ?   ; Start vertex index for line2
  17. EndVert1   dw ?   ; End vertex index for line 1
  18. EndVert2   dw ?   ; End vertex index for line 2
  19. Delta1     dd ?   ; Change in X location for line 1
  20. Delta2     dd ?   ; Change in X location for line 2
  21. Count1     dw ?   ; Y Counter for line 1
  22. Count2     dw ?   ; Y Counter for line 2
  23. XVal1      dd ?   ; X place holder for line 1
  24. XVal2      dd ?   ; X place holder for line 2
  25. YCount     dw ?   ; Store current Y location
  26. PolyColor  db ?   ; Color to draw polygon
  27.  
  28. .Code
  29. .386
  30.  
  31. Public DrawPoly
  32.  
  33.  
  34. FINDMINMAX Macro
  35.   Lfs  di,Vert     ;; Make fs:di point to screen vertex list
  36. ;*** Find the minimum and maximum values of polyogn
  37.   Mov  ax,06fffh   ; use ax for minimum value
  38.   Mov  MinY,ax
  39.   Mov  cx,0
  40.  @MinMaxLooper:
  41.   Mov  bx,cx
  42.   Shl  bx,2
  43.   Mov  ax,Word Ptr fs:[di+bx+2]   ; Get Y value for vertex list
  44.   Cmp  ax,MinY                    ; Check for minimum value
  45.   Jge @SkipMinStore
  46.   Mov  StartVert1,cx              ; Store the starting (top) vertex
  47.   Mov  MinY,ax
  48.  @SkipMinStore:
  49.   Inc  cx
  50.   Cmp  cx,Num
  51.   Jnz @MinMaxLooper
  52.   Mov  ax,MinY
  53.   Mov  YCount,ax
  54. EndM ;FINDMINMAX
  55.  
  56. Proc ZeroCheck Near
  57.   Cmp bx,0
  58.   Jne @Skip
  59.   Inc bx
  60.  @Skip:
  61.   Ret
  62. EndP ZeroCheck
  63.  
  64. CALCDELTA1 Macro
  65.   Mov   bx,StartVert1
  66.   Shl   bx,2
  67.   Movsx eax,Word Ptr fs:[di+bx]   ;  Get first X value, X1
  68.   Mov   XVal1,eax
  69.   Push  eax                       ; Store X value on stack
  70.   Movsx eax,Word Ptr fs:[di+bx+2] ; Get Y Value, Y1
  71.   Mov   bx,EndVert1
  72.   Shl   bx,2
  73.   Movsx ecx,Word Ptr fs:[di+bx]   ; Get Second X Value, X2
  74.   Push  ecx
  75.   Movsx ecx,Word Ptr fs:[di+bx+2] ; Get Y value, Y2
  76.   Sub   ecx,eax
  77.   Inc   ecx        ; cx := Y2-Y1+1
  78.   Mov   edx,ecx    ; store in dx for later use
  79.   Pop   ecx        ; Restore X2
  80.   Pop   eax        ; Restore X1
  81.   Sub   ecx,eax
  82.   Shl   ecx,8      ; cx := (X2-X1) Shl 8
  83.   Mov   eax,ecx
  84.   Mov   ebx,edx    ; bx := Y2-Y1+1
  85.   Mov   Count1,bx  ; Store Count1
  86.   Cdq              ; Convert DWord in EAX to QWord in EDX:EAX
  87.   Call  ZeroCheck  ; Make sure divisor isn't 0
  88.   Idiv  ebx
  89.   Mov   Delta1,eax ; Store the first delta
  90. EndM
  91.  
  92. CALCDELTA2 Macro
  93.   Mov  bx,StartVert2
  94.   Shl  bx,2
  95.   Movsx eax,Word Ptr fs:[di+bx]  ;  Get first X value, X1
  96.   Mov  XVal2,eax
  97.   Push eax                       ; Store X value on stack
  98.   Movsx eax,Word Ptr fs:[di+bx+2]  ; Get Y Value, Y1
  99.   Mov  bx,EndVert2
  100.   Shl  bx,2
  101.   Movsx ecx,Word Ptr fs:[di+bx]  ; Get Second X Value, X2
  102.   Push ecx
  103.   Movsx ecx,Word Ptr fs:[di+bx+2]  ; Get Y value, Y2
  104.   Sub  ecx,eax
  105.   Inc  ecx        ; cx := Y2-Y1+1
  106.   Mov  edx,ecx    ; store in dx for later use
  107.   Pop  ecx        ; Restore X2
  108.   Pop  eax        ; Restore X1
  109.   Sub  ecx,eax
  110.   Shl  ecx,8      ; cx := (X2-X1) Shl 8
  111.   Mov  eax,ecx
  112.   Mov  ebx,edx    ; bx := Y2-Y1+1
  113.   Mov  Count2,bx  ; Store Count2
  114.   Cdq             ; Convert DWord in EAX to QWord in EDX:EAX
  115.   Call ZeroCheck  ; Make sure divisor isn't 0
  116.   Idiv ebx
  117.   Mov  Delta2,eax    ; Store the first delta
  118. EndM
  119.  
  120. HLINE Macro
  121. ;;  eax = X1, ebx = x2, dx = y
  122.   Push eax
  123.   Push ebx
  124.   Push dx
  125.   Cmp  dx,0     ; Check lower Y bound
  126.   Jl  @Nothing
  127.   Cmp  dx,199   ; Check upper Y bound
  128.   Jg  @Nothing
  129.   Shr  eax,8
  130.   Shr  ebx,8
  131. ;;; Do clipping
  132.   Cmp  bx,ax
  133.   Jl @Nothing
  134.   Cmp  ax,319
  135.   Jg  @Nothing
  136.   Cmp  bx,0
  137.   Jl  @Nothing
  138.   Cmp  ax,0      ; Left Clip
  139.   Jg  @CheckBX
  140.   Mov  ax,0
  141.  @CheckBX:
  142.   Cmp  bx,319    ; Right Clip
  143.   Jle @DrawIt
  144.   Mov  bx,319
  145.  @DrawIt:
  146.   Sub bx,ax
  147.   Inc bx
  148.   Mov cx,bx  ; Put Count in CX
  149.   Mov di,ax
  150.   Mov ax,320
  151.   Mov bx,dx
  152.   Mul bx
  153.   Add di,ax
  154.   Mov al,PolyColor
  155.   Mov ah,al
  156.   Shr cx,1
  157.   Jnc @SkipSingle
  158.   Stosb
  159.  @SkipSingle:
  160.   Jcxz @Nothing
  161.   Rep Stosw
  162.  @Nothing:
  163.   Pop dx
  164.   Pop ebx
  165.   Pop eax
  166. EndM
  167.  
  168.  
  169. Proc DrawPoly Uses es si di,Vert:DWord,Num:Word,Col:Byte,PgSg:Word
  170. ;; VERT is a list of screen points
  171. ;; NUM is the number of points
  172. ;; COL is the color to draw the polygon
  173. ;; PGSG is the segment pointer to the screen
  174.   Mov  al,Col
  175.   Mov  PolyColor,al
  176.   Mov  es,PgSg
  177.   Mov  ax,Num
  178.   Mov  Edges,ax
  179. ;;; Find minimum and maximum values
  180.   FINDMINMAX
  181. ;;; Set up initial start an end vertex indicies
  182.   Mov  ax,StartVert1
  183.   Mov  StartVert2,ax
  184.   Dec  ax
  185.   Jns  @SkipV1Reset
  186.   Mov  ax,Num
  187.   Dec  ax
  188.  @SkipV1Reset:
  189.   Mov  EndVert1,ax   ; EVert1 := StartVert1 - 1;
  190.   Mov  ax,StartVert2
  191.   Inc  ax
  192.   Cmp  ax,Num
  193.   Jnz  @SkipV2Reset
  194.   Mov  ax,0
  195.  @SkipV2Reset:       ; EVert2 := StartVert2 + 1;
  196.   Mov EndVert2,ax
  197. ;;;;  Calculate the delta values
  198.   CALCDELTA1
  199.   CALCDELTA2
  200. ;;;;  Begin Main loop
  201.   ; While Edge > 1 do
  202.  @EdgeLooper:
  203.   Mov  ax,Edges
  204.   Cmp  ax,1
  205.   Jle @ExitTraceLoop
  206.   Push di
  207.   Mov  eax,XVal1
  208.   Mov  ebx,XVal2
  209.   Shl  eax,8        ; Make XVals .8 fixed point
  210.   Shl  ebx,8
  211.   Mov  cx,Count1
  212.   Shl  ecx,16
  213.   Mov  cx,Count2
  214.   Mov  dx,YCount
  215.   Push ebp
  216.   Mov  ebp,Delta1
  217.   Mov  esi,Delta2
  218.  @TraceLoop:     ; This is it! This loop draws everything
  219.   Push ecx
  220.   Cmp  cx,0      ; Check if count2 is zero yet
  221.   Jle  @ExitInnerTraceLoop
  222.   Shr  ecx,16
  223.   Cmp  cx,0      ; Check if count1 is zero
  224.   Jle  @ExitInnerTraceLoop
  225.   HLINE
  226.   Add  eax,ebp   ; Increment XVal1 by Delta1
  227.   Add  ebx,esi   ; Increment XVal2 by Delta2
  228.   Pop  ecx
  229.   Sub  ecx,00010001h  ; Subtract 1 from Count1 and Count2
  230.   Inc  dx
  231.   Jmp  @TraceLoop
  232.  @ExitInnerTraceLoop:
  233.   Shr  eax,8
  234.   Mov  XVal1,eax   ; Store updated XVals
  235.   Shr  ebx,8
  236.   Mov  XVal2,ebx
  237.   Pop  ecx
  238.   Pop  ebp
  239.   Pop  di
  240.   Mov  Count2,cx
  241.   Shr  ecx,16
  242.   Mov  Count1,cx
  243.   Mov  YCount,dx
  244. ;;;;;;  Up date left side vertex
  245.   Mov  ax,Count1
  246.   Cmp  ax,0      ; Check Count1
  247.   Jg  @CheckNext
  248.   Mov  ax,EndVert1
  249.   Mov  StartVert1,ax
  250.   Dec  ax
  251.   Jns @SkipV1Reset2
  252.   Mov  ax,Num
  253.   Dec  ax
  254.  @SkipV1Reset2:
  255.   Mov  EndVert1,ax
  256.   CALCDELTA1
  257.   Dec  Edges
  258.  @CheckNext:
  259. ;;;;;; Update right side vertex
  260.   Mov  ax,Count2
  261.   Cmp  ax,0
  262.   Jg  @SkipCount2
  263.   Mov  ax,EndVert2
  264.   Mov  StartVert2,ax
  265.   Inc  ax
  266.   Cmp  ax,Num
  267.   Jnz  @SkipV2Reset2
  268.   Mov  ax,0
  269.  @SkipV2Reset2:
  270.   Mov  EndVert2,ax
  271.   CALCDELTA2
  272.   Dec  Edges
  273.  @SkipCount2:
  274.   Jmp @EdgeLooper
  275.  @ExitTraceLoop:
  276.   Ret
  277. Endp DrawPoly
  278.  
  279. End