3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): Raul Fernandez Hernandez (Farsthary), Matt Ebb.
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/render/intern/source/voxeldata.c
38 #include "MEM_guardedalloc.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_voxel.h"
43 #include "BLI_utildefines.h"
45 #include "IMB_imbuf.h"
46 #include "IMB_imbuf_types.h"
48 #include "BKE_global.h"
49 #include "BKE_image.h"
51 #include "BKE_modifier.h"
53 #include "smoke_API.h"
55 #include "DNA_texture_types.h"
56 #include "DNA_object_force.h"
57 #include "DNA_object_types.h"
58 #include "DNA_modifier_types.h"
59 #include "DNA_smoke_types.h"
62 #include "render_types.h"
63 #include "renderdatabase.h"
65 #include "voxeldata.h"
67 static int is_vd_res_ok(VoxelData *vd)
69 /* arbitrary large value so corrupt headers dont break */
70 const int min= 1, max= 100000;
71 return (vd->resol[0] >= min && vd->resol[0] <= max) &&
72 (vd->resol[1] >= min && vd->resol[1] <= max) &&
73 (vd->resol[2] >= min && vd->resol[2] <= max);
76 /* use size_t because the result may exceed INT_MAX */
77 static size_t vd_resol_size(VoxelData *vd)
79 return (size_t)vd->resol[0] * (size_t)vd->resol[1] * (size_t)vd->resol[2];
82 static int load_frame_blendervoxel(VoxelData *vd, FILE *fp, int frame)
84 const size_t size = vd_resol_size(vd);
85 size_t offset = sizeof(VoxelDataHeader);
87 if(is_vd_res_ok(vd) == FALSE)
90 vd->dataset = MEM_mapallocN(sizeof(float)*size, "voxel dataset");
91 if(vd->dataset == NULL) return 0;
93 if(fseek(fp, frame*size*sizeof(float)+offset, 0) == -1)
95 if(fread(vd->dataset, sizeof(float), size, fp) != size)
98 vd->cachedframe = frame;
103 static int load_frame_raw8(VoxelData *vd, FILE *fp, int frame)
105 const size_t size = vd_resol_size(vd);
109 if(is_vd_res_ok(vd) == FALSE)
112 vd->dataset = MEM_mapallocN(sizeof(float)*size, "voxel dataset");
113 if(vd->dataset == NULL) return 0;
114 data_c = (char *)MEM_mallocN(sizeof(char)*size, "temporary voxel file reading storage");
116 MEM_freeN(vd->dataset);
121 if(fseek(fp,(frame-1)*size*sizeof(char),0) == -1) {
123 MEM_freeN(vd->dataset);
127 if(fread(data_c, sizeof(char), size, fp) != size) {
129 MEM_freeN(vd->dataset);
134 for (i=0; i<size; i++) {
135 vd->dataset[i] = (float)data_c[i] / 255.f;
139 vd->cachedframe = frame;
144 static void load_frame_image_sequence(VoxelData *vd, Tex *tex)
147 Image *ima = tex->ima;
148 ImageUser *tiuser = &tex->iuser;
149 ImageUser iuser = *(tiuser);
153 if (!ima || !tiuser) return;
154 if (iuser.frames == 0) return;
156 ima->source = IMA_SRC_SEQUENCE;
157 iuser.framenr = 1 + iuser.offset;
159 /* find the first valid ibuf and use it to initialise the resolution of the data set */
160 /* need to do this in advance so we know how much memory to allocate */
161 ibuf= BKE_image_get_ibuf(ima, &iuser);
162 while (!ibuf && (iuser.framenr < iuser.frames)) {
164 ibuf= BKE_image_get_ibuf(ima, &iuser);
167 if (!ibuf->rect_float) IMB_float_from_rect(ibuf);
169 vd->flag |= TEX_VD_STILL;
170 vd->resol[0] = ibuf->x;
171 vd->resol[1] = ibuf->y;
172 vd->resol[2] = iuser.frames;
173 vd->dataset = MEM_mapallocN(sizeof(float)*vd_resol_size(vd), "voxel dataset");
175 for (z=0; z < iuser.frames; z++)
177 /* get a new ibuf for each frame */
180 ibuf= BKE_image_get_ibuf(ima, &iuser);
182 if (!ibuf->rect_float) IMB_float_from_rect(ibuf);
184 rf = ibuf->rect_float;
186 for (y=0; y < ibuf->y; y++)
188 for (x=0; x < ibuf->x; x++)
190 /* currently averaged to monchrome */
191 vd->dataset[ V_I(x, y, z, vd->resol) ] = (rf[0] + rf[1] + rf[2])*0.333f;
196 BKE_image_free_anim_ibufs(ima, iuser.framenr);
203 static int read_voxeldata_header(FILE *fp, struct VoxelData *vd)
205 VoxelDataHeader *h=(VoxelDataHeader *)MEM_mallocN(sizeof(VoxelDataHeader), "voxel data header");
208 if(fread(h,sizeof(VoxelDataHeader),1,fp) != 1) {
213 vd->resol[0]=h->resolX;
214 vd->resol[1]=h->resolY;
215 vd->resol[2]=h->resolZ;
221 static void init_frame_smoke(VoxelData *vd, float cfra)
227 if (vd->object == NULL) return;
230 /* draw code for smoke */
231 if( (md = (ModifierData *)modifiers_findByType(ob, eModifierType_Smoke)) )
233 SmokeModifierData *smd = (SmokeModifierData *)md;
236 if(smd->domain && smd->domain->fluid) {
237 if(cfra < smd->domain->point_cache[0]->startframe)
238 ; /* don't show smoke before simulation starts, this could be made an option in the future */
239 else if (vd->smoked_type == TEX_VD_SMOKEHEAT) {
244 VECCOPY(vd->resol, smd->domain->res);
245 totRes= vd_resol_size(vd);
247 // scaling heat values from -2.0-2.0 to 0.0-1.0
248 vd->dataset = MEM_mapallocN(sizeof(float)*(totRes), "smoke data");
251 heat = smoke_get_heat(smd->domain->fluid);
253 for (i=0; i<totRes; i++)
255 vd->dataset[i] = (heat[i]+2.0f)/4.0f;
258 //vd->dataset = smoke_get_heat(smd->domain->fluid);
260 else if (vd->smoked_type == TEX_VD_SMOKEVEL) {
263 float *xvel, *yvel, *zvel;
265 VECCOPY(vd->resol, smd->domain->res);
266 totRes= vd_resol_size(vd);
268 // scaling heat values from -2.0-2.0 to 0.0-1.0
269 vd->dataset = MEM_mapallocN(sizeof(float)*(totRes), "smoke data");
271 xvel = smoke_get_velocity_x(smd->domain->fluid);
272 yvel = smoke_get_velocity_y(smd->domain->fluid);
273 zvel = smoke_get_velocity_z(smd->domain->fluid);
275 for (i=0; i<totRes; i++)
277 vd->dataset[i] = sqrt(xvel[i]*xvel[i] + yvel[i]*yvel[i] + zvel[i]*zvel[i])*3.0f;
285 if (smd->domain->flags & MOD_SMOKE_HIGHRES) {
286 smoke_turbulence_get_res(smd->domain->wt, vd->resol);
287 density = smoke_turbulence_get_density(smd->domain->wt);
289 VECCOPY(vd->resol, smd->domain->res);
290 density = smoke_get_density(smd->domain->fluid);
293 /* TODO: is_vd_res_ok(rvd) doesnt check this resolution */
294 totRes= vd_resol_size(vd);
295 /* always store copy, as smoke internal data can change */
296 vd->dataset = MEM_mapallocN(sizeof(float)*(totRes), "smoke data");
297 memcpy(vd->dataset, density, sizeof(float)*totRes);
298 } // end of fluid condition
306 static void cache_voxeldata(struct Render *re, Tex *tex)
308 VoxelData *vd = tex->vd;
311 char path[sizeof(vd->source_path)];
313 /* only re-cache if dataset needs updating */
314 if ((vd->flag & TEX_VD_STILL) || (vd->cachedframe == re->r.cfra))
317 /* clear out old cache, ready for new */
319 MEM_freeN(vd->dataset);
323 if (vd->flag & TEX_VD_STILL)
324 curframe = vd->still_frame;
326 curframe = re->r.cfra;
328 BLI_strncpy(path, vd->source_path, sizeof(path));
330 switch(vd->file_format) {
331 case TEX_VD_IMAGE_SEQUENCE:
332 load_frame_image_sequence(vd, tex);
335 init_frame_smoke(vd, re->r.cfra);
337 case TEX_VD_BLENDERVOXEL:
338 BLI_path_abs(path, G.main->name);
339 if (!BLI_exists(path)) return;
340 fp = fopen(path,"rb");
343 if(read_voxeldata_header(fp, vd))
344 load_frame_blendervoxel(vd, fp, curframe-1);
348 case TEX_VD_RAW_8BIT:
349 BLI_path_abs(path, G.main->name);
350 if (!BLI_exists(path)) return;
351 fp = fopen(path,"rb");
354 load_frame_raw8(vd, fp, curframe);
360 void make_voxeldata(struct Render *re)
364 re->i.infostr= "Loading voxel datasets";
365 re->stats_draw(re->sdh, &re->i);
367 /* XXX: should be doing only textures used in this render */
368 for (tex= re->main->tex.first; tex; tex= tex->id.next) {
369 if(tex->id.us && tex->type==TEX_VOXELDATA) {
370 cache_voxeldata(re, tex);
375 re->stats_draw(re->sdh, &re->i);
379 int voxeldatatex(struct Tex *tex, float *texvec, struct TexResult *texres)
381 int retval = TEX_INT;
382 VoxelData *vd = tex->vd;
383 float co[3], offset[3] = {0.5, 0.5, 0.5};
385 if ((!vd) || (vd->dataset==NULL)) {
390 /* scale lookup from 0.0-1.0 (original location) to -1.0, 1.0, consistent with image texture tex coords */
391 /* in implementation this works backwards, bringing sample locations from -1.0, 1.0
392 * to the range 0.0, 1.0, before looking up in the voxel structure. */
393 copy_v3_v3(co, texvec);
395 add_v3_v3(co, offset);
397 /* co is now in the range 0.0, 1.0 */
398 switch (vd->extend) {
401 if ((co[0] < 0.f || co[0] > 1.f) || (co[1] < 0.f || co[1] > 1.f) || (co[2] < 0.f || co[2] > 1.f)) {
409 co[0] = co[0] - floor(co[0]);
410 co[1] = co[1] - floor(co[1]);
411 co[2] = co[2] - floor(co[2]);
416 CLAMP(co[0], 0.f, 1.f);
417 CLAMP(co[1], 0.f, 1.f);
418 CLAMP(co[2], 0.f, 1.f);
423 switch (vd->interp_type) {
424 case TEX_VD_NEARESTNEIGHBOR:
425 texres->tin = voxel_sample_nearest(vd->dataset, vd->resol, co);
428 texres->tin = voxel_sample_trilinear(vd->dataset, vd->resol, co);
430 case TEX_VD_QUADRATIC:
431 texres->tin = voxel_sample_triquadratic(vd->dataset, vd->resol, co);
433 case TEX_VD_TRICUBIC_CATROM:
434 case TEX_VD_TRICUBIC_BSPLINE:
435 texres->tin = voxel_sample_tricubic(vd->dataset, vd->resol, co, (vd->interp_type == TEX_VD_TRICUBIC_BSPLINE));
439 texres->tin *= vd->int_multiplier;
442 texres->tr = texres->tin;
443 texres->tg = texres->tin;
444 texres->tb = texres->tin;
445 texres->ta = texres->tin;