6485e367b782439004202034423f298e6dca1a2f
[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 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 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 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 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_KILLED) continue;                                                                  
836                                                         else if(pa->alive == PARS_UNBORN && (part->flag & PART_UNBORN)==0) continue;                                                                    
837                                                         else if(pa->alive == PARS_DEAD && (part->flag & PART_DIED)==0) continue;                                                                        
838                                                         else if(pa->flag & (PARS_UNEXIST+PARS_NO_DISP)) continue;                                                                                                                                               
839                                                         // VECCOPY(pos, pa->state.co);                                                                  
840                                                         // Mat4MulVecfl (ob->imat, pos);                                                                                                                                                
841                                                         // 1. get corresponding cell    
842                                                         get_cell(smd->domain->p0, smd->domain->res, smd->domain->dx, pa->state.co, cell, 0);                                                                                                                                    
843                                                         // check if cell is valid (in the domain boundary)                                                                      
844                                                         for(i = 0; i < 3; i++)                                                                  
845                                                         {                                                                               
846                                                                 if((cell[i] > sds->res[i] - 1) || (cell[i] < 0))                                                                                
847                                                                 {                                                                                       
848                                                                         badcell = 1;                                                                                    
849                                                                         break;                                                                          
850                                                                 }                                                                       
851                                                         }                                                                                                                                                       
852                                                         if(badcell)                                                                             
853                                                                 continue;                                                                                                                                               
854                                                         // 2. set cell values (heat, density and velocity)                                                                      
855                                                         index = smoke_get_index(cell[0], sds->res[0], cell[1], sds->res[1], cell[2]);                                                                                                                                           
856                                                         if(!(sfs->type & MOD_SMOKE_FLOW_TYPE_OUTFLOW) && !(obstacle[index] & 2)) // this is inflow                                                                      
857                                                         {                                                                               
858                                                                 // heat[index] += sfs->temp * 0.1;                                                                              
859                                                                 // density[index] += sfs->density * 0.1;
860                                                                 heat[index] = sfs->temp;
861                                                                 density[index] = sfs->density;
862
863                                                                 /*
864                                                                 velocity_x[index] = pa->state.vel[0];
865                                                                 velocity_y[index] = pa->state.vel[1];
866                                                                 velocity_z[index] = pa->state.vel[2];                                                                           
867                                                                 */                                                                              
868                                                                 
869                                                                 // obstacle[index] |= 2;
870                                                                 // we need different handling for the high-res feature
871                                                                 if(bigdensity)
872                                                                 {
873                                                                         // init all surrounding cells according to amplification, too
874                                                                         int i, j, k;
875
876                                                                         smoke_turbulence_get_res(smd->domain->wt, bigres);
877
878                                                                         for(i = 0; i < smd->domain->amplify + 1; i++)
879                                                                                 for(j = 0; j < smd->domain->amplify + 1; j++)
880                                                                                         for(k = 0; k < smd->domain->amplify + 1; k++)                                                                                                   
881                                                                                         {                                                                                                               
882                                                                                                 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);                                                                                                               
883                                                                                                 bigdensity[index] = sfs->density;                                                                                                       
884                                                                                         }                                                                               
885                                                                 }                                                                       
886                                                         }                                                                       
887                                                         else if(sfs->type & MOD_SMOKE_FLOW_TYPE_OUTFLOW) // outflow                                                                     
888                                                         {                                                                               
889                                                                 heat[index] = 0.f;                                                                              
890                                                                 density[index] = 0.f;                                                                           
891                                                                 velocity_x[index] = 0.f;                                                                                
892                                                                 velocity_y[index] = 0.f;                                                                                
893                                                                 velocity_z[index] = 0.f;
894                                                                 // we need different handling for the high-res feature
895                                                                 if(bigdensity)
896                                                                 {
897                                                                         // init all surrounding cells according to amplification, too                                                                                   
898                                                                         int i, j, k;
899                                                                         smoke_turbulence_get_res(smd->domain->wt, bigres);
900
901                                                                         for(i = 0; i < smd->domain->amplify + 1; i++)
902                                                                                 for(j = 0; j < smd->domain->amplify + 1; j++)
903                                                                                         for(k = 0; k < smd->domain->amplify + 1; k++)
904                                                                                         {                                                                                                               
905                                                                                                 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);                                                                                                               
906                                                                                                 bigdensity[index] = 0.f;                                                                                                        
907                                                                                         }                                                                               
908                                                                 }                                                                       
909                                                         }       // particles loop                                                       
910                                         }                                                       
911                                 }                                                       
912                                 else                                                    
913                                 {                                                               
914                                         /*                                                              
915                                         for()                                                           
916                                         {                                                                       
917                                                 // no psys                                                                      
918                                                 BVHTreeNearest nearest;
919                                                 nearest.index = -1;
920                                                 nearest.dist = FLT_MAX;
921
922                                                 BLI_bvhtree_find_nearest(sfs->bvh->tree, pco, &nearest, sfs->bvh->nearest_callback, sfs->bvh);
923                                         }*/                                                     
924                                 }                                               
925                         }                                               
926                 }
927                         if(sds->fluid_group)
928                                 go = go->next;
929                         else
930                                 base= base->next;
931                 }
932         }
933
934         // do effectors
935         /*
936         if(sds->eff_group)
937         {
938                 for(go = sds->eff_group->gobject.first; go; go = go->next) 
939                 {
940                         if(go->ob)
941                         {
942                                 if(ob->pd)
943                                 {
944                                         
945                                 }                                       
946                         }
947                 }
948         }
949         */
950
951         // do collisions        
952         if(1)
953         {
954                 Object *otherobj = NULL;
955                 ModifierData *md = NULL;
956
957                 if(sds->coll_group) // we use groups since we have 2 domains
958                         go = sds->coll_group->gobject.first;
959                 else
960                         base = scene->base.first;
961
962                 while(base || go)
963                 {
964                         otherobj = NULL;
965                         if(sds->coll_group) 
966                         {                                               
967                                 if(go->ob)                                                      
968                                         otherobj = go->ob;                                      
969                         }                                       
970                         else                                            
971                                 otherobj = base->object;                                        
972                         if(!otherobj)                                   
973                         {                                               
974                                 if(sds->coll_group)                                                     
975                                         go = go->next;                                          
976                                 else                                                    
977                                         base= base->next;                                               
978                                 continue;                                       
979                         }                       
980                         md = modifiers_findByType(otherobj, eModifierType_Smoke);
981                         
982                         // check for active smoke modifier
983                         if(md && md->mode & (eModifierMode_Realtime | eModifierMode_Render))                                    
984                         {
985                                 SmokeModifierData *smd2 = (SmokeModifierData *)md;
986
987                                 if((smd2->type & MOD_SMOKE_TYPE_COLL) && smd2->coll)
988                                 {
989                                         // we got nice collision object
990                                         SmokeCollSettings *scs = smd2->coll;
991                                         size_t i, j;
992                                         unsigned char *obstacles = smoke_get_obstacle(smd->domain->fluid);
993
994                                         for(i = 0; i < scs->numpoints; i++)
995                                         {
996                                                 int badcell = 0;
997                                                 size_t index = 0;
998                                                 int cell[3];
999
1000                                                 // 1. get corresponding cell
1001                                                 get_cell(smd->domain->p0, smd->domain->res, smd->domain->dx, &scs->points[3 * i], cell, 0);
1002                                         
1003                                                 // check if cell is valid (in the domain boundary)
1004                                                 for(j = 0; j < 3; j++)
1005                                                         if((cell[j] > sds->res[j] - 1) || (cell[j] < 0))
1006                                                         {
1007                                                                 badcell = 1;
1008                                                                 break;
1009                                                         }
1010                                                                                                                                 
1011                                                         if(badcell)                                                                     
1012                                                                 continue;
1013                                                 // 2. set cell values (heat, density and velocity)
1014                                                 index = smoke_get_index(cell[0], sds->res[0], cell[1], sds->res[1], cell[2]);
1015                                                                                                                 
1016                                                 // printf("cell[0]: %d, cell[1]: %d, cell[2]: %d\n", cell[0], cell[1], cell[2]);                                                                
1017                                                 // printf("res[0]: %d, res[1]: %d, res[2]: %d, index: %d\n\n", sds->res[0], sds->res[1], sds->res[2], index);                                                                                                                                   
1018                                                 obstacles[index] = 1;
1019                                                 // for moving gobstacles                                                                
1020                                                 /*
1021                                                 const LbmFloat maxVelVal = 0.1666;
1022                                                 const LbmFloat maxusqr = maxVelVal*maxVelVal*3. *1.5;
1023
1024                                                 LbmVec objvel = vec2L((mMOIVertices[n]-mMOIVerticesOld[n]) /dvec); 
1025                                                 {                                                               
1026                                                 const LbmFloat usqr = (objvel[0]*objvel[0]+objvel[1]*objvel[1]+objvel[2]*objvel[2])*1.5;                                                                
1027                                                 USQRMAXCHECK(usqr, objvel[0],objvel[1],objvel[2], mMaxVlen, mMxvx,mMxvy,mMxvz);                                                                 
1028                                                 if(usqr>maxusqr) {                                                                      
1029                                                 // cutoff at maxVelVal                                                                  
1030                                                 for(int jj=0; jj<3; jj++) {                                                                             
1031                                                 if(objvel[jj]>0.) objvel[jj] =  maxVelVal;                                                                              
1032                                                 if(objvel[jj]<0.) objvel[jj] = -maxVelVal;                                                                      
1033                                                 }                                                               
1034                                                 } 
1035                                                 }                                                               
1036                                                 const LbmFloat dp=dot(objvel, vec2L((*pNormals)[n]) );                                                          
1037                                                 const LbmVec oldov=objvel; // debug                                                             
1038                                                 objvel = vec2L((*pNormals)[n]) *dp;                                                             
1039                                                 */
1040                                         }
1041                                 }
1042                         }
1043
1044                         if(sds->coll_group)
1045                                 go = go->next;
1046                         else
1047                                 base= base->next;
1048                 }
1049         }
1050 }
1051 void smokeModifier_do(SmokeModifierData *smd, Scene *scene, Object *ob, DerivedMesh *dm, int useRenderParams, int isFinalCalc)
1052 {       
1053         if((smd->type & MOD_SMOKE_TYPE_FLOW))
1054         {
1055                 if(scene->r.cfra >= smd->time)
1056                         smokeModifier_init(smd, ob, scene, dm);
1057
1058                 if(scene->r.cfra > smd->time)
1059                 {
1060                         // XXX TODO
1061                         smd->time = scene->r.cfra;
1062
1063                         // rigid movement support
1064                         /*
1065                         Mat4CpyMat4(smd->flow->mat_old, smd->flow->mat);
1066                         Mat4CpyMat4(smd->flow->mat, ob->obmat);
1067                         */
1068                 }
1069                 else if(scene->r.cfra < smd->time)
1070                 {
1071                         smd->time = scene->r.cfra;
1072                         smokeModifier_reset(smd);
1073                 }
1074         }
1075         else if(smd->type & MOD_SMOKE_TYPE_COLL)
1076         {
1077                 if(scene->r.cfra >= smd->time)
1078                         smokeModifier_init(smd, ob, scene, dm);
1079
1080                 if(scene->r.cfra > smd->time)
1081                 {
1082                         // XXX TODO
1083                         smd->time = scene->r.cfra;
1084                         
1085                         if(smd->coll->dm)
1086                                 smd->coll->dm->release(smd->coll->dm);
1087
1088                         smd->coll->dm = CDDM_copy(dm);
1089
1090                         // rigid movement support
1091                         Mat4CpyMat4(smd->coll->mat_old, smd->coll->mat);
1092                         Mat4CpyMat4(smd->coll->mat, ob->obmat);
1093                 }
1094                 else if(scene->r.cfra < smd->time)
1095                 {
1096                         smd->time = scene->r.cfra;
1097                         smokeModifier_reset(smd);
1098                 }
1099         }
1100         else if(smd->type & MOD_SMOKE_TYPE_DOMAIN)
1101         {
1102                 SmokeDomainSettings *sds = smd->domain;
1103                 float light[3]; 
1104                 PointCache *cache = NULL;
1105                 PTCacheID pid;
1106                 PointCache *cache_wt = NULL;
1107                 PTCacheID pid_wt;
1108                 int startframe, endframe, framenr;
1109                 float timescale;
1110                 int cache_result = 0, cache_result_wt = 0;
1111
1112                 framenr = scene->r.cfra;
1113
1114                 printf("time: %d\n", scene->r.cfra);
1115
1116                 if(framenr == smd->time)
1117                         return;
1118
1119                 cache = sds->point_cache[0];
1120                 BKE_ptcache_id_from_smoke(&pid, ob, smd);
1121                 BKE_ptcache_id_time(&pid, scene, framenr, &startframe, &endframe, &timescale);
1122
1123                 cache_wt = sds->point_cache[1];
1124                 BKE_ptcache_id_from_smoke_turbulence(&pid_wt, ob, smd);
1125
1126                 if(framenr < startframe)
1127                         return;
1128
1129                 if(framenr > endframe)
1130                         return;
1131
1132                 if(!smd->domain->fluid)
1133                 {
1134                         BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_OUTDATED);
1135                         BKE_ptcache_id_reset(scene, &pid_wt, PTCACHE_RESET_OUTDATED);
1136                 }
1137
1138                 if(!smokeModifier_init(smd, ob, scene, dm))
1139                 {
1140                         printf("bad smokeModifier_init\n");
1141                         return;
1142                 }
1143
1144                 /* try to read from cache */
1145                 cache_result =  BKE_ptcache_read_cache(&pid, (float)framenr, scene->r.frs_sec);
1146                 printf("cache_result: %d\n", cache_result);
1147
1148                 if(cache_result == PTCACHE_READ_EXACT) 
1149                 {
1150                         cache->flag |= PTCACHE_SIMULATION_VALID;
1151                         cache->simframe= framenr;
1152
1153                         if(sds->wt)
1154                         {
1155                                 cache_result_wt = BKE_ptcache_read_cache(&pid_wt, (float)framenr, scene->r.frs_sec);
1156                                 
1157                                 if(cache_result_wt == PTCACHE_READ_EXACT) 
1158                                 {
1159                                         cache_wt->flag |= PTCACHE_SIMULATION_VALID;
1160                                         cache_wt->simframe= framenr;
1161                                 }
1162                         }
1163                         return;
1164                 }
1165
1166                 tstart();
1167
1168                 smoke_calc_domain(scene, ob, smd);
1169                 
1170                 // set new time
1171                 smd->time = scene->r.cfra;
1172
1173                 /* do simulation */
1174
1175                 // low res
1176                 cache->flag |= PTCACHE_SIMULATION_VALID;
1177                 cache->simframe= framenr;
1178
1179                 // simulate the actual smoke (c++ code in intern/smoke)
1180                 if(framenr!=startframe)
1181                         smoke_step(sds->fluid, smd->time);
1182
1183                 // create shadows before writing cache so we get nice shadows for sstartframe, too
1184                 if(get_lamp(scene, light))
1185                         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);
1186         
1187                 BKE_ptcache_write_cache(&pid, framenr);
1188
1189                 if(sds->wt)
1190                 {
1191                         if(framenr!=startframe)
1192                                 smoke_turbulence_step(sds->wt, sds->fluid);
1193
1194                         cache_wt->flag |= PTCACHE_SIMULATION_VALID;
1195                         cache_wt->simframe= framenr;
1196                         BKE_ptcache_write_cache(&pid_wt, framenr);
1197                 }
1198
1199                 tend();
1200                 printf ( "Frame: %d, Time: %f\n", (int)smd->time, ( float ) tval() );
1201         }
1202 }
1203
1204 static float calc_voxel_transp(float *result, float *input, int res[3], int *pixel, float *tRay, float correct)
1205 {
1206         const size_t index = smoke_get_index(pixel[0], res[0], pixel[1], res[1], pixel[2]);
1207
1208         // T_ray *= T_vox
1209         *tRay *= exp(input[index]*correct);
1210         
1211         if(result[index] < 0.0f)        
1212         {
1213 #pragma omp critical            
1214                 result[index] = *tRay;  
1215         }       
1216
1217         return *tRay;
1218 }
1219
1220 long long smoke_get_mem_req(int xres, int yres, int zres, int amplify)
1221 {
1222           int totalCells = xres * yres * zres;
1223           int amplifiedCells = totalCells * amplify * amplify * amplify;
1224
1225           // print out memory requirements
1226           long long int coarseSize = sizeof(float) * totalCells * 22 +
1227                            sizeof(unsigned char) * totalCells;
1228
1229           long long int fineSize = sizeof(float) * amplifiedCells * 7 + // big grids
1230                          sizeof(float) * totalCells * 8 +     // small grids
1231                          sizeof(float) * 128 * 128 * 128;     // noise tile
1232
1233           long long int totalMB = (coarseSize + fineSize) / (1024 * 1024);
1234
1235           return totalMB;
1236 }
1237
1238 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)
1239 {
1240     int dx, dy, dz, i, l, m, n, x_inc, y_inc, z_inc, err_1, err_2, dx2, dy2, dz2;
1241     int pixel[3];
1242
1243     pixel[0] = x1;
1244     pixel[1] = y1;
1245     pixel[2] = z1;
1246
1247     dx = x2 - x1;
1248     dy = y2 - y1;
1249     dz = z2 - z1;
1250
1251     x_inc = (dx < 0) ? -1 : 1;
1252     l = abs(dx);
1253     y_inc = (dy < 0) ? -1 : 1;
1254     m = abs(dy);
1255     z_inc = (dz < 0) ? -1 : 1;
1256     n = abs(dz);
1257     dx2 = l << 1;
1258     dy2 = m << 1;
1259     dz2 = n << 1;
1260
1261     if ((l >= m) && (l >= n)) {
1262         err_1 = dy2 - l;
1263         err_2 = dz2 - l;
1264         for (i = 0; i < l; i++) {
1265                 if(cb(result, input, res, pixel, tRay, correct) <= FLT_EPSILON)
1266                         break;
1267             if (err_1 > 0) {
1268                 pixel[1] += y_inc;
1269                 err_1 -= dx2;
1270             }
1271             if (err_2 > 0) {
1272                 pixel[2] += z_inc;
1273                 err_2 -= dx2;
1274             }
1275             err_1 += dy2;
1276             err_2 += dz2;
1277             pixel[0] += x_inc;
1278         }
1279     } else if ((m >= l) && (m >= n)) {
1280         err_1 = dx2 - m;
1281         err_2 = dz2 - m;
1282         for (i = 0; i < m; i++) {
1283                 if(cb(result, input, res, pixel, tRay, correct) <= FLT_EPSILON)
1284                         break;
1285             if (err_1 > 0) {
1286                 pixel[0] += x_inc;
1287                 err_1 -= dy2;
1288             }
1289             if (err_2 > 0) {
1290                 pixel[2] += z_inc;
1291                 err_2 -= dy2;
1292             }
1293             err_1 += dx2;
1294             err_2 += dz2;
1295             pixel[1] += y_inc;
1296         }
1297     } else {
1298         err_1 = dy2 - n;
1299         err_2 = dx2 - n;
1300         for (i = 0; i < n; i++) {
1301                 if(cb(result, input, res, pixel, tRay, correct) <= FLT_EPSILON)
1302                         break;
1303             if (err_1 > 0) {
1304                 pixel[1] += y_inc;
1305                 err_1 -= dz2;
1306             }
1307             if (err_2 > 0) {
1308                 pixel[0] += x_inc;
1309                 err_2 -= dz2;
1310             }
1311             err_1 += dy2;
1312             err_2 += dx2;
1313             pixel[2] += z_inc;
1314         }
1315     }
1316     cb(result, input, res, pixel, tRay, correct);
1317 }
1318
1319 static void get_cell(float *p0, int res[3], float dx, float *pos, int *cell, int correct)
1320 {
1321         float tmp[3];
1322
1323         VECSUB(tmp, pos, p0);
1324         VecMulf(tmp, 1.0 / dx);
1325
1326         if(correct)
1327         {
1328                 cell[0] = MIN2(res[0] - 1, MAX2(0, (int)floor(tmp[0])));
1329                 cell[1] = MIN2(res[1] - 1, MAX2(0, (int)floor(tmp[1])));
1330                 cell[2] = MIN2(res[2] - 1, MAX2(0, (int)floor(tmp[2])));
1331         }
1332         else
1333         {
1334                 cell[0] = (int)floor(tmp[0]);
1335                 cell[1] = (int)floor(tmp[1]);
1336                 cell[2] = (int)floor(tmp[2]);
1337         }
1338 }
1339
1340 void smoke_calc_transparency(float *result, float *input, float *p0, float *p1, int res[3], float dx, float *light, bresenham_callback cb, float correct)
1341 {
1342         int x, y, z;
1343         float bv[6];
1344
1345         memset(result, -1, sizeof(float)*res[0]*res[1]*res[2]); // x
1346         bv[0] = p0[0];
1347         bv[1] = p1[0];
1348         // y
1349         bv[2] = p0[1];
1350         bv[3] = p1[1];
1351         // z
1352         bv[4] = p0[2];
1353         bv[5] = p1[2];
1354
1355 #pragma omp parallel for schedule(static) private(y, z)
1356         for(x = 0; x < res[0]; x++)
1357                 for(y = 0; y < res[1]; y++)
1358                         for(z = 0; z < res[2]; z++)
1359                         {
1360                                 float voxelCenter[3];
1361                                 size_t index;
1362                                 float pos[3];
1363                                 int cell[3];
1364                                 float tRay = 1.0;
1365
1366                                 index = smoke_get_index(x, res[0], y, res[1], z);
1367
1368                                 if(result[index] >= 0.0f)                                       
1369                                         continue;                                                               
1370                                 voxelCenter[0] = p0[0] + dx *  x + dx * 0.5;
1371                                 voxelCenter[1] = p0[1] + dx *  y + dx * 0.5;
1372                                 voxelCenter[2] = p0[2] + dx *  z + dx * 0.5;
1373
1374                                 // get starting position (in voxel coords)
1375                                 if(BLI_bvhtree_bb_raycast(bv, light, voxelCenter, pos) > FLT_EPSILON)
1376                                 {
1377                                         // we're ouside
1378                                         get_cell(p0, res, dx, pos, cell, 1);
1379                                 }
1380                                 else
1381                                 {
1382                                         // we're inside
1383                                         get_cell(p0, res, dx, light, cell, 1);
1384                                 }
1385
1386                                 bresenham_linie_3D(cell[0], cell[1], cell[2], x, y, z, &tRay, cb, result, input, res, correct);
1387
1388                                 // convention -> from a RGBA float array, use G value for tRay
1389 // #pragma omp critical
1390                                 result[index] = tRay;                   
1391                         }
1392 }
1393