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

overlay_motion_path.c « overlay « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0e5a52702fee9043e9be1a99e0daf45b21d90d8f (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
/*
 * 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.
 *
 * Copyright 2019, Blender Foundation.
 */

/** \file
 * \ingroup draw_engine
 */

#include "DRW_render.h"

#include "BLI_listbase.h"
#include "BLI_string.h"

#include "DNA_armature_types.h"

#include "DEG_depsgraph_query.h"

#include "GPU_batch.h"

#include "UI_resources.h"

#include "draw_manager_text.h"

#include "overlay_private.h"

void OVERLAY_motion_path_cache_init(OVERLAY_Data *vedata)
{
  OVERLAY_PassList *psl = vedata->psl;
  OVERLAY_PrivateData *pd = vedata->stl->pd;
  DRWShadingGroup *grp;
  GPUShader *sh;

  DRWState state = DRW_STATE_WRITE_COLOR;
  DRW_PASS_CREATE(psl->motion_paths_ps, state | pd->clipping_state);

  sh = OVERLAY_shader_motion_path_line();
  pd->motion_path_lines_grp = grp = DRW_shgroup_create(sh, psl->motion_paths_ps);
  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);

  sh = OVERLAY_shader_motion_path_vert();
  pd->motion_path_points_grp = grp = DRW_shgroup_create(sh, psl->motion_paths_ps);
  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
}

/* Just convert the CPU cache to GPU cache. */
/* T0D0(fclem) This should go into a draw_cache_impl_motionpath. */
static GPUVertBuf *mpath_vbo_get(bMotionPath *mpath)
{
  if (!mpath->points_vbo) {
    GPUVertFormat format = {0};
    /* Match structure of bMotionPathVert. */
    GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
    GPU_vertformat_attr_add(&format, "flag", GPU_COMP_I32, 1, GPU_FETCH_INT);
    mpath->points_vbo = GPU_vertbuf_create_with_format(&format);
    GPU_vertbuf_data_alloc(mpath->points_vbo, mpath->length);
    /* meh... a useless memcpy. */
    memcpy(mpath->points_vbo->data, mpath->points, sizeof(bMotionPathVert) * mpath->length);
  }
  return mpath->points_vbo;
}

static GPUBatch *mpath_batch_line_get(bMotionPath *mpath)
{
  if (!mpath->batch_line) {
    mpath->batch_line = GPU_batch_create(GPU_PRIM_LINE_STRIP, mpath_vbo_get(mpath), NULL);
  }
  return mpath->batch_line;
}

static GPUBatch *mpath_batch_points_get(bMotionPath *mpath)
{
  if (!mpath->batch_points) {
    mpath->batch_points = GPU_batch_create(GPU_PRIM_POINTS, mpath_vbo_get(mpath), NULL);
  }
  return mpath->batch_points;
}

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)
{
  int start, end;

  if (avs->path_type == MOTIONPATH_TYPE_ACFRA) {
    start = current_frame - avs->path_bc;
    end = current_frame + avs->path_ac + 1;
  }
  else {
    start = avs->path_sf;
    end = avs->path_ef;
  }

  if (start > end) {
    SWAP(int, start, end);
  }

  CLAMP(start, mpath->start_frame, mpath->end_frame);
  CLAMP(end, mpath->start_frame, mpath->end_frame);

  *r_start = start;
  *r_end = end;
  *r_step = max_ii(avs->path_step, 1);
}

