  $ FileName "t.exe"
  'uses RTL32
  'uses RTL64
  string title="Plane"
  % ExplicitMain
  uses OpenglSceneFrame
  uses glo2\shapes
  uses glo2\textures
  uses glo2\materials
  uses glo2\particles

  indexbase 1
  sys   GdiplusToken
  float ang1
  float ma[16]
  sys   cube,sphere
  Cloud cloud1


  type vector double x,y,z

  'constants

  'rp=1.0
  'phi=(sqr(5)-1)*.5 'equ .5*s18
  'hphi=phi*.5 '==s18
  double s18=sin(0.1*pi),c18=cos(0.1*pi)
  double s36=sin(0.2*pi),c36=cos(0.2*pi)
  double t18=s18/c18
  double st18=s18*t18
  '
  'initial
  '
  vector pp0,pp1,pp2,pp3,pp4,pp5
  ' .z is 0.0
  pp1={-s36,-c36}
  pp2={ s36,-c36}
  pp3={ c18, s18}
  pp4={ 0.0, 1.0}
  pp5={-c18, s18}
  '
  'parent to child
  '
  double hsp=s36
  double rc=1.0/(1.0+2.0*c36)    'child radius
  double sc=2.0*rc*s36           'child side

  function interp(vector *p,*q,*r,double v)
  =========================================
  r.x=p.x+(q.x-p.x)*v
  r.y=p.y+(q.y-p.y)*v
  end function

  function RenderPent(vector *p0,*p1,*p2,*p3,*p4,*p5,sys m)
  =========================================================
  glBegin GL_TRIANGLE_FAN
  glNormal3f 0.,0.,1.
  glTexCoord2f 0.5,0.5 : glVertex3dv p0
  glTexCoord2f 0.0,0.0 : glVertex3dv p1
  glTexCoord2f 1.0,0.0 : glVertex3dv p2
  glTexCoord2f 1.0,1.0 : glVertex3dv p3
  glTexCoord2f 0.5,1.0 : glVertex3dv p4
  glTexCoord2f 0.0,1.0 : glVertex3dv p5
  glTexCoord2f 0.0,0.0 : glVertex3dv p1
  glend
  if m=1
    glTranslatef 0.,0.,.001
    glDisable GL_LIGHTING
    glDisable GL_TEXTURE_2D
    glColor4f 1.,1.,1.,1.
    glBegin GL_LINE_STRIP
    glVertex3dv p1
    glVertex3dv p2
    glVertex3dv p3
    glVertex3dv p4
    glVertex3dv p5
    glVertex3dv p1
    glEnd
    glEnable GL_LIGHTING
    glEnable GL_TEXTURE_2D
    glTranslatef 0.,0.,-.001
  end if
  end function


  'recursion
  function PentFractal(vector *p0,*p1,*p2,*p3,*p4,*p5,int c,m)
  ==========================================================
  if c=0
    RenderPent(p0,p1,p2,p3,p4,p5,m)
    exit function
  end if
  c--
  vector q0,q1,q2,q3,q4,q5,qi1,qi2,qi3,qi4,qi5
  vector q12,q21,q23,q32,q34,q43,q45,q54,q51,q15
  vector qc1,qc2,qc3,qc4,qc5
  vector mp12,mp23,mp34,mp45,mp51
  '
  double f=rc
  interp(p1,p2, q12, f) : interp(p2,p1,q21, f)
  interp(p2,p3, q23, f) : interp(p3,p2,q32, f)
  interp(p3,p4, q34, f) : interp(p4,p3,q43, f)
  interp(p4,p5, q45, f) : interp(p5,p4,q54, f)
  interp(p5,p1, q51, f) : interp(p1,p5,q15, f)
  '
  interp(p1,p0, qc1, rc)
  interp(p2,p0, qc2, rc)
  interp(p3,p0, qc3, rc)
  interp(p4,p0, qc4, rc)
  interp(p5,p0, qc5, rc)
  '
  interp(p1,p2, mp12,.5)
  interp(p2,p3, mp23,.5)
  interp(p3,p4, mp34,.5)
  interp(p4,p5, mp45,.5)
  interp(p5,p1, mp51,.5)
  '
  double f=rc/c36
  interp(p0,mp12,qi1,f)
  interp(p0,mp23,qi2,f)
  interp(p0,mp34,qi3,f)
  interp(p0,mp45,qi4,f)
  interp(p0,mp51,qi5,f)
  '
  'recurse centre then anticlockwise
  PentFractal(p0,qi1,qi2,qi3,qi4,qi5,c,m) 'centre
  PentFractal(qc1,p1,q12,qi1,qi5,q15,c,m) 'bottom left
  PentFractal(qc2,q21,p2,q23,qi2,qi1,c,m) 'bottom right
  PentFractal(qc3,qi2,q32,p3,q34,qi3,c,m) 'upper right
  PentFractal(qc4,qi4,qi3,q43,p4,q45,c,m) 'top
  PentFractal(qc5,q51,qi5,qi4,q54,p5,c,m) 'upper left
  if c>0
    'grandchild fill-ins
    c--
    vector qa1,qa2,qa3,qa4,qa5
    vector qb1,qb2,qb3,qb4,qb5
    vector qd1,qd2,qd3,qd4,qd5
    vector qe1,qe2,qe3,qe4,qe5
    double f=rc
    interp(q12,qi1,qa1,f) : interp(q21,qi1,qb1,f)
    interp(q23,qi2,qa2,f) : interp(q32,qi2,qb2,f)
    interp(q34,qi3,qa3,f) : interp(q43,qi3,qb3,f)
    interp(q45,qi4,qa4,f) : interp(q54,qi4,qb4,f)
    interp(q51,qi5,qa5,f) : interp(q15,qi5,qb5,f)
    '
    'qd centre vectors for grandchildren
    double f=1.0-rc*rc*s18 'cos radius scaling
    interp(p0,mp12,qd1,f)
    interp(p0,mp23,qd2,f)
    interp(p0,mp34,qd3,f)
    interp(p0,mp45,qd4,f)
    interp(p0,mp51,qd5,f)
    '
    'double f=1.0/(c18*c18)
    double f=1.0/c36
    interp(qi1,mp12,qe1,f)
    interp(qi2,mp23,qe2,f)
    interp(qi3,mp34,qe3,f)
    interp(qi4,mp45,qe4,f)
    interp(qi5,mp51,qe5,f)
    'recurse fill-ins
    'PentFractal(qd1,qb1,qa1,q12,qe1,q21,c,m) 'bottom
    'PentFractal(qd2,qb2,qa2,q23,qe2,q32,c,m) 'lower right
    'PentFractal(qd3,qb3,qa3,q34,qe3,q43,c,m) 'upper right
    'PentFractal(qd4,qb4,qa4,q45,qe4,q54,c,m) 'upper left
    'PentFractal(qd5,qb5,qa5,q51,qe5,q15,c,m) 'lower left
  end if
  end function


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

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

  sub RenderCube(float x,y,z,sc,sys 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,tex,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
  '
  StillFrame
  glClearColor 0.5, 0.5, 0.7, 0
  Fog          0.5, 0.5, 0.7, 0.035
  '
  sys t1=texn[10]
  sys t2=texn[11]
  StandardLighting li
  '
  '
  'VIEW CONTROL WITH KEYBOARD KEYS
  '
  static float cmmx,cmmy,cmmz,cmax,cmay,cmaz
  glrotatef    cmax,1,0,0 'rotate yz : pitch
  glrotatef    cmay,0,1,0 'rotate xz : yaw
  glrotatef    cmaz,0,0,1 'rotate xy : yaw
  glTranslatef cmmx,cmmy,cmmz
  '
  if not key[16] 'shift'
    if key[37] then cmmx+=.01*cos(rad(cmay)) : cmmz+=.01*sin(rad(cmay))
    if key[39] then cmmx-=.01*cos(rad(cmay)) : cmmz-=.01*sin(rad(cmay))
    if key[38] then cmmx-=.01*sin(rad(cmay)) : cmmz+=.01*cos(rad(cmay))
    if key[40] then cmmx+=.01*sin(rad(cmay)) : cmmz-=.01*cos(rad(cmay))
    if key[33] then cmmy-=.01
    if key[34] then cmmy+=.01
  else
    if key[37] then cmay-=1
    if key[39] then cmay+=1
    if key[38] then cmax-=1
    if key[40] then cmax+=1
    if key[33] then cmaz+=1
    if key[34] then cmaz-=1
    if key[36] 
      cmax=0. : cmay=0. : cmaz=0. 'HOME'
    end if
  end if
  '
  'SPHERES
  glPushMatrix
  indexbase 1
  glTranslatef -20.,10.,-60.0
  Material Red
  glScalef      6.,6.,6.
  glCallList sphere
  Material White
  glPopMatrix
  glPushMatrix
  glTranslatef 2.5,-1.0,-6.0
  glScalef      .5,.5,.5
  glCallList sphere
   glPopMatrix
  '
  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
  '
  glBindTexture GL_TEXTURE_2D,t2
  glPushMatrix
  glTranslatef 0.0, -2.0, -15.0
  glScalef     10.0,  10.0,   10.0
  glRotatef   -00.0,  1,0,0
  '
  PentFractal pp0,pp1,pp2,pp3,pp4,pp5,4,1
  '
  glPopMatrix
  '
  glDisable GL_LIGHTING
  glDisable GL_TEXTURE_2D
  '
/*
  '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
