]> git.jsancho.org Git - guile-irrlicht.git/blob - src/scene-manager.cpp
f7a2b12f094104c9b106c96622e5fe7bdeca5454
[guile-irrlicht.git] / src / scene-manager.cpp
1 /* guile-irrlicht --- GNU Guile bindings for Irrlicht Engine
2
3    Copyright (C) 2020 Javier Sancho <jsf@jsancho.org>
4
5    This file is part of guile-irrlicht.
6
7    guile-irrlicht is free software; you can redistribute it and/or modify
8    it under the terms of the GNU Lesser General Public License as
9    published by the Free Software Foundation; either version 3 of the
10    License, or (at your option) any later version.
11
12    guile-irrlicht is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU Lesser General Public
18    License along with guile-irrlicht. If not, see
19    <http://www.gnu.org/licenses/>.
20 */
21
22 #include <irrlicht/irrlicht.h>
23 #include <libguile.h>
24
25 #include "animated-mesh.h"
26 #include "animated-mesh-scene-node.h"
27 #include "box3d.h"
28 #include "camera-scene-node.h"
29 #include "device.h"
30 #include "keymap.h"
31 #include "material.h"
32 #include "mesh.h"
33 #include "mesh-scene-node.h"
34 #include "scene-manager.h"
35 #include "scene-node.h"
36 #include "scene-node-animator.h"
37 #include "vector3d.h"
38 #include "wrapped.h"
39
40 extern "C" {
41
42   void
43   init_scene_manager (void)
44   {
45     init_scene_manager_type ();
46     scm_c_define_gsubr ("add-animated-mesh-scene-node!", 2, 0, 1,
47                         (scm_t_subr)irr_scene_addAnimatedMeshSceneNode);
48     scm_c_define_gsubr ("add-camera-scene-node!", 1, 0, 1,
49                         (scm_t_subr)irr_scene_addCameraSceneNode);
50     scm_c_define_gsubr ("add-camera-scene-node-fps!", 1, 0, 1,
51                         (scm_t_subr)irr_scene_addCameraSceneNodeFPS);
52     scm_c_define_gsubr ("add-cube-scene-node!", 1, 0, 1,
53                         (scm_t_subr)irr_scene_addCubeSceneNode);
54     scm_c_define_gsubr ("add-custom-scene-node!", 5, 0, 1,
55                         (scm_t_subr)irr_scene_addCustomSceneNode);
56     scm_c_define_gsubr ("add-octree-scene-node!", 2, 0, 1,
57                         (scm_t_subr)irr_scene_addOctreeSceneNode);
58     scm_c_define_gsubr ("add-sphere-scene-node!", 1, 0, 1,
59                         (scm_t_subr)irr_scene_addSphereSceneNode);
60     scm_c_define_gsubr ("create-rotation-animator", 2, 0, 0,
61                         (scm_t_subr)irr_scene_createRotationAnimator);
62     scm_c_define_gsubr ("get-mesh", 2, 0, 0, (scm_t_subr)irr_scene_getMesh);
63     scm_c_define_gsubr ("get-root-scene-node", 1, 0, 0, (scm_t_subr)irr_scene_getRootSceneNode);
64     scm_c_define_gsubr ("get-scene-manager", 1, 0, 0, (scm_t_subr)irr_getSceneManager);
65     scm_c_export ("add-animated-mesh-scene-node!", "add-camera-scene-node!",
66                   "add-camera-scene-node-fps!", "add-custom-scene-node!", "add-cube-scene-node!",
67                   "add-octree-scene-node!", "add-sphere-scene-node!", "create-rotation-animator",
68                   "get-mesh", "get-root-scene-node", "get-scene-manager", NULL);
69   }
70
71   DEFINE_WRAPPED_TYPE (irr::scene::ISceneManager*, "scene-manager",
72                        init_scene_manager_type, scene_manager_p,
73                        wrap_scene_manager, unwrap_scene_manager);
74
75   SCM
76   irr_scene_addAnimatedMeshSceneNode (SCM wrapped_scene_manager,
77                                       SCM mesh,
78                                       SCM rest)
79   {
80     SCM parent = scm_from_bool (0);
81     SCM id = scm_from_int32 (-1);
82     SCM position = scm_list_3 (scm_from_double (0),
83                                scm_from_double (0),
84                                scm_from_double (0));
85     SCM rotation = scm_list_3 (scm_from_double (0),
86                                scm_from_double (0),
87                                scm_from_double (0));
88     SCM scale = scm_list_3 (scm_from_double (1),
89                             scm_from_double (1),
90                             scm_from_double (1));
91     SCM also_add_if_mesh_pointer_zero = scm_from_bool (0);
92
93     scm_c_bind_keyword_arguments ("add-animated-mesh-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
94                                   scm_from_utf8_keyword ("parent"), &parent,
95                                   scm_from_utf8_keyword ("id"), &id,
96                                   scm_from_utf8_keyword ("position"), &position,
97                                   scm_from_utf8_keyword ("rotation"), &rotation,
98                                   scm_from_utf8_keyword ("scale"), &scale,
99                                   scm_from_utf8_keyword ("also-add-if-mesh-pointer-zero"), &also_add_if_mesh_pointer_zero,
100                                   SCM_UNDEFINED);
101
102     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
103     irr::scene::IAnimatedMeshSceneNode* node =
104       smgr->addAnimatedMeshSceneNode (unwrap_animated_mesh (mesh),
105                                       scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
106                                       scm_to_int32 (id),
107                                       scm_to_vector3df (position),
108                                       scm_to_vector3df (rotation),
109                                       scm_to_vector3df (scale),
110                                       scm_to_bool (also_add_if_mesh_pointer_zero));
111     return wrap_animated_mesh_scene_node (node);
112   }
113
114   SCM
115   irr_scene_addCameraSceneNode (SCM wrapped_scene_manager,
116                                 SCM rest)
117   {
118     SCM parent = scm_from_bool (0);
119     SCM position = scm_list_3 (scm_from_double (0),
120                                scm_from_double (0),
121                                scm_from_double (0));
122     SCM lookat = scm_list_3 (scm_from_double (0),
123                              scm_from_double (0),
124                              scm_from_double (100));
125     SCM id = scm_from_int32 (-1);
126     SCM make_active = scm_from_bool (1);
127
128     scm_c_bind_keyword_arguments ("add-camera-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
129                                   scm_from_utf8_keyword ("parent"), &parent,
130                                   scm_from_utf8_keyword ("position"), &position,
131                                   scm_from_utf8_keyword ("lookat"), &lookat,
132                                   scm_from_utf8_keyword ("id"), &id,
133                                   scm_from_utf8_keyword ("make-active"), &make_active,
134                                   SCM_UNDEFINED);
135
136     irr::scene::ISceneManager* scene_manager = unwrap_scene_manager (wrapped_scene_manager);
137     irr::scene::ICameraSceneNode* camera =
138       scene_manager->addCameraSceneNode (scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
139                                          scm_to_vector3df (position),
140                                          scm_to_vector3df (lookat),
141                                          scm_to_int32 (id),
142                                          scm_to_bool (make_active));
143     return wrap_camera_scene_node (camera);
144   }
145
146   SCM
147   irr_scene_addCameraSceneNodeFPS (SCM wrapped_scene_manager,
148                                    SCM rest)
149   {
150     SCM parent = scm_from_bool (0);
151     SCM rotate_speed = scm_from_double (100);
152     SCM move_speed = scm_from_double (0.5);
153     SCM id = scm_from_int32 (-1);
154     SCM key_map_array = scm_from_bool (0);
155     SCM key_map_size = scm_from_int32 (0);
156     SCM no_vertical_movement = scm_from_bool (0);
157     SCM jump_speed = scm_from_double (0);
158     SCM invert_mouse = scm_from_bool (0);
159     SCM make_active = scm_from_bool (1);
160     
161     scm_c_bind_keyword_arguments ("add-camera-scene-node-fps!", rest, (scm_t_keyword_arguments_flags)0,
162                                   scm_from_utf8_keyword ("parent"), &parent,
163                                   scm_from_utf8_keyword ("rotate-speed"), &rotate_speed,
164                                   scm_from_utf8_keyword ("move-speed"), &move_speed,
165                                   scm_from_utf8_keyword ("id"), &id,
166                                   scm_from_utf8_keyword ("key-map-array"), &key_map_array,
167                                   scm_from_utf8_keyword ("key-map-size"), &key_map_size,
168                                   scm_from_utf8_keyword ("no-vertical-movement"), &no_vertical_movement,
169                                   scm_from_utf8_keyword ("jump-speed"), &jump_speed,
170                                   scm_from_utf8_keyword ("invert-mouse"), &invert_mouse,
171                                   scm_from_utf8_keyword ("make-active"), &make_active,
172                                   SCM_UNDEFINED);
173
174     irr::scene::ISceneManager* scene_manager = unwrap_scene_manager (wrapped_scene_manager);
175     irr::scene::ICameraSceneNode* camera =
176       scene_manager->addCameraSceneNodeFPS (scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
177                                             scm_to_double (rotate_speed),
178                                             scm_to_double (move_speed),
179                                             scm_to_int32 (id),
180                                             scm_is_false (key_map_array) ? 0 : unwrap_keymap (key_map_array),
181                                             scm_to_int32 (key_map_size),
182                                             scm_to_bool (no_vertical_movement),
183                                             scm_to_double (jump_speed),
184                                             scm_to_bool (invert_mouse),
185                                             scm_to_bool (make_active));
186     return wrap_camera_scene_node (camera);
187   }
188
189   SCM
190   irr_scene_addCubeSceneNode (SCM wrapped_scene_manager,
191                               SCM rest)
192   {
193     SCM size = scm_from_double (10);
194     SCM parent = scm_from_bool (0);
195     SCM id = scm_from_int32 (-1);
196     SCM position = scm_list_3 (scm_from_double (0),
197                                scm_from_double (0),
198                                scm_from_double (0));
199     SCM rotation = scm_list_3 (scm_from_double (0),
200                                scm_from_double (0),
201                                scm_from_double (0));
202     SCM scale = scm_list_3 (scm_from_double (1),
203                             scm_from_double (1),
204                             scm_from_double (1));
205
206     scm_c_bind_keyword_arguments ("add-cube-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
207                                   scm_from_utf8_keyword ("size"), &size,
208                                   scm_from_utf8_keyword ("parent"), &parent,
209                                   scm_from_utf8_keyword ("id"), &id,
210                                   scm_from_utf8_keyword ("position"), &position,
211                                   scm_from_utf8_keyword ("rotation"), &rotation,
212                                   scm_from_utf8_keyword ("scale"), &scale,
213                                   SCM_UNDEFINED);
214
215     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
216     irr::scene::IMeshSceneNode* node =
217       smgr->addCubeSceneNode (scm_to_double (size),
218                               scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
219                               scm_to_int32 (id),
220                               scm_to_vector3df (position),
221                               scm_to_vector3df (rotation),
222                               scm_to_vector3df (scale));
223     return wrap_mesh_scene_node (node);
224   }
225
226   SCM
227   irr_scene_addCustomSceneNode (SCM wrapped_scene_manager,
228                                 SCM proc_render,
229                                 SCM proc_get_bounding_box,
230                                 SCM proc_get_material_count,
231                                 SCM proc_get_material,
232                                 SCM rest)
233   {
234     irr::scene::ISceneManager* scene_manager = unwrap_scene_manager (wrapped_scene_manager);
235
236     SCM parent = wrap_scene_node (scene_manager->getRootSceneNode ());
237     SCM id = scm_from_int32 (-1);
238     SCM position = scm_list_3 (scm_from_double (0),
239                                scm_from_double (0),
240                                scm_from_double (0));
241     SCM rotation = scm_list_3 (scm_from_double (0),
242                                scm_from_double (0),
243                                scm_from_double (0));
244     SCM scale = scm_list_3 (scm_from_double (1),
245                             scm_from_double (1),
246                             scm_from_double (1));
247
248     scm_c_bind_keyword_arguments ("add-custom-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
249                                   scm_from_utf8_keyword ("parent"), &parent,
250                                   scm_from_utf8_keyword ("id"), &id,
251                                   scm_from_utf8_keyword ("position"), &position,
252                                   scm_from_utf8_keyword ("rotation"), &rotation,
253                                   scm_from_utf8_keyword ("scale"), &scale,
254                                   SCM_UNDEFINED);
255
256     class CustomSceneNode : public irr::scene::ISceneNode
257     {
258       SCM scm_render;
259       SCM scm_get_bounding_box;
260       SCM scm_get_material_count;
261       SCM scm_get_material;
262
263     public:
264       CustomSceneNode (irr::scene::ISceneNode* parent,
265                        irr::scene::ISceneManager* smgr,
266                        irr::s32 id,
267                        const irr::core::vector3df& position,
268                        const irr::core::vector3df& rotation,
269                        const irr::core::vector3df& scale,
270                        SCM render,
271                        SCM get_bounding_box,
272                        SCM get_material_count,
273                        SCM get_material)
274         : irr::scene::ISceneNode (parent, smgr, id, position, rotation, scale)
275       {
276         scm_render = render;
277         scm_get_bounding_box = get_bounding_box;
278         scm_get_material_count = get_material_count;
279         scm_get_material = get_material;
280       }
281
282       virtual void OnRegisterSceneNode ()
283       {
284         if (IsVisible)
285           {
286             SceneManager->registerNodeForRendering (this);
287           }
288         ISceneNode::OnRegisterSceneNode ();
289       }
290
291       virtual void render ()
292       {
293         scm_call_0 (scm_render);
294       }
295
296       virtual const irr::core::aabbox3d<irr::f32>& getBoundingBox () const
297       {
298         SCM box = scm_call_0 (scm_get_bounding_box);
299         return *(unwrap_box3d (box));
300       }
301
302       virtual irr::u32 getMaterialCount () const
303       {
304         return scm_to_uint32 (scm_call_0 (scm_get_material_count));
305       }
306
307       virtual irr::video::SMaterial& getMaterial (irr::u32 i)
308       {
309         SCM material = scm_call_1 (scm_get_material, scm_from_uint32 (i));
310         return *(unwrap_material (material));
311       }
312     };
313
314     CustomSceneNode* node =
315       new CustomSceneNode (unwrap_scene_node (parent),
316                            scene_manager,
317                            scm_to_int32 (id),
318                            scm_to_vector3df (position),
319                            scm_to_vector3df (rotation),
320                            scm_to_vector3df (scale),
321                            proc_render,
322                            proc_get_bounding_box,
323                            proc_get_material_count,
324                            proc_get_material);
325     return wrap_scene_node (node);
326   }
327
328   SCM
329   irr_scene_addOctreeSceneNode (SCM wrapped_scene_manager,
330                                 SCM wrapped_mesh,
331                                 SCM rest)
332   {
333     SCM parent = scm_from_bool (0);
334     SCM id = scm_from_int32 (-1);
335     SCM minimal_polys_per_node = scm_from_int32 (256);
336     SCM also_add_if_mesh_pointer_zero = scm_from_bool (0);
337
338     scm_c_bind_keyword_arguments ("add-octree-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
339                                   scm_from_utf8_keyword ("parent"), &parent,
340                                   scm_from_utf8_keyword ("id"), &id,
341                                   scm_from_utf8_keyword ("minimal-polys-per-node"), &minimal_polys_per_node,
342                                   scm_from_utf8_keyword ("also-add-if-mesh-pointer-zero"), &also_add_if_mesh_pointer_zero,
343                                   SCM_UNDEFINED);
344
345     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
346     irr::scene::IMeshSceneNode* node;
347     if (animated_mesh_p (wrapped_mesh))
348       {
349         node = smgr->addOctreeSceneNode (unwrap_animated_mesh (wrapped_mesh),
350                                          scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
351                                          scm_to_int32 (id),
352                                          scm_to_int32 (minimal_polys_per_node),
353                                          scm_to_bool (also_add_if_mesh_pointer_zero));
354       }
355     else
356       {
357         node = smgr->addOctreeSceneNode (unwrap_mesh (wrapped_mesh),
358                                          scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
359                                          scm_to_int32 (id),
360                                          scm_to_int32 (minimal_polys_per_node),
361                                          scm_to_bool (also_add_if_mesh_pointer_zero));
362       }
363     return wrap_mesh_scene_node (node);
364   }
365
366   SCM
367   irr_scene_addSphereSceneNode (SCM wrapped_scene_manager,
368                                 SCM rest)
369   {
370     SCM radius = scm_from_double (5.0);
371     SCM poly_count = scm_from_int32 (16);
372     SCM parent = scm_from_bool (0);
373     SCM id = scm_from_int32 (-1);
374     SCM position = scm_list_3 (scm_from_double (0),
375                                scm_from_double (0),
376                                scm_from_double (0));
377     SCM rotation = scm_list_3 (scm_from_double (0),
378                                scm_from_double (0),
379                                scm_from_double (0));
380     SCM scale = scm_list_3 (scm_from_double (1),
381                             scm_from_double (1),
382                             scm_from_double (1));
383
384     scm_c_bind_keyword_arguments ("add-sphere-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
385                                   scm_from_utf8_keyword ("radius"), &radius,
386                                   scm_from_utf8_keyword ("poly-count"), &poly_count,
387                                   scm_from_utf8_keyword ("parent"), &parent,
388                                   scm_from_utf8_keyword ("id"), &id,
389                                   scm_from_utf8_keyword ("position"), &position,
390                                   scm_from_utf8_keyword ("rotation"), &rotation,
391                                   scm_from_utf8_keyword ("scale"), &scale,
392                                   SCM_UNDEFINED);
393
394     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
395     irr::scene::IMeshSceneNode* node =
396       smgr->addSphereSceneNode (scm_to_double (radius),
397                                 scm_to_int32 (poly_count),
398                                 scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
399                                 scm_to_int32 (id),
400                                 scm_to_vector3df (position),
401                                 scm_to_vector3df (rotation),
402                                 scm_to_vector3df (scale));
403     return wrap_mesh_scene_node (node);
404   }
405
406   SCM
407   irr_scene_createRotationAnimator (SCM wrapped_scene_manager,
408                                     SCM rotation_speed)
409   {
410     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
411     irr::scene::ISceneNodeAnimator* anim =
412       smgr->createRotationAnimator (scm_to_vector3df (rotation_speed));
413     return wrap_scene_node_animator (anim);
414   }
415
416   SCM
417   irr_scene_getMesh (SCM wrapped_scene_manager,
418                      SCM filename)
419   {
420     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
421     irr::scene::IAnimatedMesh* mesh = smgr->getMesh(scm_to_utf8_stringn (filename, NULL));
422     return wrap_animated_mesh (mesh);
423   }
424
425   SCM
426   irr_scene_getRootSceneNode (SCM wrapped_scene_manager)
427   {
428     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
429     return wrap_scene_node (smgr->getRootSceneNode ());
430   }
431
432   SCM
433   irr_getSceneManager (SCM wrapped_obj)
434   {
435     irr::scene::ISceneManager* scene_manager;
436     if (device_p (wrapped_obj))
437       {
438         scene_manager = unwrap_device (wrapped_obj)->getSceneManager ();
439       }
440     else
441       {
442         scm_error (scm_arg_type_key, NULL, "Cannot get scene manager from object: ~S",
443                    scm_list_1 (wrapped_obj), scm_list_1 (wrapped_obj));
444       }
445     return wrap_scene_manager (scene_manager);
446   }
447
448 }