]> git.jsancho.org Git - guile-irrlicht.git/blob - src/material.cpp
Material creation
[guile-irrlicht.git] / src / material.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 "material.h"
27 #include "material-types.h"
28 #include "wrapped.h"
29
30 extern "C" {
31
32   void
33   init_material (void)
34   {
35     init_material_type ();
36     scm_c_define_gsubr ("make-material", 0, 0, 1, (scm_t_subr)make_material);
37     scm_c_export ("make-material", NULL);
38   }
39
40   DEFINE_WRAPPED_TYPE (irr::video::SMaterial*, "material",
41                        init_material_type, material_p,
42                        wrap_material, unwrap_material);
43
44   SCM
45   make_material (SCM rest)
46   {
47     SCM material_type = scm_from_utf8_symbol ("solid");
48     SCM ambient_color = scm_list_4 (scm_from_uint32 (255), scm_from_uint32 (255),
49                                     scm_from_uint32 (255), scm_from_uint32 (255));
50     SCM diffuse_color = scm_list_4 (scm_from_uint32 (255), scm_from_uint32 (255),
51                                     scm_from_uint32 (255), scm_from_uint32 (255));
52     SCM emissive_color = scm_list_4 (scm_from_uint32 (255), scm_from_uint32 (255),
53                                      scm_from_uint32 (255), scm_from_uint32 (255));
54     SCM specular_color = scm_list_4 (scm_from_uint32 (255), scm_from_uint32 (255),
55                                      scm_from_uint32 (255), scm_from_uint32 (255));
56     SCM shininess = scm_from_double (0.0f);
57     SCM material_type_param = scm_from_double (0.0f);
58     SCM material_type_param_2 = scm_from_double (0.0f);
59     SCM thickness = scm_from_double (1.0f);
60     SCM z_buffer = scm_from_utf8_symbol ("less-equal");
61     SCM anti_aliasing = scm_from_utf8_symbol ("simple");
62     SCM color_mask = scm_from_utf8_symbol ("all");
63     SCM color_material = scm_from_utf8_symbol ("diffuse");
64     SCM blend_operation = scm_from_utf8_symbol ("none");
65     SCM polygon_offset_factor = scm_from_uint8 (0);
66     SCM polygon_offset_direction = scm_from_utf8_symbol ("front");
67     SCM wireframe = scm_from_bool (0);
68     SCM point_cloud = scm_from_bool (0);
69     SCM gouraud_shading = scm_from_bool (1);
70     SCM lighting = scm_from_bool (1);
71     SCM z_write_enable = scm_from_bool (1);
72     SCM backface_culling = scm_from_bool (1);
73     SCM frontface_culling = scm_from_bool (0);
74     SCM fog_enable = scm_from_bool (0);
75     SCM normalize_normals = scm_from_bool (0);
76     SCM use_mip_maps = scm_from_bool (1);
77
78     scm_c_bind_keyword_arguments
79       ("make-material", rest, (scm_t_keyword_arguments_flags)0,
80        scm_from_utf8_keyword ("material-type"), &material_type,
81        scm_from_utf8_keyword ("ambient-color"), &ambient_color,
82        scm_from_utf8_keyword ("diffuse-color"), &diffuse_color,
83        scm_from_utf8_keyword ("emissive-color"), &emissive_color,
84        scm_from_utf8_keyword ("specular-color"), &specular_color,
85        scm_from_utf8_keyword ("shininess"), &shininess,
86        scm_from_utf8_keyword ("material-type-param"), &material_type_param,
87        scm_from_utf8_keyword ("material-type-param-2"), &material_type_param_2,
88        scm_from_utf8_keyword ("thickness"), &thickness,
89        scm_from_utf8_keyword ("z-buffer"), &z_buffer,
90        scm_from_utf8_keyword ("anti-aliasing"), &anti_aliasing,
91        scm_from_utf8_keyword ("color-mask"), &color_mask,
92        scm_from_utf8_keyword ("color-material"), &color_material,
93        scm_from_utf8_keyword ("blend-operation"), &blend_operation,
94        scm_from_utf8_keyword ("polygon-offset-factor"), &polygon_offset_factor,
95        scm_from_utf8_keyword ("polygon-offset-direction"), &polygon_offset_direction,
96        scm_from_utf8_keyword ("wireframe"), &wireframe,
97        scm_from_utf8_keyword ("point-cloud"), &point_cloud,
98        scm_from_utf8_keyword ("gouraud-shading"), &gouraud_shading,
99        scm_from_utf8_keyword ("lighting"), &lighting,
100        scm_from_utf8_keyword ("z-write-enable"), &z_write_enable,
101        scm_from_utf8_keyword ("backface-culling"), &backface_culling,
102        scm_from_utf8_keyword ("frontface-culling"), &frontface_culling,
103        scm_from_utf8_keyword ("fog-enable"), &fog_enable,
104        scm_from_utf8_keyword ("normalize-normals"), &normalize_normals,
105        scm_from_utf8_keyword ("use-mip-maps"), &use_mip_maps,
106        SCM_UNDEFINED);
107
108     irr::video::SMaterial* material = new irr::video::SMaterial ();
109     material->MaterialType = scm_to_material_type(material_type);
110     material->AmbientColor = scm_to_color (ambient_color);
111     material->DiffuseColor = scm_to_color (diffuse_color);
112     material->EmissiveColor = scm_to_color (emissive_color);
113     material->SpecularColor = scm_to_color (specular_color);
114     material->Shininess = scm_to_double (shininess);
115     material->MaterialTypeParam = scm_to_double (material_type_param);
116     material->MaterialTypeParam2 = scm_to_double (material_type_param_2);
117     material->Thickness = scm_to_double (thickness);
118     material->ZBuffer = scm_to_comparison_func (z_buffer);
119     material->AntiAliasing = scm_to_anti_aliasing_mode (anti_aliasing);
120     material->ColorMask = scm_to_color_plane (color_mask);
121     material->ColorMaterial = scm_to_color_material (color_material);
122     material->BlendOperation = scm_to_blend_operation (blend_operation);
123     material->PolygonOffsetFactor = scm_to_uint8 (polygon_offset_factor);
124     material->PolygonOffsetDirection = scm_to_polygon_offset (polygon_offset_direction);
125     material->Wireframe = scm_to_bool (wireframe);
126     material->PointCloud = scm_to_bool (point_cloud);
127     material->GouraudShading = scm_to_bool (gouraud_shading);
128     material->Lighting = scm_to_bool (lighting);
129     material->ZWriteEnable = scm_to_bool (z_write_enable);
130     material->BackfaceCulling = scm_to_bool (backface_culling);
131     material->FrontfaceCulling = scm_to_bool (frontface_culling);
132     material->FogEnable = scm_to_bool (fog_enable);
133     material->NormalizeNormals = scm_to_bool (normalize_normals);
134     material->UseMipMaps = scm_to_bool (use_mip_maps);
135     return wrap_material (material);
136   }
137
138   irr::video::E_ANTI_ALIASING_MODE
139   scm_to_anti_aliasing_mode (SCM anti_aliasing_mode)
140   {
141     char* mode = scm_to_utf8_stringn (scm_symbol_to_string (anti_aliasing_mode), NULL);
142     if (!strcmp (mode, "off"))
143       {
144         return irr::video::EAAM_OFF;
145       }
146     else if (!strcmp (mode, "simple"))
147       {
148         return irr::video::EAAM_SIMPLE;
149       }
150     else if (!strcmp (mode, "quality"))
151       {
152         return irr::video::EAAM_QUALITY;
153       }
154     else if (!strcmp (mode, "line-smooth"))
155       {
156         return irr::video::EAAM_LINE_SMOOTH;
157       }
158     else if (!strcmp (mode, "point-smooth"))
159       {
160         return irr::video::EAAM_POINT_SMOOTH;
161       }
162     else if (!strcmp (mode, "full-basic"))
163       {
164         return irr::video::EAAM_FULL_BASIC;
165       }
166     else if (!strcmp (mode, "alpha-to-coverage"))
167       {
168         return irr::video::EAAM_ALPHA_TO_COVERAGE;
169       }
170     else
171       {
172         scm_error (scm_arg_type_key, NULL, "Wrong anti aliasing mode: ~S",
173                    scm_list_1 (anti_aliasing_mode), scm_list_1 (anti_aliasing_mode));
174       }
175   }
176
177   irr::video::E_BLEND_OPERATION
178   scm_to_blend_operation (SCM blend_operation)
179   {
180     char* operation = scm_to_utf8_stringn (scm_symbol_to_string (blend_operation), NULL);
181     if (!strcmp (operation, "none"))
182       {
183         return irr::video::EBO_NONE;
184       }
185     else if (!strcmp (operation, "add"))
186       {
187         return irr::video::EBO_ADD;
188       }
189     else if (!strcmp (operation, "subtract"))
190       {
191         return irr::video::EBO_SUBTRACT;
192       }
193     else if (!strcmp (operation, "rev-subtract"))
194       {
195         return irr::video::EBO_REVSUBTRACT;
196       }
197     else if (!strcmp (operation, "min"))
198       {
199         return irr::video::EBO_MIN;
200       }
201     else if (!strcmp (operation, "max"))
202       {
203         return irr::video::EBO_MAX;
204       }
205     else if (!strcmp (operation, "min-factor"))
206       {
207         return irr::video::EBO_MIN_FACTOR;
208       }
209     else if (!strcmp (operation, "max-factor"))
210       {
211         return irr::video::EBO_MAX_FACTOR;
212       }
213     else if (!strcmp (operation, "min-alpha"))
214       {
215         return irr::video::EBO_MIN_ALPHA;
216       }
217     else if (!strcmp (operation, "max-alpha"))
218       {
219         return irr::video::EBO_MAX_ALPHA;
220       }
221     else
222       {
223         scm_error (scm_arg_type_key, NULL, "Wrong blend operation: ~S",
224                    scm_list_1 (blend_operation), scm_list_1 (blend_operation));
225       }
226   }
227
228   irr::video::E_COLOR_MATERIAL
229   scm_to_color_material (SCM color_material)
230   {
231     char* material = scm_to_utf8_stringn (scm_symbol_to_string (color_material), NULL);
232     if (!strcmp (material, "none"))
233       {
234         return irr::video::ECM_NONE;
235       }
236     else if (!strcmp (material, "diffuse"))
237       {
238         return irr::video::ECM_DIFFUSE;
239       }
240     else if (!strcmp (material, "ambient"))
241       {
242         return irr::video::ECM_AMBIENT;
243       }
244     else if (!strcmp (material, "emissive"))
245       {
246         return irr::video::ECM_EMISSIVE;
247       }
248     else if (!strcmp (material, "specular"))
249       {
250         return irr::video::ECM_SPECULAR;
251       }
252     else if (!strcmp (material, "diffuse-and-ambient"))
253       {
254         return irr::video::ECM_DIFFUSE_AND_AMBIENT;
255       }
256     else
257       {
258         scm_error (scm_arg_type_key, NULL, "Wrong color material: ~S",
259                    scm_list_1 (color_material), scm_list_1 (color_material));
260       }
261   }
262
263   irr::video::E_COLOR_PLANE
264   scm_to_color_plane (SCM color_plane)
265   {
266     char* plane = scm_to_utf8_stringn (scm_symbol_to_string (color_plane), NULL);
267     if (!strcmp (plane, "none"))
268       {
269         return irr::video::ECP_NONE;
270       }
271     else if (!strcmp (plane, "alpha"))
272       {
273         return irr::video::ECP_ALPHA;
274       }
275     else if (!strcmp (plane, "red"))
276       {
277         return irr::video::ECP_RED;
278       }
279     else if (!strcmp (plane, "green"))
280       {
281         return irr::video::ECP_GREEN;
282       }
283     else if (!strcmp (plane, "blue"))
284       {
285         return irr::video::ECP_BLUE;
286       }
287     else if (!strcmp (plane, "rgb"))
288       {
289         return irr::video::ECP_RGB;
290       }
291     else if (!strcmp (plane, "all"))
292       {
293         return irr::video::ECP_ALL;
294       }
295     else
296       {
297         scm_error (scm_arg_type_key, NULL, "Wrong color plane: ~S",
298                    scm_list_1 (color_plane), scm_list_1 (color_plane));
299       }
300   }
301
302   irr::video::E_COMPARISON_FUNC
303   scm_to_comparison_func (SCM comparison_func)
304   {
305     char* func = scm_to_utf8_stringn (scm_symbol_to_string (comparison_func), NULL);
306     if (!strcmp (func, "never"))
307       {
308         return irr::video::ECFN_NEVER;
309       }
310     else if (!strcmp (func, "less-equal"))
311       {
312         return irr::video::ECFN_LESSEQUAL;
313       }
314     else if (!strcmp (func, "equal"))
315       {
316         return irr::video::ECFN_EQUAL;
317       }
318     else if (!strcmp (func, "less"))
319       {
320         return irr::video::ECFN_LESS;
321       }
322     else if (!strcmp (func, "not-equal"))
323       {
324         return irr::video::ECFN_NOTEQUAL;
325       }
326     else if (!strcmp (func, "greater-equal"))
327       {
328         return irr::video::ECFN_GREATEREQUAL;
329       }
330     else if (!strcmp (func, "greater"))
331       {
332         return irr::video::ECFN_GREATER;
333       }
334     else if (!strcmp (func, "always"))
335       {
336         return irr::video::ECFN_ALWAYS;
337       }
338     else
339       {
340         scm_error (scm_arg_type_key, NULL, "Wrong comparison func: ~S",
341                    scm_list_1 (comparison_func), scm_list_1 (comparison_func));
342       }
343   }
344
345   irr::video::E_POLYGON_OFFSET
346   scm_to_polygon_offset (SCM polygon_offset)
347   {
348     char* offset = scm_to_utf8_stringn (scm_symbol_to_string (polygon_offset), NULL);
349     if (!strcmp (offset, "back"))
350       {
351         return irr::video::EPO_BACK;
352       }
353     else if (!strcmp (offset, "front"))
354       {
355         return irr::video::EPO_FRONT;
356       }
357     else
358       {
359         scm_error (scm_arg_type_key, NULL, "Wrong polygon offset: ~S",
360                    scm_list_1 (polygon_offset), scm_list_1 (polygon_offset));
361       }
362   }
363
364 }