Cycles: distance culling for objects.
[blender-staging.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 "camera.h"
18 #include "integrator.h"
19 #include "graph.h"
20 #include "light.h"
21 #include "mesh.h"
22 #include "object.h"
23 #include "scene.h"
24 #include "nodes.h"
25 #include "particles.h"
26 #include "shader.h"
27
28 #include "blender_sync.h"
29 #include "blender_util.h"
30
31 #include "util_foreach.h"
32 #include "util_hash.h"
33 #include "util_logging.h"
34
35 CCL_NAMESPACE_BEGIN
36
37 /* Utilities */
38
39 bool BlenderSync::BKE_object_is_modified(BL::Object& b_ob)
40 {
41         /* test if we can instance or if the object is modified */
42         if(b_ob.type() == BL::Object::type_META) {
43                 /* multi-user and dupli metaballs are fused, can't instance */
44                 return true;
45         }
46         else if(ccl::BKE_object_is_modified(b_ob, b_scene, preview)) {
47                 /* modifiers */
48                 return true;
49         }
50         else {
51                 /* object level material links */
52                 BL::Object::material_slots_iterator slot;
53                 for(b_ob.material_slots.begin(slot); slot != b_ob.material_slots.end(); ++slot)
54                         if(slot->link() == BL::MaterialSlot::link_OBJECT)
55                                 return true;
56         }
57
58         return false;
59 }
60
61 bool BlenderSync::object_is_mesh(BL::Object& b_ob)
62 {
63         BL::ID b_ob_data = b_ob.data();
64
65         return (b_ob_data && (b_ob_data.is_a(&RNA_Mesh) ||
66                 b_ob_data.is_a(&RNA_Curve) || b_ob_data.is_a(&RNA_MetaBall)));
67 }
68
69 bool BlenderSync::object_is_light(BL::Object& b_ob)
70 {
71         BL::ID b_ob_data = b_ob.data();
72
73         return (b_ob_data && b_ob_data.is_a(&RNA_Lamp));
74 }
75
76 static uint object_ray_visibility(BL::Object& b_ob)
77 {
78         PointerRNA cvisibility = RNA_pointer_get(&b_ob.ptr, "cycles_visibility");
79         uint flag = 0;
80
81         flag |= get_boolean(cvisibility, "camera")? PATH_RAY_CAMERA: 0;
82         flag |= get_boolean(cvisibility, "diffuse")? PATH_RAY_DIFFUSE: 0;
83         flag |= get_boolean(cvisibility, "glossy")? PATH_RAY_GLOSSY: 0;
84         flag |= get_boolean(cvisibility, "transmission")? PATH_RAY_TRANSMIT: 0;
85         flag |= get_boolean(cvisibility, "shadow")? PATH_RAY_SHADOW: 0;
86         flag |= get_boolean(cvisibility, "scatter")? PATH_RAY_VOLUME_SCATTER: 0;
87
88         return flag;
89 }
90
91 /* Light */
92
93 void BlenderSync::sync_light(BL::Object& b_parent,
94                              int persistent_id[OBJECT_PERSISTENT_ID_SIZE],
95                              BL::Object& b_ob,
96                              Transform& tfm,
97                              bool *use_portal)
98 {
99         /* test if we need to sync */
100         Light *light;
101         ObjectKey key(b_parent, persistent_id, b_ob);
102
103         if(!light_map.sync(&light, b_ob, b_parent, key)) {
104                 if(light->is_portal)
105                         *use_portal = true;
106                 return;
107         }
108         
109         BL::Lamp b_lamp(b_ob.data());
110
111         /* type */
112         switch(b_lamp.type()) {
113                 case BL::Lamp::type_POINT: {
114                         BL::PointLamp b_point_lamp(b_lamp);
115                         light->size = b_point_lamp.shadow_soft_size();
116                         light->type = LIGHT_POINT;
117                         break;
118                 }
119                 case BL::Lamp::type_SPOT: {
120                         BL::SpotLamp b_spot_lamp(b_lamp);
121                         light->size = b_spot_lamp.shadow_soft_size();
122                         light->type = LIGHT_SPOT;
123                         light->spot_angle = b_spot_lamp.spot_size();
124                         light->spot_smooth = b_spot_lamp.spot_blend();
125                         break;
126                 }
127                 case BL::Lamp::type_HEMI: {
128                         light->type = LIGHT_DISTANT;
129                         light->size = 0.0f;
130                         break;
131                 }
132                 case BL::Lamp::type_SUN: {
133                         BL::SunLamp b_sun_lamp(b_lamp);
134                         light->size = b_sun_lamp.shadow_soft_size();
135                         light->type = LIGHT_DISTANT;
136                         break;
137                 }
138                 case BL::Lamp::type_AREA: {
139                         BL::AreaLamp b_area_lamp(b_lamp);
140                         light->size = 1.0f;
141                         light->axisu = transform_get_column(&tfm, 0);
142                         light->axisv = transform_get_column(&tfm, 1);
143                         light->sizeu = b_area_lamp.size();
144                         if(b_area_lamp.shape() == BL::AreaLamp::shape_RECTANGLE)
145                                 light->sizev = b_area_lamp.size_y();
146                         else
147                                 light->sizev = light->sizeu;
148                         light->type = LIGHT_AREA;
149                         break;
150                 }
151         }
152
153         /* location and (inverted!) direction */
154         light->co = transform_get_column(&tfm, 3);
155         light->dir = -transform_get_column(&tfm, 2);
156         light->tfm = tfm;
157
158         /* shader */
159         vector<Shader*> used_shaders;
160         find_shader(b_lamp, used_shaders, scene->default_light);
161         light->shader = used_shaders[0];
162
163         /* shadow */
164         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
165         PointerRNA clamp = RNA_pointer_get(&b_lamp.ptr, "cycles");
166         light->cast_shadow = get_boolean(clamp, "cast_shadow");
167         light->use_mis = get_boolean(clamp, "use_multiple_importance_sampling");
168         
169         int samples = get_int(clamp, "samples");
170         if(get_boolean(cscene, "use_square_samples"))
171                 light->samples = samples * samples;
172         else
173                 light->samples = samples;
174
175         light->max_bounces = get_int(clamp, "max_bounces");
176
177         if(light->type == LIGHT_AREA)
178                 light->is_portal = get_boolean(clamp, "is_portal");
179         else
180                 light->is_portal = false;
181
182         if(light->is_portal)
183                 *use_portal = true;
184
185         /* visibility */
186         uint visibility = object_ray_visibility(b_ob);
187         light->use_diffuse = (visibility & PATH_RAY_DIFFUSE) != 0;
188         light->use_glossy = (visibility & PATH_RAY_GLOSSY) != 0;
189         light->use_transmission = (visibility & PATH_RAY_TRANSMIT) != 0;
190         light->use_scatter = (visibility & PATH_RAY_VOLUME_SCATTER) != 0;
191
192         /* tag */
193         light->tag_update(scene);
194 }
195
196 void BlenderSync::sync_background_light(bool use_portal)
197 {
198         BL::World b_world = b_scene.world();
199
200         if(b_world) {
201                 PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
202                 PointerRNA cworld = RNA_pointer_get(&b_world.ptr, "cycles");
203                 bool sample_as_light = get_boolean(cworld, "sample_as_light");
204
205                 if(sample_as_light || use_portal) {
206                         /* test if we need to sync */
207                         Light *light;
208                         ObjectKey key(b_world, 0, b_world);
209
210                         if(light_map.sync(&light, b_world, b_world, key) ||
211                             world_recalc ||
212                             b_world.ptr.data != world_map)
213                         {
214                                 light->type = LIGHT_BACKGROUND;
215                                 light->map_resolution  = get_int(cworld, "sample_map_resolution");
216                                 light->shader = scene->default_background;
217                                 light->use_mis = sample_as_light;
218                                 light->max_bounces = get_int(cworld, "max_bounces");
219
220                                 int samples = get_int(cworld, "samples");
221                                 if(get_boolean(cscene, "use_square_samples"))
222                                         light->samples = samples * samples;
223                                 else
224                                         light->samples = samples;
225
226                                 light->tag_update(scene);
227                                 light_map.set_recalc(b_world);
228                         }
229                 }
230         }
231
232         world_map = b_world.ptr.data;
233         world_recalc = false;
234 }
235
236 /* Object */
237
238 /* TODO(sergey): Not really optimal, consider approaches based on k-DOP in order
239  * to reduce number of objects which are wrongly considered visible.
240  */
241 static bool object_boundbox_clip(Scene *scene,
242                                  BL::Object& b_ob,
243                                  Transform& tfm,
244                                  float margin)
245 {
246         Camera *cam = scene->camera;
247         Transform& worldtondc = cam->worldtondc;
248         BL::Array<float, 24> boundbox = b_ob.bound_box();
249         float3 bb_min = make_float3(FLT_MAX, FLT_MAX, FLT_MAX),
250                bb_max = make_float3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
251         bool all_behind = true;
252         for(int i = 0; i < 8; ++i) {
253                 float3 p = make_float3(boundbox[3 * i + 0],
254                                        boundbox[3 * i + 1],
255                                        boundbox[3 * i + 2]);
256                 p = transform_point(&tfm, p);
257
258                 float4 b = make_float4(p.x, p.y, p.z, 1.0f);
259                 float4 c = make_float4(dot(worldtondc.x, b),
260                                        dot(worldtondc.y, b),
261                                        dot(worldtondc.z, b),
262                                        dot(worldtondc.w, b));
263                 p = float4_to_float3(c / c.w);
264                 if(c.z < 0.0f) {
265                         p.x = 1.0f - p.x;
266                         p.y = 1.0f - p.y;
267                 }
268                 if(c.z >= -margin) {
269                         all_behind = false;
270                 }
271                 bb_min = min(bb_min, p);
272                 bb_max = max(bb_max, p);
273         }
274         if(!all_behind) {
275                 if(bb_min.x >= 1.0f + margin ||
276                    bb_min.y >= 1.0f + margin ||
277                    bb_max.x <= -margin ||
278                    bb_max.y <= -margin)
279                 {
280                         return true;
281                 }
282                 return false;
283         }
284         return true;
285 }
286
287 static bool object_distance_clip(Scene *scene,
288                                  BL::Object& b_ob,
289                                  Transform& tfm,
290                                  float margin)
291 {
292         BL::Array<float, 24> boundbox = b_ob.bound_box();
293         float3 camera_position = transform_get_column(&scene->camera->matrix, 3);
294
295         float3 bb_min = make_float3(FLT_MAX, FLT_MAX, FLT_MAX),
296                bb_max = make_float3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
297
298         /* Find min & max points for x & y & z on bounding box */
299         for(int i = 0; i < 8; ++i) {
300                 float3 p = make_float3(boundbox[3 * i + 0],
301                                        boundbox[3 * i + 1],
302                                        boundbox[3 * i + 2]);
303                 p = transform_point(&tfm, p);
304                 bb_min = min(bb_min, p);
305                 bb_max = max(bb_max, p);
306         }
307
308         float3 closest_point = max(min(bb_max,camera_position),bb_min);
309         return (len_squared(camera_position - closest_point) > margin * margin);
310 }
311
312 Object *BlenderSync::sync_object(BL::Object& b_parent,
313                                  int persistent_id[OBJECT_PERSISTENT_ID_SIZE],
314                                  BL::DupliObject& b_dupli_ob,
315                                  Transform& tfm,
316                                  uint layer_flag,
317                                  float motion_time,
318                                  bool hide_tris,
319                                  bool use_camera_cull,
320                                  bool use_distance_cull,
321                                  float camera_cull_margin,
322                                  float distance_cull_margin,
323                                  bool *use_portal)
324 {
325         BL::Object b_ob = (b_dupli_ob ? b_dupli_ob.object() : b_parent);
326         bool motion = motion_time != 0.0f;
327         
328         /* light is handled separately */
329         if(object_is_light(b_ob)) {
330                 /* don't use lamps for excluded layers used as mask layer */
331                 if(!motion && !((layer_flag & render_layer.holdout_layer) && (layer_flag & render_layer.exclude_layer)))
332                         sync_light(b_parent, persistent_id, b_ob, tfm, use_portal);
333
334                 return NULL;
335         }
336
337         /* only interested in object that we can create meshes from */
338         if(!object_is_mesh(b_ob))
339                 return NULL;
340
341         /* Perform object culling. */
342         bool camera_culled = use_camera_cull && object_boundbox_clip(scene, b_ob, tfm, camera_cull_margin);
343         bool distance_culled = use_distance_cull && object_distance_clip(scene, b_ob, tfm, distance_cull_margin);
344
345         if ((camera_culled && distance_culled) ||
346             (camera_culled && !use_distance_cull) ||
347             (distance_culled && !use_camera_cull))
348         {
349                 return NULL;
350         }
351
352         /* key to lookup object */
353         ObjectKey key(b_parent, persistent_id, b_ob);
354         Object *object;
355
356         /* motion vector case */
357         if(motion) {
358                 object = object_map.find(key);
359
360                 if(object && (scene->need_motion() == Scene::MOTION_PASS ||
361                               object_use_motion(b_parent, b_ob)))
362                 {
363                         /* object transformation */
364                         if(tfm != object->tfm) {
365                                 VLOG(1) << "Object " << b_ob.name() << " motion detected.";
366                                 if(motion_time == -1.0f || motion_time == 1.0f) {
367                                         object->use_motion = true;
368                                 }
369                         }
370
371                         if(motion_time == -1.0f) {
372                                 object->motion.pre = tfm;
373                         }
374                         else if(motion_time == 1.0f) {
375                                 object->motion.post = tfm;
376                         }
377
378                         /* mesh deformation */
379                         if(object->mesh)
380                                 sync_mesh_motion(b_ob, object, motion_time);
381                 }
382
383                 return object;
384         }
385
386         /* test if we need to sync */
387         bool object_updated = false;
388
389         if(object_map.sync(&object, b_ob, b_parent, key))
390                 object_updated = true;
391         
392         bool use_holdout = (layer_flag & render_layer.holdout_layer) != 0;
393         
394         /* mesh sync */
395         object->mesh = sync_mesh(b_ob, object_updated, hide_tris);
396
397         /* special case not tracked by object update flags */
398
399         /* holdout */
400         if(use_holdout != object->use_holdout) {
401                 object->use_holdout = use_holdout;
402                 scene->object_manager->tag_update(scene);
403                 object_updated = true;
404         }
405
406         /* visibility flags for both parent and child */
407         uint visibility = object_ray_visibility(b_ob) & PATH_RAY_ALL_VISIBILITY;
408         if(b_parent.ptr.data != b_ob.ptr.data) {
409                 visibility &= object_ray_visibility(b_parent);
410         }
411
412         /* make holdout objects on excluded layer invisible for non-camera rays */
413         if(use_holdout && (layer_flag & render_layer.exclude_layer)) {
414                 visibility &= ~(PATH_RAY_ALL_VISIBILITY - PATH_RAY_CAMERA);
415         }
416
417         /* hide objects not on render layer from camera rays */
418         if(!(layer_flag & render_layer.layer)) {
419                 visibility &= ~PATH_RAY_CAMERA;
420         }
421
422         if(visibility != object->visibility) {
423                 object->visibility = visibility;
424                 object_updated = true;
425         }
426
427         /* object sync
428          * transform comparison should not be needed, but duplis don't work perfect
429          * in the depsgraph and may not signal changes, so this is a workaround */
430         if(object_updated || (object->mesh && object->mesh->need_update) || tfm != object->tfm) {
431                 object->name = b_ob.name().c_str();
432                 object->pass_id = b_ob.pass_index();
433                 object->tfm = tfm;
434                 object->motion.pre = transform_empty();
435                 object->motion.post = transform_empty();
436                 object->use_motion = false;
437
438                 /* motion blur */
439                 if(scene->need_motion() == Scene::MOTION_BLUR && object->mesh) {
440                         Mesh *mesh = object->mesh;
441
442                         mesh->use_motion_blur = false;
443
444                         if(object_use_motion(b_parent, b_ob)) {
445                                 if(object_use_deform_motion(b_parent, b_ob)) {
446                                         mesh->motion_steps = object_motion_steps(b_ob);
447                                         mesh->use_motion_blur = true;
448                                 }
449
450                                 vector<float> times = object->motion_times();
451                                 foreach(float time, times)
452                                         motion_times.insert(time);
453                         }
454                 }
455
456                 /* random number */
457                 object->random_id = hash_string(object->name.c_str());
458
459                 if(persistent_id) {
460                         for(int i = 0; i < OBJECT_PERSISTENT_ID_SIZE; i++)
461                                 object->random_id = hash_int_2d(object->random_id, persistent_id[i]);
462                 }
463                 else
464                         object->random_id = hash_int_2d(object->random_id, 0);
465
466                 if(b_parent.ptr.data != b_ob.ptr.data)
467                         object->random_id ^= hash_int(hash_string(b_parent.name().c_str()));
468
469                 /* dupli texture coordinates */
470                 if(b_dupli_ob) {
471                         object->dupli_generated = 0.5f*get_float3(b_dupli_ob.orco()) - make_float3(0.5f, 0.5f, 0.5f);
472                         object->dupli_uv = get_float2(b_dupli_ob.uv());
473                 }
474                 else {
475                         object->dupli_generated = make_float3(0.0f, 0.0f, 0.0f);
476                         object->dupli_uv = make_float2(0.0f, 0.0f);
477                 }
478
479                 object->tag_update(scene);
480         }
481
482         return object;
483 }
484
485 static bool object_render_hide_original(BL::Object::type_enum ob_type,
486                                         BL::Object::dupli_type_enum dupli_type)
487 {
488         /* metaball exception, they duplicate self */
489         if(ob_type == BL::Object::type_META)
490                 return false;
491
492         return (dupli_type == BL::Object::dupli_type_VERTS ||
493                 dupli_type == BL::Object::dupli_type_FACES ||
494                 dupli_type == BL::Object::dupli_type_FRAMES);
495 }
496
497 static bool object_render_hide(BL::Object& b_ob,
498                                bool top_level,
499                                bool parent_hide,
500                                bool& hide_triangles)
501 {
502         /* check if we should render or hide particle emitter */
503         BL::Object::particle_systems_iterator b_psys;
504
505         bool hair_present = false;
506         bool show_emitter = false;
507         bool hide_emitter = false;
508         bool hide_as_dupli_parent = false;
509         bool hide_as_dupli_child_original = false;
510
511         for(b_ob.particle_systems.begin(b_psys); b_psys != b_ob.particle_systems.end(); ++b_psys) {
512                 if((b_psys->settings().render_type() == BL::ParticleSettings::render_type_PATH) &&
513                    (b_psys->settings().type()==BL::ParticleSettings::type_HAIR))
514                         hair_present = true;
515
516                 if(b_psys->settings().use_render_emitter())
517                         show_emitter = true;
518                 else
519                         hide_emitter = true;
520         }
521
522         if(show_emitter)
523                 hide_emitter = false;
524
525         /* duplicators hidden by default, except dupliframes which duplicate self */
526         if(b_ob.is_duplicator())
527                 if(top_level || b_ob.dupli_type() != BL::Object::dupli_type_FRAMES)
528                         hide_as_dupli_parent = true;
529
530         /* hide original object for duplis */
531         BL::Object parent = b_ob.parent();
532         while(parent) {
533                 if(object_render_hide_original(b_ob.type(),
534                                                parent.dupli_type()))
535                 {
536                         if(parent_hide) {
537                                 hide_as_dupli_child_original = true;
538                                 break;
539                         }
540                 }
541                 parent = parent.parent();
542         }
543         
544         hide_triangles = hide_emitter;
545
546         if(show_emitter) {
547                 return false;
548         }
549         else if(hair_present) {
550                 return hide_as_dupli_child_original;
551         }
552         else {
553                 return (hide_as_dupli_parent || hide_as_dupli_child_original);
554         }
555 }
556
557 static bool object_render_hide_duplis(BL::Object& b_ob)
558 {
559         BL::Object parent = b_ob.parent();
560
561         return (parent && object_render_hide_original(b_ob.type(), parent.dupli_type()));
562 }
563
564 /* Object Loop */
565
566 void BlenderSync::sync_objects(BL::SpaceView3D& b_v3d, float motion_time)
567 {
568         /* layer data */
569         uint scene_layer = render_layer.scene_layer;
570         bool motion = motion_time != 0.0f;
571         
572         if(!motion) {
573                 /* prepare for sync */
574                 light_map.pre_sync();
575                 mesh_map.pre_sync();
576                 object_map.pre_sync();
577                 particle_system_map.pre_sync();
578                 motion_times.clear();
579         }
580         else {
581                 mesh_motion_synced.clear();
582         }
583
584         bool allow_camera_cull = false;
585         bool allow_distance_cull = false;
586         float camera_cull_margin = 0.0f;
587         float distance_cull_margin = 0.0f;
588         if(b_scene.render().use_simplify()) {
589                 PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
590                 allow_camera_cull = scene->camera->type != CAMERA_PANORAMA &&
591                                     !b_scene.render().use_multiview() &&
592                                     get_boolean(cscene, "use_camera_cull");
593                 allow_distance_cull = scene->camera->type != CAMERA_PANORAMA &&
594                                     !b_scene.render().use_multiview() &&
595                                     get_boolean(cscene, "use_distance_cull");
596                 if(allow_camera_cull) {
597                         camera_cull_margin = get_float(cscene, "camera_cull_margin");
598                 }
599                 if(allow_distance_cull) {
600                         distance_cull_margin = get_float(cscene, "distance_cull_margin");
601                         if (distance_cull_margin == 0.0f) {
602                                 allow_distance_cull = false;
603                         }
604                 }
605         }
606
607         /* object loop */
608         BL::Scene::object_bases_iterator b_base;
609         BL::Scene b_sce = b_scene;
610         /* modifier result type (not exposed as enum in C++ API)
611          * 1 : DAG_EVAL_PREVIEW
612          * 2 : DAG_EVAL_RENDER
613          */
614         int dupli_settings = preview ? 1 : 2;
615
616         bool cancel = false;
617         bool use_portal = false;
618
619         uint layer_override = get_layer(b_engine.layer_override());
620         for(; b_sce && !cancel; b_sce = b_sce.background_set()) {
621                 /* Render layer's scene_layer is affected by local view already,
622                  * which is not a desired behavior here.
623                  */
624                 uint scene_layers = layer_override ? layer_override : get_layer(b_scene.layers());
625                 for(b_sce.object_bases.begin(b_base); b_base != b_sce.object_bases.end() && !cancel; ++b_base) {
626                         BL::Object b_ob = b_base->object();
627                         bool hide = (render_layer.use_viewport_visibility)? b_ob.hide(): b_ob.hide_render();
628                         uint ob_layer = get_layer(b_base->layers(),
629                                                   b_base->layers_local_view(),
630                                                   object_is_light(b_ob),
631                                                   scene_layers);
632                         hide = hide || !(ob_layer & scene_layer);
633
634                         if(!hide) {
635                                 progress.set_sync_status("Synchronizing object", b_ob.name());
636
637                                 PointerRNA cobject = RNA_pointer_get(&b_ob.ptr, "cycles");
638                                 bool use_camera_cull = allow_camera_cull && get_boolean(cobject, "use_camera_cull");
639                                 bool use_distance_cull = allow_distance_cull && get_boolean(cobject, "use_distance_cull");
640                                 if(use_camera_cull || use_distance_cull) {
641                                         /* Need to have proper projection matrix. */
642                                         scene->camera->update();
643                                 }
644                                 if(b_ob.is_duplicator() && !object_render_hide_duplis(b_ob)) {
645                                         /* dupli objects */
646                                         b_ob.dupli_list_create(b_scene, dupli_settings);
647
648                                         BL::Object::dupli_list_iterator b_dup;
649
650                                         for(b_ob.dupli_list.begin(b_dup); b_dup != b_ob.dupli_list.end(); ++b_dup) {
651                                                 Transform tfm = get_transform(b_dup->matrix());
652                                                 BL::Object b_dup_ob = b_dup->object();
653                                                 bool dup_hide = (b_v3d)? b_dup_ob.hide(): b_dup_ob.hide_render();
654                                                 bool in_dupli_group = (b_dup->type() == BL::DupliObject::type_GROUP);
655                                                 bool hide_tris;
656
657                                                 if(!(b_dup->hide() || dup_hide || object_render_hide(b_dup_ob, false, in_dupli_group, hide_tris))) {
658                                                         /* the persistent_id allows us to match dupli objects
659                                                          * between frames and updates */
660                                                         BL::Array<int, OBJECT_PERSISTENT_ID_SIZE> persistent_id = b_dup->persistent_id();
661
662                                                         /* sync object and mesh or light data */
663                                                         Object *object = sync_object(b_ob,
664                                                                                      persistent_id.data,
665                                                                                      *b_dup,
666                                                                                      tfm,
667                                                                                      ob_layer,
668                                                                                      motion_time,
669                                                                                      hide_tris,
670                                                                                      use_camera_cull,
671                                                                                      use_distance_cull,
672                                                                                      camera_cull_margin,
673                                                                                      distance_cull_margin,
674                                                                                      &use_portal);
675
676                                                         /* sync possible particle data, note particle_id
677                                                          * starts counting at 1, first is dummy particle */
678                                                         if(!motion && object) {
679                                                                 sync_dupli_particle(b_ob, *b_dup, object);
680                                                         }
681
682                                                 }
683                                         }
684
685                                         b_ob.dupli_list_clear();
686                                 }
687
688                                 /* test if object needs to be hidden */
689                                 bool hide_tris;
690
691                                 if(!object_render_hide(b_ob, true, true, hide_tris)) {
692                                         /* object itself */
693                                         Transform tfm = get_transform(b_ob.matrix_world());
694                                         BL::DupliObject b_empty_dupli_ob(PointerRNA_NULL);
695                                         sync_object(b_ob,
696                                                     NULL,
697                                                     b_empty_dupli_ob,
698                                                     tfm,
699                                                     ob_layer,
700                                                     motion_time,
701                                                     hide_tris,
702                                                     use_camera_cull,
703                                                     use_distance_cull,
704                                                     camera_cull_margin,
705                                                     distance_cull_margin,
706                                                     &use_portal);
707                                 }
708                         }
709
710                         cancel = progress.get_cancel();
711                 }
712         }
713
714         progress.set_sync_status("");
715
716         if(!cancel && !motion) {
717                 sync_background_light(use_portal);
718
719                 /* handle removed data and modified pointers */
720                 if(light_map.post_sync())
721                         scene->light_manager->tag_update(scene);
722                 if(mesh_map.post_sync())
723                         scene->mesh_manager->tag_update(scene);
724                 if(object_map.post_sync())
725                         scene->object_manager->tag_update(scene);
726                 if(particle_system_map.post_sync())
727                         scene->particle_system_manager->tag_update(scene);
728         }
729
730         if(motion)
731                 mesh_motion_synced.clear();
732 }
733
734 void BlenderSync::sync_motion(BL::RenderSettings& b_render,
735                               BL::SpaceView3D& b_v3d,
736                               BL::Object& b_override,
737                               int width, int height,
738                               void **python_thread_state)
739 {
740         if(scene->need_motion() == Scene::MOTION_NONE)
741                 return;
742
743         /* get camera object here to deal with camera switch */
744         BL::Object b_cam = b_scene.camera();
745         if(b_override)
746                 b_cam = b_override;
747
748         Camera prevcam = *(scene->camera);
749
750         int frame_center = b_scene.frame_current();
751         float subframe_center = b_scene.frame_subframe();
752         float frame_center_delta = 0.0f;
753
754         if(scene->need_motion() != Scene::MOTION_PASS &&
755            scene->camera->motion_position != Camera::MOTION_POSITION_CENTER)
756         {
757                 float shuttertime = scene->camera->shuttertime;
758                 if(scene->camera->motion_position == Camera::MOTION_POSITION_END) {
759                         frame_center_delta = -shuttertime * 0.5f;
760                 }
761                 else {
762                         assert(scene->camera->motion_position == Camera::MOTION_POSITION_START);
763                         frame_center_delta = shuttertime * 0.5f;
764                 }
765                 float time = frame_center + subframe_center + frame_center_delta;
766                 int frame = (int)floorf(time);
767                 float subframe = time - frame;
768                 python_thread_state_restore(python_thread_state);
769                 b_engine.frame_set(frame, subframe);
770                 python_thread_state_save(python_thread_state);
771                 sync_camera_motion(b_render, b_cam, width, height, 0.0f);
772                 sync_objects(b_v3d, 0.0f);
773         }
774
775         /* always sample these times for camera motion */
776         motion_times.insert(-1.0f);
777         motion_times.insert(1.0f);
778
779         /* note iteration over motion_times set happens in sorted order */
780         foreach(float relative_time, motion_times) {
781                 VLOG(1) << "Synchronizing motion for the relative time "
782                         << relative_time << ".";
783
784                 /* fixed shutter time to get previous and next frame for motion pass */
785                 float shuttertime = scene->motion_shutter_time();
786
787                 /* compute frame and subframe time */
788                 float time = frame_center + subframe_center + frame_center_delta + relative_time * shuttertime * 0.5f;
789                 int frame = (int)floorf(time);
790                 float subframe = time - frame;
791
792                 /* change frame */
793                 python_thread_state_restore(python_thread_state);
794                 b_engine.frame_set(frame, subframe);
795                 python_thread_state_save(python_thread_state);
796
797                 /* sync camera, only supports two times at the moment */
798                 if(relative_time == -1.0f || relative_time == 1.0f) {
799                         sync_camera_motion(b_render,
800                                            b_cam,
801                                            width, height,
802                                            relative_time);
803                 }
804
805                 /* sync object */
806                 sync_objects(b_v3d, relative_time);
807         }
808
809         /* we need to set the python thread state again because this
810          * function assumes it is being executed from python and will
811          * try to save the thread state */
812         python_thread_state_restore(python_thread_state);
813         b_engine.frame_set(frame_center, subframe_center);
814         python_thread_state_save(python_thread_state);
815
816         /* tag camera for motion update */
817         if(scene->camera->motion_modified(prevcam))
818                 scene->camera->tag_update();
819 }
820
821 CCL_NAMESPACE_END
822