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