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