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