]> git.jsancho.org Git - guile-irrlicht.git/blob - irrlicht/scene.scm
Transformation
[guile-irrlicht.git] / irrlicht / scene.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 (define-module (irrlicht scene)
22   #:use-module (ice-9 match)
23   #:use-module (system foreign)
24   #:use-module ((irrlicht bindings core) #:prefix ffi-core:)
25   #:use-module ((irrlicht bindings scene) #:prefix ffi-scene:)
26   #:use-module ((irrlicht bindings video) #:prefix ffi-video:)
27   #:use-module (irrlicht util)
28   #:export (add-animated-mesh-scene-node
29             add-animator!
30             add-camera-scene-node!
31             add-camera-scene-node-fps!
32             add-custom-scene-node!
33             add-octree-scene-node
34             create-rotation-animator
35             get-absolute-transformation
36             get-mesh
37             get-root-scene-node
38             scene-draw-all
39             set-material-flag!
40             set-material-texture!
41             set-md2-animation!
42             set-position!))
43
44 (define* (add-animated-mesh-scene-node scene-manager mesh
45                                        #:key
46                                        (parent %null-pointer)
47                                        (id -1)
48                                        (position '(0 0 0))
49                                        (rotation '(0 0 0))
50                                        (scale '(1 1 1))
51                                        (also-add-if-mesh-pointer-zero #f))
52   (let ((node (ffi-scene:add-animated-mesh-scene-node
53                scene-manager
54                mesh
55                parent
56                id
57                (ffi-core:vector3df->pointer position)
58                (ffi-core:vector3df->pointer rotation)
59                (ffi-core:vector3df->pointer scale)
60                (bool->integer also-add-if-mesh-pointer-zero))))
61     (if (null-pointer? node) #f node)))
62
63 (define (add-animator! node animator)
64   (ffi-scene:add-animator node animator))
65
66 (define* (add-camera-scene-node! scene-manager
67                                  #:key
68                                  (parent %null-pointer)
69                                  (position '(0 0 0))
70                                  (lookat '(0 0 100))
71                                  (id -1)
72                                  (make-active #t))
73   (let ((camera (ffi-scene:add-camera-scene-node
74                  scene-manager
75                  parent
76                  (ffi-core:vector3df->pointer position)
77                  (ffi-core:vector3df->pointer lookat)
78                  id
79                  (bool->integer make-active))))
80     (if (null-pointer? camera) #f camera)))
81
82 (define* (add-camera-scene-node-fps! scene-manager
83                                      #:key
84                                      (parent %null-pointer)
85                                      (rotate-speed 100.0)
86                                      (move-speed 0.5)
87                                      (id -1)
88                                      (key-map-array %null-pointer)
89                                      (key-map-size 0)
90                                      (no-vertical-movement #f)
91                                      (jump-speed 0.0)
92                                      (invert-mouse #f)
93                                      (make-active #t))
94   (ffi-scene:add-camera-scene-node-fps
95    scene-manager
96    parent
97    rotate-speed
98    move-speed
99    id
100    key-map-array
101    key-map-size
102    (bool->integer no-vertical-movement)
103    jump-speed
104    (bool->integer invert-mouse)
105    (bool->integer make-active)))
106
107 (define* (add-custom-scene-node! scene-manager
108                                  render
109                                  get-bounding-box
110                                  get-material-count
111                                  get-material
112                                  #:key
113                                  (parent %null-pointer)
114                                  (id -1)
115                                  (position '(0 0 0))
116                                  (rotation '(0 0 0))
117                                  (scale '(1 1 1)))
118   (let ((c-get-bounding-box
119          (lambda ()
120            (ffi-core:aabbox3df->pointer (get-bounding-box))))
121         (c-get-material
122          (lambda (i)
123            (ffi-video:smaterial->pointer (get-material i)))))
124     (ffi-scene:add-custom-scene-node
125      scene-manager
126      parent
127      id
128      (ffi-core:vector3df->pointer position)
129      (ffi-core:vector3df->pointer rotation)
130      (ffi-core:vector3df->pointer scale)
131      (procedure->pointer void render '())
132      (procedure->pointer '* c-get-bounding-box '())
133      (procedure->pointer uint32 get-material-count '())
134      (procedure->pointer '* c-get-material (list uint32)))))
135
136 (define* (add-octree-scene-node scene-manager mesh
137                                 #:key
138                                 (parent %null-pointer)
139                                 (id -1)
140                                 (minimal-polys-per-node 512)
141                                 (also-add-if-mesh-pointer-zero #f))
142   (ffi-scene:add-octree-scene-node
143    scene-manager
144    mesh
145    parent
146    id
147    minimal-polys-per-node
148    (bool->integer also-add-if-mesh-pointer-zero)))
149
150 (define (create-rotation-animator scene-manager rotation-speed)
151   (let ((animator (ffi-scene:create-rotation-animator
152                    scene-manager
153                    (ffi-core:vector3df->pointer rotation-speed))))
154     (if (null-pointer? animator) #f animator)))
155
156 (define (get-absolute-transformation node)
157   (ffi-scene:get-absolute-transformation node))
158
159 (define (get-mesh scene-manager filename)
160   (let ((mesh (ffi-scene:get-mesh scene-manager (string->pointer filename))))
161     (if (null-pointer? mesh) #f mesh)))
162
163 (define (get-root-scene-node scene-manager)
164   (ffi-scene:get-root-scene-node scene-manager))
165
166 (define (scene-draw-all scene-manager)
167   (ffi-scene:draw-all scene-manager))
168
169 (define (set-material-flag! node flag newvalue)
170   (let ((material-flag
171          (match flag
172                 ('wireframe ffi-video:EMF_WIREFRAME)
173                 ('pointcloud ffi-video:EMF_POINTCLOUD)
174                 ('gouraud-shading ffi-video:EMF_GOURAUD_SHADING)
175                 ('lighting ffi-video:EMF_LIGHTING)
176                 ('zbuffer ffi-video:EMF_ZBUFFER)
177                 ('zwrite-enable ffi-video:EMF_ZWRITE_ENABLE)
178                 ('back-face-culling ffi-video:EMF_BACK_FACE_CULLING)
179                 ('front-face-culling ffi-video:EMF_FRONT_FACE_CULLING)
180                 ('bilinear-filter ffi-video:EMF_BILINEAR_FILTER)
181                 ('trilinear-filter ffi-video:EMF_TRILINEAR_FILTER)
182                 ('anisotropic-filter ffi-video:EMF_ANISOTROPIC_FILTER)
183                 ('fog-enable ffi-video:EMF_FOG_ENABLE)
184                 ('normalize-normals ffi-video:EMF_NORMALIZE_NORMALS)
185                 ('texture-wrap ffi-video:EMF_TEXTURE_WRAP)
186                 ('anti-aliasing ffi-video:EMF_ANTI_ALIASING)
187                 ('color-mask ffi-video:EMF_COLOR_MASK)
188                 ('color-material ffi-video:EMF_COLOR_MATERIAL)
189                 ('use-mip-maps ffi-video:EMF_USE_MIP_MAPS)
190                 ('blend-operation ffi-video:EMF_BLEND_OPERATION)
191                 ('polygon-offset ffi-video:EMF_POLYGON_OFFSET))))
192     (ffi-scene:set-material-flag
193      node
194      material-flag
195      (bool->integer newvalue))))
196
197 (define (set-material-texture! node texture-layer texture)
198   (ffi-scene:set-material-texture node texture-layer texture))
199
200 (define (set-md2-animation! node anim)
201   (let ((animation-type
202          (match anim
203                 ('stand ffi-scene:EMAT_STAND)
204                 ('run ffi-scene:EMAT_RUN)
205                 ('attack ffi-scene:EMAT_ATTACK)
206                 ('pain-a ffi-scene:EMAT_PAIN_A)
207                 ('pain-b ffi-scene:EMAT_PAIN_B)
208                 ('pain-c ffi-scene:EMAT_PAIN_C)
209                 ('jump ffi-scene:EMAT_JUMP)
210                 ('flip ffi-scene:EMAT_FLIP)
211                 ('salute ffi-scene:EMAT_SALUTE)
212                 ('fallback ffi-scene:EMAT_FALLBACK)
213                 ('wave ffi-scene:EMAT_WAVE)
214                 ('point ffi-scene:EMAT_POINT)
215                 ('crouch-stand ffi-scene:EMAT_CROUCH_STAND)
216                 ('crouch-walk ffi-scene:EMAT_CROUCH_WALK)
217                 ('crouch-attack ffi-scene:EMAT_CROUCH_ATTACK)
218                 ('crouch-pain ffi-scene:EMAT_CROUCH_PAIN)
219                 ('crouch-death ffi-scene:EMAT_CROUCH_DEATH)
220                 ('death-fallback ffi-scene:EMAT_DEATH_FALLBACK)
221                 ('death-fallforward ffi-scene:EMAT_DEATH_FALLFORWARD)
222                 ('death-fallbackslow ffi-scene:EMAT_DEATH_FALLBACKSLOW)
223                 ('boom ffi-scene:EMAT_BOOM)
224                 ('count ffi-scene:EMAT_COUNT))))
225     (ffi-scene:set-md2-animation
226      node
227      animation-type)))
228
229 (define (set-position! node newpos)
230   (ffi-scene:set-position
231    node
232    (ffi-core:vector3df->pointer newpos)))