'                  (C) Shockwave 2002.
'                 ** The Cobra Demo! **
'
'                To Pay Homage To 'ELITE'
'
'   3D Routine... This one uses a hidden line algorithm
'  to take away the need for depth sorting... It's much
' quicker than other methods but less flexible.
'
' Greetings to all my friends and acquaintences on the
' Yabasic forums, especially these (In Random Order):
'
'  Xalthorn, Doctor, Jinx, Master Tonberry, Jacob Busby,
'    Dougal The Dogg, Demoneye, Asiv, Liquid Pia, Ian,
'     Static Gerbil, Verybasic, Snakedogg, Jomorrow.
'   And To All The Rest Of You Too :o) Enjoy The Demo!
'#########################################################

gosub initialise
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
debug=-1
repeat
setdrawbuf dw
dw=1-dw
setdispbuf dw
   setrgb 1,20,10,10
   setrgb 2,10,20,10
   setrgb 3,20,10,10
    gtriangle 0,0 to 640,512 to 0,512
    setrgb 3,40,30,20
    gtriangle 0,0 to 640,512 to 640,0


   setrgb 1,0,0,10
   setrgb 2,0,0,40
   setrgb 3,0,0,40
    gtriangle 0,370 to 640,512 to 0,512
    setrgb 3,0,0,10
    gtriangle 0,370 to 640,512 to 640,370


    gosub cobra

setrgb 1,0,0,0
text 323,43,"THE COBRA DEMO BY SHOCKWAVE","cc"
rect 173,23 to 463,53
text scx+3,203,mid$(s$,p,67)
setrgb 1,255,255,255
text 320,40,"THE COBRA DEMO BY SHOCKWAVE","cc"
rect 170,20 to 460,50
text scx,200,mid$(s$,p,67)

scx=scx-1
if scx<-10 then
 scx=scx+10
 p=p+1
 if p>len(s$) p=0
fi


if debug>0 then
setrgb 1,250,250,250
for a=1 to polys
text tx(a),ty(a),str$(a),"cc"
next a
fi
if and(peek("port1"),64)<>0 debug=1
if and(peek("port1"),16)<>0 debug=-1

until (and(peek("port1"),16384)<>0)
exit

label cobra
    gosub rotate
    r=80 : g=20 : b=20
    f1=1 : f2=3 : f3=2 
    gosub draw
    gosub draw2

    r=90 : g=30 : b=30
    f1=2 : f2=3 : f3=4 
    gosub draw
    gosub draw2

    r=100 : g=100 : b=200
    f1=3 : f2=5 : f3=4 
    gosub draw
    gosub draw2

    r=90 : g=30 : b=30
    f1=4 : f2=5 : f3=6
    gosub draw
    gosub draw2

    r=80 : g=20 : b=20
    f1=5 : f2=7 : f3=6
    gosub draw
    gosub draw2

    r=10 : g=40 : b=10
    f1=9 : f2=3 : f3=1
    gosub draw
    gosub draw2

    r=10 : g=40 : b=10
    f1=5 : f2=10 : f3=8
    gosub draw
    gosub draw2

    r=10 : g=40 : b=10
    f1=9 : f2=10 : f3=3
    gosub draw
    gosub draw2

    r=10 : g=20 : b=10
    f1=10 : f2=5 : f3=3
    gosub draw
    gosub draw2

    r=10 : g=20 : b=10
    f1=7 : f2=5 : f3=8
    gosub draw
    gosub draw2

    r=100 : g=0 : b=0
    f1=1 : f2=2 : f3=9
    gosub draw
    gosub draw2

    r=100 : g=0 : b=0
    f1=7 : f2=8 : f3=6
    gosub draw
    gosub draw2

    r=40 : g=40 : b=40
    f1=2 : f2=4 : f3=9
    gosub draw
    gosub draw2

    r=40 : g=40 : b=40
    f1=4 : f2=6 : f3=8
    gosub draw
    gosub draw2

    r=60 : g=65 : b=60
    f1=9 : f2=4 : f3=8
    gosub draw
    gosub draw2
return
label draw
'##############################
'## Draw A Face Of The Cobra ##
'##############################
  vx1= tx(f1)-tx(f2)
  vy1= ty(f1)-ty(f2)
  vx2= tx(f3)-tx(f2)
  vy2= ty(f3)-ty(f2)
  if  (vx1*vy2-vx2*vy1)<0 then
light=-(tz(f1)+tz(f2)+tz(f3))*4
setrgb 1,r+light,g+light,b+light
fill triangle tx(f1),ty(f1) to tx(f2),ty(f2) to tx(f3),ty(f3)
fi
return

label draw2
'#####################################
'## Draw inverted Face Of The Cobra ##
'#####################################
  vx1= tx(f1)-tx(f2)
  vy1= ty(f1)-ty(f2)
  vx2= tx(f3)-tx(f2)
  vy2= ty(f3)-ty(f2)
  if  (vx1*vy2-vx2*vy1)<0 then
light=-(tz(f1)+tz(f2)+tz(f3))*3
setrgb 1,r+light,g+light,b+light+20
fill triangle tx(f1),ry(f1) to tx(f2),ry(f2) to tx(f3),ry(f3)
fi
return


