Blender V2.61 - r43446
|
00001 /* 00002 * ***** BEGIN GPL LICENSE BLOCK ***** 00003 * 00004 * This program is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU General Public License 00006 * as published by the Free Software Foundation; either version 2 00007 * of the License, or (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software Foundation, 00016 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00017 * 00018 * The Original Code is Copyright (C) 2009 by Daniel Genrich 00019 * All rights reserved. 00020 * 00021 * Contributor(s): Daniel Genrich 00022 * 00023 * ***** END GPL LICENSE BLOCK ***** 00024 */ 00025 00031 #include "FLUID_3D.h" 00032 #include "WTURBULENCE.h" 00033 00034 #include <stdio.h> 00035 #include <stdlib.h> 00036 #include <math.h> 00037 00038 // y in smoke is z in blender 00039 extern "C" FLUID_3D *smoke_init(int *res, float *p0) 00040 { 00041 // smoke lib uses y as top-bottom/vertical axis where blender uses z 00042 FLUID_3D *fluid = new FLUID_3D(res, p0); 00043 00044 // printf("xres: %d, yres: %d, zres: %d\n", res[0], res[1], res[2]); 00045 00046 return fluid; 00047 } 00048 00049 extern "C" WTURBULENCE *smoke_turbulence_init(int *res, int amplify, int noisetype) 00050 { 00051 // initialize wavelet turbulence 00052 if(amplify) 00053 return new WTURBULENCE(res[0],res[1],res[2], amplify, noisetype); 00054 else 00055 return NULL; 00056 } 00057 00058 extern "C" void smoke_free(FLUID_3D *fluid) 00059 { 00060 delete fluid; 00061 fluid = NULL; 00062 } 00063 00064 extern "C" void smoke_turbulence_free(WTURBULENCE *wt) 00065 { 00066 delete wt; 00067 wt = NULL; 00068 } 00069 00070 extern "C" size_t smoke_get_index(int x, int max_x, int y, int max_y, int z /*, int max_z */) 00071 { 00072 // // const int index = x + y * smd->res[0] + z * smd->res[0]*smd->res[1]; 00073 return x + y * max_x + z * max_x*max_y; 00074 } 00075 00076 extern "C" size_t smoke_get_index2d(int x, int max_x, int y /*, int max_y, int z, int max_z */) 00077 { 00078 return x + y * max_x; 00079 } 00080 00081 extern "C" void smoke_step(FLUID_3D *fluid, size_t framenr, float fps) 00082 { 00083 /* stability values copied from wturbulence.cpp */ 00084 const int maxSubSteps = 25; 00085 const float maxVel = 0.5f; /* TODO: maybe 0.5 is still too high, please confirm! -dg */ 00086 00087 float dt = DT_DEFAULT; 00088 float maxVelMag = 0.0f; 00089 int totalSubsteps; 00090 int substep = 0; 00091 float dtSubdiv; 00092 00093 /* get max velocity and lower the dt value if it is too high */ 00094 size_t size= fluid->_xRes * fluid->_yRes * fluid->_zRes; 00095 00096 for(size_t i = 0; i < size; i++) 00097 { 00098 float vtemp = (fluid->_xVelocity[i]*fluid->_xVelocity[i]+fluid->_yVelocity[i]*fluid->_yVelocity[i]+fluid->_zVelocity[i]*fluid->_zVelocity[i]); 00099 if(vtemp > maxVelMag) 00100 maxVelMag = vtemp; 00101 } 00102 00103 /* adapt timestep for different framerates, dt = 0.1 is at 25fps */ 00104 dt *= (25.0f / fps); 00105 00106 maxVelMag = sqrt(maxVelMag) * dt * (*(fluid->_dtFactor)); 00107 totalSubsteps = (int)((maxVelMag / maxVel) + 1.0f); /* always round up */ 00108 totalSubsteps = (totalSubsteps < 1) ? 1 : totalSubsteps; 00109 totalSubsteps = (totalSubsteps > maxSubSteps) ? maxSubSteps : totalSubsteps; 00110 dtSubdiv = (float)dt / (float)totalSubsteps; 00111 00112 // printf("totalSubsteps: %d, maxVelMag: %f, dt: %f\n", totalSubsteps, maxVelMag, dt); 00113 00114 for(substep = 0; substep < totalSubsteps; substep++) 00115 fluid->step(dtSubdiv); 00116 } 00117 00118 extern "C" void smoke_turbulence_step(WTURBULENCE *wt, FLUID_3D *fluid) 00119 { 00120 wt->stepTurbulenceFull(fluid->_dt/fluid->_dx, fluid->_xVelocity, fluid->_yVelocity, fluid->_zVelocity, fluid->_obstacles); 00121 } 00122 00123 extern "C" void smoke_initBlenderRNA(FLUID_3D *fluid, float *alpha, float *beta, float *dt_factor, float *vorticity, int *border_colli) 00124 { 00125 fluid->initBlenderRNA(alpha, beta, dt_factor, vorticity, border_colli); 00126 } 00127 00128 extern "C" void smoke_dissolve(FLUID_3D *fluid, int speed, int log) 00129 { 00130 float *density = fluid->_density; 00131 //float *densityOld = fluid->_densityOld; 00132 float *heat = fluid->_heat; 00133 00134 if(log) 00135 { 00136 /* max density/speed = dydx */ 00137 float dydx = 1.0 / (float)speed; 00138 size_t size= fluid->_xRes * fluid->_yRes * fluid->_zRes; 00139 00140 for(size_t i = 0; i < size; i++) 00141 { 00142 density[i] *= (1.0 - dydx); 00143 00144 if(density[i] < 0.0f) 00145 density[i] = 0.0f; 00146 00147 heat[i] *= (1.0 - dydx); 00148 00149 /*if(heat[i] < 0.0f) 00150 heat[i] = 0.0f;*/ 00151 } 00152 } 00153 else // linear falloff 00154 { 00155 /* max density/speed = dydx */ 00156 float dydx = 1.0 / (float)speed; 00157 size_t size= fluid->_xRes * fluid->_yRes * fluid->_zRes; 00158 00159 for(size_t i = 0; i < size; i++) 00160 { 00161 density[i] -= dydx; 00162 00163 if(density[i] < 0.0f) 00164 density[i] = 0.0f; 00165 00166 if(abs(heat[i]) < dydx) heat[i] = 0.0f; 00167 else if (heat[i]>0.0f) heat[i] -= dydx; 00168 else if (heat[i]<0.0f) heat[i] += dydx; 00169 00170 } 00171 } 00172 } 00173 00174 extern "C" void smoke_dissolve_wavelet(WTURBULENCE *wt, int speed, int log) 00175 { 00176 float *density = wt->getDensityBig(); 00177 Vec3Int r = wt->getResBig(); 00178 00179 if(log) 00180 { 00181 /* max density/speed = dydx */ 00182 float dydx = 1.0 / (float)speed; 00183 size_t size= r[0] * r[1] * r[2]; 00184 00185 for(size_t i = 0; i < size; i++) 00186 { 00187 density[i] *= (1.0 - dydx); 00188 00189 if(density[i] < 0.0f) 00190 density[i] = 0.0f; 00191 } 00192 } 00193 else // linear falloff 00194 { 00195 /* max density/speed = dydx */ 00196 float dydx = 1.0 / (float)speed; 00197 size_t size= r[0] * r[1] * r[2]; 00198 00199 for(size_t i = 0; i < size; i++) 00200 { 00201 density[i] -= dydx; 00202 00203 if(density[i] < 0.0f) 00204 density[i] = 0.0f; 00205 } 00206 } 00207 } 00208 00209 extern "C" void smoke_initWaveletBlenderRNA(WTURBULENCE *wt, float *strength) 00210 { 00211 wt->initBlenderRNA(strength); 00212 } 00213 00214 template < class T > inline T ABS( T a ) 00215 { 00216 return (0 < a) ? a : -a ; 00217 } 00218 00219 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) 00220 { 00221 *dens = fluid->_density; 00222 *densold = fluid->_densityOld; 00223 *heat = fluid->_heat; 00224 *heatold = fluid->_heatOld; 00225 *vx = fluid->_xVelocity; 00226 *vy = fluid->_yVelocity; 00227 *vz = fluid->_zVelocity; 00228 *vxold = fluid->_xVelocityOld; 00229 *vyold = fluid->_yVelocityOld; 00230 *vzold = fluid->_zVelocityOld; 00231 *obstacles = fluid->_obstacles; 00232 dt = &(fluid->_dt); 00233 dx = &(fluid->_dx); 00234 00235 } 00236 00237 extern "C" void smoke_turbulence_export(WTURBULENCE *wt, float **dens, float **densold, float **tcu, float **tcv, float **tcw) 00238 { 00239 if(!wt) 00240 return; 00241 00242 *dens = wt->_densityBig; 00243 *densold = wt->_densityBigOld; 00244 *tcu = wt->_tcU; 00245 *tcv = wt->_tcV; 00246 *tcw = wt->_tcW; 00247 } 00248 00249 extern "C" float *smoke_get_density(FLUID_3D *fluid) 00250 { 00251 return fluid->_density; 00252 } 00253 00254 extern "C" float *smoke_get_heat(FLUID_3D *fluid) 00255 { 00256 return fluid->_heat; 00257 } 00258 00259 extern "C" float *smoke_get_velocity_x(FLUID_3D *fluid) 00260 { 00261 return fluid->_xVelocity; 00262 } 00263 00264 extern "C" float *smoke_get_velocity_y(FLUID_3D *fluid) 00265 { 00266 return fluid->_yVelocity; 00267 } 00268 00269 extern "C" float *smoke_get_velocity_z(FLUID_3D *fluid) 00270 { 00271 return fluid->_zVelocity; 00272 } 00273 00274 extern "C" float *smoke_get_force_x(FLUID_3D *fluid) 00275 { 00276 return fluid->_xForce; 00277 } 00278 00279 extern "C" float *smoke_get_force_y(FLUID_3D *fluid) 00280 { 00281 return fluid->_yForce; 00282 } 00283 00284 extern "C" float *smoke_get_force_z(FLUID_3D *fluid) 00285 { 00286 return fluid->_zForce; 00287 } 00288 00289 extern "C" float *smoke_turbulence_get_density(WTURBULENCE *wt) 00290 { 00291 return wt ? wt->getDensityBig() : NULL; 00292 } 00293 00294 extern "C" void smoke_turbulence_get_res(WTURBULENCE *wt, int *res) 00295 { 00296 if(wt) 00297 { 00298 Vec3Int r = wt->getResBig(); 00299 res[0] = r[0]; 00300 res[1] = r[1]; 00301 res[2] = r[2]; 00302 } 00303 } 00304 00305 extern "C" unsigned char *smoke_get_obstacle(FLUID_3D *fluid) 00306 { 00307 return fluid->_obstacles; 00308 } 00309 00310 extern "C" void smoke_turbulence_set_noise(WTURBULENCE *wt, int type) 00311 { 00312 wt->setNoise(type); 00313 }