c1cca4e217a7949ee44da05cc7107607d573128f
[blender.git] / source / blender / render / intern / source / pointdensity.c
1 /* 
2  * ***** BEGIN GPL LICENSE BLOCK *****
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributors: Matt Ebb
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 #include <math.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29
30 #include "MEM_guardedalloc.h"
31
32 #include "BLI_arithb.h"
33 #include "BLI_blenlib.h"
34 #include "BLI_kdopbvh.h"
35
36 #include "BKE_DerivedMesh.h"
37 #include "BKE_global.h"
38 #include "BKE_lattice.h"
39 #include "BKE_main.h"
40 #include "BKE_object.h"
41 #include "BKE_particle.h"
42 #include "BKE_texture.h"
43
44 #include "DNA_meshdata_types.h"
45 #include "DNA_texture_types.h"
46 #include "DNA_particle_types.h"
47
48 #include "render_types.h"
49 #include "renderdatabase.h"
50 #include "texture.h"
51
52 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
53 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
54 /* only to be used here in this file, it's for speed */
55 extern struct Render R;
56 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
57
58
59 static int point_data_used(PointDensity *pd)
60 {
61         int pd_bitflag = 0;
62         
63         if ((pd->noise_influence == TEX_PD_NOISE_VEL) || (pd->color_source == TEX_PD_COLOR_PARTVEL) || (pd->color_source == TEX_PD_COLOR_PARTSPEED))
64                 pd_bitflag |= POINT_DATA_VEL;
65         if ((pd->noise_influence == TEX_PD_NOISE_AGE) || (pd->color_source == TEX_PD_COLOR_PARTAGE)) 
66                 pd_bitflag |= POINT_DATA_LIFE;
67                 
68         return pd_bitflag;
69 }
70
71
72 /* additional data stored alongside the point density BVH, 
73  * accessible by point index number to retrieve other information 
74  * such as particle velocity or lifetime */
75 static void alloc_point_data(PointDensity *pd, int total_particles, int point_data_used)
76 {
77         int data_size = 0;
78         
79         if (point_data_used & POINT_DATA_VEL) {
80                 /* store 3 channels of velocity data */
81                 data_size += 3;
82         }
83         if (point_data_used & POINT_DATA_LIFE) {
84                 /* store 1 channel of lifetime data */
85                 data_size += 1;
86         }
87
88         if (data_size)
89                 pd->point_data = MEM_mallocN(sizeof(float)*data_size*total_particles, "particle point data");
90 }
91
92 static void pointdensity_cache_psys(Render *re, PointDensity *pd, Object *ob, ParticleSystem *psys)
93 {
94         DerivedMesh* dm;
95         ParticleKey state;
96         ParticleSimulationData sim = {re->scene, ob, psys, NULL};
97         ParticleData *pa=NULL;
98         float cfra = bsystem_time(re->scene, ob, (float)re->scene->r.cfra, 0.0);
99         int i, childexists;
100         int total_particles, offset=0;
101         int data_used = point_data_used(pd);
102         float partco[3];
103         float obview[4][4];
104         
105         
106         /* init everything */
107         if (!psys || !ob || !pd) return;
108         
109         Mat4MulMat4(obview, re->viewinv, ob->obmat);
110         
111         /* Just to create a valid rendering context for particles */
112         psys_render_set(ob, psys, re->viewmat, re->winmat, re->winx, re->winy, 0);
113         
114         dm = mesh_create_derived_render(re->scene, ob,CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL);
115         
116         if ( !psys_check_enabled(ob, psys)) {
117                 psys_render_restore(ob, psys);
118                 return;
119         }
120         
121         /* in case ob->imat isn't up-to-date */
122         Mat4Invert(ob->imat, ob->obmat);
123         
124         total_particles = psys->totpart+psys->totchild;
125         psys->lattice=psys_get_lattice(&sim);
126         
127         pd->point_tree = BLI_bvhtree_new(total_particles, 0.0, 4, 6);
128         alloc_point_data(pd, total_particles, data_used);
129         pd->totpoints = total_particles;
130         if (data_used & POINT_DATA_VEL) offset = pd->totpoints*3;
131         
132         if (psys->totchild > 0 && !(psys->part->draw & PART_DRAW_PARENT))
133                 childexists = 1;
134         
135         for (i=0, pa=psys->particles; i < total_particles; i++, pa++) {
136
137                 state.time = cfra;
138                 if(psys_get_particle_state(&sim, i, &state, 0)) {
139                         
140                         VECCOPY(partco, state.co);
141                         
142                         if (pd->psys_cache_space == TEX_PD_OBJECTSPACE)
143                                 Mat4MulVecfl(ob->imat, partco);
144                         else if (pd->psys_cache_space == TEX_PD_OBJECTLOC) {
145                                 float obloc[3];
146                                 VECCOPY(obloc, ob->loc);
147                                 VecSubf(partco, partco, obloc);
148                         } else {
149                                 /* TEX_PD_WORLDSPACE */
150                         }
151                         
152                         BLI_bvhtree_insert(pd->point_tree, i, partco, 1);
153                         
154                         if (data_used & POINT_DATA_VEL) {
155                                 pd->point_data[i*3 + 0] = state.vel[0];
156                                 pd->point_data[i*3 + 1] = state.vel[1];
157                                 pd->point_data[i*3 + 2] = state.vel[2];
158                         } 
159                         if (data_used & POINT_DATA_LIFE) {
160                                 float pa_time;
161                                 
162                                 if (i < psys->totpart) {
163                                         pa_time = (cfra - pa->time)/pa->lifetime;
164                                 } else {
165                                         ChildParticle *cpa= (psys->child + i) - psys->totpart;
166                                         float pa_birthtime, pa_dietime;
167                                         
168                                         pa_time = psys_get_child_time(psys, cpa, cfra, &pa_birthtime, &pa_dietime);
169                                 }
170                                 
171                                 pd->point_data[offset + i] = pa_time;
172                         }
173                 }
174         }
175         
176         BLI_bvhtree_balance(pd->point_tree);
177         dm->release(dm);
178         
179         if(psys->lattice){
180                 end_latt_deform(psys->lattice);
181                 psys->lattice=0;
182         }
183         
184         psys_render_restore(ob, psys);
185 }
186
187
188 static void pointdensity_cache_object(Render *re, PointDensity *pd, Object *ob)
189 {
190         int i;
191         DerivedMesh *dm;
192         MVert *mvert = NULL;
193         float cam_mat[4][4];
194         
195         dm = mesh_create_derived_render(re->scene, ob,  CD_MASK_BAREMESH|CD_MASK_MTFACE|CD_MASK_MCOL);
196         mvert= dm->getVertArray(dm);    /* local object space */
197         
198         pd->totpoints= dm->getNumVerts(dm);
199         if (pd->totpoints == 0) return;
200
201         pd->point_tree = BLI_bvhtree_new(pd->totpoints, 0.0, 4, 6);
202         
203         for(i=0; i < pd->totpoints; i++, mvert++) {
204                 float co[3];
205                 
206                 VECCOPY(co, mvert->co);
207
208                 switch(pd->ob_cache_space) {
209                         case TEX_PD_OBJECTSPACE:
210                                 break;
211                         case TEX_PD_OBJECTLOC:
212                                 Mat4MulVecfl(ob->obmat, co);
213                                 VecSubf(co, co, ob->loc);
214                                 break;
215                         case TEX_PD_WORLDSPACE:
216                         default:
217                                 Mat4MulVecfl(ob->obmat, co);
218                                 break;
219                 }
220
221                 BLI_bvhtree_insert(pd->point_tree, i, co, 1);
222         }
223         
224         BLI_bvhtree_balance(pd->point_tree);
225         dm->release(dm);
226
227 }
228 static void cache_pointdensity(Render *re, Tex *tex)
229 {
230         PointDensity *pd = tex->pd;
231
232         if (pd->point_tree) {
233                 BLI_bvhtree_free(pd->point_tree);
234                 pd->point_tree = NULL;
235         }
236         
237         if (pd->source == TEX_PD_PSYS) {
238                 Object *ob = pd->object;
239
240                 if (!ob) return;
241                 if (!pd->psys) return;
242                 
243                 pointdensity_cache_psys(re, pd, ob, pd->psys);
244         }
245         else if (pd->source == TEX_PD_OBJECT) {
246                 Object *ob = pd->object;
247                 if (ob) pointdensity_cache_object(re, pd, ob);
248         }
249 }
250
251 static void free_pointdensity(Render *re, Tex *tex)
252 {
253         PointDensity *pd = tex->pd;
254
255         if (!pd) return;
256         
257         if (pd->point_tree) {
258                 BLI_bvhtree_free(pd->point_tree);
259                 pd->point_tree = NULL;
260         }
261
262         if (pd->point_data) {
263                 MEM_freeN(pd->point_data);
264                 pd->point_data = NULL;
265         }
266         pd->totpoints = 0;
267 }
268
269
270
271 void make_pointdensities(Render *re)
272 {
273         Tex *tex;
274         
275         if(re->scene->r.scemode & R_PREVIEWBUTS)
276                 return;
277         
278         re->i.infostr= "Caching Point Densities";
279         re->stats_draw(re->sdh, &re->i);
280
281         for (tex= G.main->tex.first; tex; tex= tex->id.next) {
282                 if(tex->id.us && tex->type==TEX_POINTDENSITY) {
283                         cache_pointdensity(re, tex);
284                 }
285         }
286         
287         re->i.infostr= NULL;
288         re->stats_draw(re->sdh, &re->i);
289 }
290
291 void free_pointdensities(Render *re)
292 {
293         Tex *tex;
294         
295         if(re->scene->r.scemode & R_PREVIEWBUTS)
296                 return;
297         
298         for (tex= G.main->tex.first; tex; tex= tex->id.next) {
299                 if(tex->id.us && tex->type==TEX_POINTDENSITY) {
300                         free_pointdensity(re, tex);
301                 }
302         }
303 }
304
305 typedef struct PointDensityRangeData
306 {
307     float *density;
308     float squared_radius;
309     float *point_data;
310         float *vec;
311         float softness;
312     short falloff_type;
313         short noise_influence;
314         float *age;
315         int point_data_used;
316         int offset;
317 } PointDensityRangeData;
318
319 void accum_density(void *userdata, int index, float squared_dist)
320 {
321         PointDensityRangeData *pdr = (PointDensityRangeData *)userdata;
322         const float dist = (pdr->squared_radius - squared_dist) / pdr->squared_radius * 0.5f;
323         float density = 0.0f;
324         
325         if (pdr->falloff_type == TEX_PD_FALLOFF_STD)
326                 density = dist;
327         else if (pdr->falloff_type == TEX_PD_FALLOFF_SMOOTH)
328                 density = 3.0f*dist*dist - 2.0f*dist*dist*dist;
329         else if (pdr->falloff_type == TEX_PD_FALLOFF_SOFT)
330                 density = pow(dist, pdr->softness);
331         else if (pdr->falloff_type == TEX_PD_FALLOFF_CONSTANT)
332                 density = pdr->squared_radius;
333         else if (pdr->falloff_type == TEX_PD_FALLOFF_ROOT)
334                 density = sqrt(dist);
335         
336         if (pdr->point_data_used & POINT_DATA_VEL) {
337                 pdr->vec[0] += pdr->point_data[index*3 + 0]; //* density;
338                 pdr->vec[1] += pdr->point_data[index*3 + 1]; //* density;
339                 pdr->vec[2] += pdr->point_data[index*3 + 2]; //* density;
340         }
341         if (pdr->point_data_used & POINT_DATA_LIFE) {
342                 *pdr->age += pdr->point_data[pdr->offset + index]; // * density;
343         }
344         
345         *pdr->density += density;
346 }
347
348
349 static void init_pointdensityrangedata(PointDensity *pd, PointDensityRangeData *pdr, float *density, float *vec, float *age)
350 {
351         pdr->squared_radius = pd->radius*pd->radius;
352         pdr->density = density;
353         pdr->point_data = pd->point_data;
354         pdr->falloff_type = pd->falloff_type;
355         pdr->vec = vec;
356         pdr->age = age;
357         pdr->softness = pd->falloff_softness;
358         pdr->noise_influence = pd->noise_influence;
359         pdr->point_data_used = point_data_used(pd);
360         pdr->offset = (pdr->point_data_used & POINT_DATA_VEL)?pd->totpoints*3:0;
361 }
362
363
364 int pointdensitytex(Tex *tex, float *texvec, TexResult *texres)
365 {
366         int retval = TEX_INT;
367         PointDensity *pd = tex->pd;
368         PointDensityRangeData pdr;
369         float density=0.0f, age=0.0f, time=0.0f;
370         float vec[3] = {0.0f, 0.0f, 0.0f}, co[3];
371         float col[4];
372         float turb, noise_fac;
373         int num=0;
374         
375         texres->tin = 0.0f;
376         
377         if ((!pd) || (!pd->point_tree))         
378                 return 0;
379                 
380         init_pointdensityrangedata(pd, &pdr, &density, vec, &age);
381         noise_fac = pd->noise_fac * 0.5f;       /* better default */
382         
383         VECCOPY(co, texvec);
384         
385         if (point_data_used(pd)) {
386                 /* does a BVH lookup to find accumulated density and additional point data *
387                  * stores particle velocity vector in 'vec', and particle lifetime in 'time' */
388                 num = BLI_bvhtree_range_query(pd->point_tree, co, pd->radius, accum_density, &pdr);
389                 if (num > 0) {
390                         age /= num;
391                         VecMulf(vec, 1.0f/num);
392                 }
393                 
394                 /* reset */
395                 density = vec[0] = vec[1] = vec[2] = 0.0f;
396         }
397         
398         if (pd->flag & TEX_PD_TURBULENCE) {
399         
400                 if (pd->noise_influence == TEX_PD_NOISE_AGE) {
401                         turb = BLI_gTurbulence(pd->noise_size, texvec[0]+age, texvec[1]+age, texvec[2]+age, pd->noise_depth, 0, pd->noise_basis);
402                 }
403                 else if (pd->noise_influence == TEX_PD_NOISE_TIME) {
404                         time = R.cfra / (float)R.r.efra;
405                         turb = BLI_gTurbulence(pd->noise_size, texvec[0]+time, texvec[1]+time, texvec[2]+time, pd->noise_depth, 0, pd->noise_basis);
406                         //turb = BLI_turbulence(pd->noise_size, texvec[0]+time, texvec[1]+time, texvec[2]+time, pd->noise_depth);
407                 }
408                 else {
409                         turb = BLI_gTurbulence(pd->noise_size, texvec[0]+vec[0], texvec[1]+vec[1], texvec[2]+vec[2], pd->noise_depth, 0, pd->noise_basis);
410                 }
411
412                 turb -= 0.5f;   /* re-center 0.0-1.0 range around 0 to prevent offsetting result */
413                 
414                 /* now we have an offset coordinate to use for the density lookup */
415                 co[0] = texvec[0] + noise_fac * turb;
416                 co[1] = texvec[1] + noise_fac * turb;
417                 co[2] = texvec[2] + noise_fac * turb;
418         }
419
420         /* BVH query with the potentially perturbed coordinates */
421         num = BLI_bvhtree_range_query(pd->point_tree, co, pd->radius, accum_density, &pdr);
422         if (num > 0) {
423                 age /= num;
424                 VecMulf(vec, 1.0f/num);
425         }
426         
427         texres->tin = density;
428         BRICONT;
429         
430         if (pd->color_source == TEX_PD_COLOR_CONSTANT)
431                 return retval;
432         
433         retval |= TEX_RGB;
434         
435         switch (pd->color_source) {
436                 case TEX_PD_COLOR_PARTAGE:
437                         if (pd->coba) {
438                                 if (do_colorband(pd->coba, age, col)) {
439                                         texres->talpha= 1;
440                                         VECCOPY(&texres->tr, col);
441                                         texres->tin *= col[3];
442                                         texres->ta = texres->tin;
443                                 }
444                         }
445                         break;
446                 case TEX_PD_COLOR_PARTSPEED:
447                 {
448                         float speed = VecLength(vec) * pd->speed_scale;
449                         
450                         if (pd->coba) {
451                                 if (do_colorband(pd->coba, speed, col)) {
452                                         texres->talpha= 1;      
453                                         VECCOPY(&texres->tr, col);
454                                         texres->tin *= col[3];
455                                         texres->ta = texres->tin;
456                                 }
457                         }
458                         break;
459                 }
460                 case TEX_PD_COLOR_PARTVEL:
461                         texres->talpha= 1;
462                         VecMulf(vec, pd->speed_scale);
463                         VECCOPY(&texres->tr, vec);
464                         texres->ta = texres->tin;
465                         break;
466                 case TEX_PD_COLOR_CONSTANT:
467                 default:
468                         texres->tr = texres->tg = texres->tb = texres->ta = 1.0f;
469                         break;
470         }
471         BRICONTRGB;
472         
473         return retval;
474         
475         /*
476         if (texres->nor!=NULL) {
477                 texres->nor[0] = texres->nor[1] = texres->nor[2] = 0.0f;
478         }
479         */
480 }