home *** CD-ROM | disk | FTP | other *** search
/ The Equalizer BBS / equalizer-bbs-collection_2004.zip / equalizer-bbs-collection / DEMOSCENE-STUFF / HQ_WATER.ZIP / SURF.ASM < prev    next >
Assembly Source File  |  1994-08-13  |  5KB  |  195 lines

  1. ;
  2. ;  SURF.ASM  //  ARM 12/93
  3. ;
  4.  
  5.         Ideal
  6.         Model TPascal
  7.         P286
  8.         Radix 10
  9.  
  10. ; Constants:
  11.  
  12. INCLUDE "meshsize.inc"
  13.  
  14.         DATASEG
  15.  
  16. EXTRN   U      : DWord
  17. EXTRN   B      : DWord
  18. EXTRN   CT     : Word
  19. EXTRN   TOPS   : Word
  20. EXTRN   DAMP   : Byte
  21.  
  22.         CODESEG
  23.  
  24. PUBLIC UpdateTable;
  25. PUBLIC DrawSurf;
  26.  
  27. ; // UpdateTable : performs one integration step on U[CT]
  28.  
  29. ; Differential equation is:  u  = a²( u  + u  )
  30. ;                             tt       xx   yy
  31. ;
  32. ; Where a² = tension * gravity / surface_density.
  33. ;
  34. ; Aproximating second derivatives by central differences:
  35. ;
  36. ;  [ u(t+1)-2u(t)+u(t-1) ] / δt² = a² (u(x+1)+u(x-1)+u(y+1)+u(y-1)-4u) / h²
  37. ;
  38. ; (where δt = time step, h=δx=δy = mesh resolution
  39. ;
  40. ; From where u(t+1) may be calculated as:
  41. ;                   ┌   1   ┐
  42. ; u(t+1) = a²δt²/h² │ 1 0 1 │u - u(t-1) + (2-4a²δt²/h²)u
  43. ;                   └   1   ┘
  44. ;
  45. ; When a²δt²/h² = ½ last term vanishes, giving:
  46. ;                   ┌   1   ┐
  47. ;        u(t+1) = ½ │ 1 0 1 │u - u(t-1)
  48. ;                   └   1   ┘
  49. ;
  50. ; This needs only 4 ADD/SUB and one SAR operation per mesh point!
  51. ;
  52. ; (note that u(t-1,x,y) is only used to calculate u(t+1,x,y) so
  53. ;  we can use the same array for both t-1 and t+1, needing only
  54. ;  two arrays, U[0] and U[1])
  55. ;
  56. ; Dampening is simulated by subtracting 1/2^n of result.
  57. ; n=4 gives best-looking result
  58. ; n<4 (eg 2 or 3) thicker consistency, waves die out immediately
  59. ; n>4 (eg 8 or 12) more fluid, waves die out slowly
  60. ;
  61.  
  62.        PROC UpdateTable NEAR
  63.        push bp
  64.          mov si, [word ptr U]
  65.          add si, NY*2 - 4
  66.          mov di, si
  67.          add si, NX*NY*2
  68.          test [CT], 1
  69.          jz @@0
  70.          xchg si,di
  71.       @@0:              ; si->next/last, di->this
  72.  
  73.          mov cl, [Damp]
  74.          mov dx, NX-2
  75.  
  76.          push ds
  77.          mov ax, [word ptr U+2]
  78.          mov ds, ax
  79.  
  80.    @@fori:
  81.          add si, 4
  82.          add di, 4
  83.          mov bp, NY-2
  84.       @@forj:
  85.           add si, 2
  86.           add di, 2             ; add up N,S,E,W points :
  87.           mov ax, [di-2*NY]     ; [   u(t,x-1,y)
  88.           add ax, [di+2*NY]     ;   + u(t,x+1,y)
  89.           add ax, [di-2]        ;   + u(t,x,y-1)
  90.           add ax, [di+2]        ;   + u(t,x,y+1) ]
  91.           sar ax,1              ; /2
  92.           sub ax, [si]          ; -u(t-1,x,y)
  93.           mov bx, ax
  94.           sar bx, cl            ; subtract 1/16 of result for dampening
  95.           sub ax, bx
  96.           mov [si],ax           ; save result
  97.           dec bp
  98.          jnz @@forj
  99.  
  100.          dec dx
  101.        jnz @@fori
  102.  
  103.        pop ds
  104.        pop bp
  105.        ret
  106.        ENDP
  107.  
  108.  
  109. ; // DrawSurf : draws water surface in fake 3D
  110. ;
  111.  
  112.        PROC DrawSurf NEAR
  113. P386
  114.         push bp
  115.  
  116.         mov si, [word ptr U]
  117.         test [CT], 1
  118.         jz @@0
  119.         add si, NX*NY*2
  120.       @@0:                        ; ds:si -> U[CT]
  121.  
  122.         mov ax, 0a000h
  123.         mov es, ax
  124.         mov di, 199*320           ; es:di -> Screen[199,0]
  125.  
  126.         mov bx, offset TOPS
  127.         mov cx, NX
  128.  
  129.         push gs fs ds
  130.         mov ax, ds
  131.         mov fs, ax
  132.         mov ax, [word ptr B+2]
  133.         mov gs, ax
  134.         mov ax, [word ptr U+2]
  135.         mov ds, ax
  136.  
  137.         @@forX:
  138.           push cx
  139.           push di
  140.           push bx
  141.  
  142.           mov bx,7                ; highest so far
  143.           mov bp, 200*(101h)
  144.           mov cx, NY
  145.           @@forY:
  146.             sub bx,8              ; each row is 8 (=1 pixel) higher
  147.             mov ax, [si]          ; then the last.
  148.             mov dx, ax
  149.             sub dx, bx
  150.             jng @@1               ; not above current highest
  151.              or ax, 0007          ; round to 7 + a multiple of 8
  152.              mov bx, ax
  153.                           ; dec dx/sar dx,3/.../dec dx/jge @2 (dx+1 times)
  154.              @@2:
  155.               mov [es:di],bp
  156.               sub di,320          ; point to next row, same column
  157.               sub dx,8
  158.               jg @@2      ; (you wouldn't believe how much
  159.                           ;  slower LOOP is on a 486!)
  160.            @@1:
  161.             add si,2              ; point to next column, U[CT,X,Y+1]
  162.             sub bp, 0101h         ; dim color one notch
  163.           dec cx
  164.           jnz @@forY      ; LOOPs suck!
  165.  
  166.           pop bx
  167.           mov ax, [fs:bx]         ; get last top of column address
  168.           mov [fs:bx], di         ; ...and save current one for next time
  169.           add bx,2                ; (update ptr for next col)
  170.  
  171.           @@3:
  172.           cmp ax, di
  173.           jae @@4
  174.             mov bp, [gs:di]
  175.             mov [es:di],bp        ; if this column is lower than the
  176.             sub di, 320           ; last one, clear extra rows w/ background
  177.             jmp @@3
  178.           @@4:
  179.  
  180.           pop di                  ; fetch addr of row 199
  181.           add di,2                ; skip to next column
  182.           pop cx                  ; fetch counter
  183.           dec cx
  184.           jnz @@forX      ; did I mention...?  ;-)
  185.  
  186.           pop ds fs gs
  187.           pop bp
  188. P286
  189.        ret
  190.        ENDP
  191.  
  192.  
  193.        END
  194.  
  195.