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