`(kakko ,man)

Find a guide into tomorrow by taking lessons from the past

cl-glut pre-study <page 2>

聴講2日目はここからスタート。

レッスン3.線を引く
lesson3.lisp

;;
;; lesson1 <Open the window>
;; lesson2 <Fill in the blue>
;; lesson3 <Draw the lines>
;; -----------------------------------------------------------------------------------------------
;; Set window class
(defclass Window (glut:window) 
  ()
  (:default-initargs
   :mode '(:rgba)))

;; Init
(defmethod glut:display-window :before ((window Window))
  (gl:clear-color 0 0 1 1))                  ; set blue

;; Display
(defmethod glut:display ((window Window))
  (gl:clear :color-buffer-bit)               ; clear buffer
  ; draw a square
  (gl:begin :line-loop)
    (%gl:vertex-2d -0.9 -0.9)
    (%gl:vertex-2d 0.9 -0.9)
    (%gl:vertex-2d 0.9 0.9)
    (%gl:vertex-2d -0.9 0.9)
  (gl:end)
  (gl:flush))                                ; show window

;; Main
(defun lesson3 ()
  (glut:display-window (make-instance 'Window)))  ; create window

(lesson3)

f:id:tomekame0126:20151031214339p:plain
まぁ、想定通り。色を指定しなければ、デフォルトでは白ってことね。
画像を見てわかると思うけど、(%gl:vertex-2d X Y)のXとYは1~0の範囲で指定できる。
※1だと、ウィンドウの枠ぎりぎりに表示され、0だと表示されない。


お次は?
レッスン4.線に色をつける
lesson4.lisp

;;
;; lesson1 <Open the window>
;; lesson2 <Fill in the blue>
;; lesson3 <Draw the lines>
;; lesson4 <Put the color lines>
;; -----------------------------------------------------------------------------------------------
;; Set window class
(defclass Window (glut:window) 
  ()
  (:default-initargs
   :mode '(:rgba)))

;; Init
(defmethod glut:display-window :before ((window Window))
  (gl:clear-color 0 0 1 1))                  ; set blue

;; Display
(defmethod glut:display ((window Window))
  (gl:clear :color-buffer-bit)               ; clear buffer
  ; set red color 
  (%gl:color-3d 1 0 0)
  ; draw a square
  (gl:begin :line-loop)
    (%gl:vertex-2d -0.9 -0.9)
    (%gl:vertex-2d 0.9 -0.9)
    (%gl:vertex-2d 0.9 0.9)
    (%gl:vertex-2d -0.9 0.9)
  (gl:end)
  (gl:flush))                                ; show window

;; Main
(defun lesson4 ()
  (glut:display-window (make-instance 'Window)))  ; create window

(lesson4)

f:id:tomekame0126:20151031214845p:plain
これも想定通り。

そして、そして。
レッスン5.図形を塗りつぶす
lesson5.lisp

;;
;; lesson1 <Open the window>
;; lesson2 <Fill in the blue>
;; lesson3 <Draw the lines>
;; lesson4 <Put the color lines>
;; lesson5 <Set polygon and several colors>
;; -----------------------------------------------------------------------------------------------
;; Set window class
(defclass Window (glut:window) 
  ()
  (:default-initargs
   :mode '(:rgba)))

;; Init
(defmethod glut:display-window :before ((window Window))
  (gl:clear-color 1 1 1 1))                  ; set white

;; Display
(defmethod glut:display ((window Window))
  (gl:clear :color-buffer-bit)               ; clear buffer
  ; draw a square
  (gl:begin :polygon)
    ; set red color 
    (%gl:color-3d 1 0 0)
    (%gl:vertex-2d -0.9 -0.9)
    ; set green color 
    (%gl:color-3d 0 1 0)
    (%gl:vertex-2d 0.9 -0.9)
    ; set blue color 
    (%gl:color-3d 0 0 1)
    (%gl:vertex-2d 0.9 0.9)
    ; set yellow color 
    (%gl:color-3d 1 1 0)
    (%gl:vertex-2d -0.9 0.9)
  (gl:end)
  (gl:flush))                                ; show window

;; Main
(defun lesson5 ()
  (glut:display-window (make-instance 'Window)))  ; create window

(lesson5)

f:id:tomekame0126:20151031215658p:plain
これはこれは、色どり鮮やかなことで!
てなとこで、今日はここまで。

H27.11.3修正
cl-openglのbindings-packageを使用するバージョンに変更した。
(gl:xxxx) ⇒ (%gl:xxxx)

ついでに書くと、このpackageはこんな感じで定義されている。

(defpackage #:cl-opengl-bindings
(:nicknames #:%gl) <-------------------- ここね!
(:use #:common-lisp #:cffi)
 ・・・・・・・・・・・・・・・