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