  #compact
  '
  % ExplicitMain
  % title        "Visualising Surface Normals"
  % fontA        "Arial",FW_SEMIBOLD

  'macro keydown
  'case 27 : 'no action
  'case 32 : 'no action
  'end macro
  '
  % filename "t.exe"
  'uses rtl64
  uses OpenglSceneFrame
  uses glo2\shapes
  uses glo2\textures
  uses glo2\materials
  uses glo2\particles
  'uses console

  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 RenderPlane(float su,sv, eu,ev)
  ===================================
  eu+=su : ev+=sv
  glBegin GL_QUADS
  glNormal3f 0.0,1.0,0.0
  glTexCoord2f su,sv : glVertex3f -1.0,0,-1.0
  glTexCoord2f eu,sv : glVertex3f  1.0,0,-1.0
  glTexCoord2f eu,ev : glVertex3f  1.0,0, 1.0
  glTexCoord2f su,ev : glVertex3f -1.0,0, 1.0    
  glend
  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
  =============================================
  float f=-(x*x+z*z)
  return exp(f)
  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 int   en,i,j,k,v,p1,p2
  static as int terrain
  '
  def res 128
  def sc   1
  static int strd
  if not strd
    en=res     'number of quads along each axis
    sr=4       'size of surface for x and z axes
    htn=0.250  'height noise overall scale
    texs=.050  'texture map step
    ght=1.0    'gauss height factor
    st=sr/en   'x and z step
    tt=st*.125 'x and z jitter
    '
    'CREATE INTERPOLATED MAP
    strd = 8
    int ofs  = 0x35357575
    static float fip(res*res+res)
   'terrain octaves
   AddNoisef @fip,res,res,16,ofs, .400
   AddNoisef @fip,res,res, 8,ofs, .200
   'AddNoisef @fip,res,res, 4,ofs, .100
   'AddNoisef @fip,res,res, 2,ofs, .050
   'AddNoisef @fip,res,res, 1,ofs, .025
  end if
  '
  'BeginGlCompile Terrain
  '
  static vector v[res*res+res] 'vertex array
  static vector n[res*res+res] 'normal array
  '
  'GENERATE VERTICES
  '
  z=sr*.5
  for i=1 to en 'Z
    x=-sr*.5
    for j=1 to en 'X
      p2=i*en+j
      p1=p2-en
      y=fip(p1)*htn
      nx=x+noise(p1)*tt
      nz=z+noise(p2)*tt
      ny=Gauss2D(nx,nz)*ght
      v[p1]={nx,ny+y,nz}
      'n[p1]={nx,ny*.5+y*2.0,nz} 'accentuate chaos
      x+=st
    next 'X
    z-=st
  next 'Z
  '
  'GENERATE SMOOTH NORMALS
  '
  for i=1 to en 'Z
    for j=1 to en 'X
      k=i*en-res+j
      if i>1 and j>1 and i<en and j<en
        SurfaceNormal n[k],v[k],v[k+1],v[k+en]
      else
        n[k]={ 0.0, 1.0, 0.0 }
      end if
    next 'X
  next 'Z
  '
  'RENDER
  '
  texz=0.0
  for i=1 to <en 'Z
    texx=0.
    glBegin GL_QUAD_STRIP
    for j=1 to en 'X
      p2=i*en+j
      p1=p2-en
      'PAIRS OF VERTICES
      glTexCoord2f texx,texz
      glNormal3f n[p1].x,n[p1].y,n[p1].z
      glVertex3f v[p1].x,v[p1].y,v[p1].z
      '
      glTexCoord2f texx,texz+texs
      glNormal3f n[p2].x,n[p2].y,n[p2].z
      glVertex3f v[p2].x,v[p2].y,v[p2].z
      '
      texx+=texs
      'if texx>=1 then texx-=1
    next 'X
    glEnd
    texz+=texs
    'if texz>=1 then texz-=1
  next 'Z STRIPS
  '
  glDisable GL_LIGHTING
  glDisable GL_TEXTURE_2D
  glColor4f .99,.99,.99,.99
  float f=100
  'int p1,p2
  for i=2 to <en 'z
    texx=0.
    glBegin GL_LINES
    for j=2 to <en 'X
      p2=i*en+j
      p1=p2-en
      'PAIRS OF VERTICES
      glVertex3f v[p1].x,v[p1].y,v[p1].z
      glVertex3f v[p1].x+n[p1].x*f,v[p1].y+n[p1].y*f,v[p1].z+n[p1].z*f
    next 'X
    glEnd
  next 'Z STRIPS
  '
  'EndGlCompile
  'go Terrain
  end sub



  ===================
  sub scene(sys hWnd)
  ===================
  '
  indexbase 1
  static single ra,ri,angi1=.2
  '
  'DozyFrame
  ActiveFrame
  glClearColor 0.45, 0.5, 0.8, 0
  'Fog          .50, .50, .75, 0.025
  '
  glColor4f 1,1,1,1
  StandardLighting li
  StandardMaterial ma
  '
  'MAIN SCENE
  '
  glEnable      GL_TEXTURE_2D
  glBindTexture GL_TEXTURE_2D,texn[11]
  '
  glPushMatrix
  glTranslatef  0.0,-4.0,-20.0
  glScalef      5.,5.,5.
  glRotatef ang1,0,1,0
  'glRotatef ang1,1,0,0
  DrawNoisyGauss
  glPopMatrix
  '
  'RenderCube 1.0,-2.0,-5.0,1.0,texn[11] 'x y x scale tex
  '
  glDisable     GL_TEXTURE_2D
  '
  '
  ang1+=angi1
  if ang1>360 then ang1-=360
  'sleep 12
  '
  end sub
  '
  MainWindow width,height,WS_OVERLAPPEDWINDOW
