Cycles: support for mask layers in render layer, this has the same effect as
[blender.git] / intern / cycles / blender / blender_object.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 "graph.h"
20 #include "light.h"
21 #include "mesh.h"
22 #include "object.h"
23 #include "scene.h"
24 #include "nodes.h"
25 #include "shader.h"
26
27 #include "blender_sync.h"
28 #include "blender_util.h"
29
30 #include "util_foreach.h"
31
32 CCL_NAMESPACE_BEGIN
33
34 /* Utilities */
35
36 bool BlenderSync::object_is_modified(BL::Object b_ob)
37 {
38         /* test if we can instance or if the object is modified */
39         if(ccl::object_is_modified(b_ob, b_scene, preview)) {
40                 /* modifiers */
41                 return true;
42         }
43         else {
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)
48                                 return true;
49         }
50
51         return false;
52 }
53
54 bool BlenderSync::object_is_mesh(BL::Object b_ob)
55 {
56         BL::ID b_ob_data = b_ob.data();
57
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)));
60 }
61
62 bool BlenderSync::object_is_light(BL::Object b_ob)
63 {
64         BL::ID b_ob_data = b_ob.data();
65
66         return (b_ob_data && b_ob_data.is_a(&RNA_Lamp));
67 }
68
69 static uint object_ray_visibility(BL::Object b_ob)
70 {
71         PointerRNA cvisibility = RNA_pointer_get(&b_ob.ptr, "cycles_visibility");
72         uint flag = 0;
73
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;
79
80         return flag;
81 }
82
83 /* Light */
84
85 void BlenderSync::sync_light(BL::Object b_parent, int b_index, BL::Object b_ob, Transform& tfm)
86 {
87         /* test if we need to sync */
88         Light *light;
89         ObjectKey key(b_parent, b_index, b_ob);
90
91         if(!light_map.sync(&light, b_ob, b_parent, key))
92                 return;
93         
94         BL::Lamp b_lamp(b_ob.data());
95
96         /* type */
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;
102                         break;
103                 }
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;
108                         break;
109                 }
110                 case BL::Lamp::type_HEMI: {
111                         light->type = LIGHT_DISTANT;
112                         light->size = 0.0f;
113                         break;
114                 }
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;
119                         break;
120                 }
121                 case BL::Lamp::type_AREA: {
122                         BL::AreaLamp b_area_lamp(b_lamp);
123                         light->size = 1.0f;
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();
129                         else
130                                 light->sizev = light->sizeu;
131                         light->type = LIGHT_AREA;
132                         break;
133                 }
134         }
135
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);
139
140         /* shader */
141         vector<uint> used_shaders;
142
143         find_shader(b_lamp, used_shaders, scene->default_light);
144
145         if(used_shaders.size() == 0)
146                 used_shaders.push_back(scene->default_light);
147
148         light->shader = used_shaders[0];
149
150         /* shadow */
151         PointerRNA clamp = RNA_pointer_get(&b_lamp.ptr, "cycles");
152         light->cast_shadow = get_boolean(clamp, "cast_shadow");
153
154         /* tag */
155         light->tag_update(scene);
156 }
157
158 void BlenderSync::sync_background_light()
159 {
160         BL::World b_world = b_scene.world();
161
162         if(b_world) {
163                 PointerRNA cworld = RNA_pointer_get(&b_world.ptr, "cycles");
164                 bool sample_as_light = get_boolean(cworld, "sample_as_light");
165
166                 if(sample_as_light) {
167                         /* test if we need to sync */
168                         Light *light;
169                         ObjectKey key(b_world, 0, b_world);
170
171                         if(light_map.sync(&light, b_world, b_world, key) ||
172                            world_recalc ||
173                            b_world.ptr.data != world_map)
174                         {
175                                 light->type = LIGHT_BACKGROUND;
176                                 light->map_resolution  = get_int(cworld, "sample_map_resolution");
177                                 light->shader = scene->default_background;
178
179                                 light->tag_update(scene);
180                                 light_map.set_recalc(b_world);
181                         }
182                 }
183         }
184
185         world_map = b_world.ptr.data;
186         world_recalc = false;
187 }
188
189 /* Object */
190
191 void BlenderSync::sync_object(BL::Object b_parent, int b_index, BL::Object b_ob, Transform& tfm, uint layer_flag)
192 {
193         /* light is handled separately */
194         if(object_is_light(b_ob)) {
195                 sync_light(b_parent, b_index, b_ob, tfm);
196                 return;
197         }
198
199         /* only interested in object that we can create meshes from */
200         if(!object_is_mesh(b_ob))
201                 return;
202
203         /* test if we need to sync */
204         ObjectKey key(b_parent, b_index, b_ob);
205         Object *object;
206         bool object_updated = false;
207
208         if(object_map.sync(&object, b_ob, b_parent, key))
209                 object_updated = true;
210         
211         /* holdout? */
212         bool holdout = (layer_flag & render_layer.holdout_layer) != 0;
213
214         /* mesh sync */
215         object->mesh = sync_mesh(b_ob, holdout, object_updated);
216
217         /* object sync */
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();
221                 object->tfm = tfm;
222
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);
227
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;
233                 }
234
235                 object->tag_update(scene);
236         }
237 }
238
239 /* Object Loop */
240
241 void BlenderSync::sync_objects(BL::SpaceView3D b_v3d)
242 {
243         /* layer data */
244         uint scene_layer = render_layer.scene_layer;
245         
246         /* prepare for sync */
247         light_map.pre_sync();
248         mesh_map.pre_sync();
249         object_map.pre_sync();
250         mesh_synced.clear();
251
252         /* object loop */
253         BL::Scene::objects_iterator b_ob;
254
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());
258
259                 if(!hide && (ob_layer & scene_layer)) {
260                         if(b_ob->is_duplicator()) {
261                                 /* dupli objects */
262                                 object_create_duplilist(*b_ob, b_scene);
263
264                                 BL::Object::dupli_list_iterator b_dup;
265                                 int b_index = 0;
266
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();
271
272                                         if(!(b_dup->hide() || dup_hide))
273                                                 sync_object(*b_ob, b_index, b_dup_ob, tfm, ob_layer);
274
275                                         b_index++;
276                                 }
277
278                                 object_free_duplilist(*b_ob);
279
280                                 hide = true;
281                         }
282
283                         /* check if we should render or hide particle emitter */
284                         BL::Object::particle_systems_iterator b_psys;
285                         bool render_emitter = false;
286
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()) {
289                                         hide = false;
290                                         render_emitter = true;
291                                 }
292                                 else if(!render_emitter)
293                                         hide = true;
294                         }
295
296                         if(!hide) {
297                                 /* object itself */
298                                 Transform tfm = get_transform(b_ob->matrix_world());
299                                 sync_object(*b_ob, 0, *b_ob, tfm, ob_layer);
300                         }
301                 }
302         }
303
304         sync_background_light();
305
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);
313         mesh_synced.clear();
314 }
315
316 CCL_NAMESPACE_END
317