'+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
'+          Texture routine by Gareth Bailey             +
'+         Credit to Mark Geurds for textures            +
'+                    September 2002                     +
'+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +

gosub setup

label loop
  clear window
  gosub drawcube
  gosub rotate
  gosub db
goto loop

label drawcube
for i=1 to 6
c1=c1(i):c2=c2(i)
c3=c3(i):c4=c4(i)
gosub cp
if cp<0 goto skip2
gosub fillface
label skip2
next i
return

label fillface
ax2=(xx(c2)-xx(c3))/sprx
ay2=(yy(c2)-yy(c3))/spry
ax3=(xx(c4)-xx(c1))/sprx
ay3=(yy(c4)-yy(c1))/spry

for a=0 to sprx
 ta3=(ax3*a)+xx(c1)
 tb3=(ay3*a)+yy(c1)
 ta2=(ax2*a)+xx(c3)
 tb2=(ay2*a)+yy(c3)

 tx=(ta3-ta2)/sprx
 ty=(tb3-tb2)/spry
 
 for b=0 to spry
  gx(a,b)=(tx*b)+ta2
  gy(a,b)=(ty*b)+tb2
 next b
next a

for b=0 to spry-1
for a=0 to sprx-1
if texture(a,b)>0 then

B = and(texture(a,b),16711680)/65536            REM - CREDIT TO Mark Geurds 
G = mod(and(texture(a,b),65280)/256,256)        REM - FOR COLOUR AND TEXTURE
R = mod(texture(a,b),256)                       REM - CODE
setrgb 1, R,G,B

fill triangle gx(a,b),gy(a,b) to gx(a+1,b),gy(a+1,b) to gx(a,b+1),gy(a,b+1)
fill triangle gx(a+1,b+1),gy(a+1,b+1) to gx(a+1,b),gy(a+1,b) to gx(a,b+1),gy(a,b+1)
fi
next a
next b
return

label cp
cp=(xx(c1)-xx(c2))*(yy(c3)-yy(c2))-(xx(c3)-xx(c2))*(yy(c1)-yy(c2))
return

label rotate
xr=xr+1:if xr>360 xr=xr-360
yr=yr+2:if yr>360 yr=yr-360
zr=zr+1:if zr>360 zr=zr-360
s=s+1 :if s>360 s=s-360
sz=sine(s)

for a=1 to points
x1=x(a)
 y1=y(a)
 z1=z(a)
 xx=x1
 yy=y1*cosi(xr)+z1*sine(xr)
 zz=z1*cosi(xr)-y1*sine(xr)
 y1=yy
 x1=xx*cosi(yr)-zz*sine(yr)
 z1=xx*sine(yr)+zz*cosi(yr)
 zz=z1
 xx=x1*cosi(zr)-y1*sine(zr)
 yy=x1*sine(zr)+y1*cosi(zr)
 dx(a)=xx
 dy(a)=yy
 dz(a)=zz
 xx(a)=sz*((dx(a)/((dz(a)/focus)+1)))+cx
 yy(a)=sz*((dy(a)/((dz(a)/focus)+1)))+cy
next a
return

label db
setdispbuf draw
draw =1-draw
setdrawbuf draw
return

'=========================================================
label setup
open window 640,512

points=8: focus=500: cx=320: cy=256
dim sine(361),cosi(361)
dim x(points),y(points),z(points),xx(points),yy(points)
dim dx(points),dy(points),dz(points)

for a=0 to 361
  cosi(a)=cos(a*(pi/180))
  sine(a)=sin(a*(pi/180))
next

for a=1 to points 
  read x(a)
  read y(a)
  read z(a)
next a

dim c1(6),c2(6),c3(6),c4(6)
for a=1 to 6
read c1(a),c2(a),c3(a),c4(a)
next a

read sprx
read spry
read filter
dim gx(sprx,sprx),gy(sprx,sprx)
dim texture(sprx,spry)

for b=0 to spry rem -1
for a=0 to sprx rem -1
read texture(a,b)
next a
next b
return

data  100, 100, 100,-100, 100, 100,-100, 100,-100
data  100, 100,-100, 100,-100, 100,-100,-100, 100
data -100,-100,-100, 100,-100,-100, 1,3,2,4,1,8,4,5
data 8,6,7,5,6,3,7,2,3,8,7,4,1,6,5,2

