'                  The "Underwater" demo.
'                   Coded by Shockwave!!
'
'  This one uses a simple perspective transformation to
'  create a shimmering effect similar to being submerged
'
'  The Gem object is about the most complex so far in
'  PS2 Yabasic at 50fps, it has 40 faces and 34 Vertices.
'
'  The Logo and object were created by Shockwave.
'  
'  There wasn't enough time to make decent bubbles so 
'  apologies that they look a bit ropey.
'
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gosub initialise
'---------------------------------------------------------
'                       Loop Start;
'---------------------------------------------------------
repeat
gosub swap

    gosub rotate
    gosub construct
setrgb 1,155+blu,255,255
text 320,306,"UNDERWATER DEMO!","cc"
until (1=2)
'---------------------------------------------------------
'                       Loop End OO
'---------------------------------------------------------

'---------------------------------------------------------
'                  Swap Screen Buffers;
'---------------------------------------------------------
label swap
setdrawbuf dw
dw=1-dw
setdispbuf dw
mm=mm+.1
    red=10+10*sin(mm/2)
    grn=10+10*sin(mm/2)
    blu=60+60*cos(mm/3)
    setrgb 1,red,1,blu
    setrgb 2,1,grn,blu
    setrgb 3,1,10,100
    gtriangle 0,101 to 640,512 to 0,512
    gtriangle 0,101 to 640,512 to 640,101
setrgb 1,255,255,255
fill rect 0,509 to 640,511
setrgb 1,155,155,255
for a=1 to 19
fill rect bx(a),by(a) to bx(a)+2,by(a)+2
by(a)=by(a)-ran(10)
bx(a)=bx(a)-5+ran(10)
if by(a)<102 by(a)=512
next a

return

'---------------------------------------------------------
'                     Draw The Object;
'---------------------------------------------------------
label construct
for a=1 to faces
gosub draw
next a
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
 if n<0 then
 n=-(n/400)
if n>120 n=120
 setrgb 1,r(a)+n,g(a)+n,b(a)+n
 fill triangle tx(f1(a)),ty(f1(a)) to tx(f2(a)),ty(f2(a)) to tx(f3(a)),ty(f3(a))
 fill triangle tx(f1(a)),ty(f1(a)) to tx(f4(a)),ty(f4(a)) to tx(f3(a)),ty(f3(a))
 if cls(a)=1 then
   setrgb 1,0,0,0
   line tx(f1(a)),ty(f1(a)) to tx(f2(a)),ty(f2(a))
   line tx(f2(a)),ty(f2(a)) to tx(f3(a)),ty(f3(a))
   line tx(f3(a)),ty(f3(a)) to tx(f4(a)),ty(f4(a))
   line tx(f4(a)),ty(f4(a)) to tx(f1(a)),ty(f1(a))
 fi
fi
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/2)+306
  tx(a)=xx
  ty(a)=yy
  tz(a)=zz
 next a
xr=xr+3
yr=yr+2
zr=zr+7
if xr>720 xr=xr-720
if yr>720 yr=yr-720
if zr>720 zr=zr-720
return


label initialise
open window 640,512
'---------------------------------------------------------
'       Put Logo On Logical And Physical Screens;
'---------------------------------------------------------
for a=1 to 2
dw=1-dw
setdrawbuf dw
'---------------------------------------------------------
'                   Logo Background;
'---------------------------------------------------------
    setrgb 1,0,0,0
    setrgb 2,20,120,120
    setrgb 3,20,120,120
    gtriangle 0,0 to 640,100 to 0,100
    setrgb 3,0,0,0
    gtriangle 0,0 to 640,100 to 640,0
    setrgb 1,255,255,255
    fill rect 0,100 to 640,97
'---------------------------------------------------------
'                     Logo Image;
'---------------------------------------------------------
setrgb 1,10,160,90
's
   fill rect 10,10 to 70,20
   fill rect 10,90 to 70,80
   fill rect 10,45 to 70,55
   fill rect 10,10 to 20,55
   fill rect 70,55 to 60,90
   fill rect 70,45 to 50,90
'h
   fill rect 80,10 to 90,90
   fill rect 140,10 to 130,90
   fill rect 140,45 to 80,55
   fill rect 80,45 to 100,90
'o
   fill rect 150,10 to 210,20
   fill rect 150,90 to 210,80
   fill rect 150,10 to 160,90
   fill rect 210,10 to 200,90
   fill rect 150,45 to 170,90
'c
   fill rect 220,10 to 280,20
   fill rect 220,90 to 280,80
   fill rect 220,10 to 230,90
   fill rect 220,45 to 240,90
'k
   fill rect 290,10 to 300,90
   fill rect 290,45 to 350,55
   fill rect 350,45 to 330,90
   fill rect 320,10 to 330,45
