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