Blending (Redbook Ch6)

Greg Bennett gwbennett at sentex.ca
Sat Nov 16 18:49:48 UTC 2013


;; I am trying to learn to use cl-opengl by translating some of those
;; examples from the redbook which are not in the distributed code
;; under ../examples/redbook.

;; In particular, I should like to understand some of Ch6 of the Redbook
;; dealing with blending and antialiasing. I began with blending.

;; While I seem to have pieces working, the code below, and its author,
;; are obviously missing one or more essential ideas. I would be happy
;; to learn what they are, so that I can discover how I might had found
;; them out without bothering the list about possibly trivial matters.

;; (lisp-implementation-version) returns
;; Version 1.9-r15972M (linuxX8664) for ccl.
;; I use 2-pane emacs with C-c C-c to move forms from source
;; buffer to *slime-repl ccl* buffer
;; all under Linux Mint 14 (64 bit).

;; Thanks for any and all assistance
;; Cheers /Greg Bennett

;;+ Code and comments on its execution follow.

;; This is blender.lisp
;; An attempt to adapt the code in the
;; OpenGL Programming Guide P230/1
;; to cl-opengl
;; borrowing from redbook/examples/lines.lisp for basic windowing code,
;; and with helpful hints from Bart Botta about how to convert the C
;; code to opengl's forms.

;; Comparing the Guide with the examples files, I infer that
;; the contents of void inint(void) go to (defmethod
(glut:display-window ()))
;; while void display(void) -> (defmethod (glut:display( )))

;; *Typically executing the forms below down to and including
;; (defun (gb-blend( ..))
;; and then running (gb-blend) produces a solid yellow
;; window. The x in its top right does indeed kill it.

;; Adding the (defmethod keyboard ...) form and running
;; (gb-blend) produces that yellow window, the keys a,s,r,m,x
;; have no effect on it at all.

;; The x in its top right kills it. Esc kills it too and
;; leads to this in the message
;; window of what was the repl:
;+ Lisp connection closed unexpectedly: connection broken
;+ by remote peer

;+ alt-x slime asks whether I want to start another lisp
;+ answer no -> (presumably) the lisp with which contact
;+ had been severed. This repl knows about nothing which
;+ preceded its interruption.

;; I said Typically, because just ONCE the code without
;; the keyboard method produced a yellow pane with a white
;; square in its centre.

;; Adding the keyboard method to this -> that same pair
;; which reacted to the keys as described on P230 in respect
;; of the white square:
;; a no change
;; s white -> blue
;; r blue -> yellow
;; m yellow -> black
;; x black -> white
;; Esc graceful fold.

;; Having closed the window,
;; immediately re-doing (gb-blend) -> that same collection
;; of responses to keys.

;; Closing lisp, redoing the whole exercise from
;; the same, unchanged, source file -> only the
;; solid yellow pane, unchanged by key presses, save Esc which
;; produces the train wreck described earlier.

;; A package
(defpackage :zot
  (:use :cl))

;; Use it
(in-package :zot)

;; Class for windows in which blending actions should occur
(defclass blender-window (glut:window)
  ()
  (:default-initargs
   :width 400 :height 400 :pos-x 50 :pos-y 50
   :mode '(:single :rgb) :title "Blender Window"))

;; Set up some attributes
;; Described as in void init(void) on P230
(defmethod glut:display-window ((w blender-window))
  (gl:clear-color 1.0 1.0 0.0 0.0)
  (gl:blend-func :one :one)
  (gl:enable :blend)
)

;; We seem to need this reshape method
;; in order that anything show on the screen
(defmethod glut:reshape ((w blender-window) width height)
  (gl:viewport 0 0 width height)
  (gl:matrix-mode :projection)
  (gl:load-identity)
  (glu:ortho-2d 0 width 0 height))


;; A method on display to get what seems
;; to be a rectangle to be modified by blending actions
;; code in void display(void) on P230
(defmethod glut:display ((w blender-window))
  (gl:clear :color-buffer-bit)
  (gl:color 0.0 0.0 1.0)
  (gl:rect -0.5 -0.5 0.5 0.5)
  (gl:flush))


(defun gb-blend ()
  (glut:display-window (make-instance 'blender-window)))

;; If I have translated correctly, that's all I need.
;; Let's make a blender-window

;; There is it in the top left of the screen
;; correct title, solid yellow background.
;; (? Should there have been a yellow square in the middle ?)
;; On to blending through the keyboard.


;; The keyboard method to drive the blender options
;; in the OpenGL P G P231
(defmethod glut:keyboard ((w blender-window) key x y)
  (declare (ignore x y))
  (case key
    ((#\a) (gl:blend-equation :func-add))
    ((#\s) (gl:blend-equation :func-subtract))
    ((#\r) (gl:blend-equation :func-reverse-subtract))
    ((#\m) (gl:blend-equation :min))
    ((#\x) (gl:blend-equation :max))
    ((#\Esc) (glut:destroy-current-window))
    )
  (glut:post-redisplay)
)

;; Assuming correct translation, run (gb-blend) and press 'a'
;; The story is at the top of the file.

 






More information about the cl-opengl-devel mailing list