Programming — a 3D tentacle effect in assembly

Tagan News #08

Coding

Continuing our previous discussion about programming 3D stuff.

Seen the intro to the recently released InfoGuide? In the background, there’s this little ball with “tails” spinning around. A cute thing with the pretty name “tentacle effect.” Today we’re going to make something similar.

Like it? If so, get your assemblers ready! :)

Since we’re working with vector graphics, we don’t need a fill procedure; we need a line-drawing procedure. I used a ready-made one (someone from our group wrote it ages ago), so we won’t dwell on it. All that talk about matrices and coordinate transformations from the previous article applies here too. The only difference is that back then we drew faces, and here we’ll draw edges. Consequently, we don’t need visibility-determining procedures at all, but a rotation procedure would certainly come in handy. Here’s a reminder of the sequence for transforming coordinates:

x'=x
y'=y*cos(rx)-z*sin(rx)
z'=z*cos(rx)+y*sin(rx)

Rotation around the Oy axis:

x''=x'*cos(ry)+z'*sin(ry) ;here's where the previous article had an inaccuracy
y''=y'
z''=z'*cos(ry)-x'*sin(ry) ;and here too

Around the Oz axis:

x'''=x''*cos(rz)-y''*sin(rz)
y'''=y''*cos(rz)+x''*sin(rz)
z'''=z''

Now let’s start optimizing. Let’s take the axes to point in these directions: X—to the right, Y—down, Z—doesn’t matter :) That is, we’ll just use screen coordinates. In the end, we’ll only use X and Y anyway, so let’s make things a little easier and cut half the calculations.

x'''=x''*cos(rz)-y''*sin(rz)
y'''=y''*cos(rz)+x''*sin(rz)

Since y''=y', we’ll rotate only X around Oy:

x''=x*cos(ry)+z'*sin(ry) ;taking into account that x'=x

And now we’re on the home stretch:

y''=y*cos(rx)-z*sin(rx) ;because y''=y'
z'=z*cos(rx)+y*sin(rx)

Coding

Voila! In the end, one line less. A small thing, but nice—and most importantly, faster!

We define the object as a set of coordinate triples, one for each of the three axes. We also divide the points into zones according to their distance from the center of the figure. The point coordinates were obtained using 3ds Max: I created the figure, clicked on its points, and carefully copied all the numbers onto a piece of paper.

We’ve got the points; now let’s draw this whole contraption (literally, since the “tentacles” might be shaking :) ). Drawing is done edge by edge: each edge is specified by the indices of two points in the array of transformed coordinates. See the source code for details.

Now let’s give the object a little “flexibility.” Let the points rotate out of sync, slowing down as they move farther away. This is implemented quite simply: we create three arrays of rotation angles. At each moment in time, we save one increment of the rotation angles in the arrays. The offset is increased cyclically—why waste memory? To implement this “braking,” all we need to do is read values from the array at some offset behind the current pointer. The farther back we go, the older the data we get (just don’t go too far—the array is circular!). The source code shows how this is implemented.

A few notes. This time, the sine table is built a little differently. Previously, the low byte of the address determined the angle, while the high byte determined the amplitude (the table was 16K, hence the restriction on object size: -32…31 units). Now it’s the other way around. So the rotation is a little less finely stepped (pi/32 radians), since a full rotation now takes 64 steps. But the amplitude restriction is gone, and the code is simpler too.

The demo mode is just plain wild: the poor cuttlefish gets twisted and shaken in every direction, but it looks good. The source code tells you which keys to press to get everything working.

;*
;* LITTLE TENTACLE EFFECT
;*
;* (C) Vitamin/CAIG/2001      11.01.2005
;*
;* ENTER- demo mode
;* O,P, Q,A, W,S- rotation in manual mode
;* BREAK- exit
;*

DEMX    EQU 1                   ;three constants for demo mode
DEMY    EQU 3
DEMZ    EQU 5
DOTS    EQU 32                  ;number of vertices
SPDX    EQU 3                   ;rotation speeds around the axes
SPDY    EQU 3                   ;in manual mode
SPDZ    EQU 3
DTS     EQU DOTS*3

        ORG 24576
        LD A,23
        LD BC,32765
        OUT (C),A
        LD HL,49152+6144        ;clear screen
        LD BC,767
        LD (HL),56              ;first color
        LDIR
        LD HL,22528
        LD DE,22529
        LD BC,767
        LD (HL),56              ;second color
        LDIR

        OUT (-2),A
;try the combination 120,71,0
;looks awesome! :)

        CALL MAK_PLO            ;calculate line table
        CALL DECRUNT            ;calculate sine table
;main program loop
LOOP    CALL CALCUL             ;recalculate coordinates
PPAG    LD A,23                 ;switch screens
        LD BC,32765
        OUT (C),A
        CALL CLS                ;and erase what was there
        CALL BUILD

        LD A,(PPAG+1)           ;next time, draw on the
        XOR 10                  ;visible screen, and display
        LD (PPAG+1),A           ;the one we are drawing now

        LD DE,(RX)              ;rotation angles around the axes
        LD BC,(RZ)

Coding

DEMO    LD A,-1                 ;demo mode?
        AND A
        JR Z,DRIVE              ;no, all manual...
        LD H,'SINUS             ;just recalculate the angles...
DX=$+1
        LD L,DEMX
        LD A,(HL)
        SRA A
        SRA A
        SRA A
        SRA A
        SRA A
        ADD A,E
        LD E,A
        LD A,L
        ADD A,DEMX
        LD (DX),A
DY=$+1
        LD L,DEMY
        LD A,(HL)
        SRA A
        SRA A
        SRA A
        SRA A
        SRA A
        ADD A,D
        LD D,A
        LD A,L
        ADD A,DEMY
        LD (DY),A

DZ=$+1
        LD L,DEMZ
        LD A,(HL)
        SRA A
        SRA A
        SRA A
        SRA A
        SRA A
        ADD A,C
        LD C,A
        LD A,L
        ADD A,DEMZ
        LD (DZ),A

        JR ISDEM

DRIVE   LD A,#DF        ;keyboard control
        IN A,(#FE)
        JR C,O1
        DUP SPDX
        DEC E
        EDUP
        JR C,O2
        DUP SPDX
        INC E
        EDUP
O2      LD A,#FB
        IN A,(#FE)
        JR C,O3
        DUP SPDY
        INC D
        EDUP
        JR C,O4
        DUP SPDZ
        INC C
        EDUP
O4      LD A,#FD
        IN A,(#FE)
        JR C,O5
        DUP SPDY
        DEC D
        EDUP
        JR C,O6
        DUP SPDZ
        DEC C
        EDUP
O6
ISDEM   LD A,191        ;ENTER — enter/exit demo mode
        IN A,(-2)
        RRA
        JR C,NODEM
        LD A,(DEMO+1)
        CPL
        LD (DEMO+1),A
WAYOT   LD A,191        ;wait until it's released...
        IN A,(-2)
        RRA
        JR NC,WAYOT

NODEM                   ;new rotation angles
        LD (RX),DE
        LD (RZ),BC

        LD H,'XTAB      ;save rotation history
TIMER   LD L,0
        INC L
        LD (HL),E
        INC H
        LD (HL),D
        INC H
        LD (HL),C
        LD A,L
        LD (TIMER+1),A

        CALL 8020       ;BREAK pressed — exit
        JP C,LOOP
        RET

CLS     LD (STAK+1),SP  ;clear the screen quickly
        DI
        LD HL,0
        LD B,48
PSA
        DUP 64
        PUSH HL
        EDUP
        DJNZ PSA
STAK    LD SP,0
        EI
        RET

;recalculate coordinates
CALCUL
        LD DE,(RX)      ;save the angles just in case
        LD BC,(RZ)
        PUSH DE
        LD HL,ROTDAT
        LD IX,COORDS
        LD A,(TIMER+1)  ;current point in time
        LD E,A
ROTTA   LD A,(HL)
        INC HL
        AND A
        JR Z,RESTZZ
        LD B,A          ;number of points
        LD D,'XTAB
        LD A,E
        SUB (HL)        ;how many frames ago this was
        LD E,A
        LD A,(DE)
        LD (RX),A
        INC D
        LD A,(DE)
        LD (RY),A
        INC D
        LD A,(DE)
        LD (RZ),A
        CALL SSG
        INC HL
        JR ROTTA

RESTZZ  POP DE
        LD (RX),DE      ;restore the angles
        LD (RZ),BC
        RET

;rotation data
;first, 4 points are calculated in real time (the core)
;then 12 points lag by 4 frames (the first joint)
;another 12 points lag by another 4 frames (the second joint)
;the last 4 points lag by another 5 frames (the ends)
;less lag makes the model more "springy"

SSG     EXX             ;recalculate coordinates
        LD A,(RX)
        AND 252
        RRA
        SCF
        RRA
        LD H,A

        LD B,L
        LD C,(HL)       ;Y*SIN(RX)
        LD E,(HL)       ;Z*SIN(RX)

        LD A,H
        ADD A,16
        AND 191
        LD H,A

        LD A,(HL)       ;Z*COS(RX)
        ADD A,C
        LD C,A          ;Z'= Z*COS(RX)+Y*SIN(RX)

Coding

Fragment 4/6.

        LD A,(HL)       ;Y*COS(RX)
        SUB E
        LD E,A          ;Y''=Y*COS(RX)-Z*SIN(RX)

        LD A,(RY)
        AND 252
        RRA
        SCF
        RRA
        LD H,A

        LD L,C
        LD C,(HL)       ;Z'*SIN(RY)

        LD A,H
        ADD A,16
        AND 191
        LD H,A

        LD A,(HL)       ;X*COS(RY)
        ADD A,C
        LD C,A          ;X''=X*COS(RY)+Z'*SIN(RY)

        LD A,(RZ)
        AND 252
        RRA
        SCF
        RRA
        LD H,A

        LD L,C
        LD B,(HL)       ;X''*SIN(RZ)
        LD L,E
        LD E,(HL)       ;Y''*SIN(RZ)

        LD A,H
        ADD A,16
        AND 191
        LD H,A

        LD A,(HL)       ;X''*COS(RZ)
        ADD A,B
        ADD A,96
        LD (IX-DTS+1),A ;Y'''
        LD L,C
        LD A,(HL)       ;Y''*COS(RZ)
        SUB E
        ADD A,128

        INC IX
        INC IX
        INC IX
        EXX
        DJNZ SSG
        RET

