SaltAndPepper

From MHCGraphics

Jump to: navigation, search
; Mead example file -- adding lots of salt coming out of
; a salt shaker, but now with pepper too!
;
; Jim Teresco
; Computer Science 110
; Mount Holyoke College
;
; $Id: SaltAndPepper.scm 591 2008-03-29 16:49:38Z terescoj $
;
(require (lib "Defs.ss" "Mead"))

; This function computes a random value between low
; and high (low is possible, high is not).
; The (random) call returns a random number in [0,1).
(define (rand low high)
  (+ low (* (random) (- high low))))

; define an individual salt crystal, a 5x5x5 cube
(object saltCrystal Cube
        (material whitePlaster)
        (scale .05 .05 .05)
        )

; define an individual pepper flake, a 5x5x1 cube
(object pepperFlake Cube
        (material dkGrayPlaster)
        (scale .01 .05 .05)
        )

; put in a red floor for some reason
(object floor Plane)

; the salt "pours in" from above from a circular source, 
; leading to a cylindrical area filled with salt crystals.

; to achieve this, we will place grains of salt in random
; orientations and at random positions within this cylindrical
; region, which we define as 300 in height, and 100x100 above the
; xz-plane
(define saltStreamHeight 300)
(define saltStreamRadius 50)

; our function to add the salt grains
(define (addSalt grain n group)
  (if (<= n 0) group  ; if we're done adding grains, just
                      ; return the group
      (begin          ; more to do, everything inside the
                      ; begin will happen when n>0
        (tell group
              (add grain   ; add a new grain
                   (compose   ; and transform it appropriately
                    (xRot (rand 0 360))
                    (yRot (rand 0 360))
                    (zRot (rand 0 360))  ; any orientation
                    (translate
                     (rand 0 saltStreamRadius) ; move along x
                     (rand 0 saltStreamHeight) ; move up
                     0)  ; no z - this is taken care of below
                    (yRot (rand 0 360)) ; rotate about y
                    )
                   )
              )
        (addSalt grain (- n 1) group) ; add remaining grains
        )
      )
  )
; we define an empty Group, to which we will add our grains
; of salt
(object saltGrains Group)

; add our grains using the function defined above
(addSalt saltCrystal 125 saltGrains)

(object pepperFlakes Group)
; and some pepper too
(addSalt pepperFlake 125 pepperFlakes)

(object scene Group)
(tell scene
      (add floor redPlaster)
      (add saltGrains)
      (add pepperFlakes)
      (add (new Light) (translate 0 300 -500))
      (add (new Light) (translate 200 300 -500))
      (add (new Light) (translate -200 300 -500))
)

(tell camera
      (pos '(0 300 -500))
      (coi '(0 100 0))
      (shoot))
Personal tools