Fast Texture Cube
Posted: Sun Apr 05, 2015 11:56 pm
Code: Select all
rem Fast 3d textured cube demo
rem iPhone 6 Plus / 8.3 beta 4
pi=3.1415
focus = 200
gosub setup
loop:
refresh off
graphics clear
draw color .5,.5,.5
draw text "Fast Texture Cube" at 0,0
draw color 0,0,0
draw text "Fast Texture Cube" at 1,1
gosub control
gosub rotate
gosub draw
refresh on
goto loop
draw:
for i=1 to mf
p1=ap(i,1)
p2=ap(i,2)
p3=ap(i,3)
if (sx(p2)-sx(p1))*(sy(p3)-sy(p1))-(sx(p3)-sx(p1))*(sy(p2)-sy(p1))>0 then goto nopoly
if face(i)=1 then
atx(1,1)=0
aty(1,1)=0
atx(2,1)=8
aty(2,1)=0
atx(3,1)=8
aty(3,1)=8
else
atx(1,1)=8
aty(1,1)=8
atx(2,1)=0
aty(2,1)=8
atx(3,1)=0
aty(3,1)=0
end if
itex=tex(i)
avx(1,1)=arx(p1)
avy(1,1)=ary(p1)
avz(1,1)=arz(p1)
avx(2,1)=arx(p2)
avy(2,1)=ary(p2)
avz(2,1)=arz(p2)
avx(3,1)=arx(p3)
avy(3,1)=ary(p3)
avz(3,1)=arz(p3)
gosub transform
nopoly:
next i
return
transform:
level=level+1
if level<4 then goto nl
z=focus/avz(1,level)
sx1=avx(1,level)*z+sw/2
sy1=avy(1,level)*z+sh/2
z=focus/avz(2,level)
sx2=avx(2,level)*z+sw/2
sy2=avy(2,level)*z+sh/2
z=focus/avz(3,level)
sx3=avx(3,level)*z+sw/2
sy3=avy(3,level)*z+sh/2
itx=atx(1,level)!ity=aty(1,level)
r1=cr(itx,ity,itex)
g1=cg(itx,ity,itex)
b1=cb(itx,ity,itex)
itx=atx(2,level)!ity=aty(2,level)
r2=cr(itx,ity,itex)
g2=cg(itx,ity,itex)
b2=cb(itx,ity,itex)
itx=atx(3,level)!ity=aty(3,level)
r3=cr(itx,ity,itex)
g3=cg(itx,ity,itex)
b3=cb(itx,ity,itex)
fill color r1/255,g1/255,b1/255
'triangle sx1,sy1 to sx2,sy2 to sx3,sy3
trix(0)=sx1
triy(0)=sy1
trix(1)=sx2
triy(1)=sy2
trix(2)=sx3
triy(2)=sy3
fill poly trix,triy count 3
goto n
nl:
avx(4,level)=(avx(1,level)+avx(2,level))/2
avy(4,level)=(avy(1,level)+avy(2,level))/2
avz(4,level)=(avz(1,level)+avz(2,level))/2
avx(5,level)=(avx(2,level)+avx(3,level))/2
avy(5,level)=(avy(2,level)+avy(3,level))/2
avz(5,level)=(avz(2,level)+avz(3,level))/2
avx(6,level)=(avx(3,level)+avx(1,level))/2
avy(6,level)=(avy(3,level)+avy(1,level))/2
avz(6,level)=(avz(3,level)+avz(1,level))/2
atx(4,level)=(atx(1,level)+atx(2,level))/2
aty(4,level)=(aty(1,level)+aty(2,level))/2
atx(5,level)=(atx(2,level)+atx(3,level))/2
aty(5,level)=(aty(2,level)+aty(3,level))/2
atx(6,level)=(atx(3,level)+atx(1,level))/2
aty(6,level)=(aty(3,level)+aty(1,level))/2
avx(1,1+level)=avx(1,level)
avy(1,1+level)=avy(1,level)
avz(1,1+level)=avz(1,level)
avx(2,1+level)=avx(4,level)
avy(2,1+level)=avy(4,level)
avz(2,1+level)=avz(4,level)
avx(3,1+level)=avx(6,level)
avy(3,1+level)=avy(6,level)
avz(3,1+level)=avz(6,level)
atx(1,1+level)=atx(1,level)
aty(1,1+level)=aty(1,level)
atx(2,1+level)=atx(4,level)
aty(2,1+level)=aty(4,level)
atx(3,1+level)=atx(6,level)
aty(3,1+level)=aty(6,level)
gosub transform
avx(1,1+level)=avx(5,level)
avy(1,1+level)=avy(5,level)
avz(1,1+level)=avz(5,level)
atx(1,1+level)=atx(5,level)
aty(1,1+level)=aty(5,level)
gosub transform
avx(3,1+level)=avx(2,level)
avy(3,1+level)=avy(2,level)
avz(3,1+level)=avz(2,level)
atx(3,1+level)=atx(2,level)
aty(3,1+level)=aty(2,level)
gosub transform
avx(2,1+level)=avx(3,level)
avy(2,1+level)=avy(3,level)
avz(2,1+level)=avz(3,level)
avx(3,1+level)=avx(6,level)
avy(3,1+level)=avy(6,level)
avz(3,1+level)=avz(6,level)
atx(2,1+level)=atx(3,level)
aty(2,1+level)=aty(3,level)
atx(3,1+level)=atx(6,level)
aty(3,1+level)=aty(6,level)
gosub transform
n:
level=level-1
return
control:
a=(a+3)%360
b=(b+2)%360
c=(c+1)%360
return
setup:
graphics
sw=screen_width()
sh=screen_height()
pi=3.1415
read mp,mf
dim avx(11,11),avz(11,11),avy(11,11)
dim atx(11,11),aty(11,11)
dim cr(101,101,7),cb(101,101,7),cg(101,101,7)
dim sx(mp+1),sy(mp+1)
dim ax(mp+1),ay(mp+1),az(mp+1),ap(mf+1,4),tex(mf+1),face(mf+1)
dim arx(mp+1),ary(mp+1),arz(mp+1)
dim sn(361),cs(361)
for i=0 to 360
cs(i)=cos(i/180*pi)
sn(i)=sin(i/180*pi)
next i
for y=0 to 31
for x=0 to 31
cr(x,y,1)=255*and(x+y,1)
cg(x,y,1)=255*and(x,1)
cb(x,y,1)=255*and(y,1)
cr(x,y,2)=255*and(x+y,1)
cg(x,y,2)=255*and(x+y,1)
cb(x,y,2)=255*and(x+y,1)
cr(x,y,3)=100+100*sn((y*40)%360)*cs((x*50)%360)
cg(x,y,3)=0
cb(x,y,3)=3
cr(x,y,4)=rm+rnd(250)
cg(x,y,4)=gm+rnd(250)
cb(x,y,4)=bm+rnd(250)
cr(x,y,5)=100+100*sn((y*60)%360)*cs((x*70)%360)
cg(x,y,5)=100+100*sn((y*90)%360)*cs((x*20)%360)
cb(x,y,5)=0
cr(x,y,6)=100+100*sn((y*60)%360)*cs((x*70)%360)
cg(x,y,6)=0
cb(x,y,6)=255*and(x,1)
next x
next y
for i=1 to mp
read ax(i),ay(i),az(i)
next i
for i=1 to mf
read ap(i,1),ap(i,2),ap(i,3),tex(i),face(i)
next i
a=0
b=0
c=0
return
rotate:
csa=cs(a)!sna=sn(a)
csb=cs(b)!snb=sn(b)
csc=cs(c)!snc=sn(c)
for i=1 to mp
itz=az(i)*csa+ax(i)*sna
itx=ax(i)*csa-az(i)*sna
rx=itx*csb+ay(i) *snb
ity=ay(i)*csb-itx*snb
rz=300+itz*csc-ity*snc
ry=ity*csc+itz *snc
z=focus/rz
sx(i)=rx*z
sy(i)=ry*z
arx(i)=rx
ary(i)=ry
arz(i)=rz
next i
return
data 8,12
data -100,-100,-100
data -100,-100,100
data 100,-100,100
data 100,-100,-100
data -100,100,-100
data -100,100,100
data 100,100,100
data 100,100,-100
data 1,2,6,1,1,6,5,1,1,2
data 2,3,7,2,1,7,6,2,2,2
data 3,4,8,3,1,8,7,3,3,2
data 4,1,5,4,1,5,8,4,4,2
data 5,6,7,5,1,7,8,5,5,2
data 1,4,3,6,1,3,2,1,6,2