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