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