home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / vr386 / polyout.asm < prev    next >
Assembly Source File  |  1996-03-19  |  6KB  |  262 lines

  1.     TITLE    POLYOUT - POST-PIPELINE POLY PROCESS IN ASSEMBLER
  2.  
  3.     COMMENT $
  4.  
  5. // 26/12/93 by Dave Stampe
  6. // All algorithms and code (c) 1993 by Dave Stampe
  7.  
  8. /*
  9.  This code is part of the REND386 project, created by Dave Stampe and
  10.  Bernie Roehl.
  11.  
  12.  Copyright 1992, 1993, 1994 by Dave Stampe and Bernie Roehl.
  13.  
  14.  May be freely used to write software for release into the public domain;
  15.  all commercial endeavours MUST contact BOTH Bernie Roehl and Dave Stampe
  16.  for permission to incorporate any part of this software into their
  17.  products!  Usually there is no charge for under 50-100 items for
  18.  low-cost or shareware, and terms are reasonable.  Any royalties are used
  19.  for development, so equipment is often acceptable payment.
  20.  
  21.  ATTRIBUTION:  If you use any part of this source code or the libraries
  22.  in your projects, you must give attribution to REND386, Dave Stampe,
  23.  and Bernie Roehl in your documentation, source code, and at startup
  24.  of your program.  Let's keep the freeware ball rolling!  No more
  25.  code ripoffs please.
  26.  
  27.  CONTACTS: dstampe@psych.toronto.edu, broehl@sunee.uwaterloo.ca
  28.  See the COPYRITE.H file for more information.
  29. */
  30.  
  31. This file unpacks polygons from the pipeline into an XY vertex
  32. coordinate array.  It also chacks the poly to see if a test-point
  33. is inside of it.
  34.  
  35.  
  36. /* Contact: dstampe@sunee.waterloo.edu */
  37.  
  38.         $
  39.  
  40.     .MODEL large
  41.     .386
  42.  
  43.     .DATA
  44.  
  45. include 3dstruct.inc
  46. include viewdata.inc
  47.  
  48.     .CODE RENDERER
  49.  
  50.  
  51. ; /************ UNPACK VERTICES, CONVERT TO SCREEN COORDS *************/
  52.  
  53. ; unpack poly's vertices to array, in X,Y pair order
  54. ; returns number of vertices unpacked
  55. ; vertices order can be reversed if screen, even if mirrored
  56. ;
  57. ;int unpack_poly_vertices(NPOLY *poly, int *vtxarray, int direction)
  58.  
  59. polyn     equ    DWORD PTR [bp+8]          ; arguments
  60. array     equ    DWORD PTR [bp+12]
  61. direct   equ    WORD PTR [bp+16]
  62.  
  63. count   equ    WORD PTR [bp-2]      ; locals
  64.  
  65.     PUBLIC    _unpack_poly_vertices
  66.  
  67. _unpack_poly_vertices    proc    far
  68.  
  69.     .386
  70.     push    ebp
  71.     mov    ebp,esp
  72.     sub    esp,8
  73.  
  74.     push    ds
  75.     push    esi
  76.     push    edi
  77.     push    ecx
  78.  
  79.     lds     di,array          ; MAKES ASSUMPTION THAT NPOLYS
  80.                   ; AND NVERTEX STRUCTS FOR REP
  81.     les    si,polyn          ; ARE IN SAME SEGMENT!
  82.  
  83.     mov    cx,WORD PTR es:[si].NP_npoints
  84.     mov    count,cx
  85.     add    si, SIZE NPOLY       ; vertex pointer at END of structure
  86.  
  87.     test    direct,-1         ; forward or reverse?
  88.     jne    reverse
  89.  
  90. copy_fwd:
  91.     mov    bx,WORD PTR es:[si]         ; ptr to vertex
  92.     mov    ax,WORD PTR es:[bx].NV_xs   ; convert X
  93.     add    ax,2
  94.     shr    ax,2
  95.     mov    WORD PTR ds:[di],ax
  96.  
  97.     mov    ax,WORD PTR es:[bx].NV_ys   ; convert Y
  98.     add    ax,2
  99.     shr    ax,2
  100.     mov    WORD PTR ds:[di+2],ax
  101.  
  102.     add    di,4
  103.     add    si,4
  104.     dec    cx
  105.     jne    copy_fwd
  106.     jmp    end_copy
  107.  
  108. reverse:
  109.     add    si,cx     ; skip to end of vtx ptr table
  110.     add    si,cx
  111.     add    si,cx
  112.     add    si,cx
  113. copy_rev:
  114.     sub    si,4
  115.     mov    bx,WORD PTR es:[si]         ; ptr to vertex
  116.     mov    ax,WORD PTR es:[bx].NV_xs   ; convert X
  117.     add    ax,2
  118.     shr    ax,2
  119.     mov    WORD PTR ds:[di],ax
  120.  
  121.     mov    ax,WORD PTR es:[bx].NV_ys   ; convert Y
  122.     add    ax,2
  123.     shr    ax,2
  124.     mov    WORD PTR ds:[di+2],ax
  125.  
  126.     add    di,4
  127.     dec    cx
  128.     jne    copy_rev
  129.  
  130. end_copy:
  131.     mov    ax,count    ; return number of vertices copied
  132.  
  133.     pop    ecx
  134.     pop    edi
  135.     pop    esi
  136.     pop    ds
  137.  
  138.     mov    esp,ebp
  139.     pop    ebp
  140.     ret
  141.  
  142. _unpack_poly_vertices    endp
  143.  
  144.  
  145.  
  146. ;/******** TEST ROUTINE CALLED WITH SCREEN VERTEX ARRAY ********/
  147.  
  148. ; given array of screen vertices (CCW)
  149. ; returns -1 for point not in poly, else 0
  150. ;
  151. ; int monitor_test_poly(int x, int y, int n, int *array)
  152.  
  153. x            equ    WORD PTR  [bp+8]          ; arguments
  154. y        equ    WORD PTR  [bp+10]
  155. n        equ    WORD PTR  [bp+12]
  156. array        equ    DWORD PTR [bp+14]
  157.  
  158. have_left   equ    WORD PTR [bp-2]      ; locals
  159. have_right  equ    WORD PTR [bp-4]
  160. result      equ    WORD PTR [bp-6]
  161.  
  162.     PUBLIC    _monitor_test_poly
  163.  
  164. _monitor_test_poly    proc    far
  165.  
  166.     .386
  167.     push    ebp
  168.     mov    ebp,esp
  169.     sub    esp,10
  170.  
  171.     push    esi
  172.     push    edi
  173.     push    ecx
  174.  
  175.     mov    have_left,0
  176.     mov    have_right,0
  177.     mov    result,-1    ; failure flag
  178.  
  179.     mov    cx,n        ; number of edges to test
  180.     dec    cx
  181.  
  182.     les    bx,array    ; pointer to vertex array
  183.     mov    si,bx
  184.     add    si,4         ; "next" for edge
  185.  
  186. tedge:                ; begin edge:
  187.     mov    ax,y        ; get test point
  188.     mov    dx,x
  189.  
  190.     cmp    ax,[bx+2]    ; check vertical
  191.     jle    above1
  192.     cmp    ax,[si+2]
  193.     jg    notine          ; not in edge: next slice
  194. above1:
  195.     cmp    ax,[bx+2]    ; continue test
  196.     jge    vertok
  197.     cmp    ax,[si+2]
  198.     jl    notine          ; no vertical edge match
  199.  
  200. vertok:
  201.     cmp    dx,[bx]        ; have vertical, test for inside
  202.     jg    rt1
  203.     cmp    dx,[si]
  204.     jle    left            ; to left of edge
  205.     jmp    allok
  206. rt1:
  207.     cmp    dx,[si]
  208.     jg    right           ; to right of edge
  209.  
  210. allok:
  211.     sub    ax,[bx+2]       ; y - y1    ; "within": compute intercept
  212.     mov    dx,[si]
  213.     sub    dx,[bx]         ; x2 - x1
  214.     imul    dx
  215.     mov    di,[si+2]
  216.     sub    di,[bx+2]       ; y2 - y1
  217.     je    pend        ; horizontal edge: always succeeds
  218.     idiv    di
  219.     add    ax,[bx]        ; find the x coord of y intercept
  220.     cmp    ax,x
  221.     jle    right        ; to right of point
  222.  
  223. left:
  224.     inc    WORD PTR have_left      ; to left: found a matching edge yet?
  225.     test    WORD PTR have_right,-1
  226.     jnz     inpoly
  227.     jmp    pend
  228.  
  229. right:
  230.     inc    WORD PTR have_right    ; same for left
  231.     test    WORD PTR have_left,-1
  232.     jnz     inpoly
  233.  
  234. notine:                  ; test of edge failed: next
  235. pend:
  236.     add    si,4
  237.     add    bx,4
  238.     sub    cx,1
  239.     jg    tedge        ; next edge
  240.     jl    no_match        ; done
  241.     mov    si,WORD PTR array   ; do last->first vertex edge
  242.     jmp     tedge
  243.  
  244. inpoly:            ; got it!
  245.     mov    result,0    ; mark within poly
  246.  
  247. no_match:
  248.     mov    ax,result
  249.  
  250.     pop    ecx
  251.     pop    edi
  252.     pop    esi
  253.  
  254.     mov    esp,ebp
  255.     pop    ebp
  256.     ret
  257.  
  258. _monitor_test_poly  endp
  259.  
  260.  
  261.     end
  262.