4fc64782fe891c687723a441e6914ffc29c410e5
[blender.git] / source / blender / editors / space_view3d / view3d_draw.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL 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. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31 #include <math.h>
32
33 #include "DNA_action_types.h"
34 #include "DNA_armature_types.h"
35 #include "DNA_camera_types.h"
36 #include "DNA_customdata_types.h"
37 #include "DNA_group_types.h"
38 #include "DNA_key_types.h"
39 #include "DNA_object_types.h"
40 #include "DNA_space_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_screen_types.h"
43 #include "DNA_userdef_types.h"
44 #include "DNA_view3d_types.h"
45 #include "DNA_world_types.h"
46
47 #include "MEM_guardedalloc.h"
48
49 #include "BLI_blenlib.h"
50 #include "BLI_arithb.h"
51 #include "BLI_rand.h"
52
53 #include "BKE_anim.h"
54 #include "BKE_context.h"
55 #include "BKE_customdata.h"
56 #include "BKE_image.h"
57 #include "BKE_ipo.h"
58 #include "BKE_key.h"
59 #include "BKE_object.h"
60 #include "BKE_global.h"
61 #include "BKE_scene.h"
62 #include "BKE_screen.h"
63 #include "BKE_utildefines.h"
64
65 #include "RE_pipeline.h"        // make_stars
66
67 #include "IMB_imbuf_types.h"
68 #include "IMB_imbuf.h"
69
70 #include "BIF_gl.h"
71 #include "BIF_glutil.h"
72 #include "BMF_Api.h"
73
74 #include "WM_api.h"
75
76 #include "ED_armature.h"
77 #include "ED_keyframing.h"
78 #include "ED_mesh.h"
79 #include "ED_screen.h"
80 #include "ED_space_api.h"
81 #include "ED_util.h"
82 #include "ED_types.h"
83
84 #include "UI_interface.h"
85 #include "UI_interface_icons.h"
86 #include "UI_resources.h"
87 #include "UI_view2d.h"
88
89 #include "GPU_draw.h"
90 #include "GPU_material.h"
91
92 #include "view3d_intern.h"      // own include
93
94
95
96 static void star_stuff_init_func(void)
97 {
98         cpack(-1);
99         glPointSize(1.0);
100         glBegin(GL_POINTS);
101 }
102 static void star_stuff_vertex_func(float* i)
103 {
104         glVertex3fv(i);
105 }
106 static void star_stuff_term_func(void)
107 {
108         glEnd();
109 }
110
111 void circf(float x, float y, float rad)
112 {
113         GLUquadricObj *qobj = gluNewQuadric(); 
114         
115         gluQuadricDrawStyle(qobj, GLU_FILL); 
116         
117         glPushMatrix(); 
118         
119         glTranslatef(x,  y, 0.); 
120         
121         gluDisk( qobj, 0.0,  rad, 32, 1); 
122         
123         glPopMatrix(); 
124         
125         gluDeleteQuadric(qobj);
126 }
127
128 void circ(float x, float y, float rad)
129 {
130         GLUquadricObj *qobj = gluNewQuadric(); 
131         
132         gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
133         
134         glPushMatrix(); 
135         
136         glTranslatef(x,  y, 0.); 
137         
138         gluDisk( qobj, 0.0,  rad, 32, 1); 
139         
140         glPopMatrix(); 
141         
142         gluDeleteQuadric(qobj);
143 }
144
145
146 /* ********* custom clipping *********** */
147
148 static void view3d_draw_clipping(View3D *v3d)
149 {
150         BoundBox *bb= v3d->clipbb;
151         
152         UI_ThemeColorShade(TH_BACK, -8);
153         
154         glBegin(GL_QUADS);
155         
156         glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[3]);
157         glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[1]);
158         glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[5]);
159         glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[3]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[6]);
160         glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[2]);
161         glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[3]);
162         
163         glEnd();
164 }
165
166 void view3d_set_clipping(View3D *v3d)
167 {
168         double plane[4];
169         int a;
170         
171         for(a=0; a<4; a++) {
172                 QUATCOPY(plane, v3d->clip[a]);
173                 glClipPlane(GL_CLIP_PLANE0+a, plane);
174                 glEnable(GL_CLIP_PLANE0+a);
175         }
176 }
177
178 void view3d_clr_clipping(void)
179 {
180         int a;
181         
182         for(a=0; a<4; a++) {
183                 glDisable(GL_CLIP_PLANE0+a);
184         }
185 }
186
187 int view3d_test_clipping(View3D *v3d, float *vec)
188 {
189         /* vec in world coordinates, returns 1 if clipped */
190         float view[3];
191         
192         VECCOPY(view, vec);
193         
194         if(0.0f < v3d->clip[0][3] + INPR(view, v3d->clip[0]))
195                 if(0.0f < v3d->clip[1][3] + INPR(view, v3d->clip[1]))
196                         if(0.0f < v3d->clip[2][3] + INPR(view, v3d->clip[2]))
197                                 if(0.0f < v3d->clip[3][3] + INPR(view, v3d->clip[3]))
198                                         return 0;
199         
200         return 1;
201 }
202
203 /* ********* end custom clipping *********** */
204
205
206 static void drawgrid_draw(ARegion *ar, float wx, float wy, float x, float y, float dx)
207 {
208         float fx, fy;
209         
210         x+= (wx); 
211         y+= (wy);
212         fx= x/dx;
213         fx= x-dx*floor(fx);
214         
215         while(fx< ar->winx) {
216                 fdrawline(fx,  0.0,  fx,  (float)ar->winy); 
217                 fx+= dx; 
218         }
219
220         fy= y/dx;
221         fy= y-dx*floor(fy);
222         
223
224         while(fy< ar->winy) {
225                 fdrawline(0.0,  fy,  (float)ar->winx,  fy); 
226                 fy+= dx;
227         }
228
229 }
230
231 // not intern, called in editobject for constraint axis too
232 void make_axis_color(char *col, char *col2, char axis)
233 {
234         if(axis=='x') {
235                 col2[0]= col[0]>219?255:col[0]+36;
236                 col2[1]= col[1]<26?0:col[1]-26;
237                 col2[2]= col[2]<26?0:col[2]-26;
238         }
239         else if(axis=='y') {
240                 col2[0]= col[0]<46?0:col[0]-36;
241                 col2[1]= col[1]>189?255:col[1]+66;
242                 col2[2]= col[2]<46?0:col[2]-36; 
243         }
244         else {
245                 col2[0]= col[0]<26?0:col[0]-26; 
246                 col2[1]= col[1]<26?0:col[1]-26; 
247                 col2[2]= col[2]>209?255:col[2]+46;
248         }
249         
250 }
251
252 static void drawgrid(ARegion *ar, View3D *v3d)
253 {
254         /* extern short bgpicmode; */
255         float wx, wy, x, y, fw, fx, fy, dx;
256         float vec4[4];
257         char col[3], col2[3];
258         short sublines = v3d->gridsubdiv;
259         
260         vec4[0]=vec4[1]=vec4[2]=0.0; 
261         vec4[3]= 1.0;
262         Mat4MulVec4fl(v3d->persmat, vec4);
263         fx= vec4[0]; 
264         fy= vec4[1]; 
265         fw= vec4[3];
266
267         wx= (ar->winx/2.0);     /* because of rounding errors, grid at wrong location */
268         wy= (ar->winy/2.0);
269
270         x= (wx)*fx/fw;
271         y= (wy)*fy/fw;
272
273         vec4[0]=vec4[1]=v3d->grid; 
274         vec4[2]= 0.0;
275         vec4[3]= 1.0;
276         Mat4MulVec4fl(v3d->persmat, vec4);
277         fx= vec4[0]; 
278         fy= vec4[1]; 
279         fw= vec4[3];
280
281         dx= fabs(x-(wx)*fx/fw);
282         if(dx==0) dx= fabs(y-(wy)*fy/fw);
283         
284         glDepthMask(0);         // disable write in zbuffer
285
286         /* check zoom out */
287         UI_ThemeColor(TH_GRID);
288         
289         if(dx<6.0) {
290                 v3d->gridview*= sublines;
291                 dx*= sublines;
292                 
293                 if(dx<6.0) {    
294                         v3d->gridview*= sublines;
295                         dx*= sublines;
296                         
297                         if(dx<6.0) {
298                                 v3d->gridview*= sublines;
299                                 dx*=sublines;
300                                 if(dx<6.0);
301                                 else {
302                                         UI_ThemeColor(TH_GRID);
303                                         drawgrid_draw(ar, wx, wy, x, y, dx);
304                                 }
305                         }
306                         else {  // start blending out
307                                 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
308                                 drawgrid_draw(ar, wx, wy, x, y, dx);
309                         
310                                 UI_ThemeColor(TH_GRID);
311                                 drawgrid_draw(ar, wx, wy, x, y, sublines*dx);
312                         }
313                 }
314                 else {  // start blending out (6 < dx < 60)
315                         UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
316                         drawgrid_draw(ar, wx, wy, x, y, dx);
317                         
318                         UI_ThemeColor(TH_GRID);
319                         drawgrid_draw(ar, wx, wy, x, y, sublines*dx);
320                 }
321         }
322         else {
323                 if(dx>60.0) {           // start blending in
324                         v3d->gridview/= sublines;
325                         dx/= sublines;                  
326                         if(dx>60.0) {           // start blending in
327                                 v3d->gridview/= sublines;
328                                 dx/= sublines;
329                                 if(dx>60.0) {
330                                         UI_ThemeColor(TH_GRID);
331                                         drawgrid_draw(ar, wx, wy, x, y, dx);
332                                 }
333                                 else {
334                                         UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
335                                         drawgrid_draw(ar, wx, wy, x, y, dx);
336                                         UI_ThemeColor(TH_GRID);
337                                         drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
338                                 }
339                         }
340                         else {
341                                 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
342                                 drawgrid_draw(ar, wx, wy, x, y, dx);
343                                 UI_ThemeColor(TH_GRID);                         
344                                 drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
345                         }
346                 }
347                 else {
348                         UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
349                         drawgrid_draw(ar, wx, wy, x, y, dx);
350                         UI_ThemeColor(TH_GRID);
351                         drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
352                 }
353         }
354
355         x+= (wx); 
356         y+= (wy);
357         UI_GetThemeColor3ubv(TH_GRID, col);
358
359         setlinestyle(0);
360         
361         /* center cross */
362         if(v3d->view==3) make_axis_color(col, col2, 'y');
363         else make_axis_color(col, col2, 'x');
364         glColor3ubv((GLubyte *)col2);
365         
366         fdrawline(0.0,  y,  (float)ar->winx,  y); 
367         
368         if(v3d->view==7) make_axis_color(col, col2, 'y');
369         else make_axis_color(col, col2, 'z');
370         glColor3ubv((GLubyte *)col2);
371
372         fdrawline(x, 0.0, x, (float)ar->winy); 
373
374         glDepthMask(1);         // enable write in zbuffer
375 }
376
377
378 static void drawfloor(Scene *scene, View3D *v3d)
379 {
380         float vert[3], grid;
381         int a, gridlines, emphasise;
382         char col[3], col2[3];
383         short draw_line = 0;
384         
385         vert[2]= 0.0;
386         
387         if(v3d->gridlines<3) return;
388         
389         if(v3d->zbuf && scene->obedit) glDepthMask(0);  // for zbuffer-select
390         
391         gridlines= v3d->gridlines/2;
392         grid= gridlines*v3d->grid;
393         
394         UI_GetThemeColor3ubv(TH_GRID, col);
395         UI_GetThemeColor3ubv(TH_BACK, col2);
396         
397         /* emphasise division lines lighter instead of darker, if background is darker than grid */
398         if ( ((col[0]+col[1]+col[2])/3+10) > (col2[0]+col2[1]+col2[2])/3 )
399                 emphasise = 20;
400         else
401                 emphasise = -10;
402         
403         /* draw the Y axis and/or grid lines */
404         for(a= -gridlines;a<=gridlines;a++) {
405                 if(a==0) {
406                         /* check for the 'show Y axis' preference */
407                         if (v3d->gridflag & V3D_SHOW_Y) { 
408                                 make_axis_color(col, col2, 'y');
409                                 glColor3ubv((GLubyte *)col2);
410                                 
411                                 draw_line = 1;
412                         } else if (v3d->gridflag & V3D_SHOW_FLOOR) {
413                                 UI_ThemeColorShade(TH_GRID, emphasise);
414                         } else {
415                                 draw_line = 0;
416                         }
417                 } else {
418                         /* check for the 'show grid floor' preference */
419                         if (v3d->gridflag & V3D_SHOW_FLOOR) {
420                                 if( (a % 10)==0) {
421                                         UI_ThemeColorShade(TH_GRID, emphasise);
422                                 }
423                                 else UI_ThemeColorShade(TH_GRID, 10);
424                                 
425                                 draw_line = 1;
426                         } else {
427                                 draw_line = 0;
428                         }
429                 }
430                 
431                 if (draw_line) {
432                         glBegin(GL_LINE_STRIP);
433                 vert[0]= a*v3d->grid;
434                 vert[1]= grid;
435                 glVertex3fv(vert);
436                 vert[1]= -grid;
437                 glVertex3fv(vert);
438                         glEnd();
439                 }
440         }
441         
442         /* draw the X axis and/or grid lines */
443         for(a= -gridlines;a<=gridlines;a++) {
444                 if(a==0) {
445                         /* check for the 'show X axis' preference */
446                         if (v3d->gridflag & V3D_SHOW_X) { 
447                                 make_axis_color(col, col2, 'x');
448                                 glColor3ubv((GLubyte *)col2);
449                                 
450                                 draw_line = 1;
451                         } else if (v3d->gridflag & V3D_SHOW_FLOOR) {
452                                 UI_ThemeColorShade(TH_GRID, emphasise);
453                         } else {
454                                 draw_line = 0;
455                         }
456                 } else {
457                         /* check for the 'show grid floor' preference */
458                         if (v3d->gridflag & V3D_SHOW_FLOOR) {
459                                 if( (a % 10)==0) {
460                                         UI_ThemeColorShade(TH_GRID, emphasise);
461                                 }
462                                 else UI_ThemeColorShade(TH_GRID, 10);
463                                 
464                                 draw_line = 1;
465                         } else {
466                                 draw_line = 0;
467                         }
468                 }
469                 
470                 if (draw_line) {
471                         glBegin(GL_LINE_STRIP);
472                 vert[1]= a*v3d->grid;
473                 vert[0]= grid;
474                 glVertex3fv(vert );
475                 vert[0]= -grid;
476                 glVertex3fv(vert);
477                         glEnd();
478                 }
479         }
480         
481         /* draw the Z axis line */      
482         /* check for the 'show Z axis' preference */
483         if (v3d->gridflag & V3D_SHOW_Z) {
484                 make_axis_color(col, col2, 'z');
485                 glColor3ubv((GLubyte *)col2);
486                 
487                 glBegin(GL_LINE_STRIP);
488                 vert[0]= 0;
489                 vert[1]= 0;
490                 vert[2]= grid;
491                 glVertex3fv(vert );
492                 vert[2]= -grid;
493                 glVertex3fv(vert);
494                 glEnd();
495         }
496         
497         if(v3d->zbuf && scene->obedit) glDepthMask(1);  
498         
499 }
500
501 static void drawcursor(Scene *scene, ARegion *ar, View3D *v3d)
502 {
503         short mx,my,co[2];
504         int flag;
505         
506         /* we dont want the clipping for cursor */
507         flag= v3d->flag;
508         v3d->flag= 0;
509         project_short(ar, v3d, give_cursor(scene, v3d), co);
510         v3d->flag= flag;
511         
512         mx = co[0];
513         my = co[1];
514         
515         if(mx!=IS_CLIPPED) {
516                 setlinestyle(0); 
517                 cpack(0xFF);
518                 circ((float)mx, (float)my, 10.0);
519                 setlinestyle(4); 
520                 cpack(0xFFFFFF);
521                 circ((float)mx, (float)my, 10.0);
522                 setlinestyle(0);
523                 cpack(0x0);
524                 
525                 sdrawline(mx-20, my, mx-5, my);
526                 sdrawline(mx+5, my, mx+20, my);
527                 sdrawline(mx, my-20, mx, my-5);
528                 sdrawline(mx, my+5, mx, my+20);
529         }
530 }
531
532 /* Draw a live substitute of the view icon, which is always shown */
533 static void draw_view_axis(View3D *v3d)
534 {
535         const float k = U.rvisize;   /* axis size */
536         const float toll = 0.5;      /* used to see when view is quasi-orthogonal */
537         const float start = k + 1.0; /* axis center in screen coordinates, x=y */
538         float ydisp = 0.0;          /* vertical displacement to allow obj info text */
539         
540         /* rvibright ranges approx. from original axis icon color to gizmo color */
541         float bright = U.rvibright / 15.0f;
542         
543         unsigned char col[3];
544         unsigned char gridcol[3];
545         float colf[3];
546         
547         float vec[4];
548         float dx, dy;
549         float h, s, v;
550         
551         /* thickness of lines is proportional to k */
552         /*      (log(k)-1) gives a more suitable thickness, but fps decreased by about 3 fps */
553         glLineWidth(k / 10);
554         //glLineWidth(log(k)-1); // a bit slow
555         
556         UI_GetThemeColor3ubv(TH_GRID, (char *)gridcol);
557         
558         /* X */
559         vec[0] = vec[3] = 1;
560         vec[1] = vec[2] = 0;
561         QuatMulVecf(v3d->viewquat, vec);
562         
563         make_axis_color((char *)gridcol, (char *)col, 'x');
564         rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
565         s = s<0.5 ? s+0.5 : 1.0;
566         v = 0.3;
567         v = (v<1.0-(bright) ? v+bright : 1.0);
568         hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
569         glColor3fv(colf);
570         
571         dx = vec[0] * k;
572         dy = vec[1] * k;
573         fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
574         if (fabs(dx) > toll || fabs(dy) > toll) {
575                 glRasterPos2i(start + dx + 2, start + dy + ydisp + 2);
576                 BMF_DrawString(G.fonts, "x");
577         }
578         
579         /* Y */
580         vec[1] = vec[3] = 1;
581         vec[0] = vec[2] = 0;
582         QuatMulVecf(v3d->viewquat, vec);
583         
584         make_axis_color((char *)gridcol, (char *)col, 'y');
585         rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
586         s = s<0.5 ? s+0.5 : 1.0;
587         v = 0.3;
588         v = (v<1.0-(bright) ? v+bright : 1.0);
589         hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
590         glColor3fv(colf);
591         
592         dx = vec[0] * k;
593         dy = vec[1] * k;
594         fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
595         if (fabs(dx) > toll || fabs(dy) > toll) {
596                 glRasterPos2i(start + dx + 2, start + dy + ydisp + 2);
597                 BMF_DrawString(G.fonts, "y");
598         }
599         
600         /* Z */
601         vec[2] = vec[3] = 1;
602         vec[1] = vec[0] = 0;
603         QuatMulVecf(v3d->viewquat, vec);
604         
605         make_axis_color((char *)gridcol, (char *)col, 'z');
606         rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
607         s = s<0.5 ? s+0.5 : 1.0;
608         v = 0.5;
609         v = (v<1.0-(bright) ? v+bright : 1.0);
610         hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
611         glColor3fv(colf);
612         
613         dx = vec[0] * k;
614         dy = vec[1] * k;
615         fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
616         if (fabs(dx) > toll || fabs(dy) > toll) {
617                 glRasterPos2i(start + dx + 2, start + dy + ydisp + 2);
618                 BMF_DrawString(G.fonts, "z");
619         }
620         
621         /* restore line-width */
622         glLineWidth(1.0);
623 }
624
625
626 static void draw_view_icon(View3D *v3d)
627 {
628         BIFIconID icon;
629         
630         if(v3d->view==7) icon= ICON_AXIS_TOP;
631         else if(v3d->view==1) icon= ICON_AXIS_FRONT;
632         else if(v3d->view==3) icon= ICON_AXIS_SIDE;
633         else return ;
634         
635         glEnable(GL_BLEND);
636         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
637         
638         UI_icon_draw(5.0, 5.0, icon);
639         
640         glDisable(GL_BLEND);
641 }
642
643 char *view3d_get_name(View3D *v3d)
644 {
645         char *name = NULL;
646         
647         switch (v3d->view) {
648                 case 1:
649                         if (v3d->persp == V3D_ORTHO)
650                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Back Ortho" : "Front Ortho";
651                         else
652                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Back Persp" : "Front Persp";
653                         break;
654                 case 3:
655                         if (v3d->persp == V3D_ORTHO)
656                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Left Ortho" : "Right Ortho";
657                         else
658                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Left Persp" : "Right Persp";
659                         break;
660                 case 7:
661                         if (v3d->persp == V3D_ORTHO)
662                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Bottom Ortho" : "Top Ortho";
663                         else
664                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Bottom Persp" : "Top Persp";
665                         break;
666                 default:
667                         if (v3d->persp==V3D_CAMOB) {
668                                 if ((v3d->camera) && (v3d->camera->type == OB_CAMERA)) {
669                                         Camera *cam;
670                                         cam = v3d->camera->data;
671                                         name = (cam->type != CAM_ORTHO) ? "Camera Persp" : "Camera Ortho";
672                                 } else {
673                                         name = "Object as Camera";
674                                 }
675                         } else { 
676                                 name = (v3d->persp == V3D_ORTHO) ? "User Ortho" : "User Persp";
677                         }
678                         break;
679         }
680         
681         return name;
682 }
683
684 static void draw_viewport_name(ARegion *ar, View3D *v3d)
685 {
686         char *name = view3d_get_name(v3d);
687         char *printable = NULL;
688         
689         if (v3d->localview) {
690                 printable = malloc(strlen(name) + strlen(" (Local)_")); /* '_' gives space for '\0' */
691                                                                                                  strcpy(printable, name);
692                                                                                                  strcat(printable, " (Local)");
693         } else {
694                 printable = name;
695         }
696
697         if (printable) {
698                 UI_ThemeColor(TH_TEXT_HI);
699                 glRasterPos2i(10,  ar->winy-20);
700                 BMF_DrawString(G.fonts, printable);
701         }
702
703         if (v3d->localview) {
704                 free(printable);
705         }
706 }
707
708
709 static char *get_cfra_marker_name(Scene *scene)
710 {
711         ListBase *markers= &scene->markers;
712         TimeMarker *m1, *m2;
713         
714         /* search through markers for match */
715         for (m1=markers->first, m2=markers->last; m1 && m2; m1=m1->next, m2=m2->prev) {
716                 if (m1->frame==CFRA)
717                         return m1->name;
718                 
719                 if (m1 == m2)
720                         break;          
721                 
722                 if (m2->frame==CFRA)
723                         return m2->name;
724         }
725         
726         return NULL;
727 }
728
729 /* draw info beside axes in bottom left-corner: 
730 *       framenum, object name, bone name (if available), marker name (if available)
731 */
732 static void draw_selected_name(Scene *scene, Object *ob, View3D *v3d)
733 {
734         char info[256], *markern;
735         short offset=30;
736         
737         /* get name of marker on current frame (if available) */
738         markern= get_cfra_marker_name(scene);
739         
740         /* check if there is an object */
741         if(ob) {
742                 /* name(s) to display depends on type of object */
743                 if(ob->type==OB_ARMATURE) {
744                         bArmature *arm= ob->data;
745                         char *name= NULL;
746                         
747                         /* show name of active bone too (if possible) */
748                         if(arm->edbo) {
749                                 EditBone *ebo;
750                                 for (ebo=arm->edbo->first; ebo; ebo=ebo->next){
751                                         if ((ebo->flag & BONE_ACTIVE) && (ebo->layer & arm->layer)) {
752                                                 name= ebo->name;
753                                                 break;
754                                         }
755                                 }
756                         }
757                         else if(ob->pose && (ob->flag & OB_POSEMODE)) {
758                                 bPoseChannel *pchan;
759                                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
760                                         if((pchan->bone->flag & BONE_ACTIVE) && (pchan->bone->layer & arm->layer)) {
761                                                 name= pchan->name;
762                                                 break;
763                                         }
764                                 }
765                         }
766                         if(name && markern)
767                                 sprintf(info, "(%d) %s %s <%s>", CFRA, ob->id.name+2, name, markern);
768                         else if(name)
769                                 sprintf(info, "(%d) %s %s", CFRA, ob->id.name+2, name);
770                         else
771                                 sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
772                 }
773                 else if(ELEM3(ob->type, OB_MESH, OB_LATTICE, OB_CURVE)) {
774                         Key *key= NULL;
775                         KeyBlock *kb = NULL;
776                         char shapes[75];
777                         
778                         /* try to display active shapekey too */
779                         shapes[0] = 0;
780                         key = ob_get_key(ob);
781                         if(key){
782                                 kb = BLI_findlink(&key->block, ob->shapenr-1);
783                                 if(kb){
784                                         sprintf(shapes, ": %s ", kb->name);             
785                                         if(ob->shapeflag == OB_SHAPE_LOCK){
786                                                 sprintf(shapes, "%s (Pinned)",shapes);
787                                         }
788                                 }
789                         }
790                         
791                         if(markern)
792                                 sprintf(info, "(%d) %s %s <%s>", CFRA, ob->id.name+2, shapes, markern);
793                         else
794                                 sprintf(info, "(%d) %s %s", CFRA, ob->id.name+2, shapes);
795                 }
796                 else {
797                         /* standard object */
798                         if (markern)
799                                 sprintf(info, "(%d) %s <%s>", CFRA, ob->id.name+2, markern);
800                         else
801                                 sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
802                 }
803                 
804                 /* colour depends on whether there is a keyframe */
805         if (id_frame_has_keyframe((ID *)ob, frame_to_float(scene, CFRA), v3d->keyflags))
806                         UI_ThemeColor(TH_VERTEX_SELECT);
807                 else
808                         UI_ThemeColor(TH_TEXT_HI);
809         }
810         else {
811                 /* no object */
812                 if (markern)
813                         sprintf(info, "(%d) <%s>", CFRA, markern);
814                 else
815                         sprintf(info, "(%d)", CFRA);
816                 
817                 /* colour is always white */
818                 UI_ThemeColor(TH_TEXT_HI);
819         }
820         
821         if (U.uiflag & USER_SHOW_ROTVIEWICON)
822                 offset = 14 + (U.rvisize * 2);
823         
824         glRasterPos2i(offset,  10);
825         BMF_DrawString(G.fonts, info);
826 }
827
828 static void view3d_get_viewborder_size(Scene *scene, ARegion *ar, float size_r[2])
829 {
830         float winmax= MAX2(ar->winx, ar->winy);
831         float aspect= (float) (scene->r.xsch*scene->r.xasp)/(scene->r.ysch*scene->r.yasp);
832         
833         if(aspect>1.0) {
834                 size_r[0]= winmax;
835                 size_r[1]= winmax/aspect;
836         } else {
837                 size_r[0]= winmax*aspect;
838                 size_r[1]= winmax;
839         }
840 }
841
842 void calc_viewborder(Scene *scene, ARegion *ar, View3D *v3d, rctf *viewborder_r)
843 {
844         float zoomfac, size[2];
845         float dx= 0.0f, dy= 0.0f;
846         
847         view3d_get_viewborder_size(scene, ar, size);
848         
849         /* magic zoom calculation, no idea what
850                 * it signifies, if you find out, tell me! -zr
851                 */
852         /* simple, its magic dude!
853                 * well, to be honest, this gives a natural feeling zooming
854                 * with multiple keypad presses (ton)
855                 */
856         
857         zoomfac= (M_SQRT2 + v3d->camzoom/50.0);
858         zoomfac= (zoomfac*zoomfac)*0.25;
859         
860         size[0]= size[0]*zoomfac;
861         size[1]= size[1]*zoomfac;
862         
863         /* center in window */
864         viewborder_r->xmin= 0.5*ar->winx - 0.5*size[0];
865         viewborder_r->ymin= 0.5*ar->winy - 0.5*size[1];
866         viewborder_r->xmax= viewborder_r->xmin + size[0];
867         viewborder_r->ymax= viewborder_r->ymin + size[1];
868         
869         dx= ar->winx*v3d->camdx*zoomfac*2.0f;
870         dy= ar->winy*v3d->camdy*zoomfac*2.0f;
871         
872         /* apply offset */
873         viewborder_r->xmin-= dx;
874         viewborder_r->ymin-= dy;
875         viewborder_r->xmax-= dx;
876         viewborder_r->ymax-= dy;
877         
878         if(v3d->camera && v3d->camera->type==OB_CAMERA) {
879                 Camera *cam= v3d->camera->data;
880                 float w = viewborder_r->xmax - viewborder_r->xmin;
881                 float h = viewborder_r->ymax - viewborder_r->ymin;
882                 float side = MAX2(w, h);
883                 
884                 viewborder_r->xmin+= cam->shiftx*side;
885                 viewborder_r->xmax+= cam->shiftx*side;
886                 viewborder_r->ymin+= cam->shifty*side;
887                 viewborder_r->ymax+= cam->shifty*side;
888         }
889 }
890
891 void view3d_set_1_to_1_viewborder(Scene *scene, ARegion *ar, View3D *v3d)
892 {
893         float size[2];
894         int im_width= (scene->r.size*scene->r.xsch)/100;
895         
896         view3d_get_viewborder_size(scene, ar, size);
897         
898         v3d->camzoom= (sqrt(4.0*im_width/size[0]) - M_SQRT2)*50.0;
899         v3d->camzoom= CLAMPIS(v3d->camzoom, -30, 300);
900 }
901
902
903 static void drawviewborder_flymode(ARegion *ar) 
904 {
905         /* draws 4 edge brackets that frame the safe area where the
906         mouse can move during fly mode without spinning the view */
907         float x1, x2, y1, y2;
908         
909         x1= 0.45*(float)ar->winx;
910         y1= 0.45*(float)ar->winy;
911         x2= 0.55*(float)ar->winx;
912         y2= 0.55*(float)ar->winy;
913         cpack(0);
914         
915         
916         glBegin(GL_LINES);
917         /* bottom left */
918         glVertex2f(x1,y1); 
919         glVertex2f(x1,y1+5);
920         
921         glVertex2f(x1,y1); 
922         glVertex2f(x1+5,y1);
923         
924         /* top right */
925         glVertex2f(x2,y2); 
926         glVertex2f(x2,y2-5);
927         
928         glVertex2f(x2,y2); 
929         glVertex2f(x2-5,y2);
930         
931         /* top left */
932         glVertex2f(x1,y2); 
933         glVertex2f(x1,y2-5);
934         
935         glVertex2f(x1,y2); 
936         glVertex2f(x1+5,y2);
937         
938         /* bottom right */
939         glVertex2f(x2,y1); 
940         glVertex2f(x2,y1+5);
941         
942         glVertex2f(x2,y1); 
943         glVertex2f(x2-5,y1);
944         glEnd();        
945 }
946
947
948 static void drawviewborder(Scene *scene, ARegion *ar, View3D *v3d)
949 {
950         extern void gl_round_box(int mode, float minx, float miny, float maxx, float maxy, float rad);          // interface_panel.c
951         float fac, a;
952         float x1, x2, y1, y2;
953         float x3, y3, x4, y4;
954         rctf viewborder;
955         Camera *ca= NULL;
956         
957         if(v3d->camera==NULL)
958                 return;
959         if(v3d->camera->type==OB_CAMERA)
960                 ca = v3d->camera->data;
961         
962         calc_viewborder(scene, ar, v3d, &viewborder);
963         x1= viewborder.xmin;
964         y1= viewborder.ymin;
965         x2= viewborder.xmax;
966         y2= viewborder.ymax;
967         
968         /* passepartout, specified in camera edit buttons */
969         if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001) {
970                 if (ca->passepartalpha == 1.0) {
971                         glColor3f(0, 0, 0);
972                 } else {
973                         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
974                         glEnable(GL_BLEND);
975                         glColor4f(0, 0, 0, ca->passepartalpha);
976                 }
977                 if (x1 > 0.0)
978                         glRectf(0.0, (float)ar->winy, x1, 0.0);
979                 if (x2 < (float)ar->winx)
980                         glRectf(x2, (float)ar->winy, (float)ar->winx, 0.0);
981                 if (y2 < (float)ar->winy)
982                         glRectf(x1, (float)ar->winy, x2, y2);
983                 if (y2 > 0.0) 
984                         glRectf(x1, y1, x2, 0.0);
985                 
986                 glDisable(GL_BLEND);
987         }
988         
989         /* edge */
990         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
991         
992         setlinestyle(0);
993         UI_ThemeColor(TH_BACK);
994         glRectf(x1, y1, x2, y2);
995         
996         setlinestyle(3);
997         UI_ThemeColor(TH_WIRE);
998         glRectf(x1, y1, x2, y2);
999         
1000         /* camera name - draw in highlighted text color */
1001         if (ca && (ca->flag & CAM_SHOWNAME)) {
1002                 UI_ThemeColor(TH_TEXT_HI);
1003                 glRasterPos2f(x1, y1-15);
1004                 
1005                 BMF_DrawString(G.font, v3d->camera->id.name+2);
1006                 UI_ThemeColor(TH_WIRE);
1007         }
1008         
1009         
1010         /* border */
1011         if(scene->r.mode & R_BORDER) {
1012                 
1013                 cpack(0);
1014                 x3= x1+ scene->r.border.xmin*(x2-x1);
1015                 y3= y1+ scene->r.border.ymin*(y2-y1);
1016                 x4= x1+ scene->r.border.xmax*(x2-x1);
1017                 y4= y1+ scene->r.border.ymax*(y2-y1);
1018                 
1019                 cpack(0x4040FF);
1020                 glRectf(x3,  y3,  x4,  y4); 
1021         }
1022         
1023         /* safety border */
1024         if (ca && (ca->flag & CAM_SHOWTITLESAFE)) {
1025                 fac= 0.1;
1026                 
1027                 a= fac*(x2-x1);
1028                 x1+= a; 
1029                 x2-= a;
1030                 
1031                 a= fac*(y2-y1);
1032                 y1+= a;
1033                 y2-= a;
1034                 
1035                 UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1036                 
1037                 uiSetRoundBox(15);
1038                 gl_round_box(GL_LINE_LOOP, x1, y1, x2, y2, 12.0);
1039         }
1040         
1041         setlinestyle(0);
1042         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1043         
1044 }
1045
1046 /* *********************** backdraw for selection *************** */
1047
1048 void backdrawview3d(Scene *scene, ARegion *ar, View3D *v3d)
1049 {
1050         struct Base *base;
1051
1052 /*for 2.43 release, don't use glext and just define the constant.
1053   this to avoid possibly breaking platforms before release.*/
1054 #ifndef GL_MULTISAMPLE_ARB
1055         #define GL_MULTISAMPLE_ARB      0x809D
1056 #endif
1057
1058 #ifdef GL_MULTISAMPLE_ARB
1059         int m;
1060 #endif
1061
1062         if(G.f & G_VERTEXPAINT || G.f & G_WEIGHTPAINT || G.f & G_TEXTUREPAINT);
1063         else if(scene->obedit && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT));
1064         else {
1065                 v3d->flag &= ~V3D_NEEDBACKBUFDRAW;
1066                 return;
1067         }
1068
1069         if( !(v3d->flag & V3D_NEEDBACKBUFDRAW) ) return;
1070
1071 //      if(test) {
1072 //              if(qtest()) {
1073 //                      addafterqueue(ar->win, BACKBUFDRAW, 1);
1074 //                      return;
1075 //              }
1076 //      }
1077
1078         /* Disable FSAA for backbuffer selection.  
1079         
1080         Only works if GL_MULTISAMPLE_ARB is defined by the header
1081         file, which is should be for every OS that supports FSAA.*/
1082
1083 #ifdef GL_MULTISAMPLE_ARB
1084         m = glIsEnabled(GL_MULTISAMPLE_ARB);
1085         if (m) glDisable(GL_MULTISAMPLE_ARB);
1086 #endif
1087
1088         if(v3d->drawtype > OB_WIRE) v3d->zbuf= TRUE;
1089         
1090         glDisable(GL_DITHER);
1091
1092         glClearColor(0.0, 0.0, 0.0, 0.0); 
1093         if(v3d->zbuf) {
1094                 glEnable(GL_DEPTH_TEST);
1095                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1096         }
1097         else {
1098                 glClear(GL_COLOR_BUFFER_BIT);
1099                 glDisable(GL_DEPTH_TEST);
1100         }
1101         
1102         if(v3d->flag & V3D_CLIPPING)
1103                 view3d_set_clipping(v3d);
1104         
1105         G.f |= G_BACKBUFSEL;
1106         
1107         base= (scene->basact);
1108         if(base && (base->lay & v3d->lay)) {
1109                 draw_object_backbufsel(scene, v3d, base->object);
1110         }
1111
1112         v3d->flag &= ~V3D_NEEDBACKBUFDRAW;
1113
1114         G.f &= ~G_BACKBUFSEL;
1115         v3d->zbuf= FALSE; 
1116         glDisable(GL_DEPTH_TEST);
1117         glEnable(GL_DITHER);
1118
1119         if(v3d->flag & V3D_CLIPPING)
1120                 view3d_clr_clipping();
1121
1122 #ifdef GL_MULTISAMPLE_ARB
1123         if (m) glEnable(GL_MULTISAMPLE_ARB);
1124 #endif
1125
1126         /* it is important to end a view in a transform compatible with buttons */
1127 //      persp(PERSP_WIN);  // set ortho
1128
1129 }
1130
1131 void check_backbuf(ViewContext *vc)
1132 {
1133         if(vc->v3d->flag & V3D_NEEDBACKBUFDRAW)
1134                 backdrawview3d(vc->scene, vc->ar, vc->v3d);
1135 }
1136
1137 /* samples a single pixel (copied from vpaint) */
1138 unsigned int view3d_sample_backbuf(ViewContext *vc, int x, int y)
1139 {
1140         unsigned int col;
1141         
1142         if(x >= vc->ar->winx || y >= vc->ar->winy) return 0;
1143         x+= vc->ar->winrct.xmin;
1144         y+= vc->ar->winrct.ymin;
1145         
1146         check_backbuf(vc);
1147
1148         glReadPixels(x,  y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,  &col);
1149         glReadBuffer(GL_BACK);  
1150         
1151         if(ENDIAN_ORDER==B_ENDIAN) SWITCH_INT(col);
1152         
1153         return WM_framebuffer_to_index(col);
1154 }
1155
1156 /* reads full rect, converts indices */
1157 ImBuf *view3d_read_backbuf(ViewContext *vc, short xmin, short ymin, short xmax, short ymax)
1158 {
1159         unsigned int *dr, *rd;
1160         struct ImBuf *ibuf, *ibuf1;
1161         int a;
1162         short xminc, yminc, xmaxc, ymaxc, xs, ys;
1163         
1164         /* clip */
1165         if(xmin<0) xminc= 0; else xminc= xmin;
1166         if(xmax >= vc->ar->winx) xmaxc= vc->ar->winx-1; else xmaxc= xmax;
1167         if(xminc > xmaxc) return NULL;
1168
1169         if(ymin<0) yminc= 0; else yminc= ymin;
1170         if(ymax >= vc->ar->winy) ymaxc= vc->ar->winy-1; else ymaxc= ymax;
1171         if(yminc > ymaxc) return NULL;
1172         
1173         ibuf= IMB_allocImBuf((xmaxc-xminc+1), (ymaxc-yminc+1), 32, IB_rect,0);
1174
1175         check_backbuf(vc); 
1176         
1177         glReadPixels(vc->ar->winrct.xmin+xminc, vc->ar->winrct.ymin+yminc, (xmaxc-xminc+1), (ymaxc-yminc+1), GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
1178         glReadBuffer(GL_BACK);  
1179
1180         if(ENDIAN_ORDER==B_ENDIAN) IMB_convert_rgba_to_abgr(ibuf);
1181
1182         a= (xmaxc-xminc+1)*(ymaxc-yminc+1);
1183         dr= ibuf->rect;
1184         while(a--) {
1185                 if(*dr) *dr= WM_framebuffer_to_index(*dr);
1186                 dr++;
1187         }
1188         
1189         /* put clipped result back, if needed */
1190         if(xminc==xmin && xmaxc==xmax && yminc==ymin && ymaxc==ymax) 
1191                 return ibuf;
1192         
1193         ibuf1= IMB_allocImBuf( (xmax-xmin+1),(ymax-ymin+1),32,IB_rect,0);
1194         rd= ibuf->rect;
1195         dr= ibuf1->rect;
1196                 
1197         for(ys= ymin; ys<=ymax; ys++) {
1198                 for(xs= xmin; xs<=xmax; xs++, dr++) {
1199                         if( xs>=xminc && xs<=xmaxc && ys>=yminc && ys<=ymaxc) {
1200                                 *dr= *rd;
1201                                 rd++;
1202                         }
1203                 }
1204         }
1205         IMB_freeImBuf(ibuf);
1206         return ibuf1;
1207 }
1208
1209 /* smart function to sample a rect spiralling outside, nice for backbuf selection */
1210 unsigned int view3d_sample_backbuf_rect(ViewContext *vc, short mval[2], int size, unsigned int min, unsigned int max, int *dist, short strict, unsigned int (*indextest)(unsigned int index))
1211 {
1212         struct ImBuf *buf;
1213         unsigned int *bufmin, *bufmax, *tbuf;
1214         int minx, miny;
1215         int a, b, rc, nr, amount, dirvec[4][2];
1216         int distance=0;
1217         unsigned int index = 0;
1218         short indexok = 0;      
1219
1220         amount= (size-1)/2;
1221
1222         minx = mval[0]-(amount+1);
1223         miny = mval[1]-(amount+1);
1224         buf = view3d_read_backbuf(vc, minx, miny, minx+size-1, miny+size-1);
1225         if (!buf) return 0;
1226
1227         rc= 0;
1228         
1229         dirvec[0][0]= 1; dirvec[0][1]= 0;
1230         dirvec[1][0]= 0; dirvec[1][1]= -size;
1231         dirvec[2][0]= -1; dirvec[2][1]= 0;
1232         dirvec[3][0]= 0; dirvec[3][1]= size;
1233         
1234         bufmin = buf->rect;
1235         tbuf = buf->rect;
1236         bufmax = buf->rect + size*size;
1237         tbuf+= amount*size+ amount;
1238         
1239         for(nr=1; nr<=size; nr++) {
1240                 
1241                 for(a=0; a<2; a++) {
1242                         for(b=0; b<nr; b++, distance++) {
1243                                 if (*tbuf && *tbuf>=min && *tbuf<max) { //we got a hit
1244                                         if(strict){
1245                                                 indexok =  indextest(*tbuf - min+1);
1246                                                 if(indexok){
1247                                                         *dist= (short) sqrt( (float)distance   );
1248                                                         index = *tbuf - min+1;
1249                                                         goto exit; 
1250                                                 }                                               
1251                                         }
1252                                         else{
1253                                                 *dist= (short) sqrt( (float)distance ); // XXX, this distance is wrong - 
1254                                                 index = *tbuf - min+1; // messy yah, but indices start at 1
1255                                                 goto exit;
1256                                         }                       
1257                                 }
1258                                 
1259                                 tbuf+= (dirvec[rc][0]+dirvec[rc][1]);
1260                                 
1261                                 if(tbuf<bufmin || tbuf>=bufmax) {
1262                                         goto exit;
1263                                 }
1264                         }
1265                         rc++;
1266                         rc &= 3;
1267                 }
1268         }
1269
1270 exit:
1271         IMB_freeImBuf(buf);
1272         return index;
1273 }
1274
1275
1276 /* ************************************************************* */
1277
1278 static void draw_bgpic(Scene *scene, ARegion *ar, View3D *v3d)
1279 {
1280         BGpic *bgpic;
1281         Image *ima;
1282         ImBuf *ibuf= NULL;
1283         float vec[4], fac, asp, zoomx, zoomy;
1284         float x1, y1, x2, y2, cx, cy;
1285         
1286         bgpic= v3d->bgpic;
1287         if(bgpic==NULL) return;
1288         
1289         ima= bgpic->ima;
1290         
1291         if(ima)
1292                 ibuf= BKE_image_get_ibuf(ima, &bgpic->iuser);
1293         if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL) ) 
1294                 return;
1295         if(ibuf->channels!=4)
1296                 return;
1297         if(ibuf->rect==NULL)
1298                 IMB_rect_from_float(ibuf);
1299         
1300         if(v3d->persp==2) {
1301                 rctf vb;
1302                 
1303                 calc_viewborder(scene, ar, v3d, &vb);
1304                 
1305                 x1= vb.xmin;
1306                 y1= vb.ymin;
1307                 x2= vb.xmax;
1308                 y2= vb.ymax;
1309         }
1310         else {
1311                 float sco[2];
1312                 
1313                 /* calc window coord */
1314                 initgrabz(v3d, 0.0, 0.0, 0.0);
1315                 window_to_3d(ar, v3d, vec, 1, 0);
1316                 fac= MAX3( fabs(vec[0]), fabs(vec[1]), fabs(vec[1]) );
1317                 fac= 1.0/fac;
1318                 
1319                 asp= ( (float)ibuf->y)/(float)ibuf->x;
1320                 
1321                 vec[0] = vec[1] = vec[2] = 0.0;
1322                 view3d_project_float(ar, vec, sco, v3d->persmat);
1323                 cx = sco[0];
1324                 cy = sco[1];
1325                 
1326                 x1=  cx+ fac*(bgpic->xof-bgpic->size);
1327                 y1=  cy+ asp*fac*(bgpic->yof-bgpic->size);
1328                 x2=  cx+ fac*(bgpic->xof+bgpic->size);
1329                 y2=  cy+ asp*fac*(bgpic->yof+bgpic->size);
1330         }
1331         
1332         /* complete clip? */
1333         
1334         if(x2 < 0 ) return;
1335         if(y2 < 0 ) return;
1336         if(x1 > ar->winx ) return;
1337         if(y1 > ar->winy ) return;
1338         
1339         zoomx= (x2-x1)/ibuf->x;
1340         zoomy= (y2-y1)/ibuf->y;
1341         
1342         /* for some reason; zoomlevels down refuses to use GL_ALPHA_SCALE */
1343         if(zoomx < 1.0f || zoomy < 1.0f) {
1344                 float tzoom= MIN2(zoomx, zoomy);
1345                 int mip= 0;
1346                 
1347                 if(ibuf->mipmap[0]==NULL)
1348                         IMB_makemipmap(ibuf, 0);
1349                 
1350                 while(tzoom < 1.0f && mip<8 && ibuf->mipmap[mip]) {
1351                         tzoom*= 2.0f;
1352                         zoomx*= 2.0f;
1353                         zoomy*= 2.0f;
1354                         mip++;
1355                 }
1356                 if(mip>0)
1357                         ibuf= ibuf->mipmap[mip-1];
1358         }
1359         
1360         if(v3d->zbuf) glDisable(GL_DEPTH_TEST);
1361         
1362         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
1363         
1364         glMatrixMode(GL_PROJECTION);
1365         glPushMatrix();
1366         glMatrixMode(GL_MODELVIEW);
1367         glPushMatrix();
1368         
1369         glaDefine2DArea(&ar->winrct);
1370         
1371         glEnable(GL_BLEND);
1372         
1373         glPixelZoom(zoomx, zoomy);
1374         glColor4f(1.0, 1.0, 1.0, 1.0-bgpic->blend);
1375         glaDrawPixelsTex(x1, y1, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
1376         
1377         glPixelZoom(1.0, 1.0);
1378         glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
1379         
1380         glMatrixMode(GL_PROJECTION);
1381         glPopMatrix();
1382         glMatrixMode(GL_MODELVIEW);
1383         glPopMatrix();
1384         
1385         glDisable(GL_BLEND);
1386         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1387 }
1388
1389 /* ****************** View3d afterdraw *************** */
1390
1391 typedef struct View3DAfter {
1392         struct View3DAfter *next, *prev;
1393         struct Base *base;
1394         int type, flag;
1395 } View3DAfter;
1396
1397 /* temp storage of Objects that need to be drawn as last */
1398 void add_view3d_after(View3D *v3d, Base *base, int type, int flag)
1399 {
1400         View3DAfter *v3da= MEM_callocN(sizeof(View3DAfter), "View 3d after");
1401         
1402         BLI_addtail(&v3d->afterdraw, v3da);
1403         v3da->base= base;
1404         v3da->type= type;
1405         v3da->flag= flag;
1406 }
1407
1408 /* clears zbuffer and draws it over */
1409 static void view3d_draw_xray(Scene *scene, ARegion *ar, View3D *v3d, int clear)
1410 {
1411         View3DAfter *v3da, *next;
1412         int doit= 0;
1413         
1414         for(v3da= v3d->afterdraw.first; v3da; v3da= v3da->next)
1415                 if(v3da->type==V3D_XRAY) doit= 1;
1416         
1417         if(doit) {
1418                 if(clear && v3d->zbuf) glClear(GL_DEPTH_BUFFER_BIT);
1419                 v3d->xray= TRUE;
1420                 
1421                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1422                         next= v3da->next;
1423                         if(v3da->type==V3D_XRAY) {
1424                                 draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1425                                 BLI_remlink(&v3d->afterdraw, v3da);
1426                                 MEM_freeN(v3da);
1427                         }
1428                 }
1429                 v3d->xray= FALSE;
1430         }
1431 }
1432
1433 /* disables write in zbuffer and draws it over */
1434 static void view3d_draw_transp(Scene *scene, ARegion *ar, View3D *v3d)
1435 {
1436         View3DAfter *v3da, *next;
1437         
1438         glDepthMask(0);
1439         v3d->transp= TRUE;
1440         
1441         for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1442                 next= v3da->next;
1443                 if(v3da->type==V3D_TRANSP) {
1444                         draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1445                         BLI_remlink(&v3d->afterdraw, v3da);
1446                         MEM_freeN(v3da);
1447                 }
1448         }
1449         v3d->transp= FALSE;
1450         
1451         glDepthMask(1);
1452         
1453 }
1454
1455 /* *********************** */
1456
1457 /*
1458         In most cases call draw_dupli_objects,
1459         draw_dupli_objects_color was added because when drawing set dupli's
1460         we need to force the color
1461  */
1462 static void draw_dupli_objects_color(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int color)
1463 {       
1464         ListBase *lb;
1465         DupliObject *dob;
1466         Base tbase;
1467         BoundBox *bb= NULL;
1468         GLuint displist=0;
1469         short transflag, use_displist= -1;      /* -1 is initialize */
1470         char dt, dtx;
1471         
1472         if (base->object->restrictflag & OB_RESTRICT_VIEW) return;
1473         
1474         tbase.flag= OB_FROMDUPLI|base->flag;
1475         lb= object_duplilist(scene, base->object);
1476         
1477         for(dob= lb->first; dob; dob= dob->next) {
1478                 if(dob->no_draw);
1479                 else {
1480                         tbase.object= dob->ob;
1481                         
1482                         /* extra service: draw the duplicator in drawtype of parent */
1483                         /* MIN2 for the drawtype to allow bounding box objects in groups for lods */
1484                         dt= tbase.object->dt;   tbase.object->dt= MIN2(tbase.object->dt, base->object->dt);
1485                         dtx= tbase.object->dtx; tbase.object->dtx= base->object->dtx;
1486                         
1487                         /* negative scale flag has to propagate */
1488                         transflag= tbase.object->transflag;
1489                         if(base->object->transflag & OB_NEG_SCALE)
1490                                 tbase.object->transflag ^= OB_NEG_SCALE;
1491                         
1492                         UI_ThemeColorBlend(color, TH_BACK, 0.5);
1493                         
1494                         /* generate displist, test for new object */
1495                         if(use_displist==1 && dob->prev && dob->prev->ob!=dob->ob) {
1496                                 use_displist= -1;
1497                                 glDeleteLists(displist, 1);
1498                         }
1499                         /* generate displist */
1500                         if(use_displist == -1) {
1501                                 
1502                                 /* lamp drawing messes with matrices, could be handled smarter... but this works */
1503                                 if(dob->ob->type==OB_LAMP || dob->type==OB_DUPLIGROUP)
1504                                         use_displist= 0;
1505                                 else {
1506                                         /* disable boundbox check for list creation */
1507                                         object_boundbox_flag(dob->ob, OB_BB_DISABLED, 1);
1508                                         /* need this for next part of code */
1509                                         bb= object_get_boundbox(dob->ob);
1510                                         
1511                                         Mat4One(dob->ob->obmat);        /* obmat gets restored */
1512                                         
1513                                         displist= glGenLists(1);
1514                                         glNewList(displist, GL_COMPILE);
1515                                         draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
1516                                         glEndList();
1517                                         
1518                                         use_displist= 1;
1519                                         object_boundbox_flag(dob->ob, OB_BB_DISABLED, 0);
1520                                 }
1521                         }
1522                         if(use_displist) {
1523                                 wmMultMatrix(dob->mat);
1524                                 if(boundbox_clip(v3d, dob->mat, bb))
1525                                         glCallList(displist);
1526                                 wmLoadMatrix(v3d->viewmat);
1527                         }
1528                         else {
1529                                 Mat4CpyMat4(dob->ob->obmat, dob->mat);
1530                                 draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
1531                         }
1532                         
1533                         tbase.object->dt= dt;
1534                         tbase.object->dtx= dtx;
1535                         tbase.object->transflag= transflag;
1536                 }
1537         }
1538         
1539         /* Transp afterdraw disabled, afterdraw only stores base pointers, and duplis can be same obj */
1540         
1541         free_object_duplilist(lb);      /* does restore */
1542         
1543         if(use_displist)
1544                 glDeleteLists(displist, 1);
1545 }
1546
1547 static void draw_dupli_objects(Scene *scene, ARegion *ar, View3D *v3d, Base *base)
1548 {
1549         /* define the color here so draw_dupli_objects_color can be called
1550         * from the set loop */
1551         
1552         int color= (base->flag & SELECT)?TH_SELECT:TH_WIRE;
1553         /* debug */
1554         if(base->object->dup_group && base->object->dup_group->id.us<1)
1555                 color= TH_REDALERT;
1556         
1557         draw_dupli_objects_color(scene, ar, v3d, base, color);
1558 }
1559
1560
1561 void view3d_update_depths(ARegion *ar, View3D *v3d)
1562 {
1563         /* Create storage for, and, if necessary, copy depth buffer */
1564         if(!v3d->depths) v3d->depths= MEM_callocN(sizeof(ViewDepths),"ViewDepths");
1565         if(v3d->depths) {
1566                 ViewDepths *d= v3d->depths;
1567                 if(d->w != ar->winx ||
1568                    d->h != ar->winy ||
1569                    !d->depths) {
1570                         d->w= ar->winx;
1571                         d->h= ar->winy;
1572                         if(d->depths)
1573                                 MEM_freeN(d->depths);
1574                         d->depths= MEM_mallocN(sizeof(float)*d->w*d->h,"View depths");
1575                         d->damaged= 1;
1576                 }
1577                 
1578                 if(d->damaged) {
1579                         glReadPixels(ar->winrct.xmin,ar->winrct.ymin,d->w,d->h,
1580                                                  GL_DEPTH_COMPONENT,GL_FLOAT, d->depths);
1581                         
1582                         glGetDoublev(GL_DEPTH_RANGE,d->depth_range);
1583                         
1584                         d->damaged= 0;
1585                 }
1586         }
1587 }
1588
1589 /* Enable sculpting in wireframe mode by drawing sculpt object only to the depth buffer */
1590 static void draw_sculpt_depths(Scene *scene, ARegion *ar, View3D *v3d)
1591 {
1592         Object *ob = OBACT;
1593         
1594         int dt= MIN2(v3d->drawtype, ob->dt);
1595         if(v3d->zbuf==0 && dt>OB_WIRE)
1596                 dt= OB_WIRE;
1597         if(dt == OB_WIRE) {
1598                 GLboolean depth_on;
1599                 int orig_vdt = v3d->drawtype;
1600                 int orig_zbuf = v3d->zbuf;
1601                 int orig_odt = ob->dt;
1602                 
1603                 glGetBooleanv(GL_DEPTH_TEST, &depth_on);
1604                 v3d->drawtype = ob->dt = OB_SOLID;
1605                 v3d->zbuf = 1;
1606                 
1607                 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1608                 glEnable(GL_DEPTH_TEST);
1609                 draw_object(scene, ar, v3d, BASACT, 0);
1610                 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1611                 if(!depth_on)
1612                         glDisable(GL_DEPTH_TEST);
1613                 
1614                 v3d->drawtype = orig_vdt;
1615                 v3d->zbuf = orig_zbuf;
1616                 ob->dt = orig_odt;
1617         }
1618 }
1619
1620 void draw_depth(Scene *scene, ARegion *ar, View3D *v3d, int (* func)(void *))
1621 {
1622         Base *base;
1623         Scene *sce;
1624         short zbuf, flag;
1625         float glalphaclip;
1626         /* temp set drawtype to solid */
1627         
1628         /* Setting these temporarily is not nice */
1629         zbuf = v3d->zbuf;
1630         flag = v3d->flag;
1631         glalphaclip = U.glalphaclip;
1632         
1633         U.glalphaclip = 0.5; /* not that nice but means we wont zoom into billboards */
1634         v3d->flag &= ~V3D_SELECT_OUTLINE;
1635         
1636         setwinmatrixview3d(v3d, ar->winx, ar->winy, NULL);      /* 0= no pick rect */
1637         setviewmatrixview3d(scene, v3d);        /* note: calls where_is_object for camera... */
1638         
1639         Mat4MulMat4(v3d->persmat, v3d->viewmat, v3d->winmat);
1640         Mat4Invert(v3d->persinv, v3d->persmat);
1641         Mat4Invert(v3d->viewinv, v3d->viewmat);
1642         
1643         glClear(GL_DEPTH_BUFFER_BIT);
1644         
1645         wmLoadMatrix(v3d->viewmat);
1646 //      persp(PERSP_STORE);  // store correct view for persp(PERSP_VIEW) calls
1647         
1648         if(v3d->flag & V3D_CLIPPING) {
1649                 view3d_set_clipping(v3d);
1650         }
1651         
1652         v3d->zbuf= TRUE;
1653         glEnable(GL_DEPTH_TEST);
1654         
1655         /* draw set first */
1656         if(scene->set) {
1657                 for(SETLOOPER(scene->set, base)) {
1658                         if(v3d->lay & base->lay) {
1659                                 if (func == NULL || func(base)) {
1660                                         draw_object(scene, ar, v3d, base, 0);
1661                                         if(base->object->transflag & OB_DUPLI) {
1662                                                 draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
1663                                         }
1664                                 }
1665                         }
1666                 }
1667         }
1668         
1669         for(base= scene->base.first; base; base= base->next) {
1670                 if(v3d->lay & base->lay) {
1671                         if (func == NULL || func(base)) {
1672                                 /* dupli drawing */
1673                                 if(base->object->transflag & OB_DUPLI) {
1674                                         draw_dupli_objects(scene, ar, v3d, base);
1675                                 }
1676                                 draw_object(scene, ar, v3d, base, 0);
1677                         }
1678                 }
1679         }
1680         
1681         /* this isnt that nice, draw xray objects as if they are normal */
1682         if (v3d->afterdraw.first) {
1683                 View3DAfter *v3da, *next;
1684                 int num = 0;
1685                 v3d->xray= TRUE;
1686                 
1687                 glDepthFunc(GL_ALWAYS); /* always write into the depth bufer, overwriting front z values */
1688                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1689                         next= v3da->next;
1690                         if(v3da->type==V3D_XRAY) {
1691                                 draw_object(scene, ar, v3d, v3da->base, 0);
1692                                 num++;
1693                         }
1694                         /* dont remove this time */
1695                 }
1696                 v3d->xray= FALSE;
1697                 
1698                 glDepthFunc(GL_LEQUAL); /* Now write the depth buffer normally */
1699                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1700                         next= v3da->next;
1701                         if(v3da->type==V3D_XRAY) {
1702                                 v3d->xray= TRUE; v3d->transp= FALSE;  
1703                         } else if (v3da->type==V3D_TRANSP) {
1704                                 v3d->xray= FALSE; v3d->transp= TRUE;
1705                         }
1706                         
1707                         draw_object(scene, ar, v3d, v3da->base, 0); /* Draw Xray or Transp objects normally */
1708                         BLI_remlink(&v3d->afterdraw, v3da);
1709                         MEM_freeN(v3da);
1710                 }
1711                 v3d->xray= FALSE;
1712                 v3d->transp= FALSE;
1713         }
1714         
1715         v3d->zbuf = zbuf;
1716         U.glalphaclip = glalphaclip;
1717         v3d->flag = flag;
1718 }
1719
1720 typedef struct View3DShadow {
1721         struct View3DShadow *next, *prev;
1722         GPULamp *lamp;
1723 } View3DShadow;
1724
1725 static void gpu_render_lamp_update(Scene *scene, View3D *v3d, Object *ob, Object *par, float obmat[][4], ListBase *shadows)
1726 {
1727         GPULamp *lamp;
1728         View3DShadow *shadow;
1729         
1730         lamp = GPU_lamp_from_blender(scene, ob, par);
1731         
1732         if(lamp) {
1733                 GPU_lamp_update(lamp, ob->lay, obmat);
1734                 
1735                 if((ob->lay & v3d->lay) && GPU_lamp_has_shadow_buffer(lamp)) {
1736                         shadow= MEM_callocN(sizeof(View3DShadow), "View3DShadow");
1737                         shadow->lamp = lamp;
1738                         BLI_addtail(shadows, shadow);
1739                 }
1740         }
1741 }
1742
1743 static void gpu_update_lamps_shadows(Scene *scene, View3D *v3d)
1744 {
1745         ListBase shadows;
1746         View3DShadow *shadow;
1747         Scene *sce;
1748         Base *base;
1749         Object *ob;
1750         
1751         shadows.first= shadows.last= NULL;
1752         
1753         /* update lamp transform and gather shadow lamps */
1754         for(SETLOOPER(scene, base)) {
1755                 ob= base->object;
1756                 
1757                 if(ob->type == OB_LAMP)
1758                         gpu_render_lamp_update(scene, v3d, ob, NULL, ob->obmat, &shadows);
1759                 
1760                 if (ob->transflag & OB_DUPLI) {
1761                         DupliObject *dob;
1762                         ListBase *lb = object_duplilist(scene, ob);
1763                         
1764                         for(dob=lb->first; dob; dob=dob->next)
1765                                 if(dob->ob->type==OB_LAMP)
1766                                         gpu_render_lamp_update(scene, v3d, dob->ob, ob, dob->mat, &shadows);
1767                         
1768                         free_object_duplilist(lb);
1769                 }
1770         }
1771         
1772         /* render shadows after updating all lamps, nested object_duplilist
1773                 * don't work correct since it's replacing object matrices */
1774         for(shadow=shadows.first; shadow; shadow=shadow->next) {
1775                 /* this needs to be done better .. */
1776                 float viewmat[4][4], winmat[4][4];
1777                 int drawtype, lay, winsize, flag2;
1778                 
1779                 drawtype= v3d->drawtype;
1780                 lay= v3d->lay;
1781                 flag2= v3d->flag2 & V3D_SOLID_TEX;
1782                 
1783                 v3d->drawtype = OB_SOLID;
1784                 v3d->lay &= GPU_lamp_shadow_layer(shadow->lamp);
1785                 v3d->flag2 &= ~V3D_SOLID_TEX;
1786                 
1787                 GPU_lamp_shadow_buffer_bind(shadow->lamp, viewmat, &winsize, winmat);
1788 // XXX          drawview3d_render(v3d, viewmat, winsize, winsize, winmat, 1);
1789                 GPU_lamp_shadow_buffer_unbind(shadow->lamp);
1790                 
1791                 v3d->drawtype= drawtype;
1792                 v3d->lay= lay;
1793                 v3d->flag2 |= flag2;
1794         }
1795         
1796         BLI_freelistN(&shadows);
1797 }
1798
1799 /* *********************** customdata **************** */
1800
1801 /* goes over all modes and view3d settings */
1802 static CustomDataMask get_viewedit_datamask(bScreen *screen)
1803 {
1804         CustomDataMask mask = CD_MASK_BAREMESH;
1805         ScrArea *sa;
1806         
1807         /* check if we need tfaces & mcols due to face select or texture paint */
1808         if(FACESEL_PAINT_TEST || G.f & G_TEXTUREPAINT)
1809                 mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
1810         
1811         /* check if we need tfaces & mcols due to view mode */
1812         for(sa = screen->areabase.first; sa; sa = sa->next) {
1813                 if(sa->spacetype == SPACE_VIEW3D) {
1814                         View3D *view = sa->spacedata.first;
1815                         if(view->drawtype == OB_SHADED) {
1816                                 /* this includes normals for mesh_create_shadedColors */
1817                                 mask |= CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_NORMAL | CD_MASK_ORCO;
1818                         }
1819                         if((view->drawtype == OB_TEXTURE) || ((view->drawtype == OB_SOLID) && (view->flag2 & V3D_SOLID_TEX))) {
1820                                 mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
1821                                 
1822                                 if((G.fileflags & G_FILE_GAME_MAT) &&
1823                                    (G.fileflags & G_FILE_GAME_MAT_GLSL)) {
1824                                         mask |= CD_MASK_ORCO;
1825                                 }
1826                         }
1827                 }
1828         }
1829         
1830         /* check if we need mcols due to vertex paint or weightpaint */
1831         if(G.f & G_VERTEXPAINT || G.f & G_WEIGHTPAINT)
1832                 mask |= CD_MASK_MCOL;
1833         
1834         if(G.f & G_SCULPTMODE)
1835                 mask |= CD_MASK_MDISPS;
1836         
1837         return mask;
1838 }
1839
1840 void view3d_main_area_draw(const bContext *C, ARegion *ar)
1841 {
1842         Scene *scene= CTX_data_scene(C);
1843         ScrArea *sa= CTX_wm_area(C);
1844         View3D *v3d= sa->spacedata.first;       /* XXX get from region */
1845         Scene *sce;
1846         Base *base;
1847         Object *ob;
1848         char retopo= 0, sculptparticle= 0;
1849         Object *obact = OBACT;
1850         
1851         /* from now on all object derived meshes check this */
1852         v3d->customdata_mask= get_viewedit_datamask(CTX_wm_screen(C));
1853         
1854         /* update all objects, ipos, matrices, displists, etc. Flags set by depgraph or manual, 
1855                 no layer check here, gets correct flushed */
1856         /* sets first, we allow per definition current scene to have dependencies on sets */
1857         if(scene->set) {
1858                 for(SETLOOPER(scene->set, base))
1859                         object_handle_update(scene, base->object);   // bke_object.h
1860         }
1861         
1862         v3d->lay_used = 0;
1863         for(base= scene->base.first; base; base= base->next) {
1864                 object_handle_update(scene, base->object);   // bke_object.h
1865                 v3d->lay_used |= base->lay;
1866         }
1867         
1868         /* shadow buffers, before we setup matrices */
1869         if(draw_glsl_material(scene, NULL, v3d, v3d->drawtype))
1870                 gpu_update_lamps_shadows(scene, v3d);
1871         
1872         setwinmatrixview3d(v3d, ar->winx, ar->winy, NULL);      /* 0= no pick rect */
1873         setviewmatrixview3d(scene, v3d);        /* note: calls where_is_object for camera... */
1874         
1875         Mat4MulMat4(v3d->persmat, v3d->viewmat, v3d->winmat);
1876         Mat4Invert(v3d->persinv, v3d->persmat);
1877         Mat4Invert(v3d->viewinv, v3d->viewmat);
1878         
1879         /* calculate pixelsize factor once, is used for lamps and obcenters */
1880         {
1881                 float len1, len2, vec[3];
1882                 
1883                 VECCOPY(vec, v3d->persinv[0]);
1884                 len1= Normalize(vec);
1885                 VECCOPY(vec, v3d->persinv[1]);
1886                 len2= Normalize(vec);
1887                 
1888                 v3d->pixsize= 2.0f*(len1>len2?len1:len2);
1889                 
1890                 /* correct for window size */
1891                 if(ar->winx > ar->winy) v3d->pixsize/= (float)ar->winx;
1892                 else v3d->pixsize/= (float)ar->winy;
1893         }
1894         
1895         if(v3d->drawtype > OB_WIRE) {
1896                 float col[3];
1897                 UI_GetThemeColor3fv(TH_BACK, col);
1898                 glClearColor(col[0], col[1], col[2], 0.0); 
1899                 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
1900                 
1901                 glLoadIdentity();
1902         }
1903         else {
1904                 float col[3];
1905                 UI_GetThemeColor3fv(TH_BACK, col);
1906                 glClearColor(col[0], col[1], col[2], 0.0);
1907                 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
1908         }
1909         
1910         wmLoadMatrix(v3d->viewmat);
1911         
1912         if(v3d->flag & V3D_CLIPPING)
1913                 view3d_draw_clipping(v3d);
1914         
1915         /* set zbuffer after we draw clipping region */
1916         if(v3d->drawtype > OB_WIRE) {
1917                 v3d->zbuf= TRUE;
1918                 glEnable(GL_DEPTH_TEST);
1919         }
1920         
1921         // needs to be done always, gridview is adjusted in drawgrid() now
1922         v3d->gridview= v3d->grid;
1923         
1924         if(v3d->view==0 || v3d->persp!=0) {
1925                 drawfloor(scene, v3d);
1926                 if(v3d->persp==2) {
1927                         if(scene->world) {
1928                                 if(scene->world->mode & WO_STARS) {
1929                                         RE_make_stars(NULL, scene, star_stuff_init_func, star_stuff_vertex_func,
1930                                                                   star_stuff_term_func);
1931                                 }
1932                         }
1933                         if(v3d->flag & V3D_DISPBGPIC) draw_bgpic(scene, ar, v3d);
1934                 }
1935         }
1936         else {
1937                 ED_region_pixelspace(ar);
1938                 drawgrid(ar, v3d);
1939                 /* XXX make function? replaces persp(1) */
1940                 glMatrixMode(GL_PROJECTION);
1941                 wmLoadMatrix(v3d->winmat);
1942                 glMatrixMode(GL_MODELVIEW);
1943                 wmLoadMatrix(v3d->viewmat);
1944                 
1945                 if(v3d->flag & V3D_DISPBGPIC) {
1946                         draw_bgpic(scene, ar, v3d);
1947                 }
1948         }
1949         
1950         if(v3d->flag & V3D_CLIPPING)
1951                 view3d_set_clipping(v3d);
1952         
1953         /* draw set first */
1954         if(scene->set) {
1955                 for(SETLOOPER(scene->set, base)) {
1956                         
1957                         if(v3d->lay & base->lay) {
1958                                 
1959                                 UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
1960                                 draw_object(scene, ar, v3d, base, DRAW_CONSTCOLOR|DRAW_SCENESET);
1961                                 
1962                                 if(base->object->transflag & OB_DUPLI) {
1963                                         draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
1964                                 }
1965                         }
1966                 }
1967                 
1968                 /* Transp and X-ray afterdraw stuff for sets is done later */
1969         }
1970         
1971         /* then draw not selected and the duplis, but skip editmode object */
1972         for(base= scene->base.first; base; base= base->next) {
1973                 if(v3d->lay & base->lay) {
1974                         
1975                         /* dupli drawing */
1976                         if(base->object->transflag & OB_DUPLI) {
1977                                 draw_dupli_objects(scene, ar, v3d, base);
1978                         }
1979                         if((base->flag & SELECT)==0) {
1980                                 if(base->object!=scene->obedit) 
1981                                         draw_object(scene, ar, v3d, base, 0);
1982                         }
1983                 }
1984         }
1985         
1986 //      retopo= retopo_mesh_check() || retopo_curve_check();
1987 //      sculptparticle= (G.f & (G_SCULPTMODE|G_PARTICLEEDIT)) && !obedit;
1988         if(retopo)
1989                 view3d_update_depths(ar, v3d);
1990         
1991         /* draw selected and editmode */
1992         for(base= scene->base.first; base; base= base->next) {
1993                 if(v3d->lay & base->lay) {
1994                         if (base->object==scene->obedit || ( base->flag & SELECT) ) 
1995                                 draw_object(scene, ar, v3d, base, 0);
1996                 }
1997         }
1998         
1999         if(!retopo && sculptparticle && !(obact && (obact->dtx & OB_DRAWXRAY))) {
2000                 if(G.f & G_SCULPTMODE)
2001                         draw_sculpt_depths(scene, ar, v3d);
2002                 view3d_update_depths(ar, v3d);
2003         }
2004         
2005         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST);
2006         
2007 //      REEB_draw();
2008         
2009 //      if(scene->radio) RAD_drawall(v3d->drawtype>=OB_SOLID);
2010         
2011         /* Transp and X-ray afterdraw stuff */
2012         view3d_draw_transp(scene, ar, v3d);
2013         view3d_draw_xray(scene, ar, v3d, 1);    // clears zbuffer if it is used!
2014         
2015         if(!retopo && sculptparticle && (obact && (OBACT->dtx & OB_DRAWXRAY))) {
2016                 if(G.f & G_SCULPTMODE)
2017                         draw_sculpt_depths(scene, ar, v3d);
2018                 view3d_update_depths(ar, v3d);
2019         }
2020         
2021         if(v3d->flag & V3D_CLIPPING)
2022                 view3d_clr_clipping();
2023         
2024 //      BIF_draw_manipulator(ar);
2025         
2026         if(v3d->zbuf) {
2027                 v3d->zbuf= FALSE;
2028                 glDisable(GL_DEPTH_TEST);
2029         }
2030         
2031         /* draw grease-pencil stuff */
2032 //      if (v3d->flag2 & V3D_DISPGP)
2033 //              draw_gpencil_3dview(ar, 1);
2034         
2035         ED_region_pixelspace(ar);
2036         
2037         /* Draw Sculpt Mode brush XXX (removed) */
2038         
2039 //      retopo_paint_view_update(v3d);
2040 //      retopo_draw_paint_lines();
2041         
2042         /* Draw particle edit brush XXX (removed) */
2043         
2044         if(v3d->persp>1) drawviewborder(scene, ar, v3d);
2045         if(v3d->flag2 & V3D_FLYMODE) drawviewborder_flymode(ar);
2046         
2047         /* draw grease-pencil stuff */
2048 //      if (v3d->flag2 & V3D_DISPGP)
2049 //              draw_gpencil_3dview(ar, 0);
2050
2051         if(!(G.f & G_PLAYANIM)) drawcursor(scene, ar, v3d);
2052         if(U.uiflag & USER_SHOW_ROTVIEWICON)
2053                 draw_view_axis(v3d);
2054         else    
2055                 draw_view_icon(v3d);
2056         
2057         /* XXX removed viewport fps */
2058         if(U.uiflag & USER_SHOW_VIEWPORTNAME) {
2059                 draw_viewport_name(ar, v3d);
2060         }
2061         
2062         ob= OBACT;
2063         if(U.uiflag & USER_DRAWVIEWINFO) 
2064                 draw_selected_name(scene, ob, v3d);
2065         
2066         /* XXX here was the blockhandlers for floating panels */
2067
2068         if(G.f & G_VERTEXPAINT || G.f & G_WEIGHTPAINT || G.f & G_TEXTUREPAINT) {
2069                 v3d->flag |= V3D_NEEDBACKBUFDRAW;
2070                 // XXX addafterqueue(ar->win, BACKBUFDRAW, 1);
2071         }
2072         // test for backbuf select
2073         if(scene->obedit && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT)) {
2074                 
2075                 v3d->flag |= V3D_NEEDBACKBUFDRAW;
2076                 // XXX if(afterqtest(ar->win, BACKBUFDRAW)==0) {
2077                 //      addafterqueue(ar->win, BACKBUFDRAW, 1);
2078                 //}
2079         }
2080         
2081 #ifndef DISABLE_PYTHON
2082         /* XXX here was scriptlink */
2083 #endif  
2084 }
2085
2086