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