Welcome to mirror list, hosted at ThFree Co, Russian Federation.

draw_anim_viz.c « intern « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ffc921e20818cf6286748633db6bf5558a2d6854 (plain)
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
/*
 * ***** 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) 2009/2018 by the Blender Foundation.
 *
 * Contributor(s): Joshua Leung
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/draw/intern/draw_anim_viz.c
 *  \ingroup draw
 */


#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "BLI_sys_types.h"

#include "DNA_anim_types.h"
#include "DNA_armature_types.h"
#include "DNA_scene_types.h"
#include "DNA_screen_types.h"
#include "DNA_view3d_types.h"
#include "DNA_object_types.h"

#include "BLI_math.h"
#include "BLI_dlrbTree.h"

#include "BKE_animsys.h"
#include "BKE_action.h"

#include "ED_keyframes_draw.h"

#include "UI_resources.h"

#include "DEG_depsgraph_query.h"

#include "DRW_engine.h"
#include "DRW_render.h"

#include "GPU_shader.h"
#include "GPU_immediate.h"
#include "GPU_matrix.h"

#include "draw_common.h"
#include "draw_manager_text.h"

#include "draw_mode_engines.h"

extern struct GPUUniformBuffer *globals_ubo; /* draw_common.c */

/* ********************************* Lists ************************************** */
/* All lists are per viewport specific datas.
 * They are all free when viewport changes engines
 * or is free itself.
 */

/* XXX: How to show frame numbers, etc.?  Currently only doing the dots and lines */
typedef struct MPATH_PassList {
	struct DRWPass *lines;
	struct DRWPass *points;
} MPATH_PassList;

typedef struct MPATH_StorageList {
	struct MPATH_PrivateData *g_data;
} MPATH_StorageList;

typedef struct MPATH_Data {
	void *engine_type;
	DRWViewportEmptyList *fbl;
	DRWViewportEmptyList *txl;
	MPATH_PassList *psl;
	MPATH_StorageList *stl;
} MPATH_Data;

struct {
	GPUShader *mpath_line_sh;
	GPUShader *mpath_points_sh;
} e_data = {0};

/* *************************** Path Cache *********************************** */

