GP: Refactor drawing engine to single VBO
[blender.git] / source / blender / gpencil_modifiers / intern / MOD_gpencilthick.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) 2017, Blender Foundation
19  * This is a new part of Blender
20  *
21  * Contributor(s): Antonio Vazquez
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  *
25  */
26
27 /** \file blender/gpencil_modifiers/intern/MOD_gpencilthick.c
28  *  \ingroup modifiers
29  */
30
31 #include <stdio.h>
32
33 #include "DNA_meshdata_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_gpencil_types.h"
37 #include "DNA_gpencil_modifier_types.h"
38
39 #include "BLI_utildefines.h"
40
41 #include "BKE_colortools.h"
42 #include "BKE_context.h"
43 #include "BKE_deform.h"
44 #include "BKE_gpencil.h"
45 #include "BKE_gpencil_modifier.h"
46
47 #include "DEG_depsgraph.h"
48
49 #include "MOD_gpencil_util.h"
50 #include "MOD_gpencil_modifiertypes.h"
51
52 static void initData(GpencilModifierData *md)
53 {
54         ThickGpencilModifierData *gpmd = (ThickGpencilModifierData *)md;
55         gpmd->pass_index = 0;
56         gpmd->thickness = 2;
57         gpmd->layername[0] = '\0';
58         gpmd->vgname[0] = '\0';
59         gpmd->curve_thickness = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
60         if (gpmd->curve_thickness) {
61                 curvemapping_initialize(gpmd->curve_thickness);
62         }
63 }
64
65 static void freeData(GpencilModifierData *md)
66 {
67         ThickGpencilModifierData *gpmd = (ThickGpencilModifierData *)md;
68
69         if (gpmd->curve_thickness) {
70                 curvemapping_free(gpmd->curve_thickness);
71         }
72 }
73
74 static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
75 {
76         ThickGpencilModifierData *gmd = (ThickGpencilModifierData *)md;
77         ThickGpencilModifierData *tgmd = (ThickGpencilModifierData *)target;
78
79         if (tgmd->curve_thickness != NULL) {
80                 curvemapping_free(tgmd->curve_thickness);
81                 tgmd->curve_thickness = NULL;
82         }
83
84         BKE_gpencil_modifier_copyData_generic(md, target);
85
86         tgmd->curve_thickness = curvemapping_copy(gmd->curve_thickness);
87 }
88
89 /* change stroke thickness */
90 static void deformStroke(
91         GpencilModifierData *md, Depsgraph *UNUSED(depsgraph),
92         Object *ob, bGPDlayer *gpl, bGPDstroke *gps)
93 {
94         ThickGpencilModifierData *mmd = (ThickGpencilModifierData *)md;
95         const int def_nr = defgroup_name_index(ob, mmd->vgname);
96
97         if (!is_stroke_affected_by_modifier(
98                     ob,
99                     mmd->layername, mmd->pass_index, mmd->layer_pass, 1, gpl, gps,
100                     mmd->flag & GP_THICK_INVERT_LAYER, mmd->flag & GP_THICK_INVERT_PASS,
101                     mmd->flag & GP_THICK_INVERT_LAYERPASS))
102         {
103                 return;
104         }
105
106         /* if normalize, set stroke thickness */
107         if (mmd->flag & GP_THICK_NORMALIZE) {
108                 gps->thickness = mmd->thickness;
109         }
110
111         for (int i = 0; i < gps->totpoints; i++) {
112                 bGPDspoint *pt = &gps->points[i];
113                 MDeformVert *dvert = gps->dvert != NULL ? &gps->dvert[i] : NULL;
114                 float curvef = 1.0f;
115                 /* verify vertex group */
116                 const float weight = get_modifier_point_weight(dvert, (mmd->flag & GP_THICK_INVERT_VGROUP) != 0, def_nr);
117                 if (weight < 0.0f) {
118                         continue;
119                 }
120
121                 if (mmd->flag & GP_THICK_NORMALIZE) {
122                         pt->pressure = 1.0f;
123                 }
124                 else {
125                         if ((mmd->flag & GP_THICK_CUSTOM_CURVE) && (mmd->curve_thickness)) {
126                                 /* normalize value to evaluate curve */
127                                 float value = (float)i / (gps->totpoints - 1);
128                                 curvef = curvemapping_evaluateF(mmd->curve_thickness, 0, value);
129                         }
130
131                         pt->pressure += mmd->thickness * weight * curvef;
132                         CLAMP_MIN(pt->pressure, 0.1f);
133                 }
134         }
135 }
136
137 static void bakeModifier(
138         struct Main *UNUSED(bmain), Depsgraph *depsgraph,
139         GpencilModifierData *md, Object *ob)
140 {
141         bGPdata *gpd = ob->data;
142
143         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
144                 for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
145                         for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
146                                 deformStroke(md, depsgraph, ob, gpl, gps);
147                         }
148                 }
149         }
150 }
151
152 GpencilModifierTypeInfo modifierType_Gpencil_Thick = {
153         /* name */              "Thickness",
154         /* structName */        "ThickGpencilModifierData",
155         /* structSize */        sizeof(ThickGpencilModifierData),
156         /* type */              eGpencilModifierTypeType_Gpencil,
157         /* flags */             eGpencilModifierTypeFlag_SupportsEditmode,
158
159         /* copyData */          copyData,
160
161         /* deformStroke */      deformStroke,
162         /* generateStrokes */   NULL,
163         /* bakeModifier */      bakeModifier,
164         /* remapTime */         NULL,
165
166         /* initData */          initData,
167         /* freeData */          freeData,
168         /* isDisabled */        NULL,
169         /* updateDepsgraph */   NULL,
170         /* dependsOnTime */     NULL,
171         /* foreachObjectLink */ NULL,
172         /* foreachIDLink */     NULL,
173         /* foreachTexLink */    NULL,
174         /* getDuplicationFactor */ NULL,
175 };