##################################################
# normals.slf - two versions of a cube to compare 
#               shared vs. unshared vertices vs.
#               unshared vertices where the point
#               normals have been specified to 
#               mimic the shared version
#

tclinit { 

  toplevel .slfWindow.ui

  ##############################
  # Shading UI

  set shading $SLF_FLAT

  frame .slfWindow.ui.shading -relief raised -borderwidth 2

  label .slfWindow.ui.shading.title -text Shading -relief ridge -borderwidth 2
  radiobutton .slfWindow.ui.shading.flat -text Flat -variable shading \
    -value $SLF_FLAT -anchor w
  radiobutton .slfWindow.ui.shading.gouraud -text Gouraud -variable shading \
    -value $SLF_GOURAUD -anchor w

  pack .slfWindow.ui.shading.title .slfWindow.ui.shading.flat \
    .slfWindow.ui.shading.gouraud -side top -fill x

  ##############################
  # Shared UI

  set shared 0

  frame .slfWindow.ui.shared -relief raised -borderwidth 2

  label .slfWindow.ui.shared.title -text Vertices -relief ridge -borderwidth 2
  radiobutton .slfWindow.ui.shared.shared -text Shared -variable shared \
    -value 0 -anchor w
  radiobutton .slfWindow.ui.shared.unshared -text Unshared -variable shared \
    -value 1 -anchor w
  radiobutton .slfWindow.ui.shared.normals -text Nomrals -variable shared \
    -value 2 -anchor w

  pack .slfWindow.ui.shared.title .slfWindow.ui.shared.shared \
    .slfWindow.ui.shared.unshared .slfWindow.ui.shared.normals -side top -fill x

  pack .slfWindow.ui.shading .slfWindow.ui.shared -side left -fill y
}


surface sBluePlastic
  color ( 0 0 1 )
  reflectivity ( 1 1 1 )
  exponent 2
  metallic 0
endsurface

##################################################
# Cube with shared vertices
#

point pXYZ (  1  1  1 ) endpoint
point pxYZ ( -1  1  1 ) endpoint
point pXyZ (  1 -1  1 ) endpoint
point pxyZ ( -1 -1  1 ) endpoint
point pXYz (  1  1 -1 ) endpoint
point pxYz ( -1  1 -1 ) endpoint
point pXyz (  1 -1 -1 ) endpoint
point pxyz ( -1 -1 -1 ) endpoint

face fX ( pXYZ pXyZ pXyz pXYz ) 
endface
face fx ( pxYZ pxYz pxyz pxyZ ) 
endface
face fY ( pXYZ pXYz pxYz pxYZ ) 
endface
face fy ( pXyZ pxyZ pxyz pXyz ) 
endface
face fZ ( pXYZ pxYZ pxyZ pXyZ ) 
endface
face fz ( pXYz pXyz pxyz pxYz ) 
endface

object oCubeShared ( fX fx fY fy fZ fz )
endobject

##################################################
# Cube with unshared vertices
#

point pSquareXY (  1  1 0 ) endpoint
point pSquarexY ( -1  1 0 ) endpoint
point pSquareXy (  1 -1 0 ) endpoint
point pSquarexy ( -1 -1 0 ) endpoint

face fSquare ( pSquareXY pSquarexY pSquarexy pSquareXy ) 
endface

object oSquare ( fSquare )
endobject

group gCubeUnshared
  # Equivalent to fX
  instance oSquare
    translate ( 0 0 1 )
    rotate ( 0 1 0 ) ( 90 )
  endinstance
  # Equivalent to fx
  instance oSquare
    translate ( 0 0 1 )
    rotate ( 0 1 0 ) ( -90 )
  endinstance
  # Equivalent to fY
  instance oSquare
    translate ( 0 0 1 )
    rotate ( 1 0 0 ) ( -90 )
  endinstance
  # Equivalent to fy
  instance oSquare
    translate ( 0 0 1 )
    rotate ( 1 0 0 ) ( 90 )
  endinstance
  # Equivalent to fZ
  instance oSquare
    translate ( 0 0 1 )
  endinstance
  # Equivalent to fz
  instance oSquare
    translate ( 0 0 1 )
    rotate ( 0 1 0 ) ( 180 )
  endinstance
endgroup

##################################################
# Cube with unshared vertices and point normals
#

point pNormalsXY ( 1  1  0) 
  normal ( 1  1  1)
endpoint
point pNormalsxY (-1  1  0)
  normal (-1  1  1)
endpoint
point pNormalsXy ( 1 -1  0)
  normal ( 1 -1  1)
endpoint
point pNormalsxy (-1 -1  0)
  normal (-1 -1  1)
endpoint

face fNormals ( pNormalsXY pNormalsxY pNormalsxy pNormalsXy ) 
endface

object oNormals ( fNormals )
endobject

group gCubeNormals
  # Equivalent to fX
  instance oNormals
    translate ( 0 0 1 )
    rotate ( 0 1 0 ) ( 90 )
  endinstance
  # Equivalent to fx
  instance oNormals
    translate ( 0 0 1 )
    rotate ( 0 1 0 ) ( -90 )
  endinstance
  # Equivalent to fY
  instance oNormals
    translate ( 0 0 1 )
    rotate ( 1 0 0 ) ( -90 )
  endinstance
  # Equivalent to fy
  instance oNormals
    translate ( 0 0 1 )
    rotate ( 1 0 0 ) ( 90 )
  endinstance
  # Equivalent to fZ
  instance oNormals
    translate ( 0 0 1 )
  endinstance
  # Equivalent to fz
  instance oNormals
    translate ( 0 0 1 )
    rotate ( 0 1 0 ) ( 180 )
  endinstance
endgroup

##################################################
# Set up view
#

group gCube
  shading { expr $shading }
  surface sBluePlastic

  instance oCubeShared
    lod { if { $shared == 0 } { expr $SLF_FULL } else { expr $SLF_OFF } }
  endinstance
  instance gCubeUnshared
    lod { if { $shared == 1 } { expr $SLF_FULL } else { expr $SLF_OFF } }
  endinstance
  instance gCubeNormals
    lod { if { $shared == 2 } { expr $SLF_FULL } else { expr $SLF_OFF } }
  endinstance
endgroup

group gRoot
  instance gCube
    rotate ( 0 1 0 ) ( -15 )
    rotate ( 1 0 0 ) ( 20 )
    scale ( 0.10 0.10 0.10 )
  endinstance
endgroup

group gCam
  instance cam
    id iCam
    translate ( 0 0 1 )
  endinstance
endgroup

camera cam
  projection SLF_PERSPECTIVE
  frustum ( -0.5 -0.5 -2 ) ( 0.5 0.5 -0.2 )
endcamera

light lAmbient
  type SLF_AMBIENT
  color ( 0.2 0.2 0.2 )
endlight

light lTop
  type SLF_POINT
  color ( 1.0 1.0 1.0 )
endlight

group gLight
  instance lTop
    id iTop
    lookat
      eye ( 0 0 0 )
      target ( 0 -1 -1 )
      up ( 0 1 0 )
    endlookat
    translate ( 0 1 1 )
  endinstance
endgroup

render Viewport gCam.iCam.cam gRoot
  light lAmbient
  light gLight.iTop.lTop
endrender

viewport Viewport Window
endviewport

window Window
endwindow