;build object from point coordinates and edge list
BUILD
        LD HL,EDGES
BLDCYC  LD A,(HL)
        INC A
        RET Z           ;last edge?
        DEC A
        LD E,A
        INC HL
        LD A,(HL)
        INC HL
        PUSH HL
        LD L,A
EDGE                    ;L,E - vertex numbers of edge
        LD A,L
        ADD A,A
        ADD A,L
        LD L,A
        LD H,'COORDS1
        LD D,H
        LD A,E
        ADD A,A
        ADD A,E
        LD E,A

        LD A,(HL)
        INC HL
        LD H,(HL)
        LD L,A
        EX DE,HL
        LD A,(HL)
        INC HL
        LD H,(HL)
        LD L,A          ;get the coordinates of the edge endpoints
        CALL LINE_R     ;and draw it
        POP HL
        JR BLDCYC       ;for all edges

RX      DB 0            ;rotation angles
RY      DB 0
RZ      DB 0
RW      DB 0

COORDS1 DS DTS          ;buffer for transformed coordinates

;original object
COORDS  DB 0, 0, 10     ;core
        DB 9, 0, -3
        DB -5, 8, -3

;first joints

        DB 8, 0, -43

;second joints

        DB 5, 0, -73

;ends
        DB 0, 0, -93

