]> git.jsancho.org Git - guile-irrlicht.git/blob - src/scene-manager.cpp
SCM booleans
[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 "gsubr.h"
31 #include "keymap.h"
32 #include "material.h"
33 #include "mesh.h"
34 #include "mesh-scene-node.h"
35 #include "scene-manager.h"
36 #include "scene-node.h"
37 #include "scene-node-animator.h"
38 #include "vector3d.h"
39 #include "wrapped.h"
40
41 extern "C" {
42
43   void
44   init_scene_manager (void)
45   {
46     init_scene_manager_type ();
47     DEFINE_GSUBR ("add-animated-mesh-scene-node!", 2, 0, 1, irr_scene_addAnimatedMeshSceneNode);
48     DEFINE_GSUBR ("add-camera-scene-node!", 1, 0, 1, irr_scene_addCameraSceneNode);
49     DEFINE_GSUBR ("add-camera-scene-node-fps!", 1, 0, 1, irr_scene_addCameraSceneNodeFPS);
50     DEFINE_GSUBR ("add-cube-scene-node!", 1, 0, 1, irr_scene_addCubeSceneNode);
51     DEFINE_GSUBR ("add-custom-scene-node!", 5, 0, 1, irr_scene_addCustomSceneNode);
52     DEFINE_GSUBR ("add-octree-scene-node!", 2, 0, 1, irr_scene_addOctreeSceneNode);
53     DEFINE_GSUBR ("add-sphere-scene-node!", 1, 0, 1, irr_scene_addSphereSceneNode);
54     DEFINE_GSUBR ("create-fly-circle-animator", 1, 0, 1, irr_scene_createFlyCircleAnimator);
55     DEFINE_GSUBR ("create-fly-straight-animator", 4, 0, 1, irr_scene_createFlyStraightAnimator);
56     DEFINE_GSUBR ("create-rotation-animator", 2, 0, 0, irr_scene_createRotationAnimator);
57     DEFINE_GSUBR ("get-mesh", 2, 0, 0, irr_scene_getMesh);
58     DEFINE_GSUBR ("get-root-scene-node", 1, 0, 0, irr_scene_getRootSceneNode);
59     DEFINE_GSUBR ("get-scene-manager", 1, 0, 0, irr_getSceneManager);
60   }
61
62   DEFINE_WRAPPED_TYPE (irr::scene::ISceneManager*, "scene-manager",
63                        init_scene_manager_type, scene_manager_p,
64                        wrap_scene_manager, unwrap_scene_manager);
65
66   SCM
67   irr_scene_addAnimatedMeshSceneNode (SCM wrapped_scene_manager,
68                                       SCM mesh,
69                                       SCM rest)
70   {
71     SCM parent = SCM_BOOL_F;
72     SCM id = scm_from_int32 (-1);
73     SCM position = scm_list_3 (scm_from_double (0),
74                                scm_from_double (0),
75                                scm_from_double (0));
76     SCM rotation = scm_list_3 (scm_from_double (0),
77                                scm_from_double (0),
78                                scm_from_double (0));
79     SCM scale = scm_list_3 (scm_from_double (1),
80                             scm_from_double (1),
81                             scm_from_double (1));
82     SCM also_add_if_mesh_pointer_zero = SCM_BOOL_F;
83
84     scm_c_bind_keyword_arguments ("add-animated-mesh-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
85                                   scm_from_utf8_keyword ("parent"), &parent,
86                                   scm_from_utf8_keyword ("id"), &id,
87                                   scm_from_utf8_keyword ("position"), &position,
88                                   scm_from_utf8_keyword ("rotation"), &rotation,
89                                   scm_from_utf8_keyword ("scale"), &scale,
90                                   scm_from_utf8_keyword ("also-add-if-mesh-pointer-zero"), &also_add_if_mesh_pointer_zero,
91                                   SCM_UNDEFINED);
92
93     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
94     irr::scene::IAnimatedMeshSceneNode* node =
95       smgr->addAnimatedMeshSceneNode (unwrap_animated_mesh (mesh),
96                                       scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
97                                       scm_to_int32 (id),
98                                       scm_to_vector3df (position),
99                                       scm_to_vector3df (rotation),
100                                       scm_to_vector3df (scale),
101                                       scm_to_bool (also_add_if_mesh_pointer_zero));
102     return wrap_animated_mesh_scene_node (node);
103   }
104
105   SCM
106   irr_scene_addCameraSceneNode (SCM wrapped_scene_manager,
107                                 SCM rest)
108   {
109     SCM parent = SCM_BOOL_F;
110     SCM position = scm_list_3 (scm_from_double (0),
111                                scm_from_double (0),
112                                scm_from_double (0));
113     SCM lookat = scm_list_3 (scm_from_double (0),
114                              scm_from_double (0),
115                              scm_from_double (100));
116     SCM id = scm_from_int32 (-1);
117     SCM make_active = SCM_BOOL_T;
118
119     scm_c_bind_keyword_arguments ("add-camera-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
120                                   scm_from_utf8_keyword ("parent"), &parent,
121                                   scm_from_utf8_keyword ("position"), &position,
122                                   scm_from_utf8_keyword ("lookat"), &lookat,
123                                   scm_from_utf8_keyword ("id"), &id,
124                                   scm_from_utf8_keyword ("make-active"), &make_active,
125                                   SCM_UNDEFINED);
126
127     irr::scene::ISceneManager* scene_manager = unwrap_scene_manager (wrapped_scene_manager);
128     irr::scene::ICameraSceneNode* camera =
129       scene_manager->addCameraSceneNode (scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
130                                          scm_to_vector3df (position),
131                                          scm_to_vector3df (lookat),
132                                          scm_to_int32 (id),
133                                          scm_to_bool (make_active));
134     return wrap_camera_scene_node (camera);
135   }
136
137   SCM
138   irr_scene_addCameraSceneNodeFPS (SCM wrapped_scene_manager,
139                                    SCM rest)
140   {
141     SCM parent = SCM_BOOL_F;
142     SCM rotate_speed = scm_from_double (100);
143     SCM move_speed = scm_from_double (0.5);
144     SCM id = scm_from_int32 (-1);
145     SCM key_map_array = SCM_BOOL_F;
146     SCM key_map_size = scm_from_int32 (0);
147     SCM no_vertical_movement = SCM_BOOL_F;
148     SCM jump_speed = scm_from_double (0);
149     SCM invert_mouse = SCM_BOOL_F;
150     SCM make_active = SCM_BOOL_T;
151     
152     scm_c_bind_keyword_arguments ("add-camera-scene-node-fps!", rest, (scm_t_keyword_arguments_flags)0,
153                                   scm_from_utf8_keyword ("parent"), &parent,
154                                   scm_from_utf8_keyword ("rotate-speed"), &rotate_speed,
155                                   scm_from_utf8_keyword ("move-speed"), &move_speed,
156                                   scm_from_utf8_keyword ("id"), &id,
157                                   scm_from_utf8_keyword ("key-map-array"), &key_map_array,
158                                   scm_from_utf8_keyword ("key-map-size"), &key_map_size,
159                                   scm_from_utf8_keyword ("no-vertical-movement"), &no_vertical_movement,
160                                   scm_from_utf8_keyword ("jump-speed"), &jump_speed,
161                                   scm_from_utf8_keyword ("invert-mouse"), &invert_mouse,
162                                   scm_from_utf8_keyword ("make-active"), &make_active,
163                                   SCM_UNDEFINED);
164
165     irr::scene::ISceneManager* scene_manager = unwrap_scene_manager (wrapped_scene_manager);
166     irr::scene::ICameraSceneNode* camera =
167       scene_manager->addCameraSceneNodeFPS (scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
168                                             scm_to_double (rotate_speed),
169                                             scm_to_double (move_speed),
170                                             scm_to_int32 (id),
171                                             scm_is_false (key_map_array) ? 0 : unwrap_keymap (key_map_array),
172                                             scm_to_int32 (key_map_size),
173                                             scm_to_bool (no_vertical_movement),
174                                             scm_to_double (jump_speed),
175                                             scm_to_bool (invert_mouse),
176                                             scm_to_bool (make_active));
177     return wrap_camera_scene_node (camera);
178   }
179
180   SCM
181   irr_scene_addCubeSceneNode (SCM wrapped_scene_manager,
182                               SCM rest)
183   {
184     SCM size = scm_from_double (10);
185     SCM parent = SCM_BOOL_F;
186     SCM id = scm_from_int32 (-1);
187     SCM position = scm_list_3 (scm_from_double (0),
188                                scm_from_double (0),
189                                scm_from_double (0));
190     SCM rotation = scm_list_3 (scm_from_double (0),
191                                scm_from_double (0),
192                                scm_from_double (0));
193     SCM scale = scm_list_3 (scm_from_double (1),
194                             scm_from_double (1),
195                             scm_from_double (1));
196
197     scm_c_bind_keyword_arguments ("add-cube-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
198                                   scm_from_utf8_keyword ("size"), &size,
199                                   scm_from_utf8_keyword ("parent"), &parent,
200                                   scm_from_utf8_keyword ("id"), &id,
201                                   scm_from_utf8_keyword ("position"), &position,
202                                   scm_from_utf8_keyword ("rotation"), &rotation,
203                                   scm_from_utf8_keyword ("scale"), &scale,
204                                   SCM_UNDEFINED);
205
206     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
207     irr::scene::IMeshSceneNode* node =
208       smgr->addCubeSceneNode (scm_to_double (size),
209                               scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
210                               scm_to_int32 (id),
211                               scm_to_vector3df (position),
212                               scm_to_vector3df (rotation),
213                               scm_to_vector3df (scale));
214     return wrap_mesh_scene_node (node);
215   }
216
217   SCM
218   irr_scene_addCustomSceneNode (SCM wrapped_scene_manager,
219                                 SCM proc_render,
220                                 SCM proc_get_bounding_box,
221                                 SCM proc_get_material_count,
222                                 SCM proc_get_material,
223                                 SCM rest)
224   {
225     irr::scene::ISceneManager* scene_manager = unwrap_scene_manager (wrapped_scene_manager);
226
227     SCM parent = wrap_scene_node (scene_manager->getRootSceneNode ());
228     SCM id = scm_from_int32 (-1);
229     SCM position = scm_list_3 (scm_from_double (0),
230                                scm_from_double (0),
231                                scm_from_double (0));
232     SCM rotation = scm_list_3 (scm_from_double (0),
233                                scm_from_double (0),
234                                scm_from_double (0));
235     SCM scale = scm_list_3 (scm_from_double (1),
236                             scm_from_double (1),
237                             scm_from_double (1));
238
239     scm_c_bind_keyword_arguments ("add-custom-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
240                                   scm_from_utf8_keyword ("parent"), &parent,
241                                   scm_from_utf8_keyword ("id"), &id,
242                                   scm_from_utf8_keyword ("position"), &position,
243                                   scm_from_utf8_keyword ("rotation"), &rotation,
244                                   scm_from_utf8_keyword ("scale"), &scale,
245                                   SCM_UNDEFINED);
246
247     class CustomSceneNode : public irr::scene::ISceneNode
248     {
249       SCM scm_render;
250       SCM scm_get_bounding_box;
251       SCM scm_get_material_count;
252       SCM scm_get_material;
253
254     public:
255       CustomSceneNode (irr::scene::ISceneNode* parent,
256                        irr::scene::ISceneManager* smgr,
257                        irr::s32 id,
258                        const irr::core::vector3df& position,
259                        const irr::core::vector3df& rotation,
260                        const irr::core::vector3df& scale,
261                        SCM render,
262                        SCM get_bounding_box,
263                        SCM get_material_count,
264                        SCM get_material)
265         : irr::scene::ISceneNode (parent, smgr, id, position, rotation, scale)
266       {
267         scm_render = render;
268         scm_get_bounding_box = get_bounding_box;
269         scm_get_material_count = get_material_count;
270         scm_get_material = get_material;
271       }
272
273       virtual void OnRegisterSceneNode ()
274       {
275         if (IsVisible)
276           {
277             SceneManager->registerNodeForRendering (this);
278           }
279         ISceneNode::OnRegisterSceneNode ();
280       }
281
282       virtual void render ()
283       {
284         scm_call_0 (scm_render);
285       }
286
287       virtual const irr::core::aabbox3d<irr::f32>& getBoundingBox () const
288       {
289         SCM box = scm_call_0 (scm_get_bounding_box);
290         return *(unwrap_box3d (box));
291       }
292
293       virtual irr::u32 getMaterialCount () const
294       {
295         return scm_to_uint32 (scm_call_0 (scm_get_material_count));
296       }
297
298       virtual irr::video::SMaterial& getMaterial (irr::u32 i)
299       {
300         SCM material = scm_call_1 (scm_get_material, scm_from_uint32 (i));
301         return *(unwrap_material (material));
302       }
303     };
304
305     CustomSceneNode* node =
306       new CustomSceneNode (unwrap_scene_node (parent),
307                            scene_manager,
308                            scm_to_int32 (id),
309                            scm_to_vector3df (position),
310                            scm_to_vector3df (rotation),
311                            scm_to_vector3df (scale),
312                            proc_render,
313                            proc_get_bounding_box,
314                            proc_get_material_count,
315                            proc_get_material);
316     return wrap_scene_node (node);
317   }
318
319   SCM
320   irr_scene_addOctreeSceneNode (SCM wrapped_scene_manager,
321                                 SCM wrapped_mesh,
322                                 SCM rest)
323   {
324     SCM parent = SCM_BOOL_F;
325     SCM id = scm_from_int32 (-1);
326     SCM minimal_polys_per_node = scm_from_int32 (256);
327     SCM also_add_if_mesh_pointer_zero = SCM_BOOL_F;
328
329     scm_c_bind_keyword_arguments ("add-octree-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
330                                   scm_from_utf8_keyword ("parent"), &parent,
331                                   scm_from_utf8_keyword ("id"), &id,
332                                   scm_from_utf8_keyword ("minimal-polys-per-node"), &minimal_polys_per_node,
333                                   scm_from_utf8_keyword ("also-add-if-mesh-pointer-zero"), &also_add_if_mesh_pointer_zero,
334                                   SCM_UNDEFINED);
335
336     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
337     irr::scene::IMeshSceneNode* node;
338     if (animated_mesh_p (wrapped_mesh))
339       {
340         node = smgr->addOctreeSceneNode (unwrap_animated_mesh (wrapped_mesh),
341                                          scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
342                                          scm_to_int32 (id),
343                                          scm_to_int32 (minimal_polys_per_node),
344                                          scm_to_bool (also_add_if_mesh_pointer_zero));
345       }
346     else
347       {
348         node = smgr->addOctreeSceneNode (unwrap_mesh (wrapped_mesh),
349                                          scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
350                                          scm_to_int32 (id),
351                                          scm_to_int32 (minimal_polys_per_node),
352                                          scm_to_bool (also_add_if_mesh_pointer_zero));
353       }
354     return wrap_mesh_scene_node (node);
355   }
356
357   SCM
358   irr_scene_addSphereSceneNode (SCM wrapped_scene_manager,
359                                 SCM rest)
360   {
361     SCM radius = scm_from_double (5.0);
362     SCM poly_count = scm_from_int32 (16);
363     SCM parent = SCM_BOOL_F;
364     SCM id = scm_from_int32 (-1);
365     SCM position = scm_list_3 (scm_from_double (0),
366                                scm_from_double (0),
367                                scm_from_double (0));
368     SCM rotation = scm_list_3 (scm_from_double (0),
369                                scm_from_double (0),
370                                scm_from_double (0));
371     SCM scale = scm_list_3 (scm_from_double (1),
372                             scm_from_double (1),
373                             scm_from_double (1));
374
375     scm_c_bind_keyword_arguments ("add-sphere-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
376                                   scm_from_utf8_keyword ("radius"), &radius,
377                                   scm_from_utf8_keyword ("poly-count"), &poly_count,
378                                   scm_from_utf8_keyword ("parent"), &parent,
379                                   scm_from_utf8_keyword ("id"), &id,
380                                   scm_from_utf8_keyword ("position"), &position,
381                                   scm_from_utf8_keyword ("rotation"), &rotation,
382                                   scm_from_utf8_keyword ("scale"), &scale,
383                                   SCM_UNDEFINED);
384
385     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
386     irr::scene::IMeshSceneNode* node =
387       smgr->addSphereSceneNode (scm_to_double (radius),
388                                 scm_to_int32 (poly_count),
389                                 scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
390                                 scm_to_int32 (id),
391                                 scm_to_vector3df (position),
392                                 scm_to_vector3df (rotation),
393                                 scm_to_vector3df (scale));
394     return wrap_mesh_scene_node (node);
395   }
396
397   SCM
398   irr_scene_createFlyCircleAnimator (SCM wrapped_scene_manager,
399                                      SCM rest)
400   {
401     SCM center = scm_list_3 (scm_from_double (0),
402                              scm_from_double (0),
403                              scm_from_double (0));
404     SCM radius = scm_from_double (100);
405     SCM speed = scm_from_double (0.001);
406     SCM direction = scm_list_3 (scm_from_double (0),
407                                 scm_from_double (1),
408                                 scm_from_double (0));
409     SCM start_position = scm_from_double (0);
410     SCM radius_ellipsoid = scm_from_double (0);
411
412     scm_c_bind_keyword_arguments ("create-fly-circle-animator", rest, (scm_t_keyword_arguments_flags)0,
413                                   scm_from_utf8_keyword ("center"), &center,
414                                   scm_from_utf8_keyword ("radius"), &radius,
415                                   scm_from_utf8_keyword ("speed"), &speed,
416                                   scm_from_utf8_keyword ("direction"), &direction,
417                                   scm_from_utf8_keyword ("start-position"), &start_position,
418                                   scm_from_utf8_keyword ("radius-ellipsoid"), &radius_ellipsoid,
419                                   SCM_UNDEFINED);
420
421     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
422     irr::scene::ISceneNodeAnimator* anim =
423       smgr->createFlyCircleAnimator (scm_to_vector3df (center),
424                                      scm_to_double (radius),
425                                      scm_to_double (speed),
426                                      scm_to_vector3df (direction),
427                                      scm_to_double (start_position),
428                                      scm_to_double (radius_ellipsoid));
429     return wrap_scene_node_animator (anim);
430   }
431
432   SCM
433   irr_scene_createFlyStraightAnimator (SCM wrapped_scene_manager,
434                                        SCM start_point,
435                                        SCM end_point,
436                                        SCM time_for_way,
437                                        SCM rest)
438   {
439     SCM loop = SCM_BOOL_F;
440     SCM pingpong = SCM_BOOL_F;
441
442     scm_c_bind_keyword_arguments ("create-fly-straight-animator", rest, (scm_t_keyword_arguments_flags)0,
443                                   scm_from_utf8_keyword ("loop"), &loop,
444                                   scm_from_utf8_keyword ("pingpong"), &pingpong,
445                                   SCM_UNDEFINED);
446
447     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
448     irr::scene::ISceneNodeAnimator* anim =
449       smgr->createFlyStraightAnimator (scm_to_vector3df (start_point),
450                                        scm_to_vector3df (end_point),
451                                        scm_to_uint32 (time_for_way),
452                                        scm_to_bool (loop),
453                                        scm_to_bool (pingpong));
454     return wrap_scene_node_animator (anim);
455   }
456
457   SCM
458   irr_scene_createRotationAnimator (SCM wrapped_scene_manager,
459                                     SCM rotation_speed)
460   {
461     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
462     irr::scene::ISceneNodeAnimator* anim =
463       smgr->createRotationAnimator (scm_to_vector3df (rotation_speed));
464     return wrap_scene_node_animator (anim);
465   }
466
467   SCM
468   irr_scene_getMesh (SCM wrapped_scene_manager,
469                      SCM filename)
470   {
471     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
472     irr::scene::IAnimatedMesh* mesh = smgr->getMesh(scm_to_utf8_stringn (filename, NULL));
473     return wrap_animated_mesh (mesh);
474   }
475
476   SCM
477   irr_scene_getRootSceneNode (SCM wrapped_scene_manager)
478   {
479     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
480     return wrap_scene_node (smgr->getRootSceneNode ());
481   }
482
483   SCM
484   irr_getSceneManager (SCM wrapped_obj)
485   {
486     irr::scene::ISceneManager* scene_manager;
487     if (device_p (wrapped_obj))
488       {
489         scene_manager = unwrap_device (wrapped_obj)->getSceneManager ();
490       }
491     else
492       {
493         scm_error (scm_arg_type_key, NULL, "Cannot get scene manager from object: ~S",
494                    scm_list_1 (wrapped_obj), scm_list_1 (wrapped_obj));
495       }
496     return wrap_scene_manager (scene_manager);
497   }
498
499 }