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