]> git.jsancho.org Git - guile-irrlicht.git/blob - examples/03-custom-scene-node.scm
useless code
[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
57 ;; create engine and camera
58 (set-window-caption! device "Custom Scene Node - Irrlicht Engine Demo")
59
60 (define my-node #f)
61 (define driver (get-video-driver device))
62 (define scene-manager (get-scene-manager device))
63 (add-camera-scene-node! scene-manager
64                         #:position '(0 -40 0)
65                         #:lookat '(0 0 0))
66
67 ;; create our custom scene node
68 (define box (make-box3d))
69 (define vertices
70   (list (make-vertex3d '(0 0 10) '(1 1 0) '(255 0 255 255) '(0 1))
71         (make-vertex3d '(10 0 -10) '(1 0 0) '(255 255 0 255) '(1 1))
72         (make-vertex3d '(0 20 0) '(0 1 1) '(255 255 255 0) '(1 0))
73         (make-vertex3d '(-10 0 -10) '(0 0 1) '(255 0 255 0) '(0 0))))
74 (define material (make-material #:wireframe #f #:lighting #f))
75
76 (reset-box3d! box (get-position (car vertices)))
77 (for-each
78  (lambda (vertex)
79    (add-internal-point! box (get-position vertex)))
80  (cdr vertices))
81
82 (define (custom-render)
83   (let ((indices '((0 2 3) (2 1 3) (1 0 3) (2 0 1))))
84     (set-material! driver material)
85     (set-transform! driver 'world (get-absolute-transformation my-node))
86     (draw-vertex-primitive-list driver vertices indices)))
87
88 (define (custom-get-bounding-box)
89   box)
90
91 (define (custom-get-material-count)
92   1)
93
94 (define (custom-get-material i)
95   material)
96
97 (set! my-node (add-custom-scene-node! scene-manager
98                                       custom-render
99                                       custom-get-bounding-box
100                                       custom-get-material-count
101                                       custom-get-material
102                                       #:parent (get-root-scene-node scene-manager)))
103
104 ;; add rotation
105 (let ((anim (create-rotation-animator scene-manager '(0.8 0 0.8))))
106   (add-animator! my-node anim))
107
108 ;; loop
109 (define frames 0)
110 (while (run device)
111   (begin-scene driver #:color '(0 100 100 100))
112   (draw-all scene-manager)
113   (end-scene driver)
114
115   (set! frames (+ frames 1))
116   (when (= frames 100)
117     (let ((fps (get-fps driver))
118           (driver-name (get-name driver)))
119       (let ((caption
120              (format #f "Irrlicht Engine [~a] FPS:~a" driver-name fps)))
121         (set-window-caption! device caption)))
122     (set! frames 0)))
123
124 ;; delete device
125 (drop! device)
126 (exit #t)