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