]> git.jsancho.org Git - guile-irrlicht.git/blob - src/video-driver.cpp
draw-vertex-primitive-list
[guile-irrlicht.git] / src / video-driver.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 "color.h"
26 #include "device.h"
27 #include "gui-environment.h"
28 #include "material.h"
29 #include "matrix4.h"
30 #include "primitive-types.h"
31 #include "rect.h"
32 #include "scene-manager.h"
33 #include "texture.h"
34 #include "vertex3d.h"
35 #include "video-driver.h"
36 #include "wrapped.h"
37
38 extern "C" {
39
40   void
41   init_video_driver (void)
42   {
43     init_video_driver_type ();
44     scm_c_define_gsubr ("begin-scene", 1, 0, 1, (scm_t_subr)irr_video_beginScene);
45     scm_c_define_gsubr ("draw-vertex-primitive-list", 3, 0, 1,
46                         (scm_t_subr)irr_video_drawVertexPrimitiveList);
47     scm_c_define_gsubr ("end-scene", 1, 0, 0, (scm_t_subr)irr_video_endScene);
48     scm_c_define_gsubr ("get-fps", 1, 0, 0, (scm_t_subr)irr_video_getFPS);
49     scm_c_define_gsubr ("get-texture", 2, 0, 0, (scm_t_subr)irr_video_getTexture);
50     scm_c_define_gsubr ("get-video-driver", 1, 0, 0, (scm_t_subr)irr_getVideoDriver);
51     scm_c_define_gsubr ("set-transform!", 3, 0, 0, (scm_t_subr)irr_video_setTransform);
52     scm_c_export ("begin-scene", "draw-vertex-primitive-list", "end-scene", "get-fps",
53                   "get-texture", "get-video-driver", "set-transform!", NULL);
54   }
55
56   DEFINE_WRAPPED_TYPE (irr::video::IVideoDriver*, "video-driver",
57                        init_video_driver_type, video_driver_p,
58                        wrap_video_driver, unwrap_video_driver);
59
60   SCM
61   irr_video_beginScene (SCM wrapped_video_driver,
62                         SCM rest)
63   {
64     SCM back_buffer = scm_from_bool(1);
65     SCM z_buffer = scm_from_bool(1);
66     SCM color = scm_list_4 (scm_from_uint32 (255),
67                             scm_from_uint32 (0),
68                             scm_from_uint32 (0),
69                             scm_from_uint32 (0));
70     SCM video_data = scm_from_bool(0);
71     SCM source_rect = scm_from_bool(0);
72
73     scm_c_bind_keyword_arguments ("begin-scene", rest, (scm_t_keyword_arguments_flags)0,
74                                   scm_from_utf8_keyword ("back-buffer"), &back_buffer,
75                                   scm_from_utf8_keyword ("z-buffer"), &z_buffer,
76                                   scm_from_utf8_keyword ("color"), &color,
77                                   scm_from_utf8_keyword ("video-data"), &video_data,
78                                   scm_from_utf8_keyword ("source-rect"), &source_rect,
79                                   SCM_UNDEFINED);
80
81     irr::video::IVideoDriver* driver = unwrap_video_driver (wrapped_video_driver);
82     irr::core::rect<irr::s32>* sourceRectAddress = 0;
83     if (!scm_is_false (source_rect))
84       {
85         irr::core::rect<irr::s32> sourceRect = scm_to_rect_s32 (source_rect);
86         sourceRectAddress = &sourceRect;
87       }
88     return scm_from_bool (driver->beginScene (scm_to_bool (back_buffer),
89                                               scm_to_bool (z_buffer),
90                                               scm_to_color (color),
91                                               irr::video::SExposedVideoData (),
92                                               sourceRectAddress));
93   }
94
95   SCM
96   irr_video_drawVertexPrimitiveList (SCM wrapped_video_driver,
97                                      SCM vertices,
98                                      SCM indices,
99                                      SCM rest)
100   {
101     SCM v_type = scm_from_utf8_symbol ("standard");
102     SCM p_type = scm_from_utf8_symbol ("triangles");
103
104     scm_c_bind_keyword_arguments ("draw-vertex-primitive-list", rest, (scm_t_keyword_arguments_flags)0,
105                                   scm_from_utf8_keyword ("vertex-type"), &v_type,
106                                   scm_from_utf8_keyword ("primitive-type"), &p_type,
107                                   SCM_UNDEFINED);
108
109     // Build vertex array
110     irr::u32 vertex_count = scm_to_uint32 (scm_length (vertices));
111     irr::video::S3DVertex s3d_vertices [vertex_count];
112     for (int i = 0; i < vertex_count; i++)
113       {
114         irr::video::S3DVertex* vertex = unwrap_vertex3d (scm_list_ref (vertices, scm_from_int (i)));
115         s3d_vertices[i] = irr::video::S3DVertex (vertex->Pos,
116                                                  vertex->Normal,
117                                                  vertex->Color,
118                                                  vertex->TCoords);
119       }
120
121     // Build index array
122     irr::u32 index_count = scm_to_uint32 (scm_length (indices));
123     SCM flat_indices = scm_apply_0 (scm_eval_string (scm_from_utf8_string ("append")),
124                                     indices);
125     int flat_length = scm_to_int (scm_length (flat_indices));
126     irr::u32 c_indices [flat_length];
127     for (int i = 0; i < flat_length; i++)
128       {
129         c_indices[i] = scm_to_uint32 (scm_list_ref (flat_indices, scm_from_int (i)));
130       }
131
132     // Draw vertices
133     irr::video::IVideoDriver* driver = unwrap_video_driver (wrapped_video_driver);
134     driver->drawVertexPrimitiveList (&s3d_vertices[0],
135                                      vertex_count,
136                                      &c_indices[0],
137                                      index_count,
138                                      scm_to_vertex_type (v_type),
139                                      scm_to_primitive_type (p_type),
140                                      irr::video::EIT_32BIT);
141     return SCM_UNSPECIFIED;
142   }
143
144   SCM
145   irr_video_endScene (SCM wrapped_video_driver)
146   {
147     irr::video::IVideoDriver* driver = unwrap_video_driver (wrapped_video_driver);
148     return scm_from_bool (driver->endScene ());
149   }
150
151   SCM
152   irr_video_getFPS (SCM wrapped_video_driver)
153   {
154     irr::video::IVideoDriver* driver = unwrap_video_driver (wrapped_video_driver);
155     return scm_from_int32 (driver->getFPS ());
156   }
157
158   SCM
159   irr_video_getTexture (SCM wrapped_video_driver,
160                         SCM filename)
161   {
162     irr::video::IVideoDriver* driver = unwrap_video_driver (wrapped_video_driver);
163     irr::video::ITexture* texture = driver->getTexture (scm_to_utf8_stringn (filename, NULL));
164     return wrap_texture (texture);
165   }
166
167   SCM
168   irr_video_setMaterial (SCM wrapped_video_driver,
169                          SCM material)
170   {
171     irr::video::IVideoDriver* driver = unwrap_video_driver (wrapped_video_driver);
172     driver->setMaterial (*(unwrap_material (material)));
173     return SCM_UNSPECIFIED;
174   }
175
176   SCM
177   irr_video_setTransform (SCM wrapped_video_driver,
178                           SCM state,
179                           SCM mat)
180   {
181     irr::video::IVideoDriver* driver = unwrap_video_driver (wrapped_video_driver);
182     driver->setTransform (scm_to_transformation_state (state),
183                           scm_to_matrix4 (mat));
184     return SCM_UNSPECIFIED;
185   }
186
187   SCM
188   irr_getVideoDriver (SCM wrapped_obj)
189   {
190     irr::video::IVideoDriver* driver;
191     if (device_p (wrapped_obj))
192       {
193         driver = unwrap_device (wrapped_obj)->getVideoDriver ();
194       }
195     else
196       {
197         scm_error (scm_arg_type_key, NULL, "Cannot get video driver from object: ~S",
198                    scm_list_1 (wrapped_obj), scm_list_1 (wrapped_obj));
199       }
200     return wrap_video_driver (driver);
201   }
202
203   irr::video::E_TRANSFORMATION_STATE
204   scm_to_transformation_state (SCM transformation_state)
205   {
206     char* state = scm_to_utf8_stringn (scm_symbol_to_string (transformation_state), NULL);
207     if (!strcmp (state, "view"))
208       {
209         return irr::video::ETS_VIEW;
210       }
211     else if (!strcmp (state, "world"))
212       {
213         return irr::video::ETS_WORLD;
214       }
215     else if (!strcmp (state, "projection"))
216       {
217         return irr::video::ETS_PROJECTION;
218       }
219     else if (!strcmp (state, "texture0"))
220       {
221         return irr::video::ETS_TEXTURE_0;
222       }
223     else if (!strcmp (state, "texture1"))
224       {
225         return irr::video::ETS_TEXTURE_1;
226       }
227     else if (!strcmp (state, "texture2"))
228       {
229         return irr::video::ETS_TEXTURE_2;
230       }
231     else if (!strcmp (state, "texture3"))
232       {
233         return irr::video::ETS_TEXTURE_3;
234       }
235     else
236       {
237         scm_error (scm_arg_type_key, NULL, "Wrong transformation state: ~S",
238                    scm_list_1 (transformation_state), scm_list_1 (transformation_state));
239       }
240   }
241
242 }