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