soc-2008-mxcurioni: implemented (without testing) StrokeShader, Stroke and MediumType...
[blender.git] / source / blender / src / drawview.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) 2001-2002 by NaN Holding BV.
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 //#define NAN_LINEAR_PHYSICS
31
32 #include <math.h>
33 #include <string.h>
34
35 #ifndef WIN32
36 #include <unistd.h>
37 #include <sys/times.h>
38 #else
39 #include <io.h>
40 #endif   
41
42 #ifdef WIN32
43 #ifndef snprintf
44 #define snprintf _snprintf
45 #endif
46 #endif
47
48 #include "MEM_guardedalloc.h"
49
50 #include "PIL_time.h"
51
52 #include "BMF_Api.h"
53
54 #include "IMB_imbuf_types.h"
55 #include "IMB_imbuf.h"
56
57 #include "DNA_action_types.h"
58 #include "DNA_armature_types.h"
59 #include "DNA_brush_types.h"
60 #include "DNA_camera_types.h"
61 #include "DNA_constraint_types.h"
62 #include "DNA_curve_types.h"
63 #include "DNA_group_types.h"
64 #include "DNA_image_types.h"
65 #include "DNA_key_types.h"
66 #include "DNA_lattice_types.h"
67 #include "DNA_mesh_types.h"
68 #include "DNA_meshdata_types.h"
69 #include "DNA_meta_types.h"
70 #include "DNA_modifier_types.h"
71 #include "DNA_object_force.h"
72 #include "DNA_object_types.h"
73 #include "DNA_particle_types.h"
74 #include "DNA_screen_types.h"
75 #include "DNA_scene_types.h"
76 #include "DNA_space_types.h"
77 #include "DNA_texture_types.h"
78 #include "DNA_userdef_types.h"
79 #include "DNA_view3d_types.h"
80 #include "DNA_world_types.h"
81
82 #include "BLI_blenlib.h"
83 #include "BLI_arithb.h"
84 #include "BLI_editVert.h"
85
86 #include "BKE_action.h"
87 #include "BKE_armature.h"
88 #include "BKE_anim.h"
89 #include "BKE_constraint.h"
90 #include "BKE_curve.h"
91 #include "BKE_customdata.h"
92 #include "BKE_displist.h"
93 #include "BKE_depsgraph.h"
94 #include "BKE_DerivedMesh.h"
95 #include "BKE_global.h"
96 #include "BKE_lattice.h"
97 #include "BKE_library.h"
98 #include "BKE_image.h"
99 #include "BKE_ipo.h"
100 #include "BKE_key.h"
101 #include "BKE_main.h"
102 #include "BKE_mesh.h"
103 #include "BKE_modifier.h"
104 #include "BKE_object.h"
105 #include "BKE_particle.h"
106 #include "BKE_pointcache.h"
107 #include "BKE_scene.h"
108 #include "BKE_sculpt.h"
109 #include "BKE_texture.h"
110 #include "BKE_utildefines.h"
111
112 #include "BIF_butspace.h"
113 #include "BIF_drawimage.h"
114 #include "BIF_editgroup.h"
115 #include "BIF_editarmature.h"
116 #include "BIF_editmesh.h"
117 #include "BIF_editparticle.h"
118 #include "BIF_gl.h"
119 #include "BIF_glutil.h"
120 #include "BIF_interface.h"
121 #include "BIF_interface_icons.h"
122 #include "BIF_mywindow.h"
123 #include "BIF_poseobject.h"
124 #include "BIF_previewrender.h"
125 #include "BIF_radialcontrol.h"
126 #include "BIF_resources.h"
127 #include "BIF_retopo.h"
128 #include "BIF_screen.h"
129 #include "BIF_space.h"
130
131 #ifdef WITH_VERSE
132 #include "BIF_verse.h"
133 #endif
134
135 #include "BDR_drawmesh.h"
136 #include "BDR_drawobject.h"
137 #include "BDR_editobject.h"
138 #include "BDR_vpaint.h"
139 #include "BDR_sculptmode.h"
140
141 #include "BSE_drawview.h"
142 #include "BSE_filesel.h"
143 #include "BSE_headerbuttons.h"
144 #include "BSE_seqaudio.h"
145 #include "BSE_sequence.h"
146 #include "BSE_trans_types.h"
147 #include "BSE_time.h"
148 #include "BSE_view.h"
149
150 #include "BPY_extern.h"
151
152 #include "RE_render_ext.h"
153
154 #include "blendef.h"
155 #include "mydevice.h"
156 #include "butspace.h"  // event codes
157
158 #include "BIF_transform.h"
159
160 #include "RE_pipeline.h"        // make_stars
161
162 #include "multires.h"
163
164 /* For MULTISAMPLE_ARB #define.
165    Note that older systems like irix 
166    may not have this, and will need a #ifdef
167    to disable it.*/
168 /* #include "GL/glext.h" Disabled for release, to avoid possibly breaking platforms.
169    Instead, the define we need will just be #defined if it's not in the platform opengl.h.
170 */
171
172 /* Modules used */
173 #include "radio.h"
174
175 /* locals */
176 void drawname(Object *ob);
177
178 static void star_stuff_init_func(void)
179 {
180         cpack(-1);
181         glPointSize(1.0);
182         glBegin(GL_POINTS);
183 }
184 static void star_stuff_vertex_func(float* i)
185 {
186         glVertex3fv(i);
187 }
188 static void star_stuff_term_func(void)
189 {
190         glEnd();
191 }
192
193 void default_gl_light(void)
194 {
195         int a;
196         
197         /* initialize */
198         if(U.light[0].flag==0 && U.light[1].flag==0 && U.light[2].flag==0) {
199                 U.light[0].flag= 1;
200                 U.light[0].vec[0]= -0.3; U.light[0].vec[1]= 0.3; U.light[0].vec[2]= 0.9;
201                 U.light[0].col[0]= 0.8; U.light[0].col[1]= 0.8; U.light[0].col[2]= 0.8;
202                 U.light[0].spec[0]= 0.5; U.light[0].spec[1]= 0.5; U.light[0].spec[2]= 0.5;
203                 U.light[0].spec[3]= 1.0;
204                 
205                 U.light[1].flag= 0;
206                 U.light[1].vec[0]= 0.5; U.light[1].vec[1]= 0.5; U.light[1].vec[2]= 0.1;
207                 U.light[1].col[0]= 0.4; U.light[1].col[1]= 0.4; U.light[1].col[2]= 0.8;
208                 U.light[1].spec[0]= 0.3; U.light[1].spec[1]= 0.3; U.light[1].spec[2]= 0.5;
209                 U.light[1].spec[3]= 1.0;
210         
211                 U.light[2].flag= 0;
212                 U.light[2].vec[0]= 0.3; U.light[2].vec[1]= -0.3; U.light[2].vec[2]= -0.2;
213                 U.light[2].col[0]= 0.8; U.light[2].col[1]= 0.5; U.light[2].col[2]= 0.4;
214                 U.light[2].spec[0]= 0.5; U.light[2].spec[1]= 0.4; U.light[2].spec[2]= 0.3;
215                 U.light[2].spec[3]= 1.0;
216         }
217         
218
219         glLightfv(GL_LIGHT0, GL_POSITION, U.light[0].vec); 
220         glLightfv(GL_LIGHT0, GL_DIFFUSE, U.light[0].col); 
221         glLightfv(GL_LIGHT0, GL_SPECULAR, U.light[0].spec); 
222
223         glLightfv(GL_LIGHT1, GL_POSITION, U.light[1].vec); 
224         glLightfv(GL_LIGHT1, GL_DIFFUSE, U.light[1].col); 
225         glLightfv(GL_LIGHT1, GL_SPECULAR, U.light[1].spec); 
226
227         glLightfv(GL_LIGHT2, GL_POSITION, U.light[2].vec); 
228         glLightfv(GL_LIGHT2, GL_DIFFUSE, U.light[2].col); 
229         glLightfv(GL_LIGHT2, GL_SPECULAR, U.light[2].spec); 
230
231         for(a=0; a<8; a++) {
232                 if(a<3) {
233                         if(U.light[a].flag) glEnable(GL_LIGHT0+a);
234                         else glDisable(GL_LIGHT0+a);
235                         
236                         // clear stuff from other opengl lamp usage
237                         glLightf(GL_LIGHT0+a, GL_SPOT_CUTOFF, 180.0);
238                         glLightf(GL_LIGHT0+a, GL_CONSTANT_ATTENUATION, 1.0);
239                         glLightf(GL_LIGHT0+a, GL_LINEAR_ATTENUATION, 0.0);
240                 }
241                 else glDisable(GL_LIGHT0+a);
242         }
243         
244         glDisable(GL_LIGHTING);
245
246         glDisable(GL_COLOR_MATERIAL);
247 }
248
249 /* also called when render 'ogl'
250    keep synced with Myinit_gl_stuff in the game engine! */
251 void init_gl_stuff(void)        
252 {
253         float mat_ambient[] = { 0.0, 0.0, 0.0, 0.0 };
254         float mat_specular[] = { 0.5, 0.5, 0.5, 1.0 };
255         float mat_shininess[] = { 35.0 };
256         int a, x, y;
257         GLubyte pat[32*32];
258         const GLubyte *patc= pat;
259         
260         
261         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
262         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_specular);
263         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
264         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
265
266         default_gl_light();
267         
268         /* no local viewer, looks ugly in ortho mode */
269         /* glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, &one); */
270         
271         glDepthFunc(GL_LEQUAL);
272         /* scaling matrices */
273         glEnable(GL_NORMALIZE);
274
275         glShadeModel(GL_FLAT);
276
277         glDisable(GL_ALPHA_TEST);
278         glDisable(GL_BLEND);
279         glDisable(GL_DEPTH_TEST);
280         glDisable(GL_FOG);
281         glDisable(GL_LIGHTING);
282         glDisable(GL_LOGIC_OP);
283         glDisable(GL_STENCIL_TEST);
284         glDisable(GL_TEXTURE_1D);
285         glDisable(GL_TEXTURE_2D);
286
287         /* default on, disable/enable should be local per function */
288         glEnableClientState(GL_VERTEX_ARRAY);
289         glEnableClientState(GL_NORMAL_ARRAY);
290         
291         glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
292         glPixelTransferi(GL_RED_SCALE, 1);
293         glPixelTransferi(GL_RED_BIAS, 0);
294         glPixelTransferi(GL_GREEN_SCALE, 1);
295         glPixelTransferi(GL_GREEN_BIAS, 0);
296         glPixelTransferi(GL_BLUE_SCALE, 1);
297         glPixelTransferi(GL_BLUE_BIAS, 0);
298         glPixelTransferi(GL_ALPHA_SCALE, 1);
299         glPixelTransferi(GL_ALPHA_BIAS, 0);
300         
301         glPixelTransferi(GL_DEPTH_BIAS, 0);
302         glPixelTransferi(GL_DEPTH_SCALE, 1);
303         glDepthRange(0.0, 1.0);
304         
305         a= 0;
306         for(x=0; x<32; x++) {
307                 for(y=0; y<4; y++) {
308                         if( (x) & 1) pat[a++]= 0x88;
309                         else pat[a++]= 0x22;
310                 }
311         }
312         
313         glPolygonStipple(patc);
314
315
316         init_realtime_GL();     
317 }
318
319 void circf(float x, float y, float rad)
320 {
321         GLUquadricObj *qobj = gluNewQuadric(); 
322         
323         gluQuadricDrawStyle(qobj, GLU_FILL); 
324         
325         glPushMatrix(); 
326         
327         glTranslatef(x,  y, 0.); 
328         
329         gluDisk( qobj, 0.0,  rad, 32, 1); 
330         
331         glPopMatrix(); 
332         
333         gluDeleteQuadric(qobj);
334 }
335
336 void circ(float x, float y, float rad)
337 {
338         GLUquadricObj *qobj = gluNewQuadric(); 
339         
340         gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
341         
342         glPushMatrix(); 
343         
344         glTranslatef(x,  y, 0.); 
345         
346         gluDisk( qobj, 0.0,  rad, 32, 1); 
347         
348         glPopMatrix(); 
349         
350         gluDeleteQuadric(qobj);
351 }
352
353 /* **********  ********** */
354
355 static void draw_bgpic(void)
356 {
357         BGpic *bgpic;
358         Image *ima;
359         ImBuf *ibuf= NULL;
360         float vec[4], fac, asp, zoomx, zoomy;
361         float x1, y1, x2, y2, cx, cy;
362         
363         bgpic= G.vd->bgpic;
364         if(bgpic==NULL) return;
365         
366         ima= bgpic->ima;
367         
368         if(ima)
369                 ibuf= BKE_image_get_ibuf(ima, &bgpic->iuser);
370         if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL) ) 
371                 return;
372         if(ibuf->channels!=4)
373                 return;
374         if(ibuf->rect==NULL)
375                 IMB_rect_from_float(ibuf);
376         
377         if(G.vd->persp==2) {
378                 rctf vb;
379
380                 calc_viewborder(G.vd, &vb);
381
382                 x1= vb.xmin;
383                 y1= vb.ymin;
384                 x2= vb.xmax;
385                 y2= vb.ymax;
386         }
387         else {
388                 float sco[2];
389
390                 /* calc window coord */
391                 initgrabz(0.0, 0.0, 0.0);
392                 window_to_3d(vec, 1, 0);
393                 fac= MAX3( fabs(vec[0]), fabs(vec[1]), fabs(vec[1]) );
394                 fac= 1.0/fac;
395         
396                 asp= ( (float)ibuf->y)/(float)ibuf->x;
397
398                 vec[0] = vec[1] = vec[2] = 0.0;
399                 view3d_project_float(curarea, vec, sco, G.vd->persmat);
400                 cx = sco[0];
401                 cy = sco[1];
402         
403                 x1=  cx+ fac*(bgpic->xof-bgpic->size);
404                 y1=  cy+ asp*fac*(bgpic->yof-bgpic->size);
405                 x2=  cx+ fac*(bgpic->xof+bgpic->size);
406                 y2=  cy+ asp*fac*(bgpic->yof+bgpic->size);
407         }
408         
409         /* complete clip? */
410         
411         if(x2 < 0 ) return;
412         if(y2 < 0 ) return;
413         if(x1 > curarea->winx ) return;
414         if(y1 > curarea->winy ) return;
415         
416         zoomx= (x2-x1)/ibuf->x;
417         zoomy= (y2-y1)/ibuf->y;
418         
419         /* for some reason; zoomlevels down refuses to use GL_ALPHA_SCALE */
420         if(zoomx < 1.0f || zoomy < 1.0f) {
421                 float tzoom= MIN2(zoomx, zoomy);
422                 int mip= 0;
423                 
424                 if(ibuf->mipmap[0]==NULL)
425                         IMB_makemipmap(ibuf, 0);
426                 
427                 while(tzoom < 1.0f && mip<8 && ibuf->mipmap[mip]) {
428                         tzoom*= 2.0f;
429                         zoomx*= 2.0f;
430                         zoomy*= 2.0f;
431                         mip++;
432                 }
433                 if(mip>0)
434                         ibuf= ibuf->mipmap[mip-1];
435         }
436         
437         if(G.vd->zbuf) glDisable(GL_DEPTH_TEST);
438
439         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
440          
441         glMatrixMode(GL_PROJECTION);
442         glPushMatrix();
443         glMatrixMode(GL_MODELVIEW);
444         glPushMatrix();
445         
446         glaDefine2DArea(&curarea->winrct);
447
448         glEnable(GL_BLEND);
449
450         glPixelZoom(zoomx, zoomy);
451         glColor4f(1.0, 1.0, 1.0, 1.0-bgpic->blend);
452         glaDrawPixelsTex(x1, y1, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
453         
454         glPixelZoom(1.0, 1.0);
455         glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
456
457         glMatrixMode(GL_PROJECTION);
458         glPopMatrix();
459         glMatrixMode(GL_MODELVIEW);
460         glPopMatrix();
461         
462         glDisable(GL_BLEND);
463         if(G.vd->zbuf) glEnable(GL_DEPTH_TEST);
464         
465         areawinset(curarea->win);       // restore viewport / scissor
466 }
467
468 static void drawgrid_draw(float wx, float wy, float x, float y, float dx)
469 {
470         float fx, fy;
471         
472         x+= (wx); 
473         y+= (wy);
474         fx= x/dx;
475         fx= x-dx*floor(fx);
476         
477         while(fx< curarea->winx) {
478                 fdrawline(fx,  0.0,  fx,  (float)curarea->winy); 
479                 fx+= dx; 
480         }
481
482         fy= y/dx;
483         fy= y-dx*floor(fy);
484         
485
486         while(fy< curarea->winy) {
487                 fdrawline(0.0,  fy,  (float)curarea->winx,  fy); 
488                 fy+= dx;
489         }
490
491 }
492
493 // not intern, called in editobject for constraint axis too
494 void make_axis_color(char *col, char *col2, char axis)
495 {
496         if(axis=='x') {
497                 col2[0]= col[0]>219?255:col[0]+36;
498                 col2[1]= col[1]<26?0:col[1]-26;
499                 col2[2]= col[2]<26?0:col[2]-26;
500         }
501         else if(axis=='y') {
502                 col2[0]= col[0]<46?0:col[0]-36;
503                 col2[1]= col[1]>189?255:col[1]+66;
504                 col2[2]= col[2]<46?0:col[2]-36; 
505         }
506         else {
507                 col2[0]= col[0]<26?0:col[0]-26; 
508                 col2[1]= col[1]<26?0:col[1]-26; 
509                 col2[2]= col[2]>209?255:col[2]+46;
510         }
511         
512 }
513
514 static void drawgrid(void)
515 {
516         /* extern short bgpicmode; */
517         float wx, wy, x, y, fw, fx, fy, dx;
518         float vec4[4];
519         char col[3], col2[3];
520         short sublines = G.vd->gridsubdiv;
521         
522         vec4[0]=vec4[1]=vec4[2]=0.0; 
523         vec4[3]= 1.0;
524         Mat4MulVec4fl(G.vd->persmat, vec4);
525         fx= vec4[0]; 
526         fy= vec4[1]; 
527         fw= vec4[3];
528
529         wx= (curarea->winx/2.0);        /* because of rounding errors, grid at wrong location */
530         wy= (curarea->winy/2.0);
531
532         x= (wx)*fx/fw;
533         y= (wy)*fy/fw;
534
535         vec4[0]=vec4[1]=G.vd->grid; 
536         vec4[2]= 0.0;
537         vec4[3]= 1.0;
538         Mat4MulVec4fl(G.vd->persmat, vec4);
539         fx= vec4[0]; 
540         fy= vec4[1]; 
541         fw= vec4[3];
542
543         dx= fabs(x-(wx)*fx/fw);
544         if(dx==0) dx= fabs(y-(wy)*fy/fw);
545         
546         glDepthMask(0);         // disable write in zbuffer
547
548         /* check zoom out */
549         BIF_ThemeColor(TH_GRID);
550         persp(PERSP_WIN);
551         
552         if(dx<6.0) {
553                 G.vd->gridview*= sublines;
554                 dx*= sublines;
555                 
556                 if(dx<6.0) {    
557                         G.vd->gridview*= sublines;
558                         dx*= sublines;
559                         
560                         if(dx<6.0) {
561                                 G.vd->gridview*= sublines;
562                                 dx*=sublines;
563                                 if(dx<6.0);
564                                 else {
565                                         BIF_ThemeColor(TH_GRID);
566                                         drawgrid_draw(wx, wy, x, y, dx);
567                                 }
568                         }
569                         else {  // start blending out
570                                 BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
571                                 drawgrid_draw(wx, wy, x, y, dx);
572                         
573                                 BIF_ThemeColor(TH_GRID);
574                                 drawgrid_draw(wx, wy, x, y, sublines*dx);
575                         }
576                 }
577                 else {  // start blending out (6 < dx < 60)
578                         BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
579                         drawgrid_draw(wx, wy, x, y, dx);
580                         
581                         BIF_ThemeColor(TH_GRID);
582                         drawgrid_draw(wx, wy, x, y, sublines*dx);
583                 }
584         }
585         else {
586                 if(dx>60.0) {           // start blending in
587                         G.vd->gridview/= sublines;
588                         dx/= sublines;                  
589                         if(dx>60.0) {           // start blending in
590                                 G.vd->gridview/= sublines;
591                                 dx/= sublines;
592                                 if(dx>60.0) {
593                                         BIF_ThemeColor(TH_GRID);
594                                         drawgrid_draw(wx, wy, x, y, dx);
595                                 }
596                                 else {
597                                         BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
598                                         drawgrid_draw(wx, wy, x, y, dx);
599                                         BIF_ThemeColor(TH_GRID);
600                                         drawgrid_draw(wx, wy, x, y, dx*sublines);
601                                 }
602                         }
603                         else {
604                                 BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
605                                 drawgrid_draw(wx, wy, x, y, dx);
606                                 BIF_ThemeColor(TH_GRID);                                
607                                 drawgrid_draw(wx, wy, x, y, dx*sublines);
608                         }
609                 }
610                 else {
611                         BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
612                         drawgrid_draw(wx, wy, x, y, dx);
613                         BIF_ThemeColor(TH_GRID);
614                         drawgrid_draw(wx, wy, x, y, dx*sublines);
615                 }
616         }
617
618         x+= (wx); 
619         y+= (wy);
620         BIF_GetThemeColor3ubv(TH_GRID, col);
621
622         setlinestyle(0);
623         
624         /* center cross */
625         if(G.vd->view==3) make_axis_color(col, col2, 'y');
626         else make_axis_color(col, col2, 'x');
627         glColor3ubv((GLubyte *)col2);
628         
629         fdrawline(0.0,  y,  (float)curarea->winx,  y); 
630         
631         if(G.vd->view==7) make_axis_color(col, col2, 'y');
632         else make_axis_color(col, col2, 'z');
633         glColor3ubv((GLubyte *)col2);
634
635         fdrawline(x, 0.0, x, (float)curarea->winy); 
636
637         glDepthMask(1);         // enable write in zbuffer
638         persp(PERSP_VIEW);
639 }
640
641
642
643 static void drawfloor(void)
644 {
645         View3D *vd;
646         float vert[3], grid;
647         int a, gridlines, emphasise;
648         char col[3], col2[3];
649         short draw_line = 0;
650                 
651         vd= curarea->spacedata.first;
652
653         vert[2]= 0.0;
654
655         if(vd->gridlines<3) return;
656         
657         if(G.vd->zbuf && G.obedit) glDepthMask(0);      // for zbuffer-select
658         
659         gridlines= vd->gridlines/2;
660         grid= gridlines*vd->grid;
661         
662         BIF_GetThemeColor3ubv(TH_GRID, col);
663         BIF_GetThemeColor3ubv(TH_BACK, col2);
664         
665         /* emphasise division lines lighter instead of darker, if background is darker than grid */
666         if ( ((col[0]+col[1]+col[2])/3+10) > (col2[0]+col2[1]+col2[2])/3 )
667                 emphasise = 20;
668         else
669                 emphasise = -10;
670         
671         /* draw the Y axis and/or grid lines */
672         for(a= -gridlines;a<=gridlines;a++) {
673                 if(a==0) {
674                         /* check for the 'show Y axis' preference */
675                         if (vd->gridflag & V3D_SHOW_Y) { 
676                                 make_axis_color(col, col2, 'y');
677                                 glColor3ubv((GLubyte *)col2);
678                                 
679                                 draw_line = 1;
680                         } else if (vd->gridflag & V3D_SHOW_FLOOR) {
681                                 BIF_ThemeColorShade(TH_GRID, emphasise);
682                         } else {
683                                 draw_line = 0;
684                         }
685                 } else {
686                         /* check for the 'show grid floor' preference */
687                         if (vd->gridflag & V3D_SHOW_FLOOR) {
688                                 if( (a % 10)==0) {
689                                         BIF_ThemeColorShade(TH_GRID, emphasise);
690                                 }
691                                 else BIF_ThemeColorShade(TH_GRID, 10);
692                                 
693                                 draw_line = 1;
694                         } else {
695                                 draw_line = 0;
696                         }
697                 }
698                 
699                 if (draw_line) {
700                         glBegin(GL_LINE_STRIP);
701                 vert[0]= a*vd->grid;
702                 vert[1]= grid;
703                 glVertex3fv(vert);
704                 vert[1]= -grid;
705                 glVertex3fv(vert);
706                         glEnd();
707                 }
708         }
709         
710         /* draw the X axis and/or grid lines */
711         for(a= -gridlines;a<=gridlines;a++) {
712                 if(a==0) {
713                         /* check for the 'show X axis' preference */
714                         if (vd->gridflag & V3D_SHOW_X) { 
715                                 make_axis_color(col, col2, 'x');
716                                 glColor3ubv((GLubyte *)col2);
717                                 
718                                 draw_line = 1;
719                         } else if (vd->gridflag & V3D_SHOW_FLOOR) {
720                                 BIF_ThemeColorShade(TH_GRID, emphasise);
721                         } else {
722                                 draw_line = 0;
723                         }
724                 } else {
725                         /* check for the 'show grid floor' preference */
726                         if (vd->gridflag & V3D_SHOW_FLOOR) {
727                                 if( (a % 10)==0) {
728                                         BIF_ThemeColorShade(TH_GRID, emphasise);
729                                 }
730                                 else BIF_ThemeColorShade(TH_GRID, 10);
731                                 
732                                 draw_line = 1;
733                         } else {
734                                 draw_line = 0;
735                         }
736                 }
737                 
738                 if (draw_line) {
739                         glBegin(GL_LINE_STRIP);
740                 vert[1]= a*vd->grid;
741                 vert[0]= grid;
742                 glVertex3fv(vert );
743                 vert[0]= -grid;
744                 glVertex3fv(vert);
745                         glEnd();
746                 }
747         }
748         
749         /* draw the Z axis line */      
750         /* check for the 'show Z axis' preference */
751         if (vd->gridflag & V3D_SHOW_Z) {
752                 make_axis_color(col, col2, 'z');
753                 glColor3ubv((GLubyte *)col2);
754                 
755                 glBegin(GL_LINE_STRIP);
756                 vert[0]= 0;
757                 vert[1]= 0;
758                 vert[2]= grid;
759                 glVertex3fv(vert );
760                 vert[2]= -grid;
761                 glVertex3fv(vert);
762                 glEnd();
763         }
764
765         if(G.vd->zbuf && G.obedit) glDepthMask(1);      
766
767 }
768
769 static void drawcursor(View3D *v3d)
770 {
771         short mx,my,co[2];
772         int flag;
773         
774         /* we dont want the clipping for cursor */
775         flag= v3d->flag;
776         v3d->flag= 0;
777         project_short( give_cursor(), co);
778         v3d->flag= flag;
779         
780         mx = co[0];
781         my = co[1];
782
783         if(mx!=IS_CLIPPED) {
784                 setlinestyle(0); 
785                 cpack(0xFF);
786                 circ((float)mx, (float)my, 10.0);
787                 setlinestyle(4); 
788                 cpack(0xFFFFFF);
789                 circ((float)mx, (float)my, 10.0);
790                 setlinestyle(0);
791                 cpack(0x0);
792                 
793                 sdrawline(mx-20, my, mx-5, my);
794                 sdrawline(mx+5, my, mx+20, my);
795                 sdrawline(mx, my-20, mx, my-5);
796                 sdrawline(mx, my+5, mx, my+20);
797         }
798 }
799
800 /* ********* custom clipping *********** */
801
802 static void view3d_draw_clipping(View3D *v3d)
803 {
804         BoundBox *bb= v3d->clipbb;
805         
806         BIF_ThemeColorShade(TH_BACK, -8);
807         
808         glBegin(GL_QUADS);
809
810         glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[3]);
811         glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[1]);
812         glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[5]);
813         glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[3]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[6]);
814         glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[2]);
815         glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[3]);
816         
817         glEnd();
818 }
819
820 void view3d_set_clipping(View3D *v3d)
821 {
822         double plane[4];
823         int a;
824         
825         for(a=0; a<4; a++) {
826                 QUATCOPY(plane, v3d->clip[a]);
827                 glClipPlane(GL_CLIP_PLANE0+a, plane);
828                 glEnable(GL_CLIP_PLANE0+a);
829         }
830 }
831
832 void view3d_clr_clipping(void)
833 {
834         int a;
835         
836         for(a=0; a<4; a++) {
837                 glDisable(GL_CLIP_PLANE0+a);
838         }
839 }
840
841 int view3d_test_clipping(View3D *v3d, float *vec)
842 {
843         /* vec in world coordinates, returns 1 if clipped */
844         float view[3];
845         
846         VECCOPY(view, vec);
847         
848         if(0.0f < v3d->clip[0][3] + INPR(view, v3d->clip[0]))
849                 if(0.0f < v3d->clip[1][3] + INPR(view, v3d->clip[1]))
850                         if(0.0f < v3d->clip[2][3] + INPR(view, v3d->clip[2]))
851                                 if(0.0f < v3d->clip[3][3] + INPR(view, v3d->clip[3]))
852                                         return 0;
853
854         return 1;
855 }
856
857 /* ********* end custom clipping *********** */
858
859 static void view3d_get_viewborder_size(View3D *v3d, float size_r[2])
860 {
861         float winmax= MAX2(v3d->area->winx, v3d->area->winy);
862         float aspect= (float) (G.scene->r.xsch*G.scene->r.xasp)/(G.scene->r.ysch*G.scene->r.yasp);
863
864         if(aspect>1.0) {
865                 size_r[0]= winmax;
866                 size_r[1]= winmax/aspect;
867         } else {
868                 size_r[0]= winmax*aspect;
869                 size_r[1]= winmax;
870         }
871 }
872
873 void calc_viewborder(struct View3D *v3d, rctf *viewborder_r)
874 {
875         float zoomfac, size[2];
876         float dx= 0.0f, dy= 0.0f;
877         
878         view3d_get_viewborder_size(v3d, size);
879
880                 /* magic zoom calculation, no idea what
881              * it signifies, if you find out, tell me! -zr
882                  */
883         /* simple, its magic dude!
884          * well, to be honest, this gives a natural feeling zooming
885          * with multiple keypad presses (ton)
886          */
887         
888         zoomfac= (M_SQRT2 + v3d->camzoom/50.0);
889         zoomfac= (zoomfac*zoomfac)*0.25;
890         
891         size[0]= size[0]*zoomfac;
892         size[1]= size[1]*zoomfac;
893
894                 /* center in window */
895         viewborder_r->xmin= 0.5*v3d->area->winx - 0.5*size[0];
896         viewborder_r->ymin= 0.5*v3d->area->winy - 0.5*size[1];
897         viewborder_r->xmax= viewborder_r->xmin + size[0];
898         viewborder_r->ymax= viewborder_r->ymin + size[1];
899         
900         dx= v3d->area->winx*G.vd->camdx*zoomfac*2.0f;
901         dy= v3d->area->winy*G.vd->camdy*zoomfac*2.0f;
902         
903         /* apply offset */
904         viewborder_r->xmin-= dx;
905         viewborder_r->ymin-= dy;
906         viewborder_r->xmax-= dx;
907         viewborder_r->ymax-= dy;
908         
909         if(v3d->camera && v3d->camera->type==OB_CAMERA) {
910                 Camera *cam= v3d->camera->data;
911                 float w = viewborder_r->xmax - viewborder_r->xmin;
912                 float h = viewborder_r->ymax - viewborder_r->ymin;
913                 float side = MAX2(w, h);
914
915                 viewborder_r->xmin+= cam->shiftx*side;
916                 viewborder_r->xmax+= cam->shiftx*side;
917                 viewborder_r->ymin+= cam->shifty*side;
918                 viewborder_r->ymax+= cam->shifty*side;
919         }
920 }
921
922 void view3d_set_1_to_1_viewborder(View3D *v3d)
923 {
924         float size[2];
925         int im_width= (G.scene->r.size*G.scene->r.xsch)/100;
926
927         view3d_get_viewborder_size(v3d, size);
928
929         v3d->camzoom= (sqrt(4.0*im_width/size[0]) - M_SQRT2)*50.0;
930         v3d->camzoom= CLAMPIS(v3d->camzoom, -30, 300);
931 }
932
933
934 static void drawviewborder_flymode(void)        
935 {
936         /* draws 4 edge brackets that frame the safe area where the
937         mouse can move during fly mode without spinning the view */
938         float x1, x2, y1, y2;
939         
940         x1= 0.45*(float)curarea->winx;
941         y1= 0.45*(float)curarea->winy;
942         x2= 0.55*(float)curarea->winx;
943         y2= 0.55*(float)curarea->winy;
944         cpack(0);
945         
946         
947         glBegin(GL_LINES);
948         /* bottom left */
949         glVertex2f(x1,y1); 
950         glVertex2f(x1,y1+5);
951         
952         glVertex2f(x1,y1); 
953         glVertex2f(x1+5,y1);
954
955         /* top right */
956         glVertex2f(x2,y2); 
957         glVertex2f(x2,y2-5);
958         
959         glVertex2f(x2,y2); 
960         glVertex2f(x2-5,y2);
961         
962         /* top left */
963         glVertex2f(x1,y2); 
964         glVertex2f(x1,y2-5);
965         
966         glVertex2f(x1,y2); 
967         glVertex2f(x1+5,y2);
968         
969         /* bottom right */
970         glVertex2f(x2,y1); 
971         glVertex2f(x2,y1+5);
972         
973         glVertex2f(x2,y1); 
974         glVertex2f(x2-5,y1);
975         glEnd();        
976 }
977
978
979 static void drawviewborder(void)
980 {
981         extern void gl_round_box(int mode, float minx, float miny, float maxx, float maxy, float rad);          // interface_panel.c
982         float fac, a;
983         float x1, x2, y1, y2;
984         float x3, y3, x4, y4;
985         rctf viewborder;
986         Camera *ca= NULL;
987
988         if(G.vd->camera==NULL)
989                 return;
990         if(G.vd->camera->type==OB_CAMERA)
991                 ca = G.vd->camera->data;
992         
993         calc_viewborder(G.vd, &viewborder);
994         x1= viewborder.xmin;
995         y1= viewborder.ymin;
996         x2= viewborder.xmax;
997         y2= viewborder.ymax;
998
999         /* passepartout, specified in camera edit buttons */
1000         if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001) {
1001                 if (ca->passepartalpha == 1.0) {
1002                         glColor3f(0, 0, 0);
1003                 } else {
1004                         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1005                         glEnable(GL_BLEND);
1006                         glColor4f(0, 0, 0, ca->passepartalpha);
1007                 }
1008                 if (x1 > 0.0)
1009                         glRectf(0.0, (float)curarea->winy, x1, 0.0);
1010                 if (x2 < (float)curarea->winx)
1011                         glRectf(x2, (float)curarea->winy, (float)curarea->winx, 0.0);
1012                 if (y2 < (float)curarea->winy)
1013                         glRectf(x1, (float)curarea->winy, x2, y2);
1014                 if (y2 > 0.0) 
1015                         glRectf(x1, y1, x2, 0.0);
1016
1017                 glDisable(GL_BLEND);
1018         }
1019         
1020         /* edge */
1021         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
1022         
1023         setlinestyle(0);
1024         BIF_ThemeColor(TH_BACK);
1025         glRectf(x1, y1, x2, y2);
1026         
1027         setlinestyle(3);
1028         BIF_ThemeColor(TH_WIRE);
1029         glRectf(x1, y1, x2, y2);
1030                 
1031         /* camera name - draw in highlighted text color */
1032         if (ca && (ca->flag & CAM_SHOWNAME)) {
1033                 BIF_ThemeColor(TH_TEXT_HI);
1034                 glRasterPos2f(x1, y1-15);
1035                 
1036                 BMF_DrawString(G.font, G.vd->camera->id.name+2);
1037                 BIF_ThemeColor(TH_WIRE);
1038         }
1039
1040
1041         /* border */
1042         if(G.scene->r.mode & R_BORDER) {
1043                 
1044                 cpack(0);
1045                 x3= x1+ G.scene->r.border.xmin*(x2-x1);
1046                 y3= y1+ G.scene->r.border.ymin*(y2-y1);
1047                 x4= x1+ G.scene->r.border.xmax*(x2-x1);
1048                 y4= y1+ G.scene->r.border.ymax*(y2-y1);
1049                 
1050                 cpack(0x4040FF);
1051                 glRectf(x3,  y3,  x4,  y4); 
1052         }
1053
1054         /* safety border */
1055         if (ca && (ca->flag & CAM_SHOWTITLESAFE)) {
1056                 fac= 0.1;
1057                 
1058                 a= fac*(x2-x1);
1059                 x1+= a; 
1060                 x2-= a;
1061         
1062                 a= fac*(y2-y1);
1063                 y1+= a;
1064                 y2-= a;
1065         
1066                 BIF_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1067                 
1068                 uiSetRoundBox(15);
1069                 gl_round_box(GL_LINE_LOOP, x1, y1, x2, y2, 12.0);
1070         }
1071         
1072         setlinestyle(0);
1073         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1074
1075 }
1076
1077 void backdrawview3d(int test)
1078 {
1079         struct Base *base;
1080
1081 /*for 2.43 release, don't use glext and just define the constant.
1082   this to avoid possibly breaking platforms before release.*/
1083 #ifndef GL_MULTISAMPLE_ARB
1084         #define GL_MULTISAMPLE_ARB      0x809D
1085 #endif
1086
1087 #ifdef GL_MULTISAMPLE_ARB
1088         int m;
1089 #endif
1090
1091         if(G.f & G_VERTEXPAINT || G.f & G_WEIGHTPAINT || G.f & G_TEXTUREPAINT);
1092         else if(G.obedit && G.vd->drawtype>OB_WIRE && (G.vd->flag & V3D_ZBUF_SELECT));
1093         else {
1094                 G.vd->flag &= ~V3D_NEEDBACKBUFDRAW;
1095                 return;
1096         }
1097
1098         if( !(G.vd->flag & V3D_NEEDBACKBUFDRAW) ) return;
1099
1100         if(test) {
1101                 if(qtest()) {
1102                         addafterqueue(curarea->win, BACKBUFDRAW, 1);
1103                         return;
1104                 }
1105         }
1106         persp(PERSP_VIEW);
1107
1108         /*Disable FSAA for backbuffer selection.  
1109         
1110         Only works if GL_MULTISAMPLE_ARB is defined by the header
1111         file, which is should be for every OS that supports FSAA.*/
1112
1113 #ifdef GL_MULTISAMPLE_ARB
1114         m = glIsEnabled(GL_MULTISAMPLE_ARB);
1115         if (m) glDisable(GL_MULTISAMPLE_ARB);
1116 #endif
1117
1118 #ifdef __APPLE__
1119         glDrawBuffer(GL_AUX0);
1120 #endif  
1121         if(G.vd->drawtype > OB_WIRE) G.vd->zbuf= TRUE;
1122         curarea->win_swap &= ~WIN_BACK_OK;
1123         
1124         glDisable(GL_DITHER);
1125
1126         glClearColor(0.0, 0.0, 0.0, 0.0); 
1127         if(G.vd->zbuf) {
1128                 glEnable(GL_DEPTH_TEST);
1129                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1130         }
1131         else {
1132                 glClear(GL_COLOR_BUFFER_BIT);
1133                 glDisable(GL_DEPTH_TEST);
1134         }
1135         
1136         if(G.vd->flag & V3D_CLIPPING)
1137                 view3d_set_clipping(G.vd);
1138         
1139         G.f |= G_BACKBUFSEL;
1140         
1141         base= (G.scene->basact);
1142         if(base && (base->lay & G.vd->lay)) {
1143                 draw_object_backbufsel(base->object);
1144         }
1145
1146         G.vd->flag &= ~V3D_NEEDBACKBUFDRAW;
1147
1148         G.f &= ~G_BACKBUFSEL;
1149         G.vd->zbuf= FALSE; 
1150         glDisable(GL_DEPTH_TEST);
1151         glEnable(GL_DITHER);
1152
1153 #ifdef __APPLE__
1154         glDrawBuffer(GL_BACK); /* we were in aux buffers */
1155 #endif
1156
1157         if(G.vd->flag & V3D_CLIPPING)
1158                 view3d_clr_clipping();
1159
1160 #ifdef GL_MULTISAMPLE_ARB
1161         if (m) glEnable(GL_MULTISAMPLE_ARB);
1162 #endif
1163
1164         /* it is important to end a view in a transform compatible with buttons */
1165         persp(PERSP_WIN);  // set ortho
1166         bwin_scalematrix(curarea->win, G.vd->blockscale, G.vd->blockscale, G.vd->blockscale);
1167
1168 }
1169
1170 void check_backbuf(void)
1171 {
1172         if(G.vd->flag & V3D_NEEDBACKBUFDRAW)
1173                 backdrawview3d(0);
1174 }
1175
1176 /* samples a single pixel (copied from vpaint) */
1177 unsigned int sample_backbuf(int x, int y)
1178 {
1179         unsigned int col;
1180         
1181         if(x>=curarea->winx || y>=curarea->winy) return 0;
1182         x+= curarea->winrct.xmin;
1183         y+= curarea->winrct.ymin;
1184         
1185         check_backbuf(); // actually not needed for apple
1186
1187 #ifdef __APPLE__
1188         glReadBuffer(GL_AUX0);
1189 #endif
1190         glReadPixels(x,  y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,  &col);
1191         glReadBuffer(GL_BACK);  
1192         
1193         if(G.order==B_ENDIAN) SWITCH_INT(col);
1194         
1195         return framebuffer_to_index(col);
1196 }
1197
1198 /* reads full rect, converts indices */
1199 ImBuf *read_backbuf(short xmin, short ymin, short xmax, short ymax)
1200 {
1201         unsigned int *dr, *rd;
1202         struct ImBuf *ibuf, *ibuf1;
1203         int a;
1204         short xminc, yminc, xmaxc, ymaxc, xs, ys;
1205         
1206         /* clip */
1207         if(xmin<0) xminc= 0; else xminc= xmin;
1208         if(xmax>=curarea->winx) xmaxc= curarea->winx-1; else xmaxc= xmax;
1209         if(xminc > xmaxc) return NULL;
1210
1211         if(ymin<0) yminc= 0; else yminc= ymin;
1212         if(ymax>=curarea->winy) ymaxc= curarea->winy-1; else ymaxc= ymax;
1213         if(yminc > ymaxc) return NULL;
1214         
1215         ibuf= IMB_allocImBuf((xmaxc-xminc+1), (ymaxc-yminc+1), 32, IB_rect,0);
1216
1217         check_backbuf(); // actually not needed for apple
1218         
1219 #ifdef __APPLE__
1220         glReadBuffer(GL_AUX0);
1221 #endif
1222         glReadPixels(curarea->winrct.xmin+xminc, curarea->winrct.ymin+yminc, (xmaxc-xminc+1), (ymaxc-yminc+1), GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
1223         glReadBuffer(GL_BACK);  
1224
1225         if(G.order==B_ENDIAN) IMB_convert_rgba_to_abgr(ibuf);
1226
1227         a= (xmaxc-xminc+1)*(ymaxc-yminc+1);
1228         dr= ibuf->rect;
1229         while(a--) {
1230                 if(*dr) *dr= framebuffer_to_index(*dr);
1231                 dr++;
1232         }
1233         
1234         /* put clipped result back, if needed */
1235         if(xminc==xmin && xmaxc==xmax && yminc==ymin && ymaxc==ymax) 
1236                 return ibuf;
1237         
1238         ibuf1= IMB_allocImBuf( (xmax-xmin+1),(ymax-ymin+1),32,IB_rect,0);
1239         rd= ibuf->rect;
1240         dr= ibuf1->rect;
1241                 
1242         for(ys= ymin; ys<=ymax; ys++) {
1243                 for(xs= xmin; xs<=xmax; xs++, dr++) {
1244                         if( xs>=xminc && xs<=xmaxc && ys>=yminc && ys<=ymaxc) {
1245                                 *dr= *rd;
1246                                 rd++;
1247                         }
1248                 }
1249         }
1250         IMB_freeImBuf(ibuf);
1251         return ibuf1;
1252 }
1253
1254 /* smart function to sample a rect spiralling outside, nice for backbuf selection */
1255 unsigned int sample_backbuf_rect(short mval[2], int size, unsigned int min, unsigned int max, int *dist, short strict, unsigned int (*indextest)(unsigned int index))
1256 {
1257         struct ImBuf *buf;
1258         unsigned int *bufmin, *bufmax, *tbuf;
1259         int minx, miny;
1260         int a, b, rc, nr, amount, dirvec[4][2];
1261         int distance=0;
1262         unsigned int index = 0;
1263         short indexok = 0;      
1264
1265         amount= (size-1)/2;
1266
1267         minx = mval[0]-(amount+1);
1268         miny = mval[1]-(amount+1);
1269         buf = read_backbuf(minx, miny, minx+size-1, miny+size-1);
1270         if (!buf) return 0;
1271
1272         rc= 0;
1273         
1274         dirvec[0][0]= 1; dirvec[0][1]= 0;
1275         dirvec[1][0]= 0; dirvec[1][1]= -size;
1276         dirvec[2][0]= -1; dirvec[2][1]= 0;
1277         dirvec[3][0]= 0; dirvec[3][1]= size;
1278         
1279         bufmin = buf->rect;
1280         tbuf = buf->rect;
1281         bufmax = buf->rect + size*size;
1282         tbuf+= amount*size+ amount;
1283         
1284         for(nr=1; nr<=size; nr++) {
1285                 
1286                 for(a=0; a<2; a++) {
1287                         for(b=0; b<nr; b++, distance++) {
1288                                 if (*tbuf && *tbuf>=min && *tbuf<max) { //we got a hit
1289                                         if(strict){
1290                                                 indexok =  indextest(*tbuf - min+1);
1291                                                 if(indexok){
1292                                                         *dist= (short) sqrt( (float)distance   );
1293                                                         index = *tbuf - min+1;
1294                                                         goto exit; 
1295                                                 }                                               
1296                                         }
1297                                         else{
1298                                                 *dist= (short) sqrt( (float)distance ); // XXX, this distance is wrong - 
1299                                                 index = *tbuf - min+1; // messy yah, but indices start at 1
1300                                                 goto exit;
1301                                         }                       
1302                                 }
1303                                 
1304                                 tbuf+= (dirvec[rc][0]+dirvec[rc][1]);
1305                                 
1306                                 if(tbuf<bufmin || tbuf>=bufmax) {
1307                                         goto exit;
1308                                 }
1309                         }
1310                         rc++;
1311                         rc &= 3;
1312                 }
1313         }
1314
1315 exit:
1316         IMB_freeImBuf(buf);
1317         return index;
1318 }
1319
1320 void drawname(Object *ob)
1321 {
1322         cpack(0x404040);
1323         glRasterPos3f(0.0,  0.0,  0.0);
1324         
1325         BMF_DrawString(G.font, " ");
1326         BMF_DrawString(G.font, ob->id.name+2);
1327 }
1328
1329
1330 static void draw_selected_name(Object *ob)
1331 {
1332         char info[128];
1333         short offset=30;
1334
1335         if(ob->type==OB_ARMATURE) {
1336                 bArmature *arm= ob->data;
1337                 char *name= NULL;
1338                 
1339                 if(ob==G.obedit) {
1340                         EditBone *ebo;
1341                         for (ebo=G.edbo.first; ebo; ebo=ebo->next){
1342                                 if ((ebo->flag & BONE_ACTIVE) && (ebo->layer & arm->layer)) {
1343                                         name= ebo->name;
1344                                         break;
1345                                 }
1346                         }
1347                 }
1348                 else if(ob->pose && (ob->flag & OB_POSEMODE)) {
1349                         bPoseChannel *pchan;
1350                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1351                                 if((pchan->bone->flag & BONE_ACTIVE) && (pchan->bone->layer & arm->layer)) {
1352                                         name= pchan->name;
1353                                         break;
1354                                 }
1355                         }
1356                 }
1357                 if(name)
1358                         sprintf(info, "(%d) %s %s", CFRA, ob->id.name+2, name);
1359                 else
1360                         sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
1361         }
1362         else if(ob->type==OB_MESH) {
1363                 Key *key= NULL;
1364                 KeyBlock *kb = NULL;
1365                 char shapes[75];
1366                 
1367                 shapes[0] = 0;
1368                 key = ob_get_key(ob);
1369                 if(key){
1370                         kb = BLI_findlink(&key->block, ob->shapenr-1);
1371                         if(kb){
1372                                 sprintf(shapes, ": %s ", kb->name);             
1373                                 if(ob->shapeflag == OB_SHAPE_LOCK){
1374                                         sprintf(shapes, "%s (Pinned)",shapes);
1375                                 }
1376                         }
1377                 }
1378                 sprintf(info, "(%d) %s %s", CFRA, ob->id.name+2, shapes);
1379         }
1380         else sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
1381
1382         BIF_ThemeColor(TH_TEXT_HI);
1383         if (U.uiflag & USER_SHOW_ROTVIEWICON)
1384                 offset = 14 + (U.rvisize * 2);
1385
1386         glRasterPos2i(offset,  10);
1387         BMF_DrawString(G.fonts, info);
1388 }
1389
1390
1391 /* Draw a live substitute of the view icon, which is always shown */
1392 static void draw_view_axis(void)
1393 {
1394         const float k = U.rvisize;   /* axis size */
1395         const float toll = 0.5;      /* used to see when view is quasi-orthogonal */
1396         const float start = k + 1.0; /* axis center in screen coordinates, x=y */
1397         float ydisp = 0.0;          /* vertical displacement to allow obj info text */
1398         
1399         /* rvibright ranges approx. from original axis icon color to gizmo color */
1400         float bright = U.rvibright / 15.0f;
1401         
1402         unsigned char col[3];
1403         unsigned char gridcol[3];
1404         float colf[3];
1405         
1406         float vec[4];
1407         float dx, dy;
1408         float h, s, v;
1409         
1410         /* thickness of lines is proportional to k */
1411         /*      (log(k)-1) gives a more suitable thickness, but fps decreased by about 3 fps */
1412         glLineWidth(k / 10);
1413         //glLineWidth(log(k)-1); // a bit slow
1414         
1415         BIF_GetThemeColor3ubv(TH_GRID, (char *)gridcol);
1416         
1417         /* X */
1418         vec[0] = vec[3] = 1;
1419         vec[1] = vec[2] = 0;
1420         QuatMulVecf(G.vd->viewquat, vec);
1421         
1422         make_axis_color((char *)gridcol, (char *)col, 'x');
1423         rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
1424         s = s<0.5 ? s+0.5 : 1.0;
1425         v = 0.3;
1426         v = (v<1.0-(bright) ? v+bright : 1.0);
1427         hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
1428         glColor3fv(colf);
1429         
1430         dx = vec[0] * k;
1431         dy = vec[1] * k;
1432         fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
1433         if (fabs(dx) > toll || fabs(dy) > toll) {
1434                 glRasterPos2i(start + dx + 2, start + dy + ydisp + 2);
1435                 BMF_DrawString(G.fonts, "x");
1436         }
1437         
1438         /* Y */
1439         vec[1] = vec[3] = 1;
1440         vec[0] = vec[2] = 0;
1441         QuatMulVecf(G.vd->viewquat, vec);
1442         
1443         make_axis_color((char *)gridcol, (char *)col, 'y');
1444         rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
1445         s = s<0.5 ? s+0.5 : 1.0;
1446         v = 0.3;
1447         v = (v<1.0-(bright) ? v+bright : 1.0);
1448         hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
1449         glColor3fv(colf);
1450         
1451         dx = vec[0] * k;
1452         dy = vec[1] * k;
1453         fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
1454         if (fabs(dx) > toll || fabs(dy) > toll) {
1455                 glRasterPos2i(start + dx + 2, start + dy + ydisp + 2);
1456                 BMF_DrawString(G.fonts, "y");
1457         }
1458         
1459         /* Z */
1460         vec[2] = vec[3] = 1;
1461         vec[1] = vec[0] = 0;
1462         QuatMulVecf(G.vd->viewquat, vec);
1463         
1464         make_axis_color((char *)gridcol, (char *)col, 'z');
1465         rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
1466         s = s<0.5 ? s+0.5 : 1.0;
1467         v = 0.5;
1468         v = (v<1.0-(bright) ? v+bright : 1.0);
1469         hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
1470         glColor3fv(colf);
1471         
1472         dx = vec[0] * k;
1473         dy = vec[1] * k;
1474         fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
1475         if (fabs(dx) > toll || fabs(dy) > toll) {
1476                 glRasterPos2i(start + dx + 2, start + dy + ydisp + 2);
1477                 BMF_DrawString(G.fonts, "z");
1478         }
1479         
1480         /* restore line-width */
1481         glLineWidth(1.0);
1482 }
1483
1484         
1485 static void draw_view_icon(void)
1486 {
1487         BIFIconID icon;
1488         
1489         if(G.vd->view==7) icon= ICON_AXIS_TOP;
1490         else if(G.vd->view==1) icon= ICON_AXIS_FRONT;
1491         else if(G.vd->view==3) icon= ICON_AXIS_SIDE;
1492         else return ;
1493
1494         glEnable(GL_BLEND);
1495         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
1496         
1497         BIF_icon_draw(5.0, 5.0, icon);
1498         
1499         glDisable(GL_BLEND);
1500 }
1501
1502 static void draw_viewport_name(ScrArea *sa)
1503 {
1504         char *name = NULL;
1505         char *printable = NULL;
1506         
1507         switch(G.vd->view) {
1508                 case 1:
1509                         if (G.vd->persp == V3D_ORTHO)
1510                                 name = (G.vd->flag2 & V3D_OPP_DIRECTION_NAME) ? "Back Ortho" : "Front Ortho";
1511                         else
1512                                 name = (G.vd->flag2 & V3D_OPP_DIRECTION_NAME) ? "Back Persp" : "Front Persp";
1513                         break;
1514                 case 3:
1515                         if (G.vd->persp == V3D_ORTHO)
1516                                 name = (G.vd->flag2 & V3D_OPP_DIRECTION_NAME) ? "Left Ortho" : "Right Ortho";
1517                         else
1518                                 name = (G.vd->flag2 & V3D_OPP_DIRECTION_NAME) ? "Left Persp" : "Right Persp";
1519                         break;
1520                 case 7:
1521                         if (G.vd->persp == V3D_ORTHO)
1522                                 name = (G.vd->flag2 & V3D_OPP_DIRECTION_NAME) ? "Bottom Ortho" : "Top Ortho";
1523                         else
1524                                 name = (G.vd->flag2 & V3D_OPP_DIRECTION_NAME) ? "Bottom Persp" : "Top Persp";
1525                         break;
1526                 default:
1527                         if (G.vd->persp==V3D_CAMOB) {
1528                                 if ((G.vd->camera) && (G.vd->camera->type == OB_CAMERA)) {
1529                                         Camera *cam;
1530                                         cam = G.vd->camera->data;
1531                                         name = (cam->type != CAM_ORTHO) ? "Camera Persp" : "Camera Ortho";
1532                                 } else {
1533                                         name = "Object as Camera";
1534                                 }
1535                         } else { 
1536                                 name = (G.vd->persp == V3D_ORTHO) ? "User Ortho" : "User Persp";
1537                         }
1538         }
1539         
1540         if (G.vd->localview) {
1541                 printable = malloc(strlen(name) + strlen(" (Local)_")); /* '_' gives space for '\0' */
1542                 strcpy(printable, name);
1543                 strcat(printable, " (Local)");
1544         } else {
1545                 printable = name;
1546         }
1547
1548         if (printable) {
1549                 BIF_ThemeColor(TH_TEXT_HI);
1550                 glRasterPos2i(10,  sa->winy-20);
1551                 BMF_DrawString(G.fonts, printable);
1552         }
1553
1554         if (G.vd->localview) {
1555                 free(printable);
1556         }
1557 }
1558
1559 /* ******************* view3d space & buttons ************** */
1560
1561
1562 /* temporal struct for storing transform properties */
1563 typedef struct {
1564         float ob_eul[4];        // used for quat too....
1565         float ob_scale[3]; // need temp space due to linked values
1566         float ob_dims[3];
1567         short link_scale;
1568         float ve_median[5];
1569         int curdef;
1570         float *defweightp;
1571 } TransformProperties;
1572
1573 /* is used for both read and write... */
1574 static void v3d_editvertex_buts(uiBlock *block, Object *ob, float lim)
1575 {
1576         EditMesh *em = G.editMesh;
1577         EditVert *eve, *evedef=NULL;
1578         EditEdge *eed;
1579         MDeformVert *dvert=NULL;
1580         TransformProperties *tfp= G.vd->properties_storage;
1581         float median[5], ve_median[5];
1582         int tot, totw, totweight, totedge;
1583         char defstr[320];
1584         
1585         median[0]= median[1]= median[2]= median[3]= median[4]= 0.0;
1586         tot= totw= totweight= totedge= 0;
1587         defstr[0]= 0;
1588
1589         if(ob->type==OB_MESH) {         
1590                 eve= em->verts.first;
1591                 while(eve) {
1592                         if(eve->f & SELECT) {
1593                                 evedef= eve;
1594                                 tot++;
1595                                 VecAddf(median, median, eve->co);
1596                         }
1597                         eve= eve->next;
1598                 }
1599                 eed= em->edges.first;
1600                 while(eed) {
1601                         if((eed->f & SELECT)) {
1602                                 totedge++;
1603                                 median[3]+= eed->crease;
1604                         }
1605                         eed= eed->next;
1606                 }
1607
1608                 /* check for defgroups */
1609                 if(evedef)
1610                         dvert= CustomData_em_get(&em->vdata, evedef->data, CD_MDEFORMVERT);
1611                 if(tot==1 && dvert && dvert->totweight) {
1612                         bDeformGroup *dg;
1613                         int i, max=1, init=1;
1614                         char str[320];
1615                         
1616                         for (i=0; i<dvert->totweight; i++){
1617                                 dg = BLI_findlink (&ob->defbase, dvert->dw[i].def_nr);
1618                                 if(dg) {
1619                                         max+= snprintf(str, sizeof(str), "%s %%x%d|", dg->name, dvert->dw[i].def_nr); 
1620                                         if(max<320) strcat(defstr, str);
1621                                 }
1622                                 else printf("oh no!\n");
1623                                 if(tfp->curdef==dvert->dw[i].def_nr) {
1624                                         init= 0;
1625                                         tfp->defweightp= &dvert->dw[i].weight;
1626                                 }
1627                         }
1628                         
1629                         if(init) {      // needs new initialized 
1630                                 tfp->curdef= dvert->dw[0].def_nr;
1631                                 tfp->defweightp= &dvert->dw[0].weight;
1632                         }
1633                 }
1634         }
1635         else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
1636                 extern ListBase editNurb; /* editcurve.c */
1637                 Nurb *nu;
1638                 BPoint *bp;
1639                 BezTriple *bezt;
1640                 int a;
1641                 
1642                 nu= editNurb.first;
1643                 while(nu) {
1644                         if((nu->type & 7)==CU_BEZIER) {
1645                                 bezt= nu->bezt;
1646                                 a= nu->pntsu;
1647                                 while(a--) {
1648                                         if(bezt->f2 & SELECT) {
1649                                                 VecAddf(median, median, bezt->vec[1]);
1650                                                 tot++;
1651                                                 median[4]+= bezt->weight;
1652                                                 totweight++;
1653                                         }
1654                                         else {
1655                                                 if(bezt->f1 & SELECT) {
1656                                                         VecAddf(median, median, bezt->vec[0]);
1657                                                         tot++;
1658                                                 }
1659                                                 if(bezt->f3 & SELECT) {
1660                                                         VecAddf(median, median, bezt->vec[2]);
1661                                                         tot++;
1662                                                 }
1663                                         }
1664                                         bezt++;
1665                                 }
1666                         }
1667                         else {
1668                                 bp= nu->bp;
1669                                 a= nu->pntsu*nu->pntsv;
1670                                 while(a--) {
1671                                         if(bp->f1 & SELECT) {
1672                                                 VecAddf(median, median, bp->vec);
1673                                                 median[3]+= bp->vec[3];
1674                                                 totw++;
1675                                                 tot++;
1676                                                 median[4]+= bp->weight;
1677                                                 totweight++;
1678                                         }
1679                                         bp++;
1680                                 }
1681                         }
1682                         nu= nu->next;
1683                 }
1684         }
1685         else if(ob->type==OB_LATTICE) {
1686                 BPoint *bp;
1687                 int a;
1688                 
1689                 a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
1690                 bp= editLatt->def;
1691                 while(a--) {
1692                         if(bp->f1 & SELECT) {
1693                                 VecAddf(median, median, bp->vec);
1694                                 tot++;
1695                                 median[4]+= bp->weight;
1696                                 totweight++;
1697                         }
1698                         bp++;
1699                 }
1700         }
1701         
1702         if(tot==0) return;
1703
1704         median[0] /= (float)tot;
1705         median[1] /= (float)tot;
1706         median[2] /= (float)tot;
1707         if(totedge) median[3] /= (float)totedge;
1708         else if(totw) median[3] /= (float)totw;
1709         if(totweight) median[4] /= (float)totweight;
1710         
1711         if(G.vd->flag & V3D_GLOBAL_STATS)
1712                 Mat4MulVecfl(ob->obmat, median);
1713         
1714         if(block) {     // buttons
1715                 int but_y;
1716                 if((ob->parent) && (ob->partype == PARBONE))    but_y = 135;
1717                 else                                                                                    but_y = 150;
1718                 
1719                 uiBlockBeginAlign(block);
1720                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, REDRAWVIEW3D, "Global",              160, but_y, 70, 19, &G.vd->flag, 0, 0, 0, 0, "Displays global values");
1721                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, REDRAWVIEW3D, "Local",              230, but_y, 70, 19, &G.vd->flag, 0, 0, 0, 0, "Displays local values");
1722                 uiBlockEndAlign(block);
1723                 
1724                 memcpy(tfp->ve_median, median, sizeof(tfp->ve_median));
1725                 
1726                 uiBlockBeginAlign(block);
1727                 if(tot==1) {
1728                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex X:", 10, 110, 290, 19, &(tfp->ve_median[0]), -lim, lim, 10, 3, "");
1729                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex Y:", 10, 90, 290, 19, &(tfp->ve_median[1]), -lim, lim, 10, 3, "");
1730                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex Z:", 10, 70, 290, 19, &(tfp->ve_median[2]), -lim, lim, 10, 3, "");
1731                         if(totw==1)
1732                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex W:", 10, 50, 290, 19, &(tfp->ve_median[3]), 0.01, 100.0, 10, 3, "");
1733                         uiBlockEndAlign(block);
1734         
1735                         if(defstr[0]) {
1736                                 uiDefBut(block, LABEL, 1, "Vertex Deform Groups",               10, 40, 290, 20, NULL, 0.0, 0.0, 0, 0, "");
1737
1738                                 uiBlockBeginAlign(block);
1739                                 uiDefButF(block, NUM, B_NOP, "Weight:",                 10, 20, 150, 19, tfp->defweightp, 0.0f, 1.0f, 10, 3, "Weight value");
1740                                 uiDefButI(block, MENU, REDRAWVIEW3D, defstr,    160, 20, 140, 19, &tfp->curdef, 0.0, 0.0, 0, 0, "Current Vertex Group");
1741                                 uiBlockEndAlign(block);
1742                         }
1743                         else if(totweight)
1744                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   10, 20, 290, 19, &(tfp->ve_median[4]), 0.0, 1.0, 10, 3, "");
1745
1746                 }
1747                 else {
1748                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median X:", 10, 110, 290, 19, &(tfp->ve_median[0]), -lim, lim, 10, 3, "");
1749                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Y:", 10, 90, 290, 19, &(tfp->ve_median[1]), -lim, lim, 10, 3, "");
1750                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Z:", 10, 70, 290, 19, &(tfp->ve_median[2]), -lim, lim, 10, 3, "");
1751                         if(totw==tot)
1752                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median W:", 10, 50, 290, 19, &(tfp->ve_median[3]), 0.01, 100.0, 10, 3, "");
1753                         uiBlockEndAlign(block);
1754                         if(totweight)
1755                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   10, 20, 290, 19, &(tfp->ve_median[4]), 0.0, 1.0, 10, 3, "Weight is used for SoftBody Goal");
1756                 }
1757                 
1758                 if(ob->type==OB_CURVE && (totw==0)) { /* bez curves have no w */
1759                         uiBlockBeginAlign(block);
1760                         uiDefBut(block, BUT,B_SETPT_AUTO,"Auto",        10, 44, 72, 19, 0, 0, 0, 0, 0, "Auto handles (Shift H)");
1761                         uiDefBut(block, BUT,B_SETPT_VECTOR,"Vector",82, 44, 73, 19, 0, 0, 0, 0, 0, "Vector handles (V)");
1762                         uiDefBut(block, BUT,B_SETPT_ALIGN,"Align",155, 44, 73, 19, 0, 0, 0, 0, 0, "Align handles (H Toggles)");
1763                         uiDefBut(block, BUT,B_SETPT_FREE,"Free",        227, 44, 72, 19, 0, 0, 0, 0, 0, "Align handles (H Toggles)");
1764                         uiBlockEndAlign(block);
1765                 }
1766                 
1767                 if(totedge==1)
1768                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Crease W:", 10, 30, 290, 19, &(tfp->ve_median[3]), 0.0, 1.0, 10, 3, "");
1769                 else if(totedge>1)
1770                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Crease W:",  10, 30, 290, 19, &(tfp->ve_median[3]), 0.0, 1.0, 10, 3, "");
1771                 
1772         }
1773         else {  // apply
1774                 memcpy(ve_median, tfp->ve_median, sizeof(tfp->ve_median));
1775                 
1776                 if(G.vd->flag & V3D_GLOBAL_STATS) {
1777                         Mat4Invert(ob->imat, ob->obmat);
1778                         Mat4MulVecfl(ob->imat, median);
1779                         Mat4MulVecfl(ob->imat, ve_median);
1780                 }
1781                 VecSubf(median, ve_median, median);
1782                 median[3]= ve_median[3]-median[3];
1783                 median[4]= ve_median[4]-median[4];
1784                 
1785                 if(ob->type==OB_MESH) {
1786                         
1787                         eve= em->verts.first;
1788                         while(eve) {
1789                                 if(eve->f & SELECT) {
1790                                         VecAddf(eve->co, eve->co, median);
1791                                 }
1792                                 eve= eve->next;
1793                         }
1794                         
1795                         for(eed= em->edges.first; eed; eed= eed->next) {
1796                                 if(eed->f & SELECT) {
1797                                         /* ensure the median can be set to zero or one */
1798                                         if(ve_median[3]==0.0f) eed->crease= 0.0f;
1799                                         else if(ve_median[3]==1.0f) eed->crease= 1.0f;
1800                                         else {
1801                                                 eed->crease+= median[3];
1802                                                 CLAMP(eed->crease, 0.0, 1.0);
1803                                         }
1804                                 }
1805                         }
1806                         
1807                         recalc_editnormals();
1808                 }
1809                 else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
1810                         extern ListBase editNurb; /* editcurve.c */
1811                         Nurb *nu;
1812                         BPoint *bp;
1813                         BezTriple *bezt;
1814                         int a;
1815                         
1816                         nu= editNurb.first;
1817                         while(nu) {
1818                                 if((nu->type & 7)==1) {
1819                                         bezt= nu->bezt;
1820                                         a= nu->pntsu;
1821                                         while(a--) {
1822                                                 if(bezt->f2 & SELECT) {
1823                                                         VecAddf(bezt->vec[0], bezt->vec[0], median);
1824                                                         VecAddf(bezt->vec[1], bezt->vec[1], median);
1825                                                         VecAddf(bezt->vec[2], bezt->vec[2], median);
1826                                                         bezt->weight+= median[4];
1827                                                 }
1828                                                 else {
1829                                                         if(bezt->f1 & SELECT) {
1830                                                                 VecAddf(bezt->vec[0], bezt->vec[0], median);
1831                                                         }
1832                                                         if(bezt->f3 & SELECT) {
1833                                                                 VecAddf(bezt->vec[2], bezt->vec[2], median);
1834                                                         }
1835                                                 }
1836                                                 bezt++;
1837                                         }
1838                                 }
1839                                 else {
1840                                         bp= nu->bp;
1841                                         a= nu->pntsu*nu->pntsv;
1842                                         while(a--) {
1843                                                 if(bp->f1 & SELECT) {
1844                                                         VecAddf(bp->vec, bp->vec, median);
1845                                                         bp->vec[3]+= median[3];
1846                                                         bp->weight+= median[4];
1847                                                 }
1848                                                 bp++;
1849                                         }
1850                                 }
1851                                 test2DNurb(nu);
1852                                 testhandlesNurb(nu); /* test for bezier too */
1853
1854                                 nu= nu->next;
1855                         }
1856                 }
1857                 else if(ob->type==OB_LATTICE) {
1858                         BPoint *bp;
1859                         int a;
1860                         
1861                         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
1862                         bp= editLatt->def;
1863                         while(a--) {
1864                                 if(bp->f1 & SELECT) {
1865                                         VecAddf(bp->vec, bp->vec, median);
1866                                         bp->weight+= median[4];
1867                                 }
1868                                 bp++;
1869                         }
1870                 }
1871                 
1872                 BIF_undo_push("Transform properties");
1873         }
1874 }
1875
1876 /* assumes armature active */
1877 static void validate_bonebutton_cb(void *bonev, void *namev)
1878 {
1879         Object *ob= OBACT;
1880         
1881         if(ob && ob->type==OB_ARMATURE) {
1882                 Bone *bone= bonev;
1883                 char oldname[32], newname[32];
1884                 
1885                 /* need to be on the stack */
1886                 BLI_strncpy(newname, bone->name, 32);
1887                 BLI_strncpy(oldname, (char *)namev, 32);
1888                 /* restore */
1889                 BLI_strncpy(bone->name, oldname, 32);
1890                 
1891                 armature_bone_rename(ob->data, oldname, newname); // editarmature.c
1892                 allqueue(REDRAWALL, 0);
1893         }
1894 }
1895
1896 static void v3d_posearmature_buts(uiBlock *block, Object *ob, float lim)
1897 {
1898         uiBut *but;
1899         bArmature *arm;
1900         bPoseChannel *pchan;
1901         Bone *bone= NULL;
1902         TransformProperties *tfp= G.vd->properties_storage;
1903
1904         arm = get_armature(OBACT);
1905         if (!arm || !ob->pose) return;
1906
1907         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1908                 bone = pchan->bone;
1909                 if(bone && (bone->flag & BONE_ACTIVE) && (bone->layer & arm->layer))
1910                         break;
1911         }
1912         if (!pchan || !bone) return;
1913
1914         if((ob->parent) && (ob->partype == PARBONE))
1915                 but= uiDefBut (block, TEX, B_DIFF, "Bone:",                             160, 130, 140, 19, bone->name, 1, 31, 0, 0, "");
1916         else
1917                 but= uiDefBut(block, TEX, B_DIFF, "Bone:",                              160, 140, 140, 19, bone->name, 1, 31, 0, 0, "");
1918         uiButSetFunc(but, validate_bonebutton_cb, bone, NULL);
1919         
1920         QuatToEul(pchan->quat, tfp->ob_eul);
1921         tfp->ob_eul[0]*= 180.0/M_PI;
1922         tfp->ob_eul[1]*= 180.0/M_PI;
1923         tfp->ob_eul[2]*= 180.0/M_PI;
1924         
1925         uiBlockBeginAlign(block);
1926         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCX, REDRAWVIEW3D, ICON_UNLOCKED,     10,140,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1927         uiDefButF(block, NUM, B_ARMATUREPANEL2, "LocX:",        30, 140, 120, 19, pchan->loc, -lim, lim, 100, 3, "");
1928         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCY, REDRAWVIEW3D, ICON_UNLOCKED,     10,120,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1929         uiDefButF(block, NUM, B_ARMATUREPANEL2, "LocY:",        30, 120, 120, 19, pchan->loc+1, -lim, lim, 100, 3, "");
1930         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCZ, REDRAWVIEW3D, ICON_UNLOCKED,     10,100,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1931         uiDefButF(block, NUM, B_ARMATUREPANEL2, "LocZ:",        30, 100, 120, 19, pchan->loc+2, -lim, lim, 100, 3, "");
1932
1933         uiBlockBeginAlign(block);
1934         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTX, REDRAWVIEW3D, ICON_UNLOCKED,     10,70,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1935         uiDefButF(block, NUM, B_ARMATUREPANEL3, "RotX:",        30, 70, 120, 19, tfp->ob_eul, -1000.0, 1000.0, 100, 3, "");
1936         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTY, REDRAWVIEW3D, ICON_UNLOCKED,     10,50,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1937         uiDefButF(block, NUM, B_ARMATUREPANEL3, "RotY:",        30, 50, 120, 19, tfp->ob_eul+1, -1000.0, 1000.0, 100, 3, "");
1938         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTZ, REDRAWVIEW3D, ICON_UNLOCKED,     10,30,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1939         uiDefButF(block, NUM, B_ARMATUREPANEL3, "RotZ:",        30, 30, 120, 19, tfp->ob_eul+2, -1000.0, 1000.0, 100, 3, "");
1940         
1941         uiBlockBeginAlign(block);
1942         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEX, REDRAWVIEW3D, ICON_UNLOCKED,   160,70,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1943         uiDefButF(block, NUM, B_ARMATUREPANEL2, "ScaleX:",      180, 70, 120, 19, pchan->size, -lim, lim, 10, 3, "");
1944         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEY, REDRAWVIEW3D, ICON_UNLOCKED,   160,50,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1945         uiDefButF(block, NUM, B_ARMATUREPANEL2, "ScaleY:",      180, 50, 120, 19, pchan->size+1, -lim, lim, 10, 3, "");
1946         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEZ, REDRAWVIEW3D, ICON_UNLOCKED,   160,30,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1947         uiDefButF(block, NUM, B_ARMATUREPANEL2, "ScaleZ:",      180, 30, 120, 19, pchan->size+2, -lim, lim, 10, 3, "");
1948         uiBlockEndAlign(block);
1949 }
1950
1951 static void v3d_editarmature_buts(uiBlock *block, Object *ob, float lim)
1952 {
1953         bArmature *arm= G.obedit->data;
1954         EditBone *ebone;
1955         uiBut *but;
1956         TransformProperties *tfp= G.vd->properties_storage;
1957         
1958         ebone= G.edbo.first;
1959
1960         for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1961                 if ((ebone->flag & BONE_ACTIVE) && (ebone->layer & arm->layer))
1962                         break;
1963         }
1964
1965         if (!ebone)
1966                 return;
1967         
1968         if((ob->parent) && (ob->partype == PARBONE))
1969                 but= uiDefBut(block, TEX, B_DIFF, "Bone:", 160, 130, 140, 19, ebone->name, 1, 31, 0, 0, "");
1970         else
1971                 but= uiDefBut(block, TEX, B_DIFF, "Bone:",                      160, 150, 140, 19, ebone->name, 1, 31, 0, 0, "");
1972         uiButSetFunc(but, validate_editbonebutton_cb, ebone, NULL);
1973
1974         uiBlockBeginAlign(block);
1975         uiDefButF(block, NUM, B_ARMATUREPANEL1, "HeadX:",       10, 70, 140, 19, ebone->head, -lim, lim, 10, 3, "");
1976         uiDefButF(block, NUM, B_ARMATUREPANEL1, "HeadY:",       10, 50, 140, 19, ebone->head+1, -lim, lim, 10, 3, "");
1977         uiDefButF(block, NUM, B_ARMATUREPANEL1, "HeadZ:",       10, 30, 140, 19, ebone->head+2, -lim, lim, 10, 3, "");
1978         uiBlockBeginAlign(block);
1979         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TailX:",       160, 70, 140, 19, ebone->tail, -lim, lim, 10, 3, "");
1980         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TailY:",       160, 50, 140, 19, ebone->tail+1, -lim, lim, 10, 3, "");
1981         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TailZ:",       160, 30, 140, 19, ebone->tail+2, -lim, lim, 10, 3, "");
1982         uiBlockEndAlign(block);
1983         
1984         tfp->ob_eul[0]= 180.0*ebone->roll/M_PI;
1985         uiDefButF(block, NUM, B_ARMATUREPANEL1, "Roll:",        10, 100, 140, 19, tfp->ob_eul, -lim, lim, 1000, 3, "");
1986
1987         uiBlockBeginAlign(block);
1988         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TailRadius:",  10, 150, 140, 19, &ebone->rad_tail, 0, lim, 10, 3, "");
1989         if (ebone->parent && ebone->flag & BONE_CONNECTED )
1990                 uiDefButF(block, NUM, B_ARMATUREPANEL1, "HeadRadius:",  10, 130, 140, 19, &ebone->parent->rad_tail, 0, lim, 10, 3, "");
1991         else
1992                 uiDefButF(block, NUM, B_ARMATUREPANEL1, "HeadRadius:",  10, 130, 140, 19, &ebone->rad_head, 0, lim, 10, 3, "");
1993         uiBlockEndAlign(block);
1994 }
1995
1996 static void v3d_editmetaball_buts(uiBlock *block, Object *ob, float lim)
1997 {
1998         extern MetaElem *lastelem;
1999
2000         if(lastelem) {
2001                 uiBlockBeginAlign(block);
2002                 uiDefButF(block, NUM, B_RECALCMBALL, "LocX:", 10, 70, 140, 19, &lastelem->x, -lim, lim, 100, 3, "");
2003                 uiDefButF(block, NUM, B_RECALCMBALL, "LocY:", 10, 50, 140, 19, &lastelem->y, -lim, lim, 100, 3, "");
2004                 uiDefButF(block, NUM, B_RECALCMBALL, "LocZ:", 10, 30, 140, 19, &lastelem->z, -lim, lim, 100, 3, "");
2005
2006                 uiBlockBeginAlign(block);
2007                 if(lastelem->type!=MB_BALL)
2008                         uiDefButF(block, NUM, B_RECALCMBALL, "dx:", 160, 70, 140, 19, &lastelem->expx, 0, lim, 100, 3, "");
2009                 if((lastelem->type!=MB_BALL) && (lastelem->type!=MB_TUBE))
2010                         uiDefButF(block, NUM, B_RECALCMBALL, "dy:", 160, 50, 140, 19, &lastelem->expy, 0, lim, 100, 3, "");
2011                 if((lastelem->type==MB_ELIPSOID) || (lastelem->type==MB_CUBE))
2012                         uiDefButF(block, NUM, B_RECALCMBALL, "dz:", 160, 30, 140, 19, &lastelem->expz, 0, lim, 100, 3, "");
2013
2014                 uiBlockEndAlign(block); 
2015
2016                 uiBlockBeginAlign(block);
2017                 uiDefButF(block, NUM, B_RECALCMBALL, "Radius:", 10, 120, 140, 19, &lastelem->rad, 0, lim, 100, 3, "Size of the active metaball");
2018                 uiDefButF(block, NUM, B_RECALCMBALL, "Stiffness:", 10, 100, 140, 19, &lastelem->s, 0, 10, 100, 3, "Stiffness of the active metaball");
2019                 uiBlockEndAlign(block);
2020                 
2021                 uiDefButS(block, MENU, B_RECALCMBALL, "Type%t|Ball%x0|Tube%x4|Plane%x5|Elipsoid%x6|Cube%x7", 160, 120, 140, 19, &lastelem->type, 0.0, 0.0, 0, 0, "Set active element type");
2022                 
2023         }
2024 }
2025
2026 void do_viewbuts(unsigned short event)
2027 {
2028         BoundBox *bb;
2029         View3D *vd;
2030         Object *ob= OBACT;
2031         TransformProperties *tfp= G.vd->properties_storage;
2032         
2033         vd= G.vd;
2034         if(vd==NULL) return;
2035
2036         switch(event) {
2037                 
2038         case B_OBJECTPANEL:
2039                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
2040                 allqueue(REDRAWVIEW3D, 1);
2041                 break;
2042                 
2043         case B_OBJECTPANELROT:
2044                 if(ob) {
2045                         ob->rot[0]= M_PI*tfp->ob_eul[0]/180.0;
2046                         ob->rot[1]= M_PI*tfp->ob_eul[1]/180.0;
2047                         ob->rot[2]= M_PI*tfp->ob_eul[2]/180.0;
2048                         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
2049                         allqueue(REDRAWVIEW3D, 1);
2050                 }
2051                 break;
2052
2053         case B_OBJECTPANELSCALE:
2054                 if(ob) {
2055
2056                         /* link scale; figure out which axis changed */
2057                         if (tfp->link_scale) {
2058                                 float ratio, tmp, max = 0.0;
2059                                 int axis;
2060                                 
2061                                 axis = 0;
2062                                 max = fabs(tfp->ob_scale[0] - ob->size[0]);
2063                                 tmp = fabs(tfp->ob_scale[1] - ob->size[1]);
2064                                 if (tmp > max) {
2065                                         axis = 1;
2066                                         max = tmp;
2067                                 }
2068                                 tmp = fabs(tfp->ob_scale[2] - ob->size[2]);
2069                                 if (tmp > max) {
2070                                         axis = 2;
2071                                         max = tmp;
2072                                 }
2073                         
2074                                 if (ob->size[axis] != tfp->ob_scale[axis]) {
2075                                         if (fabs(ob->size[axis]) > FLT_EPSILON) {
2076                                                 ratio = tfp->ob_scale[axis] / ob->size[axis];
2077                                                 ob->size[0] *= ratio;
2078                                                 ob->size[1] *= ratio;
2079                                                 ob->size[2] *= ratio;
2080                                         }
2081                                 }
2082                         }
2083                         else {
2084                                 VECCOPY(ob->size, tfp->ob_scale);
2085                                 
2086                         }
2087                         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
2088                         allqueue(REDRAWVIEW3D, 1);
2089                 }
2090                 break;
2091
2092         case B_OBJECTPANELDIMS:
2093                 bb= object_get_boundbox(ob);
2094                 if(bb) {
2095                         float old_dims[3], scale[3], ratio, len[3];
2096                         int axis;
2097
2098                         Mat4ToSize(ob->obmat, scale);
2099
2100                         len[0] = bb->vec[4][0] - bb->vec[0][0];
2101                         len[1] = bb->vec[2][1] - bb->vec[0][1];
2102                         len[2] = bb->vec[1][2] - bb->vec[0][2];
2103
2104                         old_dims[0] = fabs(scale[0]) * len[0];
2105                         old_dims[1] = fabs(scale[1]) * len[1];
2106                         old_dims[2] = fabs(scale[2]) * len[2];
2107
2108                         /* for each axis changed */
2109                         for (axis = 0; axis<3; axis++) {
2110                                 if (fabs(old_dims[axis] - tfp->ob_dims[axis]) > 0.0001) {
2111                                         if (old_dims[axis] > 0.0) {
2112                                                 ratio = tfp->ob_dims[axis] / old_dims[axis]; 
2113                                                 if (tfp->link_scale) {
2114                                                         ob->size[0] *= ratio;
2115                                                         ob->size[1] *= ratio;
2116                                                         ob->size[2] *= ratio;
2117                                                         break;
2118                                                 }
2119                                                 else {
2120                                                         ob->size[axis] *= ratio;
2121                                                 }
2122                                         }
2123                                         else {
2124                                                 if (len[axis] > 0) {
2125                                                         ob->size[axis] = tfp->ob_dims[axis] / len[axis];
2126                                                 }
2127                                         }
2128                                 }
2129                         }
2130                         
2131                         /* prevent multiple B_OBJECTPANELDIMS events to keep scaling, cycling with TAB on buttons can cause that */
2132                         VECCOPY(tfp->ob_dims, old_dims);
2133                         
2134                         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
2135                         allqueue(REDRAWVIEW3D, 1);
2136                 }
2137                 break;
2138         
2139         case B_OBJECTPANELMEDIAN:
2140                 if(ob) {
2141                         v3d_editvertex_buts(NULL, ob, 1.0);
2142                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2143                         allqueue(REDRAWVIEW3D, 1);
2144                 }
2145                 break;
2146                 
2147                 /* note; this case also used for parbone */
2148         case B_OBJECTPANELPARENT:
2149                 if(ob) {
2150                         if(ob->id.lib || test_parent_loop(ob->parent, ob) ) 
2151                                 ob->parent= NULL;
2152                         else {
2153                                 DAG_scene_sort(G.scene);
2154                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
2155                         }
2156                         allqueue(REDRAWVIEW3D, 1);
2157                         allqueue(REDRAWBUTSOBJECT, 0);
2158                         allqueue(REDRAWOOPS, 0);
2159                 }
2160                 break;
2161                 
2162         case B_ARMATUREPANEL1:
2163                 {
2164                         bArmature *arm= G.obedit->data;
2165                         EditBone *ebone, *child;
2166                         
2167                         for (ebone = G.edbo.first; ebone; ebone=ebone->next){
2168                                 if ((ebone->flag & BONE_ACTIVE) && (ebone->layer & arm->layer))
2169                                         break;
2170                         }
2171                         if (ebone) {
2172                                 ebone->roll= M_PI*tfp->ob_eul[0]/180.0;
2173                                 //      Update our parent
2174                                 if (ebone->parent && ebone->flag & BONE_CONNECTED){
2175                                         VECCOPY (ebone->parent->tail, ebone->head);
2176                                 }
2177                         
2178                                 //      Update our children if necessary
2179                                 for (child = G.edbo.first; child; child=child->next){
2180                                         if (child->parent == ebone && (child->flag & BONE_CONNECTED)){
2181                                                 VECCOPY (child->head, ebone->tail);
2182                                         }
2183                                 }
2184                                 if(arm->flag & ARM_MIRROR_EDIT) {
2185                                         EditBone *eboflip= armature_bone_get_mirrored(ebone);
2186                                         if(eboflip) {
2187                                                 eboflip->roll= -ebone->roll;
2188                                                 eboflip->head[0]= -ebone->head[0];
2189                                                 eboflip->tail[0]= -ebone->tail[0];
2190                                                 
2191                                                 //      Update our parent
2192                                                 if (eboflip->parent && eboflip->flag & BONE_CONNECTED){
2193                                                         VECCOPY (eboflip->parent->tail, eboflip->head);
2194                                                 }
2195                                                 
2196                                                 //      Update our children if necessary
2197                                                 for (child = G.edbo.first; child; child=child->next){
2198                                                         if (child->parent == eboflip && (child->flag & BONE_CONNECTED)){
2199                                                                 VECCOPY (child->head, eboflip->tail);
2200                                                         }
2201                                                 }
2202                                         }
2203                                 }
2204                                 
2205                                 allqueue(REDRAWVIEW3D, 1);
2206                         }
2207                 }
2208                 break;
2209         case B_ARMATUREPANEL3:  // rotate button on channel
2210                 {
2211                         bArmature *arm;
2212                         bPoseChannel *pchan;
2213                         Bone *bone;
2214                         float eul[3];
2215                         
2216                         arm = get_armature(OBACT);
2217                         if (!arm || !ob->pose) return;
2218                                 
2219                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2220                                 bone = pchan->bone;
2221                                 if(bone && (bone->flag & BONE_ACTIVE) && (bone->layer & arm->layer))
2222                                         break;
2223                         }
2224                         if (!pchan) return;
2225                         
2226                         /* make a copy to eul[3], to allow TAB on buttons to work */
2227                         eul[0]= M_PI*tfp->ob_eul[0]/180.0;
2228                         eul[1]= M_PI*tfp->ob_eul[1]/180.0;
2229                         eul[2]= M_PI*tfp->ob_eul[2]/180.0;
2230                         EulToQuat(eul, pchan->quat);
2231                 }
2232                 /* no break, pass on */
2233         case B_ARMATUREPANEL2:
2234                 {
2235                         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
2236                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2237                         allqueue(REDRAWVIEW3D, 1);
2238                 }
2239                 break;
2240         case B_TRANSFORMSPACEADD:
2241                 BIF_manageTransformOrientation(1, 0);
2242                 allqueue(REDRAWVIEW3D, 1);
2243                 break;
2244         case B_TRANSFORMSPACECLEAR:
2245                 BIF_clearTransformOrientation();
2246                 allqueue(REDRAWVIEW3D, 1);
2247         }
2248 }
2249
2250 void removeTransformOrientation_func(void *target, void *unused)
2251 {
2252         BIF_removeTransformOrientation((TransformOrientation *) target);
2253 }
2254
2255 void selectTransformOrientation_func(void *target, void *unused)
2256 {
2257         BIF_selectTransformOrientation((TransformOrientation *) target);
2258 }
2259
2260 static void view3d_panel_transform_spaces(short cntrl)
2261 {
2262         ListBase *transform_spaces = &G.scene->transform_spaces;
2263         TransformOrientation *ts = transform_spaces->first;
2264         uiBlock *block;
2265         uiBut *but;
2266         int xco = 20, yco = 70, height = 140;
2267         int index;
2268
2269         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_transform", UI_EMBOSS, UI_HELV, curarea->win);
2270         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
2271         uiSetPanelHandler(VIEW3D_HANDLER_TRANSFORM);  // for close and esc
2272
2273         if(uiNewPanel(curarea, block, "Transform Orientations", "View3d", 10, 230, 318, height)==0) return;
2274
2275         uiNewPanelHeight(block, height);
2276
2277         uiBlockBeginAlign(block);
2278         
2279         if (G.obedit)
2280                 uiDefBut(block, BUT, B_TRANSFORMSPACEADD, "Add", xco,120,80,20, 0, 0, 0, 0, 0, "Add the selected element as a Transform Orientation");
2281         else
2282                 uiDefBut(block, BUT, B_TRANSFORMSPACEADD, "Add", xco,120,80,20, 0, 0, 0, 0, 0, "Add the active object as a Transform Orientation");
2283
2284         uiDefBut(block, BUT, B_TRANSFORMSPACECLEAR, "Clear", xco + 80,120,80,20, 0, 0, 0, 0, 0, "Removal all Transform Orientations");
2285         
2286         uiBlockEndAlign(block);
2287         
2288         uiBlockBeginAlign(block);
2289         
2290         uiDefButS(block, ROW, REDRAWHEADERS, "Global",  xco,            90, 40,20, &G.vd->twmode, 5.0, (float)V3D_MANIP_GLOBAL,0, 0, "Global Transform Orientation");
2291         uiDefButS(block, ROW, REDRAWHEADERS, "Local",   xco + 40,       90, 40,20, &G.vd->twmode, 5.0, (float)V3D_MANIP_LOCAL, 0, 0, "Local Transform Orientation");
2292         uiDefButS(block, ROW, REDRAWHEADERS, "Normal",  xco + 80,       90, 40,20, &G.vd->twmode, 5.0, (float)V3D_MANIP_NORMAL,0, 0, "Normal Transform Orientation");
2293         uiDefButS(block, ROW, REDRAWHEADERS, "View",            xco + 120,      90, 40,20, &G.vd->twmode, 5.0, (float)V3D_MANIP_VIEW,   0, 0, "View Transform Orientation");
2294         
2295         for (index = V3D_MANIP_CUSTOM, ts = transform_spaces->first ; ts ; ts = ts->next, index++) {
2296
2297                 BIF_ThemeColor(TH_BUT_ACTION);
2298                 if (G.vd->twmode == index) {
2299                         but = uiDefIconButS(block,ROW, REDRAWHEADERS, ICON_CHECKBOX_HLT, xco,yco,XIC,YIC, &G.vd->twmode, 5.0, (float)index, 0, 0, "Use this Custom Transform Orientation");
2300                 }
2301                 else {
2302                         but = uiDefIconButS(block,ROW, REDRAWHEADERS, ICON_CHECKBOX_DEHLT, xco,yco,XIC,YIC, &G.vd->twmode, 5.0, (float)index, 0, 0, "Use this Custom Transform Orientation");
2303                 }
2304                 uiButSetFunc(but, selectTransformOrientation_func, ts, NULL);
2305                 uiDefBut(block, TEX, 0, "", xco+=XIC, yco,100+XIC,20, &ts->name, 0, 30, 0, 0, "Edits the name of this Transform Orientation");
2306                 but = uiDefIconBut(block, BUT, REDRAWVIEW3D, ICON_X, xco+=100+XIC,yco,XIC,YIC, 0, 0, 0, 0, 0, "Deletes this Transform Orientation");
2307                 uiButSetFunc(but, removeTransformOrientation_func, ts, NULL);
2308
2309                 xco = 20;
2310                 yco -= 25;
2311         }
2312         uiBlockEndAlign(block);
2313         
2314         if(yco < 0) uiNewPanelHeight(block, height-yco);
2315 }
2316
2317
2318 static void view3d_panel_object(short cntrl)    // VIEW3D_HANDLER_OBJECT
2319 {
2320         uiBlock *block;
2321         uiBut *bt;
2322         Object *ob= OBACT;
2323         TransformProperties *tfp;
2324         float lim;
2325         static char hexcol[128];
2326         
2327         if(ob==NULL) return;
2328
2329         /* make sure we got storage */
2330         if(G.vd->properties_storage==NULL)
2331                 G.vd->properties_storage= MEM_callocN(sizeof(TransformProperties), "TransformProperties");
2332         tfp= G.vd->properties_storage;
2333         
2334         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_object", UI_EMBOSS, UI_HELV, curarea->win);
2335         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
2336         uiSetPanelHandler(VIEW3D_HANDLER_OBJECT);  // for close and esc
2337
2338         if((G.f & G_SCULPTMODE) && !G.obedit) {
2339                 if(!uiNewPanel(curarea, block, "Transform Properties", "View3d", 10, 230, 318, 234))
2340                         return;
2341         } else if(G.f & G_PARTICLEEDIT && !G.obedit){
2342                 if(!uiNewPanel(curarea, block, "Transform Properties", "View3d", 10, 230, 318, 234))
2343                         return;
2344         } else {
2345                 if(!uiNewPanel(curarea, block, "Transform Properties", "View3d", 10, 230, 318, 204))
2346                         return;
2347         }
2348
2349         uiSetButLock(object_is_libdata(ob), ERROR_LIBDATA_MESSAGE);
2350         
2351         if(G.f & (G_VERTEXPAINT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
2352                 uiBlockSetFlag(block, UI_BLOCK_FRONTBUFFER);    // force old style frontbuffer draw
2353         }
2354         else {
2355                 bt= uiDefBut(block, TEX, B_IDNAME, "OB: ",      10,180,140,20, ob->id.name+2, 0.0, 21.0, 0, 0, "");
2356 #ifdef WITH_VERSE
2357                 if(ob->vnode) uiButSetFunc(bt, test_and_send_idbutton_cb, ob, ob->id.name);
2358                 else uiButSetFunc(bt, test_idbutton_cb, ob->id.name, NULL);
2359 #else
2360                 uiButSetFunc(bt, test_idbutton_cb, ob->id.name, NULL);
2361 #endif
2362
2363                 if((G.f & G_PARTICLEEDIT)==0) {
2364                         uiBlockBeginAlign(block);
2365                         uiDefIDPoinBut(block, test_obpoin_but, ID_OB, B_OBJECTPANELPARENT, "Par:", 160, 180, 140, 20, &ob->parent, "Parent Object"); 
2366                         if((ob->parent) && (ob->partype == PARBONE)) {
2367                                 bt= uiDefBut(block, TEX, B_OBJECTPANELPARENT, "ParBone:", 160, 160, 140, 20, ob->parsubstr, 0, 30, 0, 0, "");
2368                                 uiButSetCompleteFunc(bt, autocomplete_bone, (void *)ob->parent);
2369                         }
2370                         else {
2371                                 strcpy(ob->parsubstr, "");
2372                         }
2373                         uiBlockEndAlign(block);
2374                 }
2375         }
2376
2377         lim= 10000.0f*MAX2(1.0, G.vd->grid);
2378
2379         if(ob==G.obedit) {
2380                 if(ob->type==OB_ARMATURE) v3d_editarmature_buts(block, ob, lim);
2381                 if(ob->type==OB_MBALL) v3d_editmetaball_buts(block, ob, lim);
2382                 else v3d_editvertex_buts(block, ob, lim);
2383         }
2384         else if(ob->flag & OB_POSEMODE) {
2385                 v3d_posearmature_buts(block, ob, lim);
2386         }
2387         else if(G.f & G_WEIGHTPAINT) {
2388                 uiNewPanelTitle(block, "Weight Paint Properties");
2389                 weight_paint_buttons(block);
2390         }
2391         else if(G.f & (G_VERTEXPAINT|G_TEXTUREPAINT)) {
2392                 extern VPaint Gvp;         /* from vpaint */
2393                 static float hsv[3], old[3];    // used as temp mem for picker
2394                 float *rgb= NULL;
2395                 ToolSettings *settings= G.scene->toolsettings;
2396
2397                 if(G.f & G_VERTEXPAINT) rgb= &Gvp.r;
2398                 else if(settings->imapaint.brush) rgb= settings->imapaint.brush->rgb;
2399                 
2400                 uiNewPanelTitle(block, "Paint Properties");
2401                 if (rgb)
2402                         /* 'f' is for floating panel */
2403                         uiBlockPickerButtons(block, rgb, hsv, old, hexcol, 'f', REDRAWBUTSEDIT);
2404         }
2405         else if(G.f & G_SCULPTMODE) {
2406                 uiNewPanelTitle(block, "Sculpt Properties");
2407                 sculptmode_draw_interface_tools(block,10,150);
2408         } else if(G.f & G_PARTICLEEDIT){
2409                 uiNewPanelTitle(block, "Particle Edit Properties");
2410                 particle_edit_buttons(block);
2411         } else {
2412                 BoundBox *bb = NULL;
2413
2414                 uiBlockBeginAlign(block);
2415                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCX, REDRAWVIEW3D, ICON_UNLOCKED,     10,150,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2416                 uiDefButF(block, NUM, B_OBJECTPANEL, "LocX:",           30, 150, 120, 19, &(ob->loc[0]), -lim, lim, 100, 3, "");
2417                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCY, REDRAWVIEW3D, ICON_UNLOCKED,     10,130,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2418                 uiDefButF(block, NUM, B_OBJECTPANEL, "LocY:",           30, 130, 120, 19, &(ob->loc[1]), -lim, lim, 100, 3, "");
2419                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCZ, REDRAWVIEW3D, ICON_UNLOCKED,     10,110,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2420                 uiDefButF(block, NUM, B_OBJECTPANEL, "LocZ:",           30, 110, 120, 19, &(ob->loc[2]), -lim, lim, 100, 3, "");
2421                 
2422                 tfp->ob_eul[0]= 180.0*ob->rot[0]/M_PI;
2423                 tfp->ob_eul[1]= 180.0*ob->rot[1]/M_PI;
2424                 tfp->ob_eul[2]= 180.0*ob->rot[2]/M_PI;
2425                 
2426                 uiBlockBeginAlign(block);
2427                 if ((ob->parent) && (ob->partype == PARBONE)) {
2428                         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTX, REDRAWVIEW3D, ICON_UNLOCKED,     160,130,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2429                         uiDefButF(block, NUM, B_OBJECTPANELROT, "RotX:",        180, 130, 120, 19, &(tfp->ob_eul[0]), -lim, lim, 1000, 3, "");
2430                         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTY, REDRAWVIEW3D, ICON_UNLOCKED,     160,110,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2431                         uiDefButF(block, NUM, B_OBJECTPANELROT, "RotY:",        180, 110, 120, 19, &(tfp->ob_eul[1]), -lim, lim, 1000, 3, "");
2432                         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTZ, REDRAWVIEW3D, ICON_UNLOCKED,     160,90,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2433                         uiDefButF(block, NUM, B_OBJECTPANELROT, "RotZ:",        180, 90, 120, 19, &(tfp->ob_eul[2]), -lim, lim, 1000, 3, "");
2434
2435                 }
2436                 else {
2437                         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTX, REDRAWVIEW3D, ICON_UNLOCKED,     160,150,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2438                         uiDefButF(block, NUM, B_OBJECTPANELROT, "RotX:",        180, 150, 120, 19, &(tfp->ob_eul[0]), -lim, lim, 1000, 3, "");
2439                         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTY, REDRAWVIEW3D, ICON_UNLOCKED,     160,130,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2440                         uiDefButF(block, NUM, B_OBJECTPANELROT, "RotY:",        180, 130, 120, 19, &(tfp->ob_eul[1]), -lim, lim, 1000, 3, "");
2441                         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTZ, REDRAWVIEW3D, ICON_UNLOCKED,     160,110,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2442                         uiDefButF(block, NUM, B_OBJECTPANELROT, "RotZ:",        180, 110, 120, 19, &(tfp->ob_eul[2]), -lim, lim, 1000, 3, "");
2443                 }
2444
2445                 tfp->ob_scale[0]= ob->size[0];
2446                 tfp->ob_scale[1]= ob->size[1];
2447                 tfp->ob_scale[2]= ob->size[2];
2448
2449                 uiBlockBeginAlign(block);
2450                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEX, REDRAWVIEW3D, ICON_UNLOCKED,   10,80,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2451                 uiDefButF(block, NUM, B_OBJECTPANELSCALE, "ScaleX:",            30, 80, 120, 19, &(tfp->ob_scale[0]), -lim, lim, 10, 3, "");
2452                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEY, REDRAWVIEW3D, ICON_UNLOCKED,   10,60,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2453                 uiDefButF(block, NUM, B_OBJECTPANELSCALE, "ScaleY:",            30, 60, 120, 19, &(tfp->ob_scale[1]), -lim, lim, 10, 3, "");
2454                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEZ, REDRAWVIEW3D, ICON_UNLOCKED,   10,40,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2455                 uiDefButF(block, NUM, B_OBJECTPANELSCALE, "ScaleZ:",            30, 40, 120, 19, &(tfp->ob_scale[2]), -lim, lim, 10, 3, "");
2456                 uiBlockEndAlign(block);
2457                 
2458                 uiDefButS(block, TOG, REDRAWVIEW3D, "Link Scale",               10, 10, 140, 19, &(tfp->link_scale), 0, 1, 0, 0, "Scale values vary proportionally in all directions");
2459
2460                 bb= object_get_boundbox(ob);
2461                 if (bb) {
2462                         float scale[3];
2463
2464                         Mat4ToSize(ob->obmat, scale);
2465
2466                         tfp->ob_dims[0] = fabs(scale[0]) * (bb->vec[4][0] - bb->vec[0][0]);
2467                         tfp->ob_dims[1] = fabs(scale[1]) * (bb->vec[2][1] - bb->vec[0][1]);
2468                         tfp->ob_dims[2] = fabs(scale[2]) * (bb->vec[1][2] - bb->vec[0][2]);
2469
2470                         uiBlockBeginAlign(block);
2471                         if ((ob->parent) && (ob->partype == PARBONE)) {
2472                                 uiDefButF(block, NUM, B_OBJECTPANELDIMS, "DimX:",               160, 60, 140, 19, &(tfp->ob_dims[0]), 0.0, lim, 10, 3, "Manipulate bounding box size");
2473                                 uiDefButF(block, NUM, B_OBJECTPANELDIMS, "DimY:",               160, 40, 140, 19, &(tfp->ob_dims[1]), 0.0, lim, 10, 3, "Manipulate bounding box size");
2474                                 uiDefButF(block, NUM, B_OBJECTPANELDIMS, "DimZ:",               160, 20, 140, 19, &(tfp->ob_dims[2]), 0.0, lim, 10, 3, "Manipulate bounding box size");
2475
2476                         }
2477                         else {
2478                                 uiDefButF(block, NUM, B_OBJECTPANELDIMS, "DimX:",               160, 80, 140, 19, &(tfp->ob_dims[0]), 0.0, lim, 10, 3, "Manipulate bounding box size");
2479                                 uiDefButF(block, NUM, B_OBJECTPANELDIMS, "DimY:",               160, 60, 140, 19, &(tfp->ob_dims[1]), 0.0, lim, 10, 3, "Manipulate bounding box size");
2480                                 uiDefButF(block, NUM, B_OBJECTPANELDIMS, "DimZ:",               160, 40, 140, 19, &(tfp->ob_dims[2]), 0.0, lim, 10, 3, "Manipulate bounding box size");
2481                         }
2482
2483                         uiBlockEndAlign(block);
2484                 }
2485         }
2486         uiClearButLock();
2487 }
2488
2489 static void view3d_panel_background(short cntrl)        // VIEW3D_HANDLER_BACKGROUND
2490 {
2491         uiBlock *block;
2492         View3D *vd;
2493         
2494         vd= G.vd;
2495
2496         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_background", UI_EMBOSS, UI_HELV, curarea->win);
2497         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
2498         uiSetPanelHandler(VIEW3D_HANDLER_BACKGROUND);  // for close and esc
2499         if(uiNewPanel(curarea, block, "Background Image", "View3d", 340, 10, 318, 204)==0) return;
2500
2501         if(G.f & G_VERTEXPAINT || G.f & G_WEIGHTPAINT || G.f & G_TEXTUREPAINT) {
2502                 uiBlockSetFlag(block, UI_BLOCK_FRONTBUFFER);    // force old style frontbuffer draw
2503         }
2504         
2505         if(vd->flag & V3D_DISPBGPIC) {
2506                 if(vd->bgpic==NULL) {
2507                         vd->bgpic= MEM_callocN(sizeof(BGpic), "bgpic");
2508                         vd->bgpic->size= 5.0;
2509                         vd->bgpic->blend= 0.5;
2510                         vd->bgpic->iuser.fie_ima= 2;
2511                         vd->bgpic->iuser.ok= 1;
2512                 }
2513         }
2514         
2515         if(!(vd->flag & V3D_DISPBGPIC)) {
2516                 uiDefButBitS(block, TOG, V3D_DISPBGPIC, B_REDR, "Use Background Image", 10, 180, 150, 20, &vd->flag, 0, 0, 0, 0, "Display an image in the background of this 3D View");
2517                 uiDefBut(block, LABEL, 1, " ",  160, 180, 150, 20, NULL, 0.0, 0.0, 0, 0, "");
2518         }
2519         else {
2520                 uiBlockBeginAlign(block);
2521                 uiDefButBitS(block, TOG, V3D_DISPBGPIC, B_REDR, "Use", 10, 225, 50, 20, &vd->flag, 0, 0, 0, 0, "Display an image in the background of this 3D View");
2522                 uiDefButF(block, NUMSLI, B_REDR, "Blend:",      60, 225, 150, 20, &vd->bgpic->blend, 0.0,1.0, 0, 0, "Set the transparency of the background image");
2523                 uiDefButF(block, NUM, B_REDR, "Size:",          210, 225, 100, 20, &vd->bgpic->size, 0.1, 250.0*vd->grid, 100, 0, "Set the size (width) of the background image");
2524
2525                 uiDefButF(block, NUM, B_REDR, "X Offset:",      10, 205, 150, 20, &vd->bgpic->xof, -250.0*vd->grid,250.0*vd->grid, 10, 2, "Set the horizontal offset of the background image");
2526                 uiDefButF(block, NUM, B_REDR, "Y Offset:",      160, 205, 150, 20, &vd->bgpic->yof, -250.0*vd->grid,250.0*vd->grid, 10, 2, "Set the vertical offset of the background image");
2527                 
2528                 uiblock_image_panel(block, &vd->bgpic->ima, &vd->bgpic->iuser, B_REDR, B_REDR);
2529         }
2530 }
2531
2532
2533 static void view3d_panel_properties(short cntrl)        // VIEW3D_HANDLER_SETTINGS
2534 {
2535         uiBlock *block;
2536         View3D *vd;
2537         float *curs;
2538         
2539         vd= G.vd;
2540
2541         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_properties", UI_EMBOSS, UI_HELV, curarea->win);
2542         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
2543         uiSetPanelHandler(VIEW3D_HANDLER_PROPERTIES);  // for close and esc
2544         if(uiNewPanel(curarea, block, "View Properties", "View3d", 340, 30, 318, 254)==0) return;
2545
2546         /* to force height */
2547         uiNewPanelHeight(block, 264);
2548
2549         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
2550                 uiBlockSetFlag(block, UI_BLOCK_FRONTBUFFER);    // force old style frontbuffer draw
2551         }
2552
2553         uiDefBut(block, LABEL, 1, "Grid:",                                      10, 220, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
2554         uiBlockBeginAlign(block);
2555         uiDefButF(block, NUM, REDRAWVIEW3D, "Spacing:",         10, 200, 140, 19, &vd->grid, 0.001, 100.0, 10, 0, "Set the distance between grid lines");
2556         uiDefButS(block, NUM, REDRAWVIEW3D, "Lines:",           10, 180, 140, 19, &vd->gridlines, 0.0, 100.0, 100, 0, "Set the number of grid lines in perspective view");
2557         uiDefButS(block, NUM, REDRAWVIEW3D, "Divisions:",               10, 160, 140, 19, &vd->gridsubdiv, 1.0, 100.0, 100, 0, "Set the number of grid lines");
2558         uiBlockEndAlign(block);
2559
2560         uiDefBut(block, LABEL, 1, "3D Display:",                                                        160, 220, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
2561         uiDefButBitS(block, TOG, V3D_SHOW_FLOOR, REDRAWVIEW3D, "Grid Floor",160, 200, 150, 19, &vd->gridflag, 0, 0, 0, 0, "Show the grid floor in free camera mode");
2562         uiDefButBitS(block, TOG, V3D_SHOW_X, REDRAWVIEW3D, "X Axis",            160, 176, 48, 19, &vd->gridflag, 0, 0, 0, 0, "Show the X Axis line");
2563         uiDefButBitS(block, TOG, V3D_SHOW_Y, REDRAWVIEW3D, "Y Axis",            212, 176, 48, 19, &vd->gridflag, 0, 0, 0, 0, "Show the Y Axis line");
2564         uiDefButBitS(block, TOG, V3D_SHOW_Z, REDRAWVIEW3D, "Z Axis",            262, 176, 48, 19, &vd->gridflag, 0, 0, 0, 0, "Show the Z Axis line");
2565
2566         uiDefBut(block, LABEL, 1, "View Camera:",                       10, 140, 140, 19, NULL, 0.0, 0.0, 0, 0, "");
2567         
2568         uiDefButF(block, NUM, REDRAWVIEW3D, "Lens:",            10, 120, 140, 19, &vd->lens, 10.0, 120.0, 100, 0, "The lens angle in perspective view");
2569         uiBlockBeginAlign(block);
2570         uiDefButF(block, NUM, REDRAWVIEW3D, "Clip Start:",      10, 96, 140, 19, &vd->near, vd->grid/100.0, 100.0, 10, 0, "Set the beginning of the range in which 3D objects are displayed (perspective view)");
2571         uiDefButF(block, NUM, REDRAWVIEW3D, "Clip End:",        10, 76, 140, 19, &vd->far, 1.0, 10000.0*vd->grid, 100, 0, "Set the end of the range in which 3D objects are displayed (perspective view)");
2572         uiBlockEndAlign(block);
2573
2574         uiDefBut(block, LABEL, 1, "3D Cursor:",                         160, 150, 140, 19, NULL, 0.0, 0.0, 0, 0, "");
2575
2576         uiBlockBeginAlign(block);
2577         curs= give_cursor();
2578         uiDefButF(block, NUM, REDRAWVIEW3D, "X:",                       160, 130, 150, 22, curs, -10000.0*vd->grid, 10000.0*vd->grid, 10, 0, "X co-ordinate of the 3D cursor");
2579         uiDefButF(block, NUM, REDRAWVIEW3D, "Y:",                       160, 108, 150, 22, curs+1, -10000.0*vd->grid, 10000.0*vd->grid, 10, 0, "Y co-ordinate of the 3D cursor");
2580         uiDefButF(block, NUM, REDRAWVIEW3D, "Z:",                       160, 86, 150, 22, curs+2, -10000.0*vd->grid, 10000.0*vd->grid, 10, 0, "Z co-ordinate of the 3D cursor");
2581         uiBlockEndAlign(block);
2582
2583         uiDefBut(block, LABEL, 1, "Display:",                           10, 50, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
2584         
2585         uiBlockBeginAlign(block);
2586         uiDefButBitS(block, TOG, V3D_SELECT_OUTLINE, REDRAWVIEW3D, "Outline Selected", 10, 30, 140, 19, &vd->flag, 0, 0, 0, 0, "Highlight selected objects with an outline, in Solid, Shaded or Textured viewport shading modes");
2587         uiDefButBitS(block, TOG, V3D_DRAW_CENTERS, REDRAWVIEW3D, "All Object Centers", 10, 10, 140, 19, &vd->flag, 0, 0, 0, 0, "Draw the center points on all objects");
2588         uiDefButBitS(block, TOGN, V3D_HIDE_HELPLINES, REDRAWVIEW3D, "Relationship Lines", 10, -10, 140, 19, &vd->flag, 0, 0, 0, 0, "Draw dashed lines indicating Parent, Constraint, or Hook relationships");
2589         uiDefButBitS(block, TOG, V3D_SOLID_TEX, REDRAWVIEW3D, "Solid Tex", 10, -30, 140, 19, &vd->flag2, 0, 0, 0, 0, "Display textures in Solid draw type (Shift T)");
2590         uiBlockEndAlign(block);
2591
2592         uiDefBut(block, LABEL, 1, "View Locking:",                              160, 50, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
2593         uiBlockBeginAlign(block);
2594         uiDefIDPoinBut(block, test_obpoin_but, ID_OB, REDRAWVIEW3D, "Object:", 160, 30, 140, 19, &vd->ob_centre, "Lock view to center to this Object"); 
2595         uiDefBut(block, TEX, REDRAWVIEW3D, "Bone:",                                             160, 10, 140, 19, vd->ob_centre_bone, 1, 31, 0, 0, "If view locked to Object, use this Bone to lock to view to");
2596
2597 }
2598
2599 static void view3d_panel_preview(ScrArea *sa, short cntrl)      // VIEW3D_HANDLER_PREVIEW
2600 {
2601         uiBlock *block;
2602         View3D *v3d= sa->spacedata.first;
2603         int ofsx, ofsy;
2604         
2605         block= uiNewBlock(&sa->uiblocks, "view3d_panel_preview", UI_EMBOSS, UI_HELV, sa->win);
2606         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | UI_PNL_SCALE | cntrl);
2607         uiSetPanelHandler(VIEW3D_HANDLER_PREVIEW);  // for close and esc
2608         
2609         ofsx= -150+(sa->winx/2)/v3d->blockscale;
2610         ofsy= -100+(sa->winy/2)/v3d->blockscale;
2611         if(uiNewPanel(sa, block, "Preview", "View3d", ofsx, ofsy, 300, 200)==0) return;
2612
2613         uiBlockSetDrawExtraFunc(block, BIF_view3d_previewdraw);
2614         
2615         if(G.scene->recalc & SCE_PRV_CHANGED) {
2616                 G.scene->recalc &= ~SCE_PRV_CHANGED;
2617                 //printf("found recalc\n");
2618                 BIF_view3d_previewrender_free(sa->spacedata.first);
2619                 BIF_preview_changed(0);
2620         }
2621 }
2622
2623
2624 static void view3d_blockhandlers(ScrArea *sa)
2625 {
2626         View3D *v3d= sa->spacedata.first;
2627         short a;
2628         
2629         /* warning; blocks need to be freed each time, handlers dont remove */
2630         uiFreeBlocksWin(&sa->uiblocks, sa->win);
2631         
2632         /*uv face-sel and wp mode when mixed with wire leave depth enabled causing
2633         models to draw over the UI */
2634         glDisable(GL_DEPTH_TEST); 
2635         
2636         for(a=0; a<SPACE_MAXHANDLER; a+=2) {
2637         
2638                 switch(v3d->blockhandler[a]) {
2639
2640                 case VIEW3D_HANDLER_PROPERTIES:
2641                         view3d_panel_properties(v3d->blockhandler[a+1]);
2642                         break;
2643                 case VIEW3D_HANDLER_BACKGROUND:
2644                         view3d_panel_background(v3d->blockhandler[a+1]);
2645                         break;
2646                 case VIEW3D_HANDLER_OBJECT:
2647                         view3d_panel_object(v3d->blockhandler[a+1]);
2648                         break;
2649                 case VIEW3D_HANDLER_PREVIEW:
2650                         view3d_panel_preview(sa, v3d->blockhandler[a+1]);
2651                         break;                  
2652                 case VIEW3D_HANDLER_TRANSFORM:
2653                         view3d_panel_transform_spaces(v3d->blockhandler[a+1]);
2654                         break;                  
2655                 }
2656                 /* clear action value for event */
2657                 v3d->blockhandler[a+1]= 0;
2658         }
2659         uiDrawBlocksPanels(sa, 0);
2660
2661 }
2662
2663 /* ****************** View3d afterdraw *************** */
2664
2665 typedef struct View3DAfter {
2666         struct View3DAfter *next, *prev;
2667         struct Base *base;
2668         int type, flag;
2669 } View3DAfter;
2670
2671 /* temp storage of Objects that need to be drawn as last */
2672 void add_view3d_after(View3D *v3d, Base *base, int type, int flag)
2673 {
2674         View3DAfter *v3da= MEM_callocN(sizeof(View3DAfter), "View 3d after");
2675
2676         BLI_addtail(&v3d->afterdraw, v3da);
2677         v3da->base= base;
2678         v3da->type= type;
2679         v3da->flag= flag;
2680 }
2681
2682 /* clears zbuffer and draws it over */
2683 static void view3d_draw_xray(View3D *v3d)
2684 {
2685         View3DAfter *v3da, *next;
2686         int doit= 0;
2687         
2688         for(v3da= v3d->afterdraw.first; v3da; v3da= v3da->next)
2689                 if(v3da->type==V3D_XRAY) doit= 1;
2690         
2691         if(doit) {
2692                 if(v3d->zbuf) glClear(GL_DEPTH_BUFFER_BIT);
2693                 v3d->xray= TRUE;
2694                 
2695                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
2696                         next= v3da->next;
2697                         if(v3da->type==V3D_XRAY) {
2698                                 draw_object(v3da->base, v3da->flag);
2699                                 BLI_remlink(&v3d->afterdraw, v3da);
2700                                 MEM_freeN(v3da);
2701                         }
2702                 }
2703                 v3d->xray= FALSE;
2704         }
2705 }
2706
2707 /* disables write in zbuffer and draws it over */
2708 static void view3d_draw_transp(View3D *v3d)
2709 {
2710         View3DAfter *v3da, *next;
2711
2712         glDepthMask(0);
2713         v3d->transp= TRUE;
2714                 
2715         for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
2716                 next= v3da->next;
2717                 if(v3da->type==V3D_TRANSP) {
2718                         draw_object(v3da->base, v3da->flag);
2719                         BLI_remlink(&v3d->afterdraw, v3da);
2720                         MEM_freeN(v3da);
2721                 }
2722         }
2723         v3d->transp= FALSE;
2724
2725         glDepthMask(1);
2726
2727 }
2728
2729 /* *********************** */
2730
2731 /*
2732         In most cases call draw_dupli_objects,
2733         draw_dupli_objects_color was added because when drawing set dupli's
2734         we need to force the color
2735 */
2736 static void draw_dupli_objects_color(View3D *v3d, Base *base, int color)
2737 {       
2738         ListBase *lb;
2739         DupliObject *dob;
2740         Base tbase;
2741         BoundBox *bb= NULL;
2742         GLuint displist=0;
2743         short transflag, use_displist= -1;      /* -1 is initialize */
2744         char dt, dtx;
2745         
2746         if (base->object->restrictflag & OB_RESTRICT_VIEW) return;
2747         
2748         tbase.flag= OB_FROMDUPLI|base->flag;
2749         lb= object_duplilist(G.scene, base->object);
2750
2751         for(dob= lb->first; dob; dob= dob->next) {
2752                 if(dob->no_draw);
2753                 else {
2754                         tbase.object= dob->ob;
2755                         
2756                         /* extra service: draw the duplicator in drawtype of parent */
2757                         dt= tbase.object->dt; tbase.object->dt= base->object->dt;
2758                         dtx= tbase.object->dtx; tbase.object->dtx= base->object->dtx;
2759                         
2760                         /* negative scale flag has to propagate */
2761                         transflag= tbase.object->transflag;
2762                         if(base->object->transflag & OB_NEG_SCALE)
2763                                 tbase.object->transflag ^= OB_NEG_SCALE;
2764                         
2765                         BIF_ThemeColorBlend(color, TH_BACK, 0.5);
2766                         
2767                         /* generate displist, test for new object */
2768                         if(use_displist==1 && dob->prev && dob->prev->ob!=dob->ob) {
2769                                 use_displist= -1;
2770                                 glDeleteLists(displist, 1);
2771                         }
2772                         /* generate displist */
2773                         if(use_displist == -1) {
2774                                 
2775                                 /* lamp drawing messes with matrices, could be handled smarter... but this works */
2776                                 if(dob->ob->type==OB_LAMP || dob->type==OB_DUPLIGROUP)
2777                                         use_displist= 0;
2778                                 else {
2779                                         /* disable boundbox check for list creation */
2780                                         object_boundbox_flag(dob->ob, OB_BB_DISABLED, 1);
2781                                         /* need this for next part of code */
2782                                         bb= object_get_boundbox(dob->ob);
2783                                         
2784                                         Mat4One(dob->ob->obmat);        /* obmat gets restored */
2785                                         
2786                                         displist= glGenLists(1);
2787                                         glNewList(displist, GL_COMPILE);
2788                                         draw_object(&tbase, DRAW_CONSTCOLOR);
2789                                         glEndList();
2790                                         
2791                                         use_displist= 1;
2792                                         object_boundbox_flag(dob->ob, OB_BB_DISABLED, 0);
2793                                 }
2794                         }
2795                         if(use_displist) {
2796                                 mymultmatrix(dob->mat);
2797                                 if(boundbox_clip(dob->mat, bb))
2798                                    glCallList(displist);
2799                                 myloadmatrix(G.vd->viewmat);
2800                         }
2801                         else {
2802                                 Mat4CpyMat4(dob->ob->obmat, dob->mat);
2803                                 draw_object(&tbase, DRAW_CONSTCOLOR);
2804                         }
2805                         
2806                         tbase.object->dt= dt;
2807                         tbase.object->dtx= dtx;
2808                         tbase.object->transflag= transflag;
2809                 }
2810         }
2811         
2812         /* Transp afterdraw disabled, afterdraw only stores base pointers, and duplis can be same obj */
2813         
2814         free_object_duplilist(lb);      /* does restore */
2815         
2816         if(use_displist)
2817                 glDeleteLists(displist, 1);
2818 }
2819
2820 static void draw_dupli_objects(View3D *v3d, Base *base)
2821 {
2822         /* define the color here so draw_dupli_objects_color can be called
2823          * from the set loop */
2824         
2825         int color= (base->flag & SELECT)?TH_SELECT:TH_WIRE;
2826         /* debug */
2827         if(base->object->dup_group && base->object->dup_group->id.us<1)
2828                 color= TH_REDALERT;
2829         
2830         draw_dupli_objects_color(v3d, base, color);
2831 }
2832
2833 void view3d_update_depths(View3D *v3d)
2834 {
2835         /* Create storage for, and, if necessary, copy depth buffer */
2836         if(!v3d->depths) v3d->depths= MEM_callocN(sizeof(ViewDepths),"ViewDepths");
2837         if(v3d->depths) {
2838                 ViewDepths *d= v3d->depths;
2839                 if(d->w != v3d->area->winx ||
2840                    d->h != v3d->area->winy ||
2841                    !d->depths) {
2842                         d->w= v3d->area->winx;
2843                         d->h= v3d->area->winy;
2844                         if(d->depths)
2845                                 MEM_freeN(d->depths);
2846                         d->depths= MEM_mallocN(sizeof(float)*d->w*d->h,"View depths");
2847                         d->damaged= 1;
2848                 }
2849                 
2850                 if(d->damaged) {
2851                         glReadPixels(v3d->area->winrct.xmin,v3d->area->winrct.ymin,d->w,d->h,
2852                                      GL_DEPTH_COMPONENT,GL_FLOAT, d->depths);
2853                         
2854                         glGetDoublev(GL_DEPTH_RANGE,d->depth_range);
2855                         
2856                         d->damaged= 0;
2857                 }
2858         }
2859 }
2860
2861 /* Enable sculpting in wireframe mode by drawing sculpt object only to the depth buffer */
2862 static void draw_sculpt_depths(View3D *v3d)
2863 {
2864         Object *ob = OBACT;
2865
2866         int dt= MIN2(v3d->drawtype, ob->dt);
2867         if(v3d->zbuf==0 && dt>OB_WIRE)
2868                 dt= OB_WIRE;
2869         if(dt == OB_WIRE) {
2870                 GLboolean depth_on;
2871                 int orig_vdt = v3d->drawtype;
2872                 int orig_zbuf = v3d->zbuf;
2873                 int orig_odt = ob->dt;
2874
2875                 glGetBooleanv(GL_DEPTH_TEST, &depth_on);
2876                 v3d->drawtype = ob->dt = OB_SOLID;
2877                 v3d->zbuf = 1;
2878
2879                 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2880                 glEnable(GL_DEPTH_TEST);
2881                 draw_object(BASACT, 0);
2882                 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2883                 if(!depth_on)
2884                         glDisable(GL_DEPTH_TEST);
2885
2886                 v3d->drawtype = orig_vdt;
2887                 v3d->zbuf = orig_zbuf;
2888                 ob->dt = orig_odt;
2889         }
2890 }
2891
2892 void draw_depth(ScrArea *sa, void *spacedata)
2893 {
2894         View3D *v3d= spacedata;
2895         Base *base;
2896         Scene *sce;
2897         short zbuf, flag;
2898         float glalphaclip;
2899         /* temp set drawtype to solid */
2900         
2901         /* Setting these temporarily is not nice */
2902         zbuf = v3d->zbuf;
2903         flag = v3d->flag;
2904         glalphaclip = U.glalphaclip;
2905         
2906         U.glalphaclip = 0.5; /* not that nice but means we wont zoom into billboards */
2907         v3d->flag &= ~V3D_SELECT_OUTLINE;
2908
2909         setwinmatrixview3d(sa->winx, sa->winy, NULL);   /* 0= no pick rect */
2910         setviewmatrixview3d();  /* note: calls where_is_object for camera... */
2911         
2912         Mat4MulMat4(v3d->persmat, v3d->viewmat, sa->winmat);
2913         Mat4Invert(v3d->persinv, v3d->persmat);
2914         Mat4Invert(v3d->viewinv, v3d->viewmat);
2915         
2916         glClear(GL_DEPTH_BUFFER_BIT);
2917         
2918         myloadmatrix(v3d->viewmat);
2919         persp(PERSP_STORE);  // store correct view for persp(PERSP_VIEW) calls
2920         
2921         if(v3d->flag & V3D_CLIPPING) {
2922                 view3d_set_clipping(v3d);
2923         }
2924         
2925         v3d->zbuf= TRUE;
2926         glEnable(GL_DEPTH_TEST);
2927         
2928         /* draw set first */
2929         if(G.scene->set) {
2930                 for(SETLOOPER(G.scene->set, base)) {
2931                         if(v3d->lay & base->lay) {
2932                                 draw_object(base, 0);
2933                                 if(base->object->transflag & OB_DUPLI) {
2934                                         draw_dupli_objects_color(v3d, base, TH_WIRE);
2935                                 }
2936                         }
2937                 }
2938         }
2939         
2940         for(base= G.scene->base.first; base; base= base->next) {
2941                 if(v3d->lay & base->lay) {
2942                         
2943                         /* dupli drawing */
2944                         if(base->object->transflag & OB_DUPLI) {
2945                                 draw_dupli_objects(v3d, base);
2946                         }
2947                         draw_object(base, 0);
2948                 }
2949         }
2950         
2951         /* this isnt that nice, draw xray objects as if they are normal */
2952         if (v3d->afterdraw.first) {
2953                 View3DAfter *v3da, *next;
2954                 int num = 0;
2955                 v3d->xray= TRUE;
2956                 
2957                 glDepthFunc(GL_ALWAYS); /* always write into the depth bufer, overwriting front z values */
2958                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
2959                         next= v3da->next;
2960                         if(v3da->type==V3D_XRAY) {
2961                                 draw_object(v3da->base, 0);
2962                                 num++;
2963                         }
2964                         /* dont remove this time */
2965                 }
2966                 v3d->xray= FALSE;
2967                 
2968                 glDepthFunc(GL_LEQUAL); /* Now write the depth buffer normally */
2969                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
2970                         next= v3da->next;
2971                         if(v3da->type==V3D_XRAY) {
2972                                 v3d->xray= TRUE; v3d->transp= FALSE;  
2973                         } else if (v3da->type==V3D_TRANSP) {
2974                                 v3d->xray= FALSE; v3d->transp= TRUE;
2975                         }
2976                         
2977                         draw_object(v3da->base, 0); /* Draw Xray or Transp objects normally */
2978                         BLI_remlink(&v3d->afterdraw, v3da);
2979                         MEM_freeN(v3da);
2980                 }
2981                 v3d->xray= FALSE;
2982                 v3d->transp= FALSE;
2983         }
2984         
2985         v3d->zbuf = zbuf;
2986         U.glalphaclip = glalphaclip;
2987         v3d->flag = flag;
2988 }
2989
2990 static void draw_viewport_fps(ScrArea *sa);
2991
2992
2993 void drawview3dspace(ScrArea *sa, void *spacedata)
2994 {
2995         View3D *v3d= spacedata;
2996         Base *base;
2997         Object *ob;
2998         Scene *sce;
2999         char retopo, sculptparticle;
3000         Object *obact = OBACT;
3001         
3002         /* update all objects, ipos, matrices, displists, etc. Flags set by depgraph or manual, 
3003            no layer check here, gets correct flushed */
3004         /* sets first, we allow per definition current scene to have dependencies on sets */
3005         if(G.scene->set) {
3006                 for(SETLOOPER(G.scene->set, base))
3007                         object_handle_update(base->object);   // bke_object.h
3008         }
3009
3010         for(base= G.scene->base.first; base; base= base->next)
3011                 object_handle_update(base->object);   // bke_object.h
3012         
3013         setwinmatrixview3d(sa->winx, sa->winy, NULL);   /* 0= no pick rect */
3014         setviewmatrixview3d();  /* note: calls where_is_object for camera... */
3015
3016         Mat4MulMat4(v3d->persmat, v3d->viewmat, sa->winmat);
3017         Mat4Invert(v3d->persinv, v3d->persmat);
3018         Mat4Invert(v3d->viewinv, v3d->viewmat);
3019
3020         /* calculate pixelsize factor once, is used for lamps and obcenters */
3021         {
3022                 float len1, len2, vec[3];
3023
3024                 VECCOPY(vec, v3d->persinv[0]);
3025                 len1= Normalize(vec);
3026                 VECCOPY(vec, v3d->persinv[1]);
3027                 len2= Normalize(vec);
3028                 
3029                 v3d->pixsize= 2.0f*(len1>len2?len1:len2);
3030                 
3031                 /* correct for window size */
3032                 if(sa->winx > sa->winy) v3d->pixsize/= (float)sa->winx;
3033                 else v3d->pixsize/= (float)sa->winy;
3034         }
3035         
3036         if(v3d->drawtype > OB_WIRE) {
3037                 if(G.f & G_SIMULATION)
3038                         glClearColor(0.0, 0.0, 0.0, 0.0); 
3039                 else {
3040                         float col[3];
3041                         BIF_GetThemeColor3fv(TH_BACK, col);
3042                         glClearColor(col[0], col[1], col[2], 0.0); 
3043                 }
3044                 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
3045                 
3046                 glLoadIdentity();
3047         }
3048         else {
3049                 float col[3];
3050                 BIF_GetThemeColor3fv(TH_BACK, col);
3051                 glClearColor(col[0], col[1], col[2], 0.0);
3052                 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
3053         }
3054         
3055         myloadmatrix(v3d->viewmat);
3056         persp(PERSP_STORE);  // store correct view for persp(PERSP_VIEW) calls
3057
3058         if(v3d->flag & V3D_CLIPPING)
3059                 view3d_draw_clipping(v3d);
3060         
3061         /* set zbuffer after we draw clipping region */
3062         if(v3d->drawtype > OB_WIRE) {
3063                 v3d->zbuf= TRUE;
3064                 glEnable(GL_DEPTH_TEST);
3065         }
3066         
3067         // needs to be done always, gridview is adjusted in drawgrid() now
3068         v3d->gridview= v3d->grid;
3069         
3070         if(v3d->view==0 || v3d->persp!=0) {
3071                 drawfloor();
3072                 if(v3d->persp==2) {
3073                         if(G.scene->world) {
3074                                 if(G.scene->world->mode & WO_STARS) {
3075                                         RE_make_stars(NULL, star_stuff_init_func, star_stuff_vertex_func,
3076                                                                   star_stuff_term_func);
3077                                 }
3078                         }
3079                         if(v3d->flag & V3D_DISPBGPIC) draw_bgpic();
3080                 }
3081         }
3082         else {
3083                 drawgrid();
3084
3085                 if(v3d->flag & V3D_DISPBGPIC) {
3086                         draw_bgpic();
3087                 }
3088         }
3089         
3090         if(v3d->flag & V3D_CLIPPING)
3091                 view3d_set_clipping(v3d);
3092         
3093         /* draw set first */
3094         if(G.scene->set) {
3095                 for(SETLOOPER(G.scene->set, base)) {
3096                         
3097                         if(v3d->lay & base->lay) {
3098                                 
3099                                 BIF_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
3100                                 draw_object(base, DRAW_CONSTCOLOR|DRAW_SCENESET);
3101
3102                                 if(base->object->transflag & OB_DUPLI) {
3103                                         draw_dupli_objects_color(v3d, base, TH_WIRE);
3104                                 }
3105                         }
3106                 }
3107
3108                 /* Transp and X-ray afterdraw stuff for sets is done later */
3109         }
3110         
3111         /* then draw not selected and the duplis, but skip editmode object */
3112         for(base= G.scene->base.first; base; base= base->next) {
3113                 if(v3d->lay & base->lay) {
3114                         
3115                         /* dupli drawing */
3116                         if(base->object->transflag & OB_DUPLI) {
3117                                 draw_dupli_objects(v3d, base);
3118                         }
3119                         if((base->flag & SELECT)==0) {
3120                                 if(base->object!=G.obedit) draw_object(base, 0);
3121                         }
3122                 }
3123         }
3124
3125         retopo= retopo_mesh_check() || retopo_curve_check();
3126         sculptparticle= (G.f & (G_SCULPTMODE|G_PARTICLEEDIT)) && !G.obedit;
3127         if(retopo)
3128                 view3d_update_depths(v3d);
3129
3130         /* draw selected and editmode */
3131         for(base= G.scene->base.first; base; base= base->next) {
3132                 if(v3d->lay & base->lay) {
3133                         if (base->object==G.obedit || ( base->flag & SELECT) ) 
3134                                 draw_object(base, 0);
3135                 }
3136         }
3137
3138         if(!retopo && sculptparticle && !(obact && (obact->dtx & OB_DRAWXRAY))) {
3139                 if(G.f & G_SCULPTMODE)
3140                         draw_sculpt_depths(v3d);
3141                 view3d_update_depths(v3d);
3142         }
3143
3144         if(G.moving) {
3145                 BIF_drawConstraint();
3146                 if(G.obedit || (G.f & G_PARTICLEEDIT))
3147                         BIF_drawPropCircle(); // only editmode and particles have proportional edit
3148                 BIF_drawSnap();
3149         }
3150
3151         if(G.scene->radio) RAD_drawall(v3d->drawtype>=OB_SOLID);
3152         
3153         /* Transp and X-ray afterdraw stuff */
3154         view3d_draw_xray(v3d);  // clears zbuffer if it is used!
3155         view3d_draw_transp(v3d);
3156
3157         if(!retopo && sculptparticle && (obact && (OBACT->dtx & OB_DRAWXRAY))) {
3158                 if(G.f & G_SCULPTMODE)
3159                         draw_sculpt_depths(v3d);
3160                 view3d_update_depths(v3d);
3161         }
3162         
3163         if(v3d->flag & V3D_CLIPPING)
3164                 view3d_clr_clipping();
3165         
3166         BIF_draw_manipulator(sa);
3167                 
3168         if(v3d->zbuf) {
3169                 v3d->zbuf= FALSE;
3170                 glDisable(GL_DEPTH_TEST);
3171         }