home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / VGA_VUL4.ZIP / 3D.ASM next >
Assembly Source File  |  1995-09-10  |  14KB  |  429 lines

  1. ;==============================================================================;
  2. ;                                                                              ;
  3. ;   Assembler Program By Vulture.                                              ;
  4. ;   3D-system example. Use the following formulas to rotate a point:           ;
  5. ;                                                                              ;
  6. ;        Rotate around x-axis                                                  ;
  7. ;        YT = Y * COS(xang) - Z * SIN(xang) / 256                              ;
  8. ;        ZT = Y * SIN(xang) + Z * COS(xang) / 256                              ;
  9. ;        Y = YT                                                                ;
  10. ;        Z = ZT                                                                ;
  11. ;                                                                              ;
  12. ;        Rotate around y-axis                                                  ;
  13. ;        XT = X * COS(yang) - Z * SIN(yang) / 256                              ;
  14. ;        ZT = X * SIN(yang) + Z * COS(yang) / 256                              ;
  15. ;        X = XT                                                                ;
  16. ;        Z = ZT                                                                ;
  17. ;                                                                              ;
  18. ;        Rotate around z-axis                                                  ;
  19. ;        XT = X * COS(zang) - Y * SIN(zang) / 256                              ;
  20. ;        YT = X * SIN(zang) + Y * COS(zang) / 256                              ;
  21. ;        X = XT                                                                ;
  22. ;        Y = YT                                                                ;
  23. ;                                                                              ;
  24. ;   Divide by 256 coz we have multiplyd our sin values with 256 too.           ;
  25. ;   This example isn't too fast right now but it'll work just fine.            ;
  26. ;                                                                              ;
  27. ;       Current Date: 6-9-95         Vulture                                   ;
  28. ;                                                                              ;
  29. ;==============================================================================;
  30.  
  31. IDEAL                           ; Ideal mode
  32. P386                            ; Allow 80386 instructions
  33. JUMPS                           ; Tasm handles out of range jumps (rulez!:))
  34.  
  35. SEGMENT CODE                    ; Code segment starts
  36. ASSUME cs:code,ds:code          ; Let cs and ds point to code segment
  37.  
  38. ORG 100h                        ; Make a .COM file
  39.  
  40. START:                          ; Main program
  41.  
  42.     mov     ax,0013h            ; Init vga
  43.     int     10h
  44.  
  45.     mov     ax,cs
  46.     mov     ds,ax               ; ds points to codesegment
  47.     mov     ax,0a000h
  48.     mov     es,ax               ; es points to vga
  49.  
  50.     lea     si,[Palette]        ; Set palette
  51.     mov     dx,3c8h
  52.     xor     al,al
  53.     out     dx,al
  54.     mov     dx,3c9h
  55.     mov     cx,189*3
  56.     repz    outsb
  57.  
  58. ; === Set some variables ===
  59.     mov     [DeltaX],1          ; Initial speed of rotation
  60.     mov     [DeltaY],1          ; Change this and watch what
  61.     mov     [DeltaZ],1          ; happens. It's fun!
  62.  
  63.     mov     [Xoff],256
  64.     mov     [Yoff],256          ; Used for calculating vga-pos
  65.     mov     [Zoff],300          ; Distance from viewer
  66.  
  67. MainLoop:
  68.     call    MainProgram         ; Yep... do it all... ;-)
  69.  
  70.     in      al,60h              ; Scan keyboard
  71.     cmp     al,1                ; Test on ESCAPE
  72.     jne     MainLoop            ; Continue if not keypressed
  73.  
  74. ; === Quit to DOS ===
  75.     mov     ax,0003h            ; Back to textmode
  76.     int     10h
  77.     lea     dx,[Credits]
  78.     mov     ah,9
  79.     int     21h
  80.     mov     ax,4c00h            ; Return control to DOS
  81.     int     21h                 ; Call DOS interrupt
  82.  
  83. ; === Sub-routines ===
  84.  
  85. PROC WaitVrt                    ; Waits for vertical retrace to reduce "snow"
  86.     mov     dx,3dah
  87. Vrt:
  88.     in      al,dx
  89.     test    al,8
  90.     jnz     Vrt                 ; Wait until Verticle Retrace starts
  91. NoVrt:
  92.     in      al,dx
  93.     test    al,8
  94.     jz      NoVrt               ; Wait until Verticle Retrace ends
  95.     ret                         ; Return to main program
  96. ENDP WaitVrt
  97.  
  98. PROC UpdateAngles
  99. ; Calculates new x,y,z angles
  100. ; to rotate around
  101.     mov     ax,[XAngle]         ; Load current angles
  102.     mov     bx,[YAngle]
  103.     mov     cx,[ZAngle]
  104.  
  105.     add     ax,[DeltaX]         ; Add velocity
  106.     and     ax,11111111b        ; Range from 0..255
  107.     mov     [XAngle],ax         ; Update X
  108.     add     bx,[DeltaY]         ; Add velocity
  109.     and     bx,11111111b        ; Range from 0..255
  110.     mov     [YAngle],bx         ; Update Y
  111.     add     cx,[DeltaZ]         ; Add velocity
  112.     and     cx,11111111b        ; Range from 0..255
  113.     mov     [ZAngle],cx         ; Update Z
  114.     ret
  115. ENDP UpdateAngles
  116.  
  117. PROC GetSinCos
  118. ; Needed : bx=angle (0..255)
  119. ; Returns: ax=Sin   bx=Cos
  120.     push    bx                  ; Save angle (use as pointer)
  121.     shl     bx,1                ; Grab a word so bx=bx*2
  122.     mov     ax,[SinCos + bx]    ; Get sine
  123.     pop     bx                  ; Restore pointer into bx
  124.     push    ax                  ; Save sine on stack
  125.     add     bx,64               ; Add 64 to get cosine
  126.     and     bx,11111111b        ; Range from 0..255
  127.     shl     bx,1                ; *2 coz it's a word
  128.     mov     ax,[SinCos + bx]    ; Get cosine
  129.     mov     bx,ax               ; Save it   bx=Cos
  130.     pop     ax                  ; Restore   ax=Sin
  131.     ret
  132. ENDP GetSinCos
  133.  
  134. PROC SetRotation
  135. ; Set sine & cosine of x,y,z
  136.     mov     bx,[XAngle]         ; Grab angle
  137.     call    GetSinCos           ; Get the sine&cosine
  138.     mov     [Xsin],ax           ; Save sin
  139.     mov     [Xcos],bx           ; Save cos
  140.  
  141.     mov     bx,[Yangle]
  142.     call    GetSinCos
  143.     mov     [Ysin],ax
  144.     mov     [Ycos],bx
  145.  
  146.     mov     bx,[Zangle]
  147.     call    GetSinCos
  148.     mov     [Zsin],ax
  149.     mov     [Zcos],bx
  150.     ret
  151. ENDP SetRotation
  152.  
  153. PROC RotatePoint            ; Rotates the point around x,y,z
  154. ; Gets original x,y,z values
  155. ; This can be done elsewhere
  156.     movsx   ax,[Cube+si]    ; si = X        (movsx coz of byte)
  157.     mov     [X],ax
  158.     movsx   ax,[Cube+si+1]  ; si+1 = Y
  159.     mov     [Y],ax
  160.     movsx   ax,[Cube+si+2]  ; si+2 = Z
  161.     mov     [Z],ax
  162.  
  163. ; Rotate around x-axis
  164. ; YT = Y * COS(xang) - Z * SIN(xang) / 256
  165. ; ZT = Y * SIN(xang) + Z * COS(xang) / 256
  166. ; Y = YT
  167. ; Z = ZT
  168.  
  169.     mov     ax,[Y]
  170.     mov     bx,[XCos]
  171.     imul    bx               ; ax = Y * Cos(xang)
  172.     mov     bp,ax
  173.     mov     ax,[Z]
  174.     mov     bx,[XSin]
  175.     imul    bx               ; ax = Z * Sin(xang)
  176.     sub     bp,ax            ; bp = Y * Cos(xang) - Z * Sin(xang)
  177.     sar     bp,8             ; bp = Y * Cos(xang) - Z * Sin(xang) / 256
  178.     mov     [Yt],bp
  179.  
  180.     mov     ax,[Y]
  181.     mov     bx,[XSin]
  182.     imul    bx               ; ax = Y * Sin(xang)
  183.     mov     bp,ax
  184.     mov     ax,[Z]
  185.     mov     bx,[XCos]
  186.     imul    bx               ; ax = Z * Cos(xang)
  187.     add     bp,ax            ; bp = Y * SIN(xang) + Z * COS(xang)
  188.     sar     bp,8             ; bp = Y * SIN(xang) + Z * COS(xang) / 256
  189.     mov     [Zt],bp
  190.  
  191.     mov     ax,[Yt]          ; Switch values
  192.     mov     [Y],ax
  193.     mov     ax,[Zt]
  194.     mov     [Z],ax
  195.  
  196. ; Rotate around y-axis
  197. ; XT = X * COS(yang) - Z * SIN(yang) / 256
  198. ; ZT = X * SIN(yang) + Z * COS(yang) / 256
  199. ; X = XT
  200. ; Z = ZT
  201.  
  202.     mov     ax,[X]
  203.     mov     bx,[YCos]
  204.     imul    bx               ; ax = X * Cos(yang)
  205.     mov     bp,ax
  206.     mov     ax,[Z]
  207.     mov     bx,[YSin]
  208.     imul    bx               ; ax = Z * Sin(yang)
  209.     sub     bp,ax            ; bp = X * Cos(yang) - Z * Sin(yang)
  210.     sar     bp,8             ; bp = X * Cos(yang) - Z * Sin(yang) / 256
  211.     mov     [Xt],bp
  212.  
  213.     mov     ax,[X]
  214.     mov     bx,[YSin]
  215.     imul    bx               ; ax = X * Sin(yang)
  216.     mov     bp,ax
  217.     mov     ax,[Z]
  218.     mov     bx,[YCos]
  219.     imul    bx               ; ax = Z * Cos(yang)
  220.     add     bp,ax            ; bp = X * SIN(yang) + Z * COS(yang)
  221.     sar     bp,8             ; bp = X * SIN(yang) + Z * COS(yang) / 256
  222.     mov     [Zt],bp
  223.  
  224.     mov     ax,[Xt]          ; Switch values
  225.     mov     [X],ax
  226.     mov     ax,[Zt]
  227.     mov     [Z],ax
  228.  
  229. ; Rotate around z-axis
  230. ; XT = X * COS(zang) - Y * SIN(zang) / 256
  231. ; YT = X * SIN(zang) + Y * COS(zang) / 256
  232. ; X = XT
  233. ; Y = YT
  234.  
  235.     mov     ax,[X]
  236.     mov     bx,[ZCos]
  237.     imul    bx               ; ax = X * Cos(zang)
  238.     mov     bp,ax
  239.     mov     ax,[Y]
  240.     mov     bx,[ZSin]
  241.     imul    bx               ; ax = Y * Sin(zang)
  242.     sub     bp,ax            ; bp = X * Cos(zang) - Y * Sin(zang)
  243.     sar     bp,8             ; bp = X * Cos(zang) - Y * Sin(zang) / 256
  244.     mov     [Xt],bp
  245.  
  246.     mov     ax,[X]
  247.     mov     bx,[ZSin]
  248.     imul    bx               ; ax = X * Sin(zang)
  249.     mov     bp,ax
  250.     mov     ax,[Y]
  251.     mov     bx,[ZCos]
  252.     imul    bx               ; ax = Y * Cos(zang)
  253.     add     bp,ax            ; bp = X * SIN(zang) + Y * COS(zang)
  254.     sar     bp,8             ; bp = X * SIN(zang) + Y * COS(zang) / 256
  255.     mov     [Yt],bp
  256.  
  257.     mov     ax,[Xt]          ; Switch values
  258.     mov     [X],ax
  259.     mov     ax,[Yt]
  260.     mov     [Y],ax
  261.  
  262.     ret
  263. ENDP RotatePoint
  264.  
  265. PROC ShowPoint
  266. ; Calculates screenposition and
  267. ; plots the point on the screen
  268.     mov     ax,[Xoff]           ; Xoff*X / Z+Zoff = screen x
  269.     mov     bx,[X]
  270.     imul    bx
  271.     mov     bx,[Z]
  272.     add     bx,[Zoff]           ; Distance
  273.     idiv    bx
  274.     add     ax,[Mx]             ; Center on screen
  275.     mov     bp,ax
  276.  
  277.     mov     ax,[Yoff]           ; Yoff*Y / Z+Zoff = screen y
  278.     mov     bx,[Y]
  279.     imul    bx
  280.     mov     bx,[Z]
  281.     add     bx,[Zoff]           ; Distance
  282.     idiv    bx
  283.     add     ax,[My]             ; Center on screen
  284.  
  285.     mov     bx,320
  286.     imul    bx
  287.     add     ax,bp               ; ax = (y*320)+x
  288.     mov     di,ax
  289.  
  290.     mov     ax,[Z]              ; Get color from Z
  291.     add     ax,100d             ; (This piece of code could be improved)
  292.  
  293.     mov     [byte ptr es:di],al ; Place a dot with color al
  294.     mov     [Erase+si],di       ; Save position for erase
  295.     ret
  296. ENDP ShowPoint
  297.  
  298. PROC MainProgram
  299.     call    UpdateAngles        ; Calculate new angles
  300.     call    SetRotation         ; Find sine & cosine of those angles
  301.  
  302.     xor     si,si               ; First 3d-point
  303.     mov     cx,MaxPoints
  304. ShowLoop:
  305.     call    RotatePoint         ; Rotates the point using above formulas
  306.     call    ShowPoint           ; Shows the point
  307.     add     si,3                ; Next 3d-point
  308.     loop    ShowLoop
  309.  
  310.     call    WaitVrt             ; Wait for retrace
  311.  
  312.     xor     si,si               ; Starting with point 0
  313.     xor     al,al               ; Color = 0 = black
  314.     mov     cx,MaxPoints
  315. Deletion:
  316.     mov     di,[Erase+si]       ; di = vgapos old point
  317.     mov     [byte ptr es:di],al ; Delete it
  318.     add     si,3                ; Next point
  319.     loop    Deletion
  320.     ret
  321. ENDP MainProgram
  322.  
  323. ; === DATA ===
  324.  
  325. Credits   DB   13,10,"Code by Vulture / Outlaw Triad",13,10,"$"
  326.  
  327. Label SinCos Word       ; 256 values
  328. dw 0,6,13,19,25,31,38,44,50,56
  329. dw 62,68,74,80,86,92,98,104,109,115
  330. dw 121,126,132,137,142,147,152,157,162,167
  331. dw 172,177,181,185,190,194,198,202,206,209
  332. dw 213,216,220,223,226,229,231,234,237,239
  333. dw 241,243,245,247,248,250,251,252,253,254
  334. dw 255,255,256,256,256,256,256,255,255,254
  335. dw 253,252,251,250,248,247,245,243,241,239
  336. dw 237,234,231,229,226,223,220,216,213,209
  337. dw 206,202,198,194,190,185,181,177,172,167
  338. dw 162,157,152,147,142,137,132,126,121,115
  339. dw 109,104,98,92,86,80,74,68,62,56
  340. dw 50,44,38,31,25,19,13,6,0,-6
  341. dw -13,-19,-25,-31,-38,-44,-50,-56,-62,-68
  342. dw -74,-80,-86,-92,-98,-104,-109,-115,-121,-126
  343. dw -132,-137,-142,-147,-152,-157,-162,-167,-172,-177
  344. dw -181,-185,-190,-194,-198,-202,-206,-209,-213,-216
  345. dw -220,-223,-226,-229,-231,-234,-237,-239,-241,-243
  346. dw -245,-247,-248,-250,-251,-252,-253,-254,-255,-255
  347. dw -256,-256,-256,-256,-256,-255,-255,-254,-253,-252
  348. dw -251,-250,-248,-247,-245,-243,-241,-239,-237,-234
  349. dw -231,-229,-226,-223,-220,-216,-213,-209,-206,-202
  350. dw -198,-194,-190,-185,-181,-177,-172,-167,-162,-157
  351. dw -152,-147,-142,-137,-132,-126,-121,-115,-109,-104
  352. dw -98,-92,-86,-80,-74,-68,-62,-56,-50,-44
  353. dw -38,-31,-25,-19,-13,-6
  354.  
  355. Label Cube Byte           ; The 3d points
  356.        c = -35            ; 5x*5y*5z (=125) points
  357.        rept 5
  358.          b = -35
  359.          rept 5
  360.            a = -35
  361.            rept 5
  362.              db a,b,c
  363.              a = a + 20
  364.            endm
  365.            b = b + 20
  366.          endm
  367.          c = c + 20
  368.        endm
  369.  
  370. Label Palette Byte              ; The palette to use
  371.        db 0,0,0                 ; 63*3 gray-tint
  372.        d = 63
  373.        rept 63
  374.          db d,d,d
  375.          db d,d,d
  376.          db d,d,d
  377.          d = d - 1
  378.        endm
  379.  
  380. X      DW ?             ; X variable for formula
  381. Y      DW ?
  382. Z      DW ?
  383.  
  384. Xt     DW ?             ; Temporary variable for x
  385. Yt     DW ?
  386. Zt     DW ?
  387.  
  388. XAngle DW 0             ; Angle to rotate around x
  389. YAngle DW 0
  390. ZAngle DW 0
  391.  
  392. DeltaX DW ?             ; Amound Xangle is increased each time
  393. DeltaY DW ?
  394. DeltaZ DW ?
  395.  
  396. Xoff   DW ?
  397. Yoff   DW ?
  398. Zoff   DW ?             ; Distance from viewer
  399.  
  400. XSin   DW ?             ; Sine and cosine of angle to rotate around
  401. XCos   DW ?
  402. YSin   DW ?
  403. YCos   DW ?
  404. ZSin   DW ?
  405. ZCos   DW ?
  406.  
  407. Mx     DW 160            ; Middle of the screen
  408. My     DW 100
  409.  
  410. MaxPoints EQU 125        ; Number of 3d Points
  411.  
  412. Erase  DW MaxPoints DUP (?)     ; Array for deletion screenpoints
  413.  
  414. ENDS CODE                       ; End of codesegment
  415. END START                       ; The definite end.... :)
  416.  
  417.  
  418.  
  419.  
  420. ; You may use this code in your own productions but
  421. ; give credit where credit is due. Only lamers steal
  422. ; code so try to create your own 3d-engine and use
  423. ; this code as an example.
  424. ; Thanx must go to Arno Brouwer and Ash for releasing
  425. ; example sources.
  426. ;
  427. ;    Ciao dudoz,
  428. ;
  429. ;         Vulture / Outlaw Triad