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