merge with trunk/2.5 at r24378
[blender-staging.git] / source / blender / editors / transform / transform_manipulator.c
1 /**
2 * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2005 Blender Foundation
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33
34 #ifndef WIN32
35 #include <unistd.h>
36 #else
37 #include <io.h>
38 #endif
39
40 #include "MEM_guardedalloc.h"
41
42 #include "DNA_armature_types.h"
43 #include "DNA_action_types.h"
44 #include "DNA_curve_types.h"
45 #include "DNA_lattice_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_meta_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_particle_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_space_types.h"
53 #include "DNA_userdef_types.h"
54 #include "DNA_view3d_types.h"
55
56 #include "RNA_access.h"
57
58 #include "BKE_armature.h"
59 #include "BKE_context.h"
60 #include "BKE_global.h"
61 #include "BKE_lattice.h"
62 #include "BKE_mesh.h"
63 #include "BKE_object.h"
64 #include "BKE_particle.h"
65 #include "BKE_pointcache.h"
66 #include "BKE_utildefines.h"
67 #include "BKE_tessmesh.h"
68
69 #include "BLI_arithb.h"
70 #include "BLI_editVert.h"
71
72 #include "BIF_gl.h"
73
74 #include "WM_api.h"
75 #include "WM_types.h"
76
77 #include "ED_armature.h"
78 #include "ED_mesh.h"
79 #include "ED_particle.h"
80 #include "ED_space_api.h"
81 #include "ED_transform.h"
82 #include "ED_view3d.h"
83
84 #include "UI_resources.h"
85
86 /* local module include */
87 #include "transform.h"
88
89 /* return codes for select, and drawing flags */
90
91 #define MAN_TRANS_X             1
92 #define MAN_TRANS_Y             2
93 #define MAN_TRANS_Z             4
94 #define MAN_TRANS_C             7
95
96 #define MAN_ROT_X               8
97 #define MAN_ROT_Y               16
98 #define MAN_ROT_Z               32
99 #define MAN_ROT_V               64
100 #define MAN_ROT_T               128
101 #define MAN_ROT_C               248
102
103 #define MAN_SCALE_X             256
104 #define MAN_SCALE_Y             512
105 #define MAN_SCALE_Z             1024
106 #define MAN_SCALE_C             1792
107
108 /* color codes */
109
110 #define MAN_RGB         0
111 #define MAN_GHOST       1
112 #define MAN_MOVECOL     2
113
114
115 static int is_mat4_flipped(float mat[][4])
116 {
117         float vec[3];
118
119         Crossf(vec, mat[0], mat[1]);
120         if( Inpf(vec, mat[2]) < 0.0 ) return 1;
121         return 0;
122 }
123
124 /* transform widget center calc helper for below */
125 static void calc_tw_center(Scene *scene, float *co)
126 {
127         float *twcent= scene->twcent;
128         float *min= scene->twmin;
129         float *max= scene->twmax;
130
131         DO_MINMAX(co, min, max);
132         VecAddf(twcent, twcent, co);
133 }
134
135 static void protectflag_to_drawflags(short protectflag, short *drawflags)
136 {
137         if(protectflag & OB_LOCK_LOCX)
138                 *drawflags &= ~MAN_TRANS_X;
139         if(protectflag & OB_LOCK_LOCY)
140                 *drawflags &= ~MAN_TRANS_Y;
141         if(protectflag & OB_LOCK_LOCZ)
142                 *drawflags &= ~MAN_TRANS_Z;
143
144         if(protectflag & OB_LOCK_ROTX)
145                 *drawflags &= ~MAN_ROT_X;
146         if(protectflag & OB_LOCK_ROTY)
147                 *drawflags &= ~MAN_ROT_Y;
148         if(protectflag & OB_LOCK_ROTZ)
149                 *drawflags &= ~MAN_ROT_Z;
150
151         if(protectflag & OB_LOCK_SCALEX)
152                 *drawflags &= ~MAN_SCALE_X;
153         if(protectflag & OB_LOCK_SCALEY)
154                 *drawflags &= ~MAN_SCALE_Y;
155         if(protectflag & OB_LOCK_SCALEZ)
156                 *drawflags &= ~MAN_SCALE_Z;
157 }
158
159 /* for pose mode */
160 static void stats_pose(Scene *scene, View3D *v3d, bPoseChannel *pchan)
161 {
162         Bone *bone= pchan->bone;
163
164         if(bone) {
165                 if (bone->flag & BONE_TRANSFORM) {
166                         calc_tw_center(scene, pchan->pose_head);
167                         protectflag_to_drawflags(pchan->protectflag, &v3d->twdrawflag);
168                 }
169         }
170 }
171
172 /* for editmode*/
173 static void stats_editbone(View3D *v3d, EditBone *ebo)
174 {
175         if (ebo->flag & BONE_EDITMODE_LOCKED)
176                 protectflag_to_drawflags(OB_LOCK_LOC|OB_LOCK_ROT|OB_LOCK_SCALE, &v3d->twdrawflag);
177 }
178
179
180 static int test_rotmode_euler(short rotmode)
181 {
182         return (ELEM(rotmode, ROT_MODE_AXISANGLE, ROT_MODE_QUAT)) ? 0:1;
183 }
184
185 void gimbal_axis(Object *ob, float gmat[][3])
186 {
187         if(ob->mode & OB_MODE_POSE)
188         {
189                 bPoseChannel *pchan= NULL;
190
191                 /* use channels to get stats */
192                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
193                         if (pchan->bone && pchan->bone->flag & BONE_ACTIVE) {
194                                 if(test_rotmode_euler(pchan->rotmode)) {
195                                         break;
196                                 }
197                         }
198                 }
199
200                 if(pchan) {
201                         float mat[3][3], tmat[3][3], obmat[3][3];
202
203                         EulToGimbalAxis(mat, pchan->eul, pchan->rotmode);
204
205                         /* apply bone transformation */
206                         Mat3MulMat3(tmat, pchan->bone->bone_mat, mat);
207                         
208                         if (pchan->parent)
209                         {
210                                 float parent_mat[3][3];
211
212                                 Mat3CpyMat4(parent_mat, pchan->parent->pose_mat);
213                                 Mat3MulMat3(mat, parent_mat, tmat);
214
215                                 /* needed if object transformation isn't identity */
216                                 Mat3CpyMat4(obmat, ob->obmat);
217                                 Mat3MulMat3(gmat, obmat, mat);
218                         }
219                         else
220                         {
221                                 /* needed if object transformation isn't identity */
222                                 Mat3CpyMat4(obmat, ob->obmat);
223                                 Mat3MulMat3(gmat, obmat, tmat);
224                         }
225
226                         Mat3Ortho(gmat);
227                 }
228         }
229         else {
230                 if(test_rotmode_euler(ob->rotmode)) {
231                         
232                         
233                         if (ob->parent)
234                         {
235                                 float parent_mat[3][3], amat[3][3];
236                                 
237                                 EulToGimbalAxis(amat, ob->rot, ob->rotmode);
238                                 Mat3CpyMat4(parent_mat, ob->parent->obmat);
239                                 Mat3Ortho(parent_mat);
240                                 Mat3MulMat3(gmat, parent_mat, amat);
241                         }
242                         else
243                         {
244                                 EulToGimbalAxis(gmat, ob->rot, ob->rotmode);
245                         }
246                 }
247         }
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         Mat4One(rv3d->twmat);
267
268         v3d->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(v3d, 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                         VecMulf(scene->twcent, 1.0f/(float)totsel);     // centroid!
410                         Mat4MulVecfl(obedit->obmat, scene->twcent);
411                         Mat4MulVecfl(obedit->obmat, scene->twmin);
412                         Mat4MulVecfl(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, v3d, pchan);
427                         }
428
429                         VecMulf(scene->twcent, 1.0f/(float)totsel);     // centroid!
430                         Mat4MulVecfl(ob->obmat, scene->twcent);
431                         Mat4MulVecfl(ob->obmat, scene->twmin);
432                         Mat4MulVecfl(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                                 VecMulf(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, &v3d->twdrawflag);
474                                 totsel++;
475                         }
476                 }
477
478                 /* selection center */
479                 if(totsel) {
480                         VecMulf(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                         Mat3One(mat);
496                         gimbal_axis(ob, mat);
497                         Mat4CpyMat3(rv3d->twmat, mat);
498                                                 break;
499                                                 }
500                 case V3D_MANIP_NORMAL:
501                         if(obedit || ob->mode & OB_MODE_POSE) {
502                                 getTransformOrientationMatrix(C, rv3d->twmat, (v3d->around == V3D_ACTIVE));
503                                                 break;
504                                                 }
505                         /* no break we define 'normal' as 'local' in Object mode */
506                 case V3D_MANIP_LOCAL:
507                         Mat4CpyMat4(rv3d->twmat, ob->obmat);
508                         Mat4Ortho(rv3d->twmat);
509                         break;
510
511                 case V3D_MANIP_VIEW:
512                         {
513                                 float mat[3][3];
514                                 Mat3CpyMat4(mat, rv3d->viewinv);
515                                 Mat3Ortho(mat);
516                                 Mat4CpyMat3(rv3d->twmat, mat);
517                         }
518                         break;
519                 default: /* V3D_MANIP_CUSTOM */
520                         {
521                                 float mat[3][3];
522                                 applyTransformOrientation(C, mat, NULL);
523                                 Mat4CpyMat3(rv3d->twmat, mat);
524                                 break;
525                         }
526                 }
527
528         }
529
530         return totsel;
531 }
532
533 /* ******************** DRAWING STUFFIES *********** */
534
535 static float screen_aligned(RegionView3D *rv3d, float mat[][4])
536 {
537         float vec[3], size;
538
539         VECCOPY(vec, mat[0]);
540         size= Normalize(vec);
541
542         glTranslatef(mat[3][0], mat[3][1], mat[3][2]);
543
544         /* sets view screen aligned */
545         glRotatef( -360.0f*saacos(rv3d->viewquat[0])/(float)M_PI, rv3d->viewquat[1], rv3d->viewquat[2], rv3d->viewquat[3]);
546
547         return size;
548 }
549
550
551 /* radring = radius of donut rings
552    radhole = radius hole
553    start = starting segment (based on nrings)
554    end   = end segment
555    nsides = amount of points in ring
556    nrigns = amount of rings
557 */
558 static void partial_donut(float radring, float radhole, int start, int end, int nsides, int nrings)
559 {
560         float theta, phi, theta1;
561         float cos_theta, sin_theta;
562         float cos_theta1, sin_theta1;
563         float ring_delta, side_delta;
564         int i, j, docaps= 1;
565
566         if(start==0 && end==nrings) docaps= 0;
567
568         ring_delta= 2.0f*(float)M_PI/(float)nrings;
569         side_delta= 2.0f*(float)M_PI/(float)nsides;
570
571         theta= (float)M_PI+0.5f*ring_delta;
572         cos_theta= (float)cos(theta);
573         sin_theta= (float)sin(theta);
574
575         for(i= nrings - 1; i >= 0; i--) {
576                 theta1= theta + ring_delta;
577                 cos_theta1= (float)cos(theta1);
578                 sin_theta1= (float)sin(theta1);
579
580                 if(docaps && i==start) {        // cap
581                         glBegin(GL_POLYGON);
582                         phi= 0.0;
583                         for(j= nsides; j >= 0; j--) {
584                                 float cos_phi, sin_phi, dist;
585
586                                 phi += side_delta;
587                                 cos_phi= (float)cos(phi);
588                                 sin_phi= (float)sin(phi);
589                                 dist= radhole + radring * cos_phi;
590
591                                 glVertex3f(cos_theta1 * dist, -sin_theta1 * dist,  radring * sin_phi);
592                         }
593                         glEnd();
594                 }
595                 if(i>=start && i<=end) {
596                         glBegin(GL_QUAD_STRIP);
597                         phi= 0.0;
598                         for(j= nsides; j >= 0; j--) {
599                                 float cos_phi, sin_phi, dist;
600
601                                 phi += side_delta;
602                                 cos_phi= (float)cos(phi);
603                                 sin_phi= (float)sin(phi);
604                                 dist= radhole + radring * cos_phi;
605
606                                 glVertex3f(cos_theta1 * dist, -sin_theta1 * dist, radring * sin_phi);
607                                 glVertex3f(cos_theta * dist, -sin_theta * dist,  radring * sin_phi);
608                         }
609                         glEnd();
610                 }
611
612                 if(docaps && i==end) {  // cap
613                         glBegin(GL_POLYGON);
614                         phi= 0.0;
615                         for(j= nsides; j >= 0; j--) {
616                                 float cos_phi, sin_phi, dist;
617
618                                 phi -= side_delta;
619                                 cos_phi= (float)cos(phi);
620                                 sin_phi= (float)sin(phi);
621                                 dist= radhole + radring * cos_phi;
622
623                                 glVertex3f(cos_theta * dist, -sin_theta * dist,  radring * sin_phi);
624                         }
625                         glEnd();
626                 }
627
628
629                 theta= theta1;
630                 cos_theta= cos_theta1;
631                 sin_theta= sin_theta1;
632         }
633 }
634
635 /* three colors can be set;
636    grey for ghosting
637    moving: in transform theme color
638    else the red/green/blue
639 */
640 static void manipulator_setcolor(View3D *v3d, char axis, int colcode)
641 {
642         float vec[4];
643         char col[4];
644
645         vec[3]= 0.7f; // alpha set on 0.5, can be glEnabled or not
646
647         if(colcode==MAN_GHOST) {
648                 glColor4ub(0, 0, 0, 70);
649         }
650         else if(colcode==MAN_MOVECOL) {
651                 UI_GetThemeColor3ubv(TH_TRANSFORM, col);
652                 glColor4ub(col[0], col[1], col[2], 128);
653         }
654         else {
655                 switch(axis) {
656                 case 'c':
657                         UI_GetThemeColor3ubv(TH_TRANSFORM, col);
658                         if(v3d->twmode == V3D_MANIP_LOCAL) {
659                                 col[0]= col[0]>200?255:col[0]+55;
660                                 col[1]= col[1]>200?255:col[1]+55;
661                                 col[2]= col[2]>200?255:col[2]+55;
662                         }
663                         else if(v3d->twmode == V3D_MANIP_NORMAL) {
664                                 col[0]= col[0]<55?0:col[0]-55;
665                                 col[1]= col[1]<55?0:col[1]-55;
666                                 col[2]= col[2]<55?0:col[2]-55;
667                         }
668                         glColor4ub(col[0], col[1], col[2], 128);
669                         break;
670                 case 'x':
671                         glColor4ub(220, 0, 0, 128);
672                         break;
673                 case 'y':
674                         glColor4ub(0, 220, 0, 128);
675                         break;
676                 case 'z':
677                         glColor4ub(30, 30, 220, 128);
678                         break;
679                 }
680         }
681 }
682
683 /* viewmatrix should have been set OK, also no shademode! */
684 static void draw_manipulator_axes(View3D *v3d, int colcode, int flagx, int flagy, int flagz)
685 {
686
687         /* axes */
688         if(flagx) {
689                 manipulator_setcolor(v3d, 'x', colcode);
690                 if(flagx & MAN_SCALE_X) glLoadName(MAN_SCALE_X);
691                 else if(flagx & MAN_TRANS_X) glLoadName(MAN_TRANS_X);
692                 glBegin(GL_LINES);
693                 glVertex3f(0.2f, 0.0f, 0.0f);
694                 glVertex3f(1.0f, 0.0f, 0.0f);
695                 glEnd();
696         }
697         if(flagy) {
698                 if(flagy & MAN_SCALE_Y) glLoadName(MAN_SCALE_Y);
699                 else if(flagy & MAN_TRANS_Y) glLoadName(MAN_TRANS_Y);
700                 manipulator_setcolor(v3d, 'y', colcode);
701                 glBegin(GL_LINES);
702                 glVertex3f(0.0f, 0.2f, 0.0f);
703                 glVertex3f(0.0f, 1.0f, 0.0f);
704                 glEnd();
705         }
706         if(flagz) {
707                 if(flagz & MAN_SCALE_Z) glLoadName(MAN_SCALE_Z);
708                 else if(flagz & MAN_TRANS_Z) glLoadName(MAN_TRANS_Z);
709                 manipulator_setcolor(v3d, 'z', colcode);
710                 glBegin(GL_LINES);
711                 glVertex3f(0.0f, 0.0f, 0.2f);
712                 glVertex3f(0.0f, 0.0f, 1.0f);
713                 glEnd();
714         }
715 }
716
717 static void preOrtho(int ortho, float twmat[][4], int axis)
718 {
719         if (ortho == 0) {
720                 float omat[4][4];
721                 Mat4CpyMat4(omat, twmat);
722                 Mat4Orthogonal(omat, axis);
723                 glPushMatrix();
724                 wmMultMatrix(omat);
725         }
726 }
727
728 static void preOrthoFront(int ortho, float twmat[][4], int axis)
729 {
730         if (ortho == 0) {
731                 float omat[4][4];
732                 Mat4CpyMat4(omat, twmat);
733                 Mat4Orthogonal(omat, axis);
734                 glPushMatrix();
735                 wmMultMatrix(omat);
736                 glFrontFace( is_mat4_flipped(omat)?GL_CW:GL_CCW);
737         }
738 }
739
740 static void postOrtho(int ortho)
741 {
742         if (ortho == 0) {
743                 glPopMatrix();
744         }
745 }
746
747 /* only called while G.moving */
748 static void draw_manipulator_rotate_ghost(View3D *v3d, RegionView3D *rv3d, int drawflags)
749 {
750         GLUquadricObj *qobj;
751         float size, phi, startphi, vec[3], svec[3], matt[4][4], cross[3], tmat[3][3];
752         int arcs= (G.rt!=2);
753         int ortho;
754
755         glDisable(GL_DEPTH_TEST);
756
757         qobj= gluNewQuadric();
758         gluQuadricDrawStyle(qobj, GLU_FILL);
759
760         glColor4ub(0,0,0,64);
761         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
762         glEnable(GL_BLEND);
763
764         /* we need both [4][4] transforms, t->mat seems to be premul, not post for mat[][4] */
765         Mat4CpyMat4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
766 // XXX  Mat4MulMat34(matt, t->mat, rv3d->twmat);
767
768         /* Screen aligned view rot circle */
769         if(drawflags & MAN_ROT_V) {
770
771                 /* prepare for screen aligned draw */
772                 glPushMatrix();
773                 size= screen_aligned(rv3d, rv3d->twmat);
774
775                 vec[0]= 0; // XXX (float)(t->con.imval[0] - t->center2d[0]);
776                 vec[1]= 0; // XXX (float)(t->con.imval[1] - t->center2d[1]);
777                 vec[2]= 0.0f;
778                 Normalize(vec);
779
780                 startphi= saacos( vec[1] );
781                 if(vec[0]<0.0) startphi= -startphi;
782
783                 phi= 0; // XXX (float)fmod(180.0*t->val/M_PI, 360.0);
784                 if(phi > 180.0) phi-= 360.0;
785                 else if(phi<-180.0) phi+= 360.0;
786
787                 gluPartialDisk(qobj, 0.0, size, 32, 1, 180.0*startphi/M_PI, phi);
788
789                 glPopMatrix();
790         }
791         else if(arcs) {
792                 float imat[3][3], ivmat[3][3];
793                 /* try to get the start rotation */
794
795                 svec[0]= 0; // XXX (float)(t->con.imval[0] - t->center2d[0]);
796                 svec[1]= 0; // XXX (float)(t->con.imval[1] - t->center2d[1]);
797                 svec[2]= 0.0f;
798
799                 /* screen aligned vec transform back to manipulator space */
800                 Mat3CpyMat4(ivmat, rv3d->viewinv);
801                 Mat3CpyMat4(tmat, rv3d->twmat);
802                 Mat3Inv(imat, tmat);
803                 Mat3MulMat3(tmat, imat, ivmat);
804
805                 Mat3MulVecfl(tmat, svec);       // tmat is used further on
806                 Normalize(svec);
807         }
808         
809         ortho = IsMat4Orthogonal(rv3d->twmat);
810
811         if (ortho) {
812         wmMultMatrix(rv3d->twmat);      // aligns with original widget
813         }
814         
815
816         /* Z disk */
817         if(drawflags & MAN_ROT_Z) {
818                 preOrtho(ortho, rv3d->twmat, 2);
819                 
820                 if(arcs) {
821                         /* correct for squeezed arc */
822                         svec[0]+= tmat[2][0];
823                         svec[1]+= tmat[2][1];
824                         Normalize(svec);
825
826                         startphi= (float)atan2(svec[0], svec[1]);
827                 }
828                 else startphi= 0.5f*(float)M_PI;
829
830                 VECCOPY(vec, rv3d->twmat[0]);   // use x axis to detect rotation
831                 Normalize(vec);
832                 Normalize(matt[0]);
833                 phi= saacos( Inpf(vec, matt[0]) );
834                 if(phi!=0.0) {
835                         Crossf(cross, vec, matt[0]);    // results in z vector
836                         if(Inpf(cross, rv3d->twmat[2]) > 0.0) phi= -phi;
837                         gluPartialDisk(qobj, 0.0, 1.0, 32, 1, 180.0*startphi/M_PI, 180.0*(phi)/M_PI);
838                 }
839
840                 postOrtho(ortho);
841         }
842         /* X disk */
843         if(drawflags & MAN_ROT_X) {
844                 preOrtho(ortho, rv3d->twmat, 0);
845                 
846                 if(arcs) {
847                         /* correct for squeezed arc */
848                         svec[1]+= tmat[2][1];
849                         svec[2]+= tmat[2][2];
850                         Normalize(svec);
851
852                         startphi= (float)(M_PI + atan2(svec[2], -svec[1]));
853                 }
854                 else startphi= 0.0f;
855
856                 VECCOPY(vec, rv3d->twmat[1]);   // use y axis to detect rotation
857                 Normalize(vec);
858                 Normalize(matt[1]);
859                 phi= saacos( Inpf(vec, matt[1]) );
860                 if(phi!=0.0) {
861                         Crossf(cross, vec, matt[1]);    // results in x vector
862                         if(Inpf(cross, rv3d->twmat[0]) > 0.0) phi= -phi;
863                         glRotatef(90.0, 0.0, 1.0, 0.0);
864                         gluPartialDisk(qobj, 0.0, 1.0, 32, 1, 180.0*startphi/M_PI, 180.0*phi/M_PI);
865                         glRotatef(-90.0, 0.0, 1.0, 0.0);
866                 }
867
868                 postOrtho(ortho);
869         }
870         /* Y circle */
871         if(drawflags & MAN_ROT_Y) {
872                 preOrtho(ortho, rv3d->twmat, 1);
873                 
874                 if(arcs) {
875                         /* correct for squeezed arc */
876                         svec[0]+= tmat[2][0];
877                         svec[2]+= tmat[2][2];
878                         Normalize(svec);
879
880                         startphi= (float)(M_PI + atan2(-svec[0], svec[2]));
881                 }
882                 else startphi= (float)M_PI;
883
884                 VECCOPY(vec, rv3d->twmat[2]);   // use z axis to detect rotation
885                 Normalize(vec);
886                 Normalize(matt[2]);
887                 phi= saacos( Inpf(vec, matt[2]) );
888                 if(phi!=0.0) {
889                         Crossf(cross, vec, matt[2]);    // results in y vector
890                         if(Inpf(cross, rv3d->twmat[1]) > 0.0) phi= -phi;
891                         glRotatef(-90.0, 1.0, 0.0, 0.0);
892                         gluPartialDisk(qobj, 0.0, 1.0, 32, 1, 180.0*startphi/M_PI, 180.0*phi/M_PI);
893                         glRotatef(90.0, 1.0, 0.0, 0.0);
894                 }
895
896                 postOrtho(ortho);
897         }
898
899         glDisable(GL_BLEND);
900         wmLoadMatrix(rv3d->viewmat);
901 }
902
903 static void draw_manipulator_rotate(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo)
904 {
905         GLUquadricObj *qobj;
906         double plane[4];
907         float matt[4][4];
908         float size, vec[3], unitmat[4][4];
909         float cywid= 0.33f*0.01f*(float)U.tw_handlesize;
910         float cusize= cywid*0.65f;
911         int arcs= (G.rt!=2);
912         int colcode;
913         int ortho;
914
915         if(moving) colcode= MAN_MOVECOL;
916         else colcode= MAN_RGB;
917
918         /* when called while moving in mixed mode, do not draw when... */
919         if((drawflags & MAN_ROT_C)==0) return;
920
921         /* Init stuff */
922         glDisable(GL_DEPTH_TEST);
923         Mat4One(unitmat);
924
925         qobj= gluNewQuadric();
926         gluQuadricDrawStyle(qobj, GLU_FILL);
927
928         /* prepare for screen aligned draw */
929         VECCOPY(vec, rv3d->twmat[0]);
930         size= Normalize(vec);
931         glPushMatrix();
932         glTranslatef(rv3d->twmat[3][0], rv3d->twmat[3][1], rv3d->twmat[3][2]);
933
934         if(arcs) {
935                 /* clipplane makes nice handles, calc here because of multmatrix but with translate! */
936                 VECCOPY(plane, rv3d->viewinv[2]);
937                 plane[3]= -0.02*size; // clip just a bit more
938                 glClipPlane(GL_CLIP_PLANE0, plane);
939         }
940         /* sets view screen aligned */
941         glRotatef( -360.0f*saacos(rv3d->viewquat[0])/(float)M_PI, rv3d->viewquat[1], rv3d->viewquat[2], rv3d->viewquat[3]);
942
943         /* Screen aligned help circle */
944         if(arcs) {
945                 if((G.f & G_PICKSEL)==0) {
946                         UI_ThemeColorShade(TH_BACK, -30);
947                         drawcircball(GL_LINE_LOOP, unitmat[3], size, unitmat);
948                 }
949         }
950
951         /* Screen aligned trackball rot circle */
952         if(drawflags & MAN_ROT_T) {
953                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_T);
954
955                 UI_ThemeColor(TH_TRANSFORM);
956                 drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
957         }
958
959         /* Screen aligned view rot circle */
960         if(drawflags & MAN_ROT_V) {
961                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_V);
962                 UI_ThemeColor(TH_TRANSFORM);
963                 drawcircball(GL_LINE_LOOP, unitmat[3], 1.2f*size, unitmat);
964
965                 if(moving) {
966                         float vec[3];
967                         vec[0]= 0; // XXX (float)(t->imval[0] - t->center2d[0]);
968                         vec[1]= 0; // XXX (float)(t->imval[1] - t->center2d[1]);
969                         vec[2]= 0.0f;
970                         Normalize(vec);
971                         VecMulf(vec, 1.2f*size);
972                         glBegin(GL_LINES);
973                         glVertex3f(0.0f, 0.0f, 0.0f);
974                         glVertex3fv(vec);
975                         glEnd();
976                 }
977         }
978         glPopMatrix();
979
980
981         ortho = IsMat4Orthogonal(rv3d->twmat);
982         
983         /* apply the transform delta */
984         if(moving) {
985                 Mat4CpyMat4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
986                 // XXX Mat4MulMat34(matt, t->mat, rv3d->twmat);
987                 if (ortho) {
988                 wmMultMatrix(matt);
989                 glFrontFace( is_mat4_flipped(matt)?GL_CW:GL_CCW);
990         }
991         }
992         else {
993                 if (ortho) {
994                 glFrontFace( is_mat4_flipped(rv3d->twmat)?GL_CW:GL_CCW);
995                 wmMultMatrix(rv3d->twmat);
996         }
997         }
998
999         /* axes */
1000         if(arcs==0) {
1001                 if(!(G.f & G_PICKSEL)) {
1002                         if( (combo & V3D_MANIP_SCALE)==0) {
1003                                 /* axis */
1004                                 if( (drawflags & MAN_ROT_X) || (moving && (drawflags & MAN_ROT_Z)) ) {
1005                                         preOrthoFront(ortho, rv3d->twmat, 2);
1006                                         manipulator_setcolor(v3d, 'x', colcode);
1007                                         glBegin(GL_LINES);
1008                                         glVertex3f(0.2f, 0.0f, 0.0f);
1009                                         glVertex3f(1.0f, 0.0f, 0.0f);
1010                                         glEnd();
1011                                         postOrtho(ortho);
1012                                 }
1013                                 if( (drawflags & MAN_ROT_Y) || (moving && (drawflags & MAN_ROT_X)) ) {
1014                                         preOrthoFront(ortho, rv3d->twmat, 0);
1015                                         manipulator_setcolor(v3d, 'y', colcode);
1016                                         glBegin(GL_LINES);
1017                                         glVertex3f(0.0f, 0.2f, 0.0f);
1018                                         glVertex3f(0.0f, 1.0f, 0.0f);
1019                                         glEnd();
1020                                         postOrtho(ortho);
1021                                 }
1022                                 if( (drawflags & MAN_ROT_Z) || (moving && (drawflags & MAN_ROT_Y)) ) {
1023                                         preOrthoFront(ortho, rv3d->twmat, 1);
1024                                         manipulator_setcolor(v3d, 'z', colcode);
1025                                         glBegin(GL_LINES);
1026                                         glVertex3f(0.0f, 0.0f, 0.2f);
1027                                         glVertex3f(0.0f, 0.0f, 1.0f);
1028                                 glEnd();
1029                                         postOrtho(ortho);
1030                         }
1031                 }
1032         }
1033         }
1034
1035         if(arcs==0 && moving) {
1036
1037                 /* Z circle */
1038                 if(drawflags & MAN_ROT_Z) {
1039                         preOrthoFront(ortho, matt, 2);
1040                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1041                         manipulator_setcolor(v3d, 'z', colcode);
1042                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
1043                         postOrtho(ortho);
1044                 }
1045                 /* X circle */
1046                 if(drawflags & MAN_ROT_X) {
1047                         preOrthoFront(ortho, matt, 0);
1048                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1049                         glRotatef(90.0, 0.0, 1.0, 0.0);
1050                         manipulator_setcolor(v3d, 'x', colcode);
1051                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
1052                         glRotatef(-90.0, 0.0, 1.0, 0.0);
1053                         postOrtho(ortho);
1054                 }
1055                 /* Y circle */
1056                 if(drawflags & MAN_ROT_Y) {
1057                         preOrthoFront(ortho, matt, 1);
1058                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1059                         glRotatef(-90.0, 1.0, 0.0, 0.0);
1060                         manipulator_setcolor(v3d, 'y', colcode);
1061                         drawcircball(GL_LINE_LOOP, unitmat[3], 1.0, unitmat);
1062                         glRotatef(90.0, 1.0, 0.0, 0.0);
1063                         postOrtho(ortho);
1064                 }
1065
1066                 if(arcs) glDisable(GL_CLIP_PLANE0);
1067         }
1068         // donut arcs
1069         if(arcs) {
1070                 glEnable(GL_CLIP_PLANE0);
1071
1072                 /* Z circle */
1073                 if(drawflags & MAN_ROT_Z) {
1074                         preOrthoFront(ortho, rv3d->twmat, 2);
1075                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1076                         manipulator_setcolor(v3d, 'z', colcode);
1077                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
1078                         postOrtho(ortho);
1079                 }
1080                 /* X circle */
1081                 if(drawflags & MAN_ROT_X) {
1082                         preOrthoFront(ortho, rv3d->twmat, 0);
1083                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1084                         glRotatef(90.0, 0.0, 1.0, 0.0);
1085                         manipulator_setcolor(v3d, 'x', colcode);
1086                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
1087                         glRotatef(-90.0, 0.0, 1.0, 0.0);
1088                         postOrtho(ortho);
1089                 }
1090                 /* Y circle */
1091                 if(drawflags & MAN_ROT_Y) {
1092                         preOrthoFront(ortho, rv3d->twmat, 1);
1093                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1094                         glRotatef(-90.0, 1.0, 0.0, 0.0);
1095                         manipulator_setcolor(v3d, 'y', colcode);
1096                         partial_donut(cusize/4.0f, 1.0f, 0, 48, 8, 48);
1097                         glRotatef(90.0, 1.0, 0.0, 0.0);
1098                         postOrtho(ortho);
1099                 }
1100
1101                 glDisable(GL_CLIP_PLANE0);
1102         }
1103
1104         if(arcs==0) {
1105
1106                 /* Z handle on X axis */
1107                 if(drawflags & MAN_ROT_Z) {
1108                         preOrthoFront(ortho, rv3d->twmat, 2);
1109                         glPushMatrix();
1110                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1111                         manipulator_setcolor(v3d, 'z', colcode);
1112
1113                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1114
1115                         glPopMatrix();
1116                         postOrtho(ortho);
1117                 }
1118
1119                 /* Y handle on X axis */
1120                 if(drawflags & MAN_ROT_Y) {
1121                         preOrthoFront(ortho, rv3d->twmat, 1);
1122                         glPushMatrix();
1123                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1124                         manipulator_setcolor(v3d, 'y', colcode);
1125
1126                         glRotatef(90.0, 1.0, 0.0, 0.0);
1127                         glRotatef(90.0, 0.0, 0.0, 1.0);
1128                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1129
1130                         glPopMatrix();
1131                         postOrtho(ortho);
1132                 }
1133
1134                 /* X handle on Z axis */
1135                 if(drawflags & MAN_ROT_X) {
1136                         preOrthoFront(ortho, rv3d->twmat, 0);
1137                         glPushMatrix();
1138                         if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1139                         manipulator_setcolor(v3d, 'x', colcode);
1140
1141                         glRotatef(-90.0, 0.0, 1.0, 0.0);
1142                         glRotatef(90.0, 0.0, 0.0, 1.0);
1143                         partial_donut(0.7f*cusize, 1.0f, 31, 33, 8, 64);
1144
1145                         glPopMatrix();
1146                         postOrtho(ortho);
1147                 }
1148
1149         }
1150
1151         /* restore */
1152         wmLoadMatrix(rv3d->viewmat);
1153         gluDeleteQuadric(qobj);
1154         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1155
1156 }
1157
1158 static void drawsolidcube(float size)
1159 {
1160         static float cube[8][3] = {
1161         {-1.0, -1.0, -1.0},
1162         {-1.0, -1.0,  1.0},
1163         {-1.0,  1.0,  1.0},
1164         {-1.0,  1.0, -1.0},
1165         { 1.0, -1.0, -1.0},
1166         { 1.0, -1.0,  1.0},
1167         { 1.0,  1.0,  1.0},
1168         { 1.0,  1.0, -1.0},     };
1169         float n[3];
1170
1171         glPushMatrix();
1172         glScalef(size, size, size);
1173
1174         n[0]=0; n[1]=0; n[2]=0;
1175         glBegin(GL_QUADS);
1176         n[0]= -1.0;
1177         glNormal3fv(n);
1178         glVertex3fv(cube[0]); glVertex3fv(cube[1]); glVertex3fv(cube[2]); glVertex3fv(cube[3]);
1179         n[0]=0;
1180         glEnd();
1181
1182         glBegin(GL_QUADS);
1183         n[1]= -1.0;
1184         glNormal3fv(n);
1185         glVertex3fv(cube[0]); glVertex3fv(cube[4]); glVertex3fv(cube[5]); glVertex3fv(cube[1]);
1186         n[1]=0;
1187         glEnd();
1188
1189         glBegin(GL_QUADS);
1190         n[0]= 1.0;
1191         glNormal3fv(n);
1192         glVertex3fv(cube[4]); glVertex3fv(cube[7]); glVertex3fv(cube[6]); glVertex3fv(cube[5]);
1193         n[0]=0;
1194         glEnd();
1195
1196         glBegin(GL_QUADS);
1197         n[1]= 1.0;
1198         glNormal3fv(n);
1199         glVertex3fv(cube[7]); glVertex3fv(cube[3]); glVertex3fv(cube[2]); glVertex3fv(cube[6]);
1200         n[1]=0;
1201         glEnd();
1202
1203         glBegin(GL_QUADS);
1204         n[2]= 1.0;
1205         glNormal3fv(n);
1206         glVertex3fv(cube[1]); glVertex3fv(cube[5]); glVertex3fv(cube[6]); glVertex3fv(cube[2]);
1207         n[2]=0;
1208         glEnd();
1209
1210         glBegin(GL_QUADS);
1211         n[2]= -1.0;
1212         glNormal3fv(n);
1213         glVertex3fv(cube[7]); glVertex3fv(cube[4]); glVertex3fv(cube[0]); glVertex3fv(cube[3]);
1214         glEnd();
1215
1216         glPopMatrix();
1217 }
1218
1219
1220 static void draw_manipulator_scale(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1221 {
1222         float cywid= 0.25f*0.01f*(float)U.tw_handlesize;
1223         float cusize= cywid*0.75f, dz;
1224
1225         /* when called while moving in mixed mode, do not draw when... */
1226         if((drawflags & MAN_SCALE_C)==0) return;
1227
1228         glDisable(GL_DEPTH_TEST);
1229
1230         /* not in combo mode */
1231         if( (combo & (V3D_MANIP_TRANSLATE|V3D_MANIP_ROTATE))==0) {
1232                 float size, unitmat[4][4];
1233                 int shift= 0; // XXX
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_SCALE_C);
1237
1238                 manipulator_setcolor(v3d, 'c', colcode);
1239                 glPushMatrix();
1240                 size= screen_aligned(rv3d, rv3d->twmat);
1241                 Mat4One(unitmat);
1242                 drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
1243                 glPopMatrix();
1244
1245                 dz= 1.0;
1246         }
1247         else dz= 1.0f-4.0f*cusize;
1248
1249         if(moving) {
1250                 float matt[4][4];
1251
1252                 Mat4CpyMat4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
1253                 // XXX Mat4MulMat34(matt, t->mat, rv3d->twmat);
1254                 wmMultMatrix(matt);
1255                 glFrontFace( is_mat4_flipped(matt)?GL_CW:GL_CCW);
1256         }
1257         else {
1258                 wmMultMatrix(rv3d->twmat);
1259                 glFrontFace( is_mat4_flipped(rv3d->twmat)?GL_CW:GL_CCW);
1260         }
1261
1262         /* axis */
1263
1264         /* in combo mode, this is always drawn as first type */
1265         draw_manipulator_axes(v3d, colcode, drawflags & MAN_SCALE_X, drawflags & MAN_SCALE_Y, drawflags & MAN_SCALE_Z);
1266
1267         /* Z cube */
1268         glTranslatef(0.0, 0.0, dz);
1269         if(drawflags & MAN_SCALE_Z) {
1270                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_Z);
1271                 manipulator_setcolor(v3d, 'z', colcode);
1272                 drawsolidcube(cusize);
1273         }
1274         /* X cube */
1275         glTranslatef(dz, 0.0, -dz);
1276         if(drawflags & MAN_SCALE_X) {
1277                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_X);
1278                 manipulator_setcolor(v3d, 'x', colcode);
1279                 drawsolidcube(cusize);
1280         }
1281         /* Y cube */
1282         glTranslatef(-dz, dz, 0.0);
1283         if(drawflags & MAN_SCALE_Y) {
1284                 if(G.f & G_PICKSEL) glLoadName(MAN_SCALE_Y);
1285                 manipulator_setcolor(v3d, 'y', colcode);
1286                 drawsolidcube(cusize);
1287         }
1288
1289         /* if shiftkey, center point as last, for selectbuffer order */
1290         if(G.f & G_PICKSEL) {
1291                 int shift= 0; // XXX
1292
1293                 if(shift) {
1294                         glTranslatef(0.0, -dz, 0.0);
1295                         glLoadName(MAN_SCALE_C);
1296                         glBegin(GL_POINTS);
1297                         glVertex3f(0.0, 0.0, 0.0);
1298                         glEnd();
1299                 }
1300         }
1301
1302         /* restore */
1303         wmLoadMatrix(rv3d->viewmat);
1304
1305         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1306         glFrontFace(GL_CCW);
1307 }
1308
1309
1310 static void draw_cone(GLUquadricObj *qobj, float len, float width)
1311 {
1312         glTranslatef(0.0, 0.0, -0.5f*len);
1313         gluCylinder(qobj, width, 0.0, len, 8, 1);
1314         gluQuadricOrientation(qobj, GLU_INSIDE);
1315         gluDisk(qobj, 0.0, width, 8, 1);
1316         gluQuadricOrientation(qobj, GLU_OUTSIDE);
1317         glTranslatef(0.0, 0.0, 0.5f*len);
1318 }
1319
1320 static void draw_cylinder(GLUquadricObj *qobj, float len, float width)
1321 {
1322
1323         width*= 0.8f;   // just for beauty
1324
1325         glTranslatef(0.0, 0.0, -0.5f*len);
1326         gluCylinder(qobj, width, width, len, 8, 1);
1327         gluQuadricOrientation(qobj, GLU_INSIDE);
1328         gluDisk(qobj, 0.0, width, 8, 1);
1329         gluQuadricOrientation(qobj, GLU_OUTSIDE);
1330         glTranslatef(0.0, 0.0, len);
1331         gluDisk(qobj, 0.0, width, 8, 1);
1332         glTranslatef(0.0, 0.0, -0.5f*len);
1333 }
1334
1335
1336 static void draw_manipulator_translate(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1337 {
1338         GLUquadricObj *qobj;
1339         float cylen= 0.01f*(float)U.tw_handlesize;
1340         float cywid= 0.25f*cylen, dz, size;
1341         float unitmat[4][4];
1342         int shift= 0; // XXX
1343
1344         /* when called while moving in mixed mode, do not draw when... */
1345         if((drawflags & MAN_TRANS_C)==0) return;
1346
1347         // XXX if(moving) glTranslatef(t->vec[0], t->vec[1], t->vec[2]);
1348         glDisable(GL_DEPTH_TEST);
1349
1350         qobj= gluNewQuadric();
1351         gluQuadricDrawStyle(qobj, GLU_FILL);
1352
1353         /* center circle, do not add to selection when shift is pressed (planar constraint) */
1354         if( (G.f & G_PICKSEL) && shift==0) glLoadName(MAN_TRANS_C);
1355
1356         manipulator_setcolor(v3d, 'c', colcode);
1357         glPushMatrix();
1358         size= screen_aligned(rv3d, rv3d->twmat);
1359         Mat4One(unitmat);
1360         drawcircball(GL_LINE_LOOP, unitmat[3], 0.2f*size, unitmat);
1361         glPopMatrix();
1362
1363         /* and now apply matrix, we move to local matrix drawing */
1364         wmMultMatrix(rv3d->twmat);
1365
1366         /* axis */
1367         glLoadName(-1);
1368
1369         // translate drawn as last, only axis when no combo with scale, or for ghosting
1370         if((combo & V3D_MANIP_SCALE)==0 || colcode==MAN_GHOST)
1371                 draw_manipulator_axes(v3d, colcode, drawflags & MAN_TRANS_X, drawflags & MAN_TRANS_Y, drawflags & MAN_TRANS_Z);
1372
1373
1374         /* offset in combo mode, for rotate a bit more */
1375         if(combo & (V3D_MANIP_ROTATE)) dz= 1.0f+2.0f*cylen;
1376         else if(combo & (V3D_MANIP_SCALE)) dz= 1.0f+0.5f*cylen;
1377         else dz= 1.0f;
1378
1379         /* Z Cone */
1380         glTranslatef(0.0, 0.0, dz);
1381         if(drawflags & MAN_TRANS_Z) {
1382                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_Z);
1383                 manipulator_setcolor(v3d, 'z', colcode);
1384                 draw_cone(qobj, cylen, cywid);
1385         }
1386         /* X Cone */
1387         glTranslatef(dz, 0.0, -dz);
1388         if(drawflags & MAN_TRANS_X) {
1389                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_X);
1390                 glRotatef(90.0, 0.0, 1.0, 0.0);
1391                 manipulator_setcolor(v3d, 'x', colcode);
1392                 draw_cone(qobj, cylen, cywid);
1393                 glRotatef(-90.0, 0.0, 1.0, 0.0);
1394         }
1395         /* Y Cone */
1396         glTranslatef(-dz, dz, 0.0);
1397         if(drawflags & MAN_TRANS_Y) {
1398                 if(G.f & G_PICKSEL) glLoadName(MAN_TRANS_Y);
1399                 glRotatef(-90.0, 1.0, 0.0, 0.0);
1400                 manipulator_setcolor(v3d, 'y', colcode);
1401                 draw_cone(qobj, cylen, cywid);
1402         }
1403
1404         gluDeleteQuadric(qobj);
1405         wmLoadMatrix(rv3d->viewmat);
1406
1407         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1408
1409 }
1410
1411 static void draw_manipulator_rotate_cyl(View3D *v3d, RegionView3D *rv3d, int moving, int drawflags, int combo, int colcode)
1412 {
1413         GLUquadricObj *qobj;
1414         float size;
1415         float cylen= 0.01f*(float)U.tw_handlesize;
1416         float cywid= 0.25f*cylen;
1417
1418         /* when called while moving in mixed mode, do not draw when... */
1419         if((drawflags & MAN_ROT_C)==0) return;
1420
1421         /* prepare for screen aligned draw */
1422         glPushMatrix();
1423         size= screen_aligned(rv3d, rv3d->twmat);
1424
1425         glDisable(GL_DEPTH_TEST);
1426
1427         qobj= gluNewQuadric();
1428
1429         /* Screen aligned view rot circle */
1430         if(drawflags & MAN_ROT_V) {
1431                 float unitmat[4][4];
1432                 Mat4One(unitmat);
1433
1434                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_V);
1435                 UI_ThemeColor(TH_TRANSFORM);
1436                 drawcircball(GL_LINE_LOOP, unitmat[3], 1.2f*size, unitmat);
1437
1438                 if(moving) {
1439                         float vec[3];
1440                         vec[0]= 0; // XXX (float)(t->imval[0] - t->center2d[0]);
1441                         vec[1]= 0; // XXX (float)(t->imval[1] - t->center2d[1]);
1442                         vec[2]= 0.0f;
1443                         Normalize(vec);
1444                         VecMulf(vec, 1.2f*size);
1445                         glBegin(GL_LINES);
1446                         glVertex3f(0.0, 0.0, 0.0);
1447                         glVertex3fv(vec);
1448                         glEnd();
1449                 }
1450         }
1451         glPopMatrix();
1452
1453         /* apply the transform delta */
1454         if(moving) {
1455                 float matt[4][4];
1456                 Mat4CpyMat4(matt, rv3d->twmat); // to copy the parts outside of [3][3]
1457                 // XXX          if (t->flag & T_USES_MANIPULATOR) {
1458                 // XXX                  Mat4MulMat34(matt, t->mat, rv3d->twmat);
1459                 // XXX }
1460                 wmMultMatrix(matt);
1461         }
1462         else {
1463                 wmMultMatrix(rv3d->twmat);
1464         }
1465
1466         glFrontFace( is_mat4_flipped(rv3d->twmat)?GL_CW:GL_CCW);
1467
1468         /* axis */
1469         if( (G.f & G_PICKSEL)==0 ) {
1470
1471                 // only draw axis when combo didn't draw scale axes
1472                 if((combo & V3D_MANIP_SCALE)==0)
1473                         draw_manipulator_axes(v3d, colcode, drawflags & MAN_ROT_X, drawflags & MAN_ROT_Y, drawflags & MAN_ROT_Z);
1474
1475                 /* only has to be set when not in picking */
1476                 gluQuadricDrawStyle(qobj, GLU_FILL);
1477         }
1478
1479         /* Z cyl */
1480         glTranslatef(0.0, 0.0, 1.0);
1481         if(drawflags & MAN_ROT_Z) {
1482                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Z);
1483                 manipulator_setcolor(v3d, 'z', colcode);
1484                 draw_cylinder(qobj, cylen, cywid);
1485         }
1486         /* X cyl */
1487         glTranslatef(1.0, 0.0, -1.0);
1488         if(drawflags & MAN_ROT_X) {
1489                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_X);
1490                 glRotatef(90.0, 0.0, 1.0, 0.0);
1491                 manipulator_setcolor(v3d, 'x', colcode);
1492                 draw_cylinder(qobj, cylen, cywid);
1493                 glRotatef(-90.0, 0.0, 1.0, 0.0);
1494         }
1495         /* Y cylinder */
1496         glTranslatef(-1.0, 1.0, 0.0);
1497         if(drawflags & MAN_ROT_Y) {
1498                 if(G.f & G_PICKSEL) glLoadName(MAN_ROT_Y);
1499                 glRotatef(-90.0, 1.0, 0.0, 0.0);
1500                 manipulator_setcolor(v3d, 'y', colcode);
1501                 draw_cylinder(qobj, cylen, cywid);
1502         }
1503
1504         /* restore */
1505
1506         gluDeleteQuadric(qobj);
1507         wmLoadMatrix(rv3d->viewmat);
1508
1509         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1510
1511 }
1512
1513
1514 /* ********************************************* */
1515
1516 static float get_manipulator_drawsize(ARegion *ar)
1517 {
1518         RegionView3D *rv3d= ar->regiondata;
1519         float size = get_drawsize(ar, rv3d->twmat[3]);
1520
1521         size*= (float)U.tw_size;
1522
1523         return size;
1524 }
1525
1526
1527 /* main call, does calc centers & orientation too */
1528 /* uses global G.moving */
1529 static int drawflags= 0xFFFF;           // only for the calls below, belongs in scene...?
1530
1531 void BIF_draw_manipulator(const bContext *C)
1532 {
1533         ScrArea *sa= CTX_wm_area(C);
1534         ARegion *ar= CTX_wm_region(C);
1535         Scene *scene= CTX_data_scene(C);
1536         View3D *v3d= sa->spacedata.first;
1537         RegionView3D *rv3d= ar->regiondata;
1538         int totsel;
1539
1540         if(!(v3d->twflag & V3D_USE_MANIPULATOR)) return;
1541 //      if(G.moving && (G.moving & G_TRANSFORM_MANIP)==0) return;
1542
1543 //      if(G.moving==0) {
1544         {
1545                 v3d->twflag &= ~V3D_DRAW_MANIPULATOR;
1546
1547                 totsel= calc_manipulator_stats(C);
1548                 if(totsel==0) return;
1549                 drawflags= v3d->twdrawflag;     /* set in calc_manipulator_stats */
1550
1551                 v3d->twflag |= V3D_DRAW_MANIPULATOR;
1552
1553                 /* now we can define center */
1554                 switch(v3d->around) {
1555                 case V3D_CENTER:
1556                 case V3D_ACTIVE:
1557                         rv3d->twmat[3][0]= (scene->twmin[0] + scene->twmax[0])/2.0f;
1558                         rv3d->twmat[3][1]= (scene->twmin[1] + scene->twmax[1])/2.0f;
1559                         rv3d->twmat[3][2]= (scene->twmin[2] + scene->twmax[2])/2.0f;
1560                         if(v3d->around==V3D_ACTIVE && scene->obedit==NULL) {
1561                                 Object *ob= OBACT;
1562                                 if(ob && !(ob->mode & OB_MODE_POSE))
1563                                         VECCOPY(rv3d->twmat[3], ob->obmat[3]);
1564                         }
1565                         break;
1566                 case V3D_LOCAL:
1567                 case V3D_CENTROID:
1568                         VECCOPY(rv3d->twmat[3], scene->twcent);
1569                         break;
1570                 case V3D_CURSOR:
1571                         VECCOPY(rv3d->twmat[3], give_cursor(scene, v3d));
1572                         break;
1573                 }
1574
1575                 Mat4MulFloat3((float *)rv3d->twmat, get_manipulator_drawsize(ar));
1576         }
1577
1578         if(v3d->twflag & V3D_DRAW_MANIPULATOR) {
1579
1580                 if(v3d->twtype & V3D_MANIP_ROTATE) {
1581
1582                         /* rotate has special ghosting draw, for pie chart */
1583                         if(G.moving) draw_manipulator_rotate_ghost(v3d, rv3d, drawflags);
1584
1585                         if(G.moving) glEnable(GL_BLEND);
1586
1587                         if(G.rt==3) {
1588                                 if(G.moving) draw_manipulator_rotate_cyl(v3d, rv3d, 1, drawflags, v3d->twtype, MAN_MOVECOL);
1589                                 else draw_manipulator_rotate_cyl(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1590                         }
1591                         else
1592                                 draw_manipulator_rotate(v3d, rv3d, 0 /* G.moving*/, drawflags, v3d->twtype);
1593
1594                         glDisable(GL_BLEND);
1595                 }
1596                 if(v3d->twtype & V3D_MANIP_SCALE) {
1597                         if(G.moving) {
1598                                 glEnable(GL_BLEND);
1599                                 draw_manipulator_scale(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_GHOST);
1600                                 draw_manipulator_scale(v3d, rv3d, 1, drawflags, v3d->twtype, MAN_MOVECOL);
1601                                 glDisable(GL_BLEND);
1602                         }
1603                         else draw_manipulator_scale(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1604                 }
1605                 if(v3d->twtype & V3D_MANIP_TRANSLATE) {
1606                         if(G.moving) {
1607                                 glEnable(GL_BLEND);
1608                                 draw_manipulator_translate(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_GHOST);
1609                                 draw_manipulator_translate(v3d, rv3d, 1, drawflags, v3d->twtype, MAN_MOVECOL);
1610                                 glDisable(GL_BLEND);
1611                         }
1612                         else draw_manipulator_translate(v3d, rv3d, 0, drawflags, v3d->twtype, MAN_RGB);
1613                 }
1614         }
1615 }
1616
1617 static int manipulator_selectbuf(ScrArea *sa, ARegion *ar, short *mval, float hotspot)
1618 {
1619         View3D *v3d= sa->spacedata.first;
1620         RegionView3D *rv3d= ar->regiondata;
1621         rctf rect;
1622         GLuint buffer[64];              // max 4 items per select, so large enuf
1623         short hits;
1624         extern void setwinmatrixview3d(ARegion *ar, View3D *v3d, rctf *rect); // XXX check a bit later on this... (ton)
1625
1626         G.f |= G_PICKSEL;
1627
1628         rect.xmin= mval[0]-hotspot;
1629         rect.xmax= mval[0]+hotspot;
1630         rect.ymin= mval[1]-hotspot;
1631         rect.ymax= mval[1]+hotspot;
1632
1633         setwinmatrixview3d(ar, v3d, &rect);
1634         Mat4MulMat4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1635
1636         glSelectBuffer( 64, buffer);
1637         glRenderMode(GL_SELECT);
1638         glInitNames();  /* these two calls whatfor? It doesnt work otherwise */
1639         glPushName(-2);
1640
1641         /* do the drawing */
1642         if(v3d->twtype & V3D_MANIP_ROTATE) {
1643                 if(G.rt==3) draw_manipulator_rotate_cyl(v3d, rv3d, 0, MAN_ROT_C & v3d->twdrawflag, v3d->twtype, MAN_RGB);
1644                 else draw_manipulator_rotate(v3d, rv3d, 0, MAN_ROT_C & v3d->twdrawflag, v3d->twtype);
1645         }
1646         if(v3d->twtype & V3D_MANIP_SCALE)
1647                 draw_manipulator_scale(v3d, rv3d, 0, MAN_SCALE_C & v3d->twdrawflag, v3d->twtype, MAN_RGB);
1648         if(v3d->twtype & V3D_MANIP_TRANSLATE)
1649                 draw_manipulator_translate(v3d, rv3d, 0, MAN_TRANS_C & v3d->twdrawflag, v3d->twtype, MAN_RGB);
1650
1651         glPopName();
1652         hits= glRenderMode(GL_RENDER);
1653
1654         G.f &= ~G_PICKSEL;
1655         setwinmatrixview3d(ar, v3d, NULL);
1656         Mat4MulMat4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1657
1658         if(hits==1) return buffer[3];
1659         else if(hits>1) {
1660                 GLuint val, dep, mindep=0, mindeprot=0, minval=0, minvalrot=0;
1661                 int a;
1662
1663                 /* we compare the hits in buffer, but value centers highest */
1664                 /* we also store the rotation hits separate (because of arcs) and return hits on other widgets if there are */
1665
1666                 for(a=0; a<hits; a++) {
1667                         dep= buffer[4*a + 1];
1668                         val= buffer[4*a + 3];
1669
1670                         if(val==MAN_TRANS_C) return MAN_TRANS_C;
1671                         else if(val==MAN_SCALE_C) return MAN_SCALE_C;
1672                         else {
1673                                 if(val & MAN_ROT_C) {
1674                                         if(minvalrot==0 || dep<mindeprot) {
1675                                                 mindeprot= dep;
1676                                                 minvalrot= val;
1677                                         }
1678                                 }
1679                                 else {
1680                                         if(minval==0 || dep<mindep) {
1681                                                 mindep= dep;
1682                                                 minval= val;
1683                                         }
1684                                 }
1685                         }
1686                 }
1687
1688                 if(minval)
1689                         return minval;
1690                 else
1691                         return minvalrot;
1692         }
1693         return 0;
1694 }
1695
1696 /* return 0; nothing happened */
1697 int BIF_do_manipulator(bContext *C, struct wmEvent *event, wmOperator *op)
1698 {
1699         ScrArea *sa= CTX_wm_area(C);
1700         View3D *v3d= sa->spacedata.first;
1701         ARegion *ar= CTX_wm_region(C);
1702         int constraint_axis[3] = {0, 0, 0};
1703         int val;
1704         int shift = event->shift;
1705
1706         if(!(v3d->twflag & V3D_USE_MANIPULATOR)) return 0;
1707         if(!(v3d->twflag & V3D_DRAW_MANIPULATOR)) return 0;
1708
1709         // find the hotspots first test narrow hotspot
1710         val= manipulator_selectbuf(sa, ar, event->mval, 0.5f*(float)U.tw_hotspot);
1711         if(val) {
1712
1713                 // drawflags still global, for drawing call above
1714                 drawflags= manipulator_selectbuf(sa, ar, event->mval, 0.2f*(float)U.tw_hotspot);
1715                 if(drawflags==0) drawflags= val;
1716
1717                 if (drawflags & MAN_TRANS_C) {
1718                         switch(drawflags) {
1719                         case MAN_TRANS_C:
1720                                 break;
1721                         case MAN_TRANS_X:
1722                                 if(shift) {
1723                                         constraint_axis[1] = 1;
1724                                         constraint_axis[2] = 1;
1725                                 }
1726                                 else
1727                                         constraint_axis[0] = 1;
1728                                 break;
1729                         case MAN_TRANS_Y:
1730                                 if(shift) {
1731                                         constraint_axis[0] = 1;
1732                                         constraint_axis[2] = 1;
1733                                 }
1734                                 else
1735                                         constraint_axis[1] = 1;
1736                                 break;
1737                         case MAN_TRANS_Z:
1738                                 if(shift) {
1739                                         constraint_axis[0] = 1;
1740                                         constraint_axis[1] = 1;
1741                                 }
1742                                 else
1743                                         constraint_axis[2] = 1;
1744                                 break;
1745                         }
1746                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1747                         WM_operator_name_call(C, "TFM_OT_translate", WM_OP_INVOKE_DEFAULT, op->ptr);
1748                 }
1749                 else if (drawflags & MAN_SCALE_C) {
1750                         switch(drawflags) {
1751                         case MAN_SCALE_X:
1752                                 if(shift) {
1753                                         constraint_axis[1] = 1;
1754                                         constraint_axis[2] = 1;
1755                                 }
1756                                 else
1757                                         constraint_axis[0] = 1;
1758                                 break;
1759                         case MAN_SCALE_Y:
1760                                 if(shift) {
1761                                         constraint_axis[0] = 1;
1762                                         constraint_axis[2] = 1;
1763                                 }
1764                                 else
1765                                         constraint_axis[1] = 1;
1766                                 break;
1767                         case MAN_SCALE_Z:
1768                                 if(shift) {
1769                                         constraint_axis[0] = 1;
1770                                         constraint_axis[1] = 1;
1771                                 }
1772                                 else
1773                                         constraint_axis[2] = 1;
1774                                 break;
1775                         }
1776                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1777                         WM_operator_name_call(C, "TFM_OT_resize", WM_OP_INVOKE_DEFAULT, op->ptr);
1778                 }
1779                 else if (drawflags == MAN_ROT_T) { /* trackball need special case, init is different */
1780                         WM_operator_name_call(C, "TFM_OT_trackball", WM_OP_INVOKE_DEFAULT, op->ptr);
1781                 }
1782                 else if (drawflags & MAN_ROT_C) {
1783                         switch(drawflags) {
1784                         case MAN_ROT_X:
1785                                 constraint_axis[0] = 1;
1786                                 break;
1787                         case MAN_ROT_Y:
1788                                 constraint_axis[1] = 1;
1789                                 break;
1790                         case MAN_ROT_Z:
1791                                 constraint_axis[2] = 1;
1792                                 break;
1793                         }
1794                         RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis);
1795                         WM_operator_name_call(C, "TFM_OT_rotate", WM_OP_INVOKE_DEFAULT, op->ptr);
1796                 }
1797         }
1798         /* after transform, restore drawflags */
1799         drawflags= 0xFFFF;
1800
1801         return val;
1802 }
1803