- replaced G.{edve,eded,edvl} with G.editMesh, atm just a structure to
[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         short draw_line = 0;
625                 
626         vd= curarea->spacedata.first;
627
628         vert[2]= 0.0;
629
630         if(vd->gridlines<3) return;
631
632         gridlines= vd->gridlines/2;
633         grid= gridlines*vd->grid;
634         
635         BIF_GetThemeColor3ubv(TH_GRID, col);
636         
637         /* draw the Y axis and/or grid lines */
638         for(a= -gridlines;a<=gridlines;a++) {
639                 if(a==0) {
640                         /* check for the 'show Y axis' preference */
641                         if (vd->gridflag & V3D_SHOW_Y) { 
642                                 make_axis_color(col, col2, 'y');
643                                 glColor3ubv(col2);
644                                 
645                                 draw_line = 1;
646                         } else if (vd->gridflag & V3D_SHOW_FLOOR) {
647                                 BIF_ThemeColorShade(TH_GRID, -10);
648                         } else {
649                                 draw_line = 0;
650                         }
651                 } else {
652                         /* check for the 'show grid floor' preference */
653                         if (vd->gridflag & V3D_SHOW_FLOOR) {
654                                 if( (a % 10)==0) {
655                                         BIF_ThemeColorShade(TH_GRID, -10);
656                                 }
657                                 else BIF_ThemeColorShade(TH_GRID, 10);
658                                 
659                                 draw_line = 1;
660                         } else {
661                                 draw_line = 0;
662                         }
663                 }
664                 
665                 if (draw_line) {
666                         glBegin(GL_LINE_STRIP);
667                 vert[0]= a*vd->grid;
668                 vert[1]= grid;
669                 glVertex3fv(vert);
670                 vert[1]= -grid;
671                 glVertex3fv(vert);
672                         glEnd();
673                 }
674         }
675         
676         /* draw the X axis and/or grid lines */
677         for(a= -gridlines;a<=gridlines;a++) {
678                 if(a==0) {
679                         /* check for the 'show X axis' preference */
680                         if (vd->gridflag & V3D_SHOW_X) { 
681                                 make_axis_color(col, col2, 'x');
682                                 glColor3ubv(col2);
683                                 
684                                 draw_line = 1;
685                         } else if (vd->gridflag & V3D_SHOW_FLOOR) {
686                                 BIF_ThemeColorShade(TH_GRID, -10);
687                         } else {
688                                 draw_line = 0;
689                         }
690                 } else {
691                         /* check for the 'show grid floor' preference */
692                         if (vd->gridflag & V3D_SHOW_FLOOR) {
693                                 if( (a % 10)==0) {
694                                         BIF_ThemeColorShade(TH_GRID, -10);
695                                 }
696                                 else BIF_ThemeColorShade(TH_GRID, 10);
697                                 
698                                 draw_line = 1;
699                         } else {
700                                 draw_line = 0;
701                         }
702                 }
703                 
704                 if (draw_line) {
705                         glBegin(GL_LINE_STRIP);
706                 vert[1]= a*vd->grid;
707                 vert[0]= grid;
708                 glVertex3fv(vert );
709                 vert[0]= -grid;
710                 glVertex3fv(vert);
711                         glEnd();
712                 }
713         }
714         
715         /* draw the Z axis line */      
716         /* check for the 'show Z axis' preference */
717         if (vd->gridflag & V3D_SHOW_Z) {
718                 make_axis_color(col, col2, 'z');
719                 glColor3ubv(col2);
720                 
721                 glBegin(GL_LINE_STRIP);
722                 vert[0]= 0;
723                 vert[1]= 0;
724                 vert[2]= grid;
725                 glVertex3fv(vert );
726                 vert[2]= -grid;
727                 glVertex3fv(vert);
728                 glEnd();
729         }
730
731 }
732
733 static void drawcursor(void)
734 {
735
736         if(G.f & G_PLAYANIM) return;
737         
738         project_short( give_cursor(), &G.vd->mx);
739
740         G.vd->mxo= G.vd->mx;
741         G.vd->myo= G.vd->my;
742
743         if( G.vd->mx!=3200) {
744                 
745                 setlinestyle(0); 
746                 cpack(0xFF);
747                 circ((float)G.vd->mx, (float)G.vd->my, 10.0);
748                 setlinestyle(4); 
749                 cpack(0xFFFFFF);
750                 circ((float)G.vd->mx, (float)G.vd->my, 10.0);
751                 setlinestyle(0);
752                 cpack(0x0);
753                 
754                 sdrawline(G.vd->mx-20, G.vd->my, G.vd->mx-5, G.vd->my);
755                 sdrawline(G.vd->mx+5, G.vd->my, G.vd->mx+20, G.vd->my);
756                 sdrawline(G.vd->mx, G.vd->my-20, G.vd->mx, G.vd->my-5);
757                 sdrawline(G.vd->mx, G.vd->my+5, G.vd->mx, G.vd->my+20);
758         }
759 }
760
761 static void view3d_get_viewborder_size(View3D *v3d, float size_r[2])
762 {
763         float winmax= MAX2(v3d->area->winx, v3d->area->winy);
764         float aspect= (float) (G.scene->r.xsch*G.scene->r.xasp)/(G.scene->r.ysch*G.scene->r.yasp);
765
766         if(aspect>1.0) {
767                 size_r[0]= winmax;
768                 size_r[1]= winmax/aspect;
769         } else {
770                 size_r[0]= winmax*aspect;
771                 size_r[1]= winmax;
772         }
773 }
774
775 void calc_viewborder(struct View3D *v3d, rcti *viewborder_r)
776 {
777         float zoomfac, size[2];
778
779         view3d_get_viewborder_size(v3d, size);
780
781                 /* magic zoom calculation, no idea what
782              * it signifies, if you find out, tell me! -zr
783                  */
784         /* simple, its magic dude!
785          * well, to be honest, this gives a natural feeling zooming
786          * with multiple keypad presses (ton)
787          */
788         
789         zoomfac= (M_SQRT2 + v3d->camzoom/50.0);
790         zoomfac= (zoomfac*zoomfac)*0.25;
791         
792         size[0]= size[0]*zoomfac;
793         size[1]= size[1]*zoomfac;
794
795                 /* center in window */
796         viewborder_r->xmin= 0.5*v3d->area->winx - 0.5*size[0];
797         viewborder_r->ymin= 0.5*v3d->area->winy - 0.5*size[1];
798         viewborder_r->xmax= viewborder_r->xmin + size[0];
799         viewborder_r->ymax= viewborder_r->ymin + size[1];
800 }
801
802 void view3d_set_1_to_1_viewborder(View3D *v3d)
803 {
804         float size[2];
805         int im_width= (G.scene->r.size*G.scene->r.xsch)/100;
806
807         view3d_get_viewborder_size(v3d, size);
808
809         v3d->camzoom= (sqrt(4.0*im_width/size[0]) - M_SQRT2)*50.0;
810         v3d->camzoom= CLAMPIS(v3d->camzoom, -30, 300);
811 }
812
813 static void drawviewborder(void)
814 {
815         float fac, a;
816         float x1, x2, y1, y2;
817         float x3, y3, x4, y4;
818         rcti viewborder;
819
820         calc_viewborder(G.vd, &viewborder);
821         x1= viewborder.xmin;
822         y1= viewborder.ymin;
823         x2= viewborder.xmax;
824         y2= viewborder.ymax;
825
826         /* edge */
827         setlinestyle(3);
828         cpack(0);
829         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
830         glRectf(x1+1,  y1-1,  x2+1,  y2-1); 
831         
832         cpack(0xFFFFFF);
833         glRectf(x1,  y1,  x2,  y2); 
834
835         /* border */
836         if(G.scene->r.mode & R_BORDER) {
837                 
838                 cpack(0);
839                 x3= x1+ G.scene->r.border.xmin*(x2-x1);
840                 y3= y1+ G.scene->r.border.ymin*(y2-y1);
841                 x4= x1+ G.scene->r.border.xmax*(x2-x1);
842                 y4= y1+ G.scene->r.border.ymax*(y2-y1);
843                 
844                 glRectf(x3+1,  y3-1,  x4+1,  y4-1); 
845                 
846                 cpack(0x4040FF);
847                 glRectf(x3,  y3,  x4,  y4); 
848         }
849
850         /* safety border */
851
852         fac= 0.1;
853         
854         a= fac*(x2-x1);
855         x1+= a; 
856         x2-= a;
857
858         a= fac*(y2-y1);
859         y1+= a;
860         y2-= a;
861
862         cpack(0);
863         glRectf(x1+1,  y1-1,  x2+1,  y2-1);
864         cpack(0xFFFFFF);
865         glRectf(x1,  y1,  x2,  y2);
866
867         setlinestyle(0);
868         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
869
870 }
871
872
873 void backdrawview3d(int test)
874 {
875         struct Base *base;
876         int tel=1;
877
878         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT));
879         else {
880                 G.vd->flag &= ~V3D_NEEDBACKBUFDRAW;
881                 return;
882         }
883
884         if(G.vd->flag & V3D_NEEDBACKBUFDRAW); else return;
885         if(G.obedit) {
886                 G.vd->flag &= ~V3D_NEEDBACKBUFDRAW;
887                 return;
888         }
889         
890         if(test) {
891                 if(qtest()) {
892                         addafterqueue(curarea->win, BACKBUFDRAW, 1);
893                         return;
894                 }
895         }
896         
897         persp(PERSP_VIEW);
898         
899 #ifdef __APPLE__
900         glDrawBuffer(GL_AUX0);
901 #endif  
902         if(G.vd->drawtype > OB_WIRE) G.zbuf= TRUE;
903         curarea->win_swap &= ~WIN_BACK_OK;
904         
905         glDisable(GL_DITHER);
906
907         glClearColor(0.0, 0.0, 0.0, 0.0); 
908         if(G.zbuf) {
909                 glEnable(GL_DEPTH_TEST);
910                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
911         }
912         else {
913                 glClear(GL_COLOR_BUFFER_BIT);
914                 glDisable(GL_DEPTH_TEST);
915         }
916         
917         G.f |= G_BACKBUFSEL;
918         
919         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
920                 base= (G.scene->basact);
921                 if(base && (base->lay & G.vd->lay)) {
922                         draw_object(base);
923                 }
924         }
925         else {
926
927                 base= (G.scene->base.first);
928                 while(base) {
929                         
930                         /* each base, because of multiple windows */
931                         base->selcol= 0x070707 | ( ((tel & 0xF00)<<12) + ((tel & 0xF0)<<8) + ((tel & 0xF)<<4) );
932                         tel++;
933         
934                         if(base->lay & G.vd->lay) {
935                                 
936                                 if(test) {
937                                         if(qtest()) {
938                                                 addafterqueue(curarea->win, BACKBUFDRAW, 1);
939                                                 break;
940                                         }
941                                 }
942                                 
943                                 cpack(base->selcol);
944                                 draw_object(base);
945                         }
946                         base= base->next;
947                 }
948         }
949         
950         if(base==0) G.vd->flag &= ~V3D_NEEDBACKBUFDRAW;
951
952         G.f &= ~G_BACKBUFSEL;
953         G.zbuf= FALSE; 
954         glDisable(GL_DEPTH_TEST);
955         glEnable(GL_DITHER);
956
957 #ifdef __APPLE__
958         glDrawBuffer(GL_BACK); /* we were in aux buffers */
959 #endif
960
961         /* it is important to end a view in a transform compatible with buttons */
962         persp(PERSP_WIN);  // set ortho
963         bwin_scalematrix(curarea->win, G.vd->blockscale, G.vd->blockscale, G.vd->blockscale);
964
965 }
966
967                 
968 void drawname(Object *ob)
969 {
970         cpack(0x404040);
971         glRasterPos3f(0.0,  0.0,  0.0);
972         
973         BMF_DrawString(G.font, " ");
974         BMF_DrawString(G.font, ob->id.name+2);
975 }
976
977
978 static void draw_selected_name(char *name)
979 {
980         char info[128];
981
982         sprintf(info, "(%d) %s", CFRA, name);
983
984         BIF_ThemeColor(TH_TEXT_HI);
985         glRasterPos2i(30,  10);
986         BMF_DrawString(G.fonts, info);
987 }
988
989
990 static void draw_view_icon(void)
991 {
992         BIFIconID icon;
993         
994         if(G.vd->view==7) icon= ICON_AXIS_TOP;
995         else if(G.vd->view==1) icon= ICON_AXIS_FRONT;
996         else if(G.vd->view==3) icon= ICON_AXIS_SIDE;
997         else return ;
998
999         glEnable(GL_BLEND);
1000         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
1001         
1002         glRasterPos2f(5.0, 5.0);
1003         BIF_draw_icon(icon);
1004         
1005         glBlendFunc(GL_ONE,  GL_ZERO); 
1006         glDisable(GL_BLEND);
1007 }
1008
1009 /* ******************* view3d space & buttons ************** */
1010
1011 static void view3d_change_bgpic_ima(View3D *v3d, Image *newima) {
1012         if (v3d->bgpic && v3d->bgpic->ima!=newima) {
1013                 if (newima)
1014                         id_us_plus((ID*) newima);
1015                 if (v3d->bgpic->ima)
1016                         v3d->bgpic->ima->id.us--;
1017                 v3d->bgpic->ima= newima;
1018
1019                 if(v3d->bgpic->rect) MEM_freeN(v3d->bgpic->rect);
1020                 v3d->bgpic->rect= NULL;
1021                 
1022                 allqueue(REDRAWVIEW3D, 0);
1023         }
1024 }
1025 static void view3d_change_bgpic_tex(View3D *v3d, Tex *newtex) {
1026         if (v3d->bgpic && v3d->bgpic->tex!=newtex) {
1027                 if (newtex)
1028                         id_us_plus((ID*) newtex);
1029                 if (v3d->bgpic->tex)
1030                         v3d->bgpic->tex->id.us--;
1031                 v3d->bgpic->tex= newtex;
1032                 
1033                 allqueue(REDRAWVIEW3D, 0);
1034         }
1035 }
1036
1037 static void load_bgpic_image(char *name)
1038 {
1039         Image *ima;
1040         View3D *vd;
1041         
1042         vd= G.vd;
1043         if(vd==0 || vd->bgpic==0) return;
1044         
1045         ima= add_image(name);
1046         if(ima) {
1047                 if(vd->bgpic->ima) {
1048                         vd->bgpic->ima->id.us--;
1049                 }
1050                 vd->bgpic->ima= ima;
1051                 
1052                 free_image_buffers(ima);        /* force read again */
1053                 ima->ok= 1;
1054         }
1055         allqueue(REDRAWVIEW3D, 0);
1056         
1057 }
1058
1059 /* this one assumes there is only one global active object in blender...  (for object panel) */
1060 static float ob_eul[4]; // used for quat too....
1061 /* this one assumes there is only one editmode in blender...  (for object panel) */
1062 static float ve_median[4];
1063 static int ve_median_tot=0;
1064
1065 /* is used for both read and write... */
1066 static void v3d_editvertex_buts(uiBlock *block, Object *ob, float lim)
1067 {
1068         EditMesh *em = G.editMesh;
1069         EditVert *eve;
1070         float median[4];
1071         int tot, totw;
1072         
1073         median[0]= median[1]= median[2]= median[3]= 0.0;
1074         tot= totw= 0;
1075         
1076         if(ob->type==OB_MESH) {
1077                 eve= em->verts.first;
1078                 while(eve) {
1079                         if(eve->f & 1) {
1080                                 tot++;
1081                                 VecAddf(median, median, eve->co);
1082                         }
1083                         eve= eve->next;
1084                 }
1085         }
1086         else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
1087                 extern ListBase editNurb; /* editcurve.c */
1088                 Nurb *nu;
1089                 BPoint *bp;
1090                 BezTriple *bezt;
1091                 int a;
1092                 
1093                 nu= editNurb.first;
1094                 while(nu) {
1095                         if((nu->type & 7)==1) {
1096                                 bezt= nu->bezt;
1097                                 a= nu->pntsu;
1098                                 while(a--) {
1099                                         if(bezt->f2 & 1) {
1100                                                 VecAddf(median, median, bezt->vec[1]);
1101                                                 tot++;
1102                                         }
1103                                         else {
1104                                                 if(bezt->f1 & 1) {
1105                                                         VecAddf(median, median, bezt->vec[0]);
1106                                                         tot++;
1107                                                 }
1108                                                 if(bezt->f3 & 1) {
1109                                                         VecAddf(median, median, bezt->vec[2]);
1110                                                         tot++;
1111                                                 }
1112                                         }
1113                                         bezt++;
1114                                 }
1115                         }
1116                         else {
1117                                 bp= nu->bp;
1118                                 a= nu->pntsu*nu->pntsv;
1119                                 while(a--) {
1120                                         if(bp->f1 & 1) {
1121                                                 VecAddf(median, median, bp->vec);
1122                                                 median[3]+= bp->vec[3];
1123                                                 totw++;
1124                                                 tot++;
1125                                         }
1126                                         bp++;
1127                                 }
1128                         }
1129                         nu= nu->next;
1130                 }
1131         
1132         }
1133         if(tot==0) return;
1134
1135         median[0] /= (float)tot;
1136         median[1] /= (float)tot;
1137         median[2] /= (float)tot;
1138         median[3] /= (float)tot;
1139         
1140         if(block) {     // buttons
1141         
1142                 ve_median_tot= tot;
1143                 QUATCOPY(ve_median, median);
1144                 
1145                 uiBlockBeginAlign(block);
1146                 if(ve_median_tot==1) {
1147                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex X:", 10, 140, 300, 19, &(ve_median[0]), -lim, lim, 10, 3, "");
1148                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex Y:", 10, 120, 300, 19, &(ve_median[1]), -lim, lim, 10, 3, "");
1149                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex Z:", 10, 100, 300, 19, &(ve_median[2]), -lim, lim, 10, 3, "");
1150                         if(totw==tot)
1151                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex W:", 10, 80, 300, 19, &(ve_median[3]), 0.01, 100.0, 10, 3, "");
1152                 }
1153                 else {
1154                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median X:", 10, 140, 300, 19, &(ve_median[0]), -lim, lim, 10, 3, "");
1155                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Y:", 10, 120, 300, 19, &(ve_median[1]), -lim, lim, 10, 3, "");
1156                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Z:", 10, 100, 300, 19, &(ve_median[2]), -lim, lim, 10, 3, "");
1157                         if(totw==tot)
1158                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median W:", 10, 80, 300, 19, &(ve_median[3]), 0.01, 100.0, 10, 3, "");
1159                 }
1160                 uiBlockEndAlign(block);
1161         }
1162         else {  // apply
1163                 
1164                 VecSubf(median, ve_median, median);
1165                 median[3]= ve_median[3]-median[3];
1166                 
1167                 if(ob->type==OB_MESH) {
1168                         eve= em->verts.first;
1169                         while(eve) {
1170                                 if(eve->f & 1) {
1171                                         VecAddf(eve->co, eve->co, median);
1172                                 }
1173                                 eve= eve->next;
1174                         }
1175                 }
1176                 else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
1177                         extern ListBase editNurb; /* editcurve.c */
1178                         Nurb *nu;
1179                         BPoint *bp;
1180                         BezTriple *bezt;
1181                         int a;
1182                         
1183                         nu= editNurb.first;
1184                         while(nu) {
1185                                 if((nu->type & 7)==1) {
1186                                         bezt= nu->bezt;
1187                                         a= nu->pntsu;
1188                                         while(a--) {
1189                                                 if(bezt->f2 & 1) {
1190                                                         VecAddf(bezt->vec[0], bezt->vec[0], median);
1191                                                         VecAddf(bezt->vec[1], bezt->vec[1], median);
1192                                                         VecAddf(bezt->vec[2], bezt->vec[2], median);
1193                                                 }
1194                                                 else {
1195                                                         if(bezt->f1 & 1) {
1196                                                                 VecAddf(bezt->vec[0], bezt->vec[0], median);
1197                                                         }
1198                                                         if(bezt->f3 & 1) {
1199                                                                 VecAddf(bezt->vec[2], bezt->vec[2], median);
1200                                                         }
1201                                                 }
1202                                                 bezt++;
1203                                         }
1204                                 }
1205                                 else {
1206                                         bp= nu->bp;
1207                                         a= nu->pntsu*nu->pntsv;
1208                                         while(a--) {
1209                                                 if(bp->f1 & 1) {
1210                                                         VecAddf(bp->vec, bp->vec, median);
1211                                                         bp->vec[3]+= median[3];
1212                                                         tot++;
1213                                                 }
1214                                                 bp++;
1215                                         }
1216                                 }
1217                                 test2DNurb(nu);
1218                                 testhandlesNurb(nu); /* test for bezier too */
1219
1220                                 nu= nu->next;
1221                         }
1222                         makeDispList(G.obedit);
1223                 }
1224         }
1225 }
1226
1227 static void v3d_posearmature_buts(uiBlock *block, Object *ob, float lim)
1228 {
1229         bArmature *arm;
1230         Bone *bone;
1231
1232         arm = get_armature(OBACT);
1233         if (!arm)
1234                 return;
1235
1236         bone = get_first_selected_bone();
1237
1238         if (!bone)
1239                 return;
1240         uiBlockBeginAlign(block);
1241         uiDefButF(block, NUM, B_ARMATUREPANEL2, "QuatX:",       10, 120, 140, 19, bone->quat, -100.0, 100.0, 10, 3, "");
1242         uiDefButF(block, NUM, B_ARMATUREPANEL2, "QuatZ:",       10, 100, 140, 19, bone->quat+2, -100.0, 100.0, 10, 3, "");
1243         uiBlockBeginAlign(block);
1244         uiDefButF(block, NUM, B_ARMATUREPANEL2, "QuatY:",       160, 120, 140, 19, bone->quat+1, -100.0, 100.0, 10, 3, "");
1245         uiDefButF(block, NUM, B_ARMATUREPANEL2, "QuatW:",       160, 100, 140, 19, bone->quat+3, -100.0, 100.0, 10, 3, "");
1246         uiBlockBeginAlign(block);
1247         uiDefButF(block, NUM, B_ARMATUREPANEL2, "LocX:",        10, 70, 140, 19, bone->loc, -lim, lim, 100, 3, "");
1248         uiDefButF(block, NUM, B_ARMATUREPANEL2, "LocY:",        10, 50, 140, 19, bone->loc+1, -lim, lim, 100, 3, "");
1249         uiDefButF(block, NUM, B_ARMATUREPANEL2, "locZ:",        10, 30, 140, 19, bone->loc+2, -lim, lim, 100, 3, "");
1250         uiBlockBeginAlign(block);
1251         uiDefButF(block, NUM, B_ARMATUREPANEL2, "SizeX:",       160, 70, 140, 19, bone->size, -lim, lim, 100, 3, "");
1252         uiDefButF(block, NUM, B_ARMATUREPANEL2, "SizeY:",       160, 50, 140, 19, bone->size+1, -lim, lim, 100, 3, "");
1253         uiDefButF(block, NUM, B_ARMATUREPANEL2, "SizeZ:",       160, 30, 140, 19, bone->size+2, -lim, lim, 100, 3, "");
1254         uiBlockEndAlign(block);
1255 }
1256
1257 static void v3d_editarmature_buts(uiBlock *block, Object *ob, float lim)
1258 {
1259         EditBone *ebone;
1260         
1261         ebone= G.edbo.first;
1262
1263         for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1264                 if (ebone->flag & BONE_SELECTED)
1265                         break;
1266         }
1267
1268         if (!ebone)
1269                 return;
1270         uiBlockBeginAlign(block);
1271         uiDefButF(block, NUM, B_ARMATUREPANEL1, "RootX:",       10, 70, 140, 19, ebone->head, -lim, lim, 100, 3, "");
1272         uiDefButF(block, NUM, B_ARMATUREPANEL1, "RootY:",       10, 50, 140, 19, ebone->head+1, -lim, lim, 100, 3, "");
1273         uiDefButF(block, NUM, B_ARMATUREPANEL1, "RootZ:",       10, 30, 140, 19, ebone->head+2, -lim, lim, 100, 3, "");
1274         uiBlockBeginAlign(block);
1275         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TipX:",        160, 70, 140, 19, ebone->tail, -lim, lim, 100, 3, "");
1276         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TipY:",        160, 50, 140, 19, ebone->tail+1, -lim, lim, 100, 3, "");
1277         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TipZ:",        160, 30, 140, 19, ebone->tail+2, -lim, lim, 100, 3, "");
1278         uiBlockEndAlign(block);
1279         ob_eul[0]= 180.0*ebone->roll/M_PI;
1280         uiDefButF(block, NUM, B_ARMATUREPANEL1, "Roll:",        10, 100, 140, 19, ob_eul, -lim, lim, 100, 3, "");
1281
1282 }
1283
1284
1285
1286 void do_viewbuts(unsigned short event)
1287 {
1288         View3D *vd;
1289         Object *ob= OBACT;
1290         char *name;
1291         
1292         vd= G.vd;
1293         if(vd==0) return;
1294
1295         switch(event) {
1296         case B_LOADBGPIC:
1297                 if(vd->bgpic && vd->bgpic->ima) name= vd->bgpic->ima->name;
1298                 else name= G.ima;
1299                 
1300                 if(G.qual==LR_CTRLKEY)
1301                         activate_imageselect(FILE_SPECIAL, "SELECT IMAGE", name, load_bgpic_image);
1302                 else
1303                         activate_fileselect(FILE_SPECIAL, "SELECT IMAGE", name, load_bgpic_image);
1304                 break;
1305                 
1306         case B_BLENDBGPIC:
1307                 if(vd->bgpic && vd->bgpic->rect) setalpha_bgpic(vd->bgpic);
1308                 addqueue(curarea->win, REDRAW, 1);
1309                 break;
1310                 
1311         case B_BGPICBROWSE:
1312                 if(vd->bgpic) {
1313                         if (vd->menunr==-2) {
1314                                 activate_databrowse((ID*) vd->bgpic->ima, ID_IM, 0, B_BGPICBROWSE, &vd->menunr, do_viewbuts);
1315                         } else if (vd->menunr>0) {
1316                                 Image *newima= (Image*) BLI_findlink(&G.main->image, vd->menunr-1);
1317
1318                                 if (newima)
1319                                         view3d_change_bgpic_ima(vd, newima);
1320                         }
1321                 }
1322                 break;
1323                 
1324         case B_BGPICCLEAR:
1325                 if (vd->bgpic)
1326                         view3d_change_bgpic_ima(vd, NULL);
1327                 break;
1328                 
1329         case B_BGPICTEX:
1330                 if (vd->bgpic) {
1331                         if (vd->texnr==-2) {
1332                                 activate_databrowse((ID*) vd->bgpic->tex, ID_TE, 0, B_BGPICTEX, &vd->texnr, do_viewbuts);
1333                         } else if (vd->texnr>0) {
1334                                 Tex *newtex= (Tex*) BLI_findlink(&G.main->tex, vd->texnr-1);
1335                                 
1336                                 if (newtex)
1337                                         view3d_change_bgpic_tex(vd, newtex);
1338                         }
1339                 }
1340                 break;
1341                 
1342         case B_BGPICTEXCLEAR:
1343                 if (vd->bgpic)
1344                         view3d_change_bgpic_tex(vd, NULL);
1345                 break;
1346                 
1347         case B_OBJECTPANELROT:
1348                 if(ob) {
1349                         ob->rot[0]= M_PI*ob_eul[0]/180.0;
1350                         ob->rot[1]= M_PI*ob_eul[1]/180.0;
1351                         ob->rot[2]= M_PI*ob_eul[2]/180.0;
1352                         allqueue(REDRAWVIEW3D, 1);
1353                 }
1354                 break;
1355                 
1356         case B_OBJECTPANELMEDIAN:
1357                 if(ob) {
1358                         v3d_editvertex_buts(NULL, ob, 1.0);
1359                         makeDispList(ob);
1360                         allqueue(REDRAWVIEW3D, 1);
1361                 }
1362                 break;
1363                 
1364         case B_ARMATUREPANEL1:
1365                 {
1366                         EditBone *ebone, *child;
1367                         
1368                         ebone= G.edbo.first;
1369                         for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1370                                 if (ebone->flag & BONE_SELECTED) break;
1371                         }
1372                         if (ebone) {
1373                                 ebone->roll= M_PI*ob_eul[0]/180.0;
1374                                 //      Update our parent
1375                                 if (ebone->parent && ebone->flag & BONE_IK_TOPARENT){
1376                                         VECCOPY (ebone->parent->tail, ebone->head);
1377                                 }
1378                         
1379                                 //      Update our children if necessary
1380                                 for (child = G.edbo.first; child; child=child->next){
1381                                         if (child->parent == ebone && child->flag & BONE_IK_TOPARENT){
1382                                                 VECCOPY (child->head, ebone->tail);
1383                                         }
1384                                 }
1385                                 allqueue(REDRAWVIEW3D, 1);
1386                         }
1387                 }
1388                 break;
1389         case B_ARMATUREPANEL2:
1390                 {
1391                         bPoseChannel *chan;
1392                         bArmature *arm;
1393                         Bone *bone;
1394
1395                         arm = get_armature(OBACT);
1396                         if (!arm) return;
1397                         bone = get_first_selected_bone();
1398                 
1399                         if (!bone) return;
1400
1401                         /* This is similar to code in special_trans_update */
1402         
1403                         if (!G.obpose->pose) G.obpose->pose= MEM_callocN(sizeof(bPose), "pose");
1404                         chan = MEM_callocN (sizeof (bPoseChannel), "transPoseChannel");
1405                 
1406                         chan->flag |= POSE_LOC|POSE_ROT|POSE_SIZE;
1407                         memcpy (chan->loc, bone->loc, sizeof (chan->loc));
1408                         memcpy (chan->quat, bone->quat, sizeof (chan->quat));
1409                         memcpy (chan->size, bone->size, sizeof (chan->size));
1410                         strcpy (chan->name, bone->name);
1411                         
1412                         set_pose_channel (G.obpose->pose, chan);
1413
1414                         rebuild_all_armature_displists();
1415
1416                         allqueue(REDRAWVIEW3D, 1);
1417                 }
1418         }
1419 }
1420
1421
1422 static void view3d_panel_object(short cntrl)    // VIEW3D_HANDLER_OBJECT
1423 {
1424         uiBlock *block;
1425         Object *ob= OBACT;
1426         float lim;
1427         
1428         if(ob==NULL) return;
1429
1430         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_object", UI_EMBOSS, UI_HELV, curarea->win);
1431         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
1432         uiSetPanelHandler(VIEW3D_HANDLER_OBJECT);  // for close and esc
1433         if(uiNewPanel(curarea, block, "Transform Properties", "View3d", 10, 230, 318, 204)==0) return;
1434
1435         uiDefBut(block, TEX, B_IDNAME, "OB: ",  10,180,140,20, ob->id.name+2, 0.0, 18.0, 0, 0, "");
1436         uiDefIDPoinBut(block, test_obpoin_but, B_REDR, "Par:", 160, 180, 140, 20, &ob->parent, "Parent Object"); 
1437
1438         lim= 1000.0*MAX2(1.0, G.vd->grid);
1439
1440         if(ob==G.obedit) {
1441                 if(ob->type==OB_ARMATURE) v3d_editarmature_buts(block, ob, lim);
1442                 else v3d_editvertex_buts(block, ob, lim);
1443         }
1444         else if(ob==G.obpose) {
1445                 v3d_posearmature_buts(block, ob, lim);
1446         }
1447         else {
1448                 uiBlockBeginAlign(block);
1449                 uiDefButF(block, NUM, REDRAWVIEW3D, "LocX:",            10, 140, 140, 19, &(ob->loc[0]), -lim, lim, 100, 3, "");
1450                 uiDefButF(block, NUM, REDRAWVIEW3D, "LocY:",            10, 120, 140, 19, &(ob->loc[1]), -lim, lim, 100, 3, "");
1451                 uiDefButF(block, NUM, REDRAWVIEW3D, "LocZ:",            10, 100, 140, 19, &(ob->loc[2]), -lim, lim, 100, 3, "");
1452         
1453                 ob_eul[0]= 180.0*ob->rot[0]/M_PI;
1454                 ob_eul[1]= 180.0*ob->rot[1]/M_PI;
1455                 ob_eul[2]= 180.0*ob->rot[2]/M_PI;
1456                 
1457                 uiBlockBeginAlign(block);
1458                 uiDefButF(block, NUM, B_OBJECTPANELROT, "RotX:",        10, 70, 140, 19, &(ob_eul[0]), -lim, lim, 1000, 3, "");
1459                 uiDefButF(block, NUM, B_OBJECTPANELROT, "RotY:",        10, 50, 140, 19, &(ob_eul[1]), -lim, lim, 1000, 3, "");
1460                 uiDefButF(block, NUM, B_OBJECTPANELROT, "RotZ:",        10, 30, 140, 19, &(ob_eul[2]), -lim, lim, 1000, 3, "");
1461                 uiBlockBeginAlign(block);
1462                 uiDefButF(block, NUM, REDRAWVIEW3D, "SizeX:",           160, 70, 140, 19, &(ob->size[0]), -lim, lim, 100, 3, "");
1463                 uiDefButF(block, NUM, REDRAWVIEW3D, "SizeY:",           160, 50, 140, 19, &(ob->size[1]), -lim, lim, 100, 3, "");
1464                 uiDefButF(block, NUM, REDRAWVIEW3D, "SizeZ:",           160, 30, 140, 19, &(ob->size[2]), -lim, lim, 100, 3, "");
1465                 uiBlockEndAlign(block);
1466         }
1467 }
1468
1469 static void view3d_panel_background(cntrl)      // VIEW3D_HANDLER_BACKGROUND
1470 {
1471         uiBlock *block;
1472         View3D *vd;
1473         ID *id;
1474         char *strp;
1475         
1476         vd= G.vd;
1477
1478         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_background", UI_EMBOSS, UI_HELV, curarea->win);
1479         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
1480         uiSetPanelHandler(VIEW3D_HANDLER_BACKGROUND);  // for close and esc
1481         if(uiNewPanel(curarea, block, "Background Image", "View3d", 340, 10, 318, 204)==0) return;
1482
1483         if(vd->flag & V3D_DISPBGPIC) {
1484                 if(vd->bgpic==0) {
1485                         vd->bgpic= MEM_callocN(sizeof(BGpic), "bgpic");
1486                         vd->bgpic->size= 5.0;
1487                         vd->bgpic->blend= 0.5;
1488                 }
1489         }
1490         
1491         uiDefButBitS(block, TOG, V3D_DISPBGPIC, REDRAWVIEW3D, "Use Background Image", 0, 162, 200, 20, &vd->flag, 0, 0, 0, 0, "Display an image in the background of the 3D View");
1492         
1493         uiDefBut(block, LABEL, 1, " ",  206, 162, 84, 20, NULL, 0.0, 0.0, 0, 0, "");
1494         
1495         
1496         if(vd->flag & V3D_DISPBGPIC) {
1497
1498                 /* Background Image */
1499                 uiDefBut(block, LABEL, 1, "Image:",     0, 128, 76, 19, NULL, 0.0, 0.0, 0, 0, "");
1500                 
1501                 uiBlockBeginAlign(block);
1502                 uiDefIconBut(block, BUT, B_LOADBGPIC, ICON_FILESEL,     90, 128, 20, 20, 0, 0, 0, 0, 0, "Open a new background image");
1503
1504                 id= (ID *)vd->bgpic->ima;
1505                 IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->image), id, &(vd->menunr));
1506                 if(strp[0]) {
1507                 
1508                         uiDefButS(block, MENU, B_BGPICBROWSE, strp,     110, 128, 20, 20, &(vd->menunr), 0, 0, 0, 0, "Select a background image");
1509                 
1510                         if(vd->bgpic->ima)  {
1511                                 uiDefBut(block, TEX,        0,"BG: ",           130, 128, 140, 20, &vd->bgpic->ima->name,0.0,100.0, 0, 0, "The currently selected background image");
1512                                 uiDefIconBut(block, BUT, B_BGPICCLEAR, ICON_X, 270, 128, 20, 20, 0, 0, 0, 0, 0, "Remove background image link");
1513                         }
1514                         uiBlockEndAlign(block);
1515                 } else {
1516                         uiBlockEndAlign(block);
1517                 }
1518                 MEM_freeN(strp);
1519
1520
1521                 /* Background texture */
1522                 uiDefBut(block, LABEL, 1, "Texture:",   0, 100, 76, 19, NULL, 0.0, 0.0, 0, 0, "");
1523                 
1524                 id= (ID *)vd->bgpic->tex;
1525                 IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->tex), id, &(vd->texnr));
1526                 if (strp[0])
1527                         uiBlockBeginAlign(block);
1528                         uiDefButS(block, MENU, B_BGPICTEX, strp,                        90, 100, 20,20, &(vd->texnr), 0, 0, 0, 0, "Select a texture to use as an animated background image");
1529                 MEM_freeN(strp);
1530                 
1531                 if (id) {
1532                         uiDefBut(block, TEX, B_IDNAME, "TE:",                           110, 100, 160, 20, id->name+2, 0.0, 18.0, 0, 0, "");
1533                         uiDefIconBut(block, BUT, B_BGPICTEXCLEAR, ICON_X,       270, 100, 20, 20, 0, 0, 0, 0, 0, "Remove background texture link");
1534                         uiBlockEndAlign(block);
1535                 } else {
1536                         uiBlockEndAlign(block);
1537                 }
1538
1539                 uiDefButF(block, NUMSLI, B_BLENDBGPIC, "Blend:",        0, 60 , 290, 19, &vd->bgpic->blend, 0.0,1.0, 0, 0, "Set the transparency of the background image");
1540
1541                 uiDefButF(block, NUM, REDRAWVIEW3D, "Size:",            0, 28, 140, 19, &vd->bgpic->size, 0.1, 250.0, 100, 0, "Set the size (width) of the background image");
1542
1543                 uiDefButF(block, NUM, REDRAWVIEW3D, "X Offset:",        0, 6, 140, 19, &vd->bgpic->xof, -20.0,20.0, 10, 2, "Set the horizontal offset of the background image");
1544                 uiDefButF(block, NUM, REDRAWVIEW3D, "Y Offset:",        150, 6, 140, 19, &vd->bgpic->yof, -20.0,20.0, 10, 2, "Set the vertical offset of the background image");
1545
1546         
1547                 
1548                 // 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");
1549                 
1550
1551
1552 //              uiDefButF(block, NUMSLI, B_BLENDBGPIC, "Blend:",        120,100,190,20,&vd->bgpic->blend, 0.0,1.0, 0, 0, "Set the BackGroundPic transparency");
1553                 
1554 //              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");
1555 //              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");
1556
1557         }
1558 }
1559
1560
1561 static void view3d_panel_properties(cntrl)      // VIEW3D_HANDLER_SETTINGS
1562 {
1563         uiBlock *block;
1564         View3D *vd;
1565         
1566         vd= G.vd;
1567
1568         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_properties", UI_EMBOSS, UI_HELV, curarea->win);
1569         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
1570         uiSetPanelHandler(VIEW3D_HANDLER_PROPERTIES);  // for close and esc
1571         if(uiNewPanel(curarea, block, "View Properties", "View3d", 340, 10, 318, 204)==0) return;
1572
1573         uiDefBut(block, LABEL, 1, "Grid:",      0, 162, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
1574
1575         uiDefButF(block, NUM, REDRAWVIEW3D, "Spacing:",                 0, 140, 140, 19, &vd->grid, 0.001, 100.0, 10, 0, "Set the distance between grid lines");
1576         uiDefButS(block, NUM, REDRAWVIEW3D, "Lines:",           150, 140, 140, 19, &vd->gridlines, 0.0, 100.0, 100, 0, "Set the number of grid lines");
1577
1578         uiDefBut(block, LABEL, 1, "3D Grid:",   0, 110, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
1579         
1580         uiDefButBitS(block, TOG, V3D_SHOW_FLOOR, REDRAWVIEW3D, "Grid Floor",    0, 88, 90, 19, &vd->gridflag, 0, 0, 0, 0, "Show the grid floor in free camera mode");
1581         uiDefButBitS(block, TOG, V3D_SHOW_X, REDRAWVIEW3D, "X Axis",    102, 88, 60, 19, &vd->gridflag, 0, 0, 0, 0, "Show the X Axis line");
1582         uiDefButBitS(block, TOG, V3D_SHOW_Y, REDRAWVIEW3D, "Y Axis",    166, 88, 60, 19, &vd->gridflag, 0, 0, 0, 0, "Show the Y Axis line");
1583         uiDefButBitS(block, TOG, V3D_SHOW_Z, REDRAWVIEW3D, "Z Axis",    230, 88, 60, 19, &vd->gridflag, 0, 0, 0, 0, "Show the Z Axis line");
1584
1585         uiDefBut(block, LABEL, 1, "View Camera:",       0, 50, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
1586         
1587         uiDefButF(block, NUM, REDRAWVIEW3D, "Lens:",                    0, 28, 140, 19, &vd->lens, 10.0, 120.0, 100, 0, "The lens angle in perspective view");
1588         
1589         uiDefButF(block, NUM, REDRAWVIEW3D, "Clip Start:",              0, 6, 140, 19, &vd->near, vd->grid/10.0, 100.0, 10, 0, "Set the beginning of the range in which 3D objects are displayed (perspective view)");
1590         uiDefButF(block, NUM, REDRAWVIEW3D, "Clip End:",                        150, 6, 140, 19, &vd->far, 1.0, 1000.0*vd->grid, 100, 0, "Set the end of the range in which 3D objects are displayed (perspective view)");
1591
1592 }
1593
1594
1595
1596 static void view3d_blockhandlers(ScrArea *sa)
1597 {
1598         View3D *v3d= sa->spacedata.first;
1599         short a;
1600         
1601         uiFreeBlocksWin(&sa->uiblocks, sa->win);
1602
1603         for(a=0; a<SPACE_MAXHANDLER; a+=2) {
1604         
1605                 switch(v3d->blockhandler[a]) {
1606
1607                 case VIEW3D_HANDLER_PROPERTIES:
1608                         view3d_panel_properties(v3d->blockhandler[a+1]);
1609                         break;
1610                 case VIEW3D_HANDLER_BACKGROUND:
1611                         view3d_panel_background(v3d->blockhandler[a+1]);
1612                         break;
1613                 case VIEW3D_HANDLER_OBJECT:
1614                         view3d_panel_object(v3d->blockhandler[a+1]);
1615                 
1616                         break;
1617                 
1618                 }
1619                 /* clear action value for event */
1620                 v3d->blockhandler[a+1]= 0;
1621         }
1622         uiDrawBlocksPanels(sa, 0);
1623
1624 }
1625
1626 void drawview3dspace(ScrArea *sa, void *spacedata)
1627 {
1628         extern void constline_callback(void);   // editobject.c helpline
1629         Base *base;
1630         Object *ob;
1631         
1632         setwinmatrixview3d(0);  /* 0= no pick rect */
1633         setviewmatrixview3d();
1634
1635         Mat4MulMat4(G.vd->persmat, G.vd->viewmat, curarea->winmat);
1636         Mat4Invert(G.vd->persinv, G.vd->persmat);
1637         Mat4Invert(G.vd->viewinv, G.vd->viewmat);
1638
1639         if(G.vd->drawtype > OB_WIRE) {
1640                 G.zbuf= TRUE;
1641                 glEnable(GL_DEPTH_TEST);
1642                 if(G.f & G_SIMULATION) {
1643                         glClearColor(0.0, 0.0, 0.0, 0.0); 
1644                 }
1645                 else {
1646                         float col[3];
1647                         BIF_GetThemeColor3fv(TH_BACK, col);
1648                         glClearColor(col[0], col[1], col[2], 0.0); 
1649                 }
1650                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1651                 
1652                 glLoadIdentity();
1653         }
1654         else {
1655                 float col[3];
1656                 BIF_GetThemeColor3fv(TH_BACK, col);
1657                 glClearColor(col[0], col[1], col[2], 0.0);
1658                 glClear(GL_COLOR_BUFFER_BIT);
1659         }
1660         
1661         myloadmatrix(G.vd->viewmat);
1662         persp(PERSP_STORE);  // store correct view for persp(PERSP_VIEW) calls
1663
1664         if(G.vd->view==0 || G.vd->persp!=0) {
1665                 drawfloor();
1666                 if(G.vd->persp==2) {
1667                         if(G.scene->world) {
1668                                 if(G.scene->world->mode & WO_STARS) RE_make_stars(star_stuff_init_func,
1669                                                                                                                                   star_stuff_vertex_func,
1670                                                                                                                                   star_stuff_term_func);
1671                         }
1672                         if(G.vd->flag & V3D_DISPBGPIC) draw_bgpic();
1673                 }
1674         }
1675         else {
1676                 drawgrid();
1677
1678                 if(G.vd->flag & V3D_DISPBGPIC) {
1679                         draw_bgpic();
1680                 }
1681         }
1682         
1683 #if 0
1684         /* Lets be a little more selective about when and where we do this,
1685          * or else armatures/poses/displists get recalculated all of the
1686          * time
1687          */
1688         clear_all_constraints();
1689 #endif
1690
1691         /* draw set first */
1692         if(G.scene->set) {
1693         
1694                 /* patch: color remains constant */ 
1695                 G.f |= G_PICKSEL;
1696
1697                 base= G.scene->set->base.first;
1698                 while(base) {
1699                         if(G.vd->lay & base->lay) {
1700                                 where_is_object(base->object);
1701
1702                                 cpack(0x404040);
1703                                 draw_object(base);
1704
1705                                 if(base->object->transflag & OB_DUPLI) {
1706                                         extern ListBase duplilist;
1707                                         Base tbase;
1708                                         
1709                                         tbase= *base;
1710                                         
1711                                         tbase.flag= OB_FROMDUPLI;
1712                                         make_duplilist(G.scene->set, base->object);
1713                                         ob= duplilist.first;
1714                                         while(ob) {
1715                                                 tbase.object= ob;
1716                                                 draw_object(&tbase);
1717                                                 ob= ob->id.next;
1718                                         }
1719                                         free_duplilist();
1720                                         
1721                                 }
1722                         }
1723                         base= base->next;
1724                 }
1725                 
1726                 G.f &= ~G_PICKSEL;
1727         }
1728         
1729         /* first draw not selected and the duplis */
1730         base= G.scene->base.first;
1731         while(base) {
1732                 
1733                 if(G.vd->lay & base->lay) {
1734                         
1735                         where_is_object(base->object);
1736
1737                         /* dupli drawing temporal off here */
1738                         if(FALSE && base->object->transflag & OB_DUPLI) {
1739                                 extern ListBase duplilist;
1740                                 Base tbase;
1741
1742                                 /* draw original always first because of make_displist */
1743                                 draw_object(base);
1744
1745                                 /* patch: color remains constant */ 
1746                                 G.f |= G_PICKSEL;
1747                                 cpack(0x404040);
1748                                 
1749                                 tbase.flag= OB_FROMDUPLI;
1750                                 make_duplilist(G.scene, base->object);
1751
1752                                 ob= duplilist.first;
1753                                 while(ob) {
1754                                         tbase.object= ob;
1755                                         draw_object(&tbase);
1756                                         ob= ob->id.next;
1757                                 }
1758                                 free_duplilist();
1759                                 
1760                                 G.f &= ~G_PICKSEL;                              
1761                         }
1762                         else if((base->flag & SELECT)==0) {
1763                                 draw_object(base);
1764                         }
1765                         
1766                 }
1767                 
1768                 base= base->next;
1769         }
1770         /* draw selected */
1771         base= G.scene->base.first;
1772         while(base) {
1773                 
1774                 if ( ((base)->flag & SELECT) && ((base)->lay & G.vd->lay) ) {
1775                         draw_object(base);
1776                 }
1777                 
1778                 base= base->next;
1779         }
1780
1781         if(G.moving) constline_callback();
1782
1783         /* duplis, draw as last to make sure the displists are ok */
1784         base= G.scene->base.first;
1785         while(base) {
1786                 
1787                 if(G.vd->lay & base->lay) {
1788                         if(base->object->transflag & OB_DUPLI) {
1789                                 extern ListBase duplilist;
1790                                 Base tbase;
1791
1792                                 /* patch: color remains constant */ 
1793                                 G.f |= G_PICKSEL;
1794                                 cpack(0x404040);
1795                                 
1796                                 tbase.flag= OB_FROMDUPLI;
1797                                 make_duplilist(G.scene, base->object);
1798
1799                                 ob= duplilist.first;
1800                                 while(ob) {
1801                                         tbase.object= ob;
1802                                         draw_object(&tbase);
1803                                         ob= ob->id.next;
1804                                 }
1805                                 free_duplilist();
1806                                 
1807                                 G.f &= ~G_PICKSEL;                              
1808                         }
1809                 }
1810                 base= base->next;
1811         }
1812
1813
1814         if(G.scene->radio) RAD_drawall(G.vd->drawtype>=OB_SOLID);
1815         
1816         if(G.zbuf) {
1817                 G.zbuf= FALSE;
1818                 glDisable(GL_DEPTH_TEST);
1819         }
1820
1821         persp(PERSP_WIN);  // set ortho
1822         
1823         if(G.vd->persp>1) drawviewborder();
1824         drawcursor();
1825         draw_view_icon();
1826
1827         ob= OBACT;
1828         if(ob!=0 && (U.uiflag & USER_DRAWVIEWINFO)) draw_selected_name(ob->id.name+2);
1829         
1830         draw_area_emboss(sa);
1831         
1832         /* it is important to end a view in a transform compatible with buttons */
1833
1834         bwin_scalematrix(sa->win, G.vd->blockscale, G.vd->blockscale, G.vd->blockscale);
1835         view3d_blockhandlers(sa);
1836
1837         curarea->win_swap= WIN_BACK_OK;
1838         
1839         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
1840                 G.vd->flag |= V3D_NEEDBACKBUFDRAW;
1841                 addafterqueue(curarea->win, BACKBUFDRAW, 1);
1842         }
1843         
1844 }
1845
1846
1847         /* Called back by rendering system, icky
1848          */
1849 void drawview3d_render(struct View3D *v3d)
1850 {
1851         extern void mywindow_build_and_set_renderwin(void);
1852         extern short v3d_windowmode;
1853         Base *base;
1854         Object *ob;
1855
1856         free_all_realtime_images();
1857         mywindow_build_and_set_renderwin();
1858                 
1859         v3d_windowmode= 1;
1860         setwinmatrixview3d(0);
1861         v3d_windowmode= 0;
1862         glMatrixMode(GL_PROJECTION);
1863         glLoadMatrixf(R.winmat);
1864         glMatrixMode(GL_MODELVIEW);
1865         
1866         setviewmatrixview3d();
1867         glLoadMatrixf(v3d->viewmat);
1868
1869         Mat4MulMat4(v3d->persmat, v3d->viewmat, R.winmat);
1870         Mat4Invert(v3d->persinv, v3d->persmat);
1871         Mat4Invert(v3d->viewinv, v3d->viewmat);
1872
1873         if(v3d->drawtype > OB_WIRE) {
1874                 G.zbuf= TRUE;
1875                 glEnable(GL_DEPTH_TEST);
1876         }
1877
1878         if (v3d->drawtype==OB_TEXTURE && G.scene->world) {
1879                 glClearColor(G.scene->world->horr, G.scene->world->horg, G.scene->world->horb, 0.0); 
1880         } else {
1881                 float col[3];
1882                 BIF_GetThemeColor3fv(TH_BACK, col);
1883                 glClearColor(col[0], col[1], col[2], 0.0); 
1884         }
1885         
1886         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1887         
1888         glLoadIdentity();
1889         glLoadMatrixf(v3d->viewmat);
1890         
1891         /* abuse! to make sure it doesnt draw the helpstuff */
1892         G.f |= G_SIMULATION;
1893
1894         clear_all_constraints();
1895         do_all_ipos();
1896         BPY_do_all_scripts(SCRIPT_FRAMECHANGED);
1897         do_all_keys();
1898         do_all_actions();
1899         do_all_ikas();
1900
1901         test_all_displists();
1902
1903         /* not really nice forcing of calc_ipo and where_is */
1904         ob= G.main->object.first;
1905         while(ob) {
1906                 ob->ctime= -123.456;
1907                 ob= ob->id.next;
1908         }
1909
1910         /* first deaw set */
1911         if(G.scene->set) {
1912         
1913                 /* patch: color remains constant */ 
1914                 G.f |= G_PICKSEL;
1915                 
1916                 base= G.scene->set->base.first;
1917                 while(base) {
1918                         if(v3d->lay & base->lay) {
1919                                 if ELEM3(base->object->type, OB_LAMP, OB_CAMERA, OB_LATTICE);
1920                                 else {
1921                                         where_is_object(base->object);
1922         
1923                                         cpack(0x404040);
1924                                         draw_object(base);
1925         
1926                                         if(base->object->transflag & OB_DUPLI) {
1927                                                 extern ListBase duplilist;
1928                                                 Base tbase;
1929                                                 
1930                                                 tbase.flag= OB_FROMDUPLI;
1931                                                 make_duplilist(G.scene->set, base->object);
1932                                                 ob= duplilist.first;
1933                                                 while(ob) {
1934                                                         tbase.object= ob;
1935                                                         draw_object(&tbase);
1936                                                         ob= ob->id.next;
1937                                                 }
1938                                                 free_duplilist();
1939                                         }
1940                                 }
1941                         }
1942                         base= base->next;
1943                 }
1944                 
1945                 G.f &= ~G_PICKSEL;
1946         }
1947
1948         clear_all_constraints();
1949
1950         /* first not selected and duplis */
1951         base= G.scene->base.first;
1952         while(base) {
1953                 
1954                 if(v3d->lay & base->lay) {
1955                         if ELEM3(base->object->type, OB_LAMP, OB_CAMERA, OB_LATTICE);
1956                         else {
1957                                 where_is_object(base->object);
1958         
1959                                 if(base->object->transflag & OB_DUPLI) {
1960                                         extern ListBase duplilist;
1961                                         Base tbase;
1962                                         
1963                                         /* always draw original first because of make_displist */
1964                                         draw_object(base);
1965                                         
1966                                         /* patch: color remains constant */ 
1967                                         G.f |= G_PICKSEL;
1968                                         cpack(0x404040);
1969                                         
1970                                         tbase.flag= OB_FROMDUPLI;
1971                                         make_duplilist(G.scene, base->object);
1972                                         ob= duplilist.first;
1973                                         while(ob) {
1974                                                 tbase.object= ob;
1975                                                 draw_object(&tbase);
1976                                                 ob= ob->id.next;
1977                                         }
1978                                         free_duplilist();
1979                                         
1980                                         G.f &= ~G_PICKSEL;                              
1981                                 }
1982                                 else if((base->flag & SELECT)==0) {
1983                                         draw_object(base);
1984                                 }
1985                         }
1986                 }
1987                 
1988                 base= base->next;
1989         }
1990
1991         /* draw selected */
1992         base= G.scene->base.first;
1993         while(base) {
1994                 
1995                 if ( ((base)->flag & SELECT) && ((base)->lay & v3d->lay) ) {
1996                         if ELEM3(base->object->type, OB_LAMP, OB_CAMERA, OB_LATTICE);
1997                         else draw_object(base);
1998                 }
1999                 
2000                 base= base->next;
2001         }
2002
2003         if(G.scene->radio) RAD_drawall(G.vd->drawtype>=OB_SOLID);
2004         
2005         if(G.zbuf) {
2006                 G.zbuf= FALSE;
2007                 glDisable(GL_DEPTH_TEST);
2008         }
2009         
2010         G.f &= ~G_SIMULATION;
2011
2012         glFinish();
2013
2014         glReadPixels(0, 0, R.rectx, R.recty, GL_RGBA, GL_UNSIGNED_BYTE, R.rectot);
2015         glLoadIdentity();
2016
2017         free_all_realtime_images();
2018 }
2019
2020
2021 double tottime = 0.0;
2022
2023 int update_time(void)
2024 {
2025         static double ltime;
2026         double time;
2027
2028         if ((U.mixbufsize)&&(audiostream_pos() != CFRA)&&(G.scene->audio.flag & AUDIO_SYNC)) return 0;
2029
2030         time = PIL_check_seconds_timer();
2031         
2032         tottime += (time - ltime);
2033         ltime = time;
2034         return (tottime < 0.0);
2035 }
2036
2037 double speed_to_swaptime(int speed)
2038 {
2039         switch(speed) {
2040         case 1:
2041                 return 1.0/60.0;
2042         case 2:
2043                 return 1.0/50.0;
2044         case 3:
2045                 return 1.0/30.0;
2046         case 4:
2047                 return 1.0/25.0;
2048         case 5:
2049                 return 1.0/20.0;
2050         case 6:
2051                 return 1.0/15.0;
2052         case 7:
2053                 return 1.0/12.5;
2054         case 8:
2055                 return 1.0/10.0;
2056         case 9:
2057                 return 1.0/6.0;
2058         }
2059         return 1.0/4.0;
2060 }
2061
2062 double key_to_swaptime(int key)
2063 {
2064         switch(key) {
2065         case PAD1:
2066                 G.animspeed= 1;
2067                 tottime= 0;
2068                 return speed_to_swaptime(1);
2069         case PAD2:
2070                 G.animspeed= 2;
2071                 tottime= 0;
2072                 return speed_to_swaptime(2);
2073         case PAD3:
2074                 G.animspeed= 3;
2075                 tottime= 0;
2076                 return speed_to_swaptime(3);
2077         case PAD4:
2078                 G.animspeed= 4;
2079                 tottime= 0;
2080                 return speed_to_swaptime(4);
2081         case PAD5:
2082                 G.animspeed= 5;
2083                 tottime= 0;
2084                 return speed_to_swaptime(5);
2085         case PAD6:
2086                 G.animspeed= 6;
2087                 tottime= 0;
2088                 return speed_to_swaptime(6);
2089         case PAD7:
2090                 G.animspeed= 7;
2091                 tottime= 0;
2092                 return speed_to_swaptime(7);
2093         case PAD8:
2094                 G.animspeed= 8;
2095                 tottime= 0;
2096                 return speed_to_swaptime(8);
2097         case PAD9:
2098                 G.animspeed= 9;
2099                 tottime= 0;
2100                 return speed_to_swaptime(9);
2101         }
2102         
2103         return speed_to_swaptime(G.animspeed);
2104 }
2105
2106 #ifdef NAN_LINEAR_PHYSICS
2107
2108 void sumo_callback(void *obp)
2109 {
2110         Object *ob= obp;
2111         SM_Vector3 vec;
2112         float matf[3][3];
2113         int i, j;
2114
2115     SM_GetMatrixf(ob->sumohandle, ob->obmat[0]);
2116
2117         VECCOPY(ob->loc, ob->obmat[3]);
2118         
2119     for (i = 0; i < 3; ++i) {
2120         for (j = 0; j < 3; ++j) {
2121             matf[i][j] = ob->obmat[i][j];
2122         }
2123     }
2124     Mat3ToEul(matf, ob->rot);
2125 }
2126
2127 /* for test and fun, i've written the next functions to play with dynamics
2128    using a variant of play-anim... was never released nor really tested (ton) */
2129
2130 void init_anim_sumo(void)
2131 {
2132         extern Material defmaterial;
2133         Base *base;
2134     Mesh *me;
2135         Object *ob;
2136     Material *mat;
2137         MFace *mface;
2138         MVert *mvert;
2139     float centre[3], size[3];
2140         int a;
2141     SM_ShapeHandle shape;
2142         SM_SceneHandle scene;
2143     SM_Material material;
2144     SM_MassProps massprops;
2145     SM_Vector3 vec;
2146     SM_Vector3 scaling;
2147         
2148         scene= SM_CreateScene();
2149         G.scene->sumohandle = scene;
2150         
2151         vec[0]=  0.0;
2152         vec[1]=  0.0;
2153         vec[2]= -9.8;
2154         SM_SetForceField(scene, vec);
2155         
2156     /* ton: cylinders & cones are still Y-axis up, will be Z-axis later */
2157     /* ton: write location/rotation save and restore */
2158         
2159         base= FIRSTBASE;
2160         while (base) {
2161                 if (G.vd->lay & base->lay) {
2162             ob= base->object;
2163                          
2164             /* define shape, for now only meshes take part in physics */
2165             get_local_bounds(ob, centre, size);
2166             
2167             if (ob->type==OB_MESH) {
2168                 me= ob->data;
2169                 
2170                 if (ob->gameflag & OB_DYNAMIC) {
2171                     if (me->sumohandle)
2172                         shape= me->sumohandle;
2173                     else {
2174                         /* make new handle */
2175                         switch(ob->boundtype) {
2176                         case OB_BOUND_BOX:
2177                             shape= SM_Box(2.0*size[0], 2.0*size[1], 2.0*size[2]);
2178                             break;
2179                         case OB_BOUND_SPHERE:
2180                             shape= SM_Sphere(size[0]);
2181                             break;
2182                         case OB_BOUND_CYLINDER:
2183                             shape= SM_Cylinder(size[0], 2.0*size[2]);
2184                             break;
2185                         case OB_BOUND_CONE:
2186                             shape= SM_Cone(size[0], 2.0*size[2]);
2187                             break;
2188                         }
2189                         
2190                                                 me->sumohandle= shape;
2191                                         }
2192                     /* sumo material properties */
2193                         mat= give_current_material(ob, 0);
2194                         if(mat==NULL)
2195                         mat= &defmaterial;
2196                     
2197                         material.restitution= mat->reflect;
2198                         material.static_friction= mat->friction;
2199                         material.dynamic_friction= mat->friction;
2200                     
2201                         /* sumo mass properties */
2202                         massprops.mass= ob->mass;
2203                         massprops.center[0]= 0.0;
2204                         massprops.center[1]= 0.0;
2205                         massprops.center[2]= 0.0;
2206
2207                         massprops.inertia[0]= 0.5*ob->mass;
2208                         massprops.inertia[1]= 0.5*ob->mass;
2209                         massprops.inertia[2]= 0.5*ob->mass;
2210
2211                         massprops.orientation[0]= 0.0;
2212                         massprops.orientation[1]= 0.0;
2213                         massprops.orientation[2]= 0.0;
2214                         massprops.orientation[3]= 1.0;
2215
2216                         ob->sumohandle = SM_CreateObject(ob, shape, &material, 
2217                                                      &massprops, sumo_callback);
2218                                         SM_AddObject(scene, ob->sumohandle);
2219                                         
2220                     scaling[0] = ob->size[0];
2221                     scaling[1] = ob->size[1];
2222                     scaling[2] = ob->size[2];
2223                                         SM_SetMatrixf(ob->sumohandle, ob->obmat[0]);
2224                                         SM_SetScaling(ob->sumohandle, scaling);
2225
2226                                 }
2227                                 else {
2228                                         if(me->sumohandle) shape= me->sumohandle;
2229                                         else {
2230                                                 /* make new handle */
2231                                 shape= SM_NewComplexShape();
2232                                                 
2233                                                 mface= me->mface;
2234                                                 mvert= me->mvert;
2235                                                 for(a=0; a<me->totface; a++,mface++) {
2236                                                         if(mface->v3) {
2237                                                                 SM_Begin();
2238                                                                 SM_Vertex( (mvert+mface->v1)->co[0], (mvert+mface->v1)->co[1], (mvert+mface->v1)->co[2]);
2239                                                                 SM_Vertex( (mvert+mface->v2)->co[0], (mvert+mface->v2)->co[1], (mvert+mface->v2)->co[2]);
2240                                                                 SM_Vertex( (mvert+mface->v3)->co[0], (mvert+mface->v3)->co[1], (mvert+mface->v3)->co[2]);
2241                                                                 if(mface->v4)
2242                                                                         SM_Vertex( (mvert+mface->v4)->co[0], (mvert+mface->v4)->co[1], (mvert+mface->v4)->co[2]);
2243                                                                 SM_End();
2244                                                         }
2245                                                 }
2246                                                 
2247                                                 SM_EndComplexShape();
2248                                                 
2249                                                 me->sumohandle= shape;
2250                                         }
2251                     /* sumo material properties */
2252                         mat= give_current_material(ob, 0);
2253                         if(mat==NULL)
2254                         mat= &defmaterial;
2255                         material.restitution= mat->reflect;
2256                         material.static_friction= mat->friction;
2257                         material.dynamic_friction= mat->friction;
2258
2259                         /* sumo mass properties */
2260                         massprops.mass= ob->mass;
2261                         massprops.center[0]= 0.0;
2262                         massprops.center[1]= 0.0;
2263                         massprops.center[2]= 0.0;
2264
2265                         massprops.inertia[0]= 0.5*ob->mass;
2266                         massprops.inertia[1]= 0.5*ob->mass;
2267                         massprops.inertia[2]= 0.5*ob->mass;
2268
2269                         massprops.orientation[0]= 0.0;
2270                         massprops.orientation[1]= 0.0;
2271                         massprops.orientation[2]= 0.0;
2272                         massprops.orientation[3]= 1.0;
2273
2274                         ob->sumohandle= SM_CreateObject(ob, shape, &material, NULL, NULL);
2275                                         SM_AddObject(scene, ob->sumohandle);
2276
2277                     scaling[0] = ob->size[0];
2278                     scaling[1] = ob->size[1];
2279                     scaling[2] = ob->size[2];
2280                                         SM_SetMatrixf(ob->sumohandle, ob->obmat[0]);
2281                                         SM_SetScaling(ob->sumohandle, scaling);
2282                                 }
2283             }
2284         }       
2285         base= base->next;
2286     }
2287 }
2288
2289 /* update animated objects */
2290 void update_anim_sumo(void)
2291 {
2292     SM_Vector3 scaling;
2293
2294         Base *base;
2295         Object *ob;
2296         Mesh *me;
2297         
2298         base= FIRSTBASE;
2299         while(base) {
2300                 if(G.vd->lay & base->lay) {
2301                         ob= base->object;
2302                         
2303                         if(ob->sumohandle) {
2304                                 if((ob->gameflag & OB_DYNAMIC)==0) {
2305                                         /* maybe: optimise, check for anim */
2306                     scaling[0] = ob->size[0];
2307                     scaling[1] = ob->size[1];
2308                     scaling[2] = ob->size[2];
2309                                         SM_SetMatrixf(ob->sumohandle, ob->obmat[0]);
2310                                         SM_SetScaling(ob->sumohandle, scaling);
2311                                 }
2312                         }                               
2313                 }
2314                 base= base->next;
2315         }
2316
2317 }
2318
2319 void end_anim_sumo(void)
2320 {
2321         Base *base;
2322         Object *ob;
2323         Mesh *me;
2324         
2325         base= FIRSTBASE;
2326         while(base) {
2327                 if(G.vd->lay & base->lay) {
2328                         ob= base->object;
2329                         
2330             if(ob->type==OB_MESH) {
2331                                 if(ob->sumohandle) {
2332                                         SM_RemoveObject(G.scene->sumohandle, ob->sumohandle);
2333                                         SM_DeleteObject(ob->sumohandle);
2334                                         ob->sumohandle= NULL;
2335                                 }
2336                                 me= ob->data;
2337                                 if(me->sumohandle) {
2338                                         SM_DeleteShape(me->sumohandle);
2339                                         me->sumohandle= NULL;
2340                                 }
2341                         }
2342                 }
2343                 base= base->next;
2344         }
2345         if(G.scene->sumohandle) {
2346                 SM_DeleteScene(G.scene->sumohandle);
2347                 G.scene->sumohandle= NULL;
2348         }
2349 }
2350
2351 #endif
2352
2353 void inner_play_anim_loop(int init, int mode)
2354 {
2355         ScrArea *sa;
2356         static ScrArea *oldsa;
2357         static double swaptime;
2358         static int curmode;
2359         Base *base;
2360
2361         /* init */
2362         if(init) {
2363                 oldsa= curarea;
2364                 swaptime= speed_to_swaptime(G.animspeed);
2365                 tottime= 0.0;
2366                 curmode= mode;
2367 #ifdef NAN_LINEAR_PHYSICS
2368         init_anim_sumo();
2369 #endif        
2370                 return;
2371         }
2372
2373         set_timecursor(CFRA);
2374
2375         clear_all_constraints();
2376         do_all_ipos();
2377         BPY_do_all_scripts(SCRIPT_FRAMECHANGED);
2378         do_all_keys();
2379         do_all_actions();
2380         do_all_ikas();
2381
2382
2383         test_all_displists();
2384 #ifdef NAN_LINEAR_PHYSICS       
2385         update_anim_sumo();
2386         
2387         SM_Proceed(G.scene->sumohandle, swaptime, 40, NULL);
2388 #endif
2389         sa= G.curscreen->areabase.first;
2390         while(sa) {
2391                 if(sa==oldsa) {
2392                         scrarea_do_windraw(sa);
2393                 }
2394                 else if(curmode) {
2395                         if ELEM(sa->spacetype, SPACE_VIEW3D, SPACE_SEQ) {
2396                                 scrarea_do_windraw(sa);
2397                         }
2398                 }
2399                 
2400                 sa= sa->next;   
2401         }
2402         
2403         /* make sure that swaptime passed by */
2404         tottime -= swaptime;
2405         while (update_time()) PIL_sleep_ms(1);
2406
2407         if(CFRA>=EFRA) {
2408                 if (tottime > 0.0) tottime = 0.0;
2409                 CFRA= SFRA;
2410                 audiostream_stop();
2411                 audiostream_start( CFRA );
2412         }
2413         else {
2414                 if (U.mixbufsize && (G.scene->audio.flag & AUDIO_SYNC)) CFRA = audiostream_pos();
2415                 else CFRA++;
2416         }
2417 }
2418
2419 int play_anim(int mode)
2420 {
2421         ScrArea *sa, *oldsa;
2422         int cfraont;
2423         unsigned short event=0;
2424         short val;
2425         Base *base;
2426
2427         /* patch for very very old scenes */
2428         if(SFRA==0) SFRA= 1;
2429         if(EFRA==0) EFRA= 250;
2430         
2431         if(SFRA>EFRA) return 0;
2432         
2433         update_time();
2434
2435         /* waitcursor(1); */
2436         G.f |= G_PLAYANIM;              /* in sequence.c and view.c this is handled */
2437
2438         cfraont= CFRA;
2439         oldsa= curarea;
2440
2441         audiostream_start( CFRA );
2442         
2443         inner_play_anim_loop(1, mode);  /* 1==init */
2444
2445         while(TRUE) {
2446
2447                 inner_play_anim_loop(0, 0);
2448         
2449                 screen_swapbuffers();
2450                 
2451                 while(qtest()) {
2452                 
2453                         event= extern_qread(&val);
2454                         if(event==ESCKEY) break;
2455                         else if(event==MIDDLEMOUSE) {
2456                                 if(U.flag & USER_VIEWMOVE) {
2457                                         if(G.qual & LR_SHIFTKEY) viewmove(0);
2458                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
2459                                         else viewmove(1);
2460                                 }
2461                                 else {
2462                                         if(G.qual & LR_SHIFTKEY) viewmove(1);
2463                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
2464                                         else viewmove(0);
2465                                 }
2466                         }
2467                 }
2468                 if(event==ESCKEY || event==SPACEKEY) break;
2469                                 
2470                 if(mode==2 && CFRA==EFRA) break;        
2471         }
2472
2473         if(event==SPACEKEY);
2474         else CFRA= cfraont;
2475
2476         clear_all_constraints();
2477         do_all_ipos();
2478         do_all_keys();
2479         do_all_actions();
2480         do_all_ikas();
2481
2482         audiostream_stop();
2483
2484         if(oldsa!=curarea) areawinset(oldsa->win);
2485         
2486         /* restore all areas */
2487         sa= G.curscreen->areabase.first;
2488         while(sa) {
2489                 if( (mode && sa->spacetype==SPACE_VIEW3D) || sa==curarea) addqueue(sa->win, REDRAW, 1);
2490                 
2491                 sa= sa->next;   
2492         }
2493         
2494         /* speed button */
2495         // allqueue(REDRAWBUTSOBJECT, 0);
2496         /* groups could have changed ipo */
2497         allspace(REMAKEIPO, 0);
2498         allqueue(REDRAWIPO, 0);
2499         allqueue(REDRAWNLA, 0);
2500         allqueue (REDRAWACTION, 0);
2501         /* for the time being */
2502         update_for_newframe_muted();
2503 #ifdef NAN_LINEAR_PHYSICS       
2504         end_anim_sumo();
2505 #endif
2506         waitcursor(0);
2507         G.f &= ~G_PLAYANIM;
2508         
2509         if (event==ESCKEY || event==SPACEKEY) return 1;
2510         else return 0;
2511 }