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