Cycles: Render Passes
[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         /* mesh sync */
212         object->mesh = sync_mesh(b_ob, object_updated);
213
214         /* object sync */
215         if(object_updated || (object->mesh && object->mesh->need_update)) {
216                 object->name = b_ob.name().c_str();
217                 object->pass_id = b_ob.pass_index();
218                 object->tfm = tfm;
219
220                 /* visibility flags for both parent */
221                 object->visibility = object_ray_visibility(b_ob) & PATH_RAY_ALL;
222                 if(b_parent.ptr.data != b_ob.ptr.data)
223                         object->visibility &= object_ray_visibility(b_parent);
224
225                 /* camera flag is not actually used, instead is tested
226                    against render layer flags */
227                 if(object->visibility & PATH_RAY_CAMERA) {
228                         object->visibility |= layer_flag << PATH_RAY_LAYER_SHIFT;
229                         object->visibility &= ~PATH_RAY_CAMERA;
230                 }
231
232                 object->tag_update(scene);
233         }
234 }
235
236 /* Object Loop */
237
238 void BlenderSync::sync_objects(BL::SpaceView3D b_v3d)
239 {
240         /* layer data */
241         uint scene_layer = render_layers.front().scene_layer;
242         
243         /* prepare for sync */
244         light_map.pre_sync();
245         mesh_map.pre_sync();
246         object_map.pre_sync();
247         mesh_synced.clear();
248
249         /* object loop */
250         BL::Scene::objects_iterator b_ob;
251
252         for(b_scene.objects.begin(b_ob); b_ob != b_scene.objects.end(); ++b_ob) {
253                 bool hide = (b_v3d)? b_ob->hide(): b_ob->hide_render();
254                 uint ob_layer = get_layer(b_ob->layers());
255
256                 if(!hide && (ob_layer & scene_layer)) {
257                         if(b_ob->is_duplicator()) {
258                                 /* dupli objects */
259                                 object_create_duplilist(*b_ob, b_scene);
260
261                                 BL::Object::dupli_list_iterator b_dup;
262                                 int b_index = 0;
263
264                                 for(b_ob->dupli_list.begin(b_dup); b_dup != b_ob->dupli_list.end(); ++b_dup) {
265                                         Transform tfm = get_transform(b_dup->matrix());
266                                         BL::Object b_dup_ob = b_dup->object();
267                                         bool dup_hide = (b_v3d)? b_dup_ob.hide(): b_dup_ob.hide_render();
268
269                                         if(!(b_dup->hide() || dup_hide))
270                                                 sync_object(*b_ob, b_index, b_dup_ob, tfm, ob_layer);
271
272                                         b_index++;
273                                 }
274
275                                 object_free_duplilist(*b_ob);
276
277                                 hide = true;
278                         }
279
280                         /* check if we should render or hide particle emitter */
281                         BL::Object::particle_systems_iterator b_psys;
282                         bool render_emitter = false;
283
284                         for(b_ob->particle_systems.begin(b_psys); b_psys != b_ob->particle_systems.end(); ++b_psys) {
285                                 if(b_psys->settings().use_render_emitter()) {
286                                         hide = false;
287                                         render_emitter = true;
288                                 }
289                                 else if(!render_emitter)
290                                         hide = true;
291                         }
292
293                         if(!hide) {
294                                 /* object itself */
295                                 Transform tfm = get_transform(b_ob->matrix_world());
296                                 sync_object(*b_ob, 0, *b_ob, tfm, ob_layer);
297                         }
298                 }
299         }
300
301         sync_background_light();
302
303         /* handle removed data and modified pointers */
304         if(light_map.post_sync())
305                 scene->light_manager->tag_update(scene);
306         if(mesh_map.post_sync())
307                 scene->mesh_manager->tag_update(scene);
308         if(object_map.post_sync())
309                 scene->object_manager->tag_update(scene);
310         mesh_synced.clear();
311 }
312
313 CCL_NAMESPACE_END
314