Author Topic: On Reflection Demo  (Read 2623 times)

0 Members and 1 Guest are viewing this topic.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
On Reflection Demo
« on: October 19, 2006 »
This demo rotates a cube inside a stationary mirror cube and has the correct reflection on each interior face.

Code: [Select]
'   On Reflection Demo
'   ==================
'
'   This Demo Was Coded In October 2002 By Shockwave.
'   The idea for it came about from some discussions on
'   The Yabasic Forums, I'd Like To Say Thank You To;
'   Fryer and Parabellum.
'   Who Gave Me The Idea.
'
'   Contact Address;
'   Shockwave@ps2-yabasic.co.uk (Email).
'   WWW.Yabasic.Co.Uk (Forums).
'
'   Also Visit;
'
'   WWW.PS2-Yabasic.co.uk (Rescource Site)
'   WWW.Yabasic.Co.Uk (General Site).
'
'Coded By Shockwave C) 2002.
'#########################################################

gosub initialise
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
setrgb 0,40,30,20
repeat
    setdrawbuf dw
    dw=1-dw
    setdispbuf dw
    clear window
    gosub rotate
    gosub inf
'_________________________________________________________
    o=16:gosub construct2
    o=24:gosub construct2
    o=32:gosub construct2
    o=40:gosub construct2
'_________________________________________________________
    gosub construct
    gosub rotcontrol
    setrgb 1,cf,cf,cf
    text 0,0,m$(mp),"cc"
until (and(peek("port1"),16384)<>0)
exit


'---------------------------------------------------------
'              Rotations For The Central Cube;
'---------------------------------------------------------
label rotcontrol
  tt=tt+2
    if tt<255 cf=cf+2
    if tt>500 cf=cf-2
    if tt>750 then
       tt=0
       mp=mp+1
      if mp>15 mp=1
    fi
 
zr2=zr2+1
xr2=xr2+3
yr2=yr2+2
if xr2>720 xr2=xr2-720
if yr2>720 yr2=yr2-720
if zr2>720 zr2=zr2-720
return

'---------------------------------------------------------
'                Draw The Cube Reflections;
'---------------------------------------------------------
label construct2
for a=1 to faces
gosub draw2
next a
return

'---------------------------------------------------------
'            Draw A Face Of The Central Cube;
'---------------------------------------------------------

label draw2
  vx1= tx(f1(a)+o)-tx(f2(a)+o)
  vy1= ty(f1(a)+o)-ty(f2(a)+o)
  vx2= tx(f3(a)+o)-tx(f2(a)+o)
  vy2= ty(f3(a)+o)-ty(f2(a)+o)
  n=  vx1*vy2-vx2*vy1
 if n>0 then
 n=n/100
 setrgb 1,r(a)+n,g(a)+n,b(a)+n
 fill triangle tx(f1(a)+o),ty(f1(a)+o) to tx(f2(a)+o),ty(f2(a)+o) to tx(f3(a)+o),ty(f3(a)+o)
 fill triangle tx(f1(a)+o),ty(f1(a)+o) to tx(f4(a)+o),ty(f4(a)+o) to tx(f3(a)+o),ty(f3(a)+o)
fi
return

'---------------------------------------------------------
'                     Draw Large Cube;
'---------------------------------------------------------

label inf
 setrgb 1,0,10,20
 fill triangle tx(12),ty(12) to tx(11),ty(11) to tx(10),ty(10)
 fill triangle tx(12),ty(12) to tx(9),ty(9) to tx(10),ty(10)
 setrgb 1,0,20,100
 fill triangle tx(13),ty(13) to tx(14),ty(14) to tx(10),ty(10)
 fill triangle tx(13),ty(13) to tx(9),ty(9) to tx(10),ty(10)
 setrgb 1,0,20,140
 fill triangle tx(13),ty(13) to tx(16),ty(16) to tx(12),ty(12)
 fill triangle tx(13),ty(13) to tx(12),ty(12) to tx(9),ty(9)
 setrgb 1,0,20,140
 fill triangle tx(11),ty(11) to tx(15),ty(15) to tx(14),ty(14)
 fill triangle tx(11),ty(11) to tx(10),ty(10) to tx(14),ty(14)
 setrgb 1,0,20,100
 fill triangle tx(16),ty(16) to tx(15),ty(15) to tx(11),ty(11)
 fill triangle tx(16),ty(16) to tx(11),ty(11) to tx(12),ty(12)
