  % Title "Sphere Mapping Demo"
  % Animated
  % ScaleUp
 '% PlaceCentral
 '% AnchorCentral
  uses ConsoleG

  function SphereMap(sys nPrecision)
  ==================================
  'Mike / Sphere mapping
  float ex,ey,ez,theta1,theta2,theta3
  float M_TWOPI = pi()*2
  float M_PI_2  = pi()/2
  sys   i,j,ei
  ei=(nprecision-1)\2
  For i = 0 To ei
    theta1 = i * M_TWOPI / nPrecision - M_PI_2
    theta2 = (i + 1) * M_TWOPI / nPrecision - M_PI_2
    glBegin(GL_TRIANGLE_STRIP)
    For j = 0 To nPrecision
      theta3 = j * M_TWOPI / nPrecision
      ' Splices
      ex = cos(theta2) * cos(theta3) ' calculate Euler angles on 3 axes
      ey = sin(theta2)
      ez = cos(theta2) * sin(theta3)
      glNormal3f(ex, ey, ez) ' set up normals
      glTexCoord2f(-j / nPrecision, 2 * (i + 1) / nPrecision) ' set up UVs
      glVertex3f(1.5 * ex, 1.5 * ey, 1.5 * ez) ' render vertices
      ' Sides
      ex = cos(theta1) * cos(theta3)
      ey = sin(theta1)
      ez = cos(theta1) * sin(theta3)
      glNormal3f(ex, ey, ez)
      glTexCoord2f(-j / nPrecision, 2 * i / nPrecision)
      glVertex3f(1.5 * ex, 1.5 * ey, 1.5 * ez)
    Next
    glEnd()
  Next
  end function


  sub main()
  ==========
  static int   imgn,res,wi,ht,earth
  static int   defmat,m
  static float angx,angy
  static float ma[13] 'for material
  float f1,f2,f3
  if not defmat
    defmat=1
    ma[5]={.10,.10,.10,1.} 'ambient
    ma[13]=.1 'shininess (phong)
  end if
  if not imgn
    imgn=21
    res=0 'use image wi and ht (512*256)
    LoadTexture "..\..\images\Earth.jpg",imgn,res,wi,ht
    earth=CompileList : SphereMap 128 : glEndlist
  end if
  cls 0,0,0
  'display image
  '
  pushstate
  move -2,3,-4.0
  shading
  int i,j
  for j=1 to 8
    pushstate
    move 0,-4*j
    m=1
    for i=1 to 8
      pushstate
      move 4*i
      scale 1.2
      rotateY angY
      f1=.9*j/8
      f2=.9-f1
      f3=.5*m
      m+=m 'log scale 1 2 4 8 16 .. 256
      ma[1]={f1,f1,f1,1} 'diffuse
      ma[9]={f2,f2,f2,1} 'specular
      ma[13]=f3
      material ma
      texture imgn
      go earth
      texture 0
      popstate
    next
    popstate
  next
  popstate
  angx+=.4 : if angx>=360 then angx-=360
  angy+=.5 : if angy>=360 then angy-=360
  end sub 'main

  EndScript