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