]> git.jsancho.org Git - guile-irrlicht.git/commitdiff
HelloWorld example with all the functions needed
authorJavier Sancho <jsf@jsancho.org>
Fri, 18 Oct 2019 17:47:30 +0000 (19:47 +0200)
committerJavier Sancho <jsf@jsancho.org>
Fri, 18 Oct 2019 17:47:30 +0000 (19:47 +0200)
examples/01.HelloWorld.scm
examples/media/sydney.bmp [new file with mode: 0644]
examples/media/sydney.md2 [new file with mode: 0644]
irrlicht.scm
irrlicht/bindings/core.scm
irrlicht/bindings/scene.scm [new file with mode: 0644]
irrlicht/bindings/video.scm

index 8ad550d4d2cf74659b3069d10922056cb2f7a246..45761def8d40abd414dd72ebdd7e2f9433b96c94 100644 (file)
                   '(10 10 260 22)
                   #t)
 
+;; load a Quake2 model
+(define mesh (get-mesh scene-manager "examples/media/sydney.md2"))
+(when (not mesh)
+  (device-drop! device)
+  (exit #f))
+
+(define node (add-animated-mesh-scene-node scene-manager mesh))
+(when node
+  (set-material-flag-am! node 'lighting #f)
+  (set-md2-animation! node 'stand)
+  (set-material-texture-am! node 0 (get-texture driver "examples/media/sydney.bmp")))
+
+;; place camera
+(add-camera-scene-node scene-manager #:position '(0 30 -40) #:lookat '(0 5 0))
+
 ;; draw everything
 (while (device-run? device)
   (begin-scene driver #t #t '(255 100 101 140))
+  (scene-draw-all scene-manager)
   (gui-draw-all gui-env)
   (end-scene driver))
 
diff --git a/examples/media/sydney.bmp b/examples/media/sydney.bmp
new file mode 100644 (file)
index 0000000..2f14a5a
Binary files /dev/null and b/examples/media/sydney.bmp differ
diff --git a/examples/media/sydney.md2 b/examples/media/sydney.md2
new file mode 100644 (file)
index 0000000..7d3521d
Binary files /dev/null and b/examples/media/sydney.md2 differ
index 1946da1c6b5c21c3b4247ba2f45393843ae49eaa..1863e9ac19544022982aa073950028449471dad1 100644 (file)
@@ -24,6 +24,7 @@
   #:use-module ((irrlicht bindings) #:prefix ffi:)
   #:use-module ((irrlicht bindings core) #:prefix ffi-core:)
   #:use-module ((irrlicht bindings gui) #:prefix ffi-gui:)
+  #:use-module ((irrlicht bindings scene) #:prefix ffi-scene:)
   #:use-module ((irrlicht bindings video) #:prefix ffi-video:)
   #:export (;; device
             create-device
             ;; driver
             begin-scene
             end-scene
+            get-texture
             ;; gui
             add-static-text!
-            gui-draw-all))
+            gui-draw-all
+            ;; scene
+            add-animated-mesh-scene-node
+            add-camera-scene-node
+            get-mesh
+            scene-draw-all
+            set-material-flag-am!
+            set-material-texture-am!
+            set-md2-animation!))
 
 ;; Device functions
 (define* (create-device #:optional
 (define (end-scene driver)
   (ffi-video:end-scene driver))
 
+(define (get-texture driver filename)
+  (ffi-video:get-texture driver (string->pointer filename)))
+
 
 ;; GUI functions
 (define* (add-static-text! gui-env text rectangle
 
 (define (gui-draw-all gui-env)
   (ffi-gui:draw-all gui-env))
+
+
+;; Scene functions
+(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)
+               (if also-add-if-mesh-pointer-zero 1 0))))
+    (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
+                 (if make-active 1 0))))
+    (if (null-pointer? camera) #f camera)))
+
+(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
+     (if newvalue 1 0))))
+
+(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)))
index c82b72834b89ad92b1d3734179ebf811c7c16d2b..6d9a5799b850d995d5d3c3b795ff20eea2463cd6 100644 (file)
@@ -28,3 +28,7 @@
 ;; rect struct
 (define-public rect
   (list int32 int32 int32 int32))
