]> git.jsancho.org Git - guile-irrlicht.git/blobdiff - irrlicht/scene.scm
add-octree-scene-node!
[guile-irrlicht.git] / irrlicht / scene.scm
index 264a4bd9883fce2d46d5a70257216ede6820736b..6a6486fe8565f8bf5ecdce34723df012112211a3 100644 (file)
@@ -1,5 +1,5 @@
 ;;; guile-irrlicht --- FFI bindings for Irrlicht Engine
-;;; Copyright (C) 2019 Javier Sancho <jsf@jsancho.org>
+;;; Copyright (C) 2020 Javier Sancho <jsf@jsancho.org>
 ;;;
 ;;; This file is part of guile-irrlicht.
 ;;;
 
 
 (define-module (irrlicht scene)
-  #:use-module (ice-9 match)
-  #:use-module (system foreign)
-  #:use-module ((irrlicht bindings core) #:prefix ffi-core:)
-  #:use-module ((irrlicht bindings scene) #:prefix ffi-scene:)
-  #:use-module ((irrlicht bindings video) #:prefix ffi-video:)
-  #:use-module (irrlicht util)
-  #:export (add-animated-mesh-scene-node
-            add-camera-scene-node
-            add-camera-scene-node-fps!
-            add-octree-scene-node-am
-            get-mesh
-            scene-draw-all
-            set-material-flag-am!
-            set-material-texture-am!
-            set-md2-animation!
-            set-position!))
-
-(define* (add-animated-mesh-scene-node scene-manager mesh
-                                       #:key
-                                       (parent %null-pointer)
-                                       (id -1)
-                                       (position '(0 0 0))
-                                       (rotation '(0 0 0))
-                                       (scale '(1 1 1))
-                                       (also-add-if-mesh-pointer-zero #f))
-  (let ((node (ffi-scene:add-animated-mesh-scene-node
-               scene-manager
-               mesh
-               parent
-               id
-               (make-c-struct ffi-core:vector3df position)
-               (make-c-struct ffi-core:vector3df rotation)
-               (make-c-struct ffi-core:vector3df scale)
-               (bool->integer also-add-if-mesh-pointer-zero))))
-    (if (null-pointer? node) #f node)))
-
-(define* (add-camera-scene-node scene-manager
-                                #:key
-                                (parent %null-pointer)
-                                (position '(0 0 0))
-                                (lookat '(0 0 100))
-                                (id -1)
-                                (make-active #t))
-  (let ((camera (ffi-scene:add-camera-scene-node
-                 scene-manager
-                 parent
-                 (make-c-struct ffi-core:vector3df position)
-                 (make-c-struct ffi-core:vector3df lookat)
-                 id
-                 (bool->integer make-active))))
-    (if (null-pointer? camera) #f camera)))
-
-(define* (add-camera-scene-node-fps! scene-manager
-                                     #:key
-                                     (parent %null-pointer)
-                                     (rotate-speed 100.0)
-                                     (move-speed 0.5)
-                                     (id -1)
-                                     (key-map-array %null-pointer)
-                                     (key-map-size 0)
-                                     (no-vertical-movement #f)
-                                     (jump-speed 0.0)
-                                     (invert-mouse #f)
-                                     (make-active #t))
-  (ffi-scene:add-camera-scene-node-fps
-   scene-manager
-   parent
-   rotate-speed
-   move-speed
-   id
-   key-map-array
-   key-map-size
-   (bool->integer no-vertical-movement)
-   jump-speed
-   (bool->integer invert-mouse)
-   (bool->integer make-active)))
-
-(define* (add-octree-scene-node-am scene-manager mesh
-                                   #:key
-                                   (parent %null-pointer)
-                                   (id -1)
-                                   (minimal-polys-per-node 512)
-                                   (also-add-if-mesh-pointer-zero #f))
-  (ffi-scene:add-octree-scene-node-am
-   scene-manager
-   mesh
-   parent
-   id
-   minimal-polys-per-node
-   (bool->integer also-add-if-mesh-pointer-zero)))
-
-(define (get-mesh scene-manager filename)
-  (let ((mesh (ffi-scene:get-mesh scene-manager (string->pointer filename))))
-    (if (null-pointer? mesh) #f mesh)))
-
-(define (scene-draw-all scene-manager)
-  (ffi-scene:draw-all scene-manager))
-
-(define (set-material-flag-am! node flag newvalue)
-  (let ((material-flag
-         (match flag
-                ('wireframe ffi-video:EMF_WIREFRAME)
-                ('pointcloud ffi-video:EMF_POINTCLOUD)
-                ('gouraud-shading ffi-video:EMF_GOURAUD_SHADING)
-                ('lighting ffi-video:EMF_LIGHTING)
-                ('zbuffer ffi-video:EMF_ZBUFFER)
-                ('zwrite-enable ffi-video:EMF_ZWRITE_ENABLE)
-                ('back-face-culling ffi-video:EMF_BACK_FACE_CULLING)
-                ('front-face-culling ffi-video:EMF_FRONT_FACE_CULLING)
-                ('bilinear-filter ffi-video:EMF_BILINEAR_FILTER)
-                ('trilinear-filter ffi-video:EMF_TRILINEAR_FILTER)
-                ('anisotropic-filter ffi-video:EMF_ANISOTROPIC_FILTER)
-                ('fog-enable ffi-video:EMF_FOG_ENABLE)
-                ('normalize-normals ffi-video:EMF_NORMALIZE_NORMALS)
-                ('texture-wrap ffi-video:EMF_TEXTURE_WRAP)
-                ('anti-aliasing ffi-video:EMF_ANTI_ALIASING)
-                ('color-mask ffi-video:EMF_COLOR_MASK)
-                ('color-material ffi-video:EMF_COLOR_MATERIAL)
-                ('use-mip-maps ffi-video:EMF_USE_MIP_MAPS)
-                ('blend-operation ffi-video:EMF_BLEND_OPERATION)
-                ('polygon-offset ffi-video:EMF_POLYGON_OFFSET))))
-    (ffi-scene:set-material-flag-am
-     node
-     material-flag
-     (bool->integer newvalue))))
-
-(define (set-material-texture-am! node texture-layer texture)
-  (ffi-scene:set-material-texture-am node texture-layer texture))
-
-(define (set-md2-animation! node anim)
-  (let ((animation-type
-         (match anim
-                ('stand ffi-scene:EMAT_STAND)
-                ('run ffi-scene:EMAT_RUN)
-                ('attack ffi-scene:EMAT_ATTACK)
-                ('pain-a ffi-scene:EMAT_PAIN_A)
-                ('pain-b ffi-scene:EMAT_PAIN_B)
-                ('pain-c ffi-scene:EMAT_PAIN_C)
-                ('jump ffi-scene:EMAT_JUMP)
-                ('flip ffi-scene:EMAT_FLIP)
-                ('salute ffi-scene:EMAT_SALUTE)
-                ('fallback ffi-scene:EMAT_FALLBACK)
-                ('wave ffi-scene:EMAT_WAVE)
-                ('point ffi-scene:EMAT_POINT)
-                ('crouch-stand ffi-scene:EMAT_CROUCH_STAND)
-                ('crouch-walk ffi-scene:EMAT_CROUCH_WALK)
-                ('crouch-attack ffi-scene:EMAT_CROUCH_ATTACK)
-                ('crouch-pain ffi-scene:EMAT_CROUCH_PAIN)
-                ('crouch-death ffi-scene:EMAT_CROUCH_DEATH)
-                ('death-fallback ffi-scene:EMAT_DEATH_FALLBACK)
-                ('death-fallforward ffi-scene:EMAT_DEATH_FALLFORWARD)
-                ('death-fallbackslow ffi-scene:EMAT_DEATH_FALLBACKSLOW)
-                ('boom ffi-scene:EMAT_BOOM)
-                ('count ffi-scene:EMAT_COUNT))))
-    (ffi-scene:set-md2-animation
-     node
-     animation-type)))
-
-(define (set-position! node newpos)
-  (ffi-scene:set-position
+  #:use-module (oop goops)
+  #:use-module (ice-9 optargs)
+  #:use-module (irrlicht base)
+  #:use-module (irrlicht foreign)
+  #:use-module (irrlicht io)
+  #:use-module (irrlicht irr)
+  #:use-module (irrlicht video))
+
+
+;; IMesh
+(define-class <mesh> (<reference-counted>)
+  (irr-class #:init-value "IMesh"))
+
+(export <mesh>)
+
+
+;; IAnimatedMesh
+(define-class <animated-mesh> (<mesh>)
+  (irr-class #:init-value "IAnimatedMesh"))
+
+(export <animated-mesh>)
+
+
+;; ISceneManager
+(define-class <scene-manager> (<reference-counted>)
+  (irr-class #:init-value "ISceneManager"))
+
+(define-method (add-animated-mesh-scene-node! (scene-manager <scene-manager>)
+                                              (mesh <animated-mesh>)
+                                              . rest)
+  (let-keywords rest #f
+        ((parent (make <scene-node>))
+         (id -1)
+         (position '(0 0 0))
+         (rotation '(0 0 0))
+         (scale '(1 1 1))
+         (also-add-if-mesh-pointer-zero #f))
+    (make <animated-mesh-scene-node>
+      #:irr-pointer
+      ((get-irrlicht-proc "addAnimatedMeshSceneNode" scene-manager parent)
+       scene-manager
+       mesh
+       parent
+       id
+       position
+       rotation
+       scale
+       also-add-if-mesh-pointer-zero))))
+
+(define-method (add-camera-scene-node! (scene-manager <scene-manager>) . rest)
+  (let-keywords rest #f
+        ((parent (make <scene-node>))
+         (position '(0 0 0))
+         (lookat '(0 0 100))
+         (id -1)
+         (make-active #t))
+    (make <camera-scene-node>
+      #:irr-pointer
+      ((get-irrlicht-proc "addCameraSceneNode" scene-manager parent)
+       scene-manager
+       parent
+       position
+       lookat
+       id
+       make-active))))
+
+(define-method (add-octree-scene-node! (scene-manager <scene-manager>)
+                                       (mesh <animated-mesh>)
+                                       . rest)
+  (let-keywords rest #f
+        ((parent (make <scene-node>))
+         (id -1)
+         (minimal-polys-per-node 512)
+         (also-add-if-mesh-pointer-zero #f))
+    (let ((addOctreeSceneNode (get-irrlicht-proc "addOctreeSceneNode" scene-manager parent mesh)))
+      (make <mesh-scene-node>
+        #:irr-pointer
+        (addOctreeSceneNode scene-manager mesh parent id minimal-polys-per-node
+                            also-add-if-mesh-pointer-zero)))))
+
+(define-method (add-octree-scene-node! (scene-manager <scene-manager>)
+                                       (mesh <mesh>)
+                                       . rest)
+  (let-keywords rest #f
+        ((parent (make <scene-node>))
+         (id -1)
+         (minimal-polys-per-node 256)
+         (also-add-if-mesh-pointer-zero #f))
+    (let ((addOctreeSceneNode (get-irrlicht-proc "addOctreeSceneNode" scene-manager parent mesh)))
+      (make <mesh-scene-node>
+        #:irr-pointer
+        (addOctreeSceneNode scene-manager mesh parent id minimal-polys-per-node
+                            also-add-if-mesh-pointer-zero)))))
+
+(define-method (draw-all (scene-manager <scene-manager>))
+  ((get-irrlicht-proc "drawAll" scene-manager)
+   scene-manager))
+
+(define-method (get-mesh (scene-manager <scene-manager>) filename)
+  (make <animated-mesh>
+    #:irr-pointer
+    ((get-irrlicht-proc "getMesh" scene-manager)
+     scene-manager
+     filename)))
+
+(export <scene-manager> add-animated-mesh-scene-node! add-camera-scene-node! add-octree-scene-node!
+        draw-all get-mesh)
+
+
+;; ISceneNode
+(define-class <scene-node> (<attribute-exchanging-object>)
+  (irr-class #:init-value "ISceneNode"))
+
+(define-method (set-material-flag! (node <scene-node>) flag new-value)
+  ((get-irrlicht-proc "setMaterialFlag" node)
+   node
+   flag
+   new-value))
+
+(define-method (set-material-texture! (node <scene-node>) texture-layer (texture <texture>))
+  ((get-irrlicht-proc "setMaterialTexture" node)
+   node
+   texture-layer
+   texture))
+
+(export <scene-node> set-material-flag! set-material-texture!)
+
+
+;; IAnimatedMeshSceneNode
+(define-class <animated-mesh-scene-node> (<scene-node>)
+  (irr-class #:init-value "IAnimatedMeshSceneNode"))
+
+(define-method (set-md2-animation! (node <animated-mesh-scene-node>) anim)
+  ((get-irrlicht-proc "setMD2Animation" node)
    node
-   (make-c-struct ffi-core:vector3df newpos)))
+   anim))
+
+(export <animated-mesh-scene-node> set-md2-animation!)
+
+
+;; ICameraSceneNode
+(define-class <camera-scene-node> (<scene-node>)
+  (irr-class #:init-value "ICameraSceneNode"))
+
+(export <camera-scene-node>)
+
+
+;; IMeshSceneNode
+(define-class <mesh-scene-node> (<scene-node>)
+  (irr-class #:init-value "IMeshSceneNode"))
+
+(export <mesh-scene-node>)