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