GP: Refactor drawing engine to single VBO
[blender.git] / source / blender / gpencil_modifiers / intern / MOD_gpencilcolor.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_gpencilcolor.c
28  *  \ingroup modifiers
29  */
30
31 #include <stdio.h>
32
33 #include "DNA_scene_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_gpencil_types.h"
36 #include "DNA_gpencil_modifier_types.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLI_ghash.h"
40 #include "BLI_math_color.h"
41 #include "BLI_math_vector.h"
42 #include "BLI_utildefines.h"
43
44 #include "BKE_global.h"
45 #include "BKE_context.h"
46 #include "BKE_gpencil.h"
47 #include "BKE_gpencil_modifier.h"
48 #include "BKE_main.h"
49 #include "BKE_material.h"
50
51 #include "DEG_depsgraph.h"
52
53 #include "MOD_gpencil_util.h"
54 #include "MOD_gpencil_modifiertypes.h"
55
56 static void initData(GpencilModifierData *md)
57 {
58         ColorGpencilModifierData *gpmd = (ColorGpencilModifierData *)md;
59         gpmd->pass_index = 0;
60         ARRAY_SET_ITEMS(gpmd->hsv, 1.0f, 1.0f, 1.0f);
61         gpmd->layername[0] = '\0';
62         gpmd->flag |= GP_COLOR_CREATE_COLORS;
63         gpmd->modify_color = GP_MODIFY_COLOR_BOTH;
64 }
65
66 static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
67 {
68         BKE_gpencil_modifier_copyData_generic(md, target);
69 }
70
71 /* color correction strokes */
72 static void deformStroke(
73         GpencilModifierData *md, Depsgraph *UNUSED(depsgraph),
74         Object *ob, bGPDlayer *gpl, bGPDstroke *gps)
75 {
76
77         ColorGpencilModifierData *mmd = (ColorGpencilModifierData *)md;
78         float hsv[3], factor[3];
79
80         if (!is_stroke_affected_by_modifier(
81                     ob,
82                     mmd->layername, mmd->pass_index, mmd->layer_pass, 1, gpl, gps,
83                     mmd->flag & GP_COLOR_INVERT_LAYER, mmd->flag & GP_COLOR_INVERT_PASS,
84                     mmd->flag & GP_COLOR_INVERT_LAYERPASS))
85         {
86                 return;
87         }
88
89         copy_v3_v3(factor, mmd->hsv);
90         add_v3_fl(factor, -1.0f);
91
92         if (mmd->modify_color != GP_MODIFY_COLOR_FILL) {
93                 rgb_to_hsv_v(gps->runtime.tmp_stroke_rgba, hsv);
94                 add_v3_v3(hsv, factor);
95                 CLAMP3(hsv, 0.0f, 1.0f);
96                 hsv_to_rgb_v(hsv, gps->runtime.tmp_stroke_rgba);
97         }
98
99         if (mmd->modify_color != GP_MODIFY_COLOR_STROKE) {
100                 rgb_to_hsv_v(gps->runtime.tmp_fill_rgba, hsv);
101                 add_v3_v3(hsv, factor);
102                 CLAMP3(hsv, 0.0f, 1.0f);
103                 hsv_to_rgb_v(hsv, gps->runtime.tmp_fill_rgba);
104         }
105 }
106
107 static void bakeModifier(
108         Main *bmain, Depsgraph *depsgraph,
109         GpencilModifierData *md, Object *ob)
110 {
111         ColorGpencilModifierData *mmd = (ColorGpencilModifierData *)md;
112         bGPdata *gpd = ob->data;
113
114         GHash *gh_color = BLI_ghash_str_new("GP_Color modifier");
115         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
116                 for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
117                         for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
118
119                                 Material *mat = give_current_material(ob, gps->mat_nr + 1);
120                                 if (mat == NULL)
121                                         continue;
122                                 MaterialGPencilStyle *gp_style = mat->gp_style;
123                                 /* skip stroke if it doesn't have color info */
124                                 if (ELEM(NULL, gp_style))
125                                         continue;
126
127                                 copy_v4_v4(gps->runtime.tmp_stroke_rgba, gp_style->stroke_rgba);
128                                 copy_v4_v4(gps->runtime.tmp_fill_rgba, gp_style->fill_rgba);
129
130                                 deformStroke(md, depsgraph, ob, gpl, gps);
131
132                                 gpencil_apply_modifier_material(
133                                         bmain, ob, mat, gh_color, gps,
134                                         (bool)(mmd->flag & GP_COLOR_CREATE_COLORS));
135                         }
136                 }
137         }
138         /* free hash buffers */
139         if (gh_color) {
140                 BLI_ghash_free(gh_color, NULL, NULL);
141                 gh_color = NULL;
142         }
143 }
144
145 GpencilModifierTypeInfo modifierType_Gpencil_Color = {
146         /* name */              "Hue/Saturation",
147         /* structName */        "ColorGpencilModifierData",
148         /* structSize */        sizeof(ColorGpencilModifierData),
149         /* type */              eGpencilModifierTypeType_Gpencil,
150         /* flags */             eGpencilModifierTypeFlag_SupportsEditmode,
151
152         /* copyData */          copyData,
153
154         /* deformStroke */      deformStroke,
155         /* generateStrokes */   NULL,
156         /* bakeModifier */      bakeModifier,
157         /* remapTime */         NULL,
158
159         /* initData */          initData,
160         /* freeData */          NULL,
161         /* isDisabled */        NULL,
162         /* updateDepsgraph */   NULL,
163         /* dependsOnTime */     NULL,
164         /* foreachObjectLink */ NULL,
165         /* foreachIDLink */     NULL,
166         /* foreachTexLink */    NULL,
167         /* getDuplicationFactor */ NULL,
168 };