svn merge -r37900:38000 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 = NULL;
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         defgrp_index = defgroup_name_index(ob, dmd->defgrp_name);
181
182         mvert = CDDM_get_verts(dm);
183         if(defgrp_index >= 0)
184                 dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
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         
191         for(i = 0; i < numVerts; ++i) {
192                 TexResult texres;
193                 float delta = 0, strength = dmd->strength;
194
195                 if(dvert) {
196                         weight= defvert_find_weight(dvert + i, defgrp_index);
197                         if(weight == 0.0f) continue;
198                 }
199
200                 texres.nor = NULL;
201                 get_texture_value(dmd->texture, tex_co[i], &texres);
202
203                 delta = texres.tin - dmd->midlevel;
204
205                 if(dvert) strength *= weight;
206
207                 delta *= strength;
208                 CLAMP(delta, -10000, 10000);
209
210                 switch(dmd->direction) {
211                         case MOD_DISP_DIR_X:
212                                 vertexCos[i][0] += delta;
213                                 break;
214                         case MOD_DISP_DIR_Y:
215                                 vertexCos[i][1] += delta;
216                                 break;
217                         case MOD_DISP_DIR_Z:
218                                 vertexCos[i][2] += delta;
219                                 break;
220                         case MOD_DISP_DIR_RGB_XYZ:
221                                 vertexCos[i][0] += (texres.tr - dmd->midlevel) * strength;
222                                 vertexCos[i][1] += (texres.tg - dmd->midlevel) * strength;
223                                 vertexCos[i][2] += (texres.tb - dmd->midlevel) * strength;
224                                 break;
225                         case MOD_DISP_DIR_NOR:
226                                 vertexCos[i][0] += delta * (mvert[i].no[0] / 32767.0f);
227                                 vertexCos[i][1] += delta * (mvert[i].no[1] / 32767.0f);
228                                 vertexCos[i][2] += delta * (mvert[i].no[2] / 32767.0f);
229                                 break;
230                 }
231         }
232
233         MEM_freeN(tex_co);
234 }
235
236 static void deformVerts(ModifierData *md, Object *ob,
237                                                 DerivedMesh *derivedData,
238                                                 float (*vertexCos)[3],
239                                                 int numVerts,
240                                                 int UNUSED(useRenderParams),
241                                                 int UNUSED(isFinalCalc))
242 {
243         DerivedMesh *dm= get_cddm(ob, NULL, derivedData, vertexCos);
244
245         displaceModifier_do((DisplaceModifierData *)md, ob, dm,
246                                  vertexCos, numVerts);
247
248         if(dm != derivedData)
249                 dm->release(dm);
250 }
251
252 static void deformVertsEM(
253                                            ModifierData *md, Object *ob, struct BMEditMesh *editData,
254         DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
255 {
256         DerivedMesh *dm= get_cddm(ob, editData, derivedData, vertexCos);
257
258         displaceModifier_do((DisplaceModifierData *)md, ob, dm,
259                                  vertexCos, numVerts);
260
261         if(dm != derivedData)
262                 dm->release(dm);
263 }
264
265
266 ModifierTypeInfo modifierType_Displace = {
267         /* name */              "Displace",
268         /* structName */        "DisplaceModifierData",
269         /* structSize */        sizeof(DisplaceModifierData),
270         /* type */              eModifierTypeType_OnlyDeform,
271         /* flags */             eModifierTypeFlag_AcceptsMesh
272                                                         | eModifierTypeFlag_SupportsEditmode,
273
274         /* copyData */          copyData,
275         /* deformVerts */       deformVerts,
276         /* deformMatrices */    NULL,
277         /* deformVertsEM */     deformVertsEM,
278         /* deformMatricesEM */  NULL,
279         /* applyModifier */     NULL,
280         /* applyModifierEM */   NULL,
281         /* initData */          initData,
282         /* requiredDataMask */  requiredDataMask,
283         /* freeData */          NULL,
284         /* isDisabled */        isDisabled,
285         /* updateDepgraph */    updateDepgraph,
286         /* dependsOnTime */     dependsOnTime,
287         /* dependsOnNormals */  dependsOnNormals,
288         /* foreachObjectLink */ foreachObjectLink,
289         /* foreachIDLink */     foreachIDLink,
290 };