Ocean Sim modifier patch
[blender.git] / source / blender / render / intern / source / texture_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
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 #include <stddef.h>
27
28 #include "BLI_math.h"
29 #include "BLI_utildefines.h"
30
31 #include "DNA_modifier_types.h"
32 #include "DNA_object_types.h"
33 #include "DNA_texture_types.h"
34
35 #include "BKE_global.h" /* XXX */
36
37 #include "BKE_modifier.h"
38 #include "BKE_ocean.h"
39 #include "BKE_utildefines.h"
40
41 #include "render_types.h"
42 #include "RE_shader_ext.h"
43
44 #include "texture.h"
45
46
47 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
48 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
49 /* only to be used here in this file, it's for speed */
50 extern struct Render R;
51 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
52
53
54
55
56 /* ***** actual texture sampling ***** */
57 int ocean_texture(Tex *tex, float *texvec, TexResult *texres)
58 {
59         int retval = TEX_INT;
60         OceanTex *ot= tex->ot;  
61         OceanResult or;
62         const float u = 0.5+0.5*texvec[0];
63         const float v = 0.5+0.5*texvec[1];
64         float foam;
65         int cfra = R.r.cfra;
66         int normals=0;
67         ModifierData *md;
68         
69         texres->tin = 0.0f;
70         
71         if (!ot || !ot->object || !ot->object->modifiers.first)
72                 return 0;
73         
74         if ((md = (ModifierData *)modifiers_findByType(ot->object, eModifierType_Ocean))) {
75                 OceanModifierData *omd = (OceanModifierData *)md;
76                 
77                 if (!omd->ocean)
78                         return 0;
79
80                 normals = (omd->flag & MOD_OCEAN_GENERATE_NORMALS);
81                 
82                 if (omd->oceancache && omd->cached==TRUE) {
83                         
84                         CLAMP(cfra, omd->bakestart, omd->bakeend);
85                         cfra -= omd->bakestart; // shift to 0 based
86                 
87                         BKE_ocean_cache_eval_uv(omd->oceancache, &or, cfra, u, v);
88                 
89                 } else {        // non-cached
90                         
91                         if (G.rendering)
92                                 BKE_ocean_eval_uv_catrom(omd->ocean, &or, u, v);
93                         else
94                                 BKE_ocean_eval_uv(omd->ocean, &or, u, v);
95                         
96                         or.foam = BKE_ocean_jminus_to_foam(or.Jminus, omd->foam_coverage);
97                 }
98         }
99         
100         
101         switch (ot->output) {
102                 case TEX_OCN_DISPLACEMENT:
103                         /* XYZ displacement */
104                         texres->tr = 0.5 + 0.5 * or.disp[0];
105                         texres->tg = 0.5 + 0.5 * or.disp[2];
106                         texres->tb = 0.5 + 0.5 * or.disp[1];
107                         
108                         texres->tr = MAX2(0.0, texres->tr);
109                         texres->tg = MAX2(0.0, texres->tg);
110                         texres->tb = MAX2(0.0, texres->tb);
111
112                         BRICONTRGB;
113                         
114                         retval = TEX_RGB;
115                         break;
116                 
117                 case TEX_OCN_EMINUS:
118                         /* -ve eigenvectors ? */
119                         texres->tr = or.Eminus[0];
120                         texres->tg = or.Eminus[2];
121                         texres->tb = or.Eminus[1];
122                         retval = TEX_RGB;
123                         break;
124                 
125                 case TEX_OCN_EPLUS:
126                         /* -ve eigenvectors ? */
127                         texres->tr = or.Eplus[0];
128                         texres->tg = or.Eplus[2];
129                         texres->tb = or.Eplus[1];
130                         retval = TEX_RGB;
131                         break;
132                         
133                 case TEX_OCN_JPLUS:
134                         texres->tin = or.Jplus;
135                         retval = TEX_INT;
136                 case TEX_OCN_FOAM:
137                         
138                         texres->tin = or.foam;
139
140                         BRICONT;                        
141                         
142                         retval = TEX_INT;
143                         break;
144         }
145                         
146         /* if normals needed */
147
148         if (texres->nor && normals) {
149
150                 texres->nor[0] = or.normal[0];
151                 texres->nor[1] = or.normal[2];
152                 texres->nor[2] = or.normal[1];
153
154                 normalize_v3(texres->nor);
155                 retval |= TEX_NOR;
156         }
157         
158         texres->ta = 1.0;
159         
160         return retval;
161 }
162