Cycles: Fixes for recent refactor
[blender.git] / intern / cycles / app / cycles_xml.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18
19 #include <sstream>
20 #include <algorithm>
21 #include <iterator>
22
23 #include "node_xml.h"
24
25 #include "background.h"
26 #include "camera.h"
27 #include "film.h"
28 #include "graph.h"
29 #include "integrator.h"
30 #include "light.h"
31 #include "mesh.h"
32 #include "nodes.h"
33 #include "object.h"
34 #include "osl.h"
35 #include "shader.h"
36 #include "scene.h"
37
38 #include "subd_mesh.h"
39 #include "subd_patch.h"
40 #include "subd_split.h"
41
42 #include "util_debug.h"
43 #include "util_foreach.h"
44 #include "util_path.h"
45 #include "util_transform.h"
46 #include "util_xml.h"
47
48 #include "cycles_xml.h"
49
50 CCL_NAMESPACE_BEGIN
51
52 /* XML reading state */
53
54 struct XMLReadState : public XMLReader {
55         Scene *scene;           /* scene pointer */
56         Transform tfm;          /* current transform state */
57         bool smooth;            /* smooth normal state */
58         Shader *shader;         /* current shader */
59         string base;            /* base path to current file*/
60         float dicing_rate;      /* current dicing rate */
61         Mesh::DisplacementMethod displacement_method;
62
63         XMLReadState()
64           : scene(NULL),
65             smooth(false),
66             shader(NULL),
67             dicing_rate(0.0f),
68             displacement_method(Mesh::DISPLACE_BUMP)
69         {
70                 tfm = transform_identity();
71         }
72 };
73
74 /* Attribute Reading */
75
76 static bool xml_read_bool(bool *value, pugi::xml_node node, const char *name)
77 {
78         pugi::xml_attribute attr = node.attribute(name);
79
80         if(attr) {
81                 *value = (string_iequals(attr.value(), "true")) || (atoi(attr.value()) != 0);
82                 return true;
83         }
84
85         return false;
86 }
87
88 static bool xml_read_int(int *value, pugi::xml_node node, const char *name)
89 {
90         pugi::xml_attribute attr = node.attribute(name);
91
92         if(attr) {
93                 *value = atoi(attr.value());
94                 return true;
95         }
96
97         return false;
98 }
99
100 static bool xml_read_int_array(vector<int>& value, pugi::xml_node node, const char *name)
101 {
102         pugi::xml_attribute attr = node.attribute(name);
103
104         if(attr) {
105                 vector<string> tokens;
106                 string_split(tokens, attr.value());
107
108                 foreach(const string& token, tokens)
109                         value.push_back(atoi(token.c_str()));
110
111                 return true;
112         }
113
114         return false;
115 }
116
117 static bool xml_read_float(float *value, pugi::xml_node node, const char *name)
118 {
119         pugi::xml_attribute attr = node.attribute(name);
120
121         if(attr) {
122                 *value = (float)atof(attr.value());
123                 return true;
124         }
125
126         return false;
127 }
128
129 static bool xml_read_float_array(vector<float>& value, pugi::xml_node node, const char *name)
130 {
131         pugi::xml_attribute attr = node.attribute(name);
132
133         if(attr) {
134                 vector<string> tokens;
135                 string_split(tokens, attr.value());
136
137                 foreach(const string& token, tokens)
138                         value.push_back((float)atof(token.c_str()));
139
140                 return true;
141         }
142
143         return false;
144 }
145
146 static bool xml_read_float3(float3 *value, pugi::xml_node node, const char *name)
147 {
148         vector<float> array;
149
150         if(xml_read_float_array(array, node, name) && array.size() == 3) {
151                 *value = make_float3(array[0], array[1], array[2]);
152                 return true;
153         }
154
155         return false;
156 }
157
158 static bool xml_read_float3_array(vector<float3>& value, pugi::xml_node node, const char *name)
159 {
160         vector<float> array;
161
162         if(xml_read_float_array(array, node, name)) {
163                 for(size_t i = 0; i < array.size(); i += 3)
164                         value.push_back(make_float3(array[i+0], array[i+1], array[i+2]));
165
166                 return true;
167         }
168
169         return false;
170 }
171
172 static bool xml_read_float4(float4 *value, pugi::xml_node node, const char *name)
173 {
174         vector<float> array;
175
176         if(xml_read_float_array(array, node, name) && array.size() == 4) {
177                 *value = make_float4(array[0], array[1], array[2], array[3]);
178                 return true;
179         }
180
181         return false;
182 }
183
184 static bool xml_read_string(string *str, pugi::xml_node node, const char *name)
185 {
186         pugi::xml_attribute attr = node.attribute(name);
187
188         if(attr) {
189                 *str = attr.value();
190                 return true;
191         }
192
193         return false;
194 }
195
196 static bool xml_read_ustring(ustring *str, pugi::xml_node node, const char *name)
197 {
198         pugi::xml_attribute attr = node.attribute(name);
199
200         if(attr) {
201                 *str = ustring(attr.value());
202                 return true;
203         }
204
205         return false;
206 }
207
208 static bool xml_equal_string(pugi::xml_node node, const char *name, const char *value)
209 {
210         pugi::xml_attribute attr = node.attribute(name);
211
212         if(attr)
213                 return string_iequals(attr.value(), value);
214         
215         return false;
216 }
217
218 static bool xml_read_enum_value(int *value, NodeEnum& enm, pugi::xml_node node, const char *name)
219 {
220         pugi::xml_attribute attr = node.attribute(name);
221
222         if(attr) {
223                 ustring ustr(attr.value());
224
225                 if(enm.exists(ustr)) {
226                         *value = enm[ustr];
227                         return true;
228                 }
229                 else
230                         fprintf(stderr, "Unknown value \"%s\" for attribute \"%s\".\n", ustr.c_str(), name);
231         }
232
233         return false;
234 }
235
236 /* Camera */
237
238 static void xml_read_camera(XMLReadState& state, pugi::xml_node node)
239 {
240         Camera *cam = state.scene->camera;
241
242         xml_read_int(&cam->width, node, "width");
243         xml_read_int(&cam->height, node, "height");
244
245         xml_read_node(state, cam, node);
246
247         cam->matrix = state.tfm;
248
249         cam->need_update = true;
250         cam->update();
251 }
252
253 /* Shader */
254
255 static string xml_socket_name(const char *name)
256 {
257         string sname = name;
258         size_t i;
259
260         while((i = sname.find(" ")) != string::npos)
261                 sname.replace(i, 1, "");
262         
263         return sname;
264 }
265
266 static void xml_read_shader_graph(XMLReadState& state, Shader *shader, pugi::xml_node graph_node)
267 {
268         xml_read_node(state, shader, graph_node);
269
270         ShaderManager *manager = state.scene->shader_manager;
271         ShaderGraph *graph = new ShaderGraph();
272
273         map<string, ShaderNode*> nodemap;
274
275         nodemap["output"] = graph->output();
276
277         for(pugi::xml_node node = graph_node.first_child(); node; node = node.next_sibling()) {
278                 ShaderNode *snode = NULL;
279
280                 /* ToDo: Add missing nodes
281                  * RGBCurvesNode, VectorCurvesNode, RGBRampNode and ConvertNode (RGB -> BW).
282                  */
283
284                 if(string_iequals(node.name(), "image_texture")) {
285                         ImageTextureNode *img = new ImageTextureNode();
286
287                         xml_read_string(&img->filename, node, "src");
288                         img->filename = path_join(state.base, img->filename);
289                         
290                         xml_read_enum_value((int*)&img->color_space, ImageTextureNode::color_space_enum, node, "color_space");
291                         xml_read_enum_value((int*)&img->projection, ImageTextureNode::projection_enum, node, "projection");
292                         xml_read_float(&img->projection_blend, node, "projection_blend");
293
294                         /* ToDo: Interpolation */
295
296                         snode = img;
297                 }
298                 else if(string_iequals(node.name(), "environment_texture")) {
299                         EnvironmentTextureNode *env = new EnvironmentTextureNode();
300
301                         xml_read_string(&env->filename, node, "src");
302                         env->filename = path_join(state.base, env->filename);
303                         
304                         xml_read_enum_value((int*)&env->color_space, EnvironmentTextureNode::color_space_enum, node, "color_space");
305                         xml_read_enum_value((int*)&env->projection, EnvironmentTextureNode::projection_enum, node, "projection");
306
307                         snode = env;
308                 }
309 #ifdef WITH_OSL
310                 else if(string_iequals(node.name(), "osl_shader")) {
311                         if(manager->use_osl()) {
312                                 std::string filepath;
313
314                                 if(xml_read_string(&filepath, node, "src")) {
315                                         if(path_is_relative(filepath)) {
316                                                 filepath = path_join(state.base, filepath);
317                                         }
318
319                                         snode = ((OSLShaderManager*)manager)->osl_node(filepath);
320
321                                         if(!snode) {
322                                                 fprintf(stderr, "Failed to create OSL node from \"%s\".\n", filepath.c_str());
323                                         }
324                                 }
325                                 else {
326                                         fprintf(stderr, "OSL node missing \"src\" attribute.\n");
327                                 }
328                         }
329                         else {
330                                 fprintf(stderr, "OSL node without using --shadingsys osl.\n");
331                         }
332                 }
333 #endif
334                 else if(string_iequals(node.name(), "sky_texture")) {
335                         SkyTextureNode *sky = new SkyTextureNode();
336                         
337                         xml_read_enum_value((int*)&sky->type, SkyTextureNode::type_enum, node, "type");
338                         xml_read_float3(&sky->sun_direction, node, "sun_direction");
339                         xml_read_float(&sky->turbidity, node, "turbidity");
340                         xml_read_float(&sky->ground_albedo, node, "ground_albedo");
341                         
342                         snode = sky;
343                 }
344                 else if(string_iequals(node.name(), "noise_texture")) {
345                         snode = new NoiseTextureNode();
346                 }
347                 else if(string_iequals(node.name(), "checker_texture")) {
348                         snode = new CheckerTextureNode();
349                 }
350                 else if(string_iequals(node.name(), "brick_texture")) {
351                         BrickTextureNode *brick = new BrickTextureNode();
352
353                         xml_read_float(&brick->offset, node, "offset");
354                         xml_read_int(&brick->offset_frequency, node, "offset_frequency");
355                         xml_read_float(&brick->squash, node, "squash");
356                         xml_read_int(&brick->squash_frequency, node, "squash_frequency");
357
358                         snode = brick;
359                 }
360                 else if(string_iequals(node.name(), "gradient_texture")) {
361                         GradientTextureNode *blend = new GradientTextureNode();
362                         xml_read_enum_value((int*)&blend->type, GradientTextureNode::type_enum, node, "type");
363                         snode = blend;
364                 }
365                 else if(string_iequals(node.name(), "voronoi_texture")) {
366                         VoronoiTextureNode *voronoi = new VoronoiTextureNode();
367                         xml_read_enum_value((int*)&voronoi->coloring, VoronoiTextureNode::coloring_enum, node, "coloring");
368                         snode = voronoi;
369                 }
370                 else if(string_iequals(node.name(), "musgrave_texture")) {
371                         MusgraveTextureNode *musgrave = new MusgraveTextureNode();
372                         xml_read_enum_value((int*)&musgrave->type, MusgraveTextureNode::type_enum, node, "type");
373                         snode = musgrave;
374                 }
375                 else if(string_iequals(node.name(), "magic_texture")) {
376                         MagicTextureNode *magic = new MagicTextureNode();
377                         xml_read_int(&magic->depth, node, "depth");
378                         snode = magic;
379                 }
380                 else if(string_iequals(node.name(), "wave_texture")) {
381                         WaveTextureNode *wave = new WaveTextureNode();
382                         xml_read_enum_value((int*)&wave->type, WaveTextureNode::type_enum, node, "type");
383                         xml_read_enum_value((int*)&wave->profile, WaveTextureNode::profile_enum, node, "profile");
384                         snode = wave;
385                 }
386                 else if(string_iequals(node.name(), "normal")) {
387                         NormalNode *normal = new NormalNode();
388                         xml_read_float3(&normal->direction, node, "direction");
389                         snode = normal;
390                 }
391                 else if(string_iequals(node.name(), "bump")) {
392                         BumpNode *bump = new BumpNode();
393                         xml_read_bool(&bump->invert, node, "invert");
394                         snode = bump;
395                 }
396                 else if(string_iequals(node.name(), "mapping")) {
397                         MappingNode *map = new MappingNode();
398
399                         TextureMapping *texmap = &map->tex_mapping;
400                         xml_read_enum_value((int*) &texmap->type, TextureMapping::type_enum, node, "type");
401                         xml_read_enum_value((int*) &texmap->projection, TextureMapping::projection_enum, node, "projection");
402                         xml_read_enum_value((int*) &texmap->x_mapping, TextureMapping::mapping_enum, node, "x_mapping");
403                         xml_read_enum_value((int*) &texmap->y_mapping, TextureMapping::mapping_enum, node, "y_mapping");
404                         xml_read_enum_value((int*) &texmap->z_mapping, TextureMapping::mapping_enum, node, "z_mapping");
405                         xml_read_bool(&texmap->use_minmax, node, "use_minmax");
406                         if(texmap->use_minmax) {
407                                 xml_read_float3(&texmap->min, node, "min");
408                                 xml_read_float3(&texmap->max, node, "max");
409                         }
410                         xml_read_float3(&texmap->translation, node, "translation");
411                         xml_read_float3(&texmap->rotation, node, "rotation");
412                         xml_read_float3(&texmap->scale, node, "scale");
413
414                         snode = map;
415                 }
416                 else if(string_iequals(node.name(), "anisotropic_bsdf")) {
417                         AnisotropicBsdfNode *aniso = new AnisotropicBsdfNode();
418                         xml_read_enum_value((int*)&aniso->distribution, AnisotropicBsdfNode::distribution_enum, node, "distribution");
419                         snode = aniso;
420                 }
421                 else if(string_iequals(node.name(), "diffuse_bsdf")) {
422                         snode = new DiffuseBsdfNode();
423                 }
424                 else if(string_iequals(node.name(), "translucent_bsdf")) {
425                         snode = new TranslucentBsdfNode();
426                 }
427                 else if(string_iequals(node.name(), "transparent_bsdf")) {
428                         snode = new TransparentBsdfNode();
429                 }
430                 else if(string_iequals(node.name(), "velvet_bsdf")) {
431                         snode = new VelvetBsdfNode();
432                 }
433                 else if(string_iequals(node.name(), "toon_bsdf")) {
434                         ToonBsdfNode *toon = new ToonBsdfNode();
435                         xml_read_enum_value((int*)&toon->component, ToonBsdfNode::component_enum, node, "component");
436                         snode = toon;
437                 }
438                 else if(string_iequals(node.name(), "glossy_bsdf")) {
439                         GlossyBsdfNode *glossy = new GlossyBsdfNode();
440                         xml_read_enum_value((int*)&glossy->distribution, GlossyBsdfNode::distribution_enum, node, "distribution");
441                         snode = glossy;
442                 }
443                 else if(string_iequals(node.name(), "glass_bsdf")) {
444                         GlassBsdfNode *diel = new GlassBsdfNode();
445                         xml_read_enum_value((int*)&diel->distribution, GlassBsdfNode::distribution_enum, node, "distribution");
446                         snode = diel;
447                 }
448                 else if(string_iequals(node.name(), "refraction_bsdf")) {
449                         RefractionBsdfNode *diel = new RefractionBsdfNode();
450                         xml_read_enum_value((int*)&diel->distribution, RefractionBsdfNode::distribution_enum, node, "distribution");
451                         snode = diel;
452                 }
453                 else if(string_iequals(node.name(), "hair_bsdf")) {
454                         HairBsdfNode *hair = new HairBsdfNode();
455                         xml_read_enum_value((int*)&hair->component, HairBsdfNode::component_enum, node, "component");
456                         snode = hair;
457                 }
458                 else if(string_iequals(node.name(), "emission")) {
459                         snode = new EmissionNode();
460                 }
461                 else if(string_iequals(node.name(), "ambient_occlusion")) {
462                         snode = new AmbientOcclusionNode();
463                 }
464                 else if(string_iequals(node.name(), "background")) {
465                         snode = new BackgroundNode();
466                 }
467                 else if(string_iequals(node.name(), "holdout")) {
468                         snode = new HoldoutNode();
469                 }
470                 else if(string_iequals(node.name(), "absorption_volume")) {
471                         snode = new AbsorptionVolumeNode();
472                 }
473                 else if(string_iequals(node.name(), "scatter_volume")) {
474                         snode = new ScatterVolumeNode();
475                 }
476                 else if(string_iequals(node.name(), "subsurface_scattering")) {
477                         SubsurfaceScatteringNode *sss = new SubsurfaceScatteringNode();
478
479                         string falloff;
480                         xml_read_string(&falloff, node, "falloff");
481                         if(falloff == "cubic")
482                                 sss->closure = CLOSURE_BSSRDF_CUBIC_ID;
483                         else if(falloff == "gaussian")
484                                 sss->closure = CLOSURE_BSSRDF_GAUSSIAN_ID;
485                         else /*if(falloff == "burley")*/
486                                 sss->closure = CLOSURE_BSSRDF_BURLEY_ID;
487
488                         snode = sss;
489                 }
490                 else if(string_iequals(node.name(), "geometry")) {
491                         snode = new GeometryNode();
492                 }
493                 else if(string_iequals(node.name(), "texture_coordinate")) {
494                         snode = new TextureCoordinateNode();
495                 }
496                 else if(string_iequals(node.name(), "light_path")) {
497                         snode = new LightPathNode();
498                 }
499                 else if(string_iequals(node.name(), "light_falloff")) {
500                         snode = new LightFalloffNode();
501                 }
502                 else if(string_iequals(node.name(), "object_info")) {
503                         snode = new ObjectInfoNode();
504                 }
505                 else if(string_iequals(node.name(), "particle_info")) {
506                         snode = new ParticleInfoNode();
507                 }
508                 else if(string_iequals(node.name(), "hair_info")) {
509                         snode = new HairInfoNode();
510                 }
511                 else if(string_iequals(node.name(), "value")) {
512                         ValueNode *value = new ValueNode();
513                         xml_read_float(&value->value, node, "value");
514                         snode = value;
515                 }
516                 else if(string_iequals(node.name(), "color")) {
517                         ColorNode *color = new ColorNode();
518                         xml_read_float3(&color->value, node, "value");
519                         snode = color;
520                 }
521                 else if(string_iequals(node.name(), "mix_closure")) {
522                         snode = new MixClosureNode();
523                 }
524                 else if(string_iequals(node.name(), "add_closure")) {
525                         snode = new AddClosureNode();
526                 }
527                 else if(string_iequals(node.name(), "invert")) {
528                         snode = new InvertNode();
529                 }
530                 else if(string_iequals(node.name(), "mix")) {
531                         /* ToDo: Tag Mix case for optimization */
532                         MixNode *mix = new MixNode();
533                         xml_read_enum_value((int*)&mix->type, MixNode::type_enum, node, "type");
534                         xml_read_bool(&mix->use_clamp, node, "use_clamp");
535                         snode = mix;
536                 }
537                 else if(string_iequals(node.name(), "gamma")) {
538                         snode = new GammaNode();
539                 }
540                 else if(string_iequals(node.name(), "brightness")) {
541                         snode = new BrightContrastNode();
542                 }
543                 else if(string_iequals(node.name(), "combine_rgb")) {
544                         snode = new CombineRGBNode();
545                 }
546                 else if(string_iequals(node.name(), "separate_rgb")) {
547                         snode = new SeparateRGBNode();
548                 }
549                 else if(string_iequals(node.name(), "combine_hsv")) {
550                         snode = new CombineHSVNode();
551                 }
552                 else if(string_iequals(node.name(), "separate_hsv")) {
553                         snode = new SeparateHSVNode();
554                 }
555                 else if(string_iequals(node.name(), "combine_xyz")) {
556                         snode = new CombineXYZNode();
557                 }
558                 else if(string_iequals(node.name(), "separate_xyz")) {
559                         snode = new SeparateXYZNode();
560                 }
561                 else if(string_iequals(node.name(), "hsv")) {
562                         snode = new HSVNode();
563                 }
564                 else if(string_iequals(node.name(), "wavelength")) {
565                         snode = new WavelengthNode();
566                 }
567                 else if(string_iequals(node.name(), "blackbody")) {
568                         snode = new BlackbodyNode();
569                 }
570                 else if(string_iequals(node.name(), "attribute")) {
571                         AttributeNode *attr = new AttributeNode();
572                         xml_read_ustring(&attr->attribute, node, "attribute");
573                         snode = attr;
574                 }
575                 else if(string_iequals(node.name(), "uv_map")) {
576                         UVMapNode *uvm = new UVMapNode();
577                         xml_read_ustring(&uvm->attribute, node, "uv_map");
578                         snode = uvm;
579                 }
580                 else if(string_iequals(node.name(), "camera")) {
581                         snode = new CameraNode();
582                 }
583                 else if(string_iequals(node.name(), "fresnel")) {
584                         snode = new FresnelNode();
585                 }
586                 else if(string_iequals(node.name(), "layer_weight")) {
587                         snode = new LayerWeightNode();
588                 }
589                 else if(string_iequals(node.name(), "wireframe")) {
590                         WireframeNode *wire = new WireframeNode;
591                         xml_read_bool(&wire->use_pixel_size, node, "use_pixel_size");
592                         snode = wire;
593                 }
594                 else if(string_iequals(node.name(), "normal_map")) {
595                         NormalMapNode *nmap = new NormalMapNode;
596                         xml_read_ustring(&nmap->attribute, node, "attribute");
597                         xml_read_enum_value((int*)&nmap->space, NormalMapNode::space_enum, node, "space");
598                         snode = nmap;
599                 }
600                 else if(string_iequals(node.name(), "tangent")) {
601                         TangentNode *tangent = new TangentNode;
602                         xml_read_ustring(&tangent->attribute, node, "attribute");
603                         xml_read_enum_value((int*)&tangent->direction_type, TangentNode::direction_type_enum, node, "direction_type");
604                         xml_read_enum_value((int*)&tangent->axis, TangentNode::axis_enum, node, "axis");
605                         snode = tangent;
606                 }
607                 else if(string_iequals(node.name(), "math")) {
608                         MathNode *math = new MathNode();
609                         xml_read_enum_value((int*)&math->type, MathNode::type_enum, node, "type");
610                         xml_read_bool(&math->use_clamp, node, "use_clamp");
611                         snode = math;
612                 }
613                 else if(string_iequals(node.name(), "vector_math")) {
614                         VectorMathNode *vmath = new VectorMathNode();
615                         xml_read_enum_value((int*)&vmath->type, VectorMathNode::type_enum, node, "type");
616                         snode = vmath;
617                 }
618                 else if(string_iequals(node.name(), "vector_transform")) {
619                         VectorTransformNode *vtransform = new VectorTransformNode();
620                         xml_read_enum_value((int*)&vtransform->type, VectorTransformNode::type_enum, node, "type");
621                         xml_read_enum_value((int*)&vtransform->convert_from, VectorTransformNode::convert_space_enum, node, "convert_from");
622                         xml_read_enum_value((int*)&vtransform->convert_to, VectorTransformNode::convert_space_enum, node, "convert_to");
623                         snode = vtransform;
624                 }
625                 else if(string_iequals(node.name(), "connect")) {
626                         /* connect nodes */
627                         vector<string> from_tokens, to_tokens;
628
629                         string_split(from_tokens, node.attribute("from").value());
630                         string_split(to_tokens, node.attribute("to").value());
631
632                         if(from_tokens.size() == 2 && to_tokens.size() == 2) {
633                                 /* find nodes and sockets */
634                                 ShaderOutput *output = NULL;
635                                 ShaderInput *input = NULL;
636
637                                 if(nodemap.find(from_tokens[0]) != nodemap.end()) {
638                                         ShaderNode *fromnode = nodemap[from_tokens[0]];
639
640                                         foreach(ShaderOutput *out, fromnode->outputs)
641                                                 if(string_iequals(xml_socket_name(out->name().c_str()), from_tokens[1]))
642                                                         output = out;
643
644                                         if(!output)
645                                                 fprintf(stderr, "Unknown output socket name \"%s\" on \"%s\".\n", from_tokens[1].c_str(), from_tokens[0].c_str());
646                                 }
647                                 else
648                                         fprintf(stderr, "Unknown shader node name \"%s\".\n", from_tokens[0].c_str());
649
650                                 if(nodemap.find(to_tokens[0]) != nodemap.end()) {
651                                         ShaderNode *tonode = nodemap[to_tokens[0]];
652
653                                         foreach(ShaderInput *in, tonode->inputs)
654                                                 if(string_iequals(xml_socket_name(in->name().c_str()), to_tokens[1]))
655                                                         input = in;
656
657                                         if(!input)
658                                                 fprintf(stderr, "Unknown input socket name \"%s\" on \"%s\".\n", to_tokens[1].c_str(), to_tokens[0].c_str());
659                                 }
660                                 else
661                                         fprintf(stderr, "Unknown shader node name \"%s\".\n", to_tokens[0].c_str());
662
663                                 /* connect */
664                                 if(output && input)
665                                         graph->connect(output, input);
666                         }
667                         else
668                                 fprintf(stderr, "Invalid from or to value for connect node.\n");
669                 }
670                 else
671                         fprintf(stderr, "Unknown shader node \"%s\".\n", node.name());
672
673                 if(snode) {
674                         /* add to graph */
675                         graph->add(snode);
676
677                         /* add to map for name lookups */
678                         string name = "";
679                         xml_read_string(&name, node, "name");
680
681                         nodemap[name] = snode;
682
683                         /* read input values */
684                         for(pugi::xml_attribute attr = node.first_attribute(); attr; attr = attr.next_attribute()) {
685                                 foreach(ShaderInput *in, snode->inputs) {
686                                         if(string_iequals(in->name().c_str(), attr.name())) {
687                                                 switch(in->type()) {
688                                                         case SocketType::FLOAT:
689                                                         case SocketType::INT:
690                                                                 xml_read_float(&in->value_float(), node, attr.name());
691                                                                 break;
692                                                         case SocketType::COLOR:
693                                                         case SocketType::VECTOR:
694                                                         case SocketType::POINT:
695                                                         case SocketType::NORMAL:
696                                                                 xml_read_float3(&in->value(), node, attr.name());
697                                                                 break;
698                                                         case SocketType::STRING:
699                                                                 xml_read_ustring( &in->value_string(), node, attr.name() );
700                                                                 break;
701                                                         default:
702                                                                 break;
703                                                 }
704                                         }
705                                 }
706                         }
707                 }
708         }
709
710         shader->set_graph(graph);
711         shader->tag_update(state.scene);
712 }
713
714 static void xml_read_shader(XMLReadState& state, pugi::xml_node node)
715 {
716         Shader *shader = new Shader();
717         xml_read_shader_graph(state, shader, node);
718         state.scene->shaders.push_back(shader);
719 }
720
721 /* Background */
722
723 static void xml_read_background(XMLReadState& state, pugi::xml_node node)
724 {
725         /* Background Settings */
726         xml_read_node(state, state.scene->background, node);
727
728         /* Background Shader */
729         Shader *shader = state.scene->default_background;
730         xml_read_shader_graph(state, shader, node);
731 }
732
733 /* Mesh */
734
735 static Mesh *xml_add_mesh(Scene *scene, const Transform& tfm)
736 {
737         /* create mesh */
738         Mesh *mesh = new Mesh();
739         scene->meshes.push_back(mesh);
740
741         /* create object*/
742         Object *object = new Object();
743         object->mesh = mesh;
744         object->tfm = tfm;
745         scene->objects.push_back(object);
746
747         return mesh;
748 }
749
750 static void xml_read_mesh(const XMLReadState& state, pugi::xml_node node)
751 {
752         /* add mesh */
753         Mesh *mesh = xml_add_mesh(state.scene, state.tfm);
754         mesh->used_shaders.push_back(state.shader);
755
756         /* read state */
757         int shader = 0;
758         bool smooth = state.smooth;
759
760         mesh->displacement_method = state.displacement_method;
761
762         /* read vertices and polygons, RIB style */
763         vector<float3> P;
764         vector<float> UV;
765         vector<int> verts, nverts;
766
767         xml_read_float3_array(P, node, "P");
768         xml_read_int_array(verts, node, "verts");
769         xml_read_int_array(nverts, node, "nverts");
770
771         if(xml_equal_string(node, "subdivision", "catmull-clark")) {
772                 /* create subd mesh */
773                 SubdMesh sdmesh;
774
775                 /* create subd vertices */
776                 for(size_t i = 0; i < P.size(); i++)
777                         sdmesh.add_vert(P[i]);
778
779                 /* create subd faces */
780                 int index_offset = 0;
781
782                 for(size_t i = 0; i < nverts.size(); i++) {
783                         if(nverts[i] == 4) {
784                                 int v0 = verts[index_offset + 0];
785                                 int v1 = verts[index_offset + 1];
786                                 int v2 = verts[index_offset + 2];
787                                 int v3 = verts[index_offset + 3];
788
789                                 sdmesh.add_face(v0, v1, v2, v3);
790                         }
791                         else {
792                                 for(int j = 0; j < nverts[i]-2; j++) {
793                                         int v0 = verts[index_offset];
794                                         int v1 = verts[index_offset + j + 1];
795                                         int v2 = verts[index_offset + j + 2];
796
797                                         sdmesh.add_face(v0, v1, v2);
798                                 }
799                         }
800
801                         index_offset += nverts[i];
802                 }
803
804                 /* finalize subd mesh */
805                 sdmesh.finish();
806
807                 /* parameters */
808                 SubdParams sdparams(mesh, shader, smooth);
809                 xml_read_float(&sdparams.dicing_rate, node, "dicing_rate");
810
811                 DiagSplit dsplit(sdparams);
812                 sdmesh.tessellate(&dsplit);
813         }
814         else {
815                 /* create vertices */
816                 mesh->verts = P;
817
818                 size_t num_triangles = 0;
819                 for(size_t i = 0; i < nverts.size(); i++)
820                         num_triangles += nverts[i]-2;
821                 mesh->reserve_mesh(mesh->verts.size(), num_triangles);
822
823                 /* create triangles */
824                 int index_offset = 0;
825
826                 for(size_t i = 0; i < nverts.size(); i++) {
827                         for(int j = 0; j < nverts[i]-2; j++) {
828                                 int v0 = verts[index_offset];
829                                 int v1 = verts[index_offset + j + 1];
830                                 int v2 = verts[index_offset + j + 2];
831
832                                 assert(v0 < (int)P.size());
833                                 assert(v1 < (int)P.size());
834                                 assert(v2 < (int)P.size());
835
836                                 mesh->add_triangle(v0, v1, v2, shader, smooth);
837                         }
838
839                         index_offset += nverts[i];
840                 }
841
842                 if(xml_read_float_array(UV, node, "UV")) {
843                         ustring name = ustring("UVMap");
844                         Attribute *attr = mesh->attributes.add(ATTR_STD_UV, name);
845                         float3 *fdata = attr->data_float3();
846
847                         /* loop over the triangles */
848                         index_offset = 0;
849                         for(size_t i = 0; i < nverts.size(); i++) {
850                                 for(int j = 0; j < nverts[i]-2; j++) {
851                                         int v0 = index_offset;
852                                         int v1 = index_offset + j + 1;
853                                         int v2 = index_offset + j + 2;
854
855                                         assert(v0*2+1 < (int)UV.size());
856                                         assert(v1*2+1 < (int)UV.size());
857                                         assert(v2*2+1 < (int)UV.size());
858
859                                         fdata[0] = make_float3(UV[v0*2], UV[v0*2+1], 0.0);
860                                         fdata[1] = make_float3(UV[v1*2], UV[v1*2+1], 0.0);
861                                         fdata[2] = make_float3(UV[v2*2], UV[v2*2+1], 0.0);
862                                         fdata += 3;
863                                 }
864
865                                 index_offset += nverts[i];
866                         }
867                 }
868         }
869
870         /* temporary for test compatibility */
871         mesh->attributes.remove(ATTR_STD_VERTEX_NORMAL);
872 }
873
874 /* Patch */
875
876 static void xml_read_patch(const XMLReadState& state, pugi::xml_node node)
877 {
878         /* read patch */
879         Patch *patch = NULL;
880
881         vector<float3> P;
882         xml_read_float3_array(P, node, "P");
883
884         if(xml_equal_string(node, "type", "bilinear")) {
885                 /* bilinear patch */
886                 if(P.size() == 4) {
887                         LinearQuadPatch *bpatch = new LinearQuadPatch();
888
889                         for(int i = 0; i < 4; i++)
890                                 P[i] = transform_point(&state.tfm, P[i]);
891                         memcpy(bpatch->hull, &P[0], sizeof(bpatch->hull));
892
893                         patch = bpatch;
894                 }
895                 else
896                         fprintf(stderr, "Invalid number of control points for bilinear patch.\n");
897         }
898         else if(xml_equal_string(node, "type", "bicubic")) {
899                 /* bicubic patch */
900                 if(P.size() == 16) {
901                         BicubicPatch *bpatch = new BicubicPatch();
902
903                         for(int i = 0; i < 16; i++)
904                                 P[i] = transform_point(&state.tfm, P[i]);
905                         memcpy(bpatch->hull, &P[0], sizeof(bpatch->hull));
906
907                         patch = bpatch;
908                 }
909                 else
910                         fprintf(stderr, "Invalid number of control points for bicubic patch.\n");
911         }
912         else
913                 fprintf(stderr, "Unknown patch type.\n");
914
915         if(patch) {
916                 /* add mesh */
917                 Mesh *mesh = xml_add_mesh(state.scene, transform_identity());
918
919                 mesh->used_shaders.push_back(state.shader);
920
921                 /* split */
922                 SubdParams sdparams(mesh, 0, state.smooth);
923                 xml_read_float(&sdparams.dicing_rate, node, "dicing_rate");
924
925                 DiagSplit dsplit(sdparams);
926                 dsplit.split_quad(patch);
927
928                 delete patch;
929
930                 /* temporary for test compatibility */
931                 mesh->attributes.remove(ATTR_STD_VERTEX_NORMAL);
932         }
933 }
934
935 /* Light */
936
937 static void xml_read_light(XMLReadState& state, pugi::xml_node node)
938 {
939         Light *light = new Light();
940
941         light->shader = state.shader;
942         xml_read_node(state, light, node);
943
944         state.scene->lights.push_back(light);
945 }
946
947 /* Transform */
948
949 static void xml_read_transform(pugi::xml_node node, Transform& tfm)
950 {
951         if(node.attribute("matrix")) {
952                 vector<float> matrix;
953                 if(xml_read_float_array(matrix, node, "matrix") && matrix.size() == 16)
954                         tfm = tfm * transform_transpose((*(Transform*)&matrix[0]));
955         }
956
957         if(node.attribute("translate")) {
958                 float3 translate = make_float3(0.0f, 0.0f, 0.0f);
959                 xml_read_float3(&translate, node, "translate");
960                 tfm = tfm * transform_translate(translate);
961         }
962
963         if(node.attribute("rotate")) {
964                 float4 rotate = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
965                 xml_read_float4(&rotate, node, "rotate");
966                 tfm = tfm * transform_rotate(DEG2RADF(rotate.x), make_float3(rotate.y, rotate.z, rotate.w));
967         }
968
969         if(node.attribute("scale")) {
970                 float3 scale = make_float3(0.0f, 0.0f, 0.0f);
971                 xml_read_float3(&scale, node, "scale");
972                 tfm = tfm * transform_scale(scale);
973         }
974 }
975
976 /* State */
977
978 static void xml_read_state(XMLReadState& state, pugi::xml_node node)
979 {
980         /* read shader */
981         string shadername;
982
983         if(xml_read_string(&shadername, node, "shader")) {
984                 bool found = false;
985
986                 foreach(Shader *shader, state.scene->shaders) {
987                         if(shader->name == shadername) {
988                                 state.shader = shader;
989                                 found = true;
990                                 break;
991                         }
992                 }
993
994                 if(!found)
995                         fprintf(stderr, "Unknown shader \"%s\".\n", shadername.c_str());
996         }
997
998         xml_read_float(&state.dicing_rate, node, "dicing_rate");
999
1000         /* read smooth/flat */
1001         if(xml_equal_string(node, "interpolation", "smooth"))
1002                 state.smooth = true;
1003         else if(xml_equal_string(node, "interpolation", "flat"))
1004                 state.smooth = false;
1005
1006         /* read displacement method */
1007         if(xml_equal_string(node, "displacement_method", "true"))
1008                 state.displacement_method = Mesh::DISPLACE_TRUE;
1009         else if(xml_equal_string(node, "displacement_method", "bump"))
1010                 state.displacement_method = Mesh::DISPLACE_BUMP;
1011         else if(xml_equal_string(node, "displacement_method", "both"))
1012                 state.displacement_method = Mesh::DISPLACE_BOTH;
1013 }
1014
1015 /* Scene */
1016
1017 static void xml_read_include(XMLReadState& state, const string& src);
1018
1019 static void xml_read_scene(XMLReadState& state, pugi::xml_node scene_node)
1020 {
1021         for(pugi::xml_node node = scene_node.first_child(); node; node = node.next_sibling()) {
1022                 if(string_iequals(node.name(), "film")) {
1023                         xml_read_node(state, state.scene->film, node);
1024                 }
1025                 else if(string_iequals(node.name(), "integrator")) {
1026                         xml_read_node(state, state.scene->integrator, node);
1027                 }
1028                 else if(string_iequals(node.name(), "camera")) {
1029                         xml_read_camera(state, node);
1030                 }
1031                 else if(string_iequals(node.name(), "shader")) {
1032                         xml_read_shader(state, node);
1033                 }
1034                 else if(string_iequals(node.name(), "background")) {
1035                         xml_read_background(state, node);
1036                 }
1037                 else if(string_iequals(node.name(), "mesh")) {
1038                         xml_read_mesh(state, node);
1039                 }
1040                 else if(string_iequals(node.name(), "patch")) {
1041                         xml_read_patch(state, node);
1042                 }
1043                 else if(string_iequals(node.name(), "light")) {
1044                         xml_read_light(state, node);
1045                 }
1046                 else if(string_iequals(node.name(), "transform")) {
1047                         XMLReadState substate = state;
1048
1049                         xml_read_transform(node, substate.tfm);
1050                         xml_read_scene(substate, node);
1051                 }
1052                 else if(string_iequals(node.name(), "state")) {
1053                         XMLReadState substate = state;
1054
1055                         xml_read_state(substate, node);
1056                         xml_read_scene(substate, node);
1057                 }
1058                 else if(string_iequals(node.name(), "include")) {
1059                         string src;
1060
1061                         if(xml_read_string(&src, node, "src"))
1062                                 xml_read_include(state, src);
1063                 }
1064                 else
1065                         fprintf(stderr, "Unknown node \"%s\".\n", node.name());
1066         }
1067 }
1068
1069 /* Include */
1070
1071 static void xml_read_include(XMLReadState& state, const string& src)
1072 {
1073         /* open XML document */
1074         pugi::xml_document doc;
1075         pugi::xml_parse_result parse_result;
1076
1077         string path = path_join(state.base, src);
1078         parse_result = doc.load_file(path.c_str());
1079
1080         if(parse_result) {
1081                 XMLReadState substate = state;
1082                 substate.base = path_dirname(path);
1083
1084                 pugi::xml_node cycles = doc.child("cycles");
1085                 xml_read_scene(substate, cycles);
1086         }
1087         else {
1088                 fprintf(stderr, "%s read error: %s\n", src.c_str(), parse_result.description());
1089                 exit(EXIT_FAILURE);
1090         }
1091 }
1092
1093 /* File */
1094
1095 void xml_read_file(Scene *scene, const char *filepath)
1096 {
1097         XMLReadState state;
1098
1099         state.scene = scene;
1100         state.tfm = transform_identity();
1101         state.shader = scene->default_surface;
1102         state.smooth = false;
1103         state.dicing_rate = 0.1f;
1104         state.base = path_dirname(filepath);
1105
1106         xml_read_include(state, path_filename(filepath));
1107
1108         scene->params.bvh_type = SceneParams::BVH_STATIC;
1109 }
1110
1111 CCL_NAMESPACE_END
1112