Blender V5.3
graph_draw.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cfloat>
11#include <cmath>
12#include <cstdio>
13#include <cstring>
14
15#include "BLI_listbase.hh"
17#include "BLI_utildefines.hh"
18#include "BLI_vector.hh"
19
20#include "DNA_anim_types.h"
21#include "DNA_screen_types.h"
22#include "DNA_space_types.h"
23#include "DNA_userdef_types.h"
24
25#include "BKE_anim_data.hh"
26#include "BKE_curve.hh"
27#include "BKE_fcurve.hh"
28#include "BKE_nla.hh"
29
30#include "GPU_immediate.hh"
31#include "GPU_matrix.hh"
32#include "GPU_state.hh"
33
34#include "ED_anim_api.hh"
35
36#include "graph_intern.hh"
37
38#include "UI_interface.hh"
39#include "UI_resources.hh"
40#include "UI_view2d.hh"
41
42namespace blender {
43
44static void graph_draw_driver_debug(bAnimContext *ac, ID *id, FCurve *fcu);
45
46/* -------------------------------------------------------------------- */
49
50/* determine the alpha value that should be used when
51 * drawing components for some F-Curve (fcu)
52 * - selected F-Curves should be more visible than partially visible ones
53 */
54static float fcurve_display_alpha(const FCurve *fcu)
55{
56 return (fcu->flag & FCURVE_SELECTED) ? 1.0f : U.fcu_inactive_alpha;
57}
58
61 const float min,
62 const float max)
63{
64 bool replace;
65 int first, last;
66 first = BKE_fcurve_bezt_binarysearch_index(fcu->bezt, min, fcu->totvert, &replace);
67 first = clamp_i(first - 1, 0, fcu->totvert - 1);
68
69 last = BKE_fcurve_bezt_binarysearch_index(fcu->bezt, max, fcu->totvert, &replace);
70 last = replace ? last + 1 : last;
71 last = clamp_i(last, 0, fcu->totvert - 1);
72 /* Iterating over index range is exclusive of the last index.
73 * But we need `last` to be visited. */
74 return IndexRange(first, (last - first) + 1);
75}
76
78
79/* -------------------------------------------------------------------- */
82
83/* TODO: draw a shaded poly showing the region of influence too!!! */
89 View2D *v2d,
90 bAnimListElem *ale_nla_remap)
91{
92 FMod_Envelope *env = static_cast<FMod_Envelope *>(fcm->data);
94 const float fac = 0.05f * BLI_rctf_size_x(&v2d->cur);
95 int i;
96
97 const uint shdr_pos = GPU_vertformat_attr_add(
98 immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
99
100 GPU_line_width(1.0f);
101
103
104 float viewport_size[4];
105 GPU_viewport_size_get_f(viewport_size);
106 immUniform2f("viewport_size", viewport_size[2] / UI_SCALE_FAC, viewport_size[3] / UI_SCALE_FAC);
107
108 immUniform1i("colors_len", 0); /* Simple dashes. */
109 immUniformColor3f(0.0f, 0.0f, 0.0f);
110 immUniform1f("dash_width", 10.0f);
111 immUniform1f("udash_factor", 0.5f);
112
113 /* draw two black lines showing the standard reference levels */
114
116 immVertex2f(shdr_pos, v2d->cur.xmin, env->midval + env->min);
117 immVertex2f(shdr_pos, v2d->cur.xmax, env->midval + env->min);
118
119 immVertex2f(shdr_pos, v2d->cur.xmin, env->midval + env->max);
120 immVertex2f(shdr_pos, v2d->cur.xmax, env->midval + env->max);
121 immEnd();
122
124
125 if (env->totvert > 0) {
126 /* set size of vertices (non-adjustable for now) */
127 GPU_point_size(2.0f);
128
130
131 /* for now, point color is fixed, and is white */
132 immUniformColor3f(1.0f, 1.0f, 1.0f);
133
135
136 for (i = 0, fed = env->data; i < env->totvert; i++, fed++) {
137 const float env_scene_time = ANIM_nla_tweakedit_remap(
138 ale_nla_remap, fed->time, NLATIME_CONVERT_MAP);
139
140 /* only draw if visible
141 * - min/max here are fixed, not relative
142 */
143 if (IN_RANGE(env_scene_time, (v2d->cur.xmin - fac), (v2d->cur.xmax + fac))) {
144 immVertex2f(shdr_pos, env_scene_time, fed->min);
145 immVertex2f(shdr_pos, env_scene_time, fed->max);
146 }
147 }
148
149 immEnd();
150
152 }
153}
154
156
157/* -------------------------------------------------------------------- */
160
161/* helper func - set color to draw F-Curve data with */
162static void set_fcurve_vertex_color(FCurve *fcu, bool sel)
163{
164 float color[4];
165 float diff;
166
167 /* Set color of curve vertex based on state of curve (i.e. 'Edit' Mode) */
168 if ((fcu->flag & FCURVE_PROTECTED) == 0) {
169 /* Curve's points ARE BEING edited */
171 }
172 else {
173 /* Curve's points CANNOT BE edited */
175 }
176
177 /* Fade the 'intensity' of the vertices based on the selection of the curves too
178 * - Only fade by 50% the amount the curves were faded by, so that the points
179 * still stand out for easier selection
180 */
181 diff = 1.0f - fcurve_display_alpha(fcu);
182 color[3] = 1.0f - (diff * 0.5f);
183 CLAMP(color[3], 0.2f, 1.0f);
184
186}
187
188/* Draw a cross at the given position. Shader must already be bound.
189 * NOTE: the caller MUST HAVE GL_LINE_SMOOTH & GL_BLEND ENABLED, otherwise the controls don't
190 * have a consistent appearance (due to off-pixel alignments).
191 */
192static void draw_cross(float position[2], const float scale[2], uint attr_id)
193{
196 GPU_matrix_scale_2f(1.0f / scale[0], 1.0f / scale[1]);
197
198 /* Draw X shape. */
199 const float line_length = 0.7f;
201 immVertex2f(attr_id, -line_length, -line_length);
202 immVertex2f(attr_id, +line_length, +line_length);
203
204 immVertex2f(attr_id, -line_length, +line_length);
205 immVertex2f(attr_id, +line_length, -line_length);
206 immEnd();
207
209}
210
212 bool sel,
213 uint pos,
214 const IndexRange index_range)
215{
216 set_fcurve_vertex_color(fcu, sel);
217
219
220 for (const int i : index_range) {
221 BezTriple *bezt = &fcu->bezt[i];
222 /* 'Keyframe' vertex only, as handle lines and handles have already been drawn
223 * - only draw those with correct selection state for the current drawing color
224 * -
225 */
226 if ((bezt->f2 & SELECT) == sel) {
227 immVertex2fv(pos, bezt->vec[1]);
228 }
229 }
230
231 immEnd();
232}
233
237static void draw_fcurve_active_vertex(const FCurve *fcu, const View2D *v2d, const uint pos)
238{
239 const int active_keyframe_index = BKE_fcurve_active_keyframe_index(fcu);
240 if (!(fcu->flag & FCURVE_ACTIVE) || active_keyframe_index == FCURVE_ACTIVE_KEYFRAME_NONE) {
241 return;
242 }
243
244 const float fac = 0.05f * BLI_rctf_size_x(&v2d->cur);
245 const BezTriple *bezt = &fcu->bezt[active_keyframe_index];
246
247 if (!IN_RANGE(bezt->vec[1][0], (v2d->cur.xmin - fac), (v2d->cur.xmax + fac))) {
248 return;
249 }
250 if (!(bezt->f2 & SELECT)) {
251 return;
252 }
253
256 immVertex2fv(pos, bezt->vec[1]);
257 immEnd();
258}
259
260/* helper func - draw keyframe vertices only for an F-Curve */
261static void draw_fcurve_keyframe_vertices(FCurve *fcu, View2D *v2d, const uint pos)
262{
264
265 if ((fcu->flag & FCURVE_PROTECTED) == 0) {
267 }
268 else {
269 /* Draw keyframes on locked curves slightly smaller to give them less visual weight. */
271 }
272
273 const IndexRange index_range = get_bounding_bezt_index_range(fcu, v2d->cur.xmin, v2d->cur.xmax);
274 draw_fcurve_selected_keyframe_vertices(fcu, false, pos, index_range);
275 draw_fcurve_selected_keyframe_vertices(fcu, true, pos, index_range);
276 draw_fcurve_active_vertex(fcu, v2d, pos);
277
279}
280
281/* helper func - draw handle vertices only for an F-Curve (if it is not protected) */
283 FCurve *fcu, View2D *v2d, bool sel, bool sel_handle_only, uint pos)
284{
285 const IndexRange index_range = get_bounding_bezt_index_range(fcu, v2d->cur.xmin, v2d->cur.xmax);
286
287 /* set handle color */
288 float hcolor[3];
290 immUniform4f("outlineColor", hcolor[0], hcolor[1], hcolor[2], 1.0f);
291 immUniformColor3fvAlpha(hcolor, 0.01f); /* almost invisible - only keep for smoothness */
292
294
295 BezTriple *prevbezt = nullptr;
296 for (const int i : index_range) {
297 BezTriple *bezt = &fcu->bezt[i];
298 /* Draw the editmode handles for a bezier curve (others don't have handles)
299 * if their selection status matches the selection status we're drawing for
300 * - first handle only if previous beztriple was bezier-mode
301 * - second handle only if current beztriple is bezier-mode
302 *
303 * Also, need to take into account whether the keyframe was selected
304 * if a Graph Editor option to only show handles of selected keys is on.
305 */
306 if (!sel_handle_only || BEZT_ISSEL_ANY(bezt)) {
307 if ((!prevbezt && (bezt->ipo == BEZT_IPO_BEZ)) ||
308 (prevbezt && (prevbezt->ipo == BEZT_IPO_BEZ)))
309 {
310 if ((bezt->f1 & SELECT) == sel
311 /* && v2d->cur.xmin < bezt->vec[0][0] < v2d->cur.xmax */)
312 {
313 immVertex2fv(pos, bezt->vec[0]);
314 }
315 }
316
317 if (bezt->ipo == BEZT_IPO_BEZ) {
318 if ((bezt->f3 & SELECT) == sel
319 /* && v2d->cur.xmin < bezt->vec[2][0] < v2d->cur.xmax */)
320 {
321 immVertex2fv(pos, bezt->vec[2]);
322 }
323 }
324 }
325 prevbezt = bezt;
326 }
327
328 immEnd();
329}
330
335 const bool sel_handle_only,
336 const uint pos)
337{
338 const int active_keyframe_index = BKE_fcurve_active_keyframe_index(fcu);
339 if (!(fcu->flag & FCURVE_ACTIVE) || active_keyframe_index == FCURVE_ACTIVE_KEYFRAME_NONE) {
340 return;
341 }
342
343 const BezTriple *bezt = &fcu->bezt[active_keyframe_index];
344
345 if (sel_handle_only && !BEZT_ISSEL_ANY(bezt)) {
346 return;
347 }
348
349 float active_col[4];
351 immUniform4fv("outlineColor", active_col);
352 immUniformColor3fvAlpha(active_col, 0.01f); /* Almost invisible - only keep for smoothness. */
354
355 const BezTriple *left_bezt = active_keyframe_index > 0 ? &fcu->bezt[active_keyframe_index - 1] :
356 bezt;
357 if (left_bezt->ipo == BEZT_IPO_BEZ && (bezt->f1 & SELECT)) {
358 immVertex2fv(pos, bezt->vec[0]);
359 }
360 if (bezt->ipo == BEZT_IPO_BEZ && (bezt->f3 & SELECT)) {
361 immVertex2fv(pos, bezt->vec[2]);
362 }
363 immEnd();
364}
365
366/* helper func - draw handle vertices only for an F-Curve (if it is not protected) */
367static void draw_fcurve_handle_vertices(FCurve *fcu, View2D *v2d, bool sel_handle_only, uint pos)
368{
369 /* smooth outlines for more consistent appearance */
371
372 /* set handle size */
374 immUniform1f("outlineWidth", 1.5f * UI_SCALE_FAC);
375
376 draw_fcurve_selected_handle_vertices(fcu, v2d, false, sel_handle_only, pos);
377 draw_fcurve_selected_handle_vertices(fcu, v2d, true, sel_handle_only, pos);
378 draw_fcurve_active_handle_vertices(fcu, sel_handle_only, pos);
379
381}
382
383static void draw_fcurve_vertices(ARegion *region,
384 FCurve *fcu,
385 bool do_handles,
386 bool sel_handle_only)
387{
388 View2D *v2d = &region->v2d;
389
390 /* only draw points if curve is visible
391 * - Draw unselected points before selected points as separate passes
392 * to make sure in the case of overlapping points that the selected is always visible
393 * - Draw handles before keyframes, so that keyframes will overlap handles
394 * (keyframes are more important for users).
395 */
396
397 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
398
401
402 /* draw the two handles first (if they're shown, the curve doesn't
403 * have just a single keyframe, and the curve is being edited) */
404 if (do_handles) {
405 draw_fcurve_handle_vertices(fcu, v2d, sel_handle_only, pos);
406 }
407
408 /* draw keyframes over the handles */
409 draw_fcurve_keyframe_vertices(fcu, v2d, pos);
410
413}
414
416
417/* -------------------------------------------------------------------- */
420
421static bool draw_fcurve_handles_check(const SpaceGraph *sipo, const FCurve *fcu)
422{
423 /* don't draw handle lines if handles are not to be shown */
424 if (/* handles shouldn't be shown anywhere */
425 (sipo->flag & SIPO_NOHANDLES) ||
426 /* keyframes aren't editable */
427 (fcu->flag & FCURVE_PROTECTED) ||
428#if 0
429 /* handles can still be selected and handle types set, better draw - campbell */
430 /* editing the handles here will cause weird/incorrect interpolation issues */
431 (fcu->flag & FCURVE_INT_VALUES) ||
432#endif
433 /* group that curve belongs to is not editable */
434 ((fcu->grp) && (fcu->grp->flag & AGRP_PROTECTED)))
435 {
436 return false;
437 }
438 return true;
439}
440
441/* draw lines for F-Curve handles only (this is only done in EditMode)
442 * NOTE: draw_fcurve_handles_check must be checked before running this. */
443static void draw_fcurve_handles(SpaceGraph *sipo, ARegion *region, const FCurve *fcu)
444{
445 GPUVertFormat *format = immVertexFormat();
446 uint pos = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32);
447 uint color = GPU_vertformat_attr_add(format, "color", gpu::VertAttrType::SFLOAT_32_32_32_32);
449 if (U.animation_flag & USER_ANIM_HIGH_QUALITY_DRAWING) {
450 GPU_line_smooth(true);
451 }
453
455
456 const IndexRange index_range = get_bounding_bezt_index_range(
457 fcu, region->v2d.cur.xmin, region->v2d.cur.xmax);
458
459 /* slightly hacky, but we want to draw unselected points before selected ones
460 * so that selected points are clearly visible
461 */
462 for (int sel = 0; sel < 2; sel++) {
463 int basecol = (sel) ? TH_HANDLE_SEL_FREE : TH_HANDLE_FREE;
464 float col[4];
465
466 BezTriple *prevbezt = nullptr;
467 for (const int i : index_range) {
468 BezTriple *bezt = &fcu->bezt[i];
469 /* if only selected keyframes can get their handles shown,
470 * check that keyframe is selected
471 */
472 if (sipo->flag & SIPO_SELVHANDLESONLY) {
473 if (BEZT_ISSEL_ANY(bezt) == 0) {
474 prevbezt = bezt;
475 continue;
476 }
477 }
478
479 /* draw handle with appropriate set of colors if selection is ok */
480 if ((bezt->f2 & SELECT) == sel) {
481 /* only draw first handle if previous segment had handles */
482 if ((!prevbezt && (bezt->ipo == BEZT_IPO_BEZ)) ||
483 (prevbezt && (prevbezt->ipo == BEZT_IPO_BEZ)))
484 {
485 ui::theme::get_color_3fv(basecol + bezt->h1, col);
486 col[3] = fcurve_display_alpha(fcu);
487 immAttr4fv(color, col);
488 immVertex2fv(pos, bezt->vec[0]);
489 immAttr4fv(color, col);
490 immVertex2fv(pos, bezt->vec[1]);
491 }
492
493 /* only draw second handle if this segment is bezier */
494 if (bezt->ipo == BEZT_IPO_BEZ) {
495 ui::theme::get_color_3fv(basecol + bezt->h2, col);
496 col[3] = fcurve_display_alpha(fcu);
497 immAttr4fv(color, col);
498 immVertex2fv(pos, bezt->vec[1]);
499 immAttr4fv(color, col);
500 immVertex2fv(pos, bezt->vec[2]);
501 }
502 }
503 else {
504 /* only draw first handle if previous segment was had handles, and selection is ok */
505 if (((bezt->f1 & SELECT) == sel) && ((!prevbezt && (bezt->ipo == BEZT_IPO_BEZ)) ||
506 (prevbezt && (prevbezt->ipo == BEZT_IPO_BEZ))))
507 {
508 ui::theme::get_color_3fv(basecol + bezt->h1, col);
509 col[3] = fcurve_display_alpha(fcu);
510 immAttr4fv(color, col);
511 immVertex2fv(pos, bezt->vec[0]);
512 immAttr4fv(color, col);
513 immVertex2fv(pos, bezt->vec[1]);
514 }
515
516 /* only draw second handle if this segment is bezier, and selection is ok */
517 if (((bezt->f3 & SELECT) == sel) && (bezt->ipo == BEZT_IPO_BEZ)) {
518 ui::theme::get_color_3fv(basecol + bezt->h2, col);
519 col[3] = fcurve_display_alpha(fcu);
520 immAttr4fv(color, col);
521 immVertex2fv(pos, bezt->vec[1]);
522 immAttr4fv(color, col);
523 immVertex2fv(pos, bezt->vec[2]);
524 }
525 }
526 prevbezt = bezt;
527 }
528 }
529
530 immEnd();
533 if (U.animation_flag & USER_ANIM_HIGH_QUALITY_DRAWING) {
534 GPU_line_smooth(false);
535 }
536}
537
539
540/* -------------------------------------------------------------------- */
543
544/* helper func - draw keyframe vertices only for an F-Curve */
545static void draw_fcurve_samples(ARegion *region, const FCurve *fcu, const float unit_scale)
546{
547 FPoint *first, *last;
548 float scale[2];
549
550 /* get view settings */
551 const float hsize = ui::theme::get_value_f(TH_VERTEX_SIZE);
552 ui::view2d_scale_get(&region->v2d, &scale[0], &scale[1]);
553
554 scale[0] /= hsize;
555 scale[1] /= hsize / unit_scale;
556
557 /* get verts */
558 first = fcu->fpt;
559 last = (first) ? (first + (fcu->totvert - 1)) : (nullptr);
560
561 /* draw */
562 if (first && last) {
563 /* anti-aliased lines for more consistent appearance */
564 if (U.animation_flag & USER_ANIM_HIGH_QUALITY_DRAWING) {
565 GPU_line_smooth(true);
566 }
568
569 uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
571
573
574 draw_cross(first->vec, scale, pos);
575 draw_cross(last->vec, scale, pos);
576
578
580 if (U.animation_flag & USER_ANIM_HIGH_QUALITY_DRAWING) {
581 GPU_line_smooth(false);
582 }
583 }
584}
585
587
588/* -------------------------------------------------------------------- */
591
592/* Helper func - just draw the F-Curve by sampling the visible region
593 * (for drawing curves with modifiers). */
595 ID *id,
596 const FCurve *fcu_,
597 View2D *v2d,
598 uint pos,
599 const bool use_nla_remap,
600 const bool draw_extrapolation)
601{
602 short mapping_flag = ANIM_get_normalization_flags(ac->sl);
603
604 /* when opening a blend file on a different sized screen or while dragging the toolbar this can
605 * happen best just bail out in this case. */
606 if (ui::view2d_scale_get_x(v2d) <= 0.0f) {
607 return;
608 }
609
610 /* disable any drivers */
611 FCurve fcurve_for_draw = *fcu_;
612 fcurve_for_draw.driver = nullptr;
613
614 /* compute unit correction factor */
615 float offset;
616 float unitFac = ANIM_unit_mapping_get_factor(
617 ac->scene, id, &fcurve_for_draw, mapping_flag, &offset);
618
619 /* Note about sampling frequency:
620 * Ideally, this is chosen such that we have 1-2 pixels = 1 segment
621 * which means that our curves can be as smooth as possible. However,
622 * this does mean that curves may not be fully accurate (i.e. if they have
623 * sudden spikes which happen at the sampling point, we may have problems).
624 * Also, this may introduce lower performance on less densely detailed curves,
625 * though it is impossible to predict this from the modifiers!
626 *
627 * If the automatically determined sampling frequency is likely to cause an infinite
628 * loop (i.e. too close to 0), then clamp it to a determined "safe" value. The value
629 * chosen here is just the coarsest value which still looks reasonable.
630 */
631
632 /* TODO: perhaps we should have 1.0 frames
633 * as upper limit so that curves don't get too distorted? */
634 float pixels_per_sample = 1.5f;
635 float samplefreq = pixels_per_sample / ui::view2d_scale_get_x(v2d);
636
637 if (!(U.animation_flag & USER_ANIM_HIGH_QUALITY_DRAWING)) {
638 /* Low Precision = coarse lower-bound clamping
639 *
640 * Although the "Beauty Draw" flag was originally for AA'd
641 * line drawing, the sampling rate here has a much greater
642 * impact on performance (e.g. for #40372)!
643 *
644 * This one still amounts to 10 sample-frames for each 1-frame interval
645 * which should be quite a decent approximation in many situations.
646 */
647 samplefreq = std::max(samplefreq, 0.1f);
648 }
649 else {
650 /* "Higher Precision" but slower - especially on larger windows (e.g. #40372) */
651 samplefreq = std::max(samplefreq, 0.00001f);
652 }
653
654 /* the start/end times are simply the horizontal extents of the 'cur' rect */
655 float stime = v2d->cur.xmin;
656 float etime = v2d->cur.xmax;
657
658 AnimData *adt = use_nla_remap ? BKE_animdata_from_id(id) : nullptr;
659
660 /* If not drawing extrapolation, then change fcurve drawing bounds to its keyframe bounds clamped
661 * by graph editor bounds. */
662 if (!draw_extrapolation) {
663 float fcu_start = 0;
664 float fcu_end = 0;
665 BKE_fcurve_calc_range(fcu_, &fcu_start, &fcu_end, false);
666
667 fcu_start = BKE_nla_tweakedit_remap(adt, fcu_start, NLATIME_CONVERT_MAP);
668 fcu_end = BKE_nla_tweakedit_remap(adt, fcu_end, NLATIME_CONVERT_MAP);
669
670 /* Account for reversed NLA strip effect. */
671 if (fcu_end < fcu_start) {
672 std::swap(fcu_start, fcu_end);
673 }
674
675 /* Clamp to graph editor rendering bounds. */
676 stime = max_ff(stime, fcu_start);
677 etime = min_ff(etime, fcu_end);
678 }
679
680 const int total_samples = roundf((etime - stime) / samplefreq);
681 if (total_samples <= 0) {
682 return;
683 }
684
685 /* NLA remapping is linear so we don't have to remap per iteration. */
686 const float eval_start = BKE_nla_tweakedit_remap(adt, stime, NLATIME_CONVERT_UNMAP);
687 const float eval_freq = BKE_nla_tweakedit_remap(adt, stime + samplefreq, NLATIME_CONVERT_UNMAP) -
688 eval_start;
689 const float eval_end = BKE_nla_tweakedit_remap(adt, etime, NLATIME_CONVERT_UNMAP);
690
691 immBegin(GPU_PRIM_LINE_STRIP, (total_samples + 1));
692
693 /* At each sampling interval, add a new vertex.
694 *
695 * Apply the unit correction factor to the calculated values so that the displayed values appear
696 * correctly in the viewport.
697 */
698 for (int i = 0; i < total_samples; i++) {
699 const float ctime = stime + i * samplefreq;
700 float eval_time = eval_start + i * eval_freq;
701
702 /* Prevent drawing past bounds, due to floating point problems.
703 * User-wise, prevent visual flickering.
704 *
705 * This is to cover the case where:
706 * eval_start + total_samples * eval_freq > eval_end
707 * due to floating point problems.
708 */
709 eval_time = std::min(eval_time, eval_end);
710
711 immVertex2f(pos, ctime, (evaluate_fcurve(&fcurve_for_draw, eval_time) + offset) * unitFac);
712 }
713
714 /* Ensure we include end boundary point.
715 * User-wise, prevent visual flickering.
716 *
717 * This is to cover the case where:
718 * eval_start + total_samples * eval_freq < eval_end
719 * due to floating point problems.
720 */
721 immVertex2f(pos, etime, (evaluate_fcurve(&fcurve_for_draw, eval_end) + offset) * unitFac);
722
723 immEnd();
724}
725
726/* helper func - draw a samples-based F-Curve */
728 ID *id,
729 FCurve *fcu,
730 View2D *v2d,
731 const uint shdr_pos,
732 const bool draw_extrapolation)
733{
734 if (!draw_extrapolation && fcu->totvert == 1) {
735 return;
736 }
737
738 FPoint *prevfpt = fcu->fpt;
739 FPoint *fpt = prevfpt + 1;
740 float fac, v[2];
741 int b = fcu->totvert;
742 float unit_scale, offset;
743 short mapping_flag = ANIM_get_normalization_flags(ac->sl);
744 int count = fcu->totvert;
745
746 const bool extrap_left = draw_extrapolation && prevfpt->vec[0] > v2d->cur.xmin;
747 if (extrap_left) {
748 count++;
749 }
750
751 const bool extrap_right = draw_extrapolation && (prevfpt + b - 1)->vec[0] < v2d->cur.xmax;
752 if (extrap_right) {
753 count++;
754 }
755
756 /* apply unit mapping */
758 unit_scale = ANIM_unit_mapping_get_factor(ac->scene, id, fcu, mapping_flag, &offset);
759 GPU_matrix_scale_2f(1.0f, unit_scale);
760 GPU_matrix_translate_2f(0.0f, offset);
761
763
764 /* extrapolate to left? - left-side of view comes before first keyframe? */
765 if (extrap_left) {
766 v[0] = v2d->cur.xmin;
767
768 /* y-value depends on the interpolation */
769 if ((fcu->extend == FCURVE_EXTRAPOLATE_CONSTANT) || (fcu->flag & FCURVE_INT_VALUES) ||
770 (fcu->totvert == 1))
771 {
772 /* just extend across the first keyframe's value */
773 v[1] = prevfpt->vec[1];
774 }
775 else {
776 /* extrapolate linear doesn't use the handle, use the next points center instead */
777 fac = (prevfpt->vec[0] - fpt->vec[0]) / (prevfpt->vec[0] - v[0]);
778 if (fac) {
779 fac = 1.0f / fac;
780 }
781 v[1] = prevfpt->vec[1] - fac * (prevfpt->vec[1] - fpt->vec[1]);
782 }
783
784 immVertex2fv(shdr_pos, v);
785 }
786
787 /* loop over samples, drawing segments */
788 /* draw curve between first and last keyframe (if there are enough to do so) */
789 while (b--) {
790 /* Linear interpolation: just add one point (which should add a new line segment) */
791 immVertex2fv(shdr_pos, prevfpt->vec);
792
793 /* get next pointers */
794 if (b > 0) {
795 prevfpt++;
796 }
797 }
798
799 /* extrapolate to right? (see code for left-extrapolation above too) */
800 if (extrap_right) {
801 v[0] = v2d->cur.xmax;
802
803 /* y-value depends on the interpolation */
804 if ((fcu->extend == FCURVE_EXTRAPOLATE_CONSTANT) || (fcu->flag & FCURVE_INT_VALUES) ||
805 (fcu->totvert == 1))
806 {
807 /* based on last keyframe's value */
808 v[1] = prevfpt->vec[1];
809 }
810 else {
811 /* extrapolate linear doesn't use the handle, use the previous points center instead */
812 fpt = prevfpt - 1;
813 fac = (prevfpt->vec[0] - fpt->vec[0]) / (prevfpt->vec[0] - v[0]);
814 if (fac) {
815 fac = 1.0f / fac;
816 }
817 v[1] = prevfpt->vec[1] - fac * (prevfpt->vec[1] - fpt->vec[1]);
818 }
819
820 immVertex2fv(shdr_pos, v);
821 }
822
823 immEnd();
824
826}
827
829 BezTriple *prevbezt,
830 const float2 pixels_per_unit)
831{
832 const float points_per_pixel = 0.25f;
833 const int resolution_x = int(((bezt->vec[1][0] - prevbezt->vec[1][0]) * pixels_per_unit[0]) *
834 points_per_pixel);
835 /* Include the handles in the resolution calculation to cover the case where keys have the same
836 * y-value, but their handles are offset to create an arc. */
837 const float min_y = std::min(
838 {bezt->vec[1][1], bezt->vec[2][1], prevbezt->vec[1][1], prevbezt->vec[0][1]});
839 const float max_y = std::max(
840 {bezt->vec[1][1], bezt->vec[2][1], prevbezt->vec[1][1], prevbezt->vec[0][1]});
841 const int resolution_y = int(((max_y - min_y) * pixels_per_unit[1]) * points_per_pixel);
842
843 /* Using a simple sum instead of calculating the diagonal. This gives a slightly higher
844 * resolution but it does compensate for the fact that bezier curves can create long arcs between
845 * keys. */
846 return resolution_x + resolution_y;
847}
848
853static void add_bezt_vertices(BezTriple *bezt,
854 BezTriple *prevbezt,
855 int resolution,
856 Vector<float2> &curve_vertices)
857{
858 if (resolution < 2) {
859 curve_vertices.append({prevbezt->vec[1][0], prevbezt->vec[1][1]});
860 return;
861 }
862
863 /* If the resolution goes too high the line will not end exactly at the keyframe. Probably due to
864 * accumulating floating point issues in BKE_curve_forward_diff_bezier. */
865 resolution = min_ii(64, resolution);
866
867 float prev_key[2], prev_handle[2], bez_handle[2], bez_key[2];
868 /* Allocation needs +1 on resolution because BKE_curve_forward_diff_bezier uses it to iterate
869 * inclusively. */
870 float *bezier_diff_points = MEM_new_array_uninitialized<float>(((resolution + 1) * 2),
871 "Draw bezt data");
872
873 prev_key[0] = prevbezt->vec[1][0];
874 prev_key[1] = prevbezt->vec[1][1];
875 prev_handle[0] = prevbezt->vec[2][0];
876 prev_handle[1] = prevbezt->vec[2][1];
877
878 bez_handle[0] = bezt->vec[0][0];
879 bez_handle[1] = bezt->vec[0][1];
880 bez_key[0] = bezt->vec[1][0];
881 bez_key[1] = bezt->vec[1][1];
882
883 BKE_fcurve_correct_bezpart(prev_key, prev_handle, bez_handle, bez_key);
884
886 prev_handle[0],
887 bez_handle[0],
888 bez_key[0],
889 bezier_diff_points,
890 resolution,
891 sizeof(float[2]));
893 prev_handle[1],
894 bez_handle[1],
895 bez_key[1],
896 bezier_diff_points + 1,
897 resolution,
898 sizeof(float[2]));
899
900 for (float *fp = bezier_diff_points; resolution; resolution--, fp += 2) {
901 const float x = *fp;
902 const float y = *(fp + 1);
903 curve_vertices.append({x, y});
904 }
905 MEM_delete(bezier_diff_points);
906}
907
909 const float v2d_xmin,
910 Vector<float2> &curve_vertices)
911{
912 /* left-side of view comes before first keyframe, so need to extend as not cyclic */
913 float vertex_position[2];
914 vertex_position[0] = v2d_xmin;
915 BezTriple *bezt = &fcu->bezt[0];
916
917 /* y-value depends on the interpolation */
918 if ((fcu->extend == FCURVE_EXTRAPOLATE_CONSTANT) || (bezt->ipo == BEZT_IPO_CONST) ||
919 (bezt->ipo == BEZT_IPO_LIN && fcu->totvert == 1))
920 {
921 /* just extend across the first keyframe's value */
922 vertex_position[1] = bezt->vec[1][1];
923 }
924 else if (bezt->ipo == BEZT_IPO_LIN) {
925 BezTriple *next_bezt = bezt + 1;
926 /* extrapolate linear doesn't use the handle, use the next points center instead */
927 float fac = (bezt->vec[1][0] - next_bezt->vec[1][0]) / (bezt->vec[1][0] - vertex_position[0]);
928 if (fac) {
929 fac = 1.0f / fac;
930 }
931 vertex_position[1] = bezt->vec[1][1] - fac * (bezt->vec[1][1] - next_bezt->vec[1][1]);
932 }
933 else {
934 /* based on angle of handle 1 (relative to keyframe) */
935 float fac = (bezt->vec[0][0] - bezt->vec[1][0]) / (bezt->vec[1][0] - vertex_position[0]);
936 if (fac) {
937 fac = 1.0f / fac;
938 }
939 vertex_position[1] = bezt->vec[1][1] - fac * (bezt->vec[0][1] - bezt->vec[1][1]);
940 }
941
942 curve_vertices.append(vertex_position);
943}
944
946 const float v2d_xmax,
947 Vector<float2> &curve_vertices)
948{
949 float vertex_position[2];
950 vertex_position[0] = v2d_xmax;
951 BezTriple *bezt = &fcu->bezt[fcu->totvert - 1];
952
953 /* y-value depends on the interpolation. */
954 if ((fcu->extend == FCURVE_EXTRAPOLATE_CONSTANT) || (fcu->flag & FCURVE_INT_VALUES) ||
955 (bezt->ipo == BEZT_IPO_CONST) || (bezt->ipo == BEZT_IPO_LIN && fcu->totvert == 1))
956 {
957 /* based on last keyframe's value */
958 vertex_position[1] = bezt->vec[1][1];
959 }
960 else if (bezt->ipo == BEZT_IPO_LIN) {
961 /* Extrapolate linear doesn't use the handle, use the previous points center instead. */
962 BezTriple *prev_bezt = bezt - 1;
963 float fac = (bezt->vec[1][0] - prev_bezt->vec[1][0]) / (bezt->vec[1][0] - vertex_position[0]);
964 if (fac) {
965 fac = 1.0f / fac;
966 }
967 vertex_position[1] = bezt->vec[1][1] - fac * (bezt->vec[1][1] - prev_bezt->vec[1][1]);
968 }
969 else {
970 /* Based on angle of handle 1 (relative to keyframe). */
971 float fac = (bezt->vec[2][0] - bezt->vec[1][0]) / (bezt->vec[1][0] - vertex_position[0]);
972 if (fac) {
973 fac = 1.0f / fac;
974 }
975 vertex_position[1] = bezt->vec[1][1] - fac * (bezt->vec[2][1] - bezt->vec[1][1]);
976 }
977
978 curve_vertices.append(vertex_position);
979}
980
981static float2 calculate_pixels_per_unit(View2D *v2d, const float unit_scale)
982{
983 const int window_width = BLI_rcti_size_x(&v2d->mask);
984 const int window_height = BLI_rcti_size_y(&v2d->mask);
985
986 const float v2d_frame_range = BLI_rctf_size_x(&v2d->cur);
987 const float v2d_value_range = BLI_rctf_size_y(&v2d->cur);
988 const float2 pixels_per_unit = {window_width / v2d_frame_range,
989 (window_height / v2d_value_range) * unit_scale};
990 return pixels_per_unit;
991}
992
993static float calculate_pixel_distance(const rctf &bounds, const float2 pixels_per_unit)
994{
995 return BLI_rctf_size_x(&bounds) * pixels_per_unit[0] +
996 BLI_rctf_size_y(&bounds) * pixels_per_unit[1];
997}
998
999static void expand_key_bounds(const BezTriple *left_key, const BezTriple *right_key, rctf &bounds)
1000{
1001 bounds.xmax = right_key->vec[1][0];
1002 if (left_key->ipo == BEZT_IPO_BEZ) {
1003 /* Respect handles of bezier keys. */
1004 bounds.ymin = std::min(
1005 {bounds.ymin, right_key->vec[1][1], right_key->vec[0][1], left_key->vec[2][1]});
1006 bounds.ymax = std::max(
1007 {bounds.ymax, right_key->vec[1][1], right_key->vec[0][1], left_key->vec[2][1]});
1008 }
1009 else {
1010 bounds.ymax = max_ff(bounds.ymax, right_key->vec[1][1]);
1011 bounds.ymin = min_ff(bounds.ymin, right_key->vec[1][1]);
1012 }
1013}
1014
1015/* Helper function - draw one repeat of an F-Curve (using Bezier curve approximations). */
1017 bAnimContext *ac, ID *id, FCurve *fcu, View2D *v2d, uint pos, const bool draw_extrapolation)
1018{
1019 if (!draw_extrapolation && fcu->totvert == 1) {
1020 return;
1021 }
1022
1023 /* Apply unit mapping. */
1025 float offset;
1026 short mapping_flag = ANIM_get_normalization_flags(ac->sl);
1027 const float unit_scale = ANIM_unit_mapping_get_factor(ac->scene, id, fcu, mapping_flag, &offset);
1028 GPU_matrix_scale_2f(1.0f, unit_scale);
1029 GPU_matrix_translate_2f(0.0f, offset);
1030
1031 Vector<float2> curve_vertices;
1032
1033 /* Extrapolate to the left? */
1034 if (draw_extrapolation && fcu->bezt[0].vec[1][0] > v2d->cur.xmin) {
1035 add_extrapolation_point_left(fcu, v2d->cur.xmin, curve_vertices);
1036 }
1037
1038 const IndexRange index_range = get_bounding_bezt_index_range(fcu, v2d->cur.xmin, v2d->cur.xmax);
1039
1040 /* Always add the first point so the extrapolation line doesn't jump. */
1041 curve_vertices.append(
1042 {fcu->bezt[index_range.first()].vec[1][0], fcu->bezt[index_range.first()].vec[1][1]});
1043
1044 const float2 pixels_per_unit = calculate_pixels_per_unit(v2d, unit_scale);
1045 const int window_width = BLI_rcti_size_x(&v2d->mask);
1046 const float v2d_frame_range = BLI_rctf_size_x(&v2d->cur);
1047 const float pixel_width = v2d_frame_range / window_width;
1048 const float samples_per_pixel = 0.66f;
1049 const float evaluation_step = pixel_width / samples_per_pixel;
1050
1051 BezTriple *first_key = &fcu->bezt[index_range.first()];
1052 rctf key_bounds = {
1053 first_key->vec[1][0], first_key->vec[1][0], first_key->vec[1][1], first_key->vec[1][1]};
1054 /* Used when skipping keys. */
1055 bool has_skipped_keys = false;
1056 const float min_pixel_distance = 3.0f;
1057
1058 /* Draw curve between first and last keyframe (if there are enough to do so). */
1059 for (const int i : index_range.drop_front(1)) {
1060 BezTriple *prevbezt = &fcu->bezt[i - 1];
1061 BezTriple *bezt = &fcu->bezt[i];
1062 expand_key_bounds(prevbezt, bezt, key_bounds);
1063 float pixel_distance = calculate_pixel_distance(key_bounds, pixels_per_unit);
1064
1065 if (pixel_distance >= min_pixel_distance && has_skipped_keys) {
1066 /* When the pixel distance is greater than the threshold, and we've skipped at least one, add
1067 * a point. The point position is the average of all keys from INCLUDING prevbezt to
1068 * EXCLUDING bezt. prevbezt then gets reset to the key before bezt because the distance
1069 * between those is potentially below the threshold. */
1070 curve_vertices.append({BLI_rctf_cent_x(&key_bounds), BLI_rctf_cent_y(&key_bounds)});
1071 has_skipped_keys = false;
1072 key_bounds = {
1073 prevbezt->vec[1][0], prevbezt->vec[1][0], prevbezt->vec[1][1], prevbezt->vec[1][1]};
1074 expand_key_bounds(prevbezt, bezt, key_bounds);
1075 /* Calculate again based on the new prevbezt. */
1076 pixel_distance = calculate_pixel_distance(key_bounds, pixels_per_unit);
1077 }
1078
1079 if (pixel_distance < min_pixel_distance) {
1080 /* Skip any keys that are too close to each other in screen space. */
1081 has_skipped_keys = true;
1082 continue;
1083 }
1084
1085 switch (prevbezt->ipo) {
1086
1087 case BEZT_IPO_CONST:
1088 /* Constant-Interpolation: draw segment between previous keyframe and next,
1089 * but holding same value */
1090 curve_vertices.append({prevbezt->vec[1][0], prevbezt->vec[1][1]});
1091 curve_vertices.append({bezt->vec[1][0], prevbezt->vec[1][1]});
1092 break;
1093
1094 case BEZT_IPO_LIN:
1095 /* Linear interpolation: just add one point (which should add a new line segment) */
1096 curve_vertices.append({prevbezt->vec[1][0], prevbezt->vec[1][1]});
1097 break;
1098
1099 case BEZT_IPO_BEZ: {
1100 const int resolution = calculate_bezt_draw_resolution(bezt, prevbezt, pixels_per_unit);
1101 add_bezt_vertices(bezt, prevbezt, resolution, curve_vertices);
1102 break;
1103 }
1104
1105 default: {
1106 /* In case there is no other way to get curve points, evaluate the FCurve. */
1107 curve_vertices.append(prevbezt->vec[1]);
1108 float current_frame = prevbezt->vec[1][0] + evaluation_step;
1109 while (current_frame < bezt->vec[1][0]) {
1110 curve_vertices.append({current_frame, evaluate_fcurve(fcu, current_frame)});
1111 current_frame += evaluation_step;
1112 }
1113 break;
1114 }
1115 }
1116
1117 prevbezt = bezt;
1118 }
1119
1120 /* Always add the last point so the extrapolation line doesn't jump. */
1121 curve_vertices.append(
1122 {fcu->bezt[index_range.last()].vec[1][0], fcu->bezt[index_range.last()].vec[1][1]});
1123
1124 /* Extrapolate to the right? (see code for left-extrapolation above too) */
1125 if (draw_extrapolation && fcu->bezt[fcu->totvert - 1].vec[1][0] < v2d->cur.xmax) {
1126 add_extrapolation_point_right(fcu, v2d->cur.xmax, curve_vertices);
1127 }
1128
1129 if (curve_vertices.size() < 2) {
1131 return;
1132 }
1133
1134 immBegin(GPU_PRIM_LINE_STRIP, curve_vertices.size());
1135 for (const float2 vertex : curve_vertices) {
1136 immVertex2fv(pos, vertex);
1137 }
1138 immEnd();
1139
1141}
1142
1143static void draw_fcurve(bAnimContext *ac, SpaceGraph *sipo, ARegion *region, bAnimListElem *ale)
1144{
1145 FCurve *fcu = static_cast<FCurve *>(ale->key_data);
1147
1148 /* map keyframes for drawing if scaled F-Curve */
1149 ANIM_nla_mapping_apply_if_needed_fcurve(ale, static_cast<FCurve *>(ale->key_data), false, false);
1150
1151 /* draw curve:
1152 * - curve line may be result of one or more destructive modifiers or just the raw data,
1153 * so we need to check which method should be used
1154 * - controls from active modifier take precedence over keyframes
1155 * (XXX! editing tools need to take this into account!)
1156 */
1157
1158 /* 1) draw curve line */
1159 if (((fcu->modifiers.first_) || (fcu->flag & FCURVE_INT_VALUES)) ||
1160 (((fcu->bezt) || (fcu->fpt)) && (fcu->totvert)))
1161 {
1162 /* set color/drawing style for curve itself */
1163 /* draw active F-Curve thicker than the rest to make it stand out */
1164 if (fcu->flag & FCURVE_ACTIVE && !BKE_fcurve_is_protected(*fcu)) {
1165 GPU_line_width(2.5);
1166 }
1167 else {
1168 GPU_line_width(1.0);
1169 }
1170
1171 /* anti-aliased lines for less jagged appearance */
1172 if (U.animation_flag & USER_ANIM_HIGH_QUALITY_DRAWING) {
1173 GPU_line_smooth(true);
1174 }
1176
1177 const uint shdr_pos = GPU_vertformat_attr_add(
1178 immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
1179
1180 float viewport_size[4];
1181 GPU_viewport_size_get_f(viewport_size);
1182
1183 if (BKE_fcurve_is_protected(*fcu)) {
1184 /* Protected curves (non editable) are drawn with dotted lines. */
1187 "viewport_size", viewport_size[2] / UI_SCALE_FAC, viewport_size[3] / UI_SCALE_FAC);
1188 immUniform1i("colors_len", 0); /* Simple dashes. */
1189 immUniform1f("dash_width", 16.0f * U.scale_factor);
1190 immUniform1f("udash_factor", 0.35f * U.scale_factor);
1191 }
1192 else {
1194 immUniform2fv("viewportSize", &viewport_size[2]);
1195 immUniform1f("lineWidth", GPU_line_width_get());
1196 }
1197
1198 if (((fcu->grp) && (fcu->grp->flag & AGRP_MUTED)) || (fcu->flag & FCURVE_MUTED)) {
1199 /* muted curves are drawn in a grayish hue */
1200 /* XXX should we have some variations? */
1202 }
1203 else {
1204 /* set whatever color the curve has set
1205 * - unselected curves draw less opaque to help distinguish the selected ones
1206 */
1208 }
1209
1210 const bool draw_extrapolation = (sipo->flag & SIPO_NO_DRAW_EXTRAPOLATION) == 0;
1211 /* draw F-Curve */
1212 if ((fcu->modifiers.first_) || (fcu->flag & FCURVE_INT_VALUES)) {
1213 /* draw a curve affected by modifiers or only allowed to have integer values
1214 * by sampling it at various small-intervals over the visible region
1215 */
1216 /* We have to do this mapping dance since the keyframes were remapped but the F-modifier
1217 * evaluations are not.
1218 *
1219 * So we undo the keyframe remapping and instead remap the evaluation time when drawing
1220 * the curve itself. Afterward, we go back and redo the keyframe remapping so the controls
1221 * are drawn properly. */
1223 ale, static_cast<FCurve *>(ale->key_data), true, false);
1225 ale->id,
1226 fcu,
1227 &region->v2d,
1228 shdr_pos,
1230 draw_extrapolation);
1232 ale, static_cast<FCurve *>(ale->key_data), false, false);
1233 }
1234 else if (((fcu->bezt) || (fcu->fpt)) && (fcu->totvert)) {
1235 /* just draw curve based on defined data (i.e. no modifiers) */
1236 if (fcu->bezt) {
1237 draw_fcurve_curve_keys(ac, ale->id, fcu, &region->v2d, shdr_pos, draw_extrapolation);
1238 }
1239 else if (fcu->fpt) {
1240 draw_fcurve_curve_samples(ac, ale->id, fcu, &region->v2d, shdr_pos, draw_extrapolation);
1241 }
1242 }
1243
1245
1246 if (U.animation_flag & USER_ANIM_HIGH_QUALITY_DRAWING) {
1247 GPU_line_smooth(false);
1248 }
1250 }
1251
1252 /* 2) draw handles and vertices as appropriate based on active
1253 * - If the option to only show controls if the F-Curve is selected is enabled,
1254 * we must obey this.
1255 */
1256 if (!(U.animation_flag & USER_ANIM_ONLY_SHOW_SELECTED_CURVE_KEYS) ||
1257 (fcu->flag & FCURVE_SELECTED))
1258 {
1259 if (!BKE_fcurve_are_keyframes_usable(*fcu) && !(fcu->fpt && fcu->totvert)) {
1260 /* only draw controls if this is the active modifier */
1261 if ((fcu->flag & FCURVE_ACTIVE) && (fcm)) {
1262 switch (fcm->type) {
1263 case FMODIFIER_TYPE_ENVELOPE: /* envelope */
1264 draw_fcurve_modifier_controls_envelope(fcm, &region->v2d, ale);
1265 break;
1266 default:
1267 break;
1268 }
1269 }
1270 }
1271 else if (((fcu->bezt) || (fcu->fpt)) && (fcu->totvert)) {
1272 short mapping_flag = ANIM_get_normalization_flags(ac->sl);
1273 float offset;
1274 const float unit_scale = ANIM_unit_mapping_get_factor(
1275 ac->scene, ale->id, fcu, mapping_flag, &offset);
1276
1277 /* apply unit-scaling to all values via OpenGL */
1279 GPU_matrix_scale_2f(1.0f, unit_scale);
1280 GPU_matrix_translate_2f(0.0f, offset);
1281
1282 /* Set this once and for all -
1283 * all handles and handle-verts should use the same thickness. */
1284 GPU_line_width(1.0);
1285
1286 if (fcu->bezt) {
1287 bool do_handles = draw_fcurve_handles_check(sipo, fcu);
1288
1289 if (do_handles) {
1290 /* only draw handles/vertices on keyframes */
1291 draw_fcurve_handles(sipo, region, fcu);
1292 }
1293
1294 draw_fcurve_vertices(region, fcu, do_handles, (sipo->flag & SIPO_SELVHANDLESONLY));
1295 }
1296 else {
1297 /* samples: only draw two indicators at either end as indicators */
1298 draw_fcurve_samples(region, fcu, unit_scale);
1299 }
1300
1302 }
1303 }
1304
1305 /* 3) draw driver debugging stuff */
1306 if ((ac->datatype == ANIMCONT_DRIVERS) && (fcu->flag & FCURVE_ACTIVE)) {
1307 graph_draw_driver_debug(ac, ale->id, fcu);
1308 }
1309
1310 /* undo mapping of keyframes for drawing if scaled F-Curve */
1311 ANIM_nla_mapping_apply_if_needed_fcurve(ale, static_cast<FCurve *>(ale->key_data), true, false);
1312}
1313
1315
1316/* -------------------------------------------------------------------- */
1319
1320/* Draw indicators which show the value calculated from the driver,
1321 * and how this is mapped to the value that comes out of it. This
1322 * is handy for helping users better understand how to interpret
1323 * the graphs, and also facilitates debugging.
1324 */
1326{
1327 ChannelDriver *driver = fcu->driver;
1328 View2D *v2d = &ac->region->v2d;
1329 short mapping_flag = ANIM_get_normalization_flags(ac->sl);
1330 float offset;
1331 float unitfac = ANIM_unit_mapping_get_factor(ac->scene, id, fcu, mapping_flag, &offset);
1332
1333 const uint shdr_pos = GPU_vertformat_attr_add(
1334 immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
1336
1337 float viewport_size[4];
1338 GPU_viewport_size_get_f(viewport_size);
1339 immUniform2f("viewport_size", viewport_size[2] / UI_SCALE_FAC, viewport_size[3] / UI_SCALE_FAC);
1340
1341 immUniform1i("colors_len", 0); /* Simple dashes. */
1342
1343 /* No curve to modify/visualize the result?
1344 * => We still want to show the 1-1 default...
1345 */
1346 if ((fcu->totvert == 0) && fcu->modifiers.is_empty()) {
1347 float t;
1348
1349 /* draw with thin dotted lines in style of what curve would have been */
1351
1352 immUniform1f("dash_width", 40.0f);
1353 immUniform1f("udash_factor", 0.5f);
1354 GPU_line_width(2.0f);
1355
1356 /* draw 1-1 line, stretching just past the screen limits
1357 * NOTE: we need to scale the y-values to be valid for the units
1358 */
1360
1361 t = v2d->cur.xmin;
1362 immVertex2f(shdr_pos, t, (t + offset) * unitfac);
1363
1364 t = v2d->cur.xmax;
1365 immVertex2f(shdr_pos, t, (t + offset) * unitfac);
1366
1367 immEnd();
1368 }
1369
1370 /* draw driver only if actually functional */
1371 if ((driver->flag & DRIVER_FLAG_INVALID) == 0) {
1372 /* grab "coordinates" for driver outputs */
1373 float x = driver->curval;
1374 float y = fcu->runtime->curval * unitfac;
1375
1376 /* Only draw indicators if the point is in range. */
1377 if (x >= v2d->cur.xmin) {
1378 float co[2];
1379
1380 /* draw dotted lines leading towards this point from both axes ....... */
1381 immUniformColor3f(0.9f, 0.9f, 0.9f);
1382 immUniform1f("dash_width", 10.0f);
1383 immUniform1f("udash_factor", 0.5f);
1384 GPU_line_width(1.0f);
1385
1386 immBegin(GPU_PRIM_LINES, (y <= v2d->cur.ymax) ? 4 : 2);
1387
1388 /* x-axis lookup */
1389 co[0] = x;
1390
1391 if (y <= v2d->cur.ymax) {
1392 co[1] = v2d->cur.ymax + 1.0f;
1393 immVertex2fv(shdr_pos, co);
1394
1395 co[1] = y;
1396 immVertex2fv(shdr_pos, co);
1397 }
1398
1399 /* y-axis lookup */
1400 co[1] = y;
1401
1402 co[0] = v2d->cur.xmin - 1.0f;
1403 immVertex2fv(shdr_pos, co);
1404
1405 co[0] = x;
1406 immVertex2fv(shdr_pos, co);
1407
1408 immEnd();
1409
1411
1412 /* GPU_PRIM_POINTS do not survive dashed line geometry shader... */
1414
1415 /* x marks the spot .................................................... */
1416 /* -> outer frame */
1417 immUniformColor3f(0.9f, 0.9f, 0.9f);
1418 GPU_point_size(7.0);
1419
1421 immVertex2f(shdr_pos, x, y);
1422 immEnd();
1423
1424 /* inner frame */
1425 immUniformColor3f(0.9f, 0.0f, 0.0f);
1426 GPU_point_size(3.0);
1427
1429 immVertex2f(shdr_pos, x, y);
1430 immEnd();
1431 }
1432 }
1433
1435}
1436
1438
1439/* -------------------------------------------------------------------- */
1442
1444{
1445 /* draw with thick dotted lines */
1446 GPU_line_width(3.0f);
1447
1448 /* anti-aliased lines for less jagged appearance */
1449 if (U.animation_flag & USER_ANIM_HIGH_QUALITY_DRAWING) {
1450 GPU_line_smooth(true);
1451 }
1453
1454 const uint shdr_pos = GPU_vertformat_attr_add(
1455 immVertexFormat(), "pos", gpu::VertAttrType::SFLOAT_32_32);
1456
1458
1459 float viewport_size[4];
1460 GPU_viewport_size_get_f(viewport_size);
1461 immUniform2f("viewport_size", viewport_size[2] / UI_SCALE_FAC, viewport_size[3] / UI_SCALE_FAC);
1462
1463 immUniform1i("colors_len", 0); /* Simple dashes. */
1464 immUniform1f("dash_width", 20.0f);
1465 immUniform1f("udash_factor", 0.5f);
1466
1467 /* Don't draw extrapolation on sampled ghost curves because it doesn't
1468 * match the curves they're ghosting anyway.
1469 * See issue #109920 for details. */
1470 const bool draw_extrapolation = false;
1471 /* the ghost curves are simply sampled F-Curves stored in sipo->runtime.ghost_curves */
1472 for (FCurve &fcu : sipo->runtime.ghost_curves) {
1473 /* set whatever color the curve has set
1474 * - this is set by the function which creates these
1475 * - draw with a fixed opacity of 2
1476 */
1477 immUniformColor3fvAlpha(fcu.color, 0.5f);
1478
1479 /* simply draw the stored samples */
1480 draw_fcurve_curve_samples(ac, nullptr, &fcu, &region->v2d, shdr_pos, draw_extrapolation);
1481 }
1482
1484
1485 if (U.animation_flag & USER_ANIM_HIGH_QUALITY_DRAWING) {
1486 GPU_line_smooth(false);
1487 }
1489}
1490
1491void graph_draw_curves(bAnimContext *ac, SpaceGraph *sipo, ARegion *region, short sel)
1492{
1493 ListBaseT<bAnimListElem> anim_data = {nullptr, nullptr};
1494 int filter;
1495
1496 /* build list of curves to draw */
1498 filter |= ((sel) ? (ANIMFILTER_SEL) : (ANIMFILTER_UNSEL));
1500 ac, &anim_data, eAnimFilter_Flags(filter), ac->data, eAnimCont_Types(ac->datatype));
1501
1502 /* for each curve:
1503 * draw curve, then handle-lines, and finally vertices in this order so that
1504 * the data will be layered correctly
1505 */
1506 bAnimListElem *ale_active_fcurve = nullptr;
1507 for (bAnimListElem &ale : anim_data) {
1508 const FCurve *fcu = static_cast<FCurve *>(ale.key_data);
1509 if ((fcu->flag & FCURVE_ACTIVE) && !ale_active_fcurve) {
1510 ale_active_fcurve = &ale;
1511 continue;
1512 }
1513 draw_fcurve(ac, sipo, region, &ale);
1514 }
1515
1516 /* Draw the active FCurve last so that it (especially the active keyframe)
1517 * shows on top of the other curves. */
1518 if (ale_active_fcurve != nullptr) {
1519 draw_fcurve(ac, sipo, region, ale_active_fcurve);
1520 }
1521
1522 /* free list of curves */
1523 ANIM_animdata_freelist(&anim_data);
1524}
1525
1527
1528/* -------------------------------------------------------------------- */
1531
1533 bAnimContext *ac,
1534 ARegion *region,
1535 const ListBaseT<bAnimListElem> &anim_data)
1536{
1537 bAnimListElem *ale;
1538
1539 View2D *v2d = &region->v2d;
1540
1541 const float channel_step = ANIM_UI_get_channel_step();
1542
1543 /* Loop through channels, and set up drawing depending on their type. */
1544 { /* first pass: just the standard GL-drawing for backdrop + text */
1545 size_t channel_index = 0;
1546 float ymax = ANIM_UI_get_first_channel_top(v2d);
1547
1548 for (ale = anim_data.first(); ale; ale = ale->next, ymax -= channel_step, channel_index++) {
1549 const float ymin = ymax - ANIM_UI_get_channel_height();
1550
1551 /* check if visible */
1552 if (IN_RANGE(ymin, v2d->cur.ymin, v2d->cur.ymax) ||
1553 IN_RANGE(ymax, v2d->cur.ymin, v2d->cur.ymax))
1554 {
1555 /* draw all channels using standard channel-drawing API */
1556 ANIM_channel_draw(ac, ale, ymin, ymax, channel_index);
1557 }
1558 }
1559 }
1560 { /* second pass: widgets */
1561 ui::Block *block = block_begin(C, region, __func__, ui::EmbossType::Emboss);
1562 size_t channel_index = 0;
1563 float ymax = ANIM_UI_get_first_channel_top(v2d);
1564
1565 /* set blending again, as may not be set in previous step */
1567
1568 for (ale = anim_data.first(); ale; ale = ale->next, ymax -= channel_step, channel_index++) {
1569 const float ymin = ymax - ANIM_UI_get_channel_height();
1570
1571 /* check if visible */
1572 if (IN_RANGE(ymin, v2d->cur.ymin, v2d->cur.ymax) ||
1573 IN_RANGE(ymax, v2d->cur.ymin, v2d->cur.ymax))
1574 {
1575 /* draw all channels using standard channel-drawing API */
1576 rctf channel_rect;
1577 BLI_rctf_init(&channel_rect, 0, v2d->cur.xmax - V2D_SCROLL_WIDTH, ymin, ymax);
1578 ANIM_channel_draw_widgets(C, ac, ale, block, &channel_rect, channel_index);
1579 }
1580 }
1581
1582 block_end(C, block);
1583 block_draw(C, block);
1584
1586 }
1587}
1588
1590
1591} // namespace blender
#define SELECT
Definition BKE_mask.hh:19
#define FCURVE_ACTIVE_KEYFRAME_NONE
#define BEZT_ISSEL_ANY(bezt)
#define UI_SCALE_FAC
#define C
Definition RandGen.cpp:29
#define V2D_SCROLL_WIDTH
Definition UI_view2d.hh:79
#define CLAMP(a, b, c)
#define IN_RANGE(a, b, c)
btScalar max
constexpr int64_t first() const
constexpr int64_t last(const int64_t n=0) const
constexpr IndexRange()=default
constexpr IndexRange drop_front(int64_t n) const
int64_t size() const
void append(const T &value)
#define roundf(x)
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt)
Definition frames.inl:1166
#define position
#define vertex
float3x3 scale(float3x3 mat, float2 scale)
void get_color_3fv(int colorid, float col[3])
void get_color_4fv(int colorid, float col[4])
float get_value_f(int colorid)
void get_color_shade_4fv(int colorid, int offset, float col[4])
float view2d_scale_get_x(const View2D *v2d)
Definition view2d.cc:1902
void view2d_scale_get(const View2D *v2d, float *r_x, float *r_y)
Definition view2d.cc:1893
BMesh Iterators.
MINLINE float min_ff(float a, float b)
UserDef U
Definition blender.cc:58
void ANIM_channel_draw(bAnimContext *ac, bAnimListElem *ale, float yminc, float ymaxc, size_t channel_index)
static void draw_fcurve(bAnimContext *ac, SpaceGraph *sipo, ARegion *region, bAnimListElem *ale)
bool BKE_fcurve_calc_range(const FCurve *fcu, float *r_min, float *r_max, bool selected_keys_only)
@ DRIVER_FLAG_INVALID
float BKE_nla_tweakedit_remap(AnimData *adt, float cframe, eNlaTime_ConvertModes mode)
void immEnd()
void immUniformColor3f(float r, float g, float b)
unsigned int uint
Definition BKE_text.h:55
@ ANIMCONT_DRIVERS
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static void graph_draw_driver_debug(bAnimContext *ac, ID *id, FCurve *fcu)
void graph_draw_ghost_curves(bAnimContext *ac, SpaceGraph *sipo, ARegion *region)
void immUniform2f(const char *name, float x, float y)
void immBeginAtMost(GPUPrimType, uint max_vertex_len)
static void draw_fcurve_vertices(ARegion *region, FCurve *fcu, bool do_handles, bool sel_handle_only)
static bool draw_fcurve_handles_check(const SpaceGraph *sipo, const FCurve *fcu)
GPUVertFormat * immVertexFormat()
static void add_extrapolation_point_right(const FCurve *fcu, const float v2d_xmax, Vector< float2 > &curve_vertices)
void GPU_point_size(float size)
Definition gpu_state.cc:157
@ NLATIME_CONVERT_UNMAP
Definition BKE_nla.hh:560
@ NLATIME_CONVERT_MAP
Definition BKE_nla.hh:563
static void draw_fcurve_active_handle_vertices(const FCurve *fcu, const bool sel_handle_only, const uint pos)
void immBegin(GPUPrimType, uint vertex_len)
float ANIM_nla_tweakedit_remap(bAnimListElem *ale, const float cframe, const eNlaTime_ConvertModes mode)
Definition anim_draw.cc:329
static void draw_fcurve_handle_vertices(FCurve *fcu, View2D *v2d, bool sel_handle_only, uint pos)
void GPU_line_smooth(bool enable)
Definition gpu_state.cc:80
@ GPU_PRIM_LINE_STRIP
static void expand_key_bounds(const BezTriple *left_key, const BezTriple *right_key, rctf &bounds)
@ TH_HANDLE_SEL_FREE
@ TH_HANDLE_VERTEX_SELECT
@ TH_VERTEX_SELECT
@ TH_HANDLE_VERTEX_SIZE
@ TH_VERTEX_ACTIVE
ATTR_WARN_UNUSED_RESULT const BMVert * v
void immUniformColor4fv(const float rgba[4])
@ USER_ANIM_HIGH_QUALITY_DRAWING
@ USER_ANIM_ONLY_SHOW_SELECTED_CURVE_KEYS
bool ANIM_nla_mapping_allowed(const bAnimListElem *ale)
Definition anim_draw.cc:279
void immUnbindProgram()
static void draw_fcurve_modifier_controls_envelope(FModifier *fcm, View2D *v2d, bAnimListElem *ale_nla_remap)
Definition graph_draw.cc:88
void ANIM_nla_mapping_apply_if_needed_fcurve(bAnimListElem *ale, FCurve *fcu, const bool restore, const bool only_keys)
Definition anim_draw.cc:406
static void draw_fcurve_selected_keyframe_vertices(FCurve *fcu, bool sel, uint pos, const IndexRange index_range)
void GPU_line_width(float width)
Definition gpu_state.cc:151
void GPU_matrix_scale_2f(float x, float y)
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
float ANIM_UI_get_channel_height()
static float fcurve_display_alpha(const FCurve *fcu)
Definition graph_draw.cc:54
float GPU_line_width_get()
Definition gpu_state.cc:241
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
@ FMODIFIER_TYPE_ENVELOPE
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:93
static IndexRange get_bounding_bezt_index_range(const FCurve *fcu, const float min, const float max)
Definition graph_draw.cc:60
float ANIM_UI_get_first_channel_top(View2D *v2d)
static float calculate_pixel_distance(const rctf &bounds, const float2 pixels_per_unit)
bool BKE_fcurve_is_protected(const FCurve &fcu)
void immUniformThemeColor(int color_id)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
static int calculate_bezt_draw_resolution(BezTriple *bezt, BezTriple *prevbezt, const float2 pixels_per_unit)
static void add_extrapolation_point_left(const FCurve *fcu, const float v2d_xmin, Vector< float2 > &curve_vertices)
VecBase< float, 2 > float2
void immUniform1f(const char *name, float x)
size_t ANIM_animdata_filter(bAnimContext *ac, ListBaseT< bAnimListElem > *anim_data, const eAnimFilter_Flags filter_mode, void *data, const eAnimCont_Types datatype)
void immVertex2f(uint attr_id, float x, float y)
@ ANIMFILTER_FCURVESONLY
@ ANIMFILTER_DATA_VISIBLE
@ ANIMFILTER_CURVE_VISIBLE
@ ANIMFILTER_UNSEL
void GPU_viewport_size_get_f(float coords[4])
Definition gpu_state.cc:258
void immAttr4fv(uint attr_id, const float data[4])
static void draw_fcurve_samples(ARegion *region, const FCurve *fcu, const float unit_scale)
static void draw_fcurve_curve_samples(bAnimContext *ac, ID *id, FCurve *fcu, View2D *v2d, const uint shdr_pos, const bool draw_extrapolation)
@ GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA
@ GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR
@ GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA
@ GPU_SHADER_3D_POINT_UNIFORM_COLOR
@ GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR
void immUniform4fv(const char *name, const float data[4])
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
static void draw_cross(float position[2], const float scale[2], uint attr_id)
void immUniformThemeColorShade(int color_id, int offset)
static void set_fcurve_vertex_color(FCurve *fcu, bool sel)
FModifier * find_active_fmodifier(ListBaseT< FModifier > *modifiers)
void GPU_program_point_size(bool enable)
Definition gpu_state.cc:165
void ANIM_channel_draw_widgets(const bContext *C, bAnimContext *ac, bAnimListElem *ale, ui::Block *block, const rctf *rect, size_t channel_index)
@ SIPO_NO_DRAW_EXTRAPOLATION
void GPU_matrix_translate_2f(float x, float y)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
void graph_draw_channel_names(bContext *C, bAnimContext *ac, ARegion *region, const ListBaseT< bAnimListElem > &anim_data)
static void draw_fcurve_curve(bAnimContext *ac, ID *id, const FCurve *fcu_, View2D *v2d, uint pos, const bool use_nla_remap, const bool draw_extrapolation)
void graph_draw_curves(bAnimContext *ac, SpaceGraph *sipo, ARegion *region, short sel)
void BKE_fcurve_correct_bezpart(const float v1[2], float v2[2], float v3[2], const float v4[2])
void immUniform4f(const char *name, float x, float y, float z, float w)
int BKE_fcurve_active_keyframe_index(const FCurve *fcu)
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
MINLINE float max_ff(float a, float b)
void ANIM_animdata_freelist(ListBaseT< bAnimListElem > *anim_data)
Definition anim_deps.cc:472
BLI_INLINE float BLI_rctf_cent_y(const struct rctf *rct)
Definition BLI_rect.hh:198
bool BKE_fcurve_are_keyframes_usable(const FCurve &fcu)
float ANIM_unit_mapping_get_factor(Scene *scene, ID *id, FCurve *fcu, short flag, float *r_offset)
Definition anim_draw.cc:636
void GPU_matrix_translate_2fv(const float vec[2])
void immUniform2fv(const char *name, const float data[2])
static void add_bezt_vertices(BezTriple *bezt, BezTriple *prevbezt, int resolution, Vector< float2 > &curve_vertices)
void immUniformColor3fv(const float rgb[3])
int BKE_fcurve_bezt_binarysearch_index(const BezTriple array[], float frame, int arraylen, bool *r_replace)
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
void immUniform1i(const char *name, int x)
void immVertex2fv(uint attr_id, const float data[2])
void GPU_matrix_pop()
void BKE_curve_forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
Definition curve.cc:1696
void immUniformColor3fvAlpha(const float rgb[3], float a)
MINLINE int min_ii(int a, int b)
MINLINE int clamp_i(int value, int min, int max)
@ FCURVE_EXTRAPOLATE_CONSTANT
static void draw_fcurve_selected_handle_vertices(FCurve *fcu, View2D *v2d, bool sel, bool sel_handle_only, uint pos)
static void draw_fcurve_handles(SpaceGraph *sipo, ARegion *region, const FCurve *fcu)
float ANIM_UI_get_channel_step()
BLI_INLINE float BLI_rctf_cent_x(const struct rctf *rct)
Definition BLI_rect.hh:194
static void draw_fcurve_curve_keys(bAnimContext *ac, ID *id, FCurve *fcu, View2D *v2d, uint pos, const bool draw_extrapolation)
void GPU_matrix_push()
static void draw_fcurve_keyframe_vertices(FCurve *fcu, View2D *v2d, const uint pos)
void BLI_rctf_init(struct rctf *rect, float xmin, float xmax, float ymin, float ymax)
Definition rct.cc:418
static float2 calculate_pixels_per_unit(View2D *v2d, const float unit_scale)
float evaluate_fcurve(const FCurve *fcu, float evaltime)
short ANIM_get_normalization_flags(SpaceLink *space_link)
Definition anim_draw.cc:420
static void draw_fcurve_active_vertex(const FCurve *fcu, const View2D *v2d, const uint pos)
#define min(a, b)
Definition sort.cc:38
eBezTriple_Flag f2
eBezTriple_Handle h2
eBezTriple_Flag f3
eBezTriple_Flag f1
eBezTriple_Interpolation ipo
eBezTriple_Handle h1
bActionGroup * grp
eFCurve_Flags flag
eFCurve_Extend extend
ListBaseT< FModifier > modifiers
ChannelDriver * driver
bke::FCurveRuntime * runtime
unsigned int totvert
FCM_EnvelopeData * data
eFModifier_Types type
ListBaseT< FCurve > ghost_curves
SpaceGraph_Runtime runtime
eGraphEdit_Flag flag
eAnimCont_Types datatype
bAnimListElem * next