home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / microcrn / issue_42.arc / RAYS42.ASM < prev   
Assembly Source File  |  1988-05-20  |  6KB  |  217 lines

  1. ;Support code for Ray Tracing article in Micro C issue #42 by Earl Hinrichs
  2.  
  3. ; Listing 1
  4.  
  5.   .text
  6. PointOnLine:
  7. ;In:    a8 = Ray
  8. ;       a9 = Time parameter
  9. ;       a10 = Point
  10.   move  *a8(rdx), a0, 1         ;fetch x direction
  11.   move  a9, a1                  ;scale by time parameter
  12.   calla fxMultiply
  13.   move  *a8(rox), a1, 1         ;add x starting value
  14.   add   a1, a0
  15.   move  a0, *a10(px), 1         ;store result
  16.   move  *a8(rdy), a0, 1         ;repeat for y
  17.   move  a9, a1
  18.   calla fxMultiply
  19.   move  *a8(roy), a1, 1
  20.   add   a1, a0
  21.   move  a0, *a10(py), 1
  22.   move  *a8(rdz), a0, 1         ;and for z
  23.   move  a9, a1
  24.   calla fxMultiply
  25.   move  *a8(roz), a1, 1
  26.   add   a1, a0
  27.   move  a0, *a10(pz), 1
  28.   rets
  29.  
  30.  
  31.  
  32.  
  33.  
  34. ;Listing 2 **********/
  35.  
  36. intLinePlane:
  37. ;In     a8 = Ray
  38. ;       a9 = Plane
  39. ;Out:   a0 = intersection parameter
  40. ;       a0 <= 0 if no intersection
  41.   mmtm  sp, a4, a5, a6, a7, a8, a9
  42.   calla vDot            ;ro.pn (ray_origin dot plane normal)
  43.   move  a0, a4
  44.   addi  rdx, a8
  45.   calla vDot            ;rd.pn (ray direction dot plane normal)
  46.   move  a0, a1
  47.   move  *a9(pc), a0, 1
  48.   sub   a4, a0
  49.   calla fxDivide
  50.   mmfm  sp, a4, a5, a6, a7, a8, a9
  51.   rets                  ;(pc-ro.pn)/(rd.pn)
  52.  
  53.  
  54.  
  55.  
  56.  
  57. ;Listing 3
  58.  
  59. intLineRectangle:
  60. ;In:    a8 = ray
  61. ;       a9 = rectangle data
  62.   mmtm  sp, a4, a5, a8, a9, a10
  63.   move  a9, a5          ;save rectangle pointer
  64.   calla intLinePlane    ;Test for intesection with embedding plane
  65.   btst  31, a0
  66.   jrnz  ir_01           ;if not then quit
  67.   move  a0, a4          ;Save intersection time
  68.   move  a0, a9          ;a8 = ray, a9 = time
  69.   movi  vTemp0, a10     ;a10 will hold point of intersection.
  70.   calla PointOnLine
  71.   move  a10, a8         ;Intersection point
  72.   move  a5, a9
  73.   addi  60H, a9         ;minus origin
  74.   calla vSub            ;to temp.
  75.   addi  80H, a9         ;point to X extent
  76.   calla vCalibrate      ;project temp onto X extent
  77.   btst  31, a0          ;miss if negative
  78.   jrnz  ir_01
  79.   movi  One, a1         ;or greater than one
  80.   cmp   a1, a0
  81.   jrnc  ir_01
  82.   addi  >60, a9         ;point to Y extent
  83.   calla vCalibrate      ;test projection onto Y side
  84.   btst  31, a0          ;miss if negative
  85.   jrnz  ir_01
  86.   movi  One, a1         ;or if greater than 1
  87.   cmp   a1, a0
  88.   jrnc  ir_01
  89.   move  a4, a0          ;return time of intersection
  90.   jruc  ir_00
  91. ir_01:
  92.   movi  -1, a0          ;return negative number if miss.
  93. ir_00:
  94.   mmfm  sp, a4, a8, a9, a10
  95.   rets
  96.  
  97.  
  98.  
  99.  
  100.  
  101. ;Listing 4
  102.  
  103. intLineSphere:
  104. ;In     a8 = Ray
  105. ;       a9 = Sphere
  106. ;Out:   a0 = intersection parameter
  107. ;       a0 <= 0 if no intersection
  108.   mmtm  sp, a4, a5, a6, a7, a8, a9, a10
  109.   move  a8, a4
  110.   move  a9, a5
  111.   movi  vTemp0, a10
  112.   calla vSub            ;temp0 = ro-sc
  113.   addi  rdx, a8
  114.   move  a8, a9
  115.   calla vDot            ;rd.rd
  116.   move  a0, a6          ;'a' in quadratic equation
  117.   move  a10, a9 
  118.   calla vDot
  119.   add   a0, a0
  120.   move  a6, a1
  121.   calla fxDivide
  122.   move  a0, a7          ;'b' in quadratic equation
  123.   move  a10, a8
  124.   calla vDot
  125.   move  *a5(sr2), a1, 1 ;radius squared
  126.   sub   a1, a0          ;'c' in quadratic equation
  127.   move  a6, a1
  128.   calla fxDivide
  129.   sll   2, a0           ;4ac
  130.   move  a0, a6
  131.   move  a7, a0
  132.   move  a7, a1
  133.   calla fxMultiply      ;b^2
  134.   sub   a6, a0          ;b^2-4ac
  135.   jrlt  is_00           ;no intersection if negative.
  136.   calla fxSqRoot        ;sqrt(b^2-4ac)
  137.   move  a7, a1          ;b
  138.   neg   a1              ;-b
  139.   sub   a0, a1          ;-b-sqrt(b^2-4ac)
  140.   sra   1, a1           ;(-b-sqrt(b^2-4ac))/2
  141.   cmpi SphereMinTime, a1 ;test with room to get started.
  142.   jrgt  is_01           ;if positive then is first intersection.
  143.   add   a0, a1          ;(-b+sqrt(b^2-4ac))/2
  144.                         ;if positive then it is the intersection, if negative
  145.                         ;then there is no intersection. In either case value
  146.                         ;is returned as is.
  147. is_01:
  148.   move  a1, a0
  149. is_00:
  150.   mmfm  sp, a4, a5, a6, a7, a8, a9, a10
  151.   rets
  152.  
  153.  
  154.  
  155.  
  156.  
  157. ;Listing 5
  158.  
  159. refLinePlane:
  160. ;In:    a8 = Ray
  161. ;       a9 = Plane
  162. ;       a10 = intersection (point-on-line parameter)
  163. ;       a11 = reflection
  164.   mmtm  sp, a4, a5, a6, a7, a8, a9, a10, a11
  165.   move  a8, a4          ;Line
  166.   move  a9, a5          ;Plane
  167.   move  a10, a9         ;point distance
  168.   move  a11, a10        ;build reflective line here
  169.   calla PointOnLine     ;origin.
  170. lp_01:
  171.   addi  rdx, a4         ;line direction
  172.   move  a4, a8
  173.   move  a5, a9          ;plane normal
  174.   calla vDot            ;ld.pn
  175.   sll   1, a0           ;2*ld.pn
  176.   move  a0, a7
  177.   move  a5, a8          ;pn
  178.   calla vDot            ;pn.pn
  179.   move  a0, a1
  180.   move  a7, a0
  181.   calla fxDivide        ;2*ld.pn/(pn.pn)
  182.   move  a0, a9
  183.   movi  vTemp0, a10
  184.   calla vScale          ;(2*ld.pn/(pn.pn))pn
  185.   move  a4, a8          ;ld
  186.   move  a10, a9
  187.   move  a11, a10
  188.   addi  rdx, a10        ;reflection direction
  189.   calla vSub            ;ld-(2*ld.pn/(pn.pn))pn
  190.   mmfm  sp, a4, a5, a6, a7, a8, a9, a10, a11
  191.   rets
  192.  
  193.  
  194.  
  195.  
  196.  
  197. ;Listing 6
  198.  
  199. refLineSphere:
  200. ;In:    a8 = Line
  201. ;       a9 = Sphere
  202. ;       a10 = intersection (point-on-line parameter)
  203. ;       a11 = reflection
  204.   mmtm  sp, a4, a5, a6, a7, a8, a9, a10, a11
  205.   move  a8, a4          ;Line
  206.   move  a9, a5          ;Sphere
  207.   move  a10, a9         ;point distance
  208.   move  a11, a10        ;build reflective line here
  209.   calla PointOnLine     ;origin.
  210.   move  a10, a8         ;create tangent plane,
  211.   move  a5, a9          ;normal = intersection point - sphere center
  212.   movi  vTemp1, a10
  213.   calla vSub
  214.   move  a10, a5         ;only need normal
  215.   jruc  lp_01           ;continue in line / plane intersection.
  216.  
  217.