Smoke:
[blender.git] / source / blender / blenkernel / intern / smoke.c
1 /**
2  * smoke.c
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) Blender Foundation.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): Daniel Genrich
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 /* Part of the code copied from elbeem fluid library, copyright by Nils Thuerey */
33
34 #include <GL/glew.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include <float.h>
39 #include <math.h>
40 #include "stdio.h"
41 #include "string.h" /* memset */
42
43 #include "BLI_linklist.h"
44 #include "BLI_rand.h"
45 #include "BLI_jitter.h"
46 #include "BLI_blenlib.h"
47 #include "BLI_arithb.h"
48 #include "BLI_edgehash.h"
49 #include "BLI_kdtree.h"
50 #include "BLI_kdopbvh.h"
51
52 #include "BKE_bvhutils.h"
53 #include "BKE_cdderivedmesh.h"
54 #include "BKE_customdata.h"
55 #include "BKE_DerivedMesh.h"
56 #include "BKE_modifier.h"
57 #include "BKE_particle.h"
58 #include "BKE_pointcache.h"
59 #include "BKE_smoke.h"
60 #include "BKE_utildefines.h"
61
62 #include "DNA_customdata_types.h"
63 #include "DNA_group_types.h"
64 #include "DNA_lamp_types.h"
65 #include "DNA_mesh_types.h"
66 #include "DNA_meshdata_types.h"
67 #include "DNA_modifier_types.h"
68 #include "DNA_object_types.h"
69 #include "DNA_particle_types.h"
70 #include "DNA_scene_types.h"
71 #include "DNA_smoke_types.h"
72
73 #include "smoke_API.h"
74
75 #include "BKE_smoke.h"
76
77 #ifdef _WIN32
78 #include <time.h>
79 #include <stdio.h>
80 #include <conio.h>
81 #include <windows.h>
82
83 static LARGE_INTEGER liFrequency;
84 static LARGE_INTEGER liStartTime;
85 static LARGE_INTEGER liCurrentTime;
86
87 static void tstart ( void )
88 {
89         QueryPerformanceFrequency ( &liFrequency );
90         QueryPerformanceCounter ( &liStartTime );
91 }
92 static void tend ( void )
93 {
94         QueryPerformanceCounter ( &liCurrentTime );
95 }
96 static double tval()
97 {
98         return ((double)( (liCurrentTime.QuadPart - liStartTime.QuadPart)* (double)1000.0/(double)liFrequency.QuadPart ));
99 }
100 #else
101 #include <sys/time.h>
102 static struct timeval _tstart, _tend;
103 static struct timezone tz;
104 static void tstart ( void )
105 {
106         gettimeofday ( &_tstart, &tz );
107 }
108 static void tend ( void )
109 {
110         gettimeofday ( &_tend,&tz );
111 }
112 static double tval()
113 {
114         double t1, t2;
115         t1 = ( double ) _tstart.tv_sec*1000 + ( double ) _tstart.tv_usec/ ( 1000 );
116         t2 = ( double ) _tend.tv_sec*1000 + ( double ) _tend.tv_usec/ ( 1000 );
117         return t2-t1;
118 }
119 #endif
120
121 struct Object;
122 struct Scene;
123 struct DerivedMesh;
124 struct SmokeModifierData;
125
126 // forward declerations
127 static void get_cell(float *p0, int res[3], float dx, float *pos, int *cell, int correct);
128 void calcTriangleDivs(Object *ob, MVert *verts, int numverts, MFace *tris, int numfaces, int numtris, int **tridivs, float cell_len);
129
130 #define TRI_UVOFFSET (1./4.)
131
132 int smokeModifier_init (SmokeModifierData *smd, Object *ob, Scene *scene, DerivedMesh *dm)
133 {
134         if((smd->type & MOD_SMOKE_TYPE_DOMAIN) && smd->domain && !smd->domain->fluid)
135         {
136                 size_t i;
137                 float min[3] = {FLT_MAX, FLT_MAX, FLT_MAX}, max[3] = {-FLT_MAX, -FLT_MAX, -FLT_MAX};
138                 float size[3];
139                 MVert *verts = dm->getVertArray(dm);
140                 float scale = 0.0;
141                 int res;                
142
143                 res = smd->domain->maxres;
144
145                 // get BB of domain
146                 for(i = 0; i < dm->getNumVerts(dm); i++)
147                 {
148                         float tmp[3];
149
150                         VECCOPY(tmp, verts[i].co);
151                         Mat4MulVecfl(ob->obmat, tmp);
152
153                         // min BB
154                         min[0] = MIN2(min[0], tmp[0]);
155                         min[1] = MIN2(min[1], tmp[1]);
156                         min[2] = MIN2(min[2], tmp[2]);
157
158                         // max BB
159                         max[0] = MAX2(max[0], tmp[0]);
160                         max[1] = MAX2(max[1], tmp[1]);
161                         max[2] = MAX2(max[2], tmp[2]);
162                 }
163
164                 VECCOPY(smd->domain->p0, min);
165                 VECCOPY(smd->domain->p1, max);
166
167                 // calc other res with max_res provided
168                 VECSUB(size, max, min);
169
170                 // printf("size: %f, %f, %f\n", size[0], size[1], size[2]);
171
172                 // prevent crash when initializing a plane as domain
173                 if((size[0] < FLT_EPSILON) || (size[1] < FLT_EPSILON) || (size[2] < FLT_EPSILON))
174                         return 0;
175
176                 if(size[0] > size[1])
177                 {
178                         if(size[0] > size[1])
179                         {
180                                 scale = res / size[0];
181                                 smd->domain->dx = size[0] / res;
182                                 smd->domain->res[0] = res;
183                                 smd->domain->res[1] = (int)(size[1] * scale + 0.5);
184                                 smd->domain->res[2] = (int)(size[2] * scale + 0.5);
185                         }
186                         else
187                         {
188                                 scale = res / size[1];
189                                 smd->domain->dx = size[1] / res;
190                                 smd->domain->res[1] = res;
191                                 smd->domain->res[0] = (int)(size[0] * scale + 0.5);
192                                 smd->domain->res[2] = (int)(size[2] * scale + 0.5);
193                         }
194                 }
195                 else
196                 {
197                         if(size[1] > size[2])
198                         {
199                                 scale = res / size[1];
200                                 smd->domain->dx = size[1] / res;
201                                 smd->domain->res[1] = res;
202                                 smd->domain->res[0] = (int)(size[0] * scale + 0.5);
203                                 smd->domain->res[2] = (int)(size[2] * scale + 0.5);
204                         }
205                         else
206                         {
207                                 scale = res / size[2];
208                                 smd->domain->dx = size[2] / res;
209                                 smd->domain->res[2] = res;
210                                 smd->domain->res[0] = (int)(size[0] * scale + 0.5);
211                                 smd->domain->res[1] = (int)(size[1] * scale + 0.5);
212                         }
213                 }
214
215                 // printf("smd->domain->dx: %f\n", smd->domain->dx);
216
217                 // TODO: put in failsafe if res<=0 - dg
218
219                 // printf("res[0]: %d, res[1]: %d, res[2]: %d\n", smd->domain->res[0], smd->domain->res[1], smd->domain->res[2]);
220                 // dt max is 0.1
221                 smd->domain->fluid = smoke_init(smd->domain->res, smd->domain->p0, 0.1);
222                 smd->time = scene->r.cfra;
223
224                 if(smd->domain->flags & MOD_SMOKE_HIGHRES)
225                 {
226                         smd->domain->wt = smoke_turbulence_init(smd->domain->res, smd->domain->amplify + 1, smd->domain->noise);
227                         smd->domain->res_wt[0] = smd->domain->res[0] * (smd->domain->amplify + 1);
228                         smd->domain->res_wt[1] = smd->domain->res[1] * (smd->domain->amplify + 1);                      
229                         smd->domain->res_wt[2] = smd->domain->res[2] * (smd->domain->amplify + 1);                      
230                         smd->domain->dx_wt = smd->domain->dx / (smd->domain->amplify + 1);              
231                         // printf("smd->domain->amplify: %d\n",  smd->domain->amplify);
232                         // printf("(smd->domain->flags & MOD_SMOKE_HIGHRES)\n");
233                 }
234
235                 if(!smd->domain->shadow)
236                         smd->domain->shadow = MEM_callocN(sizeof(float) * smd->domain->res[0] * smd->domain->res[1] * smd->domain->res[2], "SmokeDomainShadow");
237
238                 smoke_initBlenderRNA(smd->domain->fluid, &(smd->domain->alpha), &(smd->domain->beta));
239
240                 if(smd->domain->wt)     
241                 {
242                         smoke_initWaveletBlenderRNA(smd->domain->wt, &(smd->domain->strength));
243                         // printf("smoke_initWaveletBlenderRNA\n");
244                 }
245                 return 1;
246         }
247         else if((smd->type & MOD_SMOKE_TYPE_FLOW) && smd->flow)
248         {
249                 // handle flow object here
250                 // XXX TODO
251
252                 smd->time = scene->r.cfra;
253
254                 // update particle lifetime to be one frame
255                 // smd->flow->psys->part->lifetime = scene->r.efra + 1;
256 /*
257                 if(!smd->flow->bvh)
258                 {
259                         // smd->flow->bvh = MEM_callocN(sizeof(BVHTreeFromMesh), "smoke_bvhfromfaces");
260                         // bvhtree_from_mesh_faces(smd->flow->bvh, dm, 0.0, 2, 6);
261
262                         // copy obmat
263                         // Mat4CpyMat4(smd->flow->mat, ob->obmat);
264                         // Mat4CpyMat4(smd->flow->mat_old, ob->obmat);
265                 }
266 */
267
268                 return 1;
269         }
270         else if((smd->type & MOD_SMOKE_TYPE_COLL))
271         {
272                 smd->time = scene->r.cfra;
273
274                 // todo: delete this when loading colls work -dg
275                 if(!smd->coll)
276                         smokeModifier_createType(smd);
277
278                 if(!smd->coll->points)
279                 {
280                         // init collision points
281                         SmokeCollSettings *scs = smd->coll;
282                         MVert *mvert = dm->getVertArray(dm);
283                         MFace *mface = dm->getFaceArray(dm);
284                         int i = 0, divs = 0;
285                         int *tridivs = NULL;
286                         float cell_len = 1.0 / 50.0; // for res = 50
287                         int newdivs = 0;
288                         int quads = 0, facecounter = 0;
289
290                         // copy obmat
291                         Mat4CpyMat4(scs->mat, ob->obmat);
292                         Mat4CpyMat4(scs->mat_old, ob->obmat);
293
294                         // count quads
295                         for(i = 0; i < dm->getNumFaces(dm); i++)
296                         {
297                                 if(mface[i].v4)
298                                         quads++;
299                         }
300
301                         calcTriangleDivs(ob, mvert, dm->getNumVerts(dm), mface,  dm->getNumFaces(dm), dm->getNumFaces(dm) + quads, &tridivs, cell_len);
302
303                         // count triangle divisions
304                         for(i = 0; i < dm->getNumFaces(dm) + quads; i++)
305                         {
306                                 divs += (tridivs[3 * i] + 1) * (tridivs[3 * i + 1] + 1) * (tridivs[3 * i + 2] + 1);
307                         }
308
309                         // printf("divs: %d\n", divs);
310
311                         scs->points = MEM_callocN(sizeof(float) * (dm->getNumVerts(dm) + divs) * 3, "SmokeCollPoints");
312
313                         for(i = 0; i < dm->getNumVerts(dm); i++)
314                         {
315                                 float tmpvec[3];
316                                 VECCOPY(tmpvec, mvert[i].co);
317                                 Mat4MulVecfl (ob->obmat, tmpvec);
318                                 VECCOPY(&scs->points[i * 3], tmpvec);
319                         }
320                         
321                         for(i = 0, facecounter = 0; i < dm->getNumFaces(dm); i++)
322                         {
323                                 int again = 0;
324                                 do
325                                 {
326                                         int j, k;
327                                         int divs1 = tridivs[3 * facecounter + 0];
328                                         int divs2 = tridivs[3 * facecounter + 1];
329                                         //int divs3 = tridivs[3 * facecounter + 2];
330                                         float side1[3], side2[3], trinormorg[3], trinorm[3];
331                                         
332                                         if(again == 1 && mface[i].v4)
333                                         {
334                                                 VECSUB(side1,  mvert[ mface[i].v3 ].co, mvert[ mface[i].v1 ].co);
335                                                 VECSUB(side2,  mvert[ mface[i].v4 ].co, mvert[ mface[i].v1 ].co);
336                                         }
337                                         else
338                                         {
339                                                 VECSUB(side1,  mvert[ mface[i].v2 ].co, mvert[ mface[i].v1 ].co);
340                                                 VECSUB(side2,  mvert[ mface[i].v3 ].co, mvert[ mface[i].v1 ].co);
341                                         }
342
343                                         Crossf(trinormorg, side1, side2);
344                                         Normalize(trinormorg);
345                                         VECCOPY(trinorm, trinormorg);
346                                         VecMulf(trinorm, 0.25 * cell_len);
347
348                                         for(j = 0; j <= divs1; j++)
349                                         {
350                                                 for(k = 0; k <= divs2; k++)
351                                                 {
352                                                         float p1[3], p2[3], p3[3], p[3]={0,0,0}; 
353                                                         const float uf = (float)(j + TRI_UVOFFSET) / (float)(divs1 + 0.0);
354                                                         const float vf = (float)(k + TRI_UVOFFSET) / (float)(divs2 + 0.0);
355                                                         float tmpvec[3];
356                                                         
357                                                         if(uf+vf > 1.0) 
358                                                         {
359                                                                 // printf("bigger - divs1: %d, divs2: %d\n", divs1, divs2);
360                                                                 continue;
361                                                         }
362
363                                                         VECCOPY(p1, mvert[ mface[i].v1 ].co);
364                                                         if(again == 1 && mface[i].v4)
365                                                         {
366                                                                 VECCOPY(p2, mvert[ mface[i].v3 ].co);
367                                                                 VECCOPY(p3, mvert[ mface[i].v4 ].co);
368                                                         }
369                                                         else
370                                                         {
371                                                                 VECCOPY(p2, mvert[ mface[i].v2 ].co);
372                                                                 VECCOPY(p3, mvert[ mface[i].v3 ].co);
373                                                         }
374
375                                                         VecMulf(p1, (1.0-uf-vf));
376                                                         VecMulf(p2, uf);
377                                                         VecMulf(p3, vf);
378                                                         
379                                                         VECADD(p, p1, p2);
380                                                         VECADD(p, p, p3);
381
382                                                         if(newdivs > divs)
383                                                                 printf("mem problem\n");
384
385                                                         // mMovPoints.push_back(p + trinorm);
386                                                         VECCOPY(tmpvec, p);
387                                                         VECADD(tmpvec, tmpvec, trinorm);
388                                                         Mat4MulVecfl (ob->obmat, tmpvec);
389                                                         VECCOPY(&scs->points[3 * (dm->getNumVerts(dm) + newdivs)], tmpvec);
390                                                         newdivs++;
391
392                                                         if(newdivs > divs)
393                                                                 printf("mem problem\n");
394
395                                                         // mMovPoints.push_back(p - trinorm);
396                                                         VECCOPY(tmpvec, p);
397                                                         VECSUB(tmpvec, tmpvec, trinorm);
398                                                         Mat4MulVecfl (ob->obmat, tmpvec);
399                                                         VECCOPY(&scs->points[3 * (dm->getNumVerts(dm) + newdivs)], tmpvec);
400                                                         newdivs++;
401                                                 }
402                                         }
403
404                                         if(again == 0 && mface[i].v4)
405                                                 again++;
406                                         else
407                                                 again = 0;
408
409                                         facecounter++;
410
411                                 } while(again!=0);
412                         }
413
414                         scs->numpoints = dm->getNumVerts(dm) + newdivs;
415
416                         MEM_freeN(tridivs);
417                 }
418
419                 if(!smd->coll->bvhtree)
420                 {
421                         smd->coll->bvhtree = NULL; // bvhtree_build_from_smoke ( ob->obmat, dm->getFaceArray(dm), dm->getNumFaces(dm), dm->getVertArray(dm), dm->getNumVerts(dm), 0.0 );
422                 }
423                 return 1;
424         }
425
426         return 1;
427 }
428
429 /*! init triangle divisions */
430 void calcTriangleDivs(Object *ob, MVert *verts, int numverts, MFace *faces, int numfaces, int numtris, int **tridivs, float cell_len) 
431 {
432         // mTriangleDivs1.resize( faces.size() );
433         // mTriangleDivs2.resize( faces.size() );
434         // mTriangleDivs3.resize( faces.size() );
435
436         size_t i = 0, facecounter = 0;
437         float maxscale[3] = {1,1,1}; // = channelFindMaxVf(mcScale);
438         float maxpart = ABS(maxscale[0]);
439         float scaleFac = 0;
440         float fsTri = 0;
441         if(ABS(maxscale[1])>maxpart) maxpart = ABS(maxscale[1]);
442         if(ABS(maxscale[2])>maxpart) maxpart = ABS(maxscale[2]);
443         scaleFac = 1.0 / maxpart;
444         // featureSize = mLevel[mMaxRefine].nodeSize
445         fsTri = cell_len * 0.5 * scaleFac;
446
447         if(*tridivs)
448                 MEM_freeN(*tridivs);
449
450         *tridivs = MEM_callocN(sizeof(int) * numtris * 3, "Smoke_Tridivs");
451
452         for(i = 0, facecounter = 0; i < numfaces; i++) 
453         {
454                 float p0[3], p1[3], p2[3];
455                 float side1[3];
456                 float side2[3];
457                 float side3[3];
458                 int divs1=0, divs2=0, divs3=0;
459
460                 VECCOPY(p0, verts[faces[i].v1].co);
461                 Mat4MulVecfl (ob->obmat, p0);
462                 VECCOPY(p1, verts[faces[i].v2].co);
463                 Mat4MulVecfl (ob->obmat, p1);
464                 VECCOPY(p2, verts[faces[i].v3].co);
465                 Mat4MulVecfl (ob->obmat, p2);
466
467                 VECSUB(side1, p1, p0);
468                 VECSUB(side2, p2, p0);
469                 VECSUB(side3, p1, p2);
470
471                 if(INPR(side1, side1) > fsTri*fsTri) 
472                 { 
473                         float tmp = Normalize(side1);
474                         divs1 = (int)ceil(tmp/fsTri); 
475                 }
476                 if(INPR(side2, side2) > fsTri*fsTri) 
477                 { 
478                         float tmp = Normalize(side2);
479                         divs2 = (int)ceil(tmp/fsTri); 
480                         
481                         /*
482                         // debug
483                         if(i==0)
484                                 printf("b tmp: %f, fsTri: %f, divs2: %d\n", tmp, fsTri, divs2);
485                         */
486                 }
487
488                 (*tridivs)[3 * facecounter + 0] = divs1;
489                 (*tridivs)[3 * facecounter + 1] = divs2;
490                 (*tridivs)[3 * facecounter + 2] = divs3;
491
492                 // TODO quad case
493                 if(faces[i].v4)
494                 {
495                         divs1=0, divs2=0, divs3=0;
496
497                         facecounter++;
498                         
499                         VECCOPY(p0, verts[faces[i].v3].co);
500                         Mat4MulVecfl (ob->obmat, p0);
501                         VECCOPY(p1, verts[faces[i].v4].co);
502                         Mat4MulVecfl (ob->obmat, p1);
503                         VECCOPY(p2, verts[faces[i].v1].co);
504                         Mat4MulVecfl (ob->obmat, p2);
505
506                         VECSUB(side1, p1, p0);
507                         VECSUB(side2, p2, p0);
508                         VECSUB(side3, p1, p2);
509
510                         if(INPR(side1, side1) > fsTri*fsTri) 
511                         { 
512                                 float tmp = Normalize(side1);
513                                 divs1 = (int)ceil(tmp/fsTri); 
514                         }
515                         if(INPR(side2, side2) > fsTri*fsTri) 
516                         { 
517                                 float tmp = Normalize(side2);
518                                 divs2 = (int)ceil(tmp/fsTri); 
519                         }
520
521                         (*tridivs)[3 * facecounter + 0] = divs1;
522                         (*tridivs)[3 * facecounter + 1] = divs2;
523                         (*tridivs)[3 * facecounter + 2] = divs3;
524                 }
525                 facecounter++;
526         }
527 }
528
529 static void smokeModifier_freeDomain(SmokeModifierData *smd)
530 {
531         if(smd->domain)
532         {
533                 if(smd->domain->shadow)
534                                 MEM_freeN(smd->domain->shadow);
535                         smd->domain->shadow = NULL;
536
537                 if(smd->domain->fluid)
538                         smoke_free(smd->domain->fluid);
539
540                 if(smd->domain->wt)
541                         smoke_turbulence_free(smd->domain->wt);
542
543                 BKE_ptcache_free_list(&(smd->domain->ptcaches[0]));
544                 smd->domain->point_cache[0] = NULL;
545                 BKE_ptcache_free_list(&(smd->domain->ptcaches[1]));
546                 smd->domain->point_cache[1] = NULL;
547
548                 MEM_freeN(smd->domain);
549                 smd->domain = NULL;
550         }
551 }
552
553 static void smokeModifier_freeFlow(SmokeModifierData *smd)
554 {
555         if(smd->flow)
556         {
557 /*
558                 if(smd->flow->bvh)
559                 {
560                         free_bvhtree_from_mesh(smd->flow->bvh);
561                         MEM_freeN(smd->flow->bvh);
562                 }
563                 smd->flow->bvh = NULL;
564 */
565                 MEM_freeN(smd->flow);
566                 smd->flow = NULL;
567         }
568 }
569
570 static void smokeModifier_freeCollision(SmokeModifierData *smd)
571 {
572         if(smd->coll)
573         {
574                 if(smd->coll->points)
575                 {
576                         MEM_freeN(smd->coll->points);
577                         smd->coll->points = NULL;
578                 }
579
580                 if(smd->coll->bvhtree)
581                 {
582                         BLI_bvhtree_free(smd->coll->bvhtree);
583                         smd->coll->bvhtree = NULL;
584                 }
585
586                 if(smd->coll->dm)
587                         smd->coll->dm->release(smd->coll->dm);
588                 smd->coll->dm = NULL;
589
590                 MEM_freeN(smd->coll);
591                 smd->coll = NULL;
592         }
593 }
594
595 void smokeModifier_reset_turbulence(struct SmokeModifierData *smd)
596 {
597         if(smd && smd->domain && smd->domain->wt)
598         {
599                 smoke_turbulence_free(smd->domain->wt);
600                 smd->domain->wt = NULL;
601         }
602 }
603
604 void smokeModifier_reset(struct SmokeModifierData *smd)
605 {
606         if(smd)
607         {
608                 if(smd->domain)
609                 {
610                         if(smd->domain->shadow)
611                                 MEM_freeN(smd->domain->shadow);
612                         smd->domain->shadow = NULL;
613
614                         if(smd->domain->fluid)
615                         {
616                                 smoke_free(smd->domain->fluid);
617                                 smd->domain->fluid = NULL;
618                         }
619
620                         smd->domain->point_cache[0]->flag |= PTCACHE_OUTDATED;
621                         smd->domain->point_cache[1]->flag |= PTCACHE_OUTDATED;
622
623                         smokeModifier_reset_turbulence(smd);
624
625                         smd->time = -1;
626
627                         // printf("reset domain end\n");
628                 }
629                 else if(smd->flow)
630                 {
631                         /*
632                         if(smd->flow->bvh)
633                         {
634                                 free_bvhtree_from_mesh(smd->flow->bvh);
635                                 MEM_freeN(smd->flow->bvh);
636                         }
637                         smd->flow->bvh = NULL;
638                         */
639                 }
640                 else if(smd->coll)
641                 {
642                         if(smd->coll->points)
643                         {
644                                 MEM_freeN(smd->coll->points);
645                                 smd->coll->points = NULL;
646                         }
647
648                         if(smd->coll->bvhtree)
649                         {
650                                 BLI_bvhtree_free(smd->coll->bvhtree);
651                                 smd->coll->bvhtree = NULL;
652                         }
653
654                         if(smd->coll->dm)
655                                 smd->coll->dm->release(smd->coll->dm);
656                         smd->coll->dm = NULL;
657
658                 }
659         }
660 }
661
662 void smokeModifier_free (SmokeModifierData *smd)
663 {
664         if(smd)
665         {
666                 smokeModifier_freeDomain(smd);
667                 smokeModifier_freeFlow(smd);
668                 smokeModifier_freeCollision(smd);
669         }
670 }
671
672 void smokeModifier_createType(struct SmokeModifierData *smd)
673 {
674         if(smd)
675         {
676                 if(smd->type & MOD_SMOKE_TYPE_DOMAIN)
677                 {
678                         if(smd->domain)
679                                 smokeModifier_freeDomain(smd);
680
681                         smd->domain = MEM_callocN(sizeof(SmokeDomainSettings), "SmokeDomain");
682
683                         smd->domain->smd = smd;
684
685                         smd->domain->point_cache[0] = BKE_ptcache_add(&(smd->domain->ptcaches[0]));
686                         smd->domain->point_cache[0]->flag |= PTCACHE_DISK_CACHE;
687                         smd->domain->point_cache[0]->step = 1;
688
689                         smd->domain->point_cache[1] = BKE_ptcache_add(&(smd->domain->ptcaches[1]));
690                         smd->domain->point_cache[1]->flag |= PTCACHE_DISK_CACHE;
691                         smd->domain->point_cache[1]->step = 1;
692
693                         /* set some standard values */
694                         smd->domain->fluid = NULL;
695                         smd->domain->wt = NULL;                 
696                         smd->domain->eff_group = NULL;
697                         smd->domain->fluid_group = NULL;
698                         smd->domain->coll_group = NULL;
699                         smd->domain->maxres = 32;
700                         smd->domain->amplify = 1;                       
701                         smd->domain->omega = 1.0;                       
702                         smd->domain->alpha = -0.001;
703                         smd->domain->beta = 0.1;
704                         smd->domain->flags = MOD_SMOKE_DISSOLVE_LOG;
705                         smd->domain->strength = 2.0;
706                         smd->domain->noise = MOD_SMOKE_NOISEWAVE;
707                         smd->domain->diss_speed = 5;
708                         // init 3dview buffer
709                         smd->domain->viewsettings = 0;
710                 }
711                 else if(smd->type & MOD_SMOKE_TYPE_FLOW)
712                 {
713                         if(smd->flow)
714                                 smokeModifier_freeFlow(smd);
715
716                         smd->flow = MEM_callocN(sizeof(SmokeFlowSettings), "SmokeFlow");
717
718                         smd->flow->smd = smd;
719
720                         /* set some standard values */
721                         smd->flow->density = 1.0;
722                         smd->flow->temp = 1.0;
723
724                         smd->flow->psys = NULL;
725
726                 }
727                 else if(smd->type & MOD_SMOKE_TYPE_COLL)
728                 {
729                         if(smd->coll)
730                                 smokeModifier_freeCollision(smd);
731
732                         smd->coll = MEM_callocN(sizeof(SmokeCollSettings), "SmokeColl");
733
734                         smd->coll->smd = smd;
735                         smd->coll->points = NULL;
736                         smd->coll->numpoints = 0;
737                         smd->coll->bvhtree = NULL;
738                         smd->coll->dm = NULL;
739                 }
740         }
741 }
742
743 // forward decleration
744 static void smoke_calc_transparency(float *result, float *input, float *p0, float *p1, int res[3], float dx, float *light, bresenham_callback cb, float correct);
745 static float calc_voxel_transp(float *result, float *input, int res[3], int *pixel, float *tRay, float correct);
746 static int get_lamp(Scene *scene, float *light)
747 {       
748         Base *base_tmp = NULL;  
749         for(base_tmp = scene->base.first; base_tmp; base_tmp= base_tmp->next)   
750         {               
751                 if(base_tmp->object->type == OB_LAMP)           
752                 {                       
753                         Lamp *la = (Lamp *)base_tmp->object->data;      
754
755                         if(la->type == LA_LOCAL)                        
756                         {                               
757                                 VECCOPY(light, base_tmp->object->obmat[3]);                             
758                                 return 1;                       
759                         }               
760                 }       
761         }       
762         return 0;
763 }
764
765 static void smoke_calc_domain(Scene *scene, Object *ob, SmokeModifierData *smd)
766 {
767         SmokeDomainSettings *sds = smd->domain;
768         GroupObject *go = NULL;                 
769         Base *base = NULL;      
770
771         // do flows and fluids
772         if(1)                   
773         {                               
774                 Object *otherobj = NULL;                                
775                 ModifierData *md = NULL;
776                 if(sds->fluid_group) // we use groups since we have 2 domains
777                         go = sds->fluid_group->gobject.first;                           
778                 else                                    
779                         base = scene->base.first;
780                 while(base || go)
781                 {                                       
782                         otherobj = NULL;
783                         if(sds->fluid_group) 
784                         {
785                                 if(go->ob)                                                      
786                                         otherobj = go->ob;                                      
787                         }                                       
788                         else                                            
789                                 otherobj = base->object;
790                         if(!otherobj)
791                         {
792                                 if(sds->fluid_group)
793                                         go = go->next;
794                                 else
795                                         base= base->next;
796
797                                 continue;
798                         }
799
800                         md = modifiers_findByType(otherobj, eModifierType_Smoke);
801                         
802                         // check for active smoke modifier
803                         if(md && md->mode & (eModifierMode_Realtime | eModifierMode_Render))
804                         {
805                                 SmokeModifierData *smd2 = (SmokeModifierData *)md;
806                                 
807                                 // check for initialized smoke object
808                                 if((smd2->type & MOD_SMOKE_TYPE_FLOW) && smd2->flow)                                            
809                                 {
810                                         // we got nice flow object
811                                         SmokeFlowSettings *sfs = smd2->flow;
812                                         
813                                         if(sfs->psys && sfs->psys->part && sfs->psys->part->type==PART_EMITTER) // is particle system selected
814                                         {
815                                                 ParticleSystem *psys = sfs->psys;
816                                                 ParticleSettings *part=psys->part;
817                                                 ParticleData *pa = NULL;                                                                
818                                                 int p = 0;                                                              
819                                                 float *density = smoke_get_density(sds->fluid);                                                         
820                                                 float *bigdensity = smoke_turbulence_get_density(sds->wt);                                                              
821                                                 float *heat = smoke_get_heat(sds->fluid);                                                               
822                                                 float *velocity_x = smoke_get_velocity_x(sds->fluid);                                                           
823                                                 float *velocity_y = smoke_get_velocity_y(sds->fluid);                                                           
824                                                 float *velocity_z = smoke_get_velocity_z(sds->fluid);                                                           
825                                                 unsigned char *obstacle = smoke_get_obstacle(sds->fluid);                                                               
826                                                 int bigres[3];  
827                                                                                                                 
828                                                 // mostly copied from particle code                                                             
829                                                 for(p=0, pa=psys->particles; p<psys->totpart; p++, pa++)                                                                
830                                                 {                                                                       
831                                                         int cell[3];                                                                    
832                                                         size_t i = 0;                                                                   
833                                                         size_t index = 0;                                                                       
834                                                         int badcell = 0;                                                                                                                                                
835                                                         if(pa->alive == PARS_UNBORN && (part->flag & PART_UNBORN)==0) continue;                                                                 
836                                                         else if(pa->alive == PARS_DEAD && (part->flag & PART_DIED)==0) continue;                                                                        
837                                                         else if(pa->flag & (PARS_UNEXIST+PARS_NO_DISP)) continue;                                                                                                                                               
838                                                         // VECCOPY(pos, pa->state.co);                                                                  
839                                                         // Mat4MulVecfl (ob->imat, pos);                                                                                                                                                
840                                                         // 1. get corresponding cell    
841                                                         get_cell(smd->domain->p0, smd->domain->res, smd->domain->dx, pa->state.co, cell, 0);                                                                                                                                    
842                                                         // check if cell is valid (in the domain boundary)                                                                      
843                                                         for(i = 0; i < 3; i++)                                                                  
844                                                         {                                                                               
845                                                                 if((cell[i] > sds->res[i] - 1) || (cell[i] < 0))                                                                                
846                                                                 {                                                                                       
847                                                                         badcell = 1;                                                                                    
848                                                                         break;                                                                          
849                                                                 }                                                                       
850                                                         }                                                                                                                                                       
851                                                         if(badcell)                                                                             
852                                                                 continue;                                                                                                                                               
853                                                         // 2. set cell values (heat, density and velocity)                                                                      
854                                                         index = smoke_get_index(cell[0], sds->res[0], cell[1], sds->res[1], cell[2]);                                                                                                                                           
855                                                         if(!(sfs->type & MOD_SMOKE_FLOW_TYPE_OUTFLOW) && !(obstacle[index] & 2)) // this is inflow                                                                      
856                                                         {                                                                               
857                                                                 // heat[index] += sfs->temp * 0.1;                                                                              
858                                                                 // density[index] += sfs->density * 0.1;
859                                                                 heat[index] = sfs->temp;
860                                                                 density[index] = sfs->density;
861
862                                                                 /*
863                                                                 velocity_x[index] = pa->state.vel[0];
864                                                                 velocity_y[index] = pa->state.vel[1];
865                                                                 velocity_z[index] = pa->state.vel[2];                                                                           
866                                                                 */                                                                              
867                                                                 
868                                                                 // obstacle[index] |= 2;
869                                                                 // we need different handling for the high-res feature
870                                                                 if(bigdensity)
871                                                                 {
872                                                                         // init all surrounding cells according to amplification, too
873                                                                         int i, j, k;
874
875                                                                         smoke_turbulence_get_res(smd->domain->wt, bigres);
876
877                                                                         for(i = 0; i < smd->domain->amplify + 1; i++)
878                                                                                 for(j = 0; j < smd->domain->amplify + 1; j++)
879                                                                                         for(k = 0; k < smd->domain->amplify + 1; k++)                                                                                                   
880                                                                                         {                                                                                                               
881                                                                                                 index = smoke_get_index((smd->domain->amplify + 1)* cell[0] + i, bigres[0], (smd->domain->amplify + 1)* cell[1] + j, bigres[1], (smd->domain->amplify + 1)* cell[2] + k);                                                                                                               
882                                                                                                 bigdensity[index] = sfs->density;                                                                                                       
883                                                                                         }                                                                               
884                                                                 }                                                                       
885                                                         }                                                                       
886                                                         else if(sfs->type & MOD_SMOKE_FLOW_TYPE_OUTFLOW) // outflow                                                                     
887                                                         {                                                                               
888                                                                 heat[index] = 0.f;                                                                              
889                                                                 density[index] = 0.f;                                                                           
890                                                                 velocity_x[index] = 0.f;                                                                                
891                                                                 velocity_y[index] = 0.f;                                                                                
892                                                                 velocity_z[index] = 0.f;
893                                                                 // we need different handling for the high-res feature
894                                                                 if(bigdensity)
895                                                                 {
896                                                                         // init all surrounding cells according to amplification, too                                                                                   
897                                                                         int i, j, k;
898                                                                         smoke_turbulence_get_res(smd->domain->wt, bigres);
899
900                                                                         for(i = 0; i < smd->domain->amplify + 1; i++)
901                                                                                 for(j = 0; j < smd->domain->amplify + 1; j++)
902                                                                                         for(k = 0; k < smd->domain->amplify + 1; k++)
903                                                                                         {                                                                                                               
904                                                                                                 index = smoke_get_index((smd->domain->amplify + 1)* cell[0] + i, bigres[0], (smd->domain->amplify + 1)* cell[1] + j, bigres[1], (smd->domain->amplify + 1)* cell[2] + k);                                                                                                               
905                                                                                                 bigdensity[index] = 0.f;                                                                                                        
906                                                                                         }                                                                               
907                                                                 }                                                                       
908                                                         }       // particles loop                                                       
909                                         }                                                       
910                                 }                                                       
911                                 else                                                    
912                                 {                                                               
913                                         /*                                                              
914                                         for()                                                           
915                                         {                                                                       
916                                                 // no psys                                                                      
917                                                 BVHTreeNearest nearest;
918                                                 nearest.index = -1;
919                                                 nearest.dist = FLT_MAX;
920
921                                                 BLI_bvhtree_find_nearest(sfs->bvh->tree, pco, &nearest, sfs->bvh->nearest_callback, sfs->bvh);
922                                         }*/                                                     
923                                 }                                               
924                         }                                               
925                 }
926                         if(sds->fluid_group)
927                                 go = go->next;
928                         else
929                                 base= base->next;
930                 }
931         }
932
933         // do effectors
934         /*
935         if(sds->eff_group)
936         {
937                 for(go = sds->eff_group->gobject.first; go; go = go->next) 
938                 {
939                         if(go->ob)
940                         {
941                                 if(ob->pd)
942                                 {
943                                         
944                                 }                                       
945                         }
946                 }
947         }
948         */
949
950         // do collisions        
951         if(1)
952         {
953                 Object *otherobj = NULL;
954                 ModifierData *md = NULL;
955
956                 if(sds->coll_group) // we use groups since we have 2 domains
957                         go = sds->coll_group->gobject.first;
958                 else
959                         base = scene->base.first;
960
961                 while(base || go)
962                 {
963                         otherobj = NULL;
964                         if(sds->coll_group) 
965                         {                                               
966                                 if(go->ob)                                                      
967                                         otherobj = go->ob;                                      
968                         }                                       
969                         else                                            
970                                 otherobj = base->object;                                        
971                         if(!otherobj)                                   
972                         {                                               
973                                 if(sds->coll_group)                                                     
974                                         go = go->next;                                          
975                                 else                                                    
976                                         base= base->next;                                               
977                                 continue;                                       
978                         }                       
979                         md = modifiers_findByType(otherobj, eModifierType_Smoke);
980                         
981                         // check for active smoke modifier
982                         if(md && md->mode & (eModifierMode_Realtime | eModifierMode_Render))                                    
983                         {
984                                 SmokeModifierData *smd2 = (SmokeModifierData *)md;
985
986                                 if((smd2->type & MOD_SMOKE_TYPE_COLL) && smd2->coll)
987                                 {
988                                         // we got nice collision object
989                                         SmokeCollSettings *scs = smd2->coll;
990                                         size_t i, j;
991                                         unsigned char *obstacles = smoke_get_obstacle(smd->domain->fluid);
992
993                                         for(i = 0; i < scs->numpoints; i++)
994                                         {
995                                                 int badcell = 0;
996                                                 size_t index = 0;
997                                                 int cell[3];
998
999                                                 // 1. get corresponding cell
1000                                                 get_cell(smd->domain->p0, smd->domain->res, smd->domain->dx, &scs->points[3 * i], cell, 0);
1001                                         
1002                                                 // check if cell is valid (in the domain boundary)
1003                                                 for(j = 0; j < 3; j++)
1004                                                         if((cell[j] > sds->res[j] - 1) || (cell[j] < 0))
1005                                                         {
1006                                                                 badcell = 1;
1007                                                                 break;
1008                                                         }
1009                                                                                                                                 
1010                                                         if(badcell)                                                                     
1011                                                                 continue;
1012                                                 // 2. set cell values (heat, density and velocity)
1013                                                 index = smoke_get_index(cell[0], sds->res[0], cell[1], sds->res[1], cell[2]);
1014                                                                                                                 
1015                                                 // printf("cell[0]: %d, cell[1]: %d, cell[2]: %d\n", cell[0], cell[1], cell[2]);                                                                
1016                                                 // printf("res[0]: %d, res[1]: %d, res[2]: %d, index: %d\n\n", sds->res[0], sds->res[1], sds->res[2], index);                                                                                                                                   
1017                                                 obstacles[index] = 1;
1018                                                 // for moving gobstacles                                                                
1019                                                 /*
1020                                                 const LbmFloat maxVelVal = 0.1666;
1021                                                 const LbmFloat maxusqr = maxVelVal*maxVelVal*3. *1.5;
1022
1023                                                 LbmVec objvel = vec2L((mMOIVertices[n]-mMOIVerticesOld[n]) /dvec); 
1024                                                 {                                                               
1025                                                 const LbmFloat usqr = (objvel[0]*objvel[0]+objvel[1]*objvel[1]+objvel[2]*objvel[2])*1.5;                                                                
1026                                                 USQRMAXCHECK(usqr, objvel[0],objvel[1],objvel[2], mMaxVlen, mMxvx,mMxvy,mMxvz);                                                                 
1027                                                 if(usqr>maxusqr) {                                                                      
1028                                                 // cutoff at maxVelVal                                                                  
1029                                                 for(int jj=0; jj<3; jj++) {                                                                             
1030                                                 if(objvel[jj]>0.) objvel[jj] =  maxVelVal;                                                                              
1031                                                 if(objvel[jj]<0.) objvel[jj] = -maxVelVal;                                                                      
1032                                                 }                                                               
1033                                                 } 
1034                                                 }                                                               
1035                                                 const LbmFloat dp=dot(objvel, vec2L((*pNormals)[n]) );                                                          
1036                                                 const LbmVec oldov=objvel; // debug                                                             
1037                                                 objvel = vec2L((*pNormals)[n]) *dp;                                                             
1038                                                 */
1039                                         }
1040                                 }
1041                         }
1042
1043                         if(sds->coll_group)
1044                                 go = go->next;
1045                         else
1046                                 base= base->next;
1047                 }
1048         }
1049 }
1050 void smokeModifier_do(SmokeModifierData *smd, Scene *scene, Object *ob, DerivedMesh *dm, int useRenderParams, int isFinalCalc)
1051 {       
1052         if((smd->type & MOD_SMOKE_TYPE_FLOW))
1053         {
1054                 if(scene->r.cfra >= smd->time)
1055                         smokeModifier_init(smd, ob, scene, dm);
1056
1057                 if(scene->r.cfra > smd->time)
1058                 {
1059                         // XXX TODO
1060                         smd->time = scene->r.cfra;
1061
1062                         // rigid movement support
1063                         /*
1064                         Mat4CpyMat4(smd->flow->mat_old, smd->flow->mat);
1065                         Mat4CpyMat4(smd->flow->mat, ob->obmat);
1066                         */
1067                 }
1068                 else if(scene->r.cfra < smd->time)
1069                 {
1070                         smd->time = scene->r.cfra;
1071                         smokeModifier_reset(smd);
1072                 }
1073         }
1074         else if(smd->type & MOD_SMOKE_TYPE_COLL)
1075         {
1076                 if(scene->r.cfra >= smd->time)
1077                         smokeModifier_init(smd, ob, scene, dm);
1078
1079                 if(scene->r.cfra > smd->time)
1080                 {
1081                         // XXX TODO
1082                         smd->time = scene->r.cfra;
1083                         
1084                         if(smd->coll->dm)
1085                                 smd->coll->dm->release(smd->coll->dm);
1086
1087                         smd->coll->dm = CDDM_copy(dm);
1088
1089                         // rigid movement support
1090                         Mat4CpyMat4(smd->coll->mat_old, smd->coll->mat);
1091                         Mat4CpyMat4(smd->coll->mat, ob->obmat);
1092                 }
1093                 else if(scene->r.cfra < smd->time)
1094                 {
1095                         smd->time = scene->r.cfra;
1096                         smokeModifier_reset(smd);
1097                 }
1098         }
1099         else if(smd->type & MOD_SMOKE_TYPE_DOMAIN)
1100         {
1101                 SmokeDomainSettings *sds = smd->domain;
1102                 float light[3]; 
1103                 PointCache *cache = NULL;
1104                 PTCacheID pid;
1105                 PointCache *cache_wt = NULL;
1106                 PTCacheID pid_wt;
1107                 int startframe, endframe, framenr;
1108                 float timescale;
1109                 int cache_result = 0, cache_result_wt = 0;
1110
1111                 framenr = scene->r.cfra;
1112
1113                 // printf("time: %d\n", scene->r.cfra);
1114
1115                 if(framenr == smd->time)
1116                         return;
1117
1118                 cache = sds->point_cache[0];
1119                 BKE_ptcache_id_from_smoke(&pid, ob, smd);
1120                 BKE_ptcache_id_time(&pid, scene, framenr, &startframe, &endframe, &timescale);
1121
1122                 cache_wt = sds->point_cache[1];
1123                 BKE_ptcache_id_from_smoke_turbulence(&pid_wt, ob, smd);
1124
1125                 if(!smd->domain->fluid)
1126                 {
1127                         BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_OUTDATED);
1128                         BKE_ptcache_id_reset(scene, &pid_wt, PTCACHE_RESET_OUTDATED);
1129                 }
1130
1131                 if(framenr < startframe)
1132                         return;
1133
1134                 if(framenr > endframe)
1135                         return;
1136
1137                 if(!smd->domain->fluid && (framenr != startframe))
1138                         return;
1139
1140                 // printf("startframe: %d, framenr: %d\n", startframe, framenr);
1141
1142                 if(!smokeModifier_init(smd, ob, scene, dm))
1143                 {
1144                         printf("bad smokeModifier_init\n");
1145                         return;
1146                 }
1147
1148                 /* try to read from cache */
1149                 cache_result =  BKE_ptcache_read_cache(&pid, (float)framenr, scene->r.frs_sec);
1150                 // printf("cache_result: %d\n", cache_result);
1151
1152                 if(cache_result == PTCACHE_READ_EXACT) 
1153                 {
1154                         cache->flag |= PTCACHE_SIMULATION_VALID;
1155                         cache->simframe= framenr;
1156
1157                         if(sds->wt)
1158                         {
1159                                 cache_result_wt = BKE_ptcache_read_cache(&pid_wt, (float)framenr, scene->r.frs_sec);
1160                                 
1161                                 if(cache_result_wt == PTCACHE_READ_EXACT) 
1162                                 {
1163                                         cache_wt->flag |= PTCACHE_SIMULATION_VALID;
1164                                         cache_wt->simframe= framenr;
1165                                 }
1166                         }
1167                         return;
1168                 }
1169
1170                 tstart();
1171
1172                 smoke_calc_domain(scene, ob, smd);
1173                 
1174                 // set new time
1175                 smd->time = scene->r.cfra;
1176
1177                 /* do simulation */
1178
1179                 // low res
1180                 cache->flag |= PTCACHE_SIMULATION_VALID;
1181                 cache->simframe= framenr;
1182
1183                 // simulate the actual smoke (c++ code in intern/smoke)
1184                 // DG: interesting commenting this line + deactivating loading of noise files
1185                 if(framenr!=startframe)
1186                 {
1187                         if(sds->flags & MOD_SMOKE_DISSOLVE)
1188                                 smoke_dissolve(sds->fluid, sds->diss_speed, sds->flags & MOD_SMOKE_DISSOLVE_LOG);
1189                         smoke_step(sds->fluid, smd->time);
1190                 }
1191
1192                 // create shadows before writing cache so we get nice shadows for sstartframe, too
1193                 if(get_lamp(scene, light))
1194                         smoke_calc_transparency(sds->shadow, smoke_get_density(sds->fluid), sds->p0, sds->p1, sds->res, sds->dx, light, calc_voxel_transp, -7.0*sds->dx);
1195         
1196                 BKE_ptcache_write_cache(&pid, framenr);
1197
1198                 if(sds->wt)
1199                 {
1200                         if(framenr!=startframe)
1201                         {
1202                                 if(sds->flags & MOD_SMOKE_DISSOLVE)
1203                                         smoke_dissolve_wavelet(sds->wt, sds->diss_speed, sds->flags & MOD_SMOKE_DISSOLVE_LOG);
1204                                 smoke_turbulence_step(sds->wt, sds->fluid);
1205                         }
1206
1207                         cache_wt->flag |= PTCACHE_SIMULATION_VALID;
1208                         cache_wt->simframe= framenr;
1209                         BKE_ptcache_write_cache(&pid_wt, framenr);
1210                 }
1211
1212                 tend();
1213                 printf ( "Frame: %d, Time: %f\n", (int)smd->time, ( float ) tval() );
1214         }
1215 }
1216
1217 static float calc_voxel_transp(float *result, float *input, int res[3], int *pixel, float *tRay, float correct)
1218 {
1219         const size_t index = smoke_get_index(pixel[0], res[0], pixel[1], res[1], pixel[2]);
1220
1221         // T_ray *= T_vox
1222         *tRay *= exp(input[index]*correct);
1223         
1224         if(result[index] < 0.0f)        
1225         {
1226 #pragma omp critical            
1227                 result[index] = *tRay;  
1228         }       
1229
1230         return *tRay;
1231 }
1232
1233 long long smoke_get_mem_req(int xres, int yres, int zres, int amplify)
1234 {
1235           int totalCells = xres * yres * zres;
1236           int amplifiedCells = totalCells * amplify * amplify * amplify;
1237
1238           // print out memory requirements
1239           long long int coarseSize = sizeof(float) * totalCells * 22 +
1240                            sizeof(unsigned char) * totalCells;
1241
1242           long long int fineSize = sizeof(float) * amplifiedCells * 7 + // big grids
1243                          sizeof(float) * totalCells * 8 +     // small grids
1244                          sizeof(float) * 128 * 128 * 128;     // noise tile
1245
1246           long long int totalMB = (coarseSize + fineSize) / (1024 * 1024);
1247
1248           return totalMB;
1249 }
1250
1251 static void bresenham_linie_3D(int x1, int y1, int z1, int x2, int y2, int z2, float *tRay, bresenham_callback cb, float *result, float *input, int res[3], float correct)
1252 {
1253     int dx, dy, dz, i, l, m, n, x_inc, y_inc, z_inc, err_1, err_2, dx2, dy2, dz2;
1254     int pixel[3];
1255
1256     pixel[0] = x1;
1257     pixel[1] = y1;
1258     pixel[2] = z1;
1259
1260     dx = x2 - x1;
1261     dy = y2 - y1;
1262     dz = z2 - z1;
1263
1264     x_inc = (dx < 0) ? -1 : 1;
1265     l = abs(dx);
1266     y_inc = (dy < 0) ? -1 : 1;
1267     m = abs(dy);
1268     z_inc = (dz < 0) ? -1 : 1;
1269     n = abs(dz);
1270     dx2 = l << 1;
1271     dy2 = m << 1;
1272     dz2 = n << 1;
1273
1274     if ((l >= m) && (l >= n)) {
1275         err_1 = dy2 - l;
1276         err_2 = dz2 - l;
1277         for (i = 0; i < l; i++) {
1278                 if(cb(result, input, res, pixel, tRay, correct) <= FLT_EPSILON)
1279                         break;
1280             if (err_1 > 0) {
1281                 pixel[1] += y_inc;
1282                 err_1 -= dx2;
1283             }
1284             if (err_2 > 0) {
1285                 pixel[2] += z_inc;
1286                 err_2 -= dx2;
1287             }
1288             err_1 += dy2;
1289             err_2 += dz2;
1290             pixel[0] += x_inc;
1291         }
1292     } else if ((m >= l) && (m >= n)) {
1293         err_1 = dx2 - m;
1294         err_2 = dz2 - m;
1295         for (i = 0; i < m; i++) {
1296                 if(cb(result, input, res, pixel, tRay, correct) <= FLT_EPSILON)
1297                         break;
1298             if (err_1 > 0) {
1299                 pixel[0] += x_inc;
1300                 err_1 -= dy2;
1301             }
1302             if (err_2 > 0) {
1303                 pixel[2] += z_inc;
1304                 err_2 -= dy2;
1305             }
1306             err_1 += dx2;
1307             err_2 += dz2;
1308             pixel[1] += y_inc;
1309         }
1310     } else {
1311         err_1 = dy2 - n;
1312         err_2 = dx2 - n;
1313         for (i = 0; i < n; i++) {
1314                 if(cb(result, input, res, pixel, tRay, correct) <= FLT_EPSILON)
1315                         break;
1316             if (err_1 > 0) {
1317                 pixel[1] += y_inc;
1318                 err_1 -= dz2;
1319             }
1320             if (err_2 > 0) {
1321                 pixel[0] += x_inc;
1322                 err_2 -= dz2;
1323             }
1324             err_1 += dy2;
1325             err_2 += dx2;
1326             pixel[2] += z_inc;
1327         }
1328     }
1329     cb(result, input, res, pixel, tRay, correct);
1330 }
1331
1332 static void get_cell(float *p0, int res[3], float dx, float *pos, int *cell, int correct)
1333 {
1334         float tmp[3];
1335
1336         VECSUB(tmp, pos, p0);
1337         VecMulf(tmp, 1.0 / dx);
1338
1339         if(correct)
1340         {
1341                 cell[0] = MIN2(res[0] - 1, MAX2(0, (int)floor(tmp[0])));
1342                 cell[1] = MIN2(res[1] - 1, MAX2(0, (int)floor(tmp[1])));
1343                 cell[2] = MIN2(res[2] - 1, MAX2(0, (int)floor(tmp[2])));
1344         }
1345         else
1346         {
1347                 cell[0] = (int)floor(tmp[0]);
1348                 cell[1] = (int)floor(tmp[1]);
1349                 cell[2] = (int)floor(tmp[2]);
1350         }
1351 }
1352
1353 static void smoke_calc_transparency(float *result, float *input, float *p0, float *p1, int res[3], float dx, float *light, bresenham_callback cb, float correct)
1354 {
1355         int x, y, z;
1356         float bv[6];
1357
1358         memset(result, -1, sizeof(float)*res[0]*res[1]*res[2]); // x
1359         bv[0] = p0[0];
1360         bv[1] = p1[0];
1361         // y
1362         bv[2] = p0[1];
1363         bv[3] = p1[1];
1364         // z
1365         bv[4] = p0[2];
1366         bv[5] = p1[2];
1367
1368 #pragma omp parallel for schedule(static) private(y, z)
1369         for(x = 0; x < res[0]; x++)
1370                 for(y = 0; y < res[1]; y++)
1371                         for(z = 0; z < res[2]; z++)
1372                         {
1373                                 float voxelCenter[3];
1374                                 size_t index;
1375                                 float pos[3];
1376                                 int cell[3];
1377                                 float tRay = 1.0;
1378
1379                                 index = smoke_get_index(x, res[0], y, res[1], z);
1380
1381                                 if(result[index] >= 0.0f)                                       
1382                                         continue;                                                               
1383                                 voxelCenter[0] = p0[0] + dx *  x + dx * 0.5;
1384                                 voxelCenter[1] = p0[1] + dx *  y + dx * 0.5;
1385                                 voxelCenter[2] = p0[2] + dx *  z + dx * 0.5;
1386
1387                                 // get starting position (in voxel coords)
1388                                 if(BLI_bvhtree_bb_raycast(bv, light, voxelCenter, pos) > FLT_EPSILON)
1389                                 {
1390                                         // we're ouside
1391                                         get_cell(p0, res, dx, pos, cell, 1);
1392                                 }
1393                                 else
1394                                 {
1395                                         // we're inside
1396                                         get_cell(p0, res, dx, light, cell, 1);
1397                                 }
1398
1399                                 bresenham_linie_3D(cell[0], cell[1], cell[2], x, y, z, &tRay, cb, result, input, res, correct);
1400
1401                                 // convention -> from a RGBA float array, use G value for tRay
1402 // #pragma omp critical
1403                                 result[index] = tRay;                   
1404                         }
1405 }
1406