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