# BezPatch.slf
# Demo file on how to generate points procedurally in tclinit
# CHS 2000/10/3
##############################################################

#################### 
# SURFACES
#################### 
surface sBlue
  color (0.2 0.3 0.9)
endsurface

#######################################################
# Procedural Generation of Control Points for a Surface
#######################################################

tclinit {

  proc CreateBezPatch { uobez vobez subs } {

    set pts ""
    for {set j 0} {$j < $vobez} {incr j} {

      for {set i 0} {$i < $uobez} {incr i} {

        # define an individual name for each control point
        set name pt[subst $i]_[subst $j]

        # concatenate all points into a list of all control points
        set pts [concat $pts "$name"]

        # Put your own expressions here to place the control points
        # where you need them.
        set x [expr $i -3]
        set y [expr  2 -$j]
        set z [expr cos(0.9*$i*$j)]
        # As is, this is just a simple modulated height field ...

        puts "$name"
        eval "slide create point $name {$x $y $z}"
     }
   }
   puts "$pts"

   set uslices [expr $subs*$uobez]
   set vslices [expr $subs*$vobez]
    
   slide create bezierpatch bezpatch -uorder $uobez -vorder $vobez  -controlpointlist $pts -uslices $uslices -vslices $vslices -drawcontrols  1 -solid SLF_HOLLOW
  }

   # Call the patch-building procedure with u- & v-order and subdiv.steps
   CreateBezPatch 7 5  2
}

####################
# World
####################

group gWorld
  instance bezpatch
    surface sBlue
  endinstance
endgroup

group gScene
  instance gWorld
    scale (0.2 0.2 0.2)
    rotate (1 0 0) (-20)
  endinstance
endgroup


#################### 
# CAMERA
#################### 

camera cam
  projection SLF_PARALLEL
  frustum ( -1 -1 -100) (1 1 100)
endcamera

group gCamera
  instance cam
    id instCam
    translate ( 0.0 0.0 1.0 )
(*
    lookat
      eye ( 0.0 0.0 1.0 )
      target ( 0.0 0.0 0.0 )
      up ( 0.0 1.0 0.0 )
    endlookat
*)
  endinstance
endgroup

#################### 
# LIGHT
#################### 

light lite
  type SLF_DIRECTIONAL
#  type SLF_POINT
endlight

group gLight
  instance lite
    id instLite
#    translate ( 0.0 1.0 1.0 )

    lookat
      eye ( 1.0 1.0 1.0 )
      target ( 0.0 0.0 0.0 )
      up ( 0.0 1.0 0.0 )
    endlookat

  endinstance
endgroup

light lite2
  type SLF_AMBIENT
  color (0.5 0.5 0.5)
endlight

group gLight2
  instance lite2
    id instLite2
  endinstance
endgroup

####################
# RENDER
####################

window WINDOW
endwindow

viewport VIEWPORT WINDOW
  origin ( 0.0 0.0 )
  size ( 1.0 1.0 )
endviewport

render VIEWPORT gCamera.instCam.cam gScene
  light gLight.instLite.lite
  light gLight2.instLite2.lite2
endrender