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