/* Just convert the CPU cache to GPU cache. */
static Gwn_VertBuf *mpath_vbo_get(bMotionPath *mpath)
{
	if (!mpath->points_vbo) {
		Gwn_VertFormat format = {0};
		/* Match structure of bMotionPathVert. */
		uint pos = GWN_vertformat_attr_add(&format, "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
		GWN_vertformat_attr_add(&format, "flag", GWN_COMP_I32, 1, GWN_FETCH_INT);
		mpath->points_vbo = GWN_vertbuf_create_with_format(&format);
		GWN_vertbuf_data_alloc(mpath->points_vbo, mpath->length);

		/* meh... a useless memcpy. */
		Gwn_VertBufRaw raw_data;
		GWN_vertbuf_attr_get_raw_data(mpath->points_vbo, pos, &raw_data);
		memcpy(GWN_vertbuf_raw_step(&raw_data), mpath->points, sizeof(bMotionPathVert) * mpath->length);
	}
	return mpath->points_vbo;
}

static Gwn_Batch *mpath_batch_line_get(bMotionPath *mpath)
{
	if (!mpath->batch_line) {
		mpath->batch_line = GWN_batch_create(GWN_PRIM_LINE_STRIP, mpath_vbo_get(mpath), NULL);
	}
	return mpath->batch_line;
}

static Gwn_Batch *mpath_batch_points_get(bMotionPath *mpath)
{
	if (!mpath->batch_points) {
		mpath->batch_points = GWN_batch_create(GWN_PRIM_POINTS, mpath_vbo_get(mpath), NULL);
	}
	return mpath->batch_points;
}

/* *************************** Motion Path Drawing ****************************** */

/* TODO:
 * - options to draw paths with lines
 * - include support for editing the path verts */

/* Set up drawing environment for drawing motion paths */
void draw_motion_paths_init(View3D *v3d, ARegion *ar) 
{
	RegionView3D *rv3d = ar->regiondata;
	
	if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
	
	gpuPushMatrix();
	gpuLoadMatrix(rv3d->viewmat);
}

/* set color
 * - more intense for active/selected bones, less intense for unselected bones
 * - black for before current frame, green for current frame, blue for after current frame
 * - intensity decreases as distance from current frame increases
 *
 * If the user select custom color, the color is replaced for the color selected in UI panel
 * - 75% Darker color is used for previous frames
 * - 50% Darker color for current frame
 * - User selected color for next frames
 */
static void set_motion_path_color(Scene *scene, bMotionPath *mpath, int i, short sel, int sfra, int efra,
	float prev_color[3], float frame_color[3], float next_color[3], unsigned color)
{
	int frame = sfra + i;
	int blend_base = (abs(frame - CFRA) == 1) ? TH_CFRAME : TH_BACK; /* "bleed" cframe color to ease color blending */
	unsigned char ubcolor[3];

#define SET_INTENSITY(A, B, C, min, max) (((1.0f - ((C - B) / (C - A))) * (max - min)) + min)
	float intensity;  /* how faint */

	if (frame < CFRA) {
		if (mpath->flag & MOTIONPATH_FLAG_CUSTOM) {
			/* Custom color: previous frames color is darker than current frame */
			rgb_float_to_uchar(ubcolor, prev_color);
		}
		else {
			/* black - before cfra */
			if (sel) {
				/* intensity = 0.5f; */
				intensity = SET_INTENSITY(sfra, i, CFRA, 0.25f, 0.75f);
			}
			else {
				/* intensity = 0.8f; */
				intensity = SET_INTENSITY(sfra, i, CFRA, 0.68f, 0.92f);
			}

			UI_GetThemeColorBlend3ubv(TH_WIRE, blend_base, intensity, ubcolor);
		}
	}
	else if (frame > CFRA) {
		if (mpath->flag & MOTIONPATH_FLAG_CUSTOM) {
			/* Custom color: next frames color is equal to user selected color */
			rgb_float_to_uchar(ubcolor, next_color);
		}
		else {
			/* blue - after cfra */
			if (sel) {
				/* intensity = 0.5f; */
				intensity = SET_INTENSITY(CFRA, i, efra, 0.25f, 0.75f);
			}
			else {
				/* intensity = 0.8f; */
				intensity = SET_INTENSITY(CFRA, i, efra, 0.68f, 0.92f);
			}

			UI_GetThemeColorBlend3ubv(TH_BONE_POSE, blend_base, intensity, ubcolor);
		}
	}
	else {
		if (mpath->flag & MOTIONPATH_FLAG_CUSTOM) {
			/* Custom color: current frame color is slightly darker than user selected color */
			rgb_float_to_uchar(ubcolor, frame_color);
		}
		else {
			/* green - on cfra */
			if (sel) {
				intensity = 0.5f;
			}
			else {
				intensity = 0.99f;
			}
			UI_GetThemeColorBlendShade3ubv(TH_CFRAME, TH_BACK, intensity, 10, ubcolor);
		}
	}

	immAttrib3ubv(color, ubcolor);

#undef SET_INTENSITY
}

/* Draw the given motion path for an Object or a Bone 
 *  - assumes that the viewport has already been initialized properly
 *    i.e. draw_motion_paths_init() has been called
 */
void draw_motion_path_instance(Scene *scene, 
                               Object *ob, bPoseChannel *pchan,
                               bAnimVizSettings *avs, bMotionPath *mpath)
{
	//RegionView3D *rv3d = ar->regiondata;
	bMotionPathVert *mpv, *mpv_start;
	int i, stepsize = avs->path_step;
	int sfra, efra, sind, len;
	float prev_color[3];
	float frame_color[3];
	float next_color[3];

	/* Custom color - Previous frames: color is darker than current frame */
	mul_v3_v3fl(prev_color, mpath->color, 0.25f);

	/* Custom color - Current frame: color is slightly darker than user selected color */
	mul_v3_v3fl(frame_color, mpath->color, 0.5f);

	/* Custom color - Next frames: color is equal to user selection */
	copy_v3_v3(next_color, mpath->color);

	/* Save old line width */
	GLfloat old_width;
	glGetFloatv(GL_LINE_WIDTH, &old_width);
	
	/* get frame ranges */
	if (avs->path_type == MOTIONPATH_TYPE_ACFRA) {
		/* With "Around Current", we only choose frames from around 
		 * the current frame to draw.
		 */
		sfra = CFRA - avs->path_bc;
		efra = CFRA + avs->path_ac;
	}
	else {
		/* Use the current display range */
		sfra = avs->path_sf;
		efra = avs->path_ef;
	}
	
	/* no matter what, we can only show what is in the cache and no more 
	 * - abort if whole range is past ends of path
	 * - otherwise clamp endpoints to extents of path
	 */
	if (sfra < mpath->start_frame) {
		/* start clamp */
		sfra = mpath->start_frame;
	}
	if (efra > mpath->end_frame) {
		/* end clamp */
		efra = mpath->end_frame;
	}
	
	if ((sfra > mpath->end_frame) || (efra < mpath->start_frame)) {
		/* whole path is out of bounds */
		return;
	}
	
	len = efra - sfra;
	
	if ((len <= 0) || (mpath->points == NULL)) {
		return;
	}
	
	/* get pointers to parts of path */
	sind = sfra - mpath->start_frame;
	mpv_start = (mpath->points + sind);

	/* draw curve-line of path */
	/* Draw lines only if line drawing option is enabled */
	if (mpath->flag & MOTIONPATH_FLAG_LINES) {
		/* set line thickness */
		glLineWidth(mpath->line_thickness);

		Gwn_VertFormat *format = immVertexFormat();
		unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
		unsigned int color = GWN_vertformat_attr_add(format, "color", GWN_COMP_U8, 3, GWN_FETCH_INT_TO_FLOAT_UNIT);

		immBindBuiltinProgram(GPU_SHADER_3D_SMOOTH_COLOR);

		immBegin(GWN_PRIM_LINE_STRIP, len);
		for (i = 0, mpv = mpv_start; i < len; i++, mpv++) {
			short sel = (pchan) ? (pchan->bone->flag & BONE_SELECTED) : (ob->flag & SELECT);

			/* Set color */
			set_motion_path_color(scene, mpath, i, sel, sfra, efra, prev_color, frame_color, next_color, color);

			/* draw a vertex with this color */
			immVertex3fv(pos, mpv->co);
		}
		immEnd();

		immUnbindProgram();

		/* back to old line thickness */
		glLineWidth(old_width);
	}

	unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);

	immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);

	/* Point must be bigger than line thickness */
	glPointSize(mpath->line_thickness + 1.0);
	
	/* draw little black point at each frame */
	immUniformColor3ub(0, 0, 0);

	immBegin(GWN_PRIM_POINTS, len);

	for (i = 0, mpv = mpv_start; i < len; i++, mpv++) {
		immVertex3fv(pos, mpv->co);
	}

	immEnd();

	/* Draw little white dots at each framestep value or replace with custom color */
	if (mpath->flag & MOTIONPATH_FLAG_CUSTOM) {
		immUniformColor3fv(mpath->color);
	}
	else {
		immUniformThemeColor(TH_TEXT_HI);
	}

	immBegin(GWN_PRIM_POINTS, (len + stepsize - 1) / stepsize);

	for (i = 0, mpv = mpv_start; i < len; i += stepsize, mpv += stepsize) {
		immVertex3fv(pos, mpv->co);
	}

	immEnd();
	
	/* Draw big green dot where the current frame is 
	 * NOTE: this is only done when keyframes are shown, since this adds similar types of clutter
	 */
	if ((avs->path_viewflag & MOTIONPATH_VIEW_KFRAS) &&
	    (sfra < CFRA) && (CFRA <= efra)) 
	{
		glPointSize(mpath->line_thickness + 5.0);
		immUniformThemeColor(TH_CFRAME);

		immBegin(GWN_PRIM_POINTS, 1);

		mpv = mpv_start + (CFRA - sfra);
		immVertex3fv(pos, mpv->co);

		immEnd();
	}

	immUnbindProgram();
	
	/* XXX, this isn't up to date but probably should be kept so. */
	invert_m4_m4(ob->imat, ob->obmat);
	
