2020-09-20 21:49:36 +00:00
|
|
|
%import syslib
|
|
|
|
%import textio
|
2022-04-12 21:58:19 +00:00
|
|
|
%import math
|
2018-10-02 23:11:28 +00:00
|
|
|
|
2020-11-24 23:18:07 +00:00
|
|
|
|
2019-07-29 21:11:13 +00:00
|
|
|
main {
|
2018-10-02 23:11:28 +00:00
|
|
|
|
|
|
|
; vertices
|
2023-05-28 11:51:58 +00:00
|
|
|
word[] @split xcoor = [ -40, -40, -40, -40, 40, 40, 40, 40 ]
|
|
|
|
word[] @split ycoor = [ -40, -40, 40, 40, -40, -40, 40, 40 ]
|
|
|
|
word[] @split zcoor = [ -40, 40, -40, 40, -40, 40, -40, 40 ]
|
2018-10-02 23:11:28 +00:00
|
|
|
|
|
|
|
; storage for rotated coordinates
|
2023-05-28 21:19:01 +00:00
|
|
|
word[len(xcoor)] @split rotatedx
|
|
|
|
word[len(ycoor)] @split rotatedy
|
|
|
|
word[len(zcoor)] @split 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
|
2020-07-25 14:25:02 +00:00
|
|
|
repeat {
|
2019-01-08 00:27:25 +00:00
|
|
|
rotate_vertices(msb(anglex), msb(angley), msb(anglez))
|
2020-08-27 16:10:22 +00:00
|
|
|
txt.clear_screenchars(32)
|
2019-01-08 00:27:25 +00:00
|
|
|
draw_edges()
|
|
|
|
anglex+=1000
|
|
|
|
angley+=433
|
|
|
|
anglez+=907
|
2020-08-27 16:10:22 +00:00
|
|
|
txt.plot(0,0)
|
|
|
|
txt.print("3d cube! ")
|
2023-04-28 21:13:03 +00:00
|
|
|
txt.print_ub(cbm.TIME_LO)
|
2020-08-27 16:10:22 +00:00
|
|
|
txt.print(" jiffies/fr = ")
|
2023-04-28 21:13:03 +00:00
|
|
|
txt.print_ub(60/cbm.TIME_LO)
|
2020-08-27 16:10:22 +00:00
|
|
|
txt.print(" fps")
|
2023-04-28 21:13:03 +00:00
|
|
|
cbm.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
|
2022-04-12 21:58:19 +00:00
|
|
|
word wcosa = math.cos8(ax)
|
|
|
|
word wsina = math.sin8(ax)
|
|
|
|
word wcosb = math.cos8(ay)
|
|
|
|
word wsinb = math.sin8(ay)
|
|
|
|
word wcosc = math.cos8(az)
|
|
|
|
word wsinc = math.sin8(az)
|
2019-01-08 00:27:25 +00:00
|
|
|
|
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
|
|
|
|
2020-09-04 22:17:58 +00:00
|
|
|
ubyte @zp i
|
2019-08-18 01:16:23 +00:00
|
|
|
for i in 0 to len(xcoor)-1 {
|
2019-08-17 09:49:30 +00:00
|
|
|
; don't normalize by dividing by 128, instead keep some precision for perspective calc later
|
2020-03-11 19:47:42 +00:00
|
|
|
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]
|
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
|
|
|
|
2020-09-04 22:17:58 +00:00
|
|
|
ubyte @zp i
|
|
|
|
word @zp rz
|
|
|
|
word @zp persp
|
2019-08-05 19:00:55 +00:00
|
|
|
byte sx
|
|
|
|
byte sy
|
|
|
|
|
|
|
|
for i in 0 to len(xcoor)-1 {
|
|
|
|
rz = rotatedz[i]
|
2019-01-08 00:27:25 +00:00
|
|
|
if rz >= 10 {
|
2019-08-17 09:49:30 +00:00
|
|
|
persp = 900 + rz/32
|
2020-09-20 21:49:36 +00:00
|
|
|
sx = rotatedx[i] / persp as byte + txt.DEFAULT_WIDTH/2
|
|
|
|
sy = rotatedy[i] / persp as byte + txt.DEFAULT_HEIGHT/2
|
2020-08-27 16:10:22 +00:00
|
|
|
txt.setcc(sx as ubyte, sy as ubyte, 46, 7)
|
2019-01-08 00:27:25 +00:00
|
|
|
}
|
2018-10-02 23:11:28 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 19:00:55 +00:00
|
|
|
for i in 0 to len(xcoor)-1 {
|
|
|
|
rz = rotatedz[i]
|
2019-01-08 00:27:25 +00:00
|
|
|
if rz < 10 {
|
2019-08-17 09:49:30 +00:00
|
|
|
persp = 900 + rz/32
|
2020-09-20 21:49:36 +00:00
|
|
|
sx = rotatedx[i] / persp as byte + txt.DEFAULT_WIDTH/2
|
|
|
|
sy = rotatedy[i] / persp as byte + txt.DEFAULT_HEIGHT/2
|
2020-08-27 16:10:22 +00:00
|
|
|
txt.setcc(sx as ubyte, sy as ubyte, 81, 7)
|
2019-01-08 00:27:25 +00:00
|
|
|
}
|
2018-10-02 23:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|