style cleanup
[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 don't 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 initialize 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                 /* get a new ibuf for each frame */
176                 if (z > 0) {
177                         iuser.framenr++;
178                         ibuf = BKE_image_get_ibuf(ima, &iuser);
179                         if (!ibuf) break;
180                         if (!ibuf->rect_float) IMB_float_from_rect(ibuf);
181                 }
182                 rf = ibuf->rect_float;
183                 
184                 for (y = 0; y < ibuf->y; y++) {
185                         for (x = 0; x < ibuf->x; x++) {
186                                 /* currently averaged to monchrome */
187                                 vd->dataset[BLI_VOXEL_INDEX(x, y, z, vd->resol)] = (rf[0] + rf[1] + rf[2]) * 0.333f;
188                                 rf += 4;
189                         }
190                 }
191                 
192                 BKE_image_free_anim_ibufs(ima, iuser.framenr);
193         }
194         
195         vd->ok = 1;
196         return;
197 }
198
199 static int read_voxeldata_header(FILE *fp, struct VoxelData *vd)
200 {
201         VoxelDataHeader *h = (VoxelDataHeader *)MEM_mallocN(sizeof(VoxelDataHeader), "voxel data header");
202         
203         rewind(fp);
204         if (fread(h, sizeof(VoxelDataHeader), 1, fp) != 1) {
205                 MEM_freeN(h);
206                 return 0;
207         }
208         
209         vd->resol[0] = h->resolX;
210         vd->resol[1] = h->resolY;
211         vd->resol[2] = h->resolZ;
212
213         MEM_freeN(h);
214         return 1;
215 }
216
217 static void init_frame_smoke(VoxelData *vd, float cfra)
218 {
219 #ifdef WITH_SMOKE
220         Object *ob;
221         ModifierData *md;
222         
223         vd->dataset = NULL;
224         if (vd->object == NULL) return;
225         ob = vd->object;
226         
227         /* draw code for smoke */
228         if ((md = (ModifierData *)modifiers_findByType(ob, eModifierType_Smoke))) {
229                 SmokeModifierData *smd = (SmokeModifierData *)md;
230                 SmokeDomainSettings *sds = smd->domain;
231                 
232                 if (sds && sds->fluid) {
233                         if (cfra < sds->point_cache[0]->startframe)
234                                 ;  /* don't show smoke before simulation starts, this could be made an option in the future */
235                         else if (vd->smoked_type == TEX_VD_SMOKEHEAT) {
236                                 size_t totRes;
237                                 size_t i;
238                                 float *heat;
239
240                                 if (!smoke_has_heat(sds->fluid)) return;
241
242                                 copy_v3_v3_int(vd->resol, sds->res);
243                                 totRes = vd_resol_size(vd);
244                                 vd->dataset = MEM_mapallocN(sizeof(float) * (totRes), "smoke data");
245                                 /* get heat data */
246                                 heat = smoke_get_heat(sds->fluid);
247
248                                 /* scale heat values from -2.0-2.0 to 0.0-1.0 */
249                                 for (i = 0; i < totRes; i++) {
250                                         vd->dataset[i] = (heat[i] + 2.0f) / 4.0f;
251                                 }
252                         }
253                         else if (vd->smoked_type == TEX_VD_SMOKEVEL) {
254                                 size_t totRes;
255                                 size_t i;
256                                 float *xvel, *yvel, *zvel;
257
258                                 copy_v3_v3_int(vd->resol, sds->res);
259                                 totRes = vd_resol_size(vd);
260                                 vd->dataset = MEM_mapallocN(sizeof(float) * (totRes), "smoke data");
261                                 /* get velocity data */
262                                 xvel = smoke_get_velocity_x(sds->fluid);
263                                 yvel = smoke_get_velocity_y(sds->fluid);
264                                 zvel = smoke_get_velocity_z(sds->fluid);
265
266                                 /* map velocities between 0 and 0.3f */
267                                 for (i = 0; i < totRes; i++) {
268                                         vd->dataset[i] = sqrt(xvel[i] * xvel[i] + yvel[i] * yvel[i] + zvel[i] * zvel[i]) * 3.0f;
269                                 }
270
271                         }
272                         else if (vd->smoked_type == TEX_VD_SMOKEFLAME) {
273                                 size_t totRes;
274                                 float *flame;
275
276                                 if (sds->flags & MOD_SMOKE_HIGHRES) {
277                                         if (!smoke_turbulence_has_fuel(sds->wt)) return;
278                                         smoke_turbulence_get_res(sds->wt, vd->resol);
279                                         flame = smoke_turbulence_get_flame(sds->wt);
280                                 }
281                                 else {
282                                         if (!smoke_has_fuel(sds->fluid)) return;
283                                         copy_v3_v3_int(vd->resol, sds->res);
284                                         flame = smoke_get_flame(sds->fluid);
285                                 }
286
287                                 /* always store copy, as smoke internal data can change */
288                                 totRes = vd_resol_size(vd);
289                                 vd->dataset = MEM_mapallocN(sizeof(float)*(totRes), "smoke data");
290                                 memcpy(vd->dataset, flame, sizeof(float)*totRes);
291                         }
292                         else {
293                                 size_t totCells;
294                                 int depth = 4;
295                                 vd->data_type = TEX_VD_RGBA_PREMUL;
296
297                                 /* data resolution */
298                                 if (sds->flags & MOD_SMOKE_HIGHRES) {
299                                         smoke_turbulence_get_res(sds->wt, vd->resol);
300                                 }
301                                 else {
302                                         copy_v3_v3_int(vd->resol, sds->res);
303                                 }
304
305                                 /* TODO: is_vd_res_ok(rvd) doesnt check this resolution */
306                                 totCells = vd_resol_size(vd) * depth;
307                                 /* always store copy, as smoke internal data can change */
308                                 vd->dataset = MEM_mapallocN(sizeof(float) * totCells, "smoke data");
309
310                                 if (sds->flags & MOD_SMOKE_HIGHRES) {
311                                         if (smoke_turbulence_has_colors(sds->wt)) {
312                                                 smoke_turbulence_get_rgba(sds->wt, vd->dataset, 1);
313                                         }
314                                         else {
315                                                 smoke_turbulence_get_rgba_from_density(sds->wt, sds->active_color, vd->dataset, 1);
316                                         }
317                                 }
318                                 else {
319                                         if (smoke_has_colors(sds->fluid)) {
320                                                 smoke_get_rgba(sds->fluid, vd->dataset, 1);
321                                         }
322                                         else {
323                                                 smoke_get_rgba_from_density(sds->fluid, sds->active_color, vd->dataset, 1);
324                                         }
325                                 }
326                         }  /* end of fluid condition */
327                 }
328         }
329         
330         vd->ok = 1;
331
332 #else // WITH_SMOKE
333         (void)vd;
334         (void)cfra;
335
336         vd->dataset = NULL;
337 #endif
338 }
339
340 void cache_voxeldata(Tex *tex, int scene_frame)
341 {       
342         VoxelData *vd = tex->vd;
343         FILE *fp;
344         int curframe;
345         char path[sizeof(vd->source_path)];
346         
347         /* only re-cache if dataset needs updating */
348         if ((vd->flag & TEX_VD_STILL) || (vd->cachedframe == scene_frame))
349                 if (vd->ok) return;
350         
351         /* clear out old cache, ready for new */
352         if (vd->dataset) {
353                 MEM_freeN(vd->dataset);
354                 vd->dataset = NULL;
355         }
356         /* reset data_type */
357         vd->data_type = TEX_VD_INTENSITY;
358
359         if (vd->flag & TEX_VD_STILL)
360                 curframe = vd->still_frame;
361         else
362                 curframe = scene_frame;
363         
364         BLI_strncpy(path, vd->source_path, sizeof(path));
365         
366         switch (vd->file_format) {
367                 case TEX_VD_IMAGE_SEQUENCE:
368                         load_frame_image_sequence(vd, tex);
369                         return;
370                 case TEX_VD_SMOKE:
371                         init_frame_smoke(vd, scene_frame);
372                         return;
373                 case TEX_VD_BLENDERVOXEL:
374                         BLI_path_abs(path, G.main->name);
375                         if (!BLI_exists(path)) return;
376                         fp = BLI_fopen(path, "rb");
377                         if (!fp) return;
378                         
379                         if (read_voxeldata_header(fp, vd))
380                                 load_frame_blendervoxel(vd, fp, curframe - 1);
381
382                         fclose(fp);
383                         return;
384                 case TEX_VD_RAW_8BIT:
385                         BLI_path_abs(path, G.main->name);
386                         if (!BLI_exists(path)) return;
387                         fp = BLI_fopen(path, "rb");
388                         if (!fp) return;
389                         
390                         load_frame_raw8(vd, fp, curframe);
391                         fclose(fp);
392                         return;
393         }
394 }
395
396 void make_voxeldata(struct Render *re)
397 {
398         Tex *tex;
399         
400         re->i.infostr = "Loading voxel datasets";
401         re->stats_draw(re->sdh, &re->i);
402         
403         /* XXX: should be doing only textures used in this render */
404         for (tex = re->main->tex.first; tex; tex = tex->id.next) {
405                 if (tex->id.us && tex->type == TEX_VOXELDATA) {
406                         cache_voxeldata(tex, re->r.cfra);
407                 }
408         }
409         
410         re->i.infostr = NULL;
411         re->stats_draw(re->sdh, &re->i);
412         
413 }
414
415 int voxeldatatex(struct Tex *tex, const float texvec[3], struct TexResult *texres)
416 {        
417         VoxelData *vd = tex->vd;
418         float co[3], offset[3] = {0.5, 0.5, 0.5}, a;
419         int retval = (vd->data_type == TEX_VD_RGBA_PREMUL) ? TEX_RGB : TEX_INT;
420         int depth = (vd->data_type == TEX_VD_RGBA_PREMUL) ? 4 : 1;
421         int ch;
422
423         if (vd->dataset == NULL) {
424                 texres->tin = 0.0f;
425                 return 0;
426         }
427         
428         /* scale lookup from 0.0-1.0 (original location) to -1.0, 1.0, consistent with image texture tex coords */
429         /* in implementation this works backwards, bringing sample locations from -1.0, 1.0
430          * to the range 0.0, 1.0, before looking up in the voxel structure. */
431         copy_v3_v3(co, texvec);
432         mul_v3_fl(co, 0.5f);
433         add_v3_v3(co, offset);
434
435         /* co is now in the range 0.0, 1.0 */
436         switch (vd->extend) {
437                 case TEX_CLIP:
438                 {
439                         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)) {
440                                 texres->tin = 0.f;
441                                 return retval;
442                         }
443                         break;
444                 }
445                 case TEX_REPEAT:
446                 {
447                         co[0] = co[0] - floorf(co[0]);
448                         co[1] = co[1] - floorf(co[1]);
449                         co[2] = co[2] - floorf(co[2]);
450                         break;
451                 }
452                 case TEX_EXTEND:
453                 {
454                         CLAMP(co[0], 0.f, 1.f);
455                         CLAMP(co[1], 0.f, 1.f);
456                         CLAMP(co[2], 0.f, 1.f);
457                         break;
458                 }
459         }
460
461         for (ch = 0; ch < depth; ch++) {
462                 float *dataset = vd->dataset + ch*vd->resol[0]*vd->resol[1]*vd->resol[2];
463                 float *result = &texres->tin;
464
465                 if (vd->data_type == TEX_VD_RGBA_PREMUL) {
466                         switch (ch) {
467                                 case 0:
468                                         result = &texres->tr;
469                                         break;
470                                 case 1:
471                                         result = &texres->tg;
472                                         break;
473                                 case 2:
474                                         result = &texres->tb;
475                                         break;
476                         }
477                 }
478
479                 switch (vd->interp_type) {
480                         case TEX_VD_NEARESTNEIGHBOR:
481                                 *result = BLI_voxel_sample_nearest(dataset, vd->resol, co);
482                                 break;  
483                         case TEX_VD_LINEAR:
484                                 *result = BLI_voxel_sample_trilinear(dataset, vd->resol, co);
485                                 break;
486                         case TEX_VD_QUADRATIC:
487                                 *result = BLI_voxel_sample_triquadratic(dataset, vd->resol, co);
488                                 break;
489                         case TEX_VD_TRICUBIC_CATROM:
490                         case TEX_VD_TRICUBIC_BSPLINE:
491                                 *result = BLI_voxel_sample_tricubic(dataset, vd->resol, co, (vd->interp_type == TEX_VD_TRICUBIC_BSPLINE));
492                                 break;
493                 }
494         }
495
496         a = texres->tin;
497         texres->tin *= vd->int_multiplier;
498         BRICONT;
499         
500         if (vd->data_type == TEX_VD_RGBA_PREMUL) {
501                 /* unmultiply */
502                 if (a>0.001f) {
503                         texres->tr /= a;
504                         texres->tg /= a;
505                         texres->tb /= a;
506                 }
507                 texres->talpha = 1;
508         }
509         else {
510                 texres->tr = texres->tin;
511                 texres->tg = texres->tin;
512                 texres->tb = texres->tin;
513         }
514
515         texres->ta = texres->tin;
516         BRICONTRGB;
517         
518         return retval;
519 }