'CopperVector Demo By Shockwave (C) 2002.
'========================================
'At the time of writing, quite a lot of the coders on
'the Yabasic forums are playing around with rasterised
'fill routines, including stencils, textures and colour
'tables.
'
'That's essentially what this is doing, however I have 
'added a nice little copper style effect in the background
'and pushed the colours through the cube which remains
'lightsourced, also I have "rubberised" the cube.
'
'This routine is so processor intensive that getting 50fps
'was never likely, it still manages a decent resolution
'though and chugs along nicely with a fair degree of
'accuracy in the fill routine.
'
'Shockwave.
'oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOo
gosub set:rem                    Initialise Program


'oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOo
'                      Start Loop;
'oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOo
repeat
    setdrawbuf dw:rem            Double Buffer
    dw=1-dw
    setdispbuf dw
    size=9+4*sin(mm)
    swo=swo+5
    if swo>360 swo=swo-360
    gosub clist:rem              Do Background RGB Coppers
    gosub rotate
    gosub construct
scx=scx-1
setrgb 1,255,255,255
text scx,310,mid$(s$,p,67)
if scx<-10 then
scx=scx+10
p=p+1
if p>len(s$) p=0
fi
until (1=2)
'oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOo
'                     Finish loop OO
'oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOo

'---------------------------------------------------------
'                     Draw The Object;
'---------------------------------------------------------
label construct
for a=1 to faces
   gosub draw
next a
return

label customfill
n=abs(n/10000)
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'       Pass four points of rectangle to renderer;
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cfx(1)=tx(f1(a))
cfx(2)=tx(f2(a))
cfx(3)=tx(f3(a))
cfx(4)=tx(f4(a))
cfy(1)=ty(f1(a))
cfy(2)=ty(f2(a))
cfy(3)=ty(f3(a))
cfy(4)=ty(f4(a))
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'               Calculate Approximate Centre;
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xc=(cfx(1)+cfx(3))/2
yc=(cfy(1)+cfy(3))/2
cfx(5)=xc
cfy(5)=yc
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'       Pass Points into render engine variables;
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ctx(1)=cfx(1) : ctx(2)=cfx(2) : ctx(3)=cfx(5)
cty(1)=cfy(1) : cty(2)=cfy(2) : cty(3)=cfy(5)
gosub cft
ctx(1)=cfx(2) : ctx(2)=cfx(3) : ctx(3)=cfx(5)
cty(1)=cfy(2) : cty(2)=cfy(3) : cty(3)=cfy(5)
gosub cft
ctx(1)=cfx(3) : ctx(2)=cfx(4) : ctx(3)=cfx(5)
cty(1)=cfy(3) : cty(2)=cfy(4) : cty(3)=cfy(5)
gosub cft
ctx(1)=cfx(4) : ctx(2)=cfx(1) : ctx(3)=cfx(5)
cty(1)=cfy(4) : cty(2)=cfy(1) : cty(3)=cfy(5)
gosub cft
return

label cft
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'       Triangle Rasteriser (C) Shockwave 2002.
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Sort Triangle Y;
for b=1 to 2
for m=1 to 2
 if cty(m)>cty(m+1) then
  tmx=ctx(m)
  tmy=cty(m)
  ctx(m)=ctx(m+1)
  cty(m)=cty(m+1)
  ctx(m+1)=tmx
  cty(m+1)=tmy
 fi
next m
next b
 lx=ctx(1)
 rx=ctx(1)
 la=(ctx(1)-ctx(2))/(cty(1)-cty(2))*res
 ra=(ctx(1)-ctx(3))/(cty(1)-cty(3))*res
 for cy=cty(1) to cty(2) step res

    setrgb 1,r(cy+rro)*n,g(cy+ggo)*n,b(cy+bbo)*n
sp=-sp
   fill rect sw(cy+swo)+lx,cy-res to sw(cy+swo)+rx,cy+res
   lx=lx+la
   rx=rx+ra
 next cy

 lx=ctx(2)
 la=(ctx(2)-ctx(3))/(cty(2)-cty(3))*res
 q=0
 for cy=cty(2) to cty(3)-res step res
    setrgb 1,r(cy+rro)*n,g(cy+ggo)*n,b(cy+bbo)*n
    fill rect sw(cy+swo)+lx,cy-res to sw(cy+swo)+rx,cy+res
    lx=lx+la
    rx=rx+ra
 next cy

