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