Merge branch 'master' into blender2.8
[blender.git] / source / blender / modifiers / intern / MOD_wireframe.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  * ***** END GPL LICENSE BLOCK *****
19  *
20  */
21
22 /** \file blender/modifiers/intern/MOD_wireframe.c
23  *  \ingroup modifiers
24  */
25
26 #include "DNA_mesh_types.h"
27 #include "DNA_object_types.h"
28
29 #include "BLI_utildefines.h"
30
31 #include "BKE_cdderivedmesh.h"
32 #include "BKE_deform.h"
33 #include "BKE_mesh.h"
34
35 #include "MOD_modifiertypes.h"
36
37 #include "bmesh.h"
38 #include "tools/bmesh_wireframe.h"
39
40 static void initData(ModifierData *md)
41 {
42         WireframeModifierData *wmd = (WireframeModifierData *)md;
43         wmd->offset = 0.02f;
44         wmd->flag = MOD_WIREFRAME_REPLACE | MOD_WIREFRAME_OFS_EVEN;
45         wmd->crease_weight = 1.0f;
46 }
47
48 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
49 {
50         WireframeModifierData *wmd = (WireframeModifierData *)md;
51         CustomDataMask dataMask = 0;
52
53         /* ask for vertexgroups if we need them */
54         if (wmd->defgrp_name[0]) dataMask |= CD_MASK_MDEFORMVERT;
55
56         return dataMask;
57
58 }
59
60 static bool dependsOnNormals(ModifierData *UNUSED(md))
61 {
62         return true;
63 }
64
65 static Mesh *WireframeModifier_do(WireframeModifierData *wmd, Object *ob, Mesh *mesh)
66 {
67         Mesh *result;
68         BMesh *bm;
69
70         const int defgrp_index = defgroup_name_index(ob, wmd->defgrp_name);
71
72         bm = BKE_mesh_to_bmesh_ex(
73                 mesh,
74                 &(struct BMeshCreateParams){0},
75                 &(struct BMeshFromMeshParams){
76                     .calc_face_normal = true,
77                     .add_key_index = false,
78                     .use_shapekey = true,
79                     .active_shapekey = ob->shapenr,
80                 });
81
82         BM_mesh_wireframe(
83                bm,
84                wmd->offset, wmd->offset_fac, wmd->offset_fac_vg,
85                (wmd->flag & MOD_WIREFRAME_REPLACE) != 0,
86                (wmd->flag & MOD_WIREFRAME_BOUNDARY) != 0,
87                (wmd->flag & MOD_WIREFRAME_OFS_EVEN) != 0,
88                (wmd->flag & MOD_WIREFRAME_OFS_RELATIVE) != 0,
89                (wmd->flag & MOD_WIREFRAME_CREASE) != 0,
90                wmd->crease_weight,
91                defgrp_index,
92                (wmd->flag & MOD_WIREFRAME_INVERT_VGROUP) != 0,
93                wmd->mat_ofs,
94                MAX2(ob->totcol - 1, 0),
95                false);
96
97         result = BKE_bmesh_to_mesh_nomain(bm, &(struct BMeshToMeshParams){0});
98         BM_mesh_free(bm);
99
100         result->runtime.cd_dirty_vert |= CD_MASK_NORMAL;
101
102         return result;
103
104 }
105
106 static Mesh *applyModifier(
107         ModifierData *md,
108         const struct ModifierEvalContext *ctx,
109         struct Mesh *mesh)
110 {
111         return WireframeModifier_do((WireframeModifierData *)md, ctx->object, mesh);
112 }
113
114
115 ModifierTypeInfo modifierType_Wireframe = {
116         /* name */              "Wireframe",
117         /* structName */        "WireframeModifierData",
118         /* structSize */        sizeof(WireframeModifierData),
119         /* type */              eModifierTypeType_Constructive,
120         /* flags */             eModifierTypeFlag_AcceptsMesh |
121                                 eModifierTypeFlag_SupportsEditmode,
122
123         /* copyData */          modifier_copyData_generic,
124
125         /* deformVerts_DM */    NULL,
126         /* deformMatrices_DM */ NULL,
127         /* deformVertsEM_DM */  NULL,
128         /* deformMatricesEM_DM*/NULL,
129         /* applyModifier_DM */  NULL,
130         /* applyModifierEM_DM */NULL,
131
132         /* deformVerts */       NULL,
133         /* deformMatrices */    NULL,
134         /* deformVertsEM */     NULL,
135         /* deformMatricesEM */  NULL,
136         /* applyModifier */     applyModifier,
137         /* applyModifierEM */   NULL,
138
139         /* initData */          initData,
140         /* requiredDataMask */  requiredDataMask,
141         /* freeData */          NULL,
142         /* isDisabled */        NULL,
143         /* updateDepgraph */    NULL,
144         /* dependsOnTime */     NULL,
145         /* dependsOnNormals */  dependsOnNormals,
146         /* foreachObjectLink */ NULL,
147         /* foreachIDLink */     NULL,
148         /* foreachTexLink */    NULL,
149 };