;edge table
EDGES   DB 0,1, 0,2     ;core
        DB 1,2, 1,3
        DB 3,0, 3,2

;first joints

;second joints

;ends

        DB -1

;calculate sine table
DECRUNT LD HL,#8000
DCR     PUSH HL
        LD A,H
        SUB #80
        ADD A,A
        ADD A,A
        LD H,L
        CALL HSINA
        POP HL
        LD (HL),A
        INC HL
        LD A,H
        CP 192
        JR NZ,DCR
        RET

;various utility routines
HSINA                           ;A=H*SIN A
        LD D,'SINUS
        LD E,A
        LD A,(DE)
HMULAA  AND A
        JP M,NEGE
        BIT 7,H
        JR Z,ALPOS
        EX AF,AF'
        LD A,H
        NEG
        LD H,A
        EX AF,AF'
        JR NEGRES
ALPOS   CALL MULS
        ADD HL,HL
        LD A,H
        RET

NEGE    NEG
        BIT 7,H
        JR Z,NEGRES
        EX AF,AF'
        LD A,H
        NEG
        LD H,A
        EX AF,AF'
        JR ALPOS
NEGRES  CALL MULS
        ADD HL,HL
        LD A,H
        NEG
        RET

HMULA   AND A
        JP M,NEGE1
        BIT 7,H
        JR Z,ALPOS1
        EX AF,AF'
        LD A,H
        NEG
        LD H,A
        EX AF,AF'
        JR NEGRES1
ALPOS1  CALL MULS
        ADD HL,HL
        RET

Coding

NEGE1   NEG
        BIT 7,H
        JR Z,NEGRES1
        EX AF,AF'
        LD A,H
        NEG
        LD H,A
        EX AF,AF'
        JR ALPOS1
NEGRES1 CALL MULS
        ADD HL,HL
        LD A,H
        CPL
        LD H,A
        LD A,L
        CPL
        LD L,A
        INC HL
        RET

