  #compact
  $ title     "Shaders and Index Buffer Objects"
  $ FileName  "t.exe"
 'uses RTL64
  %ExplicitMain
  uses glTransG
  'uses glo2\particles

  type tParticle
  ===============
  float x,y,z
  float xsize,ysize,speed,angle
  float r,g,b
  sys   tex
  end type


  static ShapeObject bob

  ===============
  class cloudBob
  ===============
  tparticle p[100]
  '
  method Render(sys en)
  sys       i,e
  tparticle q at (@p)
  static float fr=2*0.0167 '1/60
  static int texsetup
  if not texsetup
    'b background
    'h half
    'f forground
    int f,b,h
    f=0xffffffff : h=0x40ffffff : b=0x00ffffff
    '
    CreateSynthTexture simgc1, int {
    b,b,b,b,
    b,f,h,b,
    b,h,f,b,
    b,b,b,b
    }, bytesof
    '
    f=0xffffffff : h=0x40ffff80 : b 0x00ffffff
    CreateSynthTexture simgc2, int {
    b,b,b,b,
    b,f,h,b,
    b,h,f,b,
    b,b,b,b
    }, bytesof
    texsetup=1
  endif
  e=en
  for i = 1 to e
  if q.x > 5 or q.speed=0
    'renew
    static int n
    n=1-n 'toggle 0 and 1
    q.x     = Rnd()*.5
    q.y     = 0
    q.z     = -e*.1 / i 'z layers
    q.xsize = .01+aRnd()*0.02
    q.ysize = .01+aRnd()*0.01
    q.Speed = .05+aRnd()*.02
    q.Angle = aRnd()*360
    q.tex   = simgc1 + n
  else    
    q.xsize += .12*fr
    q.ysize += .10*fr
    q.x     += .9*q.Speed*fr
    q.y     += .3*q.Speed*fr
    'glBindTexture GL_TEXTURE_2D,q.tex
    'if q.x>.5
    '  'glColor4f .99,.99,.99,.7-q.x *.7 'fades with xdistance
    'else
    '  'glColor4f .99,.99,.99,q.x*.7 'increases with xdistance
    'end if
    '
    ShaderTexture shaderMaj.t0, 0, q.tex 't0 FOR COLOR TEXTURE
    ShaderVector4 ShaderMaj.v0, 1, float {1,1,1,.1}       'TINT
    ShaderVector4 ShaderMaj.v4, 1, float {0.0,0.0,0.0, -10000.0}  'FOG COLOR AND DIST LIMIT
    '
    'Draw
    PushMatrix
    LoadIdentity   
    Translatef q.x, q.y, q.z
    q.angle+=.03
    Scalef q.xsize,q.ysize,1.0
    Rotatef q.angle, 0, 0, 1
    SaveMatrix tm
    Bob.ClearMatrix
    Bob.PutMatrix tm,16
    Bob.TransferMatrix
    Bob.Render
   PopMatrix   
  end if
  @q+=sizeof tparticle
  next  
  end method
  '
  end class



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

  '
  if opening
  '
  static cloudBob cl 
  '
  BuildSky
  '
  '
  'CREATE TORUS
  =============
  '
  static ShapeObject boa
  boa.init
  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/18, mt2=0.999/6
  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
      @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>6 then st2=-st2 : ct2=5 'reversal
    if ct2<0 then st2=-st2 : ct2=1
  next 'j
  @rm=strptr(srm) 'reset 37*37*8 floats
  boa.ClearVertex
  int e=37*37*8
  boa.putVertex rm,e '8 floats per set of vertex attributes
  srm="" : srn=""

  '
  static string sel
  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=""


  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,-5
  scalef 5,5,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 RenderObject rt
  rt.Setup
  'print rt.Check
  rt.RenderToTexture
  'print rt.texturebuf
  'rt.RenderToScreen
  'rt.release


 endif 'opening
  '
  if closing
    glDeleteTextures 1,SkyTexture
    ReleaseShaderProg ps[skyboxi]
    boa.release
    bob.release
    exit sub
  endif



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

  if ShaderError then exit sub

  RenderSky 'ang1


  ang3+=0.5


  ShaderProgram shaderMaj.ph


  'SET UNIFORM VARIABLES:
 

  'TORUS RENDER
  =============

  int ortho=0


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

  loadidentity
  rotatef 10, 1,0,0    'tilt
  translatef 0,-5,-10         'set distance
  if ortho
    scalef 0.2,0.2,0.2      'correction for ortho scale
  endif
  savematrix modelView

  a=0.4  'ambient
  d=0.6  '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.2,0.6,0.7, -70.0}  'FOG COLOR AND DIST LIMIT

  'GENERATE TRANSFORMS

  '
  picklabel 999
  '
  loadidentity
  translatef 0,0,-5
  rotatef 20, 1,0,0    'tilt
  rotatef ang3,0,1,0   'Y SPIN
  boa.clearmatrix
  boa.putmatrix mats,16
  
  loadidentity
  translatef 3,6,-12
  rotatef 30, 1,0,0    'tilt
  rotatef ang3*0.5,0,1,0   'Y SPIN
  boa.putmatrix mats,16

  loadidentity
  translatef -3,6,-30
  rotatef 40, 1,0,0    'tilt
  rotatef ang3*0.5,0,1,0   'Y SPIN
  boa.putmatrix mats,16

  boa.transfermatrix 'COMMIT MATRIX ARRAY


  'glBindFramebuffer(GL_FRAMEBUFFER, 0)

  boa.render 'all panels gpu side

 

  cl.render 10 'max 100


  rt.RenderToScreen

  NewFrame

  ortho=0
  SetProjectionMatrix projection, 45, aspect, 1, 1e4, ortho
  loadidentity
  translatef 0,0,-10         'set distance
  savematrix modelView

  ShaderMatrix4 ShaderMaj.m0, 1, GL_FALSE, projection  'PROJECTION
  ShaderMatrix4 ShaderMaj.m1, 1, GL_FALSE, modelView   'MODELVIEW
  ShaderTexture shaderMaj.t0, 0, rt.texture 't0 FOR COLOR TEXTURE
  ShaderVector4 ShaderMaj.v0, 1, float {0.85, 0.85, 0.9, 1.0}     'TINT
  ShaderVector4 ShaderMaj.v4, 1, float {0.0,0.0,0.0, -10000.0}  'FOG COLOR AND DIST LIMIT
  bob.clearmatrix
  loadidentity
  rotatef ang1,0,0,1
  scalef 3,3,3
  translatef 0,0,0
  bob.putmatrix mats,16
  bob.transfermatrix
  bob.render 'StaticBackground

  rt.RenderToTexture

  ShaderProgram 0



  '
  'UPDATE ROTATION ANGLES
  '----------------------
  ang1+=0.1
  'if ang1>360 then ang1-=360
  '
  end sub

  MainWindow width,height,WS_OVERLAPPEDWINDOW
