A rotating 3D box (animation)

xiaoxiao2021-03-06  76

This program is written by foreign Vulture big brother and publishes the source code. This is a piece of his 95 years. It can be said that it is very successful at the time!

This procedure is cleverly utilized the changing coordinates, thereby realizing the box 3D rotation of the stars!

In order to respect the copyright, I did not translate the source annotation, so that the domestic compilation enthusiasts can think about the foreign assembly programming!

; Compilation: 1 Tasm 3d.asm

2 TLINK 3D.Obj

3 exe2bin 3d.exe 3d.com

This program is a very classal 3D small animation that is carefully collected. Webmaster's X86 assembly station: http://www.x86asm.com

; Permanent Name: http://x86asm.yeah.net

; ================================================== ============================= ======== ;;; assembler program by vulture. ;; 3D-system example to. 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 / 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 Fine. ;; ;; Current Date: 6-9-95 VULTURE ;;; =========================================== ============================================================================================================================================================================================================= =======;

IDEAL; Ideal modeP386; Allow 80386 instructionsJUMPS; Tasm handles out of range jumps (rulez :)!) SEGMENT CODE; Code segment startsASSUME cs: code, ds: code; Let cs and ds point to code segmentORG 100h; Make a .COM fileSTART :; Main Programmov AX, 0013H; Init Vgaint 10HMOV AX, CSMOV DS, AX; DS Points to Codesegmentmov AX, 0A000HMOV ES, AX; ES Points To Vgalea Si, [Palette]; Set PaletTemov DX, 3C8HXOR AL, Alout DX, Almov DX, 3C9HMOV CX, 189 * 3REPZ OUTSB

=== set some variables === MOV [deltax], 1; Initial Speed ​​of Rotationmov [Deltay], 1; Change this and Watch Whatmov [Deltaz], 1; Happens. It's Fun!

MOV [Xoff], 256Mov [Yoff], 256; Used for Calculating VGA-POSMOV [Zoff], 300; Distance from Viewer

Mainloop: Call Mainprogram; Yep ... do it all ... ;-)

IN AL, 60H; Scan KeyboardCMP Al, 1; Test ON escapejne mainloop; Continue if not keypressed

; === quit to dos === MOV AX, 0003H; Back to TextModeint 10hle DX, [Credits] MOV AH, 9INT 21HMOV AX, 4C00H; Return Control to Dosint 21h; Call Dos Interrupt

=== Sub-routines === proc waitvrt; Waits for vertical Retrace To Reduce "Snow" MOV DX, 3DAHVRT: IN AL, DXTEST AL, 8JNZ VRT; WAIT UNTIL VERTICE RETRACE STARTSNOVRT: IN AL, DXTEST AL, 8JZ NOVRT WAIT UNTIL VERTICLE RETRACE EndSret; Return to Main Programendp Waitvrt

PROC Updateangles; Calculates New X, Y, Z Angles; To Rotate Aroundmov AX, [Xangle]; Load Current Anglesmov BX, [Yangle] MOV CX, [Zangle] Add Ax, [DelTax]; Add Velocityand AX, 11111111b; Range from 0..255MOV [xangle], ax; update xadd bx, [deltay]; add velocityand bx, 11111111b; range from 0..255MOV [yangle], bx; update yadd cx, [deltaz]; add velocityand CX, 11111111B; Range from 0..255MOV [zangle], cx; Update Zretendp Updateanglesproc getsincos; needed: bx = angle (0..255); returns: ax = sin bx = cospush bx; save angle (use as pointer) SHL BX, 1 ; Grab A Word SO BX = BX * 2MOV AX, [Sincos BX]; Get Sinepop Bx; Restore Pointer Into Bxpush Ax; Save Sine on StackAdd BX, 64; Add 64 TO GET COSINEAND BX, 11111111B; Range from 0 .. 255SHL BX, 1; * 2 ​​Coz it's a Wordmov AX, [Sincos BX]; Get Cosinemov BX, AX; Save IT BX = COSPOP AX; restore ax = sinretendp getsincos

PROC SETROTATION; SET SINE & COSINE OF X, Y, ZMOV BX, [Xangle]; Grab AngleCall Getsincos; Get The Sine & Cosinemov [XSIN], AX; SAVE SINMOV [XCOS], BX; Save Cos

MOV BX, [yangle] Call getsincosmov [ysin], AXMOV [Ycos], BX

MOV BX, [Zangle] Call getsincosmov [zsin], AXMOV [ZCOS], BXRETENDP SETROTATION

PROC ROTATEPOINT; Rotates The Point Around X, Y, Z; Gets Original X, Y, Z VALUES; this Can Be Done elsewheremovsx AX, [Cube Si]; Si = x (Movsx Coz of Byte) MOV [x], AXMOVSX AX, [Cube Si 1]; Si 1 = YMOV [Y], AXMOVSX AX, [Cube Si 2]; Si 2 = ZMOV [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, AXMOV 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) / 256MOV [YT], BPMOV AX, [Y ] MOV BX, [XSIN] Imul Bx; AX = Y * SIN (Xang) MOV BP, AXMOV 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) / 256MOV [ZT], BP

MOV AX, [YT]; Switch Valuesmov [Y], AXMOV 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, AXMOV 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) / 256MOV [x], BP

Mov AX, [X] MOV BX, [Ysin] Imul Bx; AX = x * sin (yang) MOV BP, AXMOV 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) / 256MOV [ZT], BP