'w
   fill rect 360,10 to 370,90
   fill rect 360,45 to 380,90
   fill rect 360,90 to 420,80
   fill rect 420,90 to 410,10
   fill rect 390,90 to 400,45
'a
   fill rect 430,10 to 440,90
   fill rect 490,10 to 480,90
   fill rect 430,10 to 490,20
   fill rect 430,45 to 490,55
   fill rect 430,45 to 450,90
'v
   fill rect 500,10 to 510,90
   fill rect 510,45 to 520,90
   fill rect 560,10 to 550,45
   fill rect 500,80 to 545,90
   fill triangle 545,90 to 540,80 to 550,45
   fill triangle 560,45 to 550,45 to 545,90
'e
   fill rect 570,10 to 580,90
   fill rect 570,10 to 630,20
   fill rect 570,80 to 630,90
   fill rect 570,45 to 610,55
   fill rect 570,45 to 590,90 
next a
'---------------------------------------------------------
'            Define the necessary variables;
'---------------------------------------------------------
size=42: rem                      how big do you want it?
points=34 : Rem         The amount of points in the object
faces=40 : Rem          The Amount of faces in the object
dim bx(19):rem          Bubble X Storage.
dim by(19):rem          Bubble Y Storage
for a=1 to 19 :rem      Define bubble positions.
bx(a)=ran(640)
by(a)=100+ran(400)
next a
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 r(faces):rem                             Red Component
dim g(faces):rem                           Green Component
dim b(faces):rem                            Blue Component
dim cls(faces):rem                        Cell Shade Face?
dim sort(faces):rem                          Drawing order
'---------------------------------------------------------
'   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)
read r(a),g(a),b(a),cls(a)
next a
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'   The Object Description As Data!
'   The Data Below Describes A Cube.
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

data 0,0,-7,3,-3,-6,3,3,-6,-3,3,-6
data -3,-3,-6,4,0,-6,0,4,-6,-4,0,-6
data 0,-4,-6,0,-7,-4,0,7,-4,-7,0,-4
data 7,0,-4,5,5,-4,-5,5,-4,-5,-5,-4
data 5,-5,-4,5,-5,-2,5,5,-2,-5,5,-2
data -5,-5,-2,-7,0,-2,0,7,-2,7,0,-2
data 0,-7,-2,0,-4,3,0,4,3,4,0,3

data -4,0,3,-3,3,3,3,3,3,3,-3,3
data -3,-3,3,0,0,7
'Connection definition;
'Below are the faces of the object defined as vertice
'numbers, specified in clockwise order. These are followed
'by r,g,b values for the face and finally cell shaded
'parameter (0)=off (1)=on.

data 30,29,22,20,0,0,90,1
data 29,33,21,22,0,0,90,0
data 33,26,25,21,0,0,90,1
data 27,30,20,23,0,0,90,1
data 31,27,23,19,0,0,90,0
data 19,24,28,31,0,0,90,1
data 32,28,24,18,0,0,90,1
data 18,25,26,32,0,0,90,0
data 20,22,12,15,0,0,90,1
data 23,20,15,11,0,0,90,0
data 21,16,12,22,0,0,90,1
data 25,10,16,21,0,0,90,1
data 19,23,11,14,0,0,90,1
data 13,24,19,14,0,0,90,1
data 18,24,13,17,0,0,90,1
data 10,25,18,17,0,0,90,1

data 33,29,34,34,0,0,90,1
data 29,30,34,34,0,0,90,0
data 30,27,34,34,0,0,90,1
data 27,31,34,34,0,0,90,0
data 31,28,34,34,0,0,90,1
data 28,32,34,34,0,0,90,0
data 32,26,34,34,0,0,90,1
data 26,33,34,34,0,0,90,0

data 15,12,8,4,0,0,90,1
data 12,16,5,8,0,0,90,0
data 15,4,7,11,0,0,90,1
data 7,3,14,11,0,0,90,0
data 3,6,13,14,0,0,90,1
data 16,10,9,5,0,0,90,0
data 2,9,10,17,0,0,90,1
data 6,2,17,13,0,0,90,0

data 9,2,1,1,0,0,90,0
data 2,6,1,1,0,0,90,1
data 6,3,1,1,0,0,90,0
data 3,7,1,1,0,0,90,1
data 7,4,1,1,0,0,90,1
data 5,9,1,1,0,0,90,1
data 4,8,1,1,0,0,90,0
data 8,5,1,1,0,0,90,1


'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

'Connection definition;
'Below are the faces of the object defined as vertice
'numbers, specified in clockwise order. These are followed
'by r,g,b values for the face and finally cell shaded
'parameter (0)=off (1)=on.

data 1,2,3,4,30,0,0,1
data 5,8,7,6,30,0,0,1
data 6,2,1,5,0,30,0,1
data 8,4,3,7,0,30,0,1
data 2,6,7,3,0,0,30,1
data 8,5,1,4,0,0,30,1


return