return

'---------------------------------------------------------
'              Draw A Face Of The Object;
'---------------------------------------------------------

label draw
  vx1= tx(f1(a))-tx(f2(a))
  vy1= ty(f1(a))-ty(f2(a))
  vx2= tx(f3(a))-tx(f2(a))
  vy2= ty(f3(a))-ty(f2(a))
  n= vx1*vy2-vx2*vy1
  n=n/(size/6)
 if n<0 gosub customfill
return


label rotate
'###############################################
'## Rotate And Scale Each Point! Store Result ##
'###############################################
 for a=1 to points
  x1=x(a)
  y1=y(a)
  z1=z(a)
'######################
'## X,Y,Z rotations! ##
'######################
  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)
'########################
'## Apply Perspective! ##
'########################
  dv=(zz/40)+1
  xx=size*(xx/dv)+320
  yy=size*(yy/dv)+256
  tx(a)=xx
  ty(a)=yy
  tz(a)=zz
 next a
   xr=xr+4
   yr=yr+3
   zr=zr+1
   if xr>720 xr=xr-720
   if yr>720 yr=yr-720
   if zr>720 zr=zr-720
return

'oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOo
' Simulate an Amiga style RGB copperlist background using
' And alpha blending technique.
'oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOo

label clist
'_________________________________________________________
'                 Move Bar Table Offsets;
'_________________________________________________________
mm=mm+.02
rro=1000+999*sin(mm/2)
ggo=1000+999*sin(mm/3)
bbo=1000+999*sin(mm/4)
'_________________________________________________________
'              Draw The RGB "Copperlist";
'_________________________________________________________
for y=1 to 512 step res
 setrgb 1,r(y+rro),g(y+ggo),b(y+bbo)
 fill rect 0,y to 640,y+res
next y
return

'oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOo
'                   Define Everything;
'oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOo
label set
'_________________________________________________________
'             Graphics Screen And RGB Tables;
'_________________________________________________________
open window 640,512
dim r(2880),g(2880),b(2880)
'_________________________________________________________
'                 Precalculate RGB Bars;
'_________________________________________________________
for a=1 to 2880
 r(a)=45*sin((a/3)*pi/180)
 g(a)=55*sin((a/3)*pi/180)
 b(a)=65*sin((a/3)*pi/180)
next a

'_________________________________________________________
'                     Shockwave Logo;
'_________________________________________________________
'---------------------------------------------------------
'            Define the necessary variables;
'---------------------------------------------------------
res=4
points=8 : Rem          The amount of points in the object
faces=6 : Rem            The Amount of faces in the object
dim cfx(5),cfy(5)
dim ctx(3),cty(3)
dim x(points): Rem            Original X co-ordinate store
dim y(points): Rem            Original Y co-ordinate store
dim z(points): Rem            Original Z co-ordinate store
dim tx(points): Rem       Transformed  X co-ordinate store
dim ty(points): Rem        Transformed Y co-ordinate store
dim tz(points): Rem        Transformed Z co-ordinate store
dim f1(faces):rem                   Connections definition
dim f2(faces):rem                   Connections definition
dim f3(faces):rem                   Connections definition
dim f4(faces):rem                   Connections definition
dim sw(1440)
for a=1 to 1440
sw(a)=40*sin((a*2)*pi/180)
next a

'---------------------------------------------------------
'   Define Sine Tables for faster matrix calculations;
'---------------------------------------------------------

 dim cs(720)
 dim sn(720)
 for ang=0 to 720
  cs(ang)=cos(ang*(pi/360))
  sn(ang)=sin(ang*(pi/360))
 next ang
'---------------------------------------------------------
'               Read in the object's points;
'---------------------------------------------------------
for a=1 to points
 read x(a),y(a),z(a)
next a
'---------------------------------------------------------
'         Read In Connections and face parameters;
'---------------------------------------------------------
for a=1 to faces
read f1(a)
read f2(a)
read f3(a)
read f4(a)
next a
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'   The Object Description As Data!
'   The Data Below Describes A Cube.
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

'Points definition;
'~~~~~~~~~~~~~~~~~~
'Below are the points of the object defined as x,y,z;

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

return



