357b6e584e9b0b64d561f47635049e9e77f47394
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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_armature_types.h"
34 #include "DNA_camera_types.h"
35 #include "DNA_customdata_types.h"
36 #include "DNA_group_types.h"
37 #include "DNA_key_types.h"
38 #include "DNA_lamp_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_world_types.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_math.h"
46 #include "BLI_rand.h"
47
48 #include "BKE_anim.h"
49 #include "BKE_context.h"
50 #include "BKE_customdata.h"
51 #include "BKE_image.h"
52 #include "BKE_ipo.h"
53 #include "BKE_key.h"
54 #include "BKE_object.h"
55 #include "BKE_global.h"
56 #include "BKE_paint.h"
57 #include "BKE_scene.h"
58 #include "BKE_screen.h"
59 #include "BKE_utildefines.h"
60 #include "BKE_unit.h"
61
62 #include "RE_pipeline.h"        // make_stars
63
64 #include "IMB_imbuf_types.h"
65 #include "IMB_imbuf.h"
66
67 #include "BIF_gl.h"
68 #include "BIF_glutil.h"
69
70 #include "WM_api.h"
71 #include "BLF_api.h"
72
73 #include "ED_armature.h"
74 #include "ED_keyframing.h"
75 #include "ED_gpencil.h"
76 #include "ED_screen.h"
77 #include "ED_space_api.h"
78 #include "ED_screen_types.h"
79 #include "ED_transform.h"
80 #include "ED_gpencil.h"
81
82 #include "UI_interface.h"
83 #include "UI_interface_icons.h"
84 #include "UI_resources.h"
85
86 #include "GPU_draw.h"
87 #include "GPU_material.h"
88 #include "GPU_extensions.h"
89
90 #include "view3d_intern.h"      // own include
91
92
93
94 static void star_stuff_init_func(void)
95 {
96         cpack(-1);
97         glPointSize(1.0);
98         glBegin(GL_POINTS);
99 }
100 static void star_stuff_vertex_func(float* i)
101 {
102         glVertex3fv(i);
103 }
104 static void star_stuff_term_func(void)
105 {
106         glEnd();
107 }
108
109 void circf(float x, float y, float rad)
110 {
111         GLUquadricObj *qobj = gluNewQuadric(); 
112         
113         gluQuadricDrawStyle(qobj, GLU_FILL); 
114         
115         glPushMatrix(); 
116         
117         glTranslatef(x,  y, 0.); 
118         
119         gluDisk( qobj, 0.0,  rad, 32, 1); 
120         
121         glPopMatrix(); 
122         
123         gluDeleteQuadric(qobj);
124 }
125
126 void circ(float x, float y, float rad)
127 {
128         GLUquadricObj *qobj = gluNewQuadric(); 
129         
130         gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
131         
132         glPushMatrix(); 
133         
134         glTranslatef(x,  y, 0.); 
135         
136         gluDisk( qobj, 0.0,  rad, 32, 1); 
137         
138         glPopMatrix(); 
139         
140         gluDeleteQuadric(qobj);
141 }
142
143
144 /* ********* custom clipping *********** */
145
146 static void view3d_draw_clipping(RegionView3D *rv3d)
147 {
148         BoundBox *bb= rv3d->clipbb;
149         
150         if(bb) {
151                 UI_ThemeColorShade(TH_BACK, -8);
152                 
153                 glBegin(GL_QUADS);
154                 
155                 glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[3]);
156                 glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[1]);
157                 glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[5]);
158                 glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[3]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[6]);
159                 glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[2]);
160                 glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[3]);
161                 
162                 glEnd();
163         }
164 }
165
166 void view3d_set_clipping(RegionView3D *rv3d)
167 {
168         double plane[4];
169         int a, tot=4;
170         
171         if(rv3d->viewlock) tot= 6;
172         
173         for(a=0; a<tot; a++) {
174                 QUATCOPY(plane, rv3d->clip[a]);
175                 glClipPlane(GL_CLIP_PLANE0+a, plane);
176                 glEnable(GL_CLIP_PLANE0+a);
177         }
178 }
179
180 void view3d_clr_clipping(void)
181 {
182         int a;
183         
184         for(a=0; a<6; a++) {
185                 glDisable(GL_CLIP_PLANE0+a);
186         }
187 }
188
189 static int test_clipping(float *vec, float clip[][4])
190 {
191         float view[3];
192         copy_v3_v3(view, vec);
193         
194         if(0.0f < clip[0][3] + INPR(view, clip[0]))
195                 if(0.0f < clip[1][3] + INPR(view, clip[1]))
196                         if(0.0f < clip[2][3] + INPR(view, clip[2]))
197                                 if(0.0f < clip[3][3] + INPR(view, clip[3]))
198                                         return 0;
199
200         return 1;
201 }
202
203 /* for 'local' ED_view3d_local_clipping must run first
204  * then all comparisons can be done in localspace */
205 int view3d_test_clipping(RegionView3D *rv3d, float *vec, int local)
206 {
207         return test_clipping(vec, local ? rv3d->clip_local : rv3d->clip);
208 }
209
210 /* ********* end custom clipping *********** */
211
212
213 static void drawgrid_draw(ARegion *ar, float wx, float wy, float x, float y, float dx)
214 {
215         float fx, fy;
216         
217         x+= (wx); 
218         y+= (wy);
219         fx= x/dx;
220         fx= x-dx*floor(fx);
221         
222         while(fx< ar->winx) {
223                 fdrawline(fx,  0.0,  fx,  (float)ar->winy); 
224                 fx+= dx; 
225         }
226
227         fy= y/dx;
228         fy= y-dx*floor(fy);
229         
230
231         while(fy< ar->winy) {
232                 fdrawline(0.0,  fy,  (float)ar->winx,  fy); 
233                 fy+= dx;
234         }
235
236 }
237
238 #define GRID_MIN_PX 6.0f
239
240 static void drawgrid(UnitSettings *unit, ARegion *ar, View3D *v3d, char **grid_unit)
241 {
242         /* extern short bgpicmode; */
243         RegionView3D *rv3d= ar->regiondata;
244         float wx, wy, x, y, fw, fx, fy, dx;
245         float vec4[4];
246         char col[3], col2[3];
247         
248         *grid_unit= NULL;
249
250         vec4[0]=vec4[1]=vec4[2]=0.0; 
251         vec4[3]= 1.0;
252         mul_m4_v4(rv3d->persmat, vec4);
253         fx= vec4[0]; 
254         fy= vec4[1]; 
255         fw= vec4[3];
256
257         wx= (ar->winx/2.0);     /* because of rounding errors, grid at wrong location */
258         wy= (ar->winy/2.0);
259
260         x= (wx)*fx/fw;
261         y= (wy)*fy/fw;
262
263         vec4[0]=vec4[1]= (unit->system) ? 1.0 : v3d->grid;
264
265         vec4[2]= 0.0;
266         vec4[3]= 1.0;
267         mul_m4_v4(rv3d->persmat, vec4);
268         fx= vec4[0]; 
269         fy= vec4[1]; 
270         fw= vec4[3];
271
272         dx= fabs(x-(wx)*fx/fw);
273         if(dx==0) dx= fabs(y-(wy)*fy/fw);
274         
275         glDepthMask(0);         // disable write in zbuffer
276
277         /* check zoom out */
278         UI_ThemeColor(TH_GRID);
279         
280         if(unit->system) {
281                 /* Use GRID_MIN_PX*2 for units because very very small grid
282                  * items are less useful when dealing with units */
283                 void *usys;
284                 int len, i;
285                 double scalar;
286                 float dx_scalar;
287                 float blend_fac;
288
289                 bUnit_GetSystem(&usys, &len, unit->system, B_UNIT_LENGTH);
290
291                 if(usys) {
292                         i= len;
293                         while(i--) {
294                                 scalar= bUnit_GetScaler(usys, i);
295
296                                 dx_scalar = dx * scalar / unit->scale_length;
297                                 if (dx_scalar < (GRID_MIN_PX*2))
298                                         continue;
299
300                                 /* Store the smallest drawn grid size units name so users know how big each grid cell is */
301                                 if(*grid_unit==NULL) {
302                                         *grid_unit= bUnit_GetNameDisplay(usys, i);
303                                         rv3d->gridview= (scalar * unit->scale_length);
304                                 }
305                                 blend_fac= 1-((GRID_MIN_PX*2)/dx_scalar);
306
307                                 /* tweak to have the fade a bit nicer */
308                                 blend_fac= (blend_fac * blend_fac) * 2.0f;
309                                 CLAMP(blend_fac, 0.3f, 1.0f);
310
311
312                                 UI_ThemeColorBlend(TH_BACK, TH_GRID, blend_fac);
313
314                                 drawgrid_draw(ar, wx, wy, x, y, dx_scalar);
315                         }
316                 }
317         }
318         else {
319                 short sublines = v3d->gridsubdiv;
320
321                 if(dx<GRID_MIN_PX) {
322                         rv3d->gridview*= sublines;
323                         dx*= sublines;
324                         
325                         if(dx<GRID_MIN_PX) {
326                                 rv3d->gridview*= sublines;
327                                 dx*= sublines;
328
329                                 if(dx<GRID_MIN_PX) {
330                                         rv3d->gridview*= sublines;
331                                         dx*=sublines;
332                                         if(dx<GRID_MIN_PX);
333                                         else {
334                                                 UI_ThemeColor(TH_GRID);
335                                                 drawgrid_draw(ar, wx, wy, x, y, dx);
336                                         }
337                                 }
338                                 else {  // start blending out
339                                         UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*6));
340                                         drawgrid_draw(ar, wx, wy, x, y, dx);
341
342                                         UI_ThemeColor(TH_GRID);
343                                         drawgrid_draw(ar, wx, wy, x, y, sublines*dx);
344                                 }
345                         }
346                         else {  // start blending out (GRID_MIN_PX < dx < (GRID_MIN_PX*10))
347                                 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*6));
348                                 drawgrid_draw(ar, wx, wy, x, y, dx);
349
350                                 UI_ThemeColor(TH_GRID);
351                                 drawgrid_draw(ar, wx, wy, x, y, sublines*dx);
352                         }
353                 }
354                 else {
355                         if(dx>(GRID_MIN_PX*10)) {               // start blending in
356                                 rv3d->gridview/= sublines;
357                                 dx/= sublines;
358                                 if(dx>(GRID_MIN_PX*10)) {               // start blending in
359                                         rv3d->gridview/= sublines;
360                                         dx/= sublines;
361                                         if(dx>(GRID_MIN_PX*10)) {
362                                                 UI_ThemeColor(TH_GRID);
363                                                 drawgrid_draw(ar, wx, wy, x, y, dx);
364                                         }
365                                         else {
366                                                 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*6));
367                                                 drawgrid_draw(ar, wx, wy, x, y, dx);
368                                                 UI_ThemeColor(TH_GRID);
369                                                 drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
370                                         }
371                                 }
372                                 else {
373                                         UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*6));
374                                         drawgrid_draw(ar, wx, wy, x, y, dx);
375                                         UI_ThemeColor(TH_GRID);
376                                         drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
377                                 }
378                         }
379                         else {
380                                 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx/(GRID_MIN_PX*6));
381                                 drawgrid_draw(ar, wx, wy, x, y, dx);
382                                 UI_ThemeColor(TH_GRID);
383                                 drawgrid_draw(ar, wx, wy, x, y, dx*sublines);
384                         }
385                 }
386         }
387
388
389         x+= (wx); 
390         y+= (wy);
391         UI_GetThemeColor3ubv(TH_GRID, col);
392
393         setlinestyle(0);
394         
395         /* center cross */
396         if( ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT)) 
397                 UI_make_axis_color(col, col2, 'y');
398         else UI_make_axis_color(col, col2, 'x');
399         glColor3ubv((GLubyte *)col2);
400         
401         fdrawline(0.0,  y,  (float)ar->winx,  y); 
402         
403         if( ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) 
404                 UI_make_axis_color(col, col2, 'y');
405         else UI_make_axis_color(col, col2, 'z');
406         glColor3ubv((GLubyte *)col2);
407
408         fdrawline(x, 0.0, x, (float)ar->winy); 
409
410         glDepthMask(1);         // enable write in zbuffer
411 }
412 #undef GRID_MIN_PX
413
414 static void drawfloor(Scene *scene, View3D *v3d)
415 {
416         float vert[3], grid;
417         int a, gridlines, emphasise;
418         char col[3], col2[3];
419         short draw_line = 0;
420         
421         vert[2]= 0.0;
422         
423         if(v3d->gridlines<3) return;
424         
425         if(v3d->zbuf && scene->obedit) glDepthMask(0);  // for zbuffer-select
426         
427         gridlines= v3d->gridlines/2;
428         grid= gridlines*v3d->grid;
429         
430         UI_GetThemeColor3ubv(TH_GRID, col);
431         UI_GetThemeColor3ubv(TH_BACK, col2);
432         
433         /* emphasise division lines lighter instead of darker, if background is darker than grid */
434         if ( ((col[0]+col[1]+col[2])/3+10) > (col2[0]+col2[1]+col2[2])/3 )
435                 emphasise = 20;
436         else
437                 emphasise = -10;
438         
439         /* draw the Y axis and/or grid lines */
440         for(a= -gridlines;a<=gridlines;a++) {
441                 if(a==0) {
442                         /* check for the 'show Y axis' preference */
443                         if (v3d->gridflag & V3D_SHOW_Y) { 
444                                 UI_make_axis_color(col, col2, 'y');
445                                 glColor3ubv((GLubyte *)col2);
446                                 
447                                 draw_line = 1;
448                         } else if (v3d->gridflag & V3D_SHOW_FLOOR) {
449                                 UI_ThemeColorShade(TH_GRID, emphasise);
450                         } else {
451                                 draw_line = 0;
452                         }
453                 } else {
454                         /* check for the 'show grid floor' preference */
455                         if (v3d->gridflag & V3D_SHOW_FLOOR) {
456                                 if( (a % 10)==0) {
457                                         UI_ThemeColorShade(TH_GRID, emphasise);
458                                 }
459                                 else UI_ThemeColorShade(TH_GRID, 10);
460                                 
461                                 draw_line = 1;
462                         } else {
463                                 draw_line = 0;
464                         }
465                 }
466                 
467                 if (draw_line) {
468                         glBegin(GL_LINE_STRIP);
469                         vert[0]= a*v3d->grid;
470                         vert[1]= grid;
471                         glVertex3fv(vert);
472                         vert[1]= -grid;
473                         glVertex3fv(vert);
474                         glEnd();
475                 }
476         }
477         
478         /* draw the X axis and/or grid lines */
479         for(a= -gridlines;a<=gridlines;a++) {
480                 if(a==0) {
481                         /* check for the 'show X axis' preference */
482                         if (v3d->gridflag & V3D_SHOW_X) { 
483                                 UI_make_axis_color(col, col2, 'x');
484                                 glColor3ubv((GLubyte *)col2);
485                                 
486                                 draw_line = 1;
487                         } else if (v3d->gridflag & V3D_SHOW_FLOOR) {
488                                 UI_ThemeColorShade(TH_GRID, emphasise);
489                         } else {
490                                 draw_line = 0;
491                         }
492                 } else {
493                         /* check for the 'show grid floor' preference */
494                         if (v3d->gridflag & V3D_SHOW_FLOOR) {
495                                 if( (a % 10)==0) {
496                                         UI_ThemeColorShade(TH_GRID, emphasise);
497                                 }
498                                 else UI_ThemeColorShade(TH_GRID, 10);
499                                 
500                                 draw_line = 1;
501                         } else {
502                                 draw_line = 0;
503                         }
504                 }
505                 
506                 if (draw_line) {
507                         glBegin(GL_LINE_STRIP);
508                         vert[1]= a*v3d->grid;
509                         vert[0]= grid;
510                         glVertex3fv(vert );
511                         vert[0]= -grid;
512                         glVertex3fv(vert);
513                         glEnd();
514                 }
515         }
516         
517         /* draw the Z axis line */      
518         /* check for the 'show Z axis' preference */
519         if (v3d->gridflag & V3D_SHOW_Z) {
520                 UI_make_axis_color(col, col2, 'z');
521                 glColor3ubv((GLubyte *)col2);
522                 
523                 glBegin(GL_LINE_STRIP);
524                 vert[0]= 0;
525                 vert[1]= 0;
526                 vert[2]= grid;
527                 glVertex3fv(vert );
528                 vert[2]= -grid;
529                 glVertex3fv(vert);
530                 glEnd();
531         }
532         
533         if(v3d->zbuf && scene->obedit) glDepthMask(1);  
534         
535 }
536
537 static void drawcursor(Scene *scene, ARegion *ar, View3D *v3d)
538 {
539         short mx,my,co[2];
540         int flag;
541         
542         /* we dont want the clipping for cursor */
543         flag= v3d->flag;
544         v3d->flag= 0;
545         project_short(ar, give_cursor(scene, v3d), co);
546         v3d->flag= flag;
547         
548         mx = co[0];
549         my = co[1];
550         
551         if(mx!=IS_CLIPPED) {
552                 setlinestyle(0); 
553                 cpack(0xFF);
554                 circ((float)mx, (float)my, 10.0);
555                 setlinestyle(4); 
556                 cpack(0xFFFFFF);
557                 circ((float)mx, (float)my, 10.0);
558                 setlinestyle(0);
559                 cpack(0x0);
560                 
561                 sdrawline(mx-20, my, mx-5, my);
562                 sdrawline(mx+5, my, mx+20, my);
563                 sdrawline(mx, my-20, mx, my-5);
564                 sdrawline(mx, my+5, mx, my+20);
565         }
566 }
567
568 /* Draw a live substitute of the view icon, which is always shown */
569 static void draw_view_axis(RegionView3D *rv3d)
570 {
571         const float k = U.rvisize;   /* axis size */
572         const float toll = 0.5;      /* used to see when view is quasi-orthogonal */
573         const float start = k + 1.0; /* axis center in screen coordinates, x=y */
574         float ydisp = 0.0;          /* vertical displacement to allow obj info text */
575         
576         /* rvibright ranges approx. from original axis icon color to gizmo color */
577         float bright = U.rvibright / 15.0f;
578         
579         unsigned char col[3];
580         unsigned char gridcol[3];
581         float colf[3];
582         
583         float vec[4];
584         float dx, dy;
585         float h, s, v;
586         
587         /* thickness of lines is proportional to k */
588         /*      (log(k)-1) gives a more suitable thickness, but fps decreased by about 3 fps */
589         glLineWidth(k / 10);
590         //glLineWidth(log(k)-1); // a bit slow
591         
592         UI_GetThemeColor3ubv(TH_GRID, (char *)gridcol);
593         
594         /* X */
595         vec[0] = vec[3] = 1;
596         vec[1] = vec[2] = 0;
597         mul_qt_v3(rv3d->viewquat, vec);
598         
599         UI_make_axis_color((char *)gridcol, (char *)col, 'x');
600         rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
601         s = s<0.5 ? s+0.5 : 1.0;
602         v = 0.3;
603         v = (v<1.0-(bright) ? v+bright : 1.0);
604         hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
605         glColor3fv(colf);
606         
607         dx = vec[0] * k;
608         dy = vec[1] * k;
609         fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
610         if (fabs(dx) > toll || fabs(dy) > toll) {
611                 BLF_draw_default(start + dx + 2, start + dy + ydisp + 2, 0.0f, "x");
612         }
613         
614         /* Y */
615         vec[1] = vec[3] = 1;
616         vec[0] = vec[2] = 0;
617         mul_qt_v3(rv3d->viewquat, vec);
618         
619         UI_make_axis_color((char *)gridcol, (char *)col, 'y');
620         rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
621         s = s<0.5 ? s+0.5 : 1.0;
622         v = 0.3;
623         v = (v<1.0-(bright) ? v+bright : 1.0);
624         hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
625         glColor3fv(colf);
626         
627         dx = vec[0] * k;
628         dy = vec[1] * k;
629         fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
630         if (fabs(dx) > toll || fabs(dy) > toll) {
631                 BLF_draw_default(start + dx + 2, start + dy + ydisp + 2, 0.0f, "y");
632         }
633         
634         /* Z */
635         vec[2] = vec[3] = 1;
636         vec[1] = vec[0] = 0;
637         mul_qt_v3(rv3d->viewquat, vec);
638         
639         UI_make_axis_color((char *)gridcol, (char *)col, 'z');
640         rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
641         s = s<0.5 ? s+0.5 : 1.0;
642         v = 0.5;
643         v = (v<1.0-(bright) ? v+bright : 1.0);
644         hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
645         glColor3fv(colf);
646         
647         dx = vec[0] * k;
648         dy = vec[1] * k;
649         fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
650         if (fabs(dx) > toll || fabs(dy) > toll) {
651                 BLF_draw_default(start + dx + 2, start + dy + ydisp + 2, 0.0f, "z");
652         }
653         
654         /* restore line-width */
655         glLineWidth(1.0);
656 }
657
658
659 static void draw_view_icon(RegionView3D *rv3d)
660 {
661         BIFIconID icon;
662         
663         if( ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) 
664                 icon= ICON_AXIS_TOP;
665         else if( ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK)) 
666                 icon= ICON_AXIS_FRONT;
667         else if( ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT)) 
668                 icon= ICON_AXIS_SIDE;
669         else return ;
670         
671         glEnable(GL_BLEND);
672         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
673         
674         UI_icon_draw(5.0, 5.0, icon);
675         
676         glDisable(GL_BLEND);
677 }
678
679 static char *view3d_get_name(View3D *v3d, RegionView3D *rv3d)
680 {
681         char *name = NULL;
682         
683         switch (rv3d->view) {
684                 case RV3D_VIEW_FRONT:
685                         if (rv3d->persp == RV3D_ORTHO) name = "Front Ortho";
686                         else name = "Front Persp";
687                         break;
688                 case RV3D_VIEW_BACK:
689                         if (rv3d->persp == RV3D_ORTHO) name = "Back Ortho";
690                         else name = "Back Persp";
691                         break;
692                 case RV3D_VIEW_TOP:
693                         if (rv3d->persp == RV3D_ORTHO) name = "Top Ortho";
694                         else name = "Top Persp";
695                         break;
696                 case RV3D_VIEW_BOTTOM:
697                         if (rv3d->persp == RV3D_ORTHO) name = "Bottom Ortho";
698                         else name = "Bottom Persp";
699                         break;
700                 case RV3D_VIEW_RIGHT:
701                         if (rv3d->persp == RV3D_ORTHO) name = "Right Ortho";
702                         else name = "Right Persp";
703                         break;
704                 case RV3D_VIEW_LEFT:
705                         if (rv3d->persp == RV3D_ORTHO) name = "Left Ortho";
706                         else name = "Left Persp";
707                         break;
708                         
709                 default:
710                         if (rv3d->persp==RV3D_CAMOB) {
711                                 if ((v3d->camera) && (v3d->camera->type == OB_CAMERA)) {
712                                         Camera *cam;
713                                         cam = v3d->camera->data;
714                                         name = (cam->type != CAM_ORTHO) ? "Camera Persp" : "Camera Ortho";
715                                 } else {
716                                         name = "Object as Camera";
717                                 }
718                         } else { 
719                                 name = (rv3d->persp == RV3D_ORTHO) ? "User Ortho" : "User Persp";
720                         }
721                         break;
722         }
723         
724         return name;
725 }
726
727 static void draw_viewport_name(ARegion *ar, View3D *v3d)
728 {
729         RegionView3D *rv3d= ar->regiondata;
730         char *name = view3d_get_name(v3d, rv3d);
731         char *printable = NULL;
732         
733         if (v3d->localvd) {
734                 printable = MEM_mallocN(strlen(name) + strlen(" (Local)_"), "viewport_name"); /* '_' gives space for '\0' */
735                                                                                                  strcpy(printable, name);
736                                                                                                  strcat(printable, " (Local)");
737         } else {
738                 printable = name;
739         }
740
741         if (printable) {
742                 UI_ThemeColor(TH_TEXT_HI);
743                 BLF_draw_default(22,  ar->winy-17, 0.0f, printable);
744         }
745
746         if (v3d->localvd) {
747                 MEM_freeN(printable);
748         }
749 }
750
751 /* draw info beside axes in bottom left-corner: 
752 *       framenum, object name, bone name (if available), marker name (if available)
753 */
754 static void draw_selected_name(Scene *scene, Object *ob, View3D *v3d)
755 {
756         char info[256], *markern;
757         short offset=30;
758         
759         /* get name of marker on current frame (if available) */
760         markern= scene_find_marker_name(scene, CFRA);
761         
762         /* check if there is an object */
763         if(ob) {
764                 /* name(s) to display depends on type of object */
765                 if(ob->type==OB_ARMATURE) {
766                         bArmature *arm= ob->data;
767                         char *name= NULL;
768                         
769                         /* show name of active bone too (if possible) */
770                         if(arm->edbo) {
771
772                                 if(arm->act_edbone)
773                                         name= ((EditBone *)arm->act_edbone)->name;
774
775                         }
776                         else if(ob->mode & OB_MODE_POSE) {
777                                 if(arm->act_bone) {
778
779                                         if(arm->act_bone->layer & arm->layer)
780                                                 name= arm->act_bone->name;
781
782                                 }
783                         }
784                         if(name && markern)
785                                 sprintf(info, "(%d) %s %s <%s>", CFRA, ob->id.name+2, name, markern);
786                         else if(name)
787                                 sprintf(info, "(%d) %s %s", CFRA, ob->id.name+2, name);
788                         else
789                                 sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
790                 }
791                 else if(ELEM3(ob->type, OB_MESH, OB_LATTICE, OB_CURVE)) {
792                         Key *key= NULL;
793                         KeyBlock *kb = NULL;
794                         char shapes[75];
795                         
796                         /* try to display active shapekey too */
797                         shapes[0] = 0;
798                         key = ob_get_key(ob);
799                         if(key){
800                                 kb = BLI_findlink(&key->block, ob->shapenr-1);
801                                 if(kb){
802                                         sprintf(shapes, ": %s ", kb->name);             
803                                         if(ob->shapeflag == OB_SHAPE_LOCK){
804                                                 strcat(shapes, " (Pinned)");
805                                         }
806                                 }
807                         }
808                         
809                         if(markern)
810                                 sprintf(info, "(%d) %s %s <%s>", CFRA, ob->id.name+2, shapes, markern);
811                         else
812                                 sprintf(info, "(%d) %s %s", CFRA, ob->id.name+2, shapes);
813                 }
814                 else {
815                         /* standard object */
816                         if (markern)
817                                 sprintf(info, "(%d) %s <%s>", CFRA, ob->id.name+2, markern);
818                         else
819                                 sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
820                 }
821                 
822                 /* colour depends on whether there is a keyframe */
823                 if (id_frame_has_keyframe((ID *)ob, /*BKE_curframe(scene)*/(float)(CFRA), v3d->keyflags))
824                         UI_ThemeColor(TH_VERTEX_SELECT);
825                 else
826                         UI_ThemeColor(TH_TEXT_HI);
827         }
828         else {
829                 /* no object */
830                 if (markern)
831                         sprintf(info, "(%d) <%s>", CFRA, markern);
832                 else
833                         sprintf(info, "(%d)", CFRA);
834                 
835                 /* colour is always white */
836                 UI_ThemeColor(TH_TEXT_HI);
837         }
838         
839         if (U.uiflag & USER_SHOW_ROTVIEWICON)
840                 offset = 14 + (U.rvisize * 2);
841
842         BLF_draw_default(offset,  10, 0.0f, info);
843 }
844
845 static void view3d_get_viewborder_size(Scene *scene, ARegion *ar, float size_r[2])
846 {
847         float winmax= MAX2(ar->winx, ar->winy);
848         float aspect= (scene->r.xsch*scene->r.xasp) / (scene->r.ysch*scene->r.yasp);
849         
850         if(aspect>1.0) {
851                 size_r[0]= winmax;
852                 size_r[1]= winmax/aspect;
853         } else {
854                 size_r[0]= winmax*aspect;
855                 size_r[1]= winmax;
856         }
857 }
858
859 void calc_viewborder(Scene *scene, ARegion *ar, RegionView3D *rv3d, View3D *v3d, rctf *viewborder_r)
860 {
861         float zoomfac, size[2];
862         float dx= 0.0f, dy= 0.0f;
863         
864         view3d_get_viewborder_size(scene, ar, size);
865         
866         if (rv3d == NULL)
867                 rv3d = ar->regiondata;
868         
869         /* magic zoom calculation, no idea what
870                 * it signifies, if you find out, tell me! -zr
871                 */
872         /* simple, its magic dude!
873                 * well, to be honest, this gives a natural feeling zooming
874                 * with multiple keypad presses (ton)
875                 */
876         
877         zoomfac= (M_SQRT2 + rv3d->camzoom/50.0);
878         zoomfac= (zoomfac*zoomfac)*0.25;
879         
880         size[0]= size[0]*zoomfac;
881         size[1]= size[1]*zoomfac;
882         
883         /* center in window */
884         viewborder_r->xmin= 0.5*ar->winx - 0.5*size[0];
885         viewborder_r->ymin= 0.5*ar->winy - 0.5*size[1];
886         viewborder_r->xmax= viewborder_r->xmin + size[0];
887         viewborder_r->ymax= viewborder_r->ymin + size[1];
888         
889         dx= ar->winx*rv3d->camdx*zoomfac*2.0f;
890         dy= ar->winy*rv3d->camdy*zoomfac*2.0f;
891         
892         /* apply offset */
893         viewborder_r->xmin-= dx;
894         viewborder_r->ymin-= dy;
895         viewborder_r->xmax-= dx;
896         viewborder_r->ymax-= dy;
897         
898         if(v3d->camera && v3d->camera->type==OB_CAMERA) {
899                 Camera *cam= v3d->camera->data;
900                 float w = viewborder_r->xmax - viewborder_r->xmin;
901                 float h = viewborder_r->ymax - viewborder_r->ymin;
902                 float side = MAX2(w, h);
903                 
904                 viewborder_r->xmin+= cam->shiftx*side;
905                 viewborder_r->xmax+= cam->shiftx*side;
906                 viewborder_r->ymin+= cam->shifty*side;
907                 viewborder_r->ymax+= cam->shifty*side;
908         }
909 }
910
911 void view3d_set_1_to_1_viewborder(Scene *scene, ARegion *ar)
912 {
913         RegionView3D *rv3d= ar->regiondata;
914         float size[2];
915         int im_width= (scene->r.size*scene->r.xsch)/100;
916         
917         view3d_get_viewborder_size(scene, ar, size);
918         
919         rv3d->camzoom= (sqrt(4.0*im_width/size[0]) - M_SQRT2)*50.0;
920         rv3d->camzoom= CLAMPIS(rv3d->camzoom, -30, 300);
921 }
922
923
924 static void drawviewborder_flymode(ARegion *ar) 
925 {
926         /* draws 4 edge brackets that frame the safe area where the
927         mouse can move during fly mode without spinning the view */
928         float x1, x2, y1, y2;
929         
930         x1= 0.45*(float)ar->winx;
931         y1= 0.45*(float)ar->winy;
932         x2= 0.55*(float)ar->winx;
933         y2= 0.55*(float)ar->winy;
934         cpack(0);
935         
936         
937         glBegin(GL_LINES);
938         /* bottom left */
939         glVertex2f(x1,y1); 
940         glVertex2f(x1,y1+5);
941         
942         glVertex2f(x1,y1); 
943         glVertex2f(x1+5,y1);
944         
945         /* top right */
946         glVertex2f(x2,y2); 
947         glVertex2f(x2,y2-5);
948         
949         glVertex2f(x2,y2); 
950         glVertex2f(x2-5,y2);
951         
952         /* top left */
953         glVertex2f(x1,y2); 
954         glVertex2f(x1,y2-5);
955         
956         glVertex2f(x1,y2); 
957         glVertex2f(x1+5,y2);
958         
959         /* bottom right */
960         glVertex2f(x2,y1); 
961         glVertex2f(x2,y1+5);
962         
963         glVertex2f(x2,y1); 
964         glVertex2f(x2-5,y1);
965         glEnd();        
966 }
967
968
969 static void drawviewborder(Scene *scene, ARegion *ar, View3D *v3d)
970 {
971         extern void gl_round_box(int mode, float minx, float miny, float maxx, float maxy, float rad);          // interface_panel.c
972         float fac, a;
973         float x1, x2, y1, y2;
974         float x1i, x2i, y1i, y2i;
975         float x3, y3, x4, y4;
976         rctf viewborder;
977         Camera *ca= NULL;
978         RegionView3D *rv3d= (RegionView3D *)ar->regiondata;
979         
980         if(v3d->camera==NULL)
981                 return;
982         if(v3d->camera->type==OB_CAMERA)
983                 ca = v3d->camera->data;
984         
985         calc_viewborder(scene, ar, rv3d, v3d, &viewborder);
986         /* the offsets */
987         x1= viewborder.xmin;
988         y1= viewborder.ymin;
989         x2= viewborder.xmax;
990         y2= viewborder.ymax;
991         
992         /* apply offsets so the real 3D camera shows through */
993         x1i= (int)(x1 - 1.0f);
994         y1i= (int)(y1 - 1.0f);
995         x2i= (int)(x2 + 1.0f);
996         y2i= (int)(y2 + 1.0f);
997         
998         /* passepartout, specified in camera edit buttons */
999         if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001) {
1000                 if (ca->passepartalpha == 1.0) {
1001                         glColor3f(0, 0, 0);
1002                 } else {
1003                         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
1004                         glEnable(GL_BLEND);
1005                         glColor4f(0, 0, 0, ca->passepartalpha);
1006                 }
1007                 if (x1i > 0.0)
1008                         glRectf(0.0, (float)ar->winy, x1i, 0.0);
1009                 if (x2i < (float)ar->winx)
1010                         glRectf(x2i, (float)ar->winy, (float)ar->winx, 0.0);
1011                 if (y2i < (float)ar->winy)
1012                         glRectf(x1i, (float)ar->winy, x2i, y2i);
1013                 if (y2i > 0.0) 
1014                         glRectf(x1i, y1i, x2i, 0.0);
1015                 
1016                 glDisable(GL_BLEND);
1017         }
1018
1019         /* edge */
1020         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);      
1021
1022         setlinestyle(0);
1023         UI_ThemeColor(TH_BACK);
1024         glRectf(x1i, y1i, x2i, y2i);
1025         
1026         setlinestyle(3);
1027         UI_ThemeColor(TH_WIRE);
1028         glRectf(x1i, y1i, x2i, y2i);
1029
1030         /* border */
1031         if(scene->r.mode & R_BORDER) {
1032                 
1033                 cpack(0);
1034                 x3= x1+ scene->r.border.xmin*(x2-x1);
1035                 y3= y1+ scene->r.border.ymin*(y2-y1);
1036                 x4= x1+ scene->r.border.xmax*(x2-x1);
1037                 y4= y1+ scene->r.border.ymax*(y2-y1);
1038                 
1039                 cpack(0x4040FF);
1040                 glRectf(x3,  y3,  x4,  y4); 
1041         }
1042     
1043         /* safety border */
1044         if (ca && (ca->flag & CAM_SHOWTITLESAFE)) {
1045                 fac= 0.1;
1046                 
1047                 a= fac*(x2-x1);
1048                 x1+= a; 
1049                 x2-= a;
1050                 
1051                 a= fac*(y2-y1);
1052                 y1+= a;
1053                 y2-= a;
1054                 
1055                 UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1056                 
1057                 uiSetRoundBox(15);
1058                 gl_round_box(GL_LINE_LOOP, x1, y1, x2, y2, 12.0);
1059         }
1060
1061         setlinestyle(0);
1062         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1063
1064         /* camera name - draw in highlighted text color */
1065         if (ca && (ca->flag & CAM_SHOWNAME)) {
1066                 UI_ThemeColor(TH_TEXT_HI);
1067                 BLF_draw_default(x1i, y1i-15, 0.0f, v3d->camera->id.name+2);
1068                 UI_ThemeColor(TH_WIRE);
1069         }
1070 }
1071
1072 /* *********************** backdraw for selection *************** */
1073
1074 static void backdrawview3d(Scene *scene, ARegion *ar, View3D *v3d)
1075 {
1076         RegionView3D *rv3d= ar->regiondata;
1077         struct Base *base = scene->basact;
1078         rcti winrct;
1079
1080         if(base && (base->object->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT) ||
1081                          paint_facesel_test(base->object)));
1082         else if((base && (base->object->mode & OB_MODE_TEXTURE_PAINT)) &&
1083                 scene->toolsettings && (scene->toolsettings->imapaint.flag & IMAGEPAINT_PROJECT_DISABLE));
1084         else if((base && (base->object->mode & OB_MODE_PARTICLE_EDIT)) && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT));
1085         else if(scene->obedit && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT));
1086         else {
1087                 v3d->flag &= ~V3D_INVALID_BACKBUF;
1088                 return;
1089         }
1090
1091         if( !(v3d->flag & V3D_INVALID_BACKBUF) ) return;
1092
1093 //      if(test) {
1094 //              if(qtest()) {
1095 //                      addafterqueue(ar->win, BACKBUFDRAW, 1);
1096 //                      return;
1097 //              }
1098 //      }
1099
1100         if(v3d->drawtype > OB_WIRE) v3d->zbuf= TRUE;
1101         
1102         glDisable(GL_DITHER);
1103
1104         region_scissor_winrct(ar, &winrct);
1105         glScissor(winrct.xmin, winrct.ymin, winrct.xmax - winrct.xmin, winrct.ymax - winrct.ymin);
1106
1107         glClearColor(0.0, 0.0, 0.0, 0.0); 
1108         if(v3d->zbuf) {
1109                 glEnable(GL_DEPTH_TEST);
1110                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1111         }
1112         else {
1113                 glClear(GL_COLOR_BUFFER_BIT);
1114                 glDisable(GL_DEPTH_TEST);
1115         }
1116         
1117         if(rv3d->rflag & RV3D_CLIPPING)
1118                 view3d_set_clipping(rv3d);
1119         
1120         G.f |= G_BACKBUFSEL;
1121         
1122         if(base && (base->lay & v3d->lay)) {
1123                 draw_object_backbufsel(scene, v3d, rv3d, base->object);
1124         }
1125
1126         v3d->flag &= ~V3D_INVALID_BACKBUF;
1127         ar->swap= 0; /* mark invalid backbuf for wm draw */
1128
1129         G.f &= ~G_BACKBUFSEL;
1130         v3d->zbuf= FALSE; 
1131         glDisable(GL_DEPTH_TEST);
1132         glEnable(GL_DITHER);
1133
1134         if(rv3d->rflag & RV3D_CLIPPING)
1135                 view3d_clr_clipping();
1136
1137         /* it is important to end a view in a transform compatible with buttons */
1138 //      persp(PERSP_WIN);  // set ortho
1139
1140 }
1141
1142 void view3d_validate_backbuf(ViewContext *vc)
1143 {
1144         if(vc->v3d->flag & V3D_INVALID_BACKBUF)
1145                 backdrawview3d(vc->scene, vc->ar, vc->v3d);
1146 }
1147
1148 /* samples a single pixel (copied from vpaint) */
1149 unsigned int view3d_sample_backbuf(ViewContext *vc, int x, int y)
1150 {
1151         unsigned int col;
1152         
1153         if(x >= vc->ar->winx || y >= vc->ar->winy) return 0;
1154         x+= vc->ar->winrct.xmin;
1155         y+= vc->ar->winrct.ymin;
1156         
1157         view3d_validate_backbuf(vc);
1158
1159         glReadPixels(x,  y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,  &col);
1160         glReadBuffer(GL_BACK);  
1161         
1162         if(ENDIAN_ORDER==B_ENDIAN) SWITCH_INT(col);
1163         
1164         return WM_framebuffer_to_index(col);
1165 }
1166
1167 /* reads full rect, converts indices */
1168 ImBuf *view3d_read_backbuf(ViewContext *vc, short xmin, short ymin, short xmax, short ymax)
1169 {
1170         unsigned int *dr, *rd;
1171         struct ImBuf *ibuf, *ibuf1;
1172         int a;
1173         short xminc, yminc, xmaxc, ymaxc, xs, ys;
1174         
1175         /* clip */
1176         if(xmin<0) xminc= 0; else xminc= xmin;
1177         if(xmax >= vc->ar->winx) xmaxc= vc->ar->winx-1; else xmaxc= xmax;
1178         if(xminc > xmaxc) return NULL;
1179
1180         if(ymin<0) yminc= 0; else yminc= ymin;
1181         if(ymax >= vc->ar->winy) ymaxc= vc->ar->winy-1; else ymaxc= ymax;
1182         if(yminc > ymaxc) return NULL;
1183         
1184         ibuf= IMB_allocImBuf((xmaxc-xminc+1), (ymaxc-yminc+1), 32, IB_rect,0);
1185
1186         view3d_validate_backbuf(vc); 
1187         
1188         glReadPixels(vc->ar->winrct.xmin+xminc, vc->ar->winrct.ymin+yminc, (xmaxc-xminc+1), (ymaxc-yminc+1), GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
1189         glReadBuffer(GL_BACK);  
1190
1191         if(ENDIAN_ORDER==B_ENDIAN) IMB_convert_rgba_to_abgr(ibuf);
1192
1193         a= (xmaxc-xminc+1)*(ymaxc-yminc+1);
1194         dr= ibuf->rect;
1195         while(a--) {
1196                 if(*dr) *dr= WM_framebuffer_to_index(*dr);
1197                 dr++;
1198         }
1199         
1200         /* put clipped result back, if needed */
1201         if(xminc==xmin && xmaxc==xmax && yminc==ymin && ymaxc==ymax) 
1202                 return ibuf;
1203         
1204         ibuf1= IMB_allocImBuf( (xmax-xmin+1),(ymax-ymin+1),32,IB_rect,0);
1205         rd= ibuf->rect;
1206         dr= ibuf1->rect;
1207                 
1208         for(ys= ymin; ys<=ymax; ys++) {
1209                 for(xs= xmin; xs<=xmax; xs++, dr++) {
1210                         if( xs>=xminc && xs<=xmaxc && ys>=yminc && ys<=ymaxc) {
1211                                 *dr= *rd;
1212                                 rd++;
1213                         }
1214                 }
1215         }
1216         IMB_freeImBuf(ibuf);
1217         return ibuf1;
1218 }
1219
1220 /* smart function to sample a rect spiralling outside, nice for backbuf selection */
1221 unsigned int view3d_sample_backbuf_rect(ViewContext *vc, short mval[2], int size, 
1222                                                                                 unsigned int min, unsigned int max, int *dist, short strict, 
1223                                                                                 void *handle, unsigned int (*indextest)(void *handle, unsigned int index))
1224 {
1225         struct ImBuf *buf;
1226         unsigned int *bufmin, *bufmax, *tbuf;
1227         int minx, miny;
1228         int a, b, rc, nr, amount, dirvec[4][2];
1229         int distance=0;
1230         unsigned int index = 0;
1231         short indexok = 0;      
1232
1233         amount= (size-1)/2;
1234
1235         minx = mval[0]-(amount+1);
1236         miny = mval[1]-(amount+1);
1237         buf = view3d_read_backbuf(vc, minx, miny, minx+size-1, miny+size-1);
1238         if (!buf) return 0;
1239
1240         rc= 0;
1241         
1242         dirvec[0][0]= 1; dirvec[0][1]= 0;
1243         dirvec[1][0]= 0; dirvec[1][1]= -size;
1244         dirvec[2][0]= -1; dirvec[2][1]= 0;
1245         dirvec[3][0]= 0; dirvec[3][1]= size;
1246         
1247         bufmin = buf->rect;
1248         tbuf = buf->rect;
1249         bufmax = buf->rect + size*size;
1250         tbuf+= amount*size+ amount;
1251         
1252         for(nr=1; nr<=size; nr++) {
1253                 
1254                 for(a=0; a<2; a++) {
1255                         for(b=0; b<nr; b++, distance++) {
1256                                 if (*tbuf && *tbuf>=min && *tbuf<max) { //we got a hit
1257                                         if(strict){
1258                                                 indexok =  indextest(handle, *tbuf - min+1);
1259                                                 if(indexok){
1260                                                         *dist= (short) sqrt( (float)distance   );
1261                                                         index = *tbuf - min+1;
1262                                                         goto exit; 
1263                                                 }                                               
1264                                         }
1265                                         else{
1266                                                 *dist= (short) sqrt( (float)distance ); // XXX, this distance is wrong - 
1267                                                 index = *tbuf - min+1; // messy yah, but indices start at 1
1268                                                 goto exit;
1269                                         }                       
1270                                 }
1271                                 
1272                                 tbuf+= (dirvec[rc][0]+dirvec[rc][1]);
1273                                 
1274                                 if(tbuf<bufmin || tbuf>=bufmax) {
1275                                         goto exit;
1276                                 }
1277                         }
1278                         rc++;
1279                         rc &= 3;
1280                 }
1281         }
1282
1283 exit:
1284         IMB_freeImBuf(buf);
1285         return index;
1286 }
1287
1288
1289 /* ************************************************************* */
1290
1291 static void draw_bgpic(Scene *scene, ARegion *ar, View3D *v3d)
1292 {
1293         RegionView3D *rv3d= ar->regiondata;
1294         BGpic *bgpic;
1295         Image *ima;
1296         ImBuf *ibuf= NULL;
1297         float vec[4], fac, asp, zoomx, zoomy;
1298         float x1, y1, x2, y2, cx, cy;
1299
1300
1301         for ( bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next ) {
1302
1303                 if(     (bgpic->view == 0) || /* zero for any */
1304                         (bgpic->view & (1<<rv3d->view)) || /* check agaist flags */
1305                         (rv3d->persp==RV3D_CAMOB && bgpic->view == (1<<RV3D_VIEW_CAMERA))
1306                 ) {
1307                         ima= bgpic->ima;
1308                         if(ima==NULL)
1309                                 continue;
1310                         BKE_image_user_calc_frame(&bgpic->iuser, CFRA, 0);
1311                         ibuf= BKE_image_get_ibuf(ima, &bgpic->iuser);
1312                         if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL) )
1313                                 continue;
1314                         if(ibuf->channels!=4)
1315                                 continue;
1316                         if(ibuf->rect==NULL)
1317                                 IMB_rect_from_float(ibuf);
1318
1319                         if(rv3d->persp==RV3D_CAMOB) {
1320                                 rctf vb;
1321
1322                                 calc_viewborder(scene, ar, rv3d, v3d, &vb);
1323
1324                                 x1= vb.xmin;
1325                                 y1= vb.ymin;
1326                                 x2= vb.xmax;
1327                                 y2= vb.ymax;
1328                         }
1329                         else {
1330                                 float sco[2];
1331
1332                                 /* calc window coord */
1333                                 initgrabz(rv3d, 0.0, 0.0, 0.0);
1334                                 window_to_3d_delta(ar, vec, 1, 0);
1335                                 fac= MAX3( fabs(vec[0]), fabs(vec[1]), fabs(vec[1]) );
1336                                 fac= 1.0/fac;
1337
1338                                 asp= ( (float)ibuf->y)/(float)ibuf->x;
1339
1340                                 vec[0] = vec[1] = vec[2] = 0.0;
1341                                 view3d_project_float(ar, vec, sco, rv3d->persmat);
1342                                 cx = sco[0];
1343                                 cy = sco[1];
1344
1345                                 x1=  cx+ fac*(bgpic->xof-bgpic->size);
1346                                 y1=  cy+ asp*fac*(bgpic->yof-bgpic->size);
1347                                 x2=  cx+ fac*(bgpic->xof+bgpic->size);
1348                                 y2=  cy+ asp*fac*(bgpic->yof+bgpic->size);
1349                         }
1350
1351                         /* complete clip? */
1352
1353                         if(x2 < 0 ) continue;
1354                         if(y2 < 0 ) continue;
1355                         if(x1 > ar->winx ) continue;
1356                         if(y1 > ar->winy ) continue;
1357
1358                         zoomx= (x2-x1)/ibuf->x;
1359                         zoomy= (y2-y1)/ibuf->y;
1360
1361                         /* for some reason; zoomlevels down refuses to use GL_ALPHA_SCALE */
1362                         if(zoomx < 1.0f || zoomy < 1.0f) {
1363                                 float tzoom= MIN2(zoomx, zoomy);
1364                                 int mip= 0;
1365
1366                                 if(ibuf->mipmap[0]==NULL)
1367                                         IMB_makemipmap(ibuf, 0);
1368
1369                                 while(tzoom < 1.0f && mip<8 && ibuf->mipmap[mip]) {
1370                                         tzoom*= 2.0f;
1371                                         zoomx*= 2.0f;
1372                                         zoomy*= 2.0f;
1373                                         mip++;
1374                                 }
1375                                 if(mip>0)
1376                                         ibuf= ibuf->mipmap[mip-1];
1377                         }
1378
1379                         if(v3d->zbuf) glDisable(GL_DEPTH_TEST);
1380                         glDepthMask(0);
1381
1382                         glEnable(GL_BLEND);
1383                         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA);
1384
1385                         glMatrixMode(GL_PROJECTION);
1386                         glPushMatrix();
1387                         glMatrixMode(GL_MODELVIEW);
1388                         glPushMatrix();
1389                         ED_region_pixelspace(ar);
1390
1391                         glPixelZoom(zoomx, zoomy);
1392                         glColor4f(1.0, 1.0, 1.0, 1.0-bgpic->blend);
1393                         glaDrawPixelsTex(x1, y1, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
1394
1395                         glPixelZoom(1.0, 1.0);
1396                         glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
1397
1398                         glMatrixMode(GL_PROJECTION);
1399                         glPopMatrix();
1400                         glMatrixMode(GL_MODELVIEW);
1401                         glPopMatrix();
1402
1403                         glDisable(GL_BLEND);
1404
1405                         glDepthMask(1);
1406                         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1407                 }
1408         }
1409 }
1410
1411 /* ****************** View3d afterdraw *************** */
1412
1413 typedef struct View3DAfter {
1414         struct View3DAfter *next, *prev;
1415         struct Base *base;
1416         int type, flag;
1417 } View3DAfter;
1418
1419 /* temp storage of Objects that need to be drawn as last */
1420 void add_view3d_after(View3D *v3d, Base *base, int type, int flag)
1421 {
1422         View3DAfter *v3da= MEM_callocN(sizeof(View3DAfter), "View 3d after");
1423         
1424         BLI_addtail(&v3d->afterdraw, v3da);
1425         v3da->base= base;
1426         v3da->type= type;
1427         v3da->flag= flag;
1428 }
1429
1430 /* clears zbuffer and draws it over */
1431 static void view3d_draw_xray(Scene *scene, ARegion *ar, View3D *v3d, int clear)
1432 {
1433         View3DAfter *v3da, *next;
1434         int doit= 0;
1435         
1436         for(v3da= v3d->afterdraw.first; v3da; v3da= v3da->next)
1437                 if(v3da->type==V3D_XRAY) doit= 1;
1438         
1439         if(doit) {
1440                 if(clear && v3d->zbuf) glClear(GL_DEPTH_BUFFER_BIT);
1441                 v3d->xray= TRUE;
1442                 
1443                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1444                         next= v3da->next;
1445                         if(v3da->type==V3D_XRAY) {
1446                                 draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1447                                 BLI_remlink(&v3d->afterdraw, v3da);
1448                                 MEM_freeN(v3da);
1449                         }
1450                 }
1451                 v3d->xray= FALSE;
1452         }
1453 }
1454
1455 /* disables write in zbuffer and draws it over */
1456 static void view3d_draw_transp(Scene *scene, ARegion *ar, View3D *v3d)
1457 {
1458         View3DAfter *v3da, *next;
1459         
1460         glDepthMask(0);
1461         v3d->transp= TRUE;
1462         
1463         for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1464                 next= v3da->next;
1465                 if(v3da->type==V3D_TRANSP) {
1466                         draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1467                         BLI_remlink(&v3d->afterdraw, v3da);
1468                         MEM_freeN(v3da);
1469                 }
1470         }
1471         v3d->transp= FALSE;
1472         
1473         glDepthMask(1);
1474         
1475 }
1476
1477 /* *********************** */
1478
1479 /*
1480         In most cases call draw_dupli_objects,
1481         draw_dupli_objects_color was added because when drawing set dupli's
1482         we need to force the color
1483  */
1484
1485 #if 0
1486 int dupli_ob_sort(void *arg1, void *arg2)
1487 {
1488         void *p1= ((DupliObject *)arg1)->ob;
1489         void *p2= ((DupliObject *)arg2)->ob;
1490         int val = 0;
1491         if (p1 < p2)            val = -1;
1492         else if (p1 > p2)       val = 1;
1493         return val;
1494 }
1495 #endif
1496
1497
1498 static DupliObject *dupli_step(DupliObject *dob)
1499 {
1500         while(dob && dob->no_draw)
1501                 dob= dob->next;
1502         return dob;
1503 }
1504
1505 static void draw_dupli_objects_color(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int color)
1506 {
1507         RegionView3D *rv3d= ar->regiondata;
1508         ListBase *lb;
1509         DupliObject *dob_prev= NULL, *dob, *dob_next;
1510         Base tbase;
1511         BoundBox bb, *bb_tmp; /* use a copy because draw_object, calls clear_mesh_caches */
1512         GLuint displist=0;
1513         short transflag, use_displist= -1;      /* -1 is initialize */
1514         char dt, dtx;
1515         
1516         if (base->object->restrictflag & OB_RESTRICT_VIEW) return;
1517         
1518         tbase.flag= OB_FROMDUPLI|base->flag;
1519         lb= object_duplilist(scene, base->object);
1520         // BLI_sortlist(lb, dupli_ob_sort); // might be nice to have if we have a dupli list with mixed objects.
1521
1522         dob=dupli_step(lb->first);
1523         if(dob) dob_next= dupli_step(dob->next);
1524
1525         for( ; dob ; dob_prev= dob, dob= dob_next, dob_next= dob_next ? dupli_step(dob_next->next) : NULL) {
1526                 tbase.object= dob->ob;
1527
1528                 /* extra service: draw the duplicator in drawtype of parent */
1529                 /* MIN2 for the drawtype to allow bounding box objects in groups for lods */
1530                 dt= tbase.object->dt;   tbase.object->dt= MIN2(tbase.object->dt, base->object->dt);
1531                 dtx= tbase.object->dtx; tbase.object->dtx= base->object->dtx;
1532
1533                 /* negative scale flag has to propagate */
1534                 transflag= tbase.object->transflag;
1535                 if(base->object->transflag & OB_NEG_SCALE)
1536                         tbase.object->transflag ^= OB_NEG_SCALE;
1537
1538                 UI_ThemeColorBlend(color, TH_BACK, 0.5);
1539
1540                 /* generate displist, test for new object */
1541                 if(dob_prev && dob_prev->ob != dob->ob) {
1542                         if(use_displist==1)
1543                                 glDeleteLists(displist, 1);
1544
1545                         use_displist= -1;
1546                 }
1547
1548                 /* generate displist */
1549                 if(use_displist == -1) {
1550
1551                         /* note, since this was added, its checked dob->type==OB_DUPLIGROUP
1552                          * however this is very slow, it was probably needed for the NLA
1553                          * offset feature (used in group-duplicate.blend but no longer works in 2.5)
1554                          * so for now it should be ok to - campbell */
1555
1556                         if(             (dob_next==NULL || dob_next->ob != dob->ob) || /* if this is the last no need  to make a displist */
1557                                         (dob->ob->type == OB_LAMP) || /* lamp drawing messes with matrices, could be handled smarter... but this works */
1558                                         (dob->type == OB_DUPLIGROUP && dob->animated) ||
1559                                         !(bb_tmp= object_get_boundbox(dob->ob))
1560                         ) {
1561                                 // printf("draw_dupli_objects_color: skipping displist for %s\n", dob->ob->id.name+2);
1562                                 use_displist= 0;
1563                         }
1564                         else {
1565                                 // printf("draw_dupli_objects_color: using displist for %s\n", dob->ob->id.name+2);
1566                                 bb= *bb_tmp; /* must make a copy  */
1567
1568                                 /* disable boundbox check for list creation */
1569                                 object_boundbox_flag(dob->ob, OB_BB_DISABLED, 1);
1570                                 /* need this for next part of code */
1571                                 unit_m4(dob->ob->obmat);        /* obmat gets restored */
1572
1573                                 displist= glGenLists(1);
1574                                 glNewList(displist, GL_COMPILE);
1575                                 draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
1576                                 glEndList();
1577
1578                                 use_displist= 1;
1579                                 object_boundbox_flag(dob->ob, OB_BB_DISABLED, 0);
1580                         }
1581                 }
1582                 if(use_displist) {
1583                         glMultMatrixf(dob->mat);
1584                         if(boundbox_clip(rv3d, dob->mat, &bb))
1585                                 glCallList(displist);
1586                         glLoadMatrixf(rv3d->viewmat);
1587                 }
1588                 else {
1589                         copy_m4_m4(dob->ob->obmat, dob->mat);
1590                         draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
1591                 }
1592
1593                 tbase.object->dt= dt;
1594                 tbase.object->dtx= dtx;
1595                 tbase.object->transflag= transflag;
1596         }
1597         
1598         /* Transp afterdraw disabled, afterdraw only stores base pointers, and duplis can be same obj */
1599         
1600         free_object_duplilist(lb);      /* does restore */
1601         
1602         if(use_displist)
1603                 glDeleteLists(displist, 1);
1604 }
1605
1606 static void draw_dupli_objects(Scene *scene, ARegion *ar, View3D *v3d, Base *base)
1607 {
1608         /* define the color here so draw_dupli_objects_color can be called
1609         * from the set loop */
1610         
1611         int color= (base->flag & SELECT)?TH_SELECT:TH_WIRE;
1612         /* debug */
1613         if(base->object->dup_group && base->object->dup_group->id.us<1)
1614                 color= TH_REDALERT;
1615         
1616         draw_dupli_objects_color(scene, ar, v3d, base, color);
1617 }
1618
1619
1620 void view3d_update_depths(ARegion *ar, View3D *v3d)
1621 {
1622         RegionView3D *rv3d= ar->regiondata;
1623         
1624         /* Create storage for, and, if necessary, copy depth buffer */
1625         if(!rv3d->depths) rv3d->depths= MEM_callocN(sizeof(ViewDepths),"ViewDepths");
1626         if(rv3d->depths) {
1627                 ViewDepths *d= rv3d->depths;
1628                 if(d->w != ar->winx ||
1629                    d->h != ar->winy ||
1630                    !d->depths) {
1631                         d->w= ar->winx;
1632                         d->h= ar->winy;
1633                         if(d->depths)
1634                                 MEM_freeN(d->depths);
1635                         d->depths= MEM_mallocN(sizeof(float)*d->w*d->h,"View depths");
1636                         d->damaged= 1;
1637                 }
1638                 
1639                 if(d->damaged) {
1640                         glReadPixels(ar->winrct.xmin,ar->winrct.ymin,d->w,d->h,
1641                                                  GL_DEPTH_COMPONENT,GL_FLOAT, d->depths);
1642                         
1643                         glGetDoublev(GL_DEPTH_RANGE,d->depth_range);
1644                         
1645                         d->damaged= 0;
1646                 }
1647         }
1648 }
1649
1650 void draw_depth_gpencil(Scene *scene, ARegion *ar, View3D *v3d)
1651 {
1652         short zbuf= v3d->zbuf;
1653         RegionView3D *rv3d= ar->regiondata;
1654
1655         setwinmatrixview3d(ar, v3d, NULL);      /* 0= no pick rect */
1656         setviewmatrixview3d(scene, v3d, rv3d);  /* note: calls where_is_object for camera... */
1657
1658         mul_m4_m4m4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1659         invert_m4_m4(rv3d->persinv, rv3d->persmat);
1660         invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
1661
1662         glClear(GL_DEPTH_BUFFER_BIT);
1663
1664         glLoadMatrixf(rv3d->viewmat);
1665
1666         v3d->zbuf= TRUE;
1667         glEnable(GL_DEPTH_TEST);
1668
1669         draw_gpencil_view3d_ext(scene, ar, 1);
1670         
1671         v3d->zbuf= zbuf;
1672
1673 }
1674
1675 void draw_depth(Scene *scene, ARegion *ar, View3D *v3d, int (* func)(void *))
1676 {
1677         RegionView3D *rv3d= ar->regiondata;
1678         Base *base;
1679         Scene *sce;
1680         short zbuf= v3d->zbuf;
1681         short flag= v3d->flag;
1682         float glalphaclip= U.glalphaclip;
1683         int obcenter_dia= U.obcenter_dia;
1684         /* temp set drawtype to solid */
1685         
1686         /* Setting these temporarily is not nice */
1687         v3d->flag &= ~V3D_SELECT_OUTLINE;
1688         U.glalphaclip = 0.5; /* not that nice but means we wont zoom into billboards */
1689         U.obcenter_dia= 0;
1690         
1691         setwinmatrixview3d(ar, v3d, NULL);      /* 0= no pick rect */
1692         setviewmatrixview3d(scene, v3d, rv3d);  /* note: calls where_is_object for camera... */
1693         
1694         mul_m4_m4m4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1695         invert_m4_m4(rv3d->persinv, rv3d->persmat);
1696         invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
1697         
1698         glClear(GL_DEPTH_BUFFER_BIT);
1699         
1700         glLoadMatrixf(rv3d->viewmat);
1701 //      persp(PERSP_STORE);  // store correct view for persp(PERSP_VIEW) calls
1702         
1703         if(rv3d->rflag & RV3D_CLIPPING) {
1704                 view3d_set_clipping(rv3d);
1705         }
1706         
1707         v3d->zbuf= TRUE;
1708         glEnable(GL_DEPTH_TEST);
1709         
1710         /* draw set first */
1711         if(scene->set) {
1712                 for(SETLOOPER(scene->set, base)) {
1713                         if(v3d->lay & base->lay) {
1714                                 if (func == NULL || func(base)) {
1715                                         draw_object(scene, ar, v3d, base, 0);
1716                                         if(base->object->transflag & OB_DUPLI) {
1717                                                 draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
1718                                         }
1719                                 }
1720                         }
1721                 }
1722         }
1723         
1724         for(base= scene->base.first; base; base= base->next) {
1725                 if(v3d->lay & base->lay) {
1726                         if (func == NULL || func(base)) {
1727                                 /* dupli drawing */
1728                                 if(base->object->transflag & OB_DUPLI) {
1729                                         draw_dupli_objects(scene, ar, v3d, base);
1730                                 }
1731                                 draw_object(scene, ar, v3d, base, 0);
1732                         }
1733                 }
1734         }
1735         
1736         /* this isnt that nice, draw xray objects as if they are normal */
1737         if (v3d->afterdraw.first) {
1738                 View3DAfter *v3da, *next;
1739                 int num = 0;
1740                 int mask_orig;
1741                 v3d->xray= TRUE;
1742                 
1743                 /* transp materials can change the depth mask, see #21388 */
1744                 glGetIntegerv(GL_DEPTH_WRITEMASK, &mask_orig);
1745
1746                 glDepthFunc(GL_ALWAYS); /* always write into the depth bufer, overwriting front z values */
1747                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1748                         next= v3da->next;
1749                         if(v3da->type==V3D_XRAY) {
1750                                 draw_object(scene, ar, v3d, v3da->base, 0);
1751                                 num++;
1752                         }
1753                         /* dont remove this time */
1754                 }
1755                 v3d->xray= FALSE;
1756                 
1757                 glDepthFunc(GL_LEQUAL); /* Now write the depth buffer normally */
1758                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1759                         next= v3da->next;
1760                         if(v3da->type==V3D_XRAY) {
1761                                 v3d->xray= TRUE; v3d->transp= FALSE;  
1762                         } else if (v3da->type==V3D_TRANSP) {
1763                                 v3d->xray= FALSE; v3d->transp= TRUE;
1764                         }
1765                         
1766                         draw_object(scene, ar, v3d, v3da->base, 0); /* Draw Xray or Transp objects normally */
1767                         BLI_remlink(&v3d->afterdraw, v3da);
1768                         MEM_freeN(v3da);
1769                 }
1770                 v3d->xray= FALSE;
1771                 v3d->transp= FALSE;
1772
1773                 glDepthMask(mask_orig);
1774         }
1775         
1776         if(rv3d->rflag & RV3D_CLIPPING)
1777                 view3d_clr_clipping();
1778         
1779         v3d->zbuf = zbuf;
1780         if(!v3d->zbuf) glDisable(GL_DEPTH_TEST);
1781
1782         U.glalphaclip = glalphaclip;
1783         v3d->flag = flag;
1784         U.obcenter_dia= obcenter_dia;
1785 }
1786
1787 typedef struct View3DShadow {
1788         struct View3DShadow *next, *prev;
1789         GPULamp *lamp;
1790 } View3DShadow;
1791
1792 static void gpu_render_lamp_update(Scene *scene, View3D *v3d, Object *ob, Object *par, float obmat[][4], ListBase *shadows)
1793 {
1794         GPULamp *lamp;
1795         Lamp *la = (Lamp*)ob->data;
1796         View3DShadow *shadow;
1797         
1798         lamp = GPU_lamp_from_blender(scene, ob, par);
1799         
1800         if(lamp) {
1801                 GPU_lamp_update(lamp, ob->lay, (ob->restrictflag & OB_RESTRICT_VIEW), obmat);
1802                 GPU_lamp_update_colors(lamp, la->r, la->g, la->b, la->energy);
1803                 
1804                 if((ob->lay & v3d->lay) && GPU_lamp_has_shadow_buffer(lamp)) {
1805                         shadow= MEM_callocN(sizeof(View3DShadow), "View3DShadow");
1806                         shadow->lamp = lamp;
1807                         BLI_addtail(shadows, shadow);
1808                 }
1809         }
1810 }
1811
1812 static void gpu_update_lamps_shadows(Scene *scene, View3D *v3d)
1813 {
1814         ListBase shadows;
1815         View3DShadow *shadow;
1816         Scene *sce;
1817         Base *base;
1818         Object *ob;
1819         ARegion ar;
1820         RegionView3D rv3d;
1821         
1822         shadows.first= shadows.last= NULL;
1823         
1824         /* update lamp transform and gather shadow lamps */
1825         for(SETLOOPER(scene, base)) {
1826                 ob= base->object;
1827                 
1828                 if(ob->type == OB_LAMP)
1829                         gpu_render_lamp_update(scene, v3d, ob, NULL, ob->obmat, &shadows);
1830                 
1831                 if (ob->transflag & OB_DUPLI) {
1832                         DupliObject *dob;
1833                         ListBase *lb = object_duplilist(scene, ob);
1834                         
1835                         for(dob=lb->first; dob; dob=dob->next)
1836                                 if(dob->ob->type==OB_LAMP)
1837                                         gpu_render_lamp_update(scene, v3d, dob->ob, ob, dob->mat, &shadows);
1838                         
1839                         free_object_duplilist(lb);
1840                 }
1841         }
1842         
1843         /* render shadows after updating all lamps, nested object_duplilist
1844                 * don't work correct since it's replacing object matrices */
1845         for(shadow=shadows.first; shadow; shadow=shadow->next) {
1846                 /* this needs to be done better .. */
1847                 float viewmat[4][4], winmat[4][4];
1848                 int drawtype, lay, winsize, flag2=v3d->flag2;
1849                 
1850                 drawtype= v3d->drawtype;
1851                 lay= v3d->lay;
1852                 
1853                 v3d->drawtype = OB_SOLID;
1854                 v3d->lay &= GPU_lamp_shadow_layer(shadow->lamp);
1855                 v3d->flag2 &= ~V3D_SOLID_TEX;
1856                 v3d->flag2 |= V3D_RENDER_OVERRIDE;
1857                 
1858                 GPU_lamp_shadow_buffer_bind(shadow->lamp, viewmat, &winsize, winmat);
1859
1860                 memset(&ar, 0, sizeof(ar));
1861                 memset(&rv3d, 0, sizeof(rv3d));
1862
1863                 ar.regiondata= &rv3d;
1864                 ar.regiontype= RGN_TYPE_WINDOW;
1865                 rv3d.persp= RV3D_CAMOB;
1866                 copy_m4_m4(rv3d.winmat, winmat);
1867                 copy_m4_m4(rv3d.viewmat, viewmat);
1868                 invert_m4_m4(rv3d.viewinv, rv3d.viewmat);
1869                 mul_m4_m4m4(rv3d.persmat, rv3d.viewmat, rv3d.winmat);
1870                 invert_m4_m4(rv3d.persinv, rv3d.viewinv);
1871
1872                 ED_view3d_draw_offscreen(scene, v3d, &ar, winsize, winsize, viewmat, winmat);
1873                 GPU_lamp_shadow_buffer_unbind(shadow->lamp);
1874                 
1875                 v3d->drawtype= drawtype;
1876                 v3d->lay= lay;
1877                 v3d->flag2 = flag2;
1878         }
1879         
1880         BLI_freelistN(&shadows);
1881 }
1882
1883 /* *********************** customdata **************** */
1884
1885 /* goes over all modes and view3d settings */
1886 static CustomDataMask get_viewedit_datamask(bScreen *screen, Scene *scene, Object *ob)
1887 {
1888         CustomDataMask mask = CD_MASK_BAREMESH;
1889         ScrArea *sa;
1890         
1891         /* check if we need tfaces & mcols due to face select or texture paint */
1892         if(paint_facesel_test(ob) || (ob && ob->mode & OB_MODE_TEXTURE_PAINT))
1893                 mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
1894         
1895         /* check if we need tfaces & mcols due to view mode */
1896         for(sa = screen->areabase.first; sa; sa = sa->next) {
1897                 if(sa->spacetype == SPACE_VIEW3D) {
1898                         View3D *view = sa->spacedata.first;
1899                         if(view->drawtype == OB_SHADED) {
1900                                 /* this includes normals for mesh_create_shadedColors */
1901                                 mask |= CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_NORMAL | CD_MASK_ORCO;
1902                         }
1903                         if((view->drawtype == OB_TEXTURE) || ((view->drawtype == OB_SOLID) && (view->flag2 & V3D_SOLID_TEX))) {
1904                                 mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
1905
1906                                 if(scene->gm.matmode == GAME_MAT_GLSL)
1907                                         mask |= CD_MASK_ORCO;
1908                         }
1909                 }
1910         }
1911         
1912         /* check if we need mcols due to vertex paint or weightpaint */
1913         if(ob) {
1914                 if(ob->mode & OB_MODE_VERTEX_PAINT)
1915                         mask |= CD_MASK_MCOL;
1916                 if(ob->mode & OB_MODE_WEIGHT_PAINT)
1917                         mask |= CD_MASK_WEIGHT_MCOL;
1918         }
1919
1920         return mask;
1921 }
1922
1923 static void view3d_main_area_setup_view(Scene *scene, View3D *v3d, ARegion *ar, float viewmat[][4], float winmat[][4])
1924 {
1925         RegionView3D *rv3d= ar->regiondata;
1926
1927         /* setup window matrices */
1928         if(winmat)
1929                 copy_m4_m4(rv3d->winmat, winmat);
1930         else
1931                 setwinmatrixview3d(ar, v3d, NULL); /* NULL= no pickrect */
1932         
1933         /* setup view matrix */
1934         if(viewmat)
1935                 copy_m4_m4(rv3d->viewmat, viewmat);
1936         else
1937                 setviewmatrixview3d(scene, v3d, rv3d);  /* note: calls where_is_object for camera... */
1938         
1939         /* update utilitity matrices */
1940         mul_m4_m4m4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1941         invert_m4_m4(rv3d->persinv, rv3d->persmat);
1942         invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
1943         
1944         /* calculate pixelsize factor once, is used for lamps and obcenters */
1945         {
1946                 float len1, len2, vec[3];
1947                 
1948                 copy_v3_v3(vec, rv3d->persinv[0]);
1949                 len1= normalize_v3(vec);
1950                 copy_v3_v3(vec, rv3d->persinv[1]);
1951                 len2= normalize_v3(vec);
1952                 
1953                 rv3d->pixsize= 2.0f*(len1>len2?len1:len2);
1954                 
1955                 /* correct for window size */
1956                 if(ar->winx > ar->winy) rv3d->pixsize/= (float)ar->winx;
1957                 else rv3d->pixsize/= (float)ar->winy;
1958         }
1959         
1960         /* set for opengl */
1961         glMatrixMode(GL_PROJECTION);
1962         glLoadMatrixf(rv3d->winmat);
1963         glMatrixMode(GL_MODELVIEW);
1964         glLoadMatrixf(rv3d->viewmat);
1965 }
1966
1967 void ED_view3d_draw_offscreen(Scene *scene, View3D *v3d, ARegion *ar, int winx, int winy, float viewmat[][4], float winmat[][4])
1968 {
1969         Scene *sce;
1970         Base *base;
1971         float backcol[3];
1972         int bwinx, bwiny;
1973
1974         glPushMatrix();
1975
1976         /* set temporary new size */
1977         bwinx= ar->winx;
1978         bwiny= ar->winy;
1979         ar->winx= winx;
1980         ar->winy= winy;
1981         
1982         /* set flags */
1983         G.f |= G_RENDER_OGL;
1984
1985         /* free images which can have changed on frame-change
1986          * warning! can be slow so only free animated images - campbell */
1987         GPU_free_images_anim();
1988
1989         /* set background color, fallback on the view background color */
1990         if(scene->world) {
1991                 if(scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
1992                         linearrgb_to_srgb_v3_v3(backcol, &scene->world->horr);
1993                 else
1994                         copy_v3_v3(backcol, &scene->world->horr);
1995                 glClearColor(backcol[0], backcol[1], backcol[2], 0.0);
1996         }
1997         else {
1998                 UI_ThemeClearColor(TH_BACK);    
1999         }
2000
2001         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
2002
2003         /* setup view matrices */
2004         view3d_main_area_setup_view(scene, v3d, ar, viewmat, winmat);
2005
2006         /* set zbuffer */
2007         if(v3d->drawtype > OB_WIRE) {
2008                 v3d->zbuf= TRUE;
2009                 glEnable(GL_DEPTH_TEST);
2010         }
2011         else
2012                 v3d->zbuf= FALSE;
2013
2014         /* draw set first */
2015         if(scene->set) {
2016                 for(SETLOOPER(scene->set, base)) {
2017                         if(v3d->lay & base->lay) {
2018                                 UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
2019                                 draw_object(scene, ar, v3d, base, DRAW_CONSTCOLOR|DRAW_SCENESET);
2020                                 
2021                                 if(base->object->transflag & OB_DUPLI)
2022                                         draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
2023                         }
2024                 }
2025         }
2026         
2027         /* then draw not selected and the duplis, but skip editmode object */
2028         for(base= scene->base.first; base; base= base->next) {
2029                 if(v3d->lay & base->lay) {
2030                         /* dupli drawing */
2031                         if(base->object->transflag & OB_DUPLI)
2032                                 draw_dupli_objects(scene, ar, v3d, base);
2033
2034                         draw_object(scene, ar, v3d, base, 0);
2035                 }
2036         }
2037
2038         /* transp and X-ray afterdraw stuff */
2039         view3d_draw_transp(scene, ar, v3d);
2040         view3d_draw_xray(scene, ar, v3d, 1);    // clears zbuffer if it is used!
2041
2042         /* cleanup */
2043         if(v3d->zbuf) {
2044                 v3d->zbuf= FALSE;
2045                 glDisable(GL_DEPTH_TEST);
2046         }
2047
2048         /* draw grease-pencil stuff */
2049         draw_gpencil_view3d_ext(scene, ar, 1);
2050
2051         ED_region_pixelspace(ar);
2052
2053         /* draw grease-pencil stuff - needed to get paint-buffer shown too (since it's 2D) */
2054         draw_gpencil_view3d_ext(scene, ar, 0);
2055
2056         /* freeing the images again here could be done after the operator runs, leaving for now */
2057         GPU_free_images_anim();
2058
2059         /* restore size */
2060         ar->winx= bwinx;
2061         ar->winy= bwiny;
2062
2063         glPopMatrix();
2064
2065         glColor4ub(255, 255, 255, 255); // XXX, without this the sequencer flickers with opengl draw enabled, need to find out why - campbell
2066
2067         G.f &= ~G_RENDER_OGL;
2068 }
2069
2070 /* utility func for ED_view3d_draw_offscreen */
2071 ImBuf *ED_view3d_draw_offscreen_imbuf(Scene *scene, View3D *v3d, ARegion *ar, int sizex, int sizey)
2072 {
2073         RegionView3D *rv3d= ar->regiondata;
2074         ImBuf *ibuf;
2075         GPUOffScreen *ofs;
2076
2077         /* bind */
2078         ofs= GPU_offscreen_create(sizex, sizey);
2079         if(ofs == NULL)
2080                 return NULL;
2081
2082         GPU_offscreen_bind(ofs);
2083
2084         /* render 3d view */
2085         if(rv3d->persp==RV3D_CAMOB && v3d->camera) {
2086                 float winmat[4][4];
2087                 float _clipsta, _clipend, _lens, _yco, _dx, _dy;
2088                 rctf _viewplane;
2089
2090                 object_camera_matrix(&scene->r, v3d->camera, sizex, sizey, 0, winmat, &_viewplane, &_clipsta, &_clipend, &_lens, &_yco, &_dx, &_dy);
2091
2092                 ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, winmat);
2093         }
2094         else {
2095                 ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, NULL);
2096         }
2097
2098         /* read in pixels & stamp */
2099         ibuf= IMB_allocImBuf(sizex, sizey, 24, IB_rect, 0);
2100         glReadPixels(0, 0, sizex, sizey, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
2101
2102         //if((scene->r.stamp & R_STAMP_ALL) && (scene->r.stamp & R_STAMP_DRAW))
2103         //      BKE_stamp_buf(scene, NULL, rr->rectf, rr->rectx, rr->recty, 4);
2104
2105         /* unbind */
2106         GPU_offscreen_unbind(ofs);
2107         GPU_offscreen_free(ofs);
2108
2109         return ibuf;
2110 }
2111
2112 /* creates own 3d views, used by the sequencer */
2113 ImBuf *ED_view3d_draw_offscreen_imbuf_simple(Scene *scene, int width, int height, int drawtype)
2114 {
2115         View3D v3d;
2116         ARegion ar;
2117         RegionView3D rv3d;
2118
2119         memset(&v3d, 0, sizeof(v3d));
2120         memset(&ar, 0, sizeof(ar));
2121         memset(&rv3d, 0, sizeof(rv3d));
2122
2123         /* connect data */
2124         v3d.regionbase.first= v3d.regionbase.last= &ar;
2125         ar.regiondata= &rv3d;
2126         ar.regiontype= RGN_TYPE_WINDOW;
2127
2128         v3d.camera= scene->camera;
2129         v3d.lay= scene->lay;
2130         v3d.drawtype = drawtype;
2131         v3d.flag2 = V3D_RENDER_OVERRIDE;
2132
2133         rv3d.persp= RV3D_CAMOB;
2134
2135         copy_m4_m4(rv3d.viewinv, v3d.camera->obmat);
2136         normalize_m4(rv3d.viewinv);
2137         invert_m4_m4(rv3d.viewmat, rv3d.viewinv);
2138
2139         {
2140                 float _yco, _dx, _dy;
2141                 rctf _viewplane;
2142                 object_camera_matrix(&scene->r, v3d.camera, width, height, 0, rv3d.winmat, &_viewplane, &v3d.near, &v3d.far, &v3d.lens, &_yco, &_dx, &_dy);
2143         }
2144
2145         mul_m4_m4m4(rv3d.persmat, rv3d.viewmat, rv3d.winmat);
2146         invert_m4_m4(rv3d.persinv, rv3d.viewinv);
2147
2148         return ED_view3d_draw_offscreen_imbuf(scene, &v3d, &ar, width, height);
2149
2150         // seq_view3d_cb(scene, cfra, render_size, seqrectx, seqrecty);
2151 }
2152
2153
2154 /* NOTE: the info that this uses is updated in ED_refresh_viewport_fps(), 
2155  * which currently gets called during SCREEN_OT_animation_step.
2156  */
2157 static void draw_viewport_fps(Scene *scene, ARegion *ar)
2158 {
2159         ScreenFrameRateInfo *fpsi= scene->fps_info;
2160         float fps;
2161         char printable[16];
2162         int i, tot;
2163         
2164         if (!fpsi || !fpsi->lredrawtime || !fpsi->redrawtime)
2165                 return;
2166         
2167         printable[0] = '\0';
2168         
2169 #if 0
2170         /* this is too simple, better do an average */
2171         fps = (float)(1.0/(fpsi->lredrawtime-fpsi->redrawtime))
2172 #else
2173         fpsi->redrawtimes_fps[fpsi->redrawtime_index] = (float)(1.0/(fpsi->lredrawtime-fpsi->redrawtime));
2174         
2175         for (i=0, tot=0, fps=0.0f ; i < REDRAW_FRAME_AVERAGE ; i++) {
2176                 if (fpsi->redrawtimes_fps[i]) {
2177                         fps += fpsi->redrawtimes_fps[i];
2178                         tot++;
2179                 }
2180         }
2181         if (tot) {
2182                 fpsi->redrawtime_index = (fpsi->redrawtime_index + 1) % REDRAW_FRAME_AVERAGE;
2183                 
2184                 //fpsi->redrawtime_index++;
2185                 //if (fpsi->redrawtime >= REDRAW_FRAME_AVERAGE)
2186                 //      fpsi->redrawtime = 0;
2187                 
2188                 fps = fps / tot;
2189         }
2190 #endif
2191
2192         /* is this more then half a frame behind? */
2193         if (fps+0.5 < FPS) {
2194                 UI_ThemeColor(TH_REDALERT);
2195                 BLI_snprintf(printable, sizeof(printable), "fps: %.2f", (float)fps);
2196         } 
2197         else {
2198                 UI_ThemeColor(TH_TEXT_HI);
2199                 BLI_snprintf(printable, sizeof(printable), "fps: %i", (int)(fps+0.5));
2200         }
2201         
2202         BLF_draw_default(22,  ar->winy-17, 0.0f, printable);
2203 }
2204
2205 void view3d_main_area_draw(const bContext *C, ARegion *ar)
2206 {
2207         Scene *scene= CTX_data_scene(C);
2208         View3D *v3d = CTX_wm_view3d(C);
2209         RegionView3D *rv3d= CTX_wm_region_view3d(C);
2210         Scene *sce;
2211         Base *base;
2212         Object *ob;
2213         float backcol[3];
2214         int retopo= 0, sculptparticle= 0;
2215         Object *obact = OBACT;
2216         char *grid_unit= NULL;
2217
2218         /* from now on all object derived meshes check this */
2219         v3d->customdata_mask= get_viewedit_datamask(CTX_wm_screen(C), scene, obact);
2220
2221         /* shadow buffers, before we setup matrices */
2222         if(draw_glsl_material(scene, NULL, v3d, v3d->drawtype))
2223                 gpu_update_lamps_shadows(scene, v3d);
2224         
2225         /* reset default OpenGL lights if needed (i.e. after preferences have been altered) */
2226         if (rv3d->rflag & RV3D_GPULIGHT_UPDATE) {
2227                 rv3d->rflag &= ~RV3D_GPULIGHT_UPDATE;
2228                 GPU_default_lights();
2229         }
2230
2231         /* clear background */
2232         if((v3d->flag2 & V3D_RENDER_OVERRIDE) && scene->world) {
2233                 if(scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
2234                         linearrgb_to_srgb_v3_v3(backcol, &scene->world->horr);
2235                 else
2236                         copy_v3_v3(backcol, &scene->world->horr);
2237                 glClearColor(backcol[0], backcol[1], backcol[2], 0.0);
2238         }
2239         else
2240                 UI_ThemeClearColor(TH_BACK);
2241
2242         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
2243         
2244         /* setup view matrices */
2245         view3d_main_area_setup_view(scene, v3d, ar, NULL, NULL);
2246
2247         ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
2248
2249         if(rv3d->rflag & RV3D_CLIPPING)
2250                 view3d_draw_clipping(rv3d);
2251         
2252         /* set zbuffer after we draw clipping region */
2253         if(v3d->drawtype > OB_WIRE) {
2254                 v3d->zbuf= TRUE;
2255                 glEnable(GL_DEPTH_TEST);
2256         }
2257         else
2258                 v3d->zbuf= FALSE;
2259
2260         /* enables anti-aliasing for 3D view drawing */
2261         /*if (!(U.gameflags & USER_DISABLE_AA))
2262                 glEnable(GL_MULTISAMPLE_ARB);*/
2263         
2264         // needs to be done always, gridview is adjusted in drawgrid() now
2265         rv3d->gridview= v3d->grid;
2266         
2267         if ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
2268
2269                 if(rv3d->view==0 || rv3d->persp != RV3D_ORTHO) {
2270                         drawfloor(scene, v3d);
2271                         if(rv3d->persp==RV3D_CAMOB) {
2272                                 if(scene->world) {
2273                                         if(scene->world->mode & WO_STARS) {
2274                                                 RE_make_stars(NULL, scene, star_stuff_init_func, star_stuff_vertex_func,
2275                                                                           star_stuff_term_func);
2276                                         }
2277                                 }
2278                                 if(v3d->flag & V3D_DISPBGPICS) draw_bgpic(scene, ar, v3d);
2279                         }
2280                 }
2281                 else {
2282                         ED_region_pixelspace(ar);
2283                         drawgrid(&scene->unit, ar, v3d, &grid_unit);
2284                         /* XXX make function? replaces persp(1) */
2285                         glMatrixMode(GL_PROJECTION);
2286                         glLoadMatrixf(rv3d->winmat);
2287                         glMatrixMode(GL_MODELVIEW);
2288                         glLoadMatrixf(rv3d->viewmat);
2289
2290                         if(v3d->flag & V3D_DISPBGPICS) {
2291                                 draw_bgpic(scene, ar, v3d);
2292                         }
2293                 }
2294         }
2295         
2296         if(rv3d->rflag & RV3D_CLIPPING)
2297                 view3d_set_clipping(rv3d);
2298
2299         /* draw set first */
2300         if(scene->set) {
2301                 for(SETLOOPER(scene->set, base)) {
2302                         
2303                         if(v3d->lay & base->lay) {
2304                                 
2305                                 UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
2306                                 draw_object(scene, ar, v3d, base, DRAW_CONSTCOLOR|DRAW_SCENESET);
2307                                 
2308                                 if(base->object->transflag & OB_DUPLI) {
2309                                         draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
2310                                 }
2311                         }
2312                 }
2313                 
2314                 /* Transp and X-ray afterdraw stuff for sets is done later */
2315         }
2316         
2317         /* extra service in layerbuttons, showing used layers */
2318         v3d->lay_used = 0;
2319
2320         /* then draw not selected and the duplis, but skip editmode object */
2321         for(base= scene->base.first; base; base= base->next) {
2322                 v3d->lay_used |= base->lay;
2323                 
2324                 if(v3d->lay & base->lay) {
2325                         
2326                         /* dupli drawing */
2327                         if(base->object->transflag & OB_DUPLI) {
2328                                 draw_dupli_objects(scene, ar, v3d, base);
2329                         }
2330                         if((base->flag & SELECT)==0) {
2331                                 if(base->object!=scene->obedit) 
2332                                         draw_object(scene, ar, v3d, base, 0);
2333                         }
2334                 }
2335         }
2336
2337 //      retopo= retopo_mesh_check() || retopo_curve_check();
2338         sculptparticle= (obact && obact->mode & (OB_MODE_PARTICLE_EDIT)) && !scene->obedit;
2339         if(retopo)
2340                 view3d_update_depths(ar, v3d);
2341         
2342         /* draw selected and editmode */
2343         for(base= scene->base.first; base; base= base->next) {
2344                 if(v3d->lay & base->lay) {
2345                         if (base->object==scene->obedit || ( base->flag & SELECT) ) 
2346                                 draw_object(scene, ar, v3d, base, 0);
2347                 }
2348         }
2349         
2350         if(!retopo && sculptparticle && !(obact && (obact->dtx & OB_DRAWXRAY))) {
2351                 view3d_update_depths(ar, v3d);
2352         }
2353         
2354 //      REEB_draw();
2355         
2356         /* Transp and X-ray afterdraw stuff */
2357         view3d_draw_transp(scene, ar, v3d);
2358         view3d_draw_xray(scene, ar, v3d, 1);    // clears zbuffer if it is used!
2359         
2360         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
2361         
2362         if(!retopo && sculptparticle && (obact && (OBACT->dtx & OB_DRAWXRAY))) {
2363                 view3d_update_depths(ar, v3d);
2364         }
2365         
2366         if(rv3d->rflag & RV3D_CLIPPING)
2367                 view3d_clr_clipping();
2368         
2369         BIF_draw_manipulator(C);
2370         
2371         /* Disable back anti-aliasing */
2372         /*if (!(U.gameflags & USER_DISABLE_AA))
2373                 glDisable(GL_MULTISAMPLE_ARB);*/
2374
2375         if(v3d->zbuf) {
2376                 v3d->zbuf= FALSE;
2377                 glDisable(GL_DEPTH_TEST);
2378         }
2379         
2380         if ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
2381                 /* draw grease-pencil stuff (3d-space strokes) */
2382                 //if (v3d->flag2 & V3D_DISPGP)
2383                         draw_gpencil_view3d((bContext *)C, 1);
2384
2385                 BDR_drawSketch(C);
2386         }
2387
2388         ED_region_pixelspace(ar);
2389         
2390 //      retopo_paint_view_update(v3d);
2391 //      retopo_draw_paint_lines();
2392         
2393         /* Draw particle edit brush XXX (removed) */
2394         
2395
2396         if(rv3d->persp==RV3D_CAMOB) drawviewborder(scene, ar, v3d);
2397         if(rv3d->rflag & RV3D_FLYMODE) drawviewborder_flymode(ar);
2398
2399         if ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
2400                 /* draw grease-pencil stuff - needed to get paint-buffer shown too (since it's 2D) */
2401         //      if (v3d->flag2 & V3D_DISPGP)
2402                         draw_gpencil_view3d((bContext *)C, 0);
2403
2404                 drawcursor(scene, ar, v3d);
2405         }
2406         
2407         if(U.uiflag & USER_SHOW_ROTVIEWICON)
2408                 draw_view_axis(rv3d);
2409         else    
2410                 draw_view_icon(rv3d);
2411         
2412         if((U.uiflag & USER_SHOW_FPS) && (CTX_wm_screen(C)->animtimer)) {
2413                 draw_viewport_fps(scene, ar);
2414         }
2415         else if(U.uiflag & USER_SHOW_VIEWPORTNAME) {
2416                 draw_viewport_name(ar, v3d);
2417         }
2418         if (grid_unit) { /* draw below the viewport name */
2419                 UI_ThemeColor(TH_TEXT_HI);
2420                 BLF_draw_default(22,  ar->winy-(USER_SHOW_VIEWPORTNAME?40:20), 0.0f, grid_unit);
2421         }
2422
2423         ob= OBACT;
2424         if(U.uiflag & USER_DRAWVIEWINFO) 
2425                 draw_selected_name(scene, ob, v3d);
2426
2427         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_PIXEL);
2428         
2429         /* XXX here was the blockhandlers for floating panels */
2430
2431         v3d->flag |= V3D_INVALID_BACKBUF;
2432 }