2018-10-21 21:03:15 +00:00
|
|
|
%import c64utils
|
2018-10-02 23:11:28 +00:00
|
|
|
|
|
|
|
~ main {
|
|
|
|
|
2019-01-08 00:27:25 +00:00
|
|
|
const uword width = 40
|
|
|
|
const uword height = 25
|
2018-10-02 23:11:28 +00:00
|
|
|
|
|
|
|
; vertices
|
2019-01-08 00:27:25 +00:00
|
|
|
byte[8] xcoor = [ -40, -40, -40, -40, 40, 40, 40, 40 ]
|
|
|
|
byte[8] ycoor = [ -40, -40, 40, 40, -40, -40, 40, 40 ]
|
|
|
|
byte[8] zcoor = [ -40, 40, -40, 40, -40, 40, -40, 40 ]
|
2018-10-02 23:11:28 +00:00
|
|
|
|
|
|
|
; storage for rotated coordinates
|
2019-01-08 00:27:25 +00:00
|
|
|
word[len(xcoor)] rotatedx
|
|
|
|
word[len(ycoor)] rotatedy
|
|
|
|
word[len(zcoor)] rotatedz
|
2018-10-02 23:11:28 +00:00
|
|
|
|
|
|
|
sub start() {
|
2019-01-09 21:01:47 +00:00
|
|
|
|
2019-01-08 00:27:25 +00:00
|
|
|
uword anglex
|
|
|
|
uword angley
|
|
|
|
uword anglez
|
2019-01-09 21:01:47 +00:00
|
|
|
word rz=33
|
2019-01-08 00:27:25 +00:00
|
|
|
while(true) {
|
|
|
|
rotate_vertices(msb(anglex), msb(angley), msb(anglez))
|
|
|
|
c64scr.clear_screenchars(32)
|
|
|
|
draw_edges()
|
|
|
|
anglex+=1000
|
|
|
|
angley+=433
|
|
|
|
anglez+=907
|
|
|
|
c64.PLOT(0,0,0)
|
|
|
|
c64scr.print("3d cube! (integer) ")
|
|
|
|
c64scr.print_ub(c64.TIME_LO)
|
|
|
|
c64scr.print(" jiffies/frame")
|
|
|
|
c64.TIME_LO=0
|
2018-10-02 23:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 00:27:25 +00:00
|
|
|
sub rotate_vertices(ubyte ax, ubyte ay, ubyte az) {
|
2018-10-02 23:11:28 +00:00
|
|
|
; rotate around origin (0,0,0)
|
|
|
|
|
|
|
|
; set up the 3d rotation matrix values
|
2019-01-08 00:27:25 +00:00
|
|
|
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
|
|
|
|
|
2019-01-09 21:01:47 +00:00
|
|
|
word wcosa_sinb = wcosa*wsinb / 128
|
|
|
|
word wsina_sinb = wsina*wsinb / 128
|
2019-01-08 00:27:25 +00:00
|
|
|
|
2019-01-09 21:01:47 +00:00
|
|
|
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
|
2019-01-08 00:27:25 +00:00
|
|
|
word Azx = -wsinb
|
2019-01-09 21:01:47 +00:00
|
|
|
word Azy = wcosb*wsinc / 128
|
|
|
|
word Azz = wcosb*wcosc / 128
|
2018-10-02 23:11:28 +00:00
|
|
|
|
2018-10-10 07:21:20 +00:00
|
|
|
for ubyte i in 0 to len(xcoor)-1 {
|
2019-01-09 21:01:47 +00:00
|
|
|
rotatedx[i] = (Axx*xcoor[i] + Axy*ycoor[i] + Axz*zcoor[i]) / 128
|
2019-01-11 23:04:26 +00:00
|
|
|
rotatedy[i] = (Ayx*xcoor[i] + Ayy*ycoor[i] + Ayz*zcoor[i]) / 128
|
2019-01-09 21:01:47 +00:00
|
|
|
rotatedz[i] = (Azx*xcoor[i] + Azy*ycoor[i] + Azz*zcoor[i]) / 128
|
2018-10-02 23:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 01:25:52 +00:00
|
|
|
ubyte[6] vertexcolors = [1,7,7,12,11,6]
|
|
|
|
|
2018-10-02 23:11:28 +00:00
|
|
|
sub draw_edges() {
|
|
|
|
|
2019-01-08 00:27:25 +00:00
|
|
|
; plot the points of the 3d cube
|
|
|
|
; first the points on the back, then the points on the front (painter algorithm)
|
2018-10-02 23:11:28 +00:00
|
|
|
|
2019-01-08 00:27:25 +00:00
|
|
|
for ubyte i in 0 to len(xcoor)-1 {
|
|
|
|
word rz = rotatedz[i]
|
|
|
|
if rz >= 10 {
|
2019-01-09 21:01:47 +00:00
|
|
|
word persp = (rz+200) / height
|
|
|
|
byte sx = rotatedx[i] / persp as byte + width/2
|
|
|
|
byte sy = rotatedy[i] / persp as byte + height/2
|
2019-01-17 01:25:52 +00:00
|
|
|
c64scr.setcc(sx as ubyte, sy as ubyte, 46, vertexcolors[(rz as byte >>5) + 3])
|
2019-01-08 00:27:25 +00:00
|
|
|
}
|
2018-10-02 23:11:28 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 07:21:20 +00:00
|
|
|
for ubyte i in 0 to len(xcoor)-1 {
|
2019-01-08 00:27:25 +00:00
|
|
|
word rz = rotatedz[i]
|
|
|
|
if rz < 10 {
|
2019-01-09 21:01:47 +00:00
|
|
|
word persp = (rz+200) / height
|
|
|
|
byte sx = rotatedx[i] / persp as byte + width/2
|
|
|
|
byte sy = rotatedy[i] / persp as byte + height/2
|
2019-01-17 01:25:52 +00:00
|
|
|
c64scr.setcc(sx as ubyte, sy as ubyte, 81, vertexcolors[(rz as byte >>5) + 3])
|
2019-01-08 00:27:25 +00:00
|
|
|
}
|
2018-10-02 23:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|