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         g_theme.const_color = const_color;
746         g_theme.const_wire = ((ob->base_flag & BASE_SELECTED) != 0) ? 1.5f : 0.0f;
747
748 #define NO_ALPHA(c) (((c)[3] = 1.0f), (c))
749
750         UI_GetThemeColor3fv(TH_SELECT, NO_ALPHA(g_theme.select_color));
751         UI_GetThemeColor3fv(TH_EDGE_SELECT, NO_ALPHA(g_theme.edge_select_color));
752         UI_GetThemeColorShade3fv(TH_EDGE_SELECT, -20, NO_ALPHA(g_theme.bone_select_color));
753         UI_GetThemeColor3fv(TH_WIRE, NO_ALPHA(g_theme.wire_color));
754         UI_GetThemeColor3fv(TH_WIRE_EDIT, NO_ALPHA(g_theme.wire_edit_color));
755         UI_GetThemeColor3fv(TH_BONE_SOLID, NO_ALPHA(g_theme.bone_solid_color));
756         UI_GetThemeColorBlendShade3fv(TH_WIRE_EDIT, TH_EDGE_SELECT, 0.15f, 0, NO_ALPHA(g_theme.bone_active_unselect_color));
757         UI_GetThemeColor3fv(TH_BONE_POSE, NO_ALPHA(g_theme.bone_pose_color));
758         UI_GetThemeColor3fv(TH_BONE_POSE_ACTIVE, NO_ALPHA(g_theme.bone_pose_active_color));
759         UI_GetThemeColorBlendShade3fv(TH_WIRE, TH_BONE_POSE, 0.15f, 0, NO_ALPHA(g_theme.bone_pose_active_unselect_color));
760         UI_GetThemeColor3fv(TH_TEXT_HI, NO_ALPHA(g_theme.text_hi_color));
761         UI_GetThemeColor3fv(TH_TEXT, NO_ALPHA(g_theme.text_color));
762         UI_GetThemeColor3fv(TH_VERTEX_SELECT, NO_ALPHA(g_theme.vertex_select_color));
763         UI_GetThemeColor3fv(TH_VERTEX, NO_ALPHA(g_theme.vertex_color));
764
765 #undef NO_ALPHA
766 }
767
768 static const float *get_bone_solid_color(
769         const EditBone *UNUSED(eBone), const bPoseChannel *pchan, const bArmature *arm,
770         const int boneflag, const short constflag)
771 {
772         if (g_theme.const_color)
773                 return g_theme.bone_solid_color;
774
775         if (arm->flag & ARM_POSEMODE) {
776                 static float disp_color[4];
777                 copy_v4_v4(disp_color, pchan->draw_data->solid_color);
778                 set_pchan_color(PCHAN_COLOR_SOLID, boneflag, constflag, disp_color);
779                 return disp_color;
780         }
781
782         return g_theme.bone_solid_color;
783 }
784
785 static const float *get_bone_solid_with_consts_color(
786         const EditBone *eBone, const bPoseChannel *pchan, const bArmature *arm,
787         const int boneflag, const short constflag)
788 {
789         if (g_theme.const_color)
790                 return g_theme.bone_solid_color;
791
792         const float *col = get_bone_solid_color(eBone, pchan, arm, boneflag, constflag);
793
794         static float consts_color[4];
795         if (set_pchan_color(PCHAN_COLOR_CONSTS, boneflag, constflag, consts_color)) {
796                 interp_v3_v3v3(consts_color, col, consts_color, 0.5f);
797         }
798         else {
799                 copy_v4_v4(consts_color, col);
800         }
801         return consts_color;
802 }
803
804 static float get_bone_wire_thickness(int boneflag)
805 {
806         if (g_theme.const_color)
807                 return g_theme.const_wire;
808         else if (boneflag & (BONE_DRAW_ACTIVE | BONE_SELECTED))
809                 return 2.0f;
810         else
811                 return 1.0f;
812 }
813
814 static const float *get_bone_wire_color(
815         const EditBone *eBone, const bPoseChannel *pchan, const bArmature *arm,
816         const int boneflag, const short constflag)
817 {
818         static float disp_color[4];
819
820         if (g_theme.const_color) {
821                 copy_v3_v3(disp_color, g_theme.const_color);
822         }
823         else if (eBone) {
824                 if (boneflag & BONE_SELECTED) {
825                         if (boneflag & BONE_DRAW_ACTIVE) {
826                                 copy_v3_v3(disp_color, g_theme.edge_select_color);
827                         }
828                         else {
829                                 copy_v3_v3(disp_color, g_theme.bone_select_color);
830                         }
831                 }
832                 else {
833                         if (boneflag & BONE_DRAW_ACTIVE) {
834                                 copy_v3_v3(disp_color, g_theme.bone_active_unselect_color);
835                         }
836                         else {
837                                 copy_v3_v3(disp_color, g_theme.wire_edit_color);
838                         }
839                 }
840         }
841         else if (arm->flag & ARM_POSEMODE) {
842                 copy_v4_v4(disp_color, pchan->draw_data->wire_color);
843                 set_pchan_color(PCHAN_COLOR_NORMAL, boneflag, constflag, disp_color);
844         }
845         else {
846                 copy_v3_v3(disp_color, g_theme.vertex_color);
847         }
848
849         disp_color[3] = get_bone_wire_thickness(boneflag);
850
851         return disp_color;
852 }
853
854 #define HINT_MUL 0.5f
855 #define HINT_SHADE 0.2f
856
857 static void bone_hint_color_shade(float hint_color[4], const float color[4])
858 {
859         mul_v3_v3fl(hint_color, color, HINT_MUL);
860         cp_shade_color3f(hint_color, -HINT_SHADE);
861         hint_color[3] = 1.0f;
862 }
863
864 static const float *get_bone_hint_color(
865         const EditBone *eBone, const bPoseChannel *pchan, const bArmature *arm,
866         const int boneflag, const short constflag)
867 {
868         static float hint_color[4] = {0.0f, 0.0f, 0.0f, 1.0f};
869
870         if (g_theme.const_color) {
871                 bone_hint_color_shade(hint_color, g_theme.bone_solid_color);
872         }
873         else {
874                 const float *wire_color = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
875                 bone_hint_color_shade(hint_color, wire_color);
876         }
877
878         return hint_color;
879 }
880
881 /** \} */
882
883
884 /* -------------------------------------------------------------------- */
885
886 /** \name Helper Utils
887  * \{ */
888
889 static void pchan_draw_data_init(bPoseChannel *pchan)
890 {
891         if (pchan->draw_data != NULL) {
892                 if (pchan->draw_data->bbone_matrix_len != pchan->bone->segments) {
893                         MEM_SAFE_FREE(pchan->draw_data);
894                 }
895         }
896
897         if (pchan->draw_data == NULL) {
898                 pchan->draw_data = MEM_mallocN(sizeof(*pchan->draw_data) + sizeof(Mat4) * pchan->bone->segments, __func__);
899                 pchan->draw_data->bbone_matrix_len = pchan->bone->segments;
900         }
901 }
902
903 static void draw_bone_update_disp_matrix_default(EditBone *eBone, bPoseChannel *pchan)
904 {
905         float s[4][4], ebmat[4][4];
906         float length;
907         float (*bone_mat)[4];
908         float (*disp_mat)[4];
909         float (*disp_tail_mat)[4];
910
911         /* TODO : This should be moved to depsgraph or armature refresh
912          * and not be tight to the draw pass creation.
913          * This would refresh armature without invalidating the draw cache */
914         if (pchan) {
915                 length = pchan->bone->length;
916                 bone_mat = pchan->pose_mat;
917                 disp_mat = pchan->disp_mat;
918                 disp_tail_mat = pchan->disp_tail_mat;
919         }
920         else {
921                 eBone->length = len_v3v3(eBone->tail, eBone->head);
922                 ED_armature_ebone_to_mat4(eBone, ebmat);
923
924                 length = eBone->length;
925                 bone_mat = ebmat;
926                 disp_mat = eBone->disp_mat;
927                 disp_tail_mat = eBone->disp_tail_mat;
928         }
929
930         scale_m4_fl(s, length);
931         mul_m4_m4m4(disp_mat, bone_mat, s);
932         copy_m4_m4(disp_tail_mat, disp_mat);
933         translate_m4(disp_tail_mat, 0.0f, 1.0f, 0.0f);
934 }
935
936 /* compute connected child pointer for B-Bone drawing */
937 static void edbo_compute_bbone_child(bArmature *arm)
938 {
939         EditBone *eBone;
940
941         for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
942                 eBone->bbone_child = NULL;
943         }
944
945         for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
946                 if (eBone->parent && (eBone->flag & BONE_CONNECTED)) {
947                         eBone->parent->bbone_child = eBone;
948                 }
949         }
950 }
951
952 /* A version of BKE_pchan_bbone_spline_setup() for previewing editmode curve settings. */
953 static void ebone_spline_preview(EditBone *ebone, float result_array[MAX_BBONE_SUBDIV][4][4])
954 {
955         BBoneSplineParameters param;
956         EditBone *prev, *next;
957         float imat[4][4], bonemat[4][4];
958         float tmp[3];
959
960         memset(&param, 0, sizeof(param));
961
962         param.segments = ebone->segments;
963         param.length = ebone->length;
964
965         /* Get "next" and "prev" bones - these are used for handle calculations. */
966         if (ebone->bbone_prev_type == BBONE_HANDLE_AUTO) {
967                 /* Use connected parent. */
968                 if (ebone->flag & BONE_CONNECTED) {
969                         prev = ebone->parent;
970                 }
971                 else {
972                         prev = NULL;
973                 }
974         }
975         else {
976                 prev = ebone->bbone_prev;
977         }
978
979         if (ebone->bbone_next_type == BBONE_HANDLE_AUTO) {
980                 /* Use connected child. */
981                 next = ebone->bbone_child;
982         }
983         else {
984                 next = ebone->bbone_next;
985         }
986
987         /* compute handles from connected bones */
988         if (prev || next) {
989                 ED_armature_ebone_to_mat4(ebone, imat);
990                 invert_m4(imat);
991
992                 if (prev) {
993                         param.use_prev = true;
994
995                         if (ebone->bbone_prev_type == BBONE_HANDLE_RELATIVE) {
996                                 zero_v3(param.prev_h);
997                         }
998                         else if (ebone->bbone_prev_type == BBONE_HANDLE_TANGENT) {
999                                 sub_v3_v3v3(tmp, prev->tail, prev->head);
1000                                 sub_v3_v3v3(tmp, ebone->head, tmp);
1001                                 mul_v3_m4v3(param.prev_h, imat, tmp);
1002                         }
1003                         else {
1004                                 param.prev_bbone = (prev->segments > 1);
1005
1006                                 mul_v3_m4v3(param.prev_h, imat, prev->head);
1007                         }
1008
1009                         if (!param.prev_bbone) {
1010                                 ED_armature_ebone_to_mat4(prev, bonemat);
1011                                 mul_m4_m4m4(param.prev_mat, imat, bonemat);
1012                         }
1013                 }
1014
1015                 if (next) {
1016                         param.use_next = true;
1017
1018                         if (ebone->bbone_next_type == BBONE_HANDLE_RELATIVE) {
1019                                 copy_v3_fl3(param.next_h, 0.0f, param.length, 0.0);
1020                         }
1021                         else if (ebone->bbone_next_type == BBONE_HANDLE_TANGENT) {
1022                                 sub_v3_v3v3(tmp, next->tail, next->head);
1023                                 add_v3_v3v3(tmp, ebone->tail, tmp);
1024                                 mul_v3_m4v3(param.next_h, imat, tmp);
1025                         }
1026                         else {
1027                                 param.next_bbone = (next->segments > 1);
1028
1029                                 mul_v3_m4v3(param.next_h, imat, next->tail);
1030                         }
1031
1032                         ED_armature_ebone_to_mat4(next, bonemat);
1033                         mul_m4_m4m4(param.next_mat, imat, bonemat);
1034                 }
1035         }
1036
1037         param.ease1 = ebone->ease1;
1038         param.ease2 = ebone->ease2;
1039         param.roll1 = ebone->roll1;
1040         param.roll2 = ebone->roll2;
1041
1042         if (prev && (ebone->flag & BONE_ADD_PARENT_END_ROLL)) {
1043                 param.roll1 += prev->roll2;
1044         }
1045
1046         param.scaleIn = ebone->scaleIn;
1047         param.scaleOut = ebone->scaleOut;
1048
1049         param.curveInX = ebone->curveInX;
1050         param.curveInY = ebone->curveInY;
1051
1052         param.curveOutX = ebone->curveOutX;
1053         param.curveOutY = ebone->curveOutY;
1054
1055         ebone->segments = BKE_pchan_bbone_spline_compute(&param, (Mat4 *)result_array);
1056 }
1057
1058 static void draw_bone_update_disp_matrix_bbone(EditBone *eBone, bPoseChannel *pchan)
1059 {
1060         float s[4][4], ebmat[4][4];
1061         float length, xwidth, zwidth;
1062         float (*bone_mat)[4];
1063         short bbone_segments;
1064
1065         /* TODO : This should be moved to depsgraph or armature refresh
1066          * and not be tight to the draw pass creation.
1067          * This would refresh armature without invalidating the draw cache */
1068         if (pchan) {
1069                 length = pchan->bone->length;
1070                 xwidth = pchan->bone->xwidth;
1071                 zwidth = pchan->bone->zwidth;
1072                 bone_mat = pchan->pose_mat;
1073                 bbone_segments = pchan->bone->segments;
1074         }
1075         else {
1076                 eBone->length = len_v3v3(eBone->tail, eBone->head);
1077                 ED_armature_ebone_to_mat4(eBone, ebmat);
1078
1079                 length = eBone->length;
1080                 xwidth = eBone->xwidth;
1081                 zwidth = eBone->zwidth;
1082                 bone_mat = ebmat;
1083                 bbone_segments = eBone->segments;
1084         }
1085
1086         size_to_mat4(s, (const float[3]){xwidth, length / bbone_segments, zwidth});
1087
1088         /* Compute BBones segment matrices... */
1089         /* Note that we need this even for one-segment bones, because box drawing need specific weirdo matrix for the box,
1090          * that we cannot use to draw end points & co. */
1091         if (pchan) {
1092                 Mat4 *bbones_mat = (Mat4 *)pchan->draw_data->bbone_matrix;
1093                 if (bbone_segments > 1) {
1094                         if (bbone_segments == pchan->runtime.bbone_segments) {
1095                                 memcpy(bbones_mat, pchan->runtime.bbone_pose_mats, sizeof(Mat4) * bbone_segments);
1096                         }
1097                         else {
1098                                 BKE_pchan_bbone_spline_setup(pchan, false, bbones_mat);
1099                         }
1100
1101                         for (int i = bbone_segments; i--; bbones_mat++) {
1102                                 mul_m4_m4m4(bbones_mat->mat, bbones_mat->mat, s);
1103                                 mul_m4_m4m4(bbones_mat->mat, bone_mat, bbones_mat->mat);
1104                         }
1105                 }
1106                 else {
1107                         mul_m4_m4m4(bbones_mat->mat, bone_mat, s);
1108                 }
1109         }
1110         else {
1111                 float (*bbones_mat)[4][4] = eBone->disp_bbone_mat;
1112
1113                 if (bbone_segments > 1) {
1114                         ebone_spline_preview(eBone, bbones_mat);
1115
1116                         for (int i = bbone_segments; i--; bbones_mat++) {
1117                                 mul_m4_m4m4(*bbones_mat, *bbones_mat, s);
1118                                 mul_m4_m4m4(*bbones_mat, bone_mat, *bbones_mat);
1119                         }
1120                 }
1121                 else {
1122                         mul_m4_m4m4(*bbones_mat, bone_mat, s);
1123                 }
1124         }
1125
1126         /* Grrr... We need default display matrix to draw end points, axes, etc. :( */
1127         draw_bone_update_disp_matrix_default(eBone, pchan);
1128 }
1129
1130 static void draw_bone_update_disp_matrix_custom(bPoseChannel *pchan)
1131 {
1132         float s[4][4];
1133         float length;
1134         float (*bone_mat)[4];
1135         float (*disp_mat)[4];
1136         float (*disp_tail_mat)[4];
1137
1138         /* See TODO above */
1139         length = PCHAN_CUSTOM_DRAW_SIZE(pchan);
1140         bone_mat = pchan->custom_tx ? pchan->custom_tx->pose_mat : pchan->pose_mat;
1141         disp_mat = pchan->disp_mat;
1142         disp_tail_mat = pchan->disp_tail_mat;
1143
1144         scale_m4_fl(s, length);
1145         mul_m4_m4m4(disp_mat, bone_mat, s);
1146         copy_m4_m4(disp_tail_mat, disp_mat);
1147         translate_m4(disp_tail_mat, 0.0f, 1.0f, 0.0f);
1148 }
1149
1150 static void draw_axes(EditBone *eBone, bPoseChannel *pchan)
1151 {
1152         float final_col[4];
1153         const float *col = (g_theme.const_color) ? g_theme.const_color :
1154                            (BONE_FLAG(eBone, pchan) & BONE_SELECTED) ? g_theme.text_hi_color : g_theme.text_color;
1155         copy_v4_v4(final_col, col);
1156         /* Mix with axes color. */
1157         final_col[3] = (g_theme.const_color) ? 1.0 : (BONE_FLAG(eBone, pchan) & BONE_SELECTED) ? 0.3 : 0.8;
1158         drw_shgroup_bone_axes(BONE_VAR(eBone, pchan, disp_mat), final_col);
1159 }
1160
1161 static void draw_points(
1162         const EditBone *eBone, const bPoseChannel *pchan, const bArmature *arm,
1163         const int boneflag, const short constflag,
1164         const int select_id)
1165 {
1166         float col_solid_root[4], col_solid_tail[4], col_wire_root[4], col_wire_tail[4];
1167         float col_hint_root[4], col_hint_tail[4];
1168
1169         copy_v4_v4(col_solid_root, g_theme.bone_solid_color);
1170         copy_v4_v4(col_solid_tail, g_theme.bone_solid_color);
1171         copy_v4_v4(col_wire_root, (g_theme.const_color) ? g_theme.const_color : g_theme.vertex_color);
1172         copy_v4_v4(col_wire_tail, (g_theme.const_color) ? g_theme.const_color : g_theme.vertex_color);
1173
1174         const bool is_envelope_draw = (arm->drawtype == ARM_ENVELOPE);
1175         static const float envelope_ignore = -1.0f;
1176
1177         col_wire_tail[3] = col_wire_root[3] = get_bone_wire_thickness(boneflag);
1178
1179         /* Edit bone points can be selected */
1180         if (eBone) {
1181                 if (eBone->flag & BONE_ROOTSEL) {
1182                         copy_v3_v3(col_wire_root, g_theme.vertex_select_color);
1183                 }
1184                 if (eBone->flag & BONE_TIPSEL) {
1185                         copy_v3_v3(col_wire_tail, g_theme.vertex_select_color);
1186                 }
1187         }
1188         else if (arm->flag & ARM_POSEMODE) {
1189                 const float *solid_color = get_bone_solid_color(eBone, pchan, arm, boneflag, constflag);
1190                 const float *wire_color = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1191                 copy_v4_v4(col_wire_tail, wire_color);
1192                 copy_v4_v4(col_wire_root, wire_color);
1193                 copy_v4_v4(col_solid_tail, solid_color);
1194                 copy_v4_v4(col_solid_root, solid_color);
1195         }
1196
1197         bone_hint_color_shade(col_hint_root, (g_theme.const_color) ? col_solid_root : col_wire_root);
1198         bone_hint_color_shade(col_hint_tail, (g_theme.const_color) ? col_solid_tail : col_wire_tail);
1199
1200         /*      Draw root point if we are not connected and parent are not hidden */
1201         if ((BONE_FLAG(eBone, pchan) & BONE_CONNECTED) == 0) {
1202                 if (select_id != -1) {
1203                         DRW_select_load_id(select_id | BONESEL_ROOT);
1204                 }
1205
1206                 if (eBone) {
1207                         if (!((eBone->parent) && !EBONE_VISIBLE(arm, eBone->parent))) {
1208                                 if (is_envelope_draw) {
1209                                         drw_shgroup_bone_envelope(
1210                                                 eBone->disp_mat, col_solid_root, col_hint_root, col_wire_root,
1211                                                 &eBone->rad_head, &envelope_ignore);
1212                                 }
1213                                 else {
1214                                         drw_shgroup_bone_point(eBone->disp_mat, col_solid_root, col_hint_root, col_wire_root);
1215                                 }
1216                         }
1217                 }
1218                 else {
1219                         Bone *bone = pchan->bone;
1220                         if (!((bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)))) {
1221                                 if (is_envelope_draw) {
1222                                         drw_shgroup_bone_envelope(
1223                                                 pchan->disp_mat, col_solid_root, col_hint_root, col_wire_root,
1224                                                 &bone->rad_head, &envelope_ignore);
1225                                 }
1226                                 else {
1227                                         drw_shgroup_bone_point(pchan->disp_mat, col_solid_root, col_hint_root, col_wire_root);
1228                                 }
1229                         }
1230                 }
1231         }
1232
1233         /*      Draw tip point */
1234         if (select_id != -1) {
1235                 DRW_select_load_id(select_id | BONESEL_TIP);
1236         }
1237
1238         if (is_envelope_draw) {
1239                 const float *rad_tail = eBone ? &eBone->rad_tail : &pchan->bone->rad_tail;
1240                 drw_shgroup_bone_envelope(
1241                         BONE_VAR(eBone, pchan, disp_mat), col_solid_tail, col_hint_tail, col_wire_tail,
1242                         &envelope_ignore, rad_tail);
1243         }
1244         else {
1245                 drw_shgroup_bone_point(BONE_VAR(eBone, pchan, disp_tail_mat), col_solid_tail, col_hint_tail, col_wire_tail);
1246         }
1247
1248         if (select_id != -1) {
1249                 DRW_select_load_id(-1);
1250         }
1251 }
1252
1253 /** \} */
1254
1255
1256 /* -------------------------------------------------------------------- */
1257
1258 /** \name Draw Bones
1259  * \{ */
1260
1261 static void draw_bone_custom_shape(
1262         EditBone *eBone, bPoseChannel *pchan, bArmature *arm,
1263         const int boneflag, const short constflag,
1264         const int select_id)
1265 {
1266         const float *col_solid = get_bone_solid_color(eBone, pchan, arm, boneflag, constflag);
1267         const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1268         const float *col_hint = get_bone_hint_color(eBone, pchan, arm, boneflag, constflag);
1269         const float (*disp_mat)[4] = pchan->disp_mat;
1270
1271         if (select_id != -1) {
1272                 DRW_select_load_id(select_id | BONESEL_BONE);
1273         }
1274
1275         if ((boneflag & BONE_DRAWWIRE) == 0) {
1276                 drw_shgroup_bone_custom_solid(disp_mat, col_solid, col_hint, col_wire, pchan->custom);
1277         }
1278         else {
1279                 drw_shgroup_bone_custom_wire(disp_mat, col_wire, pchan->custom);
1280         }
1281
1282         if (select_id != -1) {
1283                 DRW_select_load_id(-1);
1284         }
1285 }
1286
1287 static void draw_bone_envelope(
1288         EditBone *eBone, bPoseChannel *pchan, bArmature *arm,
1289         const int boneflag, const short constflag,
1290         const int select_id)
1291 {
1292         const float *col_solid = get_bone_solid_with_consts_color(eBone, pchan, arm, boneflag, constflag);
1293         const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1294         const float *col_hint = get_bone_hint_color(eBone, pchan, arm, boneflag, constflag);
1295
1296         float *rad_head, *rad_tail, *distance;
1297         if (eBone) {
1298                 rad_tail = &eBone->rad_tail;
1299                 distance = &eBone->dist;
1300                 rad_head = (eBone->parent && (boneflag & BONE_CONNECTED)) ? &eBone->parent->rad_tail : &eBone->rad_head;
1301         }
1302         else {
1303                 rad_tail = &pchan->bone->rad_tail;
1304                 distance = &pchan->bone->dist;
1305                 rad_head = (pchan->parent && (boneflag & BONE_CONNECTED)) ? &pchan->parent->bone->rad_tail : &pchan->bone->rad_head;
1306         }
1307
1308         if ((select_id == -1) &&
1309             (boneflag & BONE_NO_DEFORM) == 0 &&
1310             ((boneflag & BONE_SELECTED) || (eBone && (boneflag & (BONE_ROOTSEL | BONE_TIPSEL)))))
1311         {
1312                 drw_shgroup_bone_envelope_distance(BONE_VAR(eBone, pchan, disp_mat), rad_head, rad_tail, distance);
1313         }
1314
1315         if (select_id != -1) {
1316                 DRW_select_load_id(select_id | BONESEL_BONE);
1317         }
1318
1319         drw_shgroup_bone_envelope(
1320                 BONE_VAR(eBone, pchan, disp_mat), col_solid, col_hint, col_wire,
1321                 rad_head, rad_tail);
1322
1323         if (select_id != -1) {
1324                 DRW_select_load_id(-1);
1325         }
1326
1327         draw_points(eBone, pchan, arm, boneflag, constflag, select_id);
1328 }
1329
1330 static void draw_bone_line(
1331         EditBone *eBone, bPoseChannel *pchan, bArmature *arm,
1332         const int boneflag, const short constflag, const int select_id)
1333 {
1334         const float *col_bone = get_bone_solid_with_consts_color(eBone, pchan, arm, boneflag, constflag);
1335         const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1336         const float no_display[4] = {0.0f, 0.0f, 0.0f, 0.0f};
1337         const float *col_head = no_display;
1338         const float *col_tail = col_bone;
1339
1340         if (eBone) {
1341                 if (eBone->flag & BONE_TIPSEL) {
1342                         col_tail = g_theme.vertex_select_color;
1343                 }
1344                 if (boneflag & BONE_SELECTED) {
1345                         col_bone = g_theme.edge_select_color;
1346                 }
1347                 col_wire = g_theme.wire_color;
1348         }
1349
1350         /*      Draw root point if we are not connected and parent are not hidden */
1351         if ((BONE_FLAG(eBone, pchan) & BONE_CONNECTED) == 0) {
1352                 if (eBone && !(eBone->parent && !EBONE_VISIBLE(arm, eBone->parent))) {
1353                         col_head = (eBone->flag & BONE_ROOTSEL) ? g_theme.vertex_select_color : col_bone;
1354                 }
1355                 else if (pchan) {
1356                         Bone *bone = pchan->bone;
1357                         if (!(bone->parent && (bone->parent->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)))) {
1358                                 col_head = col_bone;
1359                         }
1360                 }
1361         }
1362
1363         if (g_theme.const_color != NULL) {
1364                 col_wire = no_display; /* actually shrink the display. */
1365                 col_bone = col_head = col_tail = g_theme.const_color;
1366         }
1367
1368         if (select_id == -1) {
1369                 /* Not in selection mode, draw everything at once. */
1370                 drw_shgroup_bone_stick(BONE_VAR(eBone, pchan, disp_mat), col_wire, col_bone, col_head, col_tail);
1371         }
1372         else {
1373                 /* In selection mode, draw bone, root and tip separately. */
1374                 DRW_select_load_id(select_id | BONESEL_BONE);
1375                 drw_shgroup_bone_stick(BONE_VAR(eBone, pchan, disp_mat), col_wire, col_bone, no_display, no_display);
1376
1377                 if (col_head[3] > 0.0f) {
1378                         DRW_select_load_id(select_id | BONESEL_ROOT);
1379                         drw_shgroup_bone_stick(BONE_VAR(eBone, pchan, disp_mat), col_wire, no_display, col_head, no_display);
1380                 }
1381
1382                 DRW_select_load_id(select_id | BONESEL_TIP);
1383                 drw_shgroup_bone_stick(BONE_VAR(eBone, pchan, disp_mat), col_wire, no_display, no_display, col_tail);
1384
1385                 DRW_select_load_id(-1);
1386         }
1387 }
1388
1389 static void draw_bone_wire(
1390         EditBone *eBone, bPoseChannel *pchan, bArmature *arm,
1391         const int boneflag, const short constflag,
1392         const int select_id)
1393 {
1394         const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1395
1396         if (select_id != -1) {
1397                 DRW_select_load_id(select_id | BONESEL_BONE);
1398         }
1399
1400         if (pchan) {
1401                 Mat4 *bbones_mat = (Mat4 *)pchan->draw_data->bbone_matrix;
1402                 BLI_assert(bbones_mat != NULL);
1403
1404                 for (int i = pchan->bone->segments; i--; bbones_mat++) {
1405                         drw_shgroup_bone_wire(bbones_mat->mat, col_wire);
1406                 }
1407         }
1408         else if (eBone) {
1409                 for (int i = 0; i < eBone->segments; i++) {
1410                         drw_shgroup_bone_wire(eBone->disp_bbone_mat[i], col_wire);
1411                 }
1412         }
1413
1414         if (select_id != -1) {
1415                 DRW_select_load_id(-1);
1416         }
1417
1418         if (eBone) {
1419                 draw_points(eBone, pchan, arm, boneflag, constflag, select_id);
1420         }
1421 }
1422
1423 static void draw_bone_box(
1424         EditBone *eBone, bPoseChannel *pchan, bArmature *arm,
1425         const int boneflag, const short constflag,
1426         const int select_id)
1427 {
1428         const float *col_solid = get_bone_solid_with_consts_color(eBone, pchan, arm, boneflag, constflag);
1429         const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1430         const float *col_hint = get_bone_hint_color(eBone, pchan, arm, boneflag, constflag);
1431
1432         if (select_id != -1) {
1433                 DRW_select_load_id(select_id | BONESEL_BONE);
1434         }
1435
1436         if (pchan) {
1437                 Mat4 *bbones_mat = (Mat4 *)pchan->draw_data->bbone_matrix;
1438                 BLI_assert(bbones_mat != NULL);
1439
1440                 for (int i = pchan->bone->segments; i--; bbones_mat++) {
1441                         drw_shgroup_bone_box(bbones_mat->mat, col_solid, col_hint, col_wire);
1442                 }
1443         }
1444         else if (eBone) {
1445                 for (int i = 0; i < eBone->segments; i++) {
1446                         drw_shgroup_bone_box(eBone->disp_bbone_mat[i], col_solid, col_hint, col_wire);
1447                 }
1448         }
1449
1450         if (select_id != -1) {
1451                 DRW_select_load_id(-1);
1452         }
1453
1454         if (eBone) {
1455                 draw_points(eBone, pchan, arm, boneflag, constflag, select_id);
1456         }
1457 }
1458
1459 static void draw_bone_octahedral(
1460         EditBone *eBone, bPoseChannel *pchan, bArmature *arm,
1461         const int boneflag, const short constflag,
1462         const int select_id)
1463 {
1464         const float *col_solid = get_bone_solid_with_consts_color(eBone, pchan, arm, boneflag, constflag);
1465         const float *col_wire = get_bone_wire_color(eBone, pchan, arm, boneflag, constflag);
1466         const float *col_hint = get_bone_hint_color(eBone, pchan, arm, boneflag, constflag);
1467
1468         if (select_id != -1) {
1469                 DRW_select_load_id(select_id | BONESEL_BONE);
1470         }
1471
1472         drw_shgroup_bone_octahedral(BONE_VAR(eBone, pchan, disp_mat), col_solid, col_hint, col_wire);
1473
1474         if (select_id != -1) {
1475                 DRW_select_load_id(-1);
1476         }
1477
1478         draw_points(eBone, pchan, arm, boneflag, constflag, select_id);
1479 }
1480
1481 /** \} */
1482
1483
1484 /* -------------------------------------------------------------------- */
1485
1486 /** \name Draw Degrees of Freedom
1487  * \{ */
1488
1489 static void draw_bone_dofs(bPoseChannel *pchan)
1490 {
1491         float final_bonemat[4][4], posetrans[4][4], mat[4][4];
1492         float amin[2], amax[2], xminmax[2], zminmax[2];
1493         float col_sphere[4] = {0.25f, 0.25f, 0.25f, 0.25f};
1494         float col_lines[4] = {0.0f, 0.0f, 0.0f, 1.0f};
1495         float col_xaxis[4] = {1.0f, 0.0f, 0.0f, 1.0f};
1496         float col_zaxis[4] = {0.0f, 0.0f, 1.0f, 1.0f};
1497
1498         if (g_data.passes.bone_envelope == NULL) {
1499                 return;
1500         }
1501
1502         if (g_data.bone_dof_sphere == NULL) {
1503                 g_data.bone_dof_lines = shgroup_instance_bone_dof(g_data.passes.bone_wire, DRW_cache_bone_dof_lines_get());
1504                 g_data.bone_dof_sphere = shgroup_instance_bone_dof(g_data.passes.bone_envelope, DRW_cache_bone_dof_sphere_get());
1505                 DRW_shgroup_state_enable(g_data.bone_dof_sphere, DRW_STATE_BLEND);
1506                 DRW_shgroup_state_disable(g_data.bone_dof_sphere, DRW_STATE_CULL_FRONT);
1507         }
1508
1509         /* *0.5f here comes from M_PI/360.0f when rotations were still in degrees */
1510         xminmax[0] = sinf(pchan->limitmin[0] * 0.5f);
1511         xminmax[1] = sinf(pchan->limitmax[0] * 0.5f);
1512         zminmax[0] = sinf(pchan->limitmin[2] * 0.5f);
1513         zminmax[1] = sinf(pchan->limitmax[2] * 0.5f);
1514
1515         unit_m4(posetrans);
1516         translate_m4(posetrans, pchan->pose_mat[3][0], pchan->pose_mat[3][1], pchan->pose_mat[3][2]);
1517         /* in parent-bone pose space... */
1518         if (pchan->parent) {
1519                 copy_m4_m4(mat, pchan->parent->pose_mat);
1520                 mat[3][0] = mat[3][1] = mat[3][2] = 0.0f;
1521                 mul_m4_m4m4(posetrans, posetrans, mat);
1522         }
1523         /* ... but own restspace */
1524         mul_m4_m4m3(posetrans, posetrans, pchan->bone->bone_mat);
1525
1526         float scale = pchan->bone->length * pchan->size[1];
1527         scale_m4_fl(mat, scale);
1528         mat[1][1] = -mat[1][1];
1529         mul_m4_m4m4(posetrans, posetrans, mat);
1530
1531         /* into world space. */
1532         mul_m4_m4m4(final_bonemat, g_data.ob->obmat, posetrans);
1533
1534         if ((pchan->ikflag & BONE_IK_XLIMIT) &&
1535             (pchan->ikflag & BONE_IK_ZLIMIT))
1536         {
1537                 amin[0] = xminmax[0];
1538                 amax[0] = xminmax[1];
1539                 amin[1] = zminmax[0];
1540                 amax[1] = zminmax[1];
1541                 DRW_shgroup_call_dynamic_add(g_data.bone_dof_sphere, final_bonemat, col_sphere, amin, amax);
1542                 DRW_shgroup_call_dynamic_add(g_data.bone_dof_lines, final_bonemat, col_lines, amin, amax);
1543         }
1544         if (pchan->ikflag & BONE_IK_XLIMIT) {
1545                 amin[0] = xminmax[0];
1546                 amax[0] = xminmax[1];
1547                 amin[1] = amax[1] = 0.0f;
1548                 DRW_shgroup_call_dynamic_add(g_data.bone_dof_lines, final_bonemat, col_xaxis, amin, amax);
1549         }
1550         if (pchan->ikflag & BONE_IK_ZLIMIT) {
1551                 amin[1] = zminmax[0];
1552                 amax[1] = zminmax[1];
1553                 amin[0] = amax[0] = 0.0f;
1554                 DRW_shgroup_call_dynamic_add(g_data.bone_dof_lines, final_bonemat, col_zaxis, amin, amax);
1555         }
1556 }
1557
1558 /** \} */
1559
1560
1561 /* -------------------------------------------------------------------- */
1562
1563 /** \name Draw Relationships
1564  * \{ */
1565
1566 static void pchan_draw_ik_lines(bPoseChannel *pchan, const bool only_temp, const int constflag)
1567 {
1568         bConstraint *con;
1569         bPoseChannel *parchan;
1570         float *line_start = NULL, *line_end = NULL;
1571
1572         for (con = pchan->constraints.first; con; con = con->next) {
1573                 if (con->enforce == 0.0f)
1574                         continue;
1575
1576                 switch (con->type) {
1577                         case CONSTRAINT_TYPE_KINEMATIC:
1578                         {
1579                                 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
1580                                 int segcount = 0;
1581
1582                                 /* if only_temp, only draw if it is a temporary ik-chain */
1583                                 if (only_temp && !(data->flag & CONSTRAINT_IK_TEMP))
1584                                         continue;
1585
1586                                 /* exclude tip from chain? */
1587                                 parchan = ((data->flag & CONSTRAINT_IK_TIP) == 0) ? pchan->parent : pchan;
1588                                 line_start = parchan->pose_tail;
1589
1590                                 /* Find the chain's root */
1591                                 while (parchan->parent) {
1592                                         segcount++;
1593                                         if (segcount == data->rootbone || segcount > 255) {
1594                                                 break;  /* 255 is weak */
1595                                         }
1596                                         parchan = parchan->parent;
1597                                 }
1598
1599                                 if (parchan) {
1600                                         line_end = parchan->pose_head;
1601
1602                                         if (constflag & PCHAN_HAS_TARGET)
1603                                                 drw_shgroup_bone_ik_lines(line_start, line_end);
1604                                         else
1605                                                 drw_shgroup_bone_ik_no_target_lines(line_start, line_end);
1606                                 }
1607                                 break;
1608                         }
1609                         case CONSTRAINT_TYPE_SPLINEIK:
1610                         {
1611                                 bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
1612                                 int segcount = 0;
1613
1614                                 /* don't draw if only_temp, as Spline IK chains cannot be temporary */
1615                                 if (only_temp)
1616                                         continue;
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
1681 /** \name Main Draw Loops
1682  * \{ */
1683
1684 static void draw_armature_edit(Object *ob)
1685 {
1686         const DRWContextState *draw_ctx = DRW_context_state_get();
1687         EditBone *eBone;
1688         bArmature *arm = ob->data;
1689         int index;
1690         const bool is_select = DRW_state_is_select();
1691
1692         update_color(ob, NULL);
1693         edbo_compute_bbone_child(arm);
1694
1695         const bool show_text = DRW_state_show_text();
1696         const bool show_relations = ((draw_ctx->v3d->flag & V3D_HIDE_HELPLINES) == 0);
1697
1698         for (eBone = arm->edbo->first, index = ob->select_color; eBone; eBone = eBone->next, index += 0x10000) {
1699                 if (eBone->layer & arm->layer) {
1700                         if ((eBone->flag & BONE_HIDDEN_A) == 0) {
1701                                 const int select_id = is_select ? index : (uint)-1;
1702
1703                                 const short constflag = 0;
1704
1705                                 /* catch exception for bone with hidden parent */
1706                                 int boneflag = eBone->flag;
1707                                 if ((eBone->parent) && !EBONE_VISIBLE(arm, eBone->parent)) {
1708                                         boneflag &= ~BONE_CONNECTED;
1709                                 }
1710
1711                                 /* set temporary flag for drawing bone as active, but only if selected */
1712                                 if (eBone == arm->act_edbone) {
1713                                         boneflag |= BONE_DRAW_ACTIVE;
1714                                 }
1715
1716                                 draw_bone_relations(eBone, NULL, arm, boneflag, constflag, show_relations);
1717
1718                                 if (arm->drawtype == ARM_ENVELOPE) {
1719                                         draw_bone_update_disp_matrix_default(eBone, NULL);
1720                                         draw_bone_envelope(eBone, NULL, arm, boneflag, constflag, select_id);
1721                                 }
1722                                 else if (arm->drawtype == ARM_LINE) {
1723                                         draw_bone_update_disp_matrix_default(eBone, NULL);
1724                                         draw_bone_line(eBone, NULL, arm, boneflag, constflag, select_id);
1725                                 }
1726                                 else if (arm->drawtype == ARM_WIRE) {
1727                                         draw_bone_update_disp_matrix_bbone(eBone, NULL);
1728                                         draw_bone_wire(eBone, NULL, arm, boneflag, constflag, select_id);
1729                                 }
1730                                 else if (arm->drawtype == ARM_B_BONE) {
1731                                         draw_bone_update_disp_matrix_bbone(eBone, NULL);
1732                                         draw_bone_box(eBone, NULL, arm, boneflag, constflag, select_id);
1733                                 }
1734                                 else {
1735                                         draw_bone_update_disp_matrix_default(eBone, NULL);
1736                                         draw_bone_octahedral(eBone, NULL, arm, boneflag, constflag, select_id);
1737                                 }
1738
1739                                 /* Draw names of bone */
1740                                 if (show_text && (arm->flag & ARM_DRAWNAMES)) {
1741                                         uchar color[4];
1742                                         UI_GetThemeColor4ubv((eBone->flag & BONE_SELECTED) ? TH_TEXT_HI : TH_TEXT, color);
1743
1744                                         float vec[3];
1745                                         mid_v3_v3v3(vec, eBone->head, eBone->tail);
1746                                         mul_m4_v3(ob->obmat, vec);
1747
1748                                         struct DRWTextStore *dt = DRW_text_cache_ensure();
1749                                         DRW_text_cache_add(
1750                                                 dt, vec, eBone->name, strlen(eBone->name),
1751                                                 10, 0, DRW_TEXT_CACHE_GLOBALSPACE | DRW_TEXT_CACHE_STRING_PTR, color);
1752                                 }
1753
1754                                 /*      Draw additional axes */
1755                                 if (arm->flag & ARM_DRAWAXES) {
1756                                         draw_axes(eBone, NULL);
1757                                 }
1758                         }
1759                 }
1760         }
1761 }
1762
1763 /* if const_color is NULL do pose mode coloring */
1764 static void draw_armature_pose(Object *ob, const float const_color[4])
1765 {
1766         const DRWContextState *draw_ctx = DRW_context_state_get();
1767         bArmature *arm = ob->data;
1768         bPoseChannel *pchan;
1769         int index = -1;
1770         Bone *bone;
1771
1772         update_color(ob, const_color);
1773
1774         /* We can't safely draw non-updated pose, might contain NULL bone pointers... */
1775         if (ob->pose->flag & POSE_RECALC) {
1776                 return;
1777         }
1778
1779         // if (!(base->flag & OB_FROMDUPLI)) // TODO
1780         {
1781                 if ((draw_ctx->object_mode & OB_MODE_POSE) || (ob == draw_ctx->object_pose)) {
1782                         arm->flag |= ARM_POSEMODE;
1783                 }
1784
1785                 if (arm->flag & ARM_POSEMODE) {
1786                         index = ob->select_color;
1787                 }
1788         }
1789
1790         const bool is_pose_select = (arm->flag & ARM_POSEMODE) && DRW_state_is_select();
1791         const bool show_text = DRW_state_show_text();
1792         const bool show_relations = ((draw_ctx->v3d->flag & V3D_HIDE_HELPLINES) == 0);
1793
1794         /* being set below */
1795         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1796                 bone = pchan->bone;
1797
1798                 /* bone must be visible */
1799                 if ((bone->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)) == 0) {
1800                         if (bone->layer & arm->layer) {
1801                                 const int select_id = is_pose_select ? index : (uint)-1;
1802
1803                                 const short constflag = pchan->constflag;
1804
1805                                 pchan_draw_data_init(pchan);
1806
1807                                 if (const_color) {
1808                                         /* keep color */
1809                                 }
1810                                 else {
1811                                         /* set color-set to use */
1812                                         set_pchan_colorset(ob, pchan);
1813                                 }
1814
1815                                 int boneflag = bone->flag;
1816                                 /* catch exception for bone with hidden parent */
1817                                 boneflag = bone->flag;
1818                                 if ((bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG))) {
1819                                         boneflag &= ~BONE_CONNECTED;
1820                                 }
1821
1822                                 /* set temporary flag for drawing bone as active, but only if selected */
1823                                 if (bone == arm->act_bone)
1824                                         boneflag |= BONE_DRAW_ACTIVE;
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, 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, 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, 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, select_id);
1847                                 }
1848                                 else {
1849                                         draw_bone_update_disp_matrix_default(NULL, pchan);
1850                                         draw_bone_octahedral(NULL, pchan, arm, boneflag, constflag, 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_FROMDUPLI) == 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 /** \} */