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