e0972ff2d32f075844d223149ed6197b48f43709
[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         {
104                 return BKE_texture_dependsOnTime(dmd->texture);
105         }
106         else
107         {
108                 return 0;
109         }
110 }
111
112 static int dependsOnNormals(ModifierData *md)
113 {
114         DisplaceModifierData *dmd = (DisplaceModifierData *)md;
115         return (dmd->direction == MOD_DISP_DIR_NOR);
116 }
117
118 static void foreachObjectLink(ModifierData *md, Object *ob,
119                                                    ObjectWalkFunc walk, void *userData)
120 {
121         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
122
123         walk(userData, ob, &dmd->map_object);
124 }
125
126 static void foreachIDLink(ModifierData *md, Object *ob,
127                                            IDWalkFunc walk, void *userData)
128 {
129         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
130
131         walk(userData, ob, (ID **)&dmd->texture);
132
133         foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
134 }
135
136 static void foreachTexLink(ModifierData *md, Object *ob,
137                                            TexWalkFunc walk, void *userData)
138 {
139         walk(userData, ob, md, "texture");
140 }
141
142 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
143 {
144         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
145
146         return (!dmd->texture || dmd->strength == 0.0f);
147 }
148
149 static void updateDepgraph(ModifierData *md, DagForest *forest,
150                                                 struct Scene *UNUSED(scene),
151                                                 Object *UNUSED(ob),
152                                                 DagNode *obNode)
153 {
154         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
155
156         if(dmd->map_object && dmd->texmapping == MOD_DISP_MAP_OBJECT) {
157                 DagNode *curNode = dag_get_node(forest, dmd->map_object);
158
159                 dag_add_relation(forest, curNode, obNode,
160                                  DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Displace Modifier");
161         }
162         
163
164         if(dmd->texmapping == MOD_DISP_MAP_GLOBAL)
165                 dag_add_relation(forest, obNode, obNode,
166                                  DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Displace Modifier");
167         
168 }
169
170 /* dm must be a CDDerivedMesh */
171 static void displaceModifier_do(
172                                 DisplaceModifierData *dmd, Object *ob,
173         DerivedMesh *dm, float (*vertexCos)[3], int numVerts)
174 {
175         int i;
176         MVert *mvert;
177         MDeformVert *dvert;
178         int defgrp_index;
179         float (*tex_co)[3];
180         float weight= 1.0f; /* init value unused but some compilers may complain */
181
182         if(!dmd->texture) return;
183         if(dmd->strength == 0.0f) return;
184
185         mvert = CDDM_get_verts(dm);
186         modifier_get_vgroup(ob, dm, dmd->defgrp_name, &dvert, &defgrp_index);
187
188         tex_co = MEM_callocN(sizeof(*tex_co) * numVerts,
189                                  "displaceModifier_do tex_co");
190         get_texture_coords((MappingInfoModifierData *)dmd, ob, dm, vertexCos, tex_co, numVerts);
191
192         for(i = 0; i < numVerts; ++i) {
193                 TexResult texres;
194                 float delta = 0, strength = dmd->strength;
195
196                 if(dvert) {
197                         weight= defvert_find_weight(dvert + i, defgrp_index);
198                         if(weight == 0.0f) continue;
199                 }
200
201                 texres.nor = NULL;
202                 get_texture_value(dmd->texture, tex_co[i], &texres);
203
204                 delta = texres.tin - dmd->midlevel;
205
206                 if(dvert) strength *= weight;
207
208                 delta *= strength;
209                 CLAMP(delta, -10000, 10000);
210
211                 switch(dmd->direction) {
212                         case MOD_DISP_DIR_X:
213                                 vertexCos[i][0] += delta;
214                                 break;
215                         case MOD_DISP_DIR_Y:
216                                 vertexCos[i][1] += delta;
217                                 break;
218                         case MOD_DISP_DIR_Z:
219                                 vertexCos[i][2] += delta;
220                                 break;
221                         case MOD_DISP_DIR_RGB_XYZ:
222                                 vertexCos[i][0] += (texres.tr - dmd->midlevel) * strength;
223                                 vertexCos[i][1] += (texres.tg - dmd->midlevel) * strength;
224                                 vertexCos[i][2] += (texres.tb - dmd->midlevel) * strength;
225                                 break;
226                         case MOD_DISP_DIR_NOR:
227                                 vertexCos[i][0] += delta * (mvert[i].no[0] / 32767.0f);
228                                 vertexCos[i][1] += delta * (mvert[i].no[1] / 32767.0f);
229                                 vertexCos[i][2] += delta * (mvert[i].no[2] / 32767.0f);
230                                 break;
231                 }
232         }
233
234         MEM_freeN(tex_co);
235 }
236
237 static void deformVerts(ModifierData *md, Object *ob,
238                                                 DerivedMesh *derivedData,
239                                                 float (*vertexCos)[3],
240                                                 int numVerts,
241                                                 int UNUSED(useRenderParams),
242                                                 int UNUSED(isFinalCalc))
243 {
244         DerivedMesh *dm= get_cddm(ob, NULL, derivedData, vertexCos);
245
246         displaceModifier_do((DisplaceModifierData *)md, ob, dm,
247                             vertexCos, numVerts);
248
249         if(dm != derivedData)
250                 dm->release(dm);
251 }
252
253 static void deformVertsEM(
254                                            ModifierData *md, Object *ob, struct BMEditMesh *editData,
255         DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
256 {
257         DerivedMesh *dm= get_cddm(ob, editData, derivedData, vertexCos);
258
259         displaceModifier_do((DisplaceModifierData *)md, ob, dm,
260                             vertexCos, numVerts);
261
262         if(dm != derivedData)
263                 dm->release(dm);
264 }
265
266
267 ModifierTypeInfo modifierType_Displace = {
268         /* name */              "Displace",
269         /* structName */        "DisplaceModifierData",
270         /* structSize */        sizeof(DisplaceModifierData),
271         /* type */              eModifierTypeType_OnlyDeform,
272         /* flags */             eModifierTypeFlag_AcceptsMesh
273                                                         | eModifierTypeFlag_SupportsEditmode,
274
275         /* copyData */          copyData,
276         /* deformVerts */       deformVerts,
277         /* deformMatrices */    NULL,
278         /* deformVertsEM */     deformVertsEM,
279         /* deformMatricesEM */  NULL,
280         /* applyModifier */     NULL,
281         /* applyModifierEM */   NULL,
282         /* initData */          initData,
283         /* requiredDataMask */  requiredDataMask,
284         /* freeData */          NULL,
285         /* isDisabled */        isDisabled,
286         /* updateDepgraph */    updateDepgraph,
287         /* dependsOnTime */     dependsOnTime,
288         /* dependsOnNormals */  dependsOnNormals,
289         /* foreachObjectLink */ foreachObjectLink,
290         /* foreachIDLink */     foreachIDLink,
291         /* foreachTexLink */    foreachTexLink,
292 };