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