]> git.jsancho.org Git - guile-assimp.git/commitdiff
Load and display asset example finished
authorJavier Sancho <jsf@jsancho.org>
Wed, 30 Jul 2014 11:16:18 +0000 (13:16 +0200)
committerJavier Sancho <jsf@jsancho.org>
Wed, 30 Jul 2014 11:16:18 +0000 (13:16 +0200)
examples/sample-figl/sample-figl.scm

index d8c75b7ef9329912ed9da53243e972b562ecaef8..b0b962391cd16c824d8b1ae496bd0027a19e13a4 100755 (executable)
@@ -22,7 +22,9 @@
             (figl gl)
             (figl gl low-level)
             (figl glu)
-            (figl glut))
+            (figl glut)
+            (figl glut low-level)
+            (system foreign))
 
 
 (define main-window #f)
 (define scene-center #f)
 
 ;;; current rotation angle
-(define rotation angle 0.0)
+(define rotation-angle 0.0)
 
 
-(define (reshape width height)
+(define (on-reshape width height)
   (let ((aspect-ratio (/ width height))
        (field-of-view 45))
     (set-gl-matrix-mode (matrix-mode projection))
@@ -47,7 +49,7 @@
     (gl-viewport 0 0 width height)))
 
 
-(define (get-bounding-box-for-node nd, vmin, vmax, trafo)
+(define (get-bounding-box-for-node nd vmin vmax trafo)
   (let ((new-trafo
         (ai-multiply-matrix4 trafo
                              (ai-node-transformation nd))))
 
 
 (define (get-material-color material color-type default-color)
-  (make-c-struct
-   (make-list 4 float)
-   (let ((color (ai-get-material-color material color-type)))
-     (cond ((ai-color4d? color)
-           (color4-to-float4 color))
-          (else
-           default-color)))))
+  (bytevector->pointer
+   (list->f32vector
+    (let ((color (ai-get-material-color material color-type)))
+      (cond ((ai-color4d? color)
+            (color4-to-float4 color))
+           (else
+            default-color))))))
 
 
 (define (apply-material mtl)
   (let ((m (ai-transpose-matrix4 (ai-node-transformation nd))))
     (with-gl-push-matrix
      ; update transform
-     (gl-multiply-matrix (list->array 1 (map cdr (ai-matrix4x4-contents m))))
+     (glMultMatrixf (bytevector->pointer (list->f32vector (map cdr (ai-matrix4x4-contents m)))))
 
      ; draw all meshes assigned to this node
      (for-each
       (lambda (mesh-index)
-       (let ((mesh (list-ref (ai-scene-meshes sc) mesh-index)))
+       (let* ((mesh (list-ref (ai-scene-meshes sc) mesh-index))
+              (mesh-colors (ai-mesh-colors mesh))
+              (mesh-normals (ai-mesh-normals mesh))
+              (mesh-vertices (ai-mesh-vertices mesh)))
          (apply-material (list-ref (ai-scene-materials sc) (ai-mesh-material-index mesh)))
          ((if (null? (ai-mesh-normals mesh))
               gl-disable
                      (else (begin-mode polygon))))
              (for-each
               (lambda (index)
-                (if (not (null? (car (ai-mesh-colors mesh))))
+                (if (not (null? (car mesh-colors)))
                     (glColor4fv
-                     (make-c-struct
-                      (make-list 4 float)
-                      (color4-to-float4
-                       (list-ref (car (ai-mesh-colors mesh)) index)))))
-                (if (not (null? (ai-mesh-normals mesh)))
-                    (glNormal3fv (ai-vector3d-x (list-ref (ai-mesh-normals mesh) index))))
-                (glVertex3fv (ai-vector3d-x (list-ref (ai-mesh-vertices mesh) index))))
+                     (bytevector->pointer
+                      (list->f32vector
+                       (color4-to-float4
+                        (list-ref (car mesh-colors) index))))))
+                (if (not (null? mesh-normals))
+                    (glNormal3fv
+                     (bytevector->pointer
+                      (list->f32vector
+                       (map cdr (ai-vector3d-contents (list-ref mesh-normals index)))))))
+                (glVertex3fv
+                 (bytevector->pointer
+                  (list->f32vector
+                   (map cdr (ai-vector3d-contents (list-ref mesh-vertices index)))))))
               (ai-face-indices face))))
           (ai-mesh-faces mesh))))
       (ai-node-meshes nd))
       (ai-node-children nd)))))
 
 
