2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
18 * The Original Code is Copyright (C) 2007 by Janne Karhu.
19 * All rights reserved.
21 * The Original Code is: all of this file.
25 * Copyright 2011-2012 AutoCRC
27 * ***** END GPL LICENSE BLOCK *****
30 #ifndef __BKE_PARTICLE_H__
31 #define __BKE_PARTICLE_H__
33 /** \file BKE_particle.h
37 #include "DNA_particle_types.h"
38 #include "DNA_object_types.h"
40 struct ParticleSystemModifierData;
41 struct ParticleSystem;
43 struct ParticleSettings;
60 struct SurfaceModifierData;
65 #define PARTICLE_P ParticleData * pa; int p
66 #define LOOP_PARTICLES for (p = 0, pa = psys->particles; p < psys->totpart; p++, pa++)
67 #define LOOP_EXISTING_PARTICLES for (p = 0, pa = psys->particles; p < psys->totpart; p++, pa++) if (!(pa->flag & PARS_UNEXIST))
68 #define LOOP_SHOWN_PARTICLES for (p = 0, pa = psys->particles; p < psys->totpart; p++, pa++) if (!(pa->flag & (PARS_UNEXIST | PARS_NO_DISP)))
69 /* OpenMP: Can only advance one variable within loop definition. */
70 #define LOOP_DYNAMIC_PARTICLES for (p = 0; p < psys->totpart; p++) if ((pa = psys->particles + p)->state.time > 0.0f)
72 #define PSYS_FRAND_COUNT 1024
73 #define PSYS_FRAND(seed) psys->frand[(seed) % PSYS_FRAND_COUNT]
75 /* fast but sure way to get the modifier*/
76 #define PARTICLE_PSMD ParticleSystemModifierData * psmd = sim->psmd ? sim->psmd : psys_get_modifier(sim->ob, sim->psys)
78 /* common stuff that many particle functions need */
79 typedef struct ParticleSimulationData {
82 struct ParticleSystem *psys;
83 struct ParticleSystemModifierData *psmd;
84 struct ListBase *colliders;
85 /* Courant number. This is used to implement an adaptive time step. Only the
86 * maximum value per time step is important. Only sph_integrate makes use of
87 * this at the moment. Other solvers could, too. */
89 } ParticleSimulationData;
91 typedef struct SPHData {
92 ParticleSystem *psys[10];
98 /* Average distance to neighbours (other particles in the support domain),
99 for calculating the Courant number (adaptive time step). */
104 /* Integrator callbacks. This allows different SPH implementations. */
105 void (*force_cb) (void *sphdata_v, ParticleKey *state, float *force, float *impulse);
106 void (*density_cb) (void *rangedata_v, int index, float squared_dist);
109 typedef struct ParticleTexture {
110 float ivel; /* used in reset */
111 float time, life, exist, size; /* used in init */
112 float damp, gravity, field; /* used in physics */
113 float length, clump, kink, effector; /* used in path caching */
114 float rough1, rough2, roughe; /* used in path caching */
117 typedef struct ParticleSeam {
119 float nor[3], dir[3], tan[3];
123 typedef struct ParticleCacheKey {
132 typedef struct ParticleThreadContext {
134 struct ParticleSimulationData sim;
135 struct DerivedMesh *dm;
141 struct ParticleSeam *seams;
144 float *jit, *jitoff, *weight;
146 int *index, *skip, jitlevel;
148 int from, cfrom, distr;
150 struct ParticleData *tpars;
153 int editupdate, between, steps;
154 int totchild, totparent, parent_pass;
158 float *vg_length, *vg_clump, *vg_kink;
159 float *vg_rough1, *vg_rough2, *vg_roughe;
161 } ParticleThreadContext;
163 typedef struct ParticleThread {
164 ParticleThreadContext *ctx;
165 struct RNG *rng, *rng_path;
169 typedef struct ParticleBillboardData {
171 float vec[3], vel[3];
174 float tilt, random, time;
179 short align, uv_split, anim, split_offset;
180 } ParticleBillboardData;
182 typedef struct ParticleCollisionElement {
183 /* pointers to original data */
186 /* values interpolated from original data*/
187 float x0[3], x1[3], x2[3], p[3];
189 /* results for found intersection point */
190 float nor[3], vel[3], uv[2];
192 /* count of original data (1-4) */
195 /* index of the collision face */
198 /* flags for inversed normal / particle already inside element at start */
199 short inv_nor, inside;
200 } ParticleCollisionElement;
202 /* container for moving data between deflet_particle and particle_intersect_face */
203 typedef struct ParticleCollision {
204 struct Object *current;
208 struct Object *emitter;
210 struct CollisionModifierData *md; // collision modifier for current object;
212 float f; // time factor of previous collision, needed for substracting face velocity
215 float cfra, old_cfra;
217 float original_ray_length; //original length of co2-co1, needed for collision time evaluation
221 ParticleCollisionElement pce;
223 float total_time, inv_timestep;
226 float co1[3], co2[3];
227 float ve1[3], ve2[3];
229 float acc[3], boid_z;
234 typedef struct ParticleDrawData {
235 float *vdata, *vd; /* vertice data */
236 float *ndata, *nd; /* normal data */
237 float *cdata, *cd; /* color data */
238 float *vedata, *ved; /* velocity data */
240 int tot_vec_size, flag;
244 #define PARTICLE_DRAW_DATA_UPDATED 1
246 /* ----------- functions needed outside particlesystem ---------------- */
248 int count_particles(struct ParticleSystem *psys);
249 int count_particles_mod(struct ParticleSystem *psys, int totgr, int cur);
251 struct ParticleSystem *psys_get_current(struct Object *ob);
253 short psys_get_current_num(struct Object *ob);
254 void psys_set_current_num(Object *ob, int index);
255 struct Object *psys_find_object(struct Scene *scene, struct ParticleSystem *psys);
257 struct Object *psys_get_lattice(struct ParticleSimulationData *sim);
259 int psys_in_edit_mode(struct Scene *scene, struct ParticleSystem *psys);
260 int psys_check_enabled(struct Object *ob, struct ParticleSystem *psys);
261 int psys_check_edited(struct ParticleSystem *psys);
263 void psys_check_group_weights(struct ParticleSettings *part);
264 int psys_uses_gravity(struct ParticleSimulationData *sim);
267 void BKE_particlesettings_free(struct ParticleSettings *part);
268 void psys_free_path_cache(struct ParticleSystem *psys, struct PTCacheEdit *edit);
269 void psys_free(struct Object *ob, struct ParticleSystem *psys);
271 void psys_render_set(struct Object *ob, struct ParticleSystem *psys, float viewmat[4][4], float winmat[4][4], int winx, int winy, int timeoffset);
272 void psys_render_restore(struct Object *ob, struct ParticleSystem *psys);
273 int psys_render_simplify_distribution(struct ParticleThreadContext *ctx, int tot);
274 int psys_render_simplify_params(struct ParticleSystem *psys, struct ChildParticle *cpa, float *params);
276 void psys_interpolate_uvs(const struct MTFace *tface, int quad, const float w[4], float uvco[2]);
277 void psys_interpolate_mcol(const struct MCol *mcol, int quad, const float w[4], struct MCol *mc);
279 void copy_particle_key(struct ParticleKey *to, struct ParticleKey *from, int time);
281 void psys_particle_on_emitter(struct ParticleSystemModifierData *psmd, int distr, int index, int index_dmcache,
282 float fuv[4], float foffset, float vec[3], float nor[3],
283 float utan[3], float vtan[3], float orco[3], float ornor[3]);
284 struct ParticleSystemModifierData *psys_get_modifier(struct Object *ob, struct ParticleSystem *psys);
286 struct ModifierData *object_add_particle_system(struct Scene *scene, struct Object *ob, const char *name);
287 void object_remove_particle_system(struct Scene *scene, struct Object *ob);
288 struct ParticleSettings *psys_new_settings(const char *name, struct Main *main);
289 struct ParticleSettings *BKE_particlesettings_copy(struct ParticleSettings *part);
290 void BKE_particlesettings_make_local(struct ParticleSettings *part);
292 void psys_reset(struct ParticleSystem *psys, int mode);
294 void psys_find_parents(struct ParticleSimulationData *sim);
296 void psys_cache_paths(struct ParticleSimulationData *sim, float cfra);
297 void psys_cache_edit_paths(struct Scene *scene, struct Object *ob, struct PTCacheEdit *edit, float cfra);
298 void psys_cache_child_paths(struct ParticleSimulationData *sim, float cfra, int editupdate);
299 int do_guides(struct ListBase *effectors, ParticleKey *state, int pa_num, float time);
300 void precalc_guides(struct ParticleSimulationData *sim, struct ListBase *effectors);
301 float psys_get_timestep(struct ParticleSimulationData *sim);
302 float psys_get_child_time(struct ParticleSystem *psys, struct ChildParticle *cpa, float cfra, float *birthtime, float *dietime);
303 float psys_get_child_size(struct ParticleSystem *psys, struct ChildParticle *cpa, float cfra, float *pa_time);
304 void psys_get_particle_on_path(struct ParticleSimulationData *sim, int pa_num, struct ParticleKey *state, int vel);
305 int psys_get_particle_state(struct ParticleSimulationData *sim, int p, struct ParticleKey *state, int always);
307 void psys_sph_init(struct ParticleSimulationData *sim, struct SPHData *sphdata);
308 void psys_sph_finalise(struct SPHData *sphdata);
309 void psys_sph_density(struct BVHTree *tree, struct SPHData* data, float co[3], float vars[2]);
312 void psys_get_dupli_texture(struct ParticleSystem *psys, struct ParticleSettings *part,
313 struct ParticleSystemModifierData *psmd, struct ParticleData *pa, struct ChildParticle *cpa,
314 float uv[2], float orco[3]);
315 void psys_get_dupli_path_transform(struct ParticleSimulationData *sim, struct ParticleData *pa, struct ChildParticle *cpa,
316 struct ParticleCacheKey *cache, float mat[4][4], float *scale);
318 ParticleThread *psys_threads_create(struct ParticleSimulationData *sim);
319 void psys_threads_free(ParticleThread *threads);
321 void psys_make_billboard(ParticleBillboardData * bb, float xvec[3], float yvec[3], float zvec[3], float center[3]);
322 void psys_apply_hair_lattice(struct Scene *scene, struct Object *ob, struct ParticleSystem *psys);
324 /* particle_system.c */
325 struct ParticleSystem *psys_get_target_system(struct Object *ob, struct ParticleTarget *pt);
326 void psys_count_keyed_targets(struct ParticleSimulationData *sim);
327 void psys_update_particle_tree(struct ParticleSystem *psys, float cfra);
329 void psys_make_temp_pointcache(struct Object *ob, struct ParticleSystem *psys);
330 void psys_get_pointcache_start_end(struct Scene *scene, ParticleSystem *psys, int *sfra, int *efra);
332 void psys_check_boid_data(struct ParticleSystem *psys);
334 void psys_get_birth_coordinates(struct ParticleSimulationData *sim, struct ParticleData *pa, struct ParticleKey *state, float dtime, float cfra);
336 void particle_system_update(struct Scene *scene, struct Object *ob, struct ParticleSystem *psys);
338 /* ----------- functions needed only inside particlesystem ------------ */
340 void psys_disable_all(struct Object *ob);
341 void psys_enable_all(struct Object *ob);
343 void free_hair(struct Object *ob, struct ParticleSystem *psys, int dynamics);
344 void free_keyed_keys(struct ParticleSystem *psys);
345 void psys_free_particles(struct ParticleSystem *psys);
346 void psys_free_children(struct ParticleSystem *psys);
348 void psys_interpolate_particle(short type, struct ParticleKey keys[4], float dt, struct ParticleKey *result, int velocity);
349 void psys_vec_rot_to_face(struct DerivedMesh *dm, struct ParticleData *pa, float vec[3]);
350 void psys_mat_hair_to_object(struct Object *ob, struct DerivedMesh *dm, short from, struct ParticleData *pa, float hairmat[4][4]);
351 void psys_mat_hair_to_global(struct Object *ob, struct DerivedMesh *dm, short from, struct ParticleData *pa, float hairmat[4][4]);
352 void psys_mat_hair_to_orco(struct Object *ob, struct DerivedMesh *dm, short from, struct ParticleData *pa, float hairmat[4][4]);
354 float psys_get_dietime_from_cache(struct PointCache *cache, int index);
356 void psys_free_pdd(struct ParticleSystem *psys);
358 float *psys_cache_vgroup(struct DerivedMesh *dm, struct ParticleSystem *psys, int vgroup);
359 void psys_get_texture(struct ParticleSimulationData *sim, struct ParticleData *pa, struct ParticleTexture *ptex, int event, float cfra);
360 void psys_interpolate_face(struct MVert *mvert, struct MFace *mface, struct MTFace *tface,
361 float (*orcodata)[3], float w[4], float vec[3], float nor[3], float utan[3], float vtan[3],
362 float orco[3], float ornor[3]);
363 float psys_particle_value_from_verts(struct DerivedMesh *dm, short from, struct ParticleData *pa, float *values);
364 void psys_get_from_key(struct ParticleKey *key, float loc[3], float vel[3], float rot[4], float *time);
366 /* BLI_bvhtree_ray_cast callback */
367 void BKE_psys_collision_neartest_cb(void *userdata, int index, const struct BVHTreeRay *ray, struct BVHTreeRayHit *hit);
368 void psys_particle_on_dm(struct DerivedMesh *dm, int from, int index, int index_dmcache,
369 const float fw[4], float foffset, float vec[3], float nor[3], float utan[3], float vtan[3],
370 float orco[3], float ornor[3]);
372 /* particle_system.c */
373 void initialize_particle(struct ParticleSimulationData *sim, struct ParticleData *pa, int p);
374 void psys_calc_dmcache(struct Object *ob, struct DerivedMesh *dm, struct ParticleSystem *psys);
375 int psys_particle_dm_face_lookup(struct Object *ob, struct DerivedMesh *dm, int index, const float fw[4], struct LinkNode *node);
377 void reset_particle(struct ParticleSimulationData *sim, struct ParticleData *pa, float dtime, float cfra);
380 #define PSYS_RESET_ALL 1
381 #define PSYS_RESET_DEPSGRAPH 2
382 /* #define PSYS_RESET_CHILDREN 3 */ /*UNUSED*/
383 #define PSYS_RESET_CACHE_MISS 4
386 #define DMCACHE_NOTFOUND -1
387 #define DMCACHE_ISCHILD -2