986f0ccda68695dc4119637a01d69c98571ec55d
[blender.git] / intern / cycles / blender / blender_object.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "render/camera.h"
18 #include "render/integrator.h"
19 #include "render/graph.h"
20 #include "render/light.h"
21 #include "render/mesh.h"
22 #include "render/object.h"
23 #include "render/scene.h"
24 #include "render/nodes.h"
25 #include "render/particles.h"
26 #include "render/shader.h"
27
28 #include "blender/blender_object_cull.h"
29 #include "blender/blender_sync.h"
30 #include "blender/blender_util.h"
31
32 #include "util/util_foreach.h"
33 #include "util/util_hash.h"
34 #include "util/util_logging.h"
35
36 CCL_NAMESPACE_BEGIN
37
38 /* Utilities */
39
40 bool BlenderSync::BKE_object_is_modified(BL::Object& b_ob)
41 {
42         /* test if we can instance or if the object is modified */
43         if(b_ob.type() == BL::Object::type_META) {
44                 /* multi-user and dupli metaballs are fused, can't instance */
45                 return true;
46         }
47         else if(ccl::BKE_object_is_modified(b_ob, b_scene, preview)) {
48                 /* modifiers */
49                 return true;
50         }
51         else {
52                 /* object level material links */
53                 BL::Object::material_slots_iterator slot;
54                 for(b_ob.material_slots.begin(slot); slot != b_ob.material_slots.end(); ++slot)
55                         if(slot->link() == BL::MaterialSlot::link_OBJECT)
56                                 return true;
57         }
58
59         return false;
60 }
61
62 bool BlenderSync::object_is_mesh(BL::Object& b_ob)
63 {
64         BL::ID b_ob_data = b_ob.data();
65
66         if(!b_ob_data) {
67                 return false;
68         }
69
70         if(b_ob.type() == BL::Object::type_CURVE) {
71                 /* Skip exporting curves without faces, overhead can be
72                  * significant if there are many for path animation. */
73                 BL::Curve b_curve(b_ob.data());
74
75                 return (b_curve.bevel_object() ||
76                         b_curve.extrude() != 0.0f ||
77                         b_curve.bevel_depth() != 0.0f ||
78                         b_curve.dimensions() == BL::Curve::dimensions_2D ||
79                         b_ob.modifiers.length());
80         }
81         else {
82                 return (b_ob_data.is_a(&RNA_Mesh) ||
83                         b_ob_data.is_a(&RNA_Curve) ||
84                         b_ob_data.is_a(&RNA_MetaBall));
85         }
86 }
87
88 bool BlenderSync::object_is_light(BL::Object& b_ob)
89 {
90         BL::ID b_ob_data = b_ob.data();
91
92         return (b_ob_data && b_ob_data.is_a(&RNA_Lamp));
93 }
94
95 static uint object_ray_visibility(BL::Object& b_ob)
96 {
97         PointerRNA cvisibility = RNA_pointer_get(&b_ob.ptr, "cycles_visibility");
98         uint flag = 0;
99
100         flag |= get_boolean(cvisibility, "camera")? PATH_RAY_CAMERA: 0;
101         flag |= get_boolean(cvisibility, "diffuse")? PATH_RAY_DIFFUSE: 0;
102         flag |= get_boolean(cvisibility, "glossy")? PATH_RAY_GLOSSY: 0;
103         flag |= get_boolean(cvisibility, "transmission")? PATH_RAY_TRANSMIT: 0;
104         flag |= get_boolean(cvisibility, "shadow")? PATH_RAY_SHADOW: 0;
105         flag |= get_boolean(cvisibility, "scatter")? PATH_RAY_VOLUME_SCATTER: 0;
106
107         return flag;
108 }
109
110 /* Light */
111
112 void BlenderSync::sync_light(BL::Object& b_parent,
113                              int persistent_id[OBJECT_PERSISTENT_ID_SIZE],
114                              BL::Object& b_ob,
115                              BL::Object& b_ob_instance,
116                              int random_id,
117                              Transform& tfm,
118                              bool *use_portal)
119 {
120         /* test if we need to sync */
121         Light *light;
122         ObjectKey key(b_parent, persistent_id, b_ob_instance);
123
124         if(!light_map.sync(&light, b_ob, b_parent, key)) {
125                 if(light->is_portal)
126                         *use_portal = true;
127                 return;
128         }
129         
130         BL::Lamp b_lamp(b_ob.data());
131
132         /* type */
133         switch(b_lamp.type()) {
134                 case BL::Lamp::type_POINT: {
135                         BL::PointLamp b_point_lamp(b_lamp);
136                         light->size = b_point_lamp.shadow_soft_size();
137                         light->type = LIGHT_POINT;
138                         break;
139                 }
140                 case BL::Lamp::type_SPOT: {
141                         BL::SpotLamp b_spot_lamp(b_lamp);
142                         light->size = b_spot_lamp.shadow_soft_size();
143                         light->type = LIGHT_SPOT;
144                         light->spot_angle = b_spot_lamp.spot_size();
145                         light->spot_smooth = b_spot_lamp.spot_blend();
146                         break;
147                 }
148                 case BL::Lamp::type_HEMI: {
149                         light->type = LIGHT_DISTANT;
150                         light->size = 0.0f;
151                         break;
152                 }
153                 case BL::Lamp::type_SUN: {
154                         BL::SunLamp b_sun_lamp(b_lamp);
155                         light->size = b_sun_lamp.shadow_soft_size();
156                         light->type = LIGHT_DISTANT;
157                         break;
158                 }
159                 case BL::Lamp::type_AREA: {
160                         BL::AreaLamp b_area_lamp(b_lamp);
161                         light->size = 1.0f;
162                         light->axisu = transform_get_column(&tfm, 0);
163                         light->axisv = transform_get_column(&tfm, 1);
164                         light->sizeu = b_area_lamp.size();
165                         if(b_area_lamp.shape() == BL::AreaLamp::shape_RECTANGLE)
166                                 light->sizev = b_area_lamp.size_y();
167                         else
168                                 light->sizev = light->sizeu;
169                         light->type = LIGHT_AREA;
170                         break;
171                 }
172         }
173
174         /* location and (inverted!) direction */
175         light->co = transform_get_column(&tfm, 3);
176         light->dir = -transform_get_column(&tfm, 2);
177         light->tfm = tfm;
178
179         /* shader */
180         vector<Shader*> used_shaders;
181         find_shader(b_lamp, used_shaders, scene->default_light);
182         light->shader = used_shaders[0];
183
184         /* shadow */
185         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
186         PointerRNA clamp = RNA_pointer_get(&b_lamp.ptr, "cycles");
187         light->cast_shadow = get_boolean(clamp, "cast_shadow");
188         light->use_mis = get_boolean(clamp, "use_multiple_importance_sampling");
189         
190         int samples = get_int(clamp, "samples");
191         if(get_boolean(cscene, "use_square_samples"))
192                 light->samples = samples * samples;
193         else
194                 light->samples = samples;
195
196         light->max_bounces = get_int(clamp, "max_bounces");
197
198         if(b_ob != b_ob_instance) {
199                 light->random_id = random_id;
200         }
201         else {
202                 light->random_id = hash_int_2d(hash_string(b_ob.name().c_str()), 0);
203         }
204
205         if(light->type == LIGHT_AREA)
206                 light->is_portal = get_boolean(clamp, "is_portal");
207         else
208                 light->is_portal = false;
209
210         if(light->is_portal)
211                 *use_portal = true;
212
213         /* visibility */
214         uint visibility = object_ray_visibility(b_ob);
215         light->use_diffuse = (visibility & PATH_RAY_DIFFUSE) != 0;
216         light->use_glossy = (visibility & PATH_RAY_GLOSSY) != 0;
217         light->use_transmission = (visibility & PATH_RAY_TRANSMIT) != 0;
218         light->use_scatter = (visibility & PATH_RAY_VOLUME_SCATTER) != 0;
219
220         /* tag */
221         light->tag_update(scene);
222 }
223
224 void BlenderSync::sync_background_light(bool use_portal)
225 {
226         BL::World b_world = b_scene.world();
227
228         if(b_world) {
229                 PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
230                 PointerRNA cworld = RNA_pointer_get(&b_world.ptr, "cycles");
231                 bool sample_as_light = get_boolean(cworld, "sample_as_light");
232
233                 if(sample_as_light || use_portal) {
234                         /* test if we need to sync */
235                         Light *light;
236                         ObjectKey key(b_world, 0, b_world);
237
238                         if(light_map.sync(&light, b_world, b_world, key) ||
239                             world_recalc ||
240                             b_world.ptr.data != world_map)
241                         {
242                                 light->type = LIGHT_BACKGROUND;
243                                 light->map_resolution  = get_int(cworld, "sample_map_resolution");
244                                 light->shader = scene->default_background;
245                                 light->use_mis = sample_as_light;
246                                 light->max_bounces = get_int(cworld, "max_bounces");
247
248                                 int samples = get_int(cworld, "samples");
249                                 if(get_boolean(cscene, "use_square_samples"))
250                                         light->samples = samples * samples;
251                                 else
252                                         light->samples = samples;
253
254                                 light->tag_update(scene);
255                                 light_map.set_recalc(b_world);
256                         }
257                 }
258         }
259
260         world_map = b_world.ptr.data;
261         world_recalc = false;
262 }
263
264 /* Object */
265
266 Object *BlenderSync::sync_object(BL::Depsgraph::duplis_iterator& b_dupli_iter,
267                                  uint layer_flag,
268                                  float motion_time,
269                                  bool hide_tris,
270                                  BlenderObjectCulling& culling,
271                                  bool *use_portal)
272 {
273         const bool is_instance = b_dupli_iter->is_instance();
274         BL::Object b_ob = b_dupli_iter->object();
275         BL::Object b_parent = is_instance ? b_dupli_iter->parent()
276                                           : b_dupli_iter->object();
277         BL::Object b_ob_instance = is_instance ? b_dupli_iter->instance_object()
278                                                : b_ob;
279         const bool motion = motion_time != 0.0f;
280         /*const*/ Transform tfm = get_transform(b_ob.matrix_world());
281         int *persistent_id = NULL;
282         BL::Array<int, OBJECT_PERSISTENT_ID_SIZE> persistent_id_array;
283         if(is_instance) {
284                 persistent_id_array = b_dupli_iter->persistent_id();
285                 persistent_id = persistent_id_array.data;
286         }
287
288         /* light is handled separately */
289         if(object_is_light(b_ob)) {
290                 /* don't use lamps for excluded layers used as mask layer */
291                 if(!motion && !((layer_flag & render_layer.holdout_layer) &&
292                                 (layer_flag & render_layer.exclude_layer)))
293                 {
294                         sync_light(b_parent,
295                                    persistent_id,
296                                    b_ob,
297                                    b_ob_instance,
298                                    is_instance ? b_dupli_iter->random_id() : 0,
299                                    tfm,
300                                    use_portal);
301                 }
302
303                 return NULL;
304         }
305
306         /* only interested in object that we can create meshes from */
307         if(!object_is_mesh(b_ob)) {
308                 return NULL;
309         }
310
311         /* Perform object culling. */
312         if(culling.test(scene, b_ob, tfm)) {
313                 return NULL;
314         }
315
316         /* Visibility flags for both parent and child. */
317         bool use_holdout = (layer_flag & render_layer.holdout_layer) != 0;
318         uint visibility = object_ray_visibility(b_ob) & PATH_RAY_ALL_VISIBILITY;
319
320         if(b_parent.ptr.data != b_ob.ptr.data) {
321                 visibility &= object_ray_visibility(b_parent);
322         }
323
324         /* Make holdout objects on excluded layer invisible for non-camera rays. */
325         if(use_holdout && (layer_flag & render_layer.exclude_layer)) {
326                 visibility &= ~(PATH_RAY_ALL_VISIBILITY - PATH_RAY_CAMERA);
327         }
328
329         /* Hide objects not on render layer from camera rays. */
330         if(!(layer_flag & render_layer.layer)) {
331                 visibility &= ~PATH_RAY_CAMERA;
332         }
333
334         /* Don't export completely invisible objects. */
335         if(visibility == 0) {
336                 return NULL;
337         }
338
339         /* key to lookup object */
340         ObjectKey key(b_parent, persistent_id, b_ob_instance);
341         Object *object;
342
343         /* motion vector case */
344         if(motion) {
345                 object = object_map.find(key);
346
347                 if(object && (scene->need_motion() == Scene::MOTION_PASS ||
348                               object_use_motion(b_parent, b_ob)))
349                 {
350                         /* object transformation */
351                         if(tfm != object->tfm) {
352                                 VLOG(1) << "Object " << b_ob.name() << " motion detected.";
353                                 if(motion_time == -1.0f || motion_time == 1.0f) {
354                                         object->use_motion = true;
355                                 }
356                         }
357
358                         if(motion_time == -1.0f) {
359                                 object->motion.pre = tfm;
360                         }
361                         else if(motion_time == 1.0f) {
362                                 object->motion.post = tfm;
363                         }
364
365                         /* mesh deformation */
366                         if(object->mesh)
367                                 sync_mesh_motion(b_ob, object, motion_time);
368                 }
369
370                 return object;
371         }
372
373         /* test if we need to sync */
374         bool object_updated = false;
375
376         if(object_map.sync(&object, b_ob, b_parent, key))
377                 object_updated = true;
378         
379         /* mesh sync */
380         object->mesh = sync_mesh(b_ob, b_ob_instance, object_updated, hide_tris);
381
382         /* special case not tracked by object update flags */
383
384         /* holdout */
385         if(use_holdout != object->use_holdout) {
386                 object->use_holdout = use_holdout;
387                 scene->object_manager->tag_update(scene);
388                 object_updated = true;
389         }
390
391         if(visibility != object->visibility) {
392                 object->visibility = visibility;
393                 object_updated = true;
394         }
395
396         PointerRNA cobject = RNA_pointer_get(&b_ob.ptr, "cycles");
397         bool is_shadow_catcher = get_boolean(cobject, "is_shadow_catcher");
398         if(is_shadow_catcher != object->is_shadow_catcher) {
399                 object->is_shadow_catcher = is_shadow_catcher;
400                 object_updated = true;
401         }
402
403         /* object sync
404          * transform comparison should not be needed, but duplis don't work perfect
405          * in the depsgraph and may not signal changes, so this is a workaround */
406         if(object_updated || (object->mesh && object->mesh->need_update) || tfm != object->tfm) {
407                 object->name = b_ob.name().c_str();
408                 object->pass_id = b_ob.pass_index();
409                 object->tfm = tfm;
410                 object->motion.pre = transform_empty();
411                 object->motion.post = transform_empty();
412                 object->use_motion = false;
413
414                 /* motion blur */
415                 if(scene->need_motion() == Scene::MOTION_BLUR && object->mesh) {
416                         Mesh *mesh = object->mesh;
417
418                         mesh->use_motion_blur = false;
419
420                         if(object_use_motion(b_parent, b_ob)) {
421                                 if(object_use_deform_motion(b_parent, b_ob)) {
422                                         mesh->motion_steps = object_motion_steps(b_ob);
423                                         mesh->use_motion_blur = true;
424                                 }
425
426                                 vector<float> times = object->motion_times();
427                                 foreach(float time, times)
428                                         motion_times.insert(time);
429                         }
430                 }
431
432                 /* dupli texture coordinates and random_id */
433                 if(is_instance) {
434                         object->dupli_generated = 0.5f*get_float3(b_dupli_iter->orco()) - make_float3(0.5f, 0.5f, 0.5f);
435                         object->dupli_uv = get_float2(b_dupli_iter->uv());
436                         object->random_id = b_dupli_iter->random_id();
437                 }
438                 else {
439                         object->dupli_generated = make_float3(0.0f, 0.0f, 0.0f);
440                         object->dupli_uv = make_float2(0.0f, 0.0f);
441                         object->random_id =  hash_int_2d(hash_string(object->name.c_str()), 0);
442                 }
443
444                 object->tag_update(scene);
445         }
446
447         return object;
448 }
449
450 static bool object_render_hide_original(BL::Object::type_enum ob_type,
451                                         BL::Object::dupli_type_enum dupli_type)
452 {
453         /* metaball exception, they duplicate self */
454         if(ob_type == BL::Object::type_META)
455                 return false;
456
457         return (dupli_type == BL::Object::dupli_type_VERTS ||
458                 dupli_type == BL::Object::dupli_type_FACES ||
459                 dupli_type == BL::Object::dupli_type_FRAMES);
460 }
461
462 static bool object_render_hide(BL::Object& b_ob,
463                                bool top_level,
464                                bool parent_hide,
465                                bool& hide_triangles)
466 {
467         /* check if we should render or hide particle emitter */
468         BL::Object::particle_systems_iterator b_psys;
469
470         bool hair_present = false;
471         bool show_emitter = false;
472         bool hide_emitter = false;
473         bool hide_as_dupli_parent = false;
474         bool hide_as_dupli_child_original = false;
475
476         for(b_ob.particle_systems.begin(b_psys); b_psys != b_ob.particle_systems.end(); ++b_psys) {
477                 if((b_psys->settings().render_type() == BL::ParticleSettings::render_type_PATH) &&
478                    (b_psys->settings().type()==BL::ParticleSettings::type_HAIR))
479                         hair_present = true;
480
481                 if(b_psys->settings().use_render_emitter())
482                         show_emitter = true;
483                 else
484                         hide_emitter = true;
485         }
486
487         if(show_emitter)
488                 hide_emitter = false;
489
490         /* duplicators hidden by default, except dupliframes which duplicate self */
491         if(b_ob.is_duplicator())
492                 if(top_level || b_ob.dupli_type() != BL::Object::dupli_type_FRAMES)
493                         hide_as_dupli_parent = true;
494
495         /* hide original object for duplis */
496         BL::Object parent = b_ob.parent();
497         while(parent) {
498                 if(object_render_hide_original(b_ob.type(),
499                                                parent.dupli_type()))
500                 {
501                         if(parent_hide) {
502                                 hide_as_dupli_child_original = true;
503                                 break;
504                         }
505                 }
506                 parent = parent.parent();
507         }
508         
509         hide_triangles = hide_emitter;
510
511         if(show_emitter) {
512                 return false;
513         }
514         else if(hair_present) {
515                 return hide_as_dupli_child_original;
516         }
517         else {
518                 return (hide_as_dupli_parent || hide_as_dupli_child_original);
519         }
520 }
521
522 /* Object Loop */
523
524 void BlenderSync::sync_objects(float motion_time)
525 {
526         /* layer data */
527         bool motion = motion_time != 0.0f;
528         
529         if(!motion) {
530                 /* prepare for sync */
531                 light_map.pre_sync();
532                 mesh_map.pre_sync();
533                 object_map.pre_sync();
534                 particle_system_map.pre_sync();
535                 motion_times.clear();
536         }
537         else {
538                 mesh_motion_synced.clear();
539         }
540
541         /* initialize culling */
542         BlenderObjectCulling culling(scene, b_scene);
543
544         /* object loop */
545         bool cancel = false;
546         bool use_portal = false;
547
548         BL::Depsgraph::duplis_iterator b_dupli_iter;
549         for(b_depsgraph.duplis.begin(b_dupli_iter);
550             b_dupli_iter != b_depsgraph.duplis.end() && !cancel;
551             ++b_dupli_iter)
552         {
553                 BL::Object b_ob = b_dupli_iter->object();
554                 if(!b_ob.is_visible()) {
555                         continue;
556                 }
557
558                 progress.set_sync_status("Synchronizing object", b_ob.name());
559
560                 /* load per-object culling data */
561                 culling.init_object(scene, b_ob);
562
563                 /* test if object needs to be hidden */
564                 bool hide_tris;
565
566                  if(!object_render_hide(b_ob, true, true, hide_tris)) {
567                         /* object itself */
568                         sync_object(b_dupli_iter,
569                                     ~(0), /* until we get rid of layers */
570                                     motion_time,
571                                     hide_tris,
572                                     culling,
573                                     &use_portal);
574                  }
575
576                 cancel = progress.get_cancel();
577         }
578
579         progress.set_sync_status("");
580
581         if(!cancel && !motion) {
582                 sync_background_light(use_portal);
583
584                 /* handle removed data and modified pointers */
585                 if(light_map.post_sync())
586                         scene->light_manager->tag_update(scene);
587                 if(mesh_map.post_sync())
588                         scene->mesh_manager->tag_update(scene);
589                 if(object_map.post_sync())
590                         scene->object_manager->tag_update(scene);
591                 if(particle_system_map.post_sync())
592                         scene->particle_system_manager->tag_update(scene);
593         }
594
595         if(motion)
596                 mesh_motion_synced.clear();
597 }
598
599 void BlenderSync::sync_motion(BL::RenderSettings& b_render,
600                               BL::Object& b_override,
601                               int width, int height,
602                               void **python_thread_state)
603 {
604         if(scene->need_motion() == Scene::MOTION_NONE)
605                 return;
606
607         /* get camera object here to deal with camera switch */
608         BL::Object b_cam = b_scene.camera();
609         if(b_override)
610                 b_cam = b_override;
611
612         Camera prevcam = *(scene->camera);
613
614         int frame_center = b_scene.frame_current();
615         float subframe_center = b_scene.frame_subframe();
616         float frame_center_delta = 0.0f;
617
618         if(scene->need_motion() != Scene::MOTION_PASS &&
619            scene->camera->motion_position != Camera::MOTION_POSITION_CENTER)
620         {
621                 float shuttertime = scene->camera->shuttertime;
622                 if(scene->camera->motion_position == Camera::MOTION_POSITION_END) {
623                         frame_center_delta = -shuttertime * 0.5f;
624                 }
625                 else {
626                         assert(scene->camera->motion_position == Camera::MOTION_POSITION_START);
627                         frame_center_delta = shuttertime * 0.5f;
628                 }
629                 float time = frame_center + subframe_center + frame_center_delta;
630                 int frame = (int)floorf(time);
631                 float subframe = time - frame;
632                 python_thread_state_restore(python_thread_state);
633                 b_engine.frame_set(frame, subframe);
634                 python_thread_state_save(python_thread_state);
635                 sync_camera_motion(b_render, b_cam, width, height, 0.0f);
636                 sync_objects(0.0f);
637         }
638
639         /* always sample these times for camera motion */
640         motion_times.insert(-1.0f);
641         motion_times.insert(1.0f);
642
643         /* note iteration over motion_times set happens in sorted order */
644         foreach(float relative_time, motion_times) {
645                 VLOG(1) << "Synchronizing motion for the relative time "
646                         << relative_time << ".";
647
648                 /* fixed shutter time to get previous and next frame for motion pass */
649                 float shuttertime = scene->motion_shutter_time();
650
651                 /* compute frame and subframe time */
652                 float time = frame_center + subframe_center + frame_center_delta + relative_time * shuttertime * 0.5f;
653                 int frame = (int)floorf(time);
654                 float subframe = time - frame;
655
656                 /* change frame */
657                 python_thread_state_restore(python_thread_state);
658                 b_engine.frame_set(frame, subframe);
659                 python_thread_state_save(python_thread_state);
660
661                 /* sync camera, only supports two times at the moment */
662                 if(relative_time == -1.0f || relative_time == 1.0f) {
663                         sync_camera_motion(b_render,
664                                            b_cam,
665                                            width, height,
666                                            relative_time);
667                 }
668
669                 /* sync object */
670                 sync_objects(relative_time);
671         }
672
673         /* we need to set the python thread state again because this
674          * function assumes it is being executed from python and will
675          * try to save the thread state */
676         python_thread_state_restore(python_thread_state);
677         b_engine.frame_set(frame_center, subframe_center);
678         python_thread_state_save(python_thread_state);
679
680         /* tag camera for motion update */
681         if(scene->camera->motion_modified(prevcam))
682                 scene->camera->tag_update();
683 }
684
685 CCL_NAMESPACE_END
686