1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/**
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): Raul Fernandez Hernandez (Farsthary), Matt Ebb.
*
* ***** END GPL LICENSE BLOCK *****
*/
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "MEM_guardedalloc.h"
#include "BLI_math.h"
#include "BLI_blenlib.h"
#include "BLI_voxel.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
#include "BKE_global.h"
#include "BKE_image.h"
#include "BKE_main.h"
#include "BKE_modifier.h"
#include "smoke_API.h"
#include "DNA_texture_types.h"
#include "DNA_object_types.h"
#include "DNA_modifier_types.h"
#include "DNA_smoke_types.h"
#include "render_types.h"
#include "renderdatabase.h"
#include "texture.h"
#include "voxeldata.h"
void load_frame_blendervoxel(FILE *fp, float *F, int size, int frame, int offset)
{
fseek(fp,frame*size*sizeof(float)+offset,0);
fread(F,sizeof(float),size,fp);
}
void load_frame_raw8(FILE *fp, float *F, int size, int frame)
{
char *tmp;
int i;
tmp = (char *)MEM_mallocN(sizeof(char)*size, "temporary voxel file reading storage");
fseek(fp,(frame-1)*size*sizeof(char),0);
fread(tmp, sizeof(char), size, fp);
for (i=0; i<size; i++) {
F[i] = (float)tmp[i] / 256.f;
}
MEM_freeN(tmp);
}
void load_frame_image_sequence(Render *re, VoxelData *vd, Tex *tex)
{
ImBuf *ibuf;
Image *ima = tex->ima;
ImageUser *iuser = &tex->iuser;
int x=0, y=0, z=0;
float *rf;
if (!ima || !iuser) return;
ima->source = IMA_SRC_SEQUENCE;
iuser->framenr = 1 + iuser->offset;
/* find the first valid ibuf and use it to initialise the resolution of the data set */
/* need to do this in advance so we know how much memory to allocate */
ibuf= BKE_image_get_ibuf(ima, iuser);
while (!ibuf && (iuser->framenr < iuser->frames)) {
iuser->framenr++;
ibuf= BKE_image_get_ibuf(ima, iuser);
}
if (!ibuf) return;
if (!ibuf->rect_float) IMB_float_from_rect(ibuf);
vd->flag |= TEX_VD_STILL;
vd->resol[0] = ibuf->x;
vd->resol[1] = ibuf->y;
vd->resol[2] = iuser->frames;
vd->dataset = MEM_mapallocN(sizeof(float)*(vd->resol[0])*(vd->resol[1])*(vd->resol[2]), "voxel dataset");
for (z=0; z < iuser->frames; z++)
{
/* get a new ibuf for each frame */
if (z > 0) {
iuser->framenr++;
ibuf= BKE_image_get_ibuf(ima, iuser);
if (!ibuf) break;
if (!ibuf->rect_float) IMB_float_from_rect(ibuf);
}
rf = ibuf->rect_float;
for (y=0; y < ibuf->y; y++)
{
for (x=0; x < ibuf->x; x++)
{
/* currently converted to monchrome */
vd->dataset[ V_I(x, y, z, vd->resol) ] = (rf[0] + rf[1] + rf[2])*0.333f;
rf +=4;
}
}
BKE_image_free_anim_ibufs(ima, iuser->framenr);
}
}
void write_voxeldata_header(struct VoxelDataHeader *h, FILE *fp)
{
fwrite(h,sizeof(struct VoxelDataHeader),1,fp);
}
void read_voxeldata_header(FILE *fp, struct VoxelData *vd)
{
VoxelDataHeader *h=(VoxelDataHeader *)MEM_mallocN(sizeof(VoxelDataHeader), "voxel data header");
rewind(fp);
fread(h,sizeof(VoxelDataHeader),1,fp);
vd->resol[0]=h->resolX;
vd->resol[1]=h->resolY;
vd->resol[2]=h->resolZ;
MEM_freeN(h);
}
void init_frame_smoke(Render *re, VoxelData *vd, Tex *tex)
{
Object *ob;
ModifierData *md;
vd->dataset = NULL;
if (vd->object == NULL) return;
ob= vd->object;
/* draw code for smoke */
if( (md = (ModifierData *)modifiers_findByType(ob, eModifierType_Smoke)) )
{
SmokeModifierData *smd = (SmokeModifierData *)md;
if(smd->domain && smd->domain->fluid) {
if (vd->smoked_type == TEX_VD_SMOKEHEAT) {
int totRes;
float *heat;
int i;
VECCOPY(vd->resol, smd->domain->res);
totRes = (vd->resol[0])*(vd->resol[1])*(vd->resol[2]);
// scaling heat values from -2.0-2.0 to 0.0-1.0
vd->dataset = MEM_mapallocN(sizeof(float)*(totRes), "smoke data");
heat = smoke_get_heat(smd->domain->fluid);
for (i=0; i<totRes; i++)
{
vd->dataset[i] = (heat[i]+2.0f)/4.0f;
}
//vd->dataset = smoke_get_heat(smd->domain->fluid);
}
else if (vd->smoked_type == TEX_VD_SMOKEVEL) {
int totRes;
float *xvel, *yvel, *zvel;
int i;
VECCOPY(vd->resol, smd->domain->res);
totRes = (vd->resol[0])*(vd->resol[1])*(vd->resol[2]);
// scaling heat values from -2.0-2.0 to 0.0-1.0
vd->dataset = MEM_mapallocN(sizeof(float)*(totRes), "smoke data");
xvel = smoke_get_velocity_x(smd->domain->fluid);
yvel = smoke_get_velocity_y(smd->domain->fluid);
zvel = smoke_get_velocity_z(smd->domain->fluid);
for (i=0; i<totRes; i++)
{
vd->dataset[i] = sqrt(xvel[i]*xvel[i] + yvel[i]*yvel[i] + zvel[i]*zvel[i])*3.0f;
}
}
else {
if (smd->domain->flags & MOD_SMOKE_HIGHRES) {
smoke_turbulence_get_res(smd->domain->wt, vd->resol);
vd->dataset = smoke_turbulence_get_density(smd->domain->wt);
} else {
VECCOPY(vd->resol, smd->domain->res);
vd->dataset = smoke_get_density(smd->domain->fluid);
}
} // end of fluid condition
}
}
}
void cache_voxeldata(struct Render *re,Tex *tex)
{
VoxelData *vd = tex->vd;
FILE *fp;
int size;
int curframe;
if (!vd) return;
/* image sequence gets special treatment */
if (vd->file_format == TEX_VD_IMAGE_SEQUENCE) {
load_frame_image_sequence(re, vd, tex);
return;
} else if (vd->file_format == TEX_VD_SMOKE) {
init_frame_smoke(re, vd, tex);
return;
}
if (!BLI_exists(vd->source_path)) return;
fp = fopen(vd->source_path,"rb");
if (!fp) return;
if (vd->file_format == TEX_VD_BLENDERVOXEL)
read_voxeldata_header(fp, vd);
size = (vd->resol[0])*(vd->resol[1])*(vd->resol[2]);
vd->dataset = MEM_mapallocN(sizeof(float)*size, "voxel dataset");
if (vd->flag & TEX_VD_STILL) curframe = vd->still_frame;
else curframe = re->r.cfra;
switch(vd->file_format) {
case TEX_VD_BLENDERVOXEL:
load_frame_blendervoxel(fp, vd->dataset, size, curframe-1, sizeof(VoxelDataHeader));
break;
case TEX_VD_RAW_8BIT:
load_frame_raw8(fp, vd->dataset, size, curframe);
break;
}
fclose(fp);
}
void make_voxeldata(struct Render *re)
{
Tex *tex;
re->i.infostr= "Loading voxel datasets";
re->stats_draw(re->sdh, &re->i);
/* XXX: should be doing only textures used in this render */
for (tex= G.main->tex.first; tex; tex= tex->id.next) {
if(tex->id.us && tex->type==TEX_VOXELDATA) {
cache_voxeldata(re, tex);
}
}
re->i.infostr= NULL;
re->stats_draw(re->sdh, &re->i);
}
static void free_voxeldata_one(Render *re, Tex *tex)
{
VoxelData *vd = tex->vd;
if (vd->dataset) {
if(vd->file_format != TEX_VD_SMOKE)
MEM_freeN(vd->dataset);
vd->dataset = NULL;
}
}
void free_voxeldata(Render *re)
{
Tex *tex;
for (tex= G.main->tex.first; tex; tex= tex->id.next) {
if(tex->id.us && tex->type==TEX_VOXELDATA) {
free_voxeldata_one(re, tex);
}
}
}
int voxeldatatex(struct Tex *tex, float *texvec, struct TexResult *texres)
{
int retval = TEX_INT;
VoxelData *vd = tex->vd;
float co[3], offset[3] = {0.5, 0.5, 0.5};
if ((!vd) || (vd->dataset==NULL)) {
texres->tin = 0.0f;
return 0;
}
/* scale lookup from 0.0-1.0 (original location) to -1.0, 1.0, consistent with image texture tex coords */
/* in implementation this works backwards, bringing sample locations from -1.0, 1.0
* to the range 0.0, 1.0, before looking up in the voxel structure. */
copy_v3_v3(co, texvec);
mul_v3_fl(co, 0.5f);
add_v3_v3v3(co, co, offset);
/* co is now in the range 0.0, 1.0 */
switch (tex->extend) {
case TEX_CLIP:
{
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)) {
texres->tin = 0.f;
return retval;
}
break;
}
case TEX_REPEAT:
{
co[0] = co[0] - floor(co[0]);
co[1] = co[1] - floor(co[1]);
co[2] = co[2] - floor(co[2]);
break;
}
case TEX_EXTEND:
{
CLAMP(co[0], 0.f, 1.f);
CLAMP(co[1], 0.f, 1.f);
CLAMP(co[2], 0.f, 1.f);
break;
}
}
switch (vd->interp_type) {
case TEX_VD_NEARESTNEIGHBOR:
texres->tin = voxel_sample_nearest(vd->dataset, vd->resol, co);
break;
case TEX_VD_LINEAR:
texres->tin = voxel_sample_trilinear(vd->dataset, vd->resol, co);
break;
case TEX_VD_QUADRATIC:
texres->tin = voxel_sample_triquadratic(vd->dataset, vd->resol, co);
break;
case TEX_VD_TRICUBIC_CATROM:
case TEX_VD_TRICUBIC_BSPLINE:
texres->tin = voxel_sample_tricubic(vd->dataset, vd->resol, co, (vd->interp_type == TEX_VD_TRICUBIC_BSPLINE));
break;
}
texres->tin *= vd->int_multiplier;
BRICONT;
texres->tr = texres->tin;
texres->tg = texres->tin;
texres->tb = texres->tin;
texres->ta = texres->tin;
BRICONTRGB;
return retval;
}
|