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