doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / blender / editors / transform / transform_manipulator.c
1 /*
2 * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2005 Blender Foundation
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33 #include <float.h>
34
35 #ifndef WIN32
36 #include <unistd.h>
37 #else
38 #include <io.h>
39 #endif
40
41 #include "MEM_guardedalloc.h"
42
43 #include "DNA_armature_types.h"
44 #include "DNA_curve_types.h"
45 #include "DNA_lattice_types.h"
46 #include "DNA_meta_types.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_view3d_types.h"
50
51 #include "RNA_access.h"
52
53 #include "BKE_action.h"
54 #include "BKE_context.h"
55 #include "BKE_global.h"
56 #include "BKE_mesh.h"
57 #include "BKE_particle.h"
58 #include "BKE_pointcache.h"
59
60 #include "BLI_math.h"
61 #include "BLI_editVert.h"
62 #include "BLI_utildefines.h"
63
64 #include "BIF_gl.h"
65
66 #include "WM_api.h"
67 #include "WM_types.h"
68
69 #include "ED_armature.h"
70 #include "ED_mesh.h"
71 #include "ED_particle.h"
72 #include "ED_view3d.h"
73 #include "ED_curve.h" /* for ED_curve_editnurbs */
74
75 #include "UI_resources.h"
76
77 /* local module include */
78 #include "transform.h"
79
80 /* return codes for select, and drawing flags */
81
82 #define MAN_TRANS_X             1
83 #define MAN_TRANS_Y             2
84 #define MAN_TRANS_Z             4
85 #define MAN_TRANS_C             7
86
87 #define MAN_ROT_X               8
88 #define MAN_ROT_Y               16
89 #define MAN_ROT_Z               32
90 #define MAN_ROT_V               64
91 #define MAN_ROT_T               128
92 #define MAN_ROT_C               248
93
94 #define MAN_SCALE_X             256
95 #define MAN_SCALE_Y             512
96 #define MAN_SCALE_Z             1024
97 #define MAN_SCALE_C             1792
98
99 /* color codes */
100
101 #define MAN_RGB         0
102 #define MAN_GHOST       1
103 #define MAN_MOVECOL     2
104
105 /* transform widget center calc helper for below */
106 static void calc_tw_center(Scene *scene, float *co)
107 {
108         float *twcent= scene->twcent;
109         float *min= scene->twmin;
110         float *max= scene->twmax;
111
112         DO_MINMAX(co, min, max);
113         add_v3_v3(twcent, co);
114 }
115
116 static void protectflag_to_drawflags(short protectflag, short *drawflags)
117 {
118         if(protectflag & OB_LOCK_LOCX)
119                 *drawflags &= ~MAN_TRANS_X;
120         if(protectflag & OB_LOCK_LOCY)
121                 *drawflags &= ~MAN_TRANS_Y;
122         if(protectflag & OB_LOCK_LOCZ)
123                 *drawflags &= ~MAN_TRANS_Z;
124
125         if(protectflag & OB_LOCK_ROTX)
126                 *drawflags &= ~MAN_ROT_X;
127         if(protectflag & OB_LOCK_ROTY)
128                 *drawflags &= ~MAN_ROT_Y;
129         if(protectflag & OB_LOCK_ROTZ)
130                 *drawflags &= ~MAN_ROT_Z;
131
132         if(protectflag & OB_LOCK_SCALEX)
133                 *drawflags &= ~MAN_SCALE_X;
134         if(protectflag & OB_LOCK_SCALEY)
135                 *drawflags &= ~MAN_SCALE_Y;
136         if(protectflag & OB_LOCK_SCALEZ)
137                 *drawflags &= ~MAN_SCALE_Z;
138 }
139
140 /* for pose mode */
141 static void stats_pose(Scene *scene, RegionView3D *rv3d, bPoseChannel *pchan)
142 {
143         Bone *bone= pchan->bone;
144
145         if(bone) {
146                 if (bone->flag & BONE_TRANSFORM) {
147                         calc_tw_center(scene, pchan->pose_head);
148                         protectflag_to_drawflags(pchan->protectflag, &rv3d->twdrawflag);
149                 }
150         }
151 }
152
153 /* for editmode*/
154 static void stats_editbone(RegionView3D *rv3d, EditBone *ebo)
155 {
156         if (ebo->flag & BONE_EDITMODE_LOCKED)
157                 protectflag_to_drawflags(OB_LOCK_LOC|OB_LOCK_ROT|OB_LOCK_SCALE, &rv3d->twdrawflag);
158 }
159
160 /* could move into BLI_math however this is only useful for display/editing purposes */
161 static void axis_angle_to_gimbal_axis(float gmat[3][3], float axis[3], float angle)
162 {
163         /* X/Y are arbitrary axies, most importantly Z is the axis of rotation */
164
165         float cross_vec[3];
166         float quat[4];
167
168         /* this is an un-scientific method to get a vector to cross with
169          * XYZ intentionally YZX */
170         cross_vec[0]= axis[1];
171         cross_vec[1]= axis[2];
172         cross_vec[2]= axis[0];
173
174         /* X-axis */
175         cross_v3_v3v3(gmat[0], cross_vec, axis);
176         normalize_v3(gmat[0]);
177         axis_angle_to_quat(quat, axis, angle);
178         mul_qt_v3(quat, gmat[0]);
179
180         /* Y-axis */
181         axis_angle_to_quat(quat, axis, M_PI/2.0);
182         copy_v3_v3(gmat[1], gmat[0]);
183         mul_qt_v3(quat, gmat[1]);
184
185         /* Z-axis */
186         copy_v3_v3(gmat[2], axis);
187
188         normalize_m3(gmat);
189 }
190
191
192 static int test_rotmode_euler(short rotmode)
193 {
194         return (ELEM(rotmode, ROT_MODE_AXISANGLE, ROT_MODE_QUAT)) ? 0:1;
195 }
196
197 int gimbal_axis(Object *ob, float gmat[][3])
198 {
199         if (ob) {
200                 if(ob->mode & OB_MODE_POSE)
201                 {
202                         bPoseChannel *pchan= get_active_posechannel(ob);
203
204                         if(pchan) {
205                                 float mat[3][3], tmat[3][3], obmat[3][3];
206                                 if(test_rotmode_euler(pchan->rotmode)) {
207                                         eulO_to_gimbal_axis(mat, pchan->eul, pchan->rotmode);
208                                 }
209                                 else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
210                                         axis_angle_to_gimbal_axis(mat, pchan->rotAxis, pchan->rotAngle);
211                                 }
212                                 else { /* quat */
213                                         return 0;
214                                 }
215
216
217                                 /* apply bone transformation */
218                                 mul_m3_m3m3(tmat, pchan->bone->bone_mat, mat);
219
220                                 if (pchan->parent)
221                                 {
222                                         float parent_mat[3][3];
223
224                                         copy_m3_m4(parent_mat, pchan->parent->pose_mat);
225                                         mul_m3_m3m3(mat, parent_mat, tmat);
226
227                                         /* needed if object transformation isn't identity */
228                                         copy_m3_m4(obmat, ob->obmat);
229                                         mul_m3_m3m3(gmat, obmat, mat);
230                                 }
231                                 else
232                                 {
233                                         /* needed if object transformation isn't identity */
234                                         copy_m3_m4(obmat, ob->obmat);
235                                         mul_m3_m3m3(gmat, obmat, tmat);
236                                 }
237
238                                 normalize_m3(gmat);
239                                 return 1;
240                         }
241                 }
242                 else {
243                         if(test_rotmode_euler(ob->rotmode)) {
244                                 eulO_to_gimbal_axis(gmat, ob->rot, ob->rotmode);
245                         }
246                         else if(ob->rotmode == ROT_MODE_AXISANGLE) {
247                                 axis_angle_to_gimbal_axis(gmat, ob->rotAxis, ob->rotAngle);
248                         }
249                         else { /* quat */
250                                 return 0;
251                         }
252
253                         if (ob->parent)
254                         {
255                                 float parent_mat[3][3];
256                                 copy_m3_m4(parent_mat, ob->parent->obmat);
257                                 normalize_m3(parent_mat);
258                                 mul_m3_m3m3(gmat, parent_mat, gmat);
259                         }
260                         return 1;
261                 }
262         }
263
264         return 0;
265 }
266
267
268 /* centroid, boundbox, of selection */
269 /* returns total items selected */
270 int calc_manipulator_stats(const bContext *C)
271 {
272         ScrArea *sa= CTX_wm_area(C);
273         ARegion *ar= CTX_wm_region(C);
274         Scene *scene= CTX_data_scene(C);
275         Object *obedit= CTX_data_edit_object(C);
276         View3D *v3d= sa->spacedata.first;
277         RegionView3D *rv3d= ar->regiondata;
278         Base *base;
279         Object *ob= OBACT;
280         int a, totsel= 0;
281
282         /* transform widget matrix */
283         unit_m4(rv3d->twmat);
284
285         rv3d->twdrawflag= 0xFFFF;
286
287         /* transform widget centroid/center */
288         scene->twcent[0]= scene->twcent[1]= scene->twcent[2]= 0.0f;
289         INIT_MINMAX(scene->twmin, scene->twmax);
290
291         if(obedit) {
292                 ob= obedit;
293                 if((ob->lay & v3d->lay)==0) return 0;
294
295                 if(obedit->type==OB_MESH) {
296                         EditMesh *em = BKE_mesh_get_editmesh(obedit->data);
297                         EditVert *eve;
298                         EditSelection ese;
299                         float vec[3]= {0,0,0};
300
301                         /* USE LAST SELECTE WITH ACTIVE */
302                         if (v3d->around==V3D_ACTIVE && EM_get_actSelection(em, &ese)) {
303                                 EM_editselection_center(vec, &ese);
304                                 calc_tw_center(scene, vec);
305                                 totsel= 1;
306                         } else {
307                                 /* do vertices for center, and if still no normal found, use vertex normals */
308                                 for(eve= em->verts.first; eve; eve= eve->next) {
309                                         if(eve->f & SELECT) {
310                                                 totsel++;
311                                                 calc_tw_center(scene, eve->co);
312                                         }
313                                 }
314                         }
315                 } /* end editmesh */
316                 else if (obedit->type==OB_ARMATURE){
317                         bArmature *arm= obedit->data;
318                         EditBone *ebo;
319                         for (ebo= arm->edbo->first; ebo; ebo=ebo->next){
320                                 if(EBONE_VISIBLE(arm, ebo)) {
321                                         if (ebo->flag & BONE_TIPSEL) {
322                                                 calc_tw_center(scene, ebo->tail);
323                                                 totsel++;
324                                         }
325                                         if (ebo->flag & BONE_ROOTSEL) {
326                                                 calc_tw_center(scene, ebo->head);
327                                                 totsel++;
328                                         }
329                                         if (ebo->flag & BONE_SELECTED) {
330                                                 stats_editbone(rv3d, ebo);
331                                         }
332                                 }
333                         }
334                 }
335                 else if ELEM(obedit->type, OB_CURVE, OB_SURF) {
336                         Curve *cu= obedit->data;
337                         Nurb *nu;
338                         BezTriple *bezt;
339                         BPoint *bp;
340                         ListBase *nurbs= ED_curve_editnurbs(cu);
341
342                         nu= nurbs->first;
343                         while(nu) {
344                                 if(nu->type == CU_BEZIER) {
345                                         bezt= nu->bezt;
346                                         a= nu->pntsu;
347                                         while(a--) {
348                                                 /* exceptions
349                                                  * if handles are hidden then only check the center points.
350                                                  * If the center knot is selected then only use this as the center point.
351                                                  */
352                                                 if (cu->drawflag & CU_HIDE_HANDLES) {
353                                                         if (bezt->f2 & SELECT) {
354                                                                 calc_tw_center(scene, bezt->vec[1]);
355                                                                 totsel++;
356                                                         }
357                                                 }
358                                                 else if (bezt->f2 & SELECT) {
359                                                         calc_tw_center(scene, bezt->vec[1]);
360                                                         totsel++;
361                                                 }
362                                                 else {
363                                                         if(bezt->f1) {
364                                                                 calc_tw_center(scene, bezt->vec[0]);
365                                                                 totsel++;
366                                                         }
367                                                         if(bezt->f3) {
368                                                                 calc_tw_center(scene, bezt->vec[2]);
369                                                                 totsel++;
370                                                         }
371                                                 }
372                                                 bezt++;
373                                         }
374                                 }
375                                 else {
376                                         bp= nu->bp;
377                                         a= nu->pntsu*nu->pntsv;
378                                         while(a--) {
379                                                 if(bp->f1 & SELECT) {
380                                                         calc_tw_center(scene, bp->vec);
381                                                         totsel++;
382                                                 }
383                                                 bp++;
384                                         }
385                                 }
386                                 nu= nu->next;
387                         }
388                 }
389                 else if(obedit->type==OB_MBALL) {
390                         MetaBall *mb = (MetaBall*)obedit->data;
391                         MetaElem *ml, *ml_sel=NULL;
392
393                         ml= mb->editelems->first;
394                         while(ml) {
395                                 if(ml->flag & SELECT) {
396                                         calc_tw_center(scene, &ml->x);
397                                         ml_sel = ml;
398                                         totsel++;
399                                 }
400                                 ml= ml->next;
401                         }
402                 }
403                 else if(obedit->type==OB_LATTICE) {
404                         BPoint *bp;
405                         Lattice *lt= obedit->data;
406
407                         bp= lt->editlatt->latt->def;
408
409                         a= lt->editlatt->latt->pntsu*lt->editlatt->latt->pntsv*lt->editlatt->latt->pntsw;
410                         while(a--) {
411                                 if(bp->f1 & SELECT) {
412                                         calc_tw_center(scene, bp->vec);
413                                         totsel++;
414                                 }
415                                 bp++;
416                         }
417                 }
418
419                 /* selection center */
420                 if(totsel) {
421                         mul_v3_fl(scene->twcent, 1.0f/(float)totsel);   // centroid!
422                         mul_m4_v3(obedit->obmat, scene->twcent);
423                         mul_m4_v3(obedit->obmat, scene->twmin);
424                         mul_m4_v3(obedit->obmat, scene->twmax);
425                 }
426         }
427         else if(ob && (ob->mode & OB_MODE_POSE)) {
428                 bPoseChannel *pchan;
429                 int mode = TFM_ROTATION; // mislead counting bones... bah. We don't know the manipulator mode, could be mixed
430
431                 if((ob->lay & v3d->lay)==0) return 0;
432
433                 totsel = count_set_pose_transflags(&mode, 0, ob);
434
435                 if(totsel) {
436                         /* use channels to get stats */
437                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
438                                 stats_pose(scene, rv3d, pchan);
439                         }
440
441                         mul_v3_fl(scene->twcent, 1.0f/(float)totsel);   // centroid!
442                         mul_m4_v3(ob->obmat, scene->twcent);
443                         mul_m4_v3(ob->obmat, scene->twmin);
444                         mul_m4_v3(ob->obmat, scene->twmax);
445                 }
446         }
447         else if(ob && (ob->mode & OB_MODE_ALL_PAINT)) {
448                 ;
449         }
450         else if(ob && ob->mode & OB_MODE_PARTICLE_EDIT) {
451                 PTCacheEdit *edit= PE_get_current(scene, ob);
452                 PTCacheEditPoint *point;
453                 PTCacheEditKey *ek;
454                 int k;
455
456                 if(edit) {
457                         point = edit->points;
458                         for(a=0; a<edit->totpoint; a++,point++) {
459                                 if(point->flag & PEP_HIDE) continue;
460
461                                 for(k=0, ek=point->keys; k<point->totkey; k++, ek++) {
462                                         if(ek->flag & PEK_SELECT) {
463                                                 calc_tw_center(scene, ek->flag & PEK_USE_WCO ? ek->world_co : ek->co);
464                                                 totsel++;
465                                         }
466                                 }
467                         }
468
469                         /* selection center */
470                         if(totsel)
471                                 mul_v3_fl(scene->twcent, 1.0f/(float)totsel);   // centroid!
472                 }
473         }
474         else {
475
476                 /* we need the one selected object, if its not active */
477                 ob= OBACT;
478                 if(ob && !(ob->flag & SELECT)) ob= NULL;
479
480                 for(base= scene->base.first; base; base= base->next) {
481                         if TESTBASELIB(v3d, base) {
482                                 if(ob==NULL)
483                                         ob= base->object;
484                                 calc_tw_center(scene, base->object->obmat[3]);
485                                 protectflag_to_drawflags(base->object->protectflag, &rv3d->twdrawflag);
486                                 totsel++;
487                         }
488                 }
489
490                 /* selection center */
491                 if(totsel) {
492                         mul_v3_fl(scene->twcent, 1.0f/(float)totsel);   // centroid!
493                 }
494         }
495
496         /* global, local or normal orientation? */
497         if(ob && totsel) {
498
499                 switch(v3d->twmode) {
500                 
501                 case V3D_MANIP_GLOBAL:
502                         break; /* nothing to do */
503
504                 case V3D_MANIP_GIMBAL:
505                 {
506                         float mat[3][3];
507                         if (gimbal_axis(ob, mat)) {
508                                 copy_m4_m3(rv3d->twmat, mat);
509                                 break;
510                         }
511                         /* if not gimbal, fall through to normal */
512                 }
513                 case V3D_MANIP_NORMAL:
514                         if(obedit || ob->mode & OB_MODE_POSE) {
515                                 float mat[3][3];
516                                 ED_getTransformOrientationMatrix(C, mat, (v3d->around == V3D_ACTIVE));
517                                 copy_m4_m3(rv3d->twmat, mat);
518                                 break;
519                         }
520                         /* no break we define 'normal' as 'local' in Object mode */
521                 case V3D_MANIP_LOCAL:
522                         copy_m4_m4(rv3d->twmat, ob->obmat);
523                         normalize_m4(rv3d->twmat);
524                         break;
525
526                 case V3D_MANIP_VIEW:
527                         {
528                                 float mat[3][3];
529                                 copy_m3_m4(mat, rv3d->viewinv);
530                                 normalize_m3(mat);
531                                 copy_m4_m3(rv3d->twmat, mat);
532                         }
533                         break;
534                 default: /* V3D_MANIP_CUSTOM */
535                         {
536                                 float mat[3][3];
537                                 applyTransformOrientation(C, mat, NULL);
538                                 copy_m4_m3(rv3d->twmat, mat);
539                                 break;
540                         }
541                 }
542
543         }
544
545         return totsel;
546 }
547
548 /* don't draw axis perpendicular to the view */
549 static void test_manipulator_axis(const bContext *C)
550 {
551         RegionView3D *rv3d= CTX_wm_region_view3d(C);
552         float angle;
553         float vec[3];
554
555         viewvector(rv3d, rv3d->twmat[3], vec);
556
557         angle = fabs(angle_v3v3(rv3d->twmat[0], vec));
558         if (angle > M_PI / 2) {
559                 angle = M_PI - angle;
560         }
561         angle = rv3d->twangle[0] = 180.0f * angle / M_PI;
562         if (angle < 5) {
563                 rv3d->twdrawflag &= ~(MAN_TRANS_X|MAN_SCALE_X);
564         }
565
566         angle = fabs(angle_v3v3(rv3d->twmat[1], vec));
567         if (angle > M_PI / 2) {
568                 angle = M_PI - angle;
569         }
570         angle = rv3d->twangle[1] = 180.0f * angle / M_PI;
571         if (angle < 5) {
572                 rv3d->twdrawflag &= ~(MAN_TRANS_Y|MAN_SCALE_Y);
573         }
574
575         angle = fabs(angle_v3v3(rv3d->twmat[2], vec));
576         if (angle > M_PI / 2) {
577                 angle = M_PI - angle;
578         }
579         angle = rv3d->twangle[2] = 180.0f * angle / M_PI;
580         if (angle < 5) {
581                 rv3d->twdrawflag &= ~(MAN_TRANS_Z|MAN_SCALE_Z);
582         }
583 }
584
585
586 /* ******************** DRAWING STUFFIES *********** */
587
588 static float screen_aligned(RegionView3D *rv3d, float mat[][4])
589 {
590         glTranslatef(mat[3][0], mat[3][1], mat[3][2]);
591
592         /* sets view screen aligned */
593         glRotatef( -360.0f*saacos(rv3d->viewquat[0])/(float)M_PI, rv3d->viewquat[1], rv3d->viewquat[2], rv3d->viewquat[3]);
594
595         return len_v3(mat[0]); /* draw scale */
596 }
597
598
599 /* radring = radius of donut rings
600    radhole = radius hole
601    start = starting segment (based on nrings)
602    end   = end segment
603    nsides = amount of points in ring
604    nrigns = amount of rings
605 */
606 static void partial_donut(float radring, float radhole, int start, int end, int nsides, int nrings)
607 {
608         float theta, phi, theta1;
609         float cos_theta, sin_theta;
610         float cos_theta1, sin_theta1;
611         float ring_delta, side_delta;
612         int i, j, docaps= 1;
613
614         if(start==0 && end==nrings) docaps= 0;
615
616         ring_delta= 2.0f*(float)M_PI/(float)nrings;
617         side_delta= 2.0f*(float)M_PI/(float)nsides;
618
619         theta= (float)M_PI+0.5f*ring_delta;
620         cos_theta= (float)cos(theta);
621         sin_theta= (float)sin(theta);
622
623         for(i= nrings - 1; i >= 0; i--) {
624                 theta1= theta + ring_delta;
625                 cos_theta1= (float)cos(theta1);
626                 sin_theta1= (float)sin(theta1);
627
628                 if(docaps && i==start) {        // cap
629                         glBegin(GL_POLYGON);
630                         phi= 0.0;
631                         for(j= nsides; j >= 0; j--) {
632                                 float cos_phi, sin_phi, dist;
633
634                                 phi += side_delta;
635                                 cos_phi= (float)cos(phi);
636                                 sin_phi= (float)sin(phi);
637                                 dist= radhole + radring * cos_phi;
638
639                                 glVertex3f(cos_theta1 * dist, -sin_theta1 * dist,  radring * sin_phi);
640                         }
641                         glEnd();
642                 }
643                 if(i>=start && i<=end) {
644                         glBegin(GL_QUAD_STRIP);
645                         phi= 0.0;
646                         for(j= nsides; j >= 0; j--) {
647                                 float cos_phi, sin_phi, dist;
648
649                                 phi += side_delta;
650                                 cos_phi= (float)cos(phi);
651                                 sin_phi= (float)sin(phi);
652                                 dist= radhole + radring * cos_phi;
653
654                                 glVertex3f(cos_theta1 * dist, -sin_theta1 * dist, radring * sin_phi);
655                                 glVertex3f(cos_theta * dist, -sin_theta * dist,  radring * sin_phi);
656                         }
657                         glEnd();
658                 }
659
660                 if(docaps && i==end) {  // cap
661                         glBegin(GL_POLYGON);
662                         phi= 0.0;
663                         for(j= nsides; j >= 0; j--) {
664                                 float cos_phi, sin_phi, dist;
665
666                                 phi -= side_delta;
667                                 cos_phi= (float)cos(phi);
668                                 sin_phi= (float)sin(phi);
669                                 dist= radhole + radring * cos_phi;
670
671                                 glVertex3f(cos_theta * dist, -sin_theta * dist,  radring * sin_phi);
672                         }
673                         glEnd();
674                 }
675
676
677                 theta= theta1;
678                 cos_theta= cos_theta1;
679                 sin_theta= sin_theta1;
680         }
681 }
682
683 static char axisBlendAngle(float angle)
684 {
685         if (angle > 20)
686                 return 255;
687
688         if (angle < 5)
689                 return 0;
690
691         return (char)(255.0f * (angle - 5) / 15.0f);
692 }
693
694 /* three colors can be set;
695    grey for ghosting
696    moving: in transform theme color
697    else the red/green/blue
698 */
699 static void manipulator_setcolor(View3D *v3d, char axis, int colcode, unsigned char alpha)
700 {
701         unsigned char col[4]= {0};
702         col[3]= alpha;
703
704         if(colcode==MAN_GHOST) {
705                 col[3]= 70;
706         }
707         else if(colcode==MAN_MOVECOL) {
708                 UI_GetThemeColor3ubv(TH_TRANSFORM, col);
709         }
710         else {
711                 switch(axis) {
712                 case 'C':
713                         UI_GetThemeColor3ubv(TH_TRANSFORM, col);
714                         if(v3d->twmode == V3D_MANIP_LOCAL) {
715                                 col[0]= col[0]>200?255:col[0]+55;
716                                 col[1]= col[1]>200?255:col[1]+55;
717                                 col[2]= col[2]>200?255:col[2]+55;
718                         }
719                         else if(v3d->twmode == V3D_MANIP_NORMAL) {
720                                 col[0]= col[0]<55?0:col[0]-55;
721                                 col[1]= col[1]<55?0:col[1]-55;
722                                 col[2]= col[2]<55?0:col[2]-55;
723                         }
724                         break;
725                 case 'X':
726                         col[0]= 220;
727                         break;
728                 case 'Y':
729                         col[1]= 220;
730                         break;
731                 case 'Z':
732                         col[0]= 30;
733                         col[1]= 30;
734                         col[2]= 220;
735                         break;
736                 default:
737                         BLI_assert(!"invalid axis arg");
738                 }
739         }
740
741         glColor4ubv(col);
742 }
743
744 /* viewmatrix should have been set OK, also no shademode! */
745 static void draw_manipulator_axes(View3D *v3d, RegionView3D *rv3d, int colcode, int flagx, int flagy, int flagz)
746 {
747
748         /* axes */
749         if(flagx) {
750                 manipulator_setcolor(v3d, 'X', colcode, axisBlendAngle(rv3d->twangle[0]));
751                 if(flagx & MAN_SCALE_X) glLoadName(MAN_SCALE_X);
752                 else if(flagx & MAN_TRANS_X) glLoadName(MAN_TRANS_X);
753                 glBegin(GL_LINES);
754                 glVertex3f(0.2f, 0.0f, 0.0f);
755                 glVertex3f(1.0f, 0.0f, 0.0f);
756                 glEnd();
757         }
758         if(flagy) {
759                 if(flagy & MAN_SCALE_Y) glLoadName(MAN_SCALE_Y);
760                 else if(flagy & MAN_TRANS_Y) glLoadName(MAN_TRANS_Y);
761                 manipulator_setcolor(v3d, 'Y', colcode, axisBlendAngle(rv3d->twangle[1]));
762                 glBegin(GL_LINES);
763                 glVertex3f(0.0f, 0.2f, 0.0f);
764                 glVertex3f(0.0f, 1.0f, 0.0f);
765                 glEnd();
766         }
767         if(flagz) {
768                 if(flagz & MAN_SCALE_Z) glLoadName(MAN_SCALE_Z);
769                 else if(flagz & MAN_TRANS_Z) glLoadName(MAN_TRANS_Z);
770                 manipulator_setcolor(v3d, 'Z', colcode, axisBlendAngle(rv3d->twangle[2]));
771                 glBegin(GL_LINES);
772                 glVertex3f(0.0f, 0.0f, 0.2f);
773                 glVertex3f(0.0f, 0.0f, 1.0f);
774                 glEnd();
775         }
776 }
777
778 static void preOrthoFront(int ortho, float twmat[][4], int axis)
779 {
780         if (ortho == 0) {
781                 float omat[4][4];
782                 copy_m4_m4(omat, twmat);
783                 orthogonalize_m4(omat, axis);
784                 glPushMatrix();
785                 glMultMatrixf(omat);
786                 glFrontFace(is_negative_m4(omat) ? GL_CW:GL_CCW);
787         }
788 }
789
790 static void postOrtho(int ortho)
791 {
792         if (ortho == 0) {
793                 glPopMatrix();
794         }
795 }
796
797 static void draw_manipulator_rotate(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo)
798 {
799         GLUquadricObj *qobj;
800         double plane[4];
801         float matt[4][4];
802         float size, unitmat[4][4];
803         float cywid= 0.33f*0.01f*(float)U.tw_handlesize;
804         float cusize= cywid*0.65f;
805         int arcs= (G.rt!=2);
806         int colcode;
807         int ortho;
808
809         if(moving) colcode= MAN_MOVECOL;
810         else colcode= MAN_RGB;
811
812         /* when called while moving in mixed mode, do not draw when... */
813         if((drawflags & MAN_ROT_C)==0) return;
814
815         /* Init stuff */
816         glDisable(GL_DEPTH_TEST);
817         unit_m4(unitmat);
818
819         qobj= gluNewQuadric();
820         gluQuadricDrawStyle(qobj, GLU_FILL);
821
822         /* prepare for screen aligned draw */
823         size= len_v3(rv3d->twmat[0]);
824         glPushMatrix();
825         glTranslatef(rv3d->twmat[3][0], rv3d->twmat[3][1], rv3d->twmat[3][2]);
826
827         if(arcs) {
828                 /* clipplane makes nice handles, calc here because of multmatrix but with translate! */
829                 VECCOPY(plane, rv3d->viewinv[2]);
830                 plane[3]= -0.02*size; // clip just a bit more
831                 glClipPlane(GL_CLIP_PLANE0, plane);
832         }
833         /* sets view screen aligned */
834         glRotatef( -360.0f*saacos(rv3d->viewquat[0])/(float)M_PI, rv3d->viewquat[1], rv3d->viewquat[2], rv3d->viewquat[3]);
835
836         /* Screen aligned help circle */
837         if(arcs) {
838                 if((G.f & G_PICKSEL)==0) {
839                         UI_ThemeColorShade(TH_BACK, -30);
840                         drawcircball(GL_LINE_LOOP, unitmat[3], size, unitmat);
841                 }
842         }
843
844         /* Screen aligned trackball rot circle */
845         if(drawflags & MAN_ROT_T) {
846                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_T);
847
848                 UI_ThemeColor(TH_TRANSFORM);
849                 drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
850         }
851
852         /* Screen aligned view rot circle */
853         if(drawflags & MAN_ROT_V) {
854                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_V);
855                 UI_ThemeColor(TH_TRANSFORM);
856                 drawcircball(GL_LINE_LOOP, unitmat[3], 1.2f*size, unitmat);
857
858                 if(moving) {
859                         float vec[3];
860                         vec[0]= 0; // XXX (float)(t->imval[0] - t->center2d[0]);
861                         vec[1]= 0; // XXX (float)(t->imval[1] - t->center2d[1]);
862                         vec[2]= 0.0f;
863                         normalize_v3(vec);
864                         mul_v3_fl(vec, 1.2f*size);
865                         glBegin(GL_LINES);
866                         glVertex3f(0.0f, 0.0f, 0.0f);
867                         glVertex3fv(vec);
868                         glEnd();
869                 }
870         }
871         glPopMatrix();
872
873
874         ortho = is_orthogonal_m4(rv3d->twmat);
875         
876         /* apply the transform delta */
877         if(moving) {
878                 copy_m4_m4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
879                 // XXX mul_m4_m3m4(matt, t->mat, rv3d->twmat);
880                 if (ortho) {
881                         glMultMatrixf(matt);
882                         glFrontFace(is_negative_m4(matt) ? GL_CW:GL_CCW);
883                 }
884         }
885         else {
886                 if (ortho) {
887                         glFrontFace(is_negative_m4(rv3d->twmat) ? GL_CW:GL_CCW);
888                         glMultMatrixf(rv3d->twmat);
889                 }
890         }
891
892         /* axes */
893         if(arcs==0) {
894                 if(!(G.f & G_PICKSEL)) {
895                         if( (combo & V3D_MANIP_SCALE)==0) {
896                                 /* axis */
897                                 if( (drawflags & MAN_ROT_X) || (moving && (drawflags & MAN_ROT_Z)) ) {
898                                         preOrthoFront(ortho, rv3d->twmat, 2);
899                                         manipulator_setcolor(v3d, 'X', colcode, 255);
900                                         glBegin(GL_LINES);
901                                         glVertex3f(0.2f, 0.0f, 0.0f);
902                                         glVertex3f(1.0f, 0.0f, 0.0f);
903                                         glEnd();
904                                         postOrtho(ortho);
905                                 }
906                                 if( (drawflags & MAN_ROT_Y) || (moving && (drawflags & MAN_ROT_X)) ) {
907                                         preOrthoFront(ortho, rv3d->twmat, 0);
908                                         manipulator_setcolor(v3d, 'Y', colcode, 255);
909                                         glBegin(GL_LINES);
910                                         glVertex3f(0.0f, 0.2f, 0.0f);
911                                         glVertex3f(0.0f, 1.0f, 0.0f);
912                                         glEnd();
913                                         postOrtho(ortho);
914                                 }
915                                 if( (drawflags & MAN_ROT_Z) || (moving && (drawflags & MAN_ROT_Y)) ) {
916                                         preOrthoFront(ortho, rv3d->twmat, 1);
917                                         manipulator_setcolor(v3d, 'Z', colcode, 255);
918                                         glBegin(GL_LINES);
919                                         glVertex3f(0.0f, 0.0f, 0.2f);
920                                         glVertex3f(0.0f, 0.0f, 1.0f);
921                                         glEnd();
922                                         postOrtho(ortho);
923                                 }
924                         }
925                 }
926         }
927
928         if(arcs==0 && moving) {
929
930                 /* Z circle */
931                 if(drawflags & MAN_ROT_Z) {
932                         preOrthoFront(ortho, matt, 2);
933                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
934                         manipulator_setcolor(v3d, 'Z', colcode, 255);
935                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
936                         postOrtho(ortho);
937                 }
938                 /* X circle */
939                 if(drawflags & MAN_ROT_X) {
940                         preOrthoFront(ortho, matt, 0);
941                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
942                         glRotatef(90.0, 0.0, 1.0, 0.0);
943                         manipulator_setcolor(v3d, 'X', colcode, 255);
944                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
945                         glRotatef(-90.0, 0.0, 1.0, 0.0);
946                         postOrtho(ortho);
947                 }
948                 /* Y circle */
949                 if(drawflags & MAN_ROT_Y) {
950                         preOrthoFront(ortho, matt, 1);
951                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
952                         glRotatef(-90.0, 1.0, 0.0, 0.0);
953                         manipulator_setcolor(v3d, 'Y', colcode, 255);
954                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
955                         glRotatef(90.0, 1.0, 0.0, 0.0);
956                         postOrtho(ortho);
957                 }
958
959                 if(arcs) glDisable(GL_CLIP_PLANE0);
960         }
961         // donut arcs
962         if(arcs) {
963                 glEnable(GL_CLIP_PLANE0);
964
965                 /* Z circle */
966                 if(drawflags & MAN_ROT_Z) {
967                         preOrthoFront(ortho, rv3d->twmat, 2);
968                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
969                         manipulator_setcolor(v3d, 'Z', colcode, 255);
970                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
971                         postOrtho(ortho);
972                 }
973                 /* X circle */
974                 if(drawflags & MAN_ROT_X) {
975                         preOrthoFront(ortho, rv3d->twmat, 0);
976                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
977                         glRotatef(90.0, 0.0, 1.0, 0.0);
978                         manipulator_setcolor(v3d, 'X', colcode, 255);
979                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
980                         glRotatef(-90.0, 0.0, 1.0, 0.0);
981                         postOrtho(ortho);
982                 }
983                 /* Y circle */
984                 if(drawflags & MAN_ROT_Y) {
985                         preOrthoFront(ortho, rv3d->twmat, 1);
986                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
987                         glRotatef(-90.0, 1.0, 0.0, 0.0);
988                         manipulator_setcolor(v3d, 'Y', colcode, 255);
989                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
990                         glRotatef(90.0, 1.0, 0.0, 0.0);
991                         postOrtho(ortho);
992                 }
993
994                 glDisable(GL_CLIP_PLANE0);
995         }
996
997         if(arcs==0) {
998
999                 /* Z handle on X axis */
1000                 if(drawflags & MAN_ROT_Z) {
1001                         preOrthoFront(ortho, rv3d->twmat, 2);
1002                         glPushMatrix();
1003                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1004                         manipulator_setcolor(v3d, 'Z', colcode, 255);
1005
1006                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1007
1008                         glPopMatrix();
1009                         postOrtho(ortho);
1010                 }
1011
1012                 /* Y handle on X axis */
1013                 if(drawflags & MAN_ROT_Y) {
1014                         preOrthoFront(ortho, rv3d->twmat, 1);
1015                         glPushMatrix();
1016                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1017                         manipulator_setcolor(v3d, 'Y', colcode, 255);
1018
1019                         glRotatef(90.0, 1.0, 0.0, 0.0);
1020                         glRotatef(90.0, 0.0, 0.0, 1.0);
1021                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1022
1023                         glPopMatrix();
1024                         postOrtho(ortho);
1025                 }
1026
1027                 /* X handle on Z axis */
1028                 if(drawflags & MAN_ROT_X) {
1029                         preOrthoFront(ortho, rv3d->twmat, 0);
1030                         glPushMatrix();
1031                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1032                         manipulator_setcolor(v3d, 'X', colcode, 255);
1033
1034                         glRotatef(-90.0, 0.0, 1.0, 0.0);
1035                         glRotatef(90.0, 0.0, 0.0, 1.0);
1036                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1037
1038                         glPopMatrix();
1039                         postOrtho(ortho);
1040                 }
1041
1042         }
1043
1044         /* restore */
1045         glLoadMatrixf(rv3d->viewmat);
1046         gluDeleteQuadric(qobj);
1047         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1048
1049 }
1050
1051 static void drawsolidcube(float size)
1052 {
1053         static float cube[8][3] = {
1054         {-1.0, -1.0, -1.0},
1055         {-1.0, -1.0,  1.0},
1056         {-1.0,  1.0,  1.0},
1057         {-1.0,  1.0, -1.0},
1058         { 1.0, -1.0, -1.0},
1059         { 1.0, -1.0,  1.0},
1060         { 1.0,  1.0,  1.0},
1061         { 1.0,  1.0, -1.0},     };
1062         float n[3];
1063
1064         glPushMatrix();
1065         glScalef(size, size, size);
1066
1067         n[0]=0; n[1]=0; n[2]=0;
1068         glBegin(GL_QUADS);
1069         n[0]= -1.0;
1070         glNormal3fv(n);
1071         glVertex3fv(cube[0]); glVertex3fv(cube[1]); glVertex3fv(cube[2]); glVertex3fv(cube[3]);
1072         n[0]=0;
1073         glEnd();
1074
1075         glBegin(GL_QUADS);
1076         n[1]= -1.0;
1077         glNormal3fv(n);
1078         glVertex3fv(cube[0]); glVertex3fv(cube[4]); glVertex3fv(cube[5]); glVertex3fv(cube[1]);
1079         n[1]=0;
1080         glEnd();
1081
1082         glBegin(GL_QUADS);
1083         n[0]= 1.0;
1084         glNormal3fv(n);
1085         glVertex3fv(cube[4]); glVertex3fv(cube[7]); glVertex3fv(cube[6]); glVertex3fv(cube[5]);
1086         n[0]=0;
1087         glEnd();
1088
1089         glBegin(GL_QUADS);
1090         n[1]= 1.0;
1091         glNormal3fv(n);
1092         glVertex3fv(cube[7]); glVertex3fv(cube[3]); glVertex3fv(cube[2]); glVertex3fv(cube[6]);
1093         n[1]=0;
1094         glEnd();
1095
1096         glBegin(GL_QUADS);
1097         n[2]= 1.0;
1098         glNormal3fv(n);
1099         glVertex3fv(cube[1]); glVertex3fv(cube[5]); glVertex3fv(cube[6]); glVertex3fv(cube[2]);
1100         n[2]=0;
1101         glEnd();
1102
1103         glBegin(GL_QUADS);
1104         n[2]= -1.0;
1105         glNormal3fv(n);
1106         glVertex3fv(cube[7]); glVertex3fv(cube[4]); glVertex3fv(cube[0]); glVertex3fv(cube[3]);
1107         glEnd();
1108
1109         glPopMatrix();
1110 }
1111
1112
1113 static void draw_manipulator_scale(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1114 {
1115         float cywid= 0.25f*0.01f*(float)U.tw_handlesize;
1116         float cusize= cywid*0.75f, dz;
1117
1118         /* when called while moving in mixed mode, do not draw when... */
1119         if((drawflags & MAN_SCALE_C)==0) return;
1120
1121         glDisable(GL_DEPTH_TEST);
1122
1123         /* not in combo mode */
1124         if( (combo & (V3D_MANIP_TRANSLATE|V3D_MANIP_ROTATE))==0) {
1125                 float size, unitmat[4][4];
1126                 int shift= 0; // XXX
1127
1128                 /* center circle, do not add to selection when shift is pressed (planar constraint)  */
1129                 if( (G.f & G_PICKSEL) && shift==0) glLoadName(MAN_SCALE_C);
1130
1131                 manipulator_setcolor(v3d, 'C', colcode, 255);
1132                 glPushMatrix();
1133                 size= screen_aligned(rv3d, rv3d->twmat);
1134                 unit_m4(unitmat);
1135                 drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
1136                 glPopMatrix();
1137
1138                 dz= 1.0;
1139         }
1140         else dz= 1.0f-4.0f*cusize;
1141
1142         if(moving) {
1143                 float matt[4][4];
1144
1145                 copy_m4_m4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
1146                 // XXX mul_m4_m3m4(matt, t->mat, rv3d->twmat);
1147                 glMultMatrixf(matt);
1148                 glFrontFace(is_negative_m4(matt) ? GL_CW:GL_CCW);
1149         }
1150         else {
1151                 glMultMatrixf(rv3d->twmat);
1152                 glFrontFace(is_negative_m4(rv3d->twmat) ? GL_CW:GL_CCW);
1153         }
1154
1155         /* axis */
1156
1157         /* in combo mode, this is always drawn as first type */
1158         draw_manipulator_axes(v3d, rv3d, colcode, drawflags & MAN_SCALE_X, drawflags & MAN_SCALE_Y, drawflags & MAN_SCALE_Z);
1159
1160         /* Z cube */
1161         glTranslatef(0.0, 0.0, dz);
1162         if(drawflags & MAN_SCALE_Z) {
1163                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_Z);
1164                 manipulator_setcolor(v3d, 'Z', colcode, axisBlendAngle(rv3d->twangle[2]));
1165                 drawsolidcube(cusize);
1166         }
1167         /* X cube */
1168         glTranslatef(dz, 0.0, -dz);
1169         if(drawflags & MAN_SCALE_X) {
1170                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_X);
1171                 manipulator_setcolor(v3d, 'X', colcode, axisBlendAngle(rv3d->twangle[0]));
1172                 drawsolidcube(cusize);
1173         }
1174         /* Y cube */
1175         glTranslatef(-dz, dz, 0.0);
1176         if(drawflags & MAN_SCALE_Y) {
1177                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_Y);
1178                 manipulator_setcolor(v3d, 'Y', colcode, axisBlendAngle(rv3d->twangle[1]));
1179                 drawsolidcube(cusize);
1180         }
1181
1182         /* if shiftkey, center point as last, for selectbuffer order */
1183         if(G.f & G_PICKSEL) {
1184                 int shift= 0; // XXX
1185
1186                 if(shift) {
1187                         glTranslatef(0.0, -dz, 0.0);
1188                         glLoadName(MAN_SCALE_C);
1189                         glBegin(GL_POINTS);
1190                         glVertex3f(0.0, 0.0, 0.0);
1191                         glEnd();
1192                 }
1193         }
1194
1195         /* restore */
1196         glLoadMatrixf(rv3d->viewmat);
1197
1198         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1199         glFrontFace(GL_CCW);
1200 }
1201
1202
1203 static void draw_cone(GLUquadricObj *qobj, float len, float width)
1204 {
1205         glTranslatef(0.0, 0.0, -0.5f*len);
1206         gluCylinder(qobj, width, 0.0, len, 8, 1);
1207         gluQuadricOrientation(qobj, GLU_INSIDE);
1208         gluDisk(qobj, 0.0, width, 8, 1);
1209         gluQuadricOrientation(qobj, GLU_OUTSIDE);
1210         glTranslatef(0.0, 0.0, 0.5f*len);
1211 }
1212
1213 static void draw_cylinder(GLUquadricObj *qobj, float len, float width)
1214 {
1215
1216         width*= 0.8f;   // just for beauty
1217
1218         glTranslatef(0.0, 0.0, -0.5f*len);
1219         gluCylinder(qobj, width, width, len, 8, 1);
1220         gluQuadricOrientation(qobj, GLU_INSIDE);
1221         gluDisk(qobj, 0.0, width, 8, 1);
1222         gluQuadricOrientation(qobj, GLU_OUTSIDE);
1223         glTranslatef(0.0, 0.0, len);
1224         gluDisk(qobj, 0.0, width, 8, 1);
1225         glTranslatef(0.0, 0.0, -0.5f*len);
1226 }
1227
1228
1229 static void draw_manipulator_translate(View3D *v3d, RegionView3D *rv3d, int UNUSED(moving), int drawflags, int combo, int colcode)
1230 {
1231         GLUquadricObj *qobj;
1232         float cylen= 0.01f*(float)U.tw_handlesize;
1233         float cywid= 0.25f*cylen, dz, size;
1234         float unitmat[4][4];
1235         int shift= 0; // XXX
1236
1237         /* when called while moving in mixed mode, do not draw when... */
1238         if((drawflags & MAN_TRANS_C)==0) return;
1239
1240         // XXX if(moving) glTranslatef(t->vec[0], t->vec[1], t->vec[2]);
1241         glDisable(GL_DEPTH_TEST);
1242
1243         qobj= gluNewQuadric();
1244         gluQuadricDrawStyle(qobj, GLU_FILL);
1245
1246         /* center circle, do not add to selection when shift is pressed (planar constraint) */
1247         if( (G.f & G_PICKSEL) && shift==0) glLoadName(MAN_TRANS_C);
1248
1249         manipulator_setcolor(v3d, 'C', colcode, 255);
1250         glPushMatrix();
1251         size= screen_aligned(rv3d, rv3d->twmat);
1252         unit_m4(unitmat);
1253         drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
1254         glPopMatrix();
1255
1256         /* and now apply matrix, we move to local matrix drawing */
1257         glMultMatrixf(rv3d->twmat);
1258
1259         /* axis */
1260         glLoadName(-1);
1261
1262         // translate drawn as last, only axis when no combo with scale, or for ghosting
1263         if((combo & V3D_MANIP_SCALE)==0 || colcode==MAN_GHOST)
1264                 draw_manipulator_axes(v3d, rv3d, colcode, drawflags & MAN_TRANS_X, drawflags & MAN_TRANS_Y, drawflags & MAN_TRANS_Z);
1265
1266
1267         /* offset in combo mode, for rotate a bit more */
1268         if(combo & (V3D_MANIP_ROTATE)) dz= 1.0f+2.0f*cylen;
1269         else if(combo & (V3D_MANIP_SCALE)) dz= 1.0f+0.5f*cylen;
1270         else dz= 1.0f;
1271
1272         /* Z Cone */
1273         glTranslatef(0.0, 0.0, dz);
1274         if(drawflags & MAN_TRANS_Z) {
1275                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_Z);
1276                 manipulator_setcolor(v3d, 'Z', colcode, axisBlendAngle(rv3d->twangle[2]));
1277                 draw_cone(qobj, cylen, cywid);
1278         }
1279         /* X Cone */
1280         glTranslatef(dz, 0.0, -dz);
1281         if(drawflags & MAN_TRANS_X) {
1282                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_X);
1283                 glRotatef(90.0, 0.0, 1.0, 0.0);
1284                 manipulator_setcolor(v3d, 'X', colcode, axisBlendAngle(rv3d->twangle[0]));
1285                 draw_cone(qobj, cylen, cywid);
1286                 glRotatef(-90.0, 0.0, 1.0, 0.0);
1287         }
1288         /* Y Cone */
1289         glTranslatef(-dz, dz, 0.0);
1290         if(drawflags & MAN_TRANS_Y) {
1291                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_Y);
1292                 glRotatef(-90.0, 1.0, 0.0, 0.0);
1293                 manipulator_setcolor(v3d, 'Y', colcode, axisBlendAngle(rv3d->twangle[1]));
1294                 draw_cone(qobj, cylen, cywid);
1295         }
1296
1297         gluDeleteQuadric(qobj);
1298         glLoadMatrixf(rv3d->viewmat);
1299
1300         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1301
1302 }
1303
1304 static void draw_manipulator_rotate_cyl(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1305 {
1306         GLUquadricObj *qobj;
1307         float size;
1308         float cylen= 0.01f*(float)U.tw_handlesize;
1309         float cywid= 0.25f*cylen;
1310
1311         /* when called while moving in mixed mode, do not draw when... */
1312         if((drawflags & MAN_ROT_C)==0) return;
1313
1314         /* prepare for screen aligned draw */
1315         glPushMatrix();
1316         size= screen_aligned(rv3d, rv3d->twmat);
1317
1318         glDisable(GL_DEPTH_TEST);
1319
1320         qobj= gluNewQuadric();
1321
1322         /* Screen aligned view rot circle */
1323         if(drawflags & MAN_ROT_V) {
1324                 float unitmat[4][4]= MAT4_UNITY;
1325
1326                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_V);
1327                 UI_ThemeColor(TH_TRANSFORM);
1328                 drawcircball(GL_LINE_LOOP, unitmat[3], 1.2f*size, unitmat);
1329
1330                 if(moving) {
1331                         float vec[3];
1332                         vec[0]= 0; // XXX (float)(t->imval[0] - t->center2d[0]);
1333                         vec[1]= 0; // XXX (float)(t->imval[1] - t->center2d[1]);
1334                         vec[2]= 0.0f;
1335                         normalize_v3(vec);
1336                         mul_v3_fl(vec, 1.2f*size);
1337                         glBegin(GL_LINES);
1338                         glVertex3f(0.0, 0.0, 0.0);
1339                         glVertex3fv(vec);
1340                         glEnd();
1341                 }
1342         }
1343         glPopMatrix();
1344
1345         /* apply the transform delta */
1346         if(moving) {
1347                 float matt[4][4];
1348                 copy_m4_m4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
1349                 // XXX          if (t->flag & T_USES_MANIPULATOR) {
1350                 // XXX                  mul_m4_m3m4(matt, t->mat, rv3d->twmat);
1351                 // XXX }
1352                 glMultMatrixf(matt);
1353         }
1354         else {
1355                 glMultMatrixf(rv3d->twmat);
1356         }
1357
1358         glFrontFace(is_negative_m4(rv3d->twmat) ? GL_CW:GL_CCW);
1359
1360         /* axis */
1361         if( (G.f & G_PICKSEL)==0 ) {
1362
1363                 // only draw axis when combo didn't draw scale axes
1364                 if((combo & V3D_MANIP_SCALE)==0)
1365                         draw_manipulator_axes(v3d, rv3d, colcode, drawflags & MAN_ROT_X, drawflags & MAN_ROT_Y, drawflags & MAN_ROT_Z);
1366
1367                 /* only has to be set when not in picking */
1368                 gluQuadricDrawStyle(qobj, GLU_FILL);
1369         }
1370
1371         /* Z cyl */
1372         glTranslatef(0.0, 0.0, 1.0);
1373         if(drawflags & MAN_ROT_Z) {
1374                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1375                 manipulator_setcolor(v3d, 'Z', colcode, 255);
1376                 draw_cylinder(qobj, cylen, cywid);
1377         }
1378         /* X cyl */
1379         glTranslatef(1.0, 0.0, -1.0);
1380         if(drawflags & MAN_ROT_X) {
1381                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1382                 glRotatef(90.0, 0.0, 1.0, 0.0);
1383                 manipulator_setcolor(v3d, 'X', colcode, 255);
1384                 draw_cylinder(qobj, cylen, cywid);
1385                 glRotatef(-90.0, 0.0, 1.0, 0.0);
1386         }
1387         /* Y cylinder */
1388         glTranslatef(-1.0, 1.0, 0.0);
1389         if(drawflags & MAN_ROT_Y) {
1390                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1391                 glRotatef(-90.0, 1.0, 0.0, 0.0);
1392                 manipulator_setcolor(v3d, 'Y', colcode, 255);
1393                 draw_cylinder(qobj, cylen, cywid);
1394         }
1395
1396         /* restore */
1397
1398         gluDeleteQuadric(qobj);
1399         glLoadMatrixf(rv3d->viewmat);
1400
1401         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1402
1403 }
1404
1405
1406 /* ********************************************* */
1407
1408 /* main call, does calc centers & orientation too */
1409 /* uses global G.moving */
1410 static int drawflags= 0xFFFF;           // only for the calls below, belongs in scene...?
1411
1412 void BIF_draw_manipulator(const bContext *C)
1413 {
1414         ScrArea *sa= CTX_wm_area(C);
1415         ARegion *ar= CTX_wm_region(C);
1416         Scene *scene= CTX_data_scene(C);
1417         View3D *v3d= sa->spacedata.first;
1418         RegionView3D *rv3d= ar->regiondata;
1419         int totsel;
1420
1421         if(!(v3d->twflag & V3D_USE_MANIPULATOR)) return;
1422 //      if(G.moving && (G.moving & G_TRANSFORM_MANIP)==0) return;
1423
1424 //      if(G.moving==0) {
1425         {
1426                 v3d->twflag &= ~V3D_DRAW_MANIPULATOR;
1427
1428                 totsel= calc_manipulator_stats(C);
1429                 if(totsel==0) return;
1430
1431                 v3d->twflag |= V3D_DRAW_MANIPULATOR;
1432
1433                 /* now we can define center */
1434                 switch(v3d->around) {
1435                 case V3D_CENTER:
1436                 case V3D_ACTIVE:
1437                         rv3d->twmat[3][0]= (scene->twmin[0] + scene->twmax[0])/2.0f;
1438                         rv3d->twmat[3][1]= (scene->twmin[1] + scene->twmax[1])/2.0f;
1439                         rv3d->twmat[3][2]= (scene->twmin[2] + scene->twmax[2])/2.0f;
1440                         if(v3d->around==V3D_ACTIVE && scene->obedit==NULL) {
1441                                 Object *ob= OBACT;
1442                                 if(ob && !(ob->mode & OB_MODE_POSE))
1443                                         VECCOPY(rv3d->twmat[3], ob->obmat[3]);
1444                         }
1445                         break;
1446                 case V3D_LOCAL:
1447                 case V3D_CENTROID:
1448                         VECCOPY(rv3d->twmat[3], scene->twcent);
1449                         break;
1450                 case V3D_CURSOR:
1451                         VECCOPY(rv3d->twmat[3], give_cursor(scene, v3d));
1452                         break;
1453                 }
1454
1455                 mul_mat3_m4_fl(rv3d->twmat, view3d_pixel_size(rv3d, rv3d->twmat[3]) * U.tw_size * 5.0f);
1456         }
1457
1458         test_manipulator_axis(C);
1459         drawflags= rv3d->twdrawflag;    /* set in calc_manipulator_stats */
1460
1461         if(v3d->twflag & V3D_DRAW_MANIPULATOR) {
1462
1463                 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1464                 glEnable(GL_BLEND);
1465                 if(v3d->twtype & V3D_MANIP_ROTATE) {
1466
1467                         if(G.rt==3) {
1468                                 if(G.moving) draw_manipulator_rotate_cyl(v3d, rv3d, 1, drawflags, v3d->twtype, MAN_MOVECOL);
1469                                 else draw_manipulator_rotate_cyl(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1470                         }
1471                         else
1472                                 draw_manipulator_rotate(v3d, rv3d, 0 /* G.moving*/, drawflags, v3d->twtype);
1473                 }
1474                 if(v3d->twtype & V3D_MANIP_SCALE) {
1475                         draw_manipulator_scale(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1476                 }
1477                 if(v3d->twtype & V3D_MANIP_TRANSLATE) {
1478                         draw_manipulator_translate(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1479                 }
1480
1481                 glDisable(GL_BLEND);
1482         }
1483 }
1484
1485 static int manipulator_selectbuf(ScrArea *sa, ARegion *ar, short *mval, float hotspot)
1486 {
1487         View3D *v3d= sa->spacedata.first;
1488         RegionView3D *rv3d= ar->regiondata;
1489         rctf rect;
1490         GLuint buffer[64];              // max 4 items per select, so large enuf
1491         short hits;
1492         extern void setwinmatrixview3d(ARegion *ar, View3D *v3d, rctf *rect); // XXX check a bit later on this... (ton)
1493
1494         G.f |= G_PICKSEL;
1495
1496         rect.xmin= mval[0]-hotspot;
1497         rect.xmax= mval[0]+hotspot;
1498         rect.ymin= mval[1]-hotspot;
1499         rect.ymax= mval[1]+hotspot;
1500
1501         setwinmatrixview3d(ar, v3d, &rect);
1502         mul_m4_m4m4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1503
1504         glSelectBuffer( 64, buffer);
1505         glRenderMode(GL_SELECT);
1506         glInitNames();  /* these two calls whatfor? It doesnt work otherwise */
1507         glPushName(-2);
1508
1509         /* do the drawing */
1510         if(v3d->twtype & V3D_MANIP_ROTATE) {
1511                 if(G.rt==3) draw_manipulator_rotate_cyl(v3d, rv3d, 0, MAN_ROT_C & rv3d->twdrawflag, v3d->twtype, MAN_RGB);
1512                 else draw_manipulator_rotate(v3d, rv3d, 0, MAN_ROT_C & rv3d->twdrawflag, v3d->twtype);
1513         }
1514         if(v3d->twtype & V3D_MANIP_SCALE)
1515                 draw_manipulator_scale(v3d, rv3d, 0, MAN_SCALE_C & rv3d->twdrawflag, v3d->twtype, MAN_RGB);
1516         if(v3d->twtype & V3D_MANIP_TRANSLATE)
1517                 draw_manipulator_translate(v3d, rv3d, 0, MAN_TRANS_C & rv3d->twdrawflag, v3d->twtype, MAN_RGB);
1518
1519         glPopName();
1520         hits= glRenderMode(GL_RENDER);
1521
1522         G.f &= ~G_PICKSEL;
1523         setwinmatrixview3d(ar, v3d, NULL);
1524         mul_m4_m4m4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1525
1526         if(hits==1) return buffer[3];
1527         else if(hits>1) {
1528                 GLuint val, dep, mindep=0, mindeprot=0, minval=0, minvalrot=0;
1529                 int a;
1530
1531                 /* we compare the hits in buffer, but value centers highest */
1532                 /* we also store the rotation hits separate (because of arcs) and return hits on other widgets if there are */
1533
1534                 for(a=0; a<hits; a++) {
1535                         dep= buffer[4*a + 1];
1536                         val= buffer[4*a + 3];
1537
1538                         if(val==MAN_TRANS_C) return MAN_TRANS_C;
1539                         else if(val==MAN_SCALE_C) return MAN_SCALE_C;
1540                         else {
1541                                 if(val & MAN_ROT_C) {
1542                                         if(minvalrot==0 || dep<mindeprot) {
1543                                                 mindeprot= dep;
1544                                                 minvalrot= val;
1545                                         }
1546                                 }
1547                                 else {
1548                                         if(minval==0 || dep<mindep) {
1549                                                 mindep= dep;
1550                                                 minval= val;
1551                                         }
1552                                 }
1553                         }
1554                 }
1555
1556                 if(minval)
1557                         return minval;
1558                 else
1559                         return minvalrot;
1560         }
1561         return 0;
1562 }
1563
1564 int wm_operator_invoke(bContext *C, wmOperatorType *ot, wmEvent *event, PointerRNA *properties, ReportList *reports);
1565
1566 /* return 0; nothing happened */
1567 int BIF_do_manipulator(bContext *C, struct wmEvent *event, wmOperator *op)
1568 {
1569         ScrArea *sa= CTX_wm_area(C);
1570         View3D *v3d= sa->spacedata.first;
1571         ARegion *ar= CTX_wm_region(C);
1572         int constraint_axis[3] = {0, 0, 0};
1573         int val;
1574         int shift = event->shift;
1575
1576         if(!(v3d->twflag & V3D_USE_MANIPULATOR)) return 0;
1577         if(!(v3d->twflag & V3D_DRAW_MANIPULATOR)) return 0;
1578
1579         /* Force orientation */
1580         RNA_enum_set(op->ptr, "constraint_orientation", v3d->twmode);
1581
1582         // find the hotspots first test narrow hotspot
1583         val= manipulator_selectbuf(sa, ar, event->mval, 0.5f*(float)U.tw_hotspot);
1584         if(val) {
1585
1586                 // drawflags still global, for drawing call above
1587                 drawflags= manipulator_selectbuf(sa, ar, event->mval, 0.2f*(float)U.tw_hotspot);
1588                 if(drawflags==0) drawflags= val;
1589
1590                 if (drawflags & MAN_TRANS_C) {
1591                         switch(drawflags) {
1592                         case MAN_TRANS_C:
1593                                 break;
1594                         case MAN_TRANS_X:
1595                                 if(shift) {
1596                                         constraint_axis[1] = 1;
1597                                         constraint_axis[2] = 1;
1598                                 }
1599                                 else
1600                                         constraint_axis[0] = 1;
1601                                 break;
1602                         case MAN_TRANS_Y:
1603                                 if(shift) {
1604                                         constraint_axis[0] = 1;
1605                                         constraint_axis[2] = 1;
1606                                 }
1607                                 else
1608                                         constraint_axis[1] = 1;
1609                                 break;
1610                         case MAN_TRANS_Z:
1611                                 if(shift) {
1612                                         constraint_axis[0] = 1;
1613                                         constraint_axis[1] = 1;
1614                                 }
1615                                 else
1616                                         constraint_axis[2] = 1;
1617                                 break;
1618                         }
1619                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1620                         WM_operator_name_call(C, "TRANSFORM_OT_translate", WM_OP_INVOKE_DEFAULT, op->ptr);
1621                         //wm_operator_invoke(C, WM_operatortype_find("TRANSFORM_OT_translate", 0), event, op->ptr, NULL);
1622                 }
1623                 else if (drawflags & MAN_SCALE_C) {
1624                         switch(drawflags) {
1625                         case MAN_SCALE_X:
1626                                 if(shift) {
1627                                         constraint_axis[1] = 1;
1628                                         constraint_axis[2] = 1;
1629                                 }
1630                                 else
1631                                         constraint_axis[0] = 1;
1632                                 break;
1633                         case MAN_SCALE_Y:
1634                                 if(shift) {
1635                                         constraint_axis[0] = 1;
1636                                         constraint_axis[2] = 1;
1637                                 }
1638                                 else
1639                                         constraint_axis[1] = 1;
1640                                 break;
1641                         case MAN_SCALE_Z:
1642                                 if(shift) {
1643                                         constraint_axis[0] = 1;
1644                                         constraint_axis[1] = 1;
1645                                 }
1646                                 else
1647                                         constraint_axis[2] = 1;
1648                                 break;
1649                         }
1650                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1651                         WM_operator_name_call(C, "TRANSFORM_OT_resize", WM_OP_INVOKE_DEFAULT, op->ptr);
1652                         //wm_operator_invoke(C, WM_operatortype_find("TRANSFORM_OT_resize", 0), event, op->ptr, NULL);
1653                 }
1654                 else if (drawflags == MAN_ROT_T) { /* trackball need special case, init is different */
1655                         //wm_operator_invoke(C, WM_operatortype_find("TRANSFORM_OT_trackball", 0), event, op->ptr, NULL);
1656                         WM_operator_name_call(C, "TRANSFORM_OT_trackball", WM_OP_INVOKE_DEFAULT, op->ptr);
1657                 }
1658                 else if (drawflags & MAN_ROT_C) {
1659                         switch(drawflags) {
1660                         case MAN_ROT_X:
1661                                 constraint_axis[0] = 1;
1662                                 break;
1663                         case MAN_ROT_Y:
1664                                 constraint_axis[1] = 1;
1665                                 break;
1666                         case MAN_ROT_Z:
1667                                 constraint_axis[2] = 1;
1668                                 break;
1669                         }
1670                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1671                         WM_operator_name_call(C, "TRANSFORM_OT_rotate", WM_OP_INVOKE_DEFAULT, op->ptr);
1672                         //wm_operator_invoke(C, WM_operatortype_find("TRANSFORM_OT_rotate", 0), event, op->ptr, NULL);
1673                 }
1674         }
1675         /* after transform, restore drawflags */
1676         drawflags= 0xFFFF;
1677
1678         return val;
1679 }
1680