From: Javier Sancho Date: Fri, 18 Oct 2019 17:47:30 +0000 (+0200) Subject: HelloWorld example with all the functions needed X-Git-Url: https://git.jsancho.org/?a=commitdiff_plain;h=84886d64879f76a593b79327836b5d251d23af53;p=guile-irrlicht.git HelloWorld example with all the functions needed --- diff --git a/examples/01.HelloWorld.scm b/examples/01.HelloWorld.scm index 8ad550d..45761de 100644 --- a/examples/01.HelloWorld.scm +++ b/examples/01.HelloWorld.scm @@ -41,9 +41,25 @@ '(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 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 index 0000000..7d3521d Binary files /dev/null and b/examples/media/sydney.md2 differ diff --git a/irrlicht.scm b/irrlicht.scm index 1946da1..1863e9a 100644 --- a/irrlicht.scm +++ b/irrlicht.scm @@ -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 @@ -36,9 +37,18 @@ ;; 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 @@ -102,6 +112,9 @@ (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 @@ -122,3 +135,107 @@ (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))) diff --git a/irrlicht/bindings/core.scm b/irrlicht/bindings/core.scm index c82b728..6d9a579 100644 --- a/irrlicht/bindings/core.scm +++ b/irrlicht/bindings/core.scm @@ -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 index 0000000..2cf4568 --- /dev/null +++ b/irrlicht/bindings/scene.scm @@ -0,0 +1,91 @@ +;;; guile-irrlicht --- FFI bindings for Irrlicht Engine +;;; Copyright (C) 2019 Javier Sancho +;;; +;;; 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 +;;; . + + +(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))) diff --git a/irrlicht/bindings/video.scm b/irrlicht/bindings/video.scm index d64e558..47b05d1 100644 --- a/irrlicht/bindings/video.scm +++ b/irrlicht/bindings/video.scm @@ -32,6 +32,29 @@ (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 '* '*)))