;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;

;; OpenGL Model Example

;;

;; Draws a single model in PLY file format

;;

;; with caps lock down 

;; f = forward 

;; b = backward 

;; l = left 

;; r = right

;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(define *gl* (gl:make-opengl 640 480))

(define *model* (gl:read-ply-model "/tmp/japan2.ply"))


(print (objc:object-for-key *model* "header"))

(print-notification "Please wait. Giving time for model to load")


(define *vertex-data* (objc:object-for-key (objc:object-for-key *model* "vertex") "data"))

(define *face-data* (objc:object-for-key (objc:object-for-key *model* "face") "data"))

(define *face-items* (objc->number (objc:object-for-key (objc:object-for-key *model* "face") "items")))

(define *face-properties* (objc:object-for-key (objc:object-for-key *model* "face") "properties"))

(define *face-type-size* (objc->number (objc:object-for-key (objc:object-for-key *face-properties* "vertex_indices") "size")))


(define init-model

   (lambda ()

      (gl:vertex-pointer *gl* 3 *gl:float* 35 0 *vertex-data*)

      (gl:normal-pointer *gl* *gl:float* 35 3 *vertex-data*)

      (gl:color-pointer *gl* 3 *gl:byte* 35 8 *vertex-data*)

      

      (gl:enable-client-state *gl* *gl:vertex-array*)

      (gl:enable-client-state *gl* *gl:normal-array* )

      (gl:enable-client-state *gl* *gl:color-array*)))


(define init

   (lambda ()

      (gl:clear-color *gl* 0.0 0.0 0.0 0.0)

      (gl:enable *gl* *gl:depth-test*)))


(define view

   (lambda ()

      (gl:viewport *gl* 0 0 640 480)

      (gl:matrix-mode *gl* *gl:projection*)

      (gl:load-identity *gl*)

      (glu:perspective *gl* 40.0 (/ 640 480) 0.1 12.0)

      (gl:matrix-mode *gl* *gl:modelview*)

      (gl:load-identity *gl*)

      (gl:translate *gl* 0.0 .4 -5.0)))


(define *z* 0.0)

(define *x* 0.0)

(define *degrees* 0.0) 


(define caps-key-down

   (lambda (key)

      (cond ((char=? key #\f)

             (set! *z* .1))

            ((char=? key #\b)

             (set! *z* -.1))

            ((char=? key #\l)             

             (set! *x* .1))

            ((char=? key #\r)

             (set! *x* -.1)))))             



(define caps-key-up

   (lambda (key)

      (cond ((char=? key #\f)

             (set! *z* .0))

            ((char=? key #\b)

             (set! *z* .0))

            ((char=? key #\l)

             (set! *x* .0))

            ((char=? key #\r)

             (set! *x* .0)))))


(define draw

   (lambda ()

      (gl:clear *gl* (io:binary-or *gl:depth-buffer-bit* *gl:color-buffer-bit*))

      (gl:translate *gl* *x* 0.0 *z*)

      

      (gl:push-matrix *gl*)

      (gl:rotate *gl* -90 1.0 0 0)

      (gl:draw-ply-vertices *gl* *gl:unsigned-int* *face-items* *face-type-size* *face-data*)

      (gl:pop-matrix *gl*)


      (gl:flush *gl*)

      (callback (+ (now) 2000) 'draw)))


(define start

   (lambda ()

      (init)

      (init-model)

      (view)

      (draw)))


(callback (+ (now) (* *second* 10)) 'start)