'        
'   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'   @ True 3D Stars  Coded By Mr. Shockwave (C) 2001! @
'   @-===============================================-@
'   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'
'   This one is a result of my re-coding all my 3D code
'  +---------------------------------------------------+
'  :It's a nice interactive  3D starfield Demonstration:
'  :and  this is a true 3D starfield.. The others I did:
'  :before never had rotations This looks nicer somehow:
'  +---------------------------------------------------+
'#########################################################

gosub initialise
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
repeat
'#######################################################
'## The Main Loop. These Calls Are Repeated Until (X) ##
'#######################################################
setdrawbuf dw
dw=1-dw
setdispbuf dw
setrgb 1,10,10,30
setrgb 2,10,10,30
setrgb 3,40,20,0

gtriangle 0,0 to 640,512 to 0,512
gtriangle 0,0 to 640,512 to 640,0

gosub rotate
gosub draw
gosub display
until (and(peek("port1"),1)<>0)
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
exit
label display
'################################################
'## The interacitve bit. Also does text stuff. ##
'################################################
c=peek("port1")
if c <> mmm mmm=0
 if mmm=c c=0
if (and(c,16384)<>0) then 
umode=-umode
mmm=16384
end if
setrgb 1,255,255,255
if umode=-1 then 
text 100,30,"THE INTERACTIVE STARFIELD DEMO BY SHOCKWAVE"
text 100,50,"USER CONTROL MODE : OFF! PRESS (X) TO START"
xin=1
yin=1
zin=2
fi
if umode=1 then
if (and(c,128)<>0) xin=-5
if (and(c,64)<>0) yin=5
if (and(c,16)<>0) yin=-5
if (and(c,32)<>0) xin=5
if (and(c,1024)<>0) zin=-5
if (and(c,2048)<>0) zin=5
text 100,30,"THE INTERACTIVE STARFIELD DEMO BY SHOCKWAVE"
text 100,50,"USER CONTROL MODE : ON! PRESS X TO TURN OFF"
text 100,70,"TRY PRESSING YOUR DPAD AND SHOULDER BUTTONS"
if xin>0 xin=xin-1
if xin<0 xin=xin+1
if yin>0 yin=yin-1
if yin<0 yin=yin+1
if zin>0 zin=zin-1
if zin<0 zin=zin+1
fi
text scx,480,mid$(s$,p,67)
scx=scx-2
if scx<-10 then
scx=scx+10
p=p+1
if p>len(s$) p=0
fi
return


label draw
'########################
'## Draw The object!!! ##
'########################
b=1
for a=1 to polys
sz=((80-tz(a))/14)
setrgb 1,sz,sz,sz*25
fill rectangle tx(a)-sz,ty(a)-sz to tx(a)+sz,ty(a)+sz
setrgb 1,50,250,250
fill rectangle tx(a)-2,ty(a)-2 to tx(a)+2,ty(a)+2
next a
return

label rotate
'###############################################
'## Rotate And Scale Each Point! Store Result ##
'###############################################
 for a=1 to polys
z(a)=z(a)+2
if z(a)>50 z(a)=z(a)-100
  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
 next a
xr=xr+xin
yr=yr+yin
zr=zr+zin
if xr>=720 xr=xr-720
if yr>=720 yr=yr-720
if zr>=720 zr=zr-720
if xr<=0 xr=xr+720
if yr<=0 yr=yr+720
if zr<=0 zr=zr+720
return


label initialise
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
' This Sub-Routine Initialises The Program.
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'######################
'## Open Gfx Screen! ##
'######################
open window 640,512
s$="                                                                "
s$=s$+"   The interactive starfield demo by Shockwave..  "
s$=s$+"This one uses a true 3D starfield code. The other "
s$=s$+"Yabasic ones you have seen are fakes... I wanted "
s$=s$+"to see if it could be achieved properly. Xalthorn"
s$=s$+" did a dot demo that showed me that it was possibl"
s$=s$+"e to get a reasnoble speed.. I set about recoding "
s$=s$+"all my 3D routines to make them faster.. Than"
s$=s$+"k you for the inspiration Marc, this is the result"
s$=s$+"!!!   Quick handshakes to these:  Xalthorn, Doctor"
s$=s$+", Adam Lay, Jinx, Fryer, Jacob Busby, Master Tonbe"
s$=s$+"rry and all the other forum users at WWW.YABASIC.C"
s$=s$+"O.UK   And to all my personal friends... Try the "
s$=s$+"(X) button and then get interactive... (C) Shockwave 2001...  "
p=0
scx=0
'#####################################
'## Define the necessary variables! ##
'#####################################
size=6: rem how big do you want it?
dw=1 : Rem            Double buffering Variable
polys=48 : Rem        The amount of Stars.
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
umode=-1 : Rem user control mode on/off 1/0.
mmm=0.0
'##########################
'## 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
'#########################
'## Create Starfield    ##
'#########################
mmm=-25
for a=1 to polys 
x(a)=-40+ran(80)
y(a)=-40+ran(80)
z(a)=mmm
mmm=mmm+100/polys
next a
return




