Dynamic Paint merge:
[blender.git] / source / blender / render / intern / source / voxeldata.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Raul Fernandez Hernandez (Farsthary), Matt Ebb.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/render/intern/source/voxeldata.c
29  *  \ingroup render
30  */
31
32
33 #include <math.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_math.h"
40 #include "BLI_blenlib.h"
41 #include "BLI_voxel.h"
42 #include "BLI_utildefines.h"
43
44 #include "IMB_imbuf.h"
45 #include "IMB_imbuf_types.h"
46
47 #include "BKE_global.h"
48 #include "BKE_image.h"
49 #include "BKE_main.h"
50 #include "BKE_modifier.h"
51
52 #include "smoke_API.h"
53
54 #include "DNA_texture_types.h"
55 #include "DNA_object_force.h"
56 #include "DNA_object_types.h"
57 #include "DNA_modifier_types.h"
58 #include "DNA_smoke_types.h"
59
60
61 #include "render_types.h"
62 #include "renderdatabase.h"
63 #include "texture.h"
64 #include "voxeldata.h"
65
66 static int is_vd_res_ok(VoxelData *vd)
67 {
68         /* arbitrary large value so corrupt headers dont break */
69         const int min= 1, max= 100000;
70         return  (vd->resol[0] >= min && vd->resol[0] <= max) &&
71                         (vd->resol[1] >= min && vd->resol[1] <= max) &&
72                         (vd->resol[2] >= min && vd->resol[2] <= max);
73 }
74
75 /* use size_t because the result may exceed INT_MAX */
76 static size_t vd_resol_size(VoxelData *vd)
77 {
78         return (size_t)vd->resol[0] * (size_t)vd->resol[1] * (size_t)vd->resol[2];
79 }
80
81 static int load_frame_blendervoxel(VoxelData *vd, FILE *fp, int frame)
82 {       
83         const size_t size = vd_resol_size(vd);
84         size_t offset = sizeof(VoxelDataHeader);
85         
86         if(is_vd_res_ok(vd) == FALSE)
87                 return 0;
88
89         vd->dataset = MEM_mapallocN(sizeof(float)*size, "voxel dataset");
90         if(vd->dataset == NULL) return 0;
91
92         if(fseek(fp, frame*size*sizeof(float)+offset, 0) == -1)
93                 return 0;
94         if(fread(vd->dataset, sizeof(float), size, fp) != size)
95                 return 0;
96         
97         vd->cachedframe = frame;
98         vd->ok = 1;
99         return 1;
100 }
101
102 static int load_frame_raw8(VoxelData *vd, FILE *fp, int frame)
103 {
104         const size_t size = vd_resol_size(vd);
105         char *data_c;
106         int i;
107
108         if(is_vd_res_ok(vd) == FALSE)
109                 return 0;
110
111         vd->dataset = MEM_mapallocN(sizeof(float)*size, "voxel dataset");
112         if(vd->dataset == NULL) return 0;
113         data_c = (char *)MEM_mallocN(sizeof(char)*size, "temporary voxel file reading storage");
114         if(data_c == NULL) {
115                 MEM_freeN(vd->dataset);
116                 vd->dataset= NULL;
117                 return 0;
118         }
119
120         if(fseek(fp,(frame-1)*size*sizeof(char),0) == -1) {
121                 MEM_freeN(data_c);
122                 MEM_freeN(vd->dataset);
123                 vd->dataset= NULL;
124                 return 0;
125         }
126         if(fread(data_c, sizeof(char), size, fp) != size) {
127                 MEM_freeN(data_c);
128                 MEM_freeN(vd->dataset);
129                 vd->dataset= NULL;
130                 return 0;
131         }
132         
133         for (i=0; i<size; i++) {
134                 vd->dataset[i] = (float)data_c[i] / 255.f;
135         }
136         MEM_freeN(data_c);
137         
138         vd->cachedframe = frame;
139         vd->ok = 1;
140         return 1;
141 }
142
143 static void load_frame_image_sequence(VoxelData *vd, Tex *tex)
144 {
145         ImBuf *ibuf;
146         Image *ima = tex->ima;
147         ImageUser *tiuser = &tex->iuser;
148         ImageUser iuser = *(tiuser);
149         int x=0, y=0, z=0;
150         float *rf;
151
152         if (!ima || !tiuser) return;
153         if (iuser.frames == 0) return;
154         
155         ima->source = IMA_SRC_SEQUENCE;
156         iuser.framenr = 1 + iuser.offset;
157
158         /* find the first valid ibuf and use it to initialise the resolution of the data set */
159         /* need to do this in advance so we know how much memory to allocate */
160         ibuf= BKE_image_get_ibuf(ima, &iuser);
161         while (!ibuf && (iuser.framenr < iuser.frames)) {
162                 iuser.framenr++;
163                 ibuf= BKE_image_get_ibuf(ima, &iuser);
164         }
165         if (!ibuf) return;
166         if (!ibuf->rect_float) IMB_float_from_rect(ibuf);
167         
168         vd->flag |= TEX_VD_STILL;
169         vd->resol[0] = ibuf->x;
170         vd->resol[1] = ibuf->y;
171         vd->resol[2] = iuser.frames;
172         vd->dataset = MEM_mapallocN(sizeof(float)*vd_resol_size(vd), "voxel dataset");
173         
174         for (z=0; z < iuser.frames; z++)
175         {       
176                 /* get a new ibuf for each frame */
177                 if (z > 0) {
178                         iuser.framenr++;
179                         ibuf= BKE_image_get_ibuf(ima, &iuser);
180                         if (!ibuf) break;
181                         if (!ibuf->rect_float) IMB_float_from_rect(ibuf);
182                 }
183                 rf = ibuf->rect_float;
184                 
185                 for (y=0; y < ibuf->y; y++)
186                 {
187                         for (x=0; x < ibuf->x; x++)
188                         {
189                                 /* currently averaged to monchrome */
190                                 vd->dataset[ V_I(x, y, z, vd->resol) ] = (rf[0] + rf[1] + rf[2])*0.333f;
191                                 rf +=4;
192                         }
193                 }
194                 
195                 BKE_image_free_anim_ibufs(ima, iuser.framenr);
196         }
197         
198         vd->ok = 1;
199         return;
200 }
201
202 static int read_voxeldata_header(FILE *fp, struct VoxelData *vd)
203 {
204         VoxelDataHeader *h=(VoxelDataHeader *)MEM_mallocN(sizeof(VoxelDataHeader), "voxel data header");
205         
206         rewind(fp);
207         if(fread(h,sizeof(VoxelDataHeader),1,fp) != 1) {
208                 MEM_freeN(h);
209                 return 0;
210         }
211         
212         vd->resol[0]=h->resolX;
213         vd->resol[1]=h->resolY;
214         vd->resol[2]=h->resolZ;
215
216         MEM_freeN(h);
217         return 1;
218 }
219
220 static void init_frame_smoke(VoxelData *vd, float cfra)
221 {
222 #ifdef WITH_SMOKE
223         Object *ob;
224         ModifierData *md;
225         
226         vd->dataset = NULL;
227         if (vd->object == NULL) return; 
228         ob= vd->object;
229         
230         /* draw code for smoke */
231         if( (md = (ModifierData *)modifiers_findByType(ob, eModifierType_Smoke)) )
232         {
233                 SmokeModifierData *smd = (SmokeModifierData *)md;
234
235                 
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) {
240                                 size_t totRes;
241                                 size_t i;
242                                 float *heat;
243
244                                 copy_v3_v3_int(vd->resol, smd->domain->res);
245                                 totRes= vd_resol_size(vd);
246
247                                 // scaling heat values from -2.0-2.0 to 0.0-1.0
248                                 vd->dataset = MEM_mapallocN(sizeof(float)*(totRes), "smoke data");
249
250
251                                 heat = smoke_get_heat(smd->domain->fluid);
252
253                                 for (i=0; i<totRes; i++)
254                                 {
255                                         vd->dataset[i] = (heat[i]+2.0f)/4.0f;
256                                 }
257
258                                 //vd->dataset = smoke_get_heat(smd->domain->fluid);
259                         }
260                         else if (vd->smoked_type == TEX_VD_SMOKEVEL) {
261                                 size_t totRes;
262                                 size_t i;
263                                 float *xvel, *yvel, *zvel;
264
265                                 copy_v3_v3_int(vd->resol, smd->domain->res);
266                                 totRes= vd_resol_size(vd);
267
268                                 // scaling heat values from -2.0-2.0 to 0.0-1.0
269                                 vd->dataset = MEM_mapallocN(sizeof(float)*(totRes), "smoke data");
270
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);
274
275                                 for (i=0; i<totRes; i++)
276                                 {
277                                         vd->dataset[i] = sqrt(xvel[i]*xvel[i] + yvel[i]*yvel[i] + zvel[i]*zvel[i])*3.0f;
278                                 }
279
280                         }
281                         else {
282                                 size_t totRes;
283                                 float *density;
284
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);
288                                 } else {
289                                         copy_v3_v3_int(vd->resol, smd->domain->res);
290                                         density = smoke_get_density(smd->domain->fluid);
291                                 }
292
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
299                 }
300         }
301         
302         vd->ok = 1;
303
304 #else // WITH_SMOKE
305         (void)vd;
306         (void)cfra;
307
308         vd->dataset= NULL;
309 #endif
310 }
311
312 void cache_voxeldata(Tex *tex, int scene_frame)
313 {       
314         VoxelData *vd = tex->vd;
315         FILE *fp;
316         int curframe;
317         char path[sizeof(vd->source_path)];
318         
319         /* only re-cache if dataset needs updating */
320         if ((vd->flag & TEX_VD_STILL) || (vd->cachedframe == scene_frame))
321                 if (vd->ok) return;
322         
323         /* clear out old cache, ready for new */
324         if (vd->dataset) {
325                 MEM_freeN(vd->dataset);
326                 vd->dataset = NULL;
327         }
328
329         if (vd->flag & TEX_VD_STILL)
330                 curframe = vd->still_frame;
331         else
332                 curframe = scene_frame;
333         
334         BLI_strncpy(path, vd->source_path, sizeof(path));
335         
336         switch(vd->file_format) {
337                 case TEX_VD_IMAGE_SEQUENCE:
338                         load_frame_image_sequence(vd, tex);
339                         return;
340                 case TEX_VD_SMOKE:
341                         init_frame_smoke(vd, scene_frame);
342                         return;
343                 case TEX_VD_BLENDERVOXEL:
344                         BLI_path_abs(path, G.main->name);
345                         if (!BLI_exists(path)) return;
346                         fp = fopen(path,"rb");
347                         if (!fp) return;
348                         
349                         if(read_voxeldata_header(fp, vd))
350                                 load_frame_blendervoxel(vd, fp, curframe-1);
351
352                         fclose(fp);
353                         return;
354                 case TEX_VD_RAW_8BIT:
355                         BLI_path_abs(path, G.main->name);
356                         if (!BLI_exists(path)) return;
357                         fp = fopen(path,"rb");
358                         if (!fp) return;
359                         
360                         load_frame_raw8(vd, fp, curframe);
361                         fclose(fp);
362                         return;
363         }
364 }
365
366 void make_voxeldata(struct Render *re)
367 {
368         Tex *tex;
369         
370         re->i.infostr= "Loading voxel datasets";
371         re->stats_draw(re->sdh, &re->i);
372         
373         /* XXX: should be doing only textures used in this render */
374         for (tex= re->main->tex.first; tex; tex= tex->id.next) {
375                 if(tex->id.us && tex->type==TEX_VOXELDATA) {
376                         cache_voxeldata(tex, re->r.cfra);
377                 }
378         }
379         
380         re->i.infostr= NULL;
381         re->stats_draw(re->sdh, &re->i);
382         
383 }
384
385 int voxeldatatex(struct Tex *tex, const float texvec[3], struct TexResult *texres)
386 {        
387         int retval = TEX_INT;
388         VoxelData *vd = tex->vd;        
389         float co[3], offset[3] = {0.5, 0.5, 0.5};
390
391         if (vd->dataset==NULL) {
392                 texres->tin = 0.0f;
393                 return 0;
394         }
395         
396         /* scale lookup from 0.0-1.0 (original location) to -1.0, 1.0, consistent with image texture tex coords */
397         /* in implementation this works backwards, bringing sample locations from -1.0, 1.0
398          * to the range 0.0, 1.0, before looking up in the voxel structure. */
399         copy_v3_v3(co, texvec);
400         mul_v3_fl(co, 0.5f);
401         add_v3_v3(co, offset);
402
403         /* co is now in the range 0.0, 1.0 */
404         switch (vd->extend) {
405                 case TEX_CLIP:
406                 {
407                         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)) {
408                                 texres->tin = 0.f;
409                                 return retval;
410                         }
411                         break;
412                 }
413                 case TEX_REPEAT:
414                 {
415                         co[0] = co[0] - floorf(co[0]);
416                         co[1] = co[1] - floorf(co[1]);
417                         co[2] = co[2] - floorf(co[2]);
418                         break;
419                 }
420                 case TEX_EXTEND:
421                 {
422                         CLAMP(co[0], 0.f, 1.f);
423                         CLAMP(co[1], 0.f, 1.f);
424                         CLAMP(co[2], 0.f, 1.f);
425                         break;
426                 }
427         }
428         
429         switch (vd->interp_type) {
430                 case TEX_VD_NEARESTNEIGHBOR:
431                         texres->tin = voxel_sample_nearest(vd->dataset, vd->resol, co);
432                         break;  
433                 case TEX_VD_LINEAR:
434                         texres->tin = voxel_sample_trilinear(vd->dataset, vd->resol, co);
435                         break;                                  
436                 case TEX_VD_QUADRATIC:
437                         texres->tin = voxel_sample_triquadratic(vd->dataset, vd->resol, co);
438                         break;
439                 case TEX_VD_TRICUBIC_CATROM:
440                 case TEX_VD_TRICUBIC_BSPLINE:
441                         texres->tin = voxel_sample_tricubic(vd->dataset, vd->resol, co, (vd->interp_type == TEX_VD_TRICUBIC_BSPLINE));
442                         break;
443         }
444         
445         texres->tin *= vd->int_multiplier;
446         BRICONT;
447         
448         texres->tr = texres->tin;
449         texres->tg = texres->tin;
450         texres->tb = texres->tin;
451         texres->ta = texres->tin;
452         BRICONTRGB;
453         
454         return retval;  
455 }
456
457