Cleanup: rename the curveInX etc bbone DNA fields to curve_in_x etc.
[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.scale_in_x = ebone->scale_in_x;
1114   param.scale_in_y = ebone->scale_in_y;
1115
1116   param.scale_out_x = ebone->scale_out_x;
1117   param.scale_out_y = ebone->scale_out_y;
1118
1119   param.curve_in_x = ebone->curve_in_x;
1120   param.curve_in_y = ebone->curve_in_y;
1121
1122   param.curve_out_x = ebone->curve_out_x;
1123   param.curve_out_y = ebone->curve_out_y;
1124
1125   ebone->segments = BKE_pchan_bbone_spline_compute(&param, false, (Mat4 *)result_array);
1126 }
1127
1128 static void draw_bone_update_disp_matrix_bbone(EditBone *eBone, bPoseChannel *pchan)
1129 {
1130   float s[4][4], ebmat[4][4];
1131   float length, xwidth, zwidth;
1132   float(*bone_mat)[4];
1133   short bbone_segments;
1134
1135   /* TODO : This should be moved to depsgraph or armature refresh
1136    * and not be tight to the draw pass creation.
1137    * This would refresh armature without invalidating the draw cache */
1138   if (pchan) {
1139     length = pchan->bone->length;
1140     xwidth = pchan->bone->xwidth;
1141     zwidth = pchan->bone->zwidth;
1142     bone_mat = pchan->pose_mat;
1143     bbone_segments = pchan->bone->segments;
1144   }
1145   else {
1146     eBone->length = len_v3v3(eBone->tail, eBone->head);
1147     ED_armature_ebone_to_mat4(eBone, ebmat);
1148
1149     length = eBone->length;
1150     xwidth = eBone->xwidth;
1151     zwidth = eBone->zwidth;
1152     bone_mat = ebmat;
1153     bbone_segments = eBone->segments;
1154   }
1155
1156   size_to_mat4(s, (const float[3]){xwidth, length / bbone_segments, zwidth});
1157
1158   /* Compute BBones segment matrices... */
1159   /* Note that we need this even for one-segment bones, because box drawing need specific weirdo matrix for the box,
1160    * that we cannot use to draw end points & co. */
1161   if (pchan) {
1162     Mat4 *bbones_mat = (Mat4 *)pchan->draw_data->bbone_matrix;
1163     if (bbone_segments > 1) {
1164       BKE_pchan_bbone_spline_setup(pchan, false, false, bbones_mat);
1165
1166       for (int i = bbone_segments; i--; bbones_mat++) {
1167         mul_m4_m4m4(bbones_mat->mat, bbones_mat->mat, s);
1168         mul_m4_m4m4(bbones_mat->mat, bone_mat, bbones_mat->mat);
1169       }
1170     }
1171     else {
1172       mul_m4_m4m4(bbones_mat->mat, bone_mat, s);
1173     }
1174   }
1175   else {
1176     float(*bbones_mat)[4][4] = eBone->disp_bbone_mat;
1177
1178     if (bbone_segments > 1) {
1179       ebone_spline_preview(eBone, bbones_mat);
1180
1181       for (int i = bbone_segments; i--; bbones_mat++) {
1182         mul_m4_m4m4(*bbones_mat, *bbones_mat, s);
1183         mul_m4_m4m4(*bbones_mat, bone_mat, *bbones_mat);
1184       }
1185     }
1186     else {
1187       mul_m4_m4m4(*bbones_mat, bone_mat, s);
1188     }
1189   }
1190
1191   /* Grrr... We need default display matrix to draw end points, axes, etc. :( */
1192   draw_bone_update_disp_matrix_default(eBone, pchan);
1193 }
1194
1195 static void draw_bone_update_disp_matrix_custom(bPoseChannel *pchan)
1196 {
1197   float s[4][4];
1198   float length;
1199   float(*bone_mat)[4];
1200   float(*disp_mat)[4];
1201   float(*disp_tail_mat)[4];
1202
1203   /* See TODO above */
1204   length = PCHAN_CUSTOM_DRAW_SIZE(pchan);
1205   bone_mat = pchan->custom_tx ? pchan->custom_tx->pose_mat : pchan->pose_mat;
1206   disp_mat = pchan->disp_mat;
1207   disp_tail_mat = pchan->disp_tail_mat;
1208
1209   scale_m4_fl(s, length);
1210   mul_m4_m4m4(disp_mat, bone_mat, s);
1211   copy_m4_m4(disp_tail_mat, disp_mat);
1212   translate_m4(disp_tail_mat, 0.0f, 1.0f, 0.0f);
1213 }
1214
1215 static void draw_axes(EditBone *eBone, bPoseChannel *pchan, const eGPUShaderConfig sh_cfg)
1216 {
1217   float final_col[4];
1218   const float *col = (g_theme.const_color) ?
1219                          g_theme.const_color :
1220                          (BONE_FLAG(eBone, pchan) & BONE_SELECTED) ? g_theme.text_hi_color :
1221                                                                      g_theme.text_color;
1222   copy_v4_v4(final_col, col);
1223   /* Mix with axes color. */
1224   final_col[3] = (g_theme.const_color) ? 1.0 :
1225                                          (BONE_FLAG(eBone, pchan) & BONE_SELECTED) ? 0.3 : 0.8;
1226   drw_shgroup_bone_axes(BONE_VAR(eBone, pchan, disp_mat), final_col, sh_cfg);
1227 }
1228
1229 static void draw_points(const EditBone *eBone,
1230                         const bPoseChannel *pchan,
1231                         const bArmature *arm,
1232                         const int boneflag,
1233                         const short constflag,
1234                         const eGPUShaderConfig sh_cfg,
1235                         const int select_id)
1236 {
1237   float col_solid_root[4], col_solid_tail[4], col_wire_root[4], col_wire_tail[4];
1238   float col_hint_root[4], col_hint_tail[4];
1239
1240   copy_v4_v4(col_solid_root, g_theme.bone_solid_color);
1241   copy_v4_v4(col_solid_tail, g_theme.bone_solid_color);
1242   copy_v4_v4(col_wire_root, (g_theme.const_color) ? g_theme.const_color : g_theme.vertex_color);
1243   copy_v4_v4(col_wire_tail, (g_theme.const_color) ? g_theme.const_color : g_theme.vertex_color);
1244
1245   const bool is_envelope_draw = (arm->drawtype == ARM_ENVELOPE);
1246   static const float envelope_ignore = -1.0f;
1247
1248   col_wire_tail[3] = col_wire_root[3] = get_bone_wire_thickness(boneflag);
1249
1250   /* Edit bone points can be selected */
1251   if (eBone) {
1252     if (eBone->flag & BONE_ROOTSEL) {
1253       copy_v3_v3(col_wire_root, g_theme.vertex_select_color);
1254     }
1255     if (eBone->flag & BONE_TIPSEL) {
1256       copy_v3_v3(col_wire_tail, g_theme.vertex_select_color);
1257     }
1258   }
1259   else if (arm->flag & ARM_POSEMODE) {
1260     const float *solid_color = get_bone_solid_color(eBone, pchan, arm, boneflag, constflag);
1261     const float *wire_color = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1262     copy_v4_v4(col_wire_tail, wire_color);
1263     copy_v4_v4(col_wire_root, wire_color);
1264     copy_v4_v4(col_solid_tail, solid_color);
1265     copy_v4_v4(col_solid_root, solid_color);
1266   }
1267
1268   bone_hint_color_shade(col_hint_root, (g_theme.const_color) ? col_solid_root : col_wire_root);
1269   bone_hint_color_shade(col_hint_tail, (g_theme.const_color) ? col_solid_tail : col_wire_tail);
1270
1271   /* Draw root point if we are not connected to our parent */
1272   if ((BONE_FLAG(eBone, pchan) & BONE_CONNECTED) == 0) {
1273     if (select_id != -1) {
1274       DRW_select_load_id(select_id | BONESEL_ROOT);
1275     }
1276
1277     if (eBone) {
1278       if (is_envelope_draw) {
1279         drw_shgroup_bone_envelope(eBone->disp_mat,
1280                                   col_solid_root,
1281                                   col_hint_root,
1282                                   col_wire_root,
1283                                   &eBone->rad_head,
1284                                   &envelope_ignore,
1285                                   sh_cfg);
1286       }
1287       else {
1288         drw_shgroup_bone_point(
1289             eBone->disp_mat, col_solid_root, col_hint_root, col_wire_root, sh_cfg);
1290       }
1291     }
1292     else {
1293       Bone *bone = pchan->bone;
1294       if (is_envelope_draw) {
1295         drw_shgroup_bone_envelope(pchan->disp_mat,
1296                                   col_solid_root,
1297                                   col_hint_root,
1298                                   col_wire_root,
1299                                   &bone->rad_head,
1300                                   &envelope_ignore,
1301                                   sh_cfg);
1302       }
1303       else {
1304         drw_shgroup_bone_point(
1305             pchan->disp_mat, col_solid_root, col_hint_root, col_wire_root, sh_cfg);
1306       }
1307     }
1308   }
1309
1310   /*  Draw tip point */
1311   if (select_id != -1) {
1312     DRW_select_load_id(select_id | BONESEL_TIP);
1313   }
1314
1315   if (is_envelope_draw) {
1316     const float *rad_tail = eBone ? &eBone->rad_tail : &pchan->bone->rad_tail;
1317     drw_shgroup_bone_envelope(BONE_VAR(eBone, pchan, disp_mat),
1318                               col_solid_tail,
1319                               col_hint_tail,
1320                               col_wire_tail,
1321                               &envelope_ignore,
1322                               rad_tail,
1323                               sh_cfg);
1324   }
1325   else {
1326     drw_shgroup_bone_point(BONE_VAR(eBone, pchan, disp_tail_mat),
1327                            col_solid_tail,
1328                            col_hint_tail,
1329                            col_wire_tail,
1330                            sh_cfg);
1331   }
1332
1333   if (select_id != -1) {
1334     DRW_select_load_id(-1);
1335   }
1336 }
1337
1338 /** \} */
1339
1340 /* -------------------------------------------------------------------- */
1341 /** \name Draw Bones
1342  * \{ */
1343
1344 static void draw_bone_custom_shape(EditBone *eBone,
1345                                    bPoseChannel *pchan,
1346                                    bArmature *arm,
1347                                    const int boneflag,
1348                                    const short constflag,
1349                                    const eGPUShaderConfig sh_cfg,
1350                                    const int select_id)
1351 {
1352   const float *col_solid = get_bone_solid_color(eBone, pchan, arm, boneflag, constflag);
1353   const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1354   const float *col_hint = get_bone_hint_color(eBone, pchan, arm, boneflag, constflag);
1355   const float(*disp_mat)[4] = pchan->disp_mat;
1356
1357   if (select_id != -1) {
1358     DRW_select_load_id(select_id | BONESEL_BONE);
1359   }
1360
1361   if ((boneflag & BONE_DRAWWIRE) == 0) {
1362     drw_shgroup_bone_custom_solid(disp_mat, col_solid, col_hint, col_wire, sh_cfg, pchan->custom);
1363   }
1364   else {
1365     drw_shgroup_bone_custom_wire(disp_mat, col_wire, pchan->custom);
1366   }
1367
1368   if (select_id != -1) {
1369     DRW_select_load_id(-1);
1370   }
1371 }
1372
1373 static void draw_bone_envelope(EditBone *eBone,
1374                                bPoseChannel *pchan,
1375                                bArmature *arm,
1376                                const int boneflag,
1377                                const short constflag,
1378                                const eGPUShaderConfig sh_cfg,
1379                                const int select_id)
1380 {
1381   const float *col_solid = get_bone_solid_with_consts_color(
1382       eBone, pchan, arm, boneflag, constflag);
1383   const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1384   const float *col_hint = get_bone_hint_color(eBone, pchan, arm, boneflag, constflag);
1385
1386   float *rad_head, *rad_tail, *distance;
1387   if (eBone) {
1388     rad_tail = &eBone->rad_tail;
1389     distance = &eBone->dist;
1390     rad_head = (eBone->parent && (boneflag & BONE_CONNECTED)) ? &eBone->parent->rad_tail :
1391                                                                 &eBone->rad_head;
1392   }
1393   else {
1394     rad_tail = &pchan->bone->rad_tail;
1395     distance = &pchan->bone->dist;
1396     rad_head = (pchan->parent && (boneflag & BONE_CONNECTED)) ? &pchan->parent->bone->rad_tail :
1397                                                                 &pchan->bone->rad_head;
1398   }
1399
1400   if ((select_id == -1) && (boneflag & BONE_NO_DEFORM) == 0 &&
1401       ((boneflag & BONE_SELECTED) || (eBone && (boneflag & (BONE_ROOTSEL | BONE_TIPSEL))))) {
1402     drw_shgroup_bone_envelope_distance(
1403         BONE_VAR(eBone, pchan, disp_mat), rad_head, rad_tail, distance, sh_cfg);
1404   }
1405
1406   if (select_id != -1) {
1407     DRW_select_load_id(select_id | BONESEL_BONE);
1408   }
1409
1410   drw_shgroup_bone_envelope(
1411       BONE_VAR(eBone, pchan, disp_mat), col_solid, col_hint, col_wire, rad_head, rad_tail, sh_cfg);
1412
1413   if (select_id != -1) {
1414     DRW_select_load_id(-1);
1415   }
1416
1417   draw_points(eBone, pchan, arm, boneflag, constflag, sh_cfg, select_id);
1418 }
1419
1420 static void draw_bone_line(EditBone *eBone,
1421                            bPoseChannel *pchan,
1422                            bArmature *arm,
1423                            const int boneflag,
1424                            const short constflag,
1425                            const eGPUShaderConfig sh_cfg,
1426                            const int select_id)
1427 {
1428   const float *col_bone = get_bone_solid_with_consts_color(eBone, pchan, arm, boneflag, constflag);
1429   const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1430   const float no_display[4] = {0.0f, 0.0f, 0.0f, 0.0f};
1431   const float *col_head = no_display;
1432   const float *col_tail = col_bone;
1433
1434   if (eBone) {
1435     if (eBone->flag & BONE_TIPSEL) {
1436       col_tail = g_theme.vertex_select_color;
1437     }
1438     if (boneflag & BONE_SELECTED) {
1439       col_bone = g_theme.edge_select_color;
1440     }
1441     col_wire = g_theme.wire_color;
1442   }
1443
1444   /* Draw root point if we are not connected to our parent */
1445   if ((BONE_FLAG(eBone, pchan) & BONE_CONNECTED) == 0) {
1446     if (eBone) {
1447       col_head = (eBone->flag & BONE_ROOTSEL) ? g_theme.vertex_select_color : col_bone;
1448     }
1449     else if (pchan) {
1450       col_head = col_bone;
1451     }
1452   }
1453
1454   if (g_theme.const_color != NULL) {
1455     col_wire = no_display; /* actually shrink the display. */
1456     col_bone = col_head = col_tail = g_theme.const_color;
1457   }
1458
1459   if (select_id == -1) {
1460     /* Not in selection mode, draw everything at once. */
1461     drw_shgroup_bone_stick(
1462         BONE_VAR(eBone, pchan, disp_mat), col_wire, col_bone, col_head, col_tail, sh_cfg);
1463   }
1464   else {
1465     /* In selection mode, draw bone, root and tip separately. */
1466     DRW_select_load_id(select_id | BONESEL_BONE);
1467     drw_shgroup_bone_stick(
1468         BONE_VAR(eBone, pchan, disp_mat), col_wire, col_bone, no_display, no_display, sh_cfg);
1469
1470     if (col_head[3] > 0.0f) {
1471       DRW_select_load_id(select_id | BONESEL_ROOT);
1472       drw_shgroup_bone_stick(
1473           BONE_VAR(eBone, pchan, disp_mat), col_wire, no_display, col_head, no_display, sh_cfg);
1474     }
1475
1476     DRW_select_load_id(select_id | BONESEL_TIP);
1477     drw_shgroup_bone_stick(
1478         BONE_VAR(eBone, pchan, disp_mat), col_wire, no_display, no_display, col_tail, sh_cfg);
1479
1480     DRW_select_load_id(-1);
1481   }
1482 }
1483
1484 static void draw_bone_wire(EditBone *eBone,
1485                            bPoseChannel *pchan,
1486                            bArmature *arm,
1487                            const int boneflag,
1488                            const short constflag,
1489                            const eGPUShaderConfig sh_cfg,
1490                            const int select_id)
1491 {
1492   const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1493
1494   if (select_id != -1) {
1495     DRW_select_load_id(select_id | BONESEL_BONE);
1496   }
1497
1498   if (pchan) {
1499     Mat4 *bbones_mat = (Mat4 *)pchan->draw_data->bbone_matrix;
1500     BLI_assert(bbones_mat != NULL);
1501
1502     for (int i = pchan->bone->segments; i--; bbones_mat++) {
1503       drw_shgroup_bone_wire(bbones_mat->mat, col_wire, sh_cfg);
1504     }
1505   }
1506   else if (eBone) {
1507     for (int i = 0; i < eBone->segments; i++) {
1508       drw_shgroup_bone_wire(eBone->disp_bbone_mat[i], col_wire, sh_cfg);
1509     }
1510   }
1511
1512   if (select_id != -1) {
1513     DRW_select_load_id(-1);
1514   }
1515
1516   if (eBone) {
1517     draw_points(eBone, pchan, arm, boneflag, constflag, sh_cfg, select_id);
1518   }
1519 }
1520
1521 static void draw_bone_box(EditBone *eBone,
1522                           bPoseChannel *pchan,
1523                           bArmature *arm,
1524                           const int boneflag,
1525                           const short constflag,
1526                           const eGPUShaderConfig sh_cfg,
1527                           const int select_id)
1528 {
1529   const float *col_solid = get_bone_solid_with_consts_color(
1530       eBone, pchan, arm, boneflag, constflag);
1531   const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1532   const float *col_hint = get_bone_hint_color(eBone, pchan, arm, boneflag, constflag);
1533
1534   if (select_id != -1) {
1535     DRW_select_load_id(select_id | BONESEL_BONE);
1536   }
1537
1538   if (pchan) {
1539     Mat4 *bbones_mat = (Mat4 *)pchan->draw_data->bbone_matrix;
1540     BLI_assert(bbones_mat != NULL);
1541
1542     for (int i = pchan->bone->segments; i--; bbones_mat++) {
1543       drw_shgroup_bone_box(bbones_mat->mat, col_solid, col_hint, col_wire, sh_cfg);
1544     }
1545   }
1546   else if (eBone) {
1547     for (int i = 0; i < eBone->segments; i++) {
1548       drw_shgroup_bone_box(eBone->disp_bbone_mat[i], col_solid, col_hint, col_wire, sh_cfg);
1549     }
1550   }
1551
1552   if (select_id != -1) {
1553     DRW_select_load_id(-1);
1554   }
1555
1556   if (eBone) {
1557     draw_points(eBone, pchan, arm, boneflag, constflag, sh_cfg, select_id);
1558   }
1559 }
1560
1561 static void draw_bone_octahedral(EditBone *eBone,
1562                                  bPoseChannel *pchan,
1563                                  bArmature *arm,
1564                                  const int boneflag,
1565                                  const short constflag,
1566                                  const eGPUShaderConfig sh_cfg,
1567                                  const int select_id)
1568 {
1569   const float *col_solid = get_bone_solid_with_consts_color(
1570       eBone, pchan, arm, boneflag, constflag);
1571   const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1572   const float *col_hint = get_bone_hint_color(eBone, pchan, arm, boneflag, constflag);
1573
1574   if (select_id != -1) {
1575     DRW_select_load_id(select_id | BONESEL_BONE);
1576   }
1577
1578   drw_shgroup_bone_octahedral(
1579       BONE_VAR(eBone, pchan, disp_mat), col_solid, col_hint, col_wire, sh_cfg);
1580
1581   if (select_id != -1) {
1582     DRW_select_load_id(-1);
1583   }
1584
1585   draw_points(eBone, pchan, arm, boneflag, constflag, sh_cfg, select_id);
1586 }
1587
1588 /** \} */
1589
1590 /* -------------------------------------------------------------------- */
1591 /** \name Draw Degrees of Freedom
1592  * \{ */
1593
1594 static void draw_bone_dofs(bPoseChannel *pchan)
1595 {
1596   float final_bonemat[4][4], posetrans[4][4], mat[4][4];
1597   float amin[2], amax[2], xminmax[2], zminmax[2];
1598   float col_sphere[4] = {0.25f, 0.25f, 0.25f, 0.25f};
1599   float col_lines[4] = {0.0f, 0.0f, 0.0f, 1.0f};
1600   float col_xaxis[4] = {1.0f, 0.0f, 0.0f, 1.0f};
1601   float col_zaxis[4] = {0.0f, 0.0f, 1.0f, 1.0f};
1602
1603   if (g_data.passes.bone_envelope == NULL) {
1604     return;
1605   }
1606
1607   if (g_data.bone_dof_sphere == NULL) {
1608     g_data.bone_dof_lines = shgroup_instance_bone_dof(g_data.passes.bone_wire,
1609                                                       DRW_cache_bone_dof_lines_get());
1610     g_data.bone_dof_sphere = shgroup_instance_bone_dof(g_data.passes.bone_envelope,
1611                                                        DRW_cache_bone_dof_sphere_get());
1612     DRW_shgroup_state_enable(g_data.bone_dof_sphere, DRW_STATE_BLEND);
1613     DRW_shgroup_state_disable(g_data.bone_dof_sphere, DRW_STATE_CULL_FRONT);
1614   }
1615
1616   /* *0.5f here comes from M_PI/360.0f when rotations were still in degrees */
1617   xminmax[0] = sinf(pchan->limitmin[0] * 0.5f);
1618   xminmax[1] = sinf(pchan->limitmax[0] * 0.5f);
1619   zminmax[0] = sinf(pchan->limitmin[2] * 0.5f);
1620   zminmax[1] = sinf(pchan->limitmax[2] * 0.5f);
1621
1622   unit_m4(posetrans);
1623   translate_m4(posetrans, pchan->pose_mat[3][0], pchan->pose_mat[3][1], pchan->pose_mat[3][2]);
1624   /* in parent-bone pose space... */
1625   if (pchan->parent) {
1626     copy_m4_m4(mat, pchan->parent->pose_mat);
1627     mat[3][0] = mat[3][1] = mat[3][2] = 0.0f;
1628     mul_m4_m4m4(posetrans, posetrans, mat);
1629   }
1630   /* ... but own restspace */
1631   mul_m4_m4m3(posetrans, posetrans, pchan->bone->bone_mat);
1632
1633   float scale = pchan->bone->length * pchan->size[1];
1634   scale_m4_fl(mat, scale);
1635   mat[1][1] = -mat[1][1];
1636   mul_m4_m4m4(posetrans, posetrans, mat);
1637
1638   /* into world space. */
1639   mul_m4_m4m4(final_bonemat, g_data.ob->obmat, posetrans);
1640
1641   if ((pchan->ikflag & BONE_IK_XLIMIT) && (pchan->ikflag & BONE_IK_ZLIMIT)) {
1642     amin[0] = xminmax[0];
1643     amax[0] = xminmax[1];
1644     amin[1] = zminmax[0];
1645     amax[1] = zminmax[1];
1646     DRW_shgroup_call_dynamic_add(g_data.bone_dof_sphere, final_bonemat, col_sphere, amin, amax);
1647     DRW_shgroup_call_dynamic_add(g_data.bone_dof_lines, final_bonemat, col_lines, amin, amax);
1648   }
1649   if (pchan->ikflag & BONE_IK_XLIMIT) {
1650     amin[0] = xminmax[0];
1651     amax[0] = xminmax[1];
1652     amin[1] = amax[1] = 0.0f;
1653     DRW_shgroup_call_dynamic_add(g_data.bone_dof_lines, final_bonemat, col_xaxis, amin, amax);
1654   }
1655   if (pchan->ikflag & BONE_IK_ZLIMIT) {
1656     amin[1] = zminmax[0];
1657     amax[1] = zminmax[1];
1658     amin[0] = amax[0] = 0.0f;
1659     DRW_shgroup_call_dynamic_add(g_data.bone_dof_lines, final_bonemat, col_zaxis, amin, amax);
1660   }
1661 }
1662
1663 /** \} */
1664
1665 /* -------------------------------------------------------------------- */
1666 /** \name Draw Relationships
1667  * \{ */
1668
1669 static void pchan_draw_ik_lines(bPoseChannel *pchan,
1670                                 const bool only_temp,
1671                                 const int constflag,
1672                                 const eGPUShaderConfig sh_cfg)
1673 {
1674   bConstraint *con;
1675   bPoseChannel *parchan;
1676   float *line_start = NULL, *line_end = NULL;
1677
1678   for (con = pchan->constraints.first; con; con = con->next) {
1679     if (con->enforce == 0.0f) {
1680       continue;
1681     }
1682
1683     switch (con->type) {
1684       case CONSTRAINT_TYPE_KINEMATIC: {
1685         bKinematicConstraint *data = (bKinematicConstraint *)con->data;
1686         int segcount = 0;
1687
1688         /* if only_temp, only draw if it is a temporary ik-chain */
1689         if (only_temp && !(data->flag & CONSTRAINT_IK_TEMP)) {
1690           continue;
1691         }
1692
1693         /* exclude tip from chain? */
1694         parchan = ((data->flag & CONSTRAINT_IK_TIP) == 0) ? pchan->parent : pchan;
1695         line_start = parchan->pose_tail;
1696
1697         /* Find the chain's root */
1698         while (parchan->parent) {
1699           segcount++;
1700           if (segcount == data->rootbone || segcount > 255) {
1701             break; /* 255 is weak */
1702           }
1703           parchan = parchan->parent;
1704         }
1705
1706         if (parchan) {
1707           line_end = parchan->pose_head;
1708
1709           if (constflag & PCHAN_HAS_TARGET) {
1710             drw_shgroup_bone_ik_lines(line_start, line_end, sh_cfg);
1711           }
1712           else {
1713             drw_shgroup_bone_ik_no_target_lines(line_start, line_end, sh_cfg);
1714           }
1715         }
1716         break;
1717       }
1718       case CONSTRAINT_TYPE_SPLINEIK: {
1719         bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
1720         int segcount = 0;
1721
1722         /* don't draw if only_temp, as Spline IK chains cannot be temporary */
1723         if (only_temp) {
1724           continue;
1725         }
1726
1727         parchan = pchan;
1728         line_start = parchan->pose_tail;
1729
1730         /* Find the chain's root */
1731         while (parchan->parent) {
1732           segcount++;
1733           /* FIXME: revise the breaking conditions */
1734           if (segcount == data->chainlen || segcount > 255) {
1735             break; /* 255 is weak */
1736           }
1737           parchan = parchan->parent;
1738         }
1739         /* Only draw line in case our chain is more than one bone long! */
1740         if (parchan != pchan) { /* XXX revise the breaking conditions to only stop at the tail? */
1741           line_end = parchan->pose_head;
1742           drw_shgroup_bone_ik_spline_lines(line_start, line_end, sh_cfg);
1743         }
1744         break;
1745       }
1746     }
1747   }
1748 }
1749
1750 static void draw_bone_relations(EditBone *ebone,
1751                                 bPoseChannel *pchan,
1752                                 bArmature *arm,
1753                                 const int boneflag,
1754                                 const short constflag,
1755                                 const bool do_relations,
1756                                 const eGPUShaderConfig sh_cfg)
1757 {
1758   if (g_data.passes.relationship_lines) {
1759     if (ebone && ebone->parent) {
1760       if (do_relations) {
1761         /* Always draw for unconnected bones, regardless of selection,
1762          * since riggers will want to know about the links between bones
1763          */
1764         if ((boneflag & BONE_CONNECTED) == 0) {
1765           drw_shgroup_bone_relationship_lines(ebone->head, ebone->parent->tail, sh_cfg);
1766         }
1767       }
1768     }
1769     else if (pchan && pchan->parent) {
1770       if (do_relations) {
1771         /* Only draw if bone or its parent is selected - reduces viewport complexity with complex rigs */
1772         if ((boneflag & BONE_SELECTED) ||
1773             (pchan->parent->bone && (pchan->parent->bone->flag & BONE_SELECTED))) {
1774           if ((boneflag & BONE_CONNECTED) == 0) {
1775             drw_shgroup_bone_relationship_lines(
1776                 pchan->pose_head, pchan->parent->pose_tail, sh_cfg);
1777           }
1778         }
1779       }
1780
1781       /* Draw a line to IK root bone if bone is selected. */
1782       if (arm->flag & ARM_POSEMODE) {
1783         if (constflag & (PCHAN_HAS_IK | PCHAN_HAS_SPLINEIK)) {
1784           if (boneflag & BONE_SELECTED) {
1785             pchan_draw_ik_lines(pchan, !do_relations, constflag, sh_cfg);
1786           }
1787         }
1788       }
1789     }
1790   }
1791 }
1792 /** \} */
1793
1794 /* -------------------------------------------------------------------- */
1795 /** \name Main Draw Loops
1796  * \{ */
1797
1798 static void draw_armature_edit(Object *ob)
1799 {
1800   const DRWContextState *draw_ctx = DRW_context_state_get();
1801   EditBone *eBone;
1802   bArmature *arm = ob->data;
1803   int index;
1804   const bool is_select = DRW_state_is_select();
1805
1806   update_color(ob, NULL);
1807   edbo_compute_bbone_child(arm);
1808
1809   const bool show_text = DRW_state_show_text();
1810   const bool show_relations = ((draw_ctx->v3d->flag & V3D_HIDE_HELPLINES) == 0);
1811
1812   for (eBone = arm->edbo->first, index = ob->select_id; eBone;
1813        eBone = eBone->next, index += 0x10000) {
1814     if (eBone->layer & arm->layer) {
1815       if ((eBone->flag & BONE_HIDDEN_A) == 0) {
1816         const int select_id = is_select ? index : (uint)-1;
1817
1818         const short constflag = 0;
1819
1820         /* catch exception for bone with hidden parent */
1821         int boneflag = eBone->flag;
1822         if ((eBone->parent) && !EBONE_VISIBLE(arm, eBone->parent)) {
1823           boneflag &= ~BONE_CONNECTED;
1824         }
1825
1826         /* set temporary flag for drawing bone as active, but only if selected */
1827         if (eBone == arm->act_edbone) {
1828           boneflag |= BONE_DRAW_ACTIVE;
1829         }
1830
1831         draw_bone_relations(
1832             eBone, NULL, arm, boneflag, constflag, show_relations, draw_ctx->sh_cfg);
1833
1834         if (arm->drawtype == ARM_ENVELOPE) {
1835           draw_bone_update_disp_matrix_default(eBone, NULL);
1836           draw_bone_envelope(eBone, NULL, arm, boneflag, constflag, draw_ctx->sh_cfg, select_id);
1837         }
1838         else if (arm->drawtype == ARM_LINE) {
1839           draw_bone_update_disp_matrix_default(eBone, NULL);
1840           draw_bone_line(eBone, NULL, arm, boneflag, constflag, draw_ctx->sh_cfg, select_id);
1841         }
1842         else if (arm->drawtype == ARM_WIRE) {
1843           draw_bone_update_disp_matrix_bbone(eBone, NULL);
1844           draw_bone_wire(eBone, NULL, arm, boneflag, constflag, draw_ctx->sh_cfg, select_id);
1845         }
1846         else if (arm->drawtype == ARM_B_BONE) {
1847           draw_bone_update_disp_matrix_bbone(eBone, NULL);
1848           draw_bone_box(eBone, NULL, arm, boneflag, constflag, draw_ctx->sh_cfg, select_id);
1849         }
1850         else {
1851           draw_bone_update_disp_matrix_default(eBone, NULL);
1852           draw_bone_octahedral(eBone, NULL, arm, boneflag, constflag, draw_ctx->sh_cfg, select_id);
1853         }
1854
1855         /* Draw names of bone */
1856         if (show_text && (arm->flag & ARM_DRAWNAMES)) {
1857           uchar color[4];
1858           UI_GetThemeColor4ubv((eBone->flag & BONE_SELECTED) ? TH_TEXT_HI : TH_TEXT, color);
1859
1860           float vec[3];
1861           mid_v3_v3v3(vec, eBone->head, eBone->tail);
1862           mul_m4_v3(ob->obmat, vec);
1863
1864           struct DRWTextStore *dt = DRW_text_cache_ensure();
1865           DRW_text_cache_add(dt,
1866                              vec,
1867                              eBone->name,
1868                              strlen(eBone->name),
1869                              10,
1870                              0,
1871                              DRW_TEXT_CACHE_GLOBALSPACE | DRW_TEXT_CACHE_STRING_PTR,
1872                              color);
1873         }
1874
1875         /*  Draw additional axes */
1876         if (arm->flag & ARM_DRAWAXES) {
1877           draw_axes(eBone, NULL, draw_ctx->sh_cfg);
1878         }
1879       }
1880     }
1881   }
1882 }
1883
1884 /* if const_color is NULL do pose mode coloring */
1885 static void draw_armature_pose(Object *ob, const float const_color[4])
1886 {
1887   const DRWContextState *draw_ctx = DRW_context_state_get();
1888   bArmature *arm = ob->data;
1889   bPoseChannel *pchan;
1890   int index = -1;
1891   Bone *bone;
1892
1893   update_color(ob, const_color);
1894
1895   /* We can't safely draw non-updated pose, might contain NULL bone pointers... */
1896   if (ob->pose->flag & POSE_RECALC) {
1897     return;
1898   }
1899
1900   // if (!(base->flag & OB_FROMDUPLI)) // TODO
1901   {
1902     if ((draw_ctx->object_mode & OB_MODE_POSE) || (ob == draw_ctx->object_pose)) {
1903       arm->flag |= ARM_POSEMODE;
1904     }
1905
1906     if (arm->flag & ARM_POSEMODE) {
1907       index = ob->select_id;
1908     }
1909   }
1910
1911   const bool is_pose_select = (arm->flag & ARM_POSEMODE) && DRW_state_is_select();
1912   const bool show_text = DRW_state_show_text();
1913   const bool show_relations = ((draw_ctx->v3d->flag & V3D_HIDE_HELPLINES) == 0);
1914
1915   /* being set below */
1916   for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1917     bone = pchan->bone;
1918
1919     /* bone must be visible */
1920     if ((bone->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)) == 0) {
1921       if (bone->layer & arm->layer) {
1922         const int select_id = is_pose_select ? index : (uint)-1;
1923
1924         const short constflag = pchan->constflag;
1925
1926         pchan_draw_data_init(pchan);
1927
1928         if (const_color) {
1929           /* keep color */
1930         }
1931         else {
1932           /* set color-set to use */
1933           set_pchan_colorset(ob, pchan);
1934         }
1935
1936         int boneflag = bone->flag;
1937         /* catch exception for bone with hidden parent */
1938         boneflag = bone->flag;
1939         if ((bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG))) {
1940           boneflag &= ~BONE_CONNECTED;
1941         }
1942
1943         /* set temporary flag for drawing bone as active, but only if selected */
1944         if (bone == arm->act_bone) {
1945           boneflag |= BONE_DRAW_ACTIVE;
1946         }
1947
1948         draw_bone_relations(
1949             NULL, pchan, arm, boneflag, constflag, show_relations, draw_ctx->sh_cfg);
1950
1951         if ((pchan->custom) && !(arm->flag & ARM_NO_CUSTOM)) {
1952           draw_bone_update_disp_matrix_custom(pchan);
1953           draw_bone_custom_shape(
1954               NULL, pchan, arm, boneflag, constflag, draw_ctx->sh_cfg, select_id);
1955         }
1956         else if (arm->drawtype == ARM_ENVELOPE) {
1957           draw_bone_update_disp_matrix_default(NULL, pchan);
1958           draw_bone_envelope(NULL, pchan, arm, boneflag, constflag, draw_ctx->sh_cfg, select_id);
1959         }
1960         else if (arm->drawtype == ARM_LINE) {
1961           draw_bone_update_disp_matrix_default(NULL, pchan);
1962           draw_bone_line(NULL, pchan, arm, boneflag, constflag, draw_ctx->sh_cfg, select_id);
1963         }
1964         else if (arm->drawtype == ARM_WIRE) {
1965           draw_bone_update_disp_matrix_bbone(NULL, pchan);
1966           draw_bone_wire(NULL, pchan, arm, boneflag, constflag, draw_ctx->sh_cfg, select_id);
1967         }
1968         else if (arm->drawtype == ARM_B_BONE) {
1969           draw_bone_update_disp_matrix_bbone(NULL, pchan);
1970           draw_bone_box(NULL, pchan, arm, boneflag, constflag, draw_ctx->sh_cfg, select_id);
1971         }
1972         else {
1973           draw_bone_update_disp_matrix_default(NULL, pchan);
1974           draw_bone_octahedral(NULL, pchan, arm, boneflag, constflag, draw_ctx->sh_cfg, select_id);
1975         }
1976
1977         if (!is_pose_select && show_relations && (arm->flag & ARM_POSEMODE) &&
1978             (bone->flag & BONE_SELECTED) && ((ob->base_flag & BASE_FROM_DUPLI) == 0) &&
1979             (pchan->ikflag & (BONE_IK_XLIMIT | BONE_IK_ZLIMIT))) {
1980           draw_bone_dofs(pchan);
1981         }
1982
1983         /* Draw names of bone */
1984         if (show_text && (arm->flag & ARM_DRAWNAMES)) {
1985           uchar color[4];
1986           UI_GetThemeColor4ubv(
1987               (arm->flag & ARM_POSEMODE) && (bone->flag & BONE_SELECTED) ? TH_TEXT_HI : TH_TEXT,
1988               color);
1989           float vec[3];
1990           mid_v3_v3v3(vec, pchan->pose_head, pchan->pose_tail);
1991           mul_m4_v3(ob->obmat, vec);
1992
1993           struct DRWTextStore *dt = DRW_text_cache_ensure();
1994           DRW_text_cache_add(dt,
1995                              vec,
1996                              pchan->name,
1997                              strlen(pchan->name),
1998                              10,
1999                              0,
2000                              DRW_TEXT_CACHE_GLOBALSPACE | DRW_TEXT_CACHE_STRING_PTR,
2001                              color);
2002         }
2003
2004         /*  Draw additional axes */
2005         if (arm->flag & ARM_DRAWAXES) {
2006           draw_axes(NULL, pchan, draw_ctx->sh_cfg);
2007         }
2008       }
2009     }
2010     if (is_pose_select) {
2011       index += 0x10000;
2012     }
2013   }
2014
2015   arm->flag &= ~ARM_POSEMODE;
2016 }
2017
2018 /**
2019  * This function set the object space to use for all subsequent `DRW_shgroup_bone_*` calls.
2020  */
2021 static void drw_shgroup_armature(Object *ob, DRWArmaturePasses passes, bool transp)
2022 {
2023   memset(&g_data, 0x0, sizeof(g_data));
2024   g_data.ob = ob;
2025   g_data.passes = passes;
2026   g_data.transparent = transp;
2027   memset(&g_color, 0x0, sizeof(g_color));
2028 }
2029
2030 void DRW_shgroup_armature_object(Object *ob,
2031                                  ViewLayer *view_layer,
2032                                  DRWArmaturePasses passes,
2033                                  bool transp)
2034 {
2035   float *color;
2036   DRW_object_wire_theme_get(ob, view_layer, &color);
2037   passes.bone_envelope = NULL; /* Don't do envelope distance in object mode. */
2038   drw_shgroup_armature(ob, passes, transp);
2039   draw_armature_pose(ob, color);
2040 }
2041
2042 void DRW_shgroup_armature_pose(Object *ob, DRWArmaturePasses passes, bool transp)
2043 {
2044   drw_shgroup_armature(ob, passes, transp);
2045   draw_armature_pose(ob, NULL);
2046 }
2047
2048 void DRW_shgroup_armature_edit(Object *ob, DRWArmaturePasses passes, bool transp)
2049 {
2050   drw_shgroup_armature(ob, passes, transp);
2051   draw_armature_edit(ob);
2052 }
2053
2054 /** \} */