1 /* guile-irrlicht --- GNU Guile bindings for Irrlicht Engine
3 Copyright (C) 2020 Javier Sancho <jsf@jsancho.org>
5 This file is part of guile-irrlicht.
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.
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.
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/>.
22 #include <irrlicht/irrlicht.h>
25 #include "animated-mesh.h"
26 #include "animated-mesh-scene-node.h"
28 #include "camera-scene-node.h"
33 #include "mesh-scene-node.h"
34 #include "scene-manager.h"
35 #include "scene-node.h"
36 #include "scene-node-animator.h"
43 init_scene_manager (void)
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);
76 DEFINE_WRAPPED_TYPE (irr::scene::ISceneManager*, "scene-manager",
77 init_scene_manager_type, scene_manager_p,
78 wrap_scene_manager, unwrap_scene_manager);
81 irr_scene_addAnimatedMeshSceneNode (SCM wrapped_scene_manager,
85 SCM parent = scm_from_bool (0);
86 SCM id = scm_from_int32 (-1);
87 SCM position = scm_list_3 (scm_from_double (0),
90 SCM rotation = scm_list_3 (scm_from_double (0),
93 SCM scale = scm_list_3 (scm_from_double (1),
96 SCM also_add_if_mesh_pointer_zero = scm_from_bool (0);
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,
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),
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);
120 irr_scene_addCameraSceneNode (SCM wrapped_scene_manager,
123 SCM parent = scm_from_bool (0);
124 SCM position = scm_list_3 (scm_from_double (0),
126 scm_from_double (0));
127 SCM lookat = scm_list_3 (scm_from_double (0),
129 scm_from_double (100));
130 SCM id = scm_from_int32 (-1);
131 SCM make_active = scm_from_bool (1);
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,
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),
147 scm_to_bool (make_active));
148 return wrap_camera_scene_node (camera);
152 irr_scene_addCameraSceneNodeFPS (SCM wrapped_scene_manager,
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);
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,
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),
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);
195 irr_scene_addCubeSceneNode (SCM wrapped_scene_manager,
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),
203 scm_from_double (0));
204 SCM rotation = scm_list_3 (scm_from_double (0),
206 scm_from_double (0));
207 SCM scale = scm_list_3 (scm_from_double (1),
209 scm_from_double (1));
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,
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),
225 scm_to_vector3df (position),
226 scm_to_vector3df (rotation),
227 scm_to_vector3df (scale));
228 return wrap_mesh_scene_node (node);
232 irr_scene_addCustomSceneNode (SCM wrapped_scene_manager,
234 SCM proc_get_bounding_box,
235 SCM proc_get_material_count,
236 SCM proc_get_material,
239 irr::scene::ISceneManager* scene_manager = unwrap_scene_manager (wrapped_scene_manager);
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),
245 scm_from_double (0));
246 SCM rotation = scm_list_3 (scm_from_double (0),
248 scm_from_double (0));
249 SCM scale = scm_list_3 (scm_from_double (1),
251 scm_from_double (1));
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,
261 class CustomSceneNode : public irr::scene::ISceneNode
264 SCM scm_get_bounding_box;
265 SCM scm_get_material_count;
266 SCM scm_get_material;
269 CustomSceneNode (irr::scene::ISceneNode* parent,
270 irr::scene::ISceneManager* smgr,
272 const irr::core::vector3df& position,
273 const irr::core::vector3df& rotation,
274 const irr::core::vector3df& scale,
276 SCM get_bounding_box,
277 SCM get_material_count,
279 : irr::scene::ISceneNode (parent, smgr, id, position, rotation, scale)
282 scm_get_bounding_box = get_bounding_box;
283 scm_get_material_count = get_material_count;
284 scm_get_material = get_material;
287 virtual void OnRegisterSceneNode ()
291 SceneManager->registerNodeForRendering (this);
293 ISceneNode::OnRegisterSceneNode ();
296 virtual void render ()
298 scm_call_0 (scm_render);
301 virtual const irr::core::aabbox3d<irr::f32>& getBoundingBox () const
303 SCM box = scm_call_0 (scm_get_bounding_box);
304 return *(unwrap_box3d (box));
307 virtual irr::u32 getMaterialCount () const
309 return scm_to_uint32 (scm_call_0 (scm_get_material_count));
312 virtual irr::video::SMaterial& getMaterial (irr::u32 i)
314 SCM material = scm_call_1 (scm_get_material, scm_from_uint32 (i));
315 return *(unwrap_material (material));
319 CustomSceneNode* node =
320 new CustomSceneNode (unwrap_scene_node (parent),
323 scm_to_vector3df (position),
324 scm_to_vector3df (rotation),
325 scm_to_vector3df (scale),
327 proc_get_bounding_box,
328 proc_get_material_count,
330 return wrap_scene_node (node);
334 irr_scene_addOctreeSceneNode (SCM wrapped_scene_manager,
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);
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,
350 irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
351 irr::scene::IMeshSceneNode* node;
352 if (animated_mesh_p (wrapped_mesh))
354 node = smgr->addOctreeSceneNode (unwrap_animated_mesh (wrapped_mesh),
355 scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
357 scm_to_int32 (minimal_polys_per_node),
358 scm_to_bool (also_add_if_mesh_pointer_zero));
362 node = smgr->addOctreeSceneNode (unwrap_mesh (wrapped_mesh),
363 scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
365 scm_to_int32 (minimal_polys_per_node),
366 scm_to_bool (also_add_if_mesh_pointer_zero));
368 return wrap_mesh_scene_node (node);
372 irr_scene_addSphereSceneNode (SCM wrapped_scene_manager,
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),
381 scm_from_double (0));
382 SCM rotation = scm_list_3 (scm_from_double (0),
384 scm_from_double (0));
385 SCM scale = scm_list_3 (scm_from_double (1),
387 scm_from_double (1));
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,
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),
405 scm_to_vector3df (position),
406 scm_to_vector3df (rotation),
407 scm_to_vector3df (scale));
408 return wrap_mesh_scene_node (node);
412 irr_scene_createFlyCircleAnimator (SCM wrapped_scene_manager,
415 SCM center = scm_list_3 (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),
422 scm_from_double (0));
423 SCM start_position = scm_from_double (0);
424 SCM radius_ellipsoid = scm_from_double (0);
426 scm_c_bind_keyword_arguments ("create-fly-circle-animator", rest, (scm_t_keyword_arguments_flags)0,
427 scm_from_utf8_keyword ("center"), ¢er,
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,
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);
447 irr_scene_createFlyStraightAnimator (SCM wrapped_scene_manager,
453 SCM loop = scm_from_bool (0);
454 SCM pingpong = scm_from_bool (0);
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,
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),
467 scm_to_bool (pingpong));
468 return wrap_scene_node_animator (anim);
472 irr_scene_createRotationAnimator (SCM wrapped_scene_manager,
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);
482 irr_scene_getMesh (SCM wrapped_scene_manager,
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);
491 irr_scene_getRootSceneNode (SCM wrapped_scene_manager)
493 irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
494 return wrap_scene_node (smgr->getRootSceneNode ());
498 irr_getSceneManager (SCM wrapped_obj)
500 irr::scene::ISceneManager* scene_manager;
501 if (device_p (wrapped_obj))
503 scene_manager = unwrap_device (wrapped_obj)->getSceneManager ();
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));
510 return wrap_scene_manager (scene_manager);