MOV AX, [XT]; Switch Valuesmov [x], AXMOV 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, AXMOV 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 / 256MOV [x], BP

MOV AX, [X] MOV BX, [Zsin] Imul Bx; AX = x * sin (zang) MOV BP, AXMOV 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) / 256MOV [YT], BPMOV AX, [x ]; Switch Valuesmov [x], AXMOV AX, [YT] MOV [Y], AX

retENDP RotatePointPROC ShowPoint; Calculates screenposition and; plots the point on the screenmov ax, [Xoff]; Xoff * X / Z Zoff = screen xmov bx, [X] imul bxmov bx, [Z] add bx, [Zoff]; Distanceidiv BXADD AX, [MX]; Center On Screenmov BP, AX

MOV AX, [YOFF]; YOFF * Y / Z ZOFF = Screen Ymov BX, [Y] Imul bxmov bx, [z] add bx, [zoff]; distanceidiv bxadd ax, [my]; Center On Screenmov BX, 320imul BXADD AX, BP; AX = (Y * 320) XMOV DI, AX

MOV AX, [Z]; Get Color from Zadd AX, 100D; (this Piece of CODE COD BE IMPROVED)

MOV [Byte PTR ES: DI], Al; Place A Dot with Color Almov [ERASE SI], DI; Save Position for Eraseretendp Showpoint

Proc MainprogramCall Updateangles; Calculate New Anglescall Setrotation; Find Sine & Cosine of Those Angles

XOR Si, Si; First 3D-Pointmov CX, MaxPointSshowloop: Call RotatePoint; Rotates The Point Using Above Formulascall showpoint; shows the pointpoint; 3; Next 3D-PointLoop ShowloP

Call Waitvrt; Wait for Retrace

XOR Si, Si; Starting With Point 0xor Al, Al; Color = 0 = Blackmov CX, MaxPointSdeletion: Mov Di, [ERASE SI]; DI = VGAPOS Old Pointmov [Byte Ptr Es: DI], Al; Delete ItAdd Si, 3; Next PointLoop DeletionRetendp Mainprogram

=== Data === Credits DB 13, 10, "Code by Vulture / Outlaw Triad", 13, 10, "$"

Label SinCos Word; 256 valuesdw 0,6,13,19,25,31,38,44,50,56dw 62,68,74,80,86,92,98,104,109,115dw 121,126,132,137,142,147,152,157,162,167dw 172,177,181,185,190,194,198,202,206,209dw 213,216,220,223,226,229,231,234,237,239dw 241,243,245,247,248,250,251,252,253,254dw 255,255,256,256,256,256,256,255,255,254dw 253,252,251,250,248,247,245,243,241,239 dw 237,234,231,229,226,223,220,216,213,209dw 206,202,198,194,190,185,181,177,172,167dw 162,157,152,147,142,137,132,126,121,115dw 109,104,98,92,86,80,74,68,62,56dw 50,44,38,31,25,19,13,6,0, -6dw -13, -19, -25, -31, -38, -44, -50, -56, -62, -68DW -74, -80, -86, -92, -98, -104, -109, -115, -121, -126DW -132, -137, -142, -147, -152, -157, -162, -167, -172, -177DW-181, -185, -190, -194, -198, -202, - 206, -209, -213, -216DW -220, -223, -226, -229, -231, -234, -237, -239, -241, -243DW-245, -247, -248,-250 -251, -252, -253, -254, -255, -255DW-256, -256, -256, -256, -256, -255, -255, -254, -253, -252DW-251, -250, -248, -247, -245, -243, -241, -239, -237, -234DW -231, -229, -226, -223, -220, -216, -213, -209, -206, -202DW -198, -1 94, -190, -185, -181, -177, -172, -167, -162, -157DW-152, -147, -142, -137, -132, -126, -121, -115, 109, -104DW -98, -92, -86, -80, -74, -68, -62, -56, -50, -44DW -38, -31, -25, -19, -13, -6Label Cube Byte; THE 3D POINTSC = -35; 5X * 5Y * 5Z (= 125) PointSrept 5B = -35REPT 5A = -35REPT 5DB A, B, CA = A 20ENDMB = B 20ENDMC = C 20ENDM

Label Palette Byte; The Palette TO Usedb 0,0,0; 63 * 3 Gray-Tintd = 63rept 63DB D, D, DDB D, D, DDB D, D, DD = D - 1ENDM

X dw?; X variable for Formulay DW? Z dw?

XT dw?; Temporary Variable for Xyt DW? ZT DW?

Xangle DW 0; Angle To Rotate Around Xyangle DW 0zangle DW 0

DELTAX DW?; AMOUND XANGLE IS INCREASED EACH TIMEDELTAY DW? DELTAZ DW?

Xoff dw? Yoff dw? Zoff dw?; Distance from viewerxsin dw?; Sine and cosine of Angle to rotate aroundxcos dw? In dw? Ycos dw? Zsin dw? Zcos dw?

MX DW 160; Middle of the Screenmy DW 100maxpoints EQU 125; Number of 3D Points

ERASE DW MAXPOINTS DUP (?); Array for deleion ScreenPoint

Ends code; end of cotsegmentend start; the definite end .... :)

; You may use this code in your own productions but; give credit where credit is due Only lamers steal;. Code so try to create your own 3d-engine and use; this code as an example .; Thanx must go to Arno Brouwer and Ash for release; example. ;; Ciao Dudoz, ;; Vulture / Outlaw Triad

转载请注明原文地址:https://www.9cbs.com/read-93468.html

New Post(0)