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