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

  '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

  '

  'cubic sphere face
  ==================
  int ns=4
  int ct=ns+ns+1
  int i=-ns, j=-ns, ei=ns, ej=ns
  float a,b,sx,sy,sz,cz
  string s=nuls(4*8*ct*ct)
  float f at strptr s
  i=-ns
  while i<=ei
    j=-ns
    float cd=0.5/ns 'texture strider
    while j<=ej
      sy=i/ei
      sx=j/ej
      'sz=1.0
      '
      'normalize for sphere
      a=1/sqr(sx*sx + sy*sy + 1.0)
      sx*=a : sy*=a : sz=a
      '
      'normalize for cylinder
      'a=1/sqr(sx*sx + 1.0)
      'sx*=a : sz=a
      '
      'normalize for cone
      'a=1/sqr(sx*sx + 1.0)
      'b=(i+ei)/ct
      'sx*=a*b : sz=a*b
      '
      'vertex position
      f[0]=sx
      f[1]=sy
      f[2]=sz
      'normals same
      f[3]=sx
      f[4]=sy
      f[5]=sz
      'texture coords
      f[6]=(j+ej)*cd 'u 
      f[7]=(i+ei)*cd 'v
      @f+=32 'byte stride of 8 floats
      j++
    wend
    i++
  wend
  @f=strptr s 'reset f to base
  boa.ClearVertex
  boa.putVertex f,8*ct*ct '8 floats per set of vertex attributes
  s=""

  string s=nuls(ct*ct*3*2*4) '(ct*ct*3*2 ints)
  int el at strptr s
  int e=ct*ct*6
  int k=0
  for i=0 to ct-2
  for j=0 to ct-2
    el[0]=j+k'0  'lower right triangle
    el[1]=j+k+1
    el[2]=j+k+ct+1
    el[3]=j+k+ct+1  'upper left triangle
    el[4]=j+k+ct
    el[5]=j+k'0
    @el+=24 'byte stride of 6 ints
  next
  k+=ct
  next
  @el=strptr s 'reset el to base
  boa.ClearIndex
  boa.putIndex el,e,GL_TRIANGLES
  s=""




  boa.Transfer 'transfer all buffer data from client 


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

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

  int a,b,c

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

  CreateSynthTexture imgn3, 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 imgn4, 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=0x77ff0000 'X
  b=0xaaff0000 'Y
  c=0xffff0000 '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

  'NORMAL XYZ IN RGB CHANNELS
  'DEPTH/BUMP SHADING IN A CHANNEL
  a=0x44ff0000 'z
  b=0x88ff0000 'z
  c=0xffff0000 'Z
  '
  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


  a=0xffff0000 'z
  b=0xffff0000 'z
  c=0xffff0000 'Z
  %=wh 32*32
  int tx[wh]
  int i=0,e=wh
  while i<e
    tx[i]=a : i++
    tx[i]=c : i++
  wend
  e*=4
  CreateSynthTexture norm3,tx,e
  

  static float ang3

  float a,d,s,sh
  float projection[16]
  float modelView[16]
  float tm[16]

  static float idmat[16]
  SetIdentity idmat



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

  float ang2
  sys   i,j,k,m
  sys   n=4 'sides
  'float ang=360/n

  ang3+=0.25

  ShaderProgram shaderMaj.ph

  'SET UNIFORM VARIABLES:
 


  int ortho=0

  SetProjectionMatrix projection, 45, aspect, 1, 1e4, ortho

  loadidentity
  translatef 0,0,-4         'set distance
  rotatef 90, 1,0,0         'tilt
  rotatef      ang3,0,1,0   'Y SPIN
  if ortho
    scalef 0.2,0.2,0.2      'correction for ortho scale
  endif
  savematrix modelView

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

  ShaderMatrix4 ShaderMaj.m0, 1, GL_FALSE, projection  'PROJECTION
  ShaderMatrix4 ShaderMaj.m1, 1, GL_FALSE, modelView   'MODELVIEW
  ShaderTexture shaderMaj.t0, 0, imgn1                 '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


  '

  '
  'GENERATE ARRAY OF TRANSFORMS

  boa.clearmatrix
  '
  ' 
  loadidentity
  'scalef 1/aspect,1,1         'correction for viewport aspect ratio

  'rotatef      ang3,0,1,0     'Y SPIN

  picklabel 999
  '

  savematrix tm
  boa.putmatrix tm,16

  rotatef 90,0,1,0
  savematrix tm
  boa.putmatrix tm,16

  rotatef 90,0,1,0
  savematrix tm
  boa.putmatrix tm,16

  rotatef 90,0,1,0
  savematrix tm
  boa.putmatrix tm,16

  rotatef 90,0,1,0

  rotatef -90,1,0,0
  savematrix tm
  boa.putmatrix tm,16 'TOP

  rotatef 180,1,0,0
  savematrix tm
  boa.putmatrix tm,16 'BOTTOM


  boa.transfermatrix 'COMMIT MATRIX ARRAY




  glBindFramebuffer(GL_FRAMEBUFFER, 0)

  boa.render 'all panels
 
  ShaderProgram -1

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


  MainWindow width,height,WS_OVERLAPPEDWINDOW
