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