Sweepit Demo
Posted: Wed Apr 08, 2015 2:49 am
Code: Select all
rem Sweepit Demo
rem iPhone 6 Plus / 8.3 b4
rem Enjoy...
pi=3.1415
size=50 'circle loading size
graphics
sw=screen_width()
sh=screen_height()
graphics clear 100/255,100/255,150/255
fill color 0,0,0
fill rect 0,0 to sw,sh-20
'Set up the Sine and Cosine Arrays
dim co(362),si(362)
for a=0 to 361
co(a)=cos(a*(pi/180)) ! si(a)=sin(a*(pi/180))
next a
'Set up the scrolltext
for a=1 to 65
m$=m$&" "
next a
m$=m$&"Welcome to another 3d demo that's a bit "
m$=m$&"diffrent. This time I thought I'd have a go at "
m$=m$&"something pre-calculated, so I had some fun with a"
m$=m$&" sweep object."
mx=-300
mlen=len(m$)
'Grab the object outline edge
read points
dim bx(points+1),by(points+1)
for a=1 to points
read bx(a),by(a)
next a
'Determine detail and prepare arrays
read sweep
mp=points*sweep
nf=points*sweep
a=170*nf
angstep=360/sweep
dim px(mp+1),py(mp+1),pz(mp+1),sx(mp+1),sy(mp+1),sz(mp+1),f1(nf+1)
dim f2(nf+1),f3(nf+1),f4(nf+1),fv(nf+1),fl(nf+1),fz(nf+1),p1x(a+1)
dim p2x(a+1),p3x(a+1),p4x(a+1),p1y(a+1),p2y(a+1),p3y(a+1),p4y(a+1)
dim p1b(a+1),p2b(a+1),p3b(a+1),p4b(a+1),fra(362)
REM Display message about delay
draw color 200/255,200/255,1
draw text "Processing Object" at 0,sh-20
'Sweep the points
for p=1 to points
'Sweep the points to make object
for a=0 to sweep-1
ang=a*angstep
x=bx(p) ! y=by(p)
px(p+(a*points))=si(ang)*x*40
py(p+(a*points))=-y*40
pz(p+(a*points))=co(ang)*x*40
next a
next p
'Make the faces
for f=1 to points-1
'Join the points to make faces
for a=0 to sweep-1
if a<sweep-1 then
'Not the final sweep
amul=(a*(points))
f1(f+amul)=f+amul
f2(f+amul)=f+amul+points
f3(f+amul)=f+amul+1+points
f4(f+amul)=f+amul+1
else
amul=(a*(points))
f1(f+amul)=f+amul
f2(f+amul)=f
f3(f+amul)=f+1
f4(f+amul)=f+amul+1
end if
next a
next f
'Create Animation
for angle=1 to 360
'Process and record animation
yr=(yr+2)%360
xr=(xr+3)%360
'Rotate the points into position
for p=1 to mp
xx=px(p) 'Get the points and rotate
y=py(p)
z=pz(p)
yy=y*co(xr)+z*si(xr)
zz=z*co(xr)-y*si(xr)
x=xx*co(yr)-zz*si(yr)
z=xx*si(yr)+zz*co(yr)
x=x/((z/2000)+1)
yy=yy/((z/2000)+1)
'Store the points
sx(p)=x+sw/2 'center x
sy(p)=yy+sh/2 'cenyer y
sz(p)=z 'depth
next p
'Check faces for visibility
' and store visible ones
fp=0
'Reset the frame face count
for f=1 to nf
'Loop through faces
gosub gf
'Get the face and check vis.
v1x=x1-x2 ! v1y=y1-y2
v2x=x3-x2 ! v2y=y3-y2
vi=v1x*v2y-v2x*v1y ! fv(f)=vi
if vi>0 then
'If the face is visible then
fp=fp+1
'add one to the count
fl(fp)=f
fz(f)=(z1+z2+z3+z4)/4
end if
next f
fra(angle)=fp
'Store the frame face count
'Bubble sort the stored faces
for f=1 to fp-1
'Bubble sort face list by Z
for a=f+1 to fp
if fz(fl(f))<fz(fl(a)) then
t=fl(f)
'Bubble swap faces
fl(f)=fl(a)
fl(a)=t
end if
next a
next f
'Go through sorted faces and store them fully
for o=1 to fp
'Store the face details
f=fl(o)
'For drawing later
gosub gf
'Get the face details
b=-((z1+z2+z3+z4)/4)+100
'Set the brightness
fct=fct+1
'Add one to face count
'Store the face polygon and vertex brightness
p1x(fct)=x1 ! p1y(fct)=y1
p2x(fct)=x2 ! p2y(fct)=y2
p3x(fct)=x3 ! p3y(fct)=y3
p4x(fct)=x4 ! p4y(fct)=y4
p1b(fct)=-z1 ! p2b(fct)=-z2
p3b(fct)=-z3 ! p4b(fct)=-z4
next o
'Processing takes time, draw a ball to show we're doing something
fill color 200/255,150/255,0
'Draw a circle to keep interest
x1=si(angle)*size ! y1=-co(angle)*size
x2=si(angle+1)*size ! y2=-co(angle+1)*size
'triangle 0,0 to x1,y1 to x2,y2
trix(0)=sw/2
triy(0)=sh/2
trix(1)=x1+sw/2
triy(1)=y1+sh/2
trix(2)=x2+sw/2
triy(2)=y2+sh/2
fill poly trix,triy count 3
next angle
'Get ready to replay the animation
fct=1 'Reset face position
a=1 'Reset angle
'The main routine, time to play
loop:
refresh off
fill color 0,50/255,80/255
fill rect 0,0 to sw,sh-20
gosub drawobj
fill color 0,150/255,180/255
fill rect 0,sh-20 to sw,sh
REM Clear scroll area
draw color 200/255,200/255,1
REM Draw the scroller
mx=mx-2
if mx>-330 then goto write
mx=mx+10
mpos=(mpos+1)%mlen
write:
draw text mid$(m$,mpos,65) at mx,sh-20
refresh on
goto loop
'Draw a frame of the animation
drawobj:
for c=1 to fra(a) 'Loop through the polygons
'Get the face polygon and vertex brightness
x1=p1x(fct) ! y1=p1y(fct)
x2=p2x(fct) ! y2=p2y(fct)
x3=p3x(fct) ! y3=p3y(fct)
x4=p4x(fct) ! y4=p4y(fct)
b1=p1b(fct) ! b2=p2b(fct)
b3=p3b(fct) ! b4=p4b(fct)
fct=fct+1 'Move to the next face
fill color (b1+100)/255,(b1+50)/255,0
'Draw the face
'triangle x1,y1 to x3,y3 to x2,y2
trix(0)=x1
triy(0)=y1
trix(1)=x3
triy(1)=y3
trix(2)=x2
triy(2)=y2
fill poly trix,triy count 3
'triangle x1,y1 to x3,y3 to x4,y4
trix(0)=x1
triy(0)=y1
trix(1)=x3
triy(1)=y3
trix(2)=x4
triy(2)=y4
fill poly trix,triy count 3
next c
a=a+1
'Next angle
if a=361 then
'All done? Reset
fct=1
a=1
end if
return
'Get the face structural details
gf:
'Get face details routine
p1=f1(f) ! p2=f2(f)
p3=f3(f) ! p4=f4(f)
x1=sx(p1) ! y1=sy(p1)
z1=sz(p1) ! x2=sx(p2)
y2=sy(p2) ! z2=sz(p2)
x3=sx(p3) ! y3=sy(p3)
z3=sz(p3) ! x4=sx(p4)
y4=sy(p4) ! z4=sz(p4)
return
'The object definition, small isn't it
'To make your own objects, define an
'X and Y edge like this: %%%
' %%% %%%
' %%% o %%%
' %%% o %%%
' %%% o %%%
' %%% o %%%
' %%% o %%%
' %%% o %%%
' %%% o %%%
' %%% o <----- Y Centre %%%
' %%% o %%%
' %%% o %%%
' %%% o %%%
' %%% o %%%
' %%% o %%%
' %%% o %%%
' %%% o %%%
' %%% %%%
'Just store the points as X and Y values
'from a center position (X is 0 on the left
'hand side and Y is 0 in the middle)
'Once you've done that, decide how many
'sweeps are to be done, and run the demo.
data 15 'How many points make the edge
data 0.001,2 'Always Start very close to 0
data 1,2.3
data 1.5,2.6
data 1.8,4
data 1.9,2.6
data 1.7,2.4
data 1.5,2
data 0.5,1.8
data 0.8,1.5
data 0.5,1.1
data 0.7,-2
data 1,-2.3
data 2,-2.8
data 2,-3
data 0.001,-3 'Always End very close to 0
data 10 'How many sweeps