Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;#SECTION "EQUS", HEADER
- OBJECT_DOT equ 0
- OBJECT_LINK equ 1
- OBJECT_TRI equ 2
- OBJECT_MODEL equ 3
- ;#SECTION "MAIN", CODE
- ; Inputs: b = X angle
- ; c= Y angle
- BuildMatrix:
- ; AX = cos(c)
- ld a, c
- call getCos
- ld (AX), a
- ; AY = 0
- xor a
- ld (AY), a
- ; AZ = sin(c)
- ld a, c
- call getSin
- ld (AZ), a
- ; BX =
- ; (cos(b-c)-cos(b+c))/2
- ld a, b
- add a, c
- call getCos
- ld d, a
- ld a, b
- sub c
- call getCos
- sub d
- sra a
- ld (BX), a
- ; BY = cos(b)
- ld a, b
- call getCos
- ld (BY), a
- ; BZ =
- ; (-sin(b-c)-sin(b+c))/2
- ld a, b
- add a, c
- call getSin
- ld d, a
- ld a, b
- sub c
- call getSin
- neg
- sub d
- sra a
- ld (BZ), a
- ; CX = (sin(b-c)-sin(b+c))/2
- ld a, b
- add a, c
- call getSin
- ld d, a
- ld a, b
- sub c
- call getSin
- sub d
- sra a
- ld (CX), a
- ; CY = sin(b)
- ld a, b
- call getSin
- ld (CY), a
- ; CZ =
- ; (cos(b-c)+cos(b+c))/2
- ld a, b
- add a, c
- call getCos
- ld d, a
- ld a, b
- sub c
- call getCos
- add a, d
- sra a
- ld (CZ), a
- ret
- ; Inputs: inX,Y,Z
- ; Output: outX,Y,Z
- ApplyMatrix:
- ; X = X[0,0]+Y[0,1]+Z[0,2]
- ld hl, (inX)
- ld a, (AX)
- call multHL_A
- push hl
- ld hl, (inY)
- ld a, (AY)
- call multHL_A
- push hl
- ld hl, (inZ)
- ld a, (AZ)
- call multHL_A
- pop de
- add hl, de
- pop de
- add hl, de
- call divHL64
- ld (outX), hl
- ; Y = X[1,0]+Y[1,1]+Z[1,2]
- ld hl, (inX)
- ld a, (BX)
- call multHL_A
- push hl
- ld hl, (inY)
- ld a, (BY)
- call multHL_A
- push hl
- ld hl, (inZ)
- ld a, (BZ)
- call multHL_A
- pop de
- add hl, de
- pop de
- add hl, de
- call divHL64
- ld (outY), hl
- ; Z = X[2,0]+Y[2,1]+Z[2,2]
- ld hl, (inX)
- ld a, (CX)
- call multHL_A
- push hl
- ld hl, (inY)
- ld a, (CY)
- call multHL_A
- push hl
- ld hl, (inZ)
- ld a, (CZ)
- call multHL_A
- pop de
- add hl, de
- pop de
- add hl, de
- call divHL64
- ld (outZ), hl
- ret
- ; Inputs: outX,Y,Z
- ; Outputs: outX,Y
- ProjectVertex:
- ; outX=outX*64/outZ+48
- ld hl, (outX)
- call mulHL64
- ld de, (outZ)
- call HLsignedDivDE
- ld de, 48
- add hl, de
- ld (outX), hl
- ; outY=outY*64/outZ+32
- ld hl, (outY)
- call mulHL64
- ld de, (outZ)
- call HLsignedDivDE
- ld de, 32
- ex de, hl
- or a
- sbc hl, de
- ld (outY), hl
- ret
- ; Inputs:
- ; a = object type
- ; b = render mode if needed
- ; doesn't matter else
- ; hl = offset or pointer to offsets
- pushObject:
- ex de, hl
- ld hl, (stackPtr)
- ld (hl), a
- inc hl
- ld (hl), b
- inc hl
- ld (hl), e
- inc hl
- ld (hl), d
- inc hl
- ld (stackPtr), hl
- ret
- renderObjects:
- ld hl, (stackBottom)
- renderObjects_loop:
- push hl
- ld a, (hl)
- cp OBJECT_LINK
- jp z, renderObjects_link
- cp OBJECT_TRI
- jp z, renderObjects_tri
- cp OBJECT_MODEL
- jp z, renderObjects_model
- renderObjects_dot:
- inc hl
- inc hl
- ld e, (hl)
- inc hl
- ld d, (hl)
- ld l, e
- ld h, d
- add hl, hl
- add hl, hl
- add hl, de
- ld (temp1), hl
- ld (temp2), de
- ld de, (saveVertices)
- add hl, de
- xor a
- cp (hl)
- jr nz, vertexDone
- ;
- ld hl, (temp1)
- ld de, (temp2)
- add hl, de
- ld de, (currentVertices)
- add hl, de
- ld de, inX
- ld bc, 6
- ldir
- call ApplyMatrix
- ld hl, (outZ)
- ld de, 180
- add hl, de
- bit 7, h
- jr nz, skipDisplay
- ld (outZ), hl
- call ProjectVertex
- ld hl, (temp1)
- ld de, (saveVertices)
- add hl, de
- ld (hl), 1
- inc hl
- ld de, outX
- ex de, hl
- ld bc, 4
- ldir
- ld hl, -5
- add hl, de
- vertexDone:
- inc hl
- ld a, (hl)
- inc hl
- inc hl
- ld l, (hl)
- ld h, a
- call pxlonhl
- inc h
- call pxlonhl
- inc l
- call pxlonhl
- dec h
- call pxlonhl
- skipDisplay:
- renderObjects_link:
- renderObjects_tri:
- renderObjects_model:
- pop hl
- ld de, 4
- add hl, de
- ld de, (stackPtr)
- or a
- sbc hl, de
- add hl, de
- ret z
- jp renderObjects_loop
- clearStack:
- ld hl, (stackBottom)
- ld (stackPtr), hl
- ret
- multHL_DE:
- ld c, h
- ld a, l
- ld hl, 0
- ld b, 16
- multHL_DE_loop:
- add hl, hl
- rla
- rl c
- jr nc, multHL_DE_skip
- add hl, de
- adc a, 0
- jr nc, multHL_DE_skip
- inc c
- multHL_DE_skip:
- djnz multHL_DE_loop
- ret
- divHL64:
- sra h
- rr l
- sra h
- rr l
- sra h
- rr l
- sra h
- rr l
- sra h
- rr l
- sra h
- rr l
- ret
- mulHL64:
- add hl, hl
- add hl, hl
- add hl, hl
- add hl, hl
- add hl, hl
- add hl, hl
- ret
- AextendSignDE:
- ld e, a
- rlca
- sbc a, a
- ld d, a
- ret
- getSin:
- sub 64
- getCos:
- push hl
- push de
- ld hl, cosLUT
- ld e, a
- ld d, 0
- add hl, de
- ld a, (hl)
- pop de
- pop hl
- ret
- ; scale = 64
- cosLUT:
- db 64, 63, 63, 63, 63, 63, 63, 63, 62, 62, 62, 61, 61, 60, 60, 59
- db 59, 58, 57, 57, 56, 55, 54, 54, 53, 52, 51, 50, 49, 48, 47, 46
- db 45, 44, 42, 41, 40, 39, 38, 36, 35, 34, 32, 31, 30, 28, 27, 25
- db 24, 23, 21, 20, 18, 17, 15, 14, 12, 10, 9, 7, 6, 4, 3, 1
- db 0, -1, -3, -4, -6, -7, -9, -10, -12, -14, -15, -17, -18, -20, -21, -23
- db -24, -25, -27, -28, -30, -31, -32, -34, -35, -36, -38, -39, -40, -41, -42, -44
- db -45, -46, -47, -48, -49, -50, -51, -52, -53, -54, -54, -55, -56, -57, -57, -58
- db -59, -59, -60, -60, -61, -61, -62, -62, -62, -63, -63, -63, -63, -63, -63, -63
- db -64, -63, -63, -63, -63, -63, -63, -63, -62, -62, -62, -61, -61, -60, -60, -59
- db -59, -58, -57, -57, -56, -55, -54, -54, -53, -52, -51, -50, -49, -48, -47, -46
- db -45, -44, -42, -41, -40, -39, -38, -36, -35, -34, -32, -31, -30, -28, -27, -25
- db -24, -23, -21, -20, -18, -17, -15, -14, -12, -10, -9, -7, -6, -4, -3, -1
- db 0, 1, 3, 4, 6, 7, 9, 10, 12, 14, 15, 17, 18, 20, 21, 23
- db 24, 25, 27, 28, 30, 31, 32, 34, 35, 36, 38, 39, 40, 41, 42, 44
- db 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 54, 55, 56, 57, 57, 58
- db 59, 59, 60, 60, 61, 61, 62, 62, 62, 63, 63, 63, 63, 63, 63, 63
- AX:
- db 0
- AY:
- db 0
- AZ:
- db 0
- BX:
- db 0
- BY:
- db 0
- BZ:
- db 0
- CX:
- db 0
- CY:
- db 0
- CZ:
- db 0
- inX:
- dw 0
- inY:
- dw 0
- inZ:
- dw 0
- outX:
- dw 0
- outY:
- dw 0
- outZ:
- dw 0
- ;#SECTION "MATHS", CODE
- HLsignedDivDE:
- ld c, e
- ld b, d
- ld a, h
- xor b
- push af
- xor b
- jp p, $ + 9
- xor a
- sub l
- ld l, a
- sbc a, a
- sub h
- ld h, a
- bit 7, b
- jr z, $ + 8
- xor a
- sub c
- ld c, a
- sbc a, a
- sub b
- ld b, a
- ;
- ld de, 0
- adc hl, hl
- jr z, divDone
- ld a, 16
- add hl, hl
- dec a
- jr nc, $ - 2
- ex de, hl
- jp jumpin
- l1:
- add hl, bc
- l2:
- dec a
- jr z, divDone
- sla e
- rl d
- jumpin:
- adc hl, hl
- sbc hl, bc
- jr c, l1
- inc e
- jp l2
- divDone:
- ;
- pop af
- ex de, hl
- ret p
- xor a
- sub l
- ld l, a
- sbc a, a
- sub h
- ld h, a
- ret
- ;
- multHL_A:
- ex de, hl
- ld hl, 0
- or a
- jp p, $ + 5
- sbc hl, de
- ld b, 8
- multHL_A_loop:
- add hl, hl
- rla
- jr nc, $ + 5
- add hl, de
- adc a, 0
- djnz multHL_A_loop
- ret
- ;#SECTION "LABELS", FOOTER
- stackBottom:
- dw 0
- stackPtr:
- dw 0
- currentVertices:
- dw 0
- saveVertices:
- dw 0
- temp1:
- dw 0
- temp2:
- dw 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement