RandomColorMarbles

From MHCGraphics

Jump to: navigation, search
; Mead example file -- a circle of randomly colored 
; marble objects, demonstrating the creation of
; random materials
;
; Jim Teresco
; Computer Science 110
; Mount Holyoke College
;
; randomMatAdd function from Duane A. Bailey
;
; $Id: RandomColorMarbles.scm 601 2008-04-01 16:02:12Z terescoj $
;
(require (lib "Defs.ss" "Mead"))

; These functions add multiple objects, each with a randomly
; colored plastic material
(define (rand low high)
   (+ low (* (random) (- high low)))
  )

(define (randomMatAdd n obj group initialXform deltaXform)
  (if (<= n 0) group
      ; the let* is like a begin but it lets us define
      ; names (like we have done with define) but names
      ; that exist only within the let*.  We'll use this
      ; to create a Material and a random color.
      (let* ([m (new Material)]
             ; to avoid too many dark and light colors
             ; and to avoid the grays completely, we'll
             ; generate a color as an HSV with a random
             ; hue, but saturation and value both of 1.
             [c (hsv2rgb (list (rand 0 360) 1 1))]
             )
        ; now we can give our material the properties we want
        (tell m
              (color c)
              (type 'plastic)
              )
        ; add one instance of our object to the group, but
        ; also specify our brand new material
        (tell group (add obj initialXform m))
        ; now add the other n-1 copies
        (randomMatAdd (- n 1) obj group
                      (compose initialXform deltaXform)
                      deltaXform)
        )
      )
  )

; we'll use this to place a bunch of marbles in a circle
; about the y-axis

; our marble is just a 50x50x50 sphere, that by default
; sits atop the origin, no material specified yet
(object marble Sphere
        (scale .5 .5 .5)
        (translate 0 25 0)
        )

; define names for the number or marbles and how far
; around we'll need to rotate each to get a nice circle of
; marbles
(define numMarbles 24)
(define angleSpacing (/ 360 numMarbles))
(define layoutRadius 300)
  
(randomMatAdd numMarbles marble scene
              ; our first marble is translated out
              ; by some amount along x
              (translate layoutRadius 0 0)
              ; subsequent marbles are each rotated around
              ; the y-axis by an appropriate amount
              (yRot angleSpacing)
              )

; put in a floor and a few lights
(object dimBulb Light
        (intensity .25)
        )
(tell scene
      (add dimBulb (translate 500 800 500))
      (add dimBulb (translate -500 800 500))
      (add dimBulb (translate 500 800 -500))
      (add dimBulb (translate -500 800 -500))
      (add (new Plane) whitePlastic))

(tell image
      (background white)
      (antiAlias 1)
      (dimensions 800 600)
      (quality 11)
      )

(tell camera
      (pos '(0 300 -600))
      (angle 60)
      (shoot))
Personal tools