#if 0 /* FIXME!!! How can we draw text in the new 3d Viewport */

	/* Draw frame numbers at each framestep value */
	if (avs->path_viewflag & MOTIONPATH_VIEW_FNUMS) {
		unsigned char col[4];
		UI_GetThemeColor3ubv(TH_TEXT_HI, col);
		col[3] = 255;
		
		for (i = 0, mpv = mpv_start; i < len; i += stepsize, mpv += stepsize) {
			int frame = sfra + i;
			char numstr[32];
			size_t numstr_len;
			float co[3];
			
			/* only draw framenum if several consecutive highlighted points don't occur on same point */
			if (i == 0) {
				numstr_len = sprintf(numstr, " %d", frame);
				mul_v3_m4v3(co, ob->imat, mpv->co);
				view3d_cached_text_draw_add(co, numstr, numstr_len,
				                            0, V3D_CACHE_TEXT_WORLDSPACE | V3D_CACHE_TEXT_ASCII, col);
			}
			else if ((i >= stepsize) && (i < len - stepsize)) {
				bMotionPathVert *mpvP = (mpv - stepsize);
				bMotionPathVert *mpvN = (mpv + stepsize);
				
				if ((equals_v3v3(mpv->co, mpvP->co) == 0) || (equals_v3v3(mpv->co, mpvN->co) == 0)) {
					numstr_len = sprintf(numstr, " %d", frame);
					mul_v3_m4v3(co, ob->imat, mpv->co);
					view3d_cached_text_draw_add(co, numstr, numstr_len,
					                            0, V3D_CACHE_TEXT_WORLDSPACE | V3D_CACHE_TEXT_ASCII, col);
				}
			}
		}
	}
	
	/* Keyframes - dots and numbers */
	if (avs->path_viewflag & MOTIONPATH_VIEW_KFRAS) {
		unsigned char col[4];
		
		AnimData *adt = BKE_animdata_from_id(&ob->id);
		DLRBT_Tree keys;
		
		/* build list of all keyframes in active action for object or pchan */
		BLI_dlrbTree_init(&keys);
		
		if (adt) {
			/* it is assumed that keyframes for bones are all grouped in a single group
			 * unless an option is set to always use the whole action
			 */
			if ((pchan) && (avs->path_viewflag & MOTIONPATH_VIEW_KFACT) == 0) {
				bActionGroup *agrp = BKE_action_group_find_name(adt->action, pchan->name);
				
				if (agrp) {
					agroup_to_keylist(adt, agrp, &keys, NULL);
					BLI_dlrbTree_linkedlist_sync(&keys);
				}
			}
			else {
				action_to_keylist(adt, adt->action, &keys, NULL);
				BLI_dlrbTree_linkedlist_sync(&keys);
			}
		}
		
		/* Draw slightly-larger yellow dots at each keyframe */
		UI_GetThemeColor3ubv(TH_VERTEX_SELECT, col);
		col[3] = 255;
		
		/* point must be bigger than line */
		glPointSize(mpath->line_thickness + 3.0);

		pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);

		immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
		immUniformColor3ubv(col);
		
		immBeginAtMost(GWN_PRIM_POINTS, len);

		for (i = 0, mpv = mpv_start; i < len; i++, mpv++) {
			int    frame = sfra + i; 
			float mframe = (float)(frame);
			
			if (BLI_dlrbTree_search_exact(&keys, compare_ak_cfraPtr, &mframe)) {
				immVertex3fv(pos, mpv->co);
			}
		}

		immEnd();

		immUnbindProgram();
		
		/* Draw frame numbers of keyframes  */
		if (avs->path_viewflag & MOTIONPATH_VIEW_KFNOS) {
			float co[3];
			for (i = 0, mpv = mpv_start; i < len; i++, mpv++) {
				float mframe = (float)(sfra + i);
				
				if (BLI_dlrbTree_search_exact(&keys, compare_ak_cfraPtr, &mframe)) {
					char numstr[32];
					size_t numstr_len;
					
					numstr_len = sprintf(numstr, " %d", (sfra + i));
					mul_v3_m4v3(co, ob->imat, mpv->co);
					view3d_cached_text_draw_add(co, numstr, numstr_len,
					                            0, V3D_CACHE_TEXT_WORLDSPACE | V3D_CACHE_TEXT_ASCII, col);
				}
			}
		}
		
		BLI_dlrbTree_free(&keys);
	}
