]> git.jsancho.org Git - guile-irrlicht.git/blobdiff - src/scene-manager.cpp
guile-irrlicht
[guile-irrlicht.git] / src / scene-manager.cpp
index cb7f80c94d0249106ff105ae3d39b0a91e5fa681..af96eebc403b09188d10ce0adbe863a9da2aa638 100644 (file)
 
 #include "animated-mesh.h"
 #include "animated-mesh-scene-node.h"
+#include "box3d.h"
 #include "camera-scene-node.h"
 #include "device.h"
+#include "gsubr.h"
+#include "keymap.h"
+#include "material.h"
+#include "mesh.h"
+#include "mesh-scene-node.h"
 #include "scene-manager.h"
 #include "scene-node.h"
+#include "scene-node-animator.h"
 #include "vector3d.h"
 #include "wrapped.h"
 
+  SCM
+  irr_drawAll (SCM wrapped_obj)
+  {
+    if (gui_environment_p (wrapped_obj))
+      {
+        unwrap_gui_environment (wrapped_obj)->drawAll ();
+      }
+    else if (scene_manager_p (wrapped_obj))
+      {
+        unwrap_scene_manager (wrapped_obj)->drawAll ();
+      }
+    else
+      {
+        scm_error (scm_arg_type_key, NULL, "Cannot draw all elements from object: ~S",
+                   scm_list_1 (wrapped_obj), scm_list_1 (wrapped_obj));
+      }
+    return SCM_UNSPECIFIED;
+  }
+
 extern "C" {
 
   void
   init_scene_manager (void)
   {
     init_scene_manager_type ();
-    scm_c_define_gsubr ("add-animated-mesh-scene-node!", 2, 0, 1,
-                        (scm_t_subr)irr_scene_addAnimatedMeshSceneNode);
-    scm_c_define_gsubr ("add-camera-scene-node!", 1, 0, 1,
-                        (scm_t_subr)irr_scene_addCameraSceneNode);
-    scm_c_define_gsubr ("get-mesh", 2, 0, 0, (scm_t_subr)irr_scene_getMesh);
-    scm_c_define_gsubr ("get-scene-manager", 1, 0, 0, (scm_t_subr)irr_getSceneManager);
-    scm_c_export ("add-animated-mesh-scene-node!", "add-camera-scene-node!",
-                  "get-mesh", "get-scene-manager", NULL);
+    DEFINE_GSUBR ("add-animated-mesh-scene-node!", 2, 0, 1, irr_scene_addAnimatedMeshSceneNode);
+    DEFINE_GSUBR ("add-camera-scene-node!", 1, 0, 1, irr_scene_addCameraSceneNode);
+    DEFINE_GSUBR ("add-camera-scene-node-fps!", 1, 0, 1, irr_scene_addCameraSceneNodeFPS);
+    DEFINE_GSUBR ("add-cube-scene-node!", 1, 0, 1, irr_scene_addCubeSceneNode);
+    DEFINE_GSUBR ("add-custom-scene-node!", 5, 0, 1, irr_scene_addCustomSceneNode);
+    DEFINE_GSUBR ("add-octree-scene-node!", 2, 0, 1, irr_scene_addOctreeSceneNode);
+    DEFINE_GSUBR ("add-sphere-scene-node!", 1, 0, 1, irr_scene_addSphereSceneNode);
+    DEFINE_GSUBR ("create-fly-circle-animator", 1, 0, 1, irr_scene_createFlyCircleAnimator);
+    DEFINE_GSUBR ("create-fly-straight-animator", 4, 0, 1, irr_scene_createFlyStraightAnimator);
+    DEFINE_GSUBR ("create-rotation-animator", 2, 0, 0, irr_scene_createRotationAnimator);
+    DEFINE_GSUBR ("get-mesh", 2, 0, 0, irr_scene_getMesh);
+    DEFINE_GSUBR ("get-root-scene-node", 1, 0, 0, irr_scene_getRootSceneNode);
   }
 
   DEFINE_WRAPPED_TYPE (irr::scene::ISceneManager*, "scene-manager",
@@ -56,7 +86,7 @@ extern "C" {
                                       SCM mesh,
                                       SCM rest)
   {
-    SCM parent = scm_from_bool (0);
+    SCM parent = SCM_UNDEFINED;
     SCM id = scm_from_int32 (-1);
     SCM position = scm_list_3 (scm_from_double (0),
                                scm_from_double (0),
@@ -67,7 +97,7 @@ extern "C" {
     SCM scale = scm_list_3 (scm_from_double (1),
                             scm_from_double (1),
                             scm_from_double (1));
-    SCM also_add_if_mesh_pointer_zero = scm_from_bool (0);
+    SCM also_add_if_mesh_pointer_zero = SCM_BOOL_F;
 
     scm_c_bind_keyword_arguments ("add-animated-mesh-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
                                   scm_from_utf8_keyword ("parent"), &parent,
@@ -81,7 +111,7 @@ extern "C" {
     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
     irr::scene::IAnimatedMeshSceneNode* node =
       smgr->addAnimatedMeshSceneNode (unwrap_animated_mesh (mesh),
-                                      scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
+                                      parent == SCM_UNDEFINED ? 0 : unwrap_scene_node (parent),
                                       scm_to_int32 (id),
                                       scm_to_vector3df (position),
                                       scm_to_vector3df (rotation),
@@ -94,7 +124,7 @@ extern "C" {
   irr_scene_addCameraSceneNode (SCM wrapped_scene_manager,
                                 SCM rest)
   {
-    SCM parent = scm_from_bool (0);
+    SCM parent = SCM_UNDEFINED;
     SCM position = scm_list_3 (scm_from_double (0),
                                scm_from_double (0),
                                scm_from_double (0));
@@ -102,7 +132,7 @@ extern "C" {
                              scm_from_double (0),
                              scm_from_double (100));
     SCM id = scm_from_int32 (-1);
-    SCM make_active = scm_from_bool (1);
+    SCM make_active = SCM_BOOL_T;
 
     scm_c_bind_keyword_arguments ("add-camera-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
                                   scm_from_utf8_keyword ("parent"), &parent,
@@ -114,7 +144,7 @@ extern "C" {
 
     irr::scene::ISceneManager* scene_manager = unwrap_scene_manager (wrapped_scene_manager);
     irr::scene::ICameraSceneNode* camera =
-      scene_manager->addCameraSceneNode (scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
+      scene_manager->addCameraSceneNode (parent == SCM_UNDEFINED ? 0 : unwrap_scene_node (parent),
                                          scm_to_vector3df (position),
                                          scm_to_vector3df (lookat),
                                          scm_to_int32 (id),
@@ -123,28 +153,349 @@ extern "C" {
   }
 
   SCM
-  irr_scene_getMesh (SCM wrapped_scene_manager,
-                     SCM filename)
+  irr_scene_addCameraSceneNodeFPS (SCM wrapped_scene_manager,
+                                   SCM rest)
   {
+    SCM parent = SCM_UNDEFINED;
+    SCM rotate_speed = scm_from_double (100);
+    SCM move_speed = scm_from_double (0.5);
+    SCM id = scm_from_int32 (-1);
+    SCM key_map_array = SCM_UNDEFINED;
+    SCM key_map_size = scm_from_int32 (0);
+    SCM no_vertical_movement = SCM_BOOL_F;
+    SCM jump_speed = scm_from_double (0);
+    SCM invert_mouse = SCM_BOOL_F;
+    SCM make_active = SCM_BOOL_T;
+    
+    scm_c_bind_keyword_arguments ("add-camera-scene-node-fps!", rest, (scm_t_keyword_arguments_flags)0,
+                                  scm_from_utf8_keyword ("parent"), &parent,
+                                  scm_from_utf8_keyword ("rotate-speed"), &rotate_speed,
+                                  scm_from_utf8_keyword ("move-speed"), &move_speed,
+                                  scm_from_utf8_keyword ("id"), &id,
+                                  scm_from_utf8_keyword ("key-map-array"), &key_map_array,
+                                  scm_from_utf8_keyword ("key-map-size"), &key_map_size,
+                                  scm_from_utf8_keyword ("no-vertical-movement"), &no_vertical_movement,
+                                  scm_from_utf8_keyword ("jump-speed"), &jump_speed,
+                                  scm_from_utf8_keyword ("invert-mouse"), &invert_mouse,
+                                  scm_from_utf8_keyword ("make-active"), &make_active,
+                                  SCM_UNDEFINED);
+
+    irr::scene::ISceneManager* scene_manager = unwrap_scene_manager (wrapped_scene_manager);
+    irr::scene::ICameraSceneNode* camera =
+      scene_manager->addCameraSceneNodeFPS (parent == SCM_UNDEFINED ? 0 : unwrap_scene_node (parent),
+                                            scm_to_double (rotate_speed),
+                                            scm_to_double (move_speed),
+                                            scm_to_int32 (id),
+                                            key_map_array == SCM_UNDEFINED ? 0 : unwrap_keymap (key_map_array),
+                                            scm_to_int32 (key_map_size),
+                                            scm_to_bool (no_vertical_movement),
+                                            scm_to_double (jump_speed),
+                                            scm_to_bool (invert_mouse),
+                                            scm_to_bool (make_active));
+    return wrap_camera_scene_node (camera);
+  }
+
+  SCM
+  irr_scene_addCubeSceneNode (SCM wrapped_scene_manager,
+                              SCM rest)
+  {
+    SCM size = scm_from_double (10);
+    SCM parent = SCM_UNDEFINED;
+    SCM id = scm_from_int32 (-1);
+    SCM position = scm_list_3 (scm_from_double (0),
+                               scm_from_double (0),
+                               scm_from_double (0));
+    SCM rotation = scm_list_3 (scm_from_double (0),
+                               scm_from_double (0),
+                               scm_from_double (0));
+    SCM scale = scm_list_3 (scm_from_double (1),
+                            scm_from_double (1),
+                            scm_from_double (1));
+
+    scm_c_bind_keyword_arguments ("add-cube-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
+                                  scm_from_utf8_keyword ("size"), &size,
+                                  scm_from_utf8_keyword ("parent"), &parent,
+                                  scm_from_utf8_keyword ("id"), &id,
+                                  scm_from_utf8_keyword ("position"), &position,
+                                  scm_from_utf8_keyword ("rotation"), &rotation,
+                                  scm_from_utf8_keyword ("scale"), &scale,
+                                  SCM_UNDEFINED);
+
     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
-    irr::scene::IAnimatedMesh* mesh = smgr->getMesh(scm_to_utf8_stringn (filename, NULL));
-    return wrap_animated_mesh (mesh);
+    irr::scene::IMeshSceneNode* node =
+      smgr->addCubeSceneNode (scm_to_double (size),
+                              parent == SCM_UNDEFINED ? 0 : unwrap_scene_node (parent),
+                              scm_to_int32 (id),
+                              scm_to_vector3df (position),
+                              scm_to_vector3df (rotation),
+                              scm_to_vector3df (scale));
+    return wrap_mesh_scene_node (node);
   }
 
   SCM
-  irr_getSceneManager (SCM wrapped_obj)
+  irr_scene_addCustomSceneNode (SCM wrapped_scene_manager,
+                                SCM proc_render,
+                                SCM proc_get_bounding_box,
+                                SCM proc_get_material_count,
+                                SCM proc_get_material,
+                                SCM rest)
   {
-    irr::scene::ISceneManager* scene_manager;
-    if (device_p (wrapped_obj))
+    irr::scene::ISceneManager* scene_manager = unwrap_scene_manager (wrapped_scene_manager);
+
+    SCM parent = wrap_scene_node (scene_manager->getRootSceneNode ());
+    SCM id = scm_from_int32 (-1);
+    SCM position = scm_list_3 (scm_from_double (0),
+                               scm_from_double (0),
+                               scm_from_double (0));
+    SCM rotation = scm_list_3 (scm_from_double (0),
+                               scm_from_double (0),
+                               scm_from_double (0));
+    SCM scale = scm_list_3 (scm_from_double (1),
+                            scm_from_double (1),
+                            scm_from_double (1));
+
+    scm_c_bind_keyword_arguments ("add-custom-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
+                                  scm_from_utf8_keyword ("parent"), &parent,
+                                  scm_from_utf8_keyword ("id"), &id,
+                                  scm_from_utf8_keyword ("position"), &position,
+                                  scm_from_utf8_keyword ("rotation"), &rotation,
+                                  scm_from_utf8_keyword ("scale"), &scale,
+                                  SCM_UNDEFINED);
+
+    class CustomSceneNode : public irr::scene::ISceneNode
+    {
+      SCM scm_render;
+      SCM scm_get_bounding_box;
+      SCM scm_get_material_count;
+      SCM scm_get_material;
+
+    public:
+      CustomSceneNode (irr::scene::ISceneNode* parent,
+                       irr::scene::ISceneManager* smgr,
+                       irr::s32 id,
+                       const irr::core::vector3df& position,
+                       const irr::core::vector3df& rotation,
+                       const irr::core::vector3df& scale,
+                       SCM render,
+                       SCM get_bounding_box,
+                       SCM get_material_count,
+                       SCM get_material)
+        : irr::scene::ISceneNode (parent, smgr, id, position, rotation, scale)
+      {
+        scm_render = render;
+        scm_get_bounding_box = get_bounding_box;
+        scm_get_material_count = get_material_count;
+        scm_get_material = get_material;
+      }
+
+      virtual void OnRegisterSceneNode ()
       {
-        scene_manager = unwrap_device (wrapped_obj)->getSceneManager ();
+        if (IsVisible)
+          {
+            SceneManager->registerNodeForRendering (this);
+          }
+        ISceneNode::OnRegisterSceneNode ();
+      }
+
+      virtual void render ()
+      {
+        scm_call_0 (scm_render);
+      }
+
+      virtual const irr::core::aabbox3d<irr::f32>& getBoundingBox () const
+      {
+        SCM box = scm_call_0 (scm_get_bounding_box);
+        return *(unwrap_box3d (box));
+      }
+
+      virtual irr::u32 getMaterialCount () const
+      {
+        return scm_to_uint32 (scm_call_0 (scm_get_material_count));
+      }
+
+      virtual irr::video::SMaterial& getMaterial (irr::u32 i)
+      {
+        SCM material = scm_call_1 (scm_get_material, scm_from_uint32 (i));
+        return *(unwrap_material (material));
+      }
+    };
+
+    CustomSceneNode* node =
+      new CustomSceneNode (unwrap_scene_node (parent),
+                           scene_manager,
+                           scm_to_int32 (id),
+                           scm_to_vector3df (position),
+                           scm_to_vector3df (rotation),
+                           scm_to_vector3df (scale),
+                           proc_render,
+                           proc_get_bounding_box,
+                           proc_get_material_count,
+                           proc_get_material);
+    return wrap_scene_node (node);
+  }
+
+  SCM
+  irr_scene_addOctreeSceneNode (SCM wrapped_scene_manager,
+                                SCM wrapped_mesh,
+                                SCM rest)
+  {
+    SCM parent = SCM_UNDEFINED;
+    SCM id = scm_from_int32 (-1);
+    SCM minimal_polys_per_node = scm_from_int32 (256);
+    SCM also_add_if_mesh_pointer_zero = SCM_BOOL_F;
+
+    scm_c_bind_keyword_arguments ("add-octree-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
+                                  scm_from_utf8_keyword ("parent"), &parent,
+                                  scm_from_utf8_keyword ("id"), &id,
+                                  scm_from_utf8_keyword ("minimal-polys-per-node"), &minimal_polys_per_node,
+                                  scm_from_utf8_keyword ("also-add-if-mesh-pointer-zero"), &also_add_if_mesh_pointer_zero,
+                                  SCM_UNDEFINED);
+
+    irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
+    irr::scene::IMeshSceneNode* node;
+    if (animated_mesh_p (wrapped_mesh))
+      {
+        node = smgr->addOctreeSceneNode (unwrap_animated_mesh (wrapped_mesh),
+                                         parent == SCM_UNDEFINED ? 0 : unwrap_scene_node (parent),
+                                         scm_to_int32 (id),
+                                         scm_to_int32 (minimal_polys_per_node),
+                                         scm_to_bool (also_add_if_mesh_pointer_zero));
       }
     else
       {
-        scm_error (scm_arg_type_key, NULL, "Cannot get scene manager from object: ~S",
-                   scm_list_1 (wrapped_obj), scm_list_1 (wrapped_obj));
+        node = smgr->addOctreeSceneNode (unwrap_mesh (wrapped_mesh),
+                                         parent == SCM_UNDEFINED ? 0 : unwrap_scene_node (parent),
+                                         scm_to_int32 (id),
+                                         scm_to_int32 (minimal_polys_per_node),
+                                         scm_to_bool (also_add_if_mesh_pointer_zero));
       }
-    return wrap_scene_manager (scene_manager);
+    return wrap_mesh_scene_node (node);
+  }
+
+  SCM
+  irr_scene_addSphereSceneNode (SCM wrapped_scene_manager,
+                                SCM rest)
+  {
+    SCM radius = scm_from_double (5.0);
+    SCM poly_count = scm_from_int32 (16);
+    SCM parent = SCM_UNDEFINED;
+    SCM id = scm_from_int32 (-1);
+    SCM position = scm_list_3 (scm_from_double (0),
+                               scm_from_double (0),
+                               scm_from_double (0));
+    SCM rotation = scm_list_3 (scm_from_double (0),
+                               scm_from_double (0),
+                               scm_from_double (0));
+    SCM scale = scm_list_3 (scm_from_double (1),
+                            scm_from_double (1),
+                            scm_from_double (1));
+
+    scm_c_bind_keyword_arguments ("add-sphere-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
+                                  scm_from_utf8_keyword ("radius"), &radius,
+                                  scm_from_utf8_keyword ("poly-count"), &poly_count,
+                                  scm_from_utf8_keyword ("parent"), &parent,
+                                  scm_from_utf8_keyword ("id"), &id,
+                                  scm_from_utf8_keyword ("position"), &position,
+                                  scm_from_utf8_keyword ("rotation"), &rotation,
+                                  scm_from_utf8_keyword ("scale"), &scale,
+                                  SCM_UNDEFINED);
+
+    irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
+    irr::scene::IMeshSceneNode* node =
+      smgr->addSphereSceneNode (scm_to_double (radius),
+                                scm_to_int32 (poly_count),
+                                parent == SCM_UNDEFINED ? 0 : unwrap_scene_node (parent),
+                                scm_to_int32 (id),
+                                scm_to_vector3df (position),
+                                scm_to_vector3df (rotation),
+                                scm_to_vector3df (scale));
+    return wrap_mesh_scene_node (node);
+  }
+
+  SCM
+  irr_scene_createFlyCircleAnimator (SCM wrapped_scene_manager,
+                                     SCM rest)
+  {
+    SCM center = scm_list_3 (scm_from_double (0),
+                             scm_from_double (0),
+                             scm_from_double (0));
+    SCM radius = scm_from_double (100);
+    SCM speed = scm_from_double (0.001);
+    SCM direction = scm_list_3 (scm_from_double (0),
+                                scm_from_double (1),
+                                scm_from_double (0));
+    SCM start_position = scm_from_double (0);
+    SCM radius_ellipsoid = scm_from_double (0);
+
+    scm_c_bind_keyword_arguments ("create-fly-circle-animator", rest, (scm_t_keyword_arguments_flags)0,
+                                  scm_from_utf8_keyword ("center"), &center,
+                                  scm_from_utf8_keyword ("radius"), &radius,
+                                  scm_from_utf8_keyword ("speed"), &speed,
+                                  scm_from_utf8_keyword ("direction"), &direction,
+                                  scm_from_utf8_keyword ("start-position"), &start_position,
+                                  scm_from_utf8_keyword ("radius-ellipsoid"), &radius_ellipsoid,
+                                  SCM_UNDEFINED);
+
+    irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
+    irr::scene::ISceneNodeAnimator* anim =
+      smgr->createFlyCircleAnimator (scm_to_vector3df (center),
+                                     scm_to_double (radius),
+                                     scm_to_double (speed),
+                                     scm_to_vector3df (direction),
+                                     scm_to_double (start_position),
+                                     scm_to_double (radius_ellipsoid));
+    return wrap_scene_node_animator (anim);
+  }
+
+  SCM
+  irr_scene_createFlyStraightAnimator (SCM wrapped_scene_manager,
+                                       SCM start_point,
+                                       SCM end_point,
+                                       SCM time_for_way,
+                                       SCM rest)
+  {
+    SCM loop = SCM_BOOL_F;
+    SCM pingpong = SCM_BOOL_F;
+
+    scm_c_bind_keyword_arguments ("create-fly-straight-animator", rest, (scm_t_keyword_arguments_flags)0,
+                                  scm_from_utf8_keyword ("loop"), &loop,
+                                  scm_from_utf8_keyword ("pingpong"), &pingpong,
+                                  SCM_UNDEFINED);
+
+    irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
+    irr::scene::ISceneNodeAnimator* anim =
+      smgr->createFlyStraightAnimator (scm_to_vector3df (start_point),
+                                       scm_to_vector3df (end_point),
+                                       scm_to_uint32 (time_for_way),
+                                       scm_to_bool (loop),
+                                       scm_to_bool (pingpong));
+    return wrap_scene_node_animator (anim);
+  }
+
+  SCM
+  irr_scene_createRotationAnimator (SCM wrapped_scene_manager,
+                                    SCM rotation_speed)
+  {
+    irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
+    irr::scene::ISceneNodeAnimator* anim =
+      smgr->createRotationAnimator (scm_to_vector3df (rotation_speed));
+    return wrap_scene_node_animator (anim);
+  }
+
+  SCM
+  irr_scene_getMesh (SCM wrapped_scene_manager,
+                     SCM filename)
+  {
+    irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
+    irr::scene::IAnimatedMesh* mesh = smgr->getMesh(scm_to_utf8_stringn (filename, NULL));
+    return wrap_animated_mesh (mesh);
+  }
+
+  SCM
+  irr_scene_getRootSceneNode (SCM wrapped_scene_manager)
+  {
+    irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
+    return wrap_scene_node (smgr->getRootSceneNode ());
   }
 
 }