Assembly source code of a rotating 3D box (animation)

Source: Internet
Author: User
Tags cos range sin
This procedure by foreign vulture eldest brother to write, and published the source code, this is his 95 years of a work, can say was very successful at that time!
This program is clever use of the constant changes in the coordinates, so as to achieve by the stars of the box 3D rotation!
, in order to respect the copyright, I did not translate the source code annotation, this can also let the domestic assembly enthusiasts themselves pondering foreign assembly programming thinking!
; Compilation method: 1 Tasm 3d.asm
; 2 Tlink 3d.obj
; 3 Exe2bin 3d.exe 3d.com
This procedure is a very classic 3D small animation that the stationmaster collects carefully. Webmaster's x86 assembly station: Http://www.x86asm.com
; Permanent Domain name: http://x86asm.yeah.net
;==============================================================================;
; ;
;                       Assembler by Vulture. ;
; 3d-system example. Use the following formulas to rotate a point:;
; ;
; Rotate around x-axis;
; YT = Y * COS (Xang)-Z * SIN (Xang)/256;
; ZT = Y * SIN (Xang) + Z * COS (Xang)/256;
; Y = YT;
; Z = ZT;
; ;
; Rotate around Y-axis;
; XT = X * COS (Yang)-Z * SIN (yang)/256;
; ZT = X * SIN (yang) + Z * COS (yang)/256;
; X = XT;
; Z = ZT;
; ;
; Rotate around Z-axis;
; XT = X * COS (Zang)-Y * SIN (Zang)/256;
; YT = X * SIN (Zang) + Y * COS (Zang)/256;
; X = XT;
; Y = YT;
; ;
;      Divide by 256 coz we have multiplyd our sin values with 256 too. ;
;      This example isn ' t too fast right now but it ' ll work just. ;
; ;
; Current date:6-9-95 Vulture;
; ;
;==============================================================================;
IDEAL; Ideal mode
P386; Allow 80386 Instructions
jumps; TASM handles out of range jumps (rulez!:))
           
SEGMENT CODE; Code segment starts
Assume Cs:code,ds:code; Let Cs and DS point to code Segment
ORG 100h; Make a. COM file
START:; Main Program
MOV ax,0013h; Init VGA
int 10h
     
MOV Ax,cs
MOV ds,ax; DS points to Codesegment
MOV ax,0a000h
MOV es,ax; Es points to VGA
Lea Si,[palette]; Set Palette
MOV dx,3c8h
XOR Al,al
Out Dx,al
MOV dx,3c9h
MOV cx,189*3
RepZ OUTSB
; = = = Set some variables = =
mov [deltax],1; Initial Speed of rotation
mov [deltay],1; Change this and watch what
mov [deltaz],1; Happens. It ' s fun!
mov [xoff],256
mov [yoff],256; Used for calculating Vga-pos
mov [zoff],300; Distance from Viewer
Mainloop:
Call Mainprogram; Yep ... do it all ...;-)
In al,60h; Scan keyboard
CMP al,1; Test on ESCAPE
Jne Mainloop; Continue If not keypressed
; = = = Quit to DOS = =
MOV ax,0003h; Back to TextMode
int 10h
Lea Dx,[credits]
MOV ah,9
int 21h
MOV ax,4c00h; return control to DOS
int 21h; Call DOS Interrupt
; = = = Sub-routines = =
     
PROC WAITVRT; Waits for vertical retrace to reduce "snow"
MOV Dx,3dah
VRT:
In AL,DX
Test al,8
JNZ Vrt; Wait until Verticle retrace starts
NOVRT:
In AL,DX
Test al,8
JZ NOVRT; Wait until verticle retrace ends
RET; Return to Main program
ENDP WAITVRT
PROC Updateangles
; Calculates new x,y,z angles
; To rotate around
mov Ax,[xangle]; Load Current Angles
MOV Bx,[yangle]
MOV Cx,[zangle]
     
