1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
/* linestyle.c
*
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2010 Blender Foundation.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*/
#include <stdio.h>
#include <stdlib.h>
#include "MEM_guardedalloc.h"
#include "DNA_material_types.h" /* for ramp blend */
#include "BKE_global.h"
#include "BKE_library.h"
#include "BKE_linestyle.h"
#include "BKE_main.h"
#include "BKE_texture.h"
#include "BKE_colortools.h"
#include "BLI_blenlib.h"
static char *modifier_name[LS_MODIFIER_NUM] = {
NULL,
"Along Stroke",
"Distance from Camera",
"Distance from Object"};
static void default_linestyle_settings(FreestyleLineStyle *linestyle)
{
linestyle->panel = LS_PANEL_COLOR;
linestyle->r = linestyle->g = linestyle->b = 0.0;
linestyle->alpha = 1.0;
linestyle->thickness = 1.0;
linestyle->color_modifiers.first = linestyle->color_modifiers.last = NULL;
linestyle->alpha_modifiers.first = linestyle->alpha_modifiers.last = NULL;
linestyle->thickness_modifiers.first = linestyle->thickness_modifiers.last = NULL;
}
FreestyleLineStyle *FRS_new_linestyle(char *name, struct Main *main)
{
FreestyleLineStyle *linestyle;
if (!main)
main = G.main;
linestyle = (FreestyleLineStyle *)alloc_libblock(&main->linestyle, ID_LS, name);
default_linestyle_settings(linestyle);
return linestyle;
}
void FRS_free_linestyle(FreestyleLineStyle *linestyle)
{
LineStyleModifier *m;
while ((m = (LineStyleModifier *)linestyle->color_modifiers.first))
FRS_remove_linestyle_color_modifier(linestyle, m);
while ((m = (LineStyleModifier *)linestyle->alpha_modifiers.first))
FRS_remove_linestyle_alpha_modifier(linestyle, m);
while ((m = (LineStyleModifier *)linestyle->thickness_modifiers.first))
FRS_remove_linestyle_thickness_modifier(linestyle, m);
}
static LineStyleModifier *new_modifier(int type, size_t size)
{
LineStyleModifier *m;
m = (LineStyleModifier *)MEM_callocN(size, "line style modifier");
if (m) {
m->type = type;
strcpy(m->name, modifier_name[type]);
m->influence = 1.0f;
m->flags = LS_MODIFIER_ENABLED | LS_MODIFIER_EXPANDED;
}
return m;
}
static void add_to_modifier_list(ListBase *lb, LineStyleModifier *m)
{
BLI_addtail(lb, (void *)m);
BLI_uniquename(lb, m, modifier_name[m->type], '.', BLI_STRUCT_OFFSET(LineStyleModifier, name), sizeof(m->name));
}
int FRS_add_linestyle_color_modifier(FreestyleLineStyle *linestyle, int type)
{
static size_t modifier_size[LS_MODIFIER_NUM] = {
0,
sizeof(LineStyleColorModifier_AlongStroke),
sizeof(LineStyleColorModifier_DistanceFromCamera),
sizeof(LineStyleColorModifier_DistanceFromObject)
};
LineStyleModifier *m;
if (type <= 0 || type >= LS_MODIFIER_NUM || modifier_size[type] == 0)
return -1;
m = new_modifier(type, modifier_size[type]);
if (!m)
return -1;
switch (type) {
case LS_MODIFIER_ALONG_STROKE:
((LineStyleColorModifier_AlongStroke *)m)->color_ramp = add_colorband(1);
((LineStyleColorModifier_AlongStroke *)m)->blend = MA_RAMP_BLEND;
break;
case LS_MODIFIER_DISTANCE_FROM_CAMERA:
((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp = add_colorband(1);
((LineStyleColorModifier_DistanceFromCamera *)m)->blend = MA_RAMP_BLEND;
((LineStyleColorModifier_DistanceFromCamera *)m)->range_min = 0.0f;
((LineStyleColorModifier_DistanceFromCamera *)m)->range_max = 10000.0f;
break;
case LS_MODIFIER_DISTANCE_FROM_OBJECT:
((LineStyleColorModifier_DistanceFromObject *)m)->target = NULL;
((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp = add_colorband(1);
((LineStyleColorModifier_DistanceFromObject *)m)->blend = MA_RAMP_BLEND;
((LineStyleColorModifier_DistanceFromObject *)m)->range_min = 0.0f;
((LineStyleColorModifier_DistanceFromObject *)m)->range_max = 10000.0f;
break;
default:
return -1; /* unknown modifier type */
}
add_to_modifier_list(&linestyle->color_modifiers, m);
return 0;
}
void FRS_remove_linestyle_color_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
{
switch (m->type) {
case LS_MODIFIER_ALONG_STROKE:
MEM_freeN(((LineStyleColorModifier_AlongStroke *)m)->color_ramp);
break;
case LS_MODIFIER_DISTANCE_FROM_CAMERA:
MEM_freeN(((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp);
break;
case LS_MODIFIER_DISTANCE_FROM_OBJECT:
MEM_freeN(((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp);
break;
}
BLI_freelinkN(&linestyle->color_modifiers, m);
}
int FRS_add_linestyle_alpha_modifier(FreestyleLineStyle *linestyle, int type)
{
static size_t modifier_size[LS_MODIFIER_NUM] = {
0,
sizeof(LineStyleAlphaModifier_AlongStroke),
sizeof(LineStyleAlphaModifier_DistanceFromCamera),
sizeof(LineStyleAlphaModifier_DistanceFromObject)
};
LineStyleModifier *m;
if (type <= 0 || type >= LS_MODIFIER_NUM || modifier_size[type] == 0)
return -1;
m = new_modifier(type, modifier_size[type]);
if (!m)
return -1;
switch (type) {
case LS_MODIFIER_ALONG_STROKE:
((LineStyleAlphaModifier_AlongStroke *)m)->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
((LineStyleAlphaModifier_AlongStroke *)m)->blend = LS_VALUE_ADD;
break;
case LS_MODIFIER_DISTANCE_FROM_CAMERA:
((LineStyleAlphaModifier_DistanceFromCamera *)m)->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
((LineStyleAlphaModifier_DistanceFromCamera *)m)->blend = LS_VALUE_ADD;
((LineStyleAlphaModifier_DistanceFromCamera *)m)->range_min = 0.0f;
((LineStyleAlphaModifier_DistanceFromCamera *)m)->range_max = 10000.0f;
break;
case LS_MODIFIER_DISTANCE_FROM_OBJECT:
((LineStyleAlphaModifier_DistanceFromObject *)m)->target = NULL;
((LineStyleAlphaModifier_DistanceFromObject *)m)->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
((LineStyleAlphaModifier_DistanceFromObject *)m)->blend = LS_VALUE_ADD;
((LineStyleAlphaModifier_DistanceFromObject *)m)->range_min = 0.0f;
((LineStyleAlphaModifier_DistanceFromObject *)m)->range_max = 10000.0f;
break;
default:
return -1; /* unknown modifier type */
}
add_to_modifier_list(&linestyle->alpha_modifiers, m);
return 0;
}
void FRS_remove_linestyle_alpha_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
{
switch (m->type) {
case LS_MODIFIER_ALONG_STROKE:
curvemapping_free(((LineStyleAlphaModifier_AlongStroke *)m)->curve);
break;
case LS_MODIFIER_DISTANCE_FROM_CAMERA:
curvemapping_free(((LineStyleAlphaModifier_DistanceFromCamera *)m)->curve);
break;
case LS_MODIFIER_DISTANCE_FROM_OBJECT:
curvemapping_free(((LineStyleAlphaModifier_DistanceFromObject *)m)->curve);
break;
}
BLI_freelinkN(&linestyle->alpha_modifiers, m);
}
int FRS_add_linestyle_thickness_modifier(FreestyleLineStyle *linestyle, int type)
{
static size_t modifier_size[LS_MODIFIER_NUM] = {
0,
sizeof(LineStyleThicknessModifier_AlongStroke),
sizeof(LineStyleThicknessModifier_DistanceFromCamera),
sizeof(LineStyleThicknessModifier_DistanceFromObject)
};
LineStyleModifier *m;
if (type <= 0 || type >= LS_MODIFIER_NUM || modifier_size[type] == 0)
return -1;
m = new_modifier(type, modifier_size[type]);
if (!m)
return -1;
switch (type) {
case LS_MODIFIER_ALONG_STROKE:
((LineStyleThicknessModifier_AlongStroke *)m)->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
((LineStyleThicknessModifier_AlongStroke *)m)->blend = LS_VALUE_ADD;
((LineStyleThicknessModifier_AlongStroke *)m)->value_min = 0.0f;
((LineStyleThicknessModifier_AlongStroke *)m)->value_max = 1.0f;
break;
case LS_MODIFIER_DISTANCE_FROM_CAMERA:
((LineStyleThicknessModifier_DistanceFromCamera *)m)->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
((LineStyleThicknessModifier_DistanceFromCamera *)m)->blend = LS_VALUE_ADD;
((LineStyleThicknessModifier_DistanceFromCamera *)m)->range_min = 0.0f;
((LineStyleThicknessModifier_DistanceFromCamera *)m)->range_max = 1000.0f;
((LineStyleThicknessModifier_DistanceFromCamera *)m)->value_min = 0.0f;
((LineStyleThicknessModifier_DistanceFromCamera *)m)->value_max = 1.0f;
break;
case LS_MODIFIER_DISTANCE_FROM_OBJECT:
((LineStyleThicknessModifier_DistanceFromObject *)m)->target = NULL;
((LineStyleThicknessModifier_DistanceFromObject *)m)->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
((LineStyleThicknessModifier_DistanceFromObject *)m)->blend = LS_VALUE_ADD;
((LineStyleThicknessModifier_DistanceFromObject *)m)->range_min = 0.0f;
((LineStyleThicknessModifier_DistanceFromObject *)m)->range_max = 1000.0f;
((LineStyleThicknessModifier_DistanceFromObject *)m)->value_min = 0.0f;
((LineStyleThicknessModifier_DistanceFromObject *)m)->value_max = 1.0f;
break;
default:
return -1; /* unknown modifier type */
}
add_to_modifier_list(&linestyle->thickness_modifiers, m);
return 0;
}
void FRS_remove_linestyle_thickness_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
{
switch (m->type) {
case LS_MODIFIER_ALONG_STROKE:
curvemapping_free(((LineStyleThicknessModifier_AlongStroke *)m)->curve);
break;
case LS_MODIFIER_DISTANCE_FROM_CAMERA:
curvemapping_free(((LineStyleThicknessModifier_DistanceFromCamera *)m)->curve);
break;
case LS_MODIFIER_DISTANCE_FROM_OBJECT:
curvemapping_free(((LineStyleThicknessModifier_DistanceFromObject *)m)->curve);
break;
}
BLI_freelinkN(&linestyle->thickness_modifiers, m);
}
static void move_modifier(ListBase *lb, LineStyleModifier *modifier, int direction)
{
BLI_remlink(lb, modifier);
if (direction > 0)
BLI_insertlinkbefore(lb, modifier->prev, modifier);
else
BLI_insertlinkafter(lb, modifier->next, modifier);
}
void FRS_move_linestyle_color_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
{
move_modifier(&linestyle->color_modifiers, modifier, direction);
}
void FRS_move_linestyle_alpha_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
{
move_modifier(&linestyle->alpha_modifiers, modifier, direction);
}
void FRS_move_linestyle_thickness_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
{
move_modifier(&linestyle->thickness_modifiers, modifier, direction);
}
|