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)
228 if (vd->object == NULL) return;
231 /* draw code for smoke */
232 if( (md = (ModifierData *)modifiers_findByType(ob, eModifierType_Smoke)) )
234 SmokeModifierData *smd = (SmokeModifierData *)md;
237 if(smd->domain && smd->domain->fluid) {
238 if(cfra < smd->domain->point_cache[0]->startframe)
239 ; /* don't show smoke before simulation starts, this could be made an option in the future */
240 else if (vd->smoked_type == TEX_VD_SMOKEHEAT) {
245 VECCOPY(vd->resol, smd->domain->res);
246 totRes= vd_resol_size(vd);
248 // scaling heat values from -2.0-2.0 to 0.0-1.0
249 vd->dataset = MEM_mapallocN(sizeof(float)*(totRes), "smoke data");
252 heat = smoke_get_heat(smd->domain->fluid);
254 for (i=0; i<totRes; i++)
256 vd->dataset[i] = (heat[i]+2.0f)/4.0f;
259 //vd->dataset = smoke_get_heat(smd->domain->fluid);
261 else if (vd->smoked_type == TEX_VD_SMOKEVEL) {
264 float *xvel, *yvel, *zvel;
266 VECCOPY(vd->resol, smd->domain->res);
267 totRes= vd_resol_size(vd);
269 // scaling heat values from -2.0-2.0 to 0.0-1.0
270 vd->dataset = MEM_mapallocN(sizeof(float)*(totRes), "smoke data");
272 xvel = smoke_get_velocity_x(smd->domain->fluid);
273 yvel = smoke_get_velocity_y(smd->domain->fluid);
274 zvel = smoke_get_velocity_z(smd->domain->fluid);
276 for (i=0; i<totRes; i++)
278 vd->dataset[i] = sqrt(xvel[i]*xvel[i] + yvel[i]*yvel[i] + zvel[i]*zvel[i])*3.0f;
286 if (smd->domain->flags & MOD_SMOKE_HIGHRES) {
287 smoke_turbulence_get_res(smd->domain->wt, vd->resol);
288 density = smoke_turbulence_get_density(smd->domain->wt);
290 VECCOPY(vd->resol, smd->domain->res);
291 density = smoke_get_density(smd->domain->fluid);
294 /* TODO: is_vd_res_ok(rvd) doesnt check this resolution */
295 totRes= vd_resol_size(vd);
296 /* always store copy, as smoke internal data can change */
297 vd->dataset = MEM_mapallocN(sizeof(float)*(totRes), "smoke data");
298 memcpy(vd->dataset, density, sizeof(float)*totRes);
299 } // end of fluid condition
313 static void cache_voxeldata(struct Render *re, Tex *tex)
315 VoxelData *vd = tex->vd;
318 char path[sizeof(vd->source_path)];
320 /* only re-cache if dataset needs updating */
321 if ((vd->flag & TEX_VD_STILL) || (vd->cachedframe == re->r.cfra))
324 /* clear out old cache, ready for new */
326 MEM_freeN(vd->dataset);
330 if (vd->flag & TEX_VD_STILL)
331 curframe = vd->still_frame;
333 curframe = re->r.cfra;
335 BLI_strncpy(path, vd->source_path, sizeof(path));
337 switch(vd->file_format) {
338 case TEX_VD_IMAGE_SEQUENCE:
339 load_frame_image_sequence(vd, tex);
342 init_frame_smoke(vd, re->r.cfra);
344 case TEX_VD_BLENDERVOXEL:
345 BLI_path_abs(path, G.main->name);
346 if (!BLI_exists(path)) return;
347 fp = fopen(path,"rb");
350 if(read_voxeldata_header(fp, vd))
351 load_frame_blendervoxel(vd, fp, curframe-1);
355 case TEX_VD_RAW_8BIT:
356 BLI_path_abs(path, G.main->name);
357 if (!BLI_exists(path)) return;
358 fp = fopen(path,"rb");
361 load_frame_raw8(vd, fp, curframe);
367 void make_voxeldata(struct Render *re)
371 re->i.infostr= "Loading voxel datasets";
372 re->stats_draw(re->sdh, &re->i);
374 /* XXX: should be doing only textures used in this render */
375 for (tex= re->main->tex.first; tex; tex= tex->id.next) {
376 if(tex->id.us && tex->type==TEX_VOXELDATA) {
377 cache_voxeldata(re, tex);
382 re->stats_draw(re->sdh, &re->i);
386 int voxeldatatex(struct Tex *tex, float *texvec, struct TexResult *texres)
388 int retval = TEX_INT;
389 VoxelData *vd = tex->vd;
390 float co[3], offset[3] = {0.5, 0.5, 0.5};
392 if ((!vd) || (vd->dataset==NULL)) {
397 /* scale lookup from 0.0-1.0 (original location) to -1.0, 1.0, consistent with image texture tex coords */
398 /* in implementation this works backwards, bringing sample locations from -1.0, 1.0
399 * to the range 0.0, 1.0, before looking up in the voxel structure. */
400 copy_v3_v3(co, texvec);
402 add_v3_v3(co, offset);
404 /* co is now in the range 0.0, 1.0 */
405 switch (vd->extend) {
408 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)) {
416 co[0] = co[0] - floor(co[0]);
417 co[1] = co[1] - floor(co[1]);
418 co[2] = co[2] - floor(co[2]);
423 CLAMP(co[0], 0.f, 1.f);
424 CLAMP(co[1], 0.f, 1.f);
425 CLAMP(co[2], 0.f, 1.f);
430 switch (vd->interp_type) {
431 case TEX_VD_NEARESTNEIGHBOR:
432 texres->tin = voxel_sample_nearest(vd->dataset, vd->resol, co);
435 texres->tin = voxel_sample_trilinear(vd->dataset, vd->resol, co);
437 case TEX_VD_QUADRATIC:
438 texres->tin = voxel_sample_triquadratic(vd->dataset, vd->resol, co);
440 case TEX_VD_TRICUBIC_CATROM:
441 case TEX_VD_TRICUBIC_BSPLINE:
442 texres->tin = voxel_sample_tricubic(vd->dataset, vd->resol, co, (vd->interp_type == TEX_VD_TRICUBIC_BSPLINE));
446 texres->tin *= vd->int_multiplier;
449 texres->tr = texres->tin;
450 texres->tg = texres->tin;
451 texres->tb = texres->tin;
452 texres->ta = texres->tin;