#endif
}

/* Clean up drawing environment after drawing motion paths */
void draw_motion_paths_cleanup(View3D *v3d)
{
	if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
	gpuPopMatrix();
}

/* ******************* Populate Cache from MotionPath Sources ******************* */

/* Motion Paths from Object */


/* *************************** Draw Engine Entrypoints ************************** */

static void MPATH_engine_init(void *UNUSED(vedata))
{
}

static void MPATH_engine_free(void)
{
}

/* Here init all passes and shading groups
 * Assume that all Passes are NULL */
static void MPATH_cache_init(void *vedata)
{
	MPATH_PassList *psl = ((MPATH_Data *)vedata)->psl;

	{
		DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS;
		psl->lines = DRW_pass_create("Motionpath Line Pass", state);
	}

	{
		DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_POINT;
		psl->points = DRW_pass_create("Motionpath Point Pass", state);
	}
}

static void MPATH_cache_motion_path(MPATH_PassList *psl,
                                    Object *ob, bPoseChannel *pchan,
                                    bAnimVizSettings *avs, bMotionPath *mpath)
{
	const DRWContextState *draw_ctx = DRW_context_state_get();
	struct DRWTextStore *dt = DRW_text_cache_ensure();
	int txt_flag = DRW_TEXT_CACHE_GLOBALSPACE | DRW_TEXT_CACHE_ASCII;
	int stepsize = avs->path_step;
	int sfra, efra, sind, len;
	int cfra = (int)DEG_get_ctime(draw_ctx->depsgraph);
	bool sel = (pchan) ? (pchan->bone->flag & BONE_SELECTED) : (ob->flag & SELECT);
	bool show_keyframes = (avs->path_viewflag & MOTIONPATH_VIEW_KFRAS) != 0;
	bMotionPathVert *mpv, *mpv_start;

	/* get frame ranges */
	if (avs->path_type == MOTIONPATH_TYPE_ACFRA) {
		/* With "Around Current", we only choose frames from around
		 * the current frame to draw.
		 */
		sfra = cfra - avs->path_bc;
		efra = cfra + avs->path_ac + 1;
	}
	else {
		/* Use the current display range */
		sfra = avs->path_sf;
		efra = avs->path_ef;
	}

	/* no matter what, we can only show what is in the cache and no more
	 * - abort if whole range is past ends of path
	 * - otherwise clamp endpoints to extents of path
	 */
	if (sfra < mpath->start_frame) {
		/* start clamp */
		sfra = mpath->start_frame;
	}
	if (efra > mpath->end_frame) {
		/* end clamp */
		efra = mpath->end_frame;
	}

	if ((sfra > mpath->end_frame) || (efra < mpath->start_frame)) {
		/* whole path is out of bounds */
		return;
	}

	len = efra - sfra;

	if ((len <= 0) || (mpath->points == NULL)) {
		return;
	}

	sind = sfra - mpath->start_frame;
	mpv_start = (mpath->points + sind);

	bool use_custom_col = (mpath->flag & MOTIONPATH_FLAG_CUSTOM) != 0;

	/* draw curve-line of path */
	/* Draw lines only if line drawing option is enabled */
	if (mpath->flag & MOTIONPATH_FLAG_LINES) {
		DRWShadingGroup *shgrp = DRW_shgroup_create(mpath_line_shader_get(), psl->lines);
		DRW_shgroup_uniform_int_copy(shgrp, "frameCurrent", cfra);
		DRW_shgroup_uniform_int_copy(shgrp, "frameStart", sfra);
		DRW_shgroup_uniform_int_copy(shgrp, "frameEnd", efra);
		DRW_shgroup_uniform_int_copy(shgrp, "cacheStart", mpath->start_frame);
		DRW_shgroup_uniform_int_copy(shgrp, "lineThickness", mpath->line_thickness);
		DRW_shgroup_uniform_bool_copy(shgrp, "selected", sel);
		DRW_shgroup_uniform_bool_copy(shgrp, "useCustomColor", use_custom_col);
		DRW_shgroup_uniform_vec2(shgrp, "viewportSize", DRW_viewport_size_get(), 1);
		DRW_shgroup_uniform_block(shgrp, "globalsBlock", globals_ubo);
		if (use_custom_col) {
			DRW_shgroup_uniform_vec3(shgrp, "customColor", mpath->color, 1);
		}
		/* Only draw the required range. */
		DRW_shgroup_call_range_add(shgrp, mpath_batch_line_get(mpath), NULL, sind, len);
	}

	/* Draw points. */
	DRWShadingGroup *shgrp = DRW_shgroup_create(mpath_points_shader_get(), psl->points);
	DRW_shgroup_uniform_int_copy(shgrp, "frameCurrent", cfra);
	DRW_shgroup_uniform_int_copy(shgrp, "cacheStart", mpath->start_frame);
	DRW_shgroup_uniform_int_copy(shgrp, "pointSize", mpath->line_thickness);
	DRW_shgroup_uniform_int_copy(shgrp, "stepSize", stepsize);
	DRW_shgroup_uniform_bool_copy(shgrp, "selected", sel);
	DRW_shgroup_uniform_bool_copy(shgrp, "showKeyFrames", show_keyframes);
	DRW_shgroup_uniform_bool_copy(shgrp, "useCustomColor", use_custom_col);
	DRW_shgroup_uniform_block(shgrp, "globalsBlock", globals_ubo);
	if (use_custom_col) {
		DRW_shgroup_uniform_vec3(shgrp, "customColor", mpath->color, 1);
	}
	/* Only draw the required range. */
	DRW_shgroup_call_range_add(shgrp, mpath_batch_points_get(mpath), NULL, sind, len);

	/* Draw frame numbers at each framestep value */
	bool show_kf_no = (avs->path_viewflag & MOTIONPATH_VIEW_KFNOS) != 0;
	if ((avs->path_viewflag & (MOTIONPATH_VIEW_FNUMS)) || (show_kf_no && show_keyframes)) {
		int i;
		unsigned char col[4], col_kf[4];
		UI_GetThemeColor3ubv(TH_TEXT_HI, col);
		UI_GetThemeColor3ubv(TH_VERTEX_SELECT, col_kf);
		col[3] = col_kf[3] = 255;

		for (i = 0, mpv = mpv_start; i < len; i += stepsize, mpv += stepsize) {
			int frame = sfra + i;
			char numstr[32];
			size_t numstr_len;
			float co[3];
			bool is_keyframe = (mpv->flag & MOTIONPATH_VERT_KEY) != 0;

			if ((show_keyframes && show_kf_no && is_keyframe) ||
			    ((avs->path_viewflag & MOTIONPATH_VIEW_FNUMS) && (i == 0)))
			{
				numstr_len = sprintf(numstr, " %d", frame);
				mul_v3_m4v3(co, ob->imat, mpv->co);
				DRW_text_cache_add(dt, co, numstr, numstr_len, 0, txt_flag, (is_keyframe) ? col_kf : col);
			}
			else if (avs->path_viewflag & MOTIONPATH_VIEW_FNUMS) {
				bMotionPathVert *mpvP = (mpv - stepsize);
				bMotionPathVert *mpvN = (mpv + stepsize);
				/* only draw framenum if several consecutive highlighted points don't occur on same point */
				if ((equals_v3v3(mpv->co, mpvP->co) == 0) || (equals_v3v3(mpv->co, mpvN->co) == 0)) {
					numstr_len = sprintf(numstr, " %d", frame);
					mul_v3_m4v3(co, ob->imat, mpv->co);
					DRW_text_cache_add(dt, co, numstr, numstr_len, 0, txt_flag, col);
				}
			}
		}
	}
}

