style cleanup, brackets in else/if, some indentation.
[blender.git] / source / blender / modifiers / intern / MOD_displace.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) 2005 by the Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Daniel Dunbar
22  *                 Ton Roosendaal,
23  *                 Ben Batt,
24  *                 Brecht Van Lommel,
25  *                 Campbell Barton
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  */
30
31 /** \file blender/modifiers/intern/MOD_displace.c
32  *  \ingroup modifiers
33  */
34
35
36 #include "DNA_meshdata_types.h"
37 #include "DNA_object_types.h"
38
39 #include "BLI_math.h"
40 #include "BLI_utildefines.h"
41 #include "BLI_string.h"
42
43
44 #include "BKE_cdderivedmesh.h"
45 #include "BKE_modifier.h"
46 #include "BKE_texture.h"
47 #include "BKE_deform.h"
48
49 #include "depsgraph_private.h"
50 #include "MEM_guardedalloc.h"
51
52 #include "MOD_util.h"
53
54 #include "RE_shader_ext.h"
55
56
57 /* Displace */
58
59 static void initData(ModifierData *md)
60 {
61         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
62
63         dmd->texture = NULL;
64         dmd->strength = 1;
65         dmd->direction = MOD_DISP_DIR_NOR;
66         dmd->midlevel = 0.5;
67 }
68
69 static void copyData(ModifierData *md, ModifierData *target)
70 {
71         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
72         DisplaceModifierData *tdmd = (DisplaceModifierData*) target;
73
74         tdmd->texture = dmd->texture;
75         tdmd->strength = dmd->strength;
76         tdmd->direction = dmd->direction;
77         BLI_strncpy(tdmd->defgrp_name, dmd->defgrp_name, sizeof(tdmd->defgrp_name));
78         tdmd->midlevel = dmd->midlevel;
79         tdmd->texmapping = dmd->texmapping;
80         tdmd->map_object = dmd->map_object;
81         BLI_strncpy(tdmd->uvlayer_name, dmd->uvlayer_name, sizeof(tdmd->uvlayer_name));
82 }
83
84 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
85 {
86         DisplaceModifierData *dmd = (DisplaceModifierData *)md;
87         CustomDataMask dataMask = 0;
88
89         /* ask for vertexgroups if we need them */
90         if(dmd->defgrp_name[0]) dataMask |= CD_MASK_MDEFORMVERT;
91
92         /* ask for UV coordinates if we need them */
93         if(dmd->texmapping == MOD_DISP_MAP_UV) dataMask |= CD_MASK_MTFACE;
94
95         return dataMask;
96 }
97
98 static int dependsOnTime(ModifierData *md)
99 {
100         DisplaceModifierData *dmd = (DisplaceModifierData *)md;
101
102         if(dmd->texture) {
103                 return BKE_texture_dependsOnTime(dmd->texture);
104         }
105         else {
106                 return 0;
107         }
108 }
109
110 static int dependsOnNormals(ModifierData *md)
111 {
112         DisplaceModifierData *dmd = (DisplaceModifierData *)md;
113         return (dmd->direction == MOD_DISP_DIR_NOR);
114 }
115
116 static void foreachObjectLink(ModifierData *md, Object *ob,
117                                                    ObjectWalkFunc walk, void *userData)
118 {
119         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
120
121         walk(userData, ob, &dmd->map_object);
122 }
123
124 static void foreachIDLink(ModifierData *md, Object *ob,
125                                            IDWalkFunc walk, void *userData)
126 {
127         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
128
129         walk(userData, ob, (ID **)&dmd->texture);
130
131         foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
132 }
133
134 static void foreachTexLink(ModifierData *md, Object *ob,
135                                            TexWalkFunc walk, void *userData)
136 {
137         walk(userData, ob, md, "texture");
138 }
139
140 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
141 {
142         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
143
144         return (!dmd->texture || dmd->strength == 0.0f);
145 }
146
147 static void updateDepgraph(ModifierData *md, DagForest *forest,
148                                                 struct Scene *UNUSED(scene),
149                                                 Object *UNUSED(ob),
150                                                 DagNode *obNode)
151 {
152         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
153
154         if(dmd->map_object && dmd->texmapping == MOD_DISP_MAP_OBJECT) {
155                 DagNode *curNode = dag_get_node(forest, dmd->map_object);
156
157                 dag_add_relation(forest, curNode, obNode,
158                                  DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Displace Modifier");
159         }
160         
161
162         if(dmd->texmapping == MOD_DISP_MAP_GLOBAL)
163                 dag_add_relation(forest, obNode, obNode,
164                                  DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Displace Modifier");
165         
166 }
167
168 /* dm must be a CDDerivedMesh */
169 static void displaceModifier_do(
170                                 DisplaceModifierData *dmd, Object *ob,
171         DerivedMesh *dm, float (*vertexCos)[3], int numVerts)
172 {
173         int i;
174         MVert *mvert;
175         MDeformVert *dvert;
176         int defgrp_index;
177         float (*tex_co)[3];
178         float weight= 1.0f; /* init value unused but some compilers may complain */
179
180         if(!dmd->texture) return;
181         if(dmd->strength == 0.0f) return;
182
183         mvert = CDDM_get_verts(dm);
184         modifier_get_vgroup(ob, dm, dmd->defgrp_name, &dvert, &defgrp_index);
185
186         tex_co = MEM_callocN(sizeof(*tex_co) * numVerts,
187                                  "displaceModifier_do tex_co");
188         get_texture_coords((MappingInfoModifierData *)dmd, ob, dm, vertexCos, tex_co, numVerts);
189
190         for(i = 0; i < numVerts; ++i) {
191                 TexResult texres;
192                 float delta = 0, strength = dmd->strength;
193
194                 if(dvert) {
195                         weight= defvert_find_weight(dvert + i, defgrp_index);
196                         if(weight == 0.0f) continue;
197                 }
198
199                 texres.nor = NULL;
200                 get_texture_value(dmd->texture, tex_co[i], &texres);
201
202                 delta = texres.tin - dmd->midlevel;
203
204                 if(dvert) strength *= weight;
205
206                 delta *= strength;
207                 CLAMP(delta, -10000, 10000);
208
209                 switch(dmd->direction) {
210                         case MOD_DISP_DIR_X:
211                                 vertexCos[i][0] += delta;
212                                 break;
213                         case MOD_DISP_DIR_Y:
214                                 vertexCos[i][1] += delta;
215                                 break;
216                         case MOD_DISP_DIR_Z:
217                                 vertexCos[i][2] += delta;
218                                 break;
219                         case MOD_DISP_DIR_RGB_XYZ:
220                                 vertexCos[i][0] += (texres.tr - dmd->midlevel) * strength;
221                                 vertexCos[i][1] += (texres.tg - dmd->midlevel) * strength;
222                                 vertexCos[i][2] += (texres.tb - dmd->midlevel) * strength;
223                                 break;
224                         case MOD_DISP_DIR_NOR:
225                                 vertexCos[i][0] += delta * (mvert[i].no[0] / 32767.0f);
226                                 vertexCos[i][1] += delta * (mvert[i].no[1] / 32767.0f);
227                                 vertexCos[i][2] += delta * (mvert[i].no[2] / 32767.0f);
228                                 break;
229                 }
230         }
231
232         MEM_freeN(tex_co);
233 }
234
235 static void deformVerts(ModifierData *md, Object *ob,
236                                                 DerivedMesh *derivedData,
237                                                 float (*vertexCos)[3],
238                                                 int numVerts,
239                                                 int UNUSED(useRenderParams),
240                                                 int UNUSED(isFinalCalc))
241 {
242         DerivedMesh *dm= get_cddm(ob, NULL, derivedData, vertexCos);
243
244         displaceModifier_do((DisplaceModifierData *)md, ob, dm,
245                             vertexCos, numVerts);
246
247         if(dm != derivedData)
248                 dm->release(dm);
249 }
250
251 static void deformVertsEM(
252                                            ModifierData *md, Object *ob, struct BMEditMesh *editData,
253         DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
254 {
255         DerivedMesh *dm= get_cddm(ob, editData, derivedData, vertexCos);
256
257         displaceModifier_do((DisplaceModifierData *)md, ob, dm,
258                             vertexCos, numVerts);
259
260         if(dm != derivedData)
261                 dm->release(dm);
262 }
263
264
265 ModifierTypeInfo modifierType_Displace = {
266         /* name */              "Displace",
267         /* structName */        "DisplaceModifierData",
268         /* structSize */        sizeof(DisplaceModifierData),
269         /* type */              eModifierTypeType_OnlyDeform,
270         /* flags */             eModifierTypeFlag_AcceptsMesh
271                                                         | eModifierTypeFlag_SupportsEditmode,
272
273         /* copyData */          copyData,
274         /* deformVerts */       deformVerts,
275         /* deformMatrices */    NULL,
276         /* deformVertsEM */     deformVertsEM,
277         /* deformMatricesEM */  NULL,
278         /* applyModifier */     NULL,
279         /* applyModifierEM */   NULL,
280         /* initData */          initData,
281         /* requiredDataMask */  requiredDataMask,
282         /* freeData */          NULL,
283         /* isDisabled */        isDisabled,
284         /* updateDepgraph */    updateDepgraph,
285         /* dependsOnTime */     dependsOnTime,
286         /* dependsOnNormals */  dependsOnNormals,
287         /* foreachObjectLink */ foreachObjectLink,
288         /* foreachIDLink */     foreachIDLink,
289         /* foreachTexLink */    foreachTexLink,
290 };