  $ title "Cubic Rock Reflection"
  $ FileName "t.exe"
 'uses RTL64
  % ExplicitMain
  % MultiSamples 4
  uses OpenglSceneFrame
  uses MinMidi
  uses glo2\shapes
  uses glo2\materials
  uses glo2\particles

  indexbase 1
  sys   GdiplusToken
  float ang1
  float ma[16]
  float li[16]
  int   cube,sphere,helix,tors,csface
  sys hmo 'midi
  Cloud cloud1
  Smoke smoke1
  plant fern


  sub MinTextureA(int n,f,h,b)
  ============================
  static long v[16]
  v<=
  b,b,b,b,
  b,f,h,b,
  b,h,h,b,
  b,b,b,b
  MakeTexture @v, 4, 4, n
  End sub


  sub InitMidi()
  ================
  int er
  er=midiOutOpen(hmo, 0, 0, 0, CALLBACK_NULL) '0=synth a 2=soft synth
  midiOutShortMsg(hmo, 0xC0+0x0D00)           'Xylophone Bells assign
  end sub

  function ding(int v,n)
  ======================
  if v>127 then v=127
  if v<10 then v=10
  'midiOutShortMsg(hmo, v*0x10000+n*0x100+0x89) 'percussion off
  midiOutShortMsg(hmo, v*0x10000+n*0x100+0x99) 'percussion on
  end function


  sub initialize(sys hWnd)
  ========================
  '
  InitMidi
  GDIplus 1
  '
  'PREPARE TEXTURES
  '
  glGenTextures 16, texn
  '
  '
  seed=0x12345678
  MinTexture  texn[1],0xffffffff,0x80ffffff,0x00ffffff 'whitecloud
  MinTexture  texn[2],0xffffffff,0x80ffffff,0x00000000 'smoke
  MinTextureA texn[3],0x80000000,0x40000000,0x00000000 'soft light shadow
  MinTextureA texn[4],0xc0000000,0x60000000,0x00000000 'soft medium shadow
  MinTextureA texn[5],0xff000000,0x80000000,0x00000000 'soft intense shadow
  '
  static int res=256,texspace
  texspace=res*res*4
  string imgs[3]
  imgs[1]=nuls texspace
  imgs[2]=imgs[1]
  imgs[3]=imgs[1]
  'LoadPixImage "../images/crate.jpg",res,imgs[1]
  '
  'pixbase,width,height,stride,colorval,offset
  --------------------------------------------
  AddNoiseC strptr imgs[1],res,res,32,060,060,100,255,0x53535353,1 
  AddNoiseC strptr imgs[1],res,res,16,060,060,100, 0,0x35353535 
  AddNoiseC strptr imgs[1],res,res,08,070,070,050, 0,0x53535353 
  AddNoiseC strptr imgs[1],res,res,02,010,010,005, 0,0x53535353 
  MakeTexture strptr imgs[1],res,res,texn[10]
  '
  AddNoiseC strptr imgs[2],res,res,08,100,100,000,255,0x35353535,1
  AddNoiseC strptr imgs[2],res,res,02,100,100,000, 0,0x35353535 
  AddNoiseC strptr imgs[2],res,res,01,050,050,000, 0,0x35353535 
  MakeTexture strptr imgs[2],res,res,texn[11]
  '
 'AddNoiseC strptr imgs[3],res,res,32,060,060,100,128,0x53535353,1 'const alpha 
  AddNoiseC strptr imgs[3],res,res,32,060,060,100,255,0x53535353
  AddNoiseC strptr imgs[3],res,res,16,060,060,100,   0,0x35353535 
  AddNoiseC strptr imgs[3],res,res,08,070,070,050,   0,0x53535353 
  AddNoiseC strptr imgs[3],res,res,02,010,010,005,   0,0x53535353 
  MakeTexture strptr imgs[3],res,res,texn[12]
  '
  sys p
  p=fern.new res,32000 'pixel map
  MakeTexture p,res,res,texn[13]
  fern.free

  cube=CompileList   : CubeForm      : glEndList
  sphere=CompileList : Spheric 1,1,6 : glEndList
  tors=CompileList   : torus 1.,.20  : glEndList
  '
  'standard lighting
  ------------------
  li<=
  0.0, 8.0, 8.0, 1.0,  'position / w=0 parallel or w=1 for directional
  1.0, 1.0, 1.0, 1.0,  'ambient
  1.0, 1.0, 1.0, 1.0,  'diffuse 
  1.0, 1.0, 1.0, 1.0   'specular

  Lighting li
  
  '
  SetTimer hWnd,1,10,null
  end sub

  sub Release(sys hwnd)
  '====================
  glDeleteLists    cube,1 
  glDeleteLists    sphere,1 
  glDeleteLists    helix,1 
  glDeleteLists    tors,1 
  glDeleteLists    csface,1 
  glDeleteTextures 16, texn
  Gdiplus 0
  killTimer hwnd, 1
  MidiOutClose hmo
  end sub

  sub RenderPlane(int tex, float u,v)
  ===================================
  glBindTexture GL_TEXTURE_2D,tex
  glBegin GL_QUADS
  glNormal3f 0.,1.,0.
  glTexCoord2f 0.0,0.0 : glVertex3f -1.0,0,-1.0
  glTexCoord2f u  ,0.0 : glVertex3f  1.0,0,-1.0
  glTexCoord2f u  ,v   : glVertex3f  1.0,0, 1.0
  glTexCoord2f 0.0,v   : glVertex3f -1.0,0, 1.0    
  glend
  end sub


  sub scene(sys hWnd)
  ===================
  '
  static single ra,ri,angi1=.4
  '
  ActiveFrame
  glClearColor 0.5, 0.5, 0.7, 0
  '
  float fcolor[4]={.5, .5, .7, .99}
  glfogi GL_FOG_MODE,GL_EXP
  glfogf GL_FOG_DENSITY,0.020
  glfogfv GL_FOG_COLOR,  fcolor
  glfogf GL_FOG_INDEX,  0
  glfogf GL_FOG_START, -1.0
  glfogf GL_FOG_END,   -100.0
  glEnable GL_FOG
  '
  glPolygonMode GL_FRONT_AND_BACK, GL_FILL
  glEnable GL_NORMALIZE
  '
  static int t1,t2,t3
  t1=texn[10] : t2=texn[11] : t3=texn[12]
  '
  'MYSTERIOUS RED PLANET
  glPushMatrix
  glTranslatef -20.,10.,-95.0
  Material Red
  glScalef      6.,6.,6.
  glEnable GL_LIGHTING
  glCallList sphere
  'glDisable GL_LIGHTING
  glPopMatrix
  '
  'OBJECTS REFLECTED IN FLOOR
  ---------------------------
  '
  li[2]=-li[2] : lighting li 'y position
  '
  'CONE REFLECTION
  glPushMatrix
  Material Silver
  glTranslatef -3.0,-2.,-8.0
  glScalef    .5,-.5,.5
  glBindTexture GL_TEXTURE_2D,t1
  ConeFaces 16,0.,2.,0.
  glPopMatrix
  '
  'CYLINDER REFLECTION
  glPushMatrix
  Material Bronze
  glTranslatef -4.,-2.,-8.0
  glScalef    .25,-.5,.25
  ConeFaces 16,1.,8.,1.
  glPopMatrix
  '
  'RING REFLECTION
  Material Gold
  glPushMatrix
  glTranslatef 3.0,-2.23,-7.0
  glScalef      .75,-1.00,.75
  glCallList tors
  glPopMatrix
  '
  'METALLIC ORBS REFLECTION
  glPushMatrix
  glTranslatef 0.5,-2.25,-8.0
  glScalef      .25,-.25,.25
  Material Silver
  glCallList sphere
  glPopMatrix
  '
  glPushMatrix
  glTranslatef 1.0,-2.25,-9.0
  glScalef      .25,-.25,.25
  Material Steel
  glCallList sphere
  glPopMatrix
  '
  '
  'PLINTH REFLECTION
  glEnable GL_TEXTURE_2D
  Material White
  glPushMatrix
  glTranslatef -1.0,-2.5,-6.0
  glrotatef   30.0,0,1,0
  glScalef    .5,-.5,.5
  glBindTexture GL_TEXTURE_2D,t1
  glCallList cube
  glPopMatrix
  li[2]=-li[2] : lighting li 'y position
  '
  'FLOOR TILES
  glPushMatrix
  glEnable GL_TEXTURE_2D
  glTranslatef -8.0,-1.99,-5.0
  glScalef     .25,1.,.25
  glPushMatrix
  int i,j,a
  macro rtile1
    Material White
    RenderPlane  t3,1.,1.
    glTranslatef 2.,0.,0.
  end macro
  macro rtile2
    Material White    
    RenderPlane  t3,1.,1.
    glTranslatef 2.,0.,0.
  end macro
  for j=1 to 16
    for i=1 to 16
      if a
        rtile1
        rtile2
      else
        rtile2
        rtile1
      end if
    next
    glTranslatef -64.0,0.,-2.
    a=1-a 'alternate
  next
  glPopMatrix
  glDisable GL_TEXTURE_2D
  glPopMatrix
  Material White
  '
  'GREEN PLANE
  glPushMatrix
  glEnable GL_TEXTURE_2D
  glTranslatef 0.0,-2.0,-29.0
  glScalef     16.,1.,16.
  RenderPlane  t2,8.,8.
  glDisable GL_TEXTURE_2D
  glPopMatrix
  '
  'SOFT SHADOWS
  glEnable GL_TEXTURE_2D
  glPushMatrix
  glTranslatef -1.2,-1.98,-5.8 'for plinth
  glScalef     2.,1.,1.5
  RenderPlane  texn[4],1.,1.
  glPopMatrix
  '
  glPushMatrix
  glTranslatef -3.5,-1.98,-7.5 'for cylinder
  glScalef     1.,1.,1.
  RenderPlane  texn[3],1.,1.
  glPopMatrix
  '
  glPushMatrix
  glTranslatef 2.5,-1.98,-5.5 'for sphere
  glScalef     .7,.7,.7
  RenderPlane  texn[3],1.,1.
  glPopMatrix
  'glDisable GL_TEXTURE_2D
  '
  glPushMatrix
  glTranslatef -0.,-1.98,-10. 'for metal balls
  glScalef     .7,.7,.7
  RenderPlane  texn[3],1.,1.
  glPopMatrix
  glDisable GL_TEXTURE_2D
  '
  '
  'RING
  Material Gold
  glPushMatrix
  glTranslatef 3.0,-1.77,-7.0
  glScalef      .75,1.00,.75
  glCallList tors
  glPopMatrix

  'METALLIC ORBS
  glPushMatrix
  glTranslatef 0.5,-1.75,-8.0
  glScalef      .25,.25,.25
  Material Silver
  glCallList sphere
  glPopMatrix
  '
  glPushMatrix
  glTranslatef 1.0,-1.75,-9.0
  glScalef      .25,.25,.25
  Material Steel
  glCallList sphere
  glPopMatrix
  '
  'GLOBE
  if not csface
    csface=CompileList
    CubicFace 8,.3,0 '-8,8
    glEndList
  end if
  glPushMatrix
  glTranslatef -1.0,0.,-6.0
  glRotatef ang1,0,1,0
  'Material RedShiny
  Material white
  glEnable GL_TEXTURE_2D
  glBindTexture GL_TEXTURE_2D,t1
  glCallList csface
  glRotatef 90.0,0,1,0
    glCallList csface
  glRotatef 90.0,0,1,0
  glCallList csface
  glRotatef 90.0,0,1,0
  glCallList csface
  glRotatef 90.0,1,0,0
  glCallList csface
  glRotatef 180.0,1,0,0 'top
  glCallList csface
  glPopMatrix
  '
  'BACKGROUND MOUND
  glPushMatrix
  glTranslatef 7.0,-10.5,-20.0
  'glRotatef ang1,0,1,0
  glRotatef -90.,1,0,0
  glScalef    10.,10.,10.
  glBindTexture GL_TEXTURE_2D,t2
  'glCallList csface
  CubicFace 8,.15, 0
  glPopMatrix
  glDisable GL_TEXTURE_2D
  '
  'CONE
  glPushMatrix
  Material Silver
  glTranslatef -3.,-2.,-8.0
  glScalef    .5,.5,.5
  glBindTexture GL_TEXTURE_2D,t1
  ConeFaces 16,0.,2.,0.
  glPopMatrix
  '
  'CYLINDER
  glPushMatrix
  Material Bronze
  glTranslatef -4.,-2.,-8.0
  glScalef    .25,.5,.25
  ConeFaces 16,1.,8.,1.
  glPopMatrix
  '
  'LIGHT SPHERE
  float e[8]={1.,1.,.5,.1}
  glMaterialfv GL_FRONT, GL_EMISSION, e
  glPushMatrix
  glTranslatef -4.0,2.5,-8.0
  glScalef      .5,.5,.5
  Material Steel
  glCallList sphere
  glPopMatrix
  glMaterialfv GL_FRONT, GL_EMISSION, e[5] 'emission off
  
  '
  'CYLINDER (SMOKER)
  glPushMatrix
  Material Steel
  gltranslatef  2, -2.,-30.
  glScalef      .125,.5,.125
  ConeFaces 16,1.,2.,1.
  glPopMatrix
  '
  '
  glEnable GL_TEXTURE_2D
  Material White
  '
  'PLINTH
  glPushMatrix
  glTranslatef -1.0,-1.5,-6.0
  glrotatef   30.0,0,1,0
  glScalef    .5,.5,.5
  glBindTexture GL_TEXTURE_2D,t1
  glCallList cube
  glPopMatrix

  glDisable GL_TEXTURE_2D
  glDisable GL_LIGHTING
  '
  'CLOUD
  glEnable GL_TEXTURE_2D
  glPushMatrix
  gltranslatef -10.0, -10.0, -90.0
  glscalef      20.0,12.0,1.0
  cloud1.render 30 'max 50
  glPopMatrix
  glDisable GL_TEXTURE_2D
  '
  'SMOKE
  glDisable GL_LIGHTING
  glEnable GL_TEXTURE_2D
  glBindTexture GL_TEXTURE_2D,texn[2]
  glPushMatrix
  gltranslatef  2, -.9, -30.
  glScalef      2.,2.5,1.0
  smoke1.r<=    .45,.45,.55
  smoke1.render 25 'max 50
  'cloud1.render  15
  glPopMatrix
  glDisable GL_TEXTURE_2D
  'glEnable GL_LIGHTING
  '
/*
  'PLANTS
  glBindTexture GL_TEXTURE_2D,texn(13)
  glEnable GL_TEXTURE_2D
  '
  glPushMatrix
  glTranslatef 1.,-1.,-3.0
  glScalef    .9,.9,1.
  glColor4f   .0,.1,.0,1.
  quadtex     1,1,0
  glPopMatrix
  '
  glPushMatrix
  glTranslatef .7,-1.,-2.99
  glscalef     .9,.9,1.
  glColor4f    .0,.2,.0,1.
  glRotatef    10.,0,0,1
  quadtex      1.,1.,0
  glPopMatrix
  '
  glPushMatrix
  glTranslatef .5,-1.,-2.98
  glscalef     .9,.9,1.
  glColor4f    .0,.3,.0,1.
  'quadtex      1.,1.,0
  glPopMatrix
  '
*/
  glDisable GL_TEXTURE_2D
  '
  ang1+=angi1
  if ang1>360 then ang1-=360
  end sub

  MainWindow width,height,WS_OVERLAPPEDWINDOW
