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