Add Ax,[deltax]; ADD Velocity
and ax,11111111b; Range from 0..255
mov [Xangle],ax; Update X
Add Bx,[deltay]; ADD Velocity
and bx,11111111b; Range from 0..255
mov [YANGLE],BX; Update Y
Add Cx,[deltaz]; ADD Velocity
and cx,11111111b; Range from 0..255
mov [ZANGLE],CX; Update Z
Ret
ENDP Updateangles
PROC Getsincos
; Needed:bx=angle (0..255)
; Returns:ax=sin Bx=cos
Push BX; Save angle (use as pointer)
SHL bx,1; Grab a word so bx=bx*2
MOV Ax,[sincos + bx]; Get sine
Pop bx; Restore pointer into BX
Push ax; Save sine on stack
Add bx,64; Add to get cosine
and bx,11111111b; Range from 0..255
SHL bx,1; *2 coz it ' s a word
MOV Ax,[sincos + bx]; Get cosine
MOV bx,ax; Save it Bx=cos
Pop ax; Restore Ax=sin
Ret
ENDP Getsincos
PROC setrotation
; Set sine & cosine of x,y,z
mov Bx,[xangle]; Grab Angle
Call Getsincos; Get the Sine&cosine
mov [Xsin],ax; Save Sin
mov [XCOS],BX; Save Cos
MOV Bx,[yangle]
Call Getsincos
mov [Ysin],ax
mov [YCOS],BX
MOV Bx,[zangle]
Call Getsincos
mov [Zsin],ax
mov [ZCOS],BX
Ret
ENDP setrotation
PROC Rotatepoint; Rotates the point around x,y,z
; Gets Original X,Y,Z values
; This can is done elsewhere
MOVSX Ax,[cube+si]; Si = X (movsx coz of Byte)
mov [X],ax
MOVSX ax,[cube+si+1]; Si+1 = Y
mov [Y],ax
MOVSX ax,[cube+si+2]; si+2 = Z
mov [Z],ax
; Rotate around X-axis
; YT = Y * COS (Xang)-Z * SIN (Xang)/256
; ZT = Y * SIN (Xang) + Z * COS (Xang)/256
; Y = YT
; Z = ZT
MOV Ax,[y]
MOV Bx,[xcos]
Imul BX; Ax = Y * Cos (Xang)
MOV Bp,ax
MOV Ax,[z]
MOV Bx,[xsin]
Imul BX; Ax = Z * Sin (Xang)
Sub Bp,ax; bp = Y * Cos (Xang)-Z * Sin (Xang)
SAR bp,8; bp = Y * Cos (Xang)-Z * Sin (Xang)/256
mov [YT],BP
MOV Ax,[y]
MOV Bx,[xsin]
Imul BX; Ax = Y * Sin (Xang)
MOV Bp,ax
MOV Ax,[z]
MOV Bx,[xcos]
Imul BX; Ax = Z * Cos (Xang)
Add Bp,ax; bp = Y * SIN (Xang) + Z * COS (Xang)
SAR bp,8; bp = Y * SIN (Xang) + Z * COS (Xang)/256
mov [ZT],BP
mov Ax,[yt]; Switch values
mov [Y],ax
MOV Ax,[zt]
mov [Z],ax
; Rotate around Y-axis
; XT = X * COS (Yang)-Z * SIN (yang)/256
; ZT = X * SIN (yang) + Z * COS (yang)/256
; X = XT
; Z = ZT
MOV Ax,[x]
MOV Bx,[ycos]
Imul BX; Ax = X * Cos (Yang)
MOV Bp,ax
MOV Ax,[z]
MOV Bx,[ysin]
Imul BX; Ax = Z * Sin (Yang)
Sub Bp,ax; bp = X * Cos (Yang)-Z * Sin (Yang)
SAR bp,8; bp = X * Cos (Yang)-Z * Sin (yang)/256
mov [XT],BP
MOV Ax,[x]
MOV Bx,[ysin]
Imul BX; Ax = X * Sin (Yang)
MOV Bp,ax
MOV Ax,[z]
MOV Bx,[ycos]
Imul BX; Ax = Z * Cos (Yang)
Add Bp,ax; bp = X * SIN (yang) + Z * COS (Yang)
SAR bp,8; bp = X * SIN (yang) + Z * COS (yang)/256
mov [ZT],BP
mov ax,[xt]; Switch values
mov [X],ax
MOV Ax,[zt]
mov [Z],ax
; Rotate around Z-axis
; XT = X * COS (Zang)-Y * SIN (Zang)/256
; YT = X * SIN (Zang) + Y * COS (Zang)/256
; X = XT
; Y = YT
MOV Ax,[x]
MOV Bx,[zcos]
Imul BX; Ax = X * Cos (Zang)
MOV Bp,ax
MOV Ax,[y]
MOV Bx,[zsin]
Imul BX; Ax = Y * Sin (Zang)
Sub Bp,ax; bp = X * Cos (Zang)-Y * Sin (Zang)
SAR bp,8; bp = X * Cos (Zang)-Y * Sin (Zang)/256
mov [XT],BP
MOV Ax,[x]
MOV Bx,[zsin]
Imul BX; Ax = X * Sin (Zang)
MOV Bp,ax
MOV Ax,[y]
MOV Bx,[zcos]
Imul BX; Ax = Y * Cos (Zang)
Add Bp,ax; bp = X * SIN (Zang) + Y * COS (Zang)
SAR bp,8; bp = X * SIN (Zang) + Y * COS (Zang)/256
mov [YT],BP
mov ax,[xt]; Switch values
mov [X],ax
MOV Ax,[yt]
mov [Y],ax
Ret
ENDP Rotatepoint
     
