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