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