SVN maintenance.
[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 #include "stddef.h"
34 #include "string.h"
35 #include "stdarg.h"
36 #include "math.h"
37 #include "float.h"
38
39 #include "BLI_kdtree.h"
40 #include "BLI_rand.h"
41 #include "BLI_uvproject.h"
42
43 #include "MEM_guardedalloc.h"
44
45 #include "DNA_armature_types.h"
46 #include "DNA_camera_types.h"
47 #include "DNA_curve_types.h"
48 #include "DNA_key_types.h"
49 #include "DNA_material_types.h"
50 #include "DNA_object_fluidsim.h"
51
52
53 #include "BKE_action.h"
54 #include "BKE_bmesh.h"
55 #include "BKE_cloth.h"
56 #include "BKE_cdderivedmesh.h"
57 #include "BKE_displist.h"
58 #include "BKE_fluidsim.h"
59 #include "BKE_global.h"
60 #include "BKE_multires.h"
61 #include "BKE_key.h"
62 #include "BKE_lattice.h"
63 #include "BKE_material.h"
64 #include "BKE_mesh.h"
65 #include "BKE_modifier.h"
66 #include "BKE_object.h"
67 #include "BKE_paint.h"
68 #include "BKE_particle.h"
69 #include "BKE_pointcache.h"
70 #include "BKE_scene.h"
71 #include "BKE_smoke.h"
72 #include "BKE_softbody.h"
73 #include "BKE_subsurf.h"
74 #include "BKE_texture.h"
75
76 #include "depsgraph_private.h"
77 #include "BKE_deform.h"
78 #include "BKE_shrinkwrap.h"
79
80 #include "LOD_decimation.h"
81
82 #include "CCGSubSurf.h"
83
84 #include "RE_shader_ext.h"
85
86 #include "MOD_modifiertypes.h"
87 #include "MOD_util.h"
88
89
90 /* Displace */
91
92 static void initData(ModifierData *md)
93 {
94         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
95
96         dmd->texture = NULL;
97         dmd->strength = 1;
98         dmd->direction = MOD_DISP_DIR_NOR;
99         dmd->midlevel = 0.5;
100 }
101
102 static void copyData(ModifierData *md, ModifierData *target)
103 {
104         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
105         DisplaceModifierData *tdmd = (DisplaceModifierData*) target;
106
107         tdmd->texture = dmd->texture;
108         tdmd->strength = dmd->strength;
109         tdmd->direction = dmd->direction;
110         strncpy(tdmd->defgrp_name, dmd->defgrp_name, 32);
111         tdmd->midlevel = dmd->midlevel;
112         tdmd->texmapping = dmd->texmapping;
113         tdmd->map_object = dmd->map_object;
114         strncpy(tdmd->uvlayer_name, dmd->uvlayer_name, 32);
115 }
116
117 static CustomDataMask requiredDataMask(Object *ob, ModifierData *md)
118 {
119         DisplaceModifierData *dmd = (DisplaceModifierData *)md;
120         CustomDataMask dataMask = 0;
121
122         /* ask for vertexgroups if we need them */
123         if(dmd->defgrp_name[0]) dataMask |= (1 << CD_MDEFORMVERT);
124
125         /* ask for UV coordinates if we need them */
126         if(dmd->texmapping == MOD_DISP_MAP_UV) dataMask |= (1 << CD_MTFACE);
127
128         return dataMask;
129 }
130
131 static int dependsOnTime(ModifierData *md)
132 {
133         DisplaceModifierData *dmd = (DisplaceModifierData *)md;
134
135         if(dmd->texture)
136         {
137                 return BKE_texture_dependsOnTime(dmd->texture);
138         }
139         else
140         {
141                 return 0;
142         }
143 }
144
145 static void foreachObjectLink(ModifierData *md, Object *ob,
146                                                    ObjectWalkFunc walk, void *userData)
147 {
148         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
149
150         walk(userData, ob, &dmd->map_object);
151 }
152
153 static void foreachIDLink(ModifierData *md, Object *ob,
154                                            IDWalkFunc walk, void *userData)
155 {
156         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
157
158         walk(userData, ob, (ID **)&dmd->texture);
159
160         foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
161 }
162
163 static int isDisabled(ModifierData *md, int useRenderParams)
164 {
165         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
166
167         return !dmd->texture;
168 }
169
170 static void updateDepgraph(
171                                                 ModifierData *md, DagForest *forest, Scene *scene,
172          Object *ob, DagNode *obNode)
173 {
174         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
175
176         if(dmd->map_object) {
177                 DagNode *curNode = dag_get_node(forest, dmd->map_object);
178
179                 dag_add_relation(forest, curNode, obNode,
180                                  DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Displace Modifier");
181         }
182 }
183
184 static void get_texture_coords(DisplaceModifierData *dmd, Object *ob,
185                                    DerivedMesh *dm,
186           float (*co)[3], float (*texco)[3],
187                   int numVerts)
188 {
189         int i;
190         int texmapping = dmd->texmapping;
191         float mapob_imat[4][4];
192
193         if(texmapping == MOD_DISP_MAP_OBJECT) {
194                 if(dmd->map_object)
195                         invert_m4_m4(mapob_imat, dmd->map_object->obmat);
196                 else /* if there is no map object, default to local */
197                         texmapping = MOD_DISP_MAP_LOCAL;
198         }
199
200         /* UVs need special handling, since they come from faces */
201         if(texmapping == MOD_DISP_MAP_UV) {
202                 if(CustomData_has_layer(&dm->faceData, CD_MTFACE)) {
203                         MFace *mface = dm->getFaceArray(dm);
204                         MFace *mf;
205                         char *done = MEM_callocN(sizeof(*done) * numVerts,
206                                         "get_texture_coords done");
207                         int numFaces = dm->getNumFaces(dm);
208                         char uvname[32];
209                         MTFace *tf;
210
211                         validate_layer_name(&dm->faceData, CD_MTFACE, dmd->uvlayer_name, uvname);
212                         tf = CustomData_get_layer_named(&dm->faceData, CD_MTFACE, uvname);
213
214                         /* verts are given the UV from the first face that uses them */
215                         for(i = 0, mf = mface; i < numFaces; ++i, ++mf, ++tf) {
216                                 if(!done[mf->v1]) {
217                                         texco[mf->v1][0] = tf->uv[0][0];
218                                         texco[mf->v1][1] = tf->uv[0][1];
219                                         texco[mf->v1][2] = 0;
220                                         done[mf->v1] = 1;
221                                 }
222                                 if(!done[mf->v2]) {
223                                         texco[mf->v2][0] = tf->uv[1][0];
224                                         texco[mf->v2][1] = tf->uv[1][1];
225                                         texco[mf->v2][2] = 0;
226                                         done[mf->v2] = 1;
227                                 }
228                                 if(!done[mf->v3]) {
229                                         texco[mf->v3][0] = tf->uv[2][0];
230                                         texco[mf->v3][1] = tf->uv[2][1];
231                                         texco[mf->v3][2] = 0;
232                                         done[mf->v3] = 1;
233                                 }
234                                 if(!done[mf->v4]) {
235                                         texco[mf->v4][0] = tf->uv[3][0];
236                                         texco[mf->v4][1] = tf->uv[3][1];
237                                         texco[mf->v4][2] = 0;
238                                         done[mf->v4] = 1;
239                                 }
240                         }
241
242                         /* remap UVs from [0, 1] to [-1, 1] */
243                         for(i = 0; i < numVerts; ++i) {
244                                 texco[i][0] = texco[i][0] * 2 - 1;
245                                 texco[i][1] = texco[i][1] * 2 - 1;
246                         }
247
248                         MEM_freeN(done);
249                         return;
250                 } else /* if there are no UVs, default to local */
251                         texmapping = MOD_DISP_MAP_LOCAL;
252         }
253
254         for(i = 0; i < numVerts; ++i, ++co, ++texco) {
255                 switch(texmapping) {
256                         case MOD_DISP_MAP_LOCAL:
257                                 VECCOPY(*texco, *co);
258                                 break;
259                         case MOD_DISP_MAP_GLOBAL:
260                                 VECCOPY(*texco, *co);
261                                 mul_m4_v3(ob->obmat, *texco);
262                                 break;
263                         case MOD_DISP_MAP_OBJECT:
264                                 VECCOPY(*texco, *co);
265                                 mul_m4_v3(ob->obmat, *texco);
266                                 mul_m4_v3(mapob_imat, *texco);
267                                 break;
268                 }
269         }
270 }
271
272 /* dm must be a CDDerivedMesh */
273 static void displaceModifier_do(
274                                 DisplaceModifierData *dmd, Object *ob,
275         DerivedMesh *dm, float (*vertexCos)[3], int numVerts)
276 {
277         int i;
278         MVert *mvert;
279         MDeformVert *dvert = NULL;
280         int defgrp_index;
281         float (*tex_co)[3];
282
283         if(!dmd->texture) return;
284
285         defgrp_index = defgroup_name_index(ob, dmd->defgrp_name);
286
287         mvert = CDDM_get_verts(dm);
288         if(defgrp_index >= 0)
289                 dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
290
291         tex_co = MEM_callocN(sizeof(*tex_co) * numVerts,
292                                  "displaceModifier_do tex_co");
293         get_texture_coords(dmd, ob, dm, vertexCos, tex_co, numVerts);
294
295         for(i = 0; i < numVerts; ++i) {
296                 TexResult texres;
297                 float delta = 0, strength = dmd->strength;
298                 MDeformWeight *def_weight = NULL;
299
300                 if(dvert) {
301                         int j;
302                         for(j = 0; j < dvert[i].totweight; ++j) {
303                                 if(dvert[i].dw[j].def_nr == defgrp_index) {
304                                         def_weight = &dvert[i].dw[j];
305                                         break;
306                                 }
307                         }
308                         if(!def_weight) continue;
309                 }
310
311                 texres.nor = NULL;
312                 get_texture_value(dmd->texture, tex_co[i], &texres);
313
314                 delta = texres.tin - dmd->midlevel;
315
316                 if(def_weight) strength *= def_weight->weight;
317
318                 delta *= strength;
319
320                 switch(dmd->direction) {
321                         case MOD_DISP_DIR_X:
322                                 vertexCos[i][0] += delta;
323                                 break;
324                         case MOD_DISP_DIR_Y:
325                                 vertexCos[i][1] += delta;
326                                 break;
327                         case MOD_DISP_DIR_Z:
328                                 vertexCos[i][2] += delta;
329                                 break;
330                         case MOD_DISP_DIR_RGB_XYZ:
331                                 vertexCos[i][0] += (texres.tr - dmd->midlevel) * strength;
332                                 vertexCos[i][1] += (texres.tg - dmd->midlevel) * strength;
333                                 vertexCos[i][2] += (texres.tb - dmd->midlevel) * strength;
334                                 break;
335                         case MOD_DISP_DIR_NOR:
336                                 vertexCos[i][0] += delta * mvert[i].no[0] / 32767.0f;
337                                 vertexCos[i][1] += delta * mvert[i].no[1] / 32767.0f;
338                                 vertexCos[i][2] += delta * mvert[i].no[2] / 32767.0f;
339                                 break;
340                 }
341         }
342
343         MEM_freeN(tex_co);
344 }
345
346 static void deformVerts(
347                                          ModifierData *md, Object *ob, DerivedMesh *derivedData,
348           float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
349 {
350         DerivedMesh *dm= get_cddm(md->scene, ob, NULL, derivedData, vertexCos);
351
352         displaceModifier_do((DisplaceModifierData *)md, ob, dm,
353                                  vertexCos, numVerts);
354
355         if(dm != derivedData)
356                 dm->release(dm);
357 }
358
359 static void deformVertsEM(
360                                            ModifierData *md, Object *ob, EditMesh *editData,
361         DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
362 {
363         DerivedMesh *dm= get_cddm(md->scene, ob, editData, derivedData, vertexCos);
364
365         displaceModifier_do((DisplaceModifierData *)md, ob, dm,
366                                  vertexCos, numVerts);
367
368         if(dm != derivedData)
369                 dm->release(dm);
370 }
371
372
373 ModifierTypeInfo modifierType_Displace = {
374         /* name */              "Displace",
375         /* structName */        "DisplaceModifierData",
376         /* structSize */        sizeof(DisplaceModifierData),
377         /* type */              eModifierTypeType_OnlyDeform,
378         /* flags */             eModifierTypeFlag_AcceptsMesh
379                                                         | eModifierTypeFlag_SupportsEditmode,
380
381         /* copyData */          copyData,
382         /* deformVerts */       deformVerts,
383         /* deformVertsEM */     deformVertsEM,
384         /* deformMatricesEM */  0,
385         /* applyModifier */     0,
386         /* applyModifierEM */   0,
387         /* initData */          initData,
388         /* requiredDataMask */  requiredDataMask,
389         /* freeData */          0,
390         /* isDisabled */        isDisabled,
391         /* updateDepgraph */    updateDepgraph,
392         /* dependsOnTime */     dependsOnTime,
393         /* foreachObjectLink */ foreachObjectLink,
394         /* foreachIDLink */     foreachIDLink,
395 };