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"
34 #include "mesh-scene-node.h"
35 #include "scene-manager.h"
36 #include "scene-node.h"
37 #include "scene-node-animator.h"
44 init_scene_manager (void)
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 DEFINE_GSUBR ("get-scene-manager", 1, 0, 0, irr_getSceneManager);
62 DEFINE_WRAPPED_TYPE (irr::scene::ISceneManager*, "scene-manager",
63 init_scene_manager_type, scene_manager_p,
64 wrap_scene_manager, unwrap_scene_manager);
67 irr_scene_addAnimatedMeshSceneNode (SCM wrapped_scene_manager,
71 SCM parent = SCM_BOOL_F;
72 SCM id = scm_from_int32 (-1);
73 SCM position = scm_list_3 (scm_from_double (0),
76 SCM rotation = scm_list_3 (scm_from_double (0),
79 SCM scale = scm_list_3 (scm_from_double (1),
82 SCM also_add_if_mesh_pointer_zero = SCM_BOOL_F;
84 scm_c_bind_keyword_arguments ("add-animated-mesh-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
85 scm_from_utf8_keyword ("parent"), &parent,
86 scm_from_utf8_keyword ("id"), &id,
87 scm_from_utf8_keyword ("position"), &position,
88 scm_from_utf8_keyword ("rotation"), &rotation,
89 scm_from_utf8_keyword ("scale"), &scale,
90 scm_from_utf8_keyword ("also-add-if-mesh-pointer-zero"), &also_add_if_mesh_pointer_zero,
93 irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
94 irr::scene::IAnimatedMeshSceneNode* node =
95 smgr->addAnimatedMeshSceneNode (unwrap_animated_mesh (mesh),
96 scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
98 scm_to_vector3df (position),
99 scm_to_vector3df (rotation),
100 scm_to_vector3df (scale),
101 scm_to_bool (also_add_if_mesh_pointer_zero));
102 return wrap_animated_mesh_scene_node (node);
106 irr_scene_addCameraSceneNode (SCM wrapped_scene_manager,
109 SCM parent = SCM_BOOL_F;
110 SCM position = scm_list_3 (scm_from_double (0),
112 scm_from_double (0));
113 SCM lookat = scm_list_3 (scm_from_double (0),
115 scm_from_double (100));
116 SCM id = scm_from_int32 (-1);
117 SCM make_active = SCM_BOOL_T;
119 scm_c_bind_keyword_arguments ("add-camera-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
120 scm_from_utf8_keyword ("parent"), &parent,
121 scm_from_utf8_keyword ("position"), &position,
122 scm_from_utf8_keyword ("lookat"), &lookat,
123 scm_from_utf8_keyword ("id"), &id,
124 scm_from_utf8_keyword ("make-active"), &make_active,
127 irr::scene::ISceneManager* scene_manager = unwrap_scene_manager (wrapped_scene_manager);
128 irr::scene::ICameraSceneNode* camera =
129 scene_manager->addCameraSceneNode (scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
130 scm_to_vector3df (position),
131 scm_to_vector3df (lookat),
133 scm_to_bool (make_active));
134 return wrap_camera_scene_node (camera);
138 irr_scene_addCameraSceneNodeFPS (SCM wrapped_scene_manager,
141 SCM parent = SCM_BOOL_F;
142 SCM rotate_speed = scm_from_double (100);
143 SCM move_speed = scm_from_double (0.5);
144 SCM id = scm_from_int32 (-1);
145 SCM key_map_array = SCM_BOOL_F;
146 SCM key_map_size = scm_from_int32 (0);
147 SCM no_vertical_movement = SCM_BOOL_F;
148 SCM jump_speed = scm_from_double (0);
149 SCM invert_mouse = SCM_BOOL_F;
150 SCM make_active = SCM_BOOL_T;
152 scm_c_bind_keyword_arguments ("add-camera-scene-node-fps!", rest, (scm_t_keyword_arguments_flags)0,
153 scm_from_utf8_keyword ("parent"), &parent,
154 scm_from_utf8_keyword ("rotate-speed"), &rotate_speed,
155 scm_from_utf8_keyword ("move-speed"), &move_speed,
156 scm_from_utf8_keyword ("id"), &id,
157 scm_from_utf8_keyword ("key-map-array"), &key_map_array,
158 scm_from_utf8_keyword ("key-map-size"), &key_map_size,
159 scm_from_utf8_keyword ("no-vertical-movement"), &no_vertical_movement,
160 scm_from_utf8_keyword ("jump-speed"), &jump_speed,
161 scm_from_utf8_keyword ("invert-mouse"), &invert_mouse,
162 scm_from_utf8_keyword ("make-active"), &make_active,
165 irr::scene::ISceneManager* scene_manager = unwrap_scene_manager (wrapped_scene_manager);
166 irr::scene::ICameraSceneNode* camera =
167 scene_manager->addCameraSceneNodeFPS (scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
168 scm_to_double (rotate_speed),
169 scm_to_double (move_speed),
171 scm_is_false (key_map_array) ? 0 : unwrap_keymap (key_map_array),
172 scm_to_int32 (key_map_size),
173 scm_to_bool (no_vertical_movement),
174 scm_to_double (jump_speed),
175 scm_to_bool (invert_mouse),
176 scm_to_bool (make_active));
177 return wrap_camera_scene_node (camera);
181 irr_scene_addCubeSceneNode (SCM wrapped_scene_manager,
184 SCM size = scm_from_double (10);
185 SCM parent = SCM_BOOL_F;
186 SCM id = scm_from_int32 (-1);
187 SCM position = scm_list_3 (scm_from_double (0),
189 scm_from_double (0));
190 SCM rotation = scm_list_3 (scm_from_double (0),
192 scm_from_double (0));
193 SCM scale = scm_list_3 (scm_from_double (1),
195 scm_from_double (1));
197 scm_c_bind_keyword_arguments ("add-cube-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
198 scm_from_utf8_keyword ("size"), &size,
199 scm_from_utf8_keyword ("parent"), &parent,
200 scm_from_utf8_keyword ("id"), &id,
201 scm_from_utf8_keyword ("position"), &position,
202 scm_from_utf8_keyword ("rotation"), &rotation,
203 scm_from_utf8_keyword ("scale"), &scale,
206 irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
207 irr::scene::IMeshSceneNode* node =
208 smgr->addCubeSceneNode (scm_to_double (size),
209 scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
211 scm_to_vector3df (position),
212 scm_to_vector3df (rotation),
213 scm_to_vector3df (scale));
214 return wrap_mesh_scene_node (node);
218 irr_scene_addCustomSceneNode (SCM wrapped_scene_manager,
220 SCM proc_get_bounding_box,
221 SCM proc_get_material_count,
222 SCM proc_get_material,
225 irr::scene::ISceneManager* scene_manager = unwrap_scene_manager (wrapped_scene_manager);
227 SCM parent = wrap_scene_node (scene_manager->getRootSceneNode ());
228 SCM id = scm_from_int32 (-1);
229 SCM position = scm_list_3 (scm_from_double (0),
231 scm_from_double (0));
232 SCM rotation = scm_list_3 (scm_from_double (0),
234 scm_from_double (0));
235 SCM scale = scm_list_3 (scm_from_double (1),
237 scm_from_double (1));
239 scm_c_bind_keyword_arguments ("add-custom-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
240 scm_from_utf8_keyword ("parent"), &parent,
241 scm_from_utf8_keyword ("id"), &id,
242 scm_from_utf8_keyword ("position"), &position,
243 scm_from_utf8_keyword ("rotation"), &rotation,
244 scm_from_utf8_keyword ("scale"), &scale,
247 class CustomSceneNode : public irr::scene::ISceneNode
250 SCM scm_get_bounding_box;
251 SCM scm_get_material_count;
252 SCM scm_get_material;
255 CustomSceneNode (irr::scene::ISceneNode* parent,
256 irr::scene::ISceneManager* smgr,
258 const irr::core::vector3df& position,
259 const irr::core::vector3df& rotation,
260 const irr::core::vector3df& scale,
262 SCM get_bounding_box,
263 SCM get_material_count,
265 : irr::scene::ISceneNode (parent, smgr, id, position, rotation, scale)
268 scm_get_bounding_box = get_bounding_box;
269 scm_get_material_count = get_material_count;
270 scm_get_material = get_material;
273 virtual void OnRegisterSceneNode ()
277 SceneManager->registerNodeForRendering (this);
279 ISceneNode::OnRegisterSceneNode ();
282 virtual void render ()
284 scm_call_0 (scm_render);
287 virtual const irr::core::aabbox3d<irr::f32>& getBoundingBox () const
289 SCM box = scm_call_0 (scm_get_bounding_box);
290 return *(unwrap_box3d (box));
293 virtual irr::u32 getMaterialCount () const
295 return scm_to_uint32 (scm_call_0 (scm_get_material_count));
298 virtual irr::video::SMaterial& getMaterial (irr::u32 i)
300 SCM material = scm_call_1 (scm_get_material, scm_from_uint32 (i));
301 return *(unwrap_material (material));
305 CustomSceneNode* node =
306 new CustomSceneNode (unwrap_scene_node (parent),
309 scm_to_vector3df (position),
310 scm_to_vector3df (rotation),
311 scm_to_vector3df (scale),
313 proc_get_bounding_box,
314 proc_get_material_count,
316 return wrap_scene_node (node);
320 irr_scene_addOctreeSceneNode (SCM wrapped_scene_manager,
324 SCM parent = SCM_BOOL_F;
325 SCM id = scm_from_int32 (-1);
326 SCM minimal_polys_per_node = scm_from_int32 (256);
327 SCM also_add_if_mesh_pointer_zero = SCM_BOOL_F;
329 scm_c_bind_keyword_arguments ("add-octree-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
330 scm_from_utf8_keyword ("parent"), &parent,
331 scm_from_utf8_keyword ("id"), &id,
332 scm_from_utf8_keyword ("minimal-polys-per-node"), &minimal_polys_per_node,
333 scm_from_utf8_keyword ("also-add-if-mesh-pointer-zero"), &also_add_if_mesh_pointer_zero,
336 irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
337 irr::scene::IMeshSceneNode* node;
338 if (animated_mesh_p (wrapped_mesh))
340 node = smgr->addOctreeSceneNode (unwrap_animated_mesh (wrapped_mesh),
341 scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
343 scm_to_int32 (minimal_polys_per_node),
344 scm_to_bool (also_add_if_mesh_pointer_zero));
348 node = smgr->addOctreeSceneNode (unwrap_mesh (wrapped_mesh),
349 scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
351 scm_to_int32 (minimal_polys_per_node),
352 scm_to_bool (also_add_if_mesh_pointer_zero));
354 return wrap_mesh_scene_node (node);
358 irr_scene_addSphereSceneNode (SCM wrapped_scene_manager,
361 SCM radius = scm_from_double (5.0);
362 SCM poly_count = scm_from_int32 (16);
363 SCM parent = SCM_BOOL_F;
364 SCM id = scm_from_int32 (-1);
365 SCM position = scm_list_3 (scm_from_double (0),
367 scm_from_double (0));
368 SCM rotation = scm_list_3 (scm_from_double (0),
370 scm_from_double (0));
371 SCM scale = scm_list_3 (scm_from_double (1),
373 scm_from_double (1));
375 scm_c_bind_keyword_arguments ("add-sphere-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
376 scm_from_utf8_keyword ("radius"), &radius,
377 scm_from_utf8_keyword ("poly-count"), &poly_count,
378 scm_from_utf8_keyword ("parent"), &parent,
379 scm_from_utf8_keyword ("id"), &id,
380 scm_from_utf8_keyword ("position"), &position,
381 scm_from_utf8_keyword ("rotation"), &rotation,
382 scm_from_utf8_keyword ("scale"), &scale,
385 irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
386 irr::scene::IMeshSceneNode* node =
387 smgr->addSphereSceneNode (scm_to_double (radius),
388 scm_to_int32 (poly_count),
389 scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
391 scm_to_vector3df (position),
392 scm_to_vector3df (rotation),
393 scm_to_vector3df (scale));
394 return wrap_mesh_scene_node (node);
398 irr_scene_createFlyCircleAnimator (SCM wrapped_scene_manager,
401 SCM center = scm_list_3 (scm_from_double (0),
403 scm_from_double (0));
404 SCM radius = scm_from_double (100);
405 SCM speed = scm_from_double (0.001);
406 SCM direction = scm_list_3 (scm_from_double (0),
408 scm_from_double (0));
409 SCM start_position = scm_from_double (0);
410 SCM radius_ellipsoid = scm_from_double (0);
412 scm_c_bind_keyword_arguments ("create-fly-circle-animator", rest, (scm_t_keyword_arguments_flags)0,
413 scm_from_utf8_keyword ("center"), ¢er,
414 scm_from_utf8_keyword ("radius"), &radius,
415 scm_from_utf8_keyword ("speed"), &speed,
416 scm_from_utf8_keyword ("direction"), &direction,
417 scm_from_utf8_keyword ("start-position"), &start_position,
418 scm_from_utf8_keyword ("radius-ellipsoid"), &radius_ellipsoid,
421 irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
422 irr::scene::ISceneNodeAnimator* anim =
423 smgr->createFlyCircleAnimator (scm_to_vector3df (center),
424 scm_to_double (radius),
425 scm_to_double (speed),
426 scm_to_vector3df (direction),
427 scm_to_double (start_position),
428 scm_to_double (radius_ellipsoid));
429 return wrap_scene_node_animator (anim);
433 irr_scene_createFlyStraightAnimator (SCM wrapped_scene_manager,
439 SCM loop = SCM_BOOL_F;
440 SCM pingpong = SCM_BOOL_F;
442 scm_c_bind_keyword_arguments ("create-fly-straight-animator", rest, (scm_t_keyword_arguments_flags)0,
443 scm_from_utf8_keyword ("loop"), &loop,
444 scm_from_utf8_keyword ("pingpong"), &pingpong,
447 irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
448 irr::scene::ISceneNodeAnimator* anim =
449 smgr->createFlyStraightAnimator (scm_to_vector3df (start_point),
450 scm_to_vector3df (end_point),
451 scm_to_uint32 (time_for_way),
453 scm_to_bool (pingpong));
454 return wrap_scene_node_animator (anim);
458 irr_scene_createRotationAnimator (SCM wrapped_scene_manager,
461 irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
462 irr::scene::ISceneNodeAnimator* anim =
463 smgr->createRotationAnimator (scm_to_vector3df (rotation_speed));
464 return wrap_scene_node_animator (anim);
468 irr_scene_getMesh (SCM wrapped_scene_manager,
471 irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
472 irr::scene::IAnimatedMesh* mesh = smgr->getMesh(scm_to_utf8_stringn (filename, NULL));
473 return wrap_animated_mesh (mesh);
477 irr_scene_getRootSceneNode (SCM wrapped_scene_manager)
479 irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
480 return wrap_scene_node (smgr->getRootSceneNode ());
484 irr_getSceneManager (SCM wrapped_obj)
486 irr::scene::ISceneManager* scene_manager;
487 if (device_p (wrapped_obj))
489 scene_manager = unwrap_device (wrapped_obj)->getSceneManager ();
493 scm_error (scm_arg_type_key, NULL, "Cannot get scene manager from object: ~S",
494 scm_list_1 (wrapped_obj), scm_list_1 (wrapped_obj));
496 return wrap_scene_manager (scene_manager);