Fix for build error with previous commit, seems the order of #includes matters here.
[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 #ifdef WIN32
38 #include "BLI_winstuff.h"
39 #endif
40
41 #include "MEM_guardedalloc.h"
42
43 #include "BLI_math.h"
44 #include "BLI_blenlib.h"
45 #include "BLI_voxel.h"
46 #include "BLI_utildefines.h"
47
48 #include "BLF_translation.h"
49
50 #include "IMB_imbuf.h"
51 #include "IMB_imbuf_types.h"
52
53 #include "BKE_global.h"
54 #include "BKE_image.h"
55 #include "BKE_main.h"
56 #include "BKE_modifier.h"
57
58 #include "smoke_API.h"
59
60 #include "DNA_texture_types.h"
61 #include "DNA_object_force.h"
62 #include "DNA_object_types.h"
63 #include "DNA_modifier_types.h"
64 #include "DNA_smoke_types.h"
65
66
67 #include "render_types.h"
68 #include "renderdatabase.h"
69 #include "texture.h"
70 #include "voxeldata.h"
71
72 static int is_vd_res_ok(VoxelData *vd)
73 {
74         /* arbitrary large value so corrupt headers don't break */
75         const int min = 1, max = 100000;
76         return (vd->resol[0] >= min && vd->resol[0] <= max) &&
77                (vd->resol[1] >= min && vd->resol[1] <= max) &&
78                (vd->resol[2] >= min && vd->resol[2] <= max);
79 }
80
81 /* use size_t because the result may exceed INT_MAX */
82 static size_t vd_resol_size(VoxelData *vd)
83 {
84         return (size_t)vd->resol[0] * (size_t)vd->resol[1] * (size_t)vd->resol[2];
85 }
86
87 static int load_frame_blendervoxel(VoxelData *vd, FILE *fp, int frame)
88 {       
89         const size_t size = vd_resol_size(vd);
90         size_t offset = sizeof(VoxelDataHeader);
91         
92         if (is_vd_res_ok(vd) == FALSE)
93                 return 0;
94
95         vd->dataset = MEM_mapallocN(sizeof(float) * size, "voxel dataset");
96         if (vd->dataset == NULL) return 0;
97
98         if (fseek(fp, frame * size * sizeof(float) + offset, 0) == -1)
99                 return 0;
100         if (fread(vd->dataset, sizeof(float), size, fp) != size)
101                 return 0;
102         
103         vd->cachedframe = frame;
104         vd->ok = 1;
105         return 1;
106 }
107
108 static int load_frame_raw8(VoxelData *vd, FILE *fp, int frame)
109 {
110         const size_t size = vd_resol_size(vd);
111         char *data_c;
112         int i;
113
114         if (is_vd_res_ok(vd) == FALSE)
115                 return 0;
116
117         vd->dataset = MEM_mapallocN(sizeof(float) * size, "voxel dataset");
118         if (vd->dataset == NULL) return 0;
119         data_c = (char *)MEM_mallocN(sizeof(char) * size, "temporary voxel file reading storage");
120         if (data_c == NULL) {
121                 MEM_freeN(vd->dataset);
122                 vd->dataset = NULL;
123                 return 0;
124         }
125
126         if (fseek(fp, (frame - 1) * size * sizeof(char), 0) == -1) {
127                 MEM_freeN(data_c);
128                 MEM_freeN(vd->dataset);
129                 vd->dataset = NULL;
130                 return 0;
131         }
132         if (fread(data_c, sizeof(char), size, fp) != size) {
133                 MEM_freeN(data_c);
134                 MEM_freeN(vd->dataset);
135                 vd->dataset = NULL;
136                 return 0;
137         }
138         
139         for (i = 0; i < size; i++) {
140                 vd->dataset[i] = (float)data_c[i] / 255.f;
141         }
142         MEM_freeN(data_c);
143         
144         vd->cachedframe = frame;
145         vd->ok = 1;
146         return 1;
147 }
148
149 static void load_frame_image_sequence(VoxelData *vd, Tex *tex)
150 {
151         ImBuf *ibuf;
152         Image *ima = tex->ima;
153         ImageUser *tiuser = &tex->iuser;
154         ImageUser iuser = *(tiuser);
155         int x = 0, y = 0, z = 0;
156         float *rf;
157
158         if (!ima || !tiuser) return;
159         if (iuser.frames == 0) return;
160         
161         ima->source = IMA_SRC_SEQUENCE;
162         iuser.framenr = 1 + iuser.offset;
163
164         /* find the first valid ibuf and use it to initialize the resolution of the data set */
165         /* need to do this in advance so we know how much memory to allocate */
166         ibuf = BKE_image_acquire_ibuf(ima, &iuser, NULL);
167         while (!ibuf && (iuser.framenr < iuser.frames)) {
168                 iuser.framenr++;
169                 ibuf = BKE_image_acquire_ibuf(ima, &iuser, NULL);
170         }
171         if (!ibuf) return;
172         if (!ibuf->rect_float) IMB_float_from_rect(ibuf);
173         
174         vd->flag |= TEX_VD_STILL;
175         vd->resol[0] = ibuf->x;
176         vd->resol[1] = ibuf->y;
177         vd->resol[2] = iuser.frames;
178         vd->dataset = MEM_mapallocN(sizeof(float) * vd_resol_size(vd), "voxel dataset");
179         
180         for (z = 0; z < iuser.frames; z++) {
181                 /* get a new ibuf for each frame */
182                 if (z > 0) {
183                         iuser.framenr++;
184                         BKE_image_release_ibuf(ima, ibuf, NULL);
185                         ibuf = BKE_image_acquire_ibuf(ima, &iuser, NULL);
186                         if (!ibuf) break;
187                         if (!ibuf->rect_float) IMB_float_from_rect(ibuf);
188                 }
189                 rf = ibuf->rect_float;
190                 
191                 for (y = 0; y < ibuf->y; y++) {
192                         for (x = 0; x < ibuf->x; x++) {
193                                 /* currently averaged to monchrome */
194                                 vd->dataset[BLI_VOXEL_INDEX(x, y, z, vd->resol)] = (rf[0] + rf[1] + rf[2]) / 3.0f;
195                                 rf += 4;
196                         }
197                 }
198                 
199                 BKE_image_free_anim_ibufs(ima, iuser.framenr);
200         }
201
202         BKE_image_release_ibuf(ima, ibuf, NULL);
203
204         vd->ok = 1;
205         return;
206 }
207
208 static int read_voxeldata_header(FILE *fp, struct VoxelData *vd)
209 {
210         VoxelDataHeader *h = (VoxelDataHeader *)MEM_mallocN(sizeof(VoxelDataHeader), "voxel data header");
211         
212         rewind(fp);
213         if (fread(h, sizeof(VoxelDataHeader), 1, fp) != 1) {
214                 MEM_freeN(h);
215                 return 0;
216         }
217         
218         vd->resol[0] = h->resolX;
219         vd->resol[1] = h->resolY;
220         vd->resol[2] = h->resolZ;
221
222         MEM_freeN(h);
223         return 1;
224 }
225
226 static void init_frame_smoke(VoxelData *vd, float cfra)
227 {
228 #ifdef WITH_SMOKE
229         Object *ob;
230         ModifierData *md;
231         
232         vd->dataset = NULL;
233         if (vd->object == NULL) return;
234         ob = vd->object;
235         
236         /* draw code for smoke */
237         if ((md = (ModifierData *)modifiers_findByType(ob, eModifierType_Smoke))) {
238                 SmokeModifierData *smd = (SmokeModifierData *)md;
239                 SmokeDomainSettings *sds = smd->domain;
240                 
241                 if (sds && sds->fluid) {
242                         if (cfra < sds->point_cache[0]->startframe)
243                                 ;  /* don't show smoke before simulation starts, this could be made an option in the future */
244                         else if (vd->smoked_type == TEX_VD_SMOKEHEAT) {
245                                 size_t totRes;
246                                 size_t i;
247                                 float *heat;
248
249                                 if (!smoke_has_heat(sds->fluid)) return;
250
251                                 copy_v3_v3_int(vd->resol, sds->res);
252                                 totRes = vd_resol_size(vd);
253                                 vd->dataset = MEM_mapallocN(sizeof(float) * (totRes), "smoke data");
254                                 /* get heat data */
255                                 heat = smoke_get_heat(sds->fluid);
256
257                                 /* scale heat values from -2.0-2.0 to 0.0-1.0 */
258                                 for (i = 0; i < totRes; i++) {
259                                         vd->dataset[i] = (heat[i] + 2.0f) / 4.0f;
260                                 }
261                         }
262                         else if (vd->smoked_type == TEX_VD_SMOKEVEL) {
263                                 size_t totRes;
264                                 size_t i;
265                                 float *xvel, *yvel, *zvel;
266
267                                 copy_v3_v3_int(vd->resol, sds->res);
268                                 totRes = vd_resol_size(vd);
269                                 vd->dataset = MEM_mapallocN(sizeof(float) * (totRes), "smoke data");
270                                 /* get velocity data */
271                                 xvel = smoke_get_velocity_x(sds->fluid);
272                                 yvel = smoke_get_velocity_y(sds->fluid);
273                                 zvel = smoke_get_velocity_z(sds->fluid);
274
275                                 /* map velocities between 0 and 0.3f */
276                                 for (i = 0; i < totRes; i++) {
277                                         vd->dataset[i] = sqrtf(xvel[i] * xvel[i] + yvel[i] * yvel[i] + zvel[i] * zvel[i]) * 3.0f;
278                                 }
279
280                         }
281                         else if (vd->smoked_type == TEX_VD_SMOKEFLAME) {
282                                 size_t totRes;
283                                 float *flame;
284
285                                 if (sds->flags & MOD_SMOKE_HIGHRES) {
286                                         if (!smoke_turbulence_has_fuel(sds->wt)) return;
287                                         smoke_turbulence_get_res(sds->wt, vd->resol);
288                                         flame = smoke_turbulence_get_flame(sds->wt);
289                                 }
290                                 else {
291                                         if (!smoke_has_fuel(sds->fluid)) return;
292                                         copy_v3_v3_int(vd->resol, sds->res);
293                                         flame = smoke_get_flame(sds->fluid);
294                                 }
295
296                                 /* always store copy, as smoke internal data can change */
297                                 totRes = vd_resol_size(vd);
298                                 vd->dataset = MEM_mapallocN(sizeof(float)*(totRes), "smoke data");
299                                 memcpy(vd->dataset, flame, sizeof(float)*totRes);
300                         }
301                         else {
302                                 size_t totCells;
303                                 int depth = 4;
304                                 vd->data_type = TEX_VD_RGBA_PREMUL;
305
306                                 /* data resolution */
307                                 if (sds->flags & MOD_SMOKE_HIGHRES) {
308                                         smoke_turbulence_get_res(sds->wt, vd->resol);
309                                 }
310                                 else {
311                                         copy_v3_v3_int(vd->resol, sds->res);
312                                 }
313
314                                 /* TODO: is_vd_res_ok(rvd) doesnt check this resolution */
315                                 totCells = vd_resol_size(vd) * depth;
316                                 /* always store copy, as smoke internal data can change */
317                                 vd->dataset = MEM_mapallocN(sizeof(float) * totCells, "smoke data");
318
319                                 if (sds->flags & MOD_SMOKE_HIGHRES) {
320                                         if (smoke_turbulence_has_colors(sds->wt)) {
321                                                 smoke_turbulence_get_rgba(sds->wt, vd->dataset, 1);
322                                         }
323                                         else {
324                                                 smoke_turbulence_get_rgba_from_density(sds->wt, sds->active_color, vd->dataset, 1);
325                                         }
326                                 }
327                                 else {
328                                         if (smoke_has_colors(sds->fluid)) {
329                                                 smoke_get_rgba(sds->fluid, vd->dataset, 1);
330                                         }
331                                         else {
332                                                 smoke_get_rgba_from_density(sds->fluid, sds->active_color, vd->dataset, 1);
333                                         }
334                                 }
335                         }  /* end of fluid condition */
336                 }
337         }
338         
339         vd->ok = 1;
340
341 #else // WITH_SMOKE
342         (void)vd;
343         (void)cfra;
344
345         vd->dataset = NULL;
346 #endif
347 }
348
349 void cache_voxeldata(Tex *tex, int scene_frame)
350 {       
351         VoxelData *vd = tex->vd;
352         FILE *fp;
353         int curframe;
354         char path[sizeof(vd->source_path)];
355         
356         /* only re-cache if dataset needs updating */
357         if ((vd->flag & TEX_VD_STILL) || (vd->cachedframe == scene_frame))
358                 if (vd->ok) return;
359         
360         /* clear out old cache, ready for new */
361         if (vd->dataset) {
362                 MEM_freeN(vd->dataset);
363                 vd->dataset = NULL;
364         }
365         /* reset data_type */
366         vd->data_type = TEX_VD_INTENSITY;
367
368         if (vd->flag & TEX_VD_STILL)
369                 curframe = vd->still_frame;
370         else
371                 curframe = scene_frame;
372         
373         BLI_strncpy(path, vd->source_path, sizeof(path));
374         
375         switch (vd->file_format) {
376                 case TEX_VD_IMAGE_SEQUENCE:
377                         load_frame_image_sequence(vd, tex);
378                         return;
379                 case TEX_VD_SMOKE:
380                         init_frame_smoke(vd, scene_frame);
381                         return;
382                 case TEX_VD_BLENDERVOXEL:
383                         BLI_path_abs(path, G.main->name);
384                         if (!BLI_exists(path)) return;
385                         fp = BLI_fopen(path, "rb");
386                         if (!fp) return;
387                         
388                         if (read_voxeldata_header(fp, vd))
389                                 load_frame_blendervoxel(vd, fp, curframe - 1);
390
391                         fclose(fp);
392                         return;
393                 case TEX_VD_RAW_8BIT:
394                         BLI_path_abs(path, G.main->name);
395                         if (!BLI_exists(path)) return;
396                         fp = BLI_fopen(path, "rb");
397                         if (!fp) return;
398                         
399                         load_frame_raw8(vd, fp, curframe);
400                         fclose(fp);
401                         return;
402         }
403 }
404
405 void make_voxeldata(struct Render *re)
406 {
407         Tex *tex;
408         
409         re->i.infostr = IFACE_("Loading voxel datasets");
410         re->stats_draw(re->sdh, &re->i);
411         
412         /* XXX: should be doing only textures used in this render */
413         for (tex = re->main->tex.first; tex; tex = tex->id.next) {
414                 if (tex->id.us && tex->type == TEX_VOXELDATA) {
415                         cache_voxeldata(tex, re->r.cfra);
416                 }
417         }
418         
419         re->i.infostr = NULL;
420         re->stats_draw(re->sdh, &re->i);
421         
422 }
423
424 int voxeldatatex(struct Tex *tex, const float texvec[3], struct TexResult *texres)
425 {        
426         VoxelData *vd = tex->vd;
427         float co[3], offset[3] = {0.5, 0.5, 0.5}, a;
428         int retval = (vd->data_type == TEX_VD_RGBA_PREMUL) ? TEX_RGB : TEX_INT;
429         int depth = (vd->data_type == TEX_VD_RGBA_PREMUL) ? 4 : 1;
430         int ch;
431
432         if (vd->dataset == NULL) {
433                 texres->tin = 0.0f;
434                 return 0;
435         }
436         
437         /* scale lookup from 0.0-1.0 (original location) to -1.0, 1.0, consistent with image texture tex coords */
438         /* in implementation this works backwards, bringing sample locations from -1.0, 1.0
439          * to the range 0.0, 1.0, before looking up in the voxel structure. */
440         copy_v3_v3(co, texvec);
441         mul_v3_fl(co, 0.5f);
442         add_v3_v3(co, offset);
443
444         /* co is now in the range 0.0, 1.0 */
445         switch (vd->extend) {
446                 case TEX_CLIP:
447                 {
448                         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)) {
449                                 texres->tin = 0.f;
450                                 return retval;
451                         }
452                         break;
453                 }
454                 case TEX_REPEAT:
455                 {
456                         co[0] = co[0] - floorf(co[0]);
457                         co[1] = co[1] - floorf(co[1]);
458                         co[2] = co[2] - floorf(co[2]);
459                         break;
460                 }
461                 case TEX_EXTEND:
462                 {
463                         CLAMP(co[0], 0.f, 1.f);
464                         CLAMP(co[1], 0.f, 1.f);
465                         CLAMP(co[2], 0.f, 1.f);
466                         break;
467                 }
468         }
469
470         for (ch = 0; ch < depth; ch++) {
471                 float *dataset = vd->dataset + ch*vd->resol[0]*vd->resol[1]*vd->resol[2];
472                 float *result = &texres->tin;
473
474                 if (vd->data_type == TEX_VD_RGBA_PREMUL) {
475                         switch (ch) {
476                                 case 0:
477                                         result = &texres->tr;
478                                         break;
479                                 case 1:
480                                         result = &texres->tg;
481                                         break;
482                                 case 2:
483                                         result = &texres->tb;
484                                         break;
485                         }
486                 }
487
488                 switch (vd->interp_type) {
489                         case TEX_VD_NEARESTNEIGHBOR:
490                                 *result = BLI_voxel_sample_nearest(dataset, vd->resol, co);
491                                 break;  
492                         case TEX_VD_LINEAR:
493                                 *result = BLI_voxel_sample_trilinear(dataset, vd->resol, co);
494                                 break;
495                         case TEX_VD_QUADRATIC:
496                                 *result = BLI_voxel_sample_triquadratic(dataset, vd->resol, co);
497                                 break;
498                         case TEX_VD_TRICUBIC_CATROM:
499                         case TEX_VD_TRICUBIC_BSPLINE:
500                                 *result = BLI_voxel_sample_tricubic(dataset, vd->resol, co, (vd->interp_type == TEX_VD_TRICUBIC_BSPLINE));
501                                 break;
502                 }
503         }
504
505         a = texres->tin;
506         texres->tin *= vd->int_multiplier;
507         BRICONT;
508         
509         if (vd->data_type == TEX_VD_RGBA_PREMUL) {
510                 /* unmultiply */
511                 if (a>0.001f) {
512                         texres->tr /= a;
513                         texres->tg /= a;
514                         texres->tb /= a;
515                 }
516                 texres->talpha = 1;
517         }
518         else {
519                 texres->tr = texres->tin;
520                 texres->tg = texres->tin;
521                 texres->tb = texres->tin;
522         }
523
524         texres->ta = texres->tin;
525         BRICONTRGB;
526         
527         return retval;
528 }