label rotate
'###############################################
'## Rotate And Scale Each Point! Store Result ##
'###############################################
 for a=1 to polys
  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/50)+1))+320
  yy=size*(yy/((zz/50)+1))+206
  tx(a)=xx
  ty(a)=yy
  ry(a)=(-(yy/3))+540
  tz(a)=zz
 next a
xr=xr+4
yr=yr+2
zr=zr+2
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.
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'######################
'## Open Gfx Screen! ##
'######################
open window 640,512
s$="                                                                    "
s$=s$+"THIS SPACESHIP SHOULD BE INSTANTLY FAMILIAR TO "
s$=s$+"ANYONE WHO PLAYED THE CLASSIC GAME 'ELITE' BY "
s$=s$+"THE GENIUS DAVID BRABEN... I REMEMBER THIS GAME "
s$=s$+"FROM THE BBC AND COMMODORE 64, A REAL CLASSIC... "
s$=s$+"I WANTED A DIFFERENT OBJECT TO SHOWCASE MY NEWEST "
s$=s$+"3D ROUTINE, WHICH IS BLINDINGLY QUICK... I WAS A " 
s$=s$+"BIT BORED WITH CUBE DERIVATIVES SO I THOUGHT "
s$=s$+"I'D MODEL AN ELITE SPACESHIP, I ALSO ADDED "
s$=s$+"THE REFLECTION AND SCROLL AND IT'S STILL ZIPPING "
s$=s$+"ALONG AT FULL FRAME RATE, FULLY LIGHTSOURCED WITH "
s$=s$+"INDEPENDANTLEY COLOURED FACES, THERE'S SO MUCH "
s$=s$+"SPARE PROCESOR TIME THAT I LEFT MY DEBUG MODE IN "
s$=s$+"HERE, PRESS DOWN ON THE D-PAD TO SEE IT AND UP TO "
s$=s$+"TURN IT OFF... I USE THIS WHEN I'M CONNECTING UP "
s$=s$+"THE VERTICES OF THE OBJECT DURING ITS' CREATION.. "
s$=s$+" I REALY LIKE THIS ONE AND I HOPE YOU DO TOO...   "
s$=s$+"CONTACT ME AT SHOCKWAVE@PS2-YABASIC.CO.UK IF YOU "
s$=s$+"WANT TO TALK TO ME....  HERE'S SOME GREETINGS:   "
s$=s$+"XALTHORN... REALLY NICE DEMOS AND GAMES M8, "
s$=s$+"CONGRATULATIONS ON GETTING YOURSELF IN PRINT AND "
s$=s$+"ON YOUR WEBSITE ( WWW.PS2-YABASIC.CO.UK ).....   "
s$=s$+"DOCTOR... IT'S REALLY HEARTWARMING TO SEE HOW FAR "
s$=s$+"YOU'VE PROGRESSED IN THE SHORT TIME YOU'VE BEEN "
s$=s$+"CODING.. THANKS ALSO FOR THE EMAILS......   "
s$=s$+"DOUGAL THE DOG (DON'T KNOW WHAT TO CALL YOU THESE "
s$=s$+"DAYS).. DART IS THE BEST EXAMPLE OF YABASIC 3D I "
s$=s$+"HAVE SEEN SO FAR....   "
s$=s$+"JACOB BUSBY... PLEASE EMAIL ME AS I LOST YOUR "
s$=s$+"ADDRESS AND I HAVE SOME THINGS TO TELL YOU ABOUT.."
s$=s$+"..   JOMORROW...  I LOVE YOUR GFX, THEY'RE SICK "
s$=s$+"BUT GREAT....   GREETINGS ALSO FLY OUT TO THESE "
s$=s$+"PEOPLE (SORRY IF I FORGET YOU) : MASTER TONBERRY, "
s$=s$+"JIM SHAW, DEMONEYE, YABASIC GURUS, SNAKEDOGG, "
s$=s$+"IAN, JINX, MATT, GOTHI, ELL, DREW, STATIC GERBIL, "
s$=s$+"ADAM, GRANT, MATT MARTIN, TREV, SEPHIROTH, ASIV, "
s$=s$+"LIQUID, ANSEL, JASON, KIRK, PAUL, PHIL AND "
s$=s$+"EVERYONE ELSE WHO KNOWS ME   " 
s$=s$+"SEE YOU ALL NEXT TIME, BYE......       "
scx=0
p=0
'#####################################
'## Define the necessary variables! ##
'#####################################
size=13.5: rem how big do you want it?
dw=1 : Rem            Double buffering Variable
polys=10 : Rem        The amount of points in the object
dim x(polys) : Rem  Original X co-ordinate store
dim y(polys) : Rem  Original Y co-ordinate store
dim z(polys) : Rem  Original Z co-ordinate store
dim tx(polys) : Rem Transformed  X co-ordinate store
dim ty(polys) : Rem Transformed Y co-ordinate store
dim tz(polys) : Rem Transformed Z co-ordinate store
dim ry(polys) : Rem Inverted Y co-ord store (reflection).
'##########################
'## 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! ##
'#########################
for a=1 to polys
 read x(a),y(a),z(a)
next a
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'   The Object Description As Data!
'   The Data Below Describes A Cube.
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
data -15,-10,0,-5,-10,5,-5,10,0
data 0,-10,5,7,10,0,5,-10,5
data 15,-10,0
data 5,-10,-5,-5,-10,-5,0,-10,-5
return








