cube3d with sprites added

This commit is contained in:
Irmen de Jong 2019-01-17 02:25:52 +01:00
parent 76aeb06c97
commit f7dcdceaaf
3 changed files with 162 additions and 111 deletions

154
examples/cube3d-sprites.p8 Normal file
View File

@ -0,0 +1,154 @@
%import c64utils
~ spritedata $2000 {
; this memory block contains the sprite data
; it must start on an address aligned to 64 bytes.
%option force_output ; make sure the data in this block appears in the resulting program
ubyte[128] sprites = [
%00000000,%00000000,%00000000,
%00000000,%00111100,%00000000,
%00000000,%11111111,%00000000,
%00000001,%11111101,%10000000,
%00000001,%11111111,%10000000,
%00000011,%11111111,%11000000,
%00000011,%11111111,%11000000,
%00000011,%11111111,%11000000,
%00000001,%11111111,%10000000,
%00000001,%11111111,%10000000,
%00000000,%11111111,%00000000,
%00000000,%00111100,%00000000,
%00000000,%00000000,%00000000,
%00000000,%00000000,%00000000,
%00000000,%00000000,%00000000,
%00000000,%00000000,%00000000,
%00000000,%00000000,%00000000,
%00000000,%00000000,%00000000,
%00000000,%00000000,%00000000,
%00000000,%00000000,%00000000,
%00000000,%00000000,%00000000,
0,
%00000000,%00111110,%00000000,
%00000000,%11111111,%10000000,
%00000001,%11111110,%11000000,
%00000011,%11111111,%01100000,
%00000011,%11111111,%11100000,
%00000111,%11111111,%11110000,
%00000111,%11111111,%11110000,
%00000111,%11111111,%11110000,
%00000111,%11111111,%11110000,
%00000011,%11111111,%11100000,
%00000011,%11111111,%11100000,
%00000001,%11111111,%11000000,
%00000000,%11111111,%10000000,
%00000000,%00111110,%00000000,
%00000000,%00000000,%00000000,
%00000000,%00000000,%00000000,
%00000000,%00000000,%00000000,
%00000000,%00000000,%00000000,
%00000000,%00000000,%00000000,
%00000000,%00000000,%00000000,
%00000000,%00000000,%00000000,
0
]
}
~ main {
const uword width = 255
const uword height = 200
; vertices
byte[8] xcoor = [ -100, -100, -100, -100, 100, 100, 100, 100 ]
byte[8] ycoor = [ -100, -100, 100, 100, -100, -100, 100, 100 ]
byte[8] 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
sub start() {
c64.SPENA = 255 ; enable all sprites
uword anglex
uword angley
uword anglez
word rz=33
while(true) {
c64.TIME_LO=0
rotate_vertices(msb(anglex), msb(angley), msb(anglez))
position_sprites()
anglex-=500
angley+=217
anglez+=452
c64.PLOT(0,0,0)
c64scr.print("3d cube! (sprites) ")
c64scr.print_ub(c64.TIME_LO)
c64scr.print(" jiffies/frame")
}
}
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) as word
word wsina = sin8(ax) as word
word wcosb = cos8(ay) as word
word wsinb = sin8(ay) as word
word wcosc = cos8(az) as word
word wsinc = sin8(az) as word
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
for ubyte 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])
}
}
ubyte[8] spritecolors = [1,1,7,15,12,11,9,9]
sub position_sprites() {
; set each of the 8 sprites to the correct vertex of the cube
; @todo sort vertices to sprite order so the back/front order is correct as well
for ubyte i in 0 to 7 {
word zc = rotatedz[i]
word persp = 300+zc/256
ubyte sx = rotatedx[i] / persp + width/2 as ubyte + 20
ubyte sy = rotatedy[i] / persp + height/2 as ubyte + 40
c64.SPXYW[i] = mkword(sx, sy)
if(zc < 30*128)
c64.SPRPTR[i] = $2000/64 +1 ; large ball
else
c64.SPRPTR[i] = $2000/64 ; small ball
c64.SPCOL[i] = spritecolors[zc>>13 as byte + 4] ; further away=darker color
}
}
}