PROC Showpoint
; Calculates Screenposition and
; Plots the "point" on the screen
mov Ax,[xoff]; Xoff*x/z+zoff = Screen X
MOV Bx,[x]
Imul BX
MOV Bx,[z]
Add Bx,[zoff]; Distance
Idiv BX
Add AX,[MX]; Center on screen
MOV Bp,ax
mov Ax,[yoff]; Yoff*y/z+zoff = Screen Y
MOV Bx,[y]
Imul BX
MOV Bx,[z]
Add Bx,[zoff]; Distance
Idiv BX
Add ax,[my]; Center on screen
     
MOV bx,320
Imul BX
Add AX,BP; Ax = (y*320) +x
MOV Di,ax
mov ax,[z]; Get color from Z
Add ax,100d; (This piece of the code could be improved)
mov [byte ptr es:di],al; Place a dot with color al
mov [Erase+si],di; Save position for Erase
Ret
ENDP Showpoint
PROC Mainprogram
Call Updateangles; Calculate New Angles
Call setrotation; Find sine & cosine of those angles
XOR Si,si; The 3d-point
MOV cx,maxpoints
Showloop:
Call Rotatepoint; Rotates the point using above formulas
Call Showpoint; Shows the point
Add si,3; Next 3d-point
Loop Showloop
Call WAITVRT; Wait for retrace
XOR Si,si; Starting with point 0
XOR Al,al; Color = 0 = Black
MOV cx,maxpoints
Deletion:
mov Di,[erase+si]; Di = Vgapos Old point
mov [byte ptr es:di],al; Delete it
Add si,3; Next Point
Loop deletion
Ret
ENDP Mainprogram
; = = = DATA = =
     
Credits DB 13,10, "Code by Vulture/outlaw Triad", 13, 10, "$"
Label Sincos Word; 256 values
DW 0,6,13,19,25,31,38,44,50,56
DW 62,68,74,80,86,92,98,104,109,115
DW 121,126,132,137,142,147,152,157,162,167
DW 172,177,181,185,190,194,198,202,206,209
DW 213,216,220,223,226,229,231,234,237,239
DW 241,243,245,247,248,250,251,252,253,254
DW 255,255,256,256,256,256,256,255,255,254
DW 253,252,251,250,248,247,245,243,241,239
DW 237,234,231,229,226,223,220,216,213,209
DW 206,202,198,194,190,185,181,177,172,167
DW 162,157,152,147,142,137,132,126,121,115
DW 109,104,98,92,86,80,74,68,62,56
DW 50,44,38,31,25,19,13,6,0,-6
dw-13,-19,-25,-31,-38,-44,-50,-56,-62,-68
dw-74,-80,-86,-92,-98,-104,-109,-115,-121,-126
dw-132,-137,-142,-147,-152,-157,-162,-167,-172,-177
dw-181,-185,-190,-194,-198,-202,-206,-209,-213,-216
dw-220,-223,-226,-229,-231,-234,-237,-239,-241,-243
dw-245,-247,-248,-250,-251,-252,-253,-254,-255,-255
dw-256,-256,-256,-256,-256,-255,-255,-254,-253,-252
dw-251,-250,-248,-247,-245,-243,-241,-239,-237,-234
dw-231,-229,-226,-223,-220,-216,-213,-209,-206,-202
dw-198,-194,-190,-185,-181,-177,-172,-167,-162,-157
dw-152,-147,-142,-137,-132,-126,-121,-115,-109,-104
Dw-98,-92,-86,-80,-74,-68,-62,-56,-50,-44
Dw-38,-31,-25,-19,-13,-6
Label Cube Byte; The 3d points
c =-35; 5x*5y*5z (=125) points
Rept 5
b =-35
Rept 5
A =-35
Rept 5
DB a,b,c
A = a + 20
Endm
B = B + 20
Endm
c = C + 20
Endm
Label Palette Byte; The palette to use
DB 0,0,0; 63*3 Gray-tint
D = 63
REPT 63
DB d,d,d
DB d,d,d
DB d,d,d
D = d-1
Endm
X DW? ; X Variable for formula
Y DW?
Z DW?
Xt DW? ; Temporary variable for x
Yt DW?
Zt DW?
Xangle DW 0; Angle to rotate around X
Yangle DW 0
Zangle DW 0
DeltaX DW? ; Amound Xangle is increased all time
DeltaY DW?
Deltaz DW?
Xoff DW?
Yoff DW?
Zoff DW? ; Distance from Viewer
Xsin DW? ; Sine and cosine of angle to rotate around
Xcos DW?
Ysin DW?
Ycos DW?
Zsin DW?
Zcos DW?
Mx DW 160; Middle of the screen
My DW 100
                
Maxpoints EQU 125; Number of 3d Points
Erase DW maxpoints DUP (?) ; Array for deletion screenpoints
ENDS CODE; End of Codesegment
End START; The definite end ...:)
; You'll use this code in your own productions but
; Give credit where the due is. Only Lamers Steal
; Code so try to create your own 3d-engine and use
; This is code as an example.
; Thanx must go to Arno Brouwer and Ash for releasing
; Example sources.
;
; Ciao Dudoz,
;
; Vulture/outlaw Triad

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.