
  $ FileName "t.exe"
 'uses RTL64.inc
  string title="Travel Towards objects (use mouse or press 1..6) "
  % ExplicitMain
  uses OpenglSceneFrame
  uses MinMidi
  uses glo2\shapes
  uses glo2\textures
  uses glo2\materials
  uses glo2\particles

  indexbase 1

  sys   GdiplusToken
  float ang1
  float ma[16]
  float li[16]
  int   cube,sphere
  Cloud cloud1


  sub initialize(sys hWnd)
  ========================
  '
  GDIplus 1
  '
  static int res=256
  BuildTextures 16,res
  '
  cube=CompileList   : CubeForm      : glEndList
  sphere=CompileList : Spheric 1,1,6 : glEndList
  '
  end sub

  sub Release(sys hwnd)
  '====================
  DeleteAllGlCompiled
  DeleteTextures  16
  Gdiplus         0
  end sub

  sub RenderCube(float x,y,z,sc,int tex)
  ======================================
  glBindTexture GL_TEXTURE_2D,tex
  glPushMatrix
  gltranslatef x,y,z
  glscalef sc,sc,sc
  glCallList cube
  'glCallList pent
  glPopMatrix
  end sub

  sub RenderPlane(float x,y,z,sc,int tex, float u,v)
  ==================================================
  glBindTexture GL_TEXTURE_2D,tex
  glPushMatrix
  gltranslatef x,y,z
  glscalef sc,1,sc
  glBegin GL_QUADS
  glTexCoord2f 0.0,0.0 : glVertex3f -1.0,0,-1.0
  glTexCoord2f u  ,0.0 : glVertex3f  1.0,0,-1.0
  glTexCoord2f u  ,v   : glVertex3f  1.0,0, 1.0
  glTexCoord2f 0.0,v   : glVertex3f -1.0,0, 1.0    
  glend
  glPopMatrix
  end sub


  sub scene(sys hWnd)
  '==================
  '
  static single ra,ri,angi1=.5
  int t1=texn[10]
  int t2=texn[11]
  '
  '
  ActiveFrame
  glClearColor 0.5, 0.5, 0.7, 0.0  'rgba
  Fog          0.5, 0.5, 0.7, 0.05 'rgb and density
  BeginPick
  Navigation
  StandardLighting li
  StandardMaterial ma
  '
  '
  'SPHERE OBJECTS
  '
  type dest
    float x,y,z,r
  end type

  static dest v=
  {
  'x   y   z     r
  -20.,10.,-60.0,6.0,
   2.5,-.5,-06.0,1.0,
   2.5,-.5,-12.0,1.0,
  -2.5,-.5,-18.0,1.0,
  -2.5,-.5,-24.0,1.0,
   0.0,-.5,-30.0,1.0
  }
  macro Translate(v) '(dest*v)
    glTranslatef v.x, v.y, v.z
    glScalef v.r,v.r,v.r
  end macro
  '
  indexbase 1
  macro RenderSphere(n,m)
    PickLabel n
    glPushMatrix
    Translate v[n]
    Material m
    glCallList sphere
    glPopMatrix
  end macro
  '
  RenderSphere 1,Red
  RenderSphere 2,White
  RenderSphere 3,RedShiny
  RenderSphere 4,GreenShiny
  RenderSphere 5,BlueShiny
  RenderSphere 6,White
  '
  label 0
  '
  '
  'ORIENTATE AND TRAVEL TOWARDS
  '
  macro PlusMinus180(a) '(float*a)
    if a>=360 then a-=360 'clamp 0 to 359..
    if a<0    then a+=360
    if a>180 then a-=360  'clamp +180 / -179..
  end macro
  '
  static float cx,cz,dh,di,dy,dz,dr,ds,ay,az,aay,clos
  static int targ,targy,targm,targsel
  '
  select lastkey
  case "1" to "6"
    targsel=LastKey-0x30
    LastKey=0              'stop further evaluation
  end select
  '
  if picked<>0 and picking=0
    targsel=picked
'SetWindowText hwnd,str picked
    picked=0
  end if
  '
  if targsel
    targ=targsel           'target object index
    dx=cmmx+v[targ].x      'x distance to target
    dy=cmmy+v[targ].y      'y distance to target
    dz=cmmz+v[targ].z      'z distance to target
    dr=hypot(dx,dz)        'distance to target
    di=dy/dr               'upward y increment
    ay=atan(dx,-dz)*180/pi '
    PlusMinus180 ay        'target angle
    PlusMinus180 cmay      'start angle
    aay=.01*(ay-cmay)      'angle increment
    ds=0.125*v[targ].r     'limit to step resolution
    clos=v[targ].r*2.0+1.0 'nearest allowed
    targy=targ             'activate turning
    targsel=0
  end if
  '
  'Turning: Look left/right
  '
  if targy then
    if abs(cmay-ay)<=abs(aay)
      aay*=.5
      if abs(aay)<.001 then
        targm=targy 'Activate Travel
        targy=0
      end if
    else
      cmay+=aay
    end if
  end if
  '
  'Travel Towards
  '
  if targm
    cx=cmmx-ds*sin(rad(cmay))
    cz=cmmz+ds*cos(rad(cmay))
    dh=hypot(cx+v[targ].x,cz+v[targm].z)
    if dh<=clos
      ds*=.5
      if ds<.00001 then targm=0
    else
      cmmx=cx
      cmmz=cz
      cmmy-=di*ds
    end if
    '
  end if
  '
  EndPick
  '
  Material White
  glEnable GL_TEXTURE_2D
  '
  RenderCube   2.5,-2.0,-6.0, 0.5,t1
  '
  RenderPlane  0.0,-2.0,-16.0,16.0,t2,8,8
  '
  glDisable GL_TEXTURE_2D
  glDisable GL_LIGHTING
  '
  'CLOUD
  glEnable GL_TEXTURE_2D
  glPushMatrix
  gltranslatef -10.0, -10.0, -48.0
  glscalef      20.0,12.0,1.0
  cloud1.render 30 'max 50
  glPopMatrix
  glDisable GL_TEXTURE_2D
  '
  ang1+=angi1
  if ang1>360 then ang1-=360
  sleep 10
  end sub

  MainWindow width,height,WS_OVERLAPPEDWINDOW
