Page 1 of 1

3D Star Cube

Posted: Wed Mar 25, 2015 6:23 pm
by DrChip

Code: Select all

rem 3D Star Cube
rem iPhone 6 Plus / 8.3 b4
'it has a lightsourced, cell shaded cube and 133 3d stars
'I used this to teach my son. 
'Enjoy...

gosub init

loop:
refresh off
graphics clear 4/255,10/255,4/255
for a=1 to str
col=255-sz(a)*30
fill color 50/255,col/255,50/255
xt=(sx(a)/sz(a))+sw/2
yt=(sy(a)/sz(a))+sh/2

fill rect xt,yt to xt+(10-sz(a)),yt+(10-sz(a))
sz(a)=sz(a)-.1
if sz(a)<0 then sz(a)=sz(a)+10
next a
mm=mm+.04 
size=7+5*sin(mm)
gosub rotate
r=20+19*sin(mm/3) 
g=20+19*cos(mm/4) 
b=20+19*sin(mm/7)
f1=1 ! f2=2 ! f3=3 ! f4=4 ! gosub draw
f1=5 ! f2=8 ! f3=7 ! f4=6 ! gosub draw
f1=6 ! f2=2 ! f3=1 ! f4=5 ! gosub draw
f1=8 ! f2=4 ! f3=3 ! f4=7 ! gosub draw
f1=2 ! f2=6 ! f3=7 ! f4=3 ! gosub draw
f1=8 ! f2=5 ! f3=1 ! f4=4 ! gosub draw
draw color 0,0,0 
draw text "3D CUBE DEMO" at 0,0
draw color 1,1,1
draw text "3D CUBE DEMO" at 1,1
refresh on
goto loop

draw: 
vx1=tx(f1)-tx(f2)
vy1=ty(f1)-ty(f2)
vx2=tx(f3)-tx(f2)
vy2=ty(f3)-ty(f2)
  if  (vx1*vy2-vx2*vy1)<0 then
light=-(tz(f1)+tz(f2)+tz(f3)+tz(f4))*7
fill color (r+light)/255,(g+light)/255,(b+light)/255
'fill triangle tx(f1),ty(f1) to tx(f2),ty(f2) to tx(f3),ty(f3)
trix(0)=tx(f1)
triy(0)=ty(f1)
trix(1)=tx(f2)
triy(1)=ty(f2)
trix(2)=tx(f3)
triy(2)=ty(f3)
fill poly trix,triy count 3
'fill triangle tx(f1),ty(f1) to tx(f4),ty(f4) to tx(f3),ty(f3)
trix(0)=tx(f1)
triy(0)=ty(f1)
trix(1)=tx(f4)
triy(1)=ty(f4)
trix(2)=tx(f3)
triy(2)=ty(f3)
fill poly trix,triy count 3
draw color 0,0,0
draw line tx(f1),ty(f1) to tx(f2),ty(f2)
draw line tx(f2),ty(f2) to tx(f3),ty(f3)
draw line tx(f3),ty(f3) to tx(f4),ty(f4)
draw line tx(f4),ty(f4) to tx(f1),ty(f1)
end if
return

rotate:
for a=1 to polys
  x1=x(a)!y1=y(a)!z1=z(a)
  xx=x1
  yy=y1*cs(xr)+z1*sn(xr)
  zz=z1*cs(xr)-y1*sn(xr)
  y1=yy
  x1=xx*cs(yr)-zz*sn(yr)
  z1=xx*sn(yr)+zz*cs(yr)
  zz=z1
  xx=x1*cs(zr)-y1*sn(zr)
  yy=x1*sn(zr)+y1*cs(zr)
  xx=size*(xx/((zz/50)+1))+sw/2
  yy=size*(yy/((zz/50)+1))+sh/2
  tx(a)=xx
  ty(a)=yy
  tz(a)=zz
next a
xr=xr+1
yr=yr+2
zr=zr+2
if xr>360 then xr=xr-360
if yr>360 then yr=yr-360
if zr>360 then zr=zr-360
return


init:
graphics
sw=screen_width()
sh=screen_height()
pi=3.1415

draw color 4/255,10/255,4/255
polys=8 
dim x(polys+1),y(polys+1),z(polys+1)
dim tx(polys+1),ty(polys+1),tz(polys+1)
str=133
dim sx(str+1),sy(str+1),sz(str+1)
mm=1
for a=1 to str 
sx(a)=-1000+rnd(2000)
sy(a)=-1000+rnd(2000)
sz(a)=mm
mm=mm+10/str
next a
dim cs(361),sn(361)
for ang=0 to 360
  cs(ang)=cos(ang*(pi/180))
  sn(ang)=sin(ang*(pi/180))
next ang
for a=1 to polys
  read x(a),y(a),z(a)
next a

data -10,10,10,10,10,10,10,-10,10,-10,-10,10,-10,10,-10,10,10,-10,10,-10,-10,-10,-10,-10
return