static void motion_path_cache(OVERLAY_Data *vedata,
                              Object *ob,
                              bPoseChannel *pchan,
                              bAnimVizSettings *avs,
                              bMotionPath *mpath)
{
  OVERLAY_PrivateData *pd = vedata->stl->pd;
  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 cfra = (int)DEG_get_ctime(draw_ctx->depsgraph);
  bool selected = (pchan) ? (pchan->bone->flag & BONE_SELECTED) : (ob->base_flag & BASE_SELECTED);
  bool show_keyframes = (avs->path_viewflag & MOTIONPATH_VIEW_KFRAS) != 0;
  bool show_keyframes_no = (avs->path_viewflag & MOTIONPATH_VIEW_KFNOS) != 0;
  bool show_frame_no = (avs->path_viewflag & MOTIONPATH_VIEW_FNUMS) != 0;
  bool show_lines = (mpath->flag & MOTIONPATH_FLAG_LINES) != 0;
  float no_custom_col[3] = {-1.0f, -1.0f, -1.0f};
  float *color = (mpath->flag & MOTIONPATH_FLAG_CUSTOM) ? mpath->color : no_custom_col;

  int sfra, efra, stepsize;
  motion_path_get_frame_range_to_draw(avs, mpath, cfra, &sfra, &efra, &stepsize);

  int len = efra - sfra;
  if (len == 0) {
    return;
  }
  int start_index = sfra - mpath->start_frame;

  /* Draw curve-line of path. */
  if (show_lines) {
    const int motion_path_settings[4] = {cfra, sfra, efra, mpath->start_frame};
    DRWShadingGroup *grp = DRW_shgroup_create_sub(pd->motion_path_lines_grp);
    DRW_shgroup_uniform_ivec4_copy(grp, "mpathLineSettings", motion_path_settings);
    DRW_shgroup_uniform_int_copy(grp, "lineThickness", mpath->line_thickness);
    DRW_shgroup_uniform_bool_copy(grp, "selected", selected);
    DRW_shgroup_uniform_vec3_copy(grp, "customColor", color);
    /* Only draw the required range. */
    DRW_shgroup_call_range(grp, NULL, mpath_batch_line_get(mpath), start_index, len);
  }

  /* Draw points. */
  {
    int pt_size = max_ii(mpath->line_thickness - 1, 1);
    const int motion_path_settings[4] = {pt_size, cfra, mpath->start_frame, stepsize};
    DRWShadingGroup *grp = DRW_shgroup_create_sub(pd->motion_path_points_grp);
    DRW_shgroup_uniform_ivec4_copy(grp, "mpathPointSettings", motion_path_settings);
    DRW_shgroup_uniform_bool_copy(grp, "showKeyFrames", show_keyframes);
    DRW_shgroup_uniform_vec3_copy(grp, "customColor", color);
    /* Only draw the required range. */
    DRW_shgroup_call_range(grp, NULL, mpath_batch_points_get(mpath), start_index, len);
  }

  /* Draw frame numbers at each frame-step value. */
  if (show_frame_no || (show_keyframes_no && show_keyframes)) {
    int i;
    uchar col[4], col_kf[4];
    /* Color Management: Exception here as texts are drawn in sRGB space directly.  */
    UI_GetThemeColor3ubv(TH_TEXT_HI, col);
    UI_GetThemeColor3ubv(TH_VERTEX_SELECT, col_kf);
    col[3] = col_kf[3] = 255;

    bMotionPathVert *mpv = mpath->points + start_index;
    for (i = 0; i < len; i += stepsize, mpv += stepsize) {
      int frame = sfra + i;
      char numstr[32];
      size_t numstr_len;
      bool is_keyframe = (mpv->flag & MOTIONPATH_VERT_KEY) != 0;

      if ((show_keyframes && show_keyframes_no && is_keyframe) || (show_frame_no && (i == 0))) {
        numstr_len = BLI_snprintf(numstr, sizeof(numstr), " %d", frame);
        DRW_text_cache_add(
            dt, mpv->co, numstr, numstr_len, 0, 0, txt_flag, (is_keyframe) ? col_kf : col);
      }
      else if (show_frame_no) {
        bMotionPathVert *mpvP = (mpv - stepsize);
        bMotionPathVert *mpvN = (mpv + stepsize);
        /* Only draw frame number 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 = BLI_snprintf(numstr, sizeof(numstr), " %d", frame);
          DRW_text_cache_add(dt, mpv->co, numstr, numstr_len, 0, 0, txt_flag, col);
        }
      }
    }
  }
}

void OVERLAY_motion_path_cache_populate(OVERLAY_Data *vedata, Object *ob)
{
  const DRWContextState *draw_ctx = DRW_context_state_get();

  if (ob->type == OB_ARMATURE) {
    if (OVERLAY_armature_is_pose_mode(ob, draw_ctx)) {
      LISTBASE_FOREACH (bPoseChannel *, pchan, &ob->pose->chanbase) {
        if (pchan->mpath) {
          motion_path_cache(vedata, ob, pchan, &ob->pose->avs, pchan->mpath);
        }
      }
    }
  }

  if (ob->mpath) {
    motion_path_cache(vedata, ob, NULL, &ob->avs, ob->mpath);
  }
}

void OVERLAY_motion_path_draw(OVERLAY_Data *vedata)
{
  OVERLAY_PassList *psl = vedata->psl;

  DRW_draw_pass(psl->motion_paths_ps);
}