prog8/examples/cube3d-gfx.p8
2020-11-22 18:35:43 +01:00

106 lines
3.0 KiB
Lua

%import syslib
%import graphics
%import test_stack
; Note: this program is compatible with C64 and CX16.
main {
; vertices
word[] xcoor = [ -100, -100, -100, -100, 100, 100, 100, 100 ]
word[] ycoor = [ -100, -100, 100, 100, -100, -100, 100, 100 ]
word[] zcoor = [ -100, 100, -100, 100, -100, 100, -100, 100 ]
; storage for rotated coordinates
word[len(xcoor)] rotatedx
word[len(ycoor)] rotatedy
word[len(zcoor)] rotatedz
; edges
ubyte[] edgesFrom = [ 0, 2, 6, 4, 1, 3, 7, 5, 0, 2, 6, 4]
ubyte[] edgesTo = [ 2, 6, 4, 0, 3, 7, 5, 1, 1, 3, 7, 5]
sub start() {
uword anglex
uword angley
uword anglez
graphics.enable_bitmap_mode()
repeat {
rotate_vertices(msb(anglex), msb(angley), msb(anglez))
graphics.clear_screen(1, 0)
draw_lines()
anglex-=500
angley+=217
anglez+=452
wait_a_little_bit()
; test_stack.test()
}
}
sub wait_a_little_bit() {
%asm {{
stx P8ZP_SCRATCH_REG
lda #0
jsr c64.SETTIM
- jsr c64.RDTIM
cmp #1
bne -
ldx P8ZP_SCRATCH_REG
rts
}}
}
sub rotate_vertices(ubyte ax, ubyte ay, ubyte az) {
; rotate around origin (0,0,0)
; set up the 3d rotation matrix values
word wcosa = cos8(ax)
word wsina = sin8(ax)
word wcosb = cos8(ay)
word wsinb = sin8(ay)
word wcosc = cos8(az)
word wsinc = sin8(az)
word wcosa_sinb = wcosa*wsinb / 128
word wsina_sinb = wsina*wsinb / 128
word Axx = wcosa*wcosb / 128
word Axy = (wcosa_sinb*wsinc - wsina*wcosc) / 128
word Axz = (wcosa_sinb*wcosc + wsina*wsinc) / 128
word Ayx = wsina*wcosb / 128
word Ayy = (wsina_sinb*wsinc + wcosa*wcosc) / 128
word Ayz = (wsina_sinb*wcosc - wcosa*wsinc) / 128
word Azx = -wsinb
word Azy = wcosb*wsinc / 128
word Azz = wcosb*wcosc / 128
ubyte @zp i
for i in 0 to len(xcoor)-1 {
; don't normalize by dividing by 128, instead keep some precision for perspective calc later
rotatedx[i] = (Axx*xcoor[i] + Axy*ycoor[i] + Axz*zcoor[i])
rotatedy[i] = (Ayx*xcoor[i] + Ayy*ycoor[i] + Ayz*zcoor[i])
rotatedz[i] = (Azx*xcoor[i] + Azy*ycoor[i] + Azz*zcoor[i])
}
}
sub draw_lines() {
ubyte @zp i
for i in len(edgesFrom) -1 downto 0 {
ubyte @zp vFrom = edgesFrom[i]
ubyte @zp vTo = edgesTo[i]
word @zp persp1 = 256 + rotatedz[vFrom]/256
word @zp persp2 = 256 + rotatedz[vTo]/256
graphics.line(rotatedx[vFrom] / persp1 + graphics.WIDTH/2 as uword,
rotatedy[vFrom] / persp1 + graphics.HEIGHT/2 as ubyte,
rotatedx[vTo] / persp2 + graphics.WIDTH/2 as uword,
rotatedy[vTo] / persp2 + graphics.HEIGHT/2 as ubyte)
}
}
}