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 "render/mesh.h"
18 #include "render/object.h"
19 #include "render/particles.h"
20 
21 #include "blender/blender_sync.h"
22 #include "blender/blender_util.h"
23 
24 #include "util/util_foreach.h"
25 
26 CCL_NAMESPACE_BEGIN
27 
28 /* Utilities */
29 
sync_dupli_particle(BL::Object & b_ob,BL::DepsgraphObjectInstance & b_instance,Object * object)30 bool BlenderSync::sync_dupli_particle(BL::Object &b_ob,
31                                       BL::DepsgraphObjectInstance &b_instance,
32                                       Object *object)
33 {
34   /* test if this dupli was generated from a particle sytem */
35   BL::ParticleSystem b_psys = b_instance.particle_system();
36   if (!b_psys)
37     return false;
38 
39   object->hide_on_missing_motion = true;
40 
41   /* test if we need particle data */
42   if (!object->geometry->need_attribute(scene, ATTR_STD_PARTICLE))
43     return false;
44 
45   /* don't handle child particles yet */
46   BL::Array<int, OBJECT_PERSISTENT_ID_SIZE> persistent_id = b_instance.persistent_id();
47 
48   if (persistent_id[0] >= b_psys.particles.length())
49     return false;
50 
51   /* find particle system */
52   ParticleSystemKey key(b_ob, persistent_id);
53   ParticleSystem *psys;
54 
55   bool first_use = !particle_system_map.is_used(key);
56   bool need_update = particle_system_map.add_or_update(&psys, b_ob, b_instance.object(), key);
57 
58   /* no update needed? */
59   if (!need_update && !object->geometry->need_update && !scene->object_manager->need_update)
60     return true;
61 
62   /* first time used in this sync loop? clear and tag update */
63   if (first_use) {
64     psys->particles.clear();
65     psys->tag_update(scene);
66   }
67 
68   /* add particle */
69   BL::Particle b_pa = b_psys.particles[persistent_id[0]];
70   Particle pa;
71 
72   pa.index = persistent_id[0];
73   pa.age = b_scene.frame_current() - b_pa.birth_time();
74   pa.lifetime = b_pa.lifetime();
75   pa.location = get_float3(b_pa.location());
76   pa.rotation = get_float4(b_pa.rotation());
77   pa.size = b_pa.size();
78   pa.velocity = get_float3(b_pa.velocity());
79   pa.angular_velocity = get_float3(b_pa.angular_velocity());
80 
81   psys->particles.push_back_slow(pa);
82 
83   if (object->particle_index != psys->particles.size() - 1)
84     scene->object_manager->tag_update(scene);
85   object->particle_system = psys;
86   object->particle_index = psys->particles.size() - 1;
87 
88   /* return that this object has particle data */
89   return true;
90 }
91 
92 CCL_NAMESPACE_END
93