View File

@ -21,10 +21,6 @@
uword angley
uword anglez
word rz=33
word persp = (rz+200)
persp = rz / 25
persp = rz / height
persp = (rz+200) / height
while(true) {
rotate_vertices(msb(anglex), msb(angley), msb(anglez))
c64scr.clear_screenchars(32)
@ -71,6 +67,8 @@
}
}
ubyte[6] vertexcolors = [1,7,7,12,11,6]
sub draw_edges() {
; plot the points of the 3d cube
@ -82,7 +80,7 @@
word persp = (rz+200) / height
byte sx = rotatedx[i] / persp as byte + width/2
byte sy = rotatedy[i] / persp as byte + height/2
c64scr.setcc(sx as ubyte, sy as ubyte, 46, i+2)
c64scr.setcc(sx as ubyte, sy as ubyte, 46, vertexcolors[(rz as byte >>5) + 3])
}
}
@ -92,7 +90,7 @@
word persp = (rz+200) / height
byte sx = rotatedx[i] / persp as byte + width/2
byte sy = rotatedy[i] / persp as byte + height/2
c64scr.setcc(sx as ubyte, sy as ubyte, 81, i+2)
c64scr.setcc(sx as ubyte, sy as ubyte, 81, vertexcolors[(rz as byte >>5) + 3])
}
}
}

View File

@ -5,113 +5,12 @@
sub start() {
ubyte ub1
ubyte ub2
ubyte ub3
ubyte ub4
byte b1
byte b2
byte b3
byte b4
word w1
word w2
word w3
word w4
uword uw1
uword uw2
uword uw3
uword uw4
float f1
float f2
float f3
float f4
memory ubyte mub1 = $c000
memory ubyte mub2 = $c000
memory ubyte mub3 = $c000
memory ubyte mub4 = $c000
memory byte mb1 = $c000
memory byte mb2 = $c000
memory byte mb3 = $c000
memory byte mb4 = $c000
memory word mw1 = $c000
memory word mw2 = $c000
memory word mw3 = $c000
memory word mw4 = $c000
memory uword muw1 = $c000
memory uword muw2 = $c000
memory uword muw3 = $c000
memory uword muw4 = $c000
memory float mf1 = $c010
memory float mf2 = $c020
memory float mf3 = $c030
memory float mf4 = $c040
str s = "hello"
byte[4] ba
ub1 = $11
ub2 = $11
ub3 = $11
mub1 = $11
mub2 = $11
mub3 = $11
ub4 = $44
mub4 = $44
b1=$11
b2=$11
b3=$11
mb1=$11
mb2=$11
mb3=$11
b4=$44
mb4=$44
w1=$1111
w2=$1111
w3=$1111
mw1=$1111
mw2=$1111
mw3=$1111
w4=$4444
mw4=$4444
uw1=$1111
uw2=$1111
uw3=$1111
muw1=$1111
muw2=$1111
muw3=$1111
uw4=$4444
muw4=$4444
f1 = 12.11
f1 = 13.11
f1 = 14.11
f1 = 15.11
f1 = 11.11
f2 = 11.11
f3 = 11.11
mf1 = 11.11
mf2 = 11.11
mf3 = 11.11
f4 = 44.44
mf4 = 44.44
c64flt.print_f(f1)
c64.CHROUT('\n')
c64flt.print_f(f2)
c64.CHROUT('\n')
c64flt.print_f(f3)
c64.CHROUT('\n')
c64flt.print_f(f4)
c64.CHROUT('\n')
c64flt.print_f(mf1)
c64.CHROUT('\n')
c64flt.print_f(mf2)
c64.CHROUT('\n')
c64flt.print_f(mf3)
c64.CHROUT('\n')
c64flt.print_f(mf4)
c64.CHROUT('\n')
float x = 33+s
x = 33+ba
}