]> git.jsancho.org Git - guile-irrlicht.git/blob - src/scene-manager.cpp
0f3e54cdae86130a56d8675dbc4364ca971ec9a9
[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 "camera-scene-node.h"
28 #include "device.h"
29 #include "mesh.h"
30 #include "mesh-scene-node.h"
31 #include "scene-manager.h"
32 #include "scene-node.h"
33 #include "vector3d.h"
34 #include "wrapped.h"
35
36 extern "C" {
37
38   void
39   init_scene_manager (void)
40   {
41     init_scene_manager_type ();
42     scm_c_define_gsubr ("add-animated-mesh-scene-node!", 2, 0, 1,
43                         (scm_t_subr)irr_scene_addAnimatedMeshSceneNode);
44     scm_c_define_gsubr ("add-camera-scene-node!", 1, 0, 1,
45                         (scm_t_subr)irr_scene_addCameraSceneNode);
46     scm_c_define_gsubr ("add-octree-scene-node!", 2, 0, 1,
47                         (scm_t_subr)irr_scene_addOctreeSceneNode);
48     scm_c_define_gsubr ("get-mesh", 2, 0, 0, (scm_t_subr)irr_scene_getMesh);
49     scm_c_define_gsubr ("get-scene-manager", 1, 0, 0, (scm_t_subr)irr_getSceneManager);
50     scm_c_export ("add-animated-mesh-scene-node!", "add-camera-scene-node!",
51                   "add-octree-scene-node!", "get-mesh", "get-scene-manager", NULL);
52   }
53
54   DEFINE_WRAPPED_TYPE (irr::scene::ISceneManager*, "scene-manager",
55                        init_scene_manager_type, scene_manager_p,
56                        wrap_scene_manager, unwrap_scene_manager);
57
58   SCM
59   irr_scene_addAnimatedMeshSceneNode (SCM wrapped_scene_manager,
60                                       SCM mesh,
61                                       SCM rest)
62   {
63     SCM parent = scm_from_bool (0);
64     SCM id = scm_from_int32 (-1);
65     SCM position = scm_list_3 (scm_from_double (0),
66                                scm_from_double (0),
67                                scm_from_double (0));
68     SCM rotation = scm_list_3 (scm_from_double (0),
69                                scm_from_double (0),
70                                scm_from_double (0));
71     SCM scale = scm_list_3 (scm_from_double (1),
72                             scm_from_double (1),
73                             scm_from_double (1));
74     SCM also_add_if_mesh_pointer_zero = scm_from_bool (0);
75
76     scm_c_bind_keyword_arguments ("add-animated-mesh-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
77                                   scm_from_utf8_keyword ("parent"), &parent,
78                                   scm_from_utf8_keyword ("id"), &id,
79                                   scm_from_utf8_keyword ("position"), &position,
80                                   scm_from_utf8_keyword ("rotation"), &rotation,
81                                   scm_from_utf8_keyword ("scale"), &scale,
82                                   scm_from_utf8_keyword ("also-add-if-mesh-pointer-zero"), &also_add_if_mesh_pointer_zero,
83                                   SCM_UNDEFINED);
84
85     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
86     irr::scene::IAnimatedMeshSceneNode* node =
87       smgr->addAnimatedMeshSceneNode (unwrap_animated_mesh (mesh),
88                                       scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
89                                       scm_to_int32 (id),
90                                       scm_to_vector3df (position),
91                                       scm_to_vector3df (rotation),
92                                       scm_to_vector3df (scale),
93                                       scm_to_bool (also_add_if_mesh_pointer_zero));
94     return wrap_animated_mesh_scene_node (node);
95   }
96
97   SCM
98   irr_scene_addCameraSceneNode (SCM wrapped_scene_manager,
99                                 SCM rest)
100   {
101     SCM parent = scm_from_bool (0);
102     SCM position = scm_list_3 (scm_from_double (0),
103                                scm_from_double (0),
104                                scm_from_double (0));
105     SCM lookat = scm_list_3 (scm_from_double (0),
106                              scm_from_double (0),
107                              scm_from_double (100));
108     SCM id = scm_from_int32 (-1);
109     SCM make_active = scm_from_bool (1);
110
111     scm_c_bind_keyword_arguments ("add-camera-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
112                                   scm_from_utf8_keyword ("parent"), &parent,
113                                   scm_from_utf8_keyword ("position"), &position,
114                                   scm_from_utf8_keyword ("lookat"), &lookat,
115                                   scm_from_utf8_keyword ("id"), &id,
116                                   scm_from_utf8_keyword ("make-active"), &make_active,
117                                   SCM_UNDEFINED);
118
119     irr::scene::ISceneManager* scene_manager = unwrap_scene_manager (wrapped_scene_manager);
120     irr::scene::ICameraSceneNode* camera =
121       scene_manager->addCameraSceneNode (scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
122                                          scm_to_vector3df (position),
123                                          scm_to_vector3df (lookat),
124                                          scm_to_int32 (id),
125                                          scm_to_bool (make_active));
126     return wrap_camera_scene_node (camera);
127   }
128
129   SCM
130   irr_scene_addOctreeSceneNode (SCM wrapped_scene_manager,
131                                 SCM wrapped_mesh,
132                                 SCM rest)
133   {
134     SCM parent = scm_from_bool (0);
135     SCM id = scm_from_int32 (-1);
136     SCM minimal_polys_per_node = scm_from_int32 (256);
137     SCM also_add_if_mesh_pointer_zero = scm_from_bool (0);
138
139     scm_c_bind_keyword_arguments ("add-octree-scene-node!", rest, (scm_t_keyword_arguments_flags)0,
140                                   scm_from_utf8_keyword ("parent"), &parent,
141                                   scm_from_utf8_keyword ("id"), &id,
142                                   scm_from_utf8_keyword ("minimal-polys-per-node"), &minimal_polys_per_node,
143                                   scm_from_utf8_keyword ("also-add-if-mesh-pointer-zero"), &also_add_if_mesh_pointer_zero,
144                                   SCM_UNDEFINED);
145
146     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
147     irr::scene::IMeshSceneNode* node;
148     if (animated_mesh_p (wrapped_mesh))
149       {
150         node = smgr->addOctreeSceneNode (unwrap_animated_mesh (wrapped_mesh),
151                                          scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
152                                          scm_to_int32 (id),
153                                          scm_to_int32 (minimal_polys_per_node),
154                                          scm_to_bool (also_add_if_mesh_pointer_zero));
155       }
156     else
157       {
158         node = smgr->addOctreeSceneNode (unwrap_mesh (wrapped_mesh),
159                                          scm_is_false (parent) ? 0 : unwrap_scene_node (parent),
160                                          scm_to_int32 (id),
161                                          scm_to_int32 (minimal_polys_per_node),
162                                          scm_to_bool (also_add_if_mesh_pointer_zero));
163       }
164     return wrap_mesh_scene_node (node);
165   }
166
167   SCM
168   irr_scene_getMesh (SCM wrapped_scene_manager,
169                      SCM filename)
170   {
171     irr::scene::ISceneManager* smgr = unwrap_scene_manager (wrapped_scene_manager);
172     irr::scene::IAnimatedMesh* mesh = smgr->getMesh(scm_to_utf8_stringn (filename, NULL));
173     return wrap_animated_mesh (mesh);
174   }
175
176   SCM
177   irr_getSceneManager (SCM wrapped_obj)
178   {
179     irr::scene::ISceneManager* scene_manager;
180     if (device_p (wrapped_obj))
181       {
182         scene_manager = unwrap_device (wrapped_obj)->getSceneManager ();
183       }
184     else
185       {
186         scm_error (scm_arg_type_key, NULL, "Cannot get scene manager from object: ~S",
187                    scm_list_1 (wrapped_obj), scm_list_1 (wrapped_obj));
188       }
189     return wrap_scene_manager (scene_manager);
190   }
191
192 }