'         *** FILLED COMPLEX COLOUR OBJECTS ***
'   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'   @ Vector Routine Coded By Mr. Shockwave (C) 2001! @
'   @-===============================================-@
'   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'
' This one uses precalculated sine tables to speed up the
' routine. It also incorporates a Z rotation which I had 
' left out of my earlier demos. This Can do 29 polygons
' at full frame rate!!
'#########################################################

gosub initialise
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
repeat

'#######################################################
'## The Main Loop. These Calls Are Repeated Until (X) ##
'#######################################################

c=peek("port1")
setdrawbuf dw
dw=1-dw
setdispbuf dw

setrgb 1,255,55,255
fill rectangle 0,0 to 640,512
setrgb 1,10,20,50
setrgb 2,20,30,40
setrgb 3,30,40,30
gtriangle 4,4 to 636,508 to 4,508
gtriangle 4,4 to 636,508 to 636,4


gosub rotate
gosub sort
gosub draw

setrgb 1,40,40,120
fill rectangle 50,50 to 590,70
setrgb 1,255,255,255
text 150,64,"ANY EXCUSE TO RELEASE A NEW DEMO!"
setrgb 1,250,150,250
text scx,450,mid$(s$,p,67)
scx=scx-1
if scx<-10 then
scx=scx+10
p=p+1
if p>len(s$) p=0
end if
until (and(c,16384)<>0)
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
exit

label sort
for fk=1 to 4
b=1
for a=1 to polys -1
if tz(b)+tz(b+1)+tz(b+2) < tz(b+3)+tz(b+4)+tz(b+5) then
for inner=0 to 2
 sz=tz(b+inner)
 sy=ty(b+inner)
 sx=tx(b+inner)
 tz(b+inner)=tz(b+inner+3)
 ty(b+inner)=ty(b+inner+3)
 tx(b+inner)=tx(b+inner+3)
 tz(inner+b+3)=sz
 ty(inner+b+3)=sy
 tx(inner+b+3)=sx
next inner
 sr=tr(a+1)
 sg=tg(a+1)
 sb=tb(a+1)
 tr(a+1)=tr(a)
 tg(a+1)=tg(a)
 tb(a+1)=tb(a)
 tr(a)=sr
 tg(a)=sg
 tb(a)=sb
end if
b=b+3
next a
next fk
return

label draw
'########################
'## Draw The object!!! ##
'########################
b=1
for a=1 to polys
lm=a*20
setrgb 1,tr(a)+lm,tg(a)+lm,tb(a)+lm
'setrgb 1,a*6,a*3,a*2+30
gtriangle tx(b),ty(b) to tx(b+1),ty(b+1) to tx(b+2),ty(b+2)
b=b+3
next a
return

label rotate
'###############################################
'## Rotate And Scale Each Point! Store Result ##
'###############################################
 for a=1 to polys*3
  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! ##
'########################
  xx=size*(xx/((zz/70)+1))+320
  yy=size*(yy/((zz/70)+1))+256
  tx(a)=xx
  ty(a)=yy
  tz(a)=zz
  tr(a)=r(a)
  tg(a)=g(a)
  tb(a)=b(a)
 next a
xr=xr+rrx
yr=yr+rry
zr=zr+rrz
  if xr>720 xr=xr-720
  if yr>720 yr=yr-720
  if zr>720 zr=zr-720
return

label initialise
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
' This Sub-Routine Initialises The Program.
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
s$="                                                                  "
s$=s$+"I'VE SPED THIS CODE UP SO THAT I CAN DO A FEW MORE"
s$=s$+" EFFECTS AS WELL AS HAVING THE OBJECT! I CAN'T BE "
s$=s$+"BOTHERED TO TYPE TODAY SO HELLO IF YOU KNOW ME.. "
s$=s$+"SEE YOU IN THE NEXT DEMO!!!   :O) (C) MR SHOCKWAVE"
scx=0
p=0


'######################
'## Open Gfx Screen! ##
'######################
open window 640,512
'#####################################
'## Define the necessary variables! ##
'#####################################
setrgb 2,0,0,0
setrgb 3,0,0,0
setrgb 0,30,30,30
size=42: rem how big do you want it?
rrx=1
rry=2
rrz=3
dw=1 : Rem            Double buffering Variable
polys=8 : Rem        The amount of polygons in the object
dim x(polys*3) : Rem  Original X co-ordinate store
dim y(polys*3) : Rem  Original Y co-ordinate store
dim z(polys*3) : Rem  Original Z co-ordinate store
dim r(polys*3) : Rem  Original Red component store
dim g(polys*3) : Rem  Original Green component store
dim b(polys*3) : Rem  Original Blue Component store
dim tx(polys*3) : Rem Transformed  X co-ordinate store
dim ty(polys*3) : Rem Transformed Y co-ordinate store
dim tz(polys*3) : Rem Transformed Z co-ordinate store
dim tr(polys*3) : Rem Transformed Red component store
dim tg(polys*3) : Rem Transformed Green component store
dim tb(polys*3) : Rem Transformed Blue Component store

'##########################
'## Define Sine Tables!! ##
'##########################
 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! ##
'#########################
a=1
for b=1 to polys
read x(a),y(a),z(a),x(a+1),y(a+1),z(a+1),x(a+2),y(a+2),z(a+2),r(b),g(b),b(b)
a=a+3
next b
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'   The Object Description As Data!
'   (uses 3 points per polygon!)
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
data -5,-1,5,5,-1,5,0,-6,0,225,225,0
data 5,-1,5,5,-1,-5,0,-6,0,0,255,255
data 5,-1,-5,-5,-1,-5,0,-6,0,255,255,0
data -5,-1,-5,-5,-1,5,0,-6,0,0,255,255
data -5,1,5,5,1,5,0,6,0,55,255,55
data 5,1,5,5,1,-5,0,6,0,255,0,225
data 5,1,-5,-5,1,-5,0,6,0,55,255,55
data -5,1,-5,-5,1,5,0,6,0,255,0,255
return




