;; -*- mode: scheme; -*-
;; Planet Fluxus Copyright (C) 2013 Dave Griffiths
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU Affero General Public License for more details.
;;
;; You should have received a copy of the GNU Affero General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(define (polyprim size type vb bb)
  (list size type vb bb))

(define (polyprim-size p) (list-ref p 0))
(define (polyprim-type p) (list-ref p 1))
(define (polyprim-pdata p) (list-ref p 2))
(define (polyprim-modify-pdata p v) (list-replace p 2 v))
(define (polyprim-bb p) (list-ref p 3))
(define (polyprim-modify-bb p v) (list-replace p 3 v))

(define (build-polyprim gl type pdata)
  (let ((p (polyprim 
			(pdata-size-verts pdata) type 
			(pdata-build-vbo gl pdata)
			(boundingbox-empty)
			)))
    (pdata-update-vbo! gl pdata)
    p))

(define (build-empty-polyprim gl type size-verts)
  ;; match load obj for the moment with points, 
  ;; normals and texturecoords 
  (build-polyprim 
   gl type
   (make-pdata 
	(list "p" "n" "t" "c")
	size-verts
	(build-list 
	 (* (* size-verts 4) 3) 
	 (lambda (i) 0)))))

(define (polyprim-combine gl polyprims)
  (build-polyprim
   gl 'triangles
   (make-pdata 
	(list "p" "n" "t" "c") ;; assume these match!
	(foldl
	 (lambda (pp r)
	   (if (eq? (primitive-type pp) 'polyprim)
		   (+ r (pdata-size-verts
				 (polyprim-pdata
				  (primitive-inner pp))))
		   r))
	 0
	 polyprims)
	(foldl
	 (lambda (pp r)
	   (if (eq? (primitive-type pp) 'polyprim)
		   (append r (pdata-interleaved-data
					  (polyprim-pdata
					   (primitive-inner pp))))
		   r))
	 '()
	 polyprims))))

(define (polyprim-update-buffers! gl p)
  (pdata-update-vbo! gl (polyprim-pdata p)))
  
(define (polyprim-render p gl view-camera camera-pos state tx)
  (pdata-render
   (polyprim-pdata p)
   (polyprim-type p)
   gl view-camera camera-pos state tx))

