2020-09-20 21:49:36 +00:00
|
|
|
%import syslib
|
2020-09-22 00:12:01 +00:00
|
|
|
%import graphics
|
2022-04-12 21:58:19 +00:00
|
|
|
%import math
|
2020-09-20 21:49:36 +00:00
|
|
|
|
2023-06-03 17:14:45 +00:00
|
|
|
; Note: this program can be compiled for multiple target systems.
|
2020-08-22 17:00:03 +00:00
|
|
|
|
|
|
|
main {
|
|
|
|
|
|
|
|
; vertices
|
2023-05-28 11:51:58 +00:00
|
|
|
word[] @split xcoor = [ -100, -100, -100, -100, 100, 100, 100, 100 ]
|
|
|
|
word[] @split ycoor = [ -100, -100, 100, 100, -100, -100, 100, 100 ]
|
|
|
|
word[] @split zcoor = [ -100, 100, -100, 100, -100, 100, -100, 100 ]
|
2020-08-22 17:00:03 +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
|
2020-08-22 17:00:03 +00:00
|
|
|
|
|
|
|
; 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))
|
2020-08-28 19:42:53 +00:00
|
|
|
graphics.clear_screen(1, 0)
|
2020-08-22 17:00:03 +00:00
|
|
|
draw_lines()
|
|
|
|
anglex-=500
|
|
|
|
angley+=217
|
|
|
|
anglez+=452
|
|
|
|
|
2024-02-19 23:35:13 +00:00
|
|
|
;sys.waitvsync()
|
|
|
|
;sys.waitvsync()
|
2020-08-22 17:00:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rotate_vertices(ubyte ax, ubyte ay, ubyte az) {
|
|
|
|
; 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)
|
2020-08-22 17:00:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-09-04 22:17:58 +00:00
|
|
|
ubyte @zp i
|
2020-08-22 17:00:03 +00:00
|
|
|
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() {
|
2020-09-04 22:17:58 +00:00
|
|
|
ubyte @zp i
|
2020-08-22 17:00:03 +00:00
|
|
|
for i in len(edgesFrom) -1 downto 0 {
|
2020-09-04 22:17:58 +00:00
|
|
|
ubyte @zp vFrom = edgesFrom[i]
|
2020-09-19 13:46:51 +00:00
|
|
|
ubyte @zp vTo = edgesTo[i]
|
2020-09-04 22:17:58 +00:00
|
|
|
word @zp persp1 = 256 + rotatedz[vFrom]/256
|
|
|
|
word @zp persp2 = 256 + rotatedz[vTo]/256
|
2020-09-22 19:50:56 +00:00
|
|
|
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)
|
2020-08-22 17:00:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|