Cycles: Start the particle index for object instances at 1 instead 0. The first parti...
[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 "camera.h"
20 #include "graph.h"
21 #include "light.h"
22 #include "mesh.h"
23 #include "object.h"
24 #include "scene.h"
25 #include "nodes.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
34 CCL_NAMESPACE_BEGIN
35
36 /* Utilities */
37
38 bool BlenderSync::BKE_object_is_modified(BL::Object b_ob)
39 {
40         /* test if we can instance or if the object is modified */
41         if(ccl::BKE_object_is_modified(b_ob, b_scene, preview)) {
42                 /* modifiers */
43                 return true;
44         }
45         else {
46                 /* object level material links */
47                 BL::Object::material_slots_iterator slot;
48                 for(b_ob.material_slots.begin(slot); slot != b_ob.material_slots.end(); ++slot)
49                         if(slot->link() == BL::MaterialSlot::link_OBJECT)
50                                 return true;
51         }
52
53         return false;
54 }
55
56 bool BlenderSync::object_is_mesh(BL::Object b_ob)
57 {
58         BL::ID b_ob_data = b_ob.data();
59
60         return (b_ob_data && (b_ob_data.is_a(&RNA_Mesh) ||
61                 b_ob_data.is_a(&RNA_Curve) || b_ob_data.is_a(&RNA_MetaBall)));
62 }
63
64 bool BlenderSync::object_is_light(BL::Object b_ob)
65 {
66         BL::ID b_ob_data = b_ob.data();
67
68         return (b_ob_data && b_ob_data.is_a(&RNA_Lamp));
69 }
70
71 static uint object_ray_visibility(BL::Object b_ob)
72 {
73         PointerRNA cvisibility = RNA_pointer_get(&b_ob.ptr, "cycles_visibility");
74         uint flag = 0;
75
76         flag |= get_boolean(cvisibility, "camera")? PATH_RAY_CAMERA: 0;
77         flag |= get_boolean(cvisibility, "diffuse")? PATH_RAY_DIFFUSE: 0;
78         flag |= get_boolean(cvisibility, "glossy")? PATH_RAY_GLOSSY: 0;
79         flag |= get_boolean(cvisibility, "transmission")? PATH_RAY_TRANSMIT: 0;
80         flag |= get_boolean(cvisibility, "shadow")? PATH_RAY_SHADOW: 0;
81
82         return flag;
83 }
84
85 /* Light */
86
87 void BlenderSync::sync_light(BL::Object b_parent, int b_index, BL::Object b_ob, Transform& tfm)
88 {
89         /* test if we need to sync */
90         Light *light;
91         ObjectKey key(b_parent, b_index, b_ob);
92
93         if(!light_map.sync(&light, b_ob, b_parent, key))
94                 return;
95         
96         BL::Lamp b_lamp(b_ob.data());
97
98         /* type */
99         switch(b_lamp.type()) {
100                 case BL::Lamp::type_POINT: {
101                         BL::PointLamp b_point_lamp(b_lamp);
102                         light->size = b_point_lamp.shadow_soft_size();
103                         light->type = LIGHT_POINT;
104                         break;
105                 }
106                 case BL::Lamp::type_SPOT: {
107                         BL::SpotLamp b_spot_lamp(b_lamp);
108                         light->size = b_spot_lamp.shadow_soft_size();
109                         light->type = LIGHT_SPOT;
110                         light->spot_angle = b_spot_lamp.spot_size();
111                         light->spot_smooth = b_spot_lamp.spot_blend();
112                         break;
113                 }
114                 case BL::Lamp::type_HEMI: {
115                         light->type = LIGHT_DISTANT;
116                         light->size = 0.0f;
117                         break;
118                 }
119                 case BL::Lamp::type_SUN: {
120                         BL::SunLamp b_sun_lamp(b_lamp);
121                         light->size = b_sun_lamp.shadow_soft_size();
122                         light->type = LIGHT_DISTANT;
123                         break;
124                 }
125                 case BL::Lamp::type_AREA: {
126                         BL::AreaLamp b_area_lamp(b_lamp);
127                         light->size = 1.0f;
128                         light->axisu = make_float3(tfm.x.x, tfm.y.x, tfm.z.x);
129                         light->axisv = make_float3(tfm.x.y, tfm.y.y, tfm.z.y);
130                         light->sizeu = b_area_lamp.size();
131                         if(b_area_lamp.shape() == BL::AreaLamp::shape_RECTANGLE)
132                                 light->sizev = b_area_lamp.size_y();
133                         else
134                                 light->sizev = light->sizeu;
135                         light->type = LIGHT_AREA;
136                         break;
137                 }
138         }
139
140         /* location and (inverted!) direction */
141         light->co = make_float3(tfm.x.w, tfm.y.w, tfm.z.w);
142         light->dir = -make_float3(tfm.x.z, tfm.y.z, tfm.z.z);
143
144         /* shader */
145         vector<uint> used_shaders;
146
147         find_shader(b_lamp, used_shaders, scene->default_light);
148
149         if(used_shaders.size() == 0)
150                 used_shaders.push_back(scene->default_light);
151
152         light->shader = used_shaders[0];
153
154         /* shadow */
155         PointerRNA clamp = RNA_pointer_get(&b_lamp.ptr, "cycles");
156         light->cast_shadow = get_boolean(clamp, "cast_shadow");
157         light->samples = get_int(clamp, "samples");
158
159         /* tag */
160         light->tag_update(scene);
161 }
162
163 void BlenderSync::sync_background_light()
164 {
165         BL::World b_world = b_scene.world();
166
167         if(b_world) {
168                 PointerRNA cworld = RNA_pointer_get(&b_world.ptr, "cycles");
169                 bool sample_as_light = get_boolean(cworld, "sample_as_light");
170
171                 if(sample_as_light) {
172                         /* test if we need to sync */
173                         Light *light;
174                         ObjectKey key(b_world, 0, b_world);
175
176                         if(light_map.sync(&light, b_world, b_world, key) ||
177                            world_recalc ||
178                            b_world.ptr.data != world_map)
179                         {
180                                 light->type = LIGHT_BACKGROUND;
181                                 light->map_resolution  = get_int(cworld, "sample_map_resolution");
182                                 light->samples  = get_int(cworld, "samples");
183                                 light->shader = scene->default_background;
184
185                                 light->tag_update(scene);
186                                 light_map.set_recalc(b_world);
187                         }
188                 }
189         }
190
191         world_map = b_world.ptr.data;
192         world_recalc = false;
193 }
194
195 /* Object */
196
197 void BlenderSync::sync_object(BL::Object b_parent, int b_index, BL::Object b_ob, Transform& tfm, uint layer_flag, int motion, int particle_id)
198 {
199         /* light is handled separately */
200         if(object_is_light(b_ob)) {
201                 if(!motion)
202                         sync_light(b_parent, b_index, b_ob, tfm);
203                 return;
204         }
205
206         /* only interested in object that we can create meshes from */
207         if(!object_is_mesh(b_ob))
208                 return;
209
210         /* key to lookup object */
211         ObjectKey key(b_parent, b_index, b_ob);
212         Object *object;
213
214         /* motion vector case */
215         if(motion) {
216                 object = object_map.find(key);
217
218                 if(object) {
219                         if(tfm != object->tfm) {
220                                 if(motion == -1)
221                                         object->motion.pre = tfm;
222                                 else
223                                         object->motion.post = tfm;
224
225                                 object->use_motion = true;
226                         }
227
228                         sync_mesh_motion(b_ob, object->mesh, motion);
229                 }
230
231                 return;
232         }
233
234         /* test if we need to sync */
235         bool object_updated = false;
236
237         if(object_map.sync(&object, b_ob, b_parent, key))
238                 object_updated = true;
239         
240         bool use_holdout = (layer_flag & render_layer.holdout_layer) != 0;
241         
242         /* mesh sync */
243         object->mesh = sync_mesh(b_ob, object_updated);
244
245         if(use_holdout != object->use_holdout) {
246                 object->use_holdout = use_holdout;
247                 scene->object_manager->tag_update(scene);
248         }
249
250         /* object sync */
251         if(object_updated || (object->mesh && object->mesh->need_update)) {
252                 object->name = b_ob.name().c_str();
253                 object->pass_id = b_ob.pass_index();
254                 object->tfm = tfm;
255                 object->motion.pre = tfm;
256                 object->motion.post = tfm;
257                 object->use_motion = false;
258
259                 object->random_id = hash_int_2d(hash_string(object->name.c_str()), b_index);
260
261                 /* visibility flags for both parent */
262                 object->visibility = object_ray_visibility(b_ob) & PATH_RAY_ALL;
263                 if(b_parent.ptr.data != b_ob.ptr.data) {
264                         object->visibility &= object_ray_visibility(b_parent);
265                         object->random_id ^= hash_int(hash_string(b_parent.name().c_str()));
266                 }
267
268                 /* camera flag is not actually used, instead is tested
269                  * against render layer flags */
270                 if(object->visibility & PATH_RAY_CAMERA) {
271                         object->visibility |= layer_flag << PATH_RAY_LAYER_SHIFT;
272                         object->visibility &= ~PATH_RAY_CAMERA;
273                 }
274
275                 object->particle_id = particle_id;
276
277                 object->tag_update(scene);
278         }
279 }
280
281 /* Object Loop */
282
283 void BlenderSync::sync_objects(BL::SpaceView3D b_v3d, int motion)
284 {
285         /* layer data */
286         uint scene_layer = render_layer.scene_layer;
287         
288         if(!motion) {
289                 /* prepare for sync */
290                 light_map.pre_sync();
291                 mesh_map.pre_sync();
292                 object_map.pre_sync();
293                 mesh_synced.clear();
294         }
295
296         /* object loop */
297         BL::Scene::objects_iterator b_ob;
298         BL::Scene b_sce = b_scene;
299         int particle_offset = 1;        /* first particle is dummy for regular, non-instanced objects */
300
301         for(; b_sce; b_sce = b_sce.background_set()) {
302                 for(b_sce.objects.begin(b_ob); b_ob != b_sce.objects.end(); ++b_ob) {
303                         bool hide = (render_layer.use_viewport_visibility)? b_ob->hide(): b_ob->hide_render();
304                         uint ob_layer = get_layer(b_ob->layers(), b_ob->layers_local_view(), object_is_light(*b_ob));
305                         CYCLES_LOCAL_LAYER_HACK(render_layer.use_localview, ob_layer);
306                         hide = hide || !(ob_layer & scene_layer);
307
308                         if(!hide) {
309
310                                 int num_particles = object_count_particles(*b_ob);
311
312                                 if(b_ob->is_duplicator()) {
313                                         hide = true;    /* duplicators hidden by default */
314
315                                         /* dupli objects */
316                                         object_create_duplilist(*b_ob, b_scene);
317
318                                         BL::Object::dupli_list_iterator b_dup;
319                                         int b_index = 0;
320
321                                         for(b_ob->dupli_list.begin(b_dup); b_dup != b_ob->dupli_list.end(); ++b_dup) {
322                                                 Transform tfm = get_transform(b_dup->matrix());
323                                                 BL::Object b_dup_ob = b_dup->object();
324                                                 bool dup_hide = (b_v3d)? b_dup_ob.hide(): b_dup_ob.hide_render();
325
326                                                 if(!(b_dup->hide() || dup_hide)) {
327                                                         sync_object(*b_ob, b_index, b_dup_ob, tfm, ob_layer, motion, b_dup->particle_index() + particle_offset);
328                                                 }
329                                                 
330                                                 ++b_index;
331                                         }
332
333                                         object_free_duplilist(*b_ob);
334                                 }
335
336                                 /* check if we should render or hide particle emitter */
337                                 BL::Object::particle_systems_iterator b_psys;
338                                 for(b_ob->particle_systems.begin(b_psys); b_psys != b_ob->particle_systems.end(); ++b_psys)
339                                         if(b_psys->settings().use_render_emitter())
340                                                 hide = false;
341
342                                 if(!hide) {
343                                         /* object itself */
344                                         Transform tfm = get_transform(b_ob->matrix_world());
345                                         sync_object(*b_ob, 0, *b_ob, tfm, ob_layer, motion, 0);
346                                 }
347
348                                 particle_offset += num_particles;
349                         }
350                 }
351         }
352
353         if(!motion) {
354                 sync_background_light();
355
356                 /* handle removed data and modified pointers */
357                 if(light_map.post_sync())
358                         scene->light_manager->tag_update(scene);
359                 if(mesh_map.post_sync())
360                         scene->mesh_manager->tag_update(scene);
361                 if(object_map.post_sync())
362                         scene->object_manager->tag_update(scene);
363                 mesh_synced.clear();
364         }
365 }
366
367 void BlenderSync::sync_motion(BL::SpaceView3D b_v3d, BL::Object b_override)
368 {
369         if(scene->need_motion() == Scene::MOTION_NONE)
370                 return;
371
372         /* get camera object here to deal with camera switch */
373         BL::Object b_cam = b_scene.camera();
374         if(b_override)
375                 b_cam = b_override;
376
377         /* go back and forth one frame */
378         int frame = b_scene.frame_current();
379
380         for(int motion = -1; motion <= 1; motion += 2) {
381                 scene_frame_set(b_scene, frame + motion);
382
383                 /* camera object */
384                 if(b_cam)
385                         sync_camera_motion(b_cam, motion);
386
387                 /* mesh objects */
388                 sync_objects(b_v3d, motion);
389         }
390
391         scene_frame_set(b_scene, frame);
392 }
393
394 CCL_NAMESPACE_END
395