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