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