  #compact
  $ FileName  "co.exe"
  'uses rtl64
  'uses console
  '
  % ExplicitMain
  % title        "Texture Synth Control"
  % fontA        "Arial",FW_SEMIBOLD

  'macro keydown
  'case 27 : 'no action
  'case 32 : 'no action
  'end macro
  '
  uses OpenglSceneFrame
 'uses glo2\shapes
 'uses glo2\materials
  uses glo2\textures
  uses ControlPanels

  indexbase 1
  float     ang1
  int       cube,pent


  sub initialize(sys hWnd)
  ========================
  '
  GDIplus 1
  BuildTextures 16,256
  BeginGlCompile cube : CubeForm : EndGlCompile
  end sub


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


  sub QuadMirrorTex(float bx,bz,sx,sz)
  ====================================
  float cx,cy,cz
  glBegin GL_TRIANGLE_STRIP
  glNormal3f 0.0,1.0,0.0
  cx=bx+sx : cz=bz+sz
  glTexCoord2f 0,0 : glVertex3f bx,0,bz
  glTexCoord2f 1,0 : glVertex3f cx,0,bz
  glTexCoord2f 0,1 : glVertex3f bx,0,cz
  glTexCoord2f 1,1 : glVertex3f cx,0,cz
  glEnd
  glBegin GL_TRIANGLE_STRIP
  glNormal3f 0.0,1.0,0.0
  cx=bx-sx : cz=bz+sz  
  glTexCoord2f 1,0 : glVertex3f cx,0,bz
  glTexCoord2f 0,0 : glVertex3f bx,0,bz
  glTexCoord2f 1,1 : glVertex3f cx,0,cz
  glTexCoord2f 0,1 : glVertex3f bx,0,cz
  glEnd
  glBegin GL_TRIANGLE_STRIP
  glNormal3f 0.0,1.0,0.0
  cx=bx-sx : cz=bz-sz
  glTexCoord2f 1,1 : glVertex3f cx,0,cz
  glTexCoord2f 0,1 : glVertex3f bx,0,cz
  glTexCoord2f 1,0 : glVertex3f cx,0,bz
  glTexCoord2f 0,0 : glVertex3f bx,0,bz
  glEnd
  cx=bx+sx : cz=bz-sz
  glBegin GL_TRIANGLE_STRIP
  glNormal3f 0.0,1.0,0.0
  glTexCoord2f 0,1 : glVertex3f bx,0,cz
  glTexCoord2f 1,1 : glVertex3f cx,0,cz
  glTexCoord2f 0,0 : glVertex3f bx,0,bz
  glTexCoord2f 1,0 : glVertex3f cx,0,bz
  glend
  end sub


  sub RenderPlane(sys eu,ev)
  ==========================
  float h,sx,sy,sz,bx,by,bz
  h=.5
  int xx,zz
  for zz=1 to ev
    bz=zz/ev
    sz=h/ev
    for xx=1 to eu
      bx=xx/eu
      sx=h/eu
      QuadMirrorTex(bx,bz,sx,sz)
    next
  next  
  end sub

  sub RenderCube(float x,y,z,sc,int tex)
  ======================================
  glBindTexture GL_TEXTURE_2D,tex
  glPushMatrix
  gltranslatef x,y,z
  glrotatef    ang1,0,1,0
  glscalef sc,sc,sc
  go cube
  glPopMatrix
  end sub

  function Gauss2D(single x,single z) as single
  '============================================
  'static single e=2.718281828 'Euler's number
  'single g=-.5*(x*x+z*z)
  'return pow(e,g)
  return exp(-hypot(x,z)*.5)-.5
  end function


  sub DrawNoisyGauss()
  '===================
  '
  static as float e,g,x,y,z,nx,ny,nz,sr,st,tt,r,texx,texz,texs,htn,ght
  static as sys   en,i,j,k,v,p1,p2
  '
  def res 256
  def sc   1
  en=res     'number of quads along each axis
  sr=5       'size of surface for x and z axes
  htn=0.250  'height noise overall scale
  texs=0.15  'texture map step
  ght=1.0    'gauss height factor
  st=sr/en   'x and z step
  tt=st*.25  'x and z jitter
  '
  static sys strd
  if not strd
    'CREATE INTERPOLATED MAP
    strd = 8
    sys ofs  = 0x35357575
    static float fip(res*res+res)
    'terrain octaves
    AddNoisef @fip,res,res,8,ofs, .100
    AddNoisef @fip,res,res,4,ofs, .400
    AddNoisef @fip,res,res,2,ofs, .400
    AddNoisef @fip,res,res,1,ofs, .050
  end if
  '
  int mg=8
  z=sr*.5
  texz=.1
  'glDisable GL_NORMALIZE
  glDisable GL_LIGHTING
  glDisable GL_TEXTURE_2D
  for i = 1 to en 'Z STRIPS
    x=-sr*.5
    texx=.1
    glBegin GL_QUAD_STRIP
    for j = 1 to en 'X PAIRS
      'glBegin GL_QUAD_STRIP
      'for k=0 to 1
        k=0
        p2=i*en+j+k
        p1=p2-en
        '
        'y=noise(p1)*htn
        y=fip(p1)*htn
        '
        nx = 0 : ny = y : nz = 0
        y+=Gauss2D(x,z)*ght
        '
        glNormal3f nx, ny, nz
        'glNormal3f ny, ny*8, nz*4
        ny=ny*mg+1.
        glColor3f ny,ny,ny
        '
        glTexCoord2f texx,texz
        'glVertex3f x+noise(p1)*tt, y, z+noise(p1+1)*tt
        glVertex3f x+fip(p1)*tt, y, z+fip(p1+1)*tt
        '
        'y=noise(p2)*htn
        y=fip(p2)*htn
        '
        nx = 0 : ny = y : nz = 0
        y+=Gauss2D(x,z-st)*ght
        '
        glNormal3f nx, ny, nz
        'glNormal3f nx, ny*8, ny*4
        ny=ny*mg+1.
        glColor3f ny,ny,ny
        '
        glTexCoord2f texx,texz+texs
        'glVertex3f x+noise(p2)*tt, y, z-st+noise(p2+1)*tt
        glVertex3f x+fip(p2)*tt, y, z-st+fip(p2+1)*tt
        '
        x+=st
        texx+=texs
      'next 'K QUAD
      'glEnd
      'x-=st
      'texx-=texs
      if texx>=1 then texx-=1
    next 'X PAIRS
    glEnd 'END QUAD STRIP
    z-=st
    texz+=texs
    if texz>=1 then texz-=1
  next 'Z STRIPS
  'glEnable GL_NORMALIZE
  glEnable GL_LIGHTING
  glEnable GL_TEXTURE_2D
  '
  end sub

  function addRGBA(sys q,m,*img,res,ControlPanelC *cb)
  ====================================================
  indexbase 1
  static sys r,g,b,a
  static string imgs
  if not imgs then imgs = nuls res*res*4
  img = strptr imgs
  sys   n=q*5   'knob row offset
  sys   st=1<<q 'stride
  float sc      'scale
  if cb.l[q+1].lit
    'read row of rgba knobs
    sc=cb.k[n+5].va*255
    r=cb.k[++n].va*sc
    g=cb.k[++n].va*sc
    b=cb.k[++n].va*sc
    a=cb.k[++n].va*sc
    if m=1 then a=255
    AddNoiseC   img,res,res,st,r,g,b,a,0x35353535,m 
  elseif m=1 'clear pixels first
    r=0 : g=0 : b=0 : a=255
    AddNoiseC   img,res,res,st,r,g,b,a,0x35353535,m 
  end if
  end function
  '


  ===================
  sub scene(sys hWnd)
  ===================
  '
  indexbase 1
  static single ra,ri,angi1=.5
  '
  DozyFrame
  'ActiveFrame
  glClearColor 0.45, 0.5, 0.8, 0
  Fog          .50, .50, .75, 0.035
  '
  BeginPick
  '
  glColor4f 1,1,1,1
  StandardLighting li
  StandardMaterial ma
  '
  'MAIN SCENE
  '
  
  '
  static ControlPanelC cb
  if closing
    cb.destructor
    exit sub
  endif
  '
  if cb.sc = .0
    cb.x   =-.3
    cb.y   =-.0
    cb.sc  = .750
    cb.lock= 0
    int i
    for i=4 to 40 step 5: cb.k[i].vx=-1.0 : next
  end if
  '
  def res 256
  static int t1=texn[11]
  static sys img
  '
  'glDeleteTextures 1,t1
  '
 if cb.l[9].lit
   if bleft then
     sys i,j,k,m
     for i=0 to 35 step 5
       for j=1 to 3
         k=i+j
         m=40+j
         cb.k[k].set cb.k[m].va 'GANG RGB
       next
     next
   end if
 end if
 sys i
  addRGBA 0,1,img,res,cb
  for i=1 to 7
    addRGBA i,0,img,res,cb
  next
  '
  static int t1=texn[11]
  MakeTexture img,res,res,t1
  '
  'MAIN SCENE
  '
  '
  PickLabel 0
  if not pick then
    '
    'PLANE / DRAW TERRAIN
    glEnable      GL_TEXTURE_2D
    glBindTexture GL_TEXTURE_2D,t1
    glPushMatrix
    glTranslatef  -32,-2.0,-32.0
    glScalef      64.,1.,64.
    RenderPlane   16,16 'nxn textures
    glPopMatrix
    '
    glPushMatrix
    glTranslatef  0.0,-2.0,-64.0
    glScalef      8.,4.,8.
    DrawNoisyGauss
    'RenderCube 1.0,-2.0,-5.0,1.0,t1 'x y x scale tex
    glPopMatrix
  end if
  '
  '
  glDisable     GL_TEXTURE_2D
  '
  '
  'CONTROL PANEL
  scope
    static float my,mm
    '
    'PANEL POSITION
    '
    'if mposy>crect.bottom-15
    '  if my=0 then mm=.01 : actn=100
    'elseif mposy<crect.bottom>>1
    '  if my>0 then mm=-.01 : actn=100
    'end if
    'if mm
    '  my+=mm
    '  if my>.414 then my=.414 : mm=0 : actn=0
    '  if my<0 then my=0 : mm=0 : actn=0
    'end if
    '
    'BUILD
    '
    my=.414
    'if my>.1
      glpushmatrix
      gltranslatef .0,.0,-1.0
      cb.act 000 'optional base id: 0000 1000 2000 3000 etc
      glpopmatrix
'gltranslatef -.1,0.-0.18,0.0004
'glscalef     .05,.05,.01
'glcolor3f 1,1,1
'gprint str (picked)

    'end if
  end scope
  '
  EndPick
  '
  '
  'ang1+=angi1
  'if ang1>360 then ang1-=360
  ang1=30
  '
  end sub
  '
  MainWindow width,height,WS_OVERLAPPEDWINDOW
