]> git.jsancho.org Git - guile-irrlicht.git/blob - examples/03-custom-scene-node.scm
get-position add-internal-point!
[guile-irrlicht.git] / examples / 03-custom-scene-node.scm
1 ;;; guile-irrlicht --- FFI bindings for Irrlicht Engine
2 ;;; Copyright (C) 2019 Javier Sancho <jsf@jsancho.org>
3 ;;;
4 ;;; This file is part of guile-irrlicht.
5 ;;;
6 ;;; Guile-irrlicht is free software; you can redistribute it and/or modify
7 ;;; it under the terms of the GNU Lesser General Public License as
8 ;;; published by the Free Software Foundation; either version 3 of the
9 ;;; License, or (at your option) any later version.
10 ;;;
11 ;;; Guile-irrlicht is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ;;; General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU Lesser General Public
17 ;;; License along with guile-irrlicht.  If not, see
18 ;;; <http://www.gnu.org/licenses/>.
19
20
21 ;;; Irrlicht 03.CustomSceneNode example
22 ;;; http://irrlicht.sourceforge.net/docu/example003.html
23
24
25 (use-modules (irrlicht)
26              (ice-9 match))
27
28 ;; ask user for driver
29 (format #t
30         "Please select the driver you want for this example:
31  (a) OpenGL 1.5
32  (b) Direct3D 9.0c
33  (c) Direct3D 8.1
34  (d) Burning's Software Renderer
35  (e) Software Renderer
36  (f) NullDevice
37  (otherKey) exit~%~%")
38
39 (define device-type (match (read-char)
40                            (#\a 'opengl)
41                            (#\b 'direct3d9)
42                            (#\c 'direct3d8)
43                            (#\d 'burnings)
44                            (#\e 'software)
45                            (#\f 'null)
46                            (_ #f)))
47
48 (when (not device-type)
49   (exit #f))
50
51 ;; start up the engine
52 (define device
53   (create-device
54    #:device-type device-type
55    #:window-size '(640 480)))
56 (when (not device)
57   (exit #f))
58
59 ;; create engine and camera
60 (set-window-caption! device "Custom Scene Node - Irrlicht Engine Demo")
61
62 (define my-node #f)
63 (define driver (get-video-driver device))
64 (define scene-manager (get-scene-manager device))
65 (add-camera-scene-node! scene-manager
66                         #:position '(0 -40 0)
67                         #:lookat '(0 0 0))
68
69 ;; create our custom scene node
70 (define box (make-box3d))
71 (define vertices
72   (list (make-vertex3d '(0 0 10) '(1 1 0) '(255 0 255 255) '(0 1))
73         (make-vertex3d '(10 0 -10) '(1 0 0) '(255 255 0 255) '(1 1))
74         (make-vertex3d '(0 20 0) '(0 1 1) '(255 255 255 0) '(1 0))
75         (make-vertex3d '(-10 0 -10) '(0 0 1) '(255 0 255 0) '(0 0))))
76 (define material (make-material #:wireframe #f #:lighting #f))
77
78 (reset-box3d! box (get-position (car vertices)))
79 (for-each
80  (lambda (vertex)
81    (add-internal-point! box (get-position vertex)))
82  (cdr vertices))
83
84 (define (custom-render)
85   (let ((indices '((0 2 3) (2 1 3) (1 0 3) (2 0 1))))
86     (set-material! driver material)
87     (set-transform! driver 'world (get-absolute-transformation my-node))
88     (draw-vertex-primitive-list driver vertices indices)))
89
90 (define (custom-get-bounding-box)
91   box)
92
93 (define (custom-get-material-count)
94   1)
95
96 (define (custom-get-material i)
97   material)
98
99 (set! my-node (add-custom-scene-node! scene-manager
100                                       custom-render
101                                       custom-get-bounding-box
102                                       custom-get-material-count
103                                       custom-get-material
104                                       #:parent (get-root-scene-node scene-manager)))
105
106 ;; add rotation
107 (let ((anim (create-rotation-animator scene-manager '(0.8 0 0.8))))
108   (add-animator! my-node anim))
109
110 ;; loop
111 (define frames 0)
112 (while (run device)
113   (begin-scene driver #:color '(0 100 100 100))
114   (draw-all scene-manager)
115   (end-scene driver)
116
117   (set! frames (+ frames 1))
118   (when (= frames 100)
119     (let ((fps (get-fps driver))
120           (driver-name (get-name driver)))
121       (let ((caption
122              (format #f "Irrlicht Engine [~a] FPS:~a" driver-name fps)))
123         (set-window-caption! device caption)))
124     (set! frames 0)))
125
126 ;; delete device
127 (drop! device)
128 (exit #t)