2.5 - Animation Playback Tweaks
[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                                                                         int valid = 1;
773                                                                         size_t i = 0;
774                                                                         size_t index = 0;
775                                                                         
776                                                                         if(pa->alive == PARS_KILLED) continue;
777                                                                         else if(pa->alive == PARS_UNBORN && (part->flag & PART_UNBORN)==0) continue;
778                                                                         else if(pa->alive == PARS_DEAD && (part->flag & PART_DIED)==0) continue;
779                                                                         else if(pa->flag & (PARS_UNEXIST+PARS_NO_DISP)) continue;
780                                                                         
781                                                                         // VECCOPY(pos, pa->state.co);
782                                                                         // Mat4MulVecfl (ob->imat, pos);
783                                                                         
784                                                                         // 1. get corresponding cell
785                                                                         get_cell(smd, pa->state.co, cell, 0);
786                                                                 
787                                                                         // check if cell is valid (in the domain boundary)
788                                                                         for(i = 0; i < 3; i++)
789                                                                                 if((cell[i] > sds->res[i] - 1) || (cell[i] < 0))
790                                                                                         valid = 0;
791                                                                         
792                                                                         if(!valid)
793                                                                                 continue;
794                                                                         
795                                                                         // 2. set cell values (heat, density and velocity)
796                                                                         index = smoke_get_index(cell[0], sds->res[0], cell[1], sds->res[1], cell[2]);
797                                                                         
798                                                                         heat[index] = sfs->temp;
799                                                                         density[index] = sfs->density;
800                                                                         velocity_x[index] = pa->state.vel[0];
801                                                                         velocity_y[index] = pa->state.vel[1];
802                                                                         velocity_z[index] = pa->state.vel[2];
803
804                                                                         // we need different handling for the high-res feature
805                                                                         if(bigdensity)
806                                                                         {
807                                                                                 // init all surrounding cells according to amplification, too
808                                                                                 int i, j, k;
809                                                                                 for(i = 0; i < smd->domain->amplify; i++)
810                                                                                         for(j = 0; j < smd->domain->amplify; j++)
811                                                                                                 for(k = 0; k < smd->domain->amplify; k++)
812                                                                                                 {
813                                                                                                         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);
814                                                                                                         bigdensity[index] = sfs->density;
815                                                                                                 }
816                                                                         }
817                                                                 }
818                                                         }       
819                                                 }       
820                                         }
821
822                                         if(cnt_domain && !sds->fluid_group)
823                                                 go = go->next;
824                                         else
825                                                 base= base->next;
826                                 }
827                         }
828
829                         // do effectors
830                         /*
831                         if(sds->eff_group)
832                         {
833                                 for(go = sds->eff_group->gobject.first; go; go = go->next) 
834                                 {
835                                         if(go->ob)
836                                         {
837                                                 if(ob->pd)
838                                                 {
839                                                         
840                                                 }
841                                         }
842                                 }
843                         }
844                         */
845
846                         // do collisions        
847                         if(sds->coll_group || !cnt_domain)
848                         {
849                                 Object *otherobj = NULL;
850                                 ModifierData *md = NULL;
851
852                                 if(cnt_domain && !sds->coll_group) // we use groups since we have 2 domains
853                                         go = sds->coll_group->gobject.first;
854                                 else
855                                         base = scene->base.first;
856
857                                 while(base || go)
858                                 {
859                                         otherobj = NULL;
860
861                                         if(cnt_domain && !sds->coll_group) 
862                                         {
863                                                 if(go->ob)
864                                                         otherobj = go->ob;
865                                         }
866                                         else
867                                                 otherobj = base->object;
868
869                                         if(!otherobj)
870                                         {
871                                                 if(cnt_domain && !sds->coll_group)
872                                                         go = go->next;
873                                                 else
874                                                         base= base->next;
875
876                                                 continue;
877                                         }
878                         
879                                         md = modifiers_findByType(otherobj, eModifierType_Smoke);
880                                         
881                                         // check for active smoke modifier
882                                         if(md && md->mode & (eModifierMode_Realtime | eModifierMode_Render))
883                                         {
884                                                 SmokeModifierData *smd2 = (SmokeModifierData *)md;
885
886                                                 if((smd2->type & MOD_SMOKE_TYPE_COLL) && smd2->coll)
887                                                 {
888                                                         // we got nice collision object
889                                                         SmokeCollSettings *scs = smd2->coll;
890                                                         int cell[3];
891                                                         int valid = 1;
892                                                         size_t index = 0;
893                                                         size_t i, j;
894                                                         unsigned char *obstacles = smoke_get_obstacle(smd->domain->fluid);
895
896                                                         for(i = 0; i < scs->numpoints; i++)
897                                                         {
898                                                                 // 1. get corresponding cell
899                                                                 get_cell(smd, &scs->points[3 * i], cell, 0);
900                                                         
901                                                                 // check if cell is valid (in the domain boundary)
902                                                                 for(j = 0; j < 3; j++)
903                                                                         if((cell[j] > sds->res[j] - 1) || (cell[j] < 0))
904                                                                                 valid = 0;
905                                                                 
906                                                                 if(!valid)
907                                                                         continue;
908                                                                 
909                                                                 // 2. set cell values (heat, density and velocity)
910                                                                 index = smoke_get_index(cell[0], sds->res[0], cell[1], sds->res[1], cell[2]);
911                                                                         
912                                                                 obstacles[index] = 1;
913
914                                                                 // for moving gobstacles
915                                                                 /*
916                                                                 const LbmFloat maxVelVal = 0.1666;
917                                                                 const LbmFloat maxusqr = maxVelVal*maxVelVal*3. *1.5;
918
919                                                                 LbmVec objvel = vec2L((mMOIVertices[n]-mMOIVerticesOld[n]) /dvec); { 
920                                                                 const LbmFloat usqr = (objvel[0]*objvel[0]+objvel[1]*objvel[1]+objvel[2]*objvel[2])*1.5; 
921                                                                 USQRMAXCHECK(usqr, objvel[0],objvel[1],objvel[2], mMaxVlen, mMxvx,mMxvy,mMxvz); 
922                                                                 if(usqr>maxusqr) { 
923                                                                         // cutoff at maxVelVal 
924                                                                         for(int jj=0; jj<3; jj++) { 
925                                                                                 if(objvel[jj]>0.) objvel[jj] =  maxVelVal;  
926                                                                                 if(objvel[jj]<0.) objvel[jj] = -maxVelVal; 
927                                                                         } 
928                                                                 } } 
929
930                                                                 const LbmFloat dp=dot(objvel, vec2L((*pNormals)[n]) ); 
931                                                                 const LbmVec oldov=objvel; // debug
932                                                                 objvel = vec2L((*pNormals)[n]) *dp;
933                                                                 */
934                                                         }
935                                                 }
936                                         }
937
938                                         if(cnt_domain && !sds->coll_group)
939                                                 go = go->next;
940                                         else
941                                                 base= base->next;
942                                 }
943                         }
944                         
945                         // set new time
946                         smd->time = scene->r.cfra;
947
948                         // simulate the actual smoke (c++ code in intern/smoke)
949                         smoke_step(sds->fluid);
950
951                         tend();
952                         printf ( "Frame: %d, Time: %f\n", (int)smd->time, ( float ) tval() );
953                 }
954                 else if(scene->r.cfra < smd->time)
955                 {
956                         // we got back in time, reset smoke in this case (TODO: use cache later)
957                         smd->time = scene->r.cfra;
958                         smokeModifier_reset(smd);
959                 }
960         }
961 }
962
963 // update necessary information for 3dview
964 void smoke_prepare_View(SmokeModifierData *smd, float *light)
965 {
966         float *density = NULL;
967         int x, y, z;
968
969         if(!smd->domain->tray)
970         {
971                 // TRay is for self shadowing
972                 smd->domain->tray = MEM_callocN(sizeof(float)*smd->domain->res[0]*smd->domain->res[1]*smd->domain->res[2], "Smoke_tRay");
973         }
974         if(!smd->domain->tvox)
975         {
976                 // TVox is for tranaparency
977                 smd->domain->tvox = MEM_callocN(sizeof(float)*smd->domain->res[0]*smd->domain->res[1]*smd->domain->res[2], "Smoke_tVox");
978         }
979
980         // update 3dview
981         density = smoke_get_density(smd->domain->fluid);
982         for(x = 0; x < smd->domain->res[0]; x++)
983                         for(y = 0; y < smd->domain->res[1]; y++)
984                                 for(z = 0; z < smd->domain->res[2]; z++)
985                                 {
986                                         size_t index;
987
988                                         index = smoke_get_index(x, smd->domain->res[0], y, smd->domain->res[1], z);
989                                         // Transparency computation
990                                         // formula taken from "Visual Simulation of Smoke" / Fedkiw et al. pg. 4
991                                         // T_vox = exp(-C_ext * h)
992                                         // C_ext/sigma_t = density * C_ext
993                                         smoke_set_tvox(smd, index, exp(-density[index] * 4.0 * smd->domain->dx));
994         }
995         smoke_calc_transparency(smd, light, 0);
996 }
997
998 // update necessary information for 3dview ("high res" option)
999 void smoke_prepare_bigView(SmokeModifierData *smd, float *light)
1000 {
1001         float *density = NULL;
1002         size_t i = 0;
1003         int bigres[3];
1004
1005         smoke_get_bigres(smd->domain->fluid, bigres);
1006
1007         if(!smd->domain->traybig)
1008         {
1009                 // TRay is for self shadowing
1010                 smd->domain->traybig = MEM_callocN(sizeof(float)*bigres[0]*bigres[1]*bigres[2], "Smoke_tRayBig");
1011         }
1012         if(!smd->domain->tvoxbig)
1013         {
1014                 // TVox is for tranaparency
1015                 smd->domain->tvoxbig = MEM_callocN(sizeof(float)*bigres[0]*bigres[1]*bigres[2], "Smoke_tVoxBig");
1016         }
1017
1018         density = smoke_get_bigdensity(smd->domain->fluid);
1019         for (i = 0; i < bigres[0] * bigres[1] * bigres[2]; i++)
1020         {
1021                 // Transparency computation
1022                 // formula taken from "Visual Simulation of Smoke" / Fedkiw et al. pg. 4
1023                 // T_vox = exp(-C_ext * h)
1024                 // C_ext/sigma_t = density * C_ext
1025                 smoke_set_bigtvox(smd, i, exp(-density[i] * 4.0 * smd->domain->dx / smd->domain->amplify) );
1026         }
1027         smoke_calc_transparency(smd, light, 1);
1028 }
1029
1030
1031 float smoke_get_tvox(SmokeModifierData *smd, size_t index)
1032 {
1033         return smd->domain->tvox[index];
1034 }
1035
1036 void smoke_set_tvox(SmokeModifierData *smd, size_t index, float tvox)
1037 {
1038         smd->domain->tvox[index] = tvox;
1039 }
1040
1041 float smoke_get_tray(SmokeModifierData *smd, size_t index)
1042 {
1043         return smd->domain->tray[index];
1044 }
1045
1046 void smoke_set_tray(SmokeModifierData *smd, size_t index, float transparency)
1047 {
1048         smd->domain->tray[index] = transparency;
1049 }
1050
1051 float smoke_get_bigtvox(SmokeModifierData *smd, size_t index)
1052 {
1053         return smd->domain->tvoxbig[index];
1054 }
1055
1056 void smoke_set_bigtvox(SmokeModifierData *smd, size_t index, float tvox)
1057 {
1058         smd->domain->tvoxbig[index] = tvox;
1059 }
1060
1061 float smoke_get_bigtray(SmokeModifierData *smd, size_t index)
1062 {
1063         return smd->domain->traybig[index];
1064 }
1065
1066 void smoke_set_bigtray(SmokeModifierData *smd, size_t index, float transparency)
1067 {
1068         smd->domain->traybig[index] = transparency;
1069 }
1070
1071 long long smoke_get_mem_req(int xres, int yres, int zres, int amplify)
1072 {
1073           int totalCells = xres * yres * zres;
1074           int amplifiedCells = totalCells * amplify * amplify * amplify;
1075
1076           // print out memory requirements
1077           long long int coarseSize = sizeof(float) * totalCells * 22 +
1078                            sizeof(unsigned char) * totalCells;
1079
1080           long long int fineSize = sizeof(float) * amplifiedCells * 7 + // big grids
1081                          sizeof(float) * totalCells * 8 +     // small grids
1082                          sizeof(float) * 128 * 128 * 128;     // noise tile
1083
1084           long long int totalMB = (coarseSize + fineSize) / (1024 * 1024);
1085
1086           return totalMB;
1087 }
1088
1089
1090 static void calc_voxel_transp(SmokeModifierData *smd, int *pixel, float *tRay)
1091 {
1092         // printf("Pixel(%d, %d, %d)\n", pixel[0], pixel[1], pixel[2]);
1093         const size_t index = smoke_get_index(pixel[0], smd->domain->res[0], pixel[1], smd->domain->res[1], pixel[2]);
1094
1095         // T_ray *= T_vox
1096         *tRay *= smoke_get_tvox(smd, index);
1097 }
1098
1099 static void calc_voxel_transp_big(SmokeModifierData *smd, int *pixel, float *tRay)
1100 {
1101         int bigres[3];
1102         size_t index;
1103
1104         smoke_get_bigres(smd->domain->fluid, bigres);
1105         index = smoke_get_index(pixel[0], bigres[0], pixel[1], bigres[1], pixel[2]);
1106
1107         /*
1108         if(index > bigres[0]*bigres[1]*bigres[2])
1109                 printf("pixel[0]: %d, [1]: %d, [2]: %d\n", pixel[0], pixel[1], pixel[2]);
1110         */
1111
1112         // T_ray *= T_vox
1113         *tRay *= smoke_get_bigtvox(smd, index);
1114 }
1115
1116 static void bresenham_linie_3D(SmokeModifierData *smd, int x1, int y1, int z1, int x2, int y2, int z2, float *tRay, int big)
1117 {
1118     int dx, dy, dz, i, l, m, n, x_inc, y_inc, z_inc, err_1, err_2, dx2, dy2, dz2;
1119     int pixel[3];
1120
1121     pixel[0] = x1;
1122     pixel[1] = y1;
1123     pixel[2] = z1;
1124
1125     dx = x2 - x1;
1126     dy = y2 - y1;
1127     dz = z2 - z1;
1128
1129     x_inc = (dx < 0) ? -1 : 1;
1130     l = abs(dx);
1131     y_inc = (dy < 0) ? -1 : 1;
1132     m = abs(dy);
1133     z_inc = (dz < 0) ? -1 : 1;
1134     n = abs(dz);
1135     dx2 = l << 1;
1136     dy2 = m << 1;
1137     dz2 = n << 1;
1138
1139     if ((l >= m) && (l >= n)) {
1140         err_1 = dy2 - l;
1141         err_2 = dz2 - l;
1142         for (i = 0; i < l; i++) {
1143                 if(!big)
1144                                 calc_voxel_transp(smd, pixel, tRay);
1145                         else
1146                                 calc_voxel_transp_big(smd, pixel, tRay);
1147                 if(*tRay < 0.0f)
1148                         return;
1149             if (err_1 > 0) {
1150                 pixel[1] += y_inc;
1151                 err_1 -= dx2;
1152             }
1153             if (err_2 > 0) {
1154                 pixel[2] += z_inc;
1155                 err_2 -= dx2;
1156             }
1157             err_1 += dy2;
1158             err_2 += dz2;
1159             pixel[0] += x_inc;
1160         }
1161     } else if ((m >= l) && (m >= n)) {
1162         err_1 = dx2 - m;
1163         err_2 = dz2 - m;
1164         for (i = 0; i < m; i++) {
1165                 if(!big)
1166                                 calc_voxel_transp(smd, pixel, tRay);
1167                         else
1168                                 calc_voxel_transp_big(smd, pixel, tRay);
1169                 if(*tRay < 0.0f)
1170                         return;
1171             if (err_1 > 0) {
1172                 pixel[0] += x_inc;
1173                 err_1 -= dy2;
1174             }
1175             if (err_2 > 0) {
1176                 pixel[2] += z_inc;
1177                 err_2 -= dy2;
1178             }
1179             err_1 += dx2;
1180             err_2 += dz2;
1181             pixel[1] += y_inc;
1182         }
1183     } else {
1184         err_1 = dy2 - n;
1185         err_2 = dx2 - n;
1186         for (i = 0; i < n; i++) {
1187                 if(!big)
1188                                 calc_voxel_transp(smd, pixel, tRay);
1189                         else
1190                                 calc_voxel_transp_big(smd, pixel, tRay);
1191                 if(*tRay < 0.0f)
1192                         return;
1193             if (err_1 > 0) {
1194                 pixel[1] += y_inc;
1195                 err_1 -= dz2;
1196             }
1197             if (err_2 > 0) {
1198                 pixel[0] += x_inc;
1199                 err_2 -= dz2;
1200             }
1201             err_1 += dy2;
1202             err_2 += dx2;
1203             pixel[2] += z_inc;
1204         }
1205     }
1206     if(!big)
1207         calc_voxel_transp(smd, pixel, tRay);
1208     else
1209         calc_voxel_transp_big(smd, pixel, tRay);
1210 }
1211
1212 static void get_cell(struct SmokeModifierData *smd, float *pos, int *cell, int correct)
1213 {
1214         float tmp[3];
1215
1216         VECSUB(tmp, pos, smd->domain->p0);
1217         VecMulf(tmp, 1.0 / smd->domain->dx);
1218
1219         if(correct)
1220         {
1221                 cell[0] = MIN2(smd->domain->res[0] - 1, MAX2(0, (int)(tmp[0] + 0.5)));
1222                 cell[1] = MIN2(smd->domain->res[1] - 1, MAX2(0, (int)(tmp[1] + 0.5)));
1223                 cell[2] = MIN2(smd->domain->res[2] - 1, MAX2(0, (int)(tmp[2] + 0.5)));
1224         }
1225         else
1226         {
1227                 cell[0] = (int)(tmp[0] + 0.5);
1228                 cell[1] = (int)(tmp[1] + 0.5);
1229                 cell[2] = (int)(tmp[2] + 0.5);
1230         }
1231 }
1232 static void get_bigcell(struct SmokeModifierData *smd, float *pos, int *cell, int correct)
1233 {
1234         float tmp[3];
1235         int res[3];
1236         smoke_get_bigres(smd->domain->fluid, res);
1237
1238         VECSUB(tmp, pos, smd->domain->p0);
1239
1240         VecMulf(tmp, smd->domain->amplify / smd->domain->dx );
1241
1242         if(correct)
1243         {
1244                 cell[0] = MIN2(res[0] - 1, MAX2(0, (int)(tmp[0] + 0.5)));
1245                 cell[1] = MIN2(res[1] - 1, MAX2(0, (int)(tmp[1] + 0.5)));
1246                 cell[2] = MIN2(res[2] - 1, MAX2(0, (int)(tmp[2] + 0.5)));
1247         }
1248         else
1249         {
1250                 cell[0] = (int)(tmp[0] + 0.5);
1251                 cell[1] = (int)(tmp[1] + 0.5);
1252                 cell[2] = (int)(tmp[2] + 0.5);
1253         }
1254 }
1255
1256
1257 void smoke_calc_transparency(struct SmokeModifierData *smd, float *light, int big)
1258 {
1259         int x, y, z;
1260         float bv[6];
1261         int res[3];
1262         float bigfactor = 1.0;
1263
1264         // x
1265         bv[0] = smd->domain->p0[0];
1266         bv[1] = smd->domain->p1[0];
1267         // y
1268         bv[2] = smd->domain->p0[1];
1269         bv[3] = smd->domain->p1[1];
1270         // z
1271         bv[4] = smd->domain->p0[2];
1272         bv[5] = smd->domain->p1[2];
1273 /*
1274         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]);
1275
1276         printf("p0[0]: %f, p0[1]: %f, p0[2]: %f\n", smd->domain->p0[0], smd->domain->p0[1], smd->domain->p0[2]);
1277         printf("p1[0]: %f, p1[1]: %f, p1[2]: %f\n", smd->domain->p1[0], smd->domain->p1[1], smd->domain->p1[2]);
1278         printf("dx: %f, amp: %d\n", smd->domain->dx, smd->domain->amplify);
1279 */
1280         if(!big)
1281         {
1282                 res[0] = smd->domain->res[0];
1283                 res[1] = smd->domain->res[1];
1284                 res[2] = smd->domain->res[2];
1285         }
1286         else
1287         {
1288                 smoke_get_bigres(smd->domain->fluid, res);
1289                 bigfactor = 1.0 / smd->domain->amplify;
1290         }
1291
1292 #pragma omp parallel for schedule(static) private(y, z) shared(big, smd, light, res, bigfactor)
1293         for(x = 0; x < res[0]; x++)
1294                 for(y = 0; y < res[1]; y++)
1295                         for(z = 0; z < res[2]; z++)
1296                         {
1297                                 float voxelCenter[3];
1298                                 size_t index;
1299                                 float pos[3];
1300                                 int cell[3];
1301                                 float tRay = 1.0;
1302
1303                                 index = smoke_get_index(x, res[0], y, res[1], z);
1304
1305                                 // voxelCenter = m_voxelarray[i].GetCenter();
1306                                 voxelCenter[0] = smd->domain->p0[0] + smd->domain->dx * bigfactor * x + smd->domain->dx * bigfactor * 0.5;
1307                                 voxelCenter[1] = smd->domain->p0[1] + smd->domain->dx * bigfactor * y + smd->domain->dx * bigfactor * 0.5;
1308                                 voxelCenter[2] = smd->domain->p0[2] + smd->domain->dx * bigfactor * z + smd->domain->dx * bigfactor * 0.5;
1309
1310                                 // printf("vc[0]: %f, vc[1]: %f, vc[2]: %f\n", voxelCenter[0], voxelCenter[1], voxelCenter[2]);
1311                                 // printf("light[0]: %f, light[1]: %f, light[2]: %f\n", light[0], light[1], light[2]);
1312
1313                                 // get starting position (in voxel coords)
1314                                 if(BLI_bvhtree_bb_raycast(bv, light, voxelCenter, pos) > FLT_EPSILON)
1315                                 {
1316                                         // we're ouside
1317                                         // printf("out: pos[0]: %f, pos[1]: %f, pos[2]: %f\n", pos[0], pos[1], pos[2]);
1318                                         if(!big)
1319                                                 get_cell(smd, pos, cell, 1);
1320                                         else
1321                                                 get_bigcell(smd, pos, cell, 1);
1322                                 }
1323                                 else
1324                                 {
1325                                         // printf("in: pos[0]: %f, pos[1]: %f, pos[2]: %f\n", light[0], light[1], light[2]);
1326                                         // we're inside
1327                                         if(!big)
1328                                                 get_cell(smd, light, cell, 1);
1329                                         else
1330                                                 get_bigcell(smd, light, cell, 1);
1331                                 }
1332
1333                                 // printf("cell - [0]: %d, [1]: %d, [2]: %d\n", cell[0], cell[1], cell[2]);
1334                                 bresenham_linie_3D(smd, cell[0], cell[1], cell[2], x, y, z, &tRay, big);
1335
1336                                 if(!big)
1337                                         smoke_set_tray(smd, index, tRay);
1338                                 else
1339                                         smoke_set_bigtray(smd, index, tRay);
1340                         }
1341 }
1342