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