]> git.jsancho.org Git - guile-irrlicht.git/blob - src/scene-manager.cpp
create-rotation-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-custom-scene-node!", 5, 0, 1,
53                         (scm_t_subr)irr_scene_addCustomSceneNode);
54     scm_c_define_gsubr ("add-octree-scene-node!", 2, 0, 1,
55                         (scm_t_subr)irr_scene_addOctreeSceneNode);
56     scm_c_define_gsubr ("create-rotation-animator", 2, 0, 0,
57                         (scm_t_subr)irr_scene_createRotationAnimator);
58     scm_c_define_gsubr ("get-mesh", 2, 0, 0, (scm_t_subr)irr_scene_getMesh);
59     scm_c_define_gsubr ("get-root-scene-node", 1, 0, 0, (scm_t_subr)irr_scene_getRootSceneNode);
60     scm_c_define_gsubr ("get-scene-manager", 1, 0, 0, (scm_t_subr)irr_getSceneManager);
61     scm_c_export ("add-animated-mesh-scene-node!", "add-camera-scene-node!",
62                   "add-camera-scene-node-fps!", "add-custom-scene-node!",
63                   "add-octree-scene-node!", "create-rotation-animator",
64                   "get-mesh", "get-root-scene-node",
65                   "get-scene-manager", NULL);
66   }
67
68   DEFINE_WRAPPED_TYPE (irr::scene::ISceneManager*, "scene-manager",
69                        init_scene_manager_type, scene_manager_p,
70                        wrap_scene_manager, unwrap_scene_manager);
71
72   SCM
73   irr_scene_addAnimatedMeshSceneNode (SCM wrapped_scene_manager,
74                                       SCM mesh,
75                                       SCM rest)
76   {
77     SCM parent = scm_from_bool (0);
78     SCM id = scm_from_int32 (-1);
79     SCM position = scm_list_3 (scm_from_double (0),
80                                scm_from_double (0),
81                                scm_from_double (0));
82     SCM rotation = scm_list_3 (scm_from_double (0),
83                                scm_from_double (0),
84                                scm_from_double (0));
85     SCM scale = scm_list_3 (scm_from_double (1),
86                             scm_from_double (1),
87                             scm_from_double (1));
88     SCM also_add_if_mesh_pointer_zero = scm_from_bool (0);
89
90     scm_c_bind_keyword_arguments ("add-animated-mesh-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
91                                   scm_from_utf8_keyword ("parent"), &parent,
92                                   scm_from_utf8_keyword ("id"), &id,
93                                   scm_from_utf8_keyword ("position"), &position,
94                                   scm_from_utf8_keyword ("rotation"), &rotation,
95                                   scm_from_utf8_keyword ("scale"), &scale,
96                                   scm_from_utf8_keyword ("also-add-if-mesh-pointer-zero"), &also_add_if_mesh_pointer_zero,
97                                   SCM_UNDEFINED);
98
99     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
100     irr::scene::IAnimatedMeshSceneNode* node =
101       smgr->addAnimatedMeshSceneNode (unwrap_animated_mesh (mesh),
102                                       scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
103                                       scm_to_int32 (id),
104                                       scm_to_vector3df (position),
105                                       scm_to_vector3df (rotation),
106                                       scm_to_vector3df (scale),
107                                       scm_to_bool (also_add_if_mesh_pointer_zero));
108     return wrap_animated_mesh_scene_node (node);
109   }
110
111   SCM
112   irr_scene_addCameraSceneNode (SCM wrapped_scene_manager,
113                                 SCM rest)
114   {
115     SCM parent = scm_from_bool (0);
116     SCM position = scm_list_3 (scm_from_double (0),
117                                scm_from_double (0),
118                                scm_from_double (0));
119     SCM lookat = scm_list_3 (scm_from_double (0),
120                              scm_from_double (0),
121                              scm_from_double (100));
122     SCM id = scm_from_int32 (-1);
123     SCM make_active = scm_from_bool (1);
124
125     scm_c_bind_keyword_arguments ("add-camera-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
126                                   scm_from_utf8_keyword ("parent"), &parent,
127                                   scm_from_utf8_keyword ("position"), &position,
128                                   scm_from_utf8_keyword ("lookat"), &lookat,
129                                   scm_from_utf8_keyword ("id"), &id,
130                                   scm_from_utf8_keyword ("make-active"), &make_active,
131                                   SCM_UNDEFINED);
132
133     irr::scene::ISceneManager* scene_manager = unwrap_scene_manager (wrapped_scene_manager);
134     irr::scene::ICameraSceneNode* camera =
135       scene_manager->addCameraSceneNode (scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
136                                          scm_to_vector3df (position),
137                                          scm_to_vector3df (lookat),
138                                          scm_to_int32 (id),
139                                          scm_to_bool (make_active));
140     return wrap_camera_scene_node (camera);
141   }
142
143   SCM
144   irr_scene_addCameraSceneNodeFPS (SCM wrapped_scene_manager,
145                                    SCM rest)
146   {
147     SCM parent = scm_from_bool (0);
148     SCM rotate_speed = scm_from_double (100);
149     SCM move_speed = scm_from_double (0.5);
150     SCM id = scm_from_int32 (-1);
151     SCM key_map_array = scm_from_bool (0);
152     SCM key_map_size = scm_from_int32 (0);
153     SCM no_vertical_movement = scm_from_bool (0);
154     SCM jump_speed = scm_from_double (0);
155     SCM invert_mouse = scm_from_bool (0);
156     SCM make_active = scm_from_bool (1);
157     
158     scm_c_bind_keyword_arguments ("add-camera-scene-node-fps!", rest, (scm_t_keyword_arguments_flags)0,
159                                   scm_from_utf8_keyword ("parent"), &parent,
160                                   scm_from_utf8_keyword ("rotate-speed"), &rotate_speed,
161                                   scm_from_utf8_keyword ("move-speed"), &move_speed,
162                                   scm_from_utf8_keyword ("id"), &id,
163                                   scm_from_utf8_keyword ("key-map-array"), &key_map_array,
164                                   scm_from_utf8_keyword ("key-map-size"), &key_map_size,
165                                   scm_from_utf8_keyword ("no-vertical-movement"), &no_vertical_movement,
166                                   scm_from_utf8_keyword ("jump-speed"), &jump_speed,
167                                   scm_from_utf8_keyword ("invert-mouse"), &invert_mouse,
168                                   scm_from_utf8_keyword ("make-active"), &make_active,
169                                   SCM_UNDEFINED);
170
171     irr::scene::ISceneManager* scene_manager = unwrap_scene_manager (wrapped_scene_manager);
172     irr::scene::ICameraSceneNode* camera =
173       scene_manager->addCameraSceneNodeFPS (scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
174                                             scm_to_double (rotate_speed),
175                                             scm_to_double (move_speed),
176                                             scm_to_int32 (id),
177                                             scm_is_false (key_map_array) ? 0 : unwrap_keymap (key_map_array),
178                                             scm_to_int32 (key_map_size),
179                                             scm_to_bool (no_vertical_movement),
180                                             scm_to_double (jump_speed),
181                                             scm_to_bool (invert_mouse),
182                                             scm_to_bool (make_active));
183     return wrap_camera_scene_node (camera);
184   }
185
186   SCM
187   irr_scene_addCustomSceneNode (SCM wrapped_scene_manager,
188                                 SCM proc_render,
189                                 SCM proc_get_bounding_box,
190                                 SCM proc_get_material_count,
191                                 SCM proc_get_material,
192                                 SCM rest)
193   {
194     irr::scene::ISceneManager* scene_manager = unwrap_scene_manager (wrapped_scene_manager);
195
196     SCM parent = wrap_scene_node (scene_manager->getRootSceneNode ());
197     SCM id = scm_from_int32 (-1);
198     SCM position = scm_list_3 (scm_from_double (0),
199                                scm_from_double (0),
200                                scm_from_double (0));
201     SCM rotation = scm_list_3 (scm_from_double (0),
202                                scm_from_double (0),
203                                scm_from_double (0));
204     SCM scale = scm_list_3 (scm_from_double (1),
205                             scm_from_double (1),
206                             scm_from_double (1));
207
208     scm_c_bind_keyword_arguments ("add-custom-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
209                                   scm_from_utf8_keyword ("parent"), &parent,
210                                   scm_from_utf8_keyword ("id"), &id,
211                                   scm_from_utf8_keyword ("position"), &position,
212                                   scm_from_utf8_keyword ("rotation"), &rotation,
213                                   scm_from_utf8_keyword ("scale"), &scale,
214                                   SCM_UNDEFINED);
215
216     class CustomSceneNode : public irr::scene::ISceneNode
217     {
218       SCM scm_render;
219       SCM scm_get_bounding_box;
220       SCM scm_get_material_count;
221       SCM scm_get_material;
222
223     public:
224       CustomSceneNode (irr::scene::ISceneNode* parent,
225                        irr::scene::ISceneManager* smgr,
226                        irr::s32 id,
227                        const irr::core::vector3df& position,
228                        const irr::core::vector3df& rotation,
229                        const irr::core::vector3df& scale,
230                        SCM render,
231                        SCM get_bounding_box,
232                        SCM get_material_count,
233                        SCM get_material)
234         : irr::scene::ISceneNode (parent, smgr, id, position, rotation, scale)
235       {
236         scm_render = render;
237         scm_get_bounding_box = get_bounding_box;
238         scm_get_material_count = get_material_count;
239         scm_get_material = get_material;
240       }
241
242       virtual void OnRegisterSceneNode ()
243       {
244         if (IsVisible)
245           {
246             SceneManager->registerNodeForRendering (this);
247           }
248         ISceneNode::OnRegisterSceneNode ();
249       }
250
251       virtual void render ()
252       {
253         scm_call_0 (scm_render);
254       }
255
256       virtual const irr::core::aabbox3d<irr::f32>& getBoundingBox () const
257       {
258         SCM box = scm_call_0 (scm_get_bounding_box);
259         return *(unwrap_box3d (box));
260       }
261
262       virtual irr::u32 getMaterialCount () const
263       {
264         return scm_to_uint32 (scm_call_0 (scm_get_material_count));
265       }
266
267       virtual irr::video::SMaterial& getMaterial (irr::u32 i)
268       {
269         SCM material = scm_call_1 (scm_get_material, scm_from_uint32 (i));
270         return *(unwrap_material (material));
271       }
272     };
273
274     CustomSceneNode* node =
275       new CustomSceneNode (unwrap_scene_node (parent),
276                            scene_manager,
277                            scm_to_int32 (id),
278                            scm_to_vector3df (position),
279                            scm_to_vector3df (rotation),
280                            scm_to_vector3df (scale),
281                            proc_render,
282                            proc_get_bounding_box,
283                            proc_get_material_count,
284                            proc_get_material);
285     return wrap_scene_node (node);
286   }
287
288   SCM
289   irr_scene_addOctreeSceneNode (SCM wrapped_scene_manager,
290                                 SCM wrapped_mesh,
291                                 SCM rest)
292   {
293     SCM parent = scm_from_bool (0);
294     SCM id = scm_from_int32 (-1);
295     SCM minimal_polys_per_node = scm_from_int32 (256);
296     SCM also_add_if_mesh_pointer_zero = scm_from_bool (0);
297
298     scm_c_bind_keyword_arguments ("add-octree-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
299                                   scm_from_utf8_keyword ("parent"), &parent,
300                                   scm_from_utf8_keyword ("id"), &id,
301                                   scm_from_utf8_keyword ("minimal-polys-per-node"), &minimal_polys_per_node,
302                                   scm_from_utf8_keyword ("also-add-if-mesh-pointer-zero"), &also_add_if_mesh_pointer_zero,
303                                   SCM_UNDEFINED);
304
305     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
306     irr::scene::IMeshSceneNode* node;
307     if (animated_mesh_p (wrapped_mesh))
308       {
309         node = smgr->addOctreeSceneNode (unwrap_animated_mesh (wrapped_mesh),
310                                          scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
311                                          scm_to_int32 (id),
312                                          scm_to_int32 (minimal_polys_per_node),
313                                          scm_to_bool (also_add_if_mesh_pointer_zero));
314       }
315     else
316       {
317         node = smgr->addOctreeSceneNode (unwrap_mesh (wrapped_mesh),
318                                          scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
319                                          scm_to_int32 (id),
320                                          scm_to_int32 (minimal_polys_per_node),
321                                          scm_to_bool (also_add_if_mesh_pointer_zero));
322       }
323     return wrap_mesh_scene_node (node);
324   }
325
326   SCM
327   irr_scene_createRotationAnimator (SCM wrapped_scene_manager,
328                                     SCM rotation_speed)
329   {
330     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
331     irr::scene::ISceneNodeAnimator* anim =
332       smgr->createRotationAnimator(scm_to_vector3df (rotation_speed));
333     return wrap_scene_node_animator (anim);
334   }
335
336   SCM
337   irr_scene_getMesh (SCM wrapped_scene_manager,
338                      SCM filename)
339   {
340     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
341     irr::scene::IAnimatedMesh* mesh = smgr->getMesh(scm_to_utf8_stringn (filename, NULL));
342     return wrap_animated_mesh (mesh);
343   }
344
345   SCM
346   irr_scene_getRootSceneNode (SCM wrapped_scene_manager)
347   {
348     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
349     return wrap_scene_node (smgr->getRootSceneNode ());
350   }
351
352   SCM
353   irr_getSceneManager (SCM wrapped_obj)
354   {
355     irr::scene::ISceneManager* scene_manager;
356     if (device_p (wrapped_obj))
357       {
358         scene_manager = unwrap_device (wrapped_obj)->getSceneManager ();
359       }
360     else
361       {
362         scm_error (scm_arg_type_key, NULL, "Cannot get scene manager from object: ~S",
363                    scm_list_1 (wrapped_obj), scm_list_1 (wrapped_obj));
364       }
365     return wrap_scene_manager (scene_manager);
366   }
367
368 }