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