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