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