'                        Shockbox
'                        ========
'
'            -= Coded By Shockwave (C) 2002. =-
'
'           This demo was inspired by Xalthorn!!
'---------------------------------------------------------
gosub initialise
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
repeat
mm=mm+.05
size=20+(10*sin(mm))
    setdrawbuf dw
    dw=1-dw
    setdispbuf dw
setrgb 1,40,70,40
setrgb 2,50,120,60
setrgb 3,90,150,90
gtriangle 0,0 to 640,512 to 0,512
gtriangle 0,0 to 640,512 to 640,0
    gosub logo
setrgb 1,255,255,255
text scx,456,mid$(s$,p,67)
setrgb 1,0,0,0
text scx+1,457,mid$(s$,p,67)
scx=scx-1
if scx<-10 then
scx=scx+10
p=p+1
if p>len(s$) p=0
fi

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

label logo
    gtriangle 300,0 to 290,130 to 310,110
    gtriangle 290,70 to 350,20 to 330,50
    gtriangle 300,50 to 350,120 to 320,100
    gtriangle 360,20 to 370,120 to 390,90
    gtriangle 370,110 to 400,20 to 400,110
    gtriangle 420,20 to 380,120 to 410,100
    gtriangle 430,120 to 460,20 to 450,100
    gtriangle 440,70 to 490,60 to 480,80
    gtriangle 490,110 to 470,100 to 450,20
    gtriangle 500,20 to 520,30 to 530,120
    gtriangle 520,130 to 560,20 to 550,20
    gtriangle 550,80 to 630,50 to 635,60
    gtriangle 570,80 to 620,20 to 630,30
    gtriangle 575,65 to 635,120 to 630,100
    gtriangle 220,80 to 280,20 to 290,40
    gtriangle 225,60 to 280,110 to 260,120
    gtriangle 180,10 to 150,110 to 160,120
    gtriangle 170,10 to 200,110 to 210,130
    gtriangle 150,100 to 210,90 to 205,110
    gtriangle 89,20 to 80,120 to 99,110
    gtriangle 140,10 to 120,20 to 130,110
    gtriangle 80,70 to 140,60 to 145,70
    gtriangle 70,10 to 75,30 to 10,40
    gtriangle 10,35 to 70,80 to 75,120
    gtriangle 10,100 to 20,120 to 80,110
return

label ball
setrgb 1,255,255,100
setrgb 2,60,60,0
setrgb 3,60,60,0
for c=0 to 640 step 80
m=size*3
xx1=m*sn(c)
yy1=m*cs(c)
xx2=m*sn((c+80))
yy2=m*cs((c+80))
 gtriangle 320,256 to xx1+320,yy1+256 to xx2+320,yy2+256
next c
return
'---------------------------------------------------------
'                     Draw The Object;
'---------------------------------------------------------
label construct
gosub draworder
for b=1 to visible
a=sort(b)
gosub draw
next b
return
'---------------------------------------------------------
'      Find Visible Faces, Sort them into order;
'---------------------------------------------------------
label draworder
visible=0
for a=1 to faces
  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(a)=  vx1*vy2-vx2*vy1
 if n(a)<0 then
 visible=visible+1
 sort(visible)=a
 n(a)=-(n(a)/250)
 fi
next a
for fk=1 to visible-1
for a=1 to visible-1
'get low z
 ref=sort(a)
 low=tz(f1(ref))
 if low<tz(f2(ref)) low=tz(f2(ref))
 if low<tz(f3(ref)) low=tz(f3(ref))
 if low<tz(f4(ref)) low=tz(f4(ref))
'get low z
 ref=sort(a+1)
 low2=tz(f1(ref))
 if low2<tz(f2(ref)) low2=tz(f2(ref))
 if low2<tz(f3(ref)) low2=tz(f3(ref))
 if low2<tz(f4(ref)) low2=tz(f4(ref))
if low2>low then
 temp=sort(a+1)
 sort(a+1)=sort(a)
 sort(a)=temp
