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