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

  1.     TITLE    HORMATH - HORIZON INTERCEPT 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.  
  32. /* Contact: dstampe@sunee.waterloo.edu */
  33.  
  34.         $
  35.  
  36.     .MODEL large
  37.     .386
  38.  
  39.     .DATA
  40.  
  41. include 3dstruct.inc
  42.  
  43.     .CODE RENDERER
  44.  
  45. ;/************ HORIZON IMPLEMENTATION MATH *****************/
  46.  
  47. ; computes if point on screen is above or below horizon
  48. ; used to determine how to draw horizon
  49. ;
  50. ;int above_horizon(long x, long y, VIEW *v, long offst)
  51.  
  52.  
  53. x    equ    [bp+8]          ; arguments
  54. y    equ    [bp+12]
  55. v    equ    [bp+16]
  56. offst    equ    [bp+20]
  57.  
  58. scx    equ    es:[bx].VP_scx    ; scaling from viewport coeffs
  59. scy    equ    es:[bx].VP_scy
  60. hsc    equ    es:[bx].VP_hsc    ; viewport screen center offsets
  61. vsc    equ    es:[bx].VP_vsc
  62.  
  63. B    equ    es:[bx].VP_xform01    ; viewport matrix elements
  64. E    equ    es:[bx].VP_xform11
  65. H    equ    es:[bx].VP_xform21
  66. orient    equ    es:[bx].VP_orientation    ; viewport flip flags
  67.  
  68.  
  69.     PUBLIC    _above_horizon
  70.  
  71. _above_horizon    proc    far
  72.  
  73.     .386
  74.     push    ebp
  75.     mov    ebp,esp
  76.  
  77.     push    ecx
  78.  
  79.     les    bx,DWORD PTR v
  80.  
  81.     mov    eax,x        ; (x-hsc)*B/scx
  82.     sub    eax,hsc
  83.     imul    DWORD PTR B
  84.     idiv    DWORD PTR scx
  85.     test    orient,XFLIP
  86.     je    pos_scx         ; flip sign if screen flipped
  87.     neg    eax
  88. pos_scx:
  89.     mov    ecx,eax
  90.  
  91.     mov    eax,y           ; -(y-vsc)*E/scy
  92.     neg    eax
  93.     add    eax,vsc
  94.     imul    DWORD PTR E
  95.     idiv    DWORD PTR scy
  96.     test    orient,YFLIP
  97.     je    pos_scy         ; flip sign if screen flipped
  98.     neg    eax
  99. pos_scy:
  100.     add    ecx,eax
  101.  
  102.     mov    eax,offst    ; -(H+offset)
  103.     shl    eax,16
  104.     add    eax, H          ; shift aligns terms
  105.     neg    eax
  106.     sar    eax,16
  107.     cmp    ecx,eax
  108.     mov    ax,0
  109.     jl    below
  110.     inc    ax
  111. below:            ; return 1 if above, 0 if below horizon
  112.     pop    ecx
  113.  
  114.     mov    esp,ebp
  115.     pop    ebp
  116.     ret
  117.  
  118. _above_horizon    endp
  119.  
  120.  
  121.  
  122. ; computes x-intercept of horizon with x side of screen
  123. ;
  124. ;long y_horizon(long x, VIEW *v, long offst)
  125.  
  126.  
  127. x    equ    [bp+8]          ; arguments
  128. v    equ    [bp+12]
  129. offst    equ    [bp+16]
  130.  
  131. scx    equ    es:[bx].VP_scx    ; scaling from viewport coeffs
  132. scy    equ    es:[bx].VP_scy
  133. hsc    equ    es:[bx].VP_hsc    ; viewport screen center offsets
  134. vsc    equ    es:[bx].VP_vsc
  135.  
  136. B    equ    es:[bx].VP_xform01    ; viewport matrix elements
  137. E    equ    es:[bx].VP_xform11
  138. H    equ    es:[bx].VP_xform21
  139. orient    equ    es:[bx].VP_orientation    ; viewport flip flags
  140.  
  141.  
  142.     PUBLIC    _y_horizon
  143.  
  144. _y_horizon    proc    far
  145.  
  146.     .386
  147.     push    ebp
  148.     mov    ebp,esp
  149.  
  150.     les    bx,DWORD PTR v
  151.  
  152.     mov    eax,x        ; (x-hsc)*B/scx
  153.     sub    eax,hsc
  154.     imul    DWORD PTR B
  155.     idiv    DWORD PTR scx
  156.     test    orient,XFLIP
  157.     je    posy_scx         ; flip sign if screen flipped
  158.     neg    eax
  159. posy_scx:
  160.     mov    edx,offst    ; -(H+offset)
  161.     shl    edx,16
  162.     add    edx, H          ; shift aligns terms
  163.     sar    edx,16
  164.     add    eax,edx         ; add in
  165.     neg    eax
  166.  
  167.     imul    DWORD PTR scy   ; vertical scaling
  168.     idiv    DWORD PTR E
  169.     test    orient,YFLIP
  170.     je    posy_scy         ; flip sign if screen flipped
  171.     neg    eax
  172. posy_scy:
  173.     neg    eax
  174.     add    eax,vsc
  175.     shld    edx,eax,16    ; result returned in both eax and dx:ax
  176.  
  177.     mov    esp,ebp
  178.     pop    ebp
  179.     ret
  180.  
  181. _y_horizon    endp
  182.  
  183.  
  184.  
  185.  
  186. ; computes y-intercept of horizon with y top/bot of screen
  187. ;
  188. ;long x_horizon(long y, VIEW *v, long offst)
  189.  
  190.  
  191. y    equ    [bp+8]          ; arguments
  192. v    equ    [bp+12]
  193. offst    equ    [bp+16]
  194.  
  195. scx    equ    es:[bx].VP_scx    ; scaling from viewport coeffs
  196. scy    equ    es:[bx].VP_scy
  197. hsc    equ    es:[bx].VP_hsc    ; viewport screen center offsets
  198. vsc    equ    es:[bx].VP_vsc
  199.  
  200. B    equ    es:[bx].VP_xform01    ; viewport matrix elements
  201. E    equ    es:[bx].VP_xform11
  202. H    equ    es:[bx].VP_xform21
  203. orient    equ    es:[bx].VP_orientation    ; viewport flip flags
  204.  
  205.  
  206.     PUBLIC    _x_horizon
  207.  
  208. _x_horizon    proc    far
  209.  
  210.     .386
  211.     push    ebp
  212.     mov    ebp,esp
  213.  
  214.     les    bx,DWORD PTR v
  215.  
  216.     mov    eax,y        ; -(y-vsc)*E/scy
  217.     sub    eax,vsc
  218.     neg    eax
  219.     imul    DWORD PTR E
  220.     idiv    DWORD PTR scy
  221.     test    orient,YFLIP
  222.     je    posx_scy         ; flip sign if screen flipped
  223.     neg    eax
  224. posx_scy:
  225.     mov    edx,offst    ; -(H+offset)
  226.     shl    edx,16
  227.     add    edx, H          ; shift aligns terms
  228.     sar    edx,16
  229.     add    eax,edx         ; add in
  230.     neg    eax
  231.  
  232.     imul    DWORD PTR scx   ; vertical scaling
  233.     idiv    DWORD PTR B
  234.     test    orient,XFLIP
  235.     je    posx_scx         ; flip sign if screen flipped
  236.     neg    eax
  237. posx_scx:
  238.     add    eax,hsc
  239.     shld    edx,eax,16    ; result returned in both eax and dx:ax
  240.  
  241.     mov    esp,ebp
  242.     pop    ebp
  243.     ret
  244.  
  245. _x_horizon    endp
  246.  
  247.  
  248.  
  249.     end