From 6260608f2ca73178ffd580af1c26ee0c424aa047 Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Sun, 1 Mar 2020 19:29:06 +0100 Subject: [PATCH] Custom Scene Node example --- examples/03.CustomSceneNode.scm | 128 ++++++++++++++++++++++++++++++++ irrlicht/bindings/scene.scm | 13 ++++ irrlicht/bindings/video.scm | 18 ++++- irrlicht/video.scm | 59 ++++++++++++++- 4 files changed, 215 insertions(+), 3 deletions(-) create mode 100644 examples/03.CustomSceneNode.scm diff --git a/examples/03.CustomSceneNode.scm b/examples/03.CustomSceneNode.scm new file mode 100644 index 0000000..46e35fd --- /dev/null +++ b/examples/03.CustomSceneNode.scm @@ -0,0 +1,128 @@ +;;; 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 +;;; . + + +;;; Irrlicht 03.CustomSceneNode example +;;; http://irrlicht.sourceforge.net/docu/example003.html + + +(use-modules (irrlicht) + (ice-9 match)) + +;; ask user for driver +(format #t + "Please select the driver you want for this example: + (a) OpenGL 1.5 + (b) Direct3D 9.0c + (c) Direct3D 8.1 + (d) Burning's Software Renderer + (e) Software Renderer + (f) NullDevice + (otherKey) exit~%~%") + +(define device-type (match (read-char) + (#\a 'opengl) + (#\b 'direct3d9) + (#\c 'direct3d8) + (#\d 'burnings) + (#\e 'software) + (#\f 'null) + (_ #f))) + +(when (not device-type) + (exit #f)) + +;; start up the engine +(define device + (create-device + #:device-type device-type + #:window-size '(640 480))) +(when (not device) + (exit #f)) + +;; create engine and camera +(set-window-caption! device "Custom Scene Node - Irrlicht Engine Demo") + +(define my-node #f) +(define driver (get-video-driver device)) +(define scene-manager (get-scene-manager device)) +(add-camera-scene-node! scene-manager + #:position '(0 -40 0) + #:lookat '(0 0 0)) + +;; create our custom scene node +(define box (make-aabbox3df)) +(define vertices + (list (make-s3dvertex '(0 0 10) '(1 1 0) '(255 0 255 255) '(0 1)) + (make-s3dvertex '(10 0 -10) '(1 0 0) '(255 255 0 255) '(1 1)) + (make-s3dvertex '(0 20 0) '(0 1 1) '(255 255 255 0) '(1 0)) + (make-s3dvertex '(-10 0 -10) '(0 0 1) '(255 0 255 0) '(0 0)))) +(define material (make-material #:wireframe #f #:lighting #f)) + +(aabbox3d-reset! box (vertex-position (car vertices))) +(for-each (lambda (vertex) + (aabbox3d-add-internal-point! box (vertex-position vertex))) + (cdr vertices)) + +(define (custom-render) + (let ((indices '((0 2 3) (2 1 3) (1 0 3) (2 0 1)))) + ;;(set-material! driver material) + ;;(set-transform! driver 'world (get-absolute-transformation my-node)) + ;;(draw-vertex-primitive-list driver vertices indices))) + #f)) + +(define (custom-get-bounding-box) + box) + +(define (custom-get-material-count) + 1) + +(define (custom-get-material i) + material) + +(set! my-node (add-custom-scene-node! scene-manager + custom-render + custom-get-bounding-box + custom-get-material-count + custom-get-material + #:parent (get-root-scene-node scene-manager))) + +;; add rotation +(let ((anim (create-rotation-animator scene-manager '(0.8 0 0.8)))) + (add-animator! my-node anim)) + +;; loop +(define frames 0) +(while (device-run? device) + (begin-scene driver #:color '(0 100 100 100)) + (scene-draw-all scene-manager) + (end-scene driver) + + (set! frames (+ frames 1)) + (when (= frames 100) + (let ((fps (get-fps driver)) + (driver-name (get-video-driver-name driver))) + (let ((caption + (format #f "Irrlicht Engine [~a] FPS:~a" driver-name fps))) + (set-window-caption! device caption))) + (set! frames 0))) + +;; delete device +(device-drop! device) +(exit #t) diff --git a/irrlicht/bindings/scene.scm b/irrlicht/bindings/scene.scm index cbb75a4..ad2646b 100644 --- a/irrlicht/bindings/scene.scm +++ b/irrlicht/bindings/scene.scm @@ -46,6 +46,19 @@ (define-public EMAT_BOOM 20) (define-public EMAT_COUNT 21) +;; irr_scene_E_PRIMITIVE_TYPE enum +(define-public EPT_POINTS 0) +(define-public EPT_LINE_STRIP 1) +(define-public EPT_LINE_LOOP 2) +(define-public EPT_LINES 3) +(define-public EPT_TRIANGLE_STRIP 4) +(define-public EPT_TRIANGLE_FAN 5) +(define-public EPT_TRIANGLES 6) +(define-public EPT_QUAD_STRIP 7) +(define-public EPT_QUADS 8) +(define-public EPT_POLYGON 9) +(define-public EPT_POINT_SPRITES 10) + ;; Scene functions (define-foreign add-animated-mesh-scene-node '* "irr_scene_addAnimatedMeshSceneNode" (list '* '* '* int '* '* '* int)) diff --git a/irrlicht/bindings/video.scm b/irrlicht/bindings/video.scm index d9f92fa..de37133 100644 --- a/irrlicht/bindings/video.scm +++ b/irrlicht/bindings/video.scm @@ -147,6 +147,14 @@ (define-public ETS_TEXTURE_7 10) (define-public ETS_COUNT 11) +;; irr_video_E_VERTEX_TYPE enum +(define-public EVT_STANDARD 0) +(define-public EVT_2TCOORDS 1) +(define-public EVT_TANGENTS 2) + +;; irr_video_E_INDEX_TYPE enum +(define-public EIT_16BIT 0) +(define-public EIT_32BIT 1) ;; scolor struct (define-public scolor @@ -159,6 +167,9 @@ (define-foreign begin-scene int "irr_video_beginScene" (list '* int int '* '* '*)) +(define-foreign draw-vertex-primitive-list + void "irr_video_drawVertexPrimitiveList" (list '* '* int '* int int int int)) + (define-foreign end-scene int "irr_video_endScene" (list '*)) @@ -225,9 +236,9 @@ (bit-field-group (uint8 4) ; colorMask:4 (uint8 3) ; colorMaterial:3 - (uint32 4) ; blendOperation:4 + (uint16 4) ; blendOperation:4 (uint8 3) ; polygonOffsetFactor:3 - (uint32 1) ; polygonOffsetDirection:1 + (uint16 1) ; polygonOffsetDirection:1 (uint8 1) ; wireframe:1 (uint8 1) ; pointCloud:1 (uint8 1) ; gouraudShading:1 @@ -239,3 +250,6 @@ (uint8 1) ; normalizeNormals:1 (uint8 1) ; useMipMaps:1 ))) + +(define-foreign make-c-material + '* "makeMaterial" (list)) diff --git a/irrlicht/video.scm b/irrlicht/video.scm index dc61f64..0c334fe 100644 --- a/irrlicht/video.scm +++ b/irrlicht/video.scm @@ -22,10 +22,12 @@ #: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) #:use-module (irrlicht util foreign) #:export (begin-scene + draw-vertex-primitive-list end-scene get-fps get-texture @@ -52,6 +54,57 @@ %null-pointer (ffi-core:rect->pointer source-rect)))) +(define* (draw-vertex-primitive-list driver vertices index-list + #:key + (v-type 'standard) + (p-type 'triangles)) + (define (make-c-vertices vertices) + (let ((vals (map (lambda (vertex) + (parse-c-struct (ffi-video:s3dvertex->pointer vertex) + ffi-video:s3dvertex)) + vertices)) + (types (make-list (length vertices) ffi-video:s3dvertex))) + (make-c-struct types vals))) + + (define (make-c-indices indices) + (let* ((vals (apply append indices)) + (types (make-list (length vals) int32))) + (make-c-struct types vals))) + + (let ((vertices-pointer (make-c-vertices vertices)) + (vertex-count (length vertices)) + (indices-pointer (make-c-indices index-list)) + (prim-count (length index-list)) + (vertex-type + (match v-type + ('standard ffi-video:EVT_STANDARD) + ('2tcoords ffi-video:EVT_2TCOORDS) + ('tangents ffi-video:EVT_TANGENTS))) + (primitive-type + (match p-type + ('points ffi-scene:EPT_POINTS) + ('strip ffi-scene:EPT_LINE_STRIP) + ('line-loop ffi-scene:EPT_LINE_LOOP) + ('lines ffi-scene:EPT_LINES) + ('triangle-strip ffi-scene:EPT_TRIANGLE_STRIP) + ('triangle-fan ffi-scene:EPT_TRIANGLE_FAN) + ('triangles ffi-scene:EPT_TRIANGLES) + ('quad-strip ffi-scene:EPT_QUAD_STRIP) + ('quads ffi-scene:EPT_QUADS) + ('polygon ffi-scene:EPT_POLYGON) + ('point-sprites ffi-scene:EPT_POINT_SPRITES)))) + + + (ffi-video:draw-vertex-primitive-list + driver + vertices-pointer + vertex-count + indices-pointer + prim-count + vertex-type + primitive-type + ffi-video:EIT_32BIT))) + (define (end-scene driver) (ffi-video:end-scene driver)) @@ -139,4 +192,8 @@ (bool->integer #t) ; useMipMaps )))) (ffi-video:pointer->smaterial - (make-c-struct+ ffi-video:smaterial material)))) + ;; (make-c-struct+ ffi-video:smaterial material)))) + (make-c-material)))) + +(define-public (make-c-material) + (ffi-video:make-c-material)) -- 2.39.5