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