Blender  V3.3
overlay_motion_path.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2019 Blender Foundation. */
3 
8 #include "DRW_render.h"
9 
10 #include "BLI_listbase.h"
11 #include "BLI_string.h"
12 
13 #include "DNA_armature_types.h"
14 
15 #include "DEG_depsgraph_query.h"
16 
17 #include "GPU_batch.h"
18 
19 #include "UI_resources.h"
20 
21 #include "draw_manager_text.h"
22 
23 #include "overlay_private.h"
24 
26 {
27  OVERLAY_PassList *psl = vedata->psl;
28  OVERLAY_PrivateData *pd = vedata->stl->pd;
29  DRWShadingGroup *grp;
30  GPUShader *sh;
31 
34 
37  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
38 
41  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
42 }
43 
44 /* Just convert the CPU cache to GPU cache. */
45 /* T0D0(fclem) This should go into a draw_cache_impl_motionpath. */
47 {
48  if (!mpath->points_vbo) {
49  GPUVertFormat format = {0};
50  /* Match structure of bMotionPathVert. */
55  /* meh... a useless memcpy. */
56  memcpy(GPU_vertbuf_get_data(mpath->points_vbo),
57  mpath->points,
58  sizeof(bMotionPathVert) * mpath->length);
59  }
60  return mpath->points_vbo;
61 }
62 
64 {
65  if (!mpath->batch_line) {
67  }
68  return mpath->batch_line;
69 }
70 
72 {
73  if (!mpath->batch_points) {
75  }
76  return mpath->batch_points;
77 }
78 
80  bMotionPath *mpath,
81  int current_frame,
82  int *r_start,
83  int *r_end,
84  int *r_step)
85 {
86  int start, end;
87 
88  if (avs->path_type == MOTIONPATH_TYPE_ACFRA) {
89  start = current_frame - avs->path_bc;
90  end = current_frame + avs->path_ac + 1;
91  }
92  else {
93  start = avs->path_sf;
94  end = avs->path_ef;
95  }
96 
97  if (start > end) {
98  SWAP(int, start, end);
99  }
100 
101  CLAMP(start, mpath->start_frame, mpath->end_frame);
102  CLAMP(end, mpath->start_frame, mpath->end_frame);
103 
104  *r_start = start;
105  *r_end = end;
106  *r_step = max_ii(avs->path_step, 1);
107 }
108 
109 static void motion_path_cache(OVERLAY_Data *vedata,
110  Object *ob,
111  bPoseChannel *pchan,
112  bAnimVizSettings *avs,
113  bMotionPath *mpath)
114 {
115  OVERLAY_PrivateData *pd = vedata->stl->pd;
116  const DRWContextState *draw_ctx = DRW_context_state_get();
117  struct DRWTextStore *dt = DRW_text_cache_ensure();
118  int txt_flag = DRW_TEXT_CACHE_GLOBALSPACE;
119  int cfra = (int)DEG_get_ctime(draw_ctx->depsgraph);
120  bool selected = (pchan) ? (pchan->bone->flag & BONE_SELECTED) : (ob->base_flag & BASE_SELECTED);
121  bool show_keyframes = (avs->path_viewflag & MOTIONPATH_VIEW_KFRAS) != 0;
122  bool show_keyframes_no = (avs->path_viewflag & MOTIONPATH_VIEW_KFNOS) != 0;
123  bool show_frame_no = (avs->path_viewflag & MOTIONPATH_VIEW_FNUMS) != 0;
124  bool show_lines = (mpath->flag & MOTIONPATH_FLAG_LINES) != 0;
125  float no_custom_col[3] = {-1.0f, -1.0f, -1.0f};
126  float *color = (mpath->flag & MOTIONPATH_FLAG_CUSTOM) ? mpath->color : no_custom_col;
127 
128  int sfra, efra, stepsize;
129  motion_path_get_frame_range_to_draw(avs, mpath, cfra, &sfra, &efra, &stepsize);
130 
131  int len = efra - sfra;
132  if (len == 0) {
133  return;
134  }
135  int start_index = sfra - mpath->start_frame;
136 
137  /* Draw curve-line of path. */
138  if (show_lines) {
139  const int motion_path_settings[4] = {cfra, sfra, efra, mpath->start_frame};
141  DRW_shgroup_uniform_ivec4_copy(grp, "mpathLineSettings", motion_path_settings);
142  DRW_shgroup_uniform_int_copy(grp, "lineThickness", mpath->line_thickness);
143  DRW_shgroup_uniform_bool_copy(grp, "selected", selected);
144  DRW_shgroup_uniform_vec3_copy(grp, "customColor", color);
145  /* Only draw the required range. */
146  DRW_shgroup_call_range(grp, NULL, mpath_batch_line_get(mpath), start_index, len);
147  }
148 
149  /* Draw points. */
150  {
151  int pt_size = max_ii(mpath->line_thickness - 1, 1);
152  const int motion_path_settings[4] = {pt_size, cfra, mpath->start_frame, stepsize};
154  DRW_shgroup_uniform_ivec4_copy(grp, "mpathPointSettings", motion_path_settings);
155  DRW_shgroup_uniform_bool_copy(grp, "showKeyFrames", show_keyframes);
156  DRW_shgroup_uniform_vec3_copy(grp, "customColor", color);
157  /* Only draw the required range. */
158  DRW_shgroup_call_range(grp, NULL, mpath_batch_points_get(mpath), start_index, len);
159  }
160 
161  /* Draw frame numbers at each frame-step value. */
162  if (show_frame_no || (show_keyframes_no && show_keyframes)) {
163  int i;
164  uchar col[4], col_kf[4];
165  /* Color Management: Exception here as texts are drawn in sRGB space directly. */
168  col[3] = col_kf[3] = 255;
169 
170  bMotionPathVert *mpv = mpath->points + start_index;
171  for (i = 0; i < len; i += stepsize, mpv += stepsize) {
172  int frame = sfra + i;
173  char numstr[32];
174  size_t numstr_len;
175  bool is_keyframe = (mpv->flag & MOTIONPATH_VERT_KEY) != 0;
176 
177  if ((show_keyframes && show_keyframes_no && is_keyframe) || (show_frame_no && (i == 0))) {
178  numstr_len = BLI_snprintf_rlen(numstr, sizeof(numstr), " %d", frame);
180  dt, mpv->co, numstr, numstr_len, 0, 0, txt_flag, (is_keyframe) ? col_kf : col);
181  }
182  else if (show_frame_no) {
183  bMotionPathVert *mpvP = (mpv - stepsize);
184  bMotionPathVert *mpvN = (mpv + stepsize);
185  /* Only draw frame number if several consecutive highlighted points
186  * don't occur on same point. */
187  if ((equals_v3v3(mpv->co, mpvP->co) == 0) || (equals_v3v3(mpv->co, mpvN->co) == 0)) {
188  numstr_len = BLI_snprintf_rlen(numstr, sizeof(numstr), " %d", frame);
189  DRW_text_cache_add(dt, mpv->co, numstr, numstr_len, 0, 0, txt_flag, col);
190  }
191  }
192  }
193  }
194 }
195 
197 {
198  const DRWContextState *draw_ctx = DRW_context_state_get();
199 
200  if (ob->type == OB_ARMATURE) {
201  if (OVERLAY_armature_is_pose_mode(ob, draw_ctx)) {
202  LISTBASE_FOREACH (bPoseChannel *, pchan, &ob->pose->chanbase) {
203  if (pchan->mpath) {
204  motion_path_cache(vedata, ob, pchan, &ob->pose->avs, pchan->mpath);
205  }
206  }
207  }
208  }
209 
210  if (ob->mpath) {
211  motion_path_cache(vedata, ob, NULL, &ob->avs, ob->mpath);
212  }
213 }
214 
216 {
217  OVERLAY_PassList *psl = vedata->psl;
218 
220 }
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
MINLINE int max_ii(int a, int b)
MINLINE bool equals_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
size_t BLI_snprintf_rlen(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
unsigned char uchar
Definition: BLI_sys_types.h:70
#define SWAP(type, a, b)
float DEG_get_ctime(const Depsgraph *graph)
@ MOTIONPATH_TYPE_ACFRA
@ MOTIONPATH_VERT_KEY
@ MOTIONPATH_VIEW_KFNOS
@ MOTIONPATH_VIEW_FNUMS
@ MOTIONPATH_VIEW_KFRAS
@ MOTIONPATH_FLAG_LINES
@ MOTIONPATH_FLAG_CUSTOM
@ BONE_SELECTED
@ BASE_SELECTED
@ OB_ARMATURE
DRWState
Definition: DRW_render.h:298
@ DRW_STATE_WRITE_COLOR
Definition: DRW_render.h:303
#define DRW_PASS_CREATE(pass, state)
Definition: DRW_render.h:690
#define DRW_shgroup_uniform_block(shgroup, name, ubo)
Definition: DRW_render.h:651
GPUBatch
Definition: GPU_batch.h:78
#define GPU_batch_create(prim, verts, elem)
Definition: GPU_batch.h:95
@ GPU_PRIM_POINTS
Definition: GPU_primitive.h:19
@ GPU_PRIM_LINE_STRIP
Definition: GPU_primitive.h:22
struct GPUShader GPUShader
Definition: GPU_shader.h:20
#define GPU_vertbuf_create_with_format(format)
struct GPUVertBuf GPUVertBuf
void GPU_vertbuf_data_alloc(GPUVertBuf *, uint v_len)
void * GPU_vertbuf_get_data(const GPUVertBuf *verts)
@ GPU_FETCH_FLOAT
@ GPU_FETCH_INT
uint GPU_vertformat_attr_add(GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_F32
@ GPU_COMP_I32
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position CLAMP
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a color
@ TH_VERTEX_SELECT
Definition: UI_resources.h:78
@ TH_TEXT_HI
Definition: UI_resources.h:43
void UI_GetThemeColor3ubv(int colorid, unsigned char col[3])
Definition: resources.c:1323
struct DRW_Global G_draw
Definition: draw_common.c:32
int len
Definition: draw_manager.c:108
const DRWContextState * DRW_context_state_get(void)
struct DRWTextStore * DRW_text_cache_ensure(void)
void DRW_shgroup_uniform_vec3_copy(DRWShadingGroup *shgroup, const char *name, const float *value)
void DRW_shgroup_uniform_int_copy(DRWShadingGroup *shgroup, const char *name, const int value)
DRWShadingGroup * DRW_shgroup_create(struct GPUShader *shader, DRWPass *pass)
DRWShadingGroup * DRW_shgroup_create_sub(DRWShadingGroup *shgroup)
void DRW_shgroup_uniform_bool_copy(DRWShadingGroup *shgroup, const char *name, const bool value)
void DRW_shgroup_call_range(DRWShadingGroup *shgroup, struct Object *ob, GPUBatch *geom, uint v_sta, uint v_num)
void DRW_shgroup_uniform_ivec4_copy(DRWShadingGroup *shgroup, const char *name, const int *value)
void DRW_draw_pass(DRWPass *pass)
void DRW_text_cache_add(DRWTextStore *dt, const float co[3], const char *str, const int str_len, short xoffs, short yoffs, short flag, const uchar col[4])
@ DRW_TEXT_CACHE_GLOBALSPACE
uint col
const int state
ccl_gpu_kernel_postfix ccl_global float int int int int sh
format
Definition: logImageCore.h:38
bool OVERLAY_armature_is_pose_mode(Object *ob, const DRWContextState *draw_ctx)
static GPUVertBuf * mpath_vbo_get(bMotionPath *mpath)
void OVERLAY_motion_path_cache_populate(OVERLAY_Data *vedata, Object *ob)
void OVERLAY_motion_path_draw(OVERLAY_Data *vedata)
void OVERLAY_motion_path_cache_init(OVERLAY_Data *vedata)
static void motion_path_cache(OVERLAY_Data *vedata, Object *ob, bPoseChannel *pchan, bAnimVizSettings *avs, bMotionPath *mpath)
static GPUBatch * mpath_batch_line_get(bMotionPath *mpath)
static GPUBatch * mpath_batch_points_get(bMotionPath *mpath)
static void motion_path_get_frame_range_to_draw(bAnimVizSettings *avs, bMotionPath *mpath, int current_frame, int *r_start, int *r_end, int *r_step)
GPUShader * OVERLAY_shader_motion_path_vert(void)
GPUShader * OVERLAY_shader_motion_path_line(void)
struct Depsgraph * depsgraph
Definition: DRW_render.h:987
struct GPUUniformBuf * block_ubo
Definition: draw_common.h:129
OVERLAY_PassList * psl
OVERLAY_StorageList * stl
DRWPass * motion_paths_ps
DRWShadingGroup * motion_path_points_grp
DRWShadingGroup * motion_path_lines_grp
struct OVERLAY_PrivateData * pd
short base_flag
struct bPose * pose
bMotionPath * mpath
bAnimVizSettings avs
struct GPUVertBuf * points_vbo
bMotionPathVert * points
struct GPUBatch * batch_points
struct GPUBatch * batch_line
float color[3]
struct Bone * bone
ListBase chanbase
bAnimVizSettings avs