Invert Color Cycles Node
[blender.git] / intern / cycles / app / cycles_xml.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include <stdio.h>
20
21 #include <sstream>
22 #include <algorithm>
23 #include <iterator>
24
25 #include "camera.h"
26 #include "film.h"
27 #include "graph.h"
28 #include "integrator.h"
29 #include "light.h"
30 #include "mesh.h"
31 #include "nodes.h"
32 #include "object.h"
33 #include "shader.h"
34 #include "scene.h"
35
36 #include "subd_mesh.h"
37 #include "subd_patch.h"
38 #include "subd_split.h"
39
40 #include "util_debug.h"
41 #include "util_foreach.h"
42 #include "util_path.h"
43 #include "util_transform.h"
44 #include "util_xml.h"
45
46 #include "cycles_xml.h"
47
48 CCL_NAMESPACE_BEGIN
49
50 /* XML reading state */
51
52 struct XMLReadState {
53         Scene *scene;           /* scene pointer */
54         Transform tfm;          /* current transform state */
55         bool smooth;            /* smooth normal state */
56         int shader;                     /* current shader */
57         string base;            /* base path to current file*/
58         float dicing_rate;      /* current dicing rate */
59         Mesh::DisplacementMethod displacement_method;
60 };
61
62 /* Attribute Reading */
63
64 static bool xml_read_bool(bool *value, pugi::xml_node node, const char *name)
65 {
66         pugi::xml_attribute attr = node.attribute(name);
67
68         if(attr) {
69                 *value = (string_iequals(attr.value(), "true")) || (atoi(attr.value()) != 0);
70                 return true;
71         }
72
73         return false;
74 }
75
76 static bool xml_read_int(int *value, pugi::xml_node node, const char *name)
77 {
78         pugi::xml_attribute attr = node.attribute(name);
79
80         if(attr) {
81                 *value = atoi(attr.value());
82                 return true;
83         }
84
85         return false;
86 }
87
88 static bool xml_read_int_array(vector<int>& value, pugi::xml_node node, const char *name)
89 {
90         pugi::xml_attribute attr = node.attribute(name);
91
92         if(attr) {
93                 vector<string> tokens;
94                 string_split(tokens, attr.value());
95
96                 foreach(const string& token, tokens)
97                         value.push_back(atoi(token.c_str()));
98
99                 return true;
100         }
101
102         return false;
103 }
104
105 static bool xml_read_float(float *value, pugi::xml_node node, const char *name)
106 {
107         pugi::xml_attribute attr = node.attribute(name);
108
109         if(attr) {
110                 *value = atof(attr.value());
111                 return true;
112         }
113
114         return false;
115 }
116
117 static bool xml_read_float_array(vector<float>& value, pugi::xml_node node, const char *name)
118 {
119         pugi::xml_attribute attr = node.attribute(name);
120
121         if(attr) {
122                 vector<string> tokens;
123                 string_split(tokens, attr.value());
124
125                 foreach(const string& token, tokens)
126                         value.push_back(atof(token.c_str()));
127
128                 return true;
129         }
130
131         return false;
132 }
133
134 static bool xml_read_float3(float3 *value, pugi::xml_node node, const char *name)
135 {
136         vector<float> array;
137
138         if(xml_read_float_array(array, node, name) && array.size() == 3) {
139                 *value = make_float3(array[0], array[1], array[2]);
140                 return true;
141         }
142
143         return false;
144 }
145
146 static bool xml_read_float3_array(vector<float3>& value, pugi::xml_node node, const char *name)
147 {
148         vector<float> array;
149
150         if(xml_read_float_array(array, node, name)) {
151                 for(size_t i = 0; i < array.size(); i += 3)
152                         value.push_back(make_float3(array[i+0], array[i+1], array[i+2]));
153
154                 return true;
155         }
156
157         return false;
158 }
159
160 static bool xml_read_float4(float4 *value, pugi::xml_node node, const char *name)
161 {
162         vector<float> array;
163
164         if(xml_read_float_array(array, node, name) && array.size() == 4) {
165                 *value = make_float4(array[0], array[1], array[2], array[3]);
166                 return true;
167         }
168
169         return false;
170 }
171
172 static bool xml_read_string(string *str, pugi::xml_node node, const char *name)
173 {
174         pugi::xml_attribute attr = node.attribute(name);
175
176         if(attr) {
177                 *str = attr.value();
178                 return true;
179         }
180
181         return false;
182 }
183
184 static bool xml_read_ustring(ustring *str, pugi::xml_node node, const char *name)
185 {
186         pugi::xml_attribute attr = node.attribute(name);
187
188         if(attr) {
189                 *str = ustring(attr.value());
190                 return true;
191         }
192
193         return false;
194 }
195
196 static bool xml_equal_string(pugi::xml_node node, const char *name, const char *value)
197 {
198         pugi::xml_attribute attr = node.attribute(name);
199
200         if(attr)
201                 return string_iequals(attr.value(), value);
202         
203         return false;
204 }
205
206 static bool xml_read_enum(ustring *str, ShaderEnum& enm, pugi::xml_node node, const char *name)
207 {
208         pugi::xml_attribute attr = node.attribute(name);
209
210         if(attr) {
211                 ustring ustr(attr.value());
212
213                 if(enm.exists(ustr)) {
214                         *str = ustr;
215                         return true;
216                 }
217                 else
218                         fprintf(stderr, "Unknown value \"%s\" for attribute \"%s\".\n", ustr.c_str(), name);
219         }
220
221         return false;
222 }
223
224 /* Film */
225
226 static void xml_read_film(const XMLReadState& state, pugi::xml_node node)
227 {
228         Camera *cam = state.scene->camera;
229
230         xml_read_int(&cam->width, node, "width");
231         xml_read_int(&cam->height, node, "height");
232
233         float aspect = (float)cam->width/(float)cam->height;
234
235         if(cam->width >= cam->height) {
236                 cam->left = -aspect;
237                 cam->right = aspect;
238                 cam->bottom = -1.0f;
239                 cam->top = 1.0f;
240         }
241         else {
242                 cam->left = -1.0f;
243                 cam->right = 1.0f;
244                 cam->bottom = -1.0f/aspect;
245                 cam->top = 1.0f/aspect;
246         }
247
248         cam->need_update = true;
249         cam->update();
250 }
251
252 /* Integrator */
253
254 static void xml_read_integrator(const XMLReadState& state, pugi::xml_node node)
255 {
256         Integrator *integrator = state.scene->integrator;
257
258         xml_read_int(&integrator->min_bounce, node, "min_bounce");
259         xml_read_int(&integrator->max_bounce, node, "max_bounce");
260         xml_read_bool(&integrator->no_caustics, node, "no_caustics");
261         xml_read_float(&integrator->blur_caustics, node, "blur_caustics");
262 }
263
264 /* Camera */
265
266 static void xml_read_camera(const XMLReadState& state, pugi::xml_node node)
267 {
268         Camera *cam = state.scene->camera;
269
270         if(xml_read_float(&cam->fov, node, "fov"))
271                 cam->fov *= M_PI/180.0f;
272
273         xml_read_float(&cam->nearclip, node, "nearclip");
274         xml_read_float(&cam->farclip, node, "farclip");
275         xml_read_float(&cam->aperturesize, node, "aperturesize"); // 0.5*focallength/fstop
276         xml_read_float(&cam->focaldistance, node, "focaldistance");
277         xml_read_float(&cam->shutteropen, node, "shutteropen");
278         xml_read_float(&cam->shutterclose, node, "shutterclose");
279
280         if(xml_equal_string(node, "type", "orthographic"))
281                 cam->ortho = true;
282         else if(xml_equal_string(node, "type", "perspective"))
283                 cam->ortho = false;
284
285         cam->matrix = state.tfm;
286
287         cam->need_update = true;
288         cam->update();
289 }
290
291 /* Shader */
292
293 static string xml_socket_name(const char *name)
294 {
295         string sname = name;
296         size_t i;
297
298         while((i = sname.find(" ")) != string::npos)
299                 sname.replace(i, 1, "");
300         
301         return sname;
302 }
303
304 static void xml_read_shader_graph(const XMLReadState& state, Shader *shader, pugi::xml_node graph_node)
305 {
306         ShaderGraph *graph = new ShaderGraph();
307
308         map<string, ShaderNode*> nodemap;
309
310         nodemap["output"] = graph->output();
311
312         for(pugi::xml_node node = graph_node.first_child(); node; node = node.next_sibling()) {
313                 ShaderNode *snode = NULL;
314
315                 if(string_iequals(node.name(), "image_texture")) {
316                         ImageTextureNode *img = new ImageTextureNode();
317
318                         xml_read_string(&img->filename, node, "src");
319                         img->filename = path_join(state.base, img->filename);
320
321                         snode = img;
322                 }
323                 else if(string_iequals(node.name(), "environment_texture")) {
324                         EnvironmentTextureNode *env = new EnvironmentTextureNode();
325
326                         xml_read_string(&env->filename, node, "src");
327                         env->filename = path_join(state.base, env->filename);
328
329                         snode = env;
330                 }
331                 else if(string_iequals(node.name(), "sky_texture")) {
332                         SkyTextureNode *sky = new SkyTextureNode();
333
334                         xml_read_float3(&sky->sun_direction, node, "sun_direction");
335                         xml_read_float(&sky->turbidity, node, "turbidity");
336                         
337                         snode = sky;
338                 }
339                 else if(string_iequals(node.name(), "noise_texture")) {
340                         snode = new NoiseTextureNode();
341                 }
342                 else if(string_iequals(node.name(), "gradient_texture")) {
343                         GradientTextureNode *blend = new GradientTextureNode();
344                         xml_read_enum(&blend->type, GradientTextureNode::type_enum, node, "type");
345                         snode = blend;
346                 }
347                 else if(string_iequals(node.name(), "voronoi_texture")) {
348                         VoronoiTextureNode *voronoi = new VoronoiTextureNode();
349                         xml_read_enum(&voronoi->coloring, VoronoiTextureNode::coloring_enum, node, "coloring");
350                         snode = voronoi;
351                 }
352                 else if(string_iequals(node.name(), "musgrave_texture")) {
353                         MusgraveTextureNode *musgrave = new MusgraveTextureNode();
354                         xml_read_enum(&musgrave->type, MusgraveTextureNode::type_enum, node, "type");
355                         snode = musgrave;
356                 }
357                 else if(string_iequals(node.name(), "magic_texture")) {
358                         MagicTextureNode *magic = new MagicTextureNode();
359                         xml_read_int(&magic->depth, node, "depth");
360                         snode = magic;
361                 }
362                 else if(string_iequals(node.name(), "noise_texture")) {
363                         NoiseTextureNode *dist = new NoiseTextureNode();
364                         snode = dist;
365                 }
366                 else if(string_iequals(node.name(), "wave_texture")) {
367                         WaveTextureNode *wood = new WaveTextureNode();
368                         xml_read_enum(&wood->type, WaveTextureNode::type_enum, node, "type");
369                         snode = wood;
370                 }
371                 else if(string_iequals(node.name(), "mapping")) {
372                         snode = new MappingNode();
373                 }
374                 else if(string_iequals(node.name(), "ward_bsdf")) {
375                         snode = new WardBsdfNode();
376                 }
377                 else if(string_iequals(node.name(), "diffuse_bsdf")) {
378                         snode = new DiffuseBsdfNode();
379                 }
380                 else if(string_iequals(node.name(), "translucent_bsdf")) {
381                         snode = new TranslucentBsdfNode();
382                 }
383                 else if(string_iequals(node.name(), "transparent_bsdf")) {
384                         snode = new TransparentBsdfNode();
385                 }
386                 else if(string_iequals(node.name(), "velvet_bsdf")) {
387                         snode = new VelvetBsdfNode();
388                 }
389                 else if(string_iequals(node.name(), "glossy_bsdf")) {
390                         GlossyBsdfNode *glossy = new GlossyBsdfNode();
391                         xml_read_enum(&glossy->distribution, GlossyBsdfNode::distribution_enum, node, "distribution");
392                         snode = glossy;
393                 }
394                 else if(string_iequals(node.name(), "glass_bsdf")) {
395                         GlassBsdfNode *diel = new GlassBsdfNode();
396                         xml_read_enum(&diel->distribution, GlassBsdfNode::distribution_enum, node, "distribution");
397                         snode = diel;
398                 }
399                 else if(string_iequals(node.name(), "emission")) {
400                         EmissionNode *emission = new EmissionNode();
401                         xml_read_bool(&emission->total_power, node, "total_power");
402                         snode = emission;
403                 }
404                 else if(string_iequals(node.name(), "background")) {
405                         snode = new BackgroundNode();
406                 }
407                 else if(string_iequals(node.name(), "transparent_volume")) {
408                         snode = new TransparentVolumeNode();
409                 }
410                 else if(string_iequals(node.name(), "isotropic_volume")) {
411                         snode = new IsotropicVolumeNode();
412                 }
413                 else if(string_iequals(node.name(), "geometry")) {
414                         snode = new GeometryNode();
415                 }
416                 else if(string_iequals(node.name(), "texture_coordinate")) {
417                         snode = new TextureCoordinateNode();
418                 }
419                 else if(string_iequals(node.name(), "lightPath")) {
420                         snode = new LightPathNode();
421                 }
422                 else if(string_iequals(node.name(), "value")) {
423                         ValueNode *value = new ValueNode();
424                         xml_read_float(&value->value, node, "value");
425                         snode = value;
426                 }
427                 else if(string_iequals(node.name(), "color")) {
428                         ColorNode *color = new ColorNode();
429                         xml_read_float3(&color->value, node, "value");
430                         snode = color;
431                 }
432                 else if(string_iequals(node.name(), "mix_closure")) {
433                         snode = new MixClosureNode();
434                 }
435                 else if(string_iequals(node.name(), "add_closure")) {
436                         snode = new AddClosureNode();
437                 }
438                 else if(string_iequals(node.name(), "invert")) {
439                         snode = new InvertNode();
440                 }
441                 else if(string_iequals(node.name(), "mix")) {
442                         MixNode *mix = new MixNode();
443                         xml_read_enum(&mix->type, MixNode::type_enum, node, "type");
444                         snode = mix;
445                 }
446                 else if(string_iequals(node.name(), "combine_rgb")) {
447                         snode = new CombineRGBNode();
448                 }
449                 else if(string_iequals(node.name(), "separate_rgb")) {
450                         snode = new SeparateRGBNode();
451                 }
452                 else if(string_iequals(node.name(), "hsv")) {
453                         snode = new HSVNode();
454                 }
455                 else if(string_iequals(node.name(), "attribute")) {
456                         AttributeNode *attr = new AttributeNode();
457                         xml_read_ustring(&attr->attribute, node, "attribute");
458                         snode = attr;
459                 }
460                 else if(string_iequals(node.name(), "camera")) {
461                         snode = new CameraNode();
462                 }
463                 else if(string_iequals(node.name(), "fresnel")) {
464                         snode = new FresnelNode();
465                 }
466                 else if(string_iequals(node.name(), "math")) {
467                         MathNode *math = new MathNode();
468                         xml_read_enum(&math->type, MathNode::type_enum, node, "type");
469                         snode = math;
470                 }
471                 else if(string_iequals(node.name(), "vector_math")) {
472                         VectorMathNode *vmath = new VectorMathNode();
473                         xml_read_enum(&vmath->type, VectorMathNode::type_enum, node, "type");
474                         snode = vmath;
475                 }
476                 else if(string_iequals(node.name(), "connect")) {
477                         /* connect nodes */
478                         vector<string> from_tokens, to_tokens;
479
480                         string_split(from_tokens, node.attribute("from").value());
481                         string_split(to_tokens, node.attribute("to").value());
482
483                         if(from_tokens.size() == 2 && to_tokens.size() == 2) {
484                                 /* find nodes and sockets */
485                                 ShaderOutput *output = NULL;
486                                 ShaderInput *input = NULL;
487
488                                 if(nodemap.find(from_tokens[0]) != nodemap.end()) {
489                                         ShaderNode *fromnode = nodemap[from_tokens[0]];
490
491                                         foreach(ShaderOutput *out, fromnode->outputs)
492                                                 if(string_iequals(xml_socket_name(out->name), from_tokens[1]))
493                                                         output = out;
494
495                                         if(!output)
496                                                 fprintf(stderr, "Unknown output socket name \"%s\" on \"%s\".\n", from_tokens[1].c_str(), from_tokens[0].c_str());
497                                 }
498                                 else
499                                         fprintf(stderr, "Unknown shader node name \"%s\".\n", from_tokens[0].c_str());
500
501                                 if(nodemap.find(to_tokens[0]) != nodemap.end()) {
502                                         ShaderNode *tonode = nodemap[to_tokens[0]];
503
504                                         foreach(ShaderInput *in, tonode->inputs)
505                                                 if(string_iequals(xml_socket_name(in->name), to_tokens[1]))
506                                                         input = in;
507
508                                         if(!input)
509                                                 fprintf(stderr, "Unknown input socket name \"%s\" on \"%s\".\n", to_tokens[1].c_str(), to_tokens[0].c_str());
510                                 }
511                                 else
512                                         fprintf(stderr, "Unknown shader node name \"%s\".\n", to_tokens[0].c_str());
513
514                                 /* connect */
515                                 if(output && input)
516                                         graph->connect(output, input);
517                         }
518                         else
519                                 fprintf(stderr, "Invalid from or to value for connect node.\n");
520                 }
521                 else
522                         fprintf(stderr, "Unknown shader node \"%s\".\n", node.name());
523
524                 if(snode) {
525                         /* add to graph */
526                         graph->add(snode);
527
528                         /* add to map for name lookups */
529                         string name = "";
530                         xml_read_string(&name, node, "name");
531
532                         nodemap[name] = snode;
533
534                         /* read input values */
535                         for(pugi::xml_attribute attr = node.first_attribute(); attr; attr = attr.next_attribute()) {
536                                 foreach(ShaderInput *in, snode->inputs) {
537                                         if(string_iequals(in->name, attr.name())) {
538                                                 switch(in->type) {
539                                                         case SHADER_SOCKET_FLOAT:
540                                                                 xml_read_float(&in->value.x, node, attr.name());
541                                                                 break;
542                                                         case SHADER_SOCKET_COLOR:
543                                                         case SHADER_SOCKET_VECTOR:
544                                                         case SHADER_SOCKET_POINT:
545                                                         case SHADER_SOCKET_NORMAL:
546                                                                 xml_read_float3(&in->value, node, attr.name());
547                                                                 break;
548                                                         default:
549                                                                 break;
550                                                 }
551                                         }
552                                 }
553                         }
554                 }
555         }
556
557         shader->set_graph(graph);
558         shader->tag_update(state.scene);
559 }
560
561 static void xml_read_shader(const XMLReadState& state, pugi::xml_node node)
562 {
563         Shader *shader = new Shader();
564         xml_read_string(&shader->name, node, "name");
565         xml_read_shader_graph(state, shader, node);
566         state.scene->shaders.push_back(shader);
567 }
568
569 /* Background */
570
571 static void xml_read_background(const XMLReadState& state, pugi::xml_node node)
572 {
573         Shader *shader = state.scene->shaders[state.scene->default_background];
574
575         xml_read_shader_graph(state, shader, node);
576 }
577
578 /* Mesh */
579
580 static Mesh *xml_add_mesh(Scene *scene, const Transform& tfm)
581 {
582         /* create mesh */
583         Mesh *mesh = new Mesh();
584         scene->meshes.push_back(mesh);
585
586         /* create object*/
587         Object *object = new Object();
588         object->mesh = mesh;
589         object->tfm = tfm;
590         scene->objects.push_back(object);
591
592         return mesh;
593 }
594
595 static void xml_read_mesh(const XMLReadState& state, pugi::xml_node node)
596 {
597         /* add mesh */
598         Mesh *mesh = xml_add_mesh(state.scene, state.tfm);
599         mesh->used_shaders.push_back(state.shader);
600
601         /* read state */
602         int shader = state.shader;
603         bool smooth = state.smooth;
604
605         mesh->displacement_method = state.displacement_method;
606
607         /* read vertices and polygons, RIB style */
608         vector<float3> P;
609         vector<int> verts, nverts;
610
611         xml_read_float3_array(P, node, "P");
612         xml_read_int_array(verts, node, "verts");
613         xml_read_int_array(nverts, node, "nverts");
614
615         if(xml_equal_string(node, "subdivision", "catmull-clark")) {
616                 /* create subd mesh */
617                 SubdMesh sdmesh;
618
619                 /* create subd vertices */
620                 for(size_t i = 0; i < P.size(); i++)
621                         sdmesh.add_vert(P[i]);
622
623                 /* create subd faces */
624                 int index_offset = 0;
625
626                 for(size_t i = 0; i < nverts.size(); i++) {
627                         if(nverts[i] == 4) {
628                                 int v0 = verts[index_offset + 0];
629                                 int v1 = verts[index_offset + 1];
630                                 int v2 = verts[index_offset + 2];
631                                 int v3 = verts[index_offset + 3];
632
633                                 sdmesh.add_face(v0, v1, v2, v3);
634                         }
635                         else {
636                                 for(int j = 0; j < nverts[i]-2; j++) {
637                                         int v0 = verts[index_offset];
638                                         int v1 = verts[index_offset + j + 1];
639                                         int v2 = verts[index_offset + j + 2];;
640
641                                         sdmesh.add_face(v0, v1, v2);
642                                 }
643                         }
644
645                         index_offset += nverts[i];
646                 }
647
648                 /* finalize subd mesh */
649                 sdmesh.link_boundary();
650
651                 /* subdivide */
652                 DiagSplit dsplit;
653                 //dsplit.camera = state.scene->camera;
654                 //dsplit.dicing_rate = 5.0f;
655                 dsplit.dicing_rate = state.dicing_rate;
656                 xml_read_float(&dsplit.dicing_rate, node, "dicing_rate");
657                 sdmesh.tesselate(&dsplit, false, mesh, shader, smooth);
658         }
659         else {
660                 /* create vertices */
661                 mesh->verts = P;
662
663                 /* create triangles */
664                 int index_offset = 0;
665
666                 for(size_t i = 0; i < nverts.size(); i++) {
667                         for(int j = 0; j < nverts[i]-2; j++) {
668                                 int v0 = verts[index_offset];
669                                 int v1 = verts[index_offset + j + 1];
670                                 int v2 = verts[index_offset + j + 2];
671
672                                 assert(v0 < (int)P.size());
673                                 assert(v1 < (int)P.size());
674                                 assert(v2 < (int)P.size());
675
676                                 mesh->add_triangle(v0, v1, v2, shader, smooth);
677                         }
678
679                         index_offset += nverts[i];
680                 }
681         }
682
683         /* temporary for test compatibility */
684         mesh->attributes.remove(Attribute::STD_VERTEX_NORMAL);
685 }
686
687 /* Patch */
688
689 static void xml_read_patch(const XMLReadState& state, pugi::xml_node node)
690 {
691         /* read patch */
692         Patch *patch = NULL;
693
694         vector<float3> P;
695         xml_read_float3_array(P, node, "P");
696
697         if(xml_equal_string(node, "type", "bilinear")) {
698                 /* bilinear patch */
699                 if(P.size() == 4) {
700                         LinearQuadPatch *bpatch = new LinearQuadPatch();
701
702                         for(int i = 0; i < 4; i++)
703                                 P[i] = transform(&state.tfm, P[i]);
704                         memcpy(bpatch->hull, &P[0], sizeof(bpatch->hull));
705
706                         patch = bpatch;
707                 }
708                 else
709                         fprintf(stderr, "Invalid number of control points for bilinear patch.\n");
710         }
711         else if(xml_equal_string(node, "type", "bicubic")) {
712                 /* bicubic patch */
713                 if(P.size() == 16) {
714                         BicubicPatch *bpatch = new BicubicPatch();
715
716                         for(int i = 0; i < 16; i++)
717                                 P[i] = transform(&state.tfm, P[i]);
718                         memcpy(bpatch->hull, &P[0], sizeof(bpatch->hull));
719
720                         patch = bpatch;
721                 }
722                 else
723                         fprintf(stderr, "Invalid number of control points for bicubic patch.\n");
724         }
725         else
726                 fprintf(stderr, "Unknown patch type.\n");
727
728         if(patch) {
729                 /* add mesh */
730                 Mesh *mesh = xml_add_mesh(state.scene, transform_identity());
731
732                 mesh->used_shaders.push_back(state.shader);
733
734                 /* split */
735                 DiagSplit dsplit;
736                 //dsplit.camera = state.scene->camera;
737                 //dsplit.dicing_rate = 5.0f;
738                 dsplit.dicing_rate = state.dicing_rate;
739                 xml_read_float(&dsplit.dicing_rate, node, "dicing_rate");
740                 dsplit.split_quad(mesh, patch, state.shader, state.smooth);
741
742                 delete patch;
743
744                 /* temporary for test compatibility */
745                 mesh->attributes.remove(Attribute::STD_VERTEX_NORMAL);
746         }
747 }
748
749 /* Light */
750
751 static void xml_read_light(const XMLReadState& state, pugi::xml_node node)
752 {
753         Light *light = new Light();
754         light->shader = state.shader;
755         xml_read_float3(&light->co, node, "P");
756         light->co = transform(&state.tfm, light->co);
757
758         state.scene->lights.push_back(light);
759 }
760
761 /* Transform */
762
763 static void xml_read_transform(pugi::xml_node node, Transform& tfm)
764 {
765         if(node.attribute("matrix")) {
766                 vector<float> matrix;
767                 if(xml_read_float_array(matrix, node, "matrix") && matrix.size() == 16)
768                         tfm = tfm * transform_transpose((*(Transform*)&matrix[0]));
769         }
770
771         if(node.attribute("translate")) {
772                 float3 translate = make_float3(0.0f, 0.0f, 0.0f);
773                 xml_read_float3(&translate, node, "translate");
774                 tfm = tfm * transform_translate(translate);
775         }
776
777         if(node.attribute("rotate")) {
778                 float4 rotate = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
779                 xml_read_float4(&rotate, node, "rotate");
780                 tfm = tfm * transform_rotate(rotate.x*M_PI/180.0f, make_float3(rotate.y, rotate.z, rotate.w));
781         }
782
783         if(node.attribute("scale")) {
784                 float3 scale = make_float3(0.0f, 0.0f, 0.0f);
785                 xml_read_float3(&scale, node, "scale");
786                 tfm = tfm * transform_scale(scale);
787         }
788 }
789
790 /* State */
791
792 static void xml_read_state(XMLReadState& state, pugi::xml_node node)
793 {
794         /* read shader */
795         string shadername;
796
797         if(xml_read_string(&shadername, node, "shader")) {
798                 int i = 0;
799                 bool found = false;
800
801                 foreach(Shader *shader, state.scene->shaders) {
802                         if(shader->name == shadername) {
803                                 state.shader = i;
804                                 found = true;
805                                 break;
806                         }
807
808                         i++;
809                 }
810
811                 if(!found)
812                         fprintf(stderr, "Unknown shader \"%s\".\n", shadername.c_str());
813         }
814
815         xml_read_float(&state.dicing_rate, node, "dicing_rate");
816
817         /* read smooth/flat */
818         if(xml_equal_string(node, "interpolation", "smooth"))
819                 state.smooth = true;
820         else if(xml_equal_string(node, "interpolation", "flat"))
821                 state.smooth = false;
822
823         /* read displacement method */
824         if(xml_equal_string(node, "displacement_method", "true"))
825                 state.displacement_method = Mesh::DISPLACE_TRUE;
826         else if(xml_equal_string(node, "displacement_method", "bump"))
827                 state.displacement_method = Mesh::DISPLACE_BUMP;
828         else if(xml_equal_string(node, "displacement_method", "both"))
829                 state.displacement_method = Mesh::DISPLACE_BOTH;
830 }
831
832 /* Scene */
833
834 static void xml_read_include(const XMLReadState& state, const string& src);
835
836 static void xml_read_scene(const XMLReadState& state, pugi::xml_node scene_node)
837 {
838         for(pugi::xml_node node = scene_node.first_child(); node; node = node.next_sibling()) {
839                 if(string_iequals(node.name(), "film")) {
840                         xml_read_film(state, node);
841                 }
842                 else if(string_iequals(node.name(), "integrator")) {
843                         xml_read_integrator(state, node);
844                 }
845                 else if(string_iequals(node.name(), "camera")) {
846                         xml_read_camera(state, node);
847                 }
848                 else if(string_iequals(node.name(), "shader")) {
849                         xml_read_shader(state, node);
850                 }
851                 else if(string_iequals(node.name(), "background")) {
852                         xml_read_background(state, node);
853                 }
854                 else if(string_iequals(node.name(), "mesh")) {
855                         xml_read_mesh(state, node);
856                 }
857                 else if(string_iequals(node.name(), "patch")) {
858                         xml_read_patch(state, node);
859                 }
860                 else if(string_iequals(node.name(), "light")) {
861                         xml_read_light(state, node);
862                 }
863                 else if(string_iequals(node.name(), "transform")) {
864                         XMLReadState substate = state;
865
866                         xml_read_transform(node, substate.tfm);
867                         xml_read_scene(substate, node);
868                 }
869                 else if(string_iequals(node.name(), "state")) {
870                         XMLReadState substate = state;
871
872                         xml_read_state(substate, node);
873                         xml_read_scene(substate, node);
874                 }
875                 else if(string_iequals(node.name(), "include")) {
876                         string src;
877
878                         if(xml_read_string(&src, node, "src"))
879                                 xml_read_include(state, src);
880                 }
881                 else
882                         fprintf(stderr, "Unknown node \"%s\".\n", node.name());
883         }
884 }
885
886 /* Include */
887
888 static void xml_read_include(const XMLReadState& state, const string& src)
889 {
890         /* open XML document */
891         pugi::xml_document doc;
892         pugi::xml_parse_result parse_result;
893
894         string path = path_join(state.base, src);
895         parse_result = doc.load_file(path.c_str());
896
897         if(parse_result) {
898                 XMLReadState substate = state;
899                 substate.base = path_dirname(path);
900
901                 xml_read_scene(substate, doc);
902         }
903         else
904                 fprintf(stderr, "%s read error: %s\n", src.c_str(), parse_result.description());
905 }
906
907 /* File */
908
909 void xml_read_file(Scene *scene, const char *filepath)
910 {
911         XMLReadState state;
912
913         state.scene = scene;
914         state.tfm = transform_identity();
915         state.shader = scene->default_surface;
916         state.smooth = false;
917         state.dicing_rate = 0.1f;
918         state.base = path_dirname(filepath);
919
920         xml_read_include(state, path_filename(filepath));
921
922         scene->params.bvh_type = SceneParams::BVH_STATIC;
923 }
924
925 CCL_NAMESPACE_END
926