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