`(kakko ,man)

Find a guide into tomorrow by taking lessons from the past

cl-glut pre-study <page 4>

レッスン8.マウスボタンをクリックする

;; 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>
;; lesson8 <mouse>
;; -----------------------------------------------------------------------------------------------
;; 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))
  ; cut program code

;; Mouse
(defmethod glut:mouse ((window Window) button state x y)
  (cond ((eql button :LEFT-BUTTON)
          (princ "left"))
        ((eql button :MIDDLE-BUTTON)
          (princ "middle")) 
        ((eql button :RIGHT-BUTTON)
    	  (princ "right")))

  (princ " button is ")

  (cond ((eql state :UP)
          (princ "up")) 
        ((eql state :DOWN)
    	  (princ "down")))

  (format t " at ~@{~d~^, ~}~%" x y))

;; Display
(defmethod glut:display ((window Window))
  (gl:clear :color-buffer-bit)               ; clear buffer
  ; cut program code
  (gl:flush))                                ; show window

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

(lesson8)

f:id:tomekame0126:20151103192522p:plain

レッスン9.マウスをクリックする(線を引く)

;; 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>
;; lesson8 <mouse>
;; lesson9 <Draw a line with the mouse>
;; -----------------------------------------------------------------------------------------------
;; 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 -0.5 (- width 0.5) (- height 0.5) -0.5 -1 1))

;; Mouse
(defvar *x0*)
(defvar *y0*)
(defmethod glut:mouse ((window Window) button state x y)
  (cond ((eql button :LEFT-BUTTON)
         (if (eql state :UP)
           (progn    
             (%gl:color-3d 0 0 0)                ; black color
             (gl:begin :lines)               ; line
               (%gl:vertex-2i *x0* *y0*)
               (%gl:vertex-2i x y)
             (gl:end)  
             (gl:flush))
           (setf *x0* x                      ; store mouse position 
                 *y0* y)))		   
	 ((eql button :MIDDLE-BUTTON)
	  ; cut program code
          )
         ((eql button :RIGHT-BUTTON)
          ; cut program code
	  )))

;; Display
(defmethod glut:display ((window Window))
  (gl:clear :color-buffer-bit)               ; clear buffer
  ; draw a square
  (gl:flush))                                ; show window

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

(lesson9)

f:id:tomekame0126:20151103193258p:plain

ここで、問題発生!
有名なサイトだけあって、いろんな人が実際に自分の環境で「やってみた」画像を掲載しているが、比較するとどうも違う。
他の人の画像では、マウスを動かした後には何本もの線が残っているのに、自分の環境では描くたびに前の線が消えてしまう。
まだ「見習い」なので、これ以上は深入りせず、この後に控えている「ラバーバンド」まで一旦スキップすることにした。
そのうち考えることにする。(?)


レッスン12.キーボードから読み込む(10・11はスキップ)

;; 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>
;; lesson8 <mouse>
;; lesson9 <Draw a line with the mouse>
;; lesson10-11 -> skip
;; lesson12 <Keyboard>
;; -----------------------------------------------------------------------------------------------
;; 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 -0.5 (- width 0.5) (- height 0.5) -0.5 -1 1))

;; Mouse
(defvar *x0*)
(defvar *y0*)
(defmethod glut:mouse ((window Window) button state x y)
  (cond ((eql button :LEFT-BUTTON)
         (if (eql state :UP)
           (progn    
             (%gl:color-3d 0 0 0)            ; black color
             (gl:begin :lines)               ; line
               (%gl:vertex-2i *x0* *y0*)
               (%gl:vertex-2i x y)
             (gl:end)  
             (gl:flush))
           (setf *x0* x                      ; store mouse position 
                 *y0* y)))		   
	 ((eql button :MIDDLE-BUTTON)
	  ; cut program code
          )
         ((eql button :RIGHT-BUTTON)
          ; cut program code
	  )))
;; Keyboard
(defmethod glut:keyboard ((window Window) key x y)
  (declare (ignore x y))
    (case key
      ((#\q #\Q)                              ; q or Q --> exit
        (glut:destroy-current-window))))

;; Special key
(defmethod glut:special ((window Window) key x y)
  (declare (ignore x y))
    (case key
      (:key-up                                ; upkey  --> exit
        (glut:destroy-current-window))))

;; Display
(defmethod glut:display ((window Window))
  (gl:clear :color-buffer-bit)               ; clear buffer
  ; draw a square
  (gl:flush))                                ; show window

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

(lesson12)

q、Qキーで終了。もう一つは矢印キー↑でも終了する。
以下に定義されているため、確認してくださいな。
https://github.com/3b/cl-opengl/blob/master/glut/interface.lisp
https://github.com/3b/cl-opengl/blob/master/glut/callbacks.lisp


なかなか、勝手が違ってたいへんだね。