Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_view3d / drawarmature.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 by the Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_view3d/drawarmature.c
29  *  \ingroup spview3d
30  */
31
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36
37 #include "DNA_anim_types.h"
38 #include "DNA_armature_types.h"
39 #include "DNA_constraint_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_view3d_types.h"
43 #include "DNA_object_types.h"
44
45 #include "BLI_blenlib.h"
46 #include "BLI_math.h"
47 #include "BLI_dlrbTree.h"
48 #include "BLI_utildefines.h"
49
50 #include "BKE_animsys.h"
51 #include "BKE_action.h"
52 #include "BKE_armature.h"
53 #include "BKE_global.h"
54 #include "BKE_modifier.h"
55 #include "BKE_nla.h"
56 #include "BKE_curve.h"
57
58
59 #include "BIF_gl.h"
60 #include "BIF_glutil.h"
61
62 #include "ED_armature.h"
63 #include "ED_keyframes_draw.h"
64
65 #include "GPU_basic_shader.h"
66 #include "GPU_batch.h"
67 #include "GPU_immediate.h"
68 #include "GPU_matrix.h"
69
70 #include "UI_resources.h"
71
72 #include "view3d_intern.h"
73
74 #include "GPU_select.h"
75
76 /* *************** Armature Drawing - Coloring API ***************************** */
77
78 /* global here is reset before drawing each bone */
79 static ThemeWireColor *bcolor = NULL;
80 static float fcolor[4] = {0.0f};
81 static bool flat_color;
82
83 /* values of colCode for set_pchan_color */
84 enum {
85         PCHAN_COLOR_NORMAL  = 0,        /* normal drawing */
86         PCHAN_COLOR_SOLID,              /* specific case where "solid" color is needed */
87         PCHAN_COLOR_CONSTS,             /* "constraint" colors (which may/may-not be suppressed) */
88
89         PCHAN_COLOR_SPHEREBONE_BASE,    /* for the 'stick' of sphere (envelope) bones */
90         PCHAN_COLOR_SPHEREBONE_END,     /* for the ends of sphere (envelope) bones */
91         PCHAN_COLOR_LINEBONE            /* for the middle of line-bones */
92 };
93
94 /* This function sets the color-set for coloring a certain bone */
95 static void set_pchan_colorset(Object *ob, bPoseChannel *pchan)
96 {
97         bPose *pose = (ob) ? ob->pose : NULL;
98         bArmature *arm = (ob) ? ob->data : NULL;
99         bActionGroup *grp = NULL;
100         short color_index = 0;
101         
102         /* sanity check */
103         if (ELEM(NULL, ob, arm, pose, pchan)) {
104                 bcolor = NULL;
105                 return;
106         }
107         
108         /* only try to set custom color if enabled for armature */
109         if (arm->flag & ARM_COL_CUSTOM) {
110                 /* currently, a bone can only use a custom color set if it's group (if it has one),
111                  * has been set to use one
112                  */
113                 if (pchan->agrp_index) {
114                         grp = (bActionGroup *)BLI_findlink(&pose->agroups, (pchan->agrp_index - 1));
115                         if (grp)
116                                 color_index = grp->customCol;
117                 }
118         }
119         
120         /* bcolor is a pointer to the color set to use. If NULL, then the default
121          * color set (based on the theme colors for 3d-view) is used. 
122          */
123         if (color_index > 0) {
124                 bTheme *btheme = UI_GetTheme();
125                 bcolor = &btheme->tarm[(color_index - 1)];
126         }
127         else if (color_index == -1) {
128                 /* use the group's own custom color set (grp is always != NULL here) */
129                 bcolor = &grp->cs;
130         }
131         else {
132                 bcolor = NULL;
133         }
134 }
135
136 /* This function is for brightening/darkening a given color (like UI_GetThemeColorShade3ubv()) */
137 static void cp_shade_color3ub(unsigned char cp[3], const int offset)
138 {
139         int r, g, b;
140         
141         r = offset + (int) cp[0];
142         CLAMP(r, 0, 255);
143         g = offset + (int) cp[1];
144         CLAMP(g, 0, 255);
145         b = offset + (int) cp[2];
146         CLAMP(b, 0, 255);
147         
148         cp[0] = r;
149         cp[1] = g;
150         cp[2] = b;
151 }
152
153 /* This function sets the gl-color for coloring a certain bone (based on bcolor) */
154 static bool set_pchan_color(short colCode, int boneflag, short constflag)
155 {
156         switch (colCode) {
157                 case PCHAN_COLOR_NORMAL:
158                 {
159                         if (bcolor) {
160                                 unsigned char cp[4] = {255};
161                         
162                                 if (boneflag & BONE_DRAW_ACTIVE) {
163                                         copy_v3_v3_char((char *)cp, bcolor->active);
164                                         if (!(boneflag & BONE_SELECTED)) {
165                                                 cp_shade_color3ub(cp, -80);
166                                         }
167                                 }
168                                 else if (boneflag & BONE_SELECTED) {
169                                         copy_v3_v3_char((char *)cp, bcolor->select);
170                                 }
171                                 else {
172                                         /* a bit darker than solid */
173                                         copy_v3_v3_char((char *)cp, bcolor->solid);
174                                         cp_shade_color3ub(cp, -50);
175                                 }
176                         
177                                 rgb_uchar_to_float(fcolor, cp);
178                         }
179                         else {
180                                 if ((boneflag & BONE_DRAW_ACTIVE) && (boneflag & BONE_SELECTED)) {
181                                         UI_GetThemeColor4fv(TH_BONE_POSE_ACTIVE, fcolor);
182                                 }
183                                 else if (boneflag & BONE_DRAW_ACTIVE) {
184                                         UI_GetThemeColorBlendShade4fv(TH_WIRE, TH_BONE_POSE, 0.15f, 0, fcolor);
185                                 }
186                                 else if (boneflag & BONE_SELECTED) {
187                                         UI_GetThemeColor4fv(TH_BONE_POSE, fcolor);
188                                 }
189                                 else {
190                                         UI_GetThemeColor4fv(TH_WIRE, fcolor);
191                                 }
192                         }
193         
194                         return true;
195                 }
196                 case PCHAN_COLOR_SOLID:
197                 {
198                         if (bcolor) {
199                                 rgb_uchar_to_float(fcolor, (unsigned char *)bcolor->solid);
200                         }
201                         else {
202                                 UI_GetThemeColor4fv(TH_BONE_SOLID, fcolor);
203                         }
204                         
205                         return true;
206                 }
207                 case PCHAN_COLOR_CONSTS:
208                 {
209                         if ((bcolor == NULL) || (bcolor->flag & TH_WIRECOLOR_CONSTCOLS)) {
210                                 unsigned char cp[4];
211                                 if (constflag & PCHAN_HAS_TARGET) rgba_char_args_set((char *)cp, 255, 150, 0, 80);
212                                 else if (constflag & PCHAN_HAS_IK) rgba_char_args_set((char *)cp, 255, 255, 0, 80);
213                                 else if (constflag & PCHAN_HAS_SPLINEIK) rgba_char_args_set((char *)cp, 200, 255, 0, 80);
214                                 else if (constflag & PCHAN_HAS_CONST) rgba_char_args_set((char *)cp, 0, 255, 120, 80);
215
216                                 rgba_uchar_to_float(fcolor, cp);
217
218                                 return true;
219                         }
220                         return false;
221                 }
222                 case PCHAN_COLOR_SPHEREBONE_BASE:
223                 {
224                         if (bcolor) {
225                                 unsigned char cp[4] = {255};
226
227                                 if (boneflag & BONE_DRAW_ACTIVE) {
228                                         copy_v3_v3_char((char *)cp, bcolor->active);
229                                 }
230                                 else if (boneflag & BONE_SELECTED) {
231                                         copy_v3_v3_char((char *)cp, bcolor->select);
232                                 }
233                                 else {
234                                         copy_v3_v3_char((char *)cp, bcolor->solid);
235                                 }
236
237                                 rgb_uchar_to_float(fcolor, cp);
238                         }
239                         else {
240                                 if (boneflag & BONE_DRAW_ACTIVE) {
241                                         UI_GetThemeColorShade4fv(TH_BONE_POSE, 40, fcolor);
242                                 }
243                                 else if (boneflag & BONE_SELECTED) {
244                                         UI_GetThemeColor4fv(TH_BONE_POSE, fcolor);
245                                 }
246                                 else {
247                                         UI_GetThemeColor4fv(TH_BONE_SOLID, fcolor);
248                                 }
249                         }
250                         
251                         return true;
252                 }
253                 case PCHAN_COLOR_SPHEREBONE_END:
254                 {
255                         if (bcolor) {
256                                 unsigned char cp[4] = {255};
257
258                                 if (boneflag & BONE_DRAW_ACTIVE) {
259                                         copy_v3_v3_char((char *)cp, bcolor->active);
260                                         cp_shade_color3ub(cp, 10);
261                                 }
262                                 else if (boneflag & BONE_SELECTED) {
263                                         copy_v3_v3_char((char *)cp, bcolor->select);
264                                         cp_shade_color3ub(cp, -30);
265                                 }
266                                 else {
267                                         copy_v3_v3_char((char *)cp, bcolor->solid);
268                                         cp_shade_color3ub(cp, -30);
269                                 }
270                         
271                                 rgb_uchar_to_float(fcolor, cp);
272                         }
273                         else {
274                                 if (boneflag & BONE_DRAW_ACTIVE) {
275                                         UI_GetThemeColorShade4fv(TH_BONE_POSE, 10, fcolor);
276                                 }
277                                 else if (boneflag & BONE_SELECTED) {
278                                         UI_GetThemeColorShade4fv(TH_BONE_POSE, -30, fcolor);
279                                 }
280                                 else {
281                                         UI_GetThemeColorShade4fv(TH_BONE_SOLID, -30, fcolor);
282                                 }
283                         }
284                         break;
285                 }
286                 case PCHAN_COLOR_LINEBONE:
287                 {
288                         /* inner part in background color or constraint */
289                         if ((constflag) && ((bcolor == NULL) || (bcolor->flag & TH_WIRECOLOR_CONSTCOLS))) {
290                                 unsigned char cp[4];
291                                 if (constflag & PCHAN_HAS_TARGET) rgba_char_args_set((char *)cp, 255, 150, 0, 255);
292                                 else if (constflag & PCHAN_HAS_IK) rgba_char_args_set((char *)cp, 255, 255, 0, 255);
293                                 else if (constflag & PCHAN_HAS_SPLINEIK) rgba_char_args_set((char *)cp, 200, 255, 0, 255);
294                                 else if (constflag & PCHAN_HAS_CONST) rgba_char_args_set((char *)cp, 0, 255, 120, 255);
295                                 else if (constflag) UI_GetThemeColor4ubv(TH_BONE_POSE, cp);  /* PCHAN_HAS_ACTION */
296
297                                 rgb_uchar_to_float(fcolor, cp);
298                         }
299                         else {
300                                 if (bcolor) {
301                                         const char *cp = bcolor->solid;
302                                         rgb_uchar_to_float(fcolor, (unsigned char *)cp);
303                                         fcolor[3] = 204.f / 255.f;
304                                 }
305                                 else {
306                                         UI_GetThemeColorShade4fv(TH_BACK, -30, fcolor);
307                                 }
308                         }
309                 
310                         return true;
311                 }
312         }
313         
314         return false;
315 }
316
317 static void set_ebone_color(const unsigned int boneflag)
318 {
319         if ((boneflag & BONE_DRAW_ACTIVE) && (boneflag & BONE_SELECTED)) {
320                 UI_GetThemeColor4fv(TH_EDGE_SELECT, fcolor);
321         }
322         else if (boneflag & BONE_DRAW_ACTIVE) {
323                 UI_GetThemeColorBlendShade4fv(TH_WIRE_EDIT, TH_EDGE_SELECT, 0.15f, 0, fcolor);
324         }
325         else if (boneflag & BONE_SELECTED) {
326                 UI_GetThemeColorShade4fv(TH_EDGE_SELECT, -20, fcolor);
327         }
328         else {
329                 UI_GetThemeColor4fv(TH_WIRE_EDIT, fcolor);
330         }
331 }
332
333 /* *************** Armature drawing, helper calls for parts ******************* */
334
335 static void add_solid_flat_triangle(VertexBuffer *vbo, unsigned int *vertex, unsigned int pos, unsigned int nor,
336                                     const float p1[3], const float p2[3], const float p3[3], const float n[3])
337 {
338         setAttrib(vbo, nor, *vertex, n);
339         setAttrib(vbo, pos, (*vertex)++, p1);
340         setAttrib(vbo, nor, *vertex, n);
341         setAttrib(vbo, pos, (*vertex)++, p2);
342         setAttrib(vbo, nor, *vertex, n);
343         setAttrib(vbo, pos, (*vertex)++, p3);
344 }
345
346 /* half the cube, in Y */
347 static const float cube_vert[8][3] = {
348         {-1.0,  0.0, -1.0},
349         {-1.0,  0.0,  1.0},
350         {-1.0,  1.0,  1.0},
351         {-1.0,  1.0, -1.0},
352         { 1.0,  0.0, -1.0},
353         { 1.0,  0.0,  1.0},
354         { 1.0,  1.0,  1.0},
355         { 1.0,  1.0, -1.0},
356 };
357
358 static const float cube_wire[24] = {
359         0, 1, 1, 2, 2, 3, 3, 0,
360         4, 5, 5, 6, 6, 7, 7, 4,
361         0, 4, 1, 5, 2, 6, 3, 7,
362 };
363
364 static void drawsolidcube_size(float xsize, float ysize, float zsize)
365 {
366         static VertexFormat format = {0};
367         static VertexBuffer vbo = {{0}};
368         static Batch batch = {{0}};
369         const float light_vec[3] = {0.0f, 0.0f, 1.0f};
370
371         if (format.attrib_ct == 0) {
372                 unsigned int i = 0;
373                 float n[3] = {0.0f};
374                 /* Vertex format */
375                 unsigned int pos = add_attrib(&format, "pos", GL_FLOAT, 3, KEEP_FLOAT);
376                 unsigned int nor = add_attrib(&format, "nor", GL_FLOAT, 3, KEEP_FLOAT);
377
378                 /* Vertices */
379                 VertexBuffer_init_with_format(&vbo, &format);
380                 VertexBuffer_allocate_data(&vbo, 36);
381
382                 n[0] = -1.0;
383                 add_solid_flat_triangle(&vbo, &i, pos, nor, cube_vert[0], cube_vert[1], cube_vert[2], n);
384                 add_solid_flat_triangle(&vbo, &i, pos, nor, cube_vert[2], cube_vert[3], cube_vert[0], n);
385                 n[0] = 0;
386                 n[1] = -1.0;
387                 add_solid_flat_triangle(&vbo, &i, pos, nor, cube_vert[0], cube_vert[4], cube_vert[5], n);
388                 add_solid_flat_triangle(&vbo, &i, pos, nor, cube_vert[5], cube_vert[1], cube_vert[0], n);
389                 n[1] = 0;
390                 n[0] = 1.0;
391                 add_solid_flat_triangle(&vbo, &i, pos, nor, cube_vert[4], cube_vert[7], cube_vert[6], n);
392                 add_solid_flat_triangle(&vbo, &i, pos, nor, cube_vert[6], cube_vert[5], cube_vert[4], n);
393                 n[0] = 0;
394                 n[1] = 1.0;
395                 add_solid_flat_triangle(&vbo, &i, pos, nor, cube_vert[7], cube_vert[3], cube_vert[2], n);
396                 add_solid_flat_triangle(&vbo, &i, pos, nor, cube_vert[2], cube_vert[6], cube_vert[7], n);
397                 n[1] = 0;
398                 n[2] = 1.0;
399                 add_solid_flat_triangle(&vbo, &i, pos, nor, cube_vert[1], cube_vert[5], cube_vert[6], n);
400                 add_solid_flat_triangle(&vbo, &i, pos, nor, cube_vert[6], cube_vert[2], cube_vert[1], n);
401                 n[2] = -1.0;
402                 add_solid_flat_triangle(&vbo, &i, pos, nor, cube_vert[7], cube_vert[4], cube_vert[0], n);
403                 add_solid_flat_triangle(&vbo, &i, pos, nor, cube_vert[0], cube_vert[3], cube_vert[7], n);
404
405                 Batch_init(&batch, GL_TRIANGLES, &vbo, NULL);
406         }
407
408         gpuMatrixBegin3D_legacy();
409         gpuScale3f(xsize, ysize, zsize);
410
411         if (flat_color) {
412                 Batch_set_builtin_program(&batch, GPU_SHADER_3D_UNIFORM_COLOR);
413         }
414         else {
415                 /* TODO replace with good default lighting shader ? */
416                 Batch_set_builtin_program(&batch, GPU_SHADER_SIMPLE_LIGHTING);
417                 Batch_Uniform3fv(&batch, "light", light_vec);
418         }
419         Batch_Uniform4fv(&batch, "color", fcolor);
420         Batch_draw(&batch);
421
422         gpuMatrixEnd();
423 }
424
425 static void drawcube_size(float xsize, float ysize, float zsize)
426 {
427         static VertexFormat format = {0};
428         static VertexBuffer vbo = {{0}};
429         static ElementListBuilder elb = {0};
430         static ElementList el = {0};
431         static Batch batch = {{0}};
432
433         if (format.attrib_ct == 0) {
434                 /* Vertex format */
435                 unsigned int pos = add_attrib(&format, "pos", GL_FLOAT, 3, KEEP_FLOAT);
436
437                 /* Elements */
438                 ElementListBuilder_init(&elb, GL_LINES, 12, 8);
439                 for (int i = 0; i < 12; ++i) {
440                         add_line_vertices(&elb, cube_wire[i*2], cube_wire[i*2+1]);
441                 }
442                 ElementList_build_in_place(&elb, &el);
443
444                 /* Vertices */
445                 VertexBuffer_init_with_format(&vbo, &format);
446                 VertexBuffer_allocate_data(&vbo, 8);
447                 for (int i = 0; i < 8; ++i) {
448                         setAttrib(&vbo, pos, i, cube_vert[i]);
449                 }
450
451                 Batch_init(&batch, GL_LINES, &vbo, &el);
452                 Batch_set_builtin_program(&batch, GPU_SHADER_3D_UNIFORM_COLOR);
453         }
454
455         gpuMatrixBegin3D_legacy();
456         gpuScale3f(xsize, ysize, zsize);
457
458         Batch_use_program(&batch);
459         Batch_Uniform4fv(&batch, "color", fcolor);
460         Batch_draw(&batch);
461
462         gpuMatrixEnd();
463 }
464
465
466 static void draw_bonevert(void)
467 {
468         static VertexFormat format = {0};
469         static VertexBuffer vbo = {{0}};
470         static Batch batch = {{0}};
471
472         if (format.attrib_ct == 0) {
473                 /* Vertex format */
474                 unsigned int pos = add_attrib(&format, "pos", GL_FLOAT, 3, KEEP_FLOAT);
475
476                 /* Vertices */
477                 VertexBuffer_init_with_format(&vbo, &format);
478                 VertexBuffer_allocate_data(&vbo, 96);
479                 for (int i = 0; i < 16; ++i) {
480                         float vert[3] = {0.f, 0.f, 0.f};
481                         const float r = 0.05f;
482
483                         vert[0] = r * cosf(2 * M_PI * i / 16.f);
484                         vert[1] = r * sinf(2 * M_PI * i / 16.f);
485                         setAttrib(&vbo, pos, i * 6 + 0, vert);
486                         vert[0] = r * cosf(2 * M_PI * (i + 1) / 16.f);
487                         vert[1] = r * sinf(2 * M_PI * (i + 1) / 16.f);
488                         setAttrib(&vbo, pos, i * 6 + 1, vert);
489
490                         vert[0] = 0.f;
491                         vert[1] = r * cosf(2 * M_PI * i / 16.f);
492                         vert[2] = r * sinf(2 * M_PI * i / 16.f);
493                         setAttrib(&vbo, pos, i * 6 + 2, vert);
494                         vert[1] = r * cosf(2 * M_PI * (i + 1) / 16.f);
495                         vert[2] = r * sinf(2 * M_PI * (i + 1) / 16.f);
496                         setAttrib(&vbo, pos, i * 6 + 3, vert);
497
498                         vert[1] = 0.f;
499                         vert[0] = r * cosf(2 * M_PI * i / 16.f);
500                         vert[2] = r * sinf(2 * M_PI * i / 16.f);
501                         setAttrib(&vbo, pos, i * 6 + 4, vert);
502                         vert[0] = r * cosf(2 * M_PI * (i + 1) / 16.f);
503                         vert[2] = r * sinf(2 * M_PI * (i + 1) / 16.f);
504                         setAttrib(&vbo, pos, i * 6 + 5, vert);
505                 }
506
507                 Batch_init(&batch, GL_LINES, &vbo, NULL);
508                 Batch_set_builtin_program(&batch, GPU_SHADER_3D_UNIFORM_COLOR);
509         }
510
511         gpuMatrixBegin3D_legacy();
512
513         Batch_use_program(&batch);
514         Batch_Uniform4fv(&batch, "color", fcolor);
515         Batch_draw(&batch);
516
517         gpuMatrixEnd();
518 }
519
520 static void draw_bonevert_solid(void)
521 {
522         Batch *batch = Batch_get_sphere(0);
523         const float light_vec[3] = {0.0f, 0.0f, 1.0f};
524
525         gpuMatrixBegin3D_legacy();
526
527         gpuScale3f(0.05, 0.05, 0.05);
528
529         if (flat_color) {
530                 Batch_set_builtin_program(batch, GPU_SHADER_3D_UNIFORM_COLOR);
531         }
532         else {
533                 /* TODO replace with good default lighting shader ? */
534                 Batch_set_builtin_program(batch, GPU_SHADER_SIMPLE_LIGHTING);
535                 Batch_Uniform3fv(batch, "light", light_vec);
536         }
537         Batch_Uniform4fv(batch, "color", fcolor);
538         Batch_draw(batch);
539
540         gpuMatrixEnd();
541 }
542
543 static const float bone_octahedral_verts[6][3] = {
544         { 0.0f, 0.0f,  0.0f},
545         { 0.1f, 0.1f,  0.1f},
546         { 0.1f, 0.1f, -0.1f},
547         {-0.1f, 0.1f, -0.1f},
548         {-0.1f, 0.1f,  0.1f},
549         { 0.0f, 1.0f,  0.0f}
550 };
551
552 static const unsigned int bone_octahedral_wire[24] = {
553         0, 1,  1, 5,  5, 3,  3, 0,
554         0, 4,  4, 5,  5, 2,  2, 0,
555         1, 2,  2, 3,  3, 4,  4, 1,
556 };
557
558 static const unsigned int bone_octahedral_solid_tris[8][3] = {
559         {2, 1, 0}, /* bottom */
560         {3, 2, 0},
561         {4, 3, 0},
562         {1, 4, 0},
563
564         {5, 1, 2}, /* top */
565         {5, 2, 3},
566         {5, 3, 4},
567         {5, 4, 1}
568 };
569
570 /* aligned with bone_octahedral_solid_tris */
571 static const float bone_octahedral_solid_normals[8][3] = {
572         { M_SQRT1_2,   -M_SQRT1_2,    0.00000000f},
573         {-0.00000000f, -M_SQRT1_2,   -M_SQRT1_2},
574         {-M_SQRT1_2,   -M_SQRT1_2,    0.00000000f},
575         { 0.00000000f, -M_SQRT1_2,    M_SQRT1_2},
576         { 0.99388373f,  0.11043154f, -0.00000000f},
577         { 0.00000000f,  0.11043154f, -0.99388373f},
578         {-0.99388373f,  0.11043154f,  0.00000000f},
579         { 0.00000000f,  0.11043154f,  0.99388373f}
580 };
581
582 static void draw_bone_octahedral(void)
583 {
584         static VertexFormat format = {0};
585         static VertexBuffer vbo = {{0}};
586         static ElementListBuilder elb = {0};
587         static ElementList el = {0};
588         static Batch batch = {{0}};
589
590         if (format.attrib_ct == 0) {
591                 /* Vertex format */
592                 unsigned int pos = add_attrib(&format, "pos", GL_FLOAT, 3, KEEP_FLOAT);
593
594                 /* Elements */
595                 ElementListBuilder_init(&elb, GL_LINES, 12, 6);
596                 for (int i = 0; i < 12; ++i) {
597                         add_line_vertices(&elb, bone_octahedral_wire[i*2], bone_octahedral_wire[i*2+1]);
598                 }
599                 ElementList_build_in_place(&elb, &el);
600
601                 /* Vertices */
602                 VertexBuffer_init_with_format(&vbo, &format);
603                 VertexBuffer_allocate_data(&vbo, 6);
604                 for (int i = 0; i < 6; ++i) {
605                         setAttrib(&vbo, pos, i, bone_octahedral_verts[i]);
606                 }
607
608                 Batch_init(&batch, GL_LINES, &vbo, &el);
609                 Batch_set_builtin_program(&batch, GPU_SHADER_3D_UNIFORM_COLOR);
610         }
611
612         gpuMatrixBegin3D_legacy();
613
614         Batch_use_program(&batch);
615         Batch_Uniform4fv(&batch, "color", fcolor);
616         Batch_draw(&batch);
617
618         gpuMatrixEnd();
619 }       
620
621 static void draw_bone_solid_octahedral(void)
622 {
623         static VertexFormat format = {0};
624         static VertexBuffer vbo = {{0}};
625         static Batch batch = {{0}};
626         const float light_vec[3] = {0.0f, 0.0f, 1.0f};
627
628         if (format.attrib_ct == 0) {
629                 unsigned int v_idx = 0;
630                 /* Vertex format */
631                 unsigned int pos = add_attrib(&format, "pos", GL_FLOAT, 3, KEEP_FLOAT);
632                 unsigned int nor = add_attrib(&format, "nor", GL_FLOAT, 3, KEEP_FLOAT);
633
634                 /* Vertices */
635                 VertexBuffer_init_with_format(&vbo, &format);
636                 VertexBuffer_allocate_data(&vbo, 24);
637
638                 for (int i = 0; i < 8; i++) {
639                         add_solid_flat_triangle(&vbo, &v_idx, pos, nor,
640                                                 bone_octahedral_verts[bone_octahedral_solid_tris[i][0]],
641                                                 bone_octahedral_verts[bone_octahedral_solid_tris[i][1]],
642                                                 bone_octahedral_verts[bone_octahedral_solid_tris[i][2]],
643                                                 bone_octahedral_solid_normals[i]);
644                 }
645
646                 Batch_init(&batch, GL_TRIANGLES, &vbo, NULL);
647         }
648
649         gpuMatrixBegin3D_legacy();
650
651         if (flat_color) {
652                 Batch_set_builtin_program(&batch, GPU_SHADER_3D_UNIFORM_COLOR);
653         }
654         else {
655                 /* TODO replace with good default lighting shader ? */
656                 Batch_set_builtin_program(&batch, GPU_SHADER_SIMPLE_LIGHTING);
657                 Batch_Uniform3fv(&batch, "light", light_vec);
658         }
659         Batch_Uniform4fv(&batch, "color", fcolor);
660         Batch_draw(&batch);
661
662         gpuMatrixEnd();
663 }       
664
665 /* *************** Armature drawing, bones ******************* */
666
667
668 static void draw_bone_points(const short dt, int armflag, unsigned int boneflag, int id)
669 {
670         /*      Draw root point if we are not connected */
671         if ((boneflag & BONE_CONNECTED) == 0) {
672                 if (id != -1)
673                         GPU_select_load_id(id | BONESEL_ROOT);
674                 
675                 if (dt <= OB_WIRE) {
676                         if (armflag & ARM_EDITMODE) {
677                                 if (boneflag & BONE_ROOTSEL) {
678                                         UI_GetThemeColor4fv(TH_VERTEX_SELECT, fcolor);
679                                 }
680                                 else {
681                                         UI_GetThemeColor4fv(TH_VERTEX, fcolor);
682                                 }
683                         }
684                 }
685                 else {
686                         if (armflag & ARM_POSEMODE) 
687                                 set_pchan_color(PCHAN_COLOR_SOLID, boneflag, 0);
688                         else {
689                                 UI_GetThemeColor4fv(TH_BONE_SOLID, fcolor);
690                         }
691                 }
692                 
693                 if (dt > OB_WIRE) 
694                         draw_bonevert_solid();
695                 else 
696                         draw_bonevert();
697         }
698         
699         /*      Draw tip point */
700         if (id != -1)
701                 GPU_select_load_id(id | BONESEL_TIP);
702         
703         if (dt <= OB_WIRE) {
704                 if (armflag & ARM_EDITMODE) {
705                         if (boneflag & BONE_TIPSEL) {
706                                 UI_GetThemeColor4fv(TH_VERTEX_SELECT, fcolor);
707                         }
708                         else {
709                                 UI_GetThemeColor4fv(TH_VERTEX, fcolor);
710                         }
711                 }
712         }
713         else {
714                 if (armflag & ARM_POSEMODE) 
715                         set_pchan_color(PCHAN_COLOR_SOLID, boneflag, 0);
716                 else {
717                         UI_GetThemeColor4fv(TH_BONE_SOLID, fcolor);
718                 }
719         }
720         
721         glTranslatef(0.0f, 1.0f, 0.0f);
722         if (dt > OB_WIRE) 
723                 draw_bonevert_solid();
724         else 
725                 draw_bonevert();
726         glTranslatef(0.0f, -1.0f, 0.0f);
727         
728 }
729
730 /* 16 values of sin function (still same result!) */
731 static const float si[16] = {
732         0.00000000f,
733         0.20129852f, 0.39435585f,
734         0.57126821f, 0.72479278f,
735         0.84864425f, 0.93775213f,
736         0.98846832f, 0.99871650f,
737         0.96807711f, 0.89780453f,
738         0.79077573f, 0.65137248f,
739         0.48530196f, 0.29936312f,
740         0.10116832f
741 };
742 /* 16 values of cos function (still same result!) */
743 static const float co[16] = {
744         1.00000000f,
745         0.97952994f, 0.91895781f,
746         0.82076344f, 0.68896691f,
747         0.52896401f, 0.34730525f,
748         0.15142777f, -0.05064916f,
749         -0.25065253f, -0.44039415f,
750         -0.61210598f, -0.75875812f,
751         -0.87434661f, -0.95413925f,
752         -0.99486932f
753 };
754
755
756
757 /* smat, imat = mat & imat to draw screenaligned */
758 static void draw_sphere_bone_dist(float smat[4][4], float imat[4][4], bPoseChannel *pchan, EditBone *ebone)
759 {
760         float head, tail, dist /*, length*/;
761         float *headvec, *tailvec, dirvec[3];
762         
763         /* figure out the sizes of spheres */
764         if (ebone) {
765                 /* this routine doesn't call get_matrix_editbone() that calculates it */
766                 ebone->length = len_v3v3(ebone->head, ebone->tail);
767
768                 /*length = ebone->length;*/ /*UNUSED*/
769                 tail = ebone->rad_tail;
770                 dist = ebone->dist;
771                 if (ebone->parent && (ebone->flag & BONE_CONNECTED))
772                         head = ebone->parent->rad_tail;
773                 else
774                         head = ebone->rad_head;
775                 headvec = ebone->head;
776                 tailvec = ebone->tail;
777         }
778         else {
779                 /*length = pchan->bone->length;*/ /*UNUSED*/
780                 tail = pchan->bone->rad_tail;
781                 dist = pchan->bone->dist;
782                 if (pchan->parent && (pchan->bone->flag & BONE_CONNECTED))
783                         head = pchan->parent->bone->rad_tail;
784                 else
785                         head = pchan->bone->rad_head;
786                 headvec = pchan->pose_head;
787                 tailvec = pchan->pose_tail;
788         }
789         
790         /* ***** draw it ***** */
791         
792         /* move vector to viewspace */
793         sub_v3_v3v3(dirvec, tailvec, headvec);
794         mul_mat3_m4_v3(smat, dirvec);
795         /* clear zcomp */
796         dirvec[2] = 0.0f;
797
798         if (head != tail) {
799                 /* correction when viewing along the bones axis
800                  * it pops in and out but better then artifacts, [#23841] */
801                 float view_dist = len_v2(dirvec);
802
803                 if (head - view_dist > tail) {
804                         tailvec = headvec;
805                         tail = head;
806                         zero_v3(dirvec);
807                         dirvec[0] = 0.00001;  /* XXX. weak but ok */
808                 }
809                 else if (tail - view_dist > head) {
810                         headvec = tailvec;
811                         head = tail;
812                         zero_v3(dirvec);
813                         dirvec[0] = 0.00001;  /* XXX. weak but ok */
814                 }
815         }
816
817         /* move vector back */
818         mul_mat3_m4_v3(imat, dirvec);
819         
820         if (0.0f != normalize_v3(dirvec)) {
821                 float norvec[3], vec1[3], vec2[3], vec[3];
822                 int a;
823                 
824                 //mul_v3_fl(dirvec, head);
825                 cross_v3_v3v3(norvec, dirvec, imat[2]);
826
827                 VertexFormat *format = immVertexFormat();
828                 unsigned pos = add_attrib(format, "pos", GL_FLOAT, 3, KEEP_FLOAT);
829
830                 immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
831                 immBegin(GL_TRIANGLE_STRIP, 66);
832                 immUniformColor4ub(255, 255, 255, 50);
833                 
834                 for (a = 0; a < 16; a++) {
835                         vec[0] = -si[a] * dirvec[0] + co[a] * norvec[0];
836                         vec[1] = -si[a] * dirvec[1] + co[a] * norvec[1];
837                         vec[2] = -si[a] * dirvec[2] + co[a] * norvec[2];
838
839                         madd_v3_v3v3fl(vec1, headvec, vec, head);
840                         madd_v3_v3v3fl(vec2, headvec, vec, head + dist);
841                         
842                         immVertex3fv(pos, vec1);
843                         immVertex3fv(pos, vec2);
844                 }
845                 
846                 for (a = 15; a >= 0; a--) {
847                         vec[0] = si[a] * dirvec[0] + co[a] * norvec[0];
848                         vec[1] = si[a] * dirvec[1] + co[a] * norvec[1];
849                         vec[2] = si[a] * dirvec[2] + co[a] * norvec[2];
850
851                         madd_v3_v3v3fl(vec1, tailvec, vec, tail);
852                         madd_v3_v3v3fl(vec2, tailvec, vec, tail + dist);
853                         
854                         immVertex3fv(pos, vec1);
855                         immVertex3fv(pos, vec2);
856                 }
857                 /* make it cyclic... */
858                 
859                 vec[0] = -si[0] * dirvec[0] + co[0] * norvec[0];
860                 vec[1] = -si[0] * dirvec[1] + co[0] * norvec[1];
861                 vec[2] = -si[0] * dirvec[2] + co[0] * norvec[2];
862
863                 madd_v3_v3v3fl(vec1, headvec, vec, head);
864                 madd_v3_v3v3fl(vec2, headvec, vec, head + dist);
865
866                 immVertex3fv(pos, vec1);
867                 immVertex3fv(pos, vec2);
868                 
869                 immEnd();
870                 immUnbindProgram();
871         }
872 }
873
874
875 /* smat, imat = mat & imat to draw screenaligned */
876 static void draw_sphere_bone_wire(float smat[4][4], float imat[4][4],
877                                   int armflag, int boneflag, short constflag, unsigned int id,
878                                   bPoseChannel *pchan, EditBone *ebone)
879 {
880         float head, tail /*, length*/;
881         float *headvec, *tailvec, dirvec[3];
882
883         VertexFormat *format = immVertexFormat();
884         unsigned int pos = add_attrib(format, "pos", GL_FLOAT, 3, KEEP_FLOAT);
885
886         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
887         
888         /* figure out the sizes of spheres */
889         if (ebone) {
890                 /* this routine doesn't call get_matrix_editbone() that calculates it */
891                 ebone->length = len_v3v3(ebone->head, ebone->tail);
892                 
893                 /*length = ebone->length;*/ /*UNUSED*/
894                 tail = ebone->rad_tail;
895                 if (ebone->parent && (boneflag & BONE_CONNECTED))
896                         head = ebone->parent->rad_tail;
897                 else
898                         head = ebone->rad_head;
899                 headvec = ebone->head;
900                 tailvec = ebone->tail;
901         }
902         else {
903                 /*length = pchan->bone->length;*/ /*UNUSED*/
904                 tail = pchan->bone->rad_tail;
905                 if ((pchan->parent) && (boneflag & BONE_CONNECTED))
906                         head = pchan->parent->bone->rad_tail;
907                 else
908                         head = pchan->bone->rad_head;
909                 headvec = pchan->pose_head;
910                 tailvec = pchan->pose_tail;
911         }
912         
913         /* sphere root color */
914         if (armflag & ARM_EDITMODE) {
915                 if (boneflag & BONE_ROOTSEL) {
916                         UI_GetThemeColor4fv(TH_VERTEX_SELECT, fcolor);
917                 }
918                 else {
919                         UI_GetThemeColor4fv(TH_VERTEX, fcolor);
920                 }
921         }
922         else if (armflag & ARM_POSEMODE)
923                 set_pchan_color(PCHAN_COLOR_NORMAL, boneflag, constflag);
924
925         immUniformColor4fv(fcolor);
926
927         /*      Draw root point if we are not connected */
928         if ((boneflag & BONE_CONNECTED) == 0) {
929                 if (id != -1)
930                         GPU_select_load_id(id | BONESEL_ROOT);
931                 
932                 imm_drawcircball(headvec, head, imat, pos);
933         }
934         
935         /*      Draw tip point */
936         if (armflag & ARM_EDITMODE) {
937                 if (boneflag & BONE_TIPSEL) {
938                         UI_GetThemeColor4fv(TH_VERTEX_SELECT, fcolor);
939                 }
940                 else {
941                         UI_GetThemeColor4fv(TH_VERTEX, fcolor);
942                 }
943         }
944         
945         if (id != -1)
946                 GPU_select_load_id(id | BONESEL_TIP);
947         
948         imm_drawcircball(tailvec, tail, imat, pos);
949         
950         /* base */
951         if (armflag & ARM_EDITMODE) {
952                 if (boneflag & BONE_SELECTED){
953                         UI_GetThemeColor4fv(TH_SELECT, fcolor);
954                 }
955                 else {
956                         UI_GetThemeColor4fv(TH_WIRE_EDIT, fcolor);
957                 }
958         }
959         
960         sub_v3_v3v3(dirvec, tailvec, headvec);
961         
962         /* move vector to viewspace */
963         mul_mat3_m4_v3(smat, dirvec);
964         /* clear zcomp */
965         dirvec[2] = 0.0f;
966         /* move vector back */
967         mul_mat3_m4_v3(imat, dirvec);
968         
969         if (0.0f != normalize_v3(dirvec)) {
970                 float norvech[3], norvect[3], vec[3];
971                 
972                 copy_v3_v3(vec, dirvec);
973                 
974                 mul_v3_fl(dirvec, head);
975                 cross_v3_v3v3(norvech, dirvec, imat[2]);
976                 
977                 mul_v3_fl(vec, tail);
978                 cross_v3_v3v3(norvect, vec, imat[2]);
979                 
980                 if (id != -1)
981                         GPU_select_load_id(id | BONESEL_BONE);
982                 
983                 immBegin(GL_LINES, 4);
984
985                 add_v3_v3v3(vec, headvec, norvech);
986                 immVertex3fv(pos, vec);
987
988                 add_v3_v3v3(vec, tailvec, norvect);
989                 immVertex3fv(pos, vec);
990
991                 sub_v3_v3v3(vec, headvec, norvech);
992                 immVertex3fv(pos, vec);
993
994                 sub_v3_v3v3(vec, tailvec, norvect);
995                 immVertex3fv(pos, vec);
996                 
997                 immEnd();
998         }
999
1000         immUnbindProgram();
1001 }
1002
1003 /* does wire only for outline selecting */
1004 static void draw_sphere_bone(const short dt, int armflag, int boneflag, short constflag, unsigned int id,
1005                              bPoseChannel *pchan, EditBone *ebone)
1006 {
1007         Batch *sphere = Batch_get_sphere(1);
1008         float head, tail, length;
1009         float fac1, fac2, size1, size2;
1010         const float light_vec[3] = {0.0f, 0.0f, 1.0f};
1011
1012         /* dt is always OB_SOlID */
1013         Batch_set_builtin_program(sphere, GPU_SHADER_SIMPLE_LIGHTING);
1014         Batch_Uniform3fv(sphere, "light", light_vec);
1015
1016         gpuMatrixBegin3D_legacy();
1017         gpuPushMatrix();
1018
1019         /* figure out the sizes of spheres */
1020         if (ebone) {
1021                 length = ebone->length;
1022                 tail = ebone->rad_tail;
1023                 if (ebone->parent && (boneflag & BONE_CONNECTED))
1024                         head = ebone->parent->rad_tail;
1025                 else
1026                         head = ebone->rad_head;
1027         }
1028         else {
1029                 length = pchan->bone->length;
1030                 tail = pchan->bone->rad_tail;
1031                 if (pchan->parent && (boneflag & BONE_CONNECTED))
1032                         head = pchan->parent->bone->rad_tail;
1033                 else
1034                         head = pchan->bone->rad_head;
1035         }
1036         
1037         /* move to z-axis space */
1038         gpuRotate3f(-90.0f, 1.0f, 0.0f, 0.0f);
1039
1040         /* sphere root color */
1041         if (armflag & ARM_EDITMODE) {
1042                 if (boneflag & BONE_ROOTSEL)
1043                         UI_GetThemeColor4fv(TH_VERTEX_SELECT, fcolor);
1044                 else
1045                         UI_GetThemeColorShade4fv(TH_BONE_SOLID, -30, fcolor);
1046         }
1047         else if (armflag & ARM_POSEMODE)
1048                 set_pchan_color(PCHAN_COLOR_SPHEREBONE_END, boneflag, constflag);
1049         else if (dt == OB_SOLID)
1050                 UI_GetThemeColorShade4fv(TH_BONE_SOLID, -30, fcolor);
1051         
1052         /*      Draw root point if we are not connected */
1053         if ((boneflag & BONE_CONNECTED) == 0) {
1054                 if (id != -1)
1055                         GPU_select_load_id(id | BONESEL_ROOT);
1056                 gpuPushMatrix();
1057                 gpuScale3f(head, head, head);
1058                 Batch_Uniform4fv(sphere, "color", fcolor);
1059                 Batch_draw(sphere);
1060                 gpuPopMatrix();
1061         }
1062         
1063         /*      Draw tip point */
1064         if (armflag & ARM_EDITMODE) {
1065                 if (boneflag & BONE_TIPSEL) UI_GetThemeColor4fv(TH_VERTEX_SELECT, fcolor);
1066                 else UI_GetThemeColorShade4fv(TH_BONE_SOLID, -30, fcolor);
1067         }
1068
1069         if (id != -1)
1070                 GPU_select_load_id(id | BONESEL_TIP);
1071         
1072         gpuTranslate3f(0.0f, 0.0f, length);
1073
1074         gpuPushMatrix();
1075         gpuScale3f(tail, tail, tail);
1076         Batch_use_program(sphere); /* hack to make the following uniforms stick */
1077         Batch_Uniform4fv(sphere, "color", fcolor);
1078         Batch_draw(sphere);
1079         gpuPopMatrix();
1080
1081         gpuTranslate3f(0.0f, 0.0f, -length);
1082         
1083         /* base */
1084         if (armflag & ARM_EDITMODE) {
1085                 if (boneflag & BONE_SELECTED) UI_GetThemeColor4fv(TH_SELECT, fcolor);
1086                 else UI_GetThemeColor4fv(TH_BONE_SOLID, fcolor);
1087         }
1088         else if (armflag & ARM_POSEMODE)
1089                 set_pchan_color(PCHAN_COLOR_SPHEREBONE_BASE, boneflag, constflag);
1090         else if (dt == OB_SOLID)
1091                 UI_GetThemeColor4fv(TH_BONE_SOLID, fcolor);
1092
1093         Batch_use_program(sphere); /* hack to make the following uniforms stick */
1094         Batch_Uniform4fv(sphere, "color", fcolor);
1095         
1096         fac1 = (length - head) / length;
1097         fac2 = (length - tail) / length;
1098         
1099         if (length > (head + tail)) {
1100                 size1 = fac2 * tail + (1.0f - fac2) * head;
1101                 size2 = fac1 * head + (1.0f - fac1) * tail;
1102
1103                 if (id != -1)
1104                         GPU_select_load_id(id | BONESEL_BONE);
1105                 
1106                 /* draw sphere on extrema */
1107                 gpuPushMatrix();
1108                 gpuTranslate3f(0.0f, 0.0f, length - tail);
1109                 gpuScale3f(size1, size1, size1);
1110
1111                 Batch_draw(sphere);
1112                 gpuPopMatrix();
1113
1114                 gpuPushMatrix();
1115                 gpuTranslate3f(0.0f, 0.0f, head);
1116                 gpuScale3f(size2, size2, size2);
1117
1118                 Batch_draw(sphere);
1119                 gpuPopMatrix();
1120
1121                 /* draw cynlinder between spheres */
1122                 glEnable(GL_POLYGON_OFFSET_FILL);
1123                 glPolygonOffset(-1.0f, -1.0f);
1124
1125                 VertexFormat *format = immVertexFormat();
1126                 unsigned int pos = add_attrib(format, "pos", GL_FLOAT, 3, KEEP_FLOAT);
1127                 unsigned int nor = add_attrib(format, "nor", GL_FLOAT, 3, KEEP_FLOAT);
1128
1129                 immBindBuiltinProgram(GPU_SHADER_SIMPLE_LIGHTING);
1130                 immUniformColor4fv(fcolor);
1131                 immUniform3fv("light", light_vec);
1132
1133                 gpuTranslate3f(0.0f, 0.0f, head);
1134                 imm_cylinder_nor(pos, nor, size2, size1, length - head - tail, 16, 1);
1135
1136                 immUnbindProgram();
1137                 
1138                 glDisable(GL_POLYGON_OFFSET_FILL);
1139         }
1140         else {
1141                 size1 = fac1 * head + (1.0f - fac1) * tail;
1142
1143                 /* 1 sphere in center */
1144                 gpuTranslate3f(0.0f, 0.0f, (head + length - tail) / 2.0f);
1145
1146                 gpuScale3f(size1, size1, size1);
1147                 Batch_draw(sphere);
1148         }
1149         
1150         gpuPopMatrix();
1151         gpuMatrixEnd();
1152 }
1153
1154 static void draw_line_bone(int armflag, int boneflag, short constflag, unsigned int id,
1155                            bPoseChannel *pchan, EditBone *ebone)
1156 {
1157         float length;
1158         
1159         if (pchan) 
1160                 length = pchan->bone->length;
1161         else 
1162                 length = ebone->length;
1163         
1164         VertexFormat *format = immVertexFormat();
1165         unsigned int pos = add_attrib(format, "pos", GL_FLOAT, 3, KEEP_FLOAT);
1166
1167         gpuMatrixBegin3D_legacy();
1168         gpuPushMatrix();
1169         gpuScale3f(length, length, length);
1170         
1171         /* this chunk not in object mode */
1172         if (armflag & (ARM_EDITMODE | ARM_POSEMODE)) {
1173                 glLineWidth(4.0f);
1174                 glPointSize(8.0f);
1175
1176                 if (armflag & ARM_POSEMODE)
1177                         set_pchan_color(PCHAN_COLOR_NORMAL, boneflag, constflag);
1178                 else if (armflag & ARM_EDITMODE) {
1179                         UI_GetThemeColor4fv(TH_WIRE_EDIT, fcolor);
1180                 }
1181
1182                 /* line */
1183                 immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
1184                 immUniformColor4fv(fcolor);
1185
1186                 if (id != -1)
1187                         GPU_select_load_id(id | BONESEL_BONE);
1188
1189                 immBegin(GL_LINES, 2);
1190                 immVertex3f(pos, 0.0f, 1.0f, 0.0f);
1191                 immVertex3f(pos, 0.0f, 0.0f, 0.0f);
1192                 immEnd();
1193
1194                 immUnbindProgram();
1195
1196                 immBindBuiltinProgram(GPU_SHADER_3D_POINT_FIXED_SIZE_UNIFORM_COLOR);
1197                 immUniformColor4fv(fcolor);
1198
1199                 /*      Draw root point if we are not connected */
1200                 if ((boneflag & BONE_CONNECTED) == 0) {
1201                         if (G.f & G_PICKSEL)
1202                                 GPU_select_load_id(id | BONESEL_ROOT);
1203
1204                         immBegin(GL_POINTS, 1);
1205                         immVertex3f(pos, 0.0f, 0.0f, 0.0f);
1206                         immEnd();
1207                 }
1208
1209                 /* tip */
1210                 if (G.f & G_PICKSEL)
1211                         GPU_select_load_id(id | BONESEL_TIP);
1212
1213                 immBegin(GL_POINTS, 1);
1214                 immVertex3f(pos, 0.0f, 1.0f, 0.0f);
1215                 immEnd();
1216
1217                 immUnbindProgram();
1218
1219
1220                 /* further we send no names */
1221                 if (id != -1)
1222                         GPU_select_load_id(id & 0xFFFF);  /* object tag, for bordersel optim */
1223                 
1224                 if (armflag & ARM_POSEMODE)
1225                         set_pchan_color(PCHAN_COLOR_LINEBONE, boneflag, constflag);
1226         }
1227
1228         /* Now draw the inner color */
1229         glLineWidth(2.0f);
1230         glPointSize(5.0f);
1231
1232         /* line */
1233         if (armflag & ARM_EDITMODE) {
1234                 if (boneflag & BONE_SELECTED) UI_GetThemeColor4fv(TH_EDGE_SELECT, fcolor);
1235                 else UI_GetThemeColorShade4fv(TH_BACK, -30, fcolor);
1236         }
1237
1238         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
1239         immUniformColor4fv(fcolor);
1240
1241         immBegin(GL_LINES, 2);
1242         immVertex3f(pos, 0.0f, 1.0f, 0.0f);
1243         immVertex3f(pos, 0.0f, 0.0f, 0.0f);
1244         immEnd();
1245
1246         immUnbindProgram();
1247
1248         immBindBuiltinProgram(GPU_SHADER_3D_POINT_FIXED_SIZE_UNIFORM_COLOR);
1249
1250         /*Draw root point if we are not connected */
1251         if ((boneflag & BONE_CONNECTED) == 0) {
1252                 if (armflag & ARM_EDITMODE) {
1253                         if (boneflag & BONE_ROOTSEL) UI_GetThemeColor4fv(TH_VERTEX_SELECT, fcolor);
1254                         else UI_GetThemeColor4fv(TH_VERTEX, fcolor);
1255                 }
1256                 immUniformColor4fv(fcolor);
1257                 immBegin(GL_POINTS, 1);
1258                 immVertex3f(pos, 0.0f, 0.0f, 0.0f);
1259                 immEnd();
1260         }
1261
1262         /* tip */
1263         if ((G.f & G_PICKSEL) == 0) {
1264                 /* no bitmap in selection mode, crashes 3d cards... */
1265                 if (armflag & ARM_EDITMODE) {
1266                         if (boneflag & BONE_TIPSEL) UI_GetThemeColor4fv(TH_VERTEX_SELECT, fcolor);
1267                         else UI_GetThemeColor4fv(TH_VERTEX, fcolor);
1268                 }
1269                 immUniformColor4fv(fcolor);
1270                 immBegin(GL_POINTS, 1);
1271                 immVertex3f(pos, 0.0f, 1.0f, 0.0f);
1272                 immEnd();
1273         }
1274
1275         immUnbindProgram();
1276
1277         gpuPopMatrix();
1278         gpuMatrixEnd();
1279 }
1280
1281 /* A partial copy of b_bone_spline_setup(), with just the parts for previewing editmode curve settings 
1282  *
1283  * This assumes that prev/next bones don't have any impact (since they should all still be in the "straight"
1284  * position here anyway), and that we can simply apply the bbone settings to get the desired effect...
1285  */
1286 static void ebone_spline_preview(EditBone *ebone, Mat4 result_array[MAX_BBONE_SUBDIV])
1287 {
1288         float h1[3], h2[3], length, hlength1, hlength2, roll1 = 0.0f, roll2 = 0.0f;
1289         float mat3[3][3];
1290         float data[MAX_BBONE_SUBDIV + 1][4], *fp;
1291         int a;
1292         
1293         length = ebone->length;
1294         
1295         hlength1 = ebone->ease1 * length * 0.390464f; /* 0.5f * sqrt(2) * kappa, the handle length for near-perfect circles */
1296         hlength2 = ebone->ease2 * length * 0.390464f;
1297         
1298         /* find the handle points, since this is inside bone space, the
1299          * first point = (0, 0, 0)
1300          * last point =  (0, length, 0)
1301          *
1302          * we also just apply all the "extra effects", since they're the whole reason we're doing this...
1303          */
1304         h1[0] = ebone->curveInX;
1305         h1[1] = hlength1;
1306         h1[2] = ebone->curveInY;
1307         roll1 = ebone->roll1;
1308         
1309         h2[0] = ebone->curveOutX;
1310         h2[1] = -hlength2;
1311         h2[2] = ebone->curveOutY;
1312         roll2 = ebone->roll2;
1313         
1314         /* make curve */
1315         if (ebone->segments > MAX_BBONE_SUBDIV)
1316                 ebone->segments = MAX_BBONE_SUBDIV;
1317
1318         BKE_curve_forward_diff_bezier(0.0f,  h1[0],                               h2[0],                               0.0f,   data[0],     MAX_BBONE_SUBDIV, 4 * sizeof(float));
1319         BKE_curve_forward_diff_bezier(0.0f,  h1[1],                               length + h2[1],                      length, data[0] + 1, MAX_BBONE_SUBDIV, 4 * sizeof(float));
1320         BKE_curve_forward_diff_bezier(0.0f,  h1[2],                               h2[2],                               0.0f,   data[0] + 2, MAX_BBONE_SUBDIV, 4 * sizeof(float));
1321         BKE_curve_forward_diff_bezier(roll1, roll1 + 0.390464f * (roll2 - roll1), roll2 - 0.390464f * (roll2 - roll1), roll2,  data[0] + 3, MAX_BBONE_SUBDIV, 4 * sizeof(float));
1322
1323         equalize_bbone_bezier(data[0], ebone->segments); /* note: does stride 4! */
1324
1325         /* make transformation matrices for the segments for drawing */
1326         for (a = 0, fp = data[0]; a < ebone->segments; a++, fp += 4) {
1327                 sub_v3_v3v3(h1, fp + 4, fp);
1328                 vec_roll_to_mat3(h1, fp[3], mat3); /* fp[3] is roll */
1329                 
1330                 copy_m4_m3(result_array[a].mat, mat3);
1331                 copy_v3_v3(result_array[a].mat[3], fp);
1332                 
1333                 /* "extra" scale facs... */
1334                 {
1335                         const int num_segments = ebone->segments;
1336                         
1337                         const float scaleFactorIn  = 1.0f + (ebone->scaleIn  - 1.0f) * ((float)(num_segments - a) / (float)num_segments);
1338                         const float scaleFactorOut = 1.0f + (ebone->scaleOut - 1.0f) * ((float)(a + 1)            / (float)num_segments);
1339                         
1340                         const float scalefac = scaleFactorIn * scaleFactorOut;
1341                         float bscalemat[4][4], bscale[3];
1342                         
1343                         bscale[0] = scalefac;
1344                         bscale[1] = 1.0f;
1345                         bscale[2] = scalefac;
1346                         
1347                         size_to_mat4(bscalemat, bscale);
1348                         
1349                         /* Note: don't multiply by inverse scale mat here, as it causes problems with scaling shearing and breaking segment chains */
1350                         mul_m4_series(result_array[a].mat, result_array[a].mat, bscalemat);
1351                 }
1352         }
1353 }
1354
1355 static void draw_b_bone_boxes(const short dt, bPoseChannel *pchan, EditBone *ebone, float xwidth, float length, float zwidth)
1356 {
1357         int segments = 0;
1358         
1359         if (pchan) 
1360                 segments = pchan->bone->segments;
1361         else if (ebone)
1362                 segments = ebone->segments;
1363         
1364         if (segments > 1) {
1365                 float dlen = length / (float)segments;
1366                 Mat4 bbone[MAX_BBONE_SUBDIV];
1367                 int a;
1368                 
1369                 if (pchan) {
1370                         b_bone_spline_setup(pchan, 0, bbone);
1371                 }
1372                 else if (ebone) {
1373                         ebone_spline_preview(ebone, bbone);
1374                 }
1375                 
1376                 for (a = 0; a < segments; a++) {
1377                         glPushMatrix();
1378                         glMultMatrixf(bbone[a].mat);
1379                         if (dt == OB_SOLID) drawsolidcube_size(xwidth, dlen, zwidth);
1380                         else drawcube_size(xwidth, dlen, zwidth);
1381                         glPopMatrix();
1382                 }
1383         }
1384         else {
1385                 glPushMatrix();
1386                 if (dt == OB_SOLID) drawsolidcube_size(xwidth, length, zwidth);
1387                 else drawcube_size(xwidth, length, zwidth);
1388                 glPopMatrix();
1389         }
1390 }
1391
1392 static void draw_b_bone(const short dt, int armflag, int boneflag, short constflag, unsigned int id,
1393                         bPoseChannel *pchan, EditBone *ebone)
1394 {
1395         float xwidth, length, zwidth;
1396         
1397         if (pchan) {
1398                 xwidth = pchan->bone->xwidth;
1399                 length = pchan->bone->length;
1400                 zwidth = pchan->bone->zwidth;
1401         }
1402         else {
1403                 xwidth = ebone->xwidth;
1404                 length = ebone->length;
1405                 zwidth = ebone->zwidth;
1406         }
1407         
1408         /* draw points only if... */
1409         if (armflag & ARM_EDITMODE) {
1410                 /* move to unitspace */
1411                 glPushMatrix();
1412                 glScalef(length, length, length);
1413                 draw_bone_points(dt, armflag, boneflag, id);
1414                 glPopMatrix();
1415                 length *= 0.95f;  /* make vertices visible */
1416         }
1417
1418         /* colors for modes */
1419         if (armflag & ARM_POSEMODE) {
1420                 if (dt <= OB_WIRE)
1421                         set_pchan_color(PCHAN_COLOR_NORMAL, boneflag, constflag);
1422                 else 
1423                         set_pchan_color(PCHAN_COLOR_SOLID, boneflag, constflag);
1424         }
1425         else if (armflag & ARM_EDITMODE) {
1426                 if (dt == OB_WIRE) {
1427                         set_ebone_color(boneflag);
1428                 }
1429                 else {
1430                         UI_GetThemeColor4fv(TH_BONE_SOLID, fcolor);
1431                 }
1432         }
1433         
1434         if (id != -1) {
1435                 GPU_select_load_id((GLuint) id | BONESEL_BONE);
1436         }
1437         
1438         /* set up solid drawing */
1439         if (dt > OB_WIRE) {
1440                 if (armflag & ARM_POSEMODE)
1441                         set_pchan_color(PCHAN_COLOR_SOLID, boneflag, constflag);
1442                 else {
1443                         UI_GetThemeColor4fv(TH_BONE_SOLID, fcolor);
1444                 }
1445                 
1446                 flat_color = false;
1447                 draw_b_bone_boxes(OB_SOLID, pchan, ebone, xwidth, length, zwidth);
1448         }
1449         else {
1450                 /* wire */
1451                 if (armflag & ARM_POSEMODE) {
1452                         if (constflag && ((G.f & G_PICKSEL) == 0)) {
1453                                 /* set constraint colors */
1454                                 if (set_pchan_color(PCHAN_COLOR_CONSTS, boneflag, constflag)) {
1455                                         glEnable(GL_BLEND);
1456                                         
1457                                         flat_color = true;
1458                                         draw_b_bone_boxes(OB_SOLID, pchan, ebone, xwidth, length, zwidth);
1459                                         
1460                                         glDisable(GL_BLEND);
1461                                 }
1462                                 
1463                                 /* restore colors */
1464                                 set_pchan_color(PCHAN_COLOR_NORMAL, boneflag, constflag);
1465                         }
1466                 }
1467                 
1468                 draw_b_bone_boxes(OB_WIRE, pchan, ebone, xwidth, length, zwidth);
1469         }
1470 }
1471
1472 static void draw_wire_bone_segments(bPoseChannel *pchan, Mat4 *bbones, float length, int segments)
1473 {
1474         VertexFormat *format = immVertexFormat();
1475         unsigned int pos = add_attrib(format, "pos", GL_FLOAT, 3, KEEP_FLOAT);
1476
1477         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
1478         immUniformColor4fv(fcolor);
1479
1480         gpuMatrixBegin3D_legacy();
1481
1482         if ((segments > 1) && (pchan)) {
1483                 float dlen = length / (float)segments;
1484                 Mat4 *bbone = bbones;
1485                 int a;
1486
1487                 for (a = 0; a < segments; a++, bbone++) {
1488                         gpuPushMatrix();
1489                         gpuMultMatrix3D(bbone->mat);
1490
1491                         immBegin(GL_LINES, 2);
1492                         immVertex3f(pos, 0.0f, 0.0f, 0.0f);
1493                         immVertex3f(pos, 0.0f, dlen, 0.0f);
1494                         immEnd();
1495
1496                         gpuPopMatrix();
1497                 }
1498         }
1499         else {
1500                 gpuPushMatrix();
1501
1502                 immBegin(GL_LINES, 2);
1503                 immVertex3f(pos, 0.0f, 0.0f, 0.0f);
1504                 immVertex3f(pos, 0.0f, length, 0.0f);
1505                 immEnd();
1506
1507                 gpuPopMatrix();
1508         }
1509
1510         gpuMatrixEnd();
1511
1512         immUnbindProgram();
1513 }
1514
1515 static void draw_wire_bone(const short dt, int armflag, int boneflag, short constflag, unsigned int id,
1516                            bPoseChannel *pchan, EditBone *ebone)
1517 {
1518         Mat4 bbones_array[MAX_BBONE_SUBDIV];
1519         Mat4 *bbones = NULL;
1520         int segments = 0;
1521         float length;
1522         
1523         if (pchan) {
1524                 segments = pchan->bone->segments;
1525                 length = pchan->bone->length;
1526                 
1527                 if (segments > 1) {
1528                         b_bone_spline_setup(pchan, 0, bbones_array);
1529                         bbones = bbones_array;
1530                 }
1531         }
1532         else 
1533                 length = ebone->length;
1534         
1535         /* draw points only if... */
1536         if (armflag & ARM_EDITMODE) {
1537                 /* move to unitspace */
1538                 glPushMatrix();
1539                 glScalef(length, length, length);
1540                 flat_color = true;
1541                 draw_bone_points(dt, armflag, boneflag, id);
1542                 glPopMatrix();
1543                 length *= 0.95f;  /* make vertices visible */
1544         }
1545         
1546         /* this chunk not in object mode */
1547         if (armflag & (ARM_EDITMODE | ARM_POSEMODE)) {
1548                 if (id != -1)
1549                         GPU_select_load_id((GLuint) id | BONESEL_BONE);
1550                 
1551                 draw_wire_bone_segments(pchan, bbones, length, segments);
1552                 
1553                 /* further we send no names */
1554                 if (id != -1)
1555                         GPU_select_load_id(id & 0xFFFF);    /* object tag, for bordersel optim */
1556         }
1557         
1558         /* colors for modes */
1559         if (armflag & ARM_POSEMODE) {
1560                 set_pchan_color(PCHAN_COLOR_NORMAL, boneflag, constflag);
1561         }
1562         else if (armflag & ARM_EDITMODE) {
1563                 set_ebone_color(boneflag);
1564         }
1565         
1566         /* draw normal */
1567         draw_wire_bone_segments(pchan, bbones, length, segments);
1568 }
1569
1570 static void draw_bone(const short dt, int armflag, int boneflag, short constflag, unsigned int id, float length)
1571 {
1572         
1573         /* Draw a 3d octahedral bone, we use normalized space based on length */
1574         glScalef(length, length, length);
1575
1576         /* set up solid drawing */
1577         if (dt > OB_WIRE) {
1578                 UI_GetThemeColor4fv(TH_BONE_SOLID, fcolor);
1579                 flat_color = false;
1580         }
1581         else
1582                 flat_color = true;
1583         
1584         /* colors for posemode */
1585         if (armflag & ARM_POSEMODE) {
1586                 if (dt <= OB_WIRE)
1587                         set_pchan_color(PCHAN_COLOR_NORMAL, boneflag, constflag);
1588                 else 
1589                         set_pchan_color(PCHAN_COLOR_SOLID, boneflag, constflag);
1590         }
1591         
1592         
1593         draw_bone_points(dt, armflag, boneflag, id);
1594         
1595         /* now draw the bone itself */
1596         if (id != -1) {
1597                 GPU_select_load_id((GLuint) id | BONESEL_BONE);
1598         }
1599         
1600         /* wire? */
1601         if (dt <= OB_WIRE) {
1602                 /* colors */
1603                 if (armflag & ARM_EDITMODE) {
1604                         set_ebone_color(boneflag);
1605                 }
1606                 else if (armflag & ARM_POSEMODE) {
1607                         if (constflag && ((G.f & G_PICKSEL) == 0)) {
1608                                 /* draw constraint colors */
1609                                 if (set_pchan_color(PCHAN_COLOR_CONSTS, boneflag, constflag)) {
1610                                         glEnable(GL_BLEND);
1611                                         
1612                                         draw_bone_solid_octahedral();
1613                                         
1614                                         glDisable(GL_BLEND);
1615                                 }
1616                                 
1617                                 /* restore colors */
1618                                 set_pchan_color(PCHAN_COLOR_NORMAL, boneflag, constflag);
1619                         }
1620                 }
1621                 draw_bone_octahedral();
1622         }
1623         else {
1624                 /* solid */
1625                 if (armflag & ARM_POSEMODE)
1626                         set_pchan_color(PCHAN_COLOR_SOLID, boneflag, constflag);
1627                 else
1628                         UI_GetThemeColor4fv(TH_BONE_SOLID, fcolor);
1629
1630                 draw_bone_solid_octahedral();
1631         }
1632 }
1633
1634 static void draw_custom_bone(Scene *scene, SceneLayer *sl, View3D *v3d, RegionView3D *rv3d, Object *ob,
1635                              const short dt, int armflag, int boneflag, unsigned int id, float length)
1636 {
1637         if (ob == NULL) return;
1638         
1639         glScalef(length, length, length);
1640         
1641         /* colors for posemode */
1642         if (armflag & ARM_POSEMODE) {
1643                 set_pchan_color(PCHAN_COLOR_NORMAL, boneflag, 0);
1644         }
1645         
1646         if (id != -1) {
1647                 GPU_select_load_id((GLuint) id | BONESEL_BONE);
1648         }
1649
1650         draw_object_instance(scene, sl, v3d, rv3d, ob, dt, armflag & ARM_POSEMODE, fcolor);
1651 }
1652
1653
1654 static void pchan_draw_IK_root_lines(bPoseChannel *pchan, short only_temp)
1655 {
1656         bConstraint *con;
1657         bPoseChannel *parchan;
1658
1659         VertexFormat *format = immVertexFormat();
1660         unsigned int pos = add_attrib(format, "pos", GL_FLOAT, 3, KEEP_FLOAT);
1661
1662         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
1663         immUniformColor4fv(fcolor);
1664
1665         setlinestyle(3);
1666         for (con = pchan->constraints.first; con; con = con->next) {
1667                 if (con->enforce == 0.0f)
1668                         continue;
1669                 
1670                 switch (con->type) {
1671                         case CONSTRAINT_TYPE_KINEMATIC:
1672                         {
1673                                 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
1674                                 int segcount = 0;
1675                                 float ik_tip[3];
1676                                 
1677                                 /* if only_temp, only draw if it is a temporary ik-chain */
1678                                 if ((only_temp) && !(data->flag & CONSTRAINT_IK_TEMP))
1679                                         continue;
1680
1681                                 /* exclude tip from chain? */
1682                                 if ((data->flag & CONSTRAINT_IK_TIP) == 0)
1683                                         parchan = pchan->parent;
1684                                 else
1685                                         parchan = pchan;
1686                                 
1687                                 copy_v3_v3(ik_tip, parchan->pose_tail);
1688                                 
1689                                 /* Find the chain's root */
1690                                 while (parchan->parent) {
1691                                         segcount++;
1692                                         /* FIXME: revise the breaking conditions */
1693                                         if (segcount == data->rootbone || segcount > 255) break;  /* 255 is weak */
1694                                         parchan = parchan->parent;
1695                                 }
1696
1697                                 if (parchan) {
1698                                         immBegin(GL_LINES, 2);
1699                                         immVertex3fv(pos, ik_tip);
1700                                         immVertex3fv(pos, parchan->pose_head);
1701                                         immEnd();
1702                                 }
1703                                 
1704                                 break;
1705                         }
1706                         case CONSTRAINT_TYPE_SPLINEIK: 
1707                         {
1708                                 bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
1709                                 int segcount = 0;
1710                                 float ik_tip[3];
1711                                 
1712                                 parchan = pchan;
1713                                 copy_v3_v3(ik_tip, parchan->pose_tail);
1714                                 
1715                                 /* Find the chain's root */
1716                                 while (parchan->parent) {
1717                                         segcount++;
1718                                         /* FIXME: revise the breaking conditions */
1719                                         if (segcount == data->chainlen || segcount > 255) break;  /* 255 is weak */
1720                                         parchan = parchan->parent;
1721                                 }
1722                                 /* Only draw line in case our chain is more than one bone long! */
1723                                 if (parchan != pchan) { /* XXX revise the breaking conditions to only stop at the tail? */
1724                                         immBegin(GL_LINES, 2);
1725                                         immVertex3fv(pos, ik_tip);
1726                                         immVertex3fv(pos, parchan->pose_head);
1727                                         immEnd();
1728                                 }
1729                                 break;
1730                         }
1731                 }
1732         }
1733         setlinestyle(0);
1734         immUnbindProgram();
1735 }
1736
1737 static void imm_sphere_project(unsigned int pos, float ax, float az)
1738 {
1739         float dir[3], sine, q3;
1740
1741         sine = 1.0f - ax * ax - az * az;
1742         q3 = (sine < 0.0f) ? 0.0f : (2.0f * sqrtf(sine));
1743
1744         dir[0] = -az * q3;
1745         dir[1] = 1.0f - 2.0f * sine;
1746         dir[2] = ax * q3;
1747
1748         immVertex3fv(pos, dir);
1749 }
1750
1751 static void draw_dof_ellipse(unsigned int pos, float ax, float az)
1752 {
1753         const int n = 16;
1754         const int tri = n*n - 2*n + 1; /* Yay fancy math ! */
1755         const float staticSine[16] = {
1756                 0.0f, 0.104528463268f, 0.207911690818f, 0.309016994375f,
1757                 0.406736643076f, 0.5f, 0.587785252292f, 0.669130606359f,
1758                 0.743144825477f, 0.809016994375f, 0.866025403784f,
1759                 0.913545457643f, 0.951056516295f, 0.978147600734f,
1760                 0.994521895368f, 1.0f
1761         };
1762
1763         int i, j;
1764         float x, z, px, pz;
1765
1766         glEnable(GL_BLEND);
1767         glDepthMask(0);
1768
1769         immUniformColor4ub(70, 70, 70, 50);
1770
1771         immBegin(GL_TRIANGLES, tri*3);
1772         pz = 0.0f;
1773         for (i = 1; i < n; i++) {
1774                 z = staticSine[i];
1775                 
1776                 px = 0.0f;
1777                 for (j = 1; j <= (n - i); j++) {
1778                         x = staticSine[j];
1779                         
1780                         if (j == n - i) {
1781                                 imm_sphere_project(pos, ax * px, az * z);
1782                                 imm_sphere_project(pos, ax * px, az * pz);
1783                                 imm_sphere_project(pos, ax * x, az * pz);
1784                         }
1785                         else {
1786                                 imm_sphere_project(pos, ax * x, az * z);
1787                                 imm_sphere_project(pos, ax * x, az * pz);
1788                                 imm_sphere_project(pos, ax * px, az * pz);
1789
1790                                 imm_sphere_project(pos, ax * px, az * pz);
1791                                 imm_sphere_project(pos, ax * px, az * z);
1792                                 imm_sphere_project(pos, ax * x, az * z);
1793                         }
1794                         
1795                         px = x;
1796                 }
1797                 pz = z;
1798         }
1799         immEnd();
1800
1801         glDisable(GL_BLEND);
1802         glDepthMask(1);
1803
1804         immUniformColor3ub(0, 0, 0);
1805
1806         immBegin(GL_LINE_STRIP, n);
1807         for (i = 0; i < n; i++)
1808                 imm_sphere_project(pos, staticSine[n - i - 1] * ax, staticSine[i] * az);
1809         immEnd();
1810 }
1811
1812 static void draw_pose_dofs(Object *ob)
1813 {
1814         bArmature *arm = ob->data;
1815         bPoseChannel *pchan;
1816         Bone *bone;
1817
1818         VertexFormat *format = immVertexFormat();
1819         unsigned int pos = add_attrib(format, "pos", GL_FLOAT, 3, KEEP_FLOAT);
1820
1821         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
1822         gpuMatrixBegin3D_legacy();
1823
1824         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1825                 bone = pchan->bone;
1826                 
1827                 if ((bone != NULL) && !(bone->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG))) {
1828                         if (bone->flag & BONE_SELECTED) {
1829                                 if (bone->layer & arm->layer) {
1830                                         if (pchan->ikflag & (BONE_IK_XLIMIT | BONE_IK_ZLIMIT)) {
1831                                                 if (BKE_pose_channel_in_IK_chain(ob, pchan)) {
1832                                                         float corner[4][3], posetrans[3], mat[4][4];
1833                                                         float phi = 0.0f, theta = 0.0f, scale;
1834                                                         int a, i;
1835                                                         
1836                                                         /* in parent-bone pose, but own restspace */
1837                                                         gpuPushMatrix();
1838                                                         
1839                                                         copy_v3_v3(posetrans, pchan->pose_mat[3]);
1840                                                         gpuTranslate3fv(posetrans);
1841                                                         
1842                                                         if (pchan->parent) {
1843                                                                 copy_m4_m4(mat, pchan->parent->pose_mat);
1844                                                                 mat[3][0] = mat[3][1] = mat[3][2] = 0.0f;
1845                                                                 gpuMultMatrix3D(mat);
1846                                                         }
1847                                                         
1848                                                         copy_m4_m3(mat, pchan->bone->bone_mat);
1849                                                         gpuMultMatrix3D(mat);
1850                                                         
1851                                                         scale = bone->length * pchan->size[1];
1852                                                         gpuScale3f(scale, scale, scale);
1853                                                         
1854                                                         if (((pchan->ikflag & BONE_IK_XLIMIT) != 0) &&
1855                                                             ((pchan->ikflag & BONE_IK_ZLIMIT) != 0))
1856                                                         {
1857                                                                 float amin[3], amax[3];
1858
1859                                                                 for (i = 0; i < 3; i++) {
1860                                                                         /* *0.5f here comes from M_PI/360.0f when rotations were still in degrees */
1861                                                                         amin[i] = sinf(pchan->limitmin[i] * 0.5f);
1862                                                                         amax[i] = sinf(pchan->limitmax[i] * 0.5f);
1863                                                                 }
1864
1865                                                                 gpuScale3f(1.0f, -1.0f, 1.0f);
1866                                                                 if ((amin[0] != 0.0f) && (amin[2] != 0.0f))
1867                                                                         draw_dof_ellipse(pos, amin[0], amin[2]);
1868                                                                 if ((amin[0] != 0.0f) && (amax[2] != 0.0f))
1869                                                                         draw_dof_ellipse(pos, amin[0], amax[2]);
1870                                                                 if ((amax[0] != 0.0f) && (amin[2] != 0.0f))
1871                                                                         draw_dof_ellipse(pos, amax[0], amin[2]);
1872                                                                 if ((amax[0] != 0.0f) && (amax[2] != 0.0f))
1873                                                                         draw_dof_ellipse(pos, amax[0], amax[2]);
1874                                                                 gpuScale3f(1.0f, -1.0f, 1.0f);
1875                                                         }
1876                                                         
1877                                                         /* arcs */
1878                                                         if (pchan->ikflag & BONE_IK_ZLIMIT) {
1879                                                                 /* OpenGL requires rotations in degrees; so we're taking the average angle here */
1880                                                                 theta = RAD2DEGF(0.5f * (pchan->limitmin[2] + pchan->limitmax[2]));
1881                                                                 gpuRotate3f(theta, 0.0f, 0.0f, 1.0f);
1882                                                                 
1883                                                                 immUniformColor3ub(50, 50, 255);  /* blue, Z axis limit */
1884                                                                 immBegin(GL_LINE_STRIP, 33);
1885                                                                 for (a = -16; a <= 16; a++) {
1886                                                                         /* *0.5f here comes from M_PI/360.0f when rotations were still in degrees */
1887                                                                         float fac = ((float)a) / 16.0f * 0.5f;
1888                                                                         
1889                                                                         phi = fac * (pchan->limitmax[2] - pchan->limitmin[2]);
1890                                                                         
1891                                                                         i = (a == -16) ? 0 : 1;
1892                                                                         corner[i][0] = sinf(phi);
1893                                                                         corner[i][1] = cosf(phi);
1894                                                                         corner[i][2] = 0.0f;
1895                                                                         immVertex3fv(pos, corner[i]);
1896                                                                 }
1897                                                                 immEnd();
1898                                                                 
1899                                                                 gpuRotate3f(-theta, 0.0f, 0.0f, 1.0f);
1900                                                         }
1901                                                         
1902                                                         if (pchan->ikflag & BONE_IK_XLIMIT) {
1903                                                                 /* OpenGL requires rotations in degrees; so we're taking the average angle here */
1904                                                                 theta = RAD2DEGF(0.5f * (pchan->limitmin[0] + pchan->limitmax[0]));
1905                                                                 gpuRotate3f(theta, 1.0f, 0.0f, 0.0f);
1906                                                                 
1907                                                                 immUniformColor3ub(255, 50, 50);  /* Red, X axis limit */
1908                                                                 immBegin(GL_LINE_STRIP, 33);
1909                                                                 for (a = -16; a <= 16; a++) {
1910                                                                         /* *0.5f here comes from M_PI/360.0f when rotations were still in degrees */
1911                                                                         float fac = ((float)a) / 16.0f * 0.5f;
1912                                                                         phi = (float)M_PI_2 + fac * (pchan->limitmax[0] - pchan->limitmin[0]);
1913                                                                         
1914                                                                         i = (a == -16) ? 2 : 3;
1915                                                                         corner[i][0] = 0.0f;
1916                                                                         corner[i][1] = sinf(phi);
1917                                                                         corner[i][2] = cosf(phi);
1918                                                                         immVertex3fv(pos, corner[i]);
1919                                                                 }
1920                                                                 immEnd();
1921                                                                 
1922                                                                 gpuRotate3f(-theta, 1.0f, 0.0f, 0.0f);
1923                                                         }
1924                                                         
1925                                                         /* out of cone, out of bone */
1926                                                         gpuPopMatrix();
1927                                                 }
1928                                         }
1929                                 }
1930                         }
1931                 }
1932         }
1933         gpuMatrixEnd();
1934         immUnbindProgram();
1935 }
1936
1937 static void bone_matrix_translate_y(float mat[4][4], float y)
1938 {
1939         float trans[3];
1940
1941         copy_v3_v3(trans, mat[1]);
1942         mul_v3_fl(trans, y);
1943         add_v3_v3(mat[3], trans);
1944 }
1945
1946 /* assumes object is Armature with pose */
1947 static void draw_pose_bones(Scene *scene, SceneLayer *sl, View3D *v3d, ARegion *ar, Base *base,
1948                             const short dt, const unsigned char ob_wire_col[4],
1949                             const bool do_const_color, const bool is_outline)
1950 {
1951         RegionView3D *rv3d = ar->regiondata;
1952         Object *ob = base->object;
1953         bArmature *arm = ob->data;
1954         bPoseChannel *pchan;
1955         Bone *bone;
1956         float smat[4][4], imat[4][4], bmat[4][4];
1957         int index = -1;
1958         const enum {
1959                 DASH_RELATIONSHIP_LINES = 1,
1960                 DASH_HELP_LINES = 2,
1961         } do_dashed = (
1962                 (is_outline ? 0 : DASH_RELATIONSHIP_LINES) |
1963                 ((v3d->flag & V3D_HIDE_HELPLINES) ? 0 : DASH_HELP_LINES));
1964         bool draw_wire = false;
1965         int flag;
1966         bool is_cull_enabled;
1967         
1968         /* being set below */
1969         arm->layer_used = 0;
1970
1971         rgba_uchar_to_float(fcolor, ob_wire_col);
1972
1973         /* precalc inverse matrix for drawing screen aligned */
1974         if (arm->drawtype == ARM_ENVELOPE) {
1975                 /* precalc inverse matrix for drawing screen aligned */
1976                 copy_m4_m4(smat, rv3d->viewmatob);
1977                 mul_mat3_m4_fl(smat, 1.0f / len_v3(ob->obmat[0]));
1978                 invert_m4_m4(imat, smat);
1979                 
1980                 /* and draw blended distances */
1981                 if (arm->flag & ARM_POSEMODE) {
1982                         glEnable(GL_BLEND);
1983                         
1984                         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
1985                         
1986                         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1987                                 bone = pchan->bone;
1988                                 if (bone) {
1989                                         /* 1) bone must be visible, 2) for OpenGL select-drawing cannot have unselectable [#27194] 
1990                                          * NOTE: this is the only case with (NO_DEFORM == 0) flag, as this is for envelope influence drawing
1991                                          */
1992                                         if (((bone->flag & (BONE_HIDDEN_P | BONE_NO_DEFORM | BONE_HIDDEN_PG)) == 0) &&
1993                                             ((G.f & G_PICKSEL) == 0 || (bone->flag & BONE_UNSELECTABLE) == 0))
1994                                         {
1995                                                 if (bone->flag & (BONE_SELECTED)) {
1996                                                         if (bone->layer & arm->layer)
1997                                                                 draw_sphere_bone_dist(smat, imat, pchan, NULL);
1998                                                 }
1999                                         }
2000                                 }
2001                         }
2002                         
2003                         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2004                         glDisable(GL_BLEND);
2005                 }
2006         }
2007         
2008         /* little speedup, also make sure transparent only draws once */
2009         glCullFace(GL_BACK);
2010         if (v3d->flag2 & V3D_BACKFACE_CULLING) {
2011                 glEnable(GL_CULL_FACE);
2012                 is_cull_enabled = true;
2013         }
2014         else {
2015                 is_cull_enabled = false;
2016         }
2017
2018         /* if solid we draw that first, with selection codes, but without names, axes etc */
2019         if (dt > OB_WIRE) {
2020                 if (arm->flag & ARM_POSEMODE) 
2021                         index = base->selcol;
2022                 
2023                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
2024                         bone = pchan->bone;
2025                         arm->layer_used |= bone->layer;
2026                         
2027                         /* 1) bone must be visible, 2) for OpenGL select-drawing cannot have unselectable [#27194] */
2028                         if (((bone->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)) == 0) &&
2029                             ((G.f & G_PICKSEL) == 0 || (bone->flag & BONE_UNSELECTABLE) == 0))
2030                         {
2031                                 if (bone->layer & arm->layer) {
2032                                         const bool use_custom = (pchan->custom) && !(arm->flag & ARM_NO_CUSTOM);
2033                                         glPushMatrix();
2034                                         
2035                                         if (use_custom && pchan->custom_tx) {
2036                                                 glMultMatrixf(pchan->custom_tx->pose_mat);
2037                                         }
2038                                         else {
2039                                                 glMultMatrixf(pchan->pose_mat);
2040                                         }
2041                                         
2042                                         /* catch exception for bone with hidden parent */
2043                                         flag = bone->flag;
2044                                         if ((bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG))) {
2045                                                 flag &= ~BONE_CONNECTED;
2046                                         }
2047                                         
2048                                         /* set temporary flag for drawing bone as active, but only if selected */
2049                                         if (bone == arm->act_bone)
2050                                                 flag |= BONE_DRAW_ACTIVE;
2051                                         
2052                                         if (do_const_color) {
2053                                                 /* keep color */
2054                                         }
2055                                         else {
2056                                                 /* set color-set to use */
2057                                                 set_pchan_colorset(ob, pchan);
2058                                         }
2059
2060                                         /* may be 2x width from custom bone's outline option */
2061                                         glLineWidth(1.0f);
2062
2063                                         if (use_custom) {
2064                                                 /* if drawwire, don't try to draw in solid */
2065                                                 if (pchan->bone->flag & BONE_DRAWWIRE) {
2066                                                         draw_wire = true;
2067                                                 }
2068                                                 else {
2069                                                         if (is_cull_enabled && (v3d->flag2 & V3D_BACKFACE_CULLING) == 0) {
2070                                                                 is_cull_enabled = false;
2071                                                                 glDisable(GL_CULL_FACE);
2072                                                         }
2073
2074                                                         draw_custom_bone(scene, sl, v3d, rv3d, pchan->custom,
2075                                                                          OB_SOLID, arm->flag, flag, index, PCHAN_CUSTOM_DRAW_SIZE(pchan));
2076                                                 }
2077                                         }
2078                                         else {
2079                                                 if (is_cull_enabled == false) {
2080                                                         is_cull_enabled = true;
2081                                                         glEnable(GL_CULL_FACE);
2082                                                 }
2083
2084                                                 if (arm->drawtype == ARM_LINE) {
2085                                                         /* nothing in solid */
2086                                                 }
2087                                                 else if (arm->drawtype == ARM_WIRE) {
2088                                                         /* nothing in solid */
2089                                                 }
2090                                                 else if (arm->drawtype == ARM_ENVELOPE) {
2091                                                         draw_sphere_bone(OB_SOLID, arm->flag, flag, 0, index, pchan, NULL);
2092                                                 }
2093                                                 else if (arm->drawtype == ARM_B_BONE) {
2094                                                         draw_b_bone(OB_SOLID, arm->flag, flag, 0, index, pchan, NULL);
2095                                                 }
2096                                                 else {
2097                                                         draw_bone(OB_SOLID, arm->flag, flag, 0, index, bone->length);
2098                                                 }
2099                                         }
2100
2101                                         glPopMatrix();
2102                                 }
2103                         }
2104                         
2105                         if (index != -1)
2106                                 index += 0x10000;  /* pose bones count in higher 2 bytes only */
2107                 }
2108                 
2109                 /* very very confusing... but in object mode, solid draw, we cannot do GPU_select_load_id yet,
2110                  * stick bones and/or wire custom-shapes are drawn in next loop 
2111                  */
2112                 if (ELEM(arm->drawtype, ARM_LINE, ARM_WIRE) == 0 && (draw_wire == false) && index != -1) {
2113                         /* object tag, for bordersel optim */
2114                         GPU_select_load_id(index & 0xFFFF);
2115                         index = -1;
2116                 }
2117         }
2118         
2119         /* draw custom bone shapes as wireframes */
2120         if (!(arm->flag & ARM_NO_CUSTOM) &&
2121             (draw_wire || (dt <= OB_WIRE)) )
2122         {
2123                 if (arm->flag & ARM_POSEMODE)
2124                         index = base->selcol;
2125                         
2126                 /* only draw custom bone shapes that need to be drawn as wires */
2127                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
2128                         bone = pchan->bone;
2129                         
2130                         /* 1) bone must be visible, 2) for OpenGL select-drawing cannot have unselectable [#27194] */
2131                         if (((bone->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)) == 0) &&
2132                             ((G.f & G_PICKSEL) == 0 || (bone->flag & BONE_UNSELECTABLE) == 0) )
2133                         {
2134                                 if (bone->layer & arm->layer) {
2135                                         if (pchan->custom) {
2136                                                 if ((dt < OB_SOLID) || (bone->flag & BONE_DRAWWIRE)) {
2137                                                         glPushMatrix();
2138                                                         
2139                                                         if (pchan->custom_tx) {
2140                                                                 glMultMatrixf(pchan->custom_tx->pose_mat);
2141                                                         }
2142                                                         else {
2143                                                                 glMultMatrixf(pchan->pose_mat);
2144                                                         }
2145                                                         
2146                                                         /* prepare colors */
2147                                                         if (do_const_color) {
2148                                                                 /* 13 October 2009, Disabled this to make ghosting show the right colors (Aligorith) */
2149                                                         }
2150                                                         else if (arm->flag & ARM_POSEMODE)
2151                                                                 set_pchan_colorset(ob, pchan);
2152                                                         else {
2153                                                                 rgba_uchar_to_float(fcolor, ob_wire_col);
2154                                                         }
2155                                                                 
2156                                                         /* catch exception for bone with hidden parent */
2157                                                         flag = bone->flag;
2158                                                         if ((bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)))
2159                                                                 flag &= ~BONE_CONNECTED;
2160                                                                 
2161                                                         /* set temporary flag for drawing bone as active, but only if selected */
2162                                                         if (bone == arm->act_bone)
2163                                                                 flag |= BONE_DRAW_ACTIVE;
2164                                                         
2165                                                         draw_custom_bone(scene, sl, v3d, rv3d, pchan->custom,
2166                                                                          OB_WIRE, arm->flag, flag, index, PCHAN_CUSTOM_DRAW_SIZE(pchan));
2167                                                         
2168                                                         glPopMatrix();
2169                                                 }
2170                                         }
2171                                 }
2172                         }
2173                         
2174                         if (index != -1) 
2175                                 index += 0x10000;  /* pose bones count in higher 2 bytes only */
2176                 }
2177                 /* stick or wire bones have not been drawn yet so don't clear object selection in this case */
2178                 if (ELEM(arm->drawtype, ARM_LINE, ARM_WIRE) == 0 && draw_wire && index != -1) {
2179                         /* object tag, for bordersel optim */
2180                         GPU_select_load_id(index & 0xFFFF);
2181                         index = -1;
2182                 }
2183         }
2184         
2185         /* custom bone may draw outline double-width */
2186         if (arm->flag & ARM_POSEMODE) {
2187                 glLineWidth(1.0f);
2188         }
2189
2190         /* wire draw over solid only in posemode */
2191         if ((dt <= OB_WIRE) || (arm->flag & ARM_POSEMODE) || ELEM(arm->drawtype, ARM_LINE, ARM_WIRE)) {
2192                 /* draw line check first. we do selection indices */
2193                 if (ELEM(arm->drawtype, ARM_LINE, ARM_WIRE)) {
2194                         if (arm->flag & ARM_POSEMODE) 
2195                                 index = base->selcol;
2196                 }
2197                 /* if solid && posemode, we draw again with polygonoffset */
2198                 else if ((dt > OB_WIRE) && (arm->flag & ARM_POSEMODE)) {
2199                         ED_view3d_polygon_offset(rv3d, 1.0);
2200                 }
2201                 else {
2202                         /* and we use selection indices if not done yet */
2203                         if (arm->flag & ARM_POSEMODE) 
2204                                 index = base->selcol;
2205                 }
2206
2207                 if (is_cull_enabled == false) {
2208                         is_cull_enabled = true;
2209                         glEnable(GL_CULL_FACE);
2210                 }
2211
2212                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
2213                         bone = pchan->bone;
2214                         arm->layer_used |= bone->layer;
2215                         
2216                         /* 1) bone must be visible, 2) for OpenGL select-drawing cannot have unselectable [#27194] */
2217                         if (((bone->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)) == 0) &&
2218                             ((G.f & G_PICKSEL) == 0 || (bone->flag & BONE_UNSELECTABLE) == 0))
2219                         {
2220                                 if (bone->layer & arm->layer) {
2221                                         const short constflag = pchan->constflag;
2222                                         if ((do_dashed & DASH_RELATIONSHIP_LINES) && (pchan->parent)) {
2223                                                 /* Draw a line from our root to the parent's tip 
2224                                                  * - only if V3D_HIDE_HELPLINES is enabled...
2225                                                  */
2226                                                 if ((do_dashed & DASH_HELP_LINES) && ((bone->flag & BONE_CONNECTED) == 0)) {
2227                                                         VertexFormat *format = immVertexFormat();
2228                                                         unsigned int pos = add_attrib(format, "pos", GL_FLOAT, 3, KEEP_FLOAT);
2229
2230                                                         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
2231
2232                                                         if (arm->flag & ARM_POSEMODE) {
2233                                                                 GPU_select_load_id(index & 0xFFFF);  /* object tag, for bordersel optim */
2234                                                                 UI_GetThemeColor4fv(TH_WIRE, fcolor);
2235                                                                 immUniformColor4fv(fcolor);
2236                                                         }
2237
2238                                                         setlinestyle(3);
2239                                                         immBegin(GL_LINES, 2);
2240                                                         immVertex3fv(pos, pchan->pose_head);
2241                                                         immVertex3fv(pos, pchan->parent->pose_tail);
2242                                                         immEnd();
2243                                                         setlinestyle(0);
2244
2245                                                         immUnbindProgram();
2246                                                 }
2247                                                 
2248                                                 /* Draw a line to IK root bone 
2249                                                  *  - only if temporary chain (i.e. "autoik")
2250                                                  */
2251                                                 if (arm->flag & ARM_POSEMODE) {
2252                                                         if (constflag & PCHAN_HAS_IK) {
2253                                                                 if (bone->flag & BONE_SELECTED) {
2254                                                                         if (constflag & PCHAN_HAS_TARGET) rgba_float_args_set(fcolor, 200.f/255.f, 120.f/255.f, 0.f/255.f, 1.0f);
2255                                                                         else rgba_float_args_set(fcolor, 200.f/255.f, 200.f/255.f, 50.f/255.f, 1.0f);  /* add theme! */
2256
2257                                                                         GPU_select_load_id(index & 0xFFFF);
2258                                                                         pchan_draw_IK_root_lines(pchan, !(do_dashed & DASH_HELP_LINES));
2259                                                                 }
2260                                                         }
2261                                                         else if (constflag & PCHAN_HAS_SPLINEIK) {
2262                                                                 if (bone->flag & BONE_SELECTED) {
2263                                                                         rgba_float_args_set(fcolor, 150.f/255.f, 200.f/255.f, 50.f/255.f, 1.0f);  /* add theme! */
2264                                                                         
2265                                                                         GPU_select_load_id(index & 0xFFFF);
2266                                                                         pchan_draw_IK_root_lines(pchan, !(do_dashed & DASH_HELP_LINES));
2267                                                                 }
2268                                                         }
2269                                                 }
2270                                         }
2271                                         
2272                                         glPushMatrix();
2273                                         if (arm->drawtype != ARM_ENVELOPE)
2274                                                 glMultMatrixf(pchan->pose_mat);
2275                                         
2276                                         /* catch exception for bone with hidden parent */
2277                                         flag = bone->flag;
2278                                         if ((bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)))
2279                                                 flag &= ~BONE_CONNECTED;
2280                                         
2281                                         /* set temporary flag for drawing bone as active, but only if selected */
2282                                         if (bone == arm->act_bone)
2283                                                 flag |= BONE_DRAW_ACTIVE;
2284                                         
2285                                         /* extra draw service for pose mode */
2286
2287                                         /* set color-set to use */
2288                                         if (do_const_color) {
2289                                                 /* keep color */
2290                                         }
2291                                         else {
2292                                                 set_pchan_colorset(ob, pchan);
2293                                         }
2294                                         
2295                                         if ((pchan->custom) && !(arm->flag & ARM_NO_CUSTOM)) {
2296                                                 /* custom bone shapes should not be drawn here! */
2297                                         }
2298                                         else if (arm->drawtype == ARM_ENVELOPE) {
2299                                                 if (dt < OB_SOLID)
2300                                                         draw_sphere_bone_wire(smat, imat, arm->flag, flag, constflag, index, pchan, NULL);
2301                                         }
2302                                         else if (arm->drawtype == ARM_LINE)
2303                                                 draw_line_bone(arm->flag, flag, constflag, index, pchan, NULL);
2304                                         else if (arm->drawtype == ARM_WIRE)
2305                                                 draw_wire_bone(dt, arm->flag, flag, constflag, index, pchan, NULL);
2306                                         else if (arm->drawtype == ARM_B_BONE)
2307                                                 draw_b_bone(OB_WIRE, arm->flag, flag, constflag, index, pchan, NULL);
2308                                         else
2309                                                 draw_bone(OB_WIRE, arm->flag, flag, constflag, index, bone->length);
2310                                         
2311                                         glPopMatrix();
2312                                 }
2313                         }
2314                         
2315                         /* pose bones count in higher 2 bytes only */
2316                         if (index != -1) 
2317                                 index += 0x10000;
2318                 }
2319                 /* restore things */
2320                 if (!ELEM(arm->drawtype, ARM_WIRE, ARM_LINE) && (dt > OB_WIRE) && (arm->flag & ARM_POSEMODE))
2321                         ED_view3d_polygon_offset(rv3d, 0.0);
2322         }
2323         
2324         /* restore */
2325         if (is_cull_enabled) {
2326                 glDisable(GL_CULL_FACE);
2327         }
2328         
2329         /* draw DoFs */
2330         if (arm->flag & ARM_POSEMODE) {
2331                 if (((base->flag_legacy & OB_FROMDUPLI) == 0) && ((v3d->flag & V3D_HIDE_HELPLINES) == 0)) {
2332                         draw_pose_dofs(ob);
2333                 }
2334         }
2335
2336         /* finally names and axes */
2337         if ((arm->flag & (ARM_DRAWNAMES | ARM_DRAWAXES)) &&
2338             (is_outline == 0) &&
2339             ((base->flag_legacy & OB_FROMDUPLI) == 0))
2340         {
2341                 /* patch for several 3d cards (IBM mostly) that crash on GL_SELECT with text drawing */
2342                 if ((G.f & G_PICKSEL) == 0) {
2343                         float vec[3];
2344
2345                         unsigned char col[4];
2346                         col[0] = ob_wire_col[0];
2347                         col[1] = ob_wire_col[1];
2348                         col[2] = ob_wire_col[2];
2349                         col[3] = 255;
2350                         
2351                         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2352                         
2353                         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
2354                                 if ((pchan->bone->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)) == 0) {
2355                                         if (pchan->bone->layer & arm->layer) {
2356                                                 if (arm->flag & (ARM_EDITMODE | ARM_POSEMODE)) {
2357                                                         bone = pchan->bone;
2358                                                         UI_GetThemeColor3ubv((bone->flag & BONE_SELECTED) ? TH_TEXT_HI : TH_TEXT, col);
2359                                                 }
2360                                                 else if (dt > OB_WIRE) {
2361                                                         UI_GetThemeColor3ubv(TH_TEXT, col);
2362                                                 }
2363                                                 
2364                                                 /*  Draw names of bone  */
2365                                                 if (arm->flag & ARM_DRAWNAMES) {
2366                                                         mid_v3_v3v3(vec, pchan->pose_head, pchan->pose_tail);
2367                                                         view3d_cached_text_draw_add(vec, pchan->name, strlen(pchan->name), 10, 0, col);
2368                                                 }
2369                                                 
2370                                                 /*      Draw additional axes on the bone tail  */
2371                                                 if ((arm->flag & ARM_DRAWAXES) && (arm->flag & ARM_POSEMODE)) {
2372                                                         glPushMatrix();
2373                                                         copy_m4_m4(bmat, pchan->pose_mat);
2374                                                         bone_matrix_translate_y(bmat, pchan->bone->length);
2375                                                         glMultMatrixf(bmat);
2376                                                         
2377                                                         float viewmat_pchan[4][4];
2378                                                         mul_m4_m4m4(viewmat_pchan, rv3d->viewmatob, bmat);
2379                                                         drawaxes(viewmat_pchan, pchan->bone->length * 0.25f, OB_ARROWS, col);
2380                                                         
2381                                                         glPopMatrix();
2382                                                 }
2383                                         }
2384                                 }
2385                         }
2386                         
2387                         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2388                 }
2389         }
2390
2391         if (index != -1) {
2392                 GPU_select_load_id(-1);
2393         }
2394 }
2395
2396 /* in editmode, we don't store the bone matrix... */
2397 static void get_matrix_editbone(EditBone *ebone, float bmat[4][4])
2398 {
2399         ebone->length = len_v3v3(ebone->tail, ebone->head);
2400         ED_armature_ebone_to_mat4(ebone, bmat);
2401 }
2402
2403 static void draw_ebones(View3D *v3d, ARegion *ar, Object *ob, const short dt)
2404 {
2405         RegionView3D *rv3d = ar->regiondata;
2406         EditBone *eBone;
2407         bArmature *arm = ob->data;
2408         float smat[4][4], imat[4][4], bmat[4][4];
2409         unsigned int index;
2410         int flag;
2411         
2412         /* being set in code below */
2413         arm->layer_used = 0;
2414
2415         ED_view3d_check_mats_rv3d(rv3d);
2416
2417         /* envelope (deform distance) */
2418         if (arm->drawtype == ARM_ENVELOPE) {
2419                 /* precalc inverse matrix for drawing screen aligned */
2420                 copy_m4_m4(smat, rv3d->viewmatob);
2421                 mul_mat3_m4_fl(smat, 1.0f / len_v3(ob->obmat[0]));
2422                 invert_m4_m4(imat, smat);
2423                 
2424                 /* and draw blended distances */
2425                 glEnable(GL_BLEND);
2426                 
2427                 if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2428
2429                 for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
2430                         if (eBone->layer & arm->layer) {
2431                                 if ((eBone->flag & (BONE_HIDDEN_A | BONE_NO_DEFORM)) == 0) {
2432                                         if (eBone->flag & (BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL))
2433                                                 draw_sphere_bone_dist(smat, imat, NULL, eBone);
2434                                 }
2435                         }
2436                 }
2437                 
2438                 if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2439                 glDisable(GL_BLEND);
2440         }
2441         
2442         /* if solid we draw it first */
2443         if ((dt > OB_WIRE) && (arm->drawtype != ARM_LINE)) {
2444                 for (eBone = arm->edbo->first, index = 0; eBone; eBone = eBone->next, index++) {
2445                         if (eBone->layer & arm->layer) {
2446                                 if ((eBone->flag & BONE_HIDDEN_A) == 0) {
2447                                         glPushMatrix();
2448                                         get_matrix_editbone(eBone, bmat);
2449                                         glMultMatrixf(bmat);
2450                                         
2451                                         /* catch exception for bone with hidden parent */
2452                                         flag = eBone->flag;
2453                                         if ((eBone->parent) && !EBONE_VISIBLE(arm, eBone->parent)) {
2454                                                 flag &= ~BONE_CONNECTED;
2455                                         }
2456                                                 
2457                                         /* set temporary flag for drawing bone as active, but only if selected */
2458                                         if (eBone == arm->act_edbone)
2459                                                 flag |= BONE_DRAW_ACTIVE;
2460                                         
2461                                         if (arm->drawtype == ARM_ENVELOPE)
2462                                                 draw_sphere_bone(OB_SOLID, arm->flag, flag, 0, index, NULL, eBone);
2463                                         else if (arm->drawtype == ARM_B_BONE)
2464                                                 draw_b_bone(OB_SOLID, arm->flag, flag, 0, index, NULL, eBone);
2465                                         else if (arm->drawtype == ARM_WIRE)
2466                                                 draw_wire_bone(OB_SOLID, arm->flag, flag, 0, index, NULL, eBone);
2467                                         else {
2468                                                 draw_bone(OB_SOLID, arm->flag, flag, 0, index, eBone->length);
2469                                         }
2470                                         
2471                                         glPopMatrix();
2472                                 }
2473                         }
2474                 }
2475         }
2476         
2477         /* if wire over solid, set offset */
2478         index = -1;
2479         GPU_select_load_id(-1);
2480         if (ELEM(arm->drawtype, ARM_LINE, ARM_WIRE)) {
2481                 if (G.f & G_PICKSEL)
2482                         index = 0;
2483         }
2484         else if (dt > OB_WIRE) 
2485                 ED_view3d_polygon_offset(rv3d, 1.0);
2486         else if (arm->flag & ARM_EDITMODE) 
2487                 index = 0;  /* do selection codes */
2488         
2489         for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
2490                 arm->layer_used |= eBone->layer;
2491                 if (eBone->layer & arm->layer) {
2492                         if ((eBone->flag & BONE_HIDDEN_A) == 0) {
2493                                 
2494                                 /* catch exception for bone with hidden parent */
2495                                 flag = eBone->flag;
2496                                 if ((eBone->parent) && !EBONE_VISIBLE(arm, eBone->parent)) {
2497                                         flag &= ~BONE_CONNECTED;
2498                                 }
2499                                         
2500                                 /* set temporary flag for drawing bone as active, but only if selected */
2501                                 if (eBone == arm->act_edbone)
2502                                         flag |= BONE_DRAW_ACTIVE;
2503                                 
2504                                 if (arm->drawtype == ARM_ENVELOPE) {
2505                                         if (dt < OB_SOLID)
2506                                                 draw_sphere_bone_wire(smat, imat, arm->flag, flag, 0, index, NULL, eBone);
2507                                 }
2508                                 else {
2509                                         glPushMatrix();
2510                                         get_matrix_editbone(eBone, bmat);
2511                                         glMultMatrixf(bmat);
2512                                         
2513                                         if (arm->drawtype == ARM_LINE) 
2514                                                 draw_line_bone(arm->flag, flag, 0, index, NULL, eBone);
2515                                         else if (arm->drawtype == ARM_WIRE)
2516                                                 draw_wire_bone(OB_WIRE, arm->flag, flag, 0, index, NULL, eBone);
2517                                         else if (arm->drawtype == ARM_B_BONE)
2518                                                 draw_b_bone(OB_WIRE, arm->flag, flag, 0, index, NULL, eBone);
2519                                         else
2520                                                 draw_bone(OB_WIRE, arm->flag, flag, 0, index, eBone->length);
2521                                         
2522                                         glPopMatrix();
2523                                 }
2524                                 
2525                                 /* offset to parent */
2526                                 if (eBone->parent) {
2527                                         VertexFormat *format = immVertexFormat();
2528                                         unsigned int pos = add_attrib(format, "pos", GL_FLOAT, 3, KEEP_FLOAT);
2529
2530                                         GPU_select_load_id(-1);  /* -1 here is OK! */
2531
2532                                         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
2533                                         UI_GetThemeColor4fv(TH_WIRE_EDIT, fcolor);
2534                                         immUniformColor4fv(fcolor);
2535                                         
2536                                         setlinestyle(3);
2537                                         immBegin(GL_LINES, 2);
2538                                         immVertex3fv(pos, eBone->head);
2539                                         immVertex3fv(pos, eBone->parent->tail);
2540                                         immEnd();
2541                                         setlinestyle(0);
2542
2543                                         immUnbindProgram();
2544                                 }
2545                         }
2546                 }
2547                 if (index != -1) index++;
2548         }
2549         
2550         /* restore */
2551         if (index != -1) {
2552                 GPU_select_load_id(-1);
2553         }
2554
2555         if (ELEM(arm->drawtype, ARM_LINE, ARM_WIRE)) {
2556                 /* pass */
2557         }
2558         else if (dt > OB_WIRE) {
2559                 ED_view3d_polygon_offset(rv3d, 0.0);
2560         }
2561         
2562         /* finally names and axes */
2563         if (arm->flag & (ARM_DRAWNAMES | ARM_DRAWAXES)) {
2564                 /* patch for several 3d cards (IBM mostly) that crash on GL_SELECT with text drawing */
2565                 if ((G.f & G_PICKSEL) == 0) {
2566                         float vec[3];
2567                         unsigned char col[4];
2568                         col[3] = 255;
2569                         
2570                         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2571                         
2572                         for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
2573                                 if (eBone->layer & arm->layer) {
2574                                         if ((eBone->flag & BONE_HIDDEN_A) == 0) {
2575
2576                                                 UI_GetThemeColor3ubv((eBone->flag & BONE_SELECTED) ? TH_TEXT_HI : TH_TEXT, col);
2577
2578                                                 /*      Draw name */
2579                                                 if (arm->flag & ARM_DRAWNAMES) {
2580                                                         mid_v3_v3v3(vec, eBone->head, eBone->tail);
2581                                                         view3d_cached_text_draw_add(vec, eBone->name, strlen(eBone->name), 10, 0, col);
2582                                                 }
2583                                                 /*      Draw additional axes */
2584                                                 if (arm->flag & ARM_DRAWAXES) {
2585                                                         glPushMatrix();
2586                                                         get_matrix_editbone(eBone, bmat);
2587                                                         bone_matrix_translate_y(bmat, eBone->length);
2588                                                         glMultMatrixf(bmat);
2589
2590                                                         float viewmat_ebone[4][4];
2591                                                         mul_m4_m4m4(viewmat_ebone, rv3d->viewmatob, bmat);
2592                                                         drawaxes(viewmat_ebone, eBone->length * 0.25f, OB_ARROWS, col);
2593                                                         
2594                                                         glPopMatrix();
2595                                                 }
2596                                                 
2597                                         }
2598                                 }
2599                         }
2600                         
2601                         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2602                 }
2603         }
2604 }
2605
2606 /* ****************************** Armature Visualization ******************************** */
2607
2608 /* ---------- Paths --------- */
2609
2610 /* draw bone paths
2611  *      - in view space 
2612  */
2613 static void draw_pose_paths(Scene *scene, View3D *v3d, ARegion *ar, Object *ob)
2614 {
2615         bAnimVizSettings *avs = &ob->pose->avs;
2616         bArmature *arm = ob->data;
2617         bPoseChannel *pchan;
2618         
2619         /* setup drawing environment for paths */
2620         draw_motion_paths_init(v3d, ar);
2621         
2622         /* draw paths where they exist and they releated bone is visible */
2623         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
2624                 if ((pchan->bone->layer & arm->layer) && (pchan->mpath))
2625                         draw_motion_path_instance(scene, ob, pchan, avs, pchan->mpath);
2626         }
2627         
2628         /* cleanup after drawing */
2629         draw_motion_paths_cleanup(v3d);
2630 }
2631
2632
2633 /* ---------- Ghosts --------- */
2634
2635 /* helper function for ghost drawing - sets/removes flags for temporarily 
2636  * hiding unselected bones while drawing ghosts
2637  */
2638 static void ghost_poses_tag_unselected(Object *ob, short unset)
2639 {
2640         bArmature *arm = ob->data;
2641         bPose *pose = ob->pose;
2642         bPoseChannel *pchan;
2643         
2644         /* don't do anything if no hiding any bones */
2645         if ((arm->flag & ARM_GHOST_ONLYSEL) == 0)
2646                 return;
2647                 
2648         /* loop over all pchans, adding/removing tags as appropriate */
2649         for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
2650                 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
2651                         if (unset) {
2652                                 /* remove tags from all pchans if cleaning up */
2653                                 pchan->bone->flag &= ~BONE_HIDDEN_PG;
2654                         }
2655                         else {
2656                                 /* set tags on unselected pchans only */
2657                                 if ((pchan->bone->flag & BONE_SELECTED) == 0)
2658                                         pchan->bone->flag |= BONE_HIDDEN_PG;
2659                         }
2660                 }
2661         }
2662 }
2663
2664 /* draw ghosts that occur within a frame range 
2665  *  note: object should be in posemode
2666  */
2667 static void draw_ghost_poses_range(Scene *scene, SceneLayer *sl, View3D *v3d, ARegion *ar, Base *base)
2668 {
2669         Object *ob = base->object;
2670         AnimData *adt = BKE_animdata_from_id(&ob->id);
2671         bArmature *arm = ob->data;
2672         bPose *posen, *poseo;
2673         float start, end, stepsize, range, colfac;
2674         int cfrao, flago;
2675         unsigned char col[4];
2676         
2677         start = (float)arm->ghostsf;
2678         end = (float)arm->ghostef;
2679         if (end <= start)
2680                 return;
2681         
2682         /* prevent infinite loops if this is set to 0 - T49527 */
2683         if (arm->ghostsize < 1)
2684                 arm->ghostsize = 1;
2685         
2686         stepsize = (float)(arm->ghostsize);
2687         range = (float)(end - start);
2688         
2689         /* store values */
2690         ob->mode &= ~OB_MODE_POSE;
2691         cfrao = CFRA;
2692         flago = arm->flag;
2693         arm->flag &= ~(ARM_DRAWNAMES | ARM_DRAWAXES);
2694         
2695         /* copy the pose */
2696         poseo = ob->pose;
2697         BKE_pose_copy_data(&posen, ob->pose, 1);
2698         ob->pose = posen;
2699         BKE_pose_rebuild(ob, ob->data);    /* child pointers for IK */
2700         ghost_poses_tag_unselected(ob, 0);      /* hide unselected bones if need be */
2701         
2702         glEnable(GL_BLEND);
2703         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2704         
2705         /* draw from first frame of range to last */
2706         for (CFRA = (int)start; CFRA <= end; CFRA += (int)stepsize) {
2707                 colfac = (end - (float)CFRA) / range;
2708                 UI_GetThemeColorShadeAlpha4ubv(TH_WIRE, 0, -128 - (int)(120.0f * sqrtf(colfac)), col);
2709                 
2710                 BKE_animsys_evaluate_animdata(scene, &ob->id, adt, (float)CFRA, ADT_RECALC_ALL);
2711                 BKE_pose_where_is(scene, ob);
2712                 draw_pose_bones(scene, sl, v3d, ar, base, OB_WIRE, col, true, false);
2713         }
2714         glDisable(GL_BLEND);
2715         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2716         
2717         /* before disposing of temp pose, use it to restore object to a sane state */
2718         BKE_animsys_evaluate_animdata(scene, &ob->id, adt, (float)cfrao, ADT_RECALC_ALL);
2719         
2720         /* clean up temporary pose */
2721         ghost_poses_tag_unselected(ob, 1);      /* unhide unselected bones if need be */
2722         BKE_pose_free(posen);
2723         
2724         /* restore */
2725         CFRA = cfrao;
2726         ob->pose = poseo;
2727         arm->flag = flago;
2728         ob->mode |= OB_MODE_POSE;
2729 }
2730
2731 /* draw ghosts on keyframes in action within range 
2732  *      - object should be in posemode 
2733  */
2734 static void draw_ghost_poses_keys(Scene *scene, SceneLayer *sl, View3D *v3d, ARegion *ar, BaseLegacy *base)
2735 {
2736         Object *ob = base->object;
2737         AnimData *adt = BKE_animdata_from_id(&ob->id);
2738         bAction *act = (adt) ? adt->action : NULL;
2739         bArmature *arm = ob->data;
2740         bPose *posen, *poseo;
2741         DLRBT_Tree keys;
2742         ActKeyColumn *ak, *akn;
2743         float start, end, range, colfac, i;
2744         int cfrao, flago;
2745         unsigned char col[4];
2746         
2747         start = (float)arm->ghostsf;
2748         end = (float)arm->ghostef;
2749         if (end <= start)
2750                 return;
2751         
2752         /* get keyframes - then clip to only within range */
2753         BLI_dlrbTree_init(&keys);
2754         action_to_keylist(adt, act, &keys, NULL);