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