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