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