SpiceMix

From MHCGraphics

Jump to: navigation, search
; Mead example file -- based on the salt shaker, but with
; a bunch of types of spices all mixed together
;
; Jim Teresco
; Computer Science 110
; Mount Holyoke College
;
; $Id: SpiceMix.scm 592 2008-03-29 21:13:52Z 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))))

; A function to get the nth entry of a list
(define (first l) (car l))
(define (rest l) (cdr l))
(define (nth list n)
  (if (<= n 1) (first list)
      (nth (rest list) (- n 1))))

; define an individual salt crystal, a 3x3x3 cube
(object saltCrystal Cube
        (material whitePlaster)
        (scale .03 .03 .03)
        )

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

; define an individual garlic powder grain, a 2x2x2 cube
; along with an appropriate material
(object garlicMat Material
        (type 'plaster)
        (color '(.4 .5 0))
        )
(object garlicGrain Cube
        (material garlicMat)
        (scale .02 .02 .02)
        )

; define a parsley flake, a little extruded polygon
(define parsleyShape
  '((-2 -2) (0 -3) (2 1) (3 3) (1 2) (-2 1)))
(define parsleyFlake
  (extrude (2to3d (closeList parsleyShape))
           (translate 0 0 2)))

; and finally, paprika, which is also a little cube, 2x2x2
(object paprikaMat Material
        (type 'plaster)
        (color '(.3 .1 0))
        )

(object paprikaGrain Cube
  (material paprikaMat)
  (scale .02 .02 .02))

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

; our spice mix "pours in" from above from a circular source, 
; leading to a cylindrical area filled with grains and crystals
; of our spices.

; to achieve this, we will place spices 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 streamHeight 300)
(define streamRadius 50)

; our function to add the spice particles
(define (addSpices grainList 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
              ; first, we want to add a new spice item,
              ; selected randomly from the list of grains
              ; provided
              (add (nth grainList 
                        (+ 1 (random (length grainList))))
                   (compose   ; and transform it appropriately
                    (xRot (rand 0 360))
                    (yRot (rand 0 360))
                    (zRot (rand 0 360))  ; any orientation
                    (translate
                     (rand 0 streamRadius) ; move along x
                     (rand 0 streamHeight) ; move up
                     0)  ; no z - this is taken care of below
                    (yRot (rand 0 360)) ; rotate about y
                    )
                   )
              )
        (addSpices grainList (- n 1) group) ; add remaining
        )
      )
  )
; we define an empty Group, to which we will add our grains
; of salt
(object spiceGrains Group)

; add our grains using the function defined above
(addSpices 
  (list 
   saltCrystal pepperFlake garlicGrain
   parsleyFlake paprikaGrain)
  300 spiceGrains)

(object scene Group)
(tell scene
      (add floor ltGrayPlaster)
      (add spiceGrains)
      (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