Patrick (patrickwonders) wrote,
Patrick
patrickwonders

  • Mood:

Some really beautiful code...

My previous post understandably prompted a request or two for what I believe are good examples of the type of code that I'm looking for. Here is one piece of code that I had in mind. This is a Lisp version of an example program that was originally written in C. The C version is quite good. The Lisp version is nearly perfect, in my opinion.

;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
;;; planet.lisp --- Lisp version of planet.c (Red Book examples)
;;;
;;; Original C version contains the following copyright notice:
;;;   Copyright (c) 1993-1997, Silicon Graphics, Inc.
;;;   ALL RIGHTS RESERVED

;;; This program shows how to composite modeling transformations
;;; to draw translated and rotated models.
;;; Interaction:  pressing the d and y keys (day and year)
;;; alters the rotation of the planet around the sun.

(in-package #:cl-glut-examples)

(defclass planet-window (glut:window)
  ((year :accessor year :initform 0)
   (day :accessor day :initform 0))
  (:default-initargs
   :pos-x 100 :pos-y 100 :width 500 :height 500
   :mode '(:double :rgb) :title "planet.lisp"))

(defmethod glut:display-window :before ((w planet-window))
  (gl:clear-color 0 0 0 0)
  (gl:shade-model :flat))

(defmethod glut:display ((w planet-window))
  (gl:clear :color-buffer)
  (gl:color 1 1 1)
  (gl:with-pushed-matrix
    ;; draw sun
    (glut:wire-sphere 1 20 16)
    ;; draw smaller planet
    (gl:rotate (year w) 0 1 0)
    (gl:translate 2 0 0)
    (gl:rotate (day w) 0 1 0)
    (glut:wire-sphere 0.2 10 8))
  (glut:swap-buffers))

(defmethod glut:reshape ((w planet-window) width height)
  (gl:viewport 0 0 width height)
  (gl:matrix-mode :projection)
  (gl:load-identity)
  (glu:perspective 60 (/ width height) 1 20)
  (gl:matrix-mode :modelview)
  (gl:load-identity)
  (glu:look-at 0 0 5 0 0 0 0 1 0))

(defmethod glut:keyboard ((w planet-window) key x y)
  (declare (ignore x y))
  (flet ((update (slot n)
           (setf (slot-value w slot) (mod (+ (slot-value w slot) n) 360))
           (glut:post-redisplay)))
    (case key
      (#\d (update 'day 10))
      (#\D (update 'day -10))
      (#\y (update 'year 5))
      (#\Y (update 'year -5))
      (#\Esc (glut:destroy-current-window)))))

(defun rb-planet ()
  (glut:display-window (make-instance 'planet-window)))

This code still has some issues (all issues shared with the C version).

  • In this code, years and days are entirely independent.
  • It is not obvious from this code what the arguments to (glut:wire-sphere ...) mean.
  • It is not obvious what the arguments to (glu:look-at ...) mean.

Almost everything else is completely transparent to one who knows enough Lisp. Even without knowing Lisp, (flet ...) is the only thing that may not be readily obvious from the formatting and naming conventions.

From the cl-opengl package:

Copyright (c) 2004, Oliver Markovic <entrox@entrox.org> 
  All rights reserved. 

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

 o Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer. 
 o Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution. 
 o Neither the name of the author nor the names of the contributors may be
   used to endorse or promote products derived from this software without
   specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

From the original C code:

Copyright (c) 1993-1997, Silicon Graphics, Inc.
ALL RIGHTS RESERVED 

Permission to use, copy, modify, and distribute this software for any
purpose and without fee is hereby granted, provided that the above
copyright notice appear in all copies and that both the copyright
notice and this permission notice appear in supporting documentation,
and that the name of Silicon Graphics, Inc. not be used in advertising
or publicity pertaining to distribution of the software without
specific, written prior permission.

THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" AND
WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, INCLUDING
WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A
PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON GRAPHICS, INC.  BE
LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT, SPECIAL, INCIDENTAL,
INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
WHATSOEVER, INCLUDING WITHOUT LIMITATION, LOSS OF PROFIT, LOSS OF USE,
SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD PARTIES, WHETHER OR NOT
SILICON GRAPHICS, INC.  HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
LOSS, HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR
IN CONNECTION WITH THE POSSESSION, USE OR PERFORMANCE OF THIS
SOFTWARE.

US Government Users Restricted Rights

Use, duplication, or disclosure by the Government is subject to
restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
(c)(1)(ii) of the Rights in Technical Data and Computer Software
clause at DFARS 252.227-7013 and/or in similar or successor clauses in
the FAR or the DOD or NASA FAR Supplement.  Unpublished-- rights
reserved under the copyright laws of the United States.
Contractor/manufacturer is Silicon Graphics, Inc., 2011 N.  Shoreline
Blvd., Mountain View, CA 94039-7311.  OpenGL(R) is a registered
trademark of Silicon Graphics, Inc.
Tags: code book
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

  • 1 comment