fi
next a
next fk
return
'---------------------------------------------------------
'              Draw A Face Of The Object;
'---------------------------------------------------------
label draw
 setrgb 1,r(a)+n(a),g(a)+n(a),b(a)+n(a)
 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
 if b=int(visible/2)-1 gosub ball
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)
'---------------------------------------------------------
'                 Perspective Transformation;
'---------------------------------------------------------
  dv=(zz/30)+1
  xx=size*(xx/dv)+320
  yy=size*(yy/dv)+256
  tx(a)=xx
  ty(a)=yy
  tz(a)=zz
 next a
'---------------------------------------------------------
'                 Rotation adds and resets;
'---------------------------------------------------------
xr=xr+1
yr=yr+2
zr=zr+4
if xr>720 xr=xr-720
if yr>720 yr=yr-720
if zr>720 zr=zr-720
return
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
' This Sub-Routine Initialises The Program.
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
label initialise
'---------------------------------------------------------
'                     Open Gfx Screen!
'---------------------------------------------------------
 open window 640,512
 p=0 : scx=0
s$="                                                                "
s$=s$+"INSPIRED BY XALTHORN, THIS IS THE SHOCKBOX DEMO..."
s$=s$+" IT WAS CODED BY SHOCKWAVE IN SEPTEMER 2002 AND IT"
s$=s$+" USES MY LATEST NON CONVEX 3D CODE.. YEP, THIS IS "
s$=s$+"ALL REAL TIME.. SORRY TO RIP OFF YOUR IDEA "
s$=s$+"XALTHORN.. THIS ROTATES X,Y AND Z BTW :O)  HI TO:"
s$=s$+"  XALTHORN, DOCTOR, JIM SHAW, PARABELLUM, JINX, "
s$=s$+"RAFRYER, JOMORROW, DEMONEYE, KYATAAVL, AZ, BALROQ,"
s$=s$+" BONGOTRUMMOR, DREW, ZINGSTER, PYRO, "
s$=s$+"SNAKEDOGG, JACOB BUSBY, ELL, BRUASET, "
s$=s$+"COMBATKING AND EVERYONE ELSE AT THE "
s$=s$+"WWW.YABASIC.CO.UK FORUM   TEXT WRAPS NOW."

'---------------------------------------------------------
'            Define the necessary variables;
'---------------------------------------------------------
'       Change the three below to suit your object;
'_________________________________________________________
size=20: rem                       how big do you want it?
points=16: Rem          The amount of points in the object
faces=16 : Rem           The Amount of faces in the object
'_________________________________________________________
'                  Leave these all alone;
dw=1 : Rem                       Double buffering Variable
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
dim n(faces):rem                           Surface normals
'---------------------------------------------------------
'   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
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
'Points definition;
'~~~~~~~~~~~~~~~~~~
'Below are the points of the object defined as x,y,z;
'This object was created vith vector object editor v1.0
'by Shockwave. Object designed by Shockwave.
data 5,5,-4,-5,5,-4,5,-5,4,5,5,4
data -5,5,4,-5,-5,4,7,-7,2,7,7,2
data -7,7,2,-7,-7,2,-7,-7,-2,7,-7,-2,7,7,-2,-7,7,-2
data -5,-5,-4,5,-5,-4
'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.
'eg:
'join points 1 to 2 to 3 to 4 to 1
'colour 10 red 20 blue 30 green cell shade on would be;
'
data 9,10,11,14,0,40,30,1
data 8,9,14,13,0,40,30,1
data 11,10,7,12,0,40,30,1
data 7,8,13,12,0,40,30,1
data 11,15,2,14,40,0,30,1
data 9,5,6,10,40,0,30,1
data 7,10,6,3,40,0,30,1
data 12,16,15,11,40,0,30,1
data 13,14,2,1,40,0,30,1
data 12,13,1,16,40,0,30,1
data 7,3,4,8,40,0,30,1
data 9,8,4,5,40,0,30,1
data 3,6,15,16,40,40,0,1
data 5,4,1,2,40,40,0,1
data 6,5,2,15,40,40,0,1
data 3,16,1,4,40,40,0,1
return








