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