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