+
+;; vector3df struct
+(define-public vector3df
+  (list float float float))
diff --git a/irrlicht/bindings/scene.scm b/irrlicht/bindings/scene.scm
new file mode 100644 (file)
index 0000000..2cf4568
--- /dev/null
@@ -0,0 +1,91 @@
+;;; guile-irrlicht --- FFI bindings for Irrlicht Engine
+;;; Copyright (C) 2019 Javier Sancho <jsf@jsancho.org>
+;;;
+;;; This file is part of guile-irrlicht.
+;;;
+;;; Guile-irrlicht is free software; you can redistribute it and/or modify
+;;; it under the terms of the GNU Lesser General Public License as
+;;; published by the Free Software Foundation; either version 3 of the
+;;; License, or (at your option) any later version.
+;;;
+;;; Guile-irrlicht 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
+;;; General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU Lesser General Public
+;;; License along with guile-irrlicht.  If not, see
+;;; <http://www.gnu.org/licenses/>.
+
+
+(define-module (irrlicht bindings scene)
+  #:use-module (system foreign))
+
+(define cirr (dynamic-link "libCIrrlicht"))
+
+;;  irr_scene_EMD2_ANIMATION_TYPE enum
+(define-public EMAT_STAND                 0)
+(define-public EMAT_RUN                   1)
+(define-public EMAT_ATTACK                2)
+(define-public EMAT_PAIN_A                3)
+(define-public EMAT_PAIN_B                4)
+(define-public EMAT_PAIN_C                5)
+(define-public EMAT_JUMP                  6)
+(define-public EMAT_FLIP                  7)
+(define-public EMAT_SALUTE                8)
+(define-public EMAT_FALLBACK              9)
+(define-public EMAT_WAVE                 10)
+(define-public EMAT_POINT                11)
+(define-public EMAT_CROUCH_STAND         12)
+(define-public EMAT_CROUCH_WALK          13)
+(define-public EMAT_CROUCH_ATTACK        14)
+(define-public EMAT_CROUCH_PAIN          15)
+(define-public EMAT_CROUCH_DEATH         16)
+(define-public EMAT_DEATH_FALLBACK       17)
+(define-public EMAT_DEATH_FALLFORWARD    18)
+(define-public EMAT_DEATH_FALLBACKSLOW   19)
+(define-public EMAT_BOOM                 20)
+(define-public EMAT_COUNT                21)
+
+;; Scene functions
+(define-public add-animated-mesh-scene-node
+  (pointer->procedure
+   '*
+   (dynamic-func "irr_scene_addAnimatedMeshSceneNode" cirr)
+   (list '* '* '* int '* '* '* int)))
+
+(define-public add-camera-scene-node
+  (pointer->procedure
+   '*
+   (dynamic-func "irr_scene_addCameraSceneNode" cirr)
+   (list '* '* '* '* int int)))
+
+(define-public draw-all
+  (pointer->procedure
+   void
+   (dynamic-func "irr_scene_drawAll" cirr)
+   (list '*)))
+
+(define-public get-mesh
+  (pointer->procedure
+   '*
+   (dynamic-func "irr_scene_getMesh" cirr)
+   (list '* '*)))
+
+(define-public set-material-flag-am
+  (pointer->procedure
+   void
+   (dynamic-func "irr_scene_setMaterialFlagAM" cirr)
+   (list '* int int)))
+
+(define-public set-material-texture-am
+  (pointer->procedure
+   void
+   (dynamic-func "irr_scene_setMaterialTextureAM" cirr)
+   (list '* int '*)))
+
+(define-public set-md2-animation
+  (pointer->procedure
+   void
+   (dynamic-func "irr_scene_setMD2Animation" cirr)
+   (list '* int)))
index d64e558d6281967b0b6ec4d6fd3af86ac4d8f3c0..47b05d1c24cf046d64b232708ea6b792835ef42c 100644 (file)
 (define-public EDT_OPENGL          5)
 (define-public EDT_COUNT           6)
 
+;; irr_video_E_MATERIAL_FLAG enum
+(define-public EMF_WIREFRAME             #x1)
+(define-public EMF_POINTCLOUD            #x2)
+(define-public EMF_GOURAUD_SHADING       #x4)
+(define-public EMF_LIGHTING              #x8)
+(define-public EMF_ZBUFFER              #x10)
+(define-public EMF_ZWRITE_ENABLE        #x20)
+(define-public EMF_BACK_FACE_CULLING    #x40)
+(define-public EMF_FRONT_FACE_CULLING   #x80)
+(define-public EMF_BILINEAR_FILTER     #x100)
+(define-public EMF_TRILINEAR_FILTER    #x200)
+(define-public EMF_ANISOTROPIC_FILTER  #x400)
+(define-public EMF_FOG_ENABLE          #x800)
+(define-public EMF_NORMALIZE_NORMALS  #x1000)
+(define-public EMF_TEXTURE_WRAP       #x2000)
+(define-public EMF_ANTI_ALIASING      #x4000)
+(define-public EMF_COLOR_MASK         #x8000)
+(define-public EMF_COLOR_MATERIAL    #x10000)
+(define-public EMF_USE_MIP_MAPS      #x20000)
+(define-public EMF_BLEND_OPERATION   #x40000)
+(define-public EMF_POLYGON_OFFSET    #x80000)
+
+
 ;; scolor struct
 (define-public scolor
   (list uint32 uint32 uint32 uint32))
@@ -48,3 +71,9 @@
    int
    (dynamic-func "irr_video_endScene" cirr)
    (list '*)))
+
+(define-public get-texture
+  (pointer->procedure
+   '*
+   (dynamic-func "irr_video_getTexture" cirr)
+   (list '* '*)))