  '% BackGroundColor 0 , 0 , 0.2, 0.0
  $ filename "t.exe"
 'uses rtl64
  uses glTransE

  'ARRAY OF MODEL VIEW MATRICES
  '
  %mmax 0x1000
  float matf[mmax*16] 'matrix array
  float vz[mmax]      'float array for z (translation)
  sys   midx[mmax]    'index for ascending-order of z


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

  StepAngle ang1,1

  if opening=0 then goto EachFrame

  Initial:
  ========

  static ShapeObject boa
  boa.init

  '

  'VERTICES
  float m=-1,l=1,d=2,o=0
  boa.PutVertex float {
  '----------------------
  'VERTEX  NORMAL  TEXCOR
  '------------------------------------
  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
  'UPPER
  m,l,o,   o,o,l,  o,o,  'lower left 
  l,l,o,   o,o,l,  l,o,  'lower right 
  l,d,o,   o,o,l,  l,l,  'upper right
  m,d,o,   o,o,l,  o,l   'upper left
  '------------------------------------
  },64 'TWO SQUARE PANELS


 'ELEMENTS FOR SQUARE PANEL
 '-------------------------
  boa.PutIndex int {0,1,2, 2,3,0,  4,5,6, 6,7,4 },12,GL_TRIANGLES
 'boa.PutIndex int {0,1,3,2},4,GL_TRIANGLE_STRIP

  boa.Transfer 'transfer all buffer data from client 


  CreateTexture       imgn0, "../../images/glass.jpg"

  int a,b,c

  a=PixelColor BackGroundColor '0x00300000
  b=0xa0a0ffff
  c=0xffffffff
  '
  CreateSynthTexture imgn1, int {
  a,a,a,
  a,b,a,
  a,a,a
  }, bytesof

  CreateSynthTexture imgn2, int {
  a,a,a,a,a,
  a,b,a,b,a,
  a,a,b,a,a,
  a,b,a,b,a,
  a,a,a,a,a
  }, bytesof

  CreateSynthTexture imgn3, int {
  a,a,a,a,a,a,a,
  a,a,a,b,a,a,a,
  a,a,b,b,b,a,a,
  a,b,b,c,b,b,a,
  a,a,b,b,b,a,a,
  a,a,a,b,a,a,a,
  a,a,a,a,a,a,a
  }, bytesof

  'NORMAL XYZ IN RGB CHANNELS
  'DEPTH SHADING IN A CHANNEL
  a=0xff0000ff 'MAX X
  b=0xff00ff00 'MAX Y
  c=0xffff0000 'MAX Z
  '
  CreateSynthTexture norm1, int {
  a,a,a,a,a,a,a,
  a,a,a,b,a,a,a,
  a,a,b,b,b,a,a,
  a,b,b,c,b,b,a,
  a,a,b,b,b,a,a,
  a,a,a,b,a,a,a,
  a,a,a,a,a,a,a
  }, bytesof
a=c
  CreateSynthTexture norm2, int {
  a,c,a,c,a,c,a,
  a,c,a,c,a,c,a,
  a,c,a,c,a,c,a,
  a,c,a,c,a,c,a,
  a,c,a,c,a,c,a,
  a,c,a,c,a,c,a,
  a,c,a,c,a,c,a
  }, bytesof

  

  float a=.15,d=.85,s=.00,sh=0
  float projection[16]
  ShaderProgram shaderMaj.ph



  'SET UNIFORM VARIABLES:

  SetProjectionMatrix projection, 45, aspect  , 1, 1e6
 'SetProjectionMatrix projection, 45, aspect  , 1, 1e6,1 'ortho

  static float idmat[16]
  SetIdentity idmat,4

  ShaderMatrix4 ShaderMaj.m0, 1, GL_FALSE, projection
  ShaderMatrix4 ShaderMaj.m1, 1, GL_FALSE, idmat       'MODELVIEW (fixed)
  ShaderTexture shaderMaj.t0, 0, imgn0                 'FOR COLOR TEXTURE
  ShaderTexture shaderMaj.t1, 1, norm2                 'FOR NORMAL TEXTURE
  ShaderVector4 ShaderMaj.v0, 1, float {1,1,1,.7}      '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


  EachFrame:
  ==========
  '

  shaderMaj.use
  '
  'GENERATE ARRAY OF TRANSFORMS
  float ang2,ang3
  sys   i,j,k,m
  sys   n=8 'sides
  float ang=360/n
  float d=tan(rad(90-ang/2))
  '
  j=0 : k=0
  'for m=0 to 1 'with inner circle
    for i=0 to <n
      ang2=i*ang
      ang3=ang1+ang2
      ' 
      loadidentity                'reset model view matrix
      scalef 1/aspect,1,1         'correction for viewport aspect ratio
      translatef   0,0,-10        'centre of ring
      'scalef 1, 1, 0.0001        'scale down for ortho projection
      if m then  scalef .5,.5,.5  'inner ring
      rotatef      20,  1,0,0     'tilt towards you
      if m then  translatef 1,0,0 'inner ring
      rotatef      ang3,0,1,0     'rotate around vertical axis
      translatef   0,0,d
      savematrix   matf(j)
      '
      vz[k]=matf[j+14] 'COLLECT Z DISPLACEMENT
      j+=16 : k+=1
    next
  'next
  '
  'ARRANGE PANELS INTO ASCENDING Z ORDER
  sortindex vz, midx, k


  glBindFramebuffer(GL_FRAMEBUFFER, 0)
  '

  'RENDER PANELS AS DIFFERENT INSTANCES (A SINGLE RENDER CALL)


  float tm[16]
  '
  '
  'RENDER BLACK BACKGROUND PANEL FOR PICKING
  PickerBackGround

  'RENDER PANELS AS DIFFERENT INSTANCES (A SINGLE RENDER CALL)
  ShaderMatrix4 ShaderMaj.m1, 1, GL_FALSE, idmat 'fixed modelView

  'RENDER PANELS IN Z ORDER (FARTHEST FIRST)
  sys nn=k
  boa.clearmatrix
  for i=0 to <nn
    j=midx[i]*16 'SELECT MATRIX VIA INDEX (ASCENDING ORDER)
    boa.putmatrix matf[j],16
  next
  '
  PickLabel 998
  boa.transfermatrix
  boa.render 'all panels
  loadidentity
  translatef 0,4,-10 'distant group
  savematrix tm
  ShaderMatrix4 ShaderMaj.m1, 1, GL_FALSE, tm ' modelView
  PickLabel 999
  boa.render 'all panels
 
  ShaderProgram -1
  if closing then
    'print hex picked
  end if
  '
  end sub


  MainWindow width,height,WS_OVERLAPPEDWINDOW
