removed slow list handeling
[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 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #ifndef WIN32
43 #include <unistd.h>
44 #include <sys/times.h>
45 #else
46 #include <io.h>
47 #endif   
48
49 #include "MEM_guardedalloc.h"
50
51 #include "PIL_time.h"
52
53 #include "BMF_Api.h"
54
55 #include "IMB_imbuf_types.h"
56
57 #include "DNA_action_types.h"
58 #include "DNA_armature_types.h"
59 #include "DNA_camera_types.h"
60 #include "DNA_constraint_types.h"
61 #include "DNA_curve_types.h"
62 #include "DNA_group_types.h"
63 #include "DNA_image_types.h"
64 #include "DNA_lattice_types.h"
65 #include "DNA_mesh_types.h"
66 #include "DNA_meshdata_types.h"
67 #include "DNA_meta_types.h"
68 #include "DNA_object_types.h"
69 #include "DNA_screen_types.h"
70 #include "DNA_texture_types.h"
71 #include "DNA_view3d_types.h"
72 #include "DNA_userdef_types.h"
73 #include "DNA_space_types.h"
74
75 #include "BLI_blenlib.h"
76 #include "BLI_arithb.h"
77 #include "BLI_editVert.h"
78
79 #include "BKE_action.h"
80 #include "BKE_armature.h"
81 #include "BKE_anim.h"
82 #include "BKE_constraint.h"
83 #include "BKE_curve.h"
84 #include "BKE_displist.h"
85 #include "BKE_depsgraph.h"
86 #include "BKE_global.h"
87 #include "BKE_lattice.h"
88 #include "BKE_library.h"
89 #include "BKE_image.h"
90 #include "BKE_ipo.h"
91 #include "BKE_key.h"
92 #include "BKE_main.h"
93 #include "BKE_object.h"
94 #include "BKE_texture.h"
95 #include "BKE_utildefines.h"
96
97 #include "BIF_butspace.h"
98 #include "BIF_drawimage.h"
99 #include "BIF_editgroup.h"
100 #include "BIF_editarmature.h"
101 #include "BIF_editmesh.h"
102 #include "BIF_gl.h"
103 #include "BIF_glutil.h"
104 #include "BIF_interface.h"
105 #include "BIF_mywindow.h"
106 #include "BIF_poseobject.h"
107 #include "BIF_resources.h"
108 #include "BIF_screen.h"
109 #include "BIF_space.h"
110
111 #include "BDR_drawmesh.h"
112 #include "BDR_drawobject.h"
113 #include "BDR_editobject.h"
114 #include "BDR_vpaint.h"
115
116 #include "BSE_drawview.h"
117 #include "BSE_filesel.h"
118 #include "BSE_headerbuttons.h"
119 #include "BSE_seqaudio.h"
120 #include "BSE_trans_types.h"
121 #include "BSE_time.h"
122 #include "BSE_view.h"
123
124 #include "RE_renderconverter.h"
125
126 #include "BPY_extern.h"
127
128 #include "blendef.h"
129 #include "mydevice.h"
130 #include "butspace.h"  // event codes
131
132 #include "BIF_transform.h"
133
134 /* Modules used */
135 #include "render.h"             // for ogl render
136 #include "radio.h"
137
138 /* locals */
139 void drawname(Object *ob);
140 void star_stuff_init_func(void);
141 void star_stuff_vertex_func(float* i);
142 void star_stuff_term_func(void);
143
144 void star_stuff_init_func(void)
145 {
146         cpack(-1);
147         glPointSize(1.0);
148         glBegin(GL_POINTS);
149 }
150 void star_stuff_vertex_func(float* i)
151 {
152         glVertex3fv(i);
153 }
154 void star_stuff_term_func(void)
155 {
156         glEnd();
157 }
158
159 void setalpha_bgpic(BGpic *bgpic)
160 {
161         int x, y, alph;
162         char *rect;
163         
164         alph= (int)(255.0*(1.0-bgpic->blend));
165         
166         rect= (char *)bgpic->rect;
167         for(y=0; y< bgpic->yim; y++) {
168                 for(x= bgpic->xim; x>0; x--, rect+=4) {
169                         rect[3]= alph;
170                 }
171         }
172 }
173
174
175 void default_gl_light(void)
176 {
177         int a;
178         
179         /* initialize */
180         if(U.light[0].flag==0 && U.light[1].flag==0 && U.light[2].flag==0) {
181                 U.light[0].flag= 1;
182                 U.light[0].vec[0]= -0.3; U.light[0].vec[1]= 0.3; U.light[0].vec[2]= 0.9;
183                 U.light[0].col[0]= 0.8; U.light[0].col[1]= 0.8; U.light[0].col[2]= 0.8;
184                 U.light[0].spec[0]= 0.5; U.light[0].spec[1]= 0.5; U.light[0].spec[2]= 0.5;
185                 U.light[0].spec[3]= 1.0;
186                 
187                 U.light[1].flag= 0;
188                 U.light[1].vec[0]= 0.5; U.light[1].vec[1]= 0.5; U.light[1].vec[2]= 0.1;
189                 U.light[1].col[0]= 0.4; U.light[1].col[1]= 0.4; U.light[1].col[2]= 0.8;
190                 U.light[1].spec[0]= 0.3; U.light[1].spec[1]= 0.3; U.light[1].spec[2]= 0.5;
191                 U.light[1].spec[3]= 1.0;
192         
193                 U.light[2].flag= 0;
194                 U.light[2].vec[0]= 0.3; U.light[2].vec[1]= -0.3; U.light[2].vec[2]= -0.2;
195                 U.light[2].col[0]= 0.8; U.light[2].col[1]= 0.5; U.light[2].col[2]= 0.4;
196                 U.light[2].spec[0]= 0.5; U.light[2].spec[1]= 0.4; U.light[2].spec[2]= 0.3;
197                 U.light[2].spec[3]= 1.0;
198         }
199         
200
201         glLightfv(GL_LIGHT0, GL_POSITION, U.light[0].vec); 
202         glLightfv(GL_LIGHT0, GL_DIFFUSE, U.light[0].col); 
203         glLightfv(GL_LIGHT0, GL_SPECULAR, U.light[0].spec); 
204
205         glLightfv(GL_LIGHT1, GL_POSITION, U.light[1].vec); 
206         glLightfv(GL_LIGHT1, GL_DIFFUSE, U.light[1].col); 
207         glLightfv(GL_LIGHT1, GL_SPECULAR, U.light[1].spec); 
208
209         glLightfv(GL_LIGHT2, GL_POSITION, U.light[2].vec); 
210         glLightfv(GL_LIGHT2, GL_DIFFUSE, U.light[2].col); 
211         glLightfv(GL_LIGHT2, GL_SPECULAR, U.light[2].spec); 
212
213         for(a=0; a<8; a++) {
214                 if(a<3) {
215                         if(U.light[a].flag) glEnable(GL_LIGHT0+a);
216                         else glDisable(GL_LIGHT0+a);
217                         
218                         // clear stuff from other opengl lamp usage
219                         glLightf(GL_LIGHT0+a, GL_SPOT_CUTOFF, 180.0);
220                         glLightf(GL_LIGHT0+a, GL_CONSTANT_ATTENUATION, 1.0);
221                         glLightf(GL_LIGHT0+a, GL_LINEAR_ATTENUATION, 0.0);
222                 }
223                 else glDisable(GL_LIGHT0+a);
224         }
225         
226         glDisable(GL_LIGHTING);
227
228         glDisable(GL_COLOR_MATERIAL);
229 }
230
231 /* also called when render 'ogl' */
232 void init_gl_stuff(void)        
233 {
234         float mat_ambient[] = { 0.0, 0.0, 0.0, 0.0 };
235         float mat_specular[] = { 0.5, 0.5, 0.5, 1.0 };
236         float mat_shininess[] = { 35.0 };
237         int a, x, y;
238         GLubyte pat[32*32];
239         const GLubyte *patc= pat;
240         
241         
242         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
243         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_specular);
244         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
245         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
246
247         default_gl_light();
248         
249         /* no local viewer, looks ugly in ortho mode */
250         /* glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, &one); */
251         
252         glDepthFunc(GL_LEQUAL);
253         /* scaling matrices */
254         glEnable(GL_NORMALIZE);
255
256         glShadeModel(GL_FLAT);
257
258         glDisable(GL_ALPHA_TEST);
259         glDisable(GL_BLEND);
260         glDisable(GL_DEPTH_TEST);
261         glDisable(GL_FOG);
262         glDisable(GL_LIGHTING);
263         glDisable(GL_LOGIC_OP);
264         glDisable(GL_STENCIL_TEST);
265         glDisable(GL_TEXTURE_1D);
266         glDisable(GL_TEXTURE_2D);
267
268         glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
269         glPixelTransferi(GL_RED_SCALE, 1);
270         glPixelTransferi(GL_RED_BIAS, 0);
271         glPixelTransferi(GL_GREEN_SCALE, 1);
272         glPixelTransferi(GL_GREEN_BIAS, 0);
273         glPixelTransferi(GL_BLUE_SCALE, 1);
274         glPixelTransferi(GL_BLUE_BIAS, 0);
275         glPixelTransferi(GL_ALPHA_SCALE, 1);
276         glPixelTransferi(GL_ALPHA_BIAS, 0);
277         
278         glPixelTransferi(GL_DEPTH_BIAS, 0);
279         glPixelTransferi(GL_DEPTH_SCALE, 1);
280         glDepthRange(0.0, 1.0);
281         
282         a= 0;
283         for(x=0; x<32; x++) {
284                 for(y=0; y<4; y++) {
285                         if( (x) & 1) pat[a++]= 0x88;
286                         else pat[a++]= 0x22;
287                 }
288         }
289         
290         glPolygonStipple(patc);
291
292
293         init_realtime_GL();     
294 }
295
296 void circf(float x, float y, float rad)
297 {
298         GLUquadricObj *qobj = gluNewQuadric(); 
299         
300         gluQuadricDrawStyle(qobj, GLU_FILL); 
301         
302         glPushMatrix(); 
303         
304         glTranslatef(x,  y, 0.); 
305         
306         gluDisk( qobj, 0.0,  rad, 32, 1); 
307         
308         glPopMatrix(); 
309         
310         gluDeleteQuadric(qobj);
311 }
312
313 void circ(float x, float y, float rad)
314 {
315         GLUquadricObj *qobj = gluNewQuadric(); 
316         
317         gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
318         
319         glPushMatrix(); 
320         
321         glTranslatef(x,  y, 0.); 
322         
323         gluDisk( qobj, 0.0,  rad, 32, 1); 
324         
325         glPopMatrix(); 
326         
327         gluDeleteQuadric(qobj);
328 }
329
330 /* **********  ********** */
331
332 static void draw_bgpic(void)
333 {
334         BGpic *bgpic;
335         Image *ima;
336         float vec[4], fac, asp, zoomx, zoomy;
337         float x1, y1, x2, y2, cx, cy;
338         
339         bgpic= G.vd->bgpic;
340         if(bgpic==0) return;
341         
342         if(bgpic->tex) {
343                 init_render_texture(bgpic->tex);
344                 free_unused_animimages();
345                 ima= bgpic->tex->ima;
346                 end_render_texture(bgpic->tex);
347         }
348         else {
349                 ima= bgpic->ima;
350         }
351         
352         if(ima==0) return;
353         if(ima->ok==0) return;
354
355         tag_image_time(ima);
356         
357         /* test for image */
358         if(ima->ibuf==0) {
359         
360                 if(bgpic->rect) MEM_freeN(bgpic->rect);
361                 bgpic->rect= 0;
362                 
363                 if(bgpic->tex) {
364                         ima_ibuf_is_nul(bgpic->tex, bgpic->tex->ima);
365                 }
366                 else {
367                         waitcursor(1);
368                         load_image(ima, IB_rect, G.sce, G.scene->r.cfra);
369                         waitcursor(0);
370                 }
371                 if(ima->ibuf==0) {
372                         ima->ok= 0;
373                         return;
374                 }
375         }
376
377         if(bgpic->rect==0) {
378                 
379                 bgpic->rect= MEM_dupallocN(ima->ibuf->rect);
380                 bgpic->xim= ima->ibuf->x;
381                 bgpic->yim= ima->ibuf->y;
382                 setalpha_bgpic(bgpic);
383         }
384
385         if(G.vd->persp==2) {
386                 rcti vb;
387
388                 calc_viewborder(G.vd, &vb);
389
390                 x1= vb.xmin;
391                 y1= vb.ymin;
392                 x2= vb.xmax;
393                 y2= vb.ymax;
394         }
395         else {
396                 float sco[2];
397
398                 /* calc window coord */
399                 initgrabz(0.0, 0.0, 0.0);
400                 window_to_3d(vec, 1, 0);
401                 fac= MAX3( fabs(vec[0]), fabs(vec[1]), fabs(vec[1]) );
402                 fac= 1.0/fac;
403         
404                 asp= ( (float)ima->ibuf->y)/(float)ima->ibuf->x;
405
406                 vec[0] = vec[1] = vec[2] = 0.0;
407                 view3d_project_float(curarea, vec, sco, G.vd->persmat);
408                 cx = sco[0];
409                 cy = sco[1];
410         
411                 x1=  cx+ fac*(bgpic->xof-bgpic->size);
412                 y1=  cy+ asp*fac*(bgpic->yof-bgpic->size);
413                 x2=  cx+ fac*(bgpic->xof+bgpic->size);
414                 y2=  cy+ asp*fac*(bgpic->yof+bgpic->size);
415         }
416         
417         /* complete clip? */
418         
419         if(x2 < 0 ) return;
420         if(y2 < 0 ) return;
421         if(x1 > curarea->winx ) return;
422         if(y1 > curarea->winy ) return;
423         
424         zoomx= (x2-x1)/ima->ibuf->x;
425         zoomy= (y2-y1)/ima->ibuf->y;
426
427         glEnable(GL_BLEND);
428         if(G.vd->zbuf) glDisable(GL_DEPTH_TEST);
429
430         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
431          
432         glMatrixMode(GL_PROJECTION);
433         glPushMatrix();
434         glMatrixMode(GL_MODELVIEW);
435         glPushMatrix();
436         
437         glaDefine2DArea(&curarea->winrct);
438         glPixelZoom(zoomx, zoomy);
439         glaDrawPixelsSafe(x1, y1, ima->ibuf->x, ima->ibuf->y, bgpic->rect);
440         glPixelZoom(1.0, 1.0);
441
442         glMatrixMode(GL_PROJECTION);
443         glPopMatrix();
444         glMatrixMode(GL_MODELVIEW);
445         glPopMatrix();
446         
447         glBlendFunc(GL_ONE,  GL_ZERO); 
448         glDisable(GL_BLEND);
449         if(G.vd->zbuf) glEnable(GL_DEPTH_TEST);
450         
451         areawinset(curarea->win);       // restore viewport / scissor
452 }
453
454 void timestr(double time, char *str)
455 {
456         /* format 00:00:00.00 (hr:min:sec) string has to be 12 long */
457         int  hr= (int)      time/(60*60);
458         int min= (int) fmod(time/60, 60.0);
459         int sec= (int) fmod(time, 60.0);
460         int hun= (int) fmod(time*100.0, 100.0);
461
462         if (hr) {
463                 sprintf(str, "%.2d:%.2d:%.2d.%.2d",hr,min,sec,hun);
464         } else {
465                 sprintf(str, "%.2d:%.2d.%.2d",min,sec,hun);
466         }
467
468         str[11]=0;
469 }
470
471 static void drawgrid_draw(float wx, float wy, float x, float y, float dx)
472 {
473         float fx, fy;
474         
475         x+= (wx); 
476         y+= (wy);
477         fx= x/dx;
478         fx= x-dx*floor(fx);
479         
480         while(fx< curarea->winx) {
481                 fdrawline(fx,  0.0,  fx,  (float)curarea->winy); 
482                 fx+= dx; 
483         }
484
485         fy= y/dx;
486         fy= y-dx*floor(fy);
487         
488
489         while(fy< curarea->winy) {
490                 fdrawline(0.0,  fy,  (float)curarea->winx,  fy); 
491                 fy+= dx;
492         }
493
494 }
495
496 // not intern, called in editobject for constraint axis too
497 void make_axis_color(char *col, char *col2, char axis)
498 {
499         if(axis=='x') {
500                 col2[0]= col[0]>219?255:col[0]+36;
501                 col2[1]= col[1]<26?0:col[1]-26;
502                 col2[2]= col[2]<26?0:col[2]-26;
503         }
504         else if(axis=='y') {
505                 col2[0]= col[0]<46?0:col[0]-36;
506                 col2[1]= col[1]>189?255:col[1]+66;
507                 col2[2]= col[2]<46?0:col[2]-36; 
508         }
509         else {
510                 col2[0]= col[0]<26?0:col[0]-26; 
511                 col2[1]= col[1]<26?0:col[1]-26; 
512                 col2[2]= col[2]>209?255:col[2]+46;
513         }
514         
515 }
516
517 static void drawgrid(void)
518 {
519         /* extern short bgpicmode; */
520         float wx, wy, x, y, fw, fx, fy, dx;
521         float vec4[4];
522         char col[3], col2[3];
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*= 10.0;
556                 dx*= 10.0;
557                 
558                 if(dx<6.0) {    
559                         G.vd->gridview*= 10.0;
560                         dx*= 10.0;
561                         
562                         if(dx<6.0) {
563                                 G.vd->gridview*= 10.0;
564                                 dx*=10;
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, 10*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, 10*dx);
585                 }
586         }
587         else {
588                 if(dx>60.0) {           // start blending in
589                         G.vd->gridview/= 10.0;
590                         dx/= 10.0;                      
591                         if(dx>60.0) {           // start blending in
592                                 G.vd->gridview/= 10.0;
593                                 dx/= 10.0;
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*10);
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*10);
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*10);
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(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(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 static void drawfloor(void)
645 {
646         View3D *vd;
647         float vert[3], grid;
648         int a, gridlines;
649         char col[3], col2[3];
650         short draw_line = 0;
651                 
652         vd= curarea->spacedata.first;
653
654         vert[2]= 0.0;
655
656         if(vd->gridlines<3) return;
657         
658         if(G.vd->zbuf && G.obedit) glDepthMask(0);      // for zbuffer-select
659         
660         gridlines= vd->gridlines/2;
661         grid= gridlines*vd->grid;
662         
663         BIF_GetThemeColor3ubv(TH_GRID, col);
664         
665         /* draw the Y axis and/or grid lines */
666         for(a= -gridlines;a<=gridlines;a++) {
667                 if(a==0) {
668                         /* check for the 'show Y axis' preference */
669                         if (vd->gridflag & V3D_SHOW_Y) { 
670                                 make_axis_color(col, col2, 'y');
671                                 glColor3ubv(col2);
672                                 
673                                 draw_line = 1;
674                         } else if (vd->gridflag & V3D_SHOW_FLOOR) {
675                                 BIF_ThemeColorShade(TH_GRID, -10);
676                         } else {
677                                 draw_line = 0;
678                         }
679                 } else {
680                         /* check for the 'show grid floor' preference */
681                         if (vd->gridflag & V3D_SHOW_FLOOR) {
682                                 if( (a % 10)==0) {
683                                         BIF_ThemeColorShade(TH_GRID, -10);
684                                 }
685                                 else BIF_ThemeColorShade(TH_GRID, 10);
686                                 
687                                 draw_line = 1;
688                         } else {
689                                 draw_line = 0;
690                         }
691                 }
692                 
693                 if (draw_line) {
694                         glBegin(GL_LINE_STRIP);
695                 vert[0]= a*vd->grid;
696                 vert[1]= grid;
697                 glVertex3fv(vert);
698                 vert[1]= -grid;
699                 glVertex3fv(vert);
700                         glEnd();
701                 }
702         }
703         
704         /* draw the X axis and/or grid lines */
705         for(a= -gridlines;a<=gridlines;a++) {
706                 if(a==0) {
707                         /* check for the 'show X axis' preference */
708                         if (vd->gridflag & V3D_SHOW_X) { 
709                                 make_axis_color(col, col2, 'x');
710                                 glColor3ubv(col2);
711                                 
712                                 draw_line = 1;
713                         } else if (vd->gridflag & V3D_SHOW_FLOOR) {
714                                 BIF_ThemeColorShade(TH_GRID, -10);
715                         } else {
716                                 draw_line = 0;
717                         }
718                 } else {
719                         /* check for the 'show grid floor' preference */
720                         if (vd->gridflag & V3D_SHOW_FLOOR) {
721                                 if( (a % 10)==0) {
722                                         BIF_ThemeColorShade(TH_GRID, -10);
723                                 }
724                                 else BIF_ThemeColorShade(TH_GRID, 10);
725                                 
726                                 draw_line = 1;
727                         } else {
728                                 draw_line = 0;
729                         }
730                 }
731                 
732                 if (draw_line) {
733                         glBegin(GL_LINE_STRIP);
734                 vert[1]= a*vd->grid;
735                 vert[0]= grid;
736                 glVertex3fv(vert );
737                 vert[0]= -grid;
738                 glVertex3fv(vert);
739                         glEnd();
740                 }
741         }
742         
743         /* draw the Z axis line */      
744         /* check for the 'show Z axis' preference */
745         if (vd->gridflag & V3D_SHOW_Z) {
746                 make_axis_color(col, col2, 'z');
747                 glColor3ubv(col2);
748                 
749                 glBegin(GL_LINE_STRIP);
750                 vert[0]= 0;
751                 vert[1]= 0;
752                 vert[2]= grid;
753                 glVertex3fv(vert );
754                 vert[2]= -grid;
755                 glVertex3fv(vert);
756                 glEnd();
757         }
758
759         if(G.vd->zbuf && G.obedit) glDepthMask(1);      
760
761 }
762
763 static void drawcursor(View3D *v3d)
764 {
765         short mx,my,co[2];
766         int flag;
767         
768         /* we dont want the clipping for cursor */
769         flag= v3d->flag;
770         v3d->flag= 0;
771         project_short( give_cursor(), co);
772         v3d->flag= flag;
773         
774         mx = co[0];
775         my = co[1];
776
777         if(mx!=IS_CLIPPED) {
778                 setlinestyle(0); 
779                 cpack(0xFF);
780                 circ((float)mx, (float)my, 10.0);
781                 setlinestyle(4); 
782                 cpack(0xFFFFFF);
783                 circ((float)mx, (float)my, 10.0);
784                 setlinestyle(0);
785                 cpack(0x0);
786                 
787                 sdrawline(mx-20, my, mx-5, my);
788                 sdrawline(mx+5, my, mx+20, my);
789                 sdrawline(mx, my-20, mx, my-5);
790                 sdrawline(mx, my+5, mx, my+20);
791         }
792 }
793
794 /* ********* custom clipping *********** */
795
796 static void view3d_draw_clipping(View3D *v3d)
797 {
798         BoundBox *bb= v3d->clipbb;
799         
800         BIF_ThemeColorShade(TH_BACK, -8);
801         
802         glBegin(GL_QUADS);
803
804         glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[3]);
805         glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[1]);
806         glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[5]);
807         glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[3]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[6]);
808         glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[2]);
809         glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[3]);
810         
811         glEnd();
812 }
813
814 void view3d_set_clipping(View3D *v3d)
815 {
816         double plane[4];
817         int a;
818         
819         for(a=0; a<4; a++) {
820                 QUATCOPY(plane, v3d->clip[a]);
821                 glClipPlane(GL_CLIP_PLANE0+a, plane);
822                 glEnable(GL_CLIP_PLANE0+a);
823         }
824 }
825
826 void view3d_clr_clipping(void)
827 {
828         int a;
829         
830         for(a=0; a<4; a++) {
831                 glDisable(GL_CLIP_PLANE0+a);
832         }
833 }
834
835 int view3d_test_clipping(View3D *v3d, float *vec)
836 {
837         /* vec in world coordinates, returns 1 if clipped */
838         float view[3];
839         
840         VECCOPY(view, vec);
841         
842         if(0.0f < v3d->clip[0][3] + INPR(view, v3d->clip[0]))
843                 if(0.0f < v3d->clip[1][3] + INPR(view, v3d->clip[1]))
844                         if(0.0f < v3d->clip[2][3] + INPR(view, v3d->clip[2]))
845                                 if(0.0f < v3d->clip[3][3] + INPR(view, v3d->clip[3]))
846                                         return 0;
847
848         return 1;
849 }
850
851 /* ********* end custom clipping *********** */
852
853 static void view3d_get_viewborder_size(View3D *v3d, float size_r[2])
854 {
855         float winmax= MAX2(v3d->area->winx, v3d->area->winy);
856         float aspect= (float) (G.scene->r.xsch*G.scene->r.xasp)/(G.scene->r.ysch*G.scene->r.yasp);
857
858         if(aspect>1.0) {
859                 size_r[0]= winmax;
860                 size_r[1]= winmax/aspect;
861         } else {
862                 size_r[0]= winmax*aspect;
863                 size_r[1]= winmax;
864         }
865 }
866
867 void calc_viewborder(struct View3D *v3d, rcti *viewborder_r)
868 {
869         float zoomfac, size[2];
870
871         view3d_get_viewborder_size(v3d, size);
872
873                 /* magic zoom calculation, no idea what
874              * it signifies, if you find out, tell me! -zr
875                  */
876         /* simple, its magic dude!
877          * well, to be honest, this gives a natural feeling zooming
878          * with multiple keypad presses (ton)
879          */
880         
881         zoomfac= (M_SQRT2 + v3d->camzoom/50.0);
882         zoomfac= (zoomfac*zoomfac)*0.25;
883         
884         size[0]= size[0]*zoomfac;
885         size[1]= size[1]*zoomfac;
886
887                 /* center in window */
888         viewborder_r->xmin= 0.5*v3d->area->winx - 0.5*size[0];
889         viewborder_r->ymin= 0.5*v3d->area->winy - 0.5*size[1];
890         viewborder_r->xmax= viewborder_r->xmin + size[0];
891         viewborder_r->ymax= viewborder_r->ymin + size[1];
892 }
893
894 void view3d_set_1_to_1_viewborder(View3D *v3d)
895 {
896         float size[2];
897         int im_width= (G.scene->r.size*G.scene->r.xsch)/100;
898
899         view3d_get_viewborder_size(v3d, size);
900
901         v3d->camzoom= (sqrt(4.0*im_width/size[0]) - M_SQRT2)*50.0;
902         v3d->camzoom= CLAMPIS(v3d->camzoom, -30, 300);
903 }
904
905 static void drawviewborder(void)
906 {
907         extern void gl_round_box(int mode, float minx, float miny, float maxx, float maxy, float rad);          // interface_panel.c
908         float fac, a;
909         float x1, x2, y1, y2;
910         float x3, y3, x4, y4;
911         rcti viewborder;
912         Camera *ca= NULL;
913
914         if(G.vd->camera==NULL)
915                 return;
916         if(G.vd->camera->type==OB_CAMERA)
917                 ca = G.vd->camera->data;
918         
919         calc_viewborder(G.vd, &viewborder);
920         x1= viewborder.xmin;
921         y1= viewborder.ymin;
922         x2= viewborder.xmax;
923         y2= viewborder.ymax;
924
925         /* passepartout, specified in camera edit buttons */
926         if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT)) {
927                 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
928                 glEnable(GL_BLEND);
929                 glColor4f(0, 0, 0, ca->passepartalpha);
930                 
931                 if (x1 > 0.0) {
932                         glRectf(0.0, (float)curarea->winy, x1, 0.0);
933                         glRectf(x2, (float)curarea->winy, (float)curarea->winx, 0.0);
934                 }
935                 if (y1 > 0.0)   {
936                         glRectf(x1, (float)curarea->winy, x2, y2);
937                         glRectf(x1, y1, x2, 0.0);
938                 }
939                 glDisable(GL_BLEND);
940         }
941         
942         /* edge */
943         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
944         
945         setlinestyle(0);
946         BIF_ThemeColor(TH_BACK);
947         glRectf(x1, y1, x2, y2);
948         
949         setlinestyle(3);
950         BIF_ThemeColor(TH_WIRE);
951         glRectf(x1, y1, x2, y2);
952                 
953         /* camera name */
954         if (ca && (ca->flag & CAM_SHOWNAME)) {
955                 glRasterPos2f(x1, y1-15);
956                 
957                 BMF_DrawString(G.font, G.vd->camera->id.name+2);
958         }
959
960
961         /* border */
962         if(G.scene->r.mode & R_BORDER) {
963                 
964                 cpack(0);
965                 x3= x1+ G.scene->r.border.xmin*(x2-x1);
966                 y3= y1+ G.scene->r.border.ymin*(y2-y1);
967                 x4= x1+ G.scene->r.border.xmax*(x2-x1);
968                 y4= y1+ G.scene->r.border.ymax*(y2-y1);
969                 
970                 cpack(0x4040FF);
971                 glRectf(x3,  y3,  x4,  y4); 
972         }
973
974         /* safety border */
975         if (ca && (ca->flag & CAM_SHOWTITLESAFE)) {
976                 fac= 0.1;
977                 
978                 a= fac*(x2-x1);
979                 x1+= a; 
980                 x2-= a;
981         
982                 a= fac*(y2-y1);
983                 y1+= a;
984                 y2-= a;
985         
986                 BIF_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
987                 
988                 uiSetRoundBox(15);
989                 gl_round_box(GL_LINE_LOOP, x1, y1, x2, y2, 12.0);
990         }
991         
992         setlinestyle(0);
993         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
994
995 }
996
997
998 void backdrawview3d(int test)
999 {
1000         struct Base *base;
1001
1002         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT));
1003         else if(G.obedit && G.vd->drawtype>OB_WIRE && (G.vd->flag & V3D_ZBUF_SELECT));
1004         else {
1005                 G.vd->flag &= ~V3D_NEEDBACKBUFDRAW;
1006                 return;
1007         }
1008
1009         if( !(G.vd->flag & V3D_NEEDBACKBUFDRAW) ) return;
1010
1011         if(test) {
1012                 if(qtest()) {
1013                         addafterqueue(curarea->win, BACKBUFDRAW, 1);
1014                         return;
1015                 }
1016         }
1017         persp(PERSP_VIEW);
1018
1019 #ifdef __APPLE__
1020         glDrawBuffer(GL_AUX0);
1021 #endif  
1022         if(G.vd->drawtype > OB_WIRE) G.vd->zbuf= TRUE;
1023         curarea->win_swap &= ~WIN_BACK_OK;
1024         
1025         glDisable(GL_DITHER);
1026
1027         glClearColor(0.0, 0.0, 0.0, 0.0); 
1028         if(G.vd->zbuf) {
1029                 glEnable(GL_DEPTH_TEST);
1030                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1031         }
1032         else {
1033                 glClear(GL_COLOR_BUFFER_BIT);
1034                 glDisable(GL_DEPTH_TEST);
1035         }
1036         
1037         if(G.vd->flag & V3D_CLIPPING)
1038                 view3d_set_clipping(G.vd);
1039         
1040         G.f |= G_BACKBUFSEL;
1041         
1042         base= (G.scene->basact);
1043         if(base && (base->lay & G.vd->lay)) {
1044                 draw_object_backbufsel(base->object);
1045         }
1046
1047         G.vd->flag &= ~V3D_NEEDBACKBUFDRAW;
1048
1049         G.f &= ~G_BACKBUFSEL;
1050         G.vd->zbuf= FALSE; 
1051         glDisable(GL_DEPTH_TEST);
1052         glEnable(GL_DITHER);
1053
1054 #ifdef __APPLE__
1055         glDrawBuffer(GL_BACK); /* we were in aux buffers */
1056 #endif
1057
1058         if(G.vd->flag & V3D_CLIPPING)
1059                 view3d_clr_clipping();
1060         
1061         /* it is important to end a view in a transform compatible with buttons */
1062         persp(PERSP_WIN);  // set ortho
1063         bwin_scalematrix(curarea->win, G.vd->blockscale, G.vd->blockscale, G.vd->blockscale);
1064
1065 }
1066
1067                 
1068 void drawname(Object *ob)
1069 {
1070         cpack(0x404040);
1071         glRasterPos3f(0.0,  0.0,  0.0);
1072         
1073         BMF_DrawString(G.font, " ");
1074         BMF_DrawString(G.font, ob->id.name+2);
1075 }
1076
1077
1078 static void draw_selected_name(Object *ob)
1079 {
1080         char info[128];
1081
1082         if(ob->type==OB_ARMATURE) {
1083                 char *name= NULL;
1084                 
1085                 if(ob==G.obedit) {
1086                         EditBone *ebo;
1087                         for (ebo=G.edbo.first; ebo; ebo=ebo->next){
1088                                 if (ebo->flag & BONE_ACTIVE) {
1089                                         name= ebo->name;
1090                                         break;
1091                                 }
1092                         }
1093                 }
1094                 else if(ob->pose && (ob->flag & OB_POSEMODE)) {
1095                         bPoseChannel *pchan;
1096                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1097                                 if(pchan->bone->flag & BONE_ACTIVE) {
1098                                         name= pchan->name;
1099                                         break;
1100                                 }
1101                         }
1102                 }
1103                 if(name)
1104                         sprintf(info, "(%d) %s %s", CFRA, ob->id.name+2, name);
1105                 else
1106                         sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
1107         }
1108         else sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
1109
1110         BIF_ThemeColor(TH_TEXT_HI);
1111         glRasterPos2i(30,  10);
1112         BMF_DrawString(G.fonts, info);
1113 }
1114
1115
1116 static void draw_view_icon(void)
1117 {
1118         BIFIconID icon;
1119         
1120         if(G.vd->view==7) icon= ICON_AXIS_TOP;
1121         else if(G.vd->view==1) icon= ICON_AXIS_FRONT;
1122         else if(G.vd->view==3) icon= ICON_AXIS_SIDE;
1123         else return ;
1124
1125         glEnable(GL_BLEND);
1126         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
1127         
1128         BIF_draw_icon(5.0, 5.0, icon);
1129         
1130         glBlendFunc(GL_ONE,  GL_ZERO); 
1131         glDisable(GL_BLEND);
1132 }
1133
1134 /* ******************* view3d space & buttons ************** */
1135
1136 static void view3d_change_bgpic_ima(View3D *v3d, Image *newima) {
1137         if (v3d->bgpic && v3d->bgpic->ima!=newima) {
1138                 if (newima)
1139                         id_us_plus((ID*) newima);
1140                 if (v3d->bgpic->ima)
1141                         v3d->bgpic->ima->id.us--;
1142                 v3d->bgpic->ima= newima;
1143
1144                 if(v3d->bgpic->rect) MEM_freeN(v3d->bgpic->rect);
1145                 v3d->bgpic->rect= NULL;
1146                 
1147                 allqueue(REDRAWVIEW3D, 0);
1148         }
1149 }
1150 static void view3d_change_bgpic_tex(View3D *v3d, Tex *newtex) {
1151         if (v3d->bgpic && v3d->bgpic->tex!=newtex) {
1152                 if (newtex)
1153                         id_us_plus((ID*) newtex);
1154                 if (v3d->bgpic->tex)
1155                         v3d->bgpic->tex->id.us--;
1156                 v3d->bgpic->tex= newtex;
1157                 
1158                 allqueue(REDRAWVIEW3D, 0);
1159         }
1160 }
1161
1162 static void load_bgpic_image(char *name)
1163 {
1164         Image *ima;
1165         View3D *vd;
1166         
1167         areawinset(curarea->win);
1168         vd= G.vd;
1169         if(vd==0 || vd->bgpic==0) return;
1170         
1171         ima= add_image(name);
1172         if(ima) {
1173                 if(vd->bgpic->ima) {
1174                         vd->bgpic->ima->id.us--;
1175                 }
1176                 vd->bgpic->ima= ima;
1177                 
1178                 free_image_buffers(ima);        /* force read again */
1179                 ima->ok= 1;
1180         }
1181         allqueue(REDRAWVIEW3D, 0);
1182         
1183 }
1184
1185 /* this one assumes there is only one global active object in blender...  (for object panel) */
1186 static float ob_eul[4]; // used for quat too....
1187 /* this one assumes there is only one editmode in blender...  (for object panel) */
1188 static float ve_median[5];
1189
1190 /* is used for both read and write... */
1191 static void v3d_editvertex_buts(uiBlock *block, Object *ob, float lim)
1192 {
1193         static int curdef=0;
1194         static float *defweightp= NULL;
1195         EditMesh *em = G.editMesh;
1196         EditVert *eve, *evedef=NULL;
1197         EditEdge *eed;
1198         float median[5];
1199         int tot, totw, totweight, totedge;
1200         char defstr[320];
1201         
1202         median[0]= median[1]= median[2]= median[3]= median[4]= 0.0;
1203         tot= totw= totweight= totedge= 0;
1204         defstr[0]= 0;
1205
1206         if(ob->type==OB_MESH) {         
1207                 eve= em->verts.first;
1208                 while(eve) {
1209                         if(eve->f & 1) {
1210                                 evedef= eve;
1211                                 tot++;
1212                                 VecAddf(median, median, eve->co);
1213                         }
1214                         eve= eve->next;
1215                 }
1216                 eed= em->edges.first;
1217                 while(eed) {
1218                         if((eed->f & SELECT)) {
1219                                 totedge++;
1220                                 median[3]+= eed->crease;
1221                         }
1222                         eed= eed->next;
1223                 }
1224                 /* check for defgroups */
1225                 if(tot==1 && evedef->totweight) {
1226                         bDeformGroup *dg;
1227                         int i, max=1, init=1;
1228                         char str[32];
1229                         
1230                         for (i=0; i<evedef->totweight; i++){
1231                                 dg = BLI_findlink (&ob->defbase, evedef->dw[i].def_nr);
1232                                 max+= sprintf(str, "%s %%x%d|", dg->name, evedef->dw[i].def_nr); 
1233                                 if(max<320) strcat(defstr, str);
1234                                 
1235                                 if(curdef==evedef->dw[i].def_nr) {
1236                                         init= 0;
1237                                         defweightp= &evedef->dw[i].weight;
1238                                 }
1239                         }
1240                         
1241                         if(init) {      // needs new initialized 
1242                                 curdef= evedef->dw[0].def_nr;
1243                                 defweightp= &evedef->dw[0].weight;
1244                         }
1245                 }
1246         }
1247         else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
1248                 extern ListBase editNurb; /* editcurve.c */
1249                 Nurb *nu;
1250                 BPoint *bp;
1251                 BezTriple *bezt;
1252                 int a;
1253                 
1254                 nu= editNurb.first;
1255                 while(nu) {
1256                         if((nu->type & 7)==1) {
1257                                 bezt= nu->bezt;
1258                                 a= nu->pntsu;
1259                                 while(a--) {
1260                                         if(bezt->f2 & 1) {
1261                                                 VecAddf(median, median, bezt->vec[1]);
1262                                                 tot++;
1263                                                 median[4]+= bezt->weight;
1264                                                 totweight++;
1265                                         }
1266                                         else {
1267                                                 if(bezt->f1 & 1) {
1268                                                         VecAddf(median, median, bezt->vec[0]);
1269                                                         tot++;
1270                                                 }
1271                                                 if(bezt->f3 & 1) {
1272                                                         VecAddf(median, median, bezt->vec[2]);
1273                                                         tot++;
1274                                                 }
1275                                         }
1276                                         bezt++;
1277                                 }
1278                         }
1279                         else {
1280                                 bp= nu->bp;
1281                                 a= nu->pntsu*nu->pntsv;
1282                                 while(a--) {
1283                                         if(bp->f1 & 1) {
1284                                                 VecAddf(median, median, bp->vec);
1285                                                 median[3]+= bp->vec[3];
1286                                                 totw++;
1287                                                 tot++;
1288                                                 median[4]+= bp->weight;
1289                                                 totweight++;
1290                                         }
1291                                         bp++;
1292                                 }
1293                         }
1294                         nu= nu->next;
1295                 }
1296         }
1297         else if(ob->type==OB_LATTICE) {
1298                 BPoint *bp;
1299                 int a;
1300                 
1301                 a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
1302                 bp= editLatt->def;
1303                 while(a--) {
1304                         if(bp->f1 & SELECT) {
1305                                 VecAddf(median, median, bp->vec);
1306                                 tot++;
1307                                 median[4]+= bp->weight;
1308                                 totweight++;
1309                         }
1310                         bp++;
1311                 }
1312         }
1313         
1314         if(tot==0) return;
1315
1316         median[0] /= (float)tot;
1317         median[1] /= (float)tot;
1318         median[2] /= (float)tot;
1319         if(totedge) median[3] /= (float)totedge;
1320         else if(totw) median[3] /= (float)totw;
1321         if(totweight) median[4] /= (float)totweight;
1322         
1323         if(G.vd->flag & V3D_GLOBAL_STATS)
1324                 Mat4MulVecfl(ob->obmat, median);
1325         
1326         if(block) {     // buttons
1327         
1328                 uiBlockBeginAlign(block);
1329                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, REDRAWVIEW3D, "Global",              160, 150, 70, 19, &G.vd->flag, 0, 0, 0, 0, "Displays global values");
1330                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, REDRAWVIEW3D, "Local",              230, 150, 70, 19, &G.vd->flag, 0, 0, 0, 0, "Displays local values");
1331                 
1332                 memcpy(ve_median, median, sizeof(ve_median));
1333                 
1334                 uiBlockBeginAlign(block);
1335                 if(tot==1) {
1336                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex X:", 10, 110, 290, 19, &(ve_median[0]), -lim, lim, 10, 3, "");
1337                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex Y:", 10, 90, 290, 19, &(ve_median[1]), -lim, lim, 10, 3, "");
1338                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex Z:", 10, 70, 290, 19, &(ve_median[2]), -lim, lim, 10, 3, "");
1339                         if(totw==1)
1340                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex W:", 10, 50, 290, 19, &(ve_median[3]), 0.01, 100.0, 10, 3, "");
1341                         uiBlockEndAlign(block);
1342         
1343                         if(defstr[0]) {
1344                                 uiDefBut(block, LABEL, 1, "Vertex Deform Groups",               10, 40, 290, 20, NULL, 0.0, 0.0, 0, 0, "");
1345
1346                                 uiBlockBeginAlign(block);
1347                                 uiDefButF(block, NUM, B_NOP, "Weight:",                 10, 20, 150, 19, defweightp, 0.0f, 1.0f, 10, 3, "Weight value");
1348                                 uiDefButI(block, MENU, REDRAWVIEW3D, defstr,    160, 20, 140, 19, &curdef, 0.0, 0.0, 0, 0, "Current Vertex Group");
1349                                 uiBlockEndAlign(block);
1350                         }
1351                         else if(totweight)
1352                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   10, 20, 290, 19, &(ve_median[4]), 0.0, 1.0, 10, 3, "");
1353
1354                 }
1355                 else {
1356                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median X:", 10, 110, 290, 19, &(ve_median[0]), -lim, lim, 10, 3, "");
1357                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Y:", 10, 90, 290, 19, &(ve_median[1]), -lim, lim, 10, 3, "");
1358                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Z:", 10, 70, 290, 19, &(ve_median[2]), -lim, lim, 10, 3, "");
1359                         if(totw==tot)
1360                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median W:", 10, 50, 290, 19, &(ve_median[3]), 0.01, 100.0, 10, 3, "");
1361                         uiBlockEndAlign(block);
1362                         if(totweight)
1363                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   10, 20, 290, 19, &(ve_median[4]), 0.0, 1.0, 10, 3, "Weight is used for SoftBody Goal");
1364                 }
1365                 
1366                 if(totedge==1)
1367                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Crease W:", 10, 30, 290, 19, &(ve_median[3]), 0.0, 1.0, 10, 3, "");
1368                 else if(totedge>1)
1369                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Crease W:",  10, 30, 290, 19, &(ve_median[3]), 0.0, 1.0, 10, 3, "");
1370                 
1371         }
1372         else {  // apply
1373                 
1374                 if(G.vd->flag & V3D_GLOBAL_STATS) {
1375                         Mat4Invert(ob->imat, ob->obmat);
1376                         Mat4MulVecfl(ob->imat, median);
1377                         Mat4MulVecfl(ob->imat, ve_median);
1378                 }
1379                 VecSubf(median, ve_median, median);
1380                 median[3]= ve_median[3]-median[3];
1381                 median[4]= ve_median[4]-median[4];
1382                 
1383                 if(ob->type==OB_MESH) {
1384                         float diffac= 1.0;
1385                         
1386                         eve= em->verts.first;
1387                         while(eve) {
1388                                 if(eve->f & 1) {
1389                                         VecAddf(eve->co, eve->co, median);
1390                                 }
1391                                 eve= eve->next;
1392                         }
1393                         
1394                         /* calculate the differences to squeeze a range smaller when values are close to 1 or 0 */
1395                         /* this way you can edit a median value which is applied on clipped values :) */
1396                         if(totedge>1) {
1397                                 float max= 0.0;
1398                                 for(eed= em->edges.first; eed; eed= eed->next) {
1399                                         if(eed->f & SELECT) {
1400                                                 if(max < ABS(eed->crease-ve_median[3])) max= ABS(eed->crease-ve_median[3]);
1401                                         }
1402                                 }
1403                                 if(max>0.0) {
1404                                         if(ve_median[3]> 0.5) diffac= (1.0-ve_median[3])/max;
1405                                         else diffac= (ve_median[3])/max;
1406                                         if(diffac>1.0) diffac= 1.0;
1407                                 }
1408                         }
1409                         
1410                         for(eed= em->edges.first; eed; eed= eed->next) {
1411                                 if(eed->f & SELECT) {
1412                                         eed->crease+= median[3];
1413                                         eed->crease= ve_median[3] + diffac*(eed->crease-ve_median[3]);
1414                                         
1415                                         CLAMP(eed->crease, 0.0, 1.0);
1416                                 }
1417                         }
1418                         
1419                         recalc_editnormals();
1420                 }
1421                 else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
1422                         extern ListBase editNurb; /* editcurve.c */
1423                         Nurb *nu;
1424                         BPoint *bp;
1425                         BezTriple *bezt;
1426                         int a;
1427                         
1428                         nu= editNurb.first;
1429                         while(nu) {
1430                                 if((nu->type & 7)==1) {
1431                                         bezt= nu->bezt;
1432                                         a= nu->pntsu;
1433                                         while(a--) {
1434                                                 if(bezt->f2 & 1) {
1435                                                         VecAddf(bezt->vec[0], bezt->vec[0], median);
1436                                                         VecAddf(bezt->vec[1], bezt->vec[1], median);
1437                                                         VecAddf(bezt->vec[2], bezt->vec[2], median);
1438                                                         bezt->weight+= median[4];
1439                                                 }
1440                                                 else {
1441                                                         if(bezt->f1 & 1) {
1442                                                                 VecAddf(bezt->vec[0], bezt->vec[0], median);
1443                                                         }
1444                                                         if(bezt->f3 & 1) {
1445                                                                 VecAddf(bezt->vec[2], bezt->vec[2], median);
1446                                                         }
1447                                                 }
1448                                                 bezt++;
1449                                         }
1450                                 }
1451                                 else {
1452                                         bp= nu->bp;
1453                                         a= nu->pntsu*nu->pntsv;
1454                                         while(a--) {
1455                                                 if(bp->f1 & 1) {
1456                                                         VecAddf(bp->vec, bp->vec, median);
1457                                                         bp->vec[3]+= median[3];
1458                                                         bp->weight+= median[4];
1459                                                 }
1460                                                 bp++;
1461                                         }
1462                                 }
1463                                 test2DNurb(nu);
1464                                 testhandlesNurb(nu); /* test for bezier too */
1465
1466                                 nu= nu->next;
1467                         }
1468                 }
1469                 else if(ob->type==OB_LATTICE) {
1470                         BPoint *bp;
1471                         int a;
1472                         
1473                         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
1474                         bp= editLatt->def;
1475                         while(a--) {
1476                                 if(bp->f1 & SELECT) {
1477                                         VecAddf(bp->vec, bp->vec, median);
1478                                         bp->weight+= median[4];
1479                                 }
1480                                 bp++;
1481                         }
1482                 }
1483                 
1484                 BIF_undo_push("Transform properties");
1485         }
1486 }
1487
1488 /* assumes armature active */
1489 static void validate_bonebutton_cb(void *bonev, void *namev)
1490 {
1491         Object *ob= OBACT;
1492         
1493         if(ob && ob->type==OB_ARMATURE) {
1494                 Bone *bone= bonev;
1495                 char oldname[32], newname[32];
1496                 
1497                 /* need to be on the stack */
1498                 BLI_strncpy(newname, bone->name, 32);
1499                 BLI_strncpy(oldname, (char *)namev, 32);
1500                 /* restore */
1501                 BLI_strncpy(bone->name, oldname, 32);
1502                 
1503                 armature_bone_rename(ob->data, oldname, newname); // editarmature.c
1504                 allqueue(REDRAWALL, 0);
1505         }
1506 }
1507
1508
1509 static void v3d_posearmature_buts(uiBlock *block, Object *ob, float lim)
1510 {
1511         uiBut *but;
1512         bArmature *arm;
1513         bPoseChannel *pchan;
1514         Bone *bone;
1515
1516         arm = get_armature(OBACT);
1517         if (!arm || !ob->pose) return;
1518
1519         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1520                 bone = pchan->bone;
1521                 if(bone && (bone->flag & BONE_ACTIVE)) break;
1522         }
1523         if (!pchan) return;
1524         
1525         but= uiDefBut(block, TEX, B_DIFF, "Bone:",                              160, 140, 140, 19, bone->name, 1, 31, 0, 0, "");
1526         uiButSetFunc(but, validate_bonebutton_cb, bone, NULL);
1527         
1528         QuatToEul(pchan->quat, ob_eul);
1529         ob_eul[0]*= 180.0/M_PI;
1530         ob_eul[1]*= 180.0/M_PI;
1531         ob_eul[2]*= 180.0/M_PI;
1532         
1533         uiBlockBeginAlign(block);
1534         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");
1535         uiDefButF(block, NUM, B_ARMATUREPANEL2, "LocX:",        30, 140, 120, 19, pchan->loc, -lim, lim, 100, 3, "");
1536         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");
1537         uiDefButF(block, NUM, B_ARMATUREPANEL2, "LocY:",        30, 120, 120, 19, pchan->loc+1, -lim, lim, 100, 3, "");
1538         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");
1539         uiDefButF(block, NUM, B_ARMATUREPANEL2, "locZ:",        30, 100, 120, 19, pchan->loc+2, -lim, lim, 100, 3, "");
1540
1541         uiBlockBeginAlign(block);
1542         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");
1543         uiDefButF(block, NUM, B_ARMATUREPANEL3, "RotX:",        30, 70, 120, 19, ob_eul, -1000.0, 1000.0, 100, 3, "");
1544         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");
1545         uiDefButF(block, NUM, B_ARMATUREPANEL3, "RotY:",        30, 50, 120, 19, ob_eul+1, -1000.0, 1000.0, 100, 3, "");
1546         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");
1547         uiDefButF(block, NUM, B_ARMATUREPANEL3, "RotZ:",        30, 30, 120, 19, ob_eul+2, -1000.0, 1000.0, 100, 3, "");
1548         
1549         uiBlockBeginAlign(block);
1550         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEX, REDRAWVIEW3D, ICON_UNLOCKED,    160,70,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1551         uiDefButF(block, NUM, B_ARMATUREPANEL2, "SizeX:",       180, 70, 120, 19, pchan->size, -lim, lim, 10, 3, "");
1552         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEY, REDRAWVIEW3D, ICON_UNLOCKED,    160,50,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1553         uiDefButF(block, NUM, B_ARMATUREPANEL2, "SizeY:",       180, 50, 120, 19, pchan->size+1, -lim, lim, 10, 3, "");
1554         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEZ, REDRAWVIEW3D, ICON_UNLOCKED,    160,30,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1555         uiDefButF(block, NUM, B_ARMATUREPANEL2, "SizeZ:",       180, 30, 120, 19, pchan->size+2, -lim, lim, 10, 3, "");
1556         uiBlockEndAlign(block);
1557 }
1558
1559 static void v3d_editarmature_buts(uiBlock *block, Object *ob, float lim)
1560 {
1561         EditBone *ebone;
1562         uiBut *but;
1563         
1564         ebone= G.edbo.first;
1565
1566         for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1567                 if (ebone->flag & BONE_ACTIVE)
1568                         break;
1569         }
1570
1571         if (!ebone)
1572                 return;
1573         
1574         but= uiDefBut(block, TEX, B_DIFF, "Bone:",                      160, 140, 140, 19, ebone->name, 1, 31, 0, 0, "");
1575         uiButSetFunc(but, validate_editbonebutton_cb, ebone, NULL);
1576
1577         uiBlockBeginAlign(block);
1578         uiDefButF(block, NUM, B_ARMATUREPANEL1, "RootX:",       10, 70, 140, 19, ebone->head, -lim, lim, 10, 3, "");
1579         uiDefButF(block, NUM, B_ARMATUREPANEL1, "RootY:",       10, 50, 140, 19, ebone->head+1, -lim, lim, 10, 3, "");
1580         uiDefButF(block, NUM, B_ARMATUREPANEL1, "RootZ:",       10, 30, 140, 19, ebone->head+2, -lim, lim, 10, 3, "");
1581         uiBlockBeginAlign(block);
1582         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TipX:",        160, 70, 140, 19, ebone->tail, -lim, lim, 10, 3, "");
1583         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TipY:",        160, 50, 140, 19, ebone->tail+1, -lim, lim, 10, 3, "");
1584         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TipZ:",        160, 30, 140, 19, ebone->tail+2, -lim, lim, 10, 3, "");
1585         uiBlockEndAlign(block);
1586         ob_eul[0]= 180.0*ebone->roll/M_PI;
1587         uiDefButF(block, NUM, B_ARMATUREPANEL1, "Roll:",        10, 100, 140, 19, ob_eul, -lim, lim, 1000, 3, "");
1588
1589 }
1590
1591 static void v3d_editmetaball_buts(uiBlock *block, Object *ob, float lim)
1592 {
1593         extern MetaElem *lastelem;
1594
1595         if(lastelem) {
1596                 uiBlockBeginAlign(block);
1597                 uiDefButF(block, NUM, B_RECALCMBALL, "LocX:", 10, 70, 140, 19, &lastelem->x, -lim, lim, 100, 3, "");
1598                 uiDefButF(block, NUM, B_RECALCMBALL, "LocY:", 10, 50, 140, 19, &lastelem->y, -lim, lim, 100, 3, "");
1599                 uiDefButF(block, NUM, B_RECALCMBALL, "LocZ:", 10, 30, 140, 19, &lastelem->z, -lim, lim, 100, 3, "");
1600
1601                 uiBlockBeginAlign(block);
1602                 if(lastelem->type!=MB_BALL)
1603                 uiDefButF(block, NUM, B_RECALCMBALL, "dx:", 160, 70, 140, 19, &lastelem->expx, 0, lim, 100, 3, "");
1604                 if((lastelem->type!=MB_BALL) && (lastelem->type!=MB_TUBE))
1605                 uiDefButF(block, NUM, B_RECALCMBALL, "dy:", 160, 50, 140, 19, &lastelem->expy, 0, lim, 100, 3, "");
1606                 if((lastelem->type==MB_ELIPSOID) || (lastelem->type==MB_CUBE))
1607                 uiDefButF(block, NUM, B_RECALCMBALL, "dz:", 160, 30, 140, 19, &lastelem->expz, 0, lim, 100, 3, "");
1608
1609                 uiBlockEndAlign(block);
1610
1611                 uiDefButF(block, NUM, B_RECALCMBALL, "Stiffness:", 10, 100, 140, 19, &lastelem->s, 0, lim, 100, 3, "");
1612         }
1613 }
1614
1615 void do_viewbuts(unsigned short event)
1616 {
1617         View3D *vd;
1618         Object *ob= OBACT;
1619         char *name;
1620         
1621         vd= G.vd;
1622         if(vd==0) return;
1623
1624         switch(event) {
1625         case B_LOADBGPIC:
1626                 if(vd->bgpic && vd->bgpic->ima) name= vd->bgpic->ima->name;
1627                 else name= G.ima;
1628                 
1629                 if(G.qual==LR_CTRLKEY)
1630                         activate_imageselect(FILE_SPECIAL, "Select Image", name, load_bgpic_image);
1631                 else
1632                         activate_fileselect(FILE_SPECIAL, "Select Image", name, load_bgpic_image);
1633                 break;
1634                 
1635         case B_BLENDBGPIC:
1636                 if(vd->bgpic && vd->bgpic->rect) setalpha_bgpic(vd->bgpic);
1637                 addqueue(curarea->win, REDRAW, 1);
1638                 break;
1639                 
1640         case B_BGPICBROWSE:
1641                 if(vd->bgpic) {
1642                         if (vd->menunr==-2) {
1643                                 activate_databrowse((ID*) vd->bgpic->ima, ID_IM, 0, B_BGPICBROWSE, &vd->menunr, do_viewbuts);
1644                         } else if (vd->menunr>0) {
1645                                 Image *newima= (Image*) BLI_findlink(&G.main->image, vd->menunr-1);
1646
1647                                 if (newima)
1648                                         view3d_change_bgpic_ima(vd, newima);
1649                         }
1650                 }
1651                 break;
1652                 
1653         case B_BGPICCLEAR:
1654                 if (vd->bgpic)
1655                         view3d_change_bgpic_ima(vd, NULL);
1656                 break;
1657                 
1658         case B_BGPICTEX:
1659                 if (vd->bgpic) {
1660                         if (vd->texnr==-2) {
1661                                 activate_databrowse((ID*) vd->bgpic->tex, ID_TE, 0, B_BGPICTEX, &vd->texnr, do_viewbuts);
1662                         } else if (vd->texnr>0) {
1663                                 Tex *newtex= (Tex*) BLI_findlink(&G.main->tex, vd->texnr-1);
1664                                 
1665                                 if (newtex)
1666                                         view3d_change_bgpic_tex(vd, newtex);
1667                         }
1668                 }
1669                 break;
1670                 
1671         case B_BGPICTEXCLEAR:
1672                 if (vd->bgpic)
1673                         view3d_change_bgpic_tex(vd, NULL);
1674                 break;
1675         
1676         case B_OBJECTPANEL:
1677                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
1678                 allqueue(REDRAWVIEW3D, 1);
1679                 break;
1680                 
1681         case B_OBJECTPANELROT:
1682                 if(ob) {
1683                         ob->rot[0]= M_PI*ob_eul[0]/180.0;
1684                         ob->rot[1]= M_PI*ob_eul[1]/180.0;
1685                         ob->rot[2]= M_PI*ob_eul[2]/180.0;
1686                         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
1687                         allqueue(REDRAWVIEW3D, 1);
1688                 }
1689                 break;
1690         
1691         case B_OBJECTPANELMEDIAN:
1692                 if(ob) {
1693                         v3d_editvertex_buts(NULL, ob, 1.0);
1694                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1695                         allqueue(REDRAWVIEW3D, 1);
1696                 }
1697                 break;
1698         case B_OBJECTPANELPARENT:
1699                 if(ob) {
1700                         if( test_parent_loop(ob->parent, ob) ) 
1701                                 ob->parent= NULL;
1702                         else {
1703                                 DAG_scene_sort(G.scene);
1704                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
1705                                 allqueue(REDRAWVIEW3D, 1);
1706                         }
1707                 }
1708                 break;
1709                 
1710         case B_ARMATUREPANEL1:
1711                 {
1712                         bArmature *arm= G.obedit->data;
1713                         EditBone *ebone, *child;
1714                         
1715                         ebone= G.edbo.first;
1716                         for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1717                                 if (ebone->flag & BONE_ACTIVE) break;
1718                         }
1719                         if (ebone) {
1720                                 ebone->roll= M_PI*ob_eul[0]/180.0;
1721                                 //      Update our parent
1722                                 if (ebone->parent && ebone->flag & BONE_CONNECTED){
1723                                         VECCOPY (ebone->parent->tail, ebone->head);
1724                                 }
1725                         
1726                                 //      Update our children if necessary
1727                                 for (child = G.edbo.first; child; child=child->next){
1728                                         if (child->parent == ebone && (child->flag & BONE_CONNECTED)){
1729                                                 VECCOPY (child->head, ebone->tail);
1730                                         }
1731                                 }
1732                                 if(arm->flag & ARM_MIRROR_EDIT) {
1733                                         EditBone *eboflip= armature_bone_get_mirrored(ebone);
1734                                         if(eboflip) {
1735                                                 eboflip->roll= -ebone->roll;
1736                                                 eboflip->head[0]= -ebone->head[0];
1737                                                 eboflip->tail[0]= -ebone->tail[0];
1738                                                 
1739                                                 //      Update our parent
1740                                                 if (eboflip->parent && eboflip->flag & BONE_CONNECTED){
1741                                                         VECCOPY (eboflip->parent->tail, eboflip->head);
1742                                                 }
1743                                                 
1744                                                 //      Update our children if necessary
1745                                                 for (child = G.edbo.first; child; child=child->next){
1746                                                         if (child->parent == eboflip && (child->flag & BONE_CONNECTED)){
1747                                                                 VECCOPY (child->head, eboflip->tail);
1748                                                         }
1749                                                 }
1750                                         }
1751                                 }
1752                                 
1753                                 allqueue(REDRAWVIEW3D, 1);
1754                         }
1755                 }
1756                 break;
1757         case B_ARMATUREPANEL3:  // rotate button on channel
1758                 {
1759                         bArmature *arm;
1760                         bPoseChannel *pchan;
1761                         Bone *bone;
1762                         
1763                         arm = get_armature(OBACT);
1764                         if (!arm || !ob->pose) return;
1765                                 
1766                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1767                                 bone = pchan->bone;
1768                                 if(bone && (bone->flag & BONE_ACTIVE)) break;
1769                         }
1770                         if (!pchan) return;
1771                         
1772                         ob_eul[0]*= M_PI/180.0;
1773                         ob_eul[1]*= M_PI/180.0;
1774                         ob_eul[2]*= M_PI/180.0;
1775                         EulToQuat(ob_eul, pchan->quat);
1776                 }
1777                 /* no break, pass on */
1778         case B_ARMATUREPANEL2:
1779                 {
1780                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1781                         allqueue(REDRAWVIEW3D, 1);
1782                 }
1783                 break;
1784         }
1785 }
1786
1787
1788 static void view3d_panel_object(short cntrl)    // VIEW3D_HANDLER_OBJECT
1789 {
1790         uiBlock *block;
1791         uiBut *bt;
1792         Object *ob= OBACT;
1793         float lim;
1794         static char hexcol[128];
1795         
1796         if(ob==NULL) return;
1797
1798         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_object", UI_EMBOSS, UI_HELV, curarea->win);
1799         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
1800         uiSetPanelHandler(VIEW3D_HANDLER_OBJECT);  // for close and esc
1801         
1802 /* (ton) can't use the rename trick for paint... panel names and settings are stored in the files and
1803    used to find previous locations when re-open. This causes flipping */
1804
1805         if(uiNewPanel(curarea, block, "Transform Properties", "View3d", 10, 230, 318, 204)==0) return;
1806         
1807         if(ob->id.lib) uiSetButLock(1, "Can't edit library data");
1808         
1809         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
1810                 uiBlockSetFlag(block, UI_BLOCK_FRONTBUFFER);    // force old style frontbuffer draw
1811         }
1812         else {
1813                 bt= uiDefBut(block, TEX, B_IDNAME, "OB: ",      10,180,140,20, ob->id.name+2, 0.0, 19.0, 0, 0, "");
1814                 uiButSetFunc(bt, test_idbutton_cb, ob->id.name, NULL);
1815
1816                 uiDefIDPoinBut(block, test_obpoin_but, ID_OB, B_OBJECTPANELPARENT, "Par:", 160, 180, 140, 20, &ob->parent, "Parent Object"); 
1817         }
1818
1819         lim= 10000.0f*MAX2(1.0, G.vd->grid);
1820
1821         if(ob==G.obedit) {
1822                 if(ob->type==OB_ARMATURE) v3d_editarmature_buts(block, ob, lim);
1823                 if(ob->type==OB_MBALL) v3d_editmetaball_buts(block, ob, lim);
1824                 else v3d_editvertex_buts(block, ob, lim);
1825         }
1826         else if(ob->flag & OB_POSEMODE) {
1827                 v3d_posearmature_buts(block, ob, lim);
1828         }
1829         else if(G.f & (G_VERTEXPAINT|G_TEXTUREPAINT)) {
1830                 extern VPaint Gvp;         /* from vpaint */
1831                 static float hsv[3], old[3];    // used as temp mem for picker
1832                 uiBlockPickerButtons(block, &Gvp.r, hsv, old, hexcol, 'f', REDRAWBUTSEDIT);     /* 'f' is for floating panel */
1833         }
1834         else {
1835                 uiBlockBeginAlign(block);
1836                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCX, REDRAWVIEW3D, ICON_UNLOCKED,     10,140,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1837                 uiDefButF(block, NUM, B_OBJECTPANEL, "LocX:",           30, 140, 120, 19, &(ob->loc[0]), -lim, lim, 100, 3, "");
1838                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCY, REDRAWVIEW3D, ICON_UNLOCKED,     10,120,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1839                 uiDefButF(block, NUM, B_OBJECTPANEL, "LocY:",           30, 120, 120, 19, &(ob->loc[1]), -lim, lim, 100, 3, "");
1840                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCZ, REDRAWVIEW3D, ICON_UNLOCKED,     10,100,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1841                 uiDefButF(block, NUM, B_OBJECTPANEL, "LocZ:",           30, 100, 120, 19, &(ob->loc[2]), -lim, lim, 100, 3, "");
1842                 
1843                 ob_eul[0]= 180.0*ob->rot[0]/M_PI;
1844                 ob_eul[1]= 180.0*ob->rot[1]/M_PI;
1845                 ob_eul[2]= 180.0*ob->rot[2]/M_PI;
1846                 
1847                 uiBlockBeginAlign(block);
1848                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTX, REDRAWVIEW3D, ICON_UNLOCKED,     10,70,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1849                 uiDefButF(block, NUM, B_OBJECTPANELROT, "RotX:",        30, 70, 120, 19, &(ob_eul[0]), -lim, lim, 1000, 3, "");
1850                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTY, REDRAWVIEW3D, ICON_UNLOCKED,     10,50,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1851                 uiDefButF(block, NUM, B_OBJECTPANELROT, "RotY:",        30, 50, 120, 19, &(ob_eul[1]), -lim, lim, 1000, 3, "");
1852                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTZ, REDRAWVIEW3D, ICON_UNLOCKED,     10,30,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1853                 uiDefButF(block, NUM, B_OBJECTPANELROT, "RotZ:",        30, 30, 120, 19, &(ob_eul[2]), -lim, lim, 1000, 3, "");
1854                 
1855                 uiBlockBeginAlign(block);
1856                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEX, REDRAWVIEW3D, ICON_UNLOCKED,    160,70,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1857                 uiDefButF(block, NUM, B_OBJECTPANEL, "SizeX:",          180, 70, 120, 19, &(ob->size[0]), -lim, lim, 10, 3, "");
1858                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEY, REDRAWVIEW3D, ICON_UNLOCKED,    160,50,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1859                 uiDefButF(block, NUM, B_OBJECTPANEL, "SizeY:",          180, 50, 120, 19, &(ob->size[1]), -lim, lim, 10, 3, "");
1860                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEZ, REDRAWVIEW3D, ICON_UNLOCKED,    160,30,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1861                 uiDefButF(block, NUM, B_OBJECTPANEL, "SizeZ:",          180, 30, 120, 19, &(ob->size[2]), -lim, lim, 10, 3, "");
1862                 uiBlockEndAlign(block);
1863         }
1864         uiClearButLock();
1865 }
1866
1867 static void view3d_panel_background(short cntrl)        // VIEW3D_HANDLER_BACKGROUND
1868 {
1869         uiBlock *block;
1870         View3D *vd;
1871         ID *id;
1872         char *strp;
1873         
1874         vd= G.vd;
1875
1876         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_background", UI_EMBOSS, UI_HELV, curarea->win);
1877         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
1878         uiSetPanelHandler(VIEW3D_HANDLER_BACKGROUND);  // for close and esc
1879         if(uiNewPanel(curarea, block, "Background Image", "View3d", 340, 10, 318, 204)==0) return;
1880
1881         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
1882                 uiBlockSetFlag(block, UI_BLOCK_FRONTBUFFER);    // force old style frontbuffer draw
1883         }
1884         
1885         if(vd->flag & V3D_DISPBGPIC) {
1886                 if(vd->bgpic==0) {
1887                         vd->bgpic= MEM_callocN(sizeof(BGpic), "bgpic");
1888                         vd->bgpic->size= 5.0;
1889                         vd->bgpic->blend= 0.5;
1890                 }
1891         }
1892         
1893         uiDefButBitS(block, TOG, V3D_DISPBGPIC, REDRAWVIEW3D, "Use Background Image", 0, 162, 200, 20, &vd->flag, 0, 0, 0, 0, "Display an image in the background of the 3D View");
1894         
1895         uiDefBut(block, LABEL, 1, " ",  206, 162, 84, 20, NULL, 0.0, 0.0, 0, 0, "");
1896         
1897         
1898         if(vd->flag & V3D_DISPBGPIC) {
1899
1900                 /* Background Image */
1901                 uiDefBut(block, LABEL, 1, "Image:",     0, 128, 76, 19, NULL, 0.0, 0.0, 0, 0, "");
1902                 
1903                 uiBlockBeginAlign(block);
1904                 uiDefIconBut(block, BUT, B_LOADBGPIC, ICON_FILESEL,     90, 128, 20, 20, 0, 0, 0, 0, 0, "Open a new background image");
1905
1906                 id= (ID *)vd->bgpic->ima;
1907                 IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->image), id, &(vd->menunr));
1908                 if(strp[0]) {
1909                 
1910                         uiDefButS(block, MENU, B_BGPICBROWSE, strp,     110, 128, 20, 20, &(vd->menunr), 0, 0, 0, 0, "Select a background image");
1911                 
1912                         if(vd->bgpic->ima)  {
1913                                 uiDefBut(block, TEX,        0,"BG: ",           130, 128, 140, 20, &vd->bgpic->ima->name,0.0,100.0, 0, 0, "The currently selected background image");
1914                                 uiDefIconBut(block, BUT, B_BGPICCLEAR, ICON_X, 270, 128, 20, 20, 0, 0, 0, 0, 0, "Remove background image link");
1915                         }
1916                         uiBlockEndAlign(block);
1917                 } else {
1918                         uiBlockEndAlign(block);
1919                 }
1920                 MEM_freeN(strp);
1921
1922
1923                 /* Background texture */
1924                 uiDefBut(block, LABEL, 1, "Texture:",   0, 100, 76, 19, NULL, 0.0, 0.0, 0, 0, "");
1925                 
1926                 id= (ID *)vd->bgpic->tex;
1927                 IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->tex), id, &(vd->texnr));
1928                 if (strp[0])
1929                         uiBlockBeginAlign(block);
1930                         uiDefButS(block, MENU, B_BGPICTEX, strp,                        90, 100, 20,20, &(vd->texnr), 0, 0, 0, 0, "Select a texture to use as an animated background image");
1931                 MEM_freeN(strp);
1932                 
1933                 if (id) {
1934                         uiDefBut(block, TEX, B_IDNAME, "TE:",                           110, 100, 160, 20, id->name+2, 0.0, 18.0, 0, 0, "");
1935                         uiDefIconBut(block, BUT, B_BGPICTEXCLEAR, ICON_X,       270, 100, 20, 20, 0, 0, 0, 0, 0, "Remove background texture link");
1936                         uiBlockEndAlign(block);
1937                 } else {
1938                         uiBlockEndAlign(block);
1939                 }
1940
1941                 uiDefButF(block, NUMSLI, B_BLENDBGPIC, "Blend:",        0, 60 , 290, 19, &vd->bgpic->blend, 0.0,1.0, 0, 0, "Set the transparency of the background image");
1942
1943                 uiDefButF(block, NUM, REDRAWVIEW3D, "Size:",            0, 28, 140, 19, &vd->bgpic->size, 0.1, 250.0, 100, 0, "Set the size (width) of the background image");
1944
1945                 uiDefButF(block, NUM, REDRAWVIEW3D, "X Offset:",        0, 6, 140, 19, &vd->bgpic->xof, -250.0,250.0, 10, 2, "Set the horizontal offset of the background image");
1946                 uiDefButF(block, NUM, REDRAWVIEW3D, "Y Offset:",        150, 6, 140, 19, &vd->bgpic->yof, -250.0,250.0, 10, 2, "Set the vertical offset of the background image");
1947
1948         
1949                 
1950                 // uiDefButF(block, NUM, REDRAWVIEW3D, "Size:",                 160,160,150,20, &vd->bgpic->size, 0.1, 250.0, 100, 0, "Set the size for the width of the BackGroundPic");
1951                 
1952
1953
1954 //              uiDefButF(block, NUMSLI, B_BLENDBGPIC, "Blend:",        120,100,190,20,&vd->bgpic->blend, 0.0,1.0, 0, 0, "Set the BackGroundPic transparency");
1955                 
1956 //              uiDefButF(block, NUM, B_DIFF, "Center X: ",     10,70,140,20,&vd->bgpic->xof, -20.0,20.0, 10, 2, "Set the BackGroundPic X Offset");
1957 //              uiDefButF(block, NUM, B_DIFF, "Center Y: ",     160,70,140,20,&vd->bgpic->yof, -20.0,20.0, 10, 2, "Set the BackGroundPic Y Offset");
1958
1959         }
1960 }
1961
1962
1963 static void view3d_panel_properties(short cntrl)        // VIEW3D_HANDLER_SETTINGS
1964 {
1965         uiBlock *block;
1966         View3D *vd;
1967         float *curs;
1968         
1969         vd= G.vd;
1970
1971         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_properties", UI_EMBOSS, UI_HELV, curarea->win);
1972         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
1973         uiSetPanelHandler(VIEW3D_HANDLER_PROPERTIES);  // for close and esc
1974         if(uiNewPanel(curarea, block, "View Properties", "View3d", 340, 10, 318, 204)==0) return;
1975
1976         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
1977                 uiBlockSetFlag(block, UI_BLOCK_FRONTBUFFER);    // force old style frontbuffer draw
1978         }
1979
1980         uiDefBut(block, LABEL, 1, "Grid:",                                      10, 180, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
1981         uiDefButF(block, NUM, REDRAWVIEW3D, "Spacing:",         10, 160, 140, 19, &vd->grid, 0.001, 100.0, 10, 0, "Set the distance between grid lines");
1982         uiDefButS(block, NUM, REDRAWVIEW3D, "Lines:",           10, 136, 140, 19, &vd->gridlines, 0.0, 100.0, 100, 0, "Set the number of grid lines");
1983
1984         uiDefBut(block, LABEL, 1, "3D Display:",                                        160, 180, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
1985         uiDefButBitS(block, TOG, V3D_SHOW_FLOOR, REDRAWVIEW3D, "Grid Floor",160, 160, 150, 19, &vd->gridflag, 0, 0, 0, 0, "Show the grid floor in free camera mode");
1986         uiDefButBitS(block, TOG, V3D_SHOW_X, REDRAWVIEW3D, "X Axis",            160, 136, 48, 19, &vd->gridflag, 0, 0, 0, 0, "Show the X Axis line");
1987         uiDefButBitS(block, TOG, V3D_SHOW_Y, REDRAWVIEW3D, "Y Axis",            212, 136, 48, 19, &vd->gridflag, 0, 0, 0, 0, "Show the Y Axis line");
1988         uiDefButBitS(block, TOG, V3D_SHOW_Z, REDRAWVIEW3D, "Z Axis",            262, 136, 48, 19, &vd->gridflag, 0, 0, 0, 0, "Show the Z Axis line");
1989
1990         uiDefBut(block, LABEL, 1, "View Camera:",                       10, 110, 140, 19, NULL, 0.0, 0.0, 0, 0, "");
1991         
1992         uiDefButF(block, NUM, REDRAWVIEW3D, "Lens:",            10, 90, 140, 19, &vd->lens, 10.0, 120.0, 100, 0, "The lens angle in perspective view");
1993         uiBlockBeginAlign(block);
1994         uiDefButF(block, NUM, REDRAWVIEW3D, "Clip Start:",      10, 66, 140, 19, &vd->near, vd->grid/10.0, 100.0, 10, 0, "Set the beginning of the range in which 3D objects are displayed (perspective view)");
1995         uiDefButF(block, NUM, REDRAWVIEW3D, "Clip End:",        10, 46, 140, 19, &vd->far, 1.0, 1000.0*vd->grid, 100, 0, "Set the end of the range in which 3D objects are displayed (perspective view)");
1996         uiBlockEndAlign(block);
1997
1998         uiDefBut(block, LABEL, 1, "3D Cursor:",                         160, 110, 140, 19, NULL, 0.0, 0.0, 0, 0, "");
1999
2000         uiBlockBeginAlign(block);
2001         curs= give_cursor();
2002         uiDefButF(block, NUM, REDRAWVIEW3D, "X:",                       160, 90, 150, 22, curs, -1000.0*vd->grid, 1000.0*vd->grid, 10, 0, "X co-ordinate of the 3D cursor");
2003         uiDefButF(block, NUM, REDRAWVIEW3D, "Y:",                       160, 68, 150, 22, curs+1, -1000.0*vd->grid, 1000.0*vd->grid, 10, 0, "Y co-ordinate of the 3D cursor");
2004         uiDefButF(block, NUM, REDRAWVIEW3D, "Z:",                       160, 46, 150, 22, curs+2, -1000.0*vd->grid, 1000.0*vd->grid, 10, 0, "Z co-ordinate of the 3D cursor");
2005         uiBlockEndAlign(block);
2006
2007         uiDefButBitS(block, TOG, V3D_SELECT_OUTLINE, REDRAWVIEW3D, "Outline Selected", 10, 10, 140, 19, &vd->flag, 0, 0, 0, 0, "Highlight selected objects with an outline, in Solid, Shaded or Textured viewport shading modes");
2008         uiDefButBitS(block, TOG, V3D_DRAW_CENTERS, REDRAWVIEW3D, "All Object Centers", 160, 10, 140, 19, &vd->flag, 0, 0, 0, 0, "Draw the center points on all objects");
2009
2010 }
2011
2012
2013
2014 static void view3d_blockhandlers(ScrArea *sa)
2015 {
2016         View3D *v3d= sa->spacedata.first;
2017         short a;
2018         
2019         /* warning; blocks need to be freed each time, handlers dont remove */
2020         uiFreeBlocksWin(&sa->uiblocks, sa->win);
2021
2022         for(a=0; a<SPACE_MAXHANDLER; a+=2) {
2023         
2024                 switch(v3d->blockhandler[a]) {
2025
2026                 case VIEW3D_HANDLER_PROPERTIES:
2027                         view3d_panel_properties(v3d->blockhandler[a+1]);
2028                         break;
2029                 case VIEW3D_HANDLER_BACKGROUND:
2030                         view3d_panel_background(v3d->blockhandler[a+1]);
2031                         break;
2032                 case VIEW3D_HANDLER_OBJECT:
2033                         view3d_panel_object(v3d->blockhandler[a+1]);
2034                 
2035                         break;
2036                 
2037                 }
2038                 /* clear action value for event */
2039                 v3d->blockhandler[a+1]= 0;
2040         }
2041         uiDrawBlocksPanels(sa, 0);
2042
2043 }
2044
2045 /* temp storage of Objects that need to be drawn as last */
2046 void add_view3d_after(View3D *v3d, Base *base, int type)
2047 {
2048         View3DAfter *v3da= MEM_callocN(sizeof(View3DAfter), "View 3d after");
2049
2050         BLI_addtail(&v3d->afterdraw, v3da);
2051         v3da->base= base;
2052         v3da->type= type;
2053 }
2054
2055 /* clears zbuffer and draws it over */
2056 static void view3d_draw_xray(View3D *v3d, int flag)
2057 {
2058         View3DAfter *v3da, *next;
2059         int doit= 0;
2060         
2061         for(v3da= v3d->afterdraw.first; v3da; v3da= v3da->next)
2062                 if(v3da->type==V3D_XRAY) doit= 1;
2063         
2064         if(doit) {
2065                 if(v3d->zbuf) glClear(GL_DEPTH_BUFFER_BIT);
2066                 v3d->xray= TRUE;
2067                 
2068                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
2069                         next= v3da->next;
2070                         if(v3da->type==V3D_XRAY) {
2071                                 draw_object(v3da->base, flag);
2072                                 BLI_remlink(&v3d->afterdraw, v3da);
2073                                 MEM_freeN(v3da);
2074                         }
2075                 }
2076                 v3d->xray= FALSE;
2077         }
2078 }
2079
2080 /* disables write in zbuffer and draws it over */
2081 static void view3d_draw_transp(View3D *v3d, int flag)
2082 {
2083         View3DAfter *v3da, *next;
2084
2085         glDepthMask(0);
2086         v3d->transp= TRUE;
2087                 
2088         for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
2089                 next= v3da->next;
2090                 if(v3da->type==V3D_TRANSP) {
2091                         draw_object(v3da->base, flag);
2092                         BLI_remlink(&v3d->afterdraw, v3da);
2093                         MEM_freeN(v3da);
2094                 }
2095         }
2096         v3d->transp= FALSE;
2097
2098         glDepthMask(1);
2099
2100 }
2101
2102 void drawview3dspace(ScrArea *sa, void *spacedata)
2103 {
2104         View3D *v3d= spacedata;
2105         Base *base;
2106         Object *ob;
2107         Scene *setscene;
2108         
2109         setwinmatrixview3d(0);  /* 0= no pick rect */
2110         setviewmatrixview3d();  /* note: calls where_is_object for camera... */
2111
2112         Mat4MulMat4(v3d->persmat, v3d->viewmat, curarea->winmat);
2113         Mat4Invert(v3d->persinv, v3d->persmat);
2114         Mat4Invert(v3d->viewinv, v3d->viewmat);
2115
2116         /* calculate pixelsize factor once, is used for lamps and obcenters */
2117         {
2118                 float len1, len2, vec[3];
2119
2120                 VECCOPY(vec, v3d->persinv[0]);
2121                 len1= Normalise(vec);
2122                 VECCOPY(vec, v3d->persinv[1]);
2123                 len2= Normalise(vec);
2124                 
2125                 v3d->pixsize= 2.0f*(len1>len2?len1:len2);
2126                 
2127                 /* correct for window size */
2128                 if(curarea->winx > sa->winy) v3d->pixsize/= (float)sa->winx;
2129                 else v3d->pixsize/= (float)sa->winy;
2130         }
2131         
2132         if(v3d->drawtype > OB_WIRE) {
2133                 if(G.f & G_SIMULATION)
2134                         glClearColor(0.0, 0.0, 0.0, 0.0); 
2135                 else {
2136                         float col[3];
2137                         BIF_GetThemeColor3fv(TH_BACK, col);
2138                         glClearColor(col[0], col[1], col[2], 0.0); 
2139                 }
2140                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2141                 
2142                 glLoadIdentity();
2143         }
2144         else {
2145                 float col[3];
2146                 BIF_GetThemeColor3fv(TH_BACK, col);
2147                 glClearColor(col[0], col[1], col[2], 0.0);
2148                 glClear(GL_COLOR_BUFFER_BIT);
2149         }
2150         
2151         myloadmatrix(v3d->viewmat);
2152         persp(PERSP_STORE);  // store correct view for persp(PERSP_VIEW) calls
2153
2154         if(v3d->flag & V3D_CLIPPING)
2155                 view3d_draw_clipping(v3d);
2156         
2157         /* set zbuffer after we draw clipping region */
2158         if(v3d->drawtype > OB_WIRE) {
2159                 v3d->zbuf= TRUE;
2160                 glEnable(GL_DEPTH_TEST);
2161         }
2162         
2163         // needs to be done always, gridview is adjusted in drawgrid() now
2164         v3d->gridview= v3d->grid;
2165         
2166         if(v3d->view==0 || v3d->persp!=0) {
2167                 drawfloor();
2168                 if(v3d->persp==2) {
2169                         if(G.scene->world) {
2170                                 if(G.scene->world->mode & WO_STARS) {
2171                                         RE_make_stars(star_stuff_init_func, star_stuff_vertex_func,
2172                                                                   star_stuff_term_func);
2173                                 }
2174                         }
2175                         if(v3d->flag & V3D_DISPBGPIC) draw_bgpic();
2176                 }
2177         }
2178         else {
2179                 drawgrid();
2180
2181                 if(v3d->flag & V3D_DISPBGPIC) {
2182                         draw_bgpic();
2183                 }
2184         }
2185         
2186         if(v3d->flag & V3D_CLIPPING)
2187                 view3d_set_clipping(v3d);
2188         
2189         /* draw set first */
2190         setscene= G.scene->set;
2191         if(setscene) { /* if(G.scene->set) { */
2192         
2193                 base= setscene->base.first; /* base= G.scene->set->base.first; */
2194                 while(base) {
2195                         
2196                         if(v3d->lay & base->lay) {
2197
2198                                 object_handle_update(base->object);
2199                                 
2200                                 BIF_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
2201                                 draw_object(base, DRAW_CONSTCOLOR);
2202
2203                                 if(base->object->transflag & OB_DUPLI) {
2204                                         extern ListBase duplilist;
2205                                         Base tbase;
2206                                         
2207                                         tbase= *base;
2208                                         
2209                                         tbase.flag= OB_FROMDUPLI;
2210                                         make_duplilist(setscene, base->object); /* make_duplilist(G.scene->set, base->object); */
2211                                         ob= duplilist.first;
2212                                         while(ob) {
2213                                                 tbase.object= ob;
2214                                                 draw_object(&tbase, DRAW_CONSTCOLOR);
2215                                                 ob= ob->id.next;
2216                                         }
2217                                         free_duplilist();
2218                                         
2219                                 }
2220                         }
2221                         
2222                         base= base->next;
2223                         if(base==0 && setscene && setscene->set) {
2224                                 setscene= setscene->set;
2225                                 base= setscene->base.first;
2226                         }
2227                 }
2228
2229                 /* Transp and X-ray afterdraw stuff */
2230                 view3d_draw_xray(v3d, DRAW_CONSTCOLOR); // clears zbuffer if it is used!
2231                 view3d_draw_transp(v3d, DRAW_CONSTCOLOR);
2232         }
2233         
2234         /* update all objects, ipos, matrices, displists, etc. Flags set by depgraph or manual, no layer check here, gets correct flushed */
2235         for(base= G.scene->base.first; base; base= base->next) {
2236                 object_handle_update(base->object);   // bke_object.h
2237         }
2238         
2239         /* then draw not selected and the duplis, but skip editmode object */
2240         for(base= G.scene->base.first; base; base= base->next) {
2241                 if(v3d->lay & base->lay) {
2242                         
2243                         /* dupli drawing */
2244                         if(base->object->transflag & OB_DUPLI) {
2245                                 extern ListBase duplilist;
2246                                 Base tbase;
2247
2248                                 BIF_ThemeColorBlend(TH_BACK, TH_WIRE, 0.5);
2249                                 
2250                                 tbase.flag= OB_FROMDUPLI;
2251                                 make_duplilist(G.scene, base->object);
2252
2253                                 ob= duplilist.first;
2254                                 while(ob) {
2255                                         tbase.object= ob;
2256                                         draw_object(&tbase, DRAW_CONSTCOLOR);
2257                                         ob= ob->id.next;
2258                                 }
2259                                 free_duplilist();
2260                         }
2261                         if((base->flag & SELECT)==0) {
2262                                 if(base->object!=G.obedit) draw_object(base, 0);
2263                         }
2264                 }
2265         }
2266         /* draw selected and editmode */
2267         for(base= G.scene->base.first; base; base= base->next) {
2268                 if(v3d->lay & base->lay) {
2269                         if (base->object==G.obedit || ( base->flag & SELECT) ) 
2270                                 draw_object(base, 0);
2271                 }
2272         }
2273
2274         if(G.moving) {
2275                 BIF_drawConstraint();
2276                 if(G.obedit) BIF_drawPropCircle();      // only editmode has proportional edit
2277         }
2278
2279         if(G.scene->radio) RAD_drawall(v3d->drawtype>=OB_SOLID);
2280         
2281         /* Transp and X-ray afterdraw stuff */
2282         view3d_draw_xray(v3d, 0);       // clears zbuffer if it is used!
2283         view3d_draw_transp(v3d, 0);
2284         
2285         if(v3d->flag & V3D_CLIPPING)
2286                 view3d_clr_clipping();
2287         
2288         BIF_draw_manipulator(sa);
2289                 
2290         if(v3d->zbuf) {
2291                 v3d->zbuf= FALSE;
2292                 glDisable(GL_DEPTH_TEST);
2293         }
2294
2295         persp(PERSP_WIN);  // set ortho
2296         
2297         if(v3d->persp>1) drawviewborder();
2298         if(!(G.f & G_PLAYANIM)) drawcursor(v3d);
2299         draw_view_icon();
2300
2301         ob= OBACT;
2302         if(ob && (U.uiflag & USER_DRAWVIEWINFO)) 
2303                 draw_selected_name(ob);
2304         
2305         draw_area_emboss(sa);
2306         
2307         /* it is important to end a view in a transform compatible with buttons */
2308
2309         bwin_scalematrix(sa->win, v3d->blockscale, v3d->blockscale, v3d->blockscale);
2310         view3d_blockhandlers(sa);
2311
2312         curarea->win_swap= WIN_BACK_OK;
2313         
2314         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
2315                 v3d->flag |= V3D_NEEDBACKBUFDRAW;
2316                 addafterqueue(curarea->win, BACKBUFDRAW, 1);
2317         }
2318         // test for backbuf select
2319         if(G.obedit && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT)) {
2320                 extern int afterqtest(short win, unsigned short evt);   //editscreen.c
2321
2322                 v3d->flag |= V3D_NEEDBACKBUFDRAW;
2323                 if(afterqtest(curarea->win, BACKBUFDRAW)==0) {
2324                         addafterqueue(curarea->win, BACKBUFDRAW, 1);
2325                 }
2326         }
2327
2328         /* run any view3d draw handler script links */
2329         if (sa->scriptlink.totscript)
2330                 BPY_do_spacehandlers(sa, 0, SPACEHANDLER_VIEW3D_DRAW);
2331
2332         /* run scene redraw script links */
2333         if((G.f & G_DOSCRIPTLINKS) && G.scene->scriptlink.totscript &&
2334                         !during_script()) {
2335                 BPY_do_pyscript((ID *)G.scene, SCRIPT_REDRAW);
2336         }
2337
2338 }
2339
2340
2341         /* Called back by rendering system, icky
2342          */
2343 void drawview3d_render(struct View3D *v3d)
2344 {
2345         extern short v3d_windowmode;
2346         Base *base;
2347         Object *ob;
2348         Scene *setscene;
2349
2350         update_for_newframe_muted();    /* first, since camera can be animated */
2351
2352         v3d_windowmode= 1;
2353         setwinmatrixview3d(0);
2354         v3d_windowmode= 0;
2355         glMatrixMode(GL_PROJECTION);
2356         myloadmatrix(R.winmat);
2357         glMatrixMode(GL_MODELVIEW);
2358         
2359         setviewmatrixview3d();
2360         myloadmatrix(v3d->viewmat);
2361         Mat4MulMat4(v3d->persmat, v3d->viewmat, R.winmat);
2362         Mat4Invert(v3d->persinv, v3d->persmat);
2363         Mat4Invert(v3d->viewinv, v3d->viewmat);
2364
2365         free_all_realtime_images();
2366         reshadeall_displist();
2367         
2368         if(v3d->drawtype > OB_WIRE) {
2369                 v3d->zbuf= TRUE;
2370                 glEnable(GL_DEPTH_TEST);
2371         }
2372
2373         if(v3d->flag & V3D_CLIPPING)
2374                 view3d_set_clipping(v3d);
2375
2376         if (v3d->drawtype==OB_TEXTURE && G.scene->world) {
2377                 glClearColor(G.scene->world->horr, G.scene->world->horg, G.scene->world->horb, 0.0); 
2378         } else {
2379                 float col[3];
2380                 BIF_GetThemeColor3fv(TH_BACK, col);
2381                 glClearColor(col[0], col[1], col[2], 0.0); 
2382         }
2383         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2384         
2385         glLoadIdentity();
2386         myloadmatrix(v3d->viewmat);
2387         
2388         /* abuse! to make sure it doesnt draw the helpstuff */
2389         G.f |= G_SIMULATION;
2390
2391         /* first draw set */
2392         setscene= G.scene->set;
2393         if(setscene) { /* if(G.scene->set) { */
2394         
2395                 base= setscene->base.first; /* base= G.scene->set->base.first; */
2396                 while(base) {
2397                         if(v3d->lay & base->lay) {
2398                                 if ELEM3(base->object->type, OB_LAMP, OB_CAMERA, OB_LATTICE);
2399                                 else {
2400                                         where_is_object(base->object);
2401         
2402                                         BIF_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
2403                                         draw_object(base, DRAW_CONSTCOLOR);
2404         
2405                                         if(base->object->transflag & OB_DUPLI) {
2406                                                 extern ListBase duplilist;
2407                                                 Base tbase;
2408                                                 
2409                                                 tbase.flag= OB_FROMDUPLI;
2410                                                 make_duplilist(setscene, base->object); /* make_duplilist(G.scene->set, base->object); */
2411                                                 ob= duplilist.first;
2412                                                 while(ob) {
2413                                                         tbase.object= ob;
2414                                                         draw_object(&tbase, DRAW_CONSTCOLOR);
2415                                                         ob= ob->id.next;
2416                                                 }
2417                                                 free_duplilist();
2418                                         }
2419                                 }
2420                         }
2421                         base= base->next;
2422                         if(base==0 && setscene && setscene->set) {
2423                                 setscene= setscene->set;
2424                                 base= setscene->base.first;
2425                         }
2426                 }
2427                 
2428                 /* Transp and X-ray afterdraw stuff */
2429                 view3d_draw_xray(v3d, DRAW_CONSTCOLOR); // clears zbuffer if it is used!
2430                 view3d_draw_transp(v3d, DRAW_CONSTCOLOR);
2431         }
2432
2433         /* first not selected and duplis */
2434         base= G.scene->base.first;
2435         while(base) {
2436                 
2437                 if(v3d->lay & base->lay) {
2438                         if ELEM3(base->object->type, OB_LAMP, OB_CAMERA, OB_LATTICE);
2439                         else {
2440         
2441                                 if(base->object->transflag & OB_DUPLI) {
2442                                         extern ListBase duplilist;
2443                                         Base tbase;
2444                                         
2445                                         draw_object(base, 0);
2446                                         
2447                                         BIF_ThemeColorBlend(TH_WIRE, TH_BACK, 0.5f);
2448                                         
2449                                         tbase.flag= OB_FROMDUPLI;
2450                                         make_duplilist(G.scene, base->object);
2451                                         ob= duplilist.first;
2452                                         while(ob) {
2453                                                 tbase.object= ob;
2454                                                 draw_object(&tbase, DRAW_CONSTCOLOR);
2455                                                 ob= ob->id.next;
2456                                         }
2457                                         free_duplilist();
2458                                 }
2459                                 else if((base->flag & SELECT)==0) {
2460                                         draw_object(base, 0);
2461                                 }
2462                         }
2463                 }
2464                 
2465                 base= base->next;
2466         }
2467
2468         /* draw selected */
2469         base= G.scene->base.first;
2470         while(base) {
2471                 
2472                 if ( ((base)->flag & SELECT) && ((base)->lay & v3d->lay) ) {
2473                         if ELEM3(base->object->type, OB_LAMP, OB_CAMERA, OB_LATTICE);
2474                         else draw_object(base, 0);
2475                 }
2476                 
2477                 base= base->next;
2478         }
2479
2480         if(G.scene->radio) RAD_drawall(v3d->drawtype>=OB_SOLID);
2481
2482         /* Transp and X-ray afterdraw stuff */
2483         view3d_draw_xray(v3d, 0);       // clears zbuffer if it is used!
2484         view3d_draw_transp(v3d, 0);
2485         
2486         if(v3d->flag & V3D_CLIPPING)
2487                 view3d_clr_clipping();
2488         
2489         if(v3d->zbuf) {
2490                 v3d->zbuf= FALSE;
2491                 glDisable(GL_DEPTH_TEST);
2492         }
2493         
2494         G.f &= ~G_SIMULATION;
2495
2496         glFlush();
2497
2498         glReadPixels(0, 0, R.rectx, R.recty, GL_RGBA, GL_UNSIGNED_BYTE, R.rectot);
2499         glLoadIdentity();
2500
2501         free_all_realtime_images();
2502 }
2503
2504
2505 double tottime = 0.0;
2506
2507 int update_time(void)
2508 {
2509         static double ltime;
2510         double time;
2511
2512         if ((U.mixbufsize)&&(audiostream_pos() != CFRA)&&(G.scene->audio.flag & AUDIO_SYNC)) return 0;
2513
2514         time = PIL_check_seconds_timer();
2515         
2516         tottime += (time - ltime);
2517         ltime = time;
2518         return (tottime < 0.0);
2519 }
2520
2521 void inner_play_anim_loop(int init, int mode)
2522 {
2523         ScrArea *sa;
2524         static ScrArea *oldsa;
2525         static double swaptime;
2526         static int curmode;
2527
2528         /* init */
2529         if(init) {
2530                 oldsa= curarea;
2531                 swaptime= 1.0/(float)G.scene->r.frs_sec;
2532                 tottime= 0.0;
2533                 curmode= mode;
2534
2535                 return;
2536         }
2537
2538         set_timecursor(CFRA);
2539         
2540         update_for_newframe_muted();
2541
2542         sa= G.curscreen->areabase.first;
2543         while(sa) {
2544                 if(sa==oldsa) {
2545                         scrarea_do_windraw(sa);
2546                 }
2547                 else if(curmode & 1) { /* catch modes 1 and 3 */
2548                         if ELEM(sa->spacetype, SPACE_VIEW3D, SPACE_SEQ) {
2549                                 scrarea_do_windraw(sa);
2550                         }
2551                 }
2552                 
2553                 sa= sa->next;   
2554         }
2555         
2556         /* make sure that swaptime passed by */
2557         tottime -= swaptime;
2558         while (update_time()) PIL_sleep_ms(1);
2559
2560         if(CFRA>=EFRA) {
2561                 if (tottime > 0.0) tottime = 0.0;
2562                 CFRA= SFRA;
2563                 audiostream_stop();
2564                 audiostream_start( CFRA );
2565         }
2566         else {
2567                 if (U.mixbufsize && (G.scene->audio.flag & AUDIO_SYNC)) CFRA = audiostream_pos();
2568                 else CFRA++;
2569         }
2570 }
2571
2572 /* play_anim: 'mode' defines where to play and if repeat is on:
2573  * - 0: current area
2574  * - 1: all view3d and seq areas
2575  * - 2: current area, no replay
2576  * - 3: all view3d and seq areas, no replay */
2577 int play_anim(int mode)
2578 {
2579         ScrArea *sa, *oldsa;
2580         int cfraont;
2581         unsigned short event=0;
2582         short val;
2583
2584         /* patch for very very old scenes */
2585         if(SFRA==0) SFRA= 1;
2586         if(EFRA==0) EFRA= 250;
2587
2588         if(SFRA>EFRA) return 0;
2589         
2590         update_time();
2591
2592         /* waitcursor(1); */
2593         G.f |= G_PLAYANIM;              /* in sequence.c and view.c this is handled */
2594
2595         cfraont= CFRA;
2596         oldsa= curarea;
2597
2598         audiostream_start( CFRA );
2599         
2600         inner_play_anim_loop(1, mode);  /* 1==init */
2601         
2602          /* forces all buffers to be OK for current frame (otherwise other windows get redrawn with CFRA+1) */
2603         curarea->win_swap= WIN_BACK_OK;
2604         screen_swapbuffers();
2605         
2606         while(TRUE) {
2607
2608                 while(qtest()) {
2609                         
2610                         /* we test events first because of MKEY event */
2611                         
2612                         event= extern_qread(&val);
2613                         if(event==ESCKEY) break;
2614                         else if(event==MIDDLEMOUSE) {
2615                                 if(U.flag & USER_VIEWMOVE) {
2616                                         if(G.qual & LR_SHIFTKEY) viewmove(0);
2617                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
2618                                         else viewmove(1);
2619                                 }
2620                                 else {
2621                                         if(G.qual & LR_SHIFTKEY) viewmove(1);
2622                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
2623                                         else viewmove(0);
2624                                 }
2625                         }
2626                         else if(event==MKEY) {
2627                                 if(val) add_timeline_marker(CFRA-1);
2628                         }
2629                 }
2630                 if(ELEM3(event, ESCKEY, SPACEKEY, RIGHTMOUSE)) break;
2631                 
2632                 inner_play_anim_loop(0, 0);
2633                 screen_swapbuffers();
2634                                 
2635                 if((mode > 1) && CFRA==EFRA) break; /* no replay */     
2636         }
2637
2638         if(event==SPACEKEY);
2639         else CFRA= cfraont;
2640         
2641         audiostream_stop();
2642
2643         if(oldsa!=curarea) areawinset(oldsa->win);
2644         
2645         /* restore all areas */
2646         sa= G.curscreen->areabase.first;
2647         while(sa) {
2648                 if( (mode && sa->spacetype==SPACE_VIEW3D) || sa==curarea) addqueue(sa->win, REDRAW, 1);
2649                 sa= sa->next;   
2650         }
2651         
2652         /* groups could have changed ipo */
2653         allspace(REMAKEIPO, 0);
2654         allqueue(REDRAWIPO, 0);
2655         allqueue(REDRAWNLA, 0);
2656         allqueue (REDRAWACTION, 0);
2657         
2658         /* restore for cfra */
2659         update_for_newframe_muted();
2660
2661         waitcursor(0);
2662         G.f &= ~G_PLAYANIM;
2663         
2664         if (event==ESCKEY || event==SPACEKEY) return 1;
2665         else return 0;
2666 }