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