2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2009 by Daniel Genrich
19 * All rights reserved.
21 * Contributor(s): Daniel Genrich
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file smoke/intern/smoke_API.cpp
33 #include "WTURBULENCE.h"
39 // y in smoke is z in blender
40 extern "C" FLUID_3D *smoke_init(int *res, float *p0, float dtdef)
42 // smoke lib uses y as top-bottom/vertical axis where blender uses z
43 FLUID_3D *fluid = new FLUID_3D(res, p0, dtdef);
45 // printf("xres: %d, yres: %d, zres: %d\n", res[0], res[1], res[2]);
50 extern "C" WTURBULENCE *smoke_turbulence_init(int *res, int amplify, int noisetype)
52 // initialize wavelet turbulence
54 return new WTURBULENCE(res[0],res[1],res[2], amplify, noisetype);
59 extern "C" void smoke_free(FLUID_3D *fluid)
65 extern "C" void smoke_turbulence_free(WTURBULENCE *wt)
71 extern "C" size_t smoke_get_index(int x, int max_x, int y, int max_y, int z /*, int max_z */)
73 // // const int index = x + y * smd->res[0] + z * smd->res[0]*smd->res[1];
74 return x + y * max_x + z * max_x*max_y;
77 extern "C" size_t smoke_get_index2d(int x, int max_x, int y /*, int max_y, int z, int max_z */)
82 extern "C" void smoke_step(FLUID_3D *fluid, float dtSubdiv)
84 fluid->step(dtSubdiv);
87 extern "C" void smoke_turbulence_step(WTURBULENCE *wt, FLUID_3D *fluid)
89 wt->stepTurbulenceFull(fluid->_dt/fluid->_dx, fluid->_xVelocity, fluid->_yVelocity, fluid->_zVelocity, fluid->_obstacles);
92 extern "C" void smoke_initBlenderRNA(FLUID_3D *fluid, float *alpha, float *beta, float *dt_factor, float *vorticity, int *border_colli)
94 fluid->initBlenderRNA(alpha, beta, dt_factor, vorticity, border_colli);
97 extern "C" void smoke_dissolve(FLUID_3D *fluid, int speed, int log)
99 float *density = fluid->_density;
100 //float *densityOld = fluid->_densityOld;
101 float *heat = fluid->_heat;
105 /* max density/speed = dydx */
106 float dydx = 1.0 / (float)speed;
107 size_t size= fluid->_xRes * fluid->_yRes * fluid->_zRes;
109 for(size_t i = 0; i < size; i++)
111 density[i] *= (1.0 - dydx);
113 if(density[i] < 0.0f)
116 heat[i] *= (1.0 - dydx);
122 else // linear falloff
124 /* max density/speed = dydx */
125 float dydx = 1.0 / (float)speed;
126 size_t size= fluid->_xRes * fluid->_yRes * fluid->_zRes;
128 for(size_t i = 0; i < size; i++)
132 if(density[i] < 0.0f)
135 if(abs(heat[i]) < dydx) heat[i] = 0.0f;
136 else if (heat[i]>0.0f) heat[i] -= dydx;
137 else if (heat[i]<0.0f) heat[i] += dydx;
143 extern "C" void smoke_dissolve_wavelet(WTURBULENCE *wt, int speed, int log)
145 float *density = wt->getDensityBig();
146 Vec3Int r = wt->getResBig();
150 /* max density/speed = dydx */
151 float dydx = 1.0 / (float)speed;
152 size_t size= r[0] * r[1] * r[2];
154 for(size_t i = 0; i < size; i++)
156 density[i] *= (1.0 - dydx);
158 if(density[i] < 0.0f)
162 else // linear falloff
164 /* max density/speed = dydx */
165 float dydx = 1.0 / (float)speed;
166 size_t size= r[0] * r[1] * r[2];
168 for(size_t i = 0; i < size; i++)
172 if(density[i] < 0.0f)
178 extern "C" void smoke_initWaveletBlenderRNA(WTURBULENCE *wt, float *strength)
180 wt->initBlenderRNA(strength);
183 template < class T > inline T ABS( T a )
185 return (0 < a) ? a : -a ;
188 extern "C" void smoke_export(FLUID_3D *fluid, float *dt, float *dx, float **dens, float **densold, float **heat, float **heatold, float **vx, float **vy, float **vz, float **vxold, float **vyold, float **vzold, unsigned char **obstacles)
190 *dens = fluid->_density;
191 *densold = fluid->_densityOld;
192 *heat = fluid->_heat;
193 *heatold = fluid->_heatOld;
194 *vx = fluid->_xVelocity;
195 *vy = fluid->_yVelocity;
196 *vz = fluid->_zVelocity;
197 *vxold = fluid->_xVelocityOld;
198 *vyold = fluid->_yVelocityOld;
199 *vzold = fluid->_zVelocityOld;
200 *obstacles = fluid->_obstacles;
206 extern "C" void smoke_turbulence_export(WTURBULENCE *wt, float **dens, float **densold, float **tcu, float **tcv, float **tcw)
211 *dens = wt->_densityBig;
212 *densold = wt->_densityBigOld;
218 extern "C" float *smoke_get_density(FLUID_3D *fluid)
220 return fluid->_density;
223 extern "C" float *smoke_get_heat(FLUID_3D *fluid)
228 extern "C" float *smoke_get_velocity_x(FLUID_3D *fluid)
230 return fluid->_xVelocity;
233 extern "C" float *smoke_get_velocity_y(FLUID_3D *fluid)
235 return fluid->_yVelocity;
238 extern "C" float *smoke_get_velocity_z(FLUID_3D *fluid)
240 return fluid->_zVelocity;
243 extern "C" float *smoke_get_force_x(FLUID_3D *fluid)
245 return fluid->_xForce;
248 extern "C" float *smoke_get_force_y(FLUID_3D *fluid)
250 return fluid->_yForce;
253 extern "C" float *smoke_get_force_z(FLUID_3D *fluid)
255 return fluid->_zForce;
258 extern "C" float *smoke_turbulence_get_density(WTURBULENCE *wt)
260 return wt ? wt->getDensityBig() : NULL;
263 extern "C" void smoke_turbulence_get_res(WTURBULENCE *wt, int *res)
267 Vec3Int r = wt->getResBig();
274 extern "C" unsigned char *smoke_get_obstacle(FLUID_3D *fluid)
276 return fluid->_obstacles;
279 extern "C" void smoke_get_ob_velocity(FLUID_3D *fluid, float **x, float **y, float **z)
281 *x = fluid->_xVelocityOb;
282 *y = fluid->_yVelocityOb;
283 *z = fluid->_zVelocityOb;
286 extern "C" unsigned char *smoke_get_obstacle_anim(FLUID_3D *fluid)
288 return fluid->_obstaclesAnim;
291 extern "C" void smoke_turbulence_set_noise(WTURBULENCE *wt, int type)