  % BackGroundColor 0 , 0 , 0.4, 0.0
  $ filename "t.exe"
 'uses rtl64
  uses glTransG


  sub main()
  ==========

  StepAngle ang1,1

  if opening=0 then goto EachFrame

  Initial:
  ========





  '

  'TORUS
  ======
  float rmin=1.0
  float rmaj=3.0
  'MINOR
  'create circle of points around minor centre
  string srn=nuls(37*6*4)
  float rn at strptr(srn)
  float x,y,z
  int i
  for i=0 to 360 step 10 '36+1 overlap
    x=cos(rad(i))
    y=-sin(rad(i)) 'go clockwise
    z=0
    rn[0]=x
    rn[1]=y
    rn[2]=z
    rn[3]=x 'norm
    rn[4]=y 'norm
    rn[5]=z 'norm
    @rn+=24
  next
  @rn=strptr(srn) 'reset
  '
  'MAJOR
  'sweep circle of points around major centre
  string srm=nuls(37*37*8*4)
  float rm at strptr srm
  int ct1,ct2 'texture mapping
  int st1=1, st2=1
  float mt1=0.999/36, mt2=0.999/18
  int i,j
  for j=0 to 360 step 10 '36 and 1 overlap
    x=cos(rad(j))
    z=-sin(rad(j)) 'go anti clockwise
    for i=0 to 360 step 10 '36 and 1 overlap
      rm[0]=x*(rmaj+rn[0])
      rm[1]=rn[1]
      rm[2]=z*(rmaj+rn[0])
      '
      rm[3]=x*rn[3] 'norm x
      rm[4]=rn[4] 'norm y
      rm[5]=z*rn[3] 'norm z
      '
      rm[6]=mt1*ct1 'texture u
      rm[7]=mt2*ct2 'texture v
      'cycle texture u coords
      ct1+=st1
      '0 1 2 3 4 3 2 1 0 .. ' mirroring textures
      if ct1>18 then st1=-st1 : ct1=17 'reversal
      if ct1<0 then st1=-st1 : ct1=1
      'if ct1>=36 then ct1=0
      @rn+=24 'stride 6 floats
      @rm+=32 'stride 8 floats
    next 'i
    @rn=strptr(srn) 'reset for next circle
    'cycle texture v coords
    ct1=0 : st1=1 'reset
    ct2+=st2
    if ct2>18 then st2=-st2 : ct2=17 'reversal
    if ct2<0 then st2=-st2 : ct2=1
  next 'j
  @rm=strptr(srm) 'reset 37*37*8 floats
  '
  static ShapeObject boa
  boa.init
  boa.ClearVertex
  int e=37*37*8
  boa.putVertex rm,e '8 floats per set of vertex attributes
  srm="" : srn=""

  '
  string sel=nuls(36*36*6*4) '(ct*4 ints)
  int el at strptr sel
  int e=36*36*6
  int k=0
  int i
  for i=0 to 35 '36 circle iterations
    for j=0 to 35 '36 point iterations
      'whole triangles anticlockwise
      el[0]=k'0  'lower right triangle
      el[1]=k+1
      el[2]=k+38
      el[3]=k+38  'upper left triangle
      el[4]=k+37
      el[5]=k'0
      @el+=24 'byte stride of 6 ints
      k+=1 'next point set
    next
    k+=1 'skip 37th
  next
  @el=strptr sel 'reset el to base
  boa.ClearIndex
  boa.putIndex el,e,GL_TRIANGLES
  sel=""
  '
  loadidentity
  savematrix tm
  boa.ClearMatrix
  boa.Putmatrix tm,16 'provisional
  '
  boa.Transfer 'transfer all buffer data from client 


  'VERTICES FOR SQUARE PANELS
  float m=-1,l=1,o=0
  static ShapeObject bob
  bob.init
  bob.ClearVertex
  bob.PutVertex float {
  '------------------------------------
  'VERTEX  NORMAL  TEXCOR
  '------------------------------------
  'FRONT
  m,m,o,   o,o,l,  o,o,  'lower left 
  l,m,o,   o,o,l,  l,o,  'lower right 
  l,l,o,   o,o,l,  l,l,  'upper right
  m,l,o,   o,o,l,  o,l   'upper left

  '------------------------------------
  },32 '8x4 floats SQUARE PANEL
  '
  'ELEMENTS FOR SQUARE PANELS
  '--------------------------
  bob.ClearIndex
  bob.PutIndex int {
    0,1,2, 2,3,0
  },6,GL_TRIANGLES '6 ints
  '
  'backgound panel
  ----------------
  loadidentity
  translatef 0,0,-2400
  scalef 1000,1000,1
  savematrix tm
  bob.ClearMatrix
  bob.Putmatrix tm,16
  bob.Transfer 'transfer all bob data from client 


  BuildTextures


  static float ang3

  float a,d,s,sh

  static float idmat[16]
  SetIdentity idmat


  a=0.2  'ambient
  d=0.8  'diffuse
  s=0.1  'specular
  sh=0.1 'shine

  ShaderProgram shaderMaj.ph

  'ShaderMatrix4 ShaderMaj.m0, 1, GL_FALSE, projection  'PROJECTION
  'ShaderMatrix4 ShaderMaj.m1, 1, GL_FALSE, modelView   'MODELVIEW
  'ShaderTexture shaderMaj.t0, 0, imgn3                'FOR COLOR TEXTURE
  ShaderTexture shaderMaj.t1, 1, norm3                 'FOR NORMAL TEXTURE
  ShaderVector4 ShaderMaj.v0, 1, float {1,1,1,1}       'TINT
  ShaderVector4 ShaderMaj.v1, 4, float {a,a,a,1, d,d,d,1, s,s,s,1, 100,0,0,0} 'LIGHT
  ShaderVector4 ShaderMaj.v2, 1, float {0,0,1,0}       'XYZ LIGHT DIRECTION FROM OBJECT
  ''ShaderVector4 ShaderMaj.v3, 1, float {1,0,0,1}     'FLAT COLOR
  'ShaderVector4 ShaderMaj.v4, 1, float {0.5,0.5,0.7, -150.0}  'FOG COLOR AND DIST LIMIT


  ==========
  EachFrame:
  ==========

  ang3+=0.25


  ShaderProgram shaderMaj.ph
  '
  'SET PROJECTION
  int ortho=0
  SetProjectionMatrix projection, 45, aspect, 1, 1e4, ortho
   'GENERATE TRANSFORM MATRICES
  ShaderMatrix4 ShaderMaj.m0, 1, GL_FALSE, projection  'PROJECTION

  'SET MODEL VIEW
  loadidentity
  if ortho
    scalef 0.2,0.2,0.2      'correction for ortho scale
  endif
  savematrix modelView

  ShaderMatrix4 ShaderMaj.m0, 1, GL_FALSE, projection  'PROJECTION
  ShaderMatrix4 ShaderMaj.m1, 1, GL_FALSE, modelView   'MODELVIEW
 
  'GENERATE TRANSFORM MATRICES


  picklabel 999
  '

  boa.clearmatrix 'clear previous
  loadidentity
  translatef 0,0,-10        'set distance
  rotatef 30, 1,0,0         'tilt
  rotatef      ang3,0,1,0   'Y SPIN
  boa.putmatrix mats,16
  boa.transfermatrix 'COMMIT MATRIX ARRAY


  glBindFramebuffer(GL_FRAMEBUFFER, 0)

  ShaderTexture shaderMaj.t0, 0, imgn4 't0 FOR COLOR TEXTURE
  ShaderVector4 ShaderMaj.v4, 1, float {0.0,0.0,0.0, -9000.0}  'FOG COLOR AND DIST LIMIT
  bob.render 'StaticBackground

  ShaderTexture shaderMaj.t0, 0, imgn3 't0 FOR COLOR TEXTURE
  ShaderVector4 ShaderMaj.v4, 1, float {0.5,0.4,0.4, -20.0}  'FOG COLOR AND DIST LIMIT
  boa.render 'Torus
 
  ShaderProgram -1


  if closing then
    'print hex picked
  end if
  '
  end sub


  MainWindow width,height,WS_OVERLAPPEDWINDOW
