2009/04/21

光源の移動

もちろん光源も移動するよね。移動の方法はモデルと同じなんだね。

緑色の光にしてみた。

『OpenGLプログラミングガイド 第2版』の movelight.c を cl-opengl で。

(eval-when (:compile-toplevel :load-toplevel :execute)
(require :cl-opengl)
(require :cl-glu)
(require :cl-glut))

(defclass move-light-window (glut:window)
((spin :initform 0))
(:default-initargs :title "Move Light"
:mode '(:single :rgb :depth)
:width 500 :height 500 :pos-x 300 :pos-y 300))

(defmethod glut:keyboard ((window move-light-window) key x y)
(declare (ignore x y))
(case key
(#\q (glut:destroy-current-window))))

(defmethod glut:display-window :before ((window move-light-window))
(gl:clear-color 0 0 0 0)
(%gl:shade-model :smooth)
(gl:enable :lighting :light0 :depth-test))

(defmethod glut:display ((window move-light-window))
(gl:clear :color-buffer-bit :depth-buffer-bit)
(gl:with-pushed-matrix
(glu:look-at 0 0 5
0 0 0
0 1 0)
(gl:with-pushed-matrix
(%gl:rotate-d (slot-value window 'spin) 1 0 0) ; x を軸に回転
(gl:light :light0 :position '(0 0 1.5 1))
(gl:light :light0 :diffuse '(0 1 0 1)) ; 緑色の光
(%gl:translate-d 0 0 1.5)
(gl:disable :lighting) ; wire-cube のとき光は関係ない
(gl:color 0 1 1)
(glut:wire-cube 0.1)
(gl:enable :lighting)) ; solid-torus のとき光が関係する
(glut:solid-torus 0.275 0.85 8 15))
(%gl:flush))

(defmethod glut:reshape ((window move-light-window) w h)
(%gl:viewport 0 0 w h)
(%gl:matrix-mode :projection)
(%gl:load-identity)
(glu:perspective 40 (/ w h) 1 20)
(%gl:matrix-mode :modelview)
(%gl:load-identity))

(defmethod glut:mouse ((window move-light-window) button state x y)
(with-slots (spin) window
(case button
(:left-button
(when (eq state :down)
(setf spin (mod (+ spin 30) 360)) ; 左クリックする度に30度光を移動する。
(glut:post-redisplay))))))

;;(glut:display-window (make-instance 'move-light-window))

0 件のコメント: