- Added button align code:
[blender.git] / source / blender / src / drawview.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 //#define NAN_LINEAR_PHYSICS
34
35 #include <math.h>
36 #include <string.h>
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #ifndef WIN32
43 #include <unistd.h>
44 #include <sys/times.h>
45 #else
46 #include <io.h>
47 #include "BLI_winstuff.h"
48 #endif   
49
50 #include "MEM_guardedalloc.h"
51
52 #include "PIL_time.h"
53
54 #include "BMF_Api.h"
55
56 #include "BLI_blenlib.h"
57 #include "BLI_arithb.h"
58 #include "BLI_editVert.h"
59
60 #include "IMB_imbuf_types.h"
61
62 #include "DNA_action_types.h"
63 #include "DNA_armature_types.h"
64 #include "DNA_constraint_types.h"
65 #include "DNA_curve_types.h"
66 #include "DNA_group_types.h"
67 #include "DNA_image_types.h"
68 #include "DNA_lattice_types.h"
69 #include "DNA_object_types.h"
70 #include "DNA_screen_types.h"
71 #include "DNA_texture_types.h"
72 #include "DNA_view3d_types.h"
73 #include "DNA_userdef_types.h"
74 #include "DNA_space_types.h"
75
76 #include "BKE_action.h"
77 #include "BKE_armature.h"
78 #include "BKE_anim.h"
79 #include "BKE_constraint.h"
80 #include "BKE_curve.h"
81 #include "BKE_displist.h"
82 #include "BKE_global.h"
83 #include "BKE_ika.h"
84 #include "BKE_lattice.h"
85 #include "BKE_library.h"
86 #include "BKE_image.h"
87 #include "BKE_ipo.h"
88 #include "BKE_key.h"
89 #include "BKE_main.h"
90 #include "BKE_object.h"
91 #include "BKE_texture.h"
92 #include "BKE_utildefines.h"
93
94 #include "BIF_gl.h"
95 #include "BIF_resources.h"
96 #include "BIF_screen.h"
97 #include "BIF_interface.h"
98 #include "BIF_space.h"
99 #include "BIF_butspace.h"
100 #include "BIF_drawimage.h"
101 #include "BIF_editgroup.h"
102 #include "BIF_mywindow.h"
103 #include "BIF_editarmature.h"
104 #include "BIF_poseobject.h"
105
106 #include "BDR_drawmesh.h"
107 #include "BDR_drawobject.h"
108
109 #include "BSE_view.h"
110 #include "BSE_drawview.h"
111 #include "BSE_headerbuttons.h"
112 #include "BSE_seqaudio.h"
113 #include "BSE_filesel.h"
114
115 #include "RE_renderconverter.h"
116
117 #include "BPY_extern.h" // Blender Python library
118
119 #include "blendef.h"
120 #include "mydevice.h"
121 #include "butspace.h"  // event codes
122
123 /* Modules used */
124 #include "render.h"
125 #include "radio.h"
126
127 /* for physics in animation playback */
128 #ifdef NAN_LINEAR_PHYSICS
129 #include "sumo.h"
130 #endif
131
132 /* locals */
133 void drawname(Object *ob);
134 void star_stuff_init_func(void);
135 void star_stuff_vertex_func(float* i);
136 void star_stuff_term_func(void);
137
138 void star_stuff_init_func(void)
139 {
140         cpack(-1);
141         glPointSize(1.0);
142         glBegin(GL_POINTS);
143 }
144 void star_stuff_vertex_func(float* i)
145 {
146         glVertex3fv(i);
147 }
148 void star_stuff_term_func(void)
149 {
150         glEnd();
151 }
152
153 void setalpha_bgpic(BGpic *bgpic)
154 {
155         int x, y, alph;
156         char *rect;
157         
158         alph= (int)(255.0*(1.0-bgpic->blend));
159         
160         rect= (char *)bgpic->rect;
161         for(y=0; y< bgpic->yim; y++) {
162                 for(x= bgpic->xim; x>0; x--, rect+=4) {
163                         rect[3]= alph;
164                 }
165         }
166 }
167
168
169 void default_gl_light(void)
170 {
171         int a;
172         
173         /* initialize */
174         if(U.light[0].flag==0 && U.light[1].flag==0 && U.light[2].flag==0) {
175                 U.light[0].flag= 1;
176                 U.light[0].vec[0]= -0.3; U.light[0].vec[1]= 0.3; U.light[0].vec[2]= 0.9;
177                 U.light[0].col[0]= 0.8; U.light[0].col[1]= 0.8; U.light[0].col[2]= 0.8;
178                 U.light[0].spec[0]= 0.5; U.light[0].spec[1]= 0.5; U.light[0].spec[2]= 0.5;
179                 U.light[0].spec[3]= 1.0;
180                 
181                 U.light[1].flag= 0;
182                 U.light[1].vec[0]= 0.5; U.light[1].vec[1]= 0.5; U.light[1].vec[2]= 0.1;
183                 U.light[1].col[0]= 0.4; U.light[1].col[1]= 0.4; U.light[1].col[2]= 0.8;
184                 U.light[1].spec[0]= 0.3; U.light[1].spec[1]= 0.3; U.light[1].spec[2]= 0.5;
185                 U.light[1].spec[3]= 1.0;
186         
187                 U.light[2].flag= 0;
188                 U.light[2].vec[0]= 0.3; U.light[2].vec[1]= -0.3; U.light[2].vec[2]= -0.2;
189                 U.light[2].col[0]= 0.8; U.light[2].col[1]= 0.5; U.light[2].col[2]= 0.4;
190                 U.light[2].spec[0]= 0.5; U.light[2].spec[1]= 0.4; U.light[2].spec[2]= 0.3;
191                 U.light[2].spec[3]= 1.0;
192         }
193         
194
195         glLightfv(GL_LIGHT0, GL_POSITION, U.light[0].vec); 
196         glLightfv(GL_LIGHT0, GL_DIFFUSE, U.light[0].col); 
197         glLightfv(GL_LIGHT0, GL_SPECULAR, U.light[0].spec); 
198
199         glLightfv(GL_LIGHT1, GL_POSITION, U.light[1].vec); 
200         glLightfv(GL_LIGHT1, GL_DIFFUSE, U.light[1].col); 
201         glLightfv(GL_LIGHT1, GL_SPECULAR, U.light[1].spec); 
202
203         glLightfv(GL_LIGHT2, GL_POSITION, U.light[2].vec); 
204         glLightfv(GL_LIGHT2, GL_DIFFUSE, U.light[2].col); 
205         glLightfv(GL_LIGHT2, GL_SPECULAR, U.light[2].spec); 
206
207         for(a=0; a<8; a++) {
208                 if(a<3) {
209                         if(U.light[a].flag) glEnable(GL_LIGHT0+a);
210                         else glDisable(GL_LIGHT0+a);
211                 }
212                 else glDisable(GL_LIGHT0+a);
213         }
214         
215         glDisable(GL_LIGHTING);
216
217         glDisable(GL_COLOR_MATERIAL);
218 }
219
220 void init_gl_stuff(void)        
221 {
222         float mat_specular[] = { 0.5, 0.5, 0.5, 1.0 };
223         float mat_shininess[] = { 35.0 };
224 /*      float one= 1.0; */
225         int a, x, y;
226         GLubyte pat[32*32];
227         const GLubyte *patc= pat;
228                 
229         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_specular);
230         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
231         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
232
233         default_gl_light();
234         
235         /* no local viewer, looks ugly in ortho mode */
236         /* glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, &one); */
237         
238         glDepthFunc(GL_LEQUAL);
239         /* scaling matrices */
240         glEnable(GL_NORMALIZE);
241
242         glShadeModel(GL_FLAT);
243
244         glDisable(GL_ALPHA_TEST);
245         glDisable(GL_BLEND);
246         glDisable(GL_DEPTH_TEST);
247         glDisable(GL_FOG);
248         glDisable(GL_LIGHTING);
249         glDisable(GL_LOGIC_OP);
250         glDisable(GL_STENCIL_TEST);
251         glDisable(GL_TEXTURE_1D);
252         glDisable(GL_TEXTURE_2D);
253
254         glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
255         glPixelTransferi(GL_RED_SCALE, 1);
256         glPixelTransferi(GL_RED_BIAS, 0);
257         glPixelTransferi(GL_GREEN_SCALE, 1);
258         glPixelTransferi(GL_GREEN_BIAS, 0);
259         glPixelTransferi(GL_BLUE_SCALE, 1);
260         glPixelTransferi(GL_BLUE_BIAS, 0);
261         glPixelTransferi(GL_ALPHA_SCALE, 1);
262         glPixelTransferi(GL_ALPHA_BIAS, 0);
263
264         a= 0;
265         for(x=0; x<32; x++) {
266                 for(y=0; y<4; y++) {
267                         if( (x) & 1) pat[a++]= 0x88;
268                         else pat[a++]= 0x22;
269                 }
270         }
271         
272         glPolygonStipple(patc);
273
274
275         init_realtime_GL();     
276 }
277
278 void two_sided(int val)
279 {
280
281         /* twosided on: gives errors with x flip! */
282         glLightModeliv(GL_LIGHT_MODEL_TWO_SIDE, (GLint *)&val);
283 }
284
285 void circf(float x, float y, float rad)
286 {
287         GLUquadricObj *qobj = gluNewQuadric(); 
288         
289         gluQuadricDrawStyle(qobj, GLU_FILL); 
290         
291         glPushMatrix(); 
292         
293         glTranslatef(x,  y, 0.); 
294         
295         gluDisk( qobj, 0.0,  rad, 32, 1); 
296         
297         glPopMatrix(); 
298         
299         gluDeleteQuadric(qobj);
300 }
301
302 void circ(float x, float y, float rad)
303 {
304         GLUquadricObj *qobj = gluNewQuadric(); 
305         
306         gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
307         
308         glPushMatrix(); 
309         
310         glTranslatef(x,  y, 0.); 
311         
312         gluDisk( qobj, 0.0,  rad, 32, 1); 
313         
314         glPopMatrix(); 
315         
316         gluDeleteQuadric(qobj);
317 }
318
319 /* **********  ********** */
320
321 static void draw_bgpic(void)
322 {
323         BGpic *bgpic;
324         Image *ima;
325         float vec[3], fac, asp, zoomx, zoomy;
326         int x1, y1, x2, y2, cx, cy;
327         short mval[2];
328         
329         bgpic= G.vd->bgpic;
330         if(bgpic==0) return;
331         
332         if(bgpic->tex) {
333                 init_render_texture(bgpic->tex);
334                 free_unused_animimages();
335                 ima= bgpic->tex->ima;
336                 end_render_texture(bgpic->tex);
337         }
338         else {
339                 ima= bgpic->ima;
340         }
341         
342         if(ima==0) return;
343         if(ima->ok==0) return;
344         
345         /* test for image */
346         if(ima->ibuf==0) {
347         
348                 if(bgpic->rect) MEM_freeN(bgpic->rect);
349                 bgpic->rect= 0;
350                 
351                 if(bgpic->tex) {
352                         ima_ibuf_is_nul(bgpic->tex);
353                 }
354                 else {
355                         waitcursor(1);
356                         load_image(ima, IB_rect, G.sce, G.scene->r.cfra);
357                         waitcursor(0);
358                 }
359                 if(ima->ibuf==0) {
360                         ima->ok= 0;
361                         return;
362                 }
363         }
364
365         if(bgpic->rect==0) {
366                 
367                 bgpic->rect= MEM_dupallocN(ima->ibuf->rect);
368                 bgpic->xim= ima->ibuf->x;
369                 bgpic->yim= ima->ibuf->y;
370                 setalpha_bgpic(bgpic);
371         }
372
373         if(G.vd->persp==2) {
374                 rcti vb;
375
376                 calc_viewborder(G.vd, &vb);
377
378                 x1= vb.xmin;
379                 y1= vb.ymin;
380                 x2= vb.xmax;
381                 y2= vb.ymax;
382         }
383         else {
384                 /* calc window coord */
385                 initgrabz(0.0, 0.0, 0.0);
386                 window_to_3d(vec, 1, 0);
387                 fac= MAX3( fabs(vec[0]), fabs(vec[1]), fabs(vec[1]) );
388                 fac= 1.0/fac;
389         
390                 asp= ( (float)ima->ibuf->y)/(float)ima->ibuf->x;
391         
392                 vec[0]= vec[1]= vec[2]= 0.0;
393                 project_short_noclip(vec, mval);
394                 cx= mval[0];
395                 cy= mval[1];
396         
397                 x1=  cx+ fac*(bgpic->xof-bgpic->size);
398                 y1=  cy+ asp*fac*(bgpic->yof-bgpic->size);
399                 x2=  cx+ fac*(bgpic->xof+bgpic->size);
400                 y2=  cy+ asp*fac*(bgpic->yof+bgpic->size);
401         }
402         
403         /* complete clip? */
404         
405         if(x2 < 0 ) return;
406         if(y2 < 0 ) return;
407         if(x1 > curarea->winx ) return;
408         if(y1 > curarea->winy ) return;
409         
410         zoomx= x2-x1;
411         zoomx /= (float)ima->ibuf->x;
412         zoomy= y2-y1;
413         zoomy /= (float)ima->ibuf->y;
414
415         glEnable(GL_BLEND);
416         if(G.zbuf) glDisable(GL_DEPTH_TEST);
417
418         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
419          
420         rectwrite_part(curarea->winrct.xmin, curarea->winrct.ymin, curarea->winrct.xmax, curarea->winrct.ymax, 
421                    x1+curarea->winrct.xmin, y1+curarea->winrct.ymin, ima->ibuf->x, ima->ibuf->y, zoomx, zoomy, bgpic->rect);
422
423         glBlendFunc(GL_ONE,  GL_ZERO); 
424         glDisable(GL_BLEND);
425         if(G.zbuf) glEnable(GL_DEPTH_TEST);
426          
427 }
428
429 void timestr(double time, char *str)
430 {
431         /* format 00:00:00.00 (hr:min:sec) string has to be 12 long */
432         int  hr= (int)      time/(60*60);
433         int min= (int) fmod(time/60, 60.0);
434         int sec= (int) fmod(time, 60.0);
435         int hun= (int) fmod(time*100.0, 100.0);
436
437         if (hr) {
438                 sprintf(str, "%.2d:%.2d:%.2d.%.2d",hr,min,sec,hun);
439         } else {
440                 sprintf(str, "%.2d:%.2d.%.2d",min,sec,hun);
441         }
442
443         str[11]=0;
444 }
445
446 static void drawgrid_draw(float wx, float wy, float x, float y, float dx)
447 {
448         float fx, fy;
449         
450         x+= (wx); 
451         y+= (wy);
452         fx= x/dx;
453         fx= x-dx*floor(fx);
454         
455         while(fx< curarea->winx) {
456                 fdrawline(fx,  0.0,  fx,  (float)curarea->winy); 
457                 fx+= dx; 
458         }
459
460         fy= y/dx;
461         fy= y-dx*floor(fy);
462         
463
464         while(fy< curarea->winy) {
465                 fdrawline(0.0,  fy,  (float)curarea->winx,  fy); 
466                 fy+= dx;
467         }
468
469 }
470
471 // not intern, called in editobject for constraint axis too
472 void make_axis_color(char *col, char *col2, char axis)
473 {
474         if(axis=='x') {
475                 col2[0]= col[0]>219?255:col[0]+36;
476                 col2[1]= col[1]<26?0:col[1]-26;
477                 col2[2]= col[2]<26?0:col[2]-26;
478         }
479         else if(axis=='y') {
480                 col2[0]= col[0]<46?0:col[0]-36;
481                 col2[1]= col[1]>189?255:col[1]+66;
482                 col2[2]= col[2]<46?0:col[2]-36; 
483         }
484         else {
485                 col2[0]= col[0]<26?0:col[0]-26; 
486                 col2[1]= col[1]<26?0:col[1]-26; 
487                 col2[2]= col[2]>209?255:col[2]+46;
488         }
489         
490 }
491
492 static void drawgrid(void)
493 {
494         /* extern short bgpicmode; */
495         float wx, wy, x, y, fw, fx, fy, dx;
496         float vec4[4];
497         char col[3], col2[3];
498         
499         vec4[0]=vec4[1]=vec4[2]=0.0; 
500         vec4[3]= 1.0;
501         Mat4MulVec4fl(G.vd->persmat, vec4);
502         fx= vec4[0]; 
503         fy= vec4[1]; 
504         fw= vec4[3];
505
506         wx= (curarea->winx/2.0);        /* because of rounding errors, grid at wrong location */
507         wy= (curarea->winy/2.0);
508
509         x= (wx)*fx/fw;
510         y= (wy)*fy/fw;
511
512         vec4[0]=vec4[1]=G.vd->grid; 
513         vec4[2]= 0.0;
514         vec4[3]= 1.0;
515         Mat4MulVec4fl(G.vd->persmat, vec4);
516         fx= vec4[0]; 
517         fy= vec4[1]; 
518         fw= vec4[3];
519
520         dx= fabs(x-(wx)*fx/fw);
521         if(dx==0) dx= fabs(y-(wy)*fy/fw);
522         
523         glDepthMask(0);         // disable write in zbuffer
524         
525         /* check zoom out */
526         BIF_ThemeColor(TH_GRID);
527         persp(PERSP_WIN);
528
529         if(dx<6.0) {
530                 dx*= 10.0;
531                 
532                 if(dx<6.0) {    
533                         dx*= 10.0;
534                         
535                         if(dx<6.0) {
536                                 dx*=10;
537                                 if(dx<6.0);
538                                 else {
539                                         BIF_ThemeColor(TH_GRID);
540                                         drawgrid_draw(wx, wy, x, y, dx);
541                                 }
542                         }
543                         else {  // start blending out
544                                 BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
545                                 drawgrid_draw(wx, wy, x, y, dx);
546                         
547                                 BIF_ThemeColor(TH_GRID);
548                                 drawgrid_draw(wx, wy, x, y, 10*dx);
549                         }
550                 }
551                 else {  // start blending out (6 < dx < 60)
552                         BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
553                         drawgrid_draw(wx, wy, x, y, dx);
554                         
555                         BIF_ThemeColor(TH_GRID);
556                         drawgrid_draw(wx, wy, x, y, 10*dx);
557                 }
558         }
559         else {
560                 if(dx>60.0) {           // start blending in
561                         dx/= 10.0;                      
562                         if(dx>60.0) {           // start blending in
563                                 dx/= 10.0;
564                                 if(dx>60.0) {
565                                         BIF_ThemeColor(TH_GRID);
566                                         drawgrid_draw(wx, wy, x, y, dx);
567                                 }
568                                 else {
569                                         BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
570                                         drawgrid_draw(wx, wy, x, y, dx);
571                                         BIF_ThemeColor(TH_GRID);
572                                         drawgrid_draw(wx, wy, x, y, dx*10);
573                                 }
574                         }
575                         else {
576                                 BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
577                                 drawgrid_draw(wx, wy, x, y, dx);
578                                 BIF_ThemeColor(TH_GRID);
579                                 drawgrid_draw(wx, wy, x, y, dx*10);
580                         }
581                 }
582                 else {
583                         BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
584                         drawgrid_draw(wx, wy, x, y, dx);
585                         BIF_ThemeColor(TH_GRID);
586                         drawgrid_draw(wx, wy, x, y, dx*10);
587                 }
588         }
589
590         x+= (wx); 
591         y+= (wy);
592         BIF_GetThemeColor3ubv(TH_GRID, col);
593         
594         /* center cross */
595         if(G.vd->view==3) make_axis_color(col, col2, 'y');
596         else make_axis_color(col, col2, 'x');
597         glColor3ubv(col2);
598         
599         fdrawline(0.0,  y,  (float)curarea->winx,  y); 
600         
601         if(G.vd->view==7) make_axis_color(col, col2, 'y');
602         else make_axis_color(col, col2, 'z');
603         glColor3ubv(col2);
604
605         fdrawline(x, 0.0, x, (float)curarea->winy); 
606
607         glDepthMask(1);         // enable write in zbuffer
608         persp(PERSP_VIEW);
609 }
610
611
612 static void drawfloor(void)
613 {
614         View3D *vd;
615         float vert[3], grid;
616         int a, gridlines;
617         char col[3], col2[3];
618         
619         vd= curarea->spacedata.first;
620
621         vert[2]= 0.0;
622
623         if(vd->gridlines<3) return;
624
625         gridlines= vd->gridlines/2;
626         grid= gridlines*vd->grid;
627         
628         BIF_GetThemeColor3ubv(TH_GRID, col);
629         
630         for(a= -gridlines;a<=gridlines;a++) {
631
632                 if(a==0) {
633                         make_axis_color(col, col2, 'y');
634                         glColor3ubv(col2);
635                 }
636                 else if( (a % 10)==0) {
637                         BIF_ThemeColorShade(TH_GRID, -10);
638                 }
639                 else BIF_ThemeColorShade(TH_GRID, 10);
640                 
641         
642                 glBegin(GL_LINE_STRIP);
643         vert[0]= a*vd->grid;
644         vert[1]= grid;
645         glVertex3fv(vert);
646         vert[1]= -grid;
647         glVertex3fv(vert);
648                 glEnd();
649         }
650         
651         for(a= -gridlines;a<=gridlines;a++) {
652                 if(a==0) {
653                         make_axis_color(col, col2, 'x');
654                         glColor3ubv(col2);
655                 }
656                 else if( (a % 10)==0) {
657                         BIF_ThemeColorShade(TH_GRID, -10);
658                 }
659                 else BIF_ThemeColorShade(TH_GRID, 10);
660         
661                 glBegin(GL_LINE_STRIP);
662         vert[1]= a*vd->grid;
663         vert[0]= grid;
664         glVertex3fv(vert );
665         vert[0]= -grid;
666         glVertex3fv(vert);
667                 glEnd();
668         }
669
670 }
671
672 static void drawcursor(void)
673 {
674
675         if(G.f & G_PLAYANIM) return;
676         
677         project_short( give_cursor(), &G.vd->mx);
678
679         G.vd->mxo= G.vd->mx;
680         G.vd->myo= G.vd->my;
681
682         if( G.vd->mx!=3200) {
683                 
684                 setlinestyle(0); 
685                 cpack(0xFF);
686                 circ((float)G.vd->mx, (float)G.vd->my, 10.0);
687                 setlinestyle(4); 
688                 cpack(0xFFFFFF);
689                 circ((float)G.vd->mx, (float)G.vd->my, 10.0);
690                 setlinestyle(0);
691                 cpack(0x0);
692                 
693                 sdrawline(G.vd->mx-20, G.vd->my, G.vd->mx-5, G.vd->my);
694                 sdrawline(G.vd->mx+5, G.vd->my, G.vd->mx+20, G.vd->my);
695                 sdrawline(G.vd->mx, G.vd->my-20, G.vd->mx, G.vd->my-5);
696                 sdrawline(G.vd->mx, G.vd->my+5, G.vd->mx, G.vd->my+20);
697         }
698 }
699
700 static void view3d_get_viewborder_size(View3D *v3d, float size_r[2])
701 {
702         float winmax= MAX2(v3d->area->winx, v3d->area->winy);
703         float aspect= (float) (G.scene->r.xsch*G.scene->r.xasp)/(G.scene->r.ysch*G.scene->r.yasp);
704
705         if(aspect>1.0) {
706                 size_r[0]= winmax;
707                 size_r[1]= winmax/aspect;
708         } else {
709                 size_r[0]= winmax*aspect;
710                 size_r[1]= winmax;
711         }
712 }
713
714 void calc_viewborder(struct View3D *v3d, rcti *viewborder_r)
715 {
716         float zoomfac, size[2];
717
718         view3d_get_viewborder_size(v3d, size);
719
720                 /* magic zoom calculation, no idea what
721              * it signifies, if you find out, tell me! -zr
722                  */
723         /* simple, its magic dude!
724          * well, to be honest, this gives a natural feeling zooming
725          * with multiple keypad presses (ton)
726          */
727         
728         zoomfac= (M_SQRT2 + v3d->camzoom/50.0);
729         zoomfac= (zoomfac*zoomfac)*0.25;
730         
731         size[0]= size[0]*zoomfac;
732         size[1]= size[1]*zoomfac;
733
734                 /* center in window */
735         viewborder_r->xmin= 0.5*v3d->area->winx - 0.5*size[0];
736         viewborder_r->ymin= 0.5*v3d->area->winy - 0.5*size[1];
737         viewborder_r->xmax= viewborder_r->xmin + size[0];
738         viewborder_r->ymax= viewborder_r->ymin + size[1];
739 }
740
741 void view3d_set_1_to_1_viewborder(View3D *v3d)
742 {
743         float size[2];
744         int im_width= (G.scene->r.size*G.scene->r.xsch)/100;
745
746         view3d_get_viewborder_size(v3d, size);
747
748         v3d->camzoom= (sqrt(4.0*im_width/size[0]) - M_SQRT2)*50.0;
749         v3d->camzoom= CLAMPIS(v3d->camzoom, -30, 300);
750 }
751
752 static void drawviewborder(void)
753 {
754         float fac, a;
755         float x1, x2, y1, y2;
756         float x3, y3, x4, y4;
757         rcti viewborder;
758
759         calc_viewborder(G.vd, &viewborder);
760         x1= viewborder.xmin;
761         y1= viewborder.ymin;
762         x2= viewborder.xmax;
763         y2= viewborder.ymax;
764
765         /* edge */
766         setlinestyle(3);
767         cpack(0);
768         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
769         glRectf(x1+1,  y1-1,  x2+1,  y2-1); 
770         
771         cpack(0xFFFFFF);
772         glRectf(x1,  y1,  x2,  y2); 
773
774         /* border */
775         if(G.scene->r.mode & R_BORDER) {
776                 
777                 cpack(0);
778                 x3= x1+ G.scene->r.border.xmin*(x2-x1);
779                 y3= y1+ G.scene->r.border.ymin*(y2-y1);
780                 x4= x1+ G.scene->r.border.xmax*(x2-x1);
781                 y4= y1+ G.scene->r.border.ymax*(y2-y1);
782                 
783                 glRectf(x3+1,  y3-1,  x4+1,  y4-1); 
784                 
785                 cpack(0x4040FF);
786                 glRectf(x3,  y3,  x4,  y4); 
787         }
788
789         /* safety border */
790
791         fac= 0.1;
792         
793         a= fac*(x2-x1);
794         x1+= a; 
795         x2-= a;
796
797         a= fac*(y2-y1);
798         y1+= a;
799         y2-= a;
800
801         cpack(0);
802         glRectf(x1+1,  y1-1,  x2+1,  y2-1);
803         cpack(0xFFFFFF);
804         glRectf(x1,  y1,  x2,  y2);
805
806         setlinestyle(0);
807         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
808
809 }
810
811
812 void backdrawview3d(int test)
813 {
814         struct Base *base;
815         int tel=1;
816
817         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT));
818         else {
819                 G.vd->flag &= ~V3D_NEEDBACKBUFDRAW;
820                 return;
821         }
822
823         if(G.vd->flag & V3D_NEEDBACKBUFDRAW); else return;
824         if(G.obedit) {
825                 G.vd->flag &= ~V3D_NEEDBACKBUFDRAW;
826                 return;
827         }
828         
829         if(test) {
830                 if(qtest()) {
831                         addafterqueue(curarea->win, BACKBUFDRAW, 1);
832                         return;
833                 }
834         }
835         
836         persp(PERSP_VIEW);
837         
838 #ifdef __APPLE__
839         glDrawBuffer(GL_AUX0);
840 #endif  
841         if(G.vd->drawtype > OB_WIRE) G.zbuf= TRUE;
842         curarea->win_swap &= ~WIN_BACK_OK;
843         
844         glDisable(GL_DITHER);
845
846         glClearColor(0.0, 0.0, 0.0, 0.0); 
847         if(G.zbuf) {
848                 glEnable(GL_DEPTH_TEST);
849                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
850         }
851         else {
852                 glClear(GL_COLOR_BUFFER_BIT);
853                 glDisable(GL_DEPTH_TEST);
854         }
855         
856         G.f |= G_BACKBUFSEL;
857         
858         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
859                 base= (G.scene->basact);
860                 if(base && (base->lay & G.vd->lay)) {
861                         draw_object(base);
862                 }
863         }
864         else {
865
866                 base= (G.scene->base.first);
867                 while(base) {
868                         
869                         /* each base, because of multiple windows */
870                         base->selcol= 0x070707 | ( ((tel & 0xF00)<<12) + ((tel & 0xF0)<<8) + ((tel & 0xF)<<4) );
871                         tel++;
872         
873                         if(base->lay & G.vd->lay) {
874                                 
875                                 if(test) {
876                                         if(qtest()) {
877                                                 addafterqueue(curarea->win, BACKBUFDRAW, 1);
878                                                 break;
879                                         }
880                                 }
881                                 
882                                 cpack(base->selcol);
883                                 draw_object(base);
884                         }
885                         base= base->next;
886                 }
887         }
888         
889         if(base==0) G.vd->flag &= ~V3D_NEEDBACKBUFDRAW;
890
891         G.f &= ~G_BACKBUFSEL;
892         G.zbuf= FALSE; 
893         glDisable(GL_DEPTH_TEST);
894         glEnable(GL_DITHER);
895
896 #ifdef __APPLE__
897         glDrawBuffer(GL_BACK); /* we were in aux buffers */
898 #endif
899 }
900
901                 
902 void drawname(Object *ob)
903 {
904         cpack(0x404040);
905         glRasterPos3f(0.0,  0.0,  0.0);
906         
907         BMF_DrawString(G.font, " ");
908         BMF_DrawString(G.font, ob->id.name+2);
909 }
910
911
912 static void draw_selected_name(char *name)
913 {
914         char info[128];
915
916         sprintf(info, "(%d) %s", CFRA, name);
917
918         BIF_ThemeColor(TH_TEXT_HI);
919         glRasterPos2i(30,  10);
920         BMF_DrawString(G.fonts, info);
921 }
922
923
924 static void draw_view_icon(void)
925 {
926         BIFIconID icon;
927         
928         if(G.vd->view==7) icon= ICON_AXIS_TOP;
929         else if(G.vd->view==1) icon= ICON_AXIS_FRONT;
930         else if(G.vd->view==3) icon= ICON_AXIS_SIDE;
931         else return ;
932
933         glEnable(GL_BLEND);
934         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
935         
936         glRasterPos2f(5.0, 5.0);
937         BIF_draw_icon(icon);
938         
939         glBlendFunc(GL_ONE,  GL_ZERO); 
940         glDisable(GL_BLEND);
941 }
942
943 /* ******************* view3d space & buttons ************** */
944
945 static void view3d_change_bgpic_ima(View3D *v3d, Image *newima) {
946         if (v3d->bgpic && v3d->bgpic->ima!=newima) {
947                 if (newima)
948                         id_us_plus((ID*) newima);
949                 if (v3d->bgpic->ima)
950                         v3d->bgpic->ima->id.us--;
951                 v3d->bgpic->ima= newima;
952
953                 if(v3d->bgpic->rect) MEM_freeN(v3d->bgpic->rect);
954                 v3d->bgpic->rect= NULL;
955                 
956                 allqueue(REDRAWVIEW3D, 0);
957         }
958 }
959 static void view3d_change_bgpic_tex(View3D *v3d, Tex *newtex) {
960         if (v3d->bgpic && v3d->bgpic->tex!=newtex) {
961                 if (newtex)
962                         id_us_plus((ID*) newtex);
963                 if (v3d->bgpic->tex)
964                         v3d->bgpic->tex->id.us--;
965                 v3d->bgpic->tex= newtex;
966                 
967                 allqueue(REDRAWVIEW3D, 0);
968         }
969 }
970
971 static void load_bgpic_image(char *name)
972 {
973         Image *ima;
974         View3D *vd;
975         
976         vd= G.vd;
977         if(vd==0 || vd->bgpic==0) return;
978         
979         ima= add_image(name);
980         if(ima) {
981                 if(vd->bgpic->ima) {
982                         vd->bgpic->ima->id.us--;
983                 }
984                 vd->bgpic->ima= ima;
985                 
986                 free_image_buffers(ima);        /* force read again */
987                 ima->ok= 1;
988         }
989         allqueue(REDRAWVIEW3D, 0);
990         
991 }
992
993 /* this one assumes there is only one global active object in blender...  (for object panel) */
994 static float ob_eul[4]; // used for quat too....
995 /* this one assumes there is only one editmode in blender...  (for object panel) */
996 static float ve_median[4];
997 static int ve_median_tot=0;
998
999 /* is used for both read and write... */
1000 static void v3d_editvertex_buts(uiBlock *block, Object *ob, float lim)
1001 {
1002         EditVert *eve;
1003         float median[3];
1004         int tot;
1005         
1006         median[0]= median[1]= median[2]= 0.0;
1007         tot= 0;
1008         
1009         if(ob->type==OB_MESH) {
1010                 eve= G.edve.first;
1011                 while(eve) {
1012                         if(eve->f & 1) {
1013                                 tot++;
1014                                 VecAddf(median, median, eve->co);
1015                         }
1016                         eve= eve->next;
1017                 }
1018         }
1019         else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
1020                 extern ListBase editNurb; /* editcurve.c */
1021                 Nurb *nu;
1022                 BPoint *bp;
1023                 BezTriple *bezt;
1024                 int a;
1025                 
1026                 nu= editNurb.first;
1027                 while(nu) {
1028                         if((nu->type & 7)==1) {
1029                                 bezt= nu->bezt;
1030                                 a= nu->pntsu;
1031                                 while(a--) {
1032                                         if(bezt->f2 & 1) {
1033                                                 VecAddf(median, median, bezt->vec[1]);
1034                                                 tot++;
1035                                         }
1036                                         else {
1037                                                 if(bezt->f1 & 1) {
1038                                                         VecAddf(median, median, bezt->vec[0]);
1039                                                         tot++;
1040                                                 }
1041                                                 if(bezt->f3 & 1) {
1042                                                         VecAddf(median, median, bezt->vec[2]);
1043                                                         tot++;
1044                                                 }
1045                                         }
1046                                         bezt++;
1047                                 }
1048                         }
1049                         else {
1050                                 bp= nu->bp;
1051                                 a= nu->pntsu*nu->pntsv;
1052                                 while(a--) {
1053                                         if(bp->f1 & 1) {
1054                                                 VecAddf(median, median, bp->vec);
1055                                                 tot++;
1056                                         }
1057                                         bp++;
1058                                 }
1059                         }
1060                         nu= nu->next;
1061                 }
1062         
1063         }
1064         if(tot==0) return;
1065
1066         median[0] /= (float)tot;
1067         median[1] /= (float)tot;
1068         median[2] /= (float)tot;
1069         
1070         if(block) {     // buttons
1071         
1072                 ve_median_tot= tot;
1073                 VECCOPY(ve_median, median);
1074                 
1075                 uiBlockBeginAlign(block, 'v');
1076                 if(ve_median_tot==1) {
1077                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex X:", 10, 140, 300, 19, &(ve_median[0]), -lim, lim, 10, 0, "");
1078                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex Y:", 10, 120, 300, 19, &(ve_median[1]), -lim, lim, 10, 0, "");
1079                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex Z:", 10, 100, 300, 19, &(ve_median[2]), -lim, lim, 10, 0, "");
1080                 }
1081                 else {
1082                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median X:", 10, 140, 300, 19, &(ve_median[0]), -lim, lim, 10, 0, "");
1083                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Y:", 10, 120, 300, 19, &(ve_median[1]), -lim, lim, 10, 0, "");
1084                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Z:", 10, 100, 300, 19, &(ve_median[2]), -lim, lim, 10, 0, "");
1085                 }
1086                 uiBlockEndAlign(block);
1087         }
1088         else {  // apply
1089                 
1090                 VecSubf(median, ve_median, median);
1091                 
1092                 if(ob->type==OB_MESH) {
1093                         eve= G.edve.first;
1094                         while(eve) {
1095                                 if(eve->f & 1) {
1096                                         VecAddf(eve->co, eve->co, median);
1097                                 }
1098                                 eve= eve->next;
1099                         }
1100                 }
1101                 else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
1102                         extern ListBase editNurb; /* editcurve.c */
1103                         Nurb *nu;
1104                         BPoint *bp;
1105                         BezTriple *bezt;
1106                         int a;
1107                         
1108                         nu= editNurb.first;
1109                         while(nu) {
1110                                 if((nu->type & 7)==1) {
1111                                         bezt= nu->bezt;
1112                                         a= nu->pntsu;
1113                                         while(a--) {
1114                                                 if(bezt->f2 & 1) {
1115                                                         VecAddf(bezt->vec[0], bezt->vec[0], median);
1116                                                         VecAddf(bezt->vec[1], bezt->vec[1], median);
1117                                                         VecAddf(bezt->vec[2], bezt->vec[2], median);
1118                                                 }
1119                                                 else {
1120                                                         if(bezt->f1 & 1) {
1121                                                                 VecAddf(bezt->vec[0], bezt->vec[0], median);
1122                                                         }
1123                                                         if(bezt->f3 & 1) {
1124                                                                 VecAddf(bezt->vec[2], bezt->vec[2], median);
1125                                                         }
1126                                                 }
1127                                                 bezt++;
1128                                         }
1129                                 }
1130                                 else {
1131                                         bp= nu->bp;
1132                                         a= nu->pntsu*nu->pntsv;
1133                                         while(a--) {
1134                                                 if(bp->f1 & 1) {
1135                                                         VecAddf(bp->vec, bp->vec, median);
1136                                                         tot++;
1137                                                 }
1138                                                 bp++;
1139                                         }
1140                                 }
1141                                 test2DNurb(nu);
1142                                 testhandlesNurb(nu); /* test for bezier too */
1143
1144                                 nu= nu->next;
1145                         }
1146                         makeDispList(G.obedit);
1147                 }
1148         }
1149 }
1150
1151 static void v3d_posearmature_buts(uiBlock *block, Object *ob, float lim)
1152 {
1153         bArmature *arm;
1154         Bone *bone;
1155
1156         arm = get_armature(OBACT);
1157         if (!arm)
1158                 return;
1159
1160         bone = get_first_selected_bone();
1161
1162         if (!bone)
1163                 return;
1164         uiBlockBeginAlign(block, 'v');
1165         uiDefButF(block, NUM, B_ARMATUREPANEL2, "QuatX:",       10, 120, 140, 19, bone->quat, -100.0, 100.0, 10, 0, "");
1166         uiDefButF(block, NUM, B_ARMATUREPANEL2, "QuatZ:",       10, 100, 140, 19, bone->quat+2, -100.0, 100.0, 10, 0, "");
1167         uiBlockBeginAlign(block, 'v');
1168         uiDefButF(block, NUM, B_ARMATUREPANEL2, "QuatY:",       160, 120, 140, 19, bone->quat+1, -100.0, 100.0, 10, 0, "");
1169         uiDefButF(block, NUM, B_ARMATUREPANEL2, "QuatW:",       160, 100, 140, 19, bone->quat+3, -100.0, 100.0, 10, 0, "");
1170         uiBlockBeginAlign(block, 'v');
1171         uiDefButF(block, NUM, B_ARMATUREPANEL2, "LocX:",        10, 70, 140, 19, bone->loc, -lim, lim, 100, 0, "");
1172         uiDefButF(block, NUM, B_ARMATUREPANEL2, "LocY:",        10, 50, 140, 19, bone->loc+1, -lim, lim, 100, 0, "");
1173         uiDefButF(block, NUM, B_ARMATUREPANEL2, "locZ:",        10, 30, 140, 19, bone->loc+2, -lim, lim, 100, 0, "");
1174         uiBlockBeginAlign(block, 'v');
1175         uiDefButF(block, NUM, B_ARMATUREPANEL2, "SizeX:",       160, 70, 140, 19, bone->size, -lim, lim, 100, 0, "");
1176         uiDefButF(block, NUM, B_ARMATUREPANEL2, "SizeZ:",       160, 50, 140, 19, bone->size+1, -lim, lim, 100, 0, "");
1177         uiDefButF(block, NUM, B_ARMATUREPANEL2, "SizeZ:",       160, 30, 140, 19, bone->size+2, -lim, lim, 100, 0, "");
1178         uiBlockEndAlign(block);
1179 }
1180
1181 static void v3d_editarmature_buts(uiBlock *block, Object *ob, float lim)
1182 {
1183         EditBone *ebone;
1184         
1185         ebone= G.edbo.first;
1186
1187         for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1188                 if (ebone->flag & BONE_SELECTED)
1189                         break;
1190         }
1191
1192         if (!ebone)
1193                 return;
1194         uiBlockBeginAlign(block, 'v');
1195         uiDefButF(block, NUM, B_ARMATUREPANEL1, "RootX:",       10, 70, 140, 19, ebone->head, -lim, lim, 100, 0, "");
1196         uiDefButF(block, NUM, B_ARMATUREPANEL1, "RootY:",       10, 50, 140, 19, ebone->head+1, -lim, lim, 100, 0, "");
1197         uiDefButF(block, NUM, B_ARMATUREPANEL1, "RootZ:",       10, 30, 140, 19, ebone->head+2, -lim, lim, 100, 0, "");
1198         uiBlockBeginAlign(block, 'v');
1199         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TipX:",        160, 70, 140, 19, ebone->tail, -lim, lim, 100, 0, "");
1200         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TipY:",        160, 50, 140, 19, ebone->tail+1, -lim, lim, 100, 0, "");
1201         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TipZ:",        160, 30, 140, 19, ebone->tail+2, -lim, lim, 100, 0, "");
1202         uiBlockEndAlign(block);
1203         ob_eul[0]= 180.0*ebone->roll/M_PI;
1204         uiDefButF(block, NUM, B_ARMATUREPANEL1, "Roll:",        10, 100, 140, 19, ob_eul, -lim, lim, 100, 0, "");
1205
1206 }
1207
1208
1209
1210 void do_viewbuts(unsigned short event)
1211 {
1212         View3D *vd;
1213         Object *ob= OBACT;
1214         char *name;
1215         
1216         vd= G.vd;
1217         if(vd==0) return;
1218
1219         switch(event) {
1220         case B_LOADBGPIC:
1221                 if(vd->bgpic && vd->bgpic->ima) name= vd->bgpic->ima->name;
1222                 else name= G.ima;
1223                 
1224                 if(G.qual==LR_CTRLKEY)
1225                         activate_imageselect(FILE_SPECIAL, "SELECT IMAGE", name, load_bgpic_image);
1226                 else
1227                         activate_fileselect(FILE_SPECIAL, "SELECT IMAGE", name, load_bgpic_image);
1228                 break;
1229                 
1230         case B_BLENDBGPIC:
1231                 if(vd->bgpic && vd->bgpic->rect) setalpha_bgpic(vd->bgpic);
1232                 addqueue(curarea->win, REDRAW, 1);
1233                 break;
1234                 
1235         case B_BGPICBROWSE:
1236                 if(vd->bgpic) {
1237                         if (G.buts->menunr==-2) {
1238                                 activate_databrowse((ID*) vd->bgpic->ima, ID_IM, 0, B_BGPICBROWSE, &G.buts->menunr, do_viewbuts);
1239                         } else if (G.buts->menunr>0) {
1240                                 Image *newima= (Image*) BLI_findlink(&G.main->image, G.buts->menunr-1);
1241
1242                                 if (newima)
1243                                         view3d_change_bgpic_ima(vd, newima);
1244                         }
1245                 }
1246                 break;
1247                 
1248         case B_BGPICCLEAR:
1249                 if (vd->bgpic)
1250                         view3d_change_bgpic_ima(vd, NULL);
1251                 break;
1252                 
1253         case B_BGPICTEX:
1254                 if (vd->bgpic) {
1255                         if (G.buts->texnr==-2) {
1256                                 activate_databrowse((ID*) vd->bgpic->tex, ID_TE, 0, B_BGPICTEX, &G.buts->texnr, do_viewbuts);
1257                         } else if (G.buts->texnr>0) {
1258                                 Tex *newtex= (Tex*) BLI_findlink(&G.main->tex, G.buts->texnr-1);
1259                                 
1260                                 if (newtex)
1261                                         view3d_change_bgpic_tex(vd, newtex);
1262                         }
1263                 }
1264                 break;
1265                 
1266         case B_BGPICTEXCLEAR:
1267                 if (vd->bgpic)
1268                         view3d_change_bgpic_tex(vd, NULL);
1269                 break;
1270                 
1271         case B_OBJECTPANELROT:
1272                 if(ob) {
1273                         ob->rot[0]= M_PI*ob_eul[0]/180.0;
1274                         ob->rot[1]= M_PI*ob_eul[1]/180.0;
1275                         ob->rot[2]= M_PI*ob_eul[2]/180.0;
1276                         allqueue(REDRAWVIEW3D, 1);
1277                 }
1278                 break;
1279                 
1280         case B_OBJECTPANELMEDIAN:
1281                 if(ob) {
1282                         v3d_editvertex_buts(NULL, ob, 1.0);
1283                         makeDispList(ob);
1284                         allqueue(REDRAWVIEW3D, 1);
1285                 }
1286                 break;
1287                 
1288         case B_ARMATUREPANEL1:
1289                 {
1290                         EditBone *ebone, *child;
1291                         
1292                         ebone= G.edbo.first;
1293                         for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1294                                 if (ebone->flag & BONE_SELECTED) break;
1295                         }
1296                         if (ebone) {
1297                                 ebone->roll= M_PI*ob_eul[0]/180.0;
1298                                 //      Update our parent
1299                                 if (ebone->parent && ebone->flag & BONE_IK_TOPARENT){
1300                                         VECCOPY (ebone->parent->tail, ebone->head);
1301                                 }
1302                         
1303                                 //      Update our children if necessary
1304                                 for (child = G.edbo.first; child; child=child->next){
1305                                         if (child->parent == ebone && child->flag & BONE_IK_TOPARENT){
1306                                                 VECCOPY (child->head, ebone->tail);
1307                                         }
1308                                 }
1309                                 allqueue(REDRAWVIEW3D, 1);
1310                         }
1311                 }
1312                 break;
1313         case B_ARMATUREPANEL2:
1314                 {
1315                         bPoseChannel *chan;
1316                         bArmature *arm;
1317                         Bone *bone;
1318                 
1319                         arm = get_armature(OBACT);
1320                         if (!arm) return;
1321                         bone = get_first_selected_bone();
1322                 
1323                         if (!bone) return;
1324
1325                         /* This is similar to code in special_trans_update */
1326         
1327                         if (!G.obpose->pose) G.obpose->pose= MEM_callocN(sizeof(bPose), "pose");
1328                         chan = MEM_callocN (sizeof (bPoseChannel), "transPoseChannel");
1329                 
1330                         chan->flag |= POSE_LOC|POSE_ROT|POSE_SIZE;
1331                         memcpy (chan->loc, bone->loc, sizeof (chan->loc));
1332                         memcpy (chan->quat, bone->quat, sizeof (chan->quat));
1333                         memcpy (chan->size, bone->size, sizeof (chan->size));
1334                         strcpy (chan->name, bone->name);
1335                         
1336                         set_pose_channel (G.obpose->pose, chan);
1337                         allqueue(REDRAWVIEW3D, 1);
1338                 }
1339         }
1340 }
1341
1342
1343 static void view3d_panel_object(short cntrl)    // VIEW3D_HANDLER_OBJECT
1344 {
1345         uiBlock *block;
1346         Object *ob= OBACT;
1347         float lim;
1348         
1349         if(ob==NULL) return;
1350
1351         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_object", UI_EMBOSS, UI_HELV, curarea->win);
1352         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
1353         uiSetPanelHandler(VIEW3D_HANDLER_OBJECT);  // for close and esc
1354         if(uiNewPanel(curarea, block, "Transform Properties", "View3d", 10, 230, 318, 204)==0) return;
1355
1356         uiDefBut(block, TEX, B_IDNAME, "OB: ",  10,180,140,20, ob->id.name+2, 0.0, 18.0, 0, 0, "");
1357         uiDefIDPoinBut(block, test_obpoin_but, B_REDR, "Par:", 160, 180, 140, 20, &ob->parent, "Parent Object"); 
1358
1359         lim= 1000.0*MAX2(1.0, G.vd->grid);
1360
1361         if(ob==G.obedit) {
1362                 if(ob->type==OB_ARMATURE) v3d_editarmature_buts(block, ob, lim);
1363                 else v3d_editvertex_buts(block, ob, lim);
1364         }
1365         else if(ob==G.obpose) {
1366                 v3d_posearmature_buts(block, ob, lim);
1367         }
1368         else {
1369                 uiBlockBeginAlign(block, 'v');
1370                 uiDefButF(block, NUM, REDRAWVIEW3D, "LocX:",            10, 140, 140, 19, &(ob->loc[0]), -lim, lim, 100, 0, "");
1371                 uiDefButF(block, NUM, REDRAWVIEW3D, "LocY:",            10, 120, 140, 19, &(ob->loc[1]), -lim, lim, 100, 0, "");
1372                 uiDefButF(block, NUM, REDRAWVIEW3D, "LocZ:",            10, 100, 140, 19, &(ob->loc[2]), -lim, lim, 100, 0, "");
1373         
1374                 ob_eul[0]= 180.0*ob->rot[0]/M_PI;
1375                 ob_eul[1]= 180.0*ob->rot[1]/M_PI;
1376                 ob_eul[2]= 180.0*ob->rot[2]/M_PI;
1377                 
1378                 uiBlockBeginAlign(block, 'v');
1379                 uiDefButF(block, NUM, B_OBJECTPANELROT, "RotX:",        10, 70, 140, 19, &(ob_eul[0]), -lim, lim, 1000, 0, "");
1380                 uiDefButF(block, NUM, B_OBJECTPANELROT, "RotY:",        10, 50, 140, 19, &(ob_eul[1]), -lim, lim, 1000, 0, "");
1381                 uiDefButF(block, NUM, B_OBJECTPANELROT, "RotZ:",        10, 30, 140, 19, &(ob_eul[2]), -lim, lim, 1000, 0, "");
1382                 uiBlockBeginAlign(block, 'v');
1383                 uiDefButF(block, NUM, REDRAWVIEW3D, "SizeX:",           160, 70, 140, 19, &(ob->size[0]), -lim, lim, 100, 0, "");
1384                 uiDefButF(block, NUM, REDRAWVIEW3D, "SizeY:",           160, 50, 140, 19, &(ob->size[1]), -lim, lim, 100, 0, "");
1385                 uiDefButF(block, NUM, REDRAWVIEW3D, "SizeZ:",           160, 30, 140, 19, &(ob->size[2]), -lim, lim, 100, 0, "");
1386                 uiBlockEndAlign(block);
1387         }
1388 }
1389
1390 static void view3d_panel_background(cntrl)      // VIEW3D_HANDLER_BACKGROUND
1391 {
1392         uiBlock *block;
1393         View3D *vd;
1394         ID *id;
1395         char *strp;
1396         
1397         vd= G.vd;
1398
1399         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_background", UI_EMBOSS, UI_HELV, curarea->win);
1400         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
1401         uiSetPanelHandler(VIEW3D_HANDLER_BACKGROUND);  // for close and esc
1402         if(uiNewPanel(curarea, block, "Background", "View3d", 340, 10, 318, 204)==0) return;
1403
1404         if(vd->flag & V3D_DISPBGPIC) {
1405                 if(vd->bgpic==0) {
1406                         vd->bgpic= MEM_callocN(sizeof(BGpic), "bgpic");
1407                         vd->bgpic->size= 5.0;
1408                         vd->bgpic->blend= 0.5;
1409                 }
1410         }
1411         uiDefButS(block, TOG|BIT|1, REDRAWVIEW3D, "BackGroundPic",      10,160,150,20 , &vd->flag, 0, 0, 0, 0, "Display a picture in the 3D background");
1412         
1413         if(vd->bgpic) {
1414                 
1415                 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");
1416                 
1417                 id= (ID *)vd->bgpic->ima;
1418                 IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->image), id, &(G.buts->menunr));
1419                 if(strp[0])
1420                         uiDefButS(block, MENU, B_BGPICBROWSE, strp,     10,140,20,19, &(G.buts->menunr), 0, 0, 0, 0, "Browse");
1421                 MEM_freeN(strp);
1422                 
1423                 if(vd->bgpic->ima)  {
1424                         uiDefBut(block, TEX,        0,"BGpic: ",                30,140,260,19,&vd->bgpic->ima->name,0.0,100.0, 0, 0, "The Selected BackGroundPic");
1425                         uiDefIconBut(block, BUT, B_BGPICCLEAR, ICON_X,  290,140,20,19, 0, 0, 0, 0, 0, "Remove background image link");
1426                 }
1427                 uiDefBut(block, BUT,        B_LOADBGPIC, "LOAD",        10,120,100,19, 0, 0, 0, 0, 0, "Specify the BackGroundPic");
1428                 uiDefButF(block, NUMSLI, B_BLENDBGPIC, "Blend:",        120,120,190,19,&vd->bgpic->blend, 0.0,1.0, 0, 0, "Set the BackGroundPic transparency");
1429                 
1430                 uiDefButF(block, NUM, B_DIFF, "Center X: ",     10,100,140,19,&vd->bgpic->xof, -20.0,20.0, 10, 2, "Set the BackGroundPic X Offset");
1431                 uiDefButF(block, NUM, B_DIFF, "Center Y: ",     160,100,140,19,&vd->bgpic->yof, -20.0,20.0, 10, 2, "Set the BackGroundPic Y Offset");
1432
1433                 /* There is a bug here ... (what bug? where? what is this? - zr) */
1434                 /* (ton) the use of G.buts->texnr is hackish */
1435                 /* texture block: */
1436                 id= (ID *)vd->bgpic->tex;
1437                 IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->tex), id, &(G.buts->texnr));
1438                 if (strp[0]) 
1439                         uiDefButS(block, MENU, B_BGPICTEX, strp,                10, 80, 20,19, &(G.buts->texnr), 0, 0, 0, 0, "Select texture for animated backgroundimage");
1440                 MEM_freeN(strp);
1441                 
1442                 if (id) {
1443                         uiDefBut(block, TEX, B_IDNAME, "TE:",                   30,80,260,19, id->name+2, 0.0, 18.0, 0, 0, "");
1444                         uiDefIconBut(block, BUT, B_BGPICTEXCLEAR, ICON_X, 290,80,20,19, 0, 0, 0, 0, 0, "Remove background texture link");
1445                 }
1446         }
1447 }
1448
1449
1450 static void view3d_panel_properties(cntrl)      // VIEW3D_HANDLER_SETTINGS
1451 {
1452         uiBlock *block;
1453         View3D *vd;
1454         
1455         vd= G.vd;
1456
1457         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_properties", UI_EMBOSS, UI_HELV, curarea->win);
1458         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
1459         uiSetPanelHandler(VIEW3D_HANDLER_PROPERTIES);  // for close and esc
1460         if(uiNewPanel(curarea, block, "3D Viewport properties", "View3d", 10, 10, 318, 204)==0) return;
1461
1462
1463         uiDefButF(block, NUM, REDRAWVIEW3D, "Grid:",                    10, 50, 140, 19, &vd->grid, 0.001, 100.0, 10, 0, "Set the distance between gridlines");
1464         uiDefButS(block, NUM, REDRAWVIEW3D, "GridLines:",               160, 50, 140, 19, &vd->gridlines, 0.0, 100.0, 100, 0, "Set the number of gridlines");
1465         uiDefButF(block, NUM, REDRAWVIEW3D, "Lens:",                    10, 30, 140, 19, &vd->lens, 10.0, 120.0, 100, 0, "Set the lens for the perspective view");
1466         
1467         uiDefButF(block, NUM, REDRAWVIEW3D, "ClipStart:",               10, 10, 140, 19, &vd->near, vd->grid/10.0, 100.0, 10, 0, "Set startvalue in perspective view mode");
1468         uiDefButF(block, NUM, REDRAWVIEW3D, "ClipEnd:",                 160, 10, 140, 19, &vd->far, 1.0, 1000.0*vd->grid, 100, 0, "Set endvalue in perspective view mode");
1469
1470
1471 }
1472
1473
1474
1475 static void view3d_blockhandlers(ScrArea *sa)
1476 {
1477         View3D *v3d= sa->spacedata.first;
1478         short a;
1479         
1480         uiFreeBlocksWin(&sa->uiblocks, sa->win);
1481
1482         for(a=0; a<SPACE_MAXHANDLER; a+=2) {
1483         
1484                 switch(v3d->blockhandler[a]) {
1485
1486                 case VIEW3D_HANDLER_PROPERTIES:
1487                         view3d_panel_properties(v3d->blockhandler[a+1]);
1488                         break;
1489                 case VIEW3D_HANDLER_BACKGROUND:
1490                         view3d_panel_background(v3d->blockhandler[a+1]);
1491                         break;
1492                 case VIEW3D_HANDLER_OBJECT:
1493                         view3d_panel_object(v3d->blockhandler[a+1]);
1494                 
1495                         break;
1496                 
1497                 }
1498                 /* clear action value for event */
1499                 v3d->blockhandler[a+1]= 0;
1500         }
1501         uiDrawBlocksPanels(sa, 0);
1502
1503 }
1504
1505 void drawview3dspace(ScrArea *sa, void *spacedata)
1506 {
1507         extern void constline_callback(void);   // editobject.c helpline
1508         Base *base;
1509         Object *ob;
1510         
1511         setwinmatrixview3d(0);  /* 0= no pick rect */
1512         setviewmatrixview3d();
1513
1514         Mat4MulMat4(G.vd->persmat, G.vd->viewmat, curarea->winmat);
1515         Mat4Invert(G.vd->persinv, G.vd->persmat);
1516         Mat4Invert(G.vd->viewinv, G.vd->viewmat);
1517
1518         if(G.vd->drawtype > OB_WIRE) {
1519                 G.zbuf= TRUE;
1520                 glEnable(GL_DEPTH_TEST);
1521                 if(G.f & G_SIMULATION) {
1522                         glClearColor(0.0, 0.0, 0.0, 0.0); 
1523                 }
1524                 else {
1525                         float col[3];
1526                         BIF_GetThemeColor3fv(TH_BACK, col);
1527                         glClearColor(col[0], col[1], col[2], 0.0); 
1528                 }
1529                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1530                 
1531                 glLoadIdentity();
1532         }
1533         else {
1534                 float col[3];
1535                 BIF_GetThemeColor3fv(TH_BACK, col);
1536                 glClearColor(col[0], col[1], col[2], 0.0);
1537                 glClear(GL_COLOR_BUFFER_BIT);
1538         }
1539         
1540         myloadmatrix(G.vd->viewmat);
1541         persp(PERSP_STORE);  // store correct view for persp(PERSP_VIEW) calls
1542
1543         if(G.vd->view==0 || G.vd->persp!=0) {
1544                 drawfloor();
1545                 if(G.vd->persp==2) {
1546                         if(G.scene->world) {
1547                                 if(G.scene->world->mode & WO_STARS) RE_make_stars(star_stuff_init_func,
1548                                                                                                                                   star_stuff_vertex_func,
1549                                                                                                                                   star_stuff_term_func);
1550                         }
1551                         if(G.vd->flag & V3D_DISPBGPIC) draw_bgpic();
1552                 }
1553         }
1554         else {
1555                 drawgrid();
1556
1557                 if(G.vd->flag & V3D_DISPBGPIC) {
1558                         draw_bgpic();
1559                 }
1560         }
1561         
1562         /* Clear the constraint "done" flags */
1563         for (base = G.scene->base.first; base; base=base->next){
1564                 clear_object_constraint_status(base->object);
1565         }
1566
1567         /* draw set first */
1568         if(G.scene->set) {
1569         
1570                 /* patch: color remains constant */ 
1571                 G.f |= G_PICKSEL;
1572
1573                 base= G.scene->set->base.first;
1574                 while(base) {
1575                         if(G.vd->lay & base->lay) {
1576                                 where_is_object(base->object);
1577
1578                                 cpack(0x404040);
1579                                 draw_object(base);
1580
1581                                 if(base->object->transflag & OB_DUPLI) {
1582                                         extern ListBase duplilist;
1583                                         Base tbase;
1584                                         
1585                                         tbase= *base;
1586                                         
1587                                         tbase.flag= OB_FROMDUPLI;
1588                                         make_duplilist(G.scene->set, base->object);
1589                                         ob= duplilist.first;
1590                                         while(ob) {
1591                                                 tbase.object= ob;
1592                                                 draw_object(&tbase);
1593                                                 ob= ob->id.next;
1594                                         }
1595                                         free_duplilist();
1596                                         
1597                                 }
1598                         }
1599                         base= base->next;
1600                 }
1601                 
1602                 G.f &= ~G_PICKSEL;
1603         }
1604         
1605         /* first draw not selected and the duplis */
1606         base= G.scene->base.first;
1607         while(base) {
1608                 
1609                 if(G.vd->lay & base->lay) {
1610                         
1611                         where_is_object(base->object);
1612
1613                         /* dupli drawing temporal off here */
1614                         if(FALSE && base->object->transflag & OB_DUPLI) {
1615                                 extern ListBase duplilist;
1616                                 Base tbase;
1617
1618                                 /* draw original always first because of make_displist */
1619                                 draw_object(base);
1620
1621                                 /* patch: color remains constant */ 
1622                                 G.f |= G_PICKSEL;
1623                                 cpack(0x404040);
1624                                 
1625                                 tbase.flag= OB_FROMDUPLI;
1626                                 make_duplilist(G.scene, base->object);
1627
1628                                 ob= duplilist.first;
1629                                 while(ob) {
1630                                         tbase.object= ob;
1631                                         draw_object(&tbase);
1632                                         ob= ob->id.next;
1633                                 }
1634                                 free_duplilist();
1635                                 
1636                                 G.f &= ~G_PICKSEL;                              
1637                         }
1638                         else if((base->flag & SELECT)==0) {
1639                                 draw_object(base);
1640                         }
1641                         
1642                 }
1643                 
1644                 base= base->next;
1645         }
1646         /* draw selected */
1647         base= G.scene->base.first;
1648         while(base) {
1649                 
1650                 if ( ((base)->flag & SELECT) && ((base)->lay & G.vd->lay) ) {
1651                         draw_object(base);
1652                 }
1653                 
1654                 base= base->next;
1655         }
1656
1657         if(G.moving) constline_callback();
1658
1659         /* duplis, draw as last to make sure the displists are ok */
1660         base= G.scene->base.first;
1661         while(base) {
1662                 
1663                 if(G.vd->lay & base->lay) {
1664                         if(base->object->transflag & OB_DUPLI) {
1665                                 extern ListBase duplilist;
1666                                 Base tbase;
1667
1668                                 /* patch: color remains constant */ 
1669                                 G.f |= G_PICKSEL;
1670                                 cpack(0x404040);
1671                                 
1672                                 tbase.flag= OB_FROMDUPLI;
1673                                 make_duplilist(G.scene, base->object);
1674
1675                                 ob= duplilist.first;
1676                                 while(ob) {
1677                                         tbase.object= ob;
1678                                         draw_object(&tbase);
1679                                         ob= ob->id.next;
1680                                 }
1681                                 free_duplilist();
1682                                 
1683                                 G.f &= ~G_PICKSEL;                              
1684                         }
1685                 }
1686                 base= base->next;
1687         }
1688
1689
1690         if(G.scene->radio) RAD_drawall(G.vd->drawtype>=OB_SOLID);
1691         
1692         if(G.zbuf) {
1693                 G.zbuf= FALSE;
1694                 glDisable(GL_DEPTH_TEST);
1695         }
1696
1697         persp(PERSP_WIN);  // set ortho
1698         
1699         if(G.vd->persp>1) drawviewborder();
1700         drawcursor();
1701         draw_view_icon();
1702
1703         ob= OBACT;
1704         if(ob!=0 && (U.uiflag & DRAWVIEWINFO)) draw_selected_name(ob->id.name+2);
1705         
1706         draw_area_emboss(sa);
1707         
1708         /* it is important to end a view in a transform compatible with buttons */
1709
1710         bwin_scalematrix(sa->win, G.vd->blockscale, G.vd->blockscale, G.vd->blockscale);
1711         view3d_blockhandlers(sa);
1712
1713         curarea->win_swap= WIN_BACK_OK;
1714         
1715         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
1716                 G.vd->flag |= V3D_NEEDBACKBUFDRAW;
1717                 addafterqueue(curarea->win, BACKBUFDRAW, 1);
1718         }
1719         
1720 }
1721
1722
1723         /* Called back by rendering system, icky
1724          */
1725 void drawview3d_render(struct View3D *v3d)
1726 {
1727         extern void mywindow_build_and_set_renderwin(void);
1728         extern short v3d_windowmode;
1729         Base *base;
1730         Object *ob;
1731
1732         free_all_realtime_images();
1733         mywindow_build_and_set_renderwin();
1734                 
1735         v3d_windowmode= 1;
1736         setwinmatrixview3d(0);
1737         v3d_windowmode= 0;
1738         glMatrixMode(GL_PROJECTION);
1739         glLoadMatrixf(R.winmat);
1740         glMatrixMode(GL_MODELVIEW);
1741         
1742         setviewmatrixview3d();
1743         glLoadMatrixf(v3d->viewmat);
1744
1745         Mat4MulMat4(v3d->persmat, v3d->viewmat, R.winmat);
1746         Mat4Invert(v3d->persinv, v3d->persmat);
1747         Mat4Invert(v3d->viewinv, v3d->viewmat);
1748
1749         if(v3d->drawtype > OB_WIRE) {
1750                 G.zbuf= TRUE;
1751                 glEnable(GL_DEPTH_TEST);
1752         }
1753
1754         if (v3d->drawtype==OB_TEXTURE && G.scene->world) {
1755                 glClearColor(G.scene->world->horr, G.scene->world->horg, G.scene->world->horb, 0.0); 
1756         } else {
1757                 float col[3];
1758                 BIF_GetThemeColor3fv(TH_BACK, col);
1759                 glClearColor(col[0], col[1], col[2], 0.0); 
1760         }
1761         
1762         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1763         
1764         glLoadIdentity();
1765         glLoadMatrixf(v3d->viewmat);
1766         
1767         /* abuse! to make sure it doesnt draw the helpstuff */
1768         G.f |= G_SIMULATION;
1769
1770         do_all_ipos();
1771         BPY_do_all_scripts(SCRIPT_FRAMECHANGED);
1772         do_all_keys();
1773         do_all_actions();
1774         do_all_ikas();
1775
1776         test_all_displists();
1777
1778         /* not really nice forcing of calc_ipo and where_is */
1779         ob= G.main->object.first;
1780         while(ob) {
1781                 ob->ctime= -123.456;
1782                 ob= ob->id.next;
1783         }
1784
1785         /* first deaw set */
1786         if(G.scene->set) {
1787         
1788                 /* patch: color remains constant */ 
1789                 G.f |= G_PICKSEL;
1790                 
1791                 base= G.scene->set->base.first;
1792                 while(base) {
1793                         if(v3d->lay & base->lay) {
1794                                 if ELEM3(base->object->type, OB_LAMP, OB_CAMERA, OB_LATTICE);
1795                                 else {
1796                                         where_is_object(base->object);
1797         
1798                                         cpack(0x404040);
1799                                         draw_object(base);
1800         
1801                                         if(base->object->transflag & OB_DUPLI) {
1802                                                 extern ListBase duplilist;
1803                                                 Base tbase;
1804                                                 
1805                                                 tbase.flag= OB_FROMDUPLI;
1806                                                 make_duplilist(G.scene->set, base->object);
1807                                                 ob= duplilist.first;
1808                                                 while(ob) {
1809                                                         tbase.object= ob;
1810                                                         draw_object(&tbase);
1811                                                         ob= ob->id.next;
1812                                                 }
1813                                                 free_duplilist();
1814                                         }
1815                                 }
1816                         }
1817                         base= base->next;
1818                 }
1819                 
1820                 G.f &= ~G_PICKSEL;
1821         }
1822         for (base = G.scene->base.first; base; base=base->next){
1823                 clear_object_constraint_status(base->object);
1824         }
1825         /* first not selected and duplis */
1826         base= G.scene->base.first;
1827         while(base) {
1828                 
1829                 if(v3d->lay & base->lay) {
1830                         if ELEM3(base->object->type, OB_LAMP, OB_CAMERA, OB_LATTICE);
1831                         else {
1832                                 where_is_object(base->object);
1833         
1834                                 if(base->object->transflag & OB_DUPLI) {
1835                                         extern ListBase duplilist;
1836                                         Base tbase;
1837                                         
1838                                         /* always draw original first because of make_displist */
1839                                         draw_object(base);
1840                                         
1841                                         /* patch: color remains constant */ 
1842                                         G.f |= G_PICKSEL;
1843                                         cpack(0x404040);
1844                                         
1845                                         tbase.flag= OB_FROMDUPLI;
1846                                         make_duplilist(G.scene, base->object);
1847                                         ob= duplilist.first;
1848                                         while(ob) {
1849                                                 tbase.object= ob;
1850                                                 draw_object(&tbase);
1851                                                 ob= ob->id.next;
1852                                         }
1853                                         free_duplilist();
1854                                         
1855                                         G.f &= ~G_PICKSEL;                              
1856                                 }
1857                                 else if((base->flag & SELECT)==0) {
1858                                         draw_object(base);
1859                                 }
1860                         }
1861                 }
1862                 
1863                 base= base->next;
1864         }
1865
1866         /* draw selected */
1867         base= G.scene->base.first;
1868         while(base) {
1869                 
1870                 if ( ((base)->flag & SELECT) && ((base)->lay & v3d->lay) ) {
1871                         if ELEM3(base->object->type, OB_LAMP, OB_CAMERA, OB_LATTICE);
1872                         else draw_object(base);
1873                 }
1874                 
1875                 base= base->next;
1876         }
1877
1878         if(G.scene->radio) RAD_drawall(G.vd->drawtype>=OB_SOLID);
1879         
1880         if(G.zbuf) {
1881                 G.zbuf= FALSE;
1882                 glDisable(GL_DEPTH_TEST);
1883         }
1884         
1885         G.f &= ~G_SIMULATION;
1886
1887         glFinish();
1888
1889         glReadPixels(0, 0, R.rectx, R.recty, GL_RGBA, GL_UNSIGNED_BYTE, R.rectot);
1890         glLoadIdentity();
1891
1892         free_all_realtime_images();
1893 }
1894
1895
1896 double tottime = 0.0;
1897
1898 int update_time(void)
1899 {
1900         static double ltime;
1901         double time;
1902
1903         if ((U.mixbufsize)&&(audiostream_pos() != CFRA)&&(G.scene->audio.flag & AUDIO_SYNC)) return 0;
1904
1905         time = PIL_check_seconds_timer();
1906         
1907         tottime += (time - ltime);
1908         ltime = time;
1909         return (tottime < 0.0);
1910 }
1911
1912 double speed_to_swaptime(int speed)
1913 {
1914         switch(speed) {
1915         case 1:
1916                 return 1.0/60.0;
1917         case 2:
1918                 return 1.0/50.0;
1919         case 3:
1920                 return 1.0/30.0;
1921         case 4:
1922                 return 1.0/25.0;
1923         case 5:
1924                 return 1.0/20.0;
1925         case 6:
1926                 return 1.0/15.0;
1927         case 7:
1928                 return 1.0/12.5;
1929         case 8:
1930                 return 1.0/10.0;
1931         case 9:
1932                 return 1.0/6.0;
1933         }
1934         return 1.0/4.0;
1935 }
1936
1937 double key_to_swaptime(int key)
1938 {
1939         switch(key) {
1940         case PAD1:
1941                 G.animspeed= 1;
1942                 tottime= 0;
1943                 return speed_to_swaptime(1);
1944         case PAD2:
1945                 G.animspeed= 2;
1946                 tottime= 0;
1947                 return speed_to_swaptime(2);
1948         case PAD3:
1949                 G.animspeed= 3;
1950                 tottime= 0;
1951                 return speed_to_swaptime(3);
1952         case PAD4:
1953                 G.animspeed= 4;
1954                 tottime= 0;
1955                 return speed_to_swaptime(4);
1956         case PAD5:
1957                 G.animspeed= 5;
1958                 tottime= 0;
1959                 return speed_to_swaptime(5);
1960         case PAD6:
1961                 G.animspeed= 6;
1962                 tottime= 0;
1963                 return speed_to_swaptime(6);
1964         case PAD7:
1965                 G.animspeed= 7;
1966                 tottime= 0;
1967                 return speed_to_swaptime(7);
1968         case PAD8:
1969                 G.animspeed= 8;
1970                 tottime= 0;
1971                 return speed_to_swaptime(8);
1972         case PAD9:
1973                 G.animspeed= 9;
1974                 tottime= 0;
1975                 return speed_to_swaptime(9);
1976         }
1977         
1978         return speed_to_swaptime(G.animspeed);
1979 }
1980
1981 #ifdef NAN_LINEAR_PHYSICS
1982
1983 void sumo_callback(void *obp)
1984 {
1985         Object *ob= obp;
1986         SM_Vector3 vec;
1987         float matf[3][3];
1988         int i, j;
1989
1990     SM_GetMatrixf(ob->sumohandle, ob->obmat[0]);
1991
1992         VECCOPY(ob->loc, ob->obmat[3]);
1993         
1994     for (i = 0; i < 3; ++i) {
1995         for (j = 0; j < 3; ++j) {
1996             matf[i][j] = ob->obmat[i][j];
1997         }
1998     }
1999     Mat3ToEul(matf, ob->rot);
2000 }
2001
2002 /* for test and fun, i've written the next functions to play with dynamics
2003    using a variant of play-anim... was never released nor really tested (ton) */
2004
2005 void init_anim_sumo(void)
2006 {
2007         extern Material defmaterial;
2008         Base *base;
2009     Mesh *me;
2010         Object *ob;
2011     Material *mat;
2012         MFace *mface;
2013         MVert *mvert;
2014     float centre[3], size[3];
2015         int a;
2016     SM_ShapeHandle shape;
2017         SM_SceneHandle scene;
2018     SM_Material material;
2019     SM_MassProps massprops;
2020     SM_Vector3 vec;
2021     SM_Vector3 scaling;
2022         
2023         scene= SM_CreateScene();
2024         G.scene->sumohandle = scene;
2025         
2026         vec[0]=  0.0;
2027         vec[1]=  0.0;
2028         vec[2]= -9.8;
2029         SM_SetForceField(scene, vec);
2030         
2031     /* ton: cylinders & cones are still Y-axis up, will be Z-axis later */
2032     /* ton: write location/rotation save and restore */
2033         
2034         base= FIRSTBASE;
2035         while (base) {
2036                 if (G.vd->lay & base->lay) {
2037             ob= base->object;
2038                          
2039             /* define shape, for now only meshes take part in physics */
2040             get_local_bounds(ob, centre, size);
2041             
2042             if (ob->type==OB_MESH) {
2043                 me= ob->data;
2044                 
2045                 if (ob->gameflag & OB_DYNAMIC) {
2046                     if (me->sumohandle)
2047                         shape= me->sumohandle;
2048                     else {
2049                         /* make new handle */
2050                         switch(ob->boundtype) {
2051                         case OB_BOUND_BOX:
2052                             shape= SM_Box(2.0*size[0], 2.0*size[1], 2.0*size[2]);
2053                             break;
2054                         case OB_BOUND_SPHERE:
2055                             shape= SM_Sphere(size[0]);
2056                             break;
2057                         case OB_BOUND_CYLINDER:
2058                             shape= SM_Cylinder(size[0], 2.0*size[2]);
2059                             break;
2060                         case OB_BOUND_CONE:
2061                             shape= SM_Cone(size[0], 2.0*size[2]);
2062                             break;
2063                         }
2064                         
2065                                                 me->sumohandle= shape;
2066                                         }
2067                     /* sumo material properties */
2068                         mat= give_current_material(ob, 0);
2069                         if(mat==NULL)
2070                         mat= &defmaterial;
2071                     
2072                         material.restitution= mat->reflect;
2073                         material.static_friction= mat->friction;
2074                         material.dynamic_friction= mat->friction;
2075                     
2076                         /* sumo mass properties */
2077                         massprops.mass= ob->mass;
2078                         massprops.center[0]= 0.0;
2079                         massprops.center[1]= 0.0;
2080                         massprops.center[2]= 0.0;
2081
2082                         massprops.inertia[0]= 0.5*ob->mass;
2083                         massprops.inertia[1]= 0.5*ob->mass;
2084                         massprops.inertia[2]= 0.5*ob->mass;
2085
2086                         massprops.orientation[0]= 0.0;
2087                         massprops.orientation[1]= 0.0;
2088                         massprops.orientation[2]= 0.0;
2089                         massprops.orientation[3]= 1.0;
2090
2091                         ob->sumohandle = SM_CreateObject(ob, shape, &material, 
2092                                                      &massprops, sumo_callback);
2093                                         SM_AddObject(scene, ob->sumohandle);
2094                                         
2095                     scaling[0] = ob->size[0];
2096                     scaling[1] = ob->size[1];
2097                     scaling[2] = ob->size[2];
2098                                         SM_SetMatrixf(ob->sumohandle, ob->obmat[0]);
2099                                         SM_SetScaling(ob->sumohandle, scaling);
2100
2101                                 }
2102                                 else {
2103                                         if(me->sumohandle) shape= me->sumohandle;
2104                                         else {
2105                                                 /* make new handle */
2106                                 shape= SM_NewComplexShape();
2107                                                 
2108                                                 mface= me->mface;
2109                                                 mvert= me->mvert;
2110                                                 for(a=0; a<me->totface; a++,mface++) {
2111                                                         if(mface->v3) {
2112                                                                 SM_Begin();
2113                                                                 SM_Vertex( (mvert+mface->v1)->co[0], (mvert+mface->v1)->co[1], (mvert+mface->v1)->co[2]);
2114                                                                 SM_Vertex( (mvert+mface->v2)->co[0], (mvert+mface->v2)->co[1], (mvert+mface->v2)->co[2]);
2115                                                                 SM_Vertex( (mvert+mface->v3)->co[0], (mvert+mface->v3)->co[1], (mvert+mface->v3)->co[2]);
2116                                                                 if(mface->v4)
2117                                                                         SM_Vertex( (mvert+mface->v4)->co[0], (mvert+mface->v4)->co[1], (mvert+mface->v4)->co[2]);
2118                                                                 SM_End();
2119                                                         }
2120                                                 }
2121                                                 
2122                                                 SM_EndComplexShape();
2123                                                 
2124                                                 me->sumohandle= shape;
2125                                         }
2126                     /* sumo material properties */
2127                         mat= give_current_material(ob, 0);
2128                         if(mat==NULL)
2129                         mat= &defmaterial;
2130                         material.restitution= mat->reflect;
2131                         material.static_friction= mat->friction;
2132                         material.dynamic_friction= mat->friction;
2133
2134                         /* sumo mass properties */
2135                         massprops.mass= ob->mass;
2136                         massprops.center[0]= 0.0;
2137                         massprops.center[1]= 0.0;
2138                         massprops.center[2]= 0.0;
2139
2140                         massprops.inertia[0]= 0.5*ob->mass;
2141                         massprops.inertia[1]= 0.5*ob->mass;
2142                         massprops.inertia[2]= 0.5*ob->mass;
2143
2144                         massprops.orientation[0]= 0.0;
2145                         massprops.orientation[1]= 0.0;
2146                         massprops.orientation[2]= 0.0;
2147                         massprops.orientation[3]= 1.0;
2148
2149                         ob->sumohandle= SM_CreateObject(ob, shape, &material, NULL, NULL);
2150                                         SM_AddObject(scene, ob->sumohandle);
2151
2152                     scaling[0] = ob->size[0];
2153                     scaling[1] = ob->size[1];
2154                     scaling[2] = ob->size[2];
2155                                         SM_SetMatrixf(ob->sumohandle, ob->obmat[0]);
2156                                         SM_SetScaling(ob->sumohandle, scaling);
2157                                 }
2158             }
2159         }       
2160         base= base->next;
2161     }
2162 }
2163
2164 /* update animated objects */
2165 void update_anim_sumo(void)
2166 {
2167     SM_Vector3 scaling;
2168
2169         Base *base;
2170         Object *ob;
2171         Mesh *me;
2172         
2173         base= FIRSTBASE;
2174         while(base) {
2175                 if(G.vd->lay & base->lay) {
2176                         ob= base->object;
2177                         
2178                         if(ob->sumohandle) {
2179                                 if((ob->gameflag & OB_DYNAMIC)==0) {
2180                                         /* maybe: optimise, check for anim */
2181                     scaling[0] = ob->size[0];
2182                     scaling[1] = ob->size[1];
2183                     scaling[2] = ob->size[2];
2184                                         SM_SetMatrixf(ob->sumohandle, ob->obmat[0]);
2185                                         SM_SetScaling(ob->sumohandle, scaling);
2186                                 }
2187                         }                               
2188                 }
2189                 base= base->next;
2190         }
2191
2192 }
2193
2194 void end_anim_sumo(void)
2195 {
2196         Base *base;
2197         Object *ob;
2198         Mesh *me;
2199         
2200         base= FIRSTBASE;
2201         while(base) {
2202                 if(G.vd->lay & base->lay) {
2203                         ob= base->object;
2204                         
2205             if(ob->type==OB_MESH) {
2206                                 if(ob->sumohandle) {
2207                                         SM_RemoveObject(G.scene->sumohandle, ob->sumohandle);
2208                                         SM_DeleteObject(ob->sumohandle);
2209                                         ob->sumohandle= NULL;
2210                                 }
2211                                 me= ob->data;
2212                                 if(me->sumohandle) {
2213                                         SM_DeleteShape(me->sumohandle);
2214                                         me->sumohandle= NULL;
2215                                 }
2216                         }
2217                 }
2218                 base= base->next;
2219         }
2220         if(G.scene->sumohandle) {
2221                 SM_DeleteScene(G.scene->sumohandle);
2222                 G.scene->sumohandle= NULL;
2223         }
2224 }
2225
2226 #endif
2227
2228 void inner_play_anim_loop(int init, int mode)
2229 {
2230         ScrArea *sa;
2231         static ScrArea *oldsa;
2232         static double swaptime;
2233         static int curmode;
2234         
2235         /* init */
2236         if(init) {
2237                 oldsa= curarea;
2238                 swaptime= speed_to_swaptime(G.animspeed);
2239                 tottime= 0.0;
2240                 curmode= mode;
2241 #ifdef NAN_LINEAR_PHYSICS
2242         init_anim_sumo();
2243 #endif        
2244                 return;
2245         }
2246
2247         set_timecursor(CFRA);
2248         do_all_ipos();
2249         BPY_do_all_scripts(SCRIPT_FRAMECHANGED);
2250         do_all_keys();
2251         do_all_actions();
2252         do_all_ikas();
2253
2254
2255         test_all_displists();
2256 #ifdef NAN_LINEAR_PHYSICS       
2257         update_anim_sumo();
2258         
2259         SM_Proceed(G.scene->sumohandle, swaptime, 40, NULL);
2260 #endif
2261         sa= G.curscreen->areabase.first;
2262         while(sa) {
2263                 if(sa==oldsa) {
2264                         scrarea_do_windraw(sa);
2265                 }
2266                 else if(curmode) {
2267                         if ELEM(sa->spacetype, SPACE_VIEW3D, SPACE_SEQ) {
2268                                 scrarea_do_windraw(sa);
2269                         }
2270                 }
2271                 
2272                 sa= sa->next;   
2273         }
2274         
2275         /* make sure that swaptime passed by */
2276         tottime -= swaptime;
2277         while (update_time()) PIL_sleep_ms(1);
2278
2279         if(CFRA>=EFRA) {
2280                 if (tottime > 0.0) tottime = 0.0;
2281                 CFRA= SFRA;
2282                 audiostream_stop();
2283                 audiostream_start( CFRA );
2284         }
2285         else {
2286                 if (U.mixbufsize && (G.scene->audio.flag & AUDIO_SYNC)) CFRA = audiostream_pos();
2287                 else CFRA++;
2288         }
2289 }
2290
2291 int play_anim(int mode)
2292 {
2293         ScrArea *sa, *oldsa;
2294         int cfraont;
2295         unsigned short event=0;
2296         short val;
2297         
2298         /* patch for very very old scenes */
2299         if(SFRA==0) SFRA= 1;
2300         if(EFRA==0) EFRA= 250;
2301         
2302         if(SFRA>EFRA) return 0;
2303         
2304         update_time();
2305
2306         /* waitcursor(1); */
2307         G.f |= G_PLAYANIM;              /* in sequence.c and view.c this is handled */
2308
2309         cfraont= CFRA;
2310         oldsa= curarea;
2311
2312         audiostream_start( CFRA );
2313         
2314         inner_play_anim_loop(1, mode);  /* 1==init */
2315
2316         while(TRUE) {
2317
2318                 inner_play_anim_loop(0, 0);
2319         
2320                 screen_swapbuffers();
2321                 
2322                 while(qtest()) {
2323                 
2324                         event= extern_qread(&val);
2325                         if(event==ESCKEY) break;
2326                         else if(event==MIDDLEMOUSE) {
2327                                 if(U.flag & VIEWMOVE) {
2328                                         if(G.qual & LR_SHIFTKEY) viewmove(0);
2329                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
2330                                         else viewmove(1);
2331                                 }
2332                                 else {
2333                                         if(G.qual & LR_SHIFTKEY) viewmove(1);
2334                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
2335                                         else viewmove(0);
2336                                 }
2337                         }
2338                 }
2339                 if(event==ESCKEY || event==SPACEKEY) break;
2340                                 
2341                 if(mode==2 && CFRA==EFRA) break;        
2342         }
2343
2344         if(event==SPACEKEY);
2345         else CFRA= cfraont;
2346         
2347         do_all_ipos();
2348         do_all_keys();
2349         do_all_actions();
2350         do_all_ikas();
2351
2352         audiostream_stop();
2353
2354         if(oldsa!=curarea) areawinset(oldsa->win);
2355         
2356         /* restore all areas */
2357         sa= G.curscreen->areabase.first;
2358         while(sa) {
2359                 if( (mode && sa->spacetype==SPACE_VIEW3D) || sa==curarea) addqueue(sa->win, REDRAW, 1);
2360                 
2361                 sa= sa->next;   
2362         }
2363         
2364         /* speed button */
2365         // allqueue(REDRAWBUTSOBJECT, 0);
2366         /* groups could have changed ipo */
2367         allspace(REMAKEIPO, 0);
2368         allqueue(REDRAWIPO, 0);
2369         allqueue(REDRAWNLA, 0);
2370         allqueue (REDRAWACTION, 0);
2371         /* for the time being */
2372         update_for_newframe_muted();
2373 #ifdef NAN_LINEAR_PHYSICS       
2374         end_anim_sumo();
2375 #endif
2376         waitcursor(0);
2377         G.f &= ~G_PLAYANIM;
2378         
2379         if (event==ESCKEY || event==SPACEKEY) return 1;
2380         else return 0;
2381 }