/* Add geometry to shading groups. Execute for each objects */
static void MPATH_cache_populate(void *vedata, Object *ob)
{
	MPATH_PassList *psl = ((MPATH_Data *)vedata)->psl;
	const DRWContextState *draw_ctx = DRW_context_state_get();

	if (draw_ctx->v3d->overlay.flag & V3D_OVERLAY_HIDE_MOTION_PATHS) {
		return;
	}

	if (ob->type == OB_ARMATURE) {
		if (DRW_pose_mode_armature(ob, draw_ctx->obact)) {
			for (bPoseChannel *pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
				if (pchan->mpath) {
					MPATH_cache_motion_path(psl, ob, pchan, &ob->pose->avs, pchan->mpath);
				}
			}
		}
	}
	else {
		if (ob->mpath) {
			MPATH_cache_motion_path(psl, ob, NULL, &ob->avs, ob->mpath);
		}
	}
}

/* Draw time! Control rendering pipeline from here */
static void MPATH_draw_scene(void *vedata)
{
	MPATH_PassList *psl = ((MPATH_Data *)vedata)->psl;
	DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
	DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
	
	MULTISAMPLE_SYNC_ENABLE(dfbl, dtxl)

	DRW_draw_pass(psl->lines);
	DRW_draw_pass(psl->points);

	MULTISAMPLE_SYNC_DISABLE(dfbl, dtxl)
}

/* *************************** Draw Engine Defines ****************************** */

static const DrawEngineDataSize MPATH_data_size = DRW_VIEWPORT_DATA_SIZE(MPATH_Data);

DrawEngineType draw_engine_motion_path_type = {
	NULL, NULL,
	N_("MotionPath"),
	&MPATH_data_size,
	&MPATH_engine_init,
	&MPATH_engine_free,
	&MPATH_cache_init,
	&MPATH_cache_populate,
	NULL,
	NULL,
	&MPATH_draw_scene,
	NULL,
	NULL,
};