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