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