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