'Vector Object Editor V1.0 Coded By Shockwave.
'This program is designed to help you create 3D objects.
'It WILL NOT write the program for you, however.
'
'I wrote this program because I wanted to make nicer
'objects and try them out in real time, before commiting
'to code. Also I was running out of graph paper.
'
'The program is fairly easy to use and it lets you choose
'where the points go in your 3D objects. It's ideal for
'making convex vector objects for demos.
'
'When you have finished editing, just press the triangle
'button to convert your creation into x,y,z data ready
'for insertion into your own 3D routine.
'
'If you want to contact me: shockwave@ps2-yabasic.co.uk
'
'A Shockwave utility. (C) 2002.
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

gosub initialise
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'                       Main Loop
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
repeat
if del>0 del=del-1
 gosub cleardisplay
 if del=0 gosub dpad
 gosub drawgrid
until (1=2)
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'               End Of Main Loop (infinife)
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'              Preview window rotation code;
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
label rotate
'###############################################
'## Rotate And Scale Each Point! Store Result ##
'###############################################
 for a=1 to tot
  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! ##
'########################
  tx=4*(-xx/((zz/50)+1))+100
  ty=4*(-yy/((zz/50)+1))+400
  fill rect tx,ty to tx+2,ty+2
 next a
xr=xr+1
yr=yr+2
zr=zr+3
if xr>720 xr=xr-720
if yr>720 yr=yr-720
if zr>720 zr=zr-720
return

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'                     Get Dpad Input;
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
label dpad
c=peek("port1")
if c<>0 del=5
if and(c,128)<>0 and xpos<12 xpos=xpos+1
if and(c,32)<>0 and xpos>-12 xpos=xpos-1
if and(c,16)<>0 and ypos<12 ypos=ypos+1
if and(c,64)<>0 and ypos>-12 ypos=ypos-1
if and(c,1024)<>0 and zpos>-12 zpos=zpos-1
if and(c,2048)<>0 and zpos<12 zpos=zpos+1
if and(c,16384)<>0 gosub addremove
if and(c,8)<>0 rot=1
if and(c,1)<>0 rot=0
if and(c,4096)<>0 gosub convert
return

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Convert the object to data and do a screen dump of it.
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
label convert
  setdrawbuf dw
  clear window
  setrgb 1,255,255,255
  text 20,14,"TOTAL VERTICES:"+str$(tot)+", DEFINED AS "+str$(tot*3)+" DATA ELEMENTS"
x=10
y=39
c=0
  for a=1 to tot
c=c+1 
if c>2 c=0
if c=0 setrgb 1,255,255,120
if c=1 setrgb 1,120,255,255
if c=2 setrgb 1,255,120,255

  text x,y,"( "+"XP:"+str$(x(a))+" YP:"+str$(y(a))+" ZP:"+str$(z(a))+" )"
x=x+240
if x>480 then y=y+18:x=10 fi
  next a
setdispbuf dw
label loop
goto loop


'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'Check Cursor Position Against All Active Points, If it
'Exists then remove it, If It Doesn't, Add it.
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
label addremove
flag=0
for a=1 to tot
 if x(a)=xpos and y(a)=ypos and z(a)=zpos then
  x(a)=0:y(a)=0:z(a)=0
  flag=1
  for b=a to tot
    x(b)=x(b+1)
    y(b)=y(b+1)
    z(b)=z(b+1)
  next b
  tot=tot-1
 fi
next a

if flag=0 and tot<mx then
 tot=tot+1
 x(tot)=xpos
 y(tot)=ypos
 z(tot)=zpos
fi
if tot=mx bell
return

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'         Draw The Current Z Plane Of The Grid
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
label drawgrid
setrgb 1,0,0,255
line 400,100 to 400,500
line 200,300 to 600,300
line 200,100 to 600,500
line 600,100 to 200,500
setrgb 1,55,155,55
for x=205 to 605 step 30
line x,100 to x,500
next x
for y=105 to 500 step 30
line 200,y to 600,y
next y
setrgb 1,155,155,255

tx=400-(xpos*15)
ty=300-(ypos*15)
rect tx-10,ty-10 to tx+10,ty+10
for a=1 to tot
if z(a)=zpos then
tx=400-(x(a)*15)
ty=300-(y(a)*15)
fill rect tx-4,ty-4 to tx+4,ty+4
fi
next a

'_________________________________________________________
'                Preview window graphics;
'_________________________________________________________
setrgb 1,255,255,255

if rot=0 then
for a=1 to tot
  tx=3*((-x(a))/((z(a)/50)+1))+100
  ty=3*((-y(a))/((z(a)/50)+1))+400
  fill rect tx,ty to tx+2,ty+2
next a
fi

if rot=1 then
gosub rotate
fi
return


'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'         Clear And Double Buffer Display Area;
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
label cleardisplay
setdrawbuf dw
dw=1-dw
setdispbuf dw
setrgb 1,155,155,255
rect 199,99 to 601,501
rect 1,300 to 200,501
rect 20,30 to 620,70
setrgb 1,0,0,0
fill rect 200,100 to 600,500
fill rect 2,301 to 198,500
fill rect 21,31 to 619,69

setrgb 1,100,250,150
text 320,42," Z POS:"+str$(zpos)+" X POS:"+str$(xpos)+" Y POS:"+str$(ypos)+" No. OF VERTICES:"+str$(tot),"cc"
if rot=0 text 320,62,"PRESS START FOR ROTATE PREVIEW","cc"
if rot=1 text 320,62,"PRESS SELECT TO END ROTATE PREVIEW","cc"
return

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'     Initialise The Display And Program Variables.
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
label initialise
open window 640,512
for a=1 to 2
setdrawbuf dw
dw=1-dw
setrgb 1,50,150,250
text 320,20,"VECTOR OBJECT EDITOR V1.0 (C) SHOCKWAVE 2002","cc"
text 200,90,"OBJECT DESIGN WINDOW (OBJECT  VERTICES):"
text 104,290,"OUTPUT AS VERTICES:","cc"
text 100,100,"DPAD MOVE CURSOR","cc"
text 100,115,"X ADD/DEL POINT","cc"
text 100,130,"L1 DECREASE Z","cc"
text 100,145,"R1 INCREASE Z","cc"
text 100,160,"L2+R2 CLEAR OBJECT","cc"
text 100,175,"","cc"
text 100,190,"WARNING!!!","cc"
text 100,205,"CONVERTING TO DATA","cc"
text 100,220,"WILL PREVENT FURTHER","cc"
text 100,235,"EDITING!","cc"
text 100,259,"  TO CONVERT","cc"
triangle 52,250 to 58,260 to 48,260

next a
rot=0

xpos=0
ypos=0
zpos=0
mx=50
tot=0
dim x(mx+1),y(mx+1),z(mx+1)
 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

return



