2.5 - Silencing various compiler warnings (mingw)
[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                         /* editmball.c */
305                         ListBase editelems= {NULL, NULL};  /* XXX */
306                         MetaElem *ml, *ml_sel=NULL;
307
308                         ml= 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->flag & OB_POSEMODE)) {
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(G.f & (G_VERTEXPAINT + G_TEXTUREPAINT + G_WEIGHTPAINT + G_SCULPTMODE)) {
363                 ;
364         }
365         else if(G.f & G_PARTICLEEDIT) {
366                 ParticleSystem *psys= PE_get_current(scene, ob);
367                 ParticleData *pa = psys->particles;
368                 ParticleEditKey *ek;
369                 int k;
370
371                 if(psys->edit) {
372                         for(a=0; a<psys->totpart; a++,pa++) {
373                                 if(pa->flag & PARS_HIDE) continue;
374
375                                 for(k=0, ek=psys->edit->keys[a]; k<pa->totkey; k++, ek++) {
376                                         if(ek->flag & PEK_SELECT) {
377                                                 calc_tw_center(scene, ek->world_co);
378                                                 totsel++;
379                                         }
380                                 }
381                         }
382
383                         /* selection center */
384                         if(totsel)
385                                 VecMulf(scene->twcent, 1.0f/(float)totsel);     // centroid!
386                 }
387         }
388         else {
389
390                 /* we need the one selected object, if its not active */
391                 ob= OBACT;
392                 if(ob && !(ob->flag & SELECT)) ob= NULL;
393
394                 for(base= scene->base.first; base; base= base->next) {
395                         if TESTBASELIB(scene, base) {
396                                 if(ob==NULL)
397                                         ob= base->object;
398                                 calc_tw_center(scene, base->object->obmat[3]);
399                                 protectflag_to_drawflags(base->object->protectflag, &v3d->twdrawflag);
400                                 totsel++;
401                         }
402                 }
403
404                 /* selection center */
405                 if(totsel) {
406                         VecMulf(scene->twcent, 1.0f/(float)totsel);     // centroid!
407                 }
408         }
409
410         /* global, local or normal orientation? */
411         if(ob && totsel) {
412
413                 switch(v3d->twmode) {
414
415                 case V3D_MANIP_NORMAL:
416                         if(obedit || ob->flag & OB_POSEMODE) {
417                                 float mat[3][3];
418                                 int type;
419
420                                 type = getTransformOrientation(C, normal, plane, (v3d->around == V3D_ACTIVE));
421
422                                 switch (type)
423                                 {
424                                         case ORIENTATION_NORMAL:
425                                                 if (createSpaceNormalTangent(mat, normal, plane) == 0)
426                                                 {
427                                                         type = ORIENTATION_NONE;
428                                                 }
429                                                 break;
430                                         case ORIENTATION_VERT:
431                                                 if (createSpaceNormal(mat, normal) == 0)
432                                                 {
433                                                         type = ORIENTATION_NONE;
434                                                 }
435                                                 break;
436                                         case ORIENTATION_EDGE:
437                                                 if (createSpaceNormalTangent(mat, normal, plane) == 0)
438                                                 {
439                                                         type = ORIENTATION_NONE;
440                                                 }
441                                                 break;
442                                         case ORIENTATION_FACE:
443                                                 if (createSpaceNormalTangent(mat, normal, plane) == 0)
444                                                 {
445                                                         type = ORIENTATION_NONE;
446                                                 }
447                                                 break;
448                                 }
449
450                                 if (type == ORIENTATION_NONE)
451                                 {
452                                         Mat4One(rv3d->twmat);
453                                 }
454                                 else
455                                 {
456                                         Mat4CpyMat3(rv3d->twmat, mat);
457                                 }
458                                 break;
459                         }
460                         /* no break we define 'normal' as 'local' in Object mode */
461                 case V3D_MANIP_LOCAL:
462                         Mat4CpyMat4(rv3d->twmat, ob->obmat);
463                         Mat4Ortho(rv3d->twmat);
464                         break;
465
466                 case V3D_MANIP_VIEW:
467                         {
468                                 float mat[3][3];
469                                 Mat3CpyMat4(mat, rv3d->viewinv);
470                                 Mat3Ortho(mat);
471                                 Mat4CpyMat3(rv3d->twmat, mat);
472                         }
473                         break;
474                 default: /* V3D_MANIP_CUSTOM */
475                         // XXX                  applyTransformOrientation(C, t);
476                         break;
477                 }
478
479         }
480
481         return totsel;
482 }
483
484 /* ******************** DRAWING STUFFIES *********** */
485
486 static float screen_aligned(RegionView3D *rv3d, float mat[][4])
487 {
488         float vec[3], size;
489
490         VECCOPY(vec, mat[0]);
491         size= Normalize(vec);
492
493         glTranslatef(mat[3][0], mat[3][1], mat[3][2]);
494
495         /* sets view screen aligned */
496         glRotatef( -360.0f*saacos(rv3d->viewquat[0])/(float)M_PI, rv3d->viewquat[1], rv3d->viewquat[2], rv3d->viewquat[3]);
497
498         return size;
499 }
500
501
502 /* radring = radius of donut rings
503    radhole = radius hole
504    start = starting segment (based on nrings)
505    end   = end segment
506    nsides = amount of points in ring
507    nrigns = amount of rings
508 */
509 static void partial_donut(float radring, float radhole, int start, int end, int nsides, int nrings)
510 {
511         float theta, phi, theta1;
512         float cos_theta, sin_theta;
513         float cos_theta1, sin_theta1;
514         float ring_delta, side_delta;
515         int i, j, docaps= 1;
516
517         if(start==0 && end==nrings) docaps= 0;
518
519         ring_delta= 2.0f*(float)M_PI/(float)nrings;
520         side_delta= 2.0f*(float)M_PI/(float)nsides;
521
522         theta= (float)M_PI+0.5f*ring_delta;
523         cos_theta= (float)cos(theta);
524         sin_theta= (float)sin(theta);
525
526         for(i= nrings - 1; i >= 0; i--) {
527                 theta1= theta + ring_delta;
528                 cos_theta1= (float)cos(theta1);
529                 sin_theta1= (float)sin(theta1);
530
531                 if(docaps && i==start) {        // cap
532                         glBegin(GL_POLYGON);
533                         phi= 0.0;
534                         for(j= nsides; j >= 0; j--) {
535                                 float cos_phi, sin_phi, dist;
536
537                                 phi += side_delta;
538                                 cos_phi= (float)cos(phi);
539                                 sin_phi= (float)sin(phi);
540                                 dist= radhole + radring * cos_phi;
541
542                                 glVertex3f(cos_theta1 * dist, -sin_theta1 * dist,  radring * sin_phi);
543                         }
544                         glEnd();
545                 }
546                 if(i>=start && i<=end) {
547                         glBegin(GL_QUAD_STRIP);
548                         phi= 0.0;
549                         for(j= nsides; j >= 0; j--) {
550                                 float cos_phi, sin_phi, dist;
551
552                                 phi += side_delta;
553                                 cos_phi= (float)cos(phi);
554                                 sin_phi= (float)sin(phi);
555                                 dist= radhole + radring * cos_phi;
556
557                                 glVertex3f(cos_theta1 * dist, -sin_theta1 * dist, radring * sin_phi);
558                                 glVertex3f(cos_theta * dist, -sin_theta * dist,  radring * sin_phi);
559                         }
560                         glEnd();
561                 }
562
563                 if(docaps && i==end) {  // cap
564                         glBegin(GL_POLYGON);
565                         phi= 0.0;
566                         for(j= nsides; j >= 0; j--) {
567                                 float cos_phi, sin_phi, dist;
568
569                                 phi -= side_delta;
570                                 cos_phi= (float)cos(phi);
571                                 sin_phi= (float)sin(phi);
572                                 dist= radhole + radring * cos_phi;
573
574                                 glVertex3f(cos_theta * dist, -sin_theta * dist,  radring * sin_phi);
575                         }
576                         glEnd();
577                 }
578
579
580                 theta= theta1;
581                 cos_theta= cos_theta1;
582                 sin_theta= sin_theta1;
583         }
584 }
585
586 /* three colors can be set;
587    grey for ghosting
588    moving: in transform theme color
589    else the red/green/blue
590 */
591 static void manipulator_setcolor(View3D *v3d, char axis, int colcode)
592 {
593         float vec[4];
594         char col[4];
595
596         vec[3]= 0.7f; // alpha set on 0.5, can be glEnabled or not
597
598         if(colcode==MAN_GHOST) {
599                 glColor4ub(0, 0, 0, 70);
600         }
601         else if(colcode==MAN_MOVECOL) {
602                 UI_GetThemeColor3ubv(TH_TRANSFORM, col);
603                 glColor4ub(col[0], col[1], col[2], 128);
604         }
605         else {
606                 switch(axis) {
607                 case 'c':
608                         UI_GetThemeColor3ubv(TH_TRANSFORM, col);
609                         if(v3d->twmode == V3D_MANIP_LOCAL) {
610                                 col[0]= col[0]>200?255:col[0]+55;
611                                 col[1]= col[1]>200?255:col[1]+55;
612                                 col[2]= col[2]>200?255:col[2]+55;
613                         }
614                         else if(v3d->twmode == V3D_MANIP_NORMAL) {
615                                 col[0]= col[0]<55?0:col[0]-55;
616                                 col[1]= col[1]<55?0:col[1]-55;
617                                 col[2]= col[2]<55?0:col[2]-55;
618                         }
619                         glColor4ub(col[0], col[1], col[2], 128);
620                         break;
621                 case 'x':
622                         glColor4ub(220, 0, 0, 128);
623                         break;
624                 case 'y':
625                         glColor4ub(0, 220, 0, 128);
626                         break;
627                 case 'z':
628                         glColor4ub(30, 30, 220, 128);
629                         break;
630                 }
631         }
632 }
633
634 /* viewmatrix should have been set OK, also no shademode! */
635 static void draw_manipulator_axes(View3D *v3d, int colcode, int flagx, int flagy, int flagz)
636 {
637
638         /* axes */
639         if(flagx) {
640                 manipulator_setcolor(v3d, 'x', colcode);
641                 if(flagx & MAN_SCALE_X) glLoadName(MAN_SCALE_X);
642                 else if(flagx & MAN_TRANS_X) glLoadName(MAN_TRANS_X);
643                 glBegin(GL_LINES);
644                 glVertex3f(0.2f, 0.0f, 0.0f);
645                 glVertex3f(1.0f, 0.0f, 0.0f);
646                 glEnd();
647         }
648         if(flagy) {
649                 if(flagy & MAN_SCALE_Y) glLoadName(MAN_SCALE_Y);
650                 else if(flagy & MAN_TRANS_Y) glLoadName(MAN_TRANS_Y);
651                 manipulator_setcolor(v3d, 'y', colcode);
652                 glBegin(GL_LINES);
653                 glVertex3f(0.0f, 0.2f, 0.0f);
654                 glVertex3f(0.0f, 1.0f, 0.0f);
655                 glEnd();
656         }
657         if(flagz) {
658                 if(flagz & MAN_SCALE_Z) glLoadName(MAN_SCALE_Z);
659                 else if(flagz & MAN_TRANS_Z) glLoadName(MAN_TRANS_Z);
660                 manipulator_setcolor(v3d, 'z', colcode);
661                 glBegin(GL_LINES);
662                 glVertex3f(0.0f, 0.0f, 0.2f);
663                 glVertex3f(0.0f, 0.0f, 1.0f);
664                 glEnd();
665         }
666 }
667
668 /* only called while G.moving */
669 static void draw_manipulator_rotate_ghost(View3D *v3d, RegionView3D *rv3d, int drawflags)
670 {
671         GLUquadricObj *qobj;
672         float size, phi, startphi, vec[3], svec[3], matt[4][4], cross[3], tmat[3][3];
673         int arcs= (G.rt!=2);
674
675         glDisable(GL_DEPTH_TEST);
676
677         qobj= gluNewQuadric();
678         gluQuadricDrawStyle(qobj, GLU_FILL);
679
680         glColor4ub(0,0,0,64);
681         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
682         glEnable(GL_BLEND);
683
684         /* we need both [4][4] transforms, t->mat seems to be premul, not post for mat[][4] */
685         Mat4CpyMat4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
686 // XXX  Mat4MulMat34(matt, t->mat, rv3d->twmat);
687
688         /* Screen aligned view rot circle */
689         if(drawflags & MAN_ROT_V) {
690
691                 /* prepare for screen aligned draw */
692                 glPushMatrix();
693                 size= screen_aligned(rv3d, rv3d->twmat);
694
695                 vec[0]= 0; // XXX (float)(t->con.imval[0] - t->center2d[0]);
696                 vec[1]= 0; // XXX (float)(t->con.imval[1] - t->center2d[1]);
697                 vec[2]= 0.0f;
698                 Normalize(vec);
699
700                 startphi= saacos( vec[1] );
701                 if(vec[0]<0.0) startphi= -startphi;
702
703                 phi= 0; // XXX (float)fmod(180.0*t->val/M_PI, 360.0);
704                 if(phi > 180.0) phi-= 360.0;
705                 else if(phi<-180.0) phi+= 360.0;
706
707                 gluPartialDisk(qobj, 0.0, size, 32, 1, 180.0*startphi/M_PI, phi);
708
709                 glPopMatrix();
710         }
711         else if(arcs) {
712                 float imat[3][3], ivmat[3][3];
713                 /* try to get the start rotation */
714
715                 svec[0]= 0; // XXX (float)(t->con.imval[0] - t->center2d[0]);
716                 svec[1]= 0; // XXX (float)(t->con.imval[1] - t->center2d[1]);
717                 svec[2]= 0.0f;
718
719                 /* screen aligned vec transform back to manipulator space */
720                 Mat3CpyMat4(ivmat, rv3d->viewinv);
721                 Mat3CpyMat4(tmat, rv3d->twmat);
722                 Mat3Inv(imat, tmat);
723                 Mat3MulMat3(tmat, imat, ivmat);
724
725                 Mat3MulVecfl(tmat, svec);       // tmat is used further on
726                 Normalize(svec);
727         }
728
729         wmMultMatrix(rv3d->twmat);      // aligns with original widget
730
731         /* Z disk */
732         if(drawflags & MAN_ROT_Z) {
733                 if(arcs) {
734                         /* correct for squeezed arc */
735                         svec[0]+= tmat[2][0];
736                         svec[1]+= tmat[2][1];
737                         Normalize(svec);
738
739                         startphi= (float)atan2(svec[0], svec[1]);
740                 }
741                 else startphi= 0.5f*(float)M_PI;
742
743                 VECCOPY(vec, rv3d->twmat[0]);   // use x axis to detect rotation
744                 Normalize(vec);
745                 Normalize(matt[0]);
746                 phi= saacos( Inpf(vec, matt[0]) );
747                 if(phi!=0.0) {
748                         Crossf(cross, vec, matt[0]);    // results in z vector
749                         if(Inpf(cross, rv3d->twmat[2]) > 0.0) phi= -phi;
750                         gluPartialDisk(qobj, 0.0, 1.0, 32, 1, 180.0*startphi/M_PI, 180.0*(phi)/M_PI);
751                 }
752         }
753         /* X disk */
754         if(drawflags & MAN_ROT_X) {
755                 if(arcs) {
756                         /* correct for squeezed arc */
757                         svec[1]+= tmat[2][1];
758                         svec[2]+= tmat[2][2];
759                         Normalize(svec);
760
761                         startphi= (float)(M_PI + atan2(svec[2], -svec[1]));
762                 }
763                 else startphi= 0.0f;
764
765                 VECCOPY(vec, rv3d->twmat[1]);   // use y axis to detect rotation
766                 Normalize(vec);
767                 Normalize(matt[1]);
768                 phi= saacos( Inpf(vec, matt[1]) );
769                 if(phi!=0.0) {
770                         Crossf(cross, vec, matt[1]);    // results in x vector
771                         if(Inpf(cross, rv3d->twmat[0]) > 0.0) phi= -phi;
772                         glRotatef(90.0, 0.0, 1.0, 0.0);
773                         gluPartialDisk(qobj, 0.0, 1.0, 32, 1, 180.0*startphi/M_PI, 180.0*phi/M_PI);
774                         glRotatef(-90.0, 0.0, 1.0, 0.0);
775                 }
776         }
777         /* Y circle */
778         if(drawflags & MAN_ROT_Y) {
779                 if(arcs) {
780                         /* correct for squeezed arc */
781                         svec[0]+= tmat[2][0];
782                         svec[2]+= tmat[2][2];
783                         Normalize(svec);
784
785                         startphi= (float)(M_PI + atan2(-svec[0], svec[2]));
786                 }
787                 else startphi= (float)M_PI;
788
789                 VECCOPY(vec, rv3d->twmat[2]);   // use z axis to detect rotation
790                 Normalize(vec);
791                 Normalize(matt[2]);
792                 phi= saacos( Inpf(vec, matt[2]) );
793                 if(phi!=0.0) {
794                         Crossf(cross, vec, matt[2]);    // results in y vector
795                         if(Inpf(cross, rv3d->twmat[1]) > 0.0) phi= -phi;
796                         glRotatef(-90.0, 1.0, 0.0, 0.0);
797                         gluPartialDisk(qobj, 0.0, 1.0, 32, 1, 180.0*startphi/M_PI, 180.0*phi/M_PI);
798                         glRotatef(90.0, 1.0, 0.0, 0.0);
799                 }
800         }
801
802         glDisable(GL_BLEND);
803         wmLoadMatrix(rv3d->viewmat);
804 }
805
806 static void draw_manipulator_rotate(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo)
807 {
808         GLUquadricObj *qobj;
809         double plane[4];
810         float size, vec[3], unitmat[4][4];
811         float cywid= 0.33f*0.01f*(float)U.tw_handlesize;
812         float cusize= cywid*0.65f;
813         int arcs= (G.rt!=2);
814         int colcode;
815
816         if(moving) colcode= MAN_MOVECOL;
817         else colcode= MAN_RGB;
818
819         /* when called while moving in mixed mode, do not draw when... */
820         if((drawflags & MAN_ROT_C)==0) return;
821
822         /* Init stuff */
823         glDisable(GL_DEPTH_TEST);
824         Mat4One(unitmat);
825
826         qobj= gluNewQuadric();
827         gluQuadricDrawStyle(qobj, GLU_FILL);
828
829         /* prepare for screen aligned draw */
830         VECCOPY(vec, rv3d->twmat[0]);
831         size= Normalize(vec);
832         glPushMatrix();
833         glTranslatef(rv3d->twmat[3][0], rv3d->twmat[3][1], rv3d->twmat[3][2]);
834
835         if(arcs) {
836                 /* clipplane makes nice handles, calc here because of multmatrix but with translate! */
837                 VECCOPY(plane, rv3d->viewinv[2]);
838                 plane[3]= -0.02*size; // clip just a bit more
839                 glClipPlane(GL_CLIP_PLANE0, plane);
840         }
841         /* sets view screen aligned */
842         glRotatef( -360.0f*saacos(rv3d->viewquat[0])/(float)M_PI, rv3d->viewquat[1], rv3d->viewquat[2], rv3d->viewquat[3]);
843
844         /* Screen aligned help circle */
845         if(arcs) {
846                 if((G.f & G_PICKSEL)==0) {
847                         UI_ThemeColorShade(TH_BACK, -30);
848                         drawcircball(GL_LINE_LOOP, unitmat[3], size, unitmat);
849                 }
850         }
851         /* Screen aligned view rot circle */
852         if(drawflags & MAN_ROT_V) {
853                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_V);
854                 UI_ThemeColor(TH_TRANSFORM);
855                 drawcircball(GL_LINE_LOOP, unitmat[3], 1.2f*size, unitmat);
856
857                 if(moving) {
858                         float vec[3];
859                         vec[0]= 0; // XXX (float)(t->imval[0] - t->center2d[0]);
860                         vec[1]= 0; // XXX (float)(t->imval[1] - t->center2d[1]);
861                         vec[2]= 0.0f;
862                         Normalize(vec);
863                         VecMulf(vec, 1.2f*size);
864                         glBegin(GL_LINES);
865                         glVertex3f(0.0f, 0.0f, 0.0f);
866                         glVertex3fv(vec);
867                         glEnd();
868                 }
869         }
870         glPopMatrix();
871
872         /* apply the transform delta */
873         if(moving) {
874                 float matt[4][4];
875                 Mat4CpyMat4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
876                 // XXX Mat4MulMat34(matt, t->mat, rv3d->twmat);
877                 wmMultMatrix(matt);
878                 glFrontFace( is_mat4_flipped(matt)?GL_CW:GL_CCW);
879         }
880         else {
881                 glFrontFace( is_mat4_flipped(rv3d->twmat)?GL_CW:GL_CCW);
882                 wmMultMatrix(rv3d->twmat);
883         }
884
885         /* axes */
886         if(arcs==0) {
887                 if(!(G.f & G_PICKSEL)) {
888                         if( (combo & V3D_MANIP_SCALE)==0) {
889                                 /* axis */
890                                 glBegin(GL_LINES);
891                                 if( (drawflags & MAN_ROT_X) || (moving && (drawflags & MAN_ROT_Z)) ) {
892                                         manipulator_setcolor(v3d, 'x', colcode);
893                                         glVertex3f(0.2f, 0.0f, 0.0f);
894                                         glVertex3f(1.0f, 0.0f, 0.0f);
895                                 }
896                                 if( (drawflags & MAN_ROT_Y) || (moving && (drawflags & MAN_ROT_X)) ) {
897                                         manipulator_setcolor(v3d, 'y', colcode);
898                                         glVertex3f(0.0f, 0.2f, 0.0f);
899                                         glVertex3f(0.0f, 1.0f, 0.0f);
900                                 }
901                                 if( (drawflags & MAN_ROT_Z) || (moving && (drawflags & MAN_ROT_Y)) ) {
902                                         manipulator_setcolor(v3d, 'z', colcode);
903                                         glVertex3f(0.0f, 0.0f, 0.2f);
904                                         glVertex3f(0.0f, 0.0f, 1.0f);
905                                 }
906                                 glEnd();
907                         }
908                 }
909         }
910
911         if(arcs==0 && moving) {
912
913                 /* Z circle */
914                 if(drawflags & MAN_ROT_Z) {
915                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
916                         manipulator_setcolor(v3d, 'z', colcode);
917                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
918                 }
919                 /* X circle */
920                 if(drawflags & MAN_ROT_X) {
921                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
922                         glRotatef(90.0, 0.0, 1.0, 0.0);
923                         manipulator_setcolor(v3d, 'x', colcode);
924                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
925                         glRotatef(-90.0, 0.0, 1.0, 0.0);
926                 }
927                 /* Y circle */
928                 if(drawflags & MAN_ROT_Y) {
929                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
930                         glRotatef(-90.0, 1.0, 0.0, 0.0);
931                         manipulator_setcolor(v3d, 'y', colcode);
932                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
933                         glRotatef(90.0, 1.0, 0.0, 0.0);
934                 }
935
936                 if(arcs) glDisable(GL_CLIP_PLANE0);
937         }
938         // donut arcs
939         if(arcs) {
940                 glEnable(GL_CLIP_PLANE0);
941
942                 /* Z circle */
943                 if(drawflags & MAN_ROT_Z) {
944                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
945                         manipulator_setcolor(v3d, 'z', colcode);
946                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
947                 }
948                 /* X circle */
949                 if(drawflags & MAN_ROT_X) {
950                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
951                         glRotatef(90.0, 0.0, 1.0, 0.0);
952                         manipulator_setcolor(v3d, 'x', colcode);
953                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
954                         glRotatef(-90.0, 0.0, 1.0, 0.0);
955                 }
956                 /* Y circle */
957                 if(drawflags & MAN_ROT_Y) {
958                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
959                         glRotatef(-90.0, 1.0, 0.0, 0.0);
960                         manipulator_setcolor(v3d, 'y', colcode);
961                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
962                         glRotatef(90.0, 1.0, 0.0, 0.0);
963                 }
964
965                 glDisable(GL_CLIP_PLANE0);
966         }
967
968         if(arcs==0) {
969
970                 /* Z handle on X axis */
971                 if(drawflags & MAN_ROT_Z) {
972                         glPushMatrix();
973                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
974                         manipulator_setcolor(v3d, 'z', colcode);
975
976                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
977
978                         glPopMatrix();
979                 }
980
981                 /* Y handle on X axis */
982                 if(drawflags & MAN_ROT_Y) {
983                         glPushMatrix();
984                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
985                         manipulator_setcolor(v3d, 'y', colcode);
986
987                         glRotatef(90.0, 1.0, 0.0, 0.0);
988                         glRotatef(90.0, 0.0, 0.0, 1.0);
989                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
990
991                         glPopMatrix();
992                 }
993
994                 /* X handle on Z axis */
995                 if(drawflags & MAN_ROT_X) {
996                         glPushMatrix();
997                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
998                         manipulator_setcolor(v3d, 'x', colcode);
999
1000                         glRotatef(-90.0, 0.0, 1.0, 0.0);
1001                         glRotatef(90.0, 0.0, 0.0, 1.0);
1002                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1003
1004                         glPopMatrix();
1005                 }
1006
1007         }
1008
1009         /* restore */
1010         wmLoadMatrix(rv3d->viewmat);
1011         gluDeleteQuadric(qobj);
1012         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1013
1014 }
1015
1016 static void drawsolidcube(float size)
1017 {
1018         static float cube[8][3] = {
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         { 1.0,  1.0, -1.0},     };
1027         float n[3];
1028
1029         glPushMatrix();
1030         glScalef(size, size, size);
1031
1032         n[0]=0; n[1]=0; n[2]=0;
1033         glBegin(GL_QUADS);
1034         n[0]= -1.0;
1035         glNormal3fv(n);
1036         glVertex3fv(cube[0]); glVertex3fv(cube[1]); glVertex3fv(cube[2]); glVertex3fv(cube[3]);
1037         n[0]=0;
1038         glEnd();
1039
1040         glBegin(GL_QUADS);
1041         n[1]= -1.0;
1042         glNormal3fv(n);
1043         glVertex3fv(cube[0]); glVertex3fv(cube[4]); glVertex3fv(cube[5]); glVertex3fv(cube[1]);
1044         n[1]=0;
1045         glEnd();
1046
1047         glBegin(GL_QUADS);
1048         n[0]= 1.0;
1049         glNormal3fv(n);
1050         glVertex3fv(cube[4]); glVertex3fv(cube[7]); glVertex3fv(cube[6]); glVertex3fv(cube[5]);
1051         n[0]=0;
1052         glEnd();
1053
1054         glBegin(GL_QUADS);
1055         n[1]= 1.0;
1056         glNormal3fv(n);
1057         glVertex3fv(cube[7]); glVertex3fv(cube[3]); glVertex3fv(cube[2]); glVertex3fv(cube[6]);
1058         n[1]=0;
1059         glEnd();
1060
1061         glBegin(GL_QUADS);
1062         n[2]= 1.0;
1063         glNormal3fv(n);
1064         glVertex3fv(cube[1]); glVertex3fv(cube[5]); glVertex3fv(cube[6]); glVertex3fv(cube[2]);
1065         n[2]=0;
1066         glEnd();
1067
1068         glBegin(GL_QUADS);
1069         n[2]= -1.0;
1070         glNormal3fv(n);
1071         glVertex3fv(cube[7]); glVertex3fv(cube[4]); glVertex3fv(cube[0]); glVertex3fv(cube[3]);
1072         glEnd();
1073
1074         glPopMatrix();
1075 }
1076
1077
1078 static void draw_manipulator_scale(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1079 {
1080         float cywid= 0.25f*0.01f*(float)U.tw_handlesize;
1081         float cusize= cywid*0.75f, dz;
1082
1083         /* when called while moving in mixed mode, do not draw when... */
1084         if((drawflags & MAN_SCALE_C)==0) return;
1085
1086         glDisable(GL_DEPTH_TEST);
1087
1088         /* not in combo mode */
1089         if( (combo & (V3D_MANIP_TRANSLATE|V3D_MANIP_ROTATE))==0) {
1090                 float size, unitmat[4][4];
1091                 int shift= 0; // XXX
1092
1093                 /* center circle, do not add to selection when shift is pressed (planar constraint)  */
1094                 if( (G.f & G_PICKSEL) && shift==0) glLoadName(MAN_SCALE_C);
1095
1096                 manipulator_setcolor(v3d, 'c', colcode);
1097                 glPushMatrix();
1098                 size= screen_aligned(rv3d, rv3d->twmat);
1099                 Mat4One(unitmat);
1100                 drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
1101                 glPopMatrix();
1102
1103                 dz= 1.0;
1104         }
1105         else dz= 1.0f-4.0f*cusize;
1106
1107         if(moving) {
1108                 float matt[4][4];
1109
1110                 Mat4CpyMat4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
1111                 // XXX Mat4MulMat34(matt, t->mat, rv3d->twmat);
1112                 wmMultMatrix(matt);
1113                 glFrontFace( is_mat4_flipped(matt)?GL_CW:GL_CCW);
1114         }
1115         else {
1116                 wmMultMatrix(rv3d->twmat);
1117                 glFrontFace( is_mat4_flipped(rv3d->twmat)?GL_CW:GL_CCW);
1118         }
1119
1120         /* axis */
1121
1122         /* in combo mode, this is always drawn as first type */
1123         draw_manipulator_axes(v3d, colcode, drawflags & MAN_SCALE_X, drawflags & MAN_SCALE_Y, drawflags & MAN_SCALE_Z);
1124
1125         /* Z cube */
1126         glTranslatef(0.0, 0.0, dz);
1127         if(drawflags & MAN_SCALE_Z) {
1128                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_Z);
1129                 manipulator_setcolor(v3d, 'z', colcode);
1130                 drawsolidcube(cusize);
1131         }
1132         /* X cube */
1133         glTranslatef(dz, 0.0, -dz);
1134         if(drawflags & MAN_SCALE_X) {
1135                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_X);
1136                 manipulator_setcolor(v3d, 'x', colcode);
1137                 drawsolidcube(cusize);
1138         }
1139         /* Y cube */
1140         glTranslatef(-dz, dz, 0.0);
1141         if(drawflags & MAN_SCALE_Y) {
1142                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_Y);
1143                 manipulator_setcolor(v3d, 'y', colcode);
1144                 drawsolidcube(cusize);
1145         }
1146
1147         /* if shiftkey, center point as last, for selectbuffer order */
1148         if(G.f & G_PICKSEL) {
1149                 int shift= 0; // XXX
1150
1151                 if(shift) {
1152                         glTranslatef(0.0, -dz, 0.0);
1153                         glLoadName(MAN_SCALE_C);
1154                         glBegin(GL_POINTS);
1155                         glVertex3f(0.0, 0.0, 0.0);
1156                         glEnd();
1157                 }
1158         }
1159
1160         /* restore */
1161         wmLoadMatrix(rv3d->viewmat);
1162
1163         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1164         glFrontFace(GL_CCW);
1165 }
1166
1167
1168 static void draw_cone(GLUquadricObj *qobj, float len, float width)
1169 {
1170         glTranslatef(0.0, 0.0, -0.5f*len);
1171         gluCylinder(qobj, width, 0.0, len, 8, 1);
1172         gluQuadricOrientation(qobj, GLU_INSIDE);
1173         gluDisk(qobj, 0.0, width, 8, 1);
1174         gluQuadricOrientation(qobj, GLU_OUTSIDE);
1175         glTranslatef(0.0, 0.0, 0.5f*len);
1176 }
1177
1178 static void draw_cylinder(GLUquadricObj *qobj, float len, float width)
1179 {
1180
1181         width*= 0.8f;   // just for beauty
1182
1183         glTranslatef(0.0, 0.0, -0.5f*len);
1184         gluCylinder(qobj, width, width, len, 8, 1);
1185         gluQuadricOrientation(qobj, GLU_INSIDE);
1186         gluDisk(qobj, 0.0, width, 8, 1);
1187         gluQuadricOrientation(qobj, GLU_OUTSIDE);
1188         glTranslatef(0.0, 0.0, len);
1189         gluDisk(qobj, 0.0, width, 8, 1);
1190         glTranslatef(0.0, 0.0, -0.5f*len);
1191 }
1192
1193
1194 static void draw_manipulator_translate(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1195 {
1196         GLUquadricObj *qobj;
1197         float cylen= 0.01f*(float)U.tw_handlesize;
1198         float cywid= 0.25f*cylen, dz, size;
1199         float unitmat[4][4];
1200         int shift= 0; // XXX
1201
1202         /* when called while moving in mixed mode, do not draw when... */
1203         if((drawflags & MAN_TRANS_C)==0) return;
1204
1205         // XXX if(moving) glTranslatef(t->vec[0], t->vec[1], t->vec[2]);
1206         glDisable(GL_DEPTH_TEST);
1207
1208         qobj= gluNewQuadric();
1209         gluQuadricDrawStyle(qobj, GLU_FILL);
1210
1211         /* center circle, do not add to selection when shift is pressed (planar constraint) */
1212         if( (G.f & G_PICKSEL) && shift==0) glLoadName(MAN_TRANS_C);
1213
1214         manipulator_setcolor(v3d, 'c', colcode);
1215         glPushMatrix();
1216         size= screen_aligned(rv3d, rv3d->twmat);
1217         Mat4One(unitmat);
1218         drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
1219         glPopMatrix();
1220
1221         /* and now apply matrix, we move to local matrix drawing */
1222         wmMultMatrix(rv3d->twmat);
1223
1224         /* axis */
1225         glLoadName(-1);
1226
1227         // translate drawn as last, only axis when no combo with scale, or for ghosting
1228         if((combo & V3D_MANIP_SCALE)==0 || colcode==MAN_GHOST)
1229                 draw_manipulator_axes(v3d, colcode, drawflags & MAN_TRANS_X, drawflags & MAN_TRANS_Y, drawflags & MAN_TRANS_Z);
1230
1231
1232         /* offset in combo mode, for rotate a bit more */
1233         if(combo & (V3D_MANIP_ROTATE)) dz= 1.0f+2.0f*cylen;
1234         else if(combo & (V3D_MANIP_SCALE)) dz= 1.0f+0.5f*cylen;
1235         else dz= 1.0f;
1236
1237         /* Z Cone */
1238         glTranslatef(0.0, 0.0, dz);
1239         if(drawflags & MAN_TRANS_Z) {
1240                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_Z);
1241                 manipulator_setcolor(v3d, 'z', colcode);
1242                 draw_cone(qobj, cylen, cywid);
1243         }
1244         /* X Cone */
1245         glTranslatef(dz, 0.0, -dz);
1246         if(drawflags & MAN_TRANS_X) {
1247                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_X);
1248                 glRotatef(90.0, 0.0, 1.0, 0.0);
1249                 manipulator_setcolor(v3d, 'x', colcode);
1250                 draw_cone(qobj, cylen, cywid);
1251                 glRotatef(-90.0, 0.0, 1.0, 0.0);
1252         }
1253         /* Y Cone */
1254         glTranslatef(-dz, dz, 0.0);
1255         if(drawflags & MAN_TRANS_Y) {
1256                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_Y);
1257                 glRotatef(-90.0, 1.0, 0.0, 0.0);
1258                 manipulator_setcolor(v3d, 'y', colcode);
1259                 draw_cone(qobj, cylen, cywid);
1260         }
1261
1262         gluDeleteQuadric(qobj);
1263         wmLoadMatrix(rv3d->viewmat);
1264
1265         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1266
1267 }
1268
1269 static void draw_manipulator_rotate_cyl(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1270 {
1271         GLUquadricObj *qobj;
1272         float size;
1273         float cylen= 0.01f*(float)U.tw_handlesize;
1274         float cywid= 0.25f*cylen;
1275
1276         /* when called while moving in mixed mode, do not draw when... */
1277         if((drawflags & MAN_ROT_C)==0) return;
1278
1279         /* prepare for screen aligned draw */
1280         glPushMatrix();
1281         size= screen_aligned(rv3d, rv3d->twmat);
1282
1283         glDisable(GL_DEPTH_TEST);
1284
1285         qobj= gluNewQuadric();
1286
1287         /* Screen aligned view rot circle */
1288         if(drawflags & MAN_ROT_V) {
1289                 float unitmat[4][4];
1290                 Mat4One(unitmat);
1291
1292                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_V);
1293                 UI_ThemeColor(TH_TRANSFORM);
1294                 drawcircball(GL_LINE_LOOP, unitmat[3], 1.2f*size, unitmat);
1295
1296                 if(moving) {
1297                         float vec[3];
1298                         vec[0]= 0; // XXX (float)(t->imval[0] - t->center2d[0]);
1299                         vec[1]= 0; // XXX (float)(t->imval[1] - t->center2d[1]);
1300                         vec[2]= 0.0f;
1301                         Normalize(vec);
1302                         VecMulf(vec, 1.2f*size);
1303                         glBegin(GL_LINES);
1304                         glVertex3f(0.0, 0.0, 0.0);
1305                         glVertex3fv(vec);
1306                         glEnd();
1307                 }
1308         }
1309         glPopMatrix();
1310
1311         /* apply the transform delta */
1312         if(moving) {
1313                 float matt[4][4];
1314                 Mat4CpyMat4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
1315                 // XXX          if (t->flag & T_USES_MANIPULATOR) {
1316                 // XXX                  Mat4MulMat34(matt, t->mat, rv3d->twmat);
1317                 // XXX }
1318                 wmMultMatrix(matt);
1319         }
1320         else {
1321                 wmMultMatrix(rv3d->twmat);
1322         }
1323
1324         glFrontFace( is_mat4_flipped(rv3d->twmat)?GL_CW:GL_CCW);
1325
1326         /* axis */
1327         if( (G.f & G_PICKSEL)==0 ) {
1328
1329                 // only draw axis when combo didn't draw scale axes
1330                 if((combo & V3D_MANIP_SCALE)==0)
1331                         draw_manipulator_axes(v3d, colcode, drawflags & MAN_ROT_X, drawflags & MAN_ROT_Y, drawflags & MAN_ROT_Z);
1332
1333                 /* only has to be set when not in picking */
1334                 gluQuadricDrawStyle(qobj, GLU_FILL);
1335         }
1336
1337         /* Z cyl */
1338         glTranslatef(0.0, 0.0, 1.0);
1339         if(drawflags & MAN_ROT_Z) {
1340                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1341                 manipulator_setcolor(v3d, 'z', colcode);
1342                 draw_cylinder(qobj, cylen, cywid);
1343         }
1344         /* X cyl */
1345         glTranslatef(1.0, 0.0, -1.0);
1346         if(drawflags & MAN_ROT_X) {
1347                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1348                 glRotatef(90.0, 0.0, 1.0, 0.0);
1349                 manipulator_setcolor(v3d, 'x', colcode);
1350                 draw_cylinder(qobj, cylen, cywid);
1351                 glRotatef(-90.0, 0.0, 1.0, 0.0);
1352         }
1353         /* Y cylinder */
1354         glTranslatef(-1.0, 1.0, 0.0);
1355         if(drawflags & MAN_ROT_Y) {
1356                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1357                 glRotatef(-90.0, 1.0, 0.0, 0.0);
1358                 manipulator_setcolor(v3d, 'y', colcode);
1359                 draw_cylinder(qobj, cylen, cywid);
1360         }
1361
1362         /* restore */
1363
1364         gluDeleteQuadric(qobj);
1365         wmLoadMatrix(rv3d->viewmat);
1366
1367         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1368
1369 }
1370
1371
1372 /* ********************************************* */
1373
1374 static float get_manipulator_drawsize(ARegion *ar)
1375 {
1376         RegionView3D *rv3d= ar->regiondata;
1377         float size = get_drawsize(ar, rv3d->twmat[3]);
1378
1379         size*= (float)U.tw_size;
1380
1381         return size;
1382 }
1383
1384
1385 /* main call, does calc centers & orientation too */
1386 /* uses global G.moving */
1387 static int drawflags= 0xFFFF;           // only for the calls below, belongs in scene...?
1388
1389 void BIF_draw_manipulator(const bContext *C)
1390 {
1391         ScrArea *sa= CTX_wm_area(C);
1392         ARegion *ar= CTX_wm_region(C);
1393         Scene *scene= CTX_data_scene(C);
1394         View3D *v3d= sa->spacedata.first;
1395         RegionView3D *rv3d= ar->regiondata;
1396         int totsel;
1397
1398         if(!(v3d->twflag & V3D_USE_MANIPULATOR)) return;
1399         if(G.moving && (G.moving & G_TRANSFORM_MANIP)==0) return;
1400
1401         if(G.moving==0) {
1402                 v3d->twflag &= ~V3D_DRAW_MANIPULATOR;
1403
1404                 totsel= calc_manipulator_stats(C);
1405                 if(totsel==0) return;
1406                 drawflags= v3d->twdrawflag;     /* set in calc_manipulator_stats */
1407
1408                 v3d->twflag |= V3D_DRAW_MANIPULATOR;
1409
1410                 /* now we can define center */
1411                 switch(v3d->around) {
1412                 case V3D_CENTER:
1413                 case V3D_ACTIVE:
1414                         rv3d->twmat[3][0]= (scene->twmin[0] + scene->twmax[0])/2.0f;
1415                         rv3d->twmat[3][1]= (scene->twmin[1] + scene->twmax[1])/2.0f;
1416                         rv3d->twmat[3][2]= (scene->twmin[2] + scene->twmax[2])/2.0f;
1417                         if(v3d->around==V3D_ACTIVE && scene->obedit==NULL) {
1418                                 Object *ob= OBACT;
1419                                 if(ob && !(ob->flag & OB_POSEMODE))
1420                                         VECCOPY(rv3d->twmat[3], ob->obmat[3]);
1421                         }
1422                         break;
1423                 case V3D_LOCAL:
1424                 case V3D_CENTROID:
1425                         VECCOPY(rv3d->twmat[3], scene->twcent);
1426                         break;
1427                 case V3D_CURSOR:
1428                         VECCOPY(rv3d->twmat[3], give_cursor(scene, v3d));
1429                         break;
1430                 }
1431
1432                 Mat4MulFloat3((float *)rv3d->twmat, get_manipulator_drawsize(ar));
1433         }
1434
1435         if(v3d->twflag & V3D_DRAW_MANIPULATOR) {
1436
1437                 if(v3d->twtype & V3D_MANIP_ROTATE) {
1438
1439                         /* rotate has special ghosting draw, for pie chart */
1440                         if(G.moving) draw_manipulator_rotate_ghost(v3d, rv3d, drawflags);
1441
1442                         if(G.moving) glEnable(GL_BLEND);
1443
1444                         if(G.rt==3) {
1445                                 if(G.moving) draw_manipulator_rotate_cyl(v3d, rv3d, 1, drawflags, v3d->twtype, MAN_MOVECOL);
1446                                 else draw_manipulator_rotate_cyl(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1447                         }
1448                         else
1449                                 draw_manipulator_rotate(v3d, rv3d, G.moving, drawflags, v3d->twtype);
1450
1451                         glDisable(GL_BLEND);
1452                 }
1453                 if(v3d->twtype & V3D_MANIP_SCALE) {
1454                         if(G.moving) {
1455                                 glEnable(GL_BLEND);
1456                                 draw_manipulator_scale(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_GHOST);
1457                                 draw_manipulator_scale(v3d, rv3d, 1, drawflags, v3d->twtype, MAN_MOVECOL);
1458                                 glDisable(GL_BLEND);
1459                         }
1460                         else draw_manipulator_scale(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1461                 }
1462                 if(v3d->twtype & V3D_MANIP_TRANSLATE) {
1463                         if(G.moving) {
1464                                 glEnable(GL_BLEND);
1465                                 draw_manipulator_translate(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_GHOST);
1466                                 draw_manipulator_translate(v3d, rv3d, 1, drawflags, v3d->twtype, MAN_MOVECOL);
1467                                 glDisable(GL_BLEND);
1468                         }
1469                         else draw_manipulator_translate(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1470                 }
1471         }
1472 }
1473
1474 static int manipulator_selectbuf(ScrArea *sa, ARegion *ar, short *mval, float hotspot)
1475 {
1476         View3D *v3d= sa->spacedata.first;
1477         RegionView3D *rv3d= ar->regiondata;
1478         rctf rect;
1479         GLuint buffer[64];              // max 4 items per select, so large enuf
1480         short hits;
1481         extern void setwinmatrixview3d(ARegion *ar, View3D *v3d, rctf *rect); // XXX check a bit later on this... (ton)
1482
1483         G.f |= G_PICKSEL;
1484
1485         rect.xmin= mval[0]-hotspot;
1486         rect.xmax= mval[0]+hotspot;
1487         rect.ymin= mval[1]-hotspot;
1488         rect.ymax= mval[1]+hotspot;
1489
1490         setwinmatrixview3d(ar, v3d, &rect);
1491         Mat4MulMat4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1492
1493         glSelectBuffer( 64, buffer);
1494         glRenderMode(GL_SELECT);
1495         glInitNames();  /* these two calls whatfor? It doesnt work otherwise */
1496         glPushName(-2);
1497
1498         /* do the drawing */
1499         if(v3d->twtype & V3D_MANIP_ROTATE) {
1500                 if(G.rt==3) draw_manipulator_rotate_cyl(v3d, rv3d, 0, MAN_ROT_C & v3d->twdrawflag, v3d->twtype, MAN_RGB);
1501                 else draw_manipulator_rotate(v3d, rv3d, 0, MAN_ROT_C & v3d->twdrawflag, v3d->twtype);
1502         }
1503         if(v3d->twtype & V3D_MANIP_SCALE)
1504                 draw_manipulator_scale(v3d, rv3d, 0, MAN_SCALE_C & v3d->twdrawflag, v3d->twtype, MAN_RGB);
1505         if(v3d->twtype & V3D_MANIP_TRANSLATE)
1506                 draw_manipulator_translate(v3d, rv3d, 0, MAN_TRANS_C & v3d->twdrawflag, v3d->twtype, MAN_RGB);
1507
1508         glPopName();
1509         hits= glRenderMode(GL_RENDER);
1510
1511         G.f &= ~G_PICKSEL;
1512         setwinmatrixview3d(ar, v3d, NULL);
1513         Mat4MulMat4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1514
1515         if(hits==1) return buffer[3];
1516         else if(hits>1) {
1517                 GLuint val, dep, mindep=0, mindeprot=0, minval=0, minvalrot=0;
1518                 int a;
1519
1520                 /* we compare the hits in buffer, but value centers highest */
1521                 /* we also store the rotation hits separate (because of arcs) and return hits on other widgets if there are */
1522
1523                 for(a=0; a<hits; a++) {
1524                         dep= buffer[4*a + 1];
1525                         val= buffer[4*a + 3];
1526
1527                         if(val==MAN_TRANS_C) return MAN_TRANS_C;
1528                         else if(val==MAN_SCALE_C) return MAN_SCALE_C;
1529                         else {
1530                                 if(val & MAN_ROT_C) {
1531                                         if(minvalrot==0 || dep<mindeprot) {
1532                                                 mindeprot= dep;
1533                                                 minvalrot= val;
1534                                         }
1535                                 }
1536                                 else {
1537                                         if(minval==0 || dep<mindep) {
1538                                                 mindep= dep;
1539                                                 minval= val;
1540                                         }
1541                                 }
1542                         }
1543                 }
1544
1545                 if(minval)
1546                         return minval;
1547                 else
1548                         return minvalrot;
1549         }
1550         return 0;
1551 }
1552
1553 /* return 0; nothing happened */
1554 int BIF_do_manipulator(bContext *C, struct wmEvent *event, wmOperator *op)
1555 {
1556         ScrArea *sa= CTX_wm_area(C);
1557         View3D *v3d= sa->spacedata.first;
1558         ARegion *ar= CTX_wm_region(C);
1559         int constraint_axis[3] = {0, 0, 0};
1560         int val;
1561         int shift = event->shift;
1562
1563         if(!(v3d->twflag & V3D_USE_MANIPULATOR)) return 0;
1564         if(!(v3d->twflag & V3D_DRAW_MANIPULATOR)) return 0;
1565
1566         // find the hotspots first test narrow hotspot
1567         val= manipulator_selectbuf(sa, ar, event->mval, 0.5f*(float)U.tw_hotspot);
1568         if(val) {
1569
1570                 // drawflags still global, for drawing call above
1571                 drawflags= manipulator_selectbuf(sa, ar, event->mval, 0.2f*(float)U.tw_hotspot);
1572                 if(drawflags==0) drawflags= val;
1573
1574                 if (drawflags & MAN_TRANS_C) {
1575                         switch(drawflags) {
1576                         case MAN_TRANS_C:
1577                                 break;
1578                         case MAN_TRANS_X:
1579                                 if(shift) {
1580                                         constraint_axis[1] = 1;
1581                                         constraint_axis[2] = 1;
1582                                 }
1583                                 else
1584                                         constraint_axis[0] = 1;
1585                                 break;
1586                         case MAN_TRANS_Y:
1587                                 if(shift) {
1588                                         constraint_axis[0] = 1;
1589                                         constraint_axis[2] = 1;
1590                                 }
1591                                 else
1592                                         constraint_axis[1] = 1;
1593                                 break;
1594                         case MAN_TRANS_Z:
1595                                 if(shift) {
1596                                         constraint_axis[0] = 1;
1597                                         constraint_axis[1] = 1;
1598                                 }
1599                                 else
1600                                         constraint_axis[2] = 1;
1601                                 break;
1602                         }
1603                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1604                         WM_operator_name_call(C, "TFM_OT_translation", WM_OP_INVOKE_REGION_WIN, op->ptr);
1605                 }
1606                 else if (drawflags & MAN_SCALE_C) {
1607                         switch(drawflags) {
1608                         case MAN_SCALE_X:
1609                                 if(shift) {
1610                                         constraint_axis[1] = 1;
1611                                         constraint_axis[2] = 1;
1612                                 }
1613                                 else
1614                                         constraint_axis[0] = 1;
1615                                 break;
1616                         case MAN_SCALE_Y:
1617                                 if(shift) {
1618                                         constraint_axis[0] = 1;
1619                                         constraint_axis[2] = 1;
1620                                 }
1621                                 else
1622                                         constraint_axis[1] = 1;
1623                                 break;
1624                         case MAN_SCALE_Z:
1625                                 if(shift) {
1626                                         constraint_axis[0] = 1;
1627                                         constraint_axis[1] = 1;
1628                                 }
1629                                 else
1630                                         constraint_axis[2] = 1;
1631                                 break;
1632                         }
1633                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1634                         WM_operator_name_call(C, "TFM_OT_resize", WM_OP_INVOKE_REGION_WIN, op->ptr);
1635                 }
1636                 else if (drawflags == MAN_ROT_T) { /* trackball need special case, init is different */
1637                         WM_operator_name_call(C, "TFM_OT_trackball", WM_OP_INVOKE_REGION_WIN, op->ptr);
1638                 }
1639                 else if (drawflags & MAN_ROT_C) {
1640                         switch(drawflags) {
1641                         case MAN_ROT_X:
1642                                 constraint_axis[0] = 1;
1643                                 break;
1644                         case MAN_ROT_Y:
1645                                 constraint_axis[1] = 1;
1646                                 break;
1647                         case MAN_ROT_Z:
1648                                 constraint_axis[2] = 1;
1649                                 break;
1650                         }
1651                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1652                         WM_operator_name_call(C, "TFM_OT_rotation", WM_OP_INVOKE_REGION_WIN, op->ptr);
1653                 }
1654         }
1655         /* after transform, restore drawflags */
1656         drawflags= 0xFFFF;
1657
1658         return val;
1659 }
1660