Merged changes in the trunk up to revision 45308.
[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  * Some useful 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 = fmodf(u,1.0f);
317         v = fmodf(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                 }
359                 else {
360                         ocr->disp[0] = 0.0;
361                         ocr->disp[2] = 0.0;
362                 }
363
364                 if (oc->_do_jacobian) {
365                         compute_eigenstuff(ocr, BILERP(oc->_Jxx),BILERP(oc->_Jzz),BILERP(oc->_Jxz));
366                 }
367         }
368 #undef BILERP
369
370         BLI_rw_mutex_unlock(&oc->oceanmutex);
371 }
372
373 // use catmullrom interpolation rather than linear
374 void BKE_ocean_eval_uv_catrom(struct Ocean *oc, struct OceanResult *ocr, float u,float v)
375 {
376         int i0,i1,i2,i3,j0,j1,j2,j3;
377         float frac_x,frac_z;
378         float uu,vv;
379
380         // first wrap the texture so 0 <= (u,v) < 1
381         u = fmod(u,1.0f);
382         v = fmod(v,1.0f);
383
384         if (u < 0) u += 1.0f;
385         if (v < 0) v += 1.0f;
386
387         BLI_rw_mutex_lock(&oc->oceanmutex, THREAD_LOCK_READ);
388
389         uu = u * oc->_M;
390         vv = v * oc->_N;
391
392         i1 = (int)floor(uu);
393         j1 = (int)floor(vv);
394
395         i2 = (i1 + 1);
396         j2 = (j1 + 1);
397
398         frac_x = uu - i1;
399         frac_z = vv - j1;
400
401         i1 = i1 % oc->_M;
402         j1 = j1 % oc->_N;
403
404         i2 = i2 % oc->_M;
405         j2 = j2 % oc->_N;
406
407         i0 = (i1-1);
408         i3 = (i2+1);
409         i0 = i0 <   0 ? i0 + oc->_M : i0;
410         i3 = i3 >= oc->_M ? i3 - oc->_M : i3;
411
412         j0 = (j1-1);
413         j3 = (j2+1);
414         j0 = j0 <   0 ? j0 + oc->_N : j0;
415         j3 = j3 >= oc->_N ? j3 - oc->_N : j3;
416
417 #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),\
418         catrom(m[i0*oc->_N+j1],m[i1*oc->_N+j1],m[i2*oc->_N+j1],m[i3*oc->_N+j1],frac_x),\
419         catrom(m[i0*oc->_N+j2],m[i1*oc->_N+j2],m[i2*oc->_N+j2],m[i3*oc->_N+j2],frac_x),\
420         catrom(m[i0*oc->_N+j3],m[i1*oc->_N+j3],m[i2*oc->_N+j3],m[i3*oc->_N+j3],frac_x),\
421         frac_z)
422
423         {
424                 if (oc->_do_disp_y)
425                 {
426                         ocr->disp[1] = INTERP(oc->_disp_y);
427                 }
428                 if (oc->_do_normals)
429                 {
430                         ocr->normal[0] = INTERP(oc->_N_x);
431                         ocr->normal[1] = oc->_N_y/*INTERP(oc->_N_y) (MEM01)*/;
432                         ocr->normal[2] = INTERP(oc->_N_z);
433                 }
434                 if (oc->_do_chop)
435                 {
436                         ocr->disp[0] = INTERP(oc->_disp_x);
437                         ocr->disp[2] = INTERP(oc->_disp_z);
438                 }
439                 else {
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                 ocr->disp[0] = 0.0f;
484                 ocr->disp[2] = 0.0f;
485         }
486
487         if (oc->_do_normals)
488         {
489                 ocr->normal[0] = oc->_N_x[i*oc->_N+j];
490                 ocr->normal[1] = oc->_N_y/*oc->_N_y[i*oc->_N+j] (MEM01)*/;
491                 ocr->normal[2] = oc->_N_z[i*oc->_N+j];
492
493                 normalize_v3(ocr->normal);
494         }
495
496         if (oc->_do_jacobian)
497         {
498                 compute_eigenstuff(ocr, oc->_Jxx[i*oc->_N+j],oc->_Jzz[i*oc->_N+j],oc->_Jxz[i*oc->_N+j]);
499         }
500
501         BLI_rw_mutex_unlock(&oc->oceanmutex);
502 }
503
504 void BKE_simulate_ocean(struct Ocean *o, float t, float scale, float chop_amount)
505 {
506         int i, j;
507
508         scale *= o->normalize_factor;
509
510         BLI_rw_mutex_lock(&o->oceanmutex, THREAD_LOCK_WRITE);
511
512         // compute a new htilda
513 #pragma omp parallel for private(i, j)
514         for (i = 0 ; i  < o->_M ; ++i)
515         {
516                 // note the <= _N/2 here, see the fftw doco about
517                 // the mechanics of the complex->real fft storage
518                 for ( j  = 0 ; j  <= o->_N / 2 ; ++j)
519                 {
520                         fftw_complex exp_param1;
521                         fftw_complex exp_param2;
522                         fftw_complex conj_param;
523
524
525                         init_complex(exp_param1, 0.0, omega(o->_k[i*(1+o->_N/2)+j],o->_depth)*t);
526                         init_complex(exp_param2, 0.0, -omega(o->_k[i*(1+o->_N/2)+j],o->_depth)*t);
527                         exp_complex(exp_param1, exp_param1);
528                         exp_complex(exp_param2, exp_param2);
529                         conj_complex(conj_param, o->_h0_minus[i*o->_N+j]);
530
531                         mul_complex_c(exp_param1, o->_h0[i*o->_N+j], exp_param1);
532                         mul_complex_c(exp_param2, conj_param, exp_param2);
533
534                         add_comlex_c(o->_htilda[i*(1+o->_N/2)+j], exp_param1, exp_param2);
535                         mul_complex_f(o->_fft_in[i*(1+o->_N/2)+j], o->_htilda[i*(1+o->_N/2)+j], scale);
536                 }
537         }
538
539 #pragma omp parallel sections private(i, j)
540         {
541
542 #pragma omp section
543                 {
544                         if (o->_do_disp_y)
545                         {
546                                 // y displacement
547                                 fftw_execute(o->_disp_y_plan);
548                         }
549                 } // section 1
550
551 #pragma omp section
552                 {
553                         if (o->_do_chop)
554                         {
555                                 // x displacement
556                                 for ( i = 0 ; i  < o->_M ; ++i)
557                                 {
558                                         for ( j  = 0 ; j  <= o->_N / 2 ; ++j)
559                                         {
560                                                 fftw_complex mul_param;
561                                                 fftw_complex minus_i;
562
563                                                 init_complex(minus_i, 0.0, -1.0);
564                                                 init_complex(mul_param, -scale, 0);
565                                                 mul_complex_f(mul_param, mul_param, chop_amount);
566                                                 mul_complex_c(mul_param, mul_param, minus_i);
567                                                 mul_complex_c(mul_param, mul_param, o->_htilda[i*(1+o->_N/2)+j]);
568                                                 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]));
569                                                 init_complex(o->_fft_in_x[i*(1+o->_N/2)+j], real_c(mul_param), image_c(mul_param));
570                                         }
571                                 }
572                                 fftw_execute(o->_disp_x_plan);
573                         }
574                 } //section 2
575
576 #pragma omp section
577                 {
578                         if (o->_do_chop)
579                         {
580                                 // z displacement
581                                 for ( i = 0 ; i  < o->_M ; ++i)
582                                 {
583                                         for ( j  = 0 ; j  <= o->_N / 2 ; ++j)
584                                         {
585                                                 fftw_complex mul_param;
586                                                 fftw_complex minus_i;
587
588                                                 init_complex(minus_i, 0.0, -1.0);
589                                                 init_complex(mul_param, -scale, 0);
590                                                 mul_complex_f(mul_param, mul_param, chop_amount);
591                                                 mul_complex_c(mul_param, mul_param, minus_i);
592                                                 mul_complex_c(mul_param, mul_param, o->_htilda[i*(1+o->_N/2)+j]);
593                                                 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]));
594                                                 init_complex(o->_fft_in_z[i*(1+o->_N/2)+j], real_c(mul_param), image_c(mul_param));
595                                         }
596                                 }
597                                 fftw_execute(o->_disp_z_plan);
598                         }
599                 } // section 3
600
601 #pragma omp section
602                 {
603                         if (o->_do_jacobian)
604                         {
605                                 // Jxx
606                                 for ( i = 0 ; i  < o->_M ; ++i)
607                                 {
608                                         for ( j  = 0 ; j  <= o->_N / 2 ; ++j)
609                                         {
610                                                 fftw_complex mul_param;
611
612                                                 //init_complex(mul_param, -scale, 0);
613                                                 init_complex(mul_param, -1, 0);
614
615                                                 mul_complex_f(mul_param, mul_param, chop_amount);
616                                                 mul_complex_c(mul_param, mul_param, o->_htilda[i*(1+o->_N/2)+j]);
617                                                 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]));
618                                                 init_complex(o->_fft_in_jxx[i*(1+o->_N/2)+j], real_c(mul_param), image_c(mul_param));
619                                         }
620                                 }
621                                 fftw_execute(o->_Jxx_plan);
622
623                                 for ( i = 0 ; i  < o->_M ; ++i)
624                                 {
625                                         for ( j  = 0 ; j  < o->_N ; ++j)
626                                         {
627                                                 o->_Jxx[i*o->_N+j] += 1.0;
628                                         }
629                                 }
630                         }
631                 } // section 4
632
633 #pragma omp section
634                 {
635                         if (o->_do_jacobian)
636                         {
637                                 // Jzz
638                                 for ( i = 0 ; i  < o->_M ; ++i)
639                                 {
640                                         for ( j  = 0 ; j  <= o->_N / 2 ; ++j)
641                                         {
642                                                 fftw_complex mul_param;
643
644                                                 //init_complex(mul_param, -scale, 0);
645                                                 init_complex(mul_param, -1, 0);
646
647                                                 mul_complex_f(mul_param, mul_param, chop_amount);
648                                                 mul_complex_c(mul_param, mul_param, o->_htilda[i*(1+o->_N/2)+j]);
649                                                 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]));
650                                                 init_complex(o->_fft_in_jzz[i*(1+o->_N/2)+j], real_c(mul_param), image_c(mul_param));
651                                         }
652                                 }
653                                 fftw_execute(o->_Jzz_plan);
654                                 for ( i = 0 ; i  < o->_M ; ++i)
655                                 {
656                                         for ( j  = 0 ; j  < o->_N ; ++j)
657                                         {
658                                                 o->_Jzz[i*o->_N+j] += 1.0;
659                                         }
660                                 }
661                         }
662                 } // section 5
663
664 #pragma omp section
665                 {
666                         if (o->_do_jacobian)
667                         {
668                                 // Jxz
669                                 for ( i = 0 ; i  < o->_M ; ++i)
670                                 {
671                                         for ( j  = 0 ; j  <= o->_N / 2 ; ++j)
672                                         {
673                                                 fftw_complex mul_param;
674
675                                                 //init_complex(mul_param, -scale, 0);
676                                                 init_complex(mul_param, -1, 0);
677
678                                                 mul_complex_f(mul_param, mul_param, chop_amount);
679                                                 mul_complex_c(mul_param, mul_param, o->_htilda[i*(1+o->_N/2)+j]);
680                                                 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]));
681                                                 init_complex(o->_fft_in_jxz[i*(1+o->_N/2)+j], real_c(mul_param), image_c(mul_param));
682                                         }
683                                 }
684                                 fftw_execute(o->_Jxz_plan);
685                         }
686                 } // section 6
687
688 #pragma omp section
689                 {
690                         // fft normals
691                         if (o->_do_normals)
692                         {
693                                 for ( i = 0 ; i  < o->_M ; ++i)
694                                 {
695                                         for ( j  = 0 ; j  <= o->_N / 2 ; ++j)
696                                         {
697                                                 fftw_complex mul_param;
698
699                                                 init_complex(mul_param, 0.0, -1.0);
700                                                 mul_complex_c(mul_param, mul_param, o->_htilda[i*(1+o->_N/2)+j]);
701                                                 mul_complex_f(mul_param, mul_param, o->_kx[i]);
702                                                 init_complex(o->_fft_in_nx[i*(1+o->_N/2)+j], real_c(mul_param), image_c(mul_param));
703                                         }
704                                 }
705                                 fftw_execute(o->_N_x_plan);
706
707                         }
708                 } // section 7
709
710 #pragma omp section
711                 {
712                         if (o->_do_normals)
713                         {
714                                 for ( i = 0 ; i  < o->_M ; ++i)
715                                 {
716                                         for ( j  = 0 ; j  <= o->_N / 2 ; ++j)
717                                         {
718                                                 fftw_complex mul_param;
719
720                                                 init_complex(mul_param, 0.0, -1.0);
721                                                 mul_complex_c(mul_param, mul_param, o->_htilda[i*(1+o->_N/2)+j]);
722                                                 mul_complex_f(mul_param, mul_param, o->_kz[i]);
723                                                 init_complex(o->_fft_in_nz[i*(1+o->_N/2)+j], real_c(mul_param), image_c(mul_param));
724                                         }
725                                 }
726                                 fftw_execute(o->_N_z_plan);
727
728 #if 0
729                                 for ( i = 0 ; i  < o->_M ; ++i) {
730                                         for ( j  = 0 ; j  < o->_N ; ++j) {
731                                                 o->_N_y[i*o->_N+j] = 1.0f/scale;
732                                         }
733                                 }
734                                 (MEM01)
735 #endif
736                         o->_N_y = 1.0f/scale;
737                         }
738                 } // section 8
739
740         } // omp sections
741
742         BLI_rw_mutex_unlock(&o->oceanmutex);
743 }
744
745 static void set_height_normalize_factor(struct Ocean *oc)
746 {
747         float res = 1.0;
748         float max_h = 0.0;
749
750         int i,j;
751
752         if (!oc->_do_disp_y) return;
753
754         oc->normalize_factor = 1.0;
755
756         BKE_simulate_ocean(oc, 0.0, 1.0, 0);
757
758         BLI_rw_mutex_lock(&oc->oceanmutex, THREAD_LOCK_READ);
759
760         for (i = 0; i < oc->_M; ++i)
761         {
762                 for (j = 0; j < oc->_N; ++j)
763                 {
764                         if ( max_h < fabsf(oc->_disp_y[i*oc->_N+j]))
765                         {
766                                 max_h = fabsf(oc->_disp_y[i*oc->_N+j]);
767                         }
768                 }
769         }
770
771         BLI_rw_mutex_unlock(&oc->oceanmutex);
772
773         if (max_h == 0.0f) max_h = 0.00001f; // just in case ...
774
775         res = 1.0f / (max_h);
776
777         oc->normalize_factor = res;
778 }
779
780 struct Ocean *BKE_add_ocean(void)
781 {
782         Ocean *oc = MEM_callocN(sizeof(Ocean), "ocean sim data");
783
784         BLI_rw_mutex_init(&oc->oceanmutex);
785
786         return oc;
787 }
788
789 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,
790                                         float alignment, float depth, float time, short do_height_field, short do_chop, short do_normals, short do_jacobian, int seed)
791 {
792         int i,j,ii;
793
794         BLI_rw_mutex_lock(&o->oceanmutex, THREAD_LOCK_WRITE);
795
796         o->_M = M;
797         o->_N = N;
798         o->_V = V;
799         o->_l = l;
800         o->_A = A;
801         o->_w = w;
802         o->_damp_reflections = 1.0f - damp;
803         o->_wind_alignment = alignment;
804         o->_depth = depth;
805         o->_Lx = Lx;
806         o->_Lz = Lz;
807         o->_wx = cos(w);
808         o->_wz = -sin(w); // wave direction
809         o->_L = V*V / GRAVITY;  // largest wave for a given velocity V
810         o->time = time;
811
812         o->_do_disp_y = do_height_field;
813         o->_do_normals = do_normals;
814         o->_do_chop = do_chop;
815         o->_do_jacobian = do_jacobian;
816
817         o->_k = (float*) MEM_mallocN(M * (1+N/2) * sizeof(float), "ocean_k");
818         o->_h0 = (fftw_complex*) MEM_mallocN(M * N * sizeof(fftw_complex), "ocean_h0");
819         o->_h0_minus = (fftw_complex*) MEM_mallocN(M * N * sizeof(fftw_complex), "ocean_h0_minus");
820         o->_kx = (float*) MEM_mallocN(o->_M * sizeof(float), "ocean_kx");
821         o->_kz = (float*) MEM_mallocN(o->_N * sizeof(float), "ocean_kz");
822
823         // make this robust in the face of erroneous usage
824         if (o->_Lx == 0.0f)
825                 o->_Lx = 0.001f;
826
827         if (o->_Lz == 0.0f)
828                 o->_Lz = 0.001f;
829
830         // the +ve components and DC
831         for (i = 0 ; i <= o->_M/2 ; ++i)
832                 o->_kx[i] = 2.0f * (float)M_PI * i / o->_Lx;
833
834         // the -ve components
835         for (i = o->_M-1,ii=0 ; i > o->_M/2 ; --i,++ii)
836                 o->_kx[i] = -2.0f * (float)M_PI * ii / o->_Lx;
837
838         // the +ve components and DC
839         for (i = 0 ; i <= o->_N/2 ; ++i)
840                 o->_kz[i] = 2.0f * (float)M_PI * i / o->_Lz;
841
842         // the -ve components
843         for (i = o->_N-1,ii=0 ; i > o->_N/2 ; --i,++ii)
844                 o->_kz[i] = -2.0f * (float)M_PI * ii / o->_Lz;
845
846         // pre-calculate the k matrix
847         for (i = 0 ; i  < o->_M ; ++i)
848                 for (j  = 0 ; j  <= o->_N / 2 ; ++j)
849                         o->_k[i*(1+o->_N/2)+j] = sqrt(o->_kx[i]*o->_kx[i] + o->_kz[j]*o->_kz[j] );
850
851         /*srand(seed);*/
852         BLI_srand(seed);
853
854         for (i = 0 ; i  < o->_M ; ++i)
855         {
856                 for (j = 0 ; j  < o->_N ; ++j)
857                 {
858                         float r1 = gaussRand();
859                         float r2 = gaussRand();
860
861                         fftw_complex r1r2;
862                         init_complex(r1r2, r1, r2);
863                         mul_complex_f(o->_h0[i*o->_N+j], r1r2, (float)(sqrt(Ph(o,  o->_kx[i], o->_kz[j]) / 2.0f)));
864                         mul_complex_f(o->_h0_minus[i*o->_N+j], r1r2, (float)(sqrt(Ph(o, -o->_kx[i],-o->_kz[j]) / 2.0f)));
865                 }
866         }
867
868         o->_fft_in = (fftw_complex*) MEM_mallocN(o->_M * (1+o->_N/2) * sizeof(fftw_complex), "ocean_fft_in");
869         o->_htilda = (fftw_complex*) MEM_mallocN(o->_M * (1+o->_N/2) * sizeof(fftw_complex), "ocean_htilda");
870
871         if (o->_do_disp_y) {
872                 o->_disp_y = (double*) MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_disp_y");
873                 o->_disp_y_plan = fftw_plan_dft_c2r_2d(o->_M,o->_N, o->_fft_in, o->_disp_y, FFTW_ESTIMATE);
874         }
875
876         if (o->_do_normals) {
877                 o->_fft_in_nx = (fftw_complex*) MEM_mallocN(o->_M * (1+o->_N/2) * sizeof(fftw_complex), "ocean_fft_in_nx");
878                 o->_fft_in_nz = (fftw_complex*) MEM_mallocN(o->_M * (1+o->_N/2) * sizeof(fftw_complex), "ocean_fft_in_nz");
879
880                 o->_N_x = (double*) MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_N_x");
881                 /*o->_N_y = (float*) fftwf_malloc(o->_M * o->_N * sizeof(float)); (MEM01)*/
882                 o->_N_z = (double*) MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_N_z");
883
884                 o->_N_x_plan = fftw_plan_dft_c2r_2d(o->_M,o->_N, o->_fft_in_nx, o->_N_x, FFTW_ESTIMATE);
885                 o->_N_z_plan = fftw_plan_dft_c2r_2d(o->_M,o->_N, o->_fft_in_nz, o->_N_z, FFTW_ESTIMATE);
886         }
887
888         if (o->_do_chop) {
889                 o->_fft_in_x = (fftw_complex*) MEM_mallocN(o->_M * (1+o->_N/2) * sizeof(fftw_complex), "ocean_fft_in_x");
890                 o->_fft_in_z = (fftw_complex*) MEM_mallocN(o->_M * (1+o->_N/2) * sizeof(fftw_complex), "ocean_fft_in_z");
891
892                 o->_disp_x = (double*) MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_disp_x");
893                 o->_disp_z = (double*) MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_disp_z");
894
895                 o->_disp_x_plan = fftw_plan_dft_c2r_2d(o->_M,o->_N, o->_fft_in_x, o->_disp_x, FFTW_ESTIMATE);
896                 o->_disp_z_plan = fftw_plan_dft_c2r_2d(o->_M,o->_N, o->_fft_in_z, o->_disp_z, FFTW_ESTIMATE);
897         }
898         if (o->_do_jacobian) {
899                 o->_fft_in_jxx = (fftw_complex*) MEM_mallocN(o->_M * (1+o->_N/2) * sizeof(fftw_complex), "ocean_fft_in_jxx");
900                 o->_fft_in_jzz = (fftw_complex*) MEM_mallocN(o->_M * (1+o->_N/2) * sizeof(fftw_complex), "ocean_fft_in_jzz");
901                 o->_fft_in_jxz = (fftw_complex*) MEM_mallocN(o->_M * (1+o->_N/2) * sizeof(fftw_complex), "ocean_fft_in_jxz");
902
903                 o->_Jxx = (double*) MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_Jxx");
904                 o->_Jzz = (double*) MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_Jzz");
905                 o->_Jxz = (double*) MEM_mallocN(o->_M * o->_N * sizeof(double), "ocean_Jxz");
906
907                 o->_Jxx_plan = fftw_plan_dft_c2r_2d(o->_M,o->_N, o->_fft_in_jxx, o->_Jxx, FFTW_ESTIMATE);
908                 o->_Jzz_plan = fftw_plan_dft_c2r_2d(o->_M,o->_N, o->_fft_in_jzz, o->_Jzz, FFTW_ESTIMATE);
909                 o->_Jxz_plan = fftw_plan_dft_c2r_2d(o->_M,o->_N, o->_fft_in_jxz, o->_Jxz, FFTW_ESTIMATE);
910         }
911
912         BLI_rw_mutex_unlock(&o->oceanmutex);
913
914         set_height_normalize_factor(o);
915
916 }
917
918 void BKE_free_ocean_data(struct Ocean *oc)
919 {
920         if (!oc) return;
921
922         BLI_rw_mutex_lock(&oc->oceanmutex, THREAD_LOCK_WRITE);
923
924         if (oc->_do_disp_y)
925         {
926                 fftw_destroy_plan(oc->_disp_y_plan);
927                 MEM_freeN(oc->_disp_y);
928         }
929
930         if (oc->_do_normals)
931         {
932                 MEM_freeN(oc->_fft_in_nx);
933                 MEM_freeN(oc->_fft_in_nz);
934                 fftw_destroy_plan(oc->_N_x_plan);
935                 fftw_destroy_plan(oc->_N_z_plan);
936                 MEM_freeN(oc->_N_x);
937                 /*fftwf_free(oc->_N_y); (MEM01)*/
938                 MEM_freeN(oc->_N_z);
939         }
940
941         if (oc->_do_chop)
942         {
943                 MEM_freeN(oc->_fft_in_x);
944                 MEM_freeN(oc->_fft_in_z);
945                 fftw_destroy_plan(oc->_disp_x_plan);
946                 fftw_destroy_plan(oc->_disp_z_plan);
947                 MEM_freeN(oc->_disp_x);
948                 MEM_freeN(oc->_disp_z);
949         }
950
951         if (oc->_do_jacobian)
952         {
953                 MEM_freeN(oc->_fft_in_jxx);
954                 MEM_freeN(oc->_fft_in_jzz);
955                 MEM_freeN(oc->_fft_in_jxz);
956                 fftw_destroy_plan(oc->_Jxx_plan);
957                 fftw_destroy_plan(oc->_Jzz_plan);
958                 fftw_destroy_plan(oc->_Jxz_plan);
959                 MEM_freeN(oc->_Jxx);
960                 MEM_freeN(oc->_Jzz);
961                 MEM_freeN(oc->_Jxz);
962         }
963
964         if (oc->_fft_in)
965                 MEM_freeN(oc->_fft_in);
966
967         /* check that ocean data has been initialized */
968         if (oc->_htilda) {
969                 MEM_freeN(oc->_htilda);
970                 MEM_freeN(oc->_k);
971                 MEM_freeN(oc->_h0);
972                 MEM_freeN(oc->_h0_minus);
973                 MEM_freeN(oc->_kx);
974                 MEM_freeN(oc->_kz);
975         }
976
977         BLI_rw_mutex_unlock(&oc->oceanmutex);
978 }
979
980 void BKE_free_ocean(struct Ocean *oc)
981 {
982         if (!oc) return;
983
984         BKE_free_ocean_data(oc);
985         BLI_rw_mutex_end(&oc->oceanmutex);
986
987         MEM_freeN(oc);
988 }
989
990 #undef GRAVITY
991
992
993 /* ********* Baking/Caching ********* */
994
995
996 #define CACHE_TYPE_DISPLACE     1
997 #define CACHE_TYPE_FOAM         2
998 #define CACHE_TYPE_NORMAL       3
999
1000 static void cache_filename(char *string, const char *path, const char *relbase, int frame, int type)
1001 {
1002         char cachepath[FILE_MAX];
1003         const char *fname;
1004
1005         switch(type) {
1006         case CACHE_TYPE_FOAM:
1007                 fname= "foam_";
1008                 break;
1009         case CACHE_TYPE_NORMAL:
1010                 fname= "normal_";
1011                 break;
1012         case CACHE_TYPE_DISPLACE:
1013         default:
1014                 fname= "disp_";
1015                 break;
1016         }
1017
1018         BLI_join_dirfile(cachepath, sizeof(cachepath), path, fname);
1019
1020         BKE_makepicstring(string, cachepath, relbase, frame, R_IMF_IMTYPE_OPENEXR, 1, TRUE);
1021 }
1022
1023 /* silly functions but useful to inline when the args do a lot of indirections */
1024 MINLINE void rgb_to_rgba_unit_alpha(float r_rgba[4], const float rgb[3])
1025 {
1026         r_rgba[0]= rgb[0];
1027         r_rgba[1]= rgb[1];
1028         r_rgba[2]= rgb[2];
1029         r_rgba[3]= 1.0f;
1030 }
1031 MINLINE void value_to_rgba_unit_alpha(float r_rgba[4], const float value)
1032 {
1033         r_rgba[0]= value;
1034         r_rgba[1]= value;
1035         r_rgba[2]= value;
1036         r_rgba[3]= 1.0f;
1037 }
1038
1039 void BKE_free_ocean_cache(struct OceanCache *och)
1040 {
1041         int i, f=0;
1042
1043         if (!och) return;
1044
1045         if (och->ibufs_disp) {
1046                 for (i=och->start, f=0; i<=och->end; i++, f++)
1047                 {
1048                         if (och->ibufs_disp[f]) {
1049                                 IMB_freeImBuf(och->ibufs_disp[f]);
1050                         }
1051                 }
1052                 MEM_freeN(och->ibufs_disp);
1053         }
1054
1055         if (och->ibufs_foam) {
1056                 for (i=och->start, f=0; i<=och->end; i++, f++)
1057                 {
1058                         if (och->ibufs_foam[f]) {
1059                                 IMB_freeImBuf(och->ibufs_foam[f]);
1060                         }
1061                 }
1062                 MEM_freeN(och->ibufs_foam);
1063         }
1064
1065         if (och->ibufs_norm) {
1066                 for (i=och->start, f=0; i<=och->end; i++, f++)
1067                 {
1068                         if (och->ibufs_norm[f]) {
1069                                 IMB_freeImBuf(och->ibufs_norm[f]);
1070                         }
1071                 }
1072                 MEM_freeN(och->ibufs_norm);
1073         }
1074
1075         if (och->time)
1076                 MEM_freeN(och->time);
1077         MEM_freeN(och);
1078 }
1079
1080 void BKE_ocean_cache_eval_uv(struct OceanCache *och, struct OceanResult *ocr, int f, float u, float v)
1081 {
1082         int res_x = och->resolution_x;
1083         int res_y = och->resolution_y;
1084         float result[4];
1085
1086         u = fmod(u, 1.0);
1087         v = fmod(v, 1.0);
1088
1089         if (u < 0) u += 1.0f;
1090         if (v < 0) v += 1.0f;
1091
1092         if (och->ibufs_disp[f]) {
1093                 ibuf_sample(och->ibufs_disp[f], u, v, (1.0f/(float)res_x), (1.0f/(float)res_y), result);
1094                 copy_v3_v3(ocr->disp, result);
1095         }
1096
1097         if (och->ibufs_foam[f]) {
1098                 ibuf_sample(och->ibufs_foam[f], u, v, (1.0f/(float)res_x), (1.0f/(float)res_y), result);
1099                 ocr->foam = result[0];
1100         }
1101
1102         if (och->ibufs_norm[f]) {
1103                 ibuf_sample(och->ibufs_norm[f], u, v, (1.0f/(float)res_x), (1.0f/(float)res_y), result);
1104                 copy_v3_v3(ocr->normal, result);
1105         }
1106 }
1107
1108 void BKE_ocean_cache_eval_ij(struct OceanCache *och, struct OceanResult *ocr, int f, int i, int j)
1109 {
1110         const int res_x = och->resolution_x;
1111         const int res_y = och->resolution_y;
1112
1113         if (i < 0) i= -i;
1114         if (j < 0) j= -j;
1115
1116         i = i % res_x;
1117         j = j % res_y;
1118
1119         if (och->ibufs_disp[f]) {
1120                 copy_v3_v3(ocr->disp, &och->ibufs_disp[f]->rect_float[4*(res_x*j + i)]);
1121         }
1122
1123         if (och->ibufs_foam[f]) {
1124                 ocr->foam = och->ibufs_foam[f]->rect_float[4*(res_x*j + i)];
1125         }
1126
1127         if (och->ibufs_norm[f]) {
1128                 copy_v3_v3(ocr->normal, &och->ibufs_norm[f]->rect_float[4*(res_x*j + i)]);
1129         }
1130 }
1131
1132 struct OceanCache *BKE_init_ocean_cache(const char *bakepath, const char *relbase,
1133                                         int start, int end, float wave_scale,
1134                                         float chop_amount, float foam_coverage, float foam_fade, int resolution)
1135 {
1136         OceanCache *och = MEM_callocN(sizeof(OceanCache), "ocean cache data");
1137
1138         och->bakepath = bakepath;
1139         och->relbase = relbase;
1140
1141         och->start = start;
1142         och->end = end;
1143         och->duration = (end - start) + 1;
1144         och->wave_scale = wave_scale;
1145         och->chop_amount = chop_amount;
1146         och->foam_coverage = foam_coverage;
1147         och->foam_fade = foam_fade;
1148         och->resolution_x = resolution*resolution;
1149         och->resolution_y = resolution*resolution;
1150
1151         och->ibufs_disp = MEM_callocN(sizeof(ImBuf *)*och->duration, "displacement imbuf pointer array");
1152         och->ibufs_foam = MEM_callocN(sizeof(ImBuf *)*och->duration, "foam imbuf pointer array");
1153         och->ibufs_norm = MEM_callocN(sizeof(ImBuf *)*och->duration, "normal imbuf pointer array");
1154
1155         och->time = NULL;
1156
1157         return och;
1158 }
1159
1160 void BKE_simulate_ocean_cache(struct OceanCache *och, int frame)
1161 {
1162         char string[FILE_MAX];
1163         int f = frame;
1164
1165         /* ibufs array is zero based, but filenames are based on frame numbers */
1166         /* still need to clamp frame numbers to valid range of images on disk though */
1167         CLAMP(frame, och->start, och->end);
1168         f = frame - och->start; // shift to 0 based
1169
1170         /* if image is already loaded in mem, return */
1171         if (och->ibufs_disp[f] != NULL ) return;
1172
1173
1174         cache_filename(string, och->bakepath, och->relbase, frame, CACHE_TYPE_DISPLACE);
1175         och->ibufs_disp[f] = IMB_loadiffname(string, 0);
1176         //if (och->ibufs_disp[f] == NULL) printf("error loading %s\n", string);
1177         //else printf("loaded cache %s\n", string);
1178
1179         cache_filename(string, och->bakepath, och->relbase, frame, CACHE_TYPE_FOAM);
1180         och->ibufs_foam[f] = IMB_loadiffname(string, 0);
1181         //if (och->ibufs_foam[f] == NULL) printf("error loading %s\n", string);
1182         //else printf("loaded cache %s\n", string);
1183
1184         cache_filename(string, och->bakepath, och->relbase, frame, CACHE_TYPE_NORMAL);
1185         och->ibufs_norm[f] = IMB_loadiffname(string, 0);
1186         //if (och->ibufs_norm[f] == NULL) printf("error loading %s\n", string);
1187         //else printf("loaded cache %s\n", string);
1188 }
1189
1190
1191 void BKE_bake_ocean(struct Ocean *o, struct OceanCache *och, void (*update_cb)(void *, float progress, int *cancel), void *update_cb_data)
1192 {
1193         /* note: some of these values remain uninitialized unless certain options
1194          * are enabled, take care that BKE_ocean_eval_ij() initializes a member
1195          * before use - campbell */
1196         OceanResult ocr;
1197
1198         ImageFormatData imf= {0};
1199
1200         int f, i=0, x, y, cancel=0;
1201         float progress;
1202
1203         ImBuf *ibuf_foam, *ibuf_disp, *ibuf_normal;
1204         float *prev_foam;
1205         int res_x = och->resolution_x;
1206         int res_y = och->resolution_y;
1207         char string[FILE_MAX];
1208
1209         if (!o) return;
1210
1211         if (o->_do_jacobian) prev_foam = MEM_callocN(res_x*res_y*sizeof(float), "previous frame foam bake data");
1212         else                 prev_foam = NULL;
1213
1214         BLI_srand(0);
1215
1216         /* setup image format */
1217         imf.imtype= R_IMF_IMTYPE_OPENEXR;
1218         imf.depth=  R_IMF_CHAN_DEPTH_16;
1219         imf.exr_codec= R_IMF_EXR_CODEC_ZIP;
1220
1221         for (f=och->start, i=0; f<=och->end; f++, i++) {
1222
1223                 /* create a new imbuf to store image for this frame */
1224                 ibuf_foam = IMB_allocImBuf(res_x, res_y, 32, IB_rectfloat);
1225                 ibuf_disp = IMB_allocImBuf(res_x, res_y, 32, IB_rectfloat);
1226                 ibuf_normal = IMB_allocImBuf(res_x, res_y, 32, IB_rectfloat);
1227
1228                 ibuf_disp->profile = ibuf_foam->profile = ibuf_normal->profile = IB_PROFILE_LINEAR_RGB;
1229
1230                 BKE_simulate_ocean(o, och->time[i], och->wave_scale, och->chop_amount);
1231
1232                 /* add new foam */
1233                 for (y=0; y < res_y; y++) {
1234                         for (x=0; x < res_x; x++) {
1235
1236                                 BKE_ocean_eval_ij(o, &ocr, x, y);
1237
1238                                 /* add to the image */
1239                                 rgb_to_rgba_unit_alpha(&ibuf_disp->rect_float[4*(res_x*y + x)], ocr.disp);
1240
1241                                 if (o->_do_jacobian) {
1242                                         /* TODO, cleanup unused code - campbell */
1243
1244                                         float /*r,*/ /* UNUSED */ pr=0.0f, foam_result;
1245                                         float neg_disp, neg_eplus;
1246
1247                                         ocr.foam = BKE_ocean_jminus_to_foam(ocr.Jminus, och->foam_coverage);
1248
1249                                         /* accumulate previous value for this cell */
1250                                         if (i > 0) {
1251                                                 pr = prev_foam[res_x*y + x];
1252                                         }
1253
1254                                         /* r = BLI_frand(); */ /* UNUSED */ // randomly reduce foam
1255
1256                                         //pr = pr * och->foam_fade;             // overall fade
1257
1258                                         // remember ocean coord sys is Y up!
1259                                         // break up the foam where height (Y) is low (wave valley),
1260                                         // and X and Z displacement is greatest
1261
1262 #if 0
1263                                         vec[0] = ocr.disp[0];
1264                                         vec[1] = ocr.disp[2];
1265                                         hor_stretch = len_v2(vec);
1266                                         CLAMP(hor_stretch, 0.0, 1.0);
1267 #endif
1268
1269                                         neg_disp = ocr.disp[1] < 0.0f ? 1.0f+ocr.disp[1] : 1.0f;
1270                                         neg_disp = neg_disp < 0.0f ? 0.0f : neg_disp;
1271
1272                                         /* foam, 'ocr.Eplus' only initialized with do_jacobian */
1273                                         neg_eplus = ocr.Eplus[2] < 0.0f ? 1.0f + ocr.Eplus[2]:1.0f;
1274                                         neg_eplus = neg_eplus<0.0f ? 0.0f : neg_eplus;
1275
1276                                         //if (ocr.disp[1] < 0.0 || r > och->foam_fade)
1277                                         //      pr *= och->foam_fade;
1278
1279
1280                                         //pr = pr * (1.0 - hor_stretch) * ocr.disp[1];
1281                                         //pr = pr * neg_disp * neg_eplus;
1282
1283                                         if (pr < 1.0f) pr *=pr;
1284
1285                                         pr *= och->foam_fade * (0.75f + neg_eplus * 0.25f);
1286
1287
1288                                         foam_result = pr + ocr.foam;
1289
1290                                         prev_foam[res_x*y + x] = foam_result;
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_write_ibuf(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_write_ibuf(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_write_ibuf(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                         return;
1329                 }
1330         }
1331
1332         if (prev_foam) MEM_freeN(prev_foam);
1333         och->baked = 1;
1334 }
1335
1336 #else // WITH_OCEANSIM
1337
1338 /* stub */
1339 typedef struct Ocean {
1340         /* need some data here, C does not allow empty struct */
1341         int stub;
1342 } Ocean;
1343
1344
1345 float BKE_ocean_jminus_to_foam(float UNUSED(jminus), float UNUSED(coverage))
1346 {
1347         return 0.0f;
1348 }
1349
1350 void BKE_ocean_eval_uv(struct Ocean *UNUSED(oc), struct OceanResult *UNUSED(ocr), float UNUSED(u),float UNUSED(v))
1351 {
1352 }
1353
1354 // use catmullrom interpolation rather than linear
1355 void BKE_ocean_eval_uv_catrom(struct Ocean *UNUSED(oc), struct OceanResult *UNUSED(ocr), float UNUSED(u),float UNUSED(v))
1356 {
1357 }
1358
1359 void BKE_ocean_eval_xz(struct Ocean *UNUSED(oc), struct OceanResult *UNUSED(ocr), float UNUSED(x),float UNUSED(z))
1360 {
1361 }
1362
1363 void BKE_ocean_eval_xz_catrom(struct Ocean *UNUSED(oc), struct OceanResult *UNUSED(ocr), float UNUSED(x),float UNUSED(z))
1364 {
1365 }
1366
1367 void BKE_ocean_eval_ij(struct Ocean *UNUSED(oc), struct OceanResult *UNUSED(ocr), int UNUSED(i),int UNUSED(j))
1368 {
1369 }
1370
1371 void BKE_simulate_ocean(struct Ocean *UNUSED(o), float UNUSED(t), float UNUSED(scale), float UNUSED(chop_amount))
1372 {
1373 }
1374
1375 struct Ocean *BKE_add_ocean(void)
1376 {
1377         Ocean *oc = MEM_callocN(sizeof(Ocean), "ocean sim data");
1378
1379         return oc;
1380 }
1381
1382 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),
1383                                            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))
1384 {
1385 }
1386
1387 void BKE_free_ocean_data(struct Ocean *UNUSED(oc))
1388 {
1389 }
1390
1391 void BKE_free_ocean(struct Ocean *oc)
1392 {
1393         if (!oc) return;
1394         MEM_freeN(oc);
1395 }
1396
1397
1398 /* ********* Baking/Caching ********* */
1399
1400
1401 void BKE_free_ocean_cache(struct OceanCache *och)
1402 {
1403         if (!och) return;
1404
1405         MEM_freeN(och);
1406 }
1407
1408 void BKE_ocean_cache_eval_uv(struct OceanCache *UNUSED(och), struct OceanResult *UNUSED(ocr), int UNUSED(f), float UNUSED(u), float UNUSED(v))
1409 {
1410 }
1411
1412 void BKE_ocean_cache_eval_ij(struct OceanCache *UNUSED(och), struct OceanResult *UNUSED(ocr), int UNUSED(f), int UNUSED(i), int UNUSED(j))
1413 {
1414 }
1415
1416 struct OceanCache *BKE_init_ocean_cache(const char *UNUSED(bakepath), const char *UNUSED(relbase),
1417                                         int UNUSED(start), int UNUSED(end), float UNUSED(wave_scale),
1418                                         float UNUSED(chop_amount), float UNUSED(foam_coverage), float UNUSED(foam_fade), int UNUSED(resolution))
1419 {
1420         OceanCache *och = MEM_callocN(sizeof(OceanCache), "ocean cache data");
1421
1422         return och;
1423 }
1424
1425 void BKE_simulate_ocean_cache(struct OceanCache *UNUSED(och), int UNUSED(frame))
1426 {
1427 }
1428
1429 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))
1430 {
1431         /* unused */
1432         (void)update_cb;
1433 }
1434 #endif // WITH_OCEANSIM