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