]> git.jsancho.org Git - guile-irrlicht.git/blob - src/scene-manager.cpp
Some doc
[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 #include "gsubr.h"
25 #include "scene-manager.h"
26 #include "vector3d.h"
27 #include "wrapped.h"
28
29 using namespace irr;
30
31 template <typename TParent>
32 SCM
33 ISceneManager_addAnimatedMeshSceneNode (SCM scene_manager,
34                                         SCM mesh,
35                                         SCM parent,
36                                         SCM id,
37                                         SCM position,
38                                         SCM rotation,
39                                         SCM scale,
40                                         SCM also_add_if_mesh_pointer_zero)
41 {
42   scene::ISceneManager* smgr = (scene::ISceneManager*) scm_to_irr_pointer (scene_manager);
43   scene::IAnimatedMeshSceneNode* node =
44     smgr->addAnimatedMeshSceneNode ((scene::IAnimatedMesh*) scm_to_irr_pointer (mesh),
45                                     (TParent) scm_to_irr_pointer (parent),
46                                     scm_to_int32 (id),
47                                     scm_to_vector3df (position),
48                                     scm_to_vector3df (rotation),
49                                     scm_to_vector3df (scale),
50                                     scm_to_bool (also_add_if_mesh_pointer_zero));
51   return scm_from_irr_pointer ("<animated-mesh-scene-node>", (void*) node);
52 }
53
54 template <typename TParent>
55 SCM
56 ISceneManager_addCameraSceneNode (SCM scene_manager,
57                                   SCM parent,
58                                   SCM position,
59                                   SCM lookat,
60                                   SCM id,
61                                   SCM make_active)
62 {
63   scene::ISceneManager* smgr = (scene::ISceneManager*) scm_to_irr_pointer (scene_manager);
64   scene::ICameraSceneNode* camera =
65     smgr->addCameraSceneNode ((TParent) scm_to_irr_pointer (parent),
66                               scm_to_vector3df (position),
67                               scm_to_vector3df (lookat),
68                               scm_to_int32 (id),
69                               scm_to_bool (make_active));
70   return scm_from_irr_pointer ("<camera-scene-node>", (void*) camera);
71 }
72
73 template <typename TParent>
74 SCM
75 ISceneManager_addCameraSceneNodeFPS (SCM scene_manager,
76                                      SCM rest)
77 {
78   SCM parent;
79   SCM rotate_speed;
80   SCM move_speed;
81   SCM id;
82   SCM key_map_array;
83   SCM key_map_size;
84   SCM no_vertical_movement;
85   SCM jump_speed;
86   SCM invert_mouse;
87   SCM make_active;
88
89   scm_c_bind_keyword_arguments ("scene_ISceneManager_addCameraSceneNodeFPS",
90                                 rest, (scm_t_keyword_arguments_flags)0,
91                                 scm_from_utf8_keyword ("parent"), &parent,
92                                 scm_from_utf8_keyword ("rotate-speed"), &rotate_speed,
93                                 scm_from_utf8_keyword ("move-speed"), &move_speed,
94                                 scm_from_utf8_keyword ("id"), &id,
95                                 scm_from_utf8_keyword ("key-map-array"), &key_map_array,
96                                 scm_from_utf8_keyword ("key-map-size"), &key_map_size,
97                                 scm_from_utf8_keyword ("no-vertical-movement"), &no_vertical_movement,
98                                 scm_from_utf8_keyword ("jump-speed"), &jump_speed,
99                                 scm_from_utf8_keyword ("invert-mouse"), &invert_mouse,
100                                 scm_from_utf8_keyword ("make-active"), &make_active,
101                                 SCM_UNDEFINED);
102
103   scene::ISceneManager* smgr = (scene::ISceneManager*) scm_to_irr_pointer (scene_manager);
104   scene::ICameraSceneNode* camera =
105     smgr->addCameraSceneNodeFPS ((TParent) scm_to_irr_pointer (parent),
106                                  scm_to_double (rotate_speed),
107                                  scm_to_double (move_speed),
108                                  scm_to_int32 (id),
109                                  (SKeyMap*) scm_to_irr_pointer (key_map_array),
110                                  scm_to_int32 (key_map_size),
111                                  scm_to_bool (no_vertical_movement),
112                                  scm_to_double (jump_speed),
113                                  scm_to_bool (invert_mouse),
114                                  scm_to_bool (make_active));
115   return scm_from_irr_pointer ("<camera-scene-node>", (void*) camera);
116 }
117
118 template <typename TParent>
119 SCM
120 ISceneManager_addCubeSceneNode (SCM scene_manager,
121                                 SCM size,
122                                 SCM parent,
123                                 SCM id,
124                                 SCM position,
125                                 SCM rotation,
126                                 SCM scale)
127 {
128   scene::ISceneManager* smgr = (scene::ISceneManager*) scm_to_irr_pointer (scene_manager);
129   scene::IMeshSceneNode* node =
130     smgr->addCubeSceneNode (scm_to_double (size),
131                             (TParent) scm_to_irr_pointer (parent),
132                             scm_to_int32 (id),
133                             scm_to_vector3df (position),
134                             scm_to_vector3df (rotation),
135                             scm_to_vector3df (scale));
136   return scm_from_irr_pointer ("<mesh-scene-node>", (void*) node);
137 }
138
139 template <typename TParent>
140 SCM
141 ISceneManager_addCustomSceneNode (SCM scene_manager,
142                                   SCM proc_render,
143                                   SCM proc_get_bounding_box,
144                                   SCM proc_get_material_count,
145                                   SCM proc_get_material,
146                                   SCM parent,
147                                   SCM id,
148                                   SCM position,
149                                   SCM rotation,
150                                   SCM scale)
151 {
152   class CustomSceneNode : public scene::ISceneNode
153   {
154     SCM scm_render;
155     SCM scm_get_bounding_box;
156     SCM scm_get_material_count;
157     SCM scm_get_material;
158
159   public:
160     CustomSceneNode (scene::ISceneNode* parent,
161                      scene::ISceneManager* smgr,
162                      s32 id,
163                      const core::vector3df& position,
164                      const core::vector3df& rotation,
165                      const core::vector3df& scale,
166                      SCM render,
167                      SCM get_bounding_box,
168                      SCM get_material_count,
169                      SCM get_material)
170       : scene::ISceneNode (parent, smgr, id, position, rotation, scale)
171     {
172       scm_render = render;
173       scm_get_bounding_box = get_bounding_box;
174       scm_get_material_count = get_material_count;
175       scm_get_material = get_material;
176     }
177
178     virtual void OnRegisterSceneNode ()
179     {
180       if (IsVisible)
181         {
182           SceneManager->registerNodeForRendering (this);
183         }
184       ISceneNode::OnRegisterSceneNode ();
185     }
186
187     virtual void render ()
188     {
189       scm_call_0 (scm_render);
190     }
191
192     virtual const core::aabbox3d<f32>& getBoundingBox () const
193     {
194       SCM box = scm_call_0 (scm_get_bounding_box);
195       return *((core::aabbox3d<f32>*) scm_to_irr_pointer (box));
196     }
197
198     virtual u32 getMaterialCount () const
199     {
200       return scm_to_uint32 (scm_call_0 (scm_get_material_count));
201     }
202
203     virtual video::SMaterial& getMaterial (u32 i)
204     {
205       SCM material = scm_call_1 (scm_get_material, scm_from_uint32 (i));
206       return *((video::SMaterial*) scm_to_irr_pointer (material));
207     }
208   };
209
210   CustomSceneNode* node =
211     new CustomSceneNode ((TParent) scm_to_irr_pointer (parent),
212                          (scene::ISceneManager*) scm_to_irr_pointer (scene_manager),
213                          scm_to_int32 (id),
214                          scm_to_vector3df (position),
215                          scm_to_vector3df (rotation),
216                          scm_to_vector3df (scale),
217                          proc_render,
218                          proc_get_bounding_box,
219                          proc_get_material_count,
220                          proc_get_material);
221   return scm_from_irr_pointer ("<scene-node>", (void*) node);
222 }
223
224 template <typename TParent, typename TMesh>
225 SCM
226 ISceneManager_addOctreeSceneNode (SCM scene_manager,
227                                   SCM mesh,
228                                   SCM parent,
229                                   SCM id,
230                                   SCM minimal_polys_per_node,
231                                   SCM also_add_if_mesh_pointer_zero)
232 {
233   scene::ISceneManager* smgr = (scene::ISceneManager*) scm_to_irr_pointer (scene_manager);
234   scene::IMeshSceneNode* node =
235     smgr->addOctreeSceneNode ((TMesh) scm_to_irr_pointer (mesh),
236                               (TParent) scm_to_irr_pointer (parent),
237                               scm_to_int32 (id),
238                               scm_to_int32 (minimal_polys_per_node),
239                               scm_to_bool (also_add_if_mesh_pointer_zero));
240   return scm_from_irr_pointer ("<mesh-scene-node>", (void*) node);
241 }
242
243 template <typename TParent>
244 SCM
245 ISceneManager_addSphereSceneNode (SCM scene_manager,
246                                   SCM radius,
247                                   SCM poly_count,
248                                   SCM parent,
249                                   SCM id,
250                                   SCM position,
251                                   SCM rotation,
252                                   SCM scale)
253 {
254   scene::ISceneManager* smgr = (scene::ISceneManager*) scm_to_irr_pointer (scene_manager);
255   scene::IMeshSceneNode* node =
256     smgr->addSphereSceneNode (scm_to_double (radius),
257                               scm_to_int32 (poly_count),
258                               (TParent) scm_to_irr_pointer (parent),
259                               scm_to_int32 (id),
260                               scm_to_vector3df (position),
261                               scm_to_vector3df (rotation),
262                               scm_to_vector3df (scale));
263   return scm_from_irr_pointer ("<mesh-scene-node>", (void*) node);
264 }
265
266 SCM
267 ISceneManager_createFlyCircleAnimator (SCM scene_manager,
268                                        SCM center,
269                                        SCM radius,
270                                        SCM speed,
271                                        SCM direction,
272                                        SCM start_position,
273                                        SCM radius_ellipsoid)
274 {
275   scene::ISceneManager* smgr = (scene::ISceneManager*) scm_to_irr_pointer (scene_manager);
276   scene::ISceneNodeAnimator* anim =
277     smgr->createFlyCircleAnimator (scm_to_vector3df (center),
278                                    scm_to_double (radius),
279                                    scm_to_double (speed),
280                                    scm_to_vector3df (direction),
281                                    scm_to_double (start_position),
282                                    scm_to_double (radius_ellipsoid));
283   return scm_from_irr_pointer ("<scene-node-animator>", (void*) anim);
284 }
285
286 SCM
287 ISceneManager_createFlyStraightAnimator (SCM scene_manager,
288                                          SCM start_point,
289                                          SCM end_point,
290                                          SCM time_for_way,
291                                          SCM loop,
292                                          SCM pingpong)
293 {
294   scene::ISceneManager* smgr = (scene::ISceneManager*) scm_to_irr_pointer (scene_manager);
295   scene::ISceneNodeAnimator* anim =
296     smgr->createFlyStraightAnimator (scm_to_vector3df (start_point),
297                                      scm_to_vector3df (end_point),
298                                      scm_to_uint32 (time_for_way),
299                                      scm_to_bool (loop),
300                                      scm_to_bool (pingpong));
301   return scm_from_irr_pointer ("<scene-node-animator>", (void*) anim);
302 }
303
304 SCM
305 ISceneManager_createRotationAnimator (SCM scene_manager,
306                                       SCM rotation_speed)
307 {
308   scene::ISceneManager* smgr = (scene::ISceneManager*) scm_to_irr_pointer (scene_manager);
309   scene::ISceneNodeAnimator* anim =
310     smgr->createRotationAnimator (scm_to_vector3df (rotation_speed));
311   return scm_from_irr_pointer ("<scene-node-animator>", (void*) anim);
312 }
313
314 SCM
315 ISceneManager_drawAll (SCM scene_manager)
316 {
317   ((scene::ISceneManager*) scm_to_irr_pointer (scene_manager))->drawAll ();
318   return SCM_UNSPECIFIED;
319 }
320
321 SCM
322 ISceneManager_getMesh (SCM scene_manager,
323                        SCM filename)
324 {
325   char* cfilename = scm_to_utf8_string (filename);
326   scene::ISceneManager* smgr = (scene::ISceneManager*) scm_to_irr_pointer (scene_manager);
327   scene::IAnimatedMesh* mesh = smgr->getMesh(cfilename);
328   free (cfilename);
329   return scm_from_irr_pointer ("<animated-mesh>", (void*) mesh);
330 }
331
332 SCM
333 ISceneManager_getRootSceneNode (SCM scene_manager)
334 {
335   scene::ISceneManager* smgr = (scene::ISceneManager*) scm_to_irr_pointer (scene_manager);
336   return scm_from_irr_pointer ("<scene-node>", (void*) smgr->getRootSceneNode ());
337 }
338
339 void
340 init_scene_manager (void)
341 {
342   DEFINE_GSUBR ("ISceneManager_addAnimatedMeshSceneNode_ISceneNode", 8, 0, 0,
343                 ISceneManager_addAnimatedMeshSceneNode<scene::ISceneNode*>);
344   DEFINE_GSUBR ("ISceneManager_addCameraSceneNode_ISceneNode", 6, 0, 0,
345                 ISceneManager_addCameraSceneNode<scene::ISceneNode*>);
346   DEFINE_GSUBR ("ISceneManager_addCameraSceneNodeFPS_ISceneNode", 1, 0, 1,
347                 ISceneManager_addCameraSceneNodeFPS<scene::ISceneNode*>);
348   DEFINE_GSUBR ("ISceneManager_addCubeSceneNode_ISceneNode", 7, 0, 0,
349                 ISceneManager_addCubeSceneNode<scene::ISceneNode*>);
350   DEFINE_GSUBR ("ISceneManager_addCustomSceneNode_ISceneNode", 10, 0, 0,
351                 ISceneManager_addCustomSceneNode<scene::ISceneNode*>);
352   DEFINE_GSUBR ("ISceneManager_addOctreeSceneNode_ISceneNode_IAnimatedMesh", 6, 0, 0,
353                 (ISceneManager_addOctreeSceneNode<scene::ISceneNode*, scene::IAnimatedMesh*>));
354   DEFINE_GSUBR ("ISceneManager_addOctreeSceneNode_ISceneNode_IMesh", 6, 0, 0,
355                 (ISceneManager_addOctreeSceneNode<scene::ISceneNode*, scene::IMesh*>));
356   DEFINE_GSUBR ("ISceneManager_addSphereSceneNode_ISceneNode", 8, 0, 0,
357                 ISceneManager_addSphereSceneNode<scene::ISceneNode*>);
358   DEFINE_GSUBR ("ISceneManager_createFlyCircleAnimator", 7, 0, 0,
359                 ISceneManager_createFlyCircleAnimator);
360   DEFINE_GSUBR ("ISceneManager_createFlyStraightAnimator", 6, 0, 0,
361                 ISceneManager_createFlyStraightAnimator);
362   DEFINE_GSUBR ("ISceneManager_createRotationAnimator", 2, 0, 0,
363                 ISceneManager_createRotationAnimator);
364   DEFINE_GSUBR ("ISceneManager_drawAll", 1, 0, 0, ISceneManager_drawAll);
365   DEFINE_GSUBR ("ISceneManager_getMesh", 2, 0, 0, ISceneManager_getMesh);
366   DEFINE_GSUBR ("ISceneManager_getRootSceneNode", 1, 0, 0, ISceneManager_getRootSceneNode);
367 }