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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
|
/*
* 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) 2012 Blender Foundation.
* All rights reserved.
*/
/** \file blender/editors/mask/mask_draw.c
* \ingroup edmask
*/
#include "MEM_guardedalloc.h"
#include "BLI_utildefines.h"
#include "BLI_math.h"
#include "BLI_rect.h"
#include "BLI_task.h"
#include "BKE_context.h"
#include "BKE_mask.h"
#include "DNA_mask_types.h"
#include "DNA_screen_types.h"
#include "DNA_object_types.h" /* SELECT */
#include "DNA_space_types.h"
#include "ED_clip.h"
#include "ED_mask.h" /* own include */
#include "ED_space_api.h"
#include "BIF_gl.h"
#include "BIF_glutil.h"
#include "GPU_basic_shader.h"
#include "UI_resources.h"
#include "UI_view2d.h"
#include "mask_intern.h" /* own include */
static void mask_spline_color_get(MaskLayer *masklay, MaskSpline *spline, const bool is_sel,
unsigned char r_rgb[4])
{
if (is_sel) {
if (masklay->act_spline == spline) {
r_rgb[0] = r_rgb[1] = r_rgb[2] = 255;
}
else {
r_rgb[0] = 255;
r_rgb[1] = r_rgb[2] = 0;
}
}
else {
r_rgb[0] = 128;
r_rgb[1] = r_rgb[2] = 0;
}
r_rgb[3] = 255;
}
static void mask_spline_feather_color_get(MaskLayer *UNUSED(masklay), MaskSpline *UNUSED(spline), const bool is_sel,
unsigned char r_rgb[4])
{
if (is_sel) {
r_rgb[1] = 255;
r_rgb[0] = r_rgb[2] = 0;
}
else {
r_rgb[1] = 128;
r_rgb[0] = r_rgb[2] = 0;
}
r_rgb[3] = 255;
}
#if 0
static void draw_spline_parents(MaskLayer *UNUSED(masklay), MaskSpline *spline)
{
int i;
MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline);
if (!spline->tot_point)
return;
GPU_basic_shader_bind_enable(GPU_SHADER_LINE | GPU_SHADER_STIPPLE);
GPU_basic_shader_line_stipple(1, 0xAAAA);
glColor3ub(0, 0, 0);
glBegin(GL_LINES);
for (i = 0; i < spline->tot_point; i++) {
MaskSplinePoint *point = &points_array[i];
BezTriple *bezt = &point->bezt;
if (point->parent.id) {
glVertex2f(bezt->vec[1][0],
bezt->vec[1][1]);
glVertex2f(bezt->vec[1][0] - point->parent.offset[0],
bezt->vec[1][1] - point->parent.offset[1]);
}
}
glEnd();
GPU_basic_shader_bind_disable(GPU_SHADER_LINE | GPU_SHADER_STIPPLE);
}
#endif
static void mask_point_undistort_pos(SpaceClip *sc, float r_co[2], const float co[2])
{
BKE_mask_coord_to_movieclip(sc->clip, &sc->user, r_co, co);
ED_clip_point_undistorted_pos(sc, r_co, r_co);
BKE_mask_coord_from_movieclip(sc->clip, &sc->user, r_co, r_co);
}
static void draw_circle(const float x, const float y,
const float size, const bool fill,
const float xscale, const float yscale)
{
static GLuint wire_displist = 0;
static GLuint fill_displist = 0;
GLuint displist = fill ? fill_displist : wire_displist;
/* Initialize round circle shape. */
if (displist == 0) {
GLUquadricObj *qobj;
displist = glGenLists(1);
glNewList(displist, GL_COMPILE);
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj, fill ? GLU_FILL : GLU_SILHOUETTE);
gluDisk(qobj, 0, 0.7, 8, 1);
gluDeleteQuadric(qobj);
glEndList();
if (fill) {
fill_displist = displist;
}
else {
wire_displist = displist;
}
}
glPushMatrix();
glTranslatef(x, y, 0.0f);
glScalef(1.0f / xscale * size, 1.0f / yscale * size, 1.0f);
glCallList(displist);
glPopMatrix();
}
static void draw_single_handle(const MaskLayer *mask_layer, const MaskSplinePoint *point,
const eMaskWhichHandle which_handle, const int draw_type,
const float handle_size, const float xscale, const float yscale,
const float point_pos[2], const float handle_pos[2])
{
const BezTriple *bezt = &point->bezt;
char handle_type;
if (which_handle == MASK_WHICH_HANDLE_STICK || which_handle == MASK_WHICH_HANDLE_LEFT) {
handle_type = bezt->h1;
}
else {
handle_type = bezt->h2;
}
if (handle_type == HD_VECT) {
return;
}
/* this could be split into its own loop */
if (draw_type == MASK_DT_OUTLINE) {
const unsigned char rgb_gray[4] = {0x60, 0x60, 0x60, 0xff};
glLineWidth(3);
glColor4ubv(rgb_gray);
glBegin(GL_LINES);
glVertex2fv(point_pos);
glVertex2fv(handle_pos);
glEnd();
}
switch (handle_type) {
case HD_FREE:
UI_ThemeColor(TH_HANDLE_FREE);
break;
case HD_AUTO:
UI_ThemeColor(TH_HANDLE_AUTO);
break;
case HD_ALIGN:
case HD_ALIGN_DOUBLESIDE:
UI_ThemeColor(TH_HANDLE_ALIGN);
break;
}
glLineWidth(1);
glBegin(GL_LINES);
glVertex2fv(point_pos);
glVertex2fv(handle_pos);
glEnd();
/* draw handle points */
if (MASKPOINT_ISSEL_HANDLE(point, which_handle)) {
if (point == mask_layer->act_point)
glColor3f(1.0f, 1.0f, 1.0f);
else
UI_ThemeColor(TH_HANDLE_VERTEX_SELECT);
}
else {
UI_ThemeColor(TH_HANDLE_VERTEX);
}
draw_circle(handle_pos[0], handle_pos[1], handle_size, false, xscale, yscale);
}
/* return non-zero if spline is selected */
static void draw_spline_points(const bContext *C, MaskLayer *masklay, MaskSpline *spline,
const char draw_flag, const char draw_type,
const float xscale, const float yscale)
{
const bool is_spline_sel = (spline->flag & SELECT) && (masklay->restrictflag & MASK_RESTRICT_SELECT) == 0;
const bool is_smooth = (draw_flag & MASK_DRAWFLAG_SMOOTH) != 0;
unsigned char rgb_spline[4];
MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline);
SpaceClip *sc = CTX_wm_space_clip(C);
bool undistort = false;
int i, handle_size, tot_feather_point;
float (*feather_points)[2], (*fp)[2];
float min[2], max[2];
if (!spline->tot_point)
return;
if (sc)
undistort = sc->clip && (sc->user.render_flag & MCLIP_PROXY_RENDER_UNDISTORT);
/* TODO, add this to sequence editor */
handle_size = UI_GetThemeValuef(TH_HANDLE_VERTEX_SIZE) * U.pixelsize;
glPointSize(handle_size);
mask_spline_color_get(masklay, spline, is_spline_sel, rgb_spline);
/* feather points */
feather_points = fp = BKE_mask_spline_feather_points(spline, &tot_feather_point);
for (i = 0; i < spline->tot_point; i++) {
/* watch it! this is intentionally not the deform array, only check for sel */
MaskSplinePoint *point = &spline->points[i];
int j;
for (j = 0; j <= point->tot_uw; j++) {
float feather_point[2];
bool sel = false;
copy_v2_v2(feather_point, *fp);
if (undistort)
mask_point_undistort_pos(sc, feather_point, feather_point);
if (j == 0) {
sel = MASKPOINT_ISSEL_ANY(point);
}
else {
sel = (point->uw[j - 1].flag & SELECT) != 0;
}
if (sel) {
if (point == masklay->act_point)
glColor3f(1.0f, 1.0f, 1.0f);
else
UI_ThemeColor(TH_HANDLE_VERTEX_SELECT);
}
else {
UI_ThemeColor(TH_HANDLE_VERTEX);
}
glBegin(GL_POINTS);
glVertex2fv(feather_point);
glEnd();
fp++;
}
}
MEM_freeN(feather_points);
if (is_smooth) {
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
/* control points */
INIT_MINMAX2(min, max);
for (i = 0; i < spline->tot_point; i++) {
/* watch it! this is intentionally not the deform array, only check for sel */
MaskSplinePoint *point = &spline->points[i];
MaskSplinePoint *point_deform = &points_array[i];
BezTriple *bezt = &point_deform->bezt;
float vert[2];
copy_v2_v2(vert, bezt->vec[1]);
if (undistort) {
mask_point_undistort_pos(sc, vert, vert);
}
/* draw handle segment */
if (BKE_mask_point_handles_mode_get(point) == MASK_HANDLE_MODE_STICK) {
float handle[2];
BKE_mask_point_handle(point_deform, MASK_WHICH_HANDLE_STICK, handle);
if (undistort) {
mask_point_undistort_pos(sc, handle, handle);
}
draw_single_handle(masklay, point, MASK_WHICH_HANDLE_STICK,
draw_type, handle_size, xscale, yscale, vert, handle);
}
else {
float handle_left[2], handle_right[2];
BKE_mask_point_handle(point_deform, MASK_WHICH_HANDLE_LEFT, handle_left);
BKE_mask_point_handle(point_deform, MASK_WHICH_HANDLE_RIGHT, handle_right);
if (undistort) {
mask_point_undistort_pos(sc, handle_left, handle_left);
mask_point_undistort_pos(sc, handle_left, handle_left);
}
draw_single_handle(masklay, point, MASK_WHICH_HANDLE_LEFT,
draw_type, handle_size, xscale, yscale, vert, handle_left);
draw_single_handle(masklay, point, MASK_WHICH_HANDLE_RIGHT,
draw_type, handle_size, xscale, yscale, vert, handle_right);
}
/* draw CV point */
if (MASKPOINT_ISSEL_KNOT(point)) {
if (point == masklay->act_point)
glColor3f(1.0f, 1.0f, 1.0f);
else
UI_ThemeColor(TH_HANDLE_VERTEX_SELECT);
}
else
UI_ThemeColor(TH_HANDLE_VERTEX);
glBegin(GL_POINTS);
glVertex2fv(vert);
glEnd();
minmax_v2v2_v2(min, max, vert);
}
if (is_spline_sel) {
float x = (min[0] + max[0]) / 2.0f;
float y = (min[1] + max[1]) / 2.0f;
/* TODO(sergey): Remove hardcoded colors. */
if (masklay->act_spline == spline) {
glColor3ub(255, 255, 255);
}
else {
glColor3ub(255, 255, 0);
}
draw_circle(x, y, 6.0f, true, xscale, yscale);
glColor3ub(0, 0, 0);
draw_circle(x, y, 6.0f, false, xscale, yscale);
}
if (is_smooth) {
glDisable(GL_LINE_SMOOTH);
glDisable(GL_BLEND);
}
}
/* #define USE_XOR */
static void mask_color_active_tint(unsigned char r_rgb[4], const unsigned char rgb[4], const bool is_active)
{
if (!is_active) {
r_rgb[0] = (unsigned char)((((int)(rgb[0])) + 128) / 2);
r_rgb[1] = (unsigned char)((((int)(rgb[1])) + 128) / 2);
r_rgb[2] = (unsigned char)((((int)(rgb[2])) + 128) / 2);
r_rgb[3] = rgb[3];
}
else {
*(unsigned int *)r_rgb = *(const unsigned int *)rgb;
}
}
static void mask_draw_curve_type(const bContext *C, MaskSpline *spline, float (*orig_points)[2], int tot_point,
const bool is_feather, const bool is_smooth, const bool is_active,
const unsigned char rgb_spline[4], const char draw_type)
{
const int draw_method = (spline->flag & MASK_SPLINE_CYCLIC) ? GL_LINE_LOOP : GL_LINE_STRIP;
const unsigned char rgb_black[4] = {0x00, 0x00, 0x00, 0xff};
// const unsigned char rgb_white[4] = {0xff, 0xff, 0xff, 0xff};
unsigned char rgb_tmp[4];
SpaceClip *sc = CTX_wm_space_clip(C);
float (*points)[2] = orig_points;
if (sc) {
const bool undistort = sc->clip && (sc->user.render_flag & MCLIP_PROXY_RENDER_UNDISTORT);
if (undistort) {
int i;
points = MEM_callocN(2 * tot_point * sizeof(float), "undistorthed mask curve");
for (i = 0; i < tot_point; i++) {
mask_point_undistort_pos(sc, points[i], orig_points[i]);
}
}
}
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, points);
switch (draw_type) {
case MASK_DT_OUTLINE:
glLineWidth(3);
mask_color_active_tint(rgb_tmp, rgb_black, is_active);
glColor4ubv(rgb_tmp);
glDrawArrays(draw_method, 0, tot_point);
glLineWidth(1);
mask_color_active_tint(rgb_tmp, rgb_spline, is_active);
glColor4ubv(rgb_tmp);
glDrawArrays(draw_method, 0, tot_point);
break;
case MASK_DT_DASH:
default:
GPU_basic_shader_bind_enable(GPU_SHADER_LINE | GPU_SHADER_STIPPLE);
GPU_basic_shader_line_stipple(3, 0xAAAA);
#ifdef USE_XOR
glEnable(GL_COLOR_LOGIC_OP);
glLogicOp(GL_OR);
#endif
mask_color_active_tint(rgb_tmp, rgb_spline, is_active);
glColor4ubv(rgb_tmp);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, points);
glDrawArrays(draw_method, 0, tot_point);
#ifdef USE_XOR
glDisable(GL_COLOR_LOGIC_OP);
#endif
mask_color_active_tint(rgb_tmp, rgb_black, is_active);
glColor4ubv(rgb_tmp);
GPU_basic_shader_line_stipple(3, 0x5555);
glDrawArrays(draw_method, 0, tot_point);
GPU_basic_shader_bind_disable(GPU_SHADER_LINE | GPU_SHADER_STIPPLE);
break;
case MASK_DT_BLACK:
case MASK_DT_WHITE:
if (draw_type == MASK_DT_BLACK) { rgb_tmp[0] = rgb_tmp[1] = rgb_tmp[2] = 0; }
else { rgb_tmp[0] = rgb_tmp[1] = rgb_tmp[2] = 255; }
/* alpha values seem too low but gl draws many points that compensate for it */
if (is_feather) { rgb_tmp[3] = 64; }
else { rgb_tmp[3] = 128; }
if (is_feather) {
rgb_tmp[0] = (unsigned char)(((short)rgb_tmp[0] + (short)rgb_spline[0]) / 2);
rgb_tmp[1] = (unsigned char)(((short)rgb_tmp[1] + (short)rgb_spline[1]) / 2);
rgb_tmp[2] = (unsigned char)(((short)rgb_tmp[2] + (short)rgb_spline[2]) / 2);
}
if (is_smooth == false && is_feather) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
mask_color_active_tint(rgb_tmp, rgb_tmp, is_active);
glColor4ubv(rgb_tmp);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, points);
glDrawArrays(draw_method, 0, tot_point);
if (is_smooth == false && is_feather) {
glDisable(GL_BLEND);
}
break;
}
glDisableClientState(GL_VERTEX_ARRAY);
if (points != orig_points)
MEM_freeN(points);
}
static void draw_spline_curve(const bContext *C, MaskLayer *masklay, MaskSpline *spline,
const char draw_flag, const char draw_type,
const bool is_active,
const int width, const int height)
{
const unsigned int resol = max_ii(BKE_mask_spline_feather_resolution(spline, width, height),
BKE_mask_spline_resolution(spline, width, height));
unsigned char rgb_tmp[4];
const bool is_spline_sel = (spline->flag & SELECT) && (masklay->restrictflag & MASK_RESTRICT_SELECT) == 0;
const bool is_smooth = (draw_flag & MASK_DRAWFLAG_SMOOTH) != 0;
const bool is_fill = (spline->flag & MASK_SPLINE_NOFILL) == 0;
unsigned int tot_diff_point;
float (*diff_points)[2];
unsigned int tot_feather_point;
float (*feather_points)[2];
diff_points = BKE_mask_spline_differentiate_with_resolution(spline, &tot_diff_point, resol);
if (!diff_points)
return;
if (is_smooth) {
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
feather_points = BKE_mask_spline_feather_differentiated_points_with_resolution(spline, &tot_feather_point, resol, (is_fill != false));
/* draw feather */
mask_spline_feather_color_get(masklay, spline, is_spline_sel, rgb_tmp);
mask_draw_curve_type(C, spline, feather_points, tot_feather_point,
true, is_smooth, is_active,
rgb_tmp, draw_type);
if (!is_fill) {
const float *fp = &diff_points[0][0];
float *fp_feather = &feather_points[0][0];
float tvec[2];
int i;
BLI_assert(tot_diff_point == tot_feather_point);
for (i = 0; i < tot_diff_point; i++, fp += 2, fp_feather += 2) {
sub_v2_v2v2(tvec, fp, fp_feather);
add_v2_v2v2(fp_feather, fp, tvec);
}
/* same as above */
mask_draw_curve_type(C, spline, feather_points, tot_feather_point,
true, is_smooth, is_active,
rgb_tmp, draw_type);
}
MEM_freeN(feather_points);
/* draw main curve */
mask_spline_color_get(masklay, spline, is_spline_sel, rgb_tmp);
mask_draw_curve_type(C, spline, diff_points, tot_diff_point,
false, is_smooth, is_active,
rgb_tmp, draw_type);
MEM_freeN(diff_points);
if (draw_flag & MASK_DRAWFLAG_SMOOTH) {
glDisable(GL_LINE_SMOOTH);
glDisable(GL_BLEND);
}
(void)draw_type;
}
static void draw_masklays(const bContext *C, Mask *mask, const char draw_flag, const char draw_type,
const int width, const int height, const float xscale, const float yscale)
{
MaskLayer *masklay;
int i;
for (masklay = mask->masklayers.first, i = 0; masklay; masklay = masklay->next, i++) {
MaskSpline *spline;
const bool is_active = (i == mask->masklay_act);
if (masklay->restrictflag & MASK_RESTRICT_VIEW) {
continue;
}
for (spline = masklay->splines.first; spline; spline = spline->next) {
/* draw curve itself first... */
draw_spline_curve(C, masklay, spline, draw_flag, draw_type, is_active, width, height);
// draw_spline_parents(masklay, spline);
if (!(masklay->restrictflag & MASK_RESTRICT_SELECT)) {
/* ...and then handles over the curve so they're nicely visible */
draw_spline_points(C, masklay, spline, draw_flag, draw_type, xscale, yscale);
}
/* show undeform for testing */
if (0) {
void *back = spline->points_deform;
spline->points_deform = NULL;
draw_spline_curve(C, masklay, spline, draw_flag, draw_type, is_active, width, height);
// draw_spline_parents(masklay, spline);
draw_spline_points(C, masklay, spline, draw_flag, draw_type, xscale, yscale);
spline->points_deform = back;
}
}
}
}
void ED_mask_draw(const bContext *C,
const char draw_flag, const char draw_type)
{
ScrArea *sa = CTX_wm_area(C);
ARegion *ar = CTX_wm_region(C);
Mask *mask = CTX_data_edit_mask(C);
int width, height;
float aspx, aspy;
float xscale, yscale;
if (!mask)
return;
ED_mask_get_size(sa, &width, &height);
ED_mask_get_aspect(sa, ar, &aspx, &aspy);
UI_view2d_scale_get(&ar->v2d, &xscale, &yscale);
draw_masklays(C, mask, draw_flag, draw_type, width, height, xscale * aspx, yscale * aspy);
}
static float *mask_rasterize(Mask *mask, const int width, const int height)
{
MaskRasterHandle *handle;
float *buffer = MEM_mallocN(sizeof(float) * height * width, "rasterized mask buffer");
/* Initialize rasterization handle. */
handle = BKE_maskrasterize_handle_new();
BKE_maskrasterize_handle_init(handle, mask, width, height, true, true, true);
BKE_maskrasterize_buffer(handle, width, height, buffer);
/* Free memory. */
BKE_maskrasterize_handle_free(handle);
return buffer;
}
/* sets up the opengl context.
* width, height are to match the values from ED_mask_get_size() */
void ED_mask_draw_region(
Mask *mask, ARegion *ar,
const char draw_flag, const char draw_type, const char overlay_mode,
/* convert directly into aspect corrected vars */
const int width_i, const int height_i,
const float aspx, const float aspy,
const bool do_scale_applied, const bool do_draw_cb,
/* optional - only used by clip */
float stabmat[4][4],
/* optional - only used when do_post_draw is set or called from clip editor */
const bContext *C)
{
struct View2D *v2d = &ar->v2d;
/* aspect always scales vertically in movie and image spaces */
const float width = width_i, height = (float)height_i * (aspy / aspx);
int x, y;
/* int w, h; */
float zoomx, zoomy;
/* frame image */
float maxdim;
float xofs, yofs;
/* find window pixel coordinates of origin */
UI_view2d_view_to_region(&ar->v2d, 0.0f, 0.0f, &x, &y);
/* w = BLI_rctf_size_x(&v2d->tot); */
/* h = BLI_rctf_size_y(&v2d->tot); */
zoomx = (float)(BLI_rcti_size_x(&ar->winrct) + 1) / BLI_rctf_size_x(&ar->v2d.cur);
zoomy = (float)(BLI_rcti_size_y(&ar->winrct) + 1) / BLI_rctf_size_y(&ar->v2d.cur);
if (do_scale_applied) {
zoomx /= width;
zoomy /= height;
}
x += v2d->tot.xmin * zoomx;
y += v2d->tot.ymin * zoomy;
/* frame the image */
maxdim = max_ff(width, height);
if (width == height) {
xofs = yofs = 0;
}
else if (width < height) {
xofs = ((height - width) / -2.0f) * zoomx;
yofs = 0.0f;
}
else { /* (width > height) */
xofs = 0.0f;
yofs = ((width - height) / -2.0f) * zoomy;
}
if (draw_flag & MASK_DRAWFLAG_OVERLAY) {
float *buffer = mask_rasterize(mask, width, height);
int format;
if (overlay_mode == MASK_OVERLAY_ALPHACHANNEL) {
glColor3f(1.0f, 1.0f, 1.0f);
format = GL_LUMINANCE;
}
else {
/* More blending types could be supported in the future. */
glEnable(GL_BLEND);
glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
format = GL_ALPHA;
}
glPushMatrix();
glTranslatef(x, y, 0);
glScalef(zoomx, zoomy, 0);
if (stabmat) {
glMultMatrixf(stabmat);
}
glaDrawPixelsTex(0.0f, 0.0f, width, height, format, GL_FLOAT, GL_NEAREST, buffer);
glPopMatrix();
if (overlay_mode != MASK_OVERLAY_ALPHACHANNEL) {
glDisable(GL_BLEND);
}
MEM_freeN(buffer);
}
/* apply transformation so mask editing tools will assume drawing from the origin in normalized space */
glPushMatrix();
glTranslatef(x + xofs, y + yofs, 0);
glScalef(zoomx, zoomy, 0);
if (stabmat) {
glMultMatrixf(stabmat);
}
glScalef(maxdim, maxdim, 0);
if (do_draw_cb) {
ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
}
/* draw! */
draw_masklays(C, mask, draw_flag, draw_type, width, height, maxdim * zoomx, maxdim * zoomy);
if (do_draw_cb) {
ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
}
glPopMatrix();
}
void ED_mask_draw_frames(Mask *mask, ARegion *ar, const int cfra, const int sfra, const int efra)
{
const float framelen = ar->winx / (float)(efra - sfra + 1);
MaskLayer *masklay = BKE_mask_layer_active(mask);
glBegin(GL_LINES);
glColor4ub(255, 175, 0, 255);
if (masklay) {
MaskLayerShape *masklay_shape;
for (masklay_shape = masklay->splines_shapes.first;
masklay_shape;
masklay_shape = masklay_shape->next)
{
int frame = masklay_shape->frame;
/* draw_keyframe(i, CFRA, sfra, framelen, 1); */
int height = (frame == cfra) ? 22 : 10;
int x = (frame - sfra) * framelen;
glVertex2i(x, 0);
glVertex2i(x, height);
}
}
glEnd();
}
|