REM - CREDIT TO Mark Geurds 
REM - FOR COLOUR AND TEXTURE CODE
REM - YOU CAN DOWNLAOD THE GRAPHIC DATA BUILDER 
rem - (by Mark Geurds) FROM YABASIC.CO.UK

              data 27,27,10
              data 2621440,2630888,2621440,2630888,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2630888,2621440
              data 2621440,2630888,2630888,2630888,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2630888,2630888,2621440
              data 2621440,2630888,58600,2630888,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2630888,58600,2630888,2630888
              data 2621440,2621440,2630888,58600,2630888,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2106440,5262416,5262416,5262416,5262416,2106440,2621440,2621440,2621440,2621440,2621440,2630888,2630888,58600,2630888,2621440
              data 2621440,2621440,58600,58600,2621440,2621440,2621440,2621440,2621440,2621440,2106440,2106440,5262416,7367792,8946824,8946824,7367792,5262416,2106440,2106440,2621440,2621440,2621440,2621440,58600,2630888,2621440,2621440
              data 5262416,5262416,7367792,13157608,7367792,5262416,2621440,2621440,2621440,5262416,7367792,5262416,5262416,7367792,7367792,7367792,7367792,5262416,7367792,5262416,5262416,2621440,5262416,5262416,7367792,13157608,7367792,5262416
              data 2621440,2621440,7367792,8946824,2621440,2621440,2621440,2621440,2106440,7367792,8946824,5262416,2106440,5262416,5262416,5262416,5262416,2106440,7367792,8946824,5262416,2106440,2621440,2621440,7367792,8946824,2621440,2621440
              data 2621440,2621440,7367792,8946824,7367792,2621440,2621440,5262416,7367792,2106440,5262416,2106440,2621440,2106440,2106440,2106440,2106440,2106440,5262416,7367792,5262416,2106440,2621440,5262416,7367792,8946824,2621440,2621440
              data 2621440,2621440,7367792,8946824,5262416,7367792,2106440,7367792,8946824,7367792,2106440,2106440,2621440,2621440,2621440,2621440,2621440,2106440,2106440,5262416,5262416,2106440,5262416,7367792,7367792,8946824,2621440,2621440
              data 2621440,2621440,5262416,7367792,2621440,5262416,2106440,7367792,7367792,5262416,2106440,2621440,2621440,2621440,2621440,2621440,2621440,2621440,2106440,2106440,2106440,2106440,7367792,2621440,5262416,7367792,2621440,2621440
              data 2621440,2621440,2621440,2621440,2621440,2106440,2106440,5262416,2106440,2106440,2621440,2621440,5262416,15221832,15221832,15221832,15221832,2621440,2621440,2621440,5262416,5262416,5262416,2106440,2621440,2621440,2621440,2621440
              data 2621440,2621440,2621440,2621440,2621440,2106440,7367792,13157608,7367792,2621440,2621440,5262416,15221832,15221832,15221832,15221832,15221832,15221832,2621440,2106440,7367792,8946824,7367792,5262416,2621440,2621440,2621440,2621440
              data 2621440,2621440,2621440,2621440,2106440,5262416,7367792,7367792,7367792,2106440,2621440,15221832,15221832,15221832,15221832,15221832,15221832,17424,2621440,2106440,5262416,5262416,5262416,5262416,2621440,2621440,2621440,2621440
              data 2621440,2621440,2621440,2621440,2106440,5262416,5262416,7367792,5262416,2106440,2621440,15221832,15221832,15221832,15221832,17424,17424,2124872,2621440,2621440,2106440,2106440,5262416,2106440,2106440,2621440,2621440,2621440
              data 2621440,2621440,2621440,2621440,2106440,2106440,5262416,5262416,2106440,2621440,2621440,15221832,15221832,15221832,17424,2124872,2124872,17424,2621440,2621440,2106440,5262416,7367792,7367792,5262416,2106440,2621440,2621440
              data 2621440,2621440,2621440,2621440,2106440,2106440,2106440,2106440,2621440,2621440,2621440,15221832,17424,2124872,2124872,2124872,17424,17424,2621440,2621440,2106440,7367792,8946824,8946824,7367792,5262416,2621440,2621440
              data 2621440,2621440,2621440,2106440,5262416,7367792,7367792,5262416,2106440,2621440,2621440,17424,2124872,2124872,2124872,17424,17424,17424,2621440,2621440,2106440,5262416,5262416,5262416,5262416,2106440,2621440,2621440
              data 2621440,2621440,2621440,5262416,7367792,13157608,7367792,7367792,2106440,2106440,2621440,2124872,2106440,2106440,2106440,2106440,2106440,17424,2621440,2621440,2621440,2106440,5262416,5262416,2106440,2106440,2621440,2621440
              data 2621440,2621440,2621440,5262416,7367792,7367792,7367792,5262416,2106440,2106440,2621440,2621440,3163264,3163264,3163264,2106440,2106440,2106440,2621440,2621440,2621440,2106440,7367792,7367792,7367792,5262416,2106440,2621440
              data 2621440,2621440,2621440,2106440,5262416,5262416,5262416,5262416,5262416,2106440,2621440,3163264,3163264,3163264,3163264,3163264,2106440,2106440,2621440,2621440,5262416,2106440,7367792,8946824,13157608,8946824,2106440,2621440
              data 2621440,2621440,2106440,2106440,2106440,5262416,5262416,2106440,2106440,2621440,2621440,3163264,4224160,4224160,3163264,3163264,3163264,2106440,2106440,2621440,2106440,2106440,5262416,7367792,7367792,5262416,2106440,2621440
              data 2621440,2621440,5262416,5262416,7367792,7367792,13157608,7367792,2106440,2621440,3163264,6329536,6329536,4224160,4224160,4224160,3163264,3163264,2106440,2621440,2106440,5262416,5262416,2106440,2106440,2106440,2106440,2621440
              data 2621440,2621440,5262416,5262416,5262416,7367792,7367792,7367792,2621440,2621440,6329536,6329536,6329536,6329536,6329536,4224160,4224160,3163264,3163264,2106440,5262416,7367792,8946824,5262416,5262416,8946824,5262416,2106440
              data 17424,4224160,3163264,5262416,5262416,5262416,5262416,2106440,2621440,4224160,3163264,4224160,4224160,4224160,4224160,6329536,6329536,4224160,3163264,2106440,2106440,5262416,17424,2106440,7367792,7367792,5262416,2106440
              data 17424,4224160,4224160,2106440,2106440,3163264,3163264,3163264,2106440,3163264,4224160,4224160,3163264,4224160,4224160,4224160,4224160,4224160,2106440,4224160,2106440,2124872,17424,2124872,17424,5262416,17424,2124872
              data 4224160,4224160,2106440,3163264,2106440,2106440,2106440,2106440,2106440,4224160,3163264,4224160,4224160,4224160,3163264,4224160,4224160,4224160,4224160,4224160,4224160,17424,17424,2124872,17424,2124872,2124872,2124872
              data 4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160
              data 4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,4224160,422416000