return


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

'---------------------------------------------------------
'            Draw A Face Of The Central Cube;
'---------------------------------------------------------

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/300)
 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))
fi
return

'---------------------------------------------------------
'                      Rotations;
'---------------------------------------------------------
label rotate
'#########################################################
'## Rotate And Scale Each Point! (Inner) Store Result   ##
'#########################################################
 for a=1 to 8
  x1=x(a)
  y1=y(a)
  z1=z(a)
'######################
'## X,Y,Z rotations! ##
'######################
  xx=x1
  yy=y1*cs(xr2)+z1*sn(xr2)
  zz=z1*cs(xr2)-y1*sn(xr2)
  y1=yy
  x1=xx*cs(yr2)-zz*sn(yr2)
  z1=xx*sn(yr2)+zz*cs(yr2)
  zz=z1
  xx=x1*cs(zr2)-y1*sn(zr2)
  yy=x1*sn(zr2)+y1*cs(zr2)
'########################
'## Apply Perspective! ##
'########################
  x(a+16)=20
  x(a+24)=-20
  x(a+32)=-xx
  x(a+40)=xx
  y(a+16)=-yy
  y(a+24)=yy
  y(a+32)=20
  y(a+40)=-20
  z(a+16)=zz
  z(a+24)=zz
  z(a+32)=zz
  z(a+40)=zz
  dv=(zz/80)+1
  xx=size*(xx/dv)
  yy=size*(yy/dv)
  tx(a)=xx
  ty(a)=yy
  tz(a)=zz
 next a
'############################################
'## Scale Each Point! (Outer) Store Result ##
'############################################
 for a=9 to points
  x1=x(a)
  y1=y(a)
  z1=z(a)
'######################
'## X,Y,Z rotations! ##
'######################
  xx=x1
  yy=y1*cs(0)+z1*sn(0)
  zz=z1*cs(0)-y1*sn(0)
  y1=yy
  x1=xx*cs(0)-zz*sn(0)
  z1=xx*sn(0)+zz*cs(0)
  zz=z1
  xx=x1*cs(0)-y1*sn(0)
  yy=x1*sn(0)+y1*cs(0)
'########################
'## Apply Perspective! ##
'########################
  dv=(zz/80)+1
  xx=size*(xx/dv)
  yy=size*(yy/dv)
  tx(a)=xx
  ty(a)=yy
  tz(a)=zz
 next a
return
label initialise
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
' This Sub-Routine Initialises The Program.
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'######################
'## Open Gfx Screen! ##
'######################
   open window 640,512
   window origin "cc"
'---------------------------------------------------------
'            Define the necessary variables;
'---------------------------------------------------------
mp=1
dw=1 : Rem                       Double buffering Variable
points=48: Rem          The amount of points in the object
faces=6 : Rem            The Amount of faces in the object
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
size=12
dim m$(15)
 m$(1)="WELCOME TO SHOCKWAVES LATEST DEMO!"
 m$(2)="CALLED: ON REFLECTION"
 m$(3)="A CUBE DEMO WITH A DIFFERENCE!"
 m$(4)="THE CENTRAL CUBE IS PROPERLY"
 m$(5)="REFLECTED AND MAPPED ONTO THE"
 m$(6)="OUTER CUBE'S FACES!"
 m$(7)="BRIEF HELLOS GO OUT TO:"
 m$(8)="PARABELLUM, XALTHORN, FRYER,"
 m$(9)="DOCTOR, JINX, AZ, JOMORROW,"
m$(10)="DEMONEYE, SNAKEDOGG, KYATA,"
m$(11)="TRICKYKEYBOARD, JIM SHAW, "
m$(12)="JACOB BUSBY, ELL, ZING, PYRO,"
m$(13)="BONGOTRUMMOR, DREW, COMBATKING,"
m$(14)="BRUASSET, BALROQ AND EVERYONE ELSE!"
m$(15)="TEXT RESTARTS NOW..."

'---------------------------------------------------------
'   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 16
 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)
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 -9,9,9,9,9,9,9,-9,9,-9,-9,9
data -9,9,-9,9,9,-9,9,-9,-9,-9,-9,-9

data -20,20,20,20,20,20,20,-20,20,-20,-20,20
data -20,20,-20,20,20,-20,20,-20,-20,-20,-20,-20

'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.

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

return


Shockwave ^ Codigos
Challenge Trophies Won: