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