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