MULS                    ;HL=H*A
        LD L,0
        LD D,L
        LD E,A
CWERT   DUP     8
        ADD HL,HL
        JR NC,$+3
        ADD     HL,DE
        EDUP
        RET

LINE_R  LD (STACK+1),SP ;LINE DRAWING PROCEDURE
        DI
        LD A,L          ;THE POINT COORDINATES ARE IN HL AND DE
        CP E
        JR NC,NO_EXCH
        EX DE,HL
        LD A,L
NO_EXCH: SUB E
        LD C,A
        LD A,H
        SUB D
        JR C,DOWN_UP
UP_DOWN: LD B,A          ;DRAW THE LINE FROM TOP TO BOTTOM
        LD L,D
        JP MISS
DOWN_UP: NEG             ;DRAW THE LINE FROM BOTTOM TO TOP
        LD B,A
        LD A,191
        SUB D
        LD L,A
MISS:   ADD HL,HL
        LD SP,HL
        LD H,'LIN_ARR
        LD L,D
        LD A,(HL)
        INC H
        LD D,(HL)
        INC H
        LD L,E
        ADD A,(HL)
        LD E,A
        INC H
        LD L,(HL)
        EX DE,HL
        LD A,B
        CP C
        JR NC,VERTIC
HORIZON: DEFB 221        ;HORIZONTAL LINE (DX>DY)
        LD L,A
        DEFB 221
        LD H,C
        LD B,C
        LD C,E
        LD A,B
        SRL A
        JR Z,END_LIN
        EX AF,AF'
HORIZ0: LD A,(HL)
        OR C
        LD (HL),A
        EX AF,AF'
        DEFB 221
        SUB L
        JR NC,HORIZ2
        DEFB 221
        ADD A,H
        POP DE
        ADD HL,DE
HORIZ2: EX AF,AF'
        RRC C
        JR NC,HORIZ1
        INC L
HORIZ1: DJNZ HORIZ0
END_LIN: LD A,(HL)
        OR C
        LD (HL),A
STACK:  LD SP,0
        EI
        RET
VERTIC: DEFB 221        ;VERTICAL LINE (DY>DX)
        LD H,A
        DEFB 221
        LD L,C
        LD C,E
        SRL A
        JR Z,END_LIN
        EX AF,AF'
VERT0:  LD A,(HL)
        OR C
        LD (HL),A
        EX AF,AF'
        DEFB 221
        SUB L
        JR NC,VERT1
        DEFB 221
        ADD A,H
        RRC C
        JR NC,VERT1
        INC L
VERT1:  EX AF,AF'
        POP DE
        ADD HL,DE
        DJNZ VERT0
        JP END_LIN
DEFB    " * MEGA FAST LINE V1.0 BY UNWINDER'95
NEXT_DE: INC D
        LD A,D
        AND 7
        RET NZ
        LD A,E
        ADD A,32
        LD E,A
        RET C
        LD A,D
        SUB 8
        LD D,A
        RET
MAK_PLO: LD HL,LIN_ARR
        LD B,192
MAK_PL0: LD (HL),E
        INC H
        LD (HL),D
        DEC H
        INC L
        CALL NEXT_DE
        DJNZ MAK_PL0
        LD HL,LIN_ARR+1023
        LD A,1
MAK_PL1: LD (HL),A
        RLCA
        DEC L
        DJNZ MAK_PL1
        DEC H
        LD A,31
MAK_PL2: LD B,8
MAK_PL3: LD (HL),A
        DEC L
        DJNZ MAK_PL3
        DEC A
        JP P,MAK_PL2
MAK_LIN: LD IX,LIN_ARR+1024
        LD B,191
MAK_LI0: LD E,L
        LD D,H
        EX DE,HL
        CALL NEXT_DE
        EX DE,HL
        AND A
        SBC HL,DE
        LD (IX),L
        INC IX
        LD (IX),H
        INC IX
        ADD HL,DE
        DJNZ MAK_LI0
        LD IX,LIN_ARR+1024+512
        LD B,191
MAK_LI1: LD E,L
        LD D,H
        CALL PREV_HL
        AND A
        SBC HL,DE
        LD (IX),L
        INC IX
        LD (IX),H
        INC IX
        ADD HL,DE
        DJNZ MAK_LI1
        RET
PREV_HL: DEC H
        LD A,H
        AND 7
        CP 7
        RET NZ
        LD A,L
        SUB 32
        LD L,A

Coding

        RET C
        LD A,H
        ADD A,8
        LD H,A
        RET

SINUS   INCBIN "SINUS
XTAB    EQU $                   ;X rotation history

Share your thoughts about the article