読者です 読者をやめる 読者になる 読者になる

`(kakko ,man)

Find a guide into tomorrow by taking lessons from the past

cl-glut pre-study <page 6>

なかなかいいサンプルがあったので、レッスン17を以下のように変えてみた
http://blog.lowsnr.net/2013/04/14/using-opengl-with-common-lisp-and-macos-x/
f:id:tomekame0126:20151117231748p:plain

;;
;; lesson17 <animation>
;; -----------------------------------------------------------------------------------------------
(defparameter *vertex*
  #(#(0.0 0.0 0.0)
    #(1.0 0.0 0.0)
    #(1.0 1.0 0.0)
    #(0.0 1.0 0.0)
    #(0.0 0.0 1.0)
    #(1.0 0.0 1.0)
    #(1.0 1.0 1.0)
    #(0.0 1.0 1.0)))
 
(defparameter *edge*
  '(#(0 1)
    #(1 2) 
    #(2 3)
    #(3 0)
    #(4 5)
    #(5 6)
    #(6 7)
    #(7 4)
    #(0 4)
    #(1 5)
    #(2 6)
    #(3 7)))

(defun draw-figure (vertex edge)
  (labels ((set-vertex (index)
             (let ((v (aref vertex index)))
	       (%gl:vertex-3d (aref v 0) (aref v 1) (aref v 2))))
              ;(gl:vertex (aref v 0) (aref v 1) (aref v 2))))
           (draw-face (vertex-indices)
             (gl:begin :lines)
               (map nil  #'set-vertex vertex-indices)
             (gl:end)))
    (map nil #'(lambda (x) (draw-face x)) edge)))

(defvar *r* 0)  ; angle
(defvar *p* 5.0) ; lookat
(defvar *flag* t) ; rotation flag
(defvar *toggle* t) ;toggle switch

;; Set window class
(defclass Window (glut:window)
  ((fullscreen :initarg :fullscreen :reader fullscreen-p)) 
  (:default-initargs
   :pos-x 100 :pos-y 100 :width 500 :height 500
   :fullscreen nil
   :mode '(:double :rgba) :title "cube-test"))

;; Init
(defmethod glut:display-window :before ((window Window))
  (gl:clear-color 1 1 1 1)        ; set white
  (when (fullscreen-p window)     ; check to see if fullscreen needed
    (glut:full-screen)))          ; if so, then tell GLUT

;; View port
(defmethod glut:reshape ((window Window) width height)
  (gl:viewport 0 0 width height)
  (gl:matrix-mode :projection)
  (gl:load-identity)
  (glu:perspective 30.0 (/ width height) 1.0 100.0) 
  (gl:matrix-mode :modelview))

; idle
(defmethod glut:idle ((window Window))
    ;; user idling process
    (sleep (/ 1.0 60.0))
    (glut:post-redisplay))

;; mouse
(defmethod glut:mouse ((window Window) button state x y)
  (declare (ignore x y))
  (case button 
    (:RIGHT-BUTTON
      (if (eql state :DOWN)
        (progn
          (setf *flag* t)
          (glut:enable-event window :idle))
        (glut:disable-event window :idle)))
    (:MIDDLE-BUTTON
      (if (eql state :DOWN)
	(glut:post-redisplay)))
    (:LEFT-BUTTON
      (if (eql state :DOWN)
        (progn
	  (setf *flag* nil)
          (glut:enable-event window :idle))
        (glut:disable-event window :idle)))))

;; mouse-wheel
(defmethod glut:mouse-wheel ((window Window) wheel direction x y)
 (declare (ignore x y))
 (case direction
   (:wheel-down
     (progn
       (incf *p* 0.2) 
       (when (>= *p* 9.8)
	 (setf *p* 9.8))
       (glut:post-redisplay)))       
   (otherwise
     (progn 
       (decf *p* 0.2)
       (when (<= *p* 0.2)
         (setf *p* 0.2))
       (glut:post-redisplay)))))

;; Keyboard
(defmethod glut:keyboard ((window Window) key x y)
  (declare (ignore x y))
    (case key
      ((#\q #\Q)                              ; q, Q --> exit
        (glut:destroy-current-window))
      ((#\f #\F)                              ; f, F --> full or windowed
        (if (eql *toggle* t)
	    (progn
	      (setf *toggle* nil)
	      (glut:full-screen))
	    (progn
	      (setf *toggle* t)
	      (glut:position-window 100 100)       ; initial position
              (glut:reshape-window 500 500))))))   ; initial width height

;; Display
(defmethod glut:display ((window Window))
  (gl:clear :color-buffer-bit)                ; clear buffer
  (gl:load-identity)
  (glu:look-at 3.0 4.0 *p* 0.0 0.0 0.0 0.0 1.0 0.0)
  (gl:rotate *r* 0.0 1.0 0.0)
  ;; cube
  (gl:scale 1 1 1)
  (gl:color 0.0 0.0 0.0)

  (draw-figure *vertex* *edge*)

  (glut:swap-buffers)
   
  (if (eql *flag* t)
    (incf *r*)                                ; right rotation
    (decf *r*))                               ; left  rotation
  (cond ((>= *r* 360)
          (setf *r* 0))
        ((<= *r* 0)
          (setf *r* 360))))  
 
;; Main
(defun lesson17 ()
  (glut:display-window (make-instance 'Window)))  ; create window

(lesson17)

レッスン18.多面体を塗りつぶす
確かに変な表示だ
f:id:tomekame0126:20151117232310p:plain

;;
;; lesson18 <fill the polyhedron>
;; -----------------------------------------------------------------------------------------------
(defparameter *vertex*
  #(#(0.0 0.0 0.0)
    #(1.0 0.0 0.0)
    #(1.0 1.0 0.0)
    #(0.0 1.0 0.0)
    #(0.0 0.0 1.0)
    #(1.0 0.0 1.0)
    #(1.0 1.0 1.0)
    #(0.0 1.0 1.0)))

(defparameter *face*
  '((#(0 1 2 3) #(1 0 0))    ; red
    (#(1 5 6 2) #(0 1 0))    ; green
    (#(5 4 7 6) #(0 0 1))    ; blue
    (#(4 0 3 7) #(1 1 0))    ; yellow
    (#(4 5 1 0) #(1 0 1))    ; magenda
    (#(3 2 6 7) #(0 1 1))))  ; cyan

(defun draw-figure (vertex face)
  (labels ((set-color (color)
	     (%gl:color-3d (aref color 0) (aref color 1) (aref color 2)))
	   (set-vertex (index)
             (let ((v (aref vertex index)))
	       (%gl:vertex-3d (aref v 0) (aref v 1) (aref v 2))))
           (draw-face (vertex-indices color)
             (gl:begin :quads)
	       (set-color color)
               (map nil  #'set-vertex vertex-indices)
             (gl:end)))
    (map nil #'(lambda (x) (draw-face (first x) (second x))) face)))

(defvar *r* 0)  ; angle
(defvar *p* 5.0) ; lookat
(defvar *flag* t) ; rotation flag
(defvar *toggle* t) ;toggle switch

;; Set window class
(defclass Window (glut:window)
  ((fullscreen :initarg :fullscreen :reader fullscreen-p)) 
  (:default-initargs
   :pos-x 100 :pos-y 100 :width 500 :height 500
   :fullscreen nil
   :mode '(:double :rgb) :title "cube-test"))

;; Init
(defmethod glut:display-window :before ((window Window))
  (gl:clear-color 1 1 1 1)        ; set white
  (when (fullscreen-p window)     ; check to see if fullscreen needed
    (glut:full-screen)))          ; if so, then tell GLUT

;; View port
(defmethod glut:reshape ((window Window) width height)
  (gl:viewport 0 0 width height)
  (gl:matrix-mode :projection)
  (gl:load-identity)
  (glu:perspective 30.0 (/ width height) 1.0 100.0) 
  (gl:matrix-mode :modelview))

; idle
(defmethod glut:idle ((window Window))
    ;; user idling process
    (sleep (/ 1.0 60.0))
    (glut:post-redisplay))

;; mouse
(defmethod glut:mouse ((window Window) button state x y)
  (declare (ignore x y))
  (case button 
    (:RIGHT-BUTTON
      (if (eql state :DOWN)
        (progn
          (setf *flag* t)
          (glut:enable-event window :idle))
        (glut:disable-event window :idle)))
    (:MIDDLE-BUTTON
      (if (eql state :DOWN)
	(glut:post-redisplay)))
    (:LEFT-BUTTON
      (if (eql state :DOWN)
        (progn
	  (setf *flag* nil)
          (glut:enable-event window :idle))
        (glut:disable-event window :idle)))))

;; mouse-wheel
(defmethod glut:mouse-wheel ((window Window) wheel direction x y)
 (declare (ignore x y))
 (case direction
   (:wheel-down
     (progn
       (incf *p* 0.2) 
       (when (>= *p* 9.8)
	 (setf *p* 9.8))
       (glut:post-redisplay)))       
   (otherwise
     (progn 
       (decf *p* 0.2)
       (when (<= *p* 0.2)
         (setf *p* 0.2))
       (glut:post-redisplay)))))

;; Keyboard
(defmethod glut:keyboard ((window Window) key x y)
  (declare (ignore x y))
    (case key
      ((#\q #\Q)                              ; q, Q --> exit
        (glut:destroy-current-window))
      ((#\f #\F)                              ; f, F --> full or windowed
        (if (eql *toggle* t)
	    (progn
	      (setf *toggle* nil)
	      (glut:full-screen))
	    (progn
	      (setf *toggle* t)
	      (glut:position-window 100 100)       ; initial position
              (glut:reshape-window 500 500))))))   ; initial width height

;; Display
(defmethod glut:display ((window Window))
  (gl:clear :color-buffer-bit)                ; clear buffer
  (gl:load-identity)
  (glu:look-at 3.0 4.0 *p* 0.0 0.0 0.0 0.0 1.0 0.0)
  (gl:rotate *r* 0.0 1.0 0.0)
  ;; cube
  (gl:scale 1 1 1)    ; x,y,z same scale

  (draw-figure *vertex* *face*)

  (glut:swap-buffers)
  (if (eql *flag* t)
    (incf *r*)                                ; right rotation
    (decf *r*))                               ; left  rotation
  (cond ((>= *r* 360)
          (setf *r* 0))
        ((<= *r* 0)
          (setf *r* 360))))  
 
;; Main
(defun lesson18 ()
  (glut:display-window (make-instance 'Window)))  ; create window

(lesson18)

レッスン19.デプスバッファを使用する
表示がまともになった
f:id:tomekame0126:20151117232839p:plain

;;
;; lesson19 <depth buffer>
;; -----------------------------------------------------------------------------------------------
(defparameter *vertex*
  #(#(0.0 0.0 0.0)
    #(1.0 0.0 0.0)
    #(1.0 1.0 0.0)
    #(0.0 1.0 0.0)
    #(0.0 0.0 1.0)
    #(1.0 0.0 1.0)
    #(1.0 1.0 1.0)
    #(0.0 1.0 1.0)))

(defparameter *face*
  '((#(0 1 2 3) #(1 0 0))    ; red
    (#(1 5 6 2) #(0 1 0))    ; green
    (#(5 4 7 6) #(0 0 1))    ; blue
    (#(4 0 3 7) #(1 1 0))    ; yellow
    (#(4 5 1 0) #(1 0 1))    ; magenda
    (#(3 2 6 7) #(0 1 1))))  ; cyan

(defun draw-figure (vertex face)
  (labels ((set-color (color)
	     (%gl:color-3d (aref color 0) (aref color 1) (aref color 2)))
	   (set-vertex (index)
             (let ((v (aref vertex index)))
	       (%gl:vertex-3d (aref v 0) (aref v 1) (aref v 2))))
           (draw-face (vertex-indices color)
             (gl:begin :quads)
	       (set-color color)
               (map nil  #'set-vertex vertex-indices)
             (gl:end)))
    (map nil #'(lambda (x) (draw-face (first x) (second x))) face)))

(defvar *r* 0)  ; angle
(defvar *p* 5.0) ; lookat
(defvar *flag* t) ; rotation flag
(defvar *toggle* t) ;toggle switch

;; Set window class
(defclass Window (glut:window)
  ((fullscreen :initarg :fullscreen :reader fullscreen-p)) 
  (:default-initargs
   :pos-x 100 :pos-y 100 :width 500 :height 500
   :fullscreen nil
   :mode '(:double :rgb) :title "cube-test"))

;; Init
(defmethod glut:display-window :before ((window Window))
  (gl:clear-color 1 1 1 1)        ; set white
  (gl:enable :depth-test)         ; enable depth testing
  (when (fullscreen-p window)     ; check to see if fullscreen needed
    (glut:full-screen)))          ; if so, then tell GLUT

;; View port
(defmethod glut:reshape ((window Window) width height)
  (gl:viewport 0 0 width height)
  (gl:matrix-mode :projection)
  (gl:load-identity)
  (glu:perspective 30.0 (/ width height) 1.0 100.0) 
  (gl:matrix-mode :modelview))

; idle
(defmethod glut:idle ((window Window))
    ;; user idling process
    (sleep (/ 1.0 60.0))
    (glut:post-redisplay))

;; mouse
(defmethod glut:mouse ((window Window) button state x y)
  (declare (ignore x y))
  (case button 
    (:RIGHT-BUTTON
      (if (eql state :DOWN)
        (progn
          (setf *flag* t)
          (glut:enable-event window :idle))
        (glut:disable-event window :idle)))
    (:MIDDLE-BUTTON
      (if (eql state :DOWN)
	(glut:post-redisplay)))
    (:LEFT-BUTTON
      (if (eql state :DOWN)
        (progn
	  (setf *flag* nil)
          (glut:enable-event window :idle))
        (glut:disable-event window :idle)))))

;; mouse-wheel
(defmethod glut:mouse-wheel ((window Window) wheel direction x y)
 (declare (ignore x y))
 (case direction
   (:wheel-down
     (progn
       (incf *p* 0.2) 
       (when (>= *p* 9.8)
	 (setf *p* 9.8))
       (glut:post-redisplay)))       
   (otherwise
     (progn 
       (decf *p* 0.2)
       (when (<= *p* 0.2)
         (setf *p* 0.2))
       (glut:post-redisplay)))))

;; Keyboard
(defmethod glut:keyboard ((window Window) key x y)
  (declare (ignore x y))
    (case key
      ((#\q #\Q)                              ; q, Q --> exit
        (glut:destroy-current-window))
      ((#\f #\F)                              ; f, F --> full or windowed
        (if (eql *toggle* t)
	    (progn
	      (setf *toggle* nil)
	      (glut:full-screen))
	    (progn
	      (setf *toggle* t)
	      (glut:position-window 100 100)       ; initial position
              (glut:reshape-window 500 500))))))   ; initial width height

;; Display
(defmethod glut:display ((window Window))
  (gl:clear :color-buffer-bit :depth-buffer-bit)    ; clear buffer and set depth buffer
  (gl:load-identity)
  (glu:look-at 3.0 4.0 *p* 0.0 0.0 0.0 0.0 1.0 0.0)
  (gl:rotate *r* 0.0 1.0 0.0)
  ;; cube
  (gl:scale 1 1 1)    ; x,y,z same scale
 
  (draw-figure *vertex* *face*)
  
  (glut:swap-buffers)
  (if (eql *flag* t)
    (incf *r*)                                ; right rotation
    (decf *r*))                               ; left  rotation
  (cond ((>= *r* 360)
          (setf *r* 0))
        ((<= *r* 0)
          (setf *r* 360))))  
 
;; Main
(defun lesson19 ()
  (glut:display-window (make-instance 'Window)))  ; create window

(lesson19)

レッスン20.カリング
見た目は全く変わらないため、画像は割愛

;;
;; lesson20 <culling>
;; -----------------------------------------------------------------------------------------------
(defparameter *vertex*
  #(#(0.0 0.0 0.0)
    #(1.0 0.0 0.0)
    #(1.0 1.0 0.0)
    #(0.0 1.0 0.0)
    #(0.0 0.0 1.0)
    #(1.0 0.0 1.0)
    #(1.0 1.0 1.0)
    #(0.0 1.0 1.0)))

(defparameter *face*
  '((#(0 1 2 3) #(1 0 0))    ; red
    (#(1 5 6 2) #(0 1 0))    ; green
    (#(5 4 7 6) #(0 0 1))    ; blue
    (#(4 0 3 7) #(1 1 0))    ; yellow
    (#(4 5 1 0) #(1 0 1))    ; magenda
    (#(3 2 6 7) #(0 1 1))))  ; cyan

(defun draw-figure (vertex face)
  (labels ((set-color (color)
	     (%gl:color-3d (aref color 0) (aref color 1) (aref color 2)))
	   (set-vertex (index)
             (let ((v (aref vertex index)))
	       (%gl:vertex-3d (aref v 0) (aref v 1) (aref v 2))))
           (draw-face (vertex-indices color)
             (gl:begin :quads)
	       (set-color color)
               (map nil  #'set-vertex vertex-indices)
             (gl:end)))
    (map nil #'(lambda (x) (draw-face (first x) (second x))) face)))

(defvar *r* 0)  ; angle
(defvar *p* 5.0) ; lookat
(defvar *flag* t) ; rotation flag
(defvar *toggle* t) ;toggle switch

;; Set window class
(defclass Window (glut:window)
  ((fullscreen :initarg :fullscreen :reader fullscreen-p)) 
  (:default-initargs
   :pos-x 100 :pos-y 100 :width 500 :height 500
   :fullscreen nil
   :mode '(:double :rgb) :title "cube-test"))

;; Init
(defmethod glut:display-window :before ((window Window))
  (gl:clear-color 1 1 1 1)        ; set white
  (gl:enable :depth-test)         ; enable depth testing
  (gl:cull-face :back)            ; cullface
  (when (fullscreen-p window)     ; check to see if fullscreen needed
    (glut:full-screen)))          ; if so, then tell GLUT

;; View port
(defmethod glut:reshape ((window Window) width height)
  (gl:viewport 0 0 width height)
  (gl:matrix-mode :projection)
  (gl:load-identity)
  (glu:perspective 30.0 (/ width height) 1.0 100.0) 
  (gl:matrix-mode :modelview))

; idle
(defmethod glut:idle ((window Window))
    ;; user idling process
    (sleep (/ 1.0 60.0))
    (glut:post-redisplay))

;; mouse
(defmethod glut:mouse ((window Window) button state x y)
  (declare (ignore x y))
  (case button 
    (:RIGHT-BUTTON
      (if (eql state :DOWN)
        (progn
          (setf *flag* t)
          (glut:enable-event window :idle))
        (glut:disable-event window :idle)))
    (:MIDDLE-BUTTON
      (if (eql state :DOWN)
	(glut:post-redisplay)))
    (:LEFT-BUTTON
      (if (eql state :DOWN)
        (progn
	  (setf *flag* nil)
          (glut:enable-event window :idle))
        (glut:disable-event window :idle)))))

;; mouse-wheel
(defmethod glut:mouse-wheel ((window Window) wheel direction x y)
 (declare (ignore x y))
 (case direction
   (:wheel-down
     (progn
       (incf *p* 0.2) 
       (when (>= *p* 9.8)
	 (setf *p* 9.8))
       (glut:post-redisplay)))       
   (otherwise
     (progn 
       (decf *p* 0.2)
       (when (<= *p* 0.2)
         (setf *p* 0.2))
       (glut:post-redisplay)))))

;; Keyboard
(defmethod glut:keyboard ((window Window) key x y)
  (declare (ignore x y))
    (case key
      ((#\q #\Q)                              ; q, Q --> exit
        (glut:destroy-current-window))
      ((#\f #\F)                              ; f, F --> full or windowed
        (if (eql *toggle* t)
	    (progn
	      (setf *toggle* nil)
	      (glut:full-screen))
	    (progn
	      (setf *toggle* t)
	      (glut:position-window 100 100)       ; initial position
              (glut:reshape-window 500 500))))))   ; initial width height

;; Display
(defmethod glut:display ((window Window))
  (gl:clear :color-buffer-bit :depth-buffer-bit)    ; clear buffer and set depth buffer
  (gl:load-identity)
  (glu:look-at 3.0 4.0 *p* 0.0 0.0 0.0 0.0 1.0 0.0)
  (gl:rotate *r* 0.0 1.0 0.0)
  ;; cube
  (gl:scale 1 1 1)    ; x,y,z same scale

  (draw-figure *vertex* *face*)

  (glut:swap-buffers)
  (if (eql *flag* t)
    (incf *r*)                                ; right rotation
    (decf *r*))                               ; left  rotation
  (cond ((>= *r* 360)
          (setf *r* 0))
        ((<= *r* 0)
          (setf *r* 360))))  
 
;; Main
(defun lesson20 ()
  (glut:display-window (make-instance 'Window)))  ; create window

(lesson20)

遅々として進まないが、なかなか面白い。