Merge branch 'master' into blender2.8
[blender.git] / source / blender / draw / intern / draw_armature.c
1 /*
2  * Copyright 2016, Blender Foundation.
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  * Contributor(s): Blender Institute
19  *
20  */
21
22 /** \file draw_armature.c
23  *  \ingroup draw
24  */
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <math.h>
29
30 #include "DNA_anim_types.h"
31 #include "DNA_armature_types.h"
32 #include "DNA_constraint_types.h"
33 #include "DNA_scene_types.h"
34 #include "DNA_screen_types.h"
35 #include "DNA_view3d_types.h"
36 #include "DNA_object_types.h"
37
38 #include "DRW_render.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_math.h"
42 #include "BLI_dlrbTree.h"
43 #include "BLI_utildefines.h"
44
45 #include "BKE_animsys.h"
46 #include "BKE_action.h"
47 #include "BKE_armature.h"
48 #include "BKE_global.h"
49 #include "BKE_modifier.h"
50 #include "BKE_nla.h"
51 #include "BKE_curve.h"
52
53 #include "BIF_gl.h"
54
55 #include "ED_armature.h"
56 #include "ED_keyframes_draw.h"
57
58 #include "GPU_select.h"
59
60 #include "UI_resources.h"
61
62 #include "draw_common.h"
63 #include "draw_manager_text.h"
64
65 #define BONE_VAR(eBone, pchan, var) ((eBone) ? (eBone->var) : (pchan->var))
66 #define BONE_FLAG(eBone, pchan) ((eBone) ? (eBone->flag) : (pchan->bone->flag))
67
68 #define PT_DEFAULT_RAD 0.05f /* radius of the point batch. */
69
70 /* For now just match 2.7x where possible. */
71 // #define USE_SOLID_COLOR
72
73 /* Reset for drawing each armature object */
74 static struct {
75         /* Current armature object */
76         Object *ob;
77         /* Reset when changing current_armature */
78         DRWShadingGroup *bone_octahedral_solid;
79         DRWShadingGroup *bone_octahedral_wire;
80         DRWShadingGroup *bone_octahedral_outline;
81         DRWShadingGroup *bone_box_solid;
82         DRWShadingGroup *bone_box_wire;
83         DRWShadingGroup *bone_box_outline;
84         DRWShadingGroup *bone_wire;
85         DRWShadingGroup *bone_stick;
86         DRWShadingGroup *bone_dof_sphere;
87         DRWShadingGroup *bone_dof_lines;
88         DRWShadingGroup *bone_envelope_solid;
89         DRWShadingGroup *bone_envelope_distance;
90         DRWShadingGroup *bone_envelope_wire;
91         DRWShadingGroup *bone_point_solid;
92         DRWShadingGroup *bone_point_wire;
93         DRWShadingGroup *bone_axes;
94         DRWShadingGroup *lines_relationship;
95         DRWShadingGroup *lines_ik;
96         DRWShadingGroup *lines_ik_no_target;
97         DRWShadingGroup *lines_ik_spline;
98
99         DRWArmaturePasses passes;
100
101         bool transparent;
102 } g_data = {NULL};
103
104
105 /**
106  * Follow `TH_*` naming except for mixed colors.
107  */
108 static struct {
109         float select_color[4];
110         float edge_select_color[4];
111         float bone_select_color[4];  /* tint */
112         float wire_color[4];
113         float wire_edit_color[4];
114         float bone_solid_color[4];
115         float bone_active_unselect_color[4];  /* mix */
116         float bone_pose_color[4];
117         float bone_pose_active_color[4];
118         float bone_pose_active_unselect_color[4];  /* mix */
119         float text_hi_color[4];
120         float text_color[4];
121         float vertex_select_color[4];
122         float vertex_color[4];
123
124         /* not a theme, this is an override */
125         const float *const_color;
126         float const_wire;
127 } g_theme;
128
129
130 /* -------------------------------------------------------------------- */
131
132 /** \name Shader Groups (DRW_shgroup)
133  * \{ */
134
135 /* Octahedral */
136 static void drw_shgroup_bone_octahedral(
137         const float (*bone_mat)[4],
138         const float bone_color[4], const float hint_color[4], const float outline_color[4])
139 {
140         if (g_data.bone_octahedral_outline == NULL) {
141                 struct GPUBatch *geom = DRW_cache_bone_octahedral_wire_get();
142                 g_data.bone_octahedral_outline = shgroup_instance_bone_shape_outline(g_data.passes.bone_outline, geom);
143         }
144         if (g_data.bone_octahedral_solid == NULL) {
145                 struct GPUBatch *geom = DRW_cache_bone_octahedral_get();
146                 g_data.bone_octahedral_solid = shgroup_instance_bone_shape_solid(g_data.passes.bone_solid, geom,
147                                                                                  g_data.transparent);
148         }
149         float final_bonemat[4][4];
150         mul_m4_m4m4(final_bonemat, g_data.ob->obmat, bone_mat);
151         DRW_shgroup_call_dynamic_add(g_data.bone_octahedral_solid, final_bonemat, bone_color, hint_color);
152         if (outline_color[3] > 0.0f) {
153                 DRW_shgroup_call_dynamic_add(g_data.bone_octahedral_outline, final_bonemat, outline_color);
154         }
155 }
156
157 /* Box / B-Bone */
158 static void drw_shgroup_bone_box(
159         const float (*bone_mat)[4],
160         const float bone_color[4], const float hint_color[4], const float outline_color[4])
161 {
162         if (g_data.bone_box_wire == NULL) {
163                 struct GPUBatch *geom = DRW_cache_bone_box_wire_get();
164                 g_data.bone_box_outline = shgroup_instance_bone_shape_outline(g_data.passes.bone_outline, geom);
165         }
166         if (g_data.bone_box_solid == NULL) {
167                 struct GPUBatch *geom = DRW_cache_bone_box_get();
168                 g_data.bone_box_solid = shgroup_instance_bone_shape_solid(g_data.passes.bone_solid, geom, g_data.transparent);
169         }
170         float final_bonemat[4][4];
171         mul_m4_m4m4(final_bonemat, g_data.ob->obmat, bone_mat);
172         DRW_shgroup_call_dynamic_add(g_data.bone_box_solid, final_bonemat, bone_color, hint_color);
173         if (outline_color[3] > 0.0f) {
174                 DRW_shgroup_call_dynamic_add(g_data.bone_box_outline, final_bonemat, outline_color);
175         }
176 }
177
178 /* Wire */
179 static void drw_shgroup_bone_wire(const float (*bone_mat)[4], const float color[4])
180 {
181         if (g_data.bone_wire == NULL) {
182                 g_data.bone_wire = shgroup_dynlines_flat_color(g_data.passes.bone_wire);
183         }
184         float head[3], tail[3];
185         mul_v3_m4v3(head, g_data.ob->obmat, bone_mat[3]);
186         DRW_shgroup_call_dynamic_add(g_data.bone_wire, head, color);
187
188         add_v3_v3v3(tail, bone_mat[3], bone_mat[1]);
189         mul_m4_v3(g_data.ob->obmat, tail);
190         DRW_shgroup_call_dynamic_add(g_data.bone_wire, tail, color);
191 }
192
193 /* Stick */
194 static void drw_shgroup_bone_stick(
195         const float (*bone_mat)[4],
196         const float col_wire[4], const float col_bone[4], const float col_head[4], const float col_tail[4])
197 {
198         if (g_data.bone_stick == NULL) {
199                 g_data.bone_stick = shgroup_instance_bone_stick(g_data.passes.bone_wire);
200         }
201         float final_bonemat[4][4], tail[4];
202         mul_m4_m4m4(final_bonemat, g_data.ob->obmat, bone_mat);
203         add_v3_v3v3(tail, final_bonemat[3], final_bonemat[1]);
204         DRW_shgroup_call_dynamic_add(g_data.bone_stick, final_bonemat[3], tail, col_wire, col_bone, col_head, col_tail);
205 }
206
207
208 /* Envelope */
209 static void drw_shgroup_bone_envelope_distance(
210         const float (*bone_mat)[4],
211         const float *radius_head, const float *radius_tail, const float *distance)
212 {
213         if (g_data.passes.bone_envelope != NULL) {
214                 if (g_data.bone_envelope_distance == NULL) {
215                         g_data.bone_envelope_distance = shgroup_instance_bone_envelope_distance(g_data.passes.bone_envelope);
216                         /* passes.bone_envelope should have the DRW_STATE_CULL_FRONT state enabled. */
217                 }
218                 float head_sphere[4] = {0.0f, 0.0f, 0.0f, 1.0f}, tail_sphere[4] = {0.0f, 1.0f, 0.0f, 1.0f};
219                 float final_bonemat[4][4];
220                 mul_m4_m4m4(final_bonemat, g_data.ob->obmat, bone_mat);
221                 /* We need matrix mul because we need shear applied. */
222                 /* NOTE: could be done in shader if that becomes a bottleneck. */
223                 mul_m4_v4(final_bonemat, head_sphere);
224                 mul_m4_v4(final_bonemat, tail_sphere);
225                 head_sphere[3]  = *radius_head;
226                 head_sphere[3] += *distance;
227                 tail_sphere[3]  = *radius_tail;
228                 tail_sphere[3] += *distance;
229                 DRW_shgroup_call_dynamic_add(g_data.bone_envelope_distance, head_sphere, tail_sphere, final_bonemat[0]);
230         }
231 }
232
233 static void drw_shgroup_bone_envelope(
234         const float (*bone_mat)[4],
235         const float bone_color[4], const float hint_color[4], const float outline_color[4],
236         const float *radius_head, const float *radius_tail)
237 {
238         if (g_data.bone_point_wire == NULL) {
239                 g_data.bone_point_wire = shgroup_instance_bone_sphere_outline(g_data.passes.bone_wire);
240         }
241         if (g_data.bone_point_solid == NULL) {
242                 g_data.bone_point_solid = shgroup_instance_bone_sphere_solid(g_data.passes.bone_solid, g_data.transparent);
243         }
244         if (g_data.bone_envelope_wire == NULL) {
245                 g_data.bone_envelope_wire = shgroup_instance_bone_envelope_outline(g_data.passes.bone_wire);
246         }
247         if (g_data.bone_envelope_solid == NULL) {
248                 g_data.bone_envelope_solid = shgroup_instance_bone_envelope_solid(g_data.passes.bone_solid, g_data.transparent);
249                 /* We can have a lot of overdraw if we don't do this. Also envelope are not subject to
250                  * inverted matrix. */
251                 DRW_shgroup_state_enable(g_data.bone_envelope_solid, DRW_STATE_CULL_BACK);
252         }
253
254         float head_sphere[4] = {0.0f, 0.0f, 0.0f, 1.0f}, tail_sphere[4] = {0.0f, 1.0f, 0.0f, 1.0f};
255         float final_bonemat[4][4];
256         mul_m4_m4m4(final_bonemat, g_data.ob->obmat, bone_mat);
257         mul_m4_v4(final_bonemat, head_sphere);
258         mul_m4_v4(final_bonemat, tail_sphere);
259         head_sphere[3] = *radius_head;
260         tail_sphere[3] = *radius_tail;
261
262         if (head_sphere[3] < 0.0f) {
263                 /* Draw Tail only */
264                 float tmp[4][4] = {{0.0f}};
265                 tmp[0][0] = tmp[1][1] = tmp[2][2] = tail_sphere[3] / PT_DEFAULT_RAD;
266                 tmp[3][3] = 1.0f;
267                 copy_v3_v3(tmp[3], tail_sphere);
268                 DRW_shgroup_call_dynamic_add(g_data.bone_point_solid, tmp, bone_color, hint_color);
269                 if (outline_color[3] > 0.0f) {
270                         DRW_shgroup_call_dynamic_add(g_data.bone_point_wire, tmp, outline_color);
271                 }
272         }
273         else if (tail_sphere[3] < 0.0f) {
274                 /* Draw Head only */
275                 float tmp[4][4] = {{0.0f}};
276                 tmp[0][0] = tmp[1][1] = tmp[2][2] = head_sphere[3] / PT_DEFAULT_RAD;
277                 tmp[3][3] = 1.0f;
278                 copy_v3_v3(tmp[3], head_sphere);
279                 DRW_shgroup_call_dynamic_add(g_data.bone_point_solid, tmp, bone_color, hint_color);
280                 if (outline_color[3] > 0.0f) {
281                         DRW_shgroup_call_dynamic_add(g_data.bone_point_wire, tmp, outline_color);
282                 }
283         }
284         else {
285                 /* Draw Body */
286                 float tmp_sphere[4];
287                 float len = len_v3v3(tail_sphere, head_sphere);
288                 float fac_head = (len - head_sphere[3]) / len;
289                 float fac_tail = (len - tail_sphere[3]) / len;
290
291                 /* Small epsilon to avoid problem with float precision in shader. */
292                 if (len > (tail_sphere[3] + head_sphere[3]) + 1e-8f) {
293
294                         copy_v4_v4(tmp_sphere, head_sphere);
295                         interp_v4_v4v4(head_sphere, tail_sphere, head_sphere, fac_head);
296                         interp_v4_v4v4(tail_sphere, tmp_sphere,  tail_sphere, fac_tail);
297                         DRW_shgroup_call_dynamic_add(
298                                 g_data.bone_envelope_solid, head_sphere, tail_sphere, bone_color, hint_color, final_bonemat[0]);
299                         if (outline_color[3] > 0.0f) {
300                                 DRW_shgroup_call_dynamic_add(
301                                         g_data.bone_envelope_wire, head_sphere, tail_sphere, outline_color, final_bonemat[0]);
302                         }
303                 }
304                 else {
305                         float tmp[4][4] = {{0.0f}};
306                         float fac = max_ff(fac_head, 1.0f - fac_tail);
307                         interp_v4_v4v4(tmp_sphere, tail_sphere, head_sphere, clamp_f(fac, 0.0f, 1.0f));
308                         tmp[0][0] = tmp[1][1] = tmp[2][2] = tmp_sphere[3] / PT_DEFAULT_RAD;
309                         tmp[3][3] = 1.0f;
310                         copy_v3_v3(tmp[3], tmp_sphere);
311                         DRW_shgroup_call_dynamic_add(g_data.bone_point_solid, tmp, bone_color, hint_color);
312                         if (outline_color[3] > 0.0f) {
313                                 DRW_shgroup_call_dynamic_add(g_data.bone_point_wire, tmp, outline_color);
314                         }
315                 }
316         }
317 }
318
319 /* Custom (geometry) */
320
321 static void drw_shgroup_bone_custom_solid(
322         const float (*bone_mat)[4],
323         const float bone_color[4], const float hint_color[4], const float outline_color[4],
324         Object *custom)
325 {
326         /* grr, not re-using instances! */
327         struct GPUBatch *surf = DRW_cache_object_surface_get(custom);
328         struct GPUBatch *edges = DRW_cache_object_edge_detection_get(custom, NULL);
329         struct GPUBatch *ledges = DRW_cache_object_loose_edges_get(custom);
330         float final_bonemat[4][4];
331
332         if (surf || edges || ledges) {
333                 mul_m4_m4m4(final_bonemat, g_data.ob->obmat, bone_mat);
334         }
335
336         if (surf) {
337                 DRWShadingGroup *shgrp_geom_solid = shgroup_instance_bone_shape_solid(g_data.passes.bone_solid, surf,
338                                                                                       g_data.transparent);
339                 DRW_shgroup_call_dynamic_add(shgrp_geom_solid, final_bonemat, bone_color, hint_color);
340         }
341
342         if (edges && outline_color[3] > 0.0f) {
343                 DRWShadingGroup *shgrp_geom_wire = shgroup_instance_bone_shape_outline(g_data.passes.bone_outline, edges);
344                 DRW_shgroup_call_dynamic_add(shgrp_geom_wire, final_bonemat, outline_color);
345         }
346
347         if (ledges) {
348                 DRWShadingGroup *shgrp_geom_ledges = shgroup_instance_wire(g_data.passes.bone_wire, ledges);
349                 float final_color[4];
350                 copy_v3_v3(final_color, outline_color);
351                 final_color[3] = 1.0f; /* hack */
352                 DRW_shgroup_call_dynamic_add(shgrp_geom_ledges, final_bonemat, final_color);
353         }
354 }
355
356 static void drw_shgroup_bone_custom_wire(
357         const float (*bone_mat)[4],
358         const float color[4], Object *custom)
359 {
360         /* grr, not re-using instances! */
361         struct GPUBatch *geom = DRW_cache_object_wire_outline_get(custom);
362         if (geom) {
363                 DRWShadingGroup *shgrp_geom_wire = shgroup_instance_wire(g_data.passes.bone_wire, geom);
364                 float final_bonemat[4][4], final_color[4];
365                 mul_m4_m4m4(final_bonemat, g_data.ob->obmat, bone_mat);
366                 copy_v3_v3(final_color, color);
367                 final_color[3] = 1.0f; /* hack */
368                 DRW_shgroup_call_dynamic_add(shgrp_geom_wire, final_bonemat, final_color);
369         }
370 }
371
372 /* Head and tail sphere */
373 static void drw_shgroup_bone_point(
374         const float (*bone_mat)[4],
375         const float bone_color[4], const float hint_color[4], const float outline_color[4])
376 {
377         if (g_data.bone_point_wire == NULL) {
378                 g_data.bone_point_wire = shgroup_instance_bone_sphere_outline(g_data.passes.bone_wire);
379         }
380         if (g_data.bone_point_solid == NULL) {
381                 g_data.bone_point_solid = shgroup_instance_bone_sphere_solid(g_data.passes.bone_solid, g_data.transparent);
382         }
383         float final_bonemat[4][4];
384         mul_m4_m4m4(final_bonemat, g_data.ob->obmat, bone_mat);
385         DRW_shgroup_call_dynamic_add(g_data.bone_point_solid, final_bonemat, bone_color, hint_color);
386         if (outline_color[3] > 0.0f) {
387                 DRW_shgroup_call_dynamic_add(g_data.bone_point_wire, final_bonemat, outline_color);
388         }
389 }
390
391 /* Axes */
392 static void drw_shgroup_bone_axes(const float (*bone_mat)[4], const float color[4])
393 {
394         if (g_data.bone_axes == NULL) {
395                 g_data.bone_axes = shgroup_instance_bone_axes(g_data.passes.bone_axes);
396         }
397         float final_bonemat[4][4];
398         mul_m4_m4m4(final_bonemat, g_data.ob->obmat, bone_mat);
399         DRW_shgroup_call_dynamic_add(g_data.bone_axes, final_bonemat, color);
400 }
401
402 /* Relationship lines */
403 static void drw_shgroup_bone_relationship_lines(const float start[3], const float end[3])
404 {
405         if (g_data.lines_relationship == NULL) {
406                 g_data.lines_relationship = shgroup_dynlines_dashed_uniform_color(
407                         g_data.passes.relationship_lines, g_theme.wire_color);
408         }
409         /* reverse order to have less stipple overlap */
410         float v[3];
411         mul_v3_m4v3(v, g_data.ob->obmat, end);
412         DRW_shgroup_call_dynamic_add(g_data.lines_relationship, v);
413         mul_v3_m4v3(v, g_data.ob->obmat, start);
414         DRW_shgroup_call_dynamic_add(g_data.lines_relationship, v);
415 }
416
417 static void drw_shgroup_bone_ik_lines(const float start[3], const float end[3])
418 {
419         if (g_data.lines_ik == NULL) {
420                 static float fcolor[4] = {0.8f, 0.5f, 0.0f, 1.0f};  /* add theme! */
421                 g_data.lines_ik = shgroup_dynlines_dashed_uniform_color(g_data.passes.relationship_lines, fcolor);
422         }
423         /* reverse order to have less stipple overlap */
424         float v[3];
425         mul_v3_m4v3(v, g_data.ob->obmat, end);
426         DRW_shgroup_call_dynamic_add(g_data.lines_ik, v);
427         mul_v3_m4v3(v, g_data.ob->obmat, start);
428         DRW_shgroup_call_dynamic_add(g_data.lines_ik, v);
429 }
430
431 static void drw_shgroup_bone_ik_no_target_lines(const float start[3], const float end[3])
432 {
433         if (g_data.lines_ik_no_target == NULL) {
434                 static float fcolor[4] = {0.8f, 0.8f, 0.2f, 1.0f};  /* add theme! */
435                 g_data.lines_ik_no_target = shgroup_dynlines_dashed_uniform_color(g_data.passes.relationship_lines, fcolor);
436         }
437         /* reverse order to have less stipple overlap */
438         float v[3];
439         mul_v3_m4v3(v, g_data.ob->obmat, end);
440         DRW_shgroup_call_dynamic_add(g_data.lines_ik_no_target, v);
441         mul_v3_m4v3(v, g_data.ob->obmat, start);
442         DRW_shgroup_call_dynamic_add(g_data.lines_ik_no_target, v);
443 }
444
445 static void drw_shgroup_bone_ik_spline_lines(const float start[3], const float end[3])
446 {
447         if (g_data.lines_ik_spline == NULL) {
448                 static float fcolor[4] = {0.8f, 0.8f, 0.2f, 1.0f};  /* add theme! */
449                 g_data.lines_ik_spline = shgroup_dynlines_dashed_uniform_color(g_data.passes.relationship_lines, fcolor);
450         }
451         /* reverse order to have less stipple overlap */
452         float v[3];
453         mul_v3_m4v3(v, g_data.ob->obmat, end);
454         DRW_shgroup_call_dynamic_add(g_data.lines_ik_spline, v);
455         mul_v3_m4v3(v, g_data.ob->obmat, start);
456         DRW_shgroup_call_dynamic_add(g_data.lines_ik_spline, v);
457 }
458
459 /** \} */
460
461
462 /* -------------------------------------------------------------------- */
463
464 /** \name Drawing Theme Helpers
465  *
466  * Note, this section is duplicate of code in 'drawarmature.c'.
467  *
468  * \{ */
469
470 /* global here is reset before drawing each bone */
471 static struct {
472         const ThemeWireColor *bcolor;
473 } g_color;
474
475 /* values of colCode for set_pchan_color */
476 enum {
477         PCHAN_COLOR_NORMAL  = 0,        /* normal drawing */
478         PCHAN_COLOR_SOLID,              /* specific case where "solid" color is needed */
479         PCHAN_COLOR_CONSTS,             /* "constraint" colors (which may/may-not be suppressed) */
480
481         PCHAN_COLOR_SPHEREBONE_BASE,    /* for the 'stick' of sphere (envelope) bones */
482         PCHAN_COLOR_SPHEREBONE_END,     /* for the ends of sphere (envelope) bones */
483         PCHAN_COLOR_LINEBONE            /* for the middle of line-bones */
484 };
485
486 /* This function sets the color-set for coloring a certain bone */
487 static void set_pchan_colorset(Object *ob, bPoseChannel *pchan)
488 {
489         bPose *pose = (ob) ? ob->pose : NULL;
490         bArmature *arm = (ob) ? ob->data : NULL;
491         bActionGroup *grp = NULL;
492         short color_index = 0;
493
494         /* sanity check */
495         if (ELEM(NULL, ob, arm, pose, pchan)) {
496                 g_color.bcolor = NULL;
497                 return;
498         }
499
500         /* only try to set custom color if enabled for armature */
501         if (arm->flag & ARM_COL_CUSTOM) {
502                 /* currently, a bone can only use a custom color set if it's group (if it has one),
503                  * has been set to use one
504                  */
505                 if (pchan->agrp_index) {
506                         grp = (bActionGroup *)BLI_findlink(&pose->agroups, (pchan->agrp_index - 1));
507                         if (grp)
508                                 color_index = grp->customCol;
509                 }
510         }
511
512         /* bcolor is a pointer to the color set to use. If NULL, then the default
513          * color set (based on the theme colors for 3d-view) is used.
514          */
515         if (color_index > 0) {
516                 bTheme *btheme = UI_GetTheme();
517                 g_color.bcolor = &btheme->tarm[(color_index - 1)];
518         }
519         else if (color_index == -1) {
520                 /* use the group's own custom color set (grp is always != NULL here) */
521                 g_color.bcolor = &grp->cs;
522         }
523         else {
524                 g_color.bcolor = NULL;
525         }
526 }
527
528 /* This function is for brightening/darkening a given color (like UI_GetThemeColorShade3ubv()) */
529 static void cp_shade_color3ub(uchar cp[3], const int offset)
530 {
531         int r, g, b;
532
533         r = offset + (int) cp[0];
534         CLAMP(r, 0, 255);
535         g = offset + (int) cp[1];
536         CLAMP(g, 0, 255);
537         b = offset + (int) cp[2];
538         CLAMP(b, 0, 255);
539
540         cp[0] = r;
541         cp[1] = g;
542         cp[2] = b;
543 }
544
545 static void cp_shade_color3f(float cp[3], const float offset)
546 {
547         add_v3_fl(cp, offset);
548         CLAMP(cp[0], 0, 255);
549         CLAMP(cp[1], 0, 255);
550         CLAMP(cp[2], 0, 255);
551 }
552
553
554 /* This function sets the gl-color for coloring a certain bone (based on bcolor) */
555 static bool set_pchan_color(short colCode, const int boneflag, const short constflag, float r_color[4])
556 {
557         float *fcolor = r_color;
558         const ThemeWireColor *bcolor = g_color.bcolor;
559
560         switch (colCode) {
561                 case PCHAN_COLOR_NORMAL:
562                 {
563                         if (bcolor) {
564                                 uchar cp[4] = {255};
565
566                                 if (boneflag & BONE_DRAW_ACTIVE) {
567                                         copy_v3_v3_char((char *)cp, bcolor->active);
568                                         if (!(boneflag & BONE_SELECTED)) {
569                                                 cp_shade_color3ub(cp, -80);
570                                         }
571                                 }
572                                 else if (boneflag & BONE_SELECTED) {
573                                         copy_v3_v3_char((char *)cp, bcolor->select);
574                                 }
575                                 else {
576                                         /* a bit darker than solid */
577                                         copy_v3_v3_char((char *)cp, bcolor->solid);
578                                         cp_shade_color3ub(cp, -50);
579                                 }
580
581                                 rgb_uchar_to_float(fcolor, cp);
582                         }
583                         else {
584                                 if ((boneflag & BONE_DRAW_ACTIVE) && (boneflag & BONE_SELECTED)) {
585                                         UI_GetThemeColor4fv(TH_BONE_POSE_ACTIVE, fcolor);
586                                 }
587                                 else if (boneflag & BONE_DRAW_ACTIVE) {
588                                         UI_GetThemeColorBlendShade4fv(TH_WIRE, TH_BONE_POSE, 0.15f, 0, fcolor);
589                                 }
590                                 else if (boneflag & BONE_SELECTED) {
591                                         UI_GetThemeColor4fv(TH_BONE_POSE, fcolor);
592                                 }
593                                 else {
594                                         UI_GetThemeColor4fv(TH_WIRE, fcolor);
595                                 }
596                         }
597
598                         return true;
599                 }
600                 case PCHAN_COLOR_SOLID:
601                 {
602                         UI_GetThemeColor4fv(TH_BONE_SOLID, fcolor);
603
604                         if (bcolor) {
605                                 float solid_bcolor[3];
606                                 rgb_uchar_to_float(solid_bcolor, (uchar *)bcolor->solid);
607                                 interp_v3_v3v3(fcolor, fcolor, solid_bcolor, 1.0f);
608                         }
609
610                         return true;
611                 }
612                 case PCHAN_COLOR_CONSTS:
613                 {
614                         if ((bcolor == NULL) || (bcolor->flag & TH_WIRECOLOR_CONSTCOLS)) {
615                                 uchar cp[4];
616                                 if (constflag & PCHAN_HAS_TARGET) rgba_char_args_set((char *)cp, 255, 150, 0, 80);
617                                 else if (constflag & PCHAN_HAS_IK) rgba_char_args_set((char *)cp, 255, 255, 0, 80);
618                                 else if (constflag & PCHAN_HAS_SPLINEIK) rgba_char_args_set((char *)cp, 200, 255, 0, 80);
619                                 else if (constflag & PCHAN_HAS_CONST) rgba_char_args_set((char *)cp, 0, 255, 120, 80);
620                                 else {
621                                         return false;
622                                 }
623
624                                 rgba_uchar_to_float(fcolor, cp);
625
626                                 return true;
627                         }
628                         return false;
629                 }
630                 case PCHAN_COLOR_SPHEREBONE_BASE:
631                 {
632                         if (bcolor) {
633                                 uchar cp[4] = {255};
634
635                                 if (boneflag & BONE_DRAW_ACTIVE) {
636                                         copy_v3_v3_char((char *)cp, bcolor->active);
637                                 }
638                                 else if (boneflag & BONE_SELECTED) {
639                                         copy_v3_v3_char((char *)cp, bcolor->select);
640                                 }
641                                 else {
642                                         copy_v3_v3_char((char *)cp, bcolor->solid);
643                                 }
644
645                                 rgb_uchar_to_float(fcolor, cp);
646                         }
647                         else {
648                                 if (boneflag & BONE_DRAW_ACTIVE) {
649                                         UI_GetThemeColorShade4fv(TH_BONE_POSE, 40, fcolor);
650                                 }
651                                 else if (boneflag & BONE_SELECTED) {
652                                         UI_GetThemeColor4fv(TH_BONE_POSE, fcolor);
653                                 }
654                                 else {
655                                         UI_GetThemeColor4fv(TH_BONE_SOLID, fcolor);
656                                 }
657                         }
658
659                         return true;
660                 }
661                 case PCHAN_COLOR_SPHEREBONE_END:
662                 {
663                         if (bcolor) {
664                                 uchar cp[4] = {255};
665
666                                 if (boneflag & BONE_DRAW_ACTIVE) {
667                                         copy_v3_v3_char((char *)cp, bcolor->active);
668                                         cp_shade_color3ub(cp, 10);
669                                 }
670                                 else if (boneflag & BONE_SELECTED) {
671                                         copy_v3_v3_char((char *)cp, bcolor->select);
672                                         cp_shade_color3ub(cp, -30);
673                                 }
674                                 else {
675                                         copy_v3_v3_char((char *)cp, bcolor->solid);
676                                         cp_shade_color3ub(cp, -30);
677                                 }
678
679                                 rgb_uchar_to_float(fcolor, cp);
680                         }
681                         else {
682                                 if (boneflag & BONE_DRAW_ACTIVE) {
683                                         UI_GetThemeColorShade4fv(TH_BONE_POSE, 10, fcolor);
684                                 }
685                                 else if (boneflag & BONE_SELECTED) {
686                                         UI_GetThemeColorShade4fv(TH_BONE_POSE, -30, fcolor);
687                                 }
688                                 else {
689                                         UI_GetThemeColorShade4fv(TH_BONE_SOLID, -30, fcolor);
690                                 }
691                         }
692                         break;
693                 }
694                 case PCHAN_COLOR_LINEBONE:
695                 {
696                         /* inner part in background color or constraint */
697                         if ((constflag) && ((bcolor == NULL) || (bcolor->flag & TH_WIRECOLOR_CONSTCOLS))) {
698                                 uchar cp[4];
699                                 if (constflag & PCHAN_HAS_TARGET) rgba_char_args_set((char *)cp, 255, 150, 0, 255);
700                                 else if (constflag & PCHAN_HAS_IK) rgba_char_args_set((char *)cp, 255, 255, 0, 255);
701                                 else if (constflag & PCHAN_HAS_SPLINEIK) rgba_char_args_set((char *)cp, 200, 255, 0, 255);
702                                 else if (constflag & PCHAN_HAS_CONST) rgba_char_args_set((char *)cp, 0, 255, 120, 255);
703                                 else if (constflag) UI_GetThemeColor4ubv(TH_BONE_POSE, cp);  /* PCHAN_HAS_ACTION */
704
705                                 rgb_uchar_to_float(fcolor, cp);
706                         }
707                         else {
708                                 if (bcolor) {
709                                         const char *cp = bcolor->solid;
710                                         rgb_uchar_to_float(fcolor, (uchar *)cp);
711                                         fcolor[3] = 204.f / 255.f;
712                                 }
713                                 else {
714                                         UI_GetThemeColorShade4fv(TH_BACK, -30, fcolor);
715                                 }
716                         }
717
718                         return true;
719                 }
720         }
721
722         return false;
723 }
724
725 /** \} */
726
727
728 /* -------------------------------------------------------------------- */
729
730 /** \name Drawing Color Helpers
731  * \{ */
732
733 /** See: 'set_pchan_color'*/
734 static void update_color(const Object *ob, const float const_color[4])
735 {
736         g_theme.const_color = const_color;
737         g_theme.const_wire = ((ob->base_flag & BASE_SELECTED) != 0) ? 1.5f : 0.0f;
738
739 #define NO_ALPHA(c) (((c)[3] = 1.0f), (c))
740
741         UI_GetThemeColor3fv(TH_SELECT, NO_ALPHA(g_theme.select_color));
742         UI_GetThemeColor3fv(TH_EDGE_SELECT, NO_ALPHA(g_theme.edge_select_color));
743         UI_GetThemeColorShade3fv(TH_EDGE_SELECT, -20, NO_ALPHA(g_theme.bone_select_color));
744         UI_GetThemeColor3fv(TH_WIRE, NO_ALPHA(g_theme.wire_color));
745         UI_GetThemeColor3fv(TH_WIRE_EDIT, NO_ALPHA(g_theme.wire_edit_color));
746         UI_GetThemeColor3fv(TH_BONE_SOLID, NO_ALPHA(g_theme.bone_solid_color));
747         UI_GetThemeColorBlendShade3fv(TH_WIRE_EDIT, TH_EDGE_SELECT, 0.15f, 0, NO_ALPHA(g_theme.bone_active_unselect_color));
748         UI_GetThemeColor3fv(TH_BONE_POSE, NO_ALPHA(g_theme.bone_pose_color));
749         UI_GetThemeColor3fv(TH_BONE_POSE_ACTIVE, NO_ALPHA(g_theme.bone_pose_active_color));
750         UI_GetThemeColorBlendShade3fv(TH_WIRE, TH_BONE_POSE, 0.15f, 0, NO_ALPHA(g_theme.bone_pose_active_unselect_color));
751         UI_GetThemeColor3fv(TH_TEXT_HI, NO_ALPHA(g_theme.text_hi_color));
752         UI_GetThemeColor3fv(TH_TEXT, NO_ALPHA(g_theme.text_color));
753         UI_GetThemeColor3fv(TH_VERTEX_SELECT, NO_ALPHA(g_theme.vertex_select_color));
754         UI_GetThemeColor3fv(TH_VERTEX, NO_ALPHA(g_theme.vertex_color));
755
756 #undef NO_ALPHA
757 }
758
759 static const float *get_bone_solid_color(
760         const EditBone *UNUSED(eBone), const bPoseChannel *pchan, const bArmature *arm,
761         const int boneflag, const short constflag)
762 {
763         if (g_theme.const_color)
764                 return g_theme.bone_solid_color;
765
766         if (arm->flag & ARM_POSEMODE) {
767                 static float disp_color[4];
768                 copy_v4_v4(disp_color, pchan->draw_data->solid_color);
769                 set_pchan_color(PCHAN_COLOR_SOLID, boneflag, constflag, disp_color);
770                 return disp_color;
771         }
772
773         return g_theme.bone_solid_color;
774 }
775
776 static const float *get_bone_solid_with_consts_color(
777         const EditBone *eBone, const bPoseChannel *pchan, const bArmature *arm,
778         const int boneflag, const short constflag)
779 {
780         if (g_theme.const_color)
781                 return g_theme.bone_solid_color;
782
783         const float *col = get_bone_solid_color(eBone, pchan, arm, boneflag, constflag);
784
785         static float consts_color[4];
786         if (set_pchan_color(PCHAN_COLOR_CONSTS, boneflag, constflag, consts_color)) {
787                 interp_v3_v3v3(consts_color, col, consts_color, 0.5f);
788         }
789         else {
790                 copy_v4_v4(consts_color, col);
791         }
792         return consts_color;
793 }
794
795 static float get_bone_wire_thickness(int boneflag)
796 {
797         if (g_theme.const_color)
798                 return g_theme.const_wire;
799         else if (boneflag & (BONE_DRAW_ACTIVE | BONE_SELECTED))
800                 return 2.0f;
801         else
802                 return 1.0f;
803 }
804
805 static const float *get_bone_wire_color(
806         const EditBone *eBone, const bPoseChannel *pchan, const bArmature *arm,
807         const int boneflag, const short constflag)
808 {
809         static float disp_color[4];
810
811         if (g_theme.const_color) {
812                 copy_v3_v3(disp_color, g_theme.const_color);
813         }
814         else if (eBone) {
815                 if (boneflag & BONE_SELECTED) {
816                         if (boneflag & BONE_DRAW_ACTIVE) {
817                                 copy_v3_v3(disp_color, g_theme.edge_select_color);
818                         }
819                         else {
820                                 copy_v3_v3(disp_color, g_theme.bone_select_color);
821                         }
822                 }
823                 else {
824                         if (boneflag & BONE_DRAW_ACTIVE) {
825                                 copy_v3_v3(disp_color, g_theme.bone_active_unselect_color);
826                         }
827                         else {
828                                 copy_v3_v3(disp_color, g_theme.wire_edit_color);
829                         }
830                 }
831         }
832         else if (arm->flag & ARM_POSEMODE) {
833                 copy_v4_v4(disp_color, pchan->draw_data->wire_color);
834                 set_pchan_color(PCHAN_COLOR_NORMAL, boneflag, constflag, disp_color);
835         }
836         else {
837                 copy_v3_v3(disp_color, g_theme.vertex_color);
838         }
839
840         disp_color[3] = get_bone_wire_thickness(boneflag);
841
842         return disp_color;
843 }
844
845 #define HINT_MUL 0.5f
846 #define HINT_SHADE 0.2f
847
848 static void bone_hint_color_shade(float hint_color[4], const float color[4])
849 {
850         mul_v3_v3fl(hint_color, color, HINT_MUL);
851         cp_shade_color3f(hint_color, -HINT_SHADE);
852         hint_color[3] = 1.0f;
853 }
854
855 static const float *get_bone_hint_color(
856         const EditBone *eBone, const bPoseChannel *pchan, const bArmature *arm,
857         const int boneflag, const short constflag)
858 {
859         static float hint_color[4] = {0.0f, 0.0f, 0.0f, 1.0f};
860
861         if (g_theme.const_color) {
862                 bone_hint_color_shade(hint_color, g_theme.bone_solid_color);
863         }
864         else {
865                 const float *wire_color = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
866                 bone_hint_color_shade(hint_color, wire_color);
867         }
868
869         return hint_color;
870 }
871
872 /** \} */
873
874
875 /* -------------------------------------------------------------------- */
876
877 /** \name Helper Utils
878  * \{ */
879
880 static void pchan_draw_data_init(bPoseChannel *pchan)
881 {
882         if (pchan->draw_data != NULL) {
883                 if (pchan->draw_data->bbone_matrix_len != pchan->bone->segments) {
884                         MEM_SAFE_FREE(pchan->draw_data);
885                 }
886         }
887
888         if (pchan->draw_data == NULL) {
889                 pchan->draw_data = MEM_mallocN(sizeof(*pchan->draw_data) + sizeof(Mat4) * pchan->bone->segments, __func__);
890                 pchan->draw_data->bbone_matrix_len = pchan->bone->segments;
891         }
892 }
893
894 static void draw_bone_update_disp_matrix_default(EditBone *eBone, bPoseChannel *pchan)
895 {
896         float s[4][4], ebmat[4][4];
897         float length;
898         float (*bone_mat)[4];
899         float (*disp_mat)[4];
900         float (*disp_tail_mat)[4];
901
902         /* TODO : This should be moved to depsgraph or armature refresh
903          * and not be tight to the draw pass creation.
904          * This would refresh armature without invalidating the draw cache */
905         if (pchan) {
906                 length = pchan->bone->length;
907                 bone_mat = pchan->pose_mat;
908                 disp_mat = pchan->disp_mat;
909                 disp_tail_mat = pchan->disp_tail_mat;
910         }
911         else {
912                 eBone->length = len_v3v3(eBone->tail, eBone->head);
913                 ED_armature_ebone_to_mat4(eBone, ebmat);
914
915                 length = eBone->length;
916                 bone_mat = ebmat;
917                 disp_mat = eBone->disp_mat;
918                 disp_tail_mat = eBone->disp_tail_mat;
919         }
920
921         scale_m4_fl(s, length);
922         mul_m4_m4m4(disp_mat, bone_mat, s);
923         copy_m4_m4(disp_tail_mat, disp_mat);
924         translate_m4(disp_tail_mat, 0.0f, 1.0f, 0.0f);
925 }
926
927 /* compute connected child pointer for B-Bone drawing */
928 static void edbo_compute_bbone_child(bArmature *arm)
929 {
930         EditBone *eBone;
931
932         for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
933                 eBone->bbone_child = NULL;
934         }
935
936         for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
937                 if (eBone->parent && (eBone->flag & BONE_CONNECTED)) {
938                         eBone->parent->bbone_child = eBone;
939                 }
940         }
941 }
942
943 /* A version of BKE_pchan_bbone_spline_setup() for previewing editmode curve settings. */
944 static void ebone_spline_preview(EditBone *ebone, float result_array[MAX_BBONE_SUBDIV][4][4])
945 {
946         BBoneSplineParameters param;
947         EditBone *prev, *next;
948         float imat[4][4], bonemat[4][4];
949         float tmp[3];
950
951         memset(&param, 0, sizeof(param));
952
953         param.segments = ebone->segments;
954         param.length = ebone->length;
955
956         /* Get "next" and "prev" bones - these are used for handle calculations. */
957         if (ebone->bbone_prev_type == BBONE_HANDLE_AUTO) {
958                 /* Use connected parent. */
959                 if (ebone->flag & BONE_CONNECTED) {
960                         prev = ebone->parent;
961                 }
962                 else {
963                         prev = NULL;
964                 }
965         }
966         else {
967                 prev = ebone->bbone_prev;
968         }
969
970         if (ebone->bbone_next_type == BBONE_HANDLE_AUTO) {
971                 /* Use connected child. */
972                 next = ebone->bbone_child;
973         }
974         else {
975                 next = ebone->bbone_next;
976         }
977
978         /* compute handles from connected bones */
979         if (prev || next) {
980                 ED_armature_ebone_to_mat4(ebone, imat);
981                 invert_m4(imat);
982
983                 if (prev) {
984                         param.use_prev = true;
985
986                         if (ebone->bbone_prev_type == BBONE_HANDLE_RELATIVE) {
987                                 zero_v3(param.prev_h);
988                         }
989                         else if (ebone->bbone_prev_type == BBONE_HANDLE_TANGENT) {
990                                 sub_v3_v3v3(tmp, prev->tail, prev->head);
991                                 sub_v3_v3v3(tmp, ebone->head, tmp);
992                                 mul_v3_m4v3(param.prev_h, imat, tmp);
993                         }
994                         else {
995                                 param.prev_bbone = (prev->segments > 1);
996
997                                 mul_v3_m4v3(param.prev_h, imat, prev->head);
998                         }
999
1000                         if (!param.prev_bbone) {
1001                                 ED_armature_ebone_to_mat4(prev, bonemat);
1002                                 mul_m4_m4m4(param.prev_mat, imat, bonemat);
1003                         }
1004                 }
1005
1006                 if (next) {
1007                         param.use_next = true;
1008
1009                         if (ebone->bbone_next_type == BBONE_HANDLE_RELATIVE) {
1010                                 copy_v3_fl3(param.next_h, 0.0f, param.length, 0.0);
1011                         }
1012                         else if (ebone->bbone_next_type == BBONE_HANDLE_TANGENT) {
1013                                 sub_v3_v3v3(tmp, next->tail, next->head);
1014                                 add_v3_v3v3(tmp, ebone->tail, tmp);
1015                                 mul_v3_m4v3(param.next_h, imat, tmp);
1016                         }
1017                         else {
1018                                 param.next_bbone = (next->segments > 1);
1019
1020                                 mul_v3_m4v3(param.next_h, imat, next->tail);
1021                         }
1022
1023                         ED_armature_ebone_to_mat4(next, bonemat);
1024                         mul_m4_m4m4(param.next_mat, imat, bonemat);
1025                 }
1026         }
1027
1028         param.ease1 = ebone->ease1;
1029         param.ease2 = ebone->ease2;
1030         param.roll1 = ebone->roll1;
1031         param.roll2 = ebone->roll2;
1032
1033         if (prev && (ebone->flag & BONE_ADD_PARENT_END_ROLL)) {
1034                 param.roll1 += prev->roll2;
1035         }
1036
1037         param.scaleIn = ebone->scaleIn;
1038         param.scaleOut = ebone->scaleOut;
1039
1040         param.curveInX = ebone->curveInX;
1041         param.curveInY = ebone->curveInY;
1042
1043         param.curveOutX = ebone->curveOutX;
1044         param.curveOutY = ebone->curveOutY;
1045
1046         ebone->segments = BKE_pchan_bbone_spline_compute(&param, (Mat4 *)result_array);
1047 }
1048
1049 static void draw_bone_update_disp_matrix_bbone(EditBone *eBone, bPoseChannel *pchan)
1050 {
1051         float s[4][4], ebmat[4][4];
1052         float length, xwidth, zwidth;
1053         float (*bone_mat)[4];
1054         short bbone_segments;
1055
1056         /* TODO : This should be moved to depsgraph or armature refresh
1057          * and not be tight to the draw pass creation.
1058          * This would refresh armature without invalidating the draw cache */
1059         if (pchan) {
1060                 length = pchan->bone->length;
1061                 xwidth = pchan->bone->xwidth;
1062                 zwidth = pchan->bone->zwidth;
1063                 bone_mat = pchan->pose_mat;
1064                 bbone_segments = pchan->bone->segments;
1065         }
1066         else {
1067                 eBone->length = len_v3v3(eBone->tail, eBone->head);
1068                 ED_armature_ebone_to_mat4(eBone, ebmat);
1069
1070                 length = eBone->length;
1071                 xwidth = eBone->xwidth;
1072                 zwidth = eBone->zwidth;
1073                 bone_mat = ebmat;
1074                 bbone_segments = eBone->segments;
1075         }
1076
1077         size_to_mat4(s, (const float[3]){xwidth, length / bbone_segments, zwidth});
1078
1079         /* Compute BBones segment matrices... */
1080         /* Note that we need this even for one-segment bones, because box drawing need specific weirdo matrix for the box,
1081          * that we cannot use to draw end points & co. */
1082         if (pchan) {
1083                 Mat4 *bbones_mat = (Mat4 *)pchan->draw_data->bbone_matrix;
1084                 if (bbone_segments > 1) {
1085                         if (bbone_segments == pchan->runtime.bbone_segments) {
1086                                 memcpy(bbones_mat, pchan->runtime.bbone_pose_mats, sizeof(Mat4) * bbone_segments);
1087                         }
1088                         else {
1089                                 BKE_pchan_bbone_spline_setup(pchan, false, bbones_mat);
1090                         }
1091
1092                         for (int i = bbone_segments; i--; bbones_mat++) {
1093                                 mul_m4_m4m4(bbones_mat->mat, bbones_mat->mat, s);
1094                                 mul_m4_m4m4(bbones_mat->mat, bone_mat, bbones_mat->mat);
1095                         }
1096                 }
1097                 else {
1098                         mul_m4_m4m4(bbones_mat->mat, bone_mat, s);
1099                 }
1100         }
1101         else {
1102                 float (*bbones_mat)[4][4] = eBone->disp_bbone_mat;
1103
1104                 if (bbone_segments > 1) {
1105                         ebone_spline_preview(eBone, bbones_mat);
1106
1107                         for (int i = bbone_segments; i--; bbones_mat++) {
1108                                 mul_m4_m4m4(*bbones_mat, *bbones_mat, s);
1109                                 mul_m4_m4m4(*bbones_mat, bone_mat, *bbones_mat);
1110                         }
1111                 }
1112                 else {
1113                         mul_m4_m4m4(*bbones_mat, bone_mat, s);
1114                 }
1115         }
1116
1117         /* Grrr... We need default display matrix to draw end points, axes, etc. :( */
1118         draw_bone_update_disp_matrix_default(eBone, pchan);
1119 }
1120
1121 static void draw_bone_update_disp_matrix_custom(bPoseChannel *pchan)
1122 {
1123         float s[4][4];
1124         float length;
1125         float (*bone_mat)[4];
1126         float (*disp_mat)[4];
1127         float (*disp_tail_mat)[4];
1128
1129         /* See TODO above */
1130         length = PCHAN_CUSTOM_DRAW_SIZE(pchan);
1131         bone_mat = pchan->custom_tx ? pchan->custom_tx->pose_mat : pchan->pose_mat;
1132         disp_mat = pchan->disp_mat;
1133         disp_tail_mat = pchan->disp_tail_mat;
1134
1135         scale_m4_fl(s, length);
1136         mul_m4_m4m4(disp_mat, bone_mat, s);
1137         copy_m4_m4(disp_tail_mat, disp_mat);
1138         translate_m4(disp_tail_mat, 0.0f, 1.0f, 0.0f);
1139 }
1140
1141 static void draw_axes(EditBone *eBone, bPoseChannel *pchan)
1142 {
1143         float final_col[4];
1144         const float *col = (g_theme.const_color) ? g_theme.const_color :
1145                            (BONE_FLAG(eBone, pchan) & BONE_SELECTED) ? g_theme.text_hi_color : g_theme.text_color;
1146         copy_v4_v4(final_col, col);
1147         /* Mix with axes color. */
1148         final_col[3] = (g_theme.const_color) ? 1.0 : (BONE_FLAG(eBone, pchan) & BONE_SELECTED) ? 0.3 : 0.8;
1149         drw_shgroup_bone_axes(BONE_VAR(eBone, pchan, disp_mat), final_col);
1150 }
1151
1152 static void draw_points(
1153         const EditBone *eBone, const bPoseChannel *pchan, const bArmature *arm,
1154         const int boneflag, const short constflag,
1155         const int select_id)
1156 {
1157         float col_solid_root[4], col_solid_tail[4], col_wire_root[4], col_wire_tail[4];
1158         float col_hint_root[4], col_hint_tail[4];
1159
1160         copy_v4_v4(col_solid_root, g_theme.bone_solid_color);
1161         copy_v4_v4(col_solid_tail, g_theme.bone_solid_color);
1162         copy_v4_v4(col_wire_root, (g_theme.const_color) ? g_theme.const_color : g_theme.vertex_color);
1163         copy_v4_v4(col_wire_tail, (g_theme.const_color) ? g_theme.const_color : g_theme.vertex_color);
1164
1165         const bool is_envelope_draw = (arm->drawtype == ARM_ENVELOPE);
1166         static const float envelope_ignore = -1.0f;
1167
1168         col_wire_tail[3] = col_wire_root[3] = get_bone_wire_thickness(boneflag);
1169
1170         /* Edit bone points can be selected */
1171         if (eBone) {
1172                 if (eBone->flag & BONE_ROOTSEL) {
1173                         copy_v3_v3(col_wire_root, g_theme.vertex_select_color);
1174                 }
1175                 if (eBone->flag & BONE_TIPSEL) {
1176                         copy_v3_v3(col_wire_tail, g_theme.vertex_select_color);
1177                 }
1178         }
1179         else if (arm->flag & ARM_POSEMODE) {
1180                 const float *solid_color = get_bone_solid_color(eBone, pchan, arm, boneflag, constflag);
1181                 const float *wire_color = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1182                 copy_v4_v4(col_wire_tail, wire_color);
1183                 copy_v4_v4(col_wire_root, wire_color);
1184                 copy_v4_v4(col_solid_tail, solid_color);
1185                 copy_v4_v4(col_solid_root, solid_color);
1186         }
1187
1188         bone_hint_color_shade(col_hint_root, (g_theme.const_color) ? col_solid_root : col_wire_root);
1189         bone_hint_color_shade(col_hint_tail, (g_theme.const_color) ? col_solid_tail : col_wire_tail);
1190
1191         /*      Draw root point if we are not connected and parent are not hidden */
1192         if ((BONE_FLAG(eBone, pchan) & BONE_CONNECTED) == 0) {
1193                 if (select_id != -1) {
1194                         DRW_select_load_id(select_id | BONESEL_ROOT);
1195                 }
1196
1197                 if (eBone) {
1198                         if (!((eBone->parent) && !EBONE_VISIBLE(arm, eBone->parent))) {
1199                                 if (is_envelope_draw) {
1200                                         drw_shgroup_bone_envelope(
1201                                                 eBone->disp_mat, col_solid_root, col_hint_root, col_wire_root,
1202                                                 &eBone->rad_head, &envelope_ignore);
1203                                 }
1204                                 else {
1205                                         drw_shgroup_bone_point(eBone->disp_mat, col_solid_root, col_hint_root, col_wire_root);
1206                                 }
1207                         }
1208                 }
1209                 else {
1210                         Bone *bone = pchan->bone;
1211                         if (!((bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)))) {
1212                                 if (is_envelope_draw) {
1213                                         drw_shgroup_bone_envelope(
1214                                                 pchan->disp_mat, col_solid_root, col_hint_root, col_wire_root,
1215                                                 &bone->rad_head, &envelope_ignore);
1216                                 }
1217                                 else {
1218                                         drw_shgroup_bone_point(pchan->disp_mat, col_solid_root, col_hint_root, col_wire_root);
1219                                 }
1220                         }
1221                 }
1222         }
1223
1224         /*      Draw tip point */
1225         if (select_id != -1) {
1226                 DRW_select_load_id(select_id | BONESEL_TIP);
1227         }
1228
1229         if (is_envelope_draw) {
1230                 const float *rad_tail = eBone ? &eBone->rad_tail : &pchan->bone->rad_tail;
1231                 drw_shgroup_bone_envelope(
1232                         BONE_VAR(eBone, pchan, disp_mat), col_solid_tail, col_hint_tail, col_wire_tail,
1233                         &envelope_ignore, rad_tail);
1234         }
1235         else {
1236                 drw_shgroup_bone_point(BONE_VAR(eBone, pchan, disp_tail_mat), col_solid_tail, col_hint_tail, col_wire_tail);
1237         }
1238
1239         if (select_id != -1) {
1240                 DRW_select_load_id(-1);
1241         }
1242 }
1243
1244 /** \} */
1245
1246
1247 /* -------------------------------------------------------------------- */
1248
1249 /** \name Draw Bones
1250  * \{ */
1251
1252 static void draw_bone_custom_shape(
1253         EditBone *eBone, bPoseChannel *pchan, bArmature *arm,
1254         const int boneflag, const short constflag,
1255         const int select_id)
1256 {
1257         const float *col_solid = get_bone_solid_color(eBone, pchan, arm, boneflag, constflag);
1258         const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1259         const float *col_hint = get_bone_hint_color(eBone, pchan, arm, boneflag, constflag);
1260         const float (*disp_mat)[4] = pchan->disp_mat;
1261
1262         if (select_id != -1) {
1263                 DRW_select_load_id(select_id | BONESEL_BONE);
1264         }
1265
1266         if ((boneflag & BONE_DRAWWIRE) == 0) {
1267                 drw_shgroup_bone_custom_solid(disp_mat, col_solid, col_hint, col_wire, pchan->custom);
1268         }
1269         else {
1270                 drw_shgroup_bone_custom_wire(disp_mat, col_wire, pchan->custom);
1271         }
1272
1273         if (select_id != -1) {
1274                 DRW_select_load_id(-1);
1275         }
1276 }
1277
1278 static void draw_bone_envelope(
1279         EditBone *eBone, bPoseChannel *pchan, bArmature *arm,
1280         const int boneflag, const short constflag,
1281         const int select_id)
1282 {
1283         const float *col_solid = get_bone_solid_with_consts_color(eBone, pchan, arm, boneflag, constflag);
1284         const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1285         const float *col_hint = get_bone_hint_color(eBone, pchan, arm, boneflag, constflag);
1286
1287         float *rad_head, *rad_tail, *distance;
1288         if (eBone) {
1289                 rad_tail = &eBone->rad_tail;
1290                 distance = &eBone->dist;
1291                 rad_head = (eBone->parent && (boneflag & BONE_CONNECTED)) ? &eBone->parent->rad_tail : &eBone->rad_head;
1292         }
1293         else {
1294                 rad_tail = &pchan->bone->rad_tail;
1295                 distance = &pchan->bone->dist;
1296                 rad_head = (pchan->parent && (boneflag & BONE_CONNECTED)) ? &pchan->parent->bone->rad_tail : &pchan->bone->rad_head;
1297         }
1298
1299         if ((select_id == -1) &&
1300             (boneflag & BONE_NO_DEFORM) == 0 &&
1301             ((boneflag & BONE_SELECTED) || (eBone && (boneflag & (BONE_ROOTSEL | BONE_TIPSEL)))))
1302         {
1303                 drw_shgroup_bone_envelope_distance(BONE_VAR(eBone, pchan, disp_mat), rad_head, rad_tail, distance);
1304         }
1305
1306         if (select_id != -1) {
1307                 DRW_select_load_id(select_id | BONESEL_BONE);
1308         }
1309
1310         drw_shgroup_bone_envelope(
1311                 BONE_VAR(eBone, pchan, disp_mat), col_solid, col_hint, col_wire,
1312                 rad_head, rad_tail);
1313
1314         if (select_id != -1) {
1315                 DRW_select_load_id(-1);
1316         }
1317
1318         draw_points(eBone, pchan, arm, boneflag, constflag, select_id);
1319 }
1320
1321 static void draw_bone_line(
1322         EditBone *eBone, bPoseChannel *pchan, bArmature *arm,
1323         const int boneflag, const short constflag, const int select_id)
1324 {
1325         const float *col_bone = get_bone_solid_with_consts_color(eBone, pchan, arm, boneflag, constflag);
1326         const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1327         const float no_display[4] = {0.0f, 0.0f, 0.0f, 0.0f};
1328         const float *col_head = no_display;
1329         const float *col_tail = col_bone;
1330
1331         if (eBone) {
1332                 if (eBone->flag & BONE_TIPSEL) {
1333                         col_tail = g_theme.vertex_select_color;
1334                 }
1335                 if (boneflag & BONE_SELECTED) {
1336                         col_bone = g_theme.edge_select_color;
1337                 }
1338                 col_wire = g_theme.wire_color;
1339         }
1340
1341         /*      Draw root point if we are not connected and parent are not hidden */
1342         if ((BONE_FLAG(eBone, pchan) & BONE_CONNECTED) == 0) {
1343                 if (eBone && !(eBone->parent && !EBONE_VISIBLE(arm, eBone->parent))) {
1344                         col_head = (eBone->flag & BONE_ROOTSEL) ? g_theme.vertex_select_color : col_bone;
1345                 }
1346                 else if (pchan) {
1347                         Bone *bone = pchan->bone;
1348                         if (!(bone->parent && (bone->parent->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)))) {
1349                                 col_head = col_bone;
1350                         }
1351                 }
1352         }
1353
1354         if (g_theme.const_color != NULL) {
1355                 col_wire = no_display; /* actually shrink the display. */
1356                 col_bone = col_head = col_tail = g_theme.const_color;
1357         }
1358
1359         if (select_id == -1) {
1360                 /* Not in selection mode, draw everything at once. */
1361                 drw_shgroup_bone_stick(BONE_VAR(eBone, pchan, disp_mat), col_wire, col_bone, col_head, col_tail);
1362         }
1363         else {
1364                 /* In selection mode, draw bone, root and tip separately. */
1365                 DRW_select_load_id(select_id | BONESEL_BONE);
1366                 drw_shgroup_bone_stick(BONE_VAR(eBone, pchan, disp_mat), col_wire, col_bone, no_display, no_display);
1367
1368                 if (col_head[3] > 0.0f) {
1369                         DRW_select_load_id(select_id | BONESEL_ROOT);
1370                         drw_shgroup_bone_stick(BONE_VAR(eBone, pchan, disp_mat), col_wire, no_display, col_head, no_display);
1371                 }
1372
1373                 DRW_select_load_id(select_id | BONESEL_TIP);
1374                 drw_shgroup_bone_stick(BONE_VAR(eBone, pchan, disp_mat), col_wire, no_display, no_display, col_tail);
1375
1376                 DRW_select_load_id(-1);
1377         }
1378 }
1379
1380 static void draw_bone_wire(
1381         EditBone *eBone, bPoseChannel *pchan, bArmature *arm,
1382         const int boneflag, const short constflag,
1383         const int select_id)
1384 {
1385         const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1386
1387         if (select_id != -1) {
1388                 DRW_select_load_id(select_id | BONESEL_BONE);
1389         }
1390
1391         if (pchan) {
1392                 Mat4 *bbones_mat = (Mat4 *)pchan->draw_data->bbone_matrix;
1393                 BLI_assert(bbones_mat != NULL);
1394
1395                 for (int i = pchan->bone->segments; i--; bbones_mat++) {
1396                         drw_shgroup_bone_wire(bbones_mat->mat, col_wire);
1397                 }
1398         }
1399         else if (eBone) {
1400                 for (int i = 0; i < eBone->segments; i++) {
1401                         drw_shgroup_bone_wire(eBone->disp_bbone_mat[i], col_wire);
1402                 }
1403         }
1404
1405         if (select_id != -1) {
1406                 DRW_select_load_id(-1);
1407         }
1408
1409         if (eBone) {
1410                 draw_points(eBone, pchan, arm, boneflag, constflag, select_id);
1411         }
1412 }
1413
1414 static void draw_bone_box(
1415         EditBone *eBone, bPoseChannel *pchan, bArmature *arm,
1416         const int boneflag, const short constflag,
1417         const int select_id)
1418 {
1419         const float *col_solid = get_bone_solid_with_consts_color(eBone, pchan, arm, boneflag, constflag);
1420         const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1421         const float *col_hint = get_bone_hint_color(eBone, pchan, arm, boneflag, constflag);
1422
1423         if (select_id != -1) {
1424                 DRW_select_load_id(select_id | BONESEL_BONE);
1425         }
1426
1427         if (pchan) {
1428                 Mat4 *bbones_mat = (Mat4 *)pchan->draw_data->bbone_matrix;
1429                 BLI_assert(bbones_mat != NULL);
1430
1431                 for (int i = pchan->bone->segments; i--; bbones_mat++) {
1432                         drw_shgroup_bone_box(bbones_mat->mat, col_solid, col_hint, col_wire);
1433                 }
1434         }
1435         else if (eBone) {
1436                 for (int i = 0; i < eBone->segments; i++) {
1437                         drw_shgroup_bone_box(eBone->disp_bbone_mat[i], col_solid, col_hint, col_wire);
1438                 }
1439         }
1440
1441         if (select_id != -1) {
1442                 DRW_select_load_id(-1);
1443         }
1444
1445         if (eBone) {
1446                 draw_points(eBone, pchan, arm, boneflag, constflag, select_id);
1447         }
1448 }
1449
1450 static void draw_bone_octahedral(
1451         EditBone *eBone, bPoseChannel *pchan, bArmature *arm,
1452         const int boneflag, const short constflag,
1453         const int select_id)
1454 {
1455         const float *col_solid = get_bone_solid_with_consts_color(eBone, pchan, arm, boneflag, constflag);
1456         const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1457         const float *col_hint = get_bone_hint_color(eBone, pchan, arm, boneflag, constflag);
1458
1459         if (select_id != -1) {
1460                 DRW_select_load_id(select_id | BONESEL_BONE);
1461         }
1462
1463         drw_shgroup_bone_octahedral(BONE_VAR(eBone, pchan, disp_mat), col_solid, col_hint, col_wire);
1464
1465         if (select_id != -1) {
1466                 DRW_select_load_id(-1);
1467         }
1468
1469         draw_points(eBone, pchan, arm, boneflag, constflag, select_id);
1470 }
1471
1472 /** \} */
1473
1474
1475 /* -------------------------------------------------------------------- */
1476
1477 /** \name Draw Degrees of Freedom
1478  * \{ */
1479
1480 static void draw_bone_dofs(bPoseChannel *pchan)
1481 {
1482         float final_bonemat[4][4], posetrans[4][4], mat[4][4];
1483         float amin[2], amax[2], xminmax[2], zminmax[2];
1484         float col_sphere[4] = {0.25f, 0.25f, 0.25f, 0.25f};
1485         float col_lines[4] = {0.0f, 0.0f, 0.0f, 1.0f};
1486         float col_xaxis[4] = {1.0f, 0.0f, 0.0f, 1.0f};
1487         float col_zaxis[4] = {0.0f, 0.0f, 1.0f, 1.0f};
1488
1489         if (g_data.passes.bone_envelope == NULL) {
1490                 return;
1491         }
1492
1493         if (g_data.bone_dof_sphere == NULL) {
1494                 g_data.bone_dof_lines = shgroup_instance_bone_dof(g_data.passes.bone_wire, DRW_cache_bone_dof_lines_get());
1495                 g_data.bone_dof_sphere = shgroup_instance_bone_dof(g_data.passes.bone_envelope, DRW_cache_bone_dof_sphere_get());
1496                 DRW_shgroup_state_enable(g_data.bone_dof_sphere, DRW_STATE_BLEND);
1497                 DRW_shgroup_state_disable(g_data.bone_dof_sphere, DRW_STATE_CULL_FRONT);
1498         }
1499
1500         /* *0.5f here comes from M_PI/360.0f when rotations were still in degrees */
1501         xminmax[0] = sinf(pchan->limitmin[0] * 0.5f);
1502         xminmax[1] = sinf(pchan->limitmax[0] * 0.5f);
1503         zminmax[0] = sinf(pchan->limitmin[2] * 0.5f);
1504         zminmax[1] = sinf(pchan->limitmax[2] * 0.5f);
1505
1506         unit_m4(posetrans);
1507         translate_m4(posetrans, pchan->pose_mat[3][0], pchan->pose_mat[3][1], pchan->pose_mat[3][2]);
1508         /* in parent-bone pose space... */
1509         if (pchan->parent) {
1510                 copy_m4_m4(mat, pchan->parent->pose_mat);
1511                 mat[3][0] = mat[3][1] = mat[3][2] = 0.0f;
1512                 mul_m4_m4m4(posetrans, posetrans, mat);
1513         }
1514         /* ... but own restspace */
1515         mul_m4_m4m3(posetrans, posetrans, pchan->bone->bone_mat);
1516
1517         float scale = pchan->bone->length * pchan->size[1];
1518         scale_m4_fl(mat, scale);
1519         mat[1][1] = -mat[1][1];
1520         mul_m4_m4m4(posetrans, posetrans, mat);
1521
1522         /* into world space. */
1523         mul_m4_m4m4(final_bonemat, g_data.ob->obmat, posetrans);
1524
1525         if ((pchan->ikflag & BONE_IK_XLIMIT) &&
1526             (pchan->ikflag & BONE_IK_ZLIMIT))
1527         {
1528                 amin[0] = xminmax[0];
1529                 amax[0] = xminmax[1];
1530                 amin[1] = zminmax[0];
1531                 amax[1] = zminmax[1];
1532                 DRW_shgroup_call_dynamic_add(g_data.bone_dof_sphere, final_bonemat, col_sphere, amin, amax);
1533                 DRW_shgroup_call_dynamic_add(g_data.bone_dof_lines, final_bonemat, col_lines, amin, amax);
1534         }
1535         if (pchan->ikflag & BONE_IK_XLIMIT) {
1536                 amin[0] = xminmax[0];
1537                 amax[0] = xminmax[1];
1538                 amin[1] = amax[1] = 0.0f;
1539                 DRW_shgroup_call_dynamic_add(g_data.bone_dof_lines, final_bonemat, col_xaxis, amin, amax);
1540         }
1541         if (pchan->ikflag & BONE_IK_ZLIMIT) {
1542                 amin[1] = zminmax[0];
1543                 amax[1] = zminmax[1];
1544                 amin[0] = amax[0] = 0.0f;
1545                 DRW_shgroup_call_dynamic_add(g_data.bone_dof_lines, final_bonemat, col_zaxis, amin, amax);
1546         }
1547 }
1548
1549 /** \} */
1550
1551
1552 /* -------------------------------------------------------------------- */
1553
1554 /** \name Draw Relationships
1555  * \{ */
1556
1557 static void pchan_draw_ik_lines(bPoseChannel *pchan, const bool only_temp, const int constflag)
1558 {
1559         bConstraint *con;
1560         bPoseChannel *parchan;
1561         float *line_start = NULL, *line_end = NULL;
1562
1563         for (con = pchan->constraints.first; con; con = con->next) {
1564                 if (con->enforce == 0.0f)
1565                         continue;
1566
1567                 switch (con->type) {
1568                         case CONSTRAINT_TYPE_KINEMATIC:
1569                         {
1570                                 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
1571                                 int segcount = 0;
1572
1573                                 /* if only_temp, only draw if it is a temporary ik-chain */
1574                                 if (only_temp && !(data->flag & CONSTRAINT_IK_TEMP))
1575                                         continue;
1576
1577                                 /* exclude tip from chain? */
1578                                 parchan = ((data->flag & CONSTRAINT_IK_TIP) == 0) ? pchan->parent : pchan;
1579                                 line_start = parchan->pose_tail;
1580
1581                                 /* Find the chain's root */
1582                                 while (parchan->parent) {
1583                                         segcount++;
1584                                         if (segcount == data->rootbone || segcount > 255) {
1585                                                 break;  /* 255 is weak */
1586                                         }
1587                                         parchan = parchan->parent;
1588                                 }
1589
1590                                 if (parchan) {
1591                                         line_end = parchan->pose_head;
1592
1593                                         if (constflag & PCHAN_HAS_TARGET)
1594                                                 drw_shgroup_bone_ik_lines(line_start, line_end);
1595                                         else
1596                                                 drw_shgroup_bone_ik_no_target_lines(line_start, line_end);
1597                                 }
1598                                 break;
1599                         }
1600                         case CONSTRAINT_TYPE_SPLINEIK:
1601                         {
1602                                 bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
1603                                 int segcount = 0;
1604
1605                                 /* don't draw if only_temp, as Spline IK chains cannot be temporary */
1606                                 if (only_temp)
1607                                         continue;
1608
1609                                 parchan = pchan;
1610                                 line_start = parchan->pose_tail;
1611
1612                                 /* Find the chain's root */
1613                                 while (parchan->parent) {
1614                                         segcount++;
1615                                         /* FIXME: revise the breaking conditions */
1616                                         if (segcount == data->chainlen || segcount > 255) break;  /* 255 is weak */
1617                                         parchan = parchan->parent;
1618                                 }
1619                                 /* Only draw line in case our chain is more than one bone long! */
1620                                 if (parchan != pchan) { /* XXX revise the breaking conditions to only stop at the tail? */
1621                                         line_end = parchan->pose_head;
1622                                         drw_shgroup_bone_ik_spline_lines(line_start, line_end);
1623                                 }
1624                                 break;
1625                         }
1626                 }
1627         }
1628 }
1629
1630 static void draw_bone_relations(
1631         EditBone *ebone, bPoseChannel *pchan, bArmature *arm,
1632         const int boneflag, const short constflag, const bool do_relations)
1633 {
1634         if (g_data.passes.relationship_lines) {
1635                 if (ebone && ebone->parent) {
1636                         if (do_relations) {
1637                                 /* Always draw for unconnected bones, regardless of selection,
1638                                  * since riggers will want to know about the links between bones
1639                                  */
1640                                 if ((boneflag & BONE_CONNECTED) == 0) {
1641                                         drw_shgroup_bone_relationship_lines(ebone->head, ebone->parent->tail);
1642                                 }
1643                         }
1644                 }
1645                 else if (pchan && pchan->parent) {
1646                         if (do_relations) {
1647                                 /* Only draw if bone or its parent is selected - reduces viewport complexity with complex rigs */
1648                                 if ((boneflag & BONE_SELECTED) ||
1649                                     (pchan->parent->bone && (pchan->parent->bone->flag & BONE_SELECTED)))
1650                                 {
1651                                         if ((boneflag & BONE_CONNECTED) == 0) {
1652                                                 drw_shgroup_bone_relationship_lines(pchan->pose_head, pchan->parent->pose_tail);
1653                                         }
1654                                 }
1655                         }
1656
1657                         /* Draw a line to IK root bone if bone is selected. */
1658                         if (arm->flag & ARM_POSEMODE) {
1659                                 if (constflag & (PCHAN_HAS_IK | PCHAN_HAS_SPLINEIK)) {
1660                                         if (boneflag & BONE_SELECTED) {
1661                                                 pchan_draw_ik_lines(pchan, !do_relations, constflag);
1662                                         }
1663                                 }
1664                         }
1665                 }
1666         }
1667 }
1668 /** \} */
1669
1670 /* -------------------------------------------------------------------- */
1671
1672 /** \name Main Draw Loops
1673  * \{ */
1674
1675 static void draw_armature_edit(Object *ob)
1676 {
1677         const DRWContextState *draw_ctx = DRW_context_state_get();
1678         EditBone *eBone;
1679         bArmature *arm = ob->data;
1680         int index;
1681         const bool is_select = DRW_state_is_select();
1682
1683         update_color(ob, NULL);
1684         edbo_compute_bbone_child(arm);
1685
1686         const bool show_text = DRW_state_show_text();
1687         const bool show_relations = ((draw_ctx->v3d->flag & V3D_HIDE_HELPLINES) == 0);
1688
1689         for (eBone = arm->edbo->first, index = ob->select_color; eBone; eBone = eBone->next, index += 0x10000) {
1690                 if (eBone->layer & arm->layer) {
1691                         if ((eBone->flag & BONE_HIDDEN_A) == 0) {
1692                                 const int select_id = is_select ? index : (uint)-1;
1693
1694                                 const short constflag = 0;
1695
1696                                 /* catch exception for bone with hidden parent */
1697                                 int boneflag = eBone->flag;
1698                                 if ((eBone->parent) && !EBONE_VISIBLE(arm, eBone->parent)) {
1699                                         boneflag &= ~BONE_CONNECTED;
1700                                 }
1701
1702                                 /* set temporary flag for drawing bone as active, but only if selected */
1703                                 if (eBone == arm->act_edbone) {
1704                                         boneflag |= BONE_DRAW_ACTIVE;
1705                                 }
1706
1707                                 draw_bone_relations(eBone, NULL, arm, boneflag, constflag, show_relations);
1708
1709                                 if (arm->drawtype == ARM_ENVELOPE) {
1710                                         draw_bone_update_disp_matrix_default(eBone, NULL);
1711                                         draw_bone_envelope(eBone, NULL, arm, boneflag, constflag, select_id);
1712                                 }
1713                                 else if (arm->drawtype == ARM_LINE) {
1714                                         draw_bone_update_disp_matrix_default(eBone, NULL);
1715                                         draw_bone_line(eBone, NULL, arm, boneflag, constflag, select_id);
1716                                 }
1717                                 else if (arm->drawtype == ARM_WIRE) {
1718                                         draw_bone_update_disp_matrix_bbone(eBone, NULL);
1719                                         draw_bone_wire(eBone, NULL, arm, boneflag, constflag, select_id);
1720                                 }
1721                                 else if (arm->drawtype == ARM_B_BONE) {
1722                                         draw_bone_update_disp_matrix_bbone(eBone, NULL);
1723                                         draw_bone_box(eBone, NULL, arm, boneflag, constflag, select_id);
1724                                 }
1725                                 else {
1726                                         draw_bone_update_disp_matrix_default(eBone, NULL);
1727                                         draw_bone_octahedral(eBone, NULL, arm, boneflag, constflag, select_id);
1728                                 }
1729
1730                                 /* Draw names of bone */
1731                                 if (show_text && (arm->flag & ARM_DRAWNAMES)) {
1732                                         uchar color[4];
1733                                         UI_GetThemeColor4ubv((eBone->flag & BONE_SELECTED) ? TH_TEXT_HI : TH_TEXT, color);
1734
1735                                         float vec[3];
1736                                         mid_v3_v3v3(vec, eBone->head, eBone->tail);
1737                                         mul_m4_v3(ob->obmat, vec);
1738
1739                                         struct DRWTextStore *dt = DRW_text_cache_ensure();
1740                                         DRW_text_cache_add(
1741                                                 dt, vec, eBone->name, strlen(eBone->name),
1742                                                 10, 0, DRW_TEXT_CACHE_GLOBALSPACE | DRW_TEXT_CACHE_STRING_PTR, color);
1743                                 }
1744
1745                                 /*      Draw additional axes */
1746                                 if (arm->flag & ARM_DRAWAXES) {
1747                                         draw_axes(eBone, NULL);
1748                                 }
1749                         }
1750                 }
1751         }
1752 }
1753
1754 /* if const_color is NULL do pose mode coloring */
1755 static void draw_armature_pose(Object *ob, const float const_color[4])
1756 {
1757         const DRWContextState *draw_ctx = DRW_context_state_get();
1758         bArmature *arm = ob->data;
1759         bPoseChannel *pchan;
1760         int index = -1;
1761         Bone *bone;
1762
1763         update_color(ob, const_color);
1764
1765         /* We can't safely draw non-updated pose, might contain NULL bone pointers... */
1766         if (ob->pose->flag & POSE_RECALC) {
1767                 return;
1768         }
1769
1770         // if (!(base->flag & OB_FROMDUPLI)) // TODO
1771         {
1772                 if ((draw_ctx->object_mode & OB_MODE_POSE) || (ob == draw_ctx->object_pose)) {
1773                         arm->flag |= ARM_POSEMODE;
1774                 }
1775
1776                 if (arm->flag & ARM_POSEMODE) {
1777                         index = ob->select_color;
1778                 }
1779         }
1780
1781         const bool is_pose_select = (arm->flag & ARM_POSEMODE) && DRW_state_is_select();
1782         const bool show_text = DRW_state_show_text();
1783         const bool show_relations = ((draw_ctx->v3d->flag & V3D_HIDE_HELPLINES) == 0);
1784
1785         /* being set below */
1786         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1787                 bone = pchan->bone;
1788
1789                 /* bone must be visible */
1790                 if ((bone->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)) == 0) {
1791                         if (bone->layer & arm->layer) {
1792                                 const int select_id = is_pose_select ? index : (uint)-1;
1793
1794                                 const short constflag = pchan->constflag;
1795
1796                                 pchan_draw_data_init(pchan);
1797
1798                                 if (const_color) {
1799                                         /* keep color */
1800                                 }
1801                                 else {
1802                                         /* set color-set to use */
1803                                         set_pchan_colorset(ob, pchan);
1804                                 }
1805
1806                                 int boneflag = bone->flag;
1807                                 /* catch exception for bone with hidden parent */
1808                                 boneflag = bone->flag;
1809                                 if ((bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG))) {
1810                                         boneflag &= ~BONE_CONNECTED;
1811                                 }
1812
1813                                 /* set temporary flag for drawing bone as active, but only if selected */
1814                                 if (bone == arm->act_bone)
1815                                         boneflag |= BONE_DRAW_ACTIVE;
1816
1817                                 draw_bone_relations(NULL, pchan, arm, boneflag, constflag, show_relations);
1818
1819                                 if ((pchan->custom) && !(arm->flag & ARM_NO_CUSTOM)) {
1820                                         draw_bone_update_disp_matrix_custom(pchan);
1821                                         draw_bone_custom_shape(NULL, pchan, arm, boneflag, constflag, select_id);
1822                                 }
1823                                 else if (arm->drawtype == ARM_ENVELOPE) {
1824                                         draw_bone_update_disp_matrix_default(NULL, pchan);
1825                                         draw_bone_envelope(NULL, pchan, arm, boneflag, constflag, select_id);
1826                                 }
1827                                 else if (arm->drawtype == ARM_LINE) {
1828                                         draw_bone_update_disp_matrix_default(NULL, pchan);
1829                                         draw_bone_line(NULL, pchan, arm, boneflag, constflag, select_id);
1830                                 }
1831                                 else if (arm->drawtype == ARM_WIRE) {
1832                                         draw_bone_update_disp_matrix_bbone(NULL, pchan);
1833                                         draw_bone_wire(NULL, pchan, arm, boneflag, constflag, select_id);
1834                                 }
1835                                 else if (arm->drawtype == ARM_B_BONE) {
1836                                         draw_bone_update_disp_matrix_bbone(NULL, pchan);
1837                                         draw_bone_box(NULL, pchan, arm, boneflag, constflag, select_id);
1838                                 }
1839                                 else {
1840                                         draw_bone_update_disp_matrix_default(NULL, pchan);
1841                                         draw_bone_octahedral(NULL, pchan, arm, boneflag, constflag, select_id);
1842                                 }
1843
1844                                 if (!is_pose_select && show_relations &&
1845                                     (arm->flag & ARM_POSEMODE) &&
1846                                     (bone->flag & BONE_SELECTED) &&
1847                                     ((ob->base_flag & BASE_FROMDUPLI) == 0) &&
1848                                     (pchan->ikflag & (BONE_IK_XLIMIT | BONE_IK_ZLIMIT)))
1849                                 {
1850                                         draw_bone_dofs(pchan);
1851                                 }
1852
1853                                 /* Draw names of bone */
1854                                 if (show_text && (arm->flag & ARM_DRAWNAMES)) {
1855                                         uchar color[4];
1856                                         UI_GetThemeColor4ubv((arm->flag & ARM_POSEMODE) &&
1857                                                              (bone->flag & BONE_SELECTED) ? TH_TEXT_HI : TH_TEXT, color);
1858                                         float vec[3];
1859                                         mid_v3_v3v3(vec, pchan->pose_head, pchan->pose_tail);
1860                                         mul_m4_v3(ob->obmat, vec);
1861
1862                                         struct DRWTextStore *dt = DRW_text_cache_ensure();
1863                                         DRW_text_cache_add(
1864                                                 dt, vec, pchan->name, strlen(pchan->name),
1865                                                 10, 0, DRW_TEXT_CACHE_GLOBALSPACE | DRW_TEXT_CACHE_STRING_PTR, color);
1866                                 }
1867
1868                                 /*      Draw additional axes */
1869                                 if (arm->flag & ARM_DRAWAXES) {
1870                                         draw_axes(NULL, pchan);
1871                                 }
1872                         }
1873                 }
1874                 if (is_pose_select) {
1875                         index += 0x10000;
1876                 }
1877         }
1878
1879         arm->flag &= ~ARM_POSEMODE;
1880 }
1881
1882 /**
1883  * This function set the object space to use for all subsequent `DRW_shgroup_bone_*` calls.
1884  */
1885 static void drw_shgroup_armature(Object *ob, DRWArmaturePasses passes, bool transp)
1886 {
1887         memset(&g_data, 0x0, sizeof(g_data));
1888         g_data.ob = ob;
1889         g_data.passes = passes;
1890         g_data.transparent = transp;
1891         memset(&g_color, 0x0, sizeof(g_color));
1892 }
1893
1894 void DRW_shgroup_armature_object(Object *ob, ViewLayer *view_layer, DRWArmaturePasses passes)
1895 {
1896         float *color;
1897         DRW_object_wire_theme_get(ob, view_layer, &color);
1898         passes.bone_envelope = NULL; /* Don't do envelope distance in object mode. */
1899         drw_shgroup_armature(ob, passes, false);
1900         draw_armature_pose(ob, color);
1901 }
1902
1903 void DRW_shgroup_armature_pose(Object *ob, DRWArmaturePasses passes, bool transp)
1904 {
1905         drw_shgroup_armature(ob, passes, transp);
1906         draw_armature_pose(ob, NULL);
1907 }
1908
1909 void DRW_shgroup_armature_edit(Object *ob, DRWArmaturePasses passes, bool transp)
1910 {
1911         drw_shgroup_armature(ob, passes, transp);
1912         draw_armature_edit(ob);
1913 }
1914
1915 /** \} */