Cycles: svn merge -r41225:41232 ^/trunk/blender
[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(), "blend_texture")) {
343                         BlendTextureNode *blend = new BlendTextureNode();
344                         xml_read_enum(&blend->progression, BlendTextureNode::progression_enum, node, "progression");
345                         xml_read_enum(&blend->axis, BlendTextureNode::axis_enum, node, "axis");
346                         snode = blend;
347                 }
348                 else if(string_iequals(node.name(), "clouds_texture")) {
349                         CloudsTextureNode *clouds = new CloudsTextureNode();
350                         xml_read_bool(&clouds->hard, node, "hard");
351                         xml_read_int(&clouds->depth, node, "depth");
352                         xml_read_enum(&clouds->basis, CloudsTextureNode::basis_enum, node, "basis");
353                         snode = clouds;
354                 }
355                 else if(string_iequals(node.name(), "voronoi_texture")) {
356                         VoronoiTextureNode *voronoi = new VoronoiTextureNode();
357                         xml_read_enum(&voronoi->distance_metric, VoronoiTextureNode::distance_metric_enum, node, "distance_metric");
358                         xml_read_enum(&voronoi->coloring, VoronoiTextureNode::coloring_enum, node, "coloring");
359                         snode = voronoi;
360                 }
361                 else if(string_iequals(node.name(), "musgrave_texture")) {
362                         MusgraveTextureNode *musgrave = new MusgraveTextureNode();
363                         xml_read_enum(&musgrave->type, MusgraveTextureNode::type_enum, node, "type");
364                         xml_read_enum(&musgrave->basis, MusgraveTextureNode::basis_enum, node, "basis");
365                         snode = musgrave;
366                 }
367                 else if(string_iequals(node.name(), "marble_texture")) {
368                         MarbleTextureNode *marble = new MarbleTextureNode();
369                         xml_read_enum(&marble->type, MarbleTextureNode::type_enum, node, "type");
370                         xml_read_enum(&marble->wave, MarbleTextureNode::wave_enum, node, "wave");
371                         xml_read_enum(&marble->basis, MarbleTextureNode::basis_enum, node, "basis");
372                         xml_read_bool(&marble->hard, node, "hard");
373                         xml_read_int(&marble->depth, node, "depth");
374                         snode = marble;
375                 }
376                 else if(string_iequals(node.name(), "magic_texture")) {
377                         MagicTextureNode *magic = new MagicTextureNode();
378                         xml_read_int(&magic->depth, node, "depth");
379                         snode = magic;
380                 }
381                 else if(string_iequals(node.name(), "stucci_texture")) {
382                         StucciTextureNode *stucci = new StucciTextureNode();
383                         xml_read_enum(&stucci->type, StucciTextureNode::type_enum, node, "type");
384                         xml_read_enum(&stucci->basis, StucciTextureNode::basis_enum, node, "basis");
385                         xml_read_bool(&stucci->hard, node, "hard");
386                         snode = stucci;
387                 }
388                 else if(string_iequals(node.name(), "distorted_noise_texture")) {
389                         DistortedNoiseTextureNode *dist = new DistortedNoiseTextureNode();
390                         xml_read_enum(&dist->basis, DistortedNoiseTextureNode::basis_enum, node, "basis");
391                         xml_read_enum(&dist->distortion_basis, DistortedNoiseTextureNode::basis_enum, node, "distortion_basis");
392                         snode = dist;
393                 }
394                 else if(string_iequals(node.name(), "wood_texture")) {
395                         WoodTextureNode *wood = new WoodTextureNode();
396                         xml_read_enum(&wood->type, WoodTextureNode::type_enum, node, "type");
397                         xml_read_enum(&wood->wave, WoodTextureNode::wave_enum, node, "wave");
398                         xml_read_enum(&wood->basis, WoodTextureNode::basis_enum, node, "basis");
399                         xml_read_bool(&wood->hard, node, "hard");
400                         snode = wood;
401                 }
402                 else if(string_iequals(node.name(), "mapping")) {
403                         snode = new MappingNode();
404                 }
405                 else if(string_iequals(node.name(), "ward_bsdf")) {
406                         snode = new WardBsdfNode();
407                 }
408                 else if(string_iequals(node.name(), "diffuse_bsdf")) {
409                         snode = new DiffuseBsdfNode();
410                 }
411                 else if(string_iequals(node.name(), "translucent_bsdf")) {
412                         snode = new TranslucentBsdfNode();
413                 }
414                 else if(string_iequals(node.name(), "transparent_bsdf")) {
415                         snode = new TransparentBsdfNode();
416                 }
417                 else if(string_iequals(node.name(), "velvet_bsdf")) {
418                         snode = new VelvetBsdfNode();
419                 }
420                 else if(string_iequals(node.name(), "glossy_bsdf")) {
421                         GlossyBsdfNode *glossy = new GlossyBsdfNode();
422                         xml_read_enum(&glossy->distribution, GlossyBsdfNode::distribution_enum, node, "distribution");
423                         snode = glossy;
424                 }
425                 else if(string_iequals(node.name(), "glass_bsdf")) {
426                         GlassBsdfNode *diel = new GlassBsdfNode();
427                         xml_read_enum(&diel->distribution, GlassBsdfNode::distribution_enum, node, "distribution");
428                         snode = diel;
429                 }
430                 else if(string_iequals(node.name(), "emission")) {
431                         EmissionNode *emission = new EmissionNode();
432                         xml_read_bool(&emission->total_power, node, "total_power");
433                         snode = emission;
434                 }
435                 else if(string_iequals(node.name(), "background")) {
436                         snode = new BackgroundNode();
437                 }
438                 else if(string_iequals(node.name(), "transparent_volume")) {
439                         snode = new TransparentVolumeNode();
440                 }
441                 else if(string_iequals(node.name(), "isotropic_volume")) {
442                         snode = new IsotropicVolumeNode();
443                 }
444                 else if(string_iequals(node.name(), "geometry")) {
445                         snode = new GeometryNode();
446                 }
447                 else if(string_iequals(node.name(), "texture_coordinate")) {
448                         snode = new TextureCoordinateNode();
449                 }
450                 else if(string_iequals(node.name(), "lightPath")) {
451                         snode = new LightPathNode();
452                 }
453                 else if(string_iequals(node.name(), "value")) {
454                         ValueNode *value = new ValueNode();
455                         xml_read_float(&value->value, node, "value");
456                         snode = value;
457                 }
458                 else if(string_iequals(node.name(), "color")) {
459                         ColorNode *color = new ColorNode();
460                         xml_read_float3(&color->value, node, "value");
461                         snode = color;
462                 }
463                 else if(string_iequals(node.name(), "mix_closure")) {
464                         snode = new MixClosureNode();
465                 }
466                 else if(string_iequals(node.name(), "add_closure")) {
467                         snode = new AddClosureNode();
468                 }
469                 else if(string_iequals(node.name(), "mix")) {
470                         MixNode *mix = new MixNode();
471                         xml_read_enum(&mix->type, MixNode::type_enum, node, "type");
472                         snode = mix;
473                 }
474                 else if(string_iequals(node.name(), "attribute")) {
475                         AttributeNode *attr = new AttributeNode();
476                         xml_read_ustring(&attr->attribute, node, "attribute");
477                         snode = attr;
478                 }
479                 else if(string_iequals(node.name(), "fresnel")) {
480                         snode = new FresnelNode();
481                 }
482                 else if(string_iequals(node.name(), "math")) {
483                         MathNode *math = new MathNode();
484                         xml_read_enum(&math->type, MathNode::type_enum, node, "type");
485                         snode = math;
486                 }
487                 else if(string_iequals(node.name(), "vector_math")) {
488                         VectorMathNode *vmath = new VectorMathNode();
489                         xml_read_enum(&vmath->type, VectorMathNode::type_enum, node, "type");
490                         snode = vmath;
491                 }
492                 else if(string_iequals(node.name(), "connect")) {
493                         /* connect nodes */
494                         vector<string> from_tokens, to_tokens;
495
496                         string_split(from_tokens, node.attribute("from").value());
497                         string_split(to_tokens, node.attribute("to").value());
498
499                         if(from_tokens.size() == 2 && to_tokens.size() == 2) {
500                                 /* find nodes and sockets */
501                                 ShaderOutput *output = NULL;
502                                 ShaderInput *input = NULL;
503
504                                 if(nodemap.find(from_tokens[0]) != nodemap.end()) {
505                                         ShaderNode *fromnode = nodemap[from_tokens[0]];
506
507                                         foreach(ShaderOutput *out, fromnode->outputs)
508                                                 if(string_iequals(xml_socket_name(out->name), from_tokens[1]))
509                                                         output = out;
510
511                                         if(!output)
512                                                 fprintf(stderr, "Unknown output socket name \"%s\" on \"%s\".\n", from_tokens[1].c_str(), from_tokens[0].c_str());
513                                 }
514                                 else
515                                         fprintf(stderr, "Unknown shader node name \"%s\".\n", from_tokens[0].c_str());
516
517                                 if(nodemap.find(to_tokens[0]) != nodemap.end()) {
518                                         ShaderNode *tonode = nodemap[to_tokens[0]];
519
520                                         foreach(ShaderInput *in, tonode->inputs)
521                                                 if(string_iequals(xml_socket_name(in->name), to_tokens[1]))
522                                                         input = in;
523
524                                         if(!input)
525                                                 fprintf(stderr, "Unknown input socket name \"%s\" on \"%s\".\n", to_tokens[1].c_str(), to_tokens[0].c_str());
526                                 }
527                                 else
528                                         fprintf(stderr, "Unknown shader node name \"%s\".\n", to_tokens[0].c_str());
529
530                                 /* connect */
531                                 if(output && input)
532                                         graph->connect(output, input);
533                         }
534                         else
535                                 fprintf(stderr, "Invalid from or to value for connect node.\n");
536                 }
537                 else
538                         fprintf(stderr, "Unknown shader node \"%s\".\n", node.name());
539
540                 if(snode) {
541                         /* add to graph */
542                         graph->add(snode);
543
544                         /* add to map for name lookups */
545                         string name = "";
546                         xml_read_string(&name, node, "name");
547
548                         nodemap[name] = snode;
549
550                         /* read input values */
551                         for(pugi::xml_attribute attr = node.first_attribute(); attr; attr = attr.next_attribute()) {
552                                 foreach(ShaderInput *in, snode->inputs) {
553                                         if(string_iequals(in->name, attr.name())) {
554                                                 switch(in->type) {
555                                                         case SHADER_SOCKET_FLOAT:
556                                                                 xml_read_float(&in->value.x, node, attr.name());
557                                                                 break;
558                                                         case SHADER_SOCKET_COLOR:
559                                                         case SHADER_SOCKET_VECTOR:
560                                                         case SHADER_SOCKET_POINT:
561                                                         case SHADER_SOCKET_NORMAL:
562                                                                 xml_read_float3(&in->value, node, attr.name());
563                                                                 break;
564                                                         default:
565                                                                 break;
566                                                 }
567                                         }
568                                 }
569                         }
570                 }
571         }
572
573         shader->set_graph(graph);
574         shader->tag_update(state.scene);
575 }
576
577 static void xml_read_shader(const XMLReadState& state, pugi::xml_node node)
578 {
579         Shader *shader = new Shader();
580         xml_read_string(&shader->name, node, "name");
581         xml_read_shader_graph(state, shader, node);
582         state.scene->shaders.push_back(shader);
583 }
584
585 /* Background */
586
587 static void xml_read_background(const XMLReadState& state, pugi::xml_node node)
588 {
589         Shader *shader = state.scene->shaders[state.scene->default_background];
590
591         xml_read_shader_graph(state, shader, node);
592 }
593
594 /* Mesh */
595
596 static Mesh *xml_add_mesh(Scene *scene, const Transform& tfm)
597 {
598         /* create mesh */
599         Mesh *mesh = new Mesh();
600         scene->meshes.push_back(mesh);
601
602         /* create object*/
603         Object *object = new Object();
604         object->mesh = mesh;
605         object->tfm = tfm;
606         scene->objects.push_back(object);
607
608         return mesh;
609 }
610
611 static void xml_read_mesh(const XMLReadState& state, pugi::xml_node node)
612 {
613         /* add mesh */
614         Mesh *mesh = xml_add_mesh(state.scene, state.tfm);
615         mesh->used_shaders.push_back(state.shader);
616
617         /* read state */
618         int shader = state.shader;
619         bool smooth = state.smooth;
620
621         mesh->displacement_method = state.displacement_method;
622
623         /* read vertices and polygons, RIB style */
624         vector<float3> P;
625         vector<int> verts, nverts;
626
627         xml_read_float3_array(P, node, "P");
628         xml_read_int_array(verts, node, "verts");
629         xml_read_int_array(nverts, node, "nverts");
630
631         if(xml_equal_string(node, "subdivision", "catmull-clark")) {
632                 /* create subd mesh */
633                 SubdMesh sdmesh;
634
635                 /* create subd vertices */
636                 for(size_t i = 0; i < P.size(); i++)
637                         sdmesh.add_vert(P[i]);
638
639                 /* create subd faces */
640                 int index_offset = 0;
641
642                 for(size_t i = 0; i < nverts.size(); i++) {
643                         if(nverts[i] == 4) {
644                                 int v0 = verts[index_offset + 0];
645                                 int v1 = verts[index_offset + 1];
646                                 int v2 = verts[index_offset + 2];
647                                 int v3 = verts[index_offset + 3];
648
649                                 sdmesh.add_face(v0, v1, v2, v3);
650                         }
651                         else {
652                                 for(int j = 0; j < nverts[i]-2; j++) {
653                                         int v0 = verts[index_offset];
654                                         int v1 = verts[index_offset + j + 1];
655                                         int v2 = verts[index_offset + j + 2];;
656
657                                         sdmesh.add_face(v0, v1, v2);
658                                 }
659                         }
660
661                         index_offset += nverts[i];
662                 }
663
664                 /* finalize subd mesh */
665                 sdmesh.link_boundary();
666
667                 /* subdivide */
668                 DiagSplit dsplit;
669                 //dsplit.camera = state.scene->camera;
670                 //dsplit.dicing_rate = 5.0f;
671                 dsplit.dicing_rate = state.dicing_rate;
672                 xml_read_float(&dsplit.dicing_rate, node, "dicing_rate");
673                 sdmesh.tesselate(&dsplit, false, mesh, shader, smooth);
674         }
675         else {
676                 /* create vertices */
677                 mesh->verts = P;
678
679                 /* create triangles */
680                 int index_offset = 0;
681
682                 for(size_t i = 0; i < nverts.size(); i++) {
683                         for(int j = 0; j < nverts[i]-2; j++) {
684                                 int v0 = verts[index_offset];
685                                 int v1 = verts[index_offset + j + 1];
686                                 int v2 = verts[index_offset + j + 2];
687
688                                 assert(v0 < (int)P.size());
689                                 assert(v1 < (int)P.size());
690                                 assert(v2 < (int)P.size());
691
692                                 mesh->add_triangle(v0, v1, v2, shader, smooth);
693                         }
694
695                         index_offset += nverts[i];
696                 }
697         }
698
699         /* temporary for test compatibility */
700         mesh->attributes.remove(Attribute::STD_VERTEX_NORMAL);
701 }
702
703 /* Patch */
704
705 static void xml_read_patch(const XMLReadState& state, pugi::xml_node node)
706 {
707         /* read patch */
708         Patch *patch = NULL;
709
710         vector<float3> P;
711         xml_read_float3_array(P, node, "P");
712
713         if(xml_equal_string(node, "type", "bilinear")) {
714                 /* bilinear patch */
715                 if(P.size() == 4) {
716                         LinearQuadPatch *bpatch = new LinearQuadPatch();
717
718                         for(int i = 0; i < 4; i++)
719                                 P[i] = transform(&state.tfm, P[i]);
720                         memcpy(bpatch->hull, &P[0], sizeof(bpatch->hull));
721
722                         patch = bpatch;
723                 }
724                 else
725                         fprintf(stderr, "Invalid number of control points for bilinear patch.\n");
726         }
727         else if(xml_equal_string(node, "type", "bicubic")) {
728                 /* bicubic patch */
729                 if(P.size() == 16) {
730                         BicubicPatch *bpatch = new BicubicPatch();
731
732                         for(int i = 0; i < 16; i++)
733                                 P[i] = transform(&state.tfm, P[i]);
734                         memcpy(bpatch->hull, &P[0], sizeof(bpatch->hull));
735
736                         patch = bpatch;
737                 }
738                 else
739                         fprintf(stderr, "Invalid number of control points for bicubic patch.\n");
740         }
741         else
742                 fprintf(stderr, "Unknown patch type.\n");
743
744         if(patch) {
745                 /* add mesh */
746                 Mesh *mesh = xml_add_mesh(state.scene, transform_identity());
747
748                 mesh->used_shaders.push_back(state.shader);
749
750                 /* split */
751                 DiagSplit dsplit;
752                 //dsplit.camera = state.scene->camera;
753                 //dsplit.dicing_rate = 5.0f;
754                 dsplit.dicing_rate = state.dicing_rate;
755                 xml_read_float(&dsplit.dicing_rate, node, "dicing_rate");
756                 dsplit.split_quad(mesh, patch, state.shader, state.smooth);
757
758                 delete patch;
759
760                 /* temporary for test compatibility */
761                 mesh->attributes.remove(Attribute::STD_VERTEX_NORMAL);
762         }
763 }
764
765 /* Light */
766
767 static void xml_read_light(const XMLReadState& state, pugi::xml_node node)
768 {
769         Light *light = new Light();
770         light->shader = state.shader;
771         xml_read_float3(&light->co, node, "P");
772         light->co = transform(&state.tfm, light->co);
773
774         state.scene->lights.push_back(light);
775 }
776
777 /* Transform */
778
779 static void xml_read_transform(pugi::xml_node node, Transform& tfm)
780 {
781         if(node.attribute("matrix")) {
782                 vector<float> matrix;
783                 if(xml_read_float_array(matrix, node, "matrix") && matrix.size() == 16)
784                         tfm = tfm * transform_transpose((*(Transform*)&matrix[0]));
785         }
786
787         if(node.attribute("translate")) {
788                 float3 translate = make_float3(0.0f, 0.0f, 0.0f);
789                 xml_read_float3(&translate, node, "translate");
790                 tfm = tfm * transform_translate(translate);
791         }
792
793         if(node.attribute("rotate")) {
794                 float4 rotate = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
795                 xml_read_float4(&rotate, node, "rotate");
796                 tfm = tfm * transform_rotate(rotate.x*M_PI/180.0f, make_float3(rotate.y, rotate.z, rotate.w));
797         }
798
799         if(node.attribute("scale")) {
800                 float3 scale = make_float3(0.0f, 0.0f, 0.0f);
801                 xml_read_float3(&scale, node, "scale");
802                 tfm = tfm * transform_scale(scale);
803         }
804 }
805
806 /* State */
807
808 static void xml_read_state(XMLReadState& state, pugi::xml_node node)
809 {
810         /* read shader */
811         string shadername;
812
813         if(xml_read_string(&shadername, node, "shader")) {
814                 int i = 0;
815                 bool found = false;
816
817                 foreach(Shader *shader, state.scene->shaders) {
818                         if(shader->name == shadername) {
819                                 state.shader = i;
820                                 found = true;
821                                 break;
822                         }
823
824                         i++;
825                 }
826
827                 if(!found)
828                         fprintf(stderr, "Unknown shader \"%s\".\n", shadername.c_str());
829         }
830
831         xml_read_float(&state.dicing_rate, node, "dicing_rate");
832
833         /* read smooth/flat */
834         if(xml_equal_string(node, "interpolation", "smooth"))
835                 state.smooth = true;
836         else if(xml_equal_string(node, "interpolation", "flat"))
837                 state.smooth = false;
838
839         /* read displacement method */
840         if(xml_equal_string(node, "displacement_method", "true"))
841                 state.displacement_method = Mesh::DISPLACE_TRUE;
842         else if(xml_equal_string(node, "displacement_method", "bump"))
843                 state.displacement_method = Mesh::DISPLACE_BUMP;
844         else if(xml_equal_string(node, "displacement_method", "both"))
845                 state.displacement_method = Mesh::DISPLACE_BOTH;
846 }
847
848 /* Scene */
849
850 static void xml_read_include(const XMLReadState& state, const string& src);
851
852 static void xml_read_scene(const XMLReadState& state, pugi::xml_node scene_node)
853 {
854         for(pugi::xml_node node = scene_node.first_child(); node; node = node.next_sibling()) {
855                 if(string_iequals(node.name(), "film")) {
856                         xml_read_film(state, node);
857                 }
858                 else if(string_iequals(node.name(), "integrator")) {
859                         xml_read_integrator(state, node);
860                 }
861                 else if(string_iequals(node.name(), "camera")) {
862                         xml_read_camera(state, node);
863                 }
864                 else if(string_iequals(node.name(), "shader")) {
865                         xml_read_shader(state, node);
866                 }
867                 else if(string_iequals(node.name(), "background")) {
868                         xml_read_background(state, node);
869                 }
870                 else if(string_iequals(node.name(), "mesh")) {
871                         xml_read_mesh(state, node);
872                 }
873                 else if(string_iequals(node.name(), "patch")) {
874                         xml_read_patch(state, node);
875                 }
876                 else if(string_iequals(node.name(), "light")) {
877                         xml_read_light(state, node);
878                 }
879                 else if(string_iequals(node.name(), "transform")) {
880                         XMLReadState substate = state;
881
882                         xml_read_transform(node, substate.tfm);
883                         xml_read_scene(substate, node);
884                 }
885                 else if(string_iequals(node.name(), "state")) {
886                         XMLReadState substate = state;
887
888                         xml_read_state(substate, node);
889                         xml_read_scene(substate, node);
890                 }
891                 else if(string_iequals(node.name(), "include")) {
892                         string src;
893
894                         if(xml_read_string(&src, node, "src"))
895                                 xml_read_include(state, src);
896                 }
897                 else
898                         fprintf(stderr, "Unknown node \"%s\".\n", node.name());
899         }
900 }
901
902 /* Include */
903
904 static void xml_read_include(const XMLReadState& state, const string& src)
905 {
906         /* open XML document */
907         pugi::xml_document doc;
908         pugi::xml_parse_result parse_result;
909
910         string path = path_join(state.base, src);
911         parse_result = doc.load_file(path.c_str());
912
913         if(parse_result) {
914                 XMLReadState substate = state;
915                 substate.base = path_dirname(path);
916
917                 xml_read_scene(substate, doc);
918         }
919         else
920                 fprintf(stderr, "%s read error: %s\n", src.c_str(), parse_result.description());
921 }
922
923 /* File */
924
925 void xml_read_file(Scene *scene, const char *filepath)
926 {
927         XMLReadState state;
928
929         state.scene = scene;
930         state.tfm = transform_identity();
931         state.shader = scene->default_surface;
932         state.smooth = false;
933         state.dicing_rate = 0.1f;
934         state.base = path_dirname(filepath);
935
936         xml_read_include(state, path_filename(filepath));
937
938         scene->params.bvh_type = SceneParams::BVH_STATIC;
939 }
940
941 CCL_NAMESPACE_END
942