Lenses

From MHCGraphics

Jump to: navigation, search
; Mead example file -- lenses and a magnifying glass
;
; Jim Teresco
; Computer Science 110
; Mount Holyoke College
; February 27, 2008
;
; $Id: Lenses.scm 525 2008-03-03 21:07:43Z terescoj $
;
(require (lib "Defs.ss" "Mead"))

; remove the default light, and add some extra lights
(object scene Group)
(object lighting Group
      (add bulb (translate 500 500 -500))
      (add bulb (translate 500 -500 -500))
      (add bulb (translate -500 500 -500))
      (add bulb (translate -500 -500 -500))
      (add bulb (translate 500 500 500))
      (add bulb (translate 500 -500 500))
      (add bulb (translate -500 500 500))
      (add bulb (translate -500 -500 500))
      (add bulb (translate 0 -500 0))
      )

; define a glass material for our lenses
(object clearGlassMat Material
        (type 'glass)
        (color white)
        )

; We are going to create a convex lens by intersecting two
; standard spheres -- define the amount of overlap that
; we want at the thickest point and use this to determine
; how much we translate the spheres before intersecting.
; Try different values and see how the scene changes, but that
; no other code modifications are needed!  The size of the
; translations and the frame below are all computed based
; on this constant definition!
(define maxLensThickness 50.0)

; The convex lens is the simple intersection of two spheres.
; We want to leave maxLensThickness overlap, so the "front"
; sphere should go back maxLensThickness/2 behind the xy-plane
; and the "back" sphere should come forward that amount
; in front of the xy-plane.
; Also if the lens at its thickest point is 50, its
; radius is (sqrt (- (* 50 50) (* 25 25))) = 43.30
(object convexLens Intersection
        (add sphere 
             (translate 0 0 (- 50 (/ maxLensThickness 2))))
        (add sphere
             (translate 0 0 (- (/ maxLensThickness 2) 50)))
        )

(define lensRadius 
  (sqrt (- (* 50 50) 
           (* (- 50 (/ maxLensThickness 2))
              (- 50 (/ maxLensThickness 2))
              )
           )
        )
  )

(define frameThickness 5.0)

; to make a magnifying glass, we'll want a round frame in which
; the lens sits
(object frame Lathe
        (profile 
         (closeList 
          (polyXform
           (2to3d '((0 0) (0 1) (1 1) (1 0)))
           (compose
            (scale frameThickness frameThickness 1)
            (translate lensRadius (- (/ frameThickness 2)) 0)
            )
           )
          )
         )
        )

; paste these together into the magnifying glass object,
; with the lens in the xy-plane centered at the origin,
; and the handle extending below the xz-plane
(object magnifyingGlass Group
        (add convexLens clearGlassMat
             (scale 1 1 .5)
             )
        (add frame 
             (xRot 90)
             )
        ; handle
        (add cylinder 
             (compose
              (scale .01 .01 .01) ; make it 1x1x1 for now
              (scale frameThickness (* 2 lensRadius)
                     frameThickness)
              (translate 0 (- (* 2 lensRadius)) 0)
              )
             )
        )

; a concave lens
(object concaveLens Difference
        (add cylinder
             (compose
              (xRot 90)
              (scale .01 .01 .01) ; 1x1x1
              (scale (* lensRadius 2)
                     (* lensRadius 2)
                     (* lensRadius 2))
              )
             )
        (add sphere
             (translate 0 0 -57.5))
        (add sphere
             (translate 0 0 57.5))
       )

; now a "miniaturizing" glass
(object miniaturizingGlass Group
        (add concaveLens clearGlassMat
             (scale 1 1 .5)
             )
        (add frame 
             (xRot 90)
             )
        ; handle
        (add cylinder 
             (compose
              (scale .01 .01 .01) ; make it 1x1x1 for now
              (scale frameThickness (* 2 lensRadius)
                     frameThickness)
              (translate 0 (- (* 2 lensRadius)) 0)
              )
             )
        )
(object floor Plane
        (material whitePlaster))

; build a simple scene that we can look at
(tell scene
      (add lighting)
      (add floor)
      (add cube redPlaster
           (compose
            (scale .5 .5 .5)
            (translate -50 25 0)
            )
           )
      (add cube greenPlastic
           (compose
            (scale .5 .5 .5)
            (translate 50 25 0)
            )
           )
      (add cone bluePlaster
           (compose
            (scale .5 .5 .5)
            (translate 0 25 0)
            )
           )
      (add sphere yellowPlastic
           (compose 
            (scale .05 .05 .05)
            (translate 0 2.5 -27.5)
            )
           )
      (add sphere yellowPlastic
           (compose 
            (scale .05 .05 .05)
            (translate 0 2.5 27.5)
            )
           )
      )


(tell image
      (background black)
      (dimensions 1024 768)
      )

; shoot a picture before adding the magnifying glass
(tell camera 
      (pos '(0 250 -500))
      (shoot))

; add the magnifying glass and miniaturizing glass to the scene
(tell scene
      (add magnifyingGlass blackPlastic
           (translate 0 0 -100)
           (xRot 30))
      (add miniaturizingGlass blackPlastic
           (translate 0 0 100)
           (xRot -30))
     )

; take another picture, through the magnifying glass
(tell camera 
      (pos '(0 250 -500))
      (shoot))

; and another through the miniaturizing glass
(tell camera 
      (pos '(0 250 500))
      (shoot))
Personal tools