Cleanup: blank lines over doxy headers
[blender.git] / source / blender / draw / intern / draw_anim_viz.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009/2018 by the Blender Foundation.
19  *
20  * Contributor(s): Joshua Leung
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/draw/intern/draw_anim_viz.c
26  *  \ingroup draw
27  */
28
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33
34 #include "BLI_sys_types.h"
35
36 #include "DNA_armature_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_view3d_types.h"
39 #include "DNA_object_types.h"
40
41 #include "BLI_math.h"
42
43 #include "UI_resources.h"
44
45 #include "DEG_depsgraph_query.h"
46
47 #include "DRW_engine.h"
48 #include "DRW_render.h"
49
50 #include "GPU_shader.h"
51 #include "GPU_immediate.h"
52
53 #include "draw_common.h"
54 #include "draw_manager_text.h"
55
56 /* ********************************* Lists ************************************** */
57 /* All lists are per viewport specific datas.
58  * They are all free when viewport changes engines
59  * or is free itself.
60  */
61
62 /* XXX: How to show frame numbers, etc.?  Currently only doing the dots and lines */
63 typedef struct MPATH_PassList {
64         struct DRWPass *lines;
65         struct DRWPass *points;
66 } MPATH_PassList;
67
68 typedef struct MPATH_StorageList {
69         struct MPATH_PrivateData *g_data;
70 } MPATH_StorageList;
71
72 typedef struct MPATH_Data {
73         void *engine_type;
74         DRWViewportEmptyList *fbl;
75         DRWViewportEmptyList *txl;
76         MPATH_PassList *psl;
77         MPATH_StorageList *stl;
78 } MPATH_Data;
79
80 #if 0
81 static struct {
82         GPUShader *mpath_line_sh;
83         GPUShader *mpath_points_sh;
84 } e_data = {0};
85 #endif
86
87 /* *************************** Path Cache *********************************** */
88
89 /* Just convert the CPU cache to GPU cache. */
90 static GPUVertBuf *mpath_vbo_get(bMotionPath *mpath)
91 {
92         if (!mpath->points_vbo) {
93                 GPUVertFormat format = {0};
94                 /* Match structure of bMotionPathVert. */
95                 uint pos = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
96                 GPU_vertformat_attr_add(&format, "flag", GPU_COMP_I32, 1, GPU_FETCH_INT);
97                 mpath->points_vbo = GPU_vertbuf_create_with_format(&format);
98                 GPU_vertbuf_data_alloc(mpath->points_vbo, mpath->length);
99
100                 /* meh... a useless memcpy. */
101                 GPUVertBufRaw raw_data;
102                 GPU_vertbuf_attr_get_raw_data(mpath->points_vbo, pos, &raw_data);
103                 memcpy(GPU_vertbuf_raw_step(&raw_data), mpath->points, sizeof(bMotionPathVert) * mpath->length);
104         }
105         return mpath->points_vbo;
106 }
107
108 static GPUBatch *mpath_batch_line_get(bMotionPath *mpath)
109 {
110         if (!mpath->batch_line) {
111                 mpath->batch_line = GPU_batch_create(GPU_PRIM_LINE_STRIP, mpath_vbo_get(mpath), NULL);
112         }
113         return mpath->batch_line;
114 }
115
116 static GPUBatch *mpath_batch_points_get(bMotionPath *mpath)
117 {
118         if (!mpath->batch_points) {
119                 mpath->batch_points = GPU_batch_create(GPU_PRIM_POINTS, mpath_vbo_get(mpath), NULL);
120         }
121         return mpath->batch_points;
122 }
123
124 /* *************************** Draw Engine Entrypoints ************************** */
125
126 static void MPATH_engine_init(void *UNUSED(vedata))
127 {
128 }
129
130 static void MPATH_engine_free(void)
131 {
132 }
133
134 /* Here init all passes and shading groups
135  * Assume that all Passes are NULL */
136 static void MPATH_cache_init(void *vedata)
137 {
138         MPATH_PassList *psl = ((MPATH_Data *)vedata)->psl;
139
140         {
141                 DRWState state = DRW_STATE_WRITE_COLOR;
142                 psl->lines = DRW_pass_create("Motionpath Line Pass", state);
143         }
144
145         {
146                 DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_POINT;
147                 psl->points = DRW_pass_create("Motionpath Point Pass", state);
148         }
149 }
150
151 static void MPATH_cache_motion_path(MPATH_PassList *psl,
152                                     Object *ob, bPoseChannel *pchan,
153                                     bAnimVizSettings *avs, bMotionPath *mpath)
154 {
155         const DRWContextState *draw_ctx = DRW_context_state_get();
156         struct DRWTextStore *dt = DRW_text_cache_ensure();
157         int txt_flag = DRW_TEXT_CACHE_GLOBALSPACE | DRW_TEXT_CACHE_ASCII;
158         int stepsize = avs->path_step;
159         int sfra, efra, sind, len;
160         int cfra = (int)DEG_get_ctime(draw_ctx->depsgraph);
161         bool sel = (pchan) ? (pchan->bone->flag & BONE_SELECTED) : (ob->flag & SELECT);
162         bool show_keyframes = (avs->path_viewflag & MOTIONPATH_VIEW_KFRAS) != 0;
163         bMotionPathVert *mpv, *mpv_start;
164
165         /* get frame ranges */
166         if (avs->path_type == MOTIONPATH_TYPE_ACFRA) {
167                 /* With "Around Current", we only choose frames from around
168                  * the current frame to draw.
169                  */
170                 sfra = cfra - avs->path_bc;
171                 efra = cfra + avs->path_ac + 1;
172         }
173         else {
174                 /* Use the current display range */
175                 sfra = avs->path_sf;
176                 efra = avs->path_ef;
177         }
178
179         /* no matter what, we can only show what is in the cache and no more
180          * - abort if whole range is past ends of path
181          * - otherwise clamp endpoints to extents of path
182          */
183         if (sfra < mpath->start_frame) {
184                 /* start clamp */
185                 sfra = mpath->start_frame;
186         }
187         if (efra > mpath->end_frame) {
188                 /* end clamp */
189                 efra = mpath->end_frame;
190         }
191
192         if ((sfra > mpath->end_frame) || (efra < mpath->start_frame)) {
193                 /* whole path is out of bounds */
194                 return;
195         }
196
197         len = efra - sfra;
198
199         if ((len <= 0) || (mpath->points == NULL)) {
200                 return;
201         }
202
203         sind = sfra - mpath->start_frame;
204         mpv_start = (mpath->points + sind);
205
206         bool use_custom_col = (mpath->flag & MOTIONPATH_FLAG_CUSTOM) != 0;
207
208         /* draw curve-line of path */
209         /* Draw lines only if line drawing option is enabled */
210         if (mpath->flag & MOTIONPATH_FLAG_LINES) {
211                 DRWShadingGroup *shgrp = DRW_shgroup_create(mpath_line_shader_get(), psl->lines);
212                 DRW_shgroup_uniform_int_copy(shgrp, "frameCurrent", cfra);
213                 DRW_shgroup_uniform_int_copy(shgrp, "frameStart", sfra);
214                 DRW_shgroup_uniform_int_copy(shgrp, "frameEnd", efra);
215                 DRW_shgroup_uniform_int_copy(shgrp, "cacheStart", mpath->start_frame);
216                 DRW_shgroup_uniform_int_copy(shgrp, "lineThickness", mpath->line_thickness);
217                 DRW_shgroup_uniform_bool_copy(shgrp, "selected", sel);
218                 DRW_shgroup_uniform_bool_copy(shgrp, "useCustomColor", use_custom_col);
219                 DRW_shgroup_uniform_vec2(shgrp, "viewportSize", DRW_viewport_size_get(), 1);
220                 DRW_shgroup_uniform_block(shgrp, "globalsBlock", G_draw.block_ubo);
221                 if (use_custom_col) {
222                         DRW_shgroup_uniform_vec3(shgrp, "customColor", mpath->color, 1);
223                 }
224                 /* Only draw the required range. */
225                 DRW_shgroup_call_range_add(shgrp, mpath_batch_line_get(mpath), NULL, sind, len);
226         }
227
228         /* Draw points. */
229         DRWShadingGroup *shgrp = DRW_shgroup_create(mpath_points_shader_get(), psl->points);
230         DRW_shgroup_uniform_int_copy(shgrp, "frameCurrent", cfra);
231         DRW_shgroup_uniform_int_copy(shgrp, "cacheStart", mpath->start_frame);
232         DRW_shgroup_uniform_int_copy(shgrp, "pointSize", mpath->line_thickness);
233         DRW_shgroup_uniform_int_copy(shgrp, "stepSize", stepsize);
234         DRW_shgroup_uniform_bool_copy(shgrp, "selected", sel);
235         DRW_shgroup_uniform_bool_copy(shgrp, "showKeyFrames", show_keyframes);
236         DRW_shgroup_uniform_bool_copy(shgrp, "useCustomColor", use_custom_col);
237         DRW_shgroup_uniform_block(shgrp, "globalsBlock", G_draw.block_ubo);
238         if (use_custom_col) {
239                 DRW_shgroup_uniform_vec3(shgrp, "customColor", mpath->color, 1);
240         }
241         /* Only draw the required range. */
242         DRW_shgroup_call_range_add(shgrp, mpath_batch_points_get(mpath), NULL, sind, len);
243
244         /* Draw frame numbers at each framestep value */
245         bool show_kf_no = (avs->path_viewflag & MOTIONPATH_VIEW_KFNOS) != 0;
246         if ((avs->path_viewflag & (MOTIONPATH_VIEW_FNUMS)) || (show_kf_no && show_keyframes)) {
247                 int i;
248                 uchar col[4], col_kf[4];
249                 UI_GetThemeColor3ubv(TH_TEXT_HI, col);
250                 UI_GetThemeColor3ubv(TH_VERTEX_SELECT, col_kf);
251                 col[3] = col_kf[3] = 255;
252
253                 for (i = 0, mpv = mpv_start; i < len; i += stepsize, mpv += stepsize) {
254                         int frame = sfra + i;
255                         char numstr[32];
256                         size_t numstr_len;
257                         bool is_keyframe = (mpv->flag & MOTIONPATH_VERT_KEY) != 0;
258
259                         if ((show_keyframes && show_kf_no && is_keyframe) ||
260                             ((avs->path_viewflag & MOTIONPATH_VIEW_FNUMS) && (i == 0)))
261                         {
262                                 numstr_len = sprintf(numstr, " %d", frame);
263                                 DRW_text_cache_add(dt, mpv->co, numstr, numstr_len, 0, 0, txt_flag, (is_keyframe) ? col_kf : col);
264                         }
265                         else if (avs->path_viewflag & MOTIONPATH_VIEW_FNUMS) {
266                                 bMotionPathVert *mpvP = (mpv - stepsize);
267                                 bMotionPathVert *mpvN = (mpv + stepsize);
268                                 /* only draw framenum if several consecutive highlighted points don't occur on same point */
269                                 if ((equals_v3v3(mpv->co, mpvP->co) == 0) || (equals_v3v3(mpv->co, mpvN->co) == 0)) {
270                                         numstr_len = sprintf(numstr, " %d", frame);
271                                         DRW_text_cache_add(dt, mpv->co, numstr, numstr_len, 0, 0, txt_flag, col);
272                                 }
273                         }
274                 }
275         }
276 }
277
278 /* Add geometry to shading groups. Execute for each objects */
279 static void MPATH_cache_populate(void *vedata, Object *ob)
280 {
281         MPATH_PassList *psl = ((MPATH_Data *)vedata)->psl;
282         const DRWContextState *draw_ctx = DRW_context_state_get();
283
284         if (draw_ctx->v3d->overlay.flag & V3D_OVERLAY_HIDE_MOTION_PATHS) {
285                 return;
286         }
287
288         if (ob->type == OB_ARMATURE) {
289                 if (DRW_pose_mode_armature(ob, draw_ctx->obact)) {
290                         for (bPoseChannel *pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
291                                 if (pchan->mpath) {
292                                         MPATH_cache_motion_path(psl, ob, pchan, &ob->pose->avs, pchan->mpath);
293                                 }
294                         }
295                 }
296         }
297         else {
298                 if (ob->mpath) {
299                         MPATH_cache_motion_path(psl, ob, NULL, &ob->avs, ob->mpath);
300                 }
301         }
302 }
303
304 /* Draw time! Control rendering pipeline from here */
305 static void MPATH_draw_scene(void *vedata)
306 {
307         MPATH_PassList *psl = ((MPATH_Data *)vedata)->psl;
308         DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
309         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
310
311         if (DRW_pass_is_empty(psl->lines) &&
312             DRW_pass_is_empty(psl->points))
313         {
314                 /* Nothing to draw. */
315                 return;
316         }
317
318         MULTISAMPLE_SYNC_ENABLE(dfbl, dtxl);
319
320         DRW_draw_pass(psl->lines);
321         DRW_draw_pass(psl->points);
322
323         MULTISAMPLE_SYNC_DISABLE_NO_DEPTH(dfbl, dtxl)
324 }
325
326 /* *************************** Draw Engine Defines ****************************** */
327
328 static const DrawEngineDataSize MPATH_data_size = DRW_VIEWPORT_DATA_SIZE(MPATH_Data);
329
330 DrawEngineType draw_engine_motion_path_type = {
331         NULL, NULL,
332         N_("MotionPath"),
333         &MPATH_data_size,
334         &MPATH_engine_init,
335         &MPATH_engine_free,
336         &MPATH_cache_init,
337         &MPATH_cache_populate,
338         NULL,
339         NULL,
340         &MPATH_draw_scene,
341         NULL,
342         NULL,
343 };