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