`(kakko ,man)

Find a guide into tomorrow by taking lessons from the past

cl-glut pre-study <page 3>

さて3日目。
レッスン6.座標軸とビューポート

;;
;; lesson1 <Open the window>
;; lesson2 <Fill in the blue>
;; lesson3 <Draw the lines>
;; lesson4 <Put the color lines>
;; lesson5 <Set polygon and several colors>
;; lesson6 <Reshape >
;; -----------------------------------------------------------------------------------------------
;; 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

;; View port
(defmethod glut:reshape ((window Window) width height)
  (gl:viewport 0 0 width height)
  (gl:load-identity)
  (gl:ortho (- (/ width 200)) (/ width 200) (- (/ height 200)) (/ height 200) -1 1))

;; 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 lesson6 ()
  (glut:display-window (make-instance 'Window)))  ; create window

(lesson6)

f:id:tomekame0126:20151101153538p:plain
レッスン5との違いは、中に表示されている色どり鮮やかなスクエアが、ウィンドウ全体を拡大したときも大きさを保つというところ。
レッスン5では、ウィンドウと連動して「でっかくなっちゃった!」になる。

レッスン7.位置とサイズを指定してウィンドウを開く(追加でタイトルも付けた)

;;
;; lesson1 <Open the window>
;; lesson2 <Fill in the blue>
;; lesson3 <Draw the lines>
;; lesson4 <Put the color lines>
;; lesson5 <Set polygon and several colors>
;; lesson6 <Reshape >
;; lesson7 <Window size and position>
;; -----------------------------------------------------------------------------------------------
;; Set window class
(defclass Window (glut:window) 
  ()
  (:default-initargs
   :width 320 :height 240 :pos-x 100 :pos-y 100
   :mode '(:rgba) :title "test-window"))

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

;; View port
(defmethod glut:reshape ((window Window) width height)
  (gl:viewport 0 0 width height)
  (gl:load-identity)
  (gl:ortho (- (/ width 200)) (/ width 200) (- (/ height 200)) (/ height 200) -1 1))

;; 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 lesson7 ()
  (glut:display-window (make-instance 'Window)))  ; create window

(lesson7)

こんな感じで、画面の左上から(100,100)の位置に(320,240)の大きさでタイトル「test-window」のウィンドウが出現する。

f:id:tomekame0126:20151101155136p:plain
まあ、ここまでは順調かな?

H27.11.3 修正

bindings-packageを使うバージョンに変更した。