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