2 * Copyright 2011, Blender Foundation.
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.
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.
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.
27 #include "blender_sync.h"
28 #include "blender_util.h"
30 #include "util_foreach.h"
36 bool BlenderSync::object_is_modified(BL::Object b_ob)
38 /* test if we can instance or if the object is modified */
39 if(ccl::object_is_modified(b_ob, b_scene, preview)) {
44 /* object level material links */
45 BL::Object::material_slots_iterator slot;
46 for(b_ob.material_slots.begin(slot); slot != b_ob.material_slots.end(); ++slot)
47 if(slot->link() == BL::MaterialSlot::link_OBJECT)
54 bool BlenderSync::object_is_mesh(BL::Object b_ob)
56 BL::ID b_ob_data = b_ob.data();
58 return (b_ob_data && (b_ob_data.is_a(&RNA_Mesh) ||
59 b_ob_data.is_a(&RNA_Curve) || b_ob_data.is_a(&RNA_MetaBall)));
62 bool BlenderSync::object_is_light(BL::Object b_ob)
64 BL::ID b_ob_data = b_ob.data();
66 return (b_ob_data && b_ob_data.is_a(&RNA_Lamp));
69 static uint object_ray_visibility(BL::Object b_ob)
71 PointerRNA cvisibility = RNA_pointer_get(&b_ob.ptr, "cycles_visibility");
74 flag |= get_boolean(cvisibility, "camera")? PATH_RAY_CAMERA: 0;
75 flag |= get_boolean(cvisibility, "diffuse")? PATH_RAY_DIFFUSE: 0;
76 flag |= get_boolean(cvisibility, "glossy")? PATH_RAY_GLOSSY: 0;
77 flag |= get_boolean(cvisibility, "transmission")? PATH_RAY_TRANSMIT: 0;
78 flag |= get_boolean(cvisibility, "shadow")? PATH_RAY_SHADOW: 0;
85 void BlenderSync::sync_light(BL::Object b_parent, int b_index, BL::Object b_ob, Transform& tfm)
87 /* test if we need to sync */
89 ObjectKey key(b_parent, b_index, b_ob);
91 if(!light_map.sync(&light, b_ob, b_parent, key))
94 BL::Lamp b_lamp(b_ob.data());
97 switch(b_lamp.type()) {
98 case BL::Lamp::type_POINT: {
99 BL::PointLamp b_point_lamp(b_lamp);
100 light->size = b_point_lamp.shadow_soft_size();
101 light->type = LIGHT_POINT;
104 case BL::Lamp::type_SPOT: {
105 BL::SpotLamp b_spot_lamp(b_lamp);
106 light->size = b_spot_lamp.shadow_soft_size();
107 light->type = LIGHT_POINT;
110 case BL::Lamp::type_HEMI: {
111 light->type = LIGHT_DISTANT;
115 case BL::Lamp::type_SUN: {
116 BL::SunLamp b_sun_lamp(b_lamp);
117 light->size = b_sun_lamp.shadow_soft_size();
118 light->type = LIGHT_DISTANT;
121 case BL::Lamp::type_AREA: {
122 BL::AreaLamp b_area_lamp(b_lamp);
124 light->axisu = make_float3(tfm.x.x, tfm.y.x, tfm.z.x);
125 light->axisv = make_float3(tfm.x.y, tfm.y.y, tfm.z.y);
126 light->sizeu = b_area_lamp.size();
127 if(b_area_lamp.shape() == BL::AreaLamp::shape_RECTANGLE)
128 light->sizev = b_area_lamp.size_y();
130 light->sizev = light->sizeu;
131 light->type = LIGHT_AREA;
136 /* location and (inverted!) direction */
137 light->co = make_float3(tfm.x.w, tfm.y.w, tfm.z.w);
138 light->dir = -make_float3(tfm.x.z, tfm.y.z, tfm.z.z);
141 vector<uint> used_shaders;
143 find_shader(b_lamp, used_shaders, scene->default_light);
145 if(used_shaders.size() == 0)
146 used_shaders.push_back(scene->default_light);
148 light->shader = used_shaders[0];
151 PointerRNA clamp = RNA_pointer_get(&b_lamp.ptr, "cycles");
152 light->cast_shadow = get_boolean(clamp, "cast_shadow");
155 light->tag_update(scene);
158 void BlenderSync::sync_background_light()
160 BL::World b_world = b_scene.world();
163 PointerRNA cworld = RNA_pointer_get(&b_world.ptr, "cycles");
164 bool sample_as_light = get_boolean(cworld, "sample_as_light");
166 if(sample_as_light) {
167 /* test if we need to sync */
169 ObjectKey key(b_world, 0, b_world);
171 if(light_map.sync(&light, b_world, b_world, key) ||
173 b_world.ptr.data != world_map)
175 light->type = LIGHT_BACKGROUND;
176 light->map_resolution = get_int(cworld, "sample_map_resolution");
177 light->shader = scene->default_background;
179 light->tag_update(scene);
180 light_map.set_recalc(b_world);
185 world_map = b_world.ptr.data;
186 world_recalc = false;
191 void BlenderSync::sync_object(BL::Object b_parent, int b_index, BL::Object b_ob, Transform& tfm, uint layer_flag)
193 /* light is handled separately */
194 if(object_is_light(b_ob)) {
195 sync_light(b_parent, b_index, b_ob, tfm);
199 /* only interested in object that we can create meshes from */
200 if(!object_is_mesh(b_ob))
203 /* test if we need to sync */
204 ObjectKey key(b_parent, b_index, b_ob);
206 bool object_updated = false;
208 if(object_map.sync(&object, b_ob, b_parent, key))
209 object_updated = true;
212 bool holdout = (layer_flag & render_layer.holdout_layer) != 0;
215 object->mesh = sync_mesh(b_ob, holdout, object_updated);
218 if(object_updated || (object->mesh && object->mesh->need_update)) {
219 object->name = b_ob.name().c_str();
220 object->pass_id = b_ob.pass_index();
223 /* visibility flags for both parent */
224 object->visibility = object_ray_visibility(b_ob) & PATH_RAY_ALL;
225 if(b_parent.ptr.data != b_ob.ptr.data)
226 object->visibility &= object_ray_visibility(b_parent);
228 /* camera flag is not actually used, instead is tested
229 against render layer flags */
230 if(object->visibility & PATH_RAY_CAMERA) {
231 object->visibility |= layer_flag << PATH_RAY_LAYER_SHIFT;
232 object->visibility &= ~PATH_RAY_CAMERA;
235 object->tag_update(scene);
241 void BlenderSync::sync_objects(BL::SpaceView3D b_v3d)
244 uint scene_layer = render_layer.scene_layer;
246 /* prepare for sync */
247 light_map.pre_sync();
249 object_map.pre_sync();
253 BL::Scene::objects_iterator b_ob;
255 for(b_scene.objects.begin(b_ob); b_ob != b_scene.objects.end(); ++b_ob) {
256 bool hide = (b_v3d)? b_ob->hide(): b_ob->hide_render();
257 uint ob_layer = get_layer(b_ob->layers());
259 if(!hide && (ob_layer & scene_layer)) {
260 if(b_ob->is_duplicator()) {
262 object_create_duplilist(*b_ob, b_scene);
264 BL::Object::dupli_list_iterator b_dup;
267 for(b_ob->dupli_list.begin(b_dup); b_dup != b_ob->dupli_list.end(); ++b_dup) {
268 Transform tfm = get_transform(b_dup->matrix());
269 BL::Object b_dup_ob = b_dup->object();
270 bool dup_hide = (b_v3d)? b_dup_ob.hide(): b_dup_ob.hide_render();
272 if(!(b_dup->hide() || dup_hide))
273 sync_object(*b_ob, b_index, b_dup_ob, tfm, ob_layer);
278 object_free_duplilist(*b_ob);
283 /* check if we should render or hide particle emitter */
284 BL::Object::particle_systems_iterator b_psys;
285 bool render_emitter = false;
287 for(b_ob->particle_systems.begin(b_psys); b_psys != b_ob->particle_systems.end(); ++b_psys) {
288 if(b_psys->settings().use_render_emitter()) {
290 render_emitter = true;
292 else if(!render_emitter)
298 Transform tfm = get_transform(b_ob->matrix_world());
299 sync_object(*b_ob, 0, *b_ob, tfm, ob_layer);
304 sync_background_light();
306 /* handle removed data and modified pointers */
307 if(light_map.post_sync())
308 scene->light_manager->tag_update(scene);
309 if(mesh_map.post_sync())
310 scene->mesh_manager->tag_update(scene);
311 if(object_map.post_sync())
312 scene->object_manager->tag_update(scene);