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