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