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