Fix T38873: Crashing on undo of ocean modifier.
[blender.git] / source / blender / blenkernel / intern / ocean.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  * Contributors: Matt Ebb, Hamed Zaghaghi
22  * Based on original code by Drew Whitehouse / Houdini Ocean Toolkit
23  * OpenMP hints by Christian Schnellhammer
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/ocean.c
29  *  \ingroup bke
30  */
31
32 #include <math.h>
33 #include <stdlib.h>
34
35 #include <string.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_scene_types.h"
40
41 #include "BLI_math.h"
42 #include "BLI_path_util.h"
43 #include "BLI_rand.h"
44 #include "BLI_string.h"
45 #include "BLI_threads.h"
46 #include "BLI_utildefines.h"
47
48 #include "BKE_image.h"
49 #include "BKE_ocean.h"
50
51 #include "IMB_imbuf.h"
52 #include "IMB_imbuf_types.h"
53
54 #include "RE_render_ext.h"
55
56 #ifdef WITH_OCEANSIM
57
58 /* Ocean code */
59 #include "fftw3.h"
60
61 #define GRAVITY  9.81f
62
63 typedef struct Ocean {
64         /* ********* input parameters to the sim ********* */
65         float _V;
66         float _l;
67         float _w;
68         float _A;
69         float _damp_reflections;
70         float _wind_alignment;
71         float _depth;
72
73         float _wx;
74         float _wz;
75
76         float _L;
77
78         /* dimensions of computational grid */
79         int _M;
80         int _N;
81
82         /* spatial size of computational grid */
83         float _Lx;
84         float _Lz;
85
86         float normalize_factor;                 /* init w */
87         float time;
88
89         short _do_disp_y;
90         short _do_normals;
91         short _do_chop;
92         short _do_jacobian;
93
94         /* mutex for threaded texture access */
95         ThreadRWMutex oceanmutex;
96
97         /* ********* sim data arrays ********* */
98
99         /* two dimensional arrays of complex */
100         fftw_complex *_fft_in;          /* init w       sim w */
101         fftw_complex *_fft_in_x;        /* init w       sim w */
102         fftw_complex *_fft_in_z;        /* init w       sim w */
103         fftw_complex *_fft_in_jxx;      /* init w       sim w */
104         fftw_complex *_fft_in_jzz;      /* init w       sim w */
105         fftw_complex *_fft_in_jxz;      /* init w       sim w */
106         fftw_complex *_fft_in_nx;       /* init w       sim w */
107         fftw_complex *_fft_in_nz;       /* init w       sim w */
108         fftw_complex *_htilda;          /* init w       sim w (only once) */
109
110         /* fftw "plans" */
111         fftw_plan _disp_y_plan;         /* init w       sim r */
112         fftw_plan _disp_x_plan;         /* init w       sim r */
113         fftw_plan _disp_z_plan;         /* init w       sim r */
114         fftw_plan _N_x_plan;            /* init w       sim r */
115         fftw_plan _N_z_plan;            /* init w       sim r */
116         fftw_plan _Jxx_plan;            /* init w       sim r */
117         fftw_plan _Jxz_plan;            /* init w       sim r */
118         fftw_plan _Jzz_plan;            /* init w       sim r */
119
120         /* two dimensional arrays of float */
121         double *_disp_y;                /* init w       sim w via plan? */
122         double *_N_x;                   /* init w       sim w via plan? */
123         /* all member of this array has same values, so convert this array to a float to reduce memory usage (MEM01)*/
124         /*float * _N_y; */
125         double _N_y;                    /*                      sim w ********* can be rearranged? */
126         double *_N_z;                   /* init w       sim w via plan? */
127         double *_disp_x;                /* init w       sim w via plan? */
128         double *_disp_z;                /* init w       sim w via plan? */
129
130         /* two dimensional arrays of float */
131         /* Jacobian and minimum eigenvalue */
132         double *_Jxx;                   /* init w       sim w */
133         double *_Jzz;                   /* init w       sim w */
134         double *_Jxz;                   /* init w       sim w */
135
136         /* one dimensional float array */
137         float *_kx;                     /* init w       sim r */
138         float *_kz;                     /* init w       sim r */
139
140         /* two dimensional complex array */
141         fftw_complex *_h0;              /* init w       sim r */
142         fftw_complex *_h0_minus;        /* init w       sim r */
143
144         /* two dimensional float array */
145         float *_k;                      /* init w       sim r */
146 } Ocean;
147
148
149
150 static float nextfr(RNG *rng, float min, float max)
151 {
152         return BLI_rng_get_float(rng) * (min - max) + max;
153 }
154
155 static float gaussRand(RNG *rng)
156 {
157         /* Note: to avoid numerical problems with very small numbers, we make these variables singe-precision floats,
158          * but later we call the double-precision log() and sqrt() functions instead of logf() and sqrtf().
159          */ 
160         float x;
161         float y;
162         float length2;
163
164         do {
165                 x = (float) (nextfr(rng, -1, 1));
166                 y = (float)(nextfr(rng, -1, 1));
167                 length2 = x * x + y * y;
168         } while (length2 >= 1 || length2 == 0);
169
170         return x * sqrtf(-2.0f * logf(length2) / length2);
171 }
172
173 /**
174  * Some useful functions
175  */
176 MINLINE float catrom(float p0, float p1, float p2, float p3, float f)
177 {
178         return 0.5f * ((2.0f * p1) +
179                        (-p0 + p2) * f +
180                        (2.0f * p0 - 5.0f * p1 + 4.0f * p2 - p3) * f * f +
181                        (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * f * f * f);
182 }
183
184 MINLINE float omega(float k, float depth)
185 {
186         return sqrtf(GRAVITY * k * tanhf(k * depth));
187 }
188
189 /* modified Phillips spectrum */
190 static float Ph(struct Ocean *o, float kx, float kz)
191 {
192         float tmp;
193         float k2 = kx * kx + kz * kz;
194
195         if (k2 == 0.0f) {
196                 return 0.0f; /* no DC component */
197         }
198
199         /* damp out the waves going in the direction opposite the wind */
200         tmp = (o->_wx * kx + o->_wz * kz) / sqrtf(k2);
201         if (tmp < 0) {
202                 tmp *= o->_damp_reflections;
203         }
204
205         return o->_A * expf(-1.0f / (k2 * (o->_L * o->_L))) * expf(-k2 * (o->_l * o->_l)) *
206                powf(fabsf(tmp), o->_wind_alignment) / (k2 * k2);
207 }
208
209 static void compute_eigenstuff(struct OceanResult *ocr, float jxx, float jzz, float jxz)
210 {
211         float a, b, qplus, qminus;
212         a = jxx + jzz;
213         b = sqrt((jxx - jzz) * (jxx - jzz) + 4 * jxz * jxz);
214
215         ocr->Jminus = 0.5f * (a - b);
216         ocr->Jplus  = 0.5f * (a + b);
217
218         qplus  = (ocr->Jplus  - jxx) / jxz;
219         qminus = (ocr->Jminus - jxx) / jxz;
220
221         a = sqrt(1 + qplus * qplus);
222         b = sqrt(1 + qminus * qminus);
223
224         ocr->Eplus[0] = 1.0f / a;
225         ocr->Eplus[1] = 0.0f;
226         ocr->Eplus[2] = qplus / a;
227
228         ocr->Eminus[0] = 1.0f / b;
229         ocr->Eminus[1] = 0.0f;
230         ocr->Eminus[2] = qminus / b;
231 }
232
233 /*
234  * instead of Complex.h
235  * in fftw.h "fftw_complex" typedefed as double[2]
236  * below you can see functions are needed to work with such complex numbers.
237  * */
238 static void init_complex(fftw_complex cmpl, float real, float image)
239 {
240         cmpl[0] = real;
241         cmpl[1] = image;
242 }
243
244 #if 0   /* unused */
245 static void add_complex_f(fftw_complex res, fftw_complex cmpl, float f)
246 {
247         res[0] = cmpl[0] + f;
248         res[1] = cmpl[1];
249 }
250 #endif
251
252 static void add_comlex_c(fftw_complex res, fftw_complex cmpl1, fftw_complex cmpl2)
253 {
254         res[0] = cmpl1[0] + cmpl2[0];
255         res[1] = cmpl1[1] + cmpl2[1];
256 }
257
258 static void mul_complex_f(fftw_complex res, fftw_complex cmpl, float f)
259 {
260         res[0] = cmpl[0] * (double)f;
261         res[1] = cmpl[1] * (double)f;
262 }
263
264 static void mul_complex_c(fftw_complex res, fftw_complex cmpl1, fftw_complex cmpl2)
265 {
266         fftwf_complex temp;
267         temp[0] = cmpl1[0] * cmpl2[0] - cmpl1[1] * cmpl2[1];
268         temp[1] = cmpl1[0] * cmpl2[1] + cmpl1[1] * cmpl2[0];
269         res[0] = temp[0];
270         res[1] = temp[1];
271 }
272
273 static float real_c(fftw_complex cmpl)
274 {
275         return cmpl[0];
276 }
277
278 static float image_c(fftw_complex cmpl)
279 {
280         return cmpl[1];
281 }
282
283 static void conj_complex(fftw_complex res, fftw_complex cmpl1)
284 {
285         res[0] = cmpl1[0];
286         res[1] = -cmpl1[1];
287 }
288
289 static void exp_complex(fftw_complex res, fftw_complex cmpl)
290 {
291         float r = expf(cmpl[0]);
292
293         res[0] = cosf(cmpl[1]) * r;
294         res[1] = sinf(cmpl[1]) * r;
295 }
296
297 float BKE_ocean_jminus_to_foam(float jminus, float coverage)
298 {
299         float foam = jminus * -0.005f + coverage;
300         CLAMP(foam, 0.0f, 1.0f);
301         return foam * foam;
302 }
303
304 void BKE_ocean_eval_uv(struct Ocean *oc, struct OceanResult *ocr, float u, float v)
305 {
306         int i0, i1, j0, j1;
307         float frac_x, frac_z;
308         float uu, vv;
309
310         /* first wrap the texture so 0 <= (u, v) < 1 */
311         u = fmodf(u, 1.0f);
312         v = fmodf(v, 1.0f);
313
314         if (u < 0) u += 1.0f;
315         if (v < 0) v += 1.0f;
316
317         BLI_rw_mutex_lock(&oc->oceanmutex, THREAD_LOCK_READ);
318
319         uu = u * oc->_M;
320         vv = v * oc->_N;
321
322         i0 = (int)floor(uu);
323         j0 = (int)floor(vv);
324
325         i1 = (i0 + 1);
326         j1 = (j0 + 1);
327
328         frac_x = uu - i0;
329         frac_z = vv - j0;
330
331         i0 = i0 % oc->_M;
332         j0 = j0 % oc->_N;
333
334         i1 = i1 % oc->_M;
335         j1 = j1 % oc->_N;
336
337
338 #define BILERP(m) (interpf(interpf(m[i1 * oc->_N + j1], m[i0 * oc->_N + j1], frac_x), \
339                            interpf(m[i1 * oc->_N + j0], m[i0 * oc->_N + j0], frac_x), \
340                            frac_z))
341         {
342                 if (oc->_do_disp_y) {
343                         ocr->disp[1] = BILERP(oc->_disp_y);
344                 }
345
346                 if (oc->_do_normals) {
347                         ocr->normal[0] = BILERP(oc->_N_x);
348                         ocr->normal[1] = oc->_N_y /*BILERP(oc->_N_y) (MEM01)*/;
349                         ocr->normal[2] = BILERP(oc->_N_z);
350                 }
351
352                 if (oc->_do_chop) {
353                         ocr->disp[0] = BILERP(oc->_disp_x);
354                         ocr->disp[2] = BILERP(oc->_disp_z);
355                 }
356                 else {
357                         ocr->disp[0] = 0.0;
358                         ocr->disp[2] = 0.0;
359                 }
360
361                 if (oc->_do_jacobian) {
362                         compute_eigenstuff(ocr, BILERP(oc->_Jxx), BILERP(oc->_Jzz), BILERP(oc->_Jxz));
363                 }
364         }
365 #undef BILERP
366
367         BLI_rw_mutex_unlock(&oc->oceanmutex);
368 }
369
370 /* use catmullrom interpolation rather than linear */
371 void BKE_ocean_eval_uv_catrom(struct Ocean *oc, struct OceanResult *ocr, float u, float v)
372 {
373         int i0, i1, i2, i3, j0, j1, j2, j3;
374         float frac_x, frac_z;
375         float uu, vv;
376
377         /* first wrap the texture so 0 <= (u, v) < 1 */
378         u = fmod(u, 1.0f);
379         v = fmod(v, 1.0f);
380
381         if (u < 0) u += 1.0f;
382         if (v < 0) v += 1.0f;
383
384         BLI_rw_mutex_lock(&oc->oceanmutex, THREAD_LOCK_READ);
385
386         uu = u * oc->_M;
387         vv = v * oc->_N;
388
389         i1 = (int)floor(uu);
390         j1 = (int)floor(vv);
391
392         i2 = (i1 + 1);
393         j2 = (j1 + 1);
394
395         frac_x = uu - i1;
396         frac_z = vv - j1;
397
398         i1 = i1 % oc->_M;
399         j1 = j1 % oc->_N;
400
401         i2 = i2 % oc->_M;
402         j2 = j2 % oc->_N;
403
404         i0 = (i1 - 1);
405         i3 = (i2 + 1);
406         i0 = i0 <   0 ? i0 + oc->_M : i0;
407         i3 = i3 >= oc->_M ? i3 - oc->_M : i3;
408
409         j0 = (j1 - 1);
410         j3 = (j2 + 1);
411         j0 = j0 <   0 ? j0 + oc->_N : j0;
412         j3 = j3 >= oc->_N ? j3 - oc->_N : j3;
413
414 #define INTERP(m) catrom(catrom(m[i0 * oc->_N + j0], m[i1 * oc->_N + j0], \
415                                 m[i2 * oc->_N + j0], m[i3 * oc->_N + j0], frac_x), \
416                          catrom(m[i0 * oc->_N + j1], m[i1 * oc->_N + j1], \
417                                 m[i2 * oc->_N + j1], m[i3 * oc->_N + j1], frac_x), \
418                          catrom(m[i0 * oc->_N + j2], m[i1 * oc->_N + j2], \
419                                 m[i2 * oc->_N + j2], m[i3 * oc->_N + j2], frac_x), \
420                          catrom(m[i0 * oc->_N + j3], m[i1 * oc->_N + j3], \
421                                 m[i2 * oc->_N + j3], m[i3 * oc->_N + j3], frac_x), \
422                          frac_z)
423
424         {
425                 if (oc->_do_disp_y) {
426                         ocr->disp[1] = INTERP(oc->_disp_y);
427                 }
428                 if (oc->_do_normals) {
429                         ocr->normal[0] = INTERP(oc->_N_x);
430                         ocr->normal[1] = oc->_N_y /*INTERP(oc->_N_y) (MEM01)*/;
431                         ocr->normal[2] = INTERP(oc->_N_z);
432                 }
433                 if (oc->_do_chop) {
434                         ocr->disp[0] = INTERP(oc->_disp_x);
435                         ocr->disp[2] = INTERP(oc->_disp_z);
436                 }
437                 else {
438                         ocr->disp[0] = 0.0;
439                         ocr->disp[2] = 0.0;
440                 }
441
442                 if (oc->_do_jacobian) {
443                         compute_eigenstuff(ocr, INTERP(oc->_Jxx), INTERP(oc->_Jzz), INTERP(oc->_Jxz));
444                 }
445         }
446 #undef INTERP
447
448         BLI_rw_mutex_unlock(&oc->oceanmutex);
449
450 }
451
452 void BKE_ocean_eval_xz(struct Ocean *oc, struct OceanResult *ocr, float x, float z)
453 {
454         BKE_ocean_eval_uv(oc, ocr, x / oc->_Lx, z / oc->_Lz);
455 }
456
457 void BKE_ocean_eval_xz_catrom(struct Ocean *oc, struct OceanResult *ocr, float x, float z)
458 {
459         BKE_ocean_eval_uv_catrom(oc, ocr, x / oc->_Lx, z / oc->_Lz);
460 }
461
462 /* note that this doesn't wrap properly for i, j < 0, but its not really meant for that being just a way to get
463  * the raw data out to save in some image format.
464  */
465 void BKE_ocean_eval_ij(struct Ocean *oc, struct OceanResult *ocr, int i, int j)
466 {
467         BLI_rw_mutex_lock(&oc->oceanmutex, THREAD_LOCK_READ);
468
469         i = abs(i) % oc->_M;
470         j = abs(j) % oc->_N;
471
472         ocr->disp[1] = oc->_do_disp_y ? (float)oc->_disp_y[i * oc->_N + j] : 0.0f;
473
474         if (oc->_do_chop) {
475                 ocr->disp[0] = oc->_disp_x[i * oc->_N + j];
476                 ocr->disp[2] = oc->_disp_z[i * oc->_N + j];
477         }
478         else {
479                 ocr->disp[0] = 0.0f;
480                 ocr->disp[2] = 0.0f;
481         }
482
483         if (oc->_do_normals) {
484                 ocr->normal[0] = oc->_N_x[i * oc->_N + j];
485                 ocr->normal[1] = oc->_N_y  /* oc->_N_y[i * oc->_N + j] (MEM01) */;
486                 ocr->normal[2] = oc->_N_z[i * oc->_N + j];
487
488                 normalize_v3(ocr->normal);
489         }
490
491         if (oc->_do_jacobian) {
492                 compute_eigenstuff(ocr, oc->_Jxx[i * oc->_N + j], oc->_Jzz[i * oc->_N + j], oc->_Jxz[i * oc->_N + j]);
493         }
494
495         BLI_rw_mutex_unlock(&oc->oceanmutex);
496 }
497
498 void BKE_simulate_ocean(struct Ocean *o, float t, float scale, float chop_amount)
499 {
500         int i, j;
501
502         scale *= o->normalize_factor;
503
504         BLI_rw_mutex_lock(&o->oceanmutex, THREAD_LOCK_WRITE);
505
506         /* compute a new htilda */
507 #pragma omp parallel for private(i, j)
508         for (i = 0; i < o->_M; ++i) {
509                 /* note the <= _N/2 here, see the fftw doco about the mechanics of the complex->real fft storage */
510                 for (j = 0; j <= o->_N / 2; ++j) {
511                         fftw_complex exp_param1;
512                         fftw_complex exp_param2;
513                         fftw_complex conj_param;
514
515
516                         init_complex(exp_param1, 0.0, omega(o->_k[i * (1 + o->_N / 2) + j], o->_depth) * t);
517                         init_complex(exp_param2, 0.0, -omega(o->_k[i * (1 + o->_N / 2) + j], o->_depth) * t);
518                         exp_complex(exp_param1, exp_param1);
519                         exp_complex(exp_param2, exp_param2);
520                         conj_complex(conj_param, o->_h0_minus[i * o->_N + j]);
521
522                         mul_complex_c(exp_param1, o->_h0[i * o->_N + j], exp_param1);
523                         mul_complex_c(exp_param2, conj_param, exp_param2);
524
525                         add_comlex_c(o->_htilda[i * (1 + o->_N / 2) + j], exp_param1, exp_param2);
526                         mul_complex_f(o->_fft_in[i * (1 + o->_N / 2) + j], o->_htilda[i * (1 + o->_N / 2) + j], scale);
527                 }
528         }
529
530 #pragma omp parallel sections private(i, j)
531         {
532
533 #pragma omp section
534                 {
535                         if (o->_do_disp_y) {
536                                 /* y displacement */
537                                 fftw_execute(o->_disp_y_plan);
538                         }
539                 } /* section 1 */
540
541 #pragma omp section
542                 {
543                         if (o->_do_chop) {
544                                 /* x displacement */
545                                 for (i = 0; i < o->_M; ++i) {
546                                         for (j = 0; j <= o->_N / 2; ++j) {
547                                                 fftw_complex mul_param;
548                                                 fftw_complex minus_i;
549
550                                                 init_complex(minus_i, 0.0, -1.0);
551                                                 init_complex(mul_param, -scale, 0);
552                                                 mul_complex_f(mul_param, mul_param, chop_amount);
553                                                 mul_complex_c(mul_param, mul_param, minus_i);
554                                                 mul_complex_c(mul_param, mul_param, o->_htilda[i * (1 + o->_N / 2) + j]);
555                                                 mul_complex_f(mul_param, mul_param,
556                                                               ((o->_k[i * (1 + o->_N / 2) + j] == 0.0f) ?
557                                                                0.0f :
558                                                                o->_kx[i] / o->_k[i * (1 + o->_N / 2) + j]));
559                                                 init_complex(o->_fft_in_x[i * (1 + o->_N / 2) + j], real_c(mul_param), image_c(mul_param));
560                                         }
561                                 }
562                                 fftw_execute(o->_disp_x_plan);
563                         }
564                 } /* section 2 */
565
566 #pragma omp section
567                 {
568                         if (o->_do_chop) {
569                                 /* z displacement */
570                                 for (i = 0; i < o->_M; ++i) {
571                                         for (j = 0; j <= o->_N / 2; ++j) {
572                                                 fftw_complex mul_param;
573                                                 fftw_complex minus_i;
574
575                                                 init_complex(minus_i, 0.0, -1.0);
576                                                 init_complex(mul_param, -scale, 0);
577                                                 mul_complex_f(mul_param, mul_param, chop_amount);
578                                                 mul_complex_c(mul_param, mul_param, minus_i);
579                                                 mul_complex_c(mul_param, mul_param, o->_htilda[i * (1 + o->_N / 2) + j]);
580                                                 mul_complex_f(mul_param, mul_param,
581                                                               ((o->_k[i * (1 + o->_N / 2) + j] == 0.0f) ?
582                                                                0.0f :
583                                                                o->_kz[j] / o->_k[i * (1 + o->_N / 2) + j]));
584                                                 init_complex(o->_fft_in_z[i * (1 + o->_N / 2) + j], real_c(mul_param), image_c(mul_param));
585                                         }
586                                 }
587                                 fftw_execute(o->_disp_z_plan);
588                         }
589                 } /* section 3 */
590
591 #pragma omp section
592                 {
593                         if (o->_do_jacobian) {
594                                 /* Jxx */
595                                 for (i = 0; i < o->_M; ++i) {
596                                         for (j = 0; j <= o->_N / 2; ++j) {
597                                                 fftw_complex mul_param;
598
599                                                 /* init_complex(mul_param, -scale, 0); */
600                                                 init_complex(mul_param, -1, 0);
601
602                                                 mul_complex_f(mul_param, mul_param, chop_amount);
603                                                 mul_complex_c(mul_param, mul_param, o->_htilda[i * (1 + o->_N / 2) + j]);
604                                                 mul_complex_f(mul_param, mul_param,
605                                                               ((o->_k[i * (1 + o->_N / 2) + j] == 0.0f) ?
606                                                                0.0f :
607                                                                o->_kx[i] * o->_kx[i] / o->_k[i * (1 + o->_N / 2) + j]));
608                                                 init_complex(o->_fft_in_jxx[i * (1 + o->_N / 2) + j], real_c(mul_param), image_c(mul_param));
609                                         }
610                                 }
611                                 fftw_execute(o->_Jxx_plan);
612
613                                 for (i = 0; i < o->_M; ++i) {
614                                         for (j = 0; j < o->_N; ++j) {
615                                                 o->_Jxx[i * o->_N + j] += 1.0;
616                                         }
617                                 }
618                         }
619                 } /* section 4 */
620
621 #pragma omp section
622                 {
623                         if (o->_do_jacobian) {
624                                 /* Jzz */
625                                 for (i = 0; i < o->_M; ++i) {
626                                         for (j = 0; j <= o->_N / 2; ++j) {
627                                                 fftw_complex mul_param;
628
629                                                 /* init_complex(mul_param, -scale, 0); */
630                                                 init_complex(mul_param, -1, 0);
631
632                                                 mul_complex_f(mul_param, mul_param, chop_amount);
633                                                 mul_complex_c(mul_param, mul_param, o->_htilda[i * (1 + o->_N / 2) + j]);
634                                                 mul_complex_f(mul_param, mul_param,
635                                                               ((o->_k[i * (1 + o->_N / 2) + j] == 0.0f) ?
636                                                                0.0f :
637                                                                o->_kz[j] * o->_kz[j] / o->_k[i * (1 + o->_N / 2) + j]));
638                                                 init_complex(o->_fft_in_jzz[i * (1 + o->_N / 2) + j], real_c(mul_param), image_c(mul_param));
639                                         }
640                                 }
641                                 fftw_execute(o->_Jzz_plan);
642                                 for (i = 0; i < o->_M; ++i) {
643                                         for (j = 0; j < o->_N; ++j) {
644                                                 o->_Jzz[i * o->_N + j] += 1.0;
645                                         }
646                                 }
647                         }
648                 } /* section 5 */
649
650 #pragma omp section
651                 {
652                         if (o->_do_jacobian) {
653                                 /* Jxz */
654                                 for (i = 0; i < o->_M; ++i) {
655                                         for (j = 0; j <= o->_N / 2; ++j) {
656                                                 fftw_complex mul_param;
657
658                                                 /* init_complex(mul_param, -scale, 0); */
659                                                 init_complex(mul_param, -1, 0);
660
661                                                 mul_complex_f(mul_param, mul_param, chop_amount);
662                                                 mul_complex_c(mul_param, mul_param, o->_htilda[i * (1 + o->_N / 2) + j]);
663                                                 mul_complex_f(mul_param, mul_param,
664                                                               ((o->_k[i * (1 + o->_N / 2) + j] == 0.0f) ?
665                                                                0.0f :
666                                                                o->_kx[i] * o->_kz[j] / o->_k[i * (1 + o->_N / 2) + j]));
667                                                 init_complex(o->_fft_in_jxz[i * (1 + o->_N / 2) + j], real_c(mul_param), image_c(mul_param));
668                                         }
669                                 }
670                                 fftw_execute(o->_Jxz_plan);
671                         }
672                 } /* section 6 */
673
674 #pragma omp section
675                 {
676                         /* fft normals */
677                         if (o->_do_normals) {
678                                 for (i = 0; i < o->_M; ++i) {
679                                         for (j = 0; j <= o->_N / 2; ++j) {
680                                                 fftw_complex mul_param;
681
682                                                 init_complex(mul_param, 0.0, -1.0);
683                                                 mul_complex_c(mul_param, mul_param, o->_htilda[i * (1 + o->_N / 2) + j]);
684                                                 mul_complex_f(mul_param, mul_param, o->_kx[i]);
685                                                 init_complex(o->_fft_in_nx[i * (1 + o->_N / 2) + j], real_c(mul_param), image_c(mul_param));
686                                         }
687                                 }
688                                 fftw_execute(o->_N_x_plan);
689
690                         }
691                 } /* section 7 */
692
693 #pragma omp section
694                 {
695                         if (o->_do_normals) {
696                                 for (i = 0; i < o->_M; ++i) {
697                                         for (j = 0; j <= o->_N / 2; ++j) {
698                                                 fftw_complex mul_param;
699
700                                                 init_complex(mul_param, 0.0, -1.0);
701                                                 mul_complex_c(mul_param, mul_param, o->_htilda[i * (1 + o->_N / 2) + j]);
702                                                 mul_complex_f(mul_param, mul_param, o->_kz[i]);
703                                                 init_complex(o->_fft_in_nz[i * (1 + o->_N / 2) + j], real_c(mul_param), image_c(mul_param));
704                                         }
705                                 }
706                                 fftw_execute(o->_N_z_plan);
707
708 #if 0
709                                 for (i = 0; i < o->_M; ++i) {
710                                         for (j = 0; j < o->_N; ++j) {
711                                                 o->_N_y[i * o->_N + j] = 1.0f / scale;
712                                         }
713                                 }
714                                 (MEM01)
715 #endif
716                                 o->_N_y = 1.0f / scale;
717                         }
718                 } /* section 8 */
719
720         } /* omp sections */
721
722         BLI_rw_mutex_unlock(&o->oceanmutex);
723 }
724
725 static void set_height_normalize_factor(struct Ocean *oc)
726 {
727         float res = 1.0;
728         float max_h = 0.0;
729
730         int i, j;
731
732         if (!oc->_do_disp_y) return;
733
734         oc->normalize_factor = 1.0;
735
736         BKE_simulate_ocean(oc, 0.0, 1.0, 0);
737
738         BLI_rw_mutex_lock(&oc->oceanmutex, THREAD_LOCK_READ);
739
740         for (i = 0; i < oc->_M; ++i) {
741                 for (j = 0; j < oc->_N; ++j) {
742                         if (max_h < fabsf(oc->_disp_y[i * oc->_N + j])) {
743                                 max_h = fabsf(oc->_disp_y[i * oc->_N + j]);
744                         }
745                 }
746         }
747
748         BLI_rw_mutex_unlock(&oc->oceanmutex);
749
750         if (max_h == 0.0f)
751                 max_h = 0.00001f;  /* just in case ... */
752
753         res = 1.0f / (max_h);
754
755         oc->normalize_factor = res;
756 }
757
758 struct Ocean *BKE_add_ocean(void)
759 {
760         Ocean *oc = MEM_callocN(sizeof(Ocean), "ocean sim data");
761
762         BLI_rw_mutex_init(&oc->oceanmutex);
763
764         return oc;
765 }
766
767 void BKE_init_ocean(struct Ocean *o, int M, int N, float Lx, float Lz, float V, float l, float A, float w, float damp,
768                     float alignment, float depth, float time, short do_height_field, short do_chop, short do_normals,
769                     short do_jacobian, int seed)
770 {
771         RNG *rng;
772         int i, j, ii;
773
774         BLI_rw_mutex_lock(&o->oceanmutex, THREAD_LOCK_WRITE);
775
776         o->_M = M;
777         o->_N = N;
778         o->_V = V;
779         o->_l = l;
780         o->_A = A;
781         o->_w = w;
782         o->_damp_reflections = 1.0f - damp;
783         o->_wind_alignment = alignment;
784         o->_depth = depth;
785         o->_Lx = Lx;
786         o->_Lz = Lz;
787         o->_wx = cos(w);
788         o->_wz = -sin(w); /* wave direction */
789         o->_L = V * V / GRAVITY;  /* largest wave for a given velocity V */
790         o->time = time;
791
792         o->_do_disp_y = do_height_field;
793         o->_do_normals = do_normals;
794         o->_do_chop = do_chop;
795         o->_do_jacobian = do_jacobian;
796
797         o->_k = (float *) MEM_mallocN(M * (1 + N / 2) * sizeof(float), "ocean_k");
798         o->_h0 = (fftw_complex *) MEM_mallocN(M * N * sizeof(fftw_complex), "ocean_h0");
799         o->_h0_minus = (fftw_complex *) MEM_mallocN(M * N * sizeof(fftw_complex), "ocean_h0_minus");
800         o->_kx = (float *) MEM_mallocN(o->_M * sizeof(float), "ocean_kx");
801         o->_kz = (float *) MEM_mallocN(o->_N * sizeof(float), "ocean_kz");
802
803         /* make this robust in the face of erroneous usage */
804         if (o->_Lx == 0.0f)
805                 o->_Lx = 0.001f;
806
807         if (o->_Lz == 0.0f)
808                 o->_Lz = 0.001f;
809
810         /* the +ve components and DC */
811         for (i = 0; i <= o->_M / 2; ++i)
812                 o->_kx[i] = 2.0f * (float)M_PI * i / o->_Lx;
813
814         /* the -ve components */
815         for (i = o->_M - 1, ii = 0; i > o->_M / 2; --i, ++ii)
816                 o->_kx[i] = -2.0f * (float)M_PI * ii / o->_Lx;
817
818         /* the +ve components and DC */
819         for (i = 0; i <= o->_N / 2; ++i)
820                 o->_kz[i] = 2.0f * (float)M_PI * i / o->_Lz;
821
822         /* the -ve components */
823         for (i = o->_N - 1, ii = 0; i > o->_N / 2; --i, ++ii)
824                 o->_kz[i] = -2.0f * (float)M_PI * ii / o->_Lz;
825
826         /* pre-calculate the k matrix */
827         for (i = 0; i < o->_M; ++i)
828                 for (j = 0; j <= o->_N / 2; ++j)
829                         o->_k[i * (1 + o->_N / 2) + j] = sqrt(o->_kx[i] * o->_kx[i] + o->_kz[j] * o->_kz[j]);
830
831         /*srand(seed);*/
832         rng = BLI_rng_new(seed);
833
834         for (i = 0; i < o->_M; ++i) {
835                 for (j = 0; j < o->_N; ++j) {
836                         float r1 = gaussRand(rng);
837                         float r2 = gaussRand(rng);
838
839                         fftw_complex r1r2;
840                         init_complex(r1r2, r1, r2);
841                         mul_complex_f(o->_h0[i * o->_N + j], r1r2, (float)(sqrt(Ph(o, o->_kx[i], o->_kz[j]) / 2.0f)));
842                         mul_complex_f(o->_h0_minus[i * o->_N + j], r1r2, (float)(sqrt(Ph(o, -o->_kx[i], -o->_kz[j]) / 2.0f)));
843                 }
844         }
845
846         o->_fft_in = (fftw_complex *)MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex), "ocean_fft_in");
847         o->_htilda = (fftw_complex *)MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex), "ocean_htilda");
848
849         BLI_lock_thread(LOCK_FFTW);
850
851         if (o->_do_disp_y) {
852                 o->_disp_y = (double *)MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_disp_y");
853                 o->_disp_y_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in, o->_disp_y, FFTW_ESTIMATE);
854         }
855
856         if (o->_do_normals) {
857                 o->_fft_in_nx = (fftw_complex *) MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex), "ocean_fft_in_nx");
858                 o->_fft_in_nz = (fftw_complex *) MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex), "ocean_fft_in_nz");
859
860                 o->_N_x = (double *)MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_N_x");
861                 /* o->_N_y = (float *) fftwf_malloc(o->_M * o->_N * sizeof(float)); (MEM01) */
862                 o->_N_z = (double *)MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_N_z");
863
864                 o->_N_x_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_nx, o->_N_x, FFTW_ESTIMATE);
865                 o->_N_z_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_nz, o->_N_z, FFTW_ESTIMATE);
866         }
867
868         if (o->_do_chop) {
869                 o->_fft_in_x = (fftw_complex *)MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex), "ocean_fft_in_x");
870                 o->_fft_in_z = (fftw_complex *)MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex), "ocean_fft_in_z");
871
872                 o->_disp_x = (double *)MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_disp_x");
873                 o->_disp_z = (double *)MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_disp_z");
874
875                 o->_disp_x_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_x, o->_disp_x, FFTW_ESTIMATE);
876                 o->_disp_z_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_z, o->_disp_z, FFTW_ESTIMATE);
877         }
878         if (o->_do_jacobian) {
879                 o->_fft_in_jxx = (fftw_complex *)MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex),
880                                                              "ocean_fft_in_jxx");
881                 o->_fft_in_jzz = (fftw_complex *)MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex),
882                                                              "ocean_fft_in_jzz");
883                 o->_fft_in_jxz = (fftw_complex *)MEM_mallocN(o->_M * (1 + o->_N / 2) * sizeof(fftw_complex),
884                                                              "ocean_fft_in_jxz");
885
886                 o->_Jxx = (double *)MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_Jxx");
887                 o->_Jzz = (double *)MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_Jzz");
888                 o->_Jxz = (double *)MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_Jxz");
889
890                 o->_Jxx_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_jxx, o->_Jxx, FFTW_ESTIMATE);
891                 o->_Jzz_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_jzz, o->_Jzz, FFTW_ESTIMATE);
892                 o->_Jxz_plan = fftw_plan_dft_c2r_2d(o->_M, o->_N, o->_fft_in_jxz, o->_Jxz, FFTW_ESTIMATE);
893         }
894
895         BLI_unlock_thread(LOCK_FFTW);
896
897         BLI_rw_mutex_unlock(&o->oceanmutex);
898
899         set_height_normalize_factor(o);
900
901         BLI_rng_free(rng);
902 }
903
904 void BKE_free_ocean_data(struct Ocean *oc)
905 {
906         if (!oc) return;
907
908         BLI_rw_mutex_lock(&oc->oceanmutex, THREAD_LOCK_WRITE);
909
910         BLI_lock_thread(LOCK_FFTW);
911
912         if (oc->_do_disp_y) {
913                 fftw_destroy_plan(oc->_disp_y_plan);
914                 MEM_freeN(oc->_disp_y);
915         }
916
917         if (oc->_do_normals) {
918                 MEM_freeN(oc->_fft_in_nx);
919                 MEM_freeN(oc->_fft_in_nz);
920                 fftw_destroy_plan(oc->_N_x_plan);
921                 fftw_destroy_plan(oc->_N_z_plan);
922                 MEM_freeN(oc->_N_x);
923                 /*fftwf_free(oc->_N_y); (MEM01)*/
924                 MEM_freeN(oc->_N_z);
925         }
926
927         if (oc->_do_chop) {
928                 MEM_freeN(oc->_fft_in_x);
929                 MEM_freeN(oc->_fft_in_z);
930                 fftw_destroy_plan(oc->_disp_x_plan);
931                 fftw_destroy_plan(oc->_disp_z_plan);
932                 MEM_freeN(oc->_disp_x);
933                 MEM_freeN(oc->_disp_z);
934         }
935
936         if (oc->_do_jacobian) {
937                 MEM_freeN(oc->_fft_in_jxx);
938                 MEM_freeN(oc->_fft_in_jzz);
939                 MEM_freeN(oc->_fft_in_jxz);
940                 fftw_destroy_plan(oc->_Jxx_plan);
941                 fftw_destroy_plan(oc->_Jzz_plan);
942                 fftw_destroy_plan(oc->_Jxz_plan);
943                 MEM_freeN(oc->_Jxx);
944                 MEM_freeN(oc->_Jzz);
945                 MEM_freeN(oc->_Jxz);
946         }
947
948         BLI_unlock_thread(LOCK_FFTW);
949
950         if (oc->_fft_in)
951                 MEM_freeN(oc->_fft_in);
952
953         /* check that ocean data has been initialized */
954         if (oc->_htilda) {
955                 MEM_freeN(oc->_htilda);
956                 MEM_freeN(oc->_k);
957                 MEM_freeN(oc->_h0);
958                 MEM_freeN(oc->_h0_minus);
959                 MEM_freeN(oc->_kx);
960                 MEM_freeN(oc->_kz);
961         }
962
963         BLI_rw_mutex_unlock(&oc->oceanmutex);
964 }
965
966 void BKE_free_ocean(struct Ocean *oc)
967 {
968         if (!oc) return;
969
970         BKE_free_ocean_data(oc);
971         BLI_rw_mutex_end(&oc->oceanmutex);
972
973         MEM_freeN(oc);
974 }
975
976 #undef GRAVITY
977
978
979 /* ********* Baking/Caching ********* */
980
981
982 #define CACHE_TYPE_DISPLACE 1
983 #define CACHE_TYPE_FOAM     2
984 #define CACHE_TYPE_NORMAL   3
985
986 static void cache_filename(char *string, const char *path, const char *relbase, int frame, int type)
987 {
988         char cachepath[FILE_MAX];
989         const char *fname;
990
991         switch (type) {
992                 case CACHE_TYPE_FOAM:
993                         fname = "foam_";
994                         break;
995                 case CACHE_TYPE_NORMAL:
996                         fname = "normal_";
997                         break;
998                 case CACHE_TYPE_DISPLACE:
999                 default:
1000                         fname = "disp_";
1001                         break;
1002         }
1003
1004         BLI_join_dirfile(cachepath, sizeof(cachepath), path, fname);
1005
1006         BKE_makepicstring_from_type(string, cachepath, relbase, frame, R_IMF_IMTYPE_OPENEXR, 1, TRUE);
1007 }
1008
1009 /* silly functions but useful to inline when the args do a lot of indirections */
1010 MINLINE void rgb_to_rgba_unit_alpha(float r_rgba[4], const float rgb[3])
1011 {
1012         r_rgba[0] = rgb[0];
1013         r_rgba[1] = rgb[1];
1014         r_rgba[2] = rgb[2];
1015         r_rgba[3] = 1.0f;
1016 }
1017 MINLINE void value_to_rgba_unit_alpha(float r_rgba[4], const float value)
1018 {
1019         r_rgba[0] = value;
1020         r_rgba[1] = value;
1021         r_rgba[2] = value;
1022         r_rgba[3] = 1.0f;
1023 }
1024
1025 void BKE_free_ocean_cache(struct OceanCache *och)
1026 {
1027         int i, f = 0;
1028
1029         if (!och) return;
1030
1031         if (och->ibufs_disp) {
1032                 for (i = och->start, f = 0; i <= och->end; i++, f++) {
1033                         if (och->ibufs_disp[f]) {
1034                                 IMB_freeImBuf(och->ibufs_disp[f]);
1035                         }
1036                 }
1037                 MEM_freeN(och->ibufs_disp);
1038         }
1039
1040         if (och->ibufs_foam) {
1041                 for (i = och->start, f = 0; i <= och->end; i++, f++) {
1042                         if (och->ibufs_foam[f]) {
1043                                 IMB_freeImBuf(och->ibufs_foam[f]);
1044                         }
1045                 }
1046                 MEM_freeN(och->ibufs_foam);
1047         }
1048
1049         if (och->ibufs_norm) {
1050                 for (i = och->start, f = 0; i <= och->end; i++, f++) {
1051                         if (och->ibufs_norm[f]) {
1052                                 IMB_freeImBuf(och->ibufs_norm[f]);
1053                         }
1054                 }
1055                 MEM_freeN(och->ibufs_norm);
1056         }
1057
1058         if (och->time)
1059                 MEM_freeN(och->time);
1060         MEM_freeN(och);
1061 }
1062
1063 void BKE_ocean_cache_eval_uv(struct OceanCache *och, struct OceanResult *ocr, int f, float u, float v)
1064 {
1065         int res_x = och->resolution_x;
1066         int res_y = och->resolution_y;
1067         float result[4];
1068
1069         u = fmod(u, 1.0);
1070         v = fmod(v, 1.0);
1071
1072         if (u < 0) u += 1.0f;
1073         if (v < 0) v += 1.0f;
1074
1075         if (och->ibufs_disp[f]) {
1076                 ibuf_sample(och->ibufs_disp[f], u, v, (1.0f / (float)res_x), (1.0f / (float)res_y), result);
1077                 copy_v3_v3(ocr->disp, result);
1078         }
1079
1080         if (och->ibufs_foam[f]) {
1081                 ibuf_sample(och->ibufs_foam[f], u, v, (1.0f / (float)res_x), (1.0f / (float)res_y), result);
1082                 ocr->foam = result[0];
1083         }
1084
1085         if (och->ibufs_norm[f]) {
1086                 ibuf_sample(och->ibufs_norm[f], u, v, (1.0f / (float)res_x), (1.0f / (float)res_y), result);
1087                 copy_v3_v3(ocr->normal, result);
1088         }
1089 }
1090
1091 void BKE_ocean_cache_eval_ij(struct OceanCache *och, struct OceanResult *ocr, int f, int i, int j)
1092 {
1093         const int res_x = och->resolution_x;
1094         const int res_y = och->resolution_y;
1095
1096         if (i < 0) i = -i;
1097         if (j < 0) j = -j;
1098
1099         i = i % res_x;
1100         j = j % res_y;
1101
1102         if (och->ibufs_disp[f]) {
1103                 copy_v3_v3(ocr->disp, &och->ibufs_disp[f]->rect_float[4 * (res_x * j + i)]);
1104         }
1105
1106         if (och->ibufs_foam[f]) {
1107                 ocr->foam = och->ibufs_foam[f]->rect_float[4 * (res_x * j + i)];
1108         }
1109
1110         if (och->ibufs_norm[f]) {
1111                 copy_v3_v3(ocr->normal, &och->ibufs_norm[f]->rect_float[4 * (res_x * j + i)]);
1112         }
1113 }
1114
1115 struct OceanCache *BKE_init_ocean_cache(const char *bakepath, const char *relbase, int start, int end, float wave_scale,
1116                                         float chop_amount, float foam_coverage, float foam_fade, int resolution)
1117 {
1118         OceanCache *och = MEM_callocN(sizeof(OceanCache), "ocean cache data");
1119
1120         och->bakepath = bakepath;
1121         och->relbase = relbase;
1122
1123         och->start = start;
1124         och->end = end;
1125         och->duration = (end - start) + 1;
1126         och->wave_scale = wave_scale;
1127         och->chop_amount = chop_amount;
1128         och->foam_coverage = foam_coverage;
1129         och->foam_fade = foam_fade;
1130         och->resolution_x = resolution * resolution;
1131         och->resolution_y = resolution * resolution;
1132
1133         och->ibufs_disp = MEM_callocN(sizeof(ImBuf *) * och->duration, "displacement imbuf pointer array");
1134         och->ibufs_foam = MEM_callocN(sizeof(ImBuf *) * och->duration, "foam imbuf pointer array");
1135         och->ibufs_norm = MEM_callocN(sizeof(ImBuf *) * och->duration, "normal imbuf pointer array");
1136
1137         och->time = NULL;
1138
1139         return och;
1140 }
1141
1142 void BKE_simulate_ocean_cache(struct OceanCache *och, int frame)
1143 {
1144         char string[FILE_MAX];
1145         int f = frame;
1146
1147         /* ibufs array is zero based, but filenames are based on frame numbers */
1148         /* still need to clamp frame numbers to valid range of images on disk though */
1149         CLAMP(frame, och->start, och->end);
1150         f = frame - och->start; /* shift to 0 based */
1151
1152         /* if image is already loaded in mem, return */
1153         if (och->ibufs_disp[f] != NULL) return;
1154
1155         /* use default color spaces since we know for sure cache files were saved with default settings too */
1156
1157         cache_filename(string, och->bakepath, och->relbase, frame, CACHE_TYPE_DISPLACE);
1158         och->ibufs_disp[f] = IMB_loadiffname(string, 0, NULL);
1159 #if 0
1160         if (och->ibufs_disp[f] == NULL)
1161                 printf("error loading %s\n", string);
1162         else
1163                 printf("loaded cache %s\n", string);
1164 #endif
1165
1166         cache_filename(string, och->bakepath, och->relbase, frame, CACHE_TYPE_FOAM);
1167         och->ibufs_foam[f] = IMB_loadiffname(string, 0, NULL);
1168 #if 0
1169         if (och->ibufs_foam[f] == NULL)
1170                 printf("error loading %s\n", string);
1171         else
1172                 printf("loaded cache %s\n", string);
1173 #endif
1174
1175         cache_filename(string, och->bakepath, och->relbase, frame, CACHE_TYPE_NORMAL);
1176         och->ibufs_norm[f] = IMB_loadiffname(string, 0, NULL);
1177 #if 0
1178         if (och->ibufs_norm[f] == NULL)
1179                 printf("error loading %s\n", string);
1180         else
1181                 printf("loaded cache %s\n", string);
1182 #endif
1183 }
1184
1185
1186 void BKE_bake_ocean(struct Ocean *o, struct OceanCache *och, void (*update_cb)(void *, float progress, int *cancel),
1187                     void *update_cb_data)
1188 {
1189         /* note: some of these values remain uninitialized unless certain options
1190          * are enabled, take care that BKE_ocean_eval_ij() initializes a member
1191          * before use - campbell */
1192         OceanResult ocr;
1193
1194         ImageFormatData imf = {0};
1195
1196         int f, i = 0, x, y, cancel = 0;
1197         float progress;
1198
1199         ImBuf *ibuf_foam, *ibuf_disp, *ibuf_normal;
1200         float *prev_foam;
1201         int res_x = och->resolution_x;
1202         int res_y = och->resolution_y;
1203         char string[FILE_MAX];
1204         //RNG *rng;
1205
1206         if (!o) return;
1207
1208         if (o->_do_jacobian) prev_foam = MEM_callocN(res_x * res_y * sizeof(float), "previous frame foam bake data");
1209         else prev_foam = NULL;
1210
1211         //rng = BLI_rng_new(0);
1212
1213         /* setup image format */
1214         imf.imtype = R_IMF_IMTYPE_OPENEXR;
1215         imf.depth =  R_IMF_CHAN_DEPTH_16;
1216         imf.exr_codec = R_IMF_EXR_CODEC_ZIP;
1217
1218         for (f = och->start, i = 0; f <= och->end; f++, i++) {
1219
1220                 /* create a new imbuf to store image for this frame */
1221                 ibuf_foam = IMB_allocImBuf(res_x, res_y, 32, IB_rectfloat);
1222                 ibuf_disp = IMB_allocImBuf(res_x, res_y, 32, IB_rectfloat);
1223                 ibuf_normal = IMB_allocImBuf(res_x, res_y, 32, IB_rectfloat);
1224
1225                 BKE_simulate_ocean(o, och->time[i], och->wave_scale, och->chop_amount);
1226
1227                 /* add new foam */
1228                 for (y = 0; y < res_y; y++) {
1229                         for (x = 0; x < res_x; x++) {
1230
1231                                 BKE_ocean_eval_ij(o, &ocr, x, y);
1232
1233                                 /* add to the image */
1234                                 rgb_to_rgba_unit_alpha(&ibuf_disp->rect_float[4 * (res_x * y + x)], ocr.disp);
1235
1236                                 if (o->_do_jacobian) {
1237                                         /* TODO, cleanup unused code - campbell */
1238
1239                                         float /*r, */ /* UNUSED */ pr = 0.0f, foam_result;
1240                                         float neg_disp, neg_eplus;
1241
1242                                         ocr.foam = BKE_ocean_jminus_to_foam(ocr.Jminus, och->foam_coverage);
1243
1244                                         /* accumulate previous value for this cell */
1245                                         if (i > 0) {
1246                                                 pr = prev_foam[res_x * y + x];
1247                                         }
1248
1249                                         /* r = BLI_rng_get_float(rng); */ /* UNUSED */ /* randomly reduce foam */
1250
1251                                         /* pr = pr * och->foam_fade; */         /* overall fade */
1252
1253                                         /* remember ocean coord sys is Y up!
1254                                          * break up the foam where height (Y) is low (wave valley), and X and Z displacement is greatest
1255                                          */
1256
1257 #if 0
1258                                         vec[0] = ocr.disp[0];
1259                                         vec[1] = ocr.disp[2];
1260                                         hor_stretch = len_v2(vec);
1261                                         CLAMP(hor_stretch, 0.0, 1.0);
1262 #endif
1263
1264                                         neg_disp = ocr.disp[1] < 0.0f ? 1.0f + ocr.disp[1] : 1.0f;
1265                                         neg_disp = neg_disp < 0.0f ? 0.0f : neg_disp;
1266
1267                                         /* foam, 'ocr.Eplus' only initialized with do_jacobian */
1268                                         neg_eplus = ocr.Eplus[2] < 0.0f ? 1.0f + ocr.Eplus[2] : 1.0f;
1269                                         neg_eplus = neg_eplus < 0.0f ? 0.0f : neg_eplus;
1270
1271 #if 0
1272                                         if (ocr.disp[1] < 0.0 || r > och->foam_fade)
1273                                                 pr *= och->foam_fade;
1274
1275
1276                                         pr = pr * (1.0 - hor_stretch) * ocr.disp[1];
1277                                         pr = pr * neg_disp * neg_eplus;
1278 #endif
1279
1280                                         if (pr < 1.0f)
1281                                                 pr *= pr;
1282
1283                                         pr *= och->foam_fade * (0.75f + neg_eplus * 0.25f);
1284
1285                                         /* A full clamping should not be needed! */
1286                                         foam_result = min_ff(pr + ocr.foam, 1.0f);
1287
1288                                         prev_foam[res_x * y + x] = foam_result;
1289
1290                                         /*foam_result = min_ff(foam_result, 1.0f); */
1291
1292                                         value_to_rgba_unit_alpha(&ibuf_foam->rect_float[4 * (res_x * y + x)], foam_result);
1293                                 }
1294
1295                                 if (o->_do_normals) {
1296                                         rgb_to_rgba_unit_alpha(&ibuf_normal->rect_float[4 * (res_x * y + x)], ocr.normal);
1297                                 }
1298                         }
1299                 }
1300
1301                 /* write the images */
1302                 cache_filename(string, och->bakepath, och->relbase, f, CACHE_TYPE_DISPLACE);
1303                 if (0 == BKE_imbuf_write(ibuf_disp, string, &imf))
1304                         printf("Cannot save Displacement File Output to %s\n", string);
1305
1306                 if (o->_do_jacobian) {
1307                         cache_filename(string, och->bakepath, och->relbase, f, CACHE_TYPE_FOAM);
1308                         if (0 == BKE_imbuf_write(ibuf_foam, string, &imf))
1309                                 printf("Cannot save Foam File Output to %s\n", string);
1310                 }
1311
1312                 if (o->_do_normals) {
1313                         cache_filename(string, och->bakepath, och->relbase, f, CACHE_TYPE_NORMAL);
1314                         if (0 == BKE_imbuf_write(ibuf_normal, string, &imf))
1315                                 printf("Cannot save Normal File Output to %s\n", string);
1316                 }
1317
1318                 IMB_freeImBuf(ibuf_disp);
1319                 IMB_freeImBuf(ibuf_foam);
1320                 IMB_freeImBuf(ibuf_normal);
1321
1322                 progress = (f - och->start) / (float)och->duration;
1323
1324                 update_cb(update_cb_data, progress, &cancel);
1325
1326                 if (cancel) {
1327                         if (prev_foam) MEM_freeN(prev_foam);
1328                         //BLI_rng_free(rng);
1329                         return;
1330                 }
1331         }
1332
1333         //BLI_rng_free(rng);
1334         if (prev_foam) MEM_freeN(prev_foam);
1335         och->baked = 1;
1336 }
1337
1338 #else /* WITH_OCEANSIM */
1339
1340 /* stub */
1341 typedef struct Ocean {
1342         /* need some data here, C does not allow empty struct */
1343         int stub;
1344 } Ocean;
1345
1346
1347 float BKE_ocean_jminus_to_foam(float UNUSED(jminus), float UNUSED(coverage))
1348 {
1349         return 0.0f;
1350 }
1351
1352 void BKE_ocean_eval_uv(struct Ocean *UNUSED(oc), struct OceanResult *UNUSED(ocr), float UNUSED(u), float UNUSED(v))
1353 {
1354 }
1355
1356 /* use catmullrom interpolation rather than linear */
1357 void BKE_ocean_eval_uv_catrom(struct Ocean *UNUSED(oc), struct OceanResult *UNUSED(ocr), float UNUSED(u),
1358                               float UNUSED(v))
1359 {
1360 }
1361
1362 void BKE_ocean_eval_xz(struct Ocean *UNUSED(oc), struct OceanResult *UNUSED(ocr), float UNUSED(x), float UNUSED(z))
1363 {
1364 }
1365
1366 void BKE_ocean_eval_xz_catrom(struct Ocean *UNUSED(oc), struct OceanResult *UNUSED(ocr), float UNUSED(x),
1367                               float UNUSED(z))
1368 {
1369 }
1370
1371 void BKE_ocean_eval_ij(struct Ocean *UNUSED(oc), struct OceanResult *UNUSED(ocr), int UNUSED(i), int UNUSED(j))
1372 {
1373 }
1374
1375 void BKE_simulate_ocean(struct Ocean *UNUSED(o), float UNUSED(t), float UNUSED(scale), float UNUSED(chop_amount))
1376 {
1377 }
1378
1379 struct Ocean *BKE_add_ocean(void)
1380 {
1381         Ocean *oc = MEM_callocN(sizeof(Ocean), "ocean sim data");
1382
1383         return oc;
1384 }
1385
1386 void BKE_init_ocean(struct Ocean *UNUSED(o), int UNUSED(M), int UNUSED(N), float UNUSED(Lx), float UNUSED(Lz),
1387                     float UNUSED(V), float UNUSED(l), float UNUSED(A), float UNUSED(w), float UNUSED(damp),
1388                     float UNUSED(alignment), float UNUSED(depth), float UNUSED(time), short UNUSED(do_height_field),
1389                     short UNUSED(do_chop), short UNUSED(do_normals), short UNUSED(do_jacobian), int UNUSED(seed))
1390 {
1391 }
1392
1393 void BKE_free_ocean_data(struct Ocean *UNUSED(oc))
1394 {
1395 }
1396
1397 void BKE_free_ocean(struct Ocean *oc)
1398 {
1399         if (!oc) return;
1400         MEM_freeN(oc);
1401 }
1402
1403
1404 /* ********* Baking/Caching ********* */
1405
1406
1407 void BKE_free_ocean_cache(struct OceanCache *och)
1408 {
1409         if (!och) return;
1410
1411         MEM_freeN(och);
1412 }
1413
1414 void BKE_ocean_cache_eval_uv(struct OceanCache *UNUSED(och), struct OceanResult *UNUSED(ocr), int UNUSED(f),
1415                              float UNUSED(u), float UNUSED(v))
1416 {
1417 }
1418
1419 void BKE_ocean_cache_eval_ij(struct OceanCache *UNUSED(och), struct OceanResult *UNUSED(ocr), int UNUSED(f),
1420                              int UNUSED(i), int UNUSED(j))
1421 {
1422 }
1423
1424 OceanCache *BKE_init_ocean_cache(const char *UNUSED(bakepath), const char *UNUSED(relbase), int UNUSED(start),
1425                                  int UNUSED(end), float UNUSED(wave_scale), float UNUSED(chop_amount),
1426                                  float UNUSED(foam_coverage), float UNUSED(foam_fade), int UNUSED(resolution))
1427 {
1428         OceanCache *och = MEM_callocN(sizeof(OceanCache), "ocean cache data");
1429
1430         return och;
1431 }
1432
1433 void BKE_simulate_ocean_cache(struct OceanCache *UNUSED(och), int UNUSED(frame))
1434 {
1435 }
1436
1437 void BKE_bake_ocean(struct Ocean *UNUSED(o), struct OceanCache *UNUSED(och),
1438                     void (*update_cb)(void *, float progress, int *cancel), void *UNUSED(update_cb_data))
1439 {
1440         /* unused */
1441         (void)update_cb;
1442 }
1443 #endif /* WITH_OCEANSIM */