-(define-glut-state get-elapsed-time elapsed-time)
-
 (define do-motion
   (let ((prev-time 0)
-       (prev_fps_time 0)
+       (prev-fps-time 0)
        (frames 0))
     (lambda ()
-      (let ((time (get-elapsed-time)))
+      (let ((time (glutGet (glut-state elapsed-time))))
        (set! rotation-angle (+ rotation-angle (* 0.01 (- time prev-time))))
        (set! prev-time time)
        (set! frames (1+ frames))
 
        (cond ((> (- time prev-fps-time) 1000)
-              (format #t "~a fps~%" (/ (* frames 1000) (- time prev-fps-time)))
+              (format #t "~a fps~%" (/ (* frames 1000.0) (- time prev-fps-time)))
               (set! frames 0)
               (set! prev-fps-time time)))
 
        (post-redisplay)))))
 
 
-(define (display)
+(define (on-display)
   (gl-clear (clear-buffer-mask color-buffer depth-buffer))
 
   (set-gl-matrix-mode (matrix-mode modelview))
   (gl-load-identity)
-  (glu-look-at 0 0 3 0 0 -5 0 1 0)
+  (glu-look-at 0.0 0.0 3.0 0.0 0.0 -5.0 0.0 1.0 0.0)
 
   ; rotate it around the y axis
-  (gl-rotate rotation-angle 0 1 0)
+  (gl-rotate rotation-angle 0.0 1.0 0.0)
 
   ; scale the whole asset to fit into our view frustum
-  (let ((tmp (apply max (map - scene-max scene-min))))
+  (let ((tmp (/ 1.0 (apply max (map - scene-max scene-min)))))
     (gl-scale tmp tmp tmp))
 
   ; center the model
   ; spelling out 20+ single postprocessing flags here.
   (set! scene (ai-import-file path ai-process-preset-target-realtime-max-quality))
 
-  (let ((box (get_bounding_box)))
+  (let ((box (get-bounding-box)))
     (set! scene-min (car box))
     (set! scene-max (cadr box))
     (set! scene-center
                          #:window-size '(900 . 600)
                          #:window-position '(100 . 100)
                          #:display-mode (display-mode rgb double depth))))
+
     (set! main-window (make-window "Assimp - Very simple OpenGL sample"))
     (set-display-callback (lambda () (on-display)))
     (set-reshape-callback (lambda (w h) (on-reshape w h)))
-    (glut-main-loop)))
+
+    ; get a handle to the predefined STDOUT log stream and attach
+    ; it to the logging system. It remains active for all further
+    ; calls to aiImportFile(Ex) and aiApplyPostProcessing.
+    (ai-attach-predefined-log-stream (ai-default-log-stream stdout))
+
+    ; ... same procedure, but this stream now writes the
+    ; log messages to assimp_log.txt
+    (ai-attach-predefined-log-stream (ai-default-log-stream file) "assimp_log.txt")
+
+    ; the model name can be specified on the command line. If none
+    ; is specified, we try to locate one of the more expressive test
+    ; models from the repository (/models-nonbsd may be missing in
+    ; some distributions so we need a fallback from /models!).
+    (load-asset (cadr args))
+
+    (set-gl-clear-color 0.1 0.1 0.1 1.0)
+
+    (gl-enable (enable-cap lighting))
+    (gl-enable (enable-cap light0))     ; Uses default lighting parameters
+
+    (gl-enable (enable-cap depth-test))
+
+    (glLightModeli (light-model-parameter light-model-two-side) (boolean true))
+    (gl-enable (enable-cap normalize))
+
+    ; XXX docs say all polygons are emitted CCW, but tests show that some aren't.
+    (cond ((getenv "MODEL_IS_BROKEN")
+          (glFrontFace (front-face-direction cw))))
+
+    (glColorMaterial (material-face front-and-back) (material-parameter diffuse))
+
+    (glutGet (glut-state elapsed-time))
+    (glut-main-loop)
+
+    ; cleanup - calling 'aiReleaseImport' is important, as the library
+    ; keeps internal resources until the scene is freed again. Not
+    ; doing so can cause severe resource leaking.
+    (ai-release-import scene)
+
+    ; We added a log stream to the library, it's our job to disable it
+    ; again. This will definitely release the last resources allocated
+    ; by Assimp.
+    (ai-detach-all-log-streams)))
 
 
 (when (batch-mode?)