Thanks Latch, I've changed it to an array
rem initialise program
x=0 : y=1 : z=2
gosub _arrays
gosub _data
DIM screen(1)
screen(x)=1024 : screen(y)=768
set display mode screen(x),screen(y),32
hide mouse
sync on : sync rate 30
wireframe= 0 : cam(x)= 0 : cam(y)= 10 : cam(z)= -50
`------------
` Main Loop
`------------
Do
for a= 0 to 359 step 2
rem rotate object
` obja(1,x)= a+60
obja(1,y)= a
rem draw object
draw_object(1,wireframe)
text 0,0,"Loop"
sync:cls 128
next a
Loop
END
`---------
` Gosubs
`---------
`---Arrays---
_arrays:
rem array sizes
maxpolygons = 16
maxvertices = 9
maxobjects = 2
`--Camera--
rem camera array - stores 3D position of the camera
Dim cam(2)
`--Vertices--
rem vertex array - stores 3D position of vertices
Dim v(maxvertices,2)
rem temp array to store vertex on screen coords
dim vpos(2,1)
`--Polygons--
rem polygon array - idexes vertices that for the polygon
Dim polyv(maxpolygons,2)
rem polygon array - stores the colour of the polygon's vertices
Dim polyc(maxpolygons,2)
`--Objects--
rem object array - stores 3D position of the object
Dim objpos(maxobjects,2)
rem object array - stores 3D angle of the object
Dim obja(maxobjects,2)
rem object array - stores how many polyygons make up the object
Dim objfaces(maxobjects)
rem object array - indexes the polygons that make up the object
Dim objpoly(maxobjects,15)
Return
`---Data---
_data:
`--Vertices--
rem square base (clockwise)
v(1,x)= -10 : v(1,y)= 0 : v(1,z)= 10
v(2,x)= 10 : v(2,y)= 0 : v(2,z)= 10
v(3,x)= 10 : v(3,y)= 0 : v(3,z)= -10
v(4,x)= -10 : v(4,y)= 0 : v(4,z)= -10
rem square top (clockwise)
v(5,x)= -10 : v(5,y)= 20 : v(5,z)= 10
v(6,x)= 10 : v(6,y)= 20 : v(6,z)= 10
v(7,x)= 10 : v(7,y)= 20 : v(7,z)= -10
v(8,x)= -10 : v(8,y)= 20 : v(8,z)= -10
rem pyramid point
v(9,x)= 0 : v(9,y)= 20 : v(9,z)= 0
`--Polygons--
rem square base
polyv(1,0)= 1
polyv(1,1)= 2
polyv(1,2)= 3
polyv(2,0)= 2
polyv(2,1)= 3
polyv(2,2)= 4
rem back pyramid slope
polyv(3,0)= 1
polyv(3,1)= 2
polyv(3,2)= 9
rem right pyramid slope
polyv(4,0)= 2
polyv(4,1)= 3
polyv(4,2)= 9
rem front pyramid slope
polyv(5,0)= 3
polyv(5,1)= 4
polyv(5,2)= 9
rem left pyramid slope
polyv(6,0)= 4
polyv(6,1)= 1
polyv(6,2)= 9
`--Objects--
rem Pyramid
objpos(1,x)= 0 : objpos(1,y)= 0 : objpos(1,z)= 0
obja(1,x)= 0 : obja(1,y)= 0 : obja(1,z)= 0
objfaces(1)= 6
rem base
objpoly(1,1)= 1
objpoly(1,2)= 2
rem slopes
objpoly(1,3)= 3
objpoly(1,4)= 4
objpoly(1,5)= 5
objpoly(1,6)= 6
return
`------------
` Functions
`------------
`---Tier 3---
Function draw_object(ob,wire)
for p= 1 to objfaces(ob)
draw_poly(ob,objpoly(ob,p),wire)
next p
Endfunction
`---Tier 2---
Function draw_poly(ob,p,wire)
x=0 : y=1 : z=2
rem object data
obx= objpos(ob,x) : oby= objpos(ob,y) : obz= objpos(ob,z)
obax= obja(ob,x) : obay= obja(ob,y) : obaz= obja(ob,z)
for v= 0 to 2
vx= v(polyv(p,v),x)
vy= v(polyv(p,v),y)
vz= v(polyv(p,v),z)
rem rotation
newy= cos(obax)*vy + sin(obax)*vz
newz= cos(obax)*vz - sin(obax)*vy
newx= cos(obay)*vx - sin(obay)*newz
newz= cos(obay)*newz - sin(obay)*vx
rem A=camera B=screen edge C=screen centre
AC= (newz + obz) - cam(z)
BC= AC * tan(31)
rem in screen?
if AC >0
xdif= (newx + obx) - cam(x)
ydif= (newy + oby) - cam(y)
rem store screen positions
vpos(v,x)= (screen(x)/2) + (screen(y)/BC)*xdif
vpos(v,y)= (screen(y)/2) - (screen(y)/BC)*ydif
endif
next v
rem draw triangle
` ink polyc(f,0),0
ink 255*65536,0
triangle(vpos(0,x),vpos(0,y),vpos(1,x),vpos(1,y),vpos(2,x),vpos(2,y))
Endfunction
`---Tier 1---
Function triangle(ax,ay,bx,by,cx,cy)
line ax,ay,bx,by
line ax,ay,cx,cy
line bx,by,cx,cy
Endfunction
It works but it doesn't seem to be recalculating the perspective because when the pyramid rotates the furthest points look skewed as they move toward the camera. I had this exact problem the first time I wrote the program but can't remember how I fixed it.
A small program that works is better than a large one that doesn't.
DBC Challenge Rank:
Rookie