2.5/Posemode:
[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_GetNamePlural(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
1096 /*for 2.43 release, don't use glext and just define the constant.
1097   this to avoid possibly breaking platforms before release.*/
1098 #ifndef GL_MULTISAMPLE_ARB
1099         #define GL_MULTISAMPLE_ARB      0x809D
1100 #endif
1101
1102 #ifdef GL_MULTISAMPLE_ARB
1103         int m;
1104 #endif
1105
1106         if(base && (base->object->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT) ||
1107                      paint_facesel_test(base->object)));
1108         else if((base && (base->object->mode & OB_MODE_TEXTURE_PAINT)) &&
1109                 scene->toolsettings && (scene->toolsettings->imapaint.flag & IMAGEPAINT_PROJECT_DISABLE));
1110         else if((base && (base->object->mode & OB_MODE_PARTICLE_EDIT)) && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT));
1111         else if(scene->obedit && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT));
1112         else {
1113                 v3d->flag &= ~V3D_NEEDBACKBUFDRAW;
1114                 return;
1115         }
1116
1117         if( !(v3d->flag & V3D_NEEDBACKBUFDRAW) ) return;
1118
1119 //      if(test) {
1120 //              if(qtest()) {
1121 //                      addafterqueue(ar->win, BACKBUFDRAW, 1);
1122 //                      return;
1123 //              }
1124 //      }
1125
1126         /* Disable FSAA for backbuffer selection.  
1127         
1128         Only works if GL_MULTISAMPLE_ARB is defined by the header
1129         file, which is should be for every OS that supports FSAA.*/
1130
1131 #ifdef GL_MULTISAMPLE_ARB
1132         m = glIsEnabled(GL_MULTISAMPLE_ARB);
1133         if (m) glDisable(GL_MULTISAMPLE_ARB);
1134 #endif
1135
1136         if(v3d->drawtype > OB_WIRE) v3d->zbuf= TRUE;
1137         
1138         glDisable(GL_DITHER);
1139
1140         glClearColor(0.0, 0.0, 0.0, 0.0); 
1141         if(v3d->zbuf) {
1142                 glEnable(GL_DEPTH_TEST);
1143                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1144         }
1145         else {
1146                 glClear(GL_COLOR_BUFFER_BIT);
1147                 glDisable(GL_DEPTH_TEST);
1148         }
1149         
1150         if(rv3d->rflag & RV3D_CLIPPING)
1151                 view3d_set_clipping(rv3d);
1152         
1153         G.f |= G_BACKBUFSEL;
1154         
1155         if(base && (base->lay & v3d->lay)) {
1156                 draw_object_backbufsel(scene, v3d, rv3d, base->object);
1157         }
1158
1159         v3d->flag &= ~V3D_NEEDBACKBUFDRAW;
1160
1161         G.f &= ~G_BACKBUFSEL;
1162         v3d->zbuf= FALSE; 
1163         glDisable(GL_DEPTH_TEST);
1164         glEnable(GL_DITHER);
1165
1166         if(rv3d->rflag & RV3D_CLIPPING)
1167                 view3d_clr_clipping();
1168
1169 #ifdef GL_MULTISAMPLE_ARB
1170         if (m) glEnable(GL_MULTISAMPLE_ARB);
1171 #endif
1172
1173         /* it is important to end a view in a transform compatible with buttons */
1174 //      persp(PERSP_WIN);  // set ortho
1175
1176 }
1177
1178 void view3d_validate_backbuf(ViewContext *vc)
1179 {
1180         if(vc->v3d->flag & V3D_NEEDBACKBUFDRAW)
1181                 backdrawview3d(vc->scene, vc->ar, vc->v3d);
1182 }
1183
1184 /* samples a single pixel (copied from vpaint) */
1185 unsigned int view3d_sample_backbuf(ViewContext *vc, int x, int y)
1186 {
1187         unsigned int col;
1188         
1189         if(x >= vc->ar->winx || y >= vc->ar->winy) return 0;
1190         x+= vc->ar->winrct.xmin;
1191         y+= vc->ar->winrct.ymin;
1192         
1193         view3d_validate_backbuf(vc);
1194
1195         glReadPixels(x,  y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,  &col);
1196         glReadBuffer(GL_BACK);  
1197         
1198         if(ENDIAN_ORDER==B_ENDIAN) SWITCH_INT(col);
1199         
1200         return WM_framebuffer_to_index(col);
1201 }
1202
1203 /* reads full rect, converts indices */
1204 ImBuf *view3d_read_backbuf(ViewContext *vc, short xmin, short ymin, short xmax, short ymax)
1205 {
1206         unsigned int *dr, *rd;
1207         struct ImBuf *ibuf, *ibuf1;
1208         int a;
1209         short xminc, yminc, xmaxc, ymaxc, xs, ys;
1210         
1211         /* clip */
1212         if(xmin<0) xminc= 0; else xminc= xmin;
1213         if(xmax >= vc->ar->winx) xmaxc= vc->ar->winx-1; else xmaxc= xmax;
1214         if(xminc > xmaxc) return NULL;
1215
1216         if(ymin<0) yminc= 0; else yminc= ymin;
1217         if(ymax >= vc->ar->winy) ymaxc= vc->ar->winy-1; else ymaxc= ymax;
1218         if(yminc > ymaxc) return NULL;
1219         
1220         ibuf= IMB_allocImBuf((xmaxc-xminc+1), (ymaxc-yminc+1), 32, IB_rect,0);
1221
1222         view3d_validate_backbuf(vc); 
1223         
1224         glReadPixels(vc->ar->winrct.xmin+xminc, vc->ar->winrct.ymin+yminc, (xmaxc-xminc+1), (ymaxc-yminc+1), GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
1225         glReadBuffer(GL_BACK);  
1226
1227         if(ENDIAN_ORDER==B_ENDIAN) IMB_convert_rgba_to_abgr(ibuf);
1228
1229         a= (xmaxc-xminc+1)*(ymaxc-yminc+1);
1230         dr= ibuf->rect;
1231         while(a--) {
1232                 if(*dr) *dr= WM_framebuffer_to_index(*dr);
1233                 dr++;
1234         }
1235         
1236         /* put clipped result back, if needed */
1237         if(xminc==xmin && xmaxc==xmax && yminc==ymin && ymaxc==ymax) 
1238                 return ibuf;
1239         
1240         ibuf1= IMB_allocImBuf( (xmax-xmin+1),(ymax-ymin+1),32,IB_rect,0);
1241         rd= ibuf->rect;
1242         dr= ibuf1->rect;
1243                 
1244         for(ys= ymin; ys<=ymax; ys++) {
1245                 for(xs= xmin; xs<=xmax; xs++, dr++) {
1246                         if( xs>=xminc && xs<=xmaxc && ys>=yminc && ys<=ymaxc) {
1247                                 *dr= *rd;
1248                                 rd++;
1249                         }
1250                 }
1251         }
1252         IMB_freeImBuf(ibuf);
1253         return ibuf1;
1254 }
1255
1256 /* smart function to sample a rect spiralling outside, nice for backbuf selection */
1257 unsigned int view3d_sample_backbuf_rect(ViewContext *vc, short mval[2], int size, 
1258                                                                                 unsigned int min, unsigned int max, int *dist, short strict, 
1259                                                                                 void *handle, unsigned int (*indextest)(void *handle, unsigned int index))
1260 {
1261         struct ImBuf *buf;
1262         unsigned int *bufmin, *bufmax, *tbuf;
1263         int minx, miny;
1264         int a, b, rc, nr, amount, dirvec[4][2];
1265         int distance=0;
1266         unsigned int index = 0;
1267         short indexok = 0;      
1268
1269         amount= (size-1)/2;
1270
1271         minx = mval[0]-(amount+1);
1272         miny = mval[1]-(amount+1);
1273         buf = view3d_read_backbuf(vc, minx, miny, minx+size-1, miny+size-1);
1274         if (!buf) return 0;
1275
1276         rc= 0;
1277         
1278         dirvec[0][0]= 1; dirvec[0][1]= 0;
1279         dirvec[1][0]= 0; dirvec[1][1]= -size;
1280         dirvec[2][0]= -1; dirvec[2][1]= 0;
1281         dirvec[3][0]= 0; dirvec[3][1]= size;
1282         
1283         bufmin = buf->rect;
1284         tbuf = buf->rect;
1285         bufmax = buf->rect + size*size;
1286         tbuf+= amount*size+ amount;
1287         
1288         for(nr=1; nr<=size; nr++) {
1289                 
1290                 for(a=0; a<2; a++) {
1291                         for(b=0; b<nr; b++, distance++) {
1292                                 if (*tbuf && *tbuf>=min && *tbuf<max) { //we got a hit
1293                                         if(strict){
1294                                                 indexok =  indextest(handle, *tbuf - min+1);
1295                                                 if(indexok){
1296                                                         *dist= (short) sqrt( (float)distance   );
1297                                                         index = *tbuf - min+1;
1298                                                         goto exit; 
1299                                                 }                                               
1300                                         }
1301                                         else{
1302                                                 *dist= (short) sqrt( (float)distance ); // XXX, this distance is wrong - 
1303                                                 index = *tbuf - min+1; // messy yah, but indices start at 1
1304                                                 goto exit;
1305                                         }                       
1306                                 }
1307                                 
1308                                 tbuf+= (dirvec[rc][0]+dirvec[rc][1]);
1309                                 
1310                                 if(tbuf<bufmin || tbuf>=bufmax) {
1311                                         goto exit;
1312                                 }
1313                         }
1314                         rc++;
1315                         rc &= 3;
1316                 }
1317         }
1318
1319 exit:
1320         IMB_freeImBuf(buf);
1321         return index;
1322 }
1323
1324
1325 /* ************************************************************* */
1326
1327 static void draw_bgpic(Scene *scene, ARegion *ar, View3D *v3d)
1328 {
1329         RegionView3D *rv3d= ar->regiondata;
1330         BGpic *bgpic;
1331         Image *ima;
1332         ImBuf *ibuf= NULL;
1333         float vec[4], fac, asp, zoomx, zoomy;
1334         float x1, y1, x2, y2, cx, cy;
1335         
1336         bgpic= v3d->bgpic;
1337         if(bgpic==NULL) return;
1338         
1339         ima= bgpic->ima;
1340         
1341         if(ima)
1342                 ibuf= BKE_image_get_ibuf(ima, &bgpic->iuser);
1343         if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL) ) 
1344                 return;
1345         if(ibuf->channels!=4)
1346                 return;
1347         if(ibuf->rect==NULL)
1348                 IMB_rect_from_float(ibuf);
1349         
1350         if(rv3d->persp==2) {
1351                 rctf vb;
1352                 
1353                 calc_viewborder(scene, ar, v3d, &vb);
1354                 
1355                 x1= vb.xmin;
1356                 y1= vb.ymin;
1357                 x2= vb.xmax;
1358                 y2= vb.ymax;
1359         }
1360         else {
1361                 float sco[2];
1362                 
1363                 /* calc window coord */
1364                 initgrabz(rv3d, 0.0, 0.0, 0.0);
1365                 window_to_3d_delta(ar, vec, 1, 0);
1366                 fac= MAX3( fabs(vec[0]), fabs(vec[1]), fabs(vec[1]) );
1367                 fac= 1.0/fac;
1368                 
1369                 asp= ( (float)ibuf->y)/(float)ibuf->x;
1370                 
1371                 vec[0] = vec[1] = vec[2] = 0.0;
1372                 view3d_project_float(ar, vec, sco, rv3d->persmat);
1373                 cx = sco[0];
1374                 cy = sco[1];
1375                 
1376                 x1=  cx+ fac*(bgpic->xof-bgpic->size);
1377                 y1=  cy+ asp*fac*(bgpic->yof-bgpic->size);
1378                 x2=  cx+ fac*(bgpic->xof+bgpic->size);
1379                 y2=  cy+ asp*fac*(bgpic->yof+bgpic->size);
1380         }
1381         
1382         /* complete clip? */
1383         
1384         if(x2 < 0 ) return;
1385         if(y2 < 0 ) return;
1386         if(x1 > ar->winx ) return;
1387         if(y1 > ar->winy ) return;
1388         
1389         zoomx= (x2-x1)/ibuf->x;
1390         zoomy= (y2-y1)/ibuf->y;
1391         
1392         /* for some reason; zoomlevels down refuses to use GL_ALPHA_SCALE */
1393         if(zoomx < 1.0f || zoomy < 1.0f) {
1394                 float tzoom= MIN2(zoomx, zoomy);
1395                 int mip= 0;
1396                 
1397                 if(ibuf->mipmap[0]==NULL)
1398                         IMB_makemipmap(ibuf, 0, 0);
1399                 
1400                 while(tzoom < 1.0f && mip<8 && ibuf->mipmap[mip]) {
1401                         tzoom*= 2.0f;
1402                         zoomx*= 2.0f;
1403                         zoomy*= 2.0f;
1404                         mip++;
1405                 }
1406                 if(mip>0)
1407                         ibuf= ibuf->mipmap[mip-1];
1408         }
1409         
1410         if(v3d->zbuf) glDisable(GL_DEPTH_TEST);
1411         
1412         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
1413         
1414         glMatrixMode(GL_PROJECTION);
1415         glPushMatrix();
1416         glMatrixMode(GL_MODELVIEW);
1417         glPushMatrix();
1418         
1419         glaDefine2DArea(&ar->winrct);
1420         
1421         glEnable(GL_BLEND);
1422         
1423         glPixelZoom(zoomx, zoomy);
1424         glColor4f(1.0, 1.0, 1.0, 1.0-bgpic->blend);
1425         glaDrawPixelsTex(x1, y1, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
1426         
1427         glPixelZoom(1.0, 1.0);
1428         glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
1429         
1430         glMatrixMode(GL_PROJECTION);
1431         glPopMatrix();
1432         glMatrixMode(GL_MODELVIEW);
1433         glPopMatrix();
1434         
1435         glDisable(GL_BLEND);
1436         if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
1437 }
1438
1439 /* ****************** View3d afterdraw *************** */
1440
1441 typedef struct View3DAfter {
1442         struct View3DAfter *next, *prev;
1443         struct Base *base;
1444         int type, flag;
1445 } View3DAfter;
1446
1447 /* temp storage of Objects that need to be drawn as last */
1448 void add_view3d_after(View3D *v3d, Base *base, int type, int flag)
1449 {
1450         View3DAfter *v3da= MEM_callocN(sizeof(View3DAfter), "View 3d after");
1451         
1452         BLI_addtail(&v3d->afterdraw, v3da);
1453         v3da->base= base;
1454         v3da->type= type;
1455         v3da->flag= flag;
1456 }
1457
1458 /* clears zbuffer and draws it over */
1459 static void view3d_draw_xray(Scene *scene, ARegion *ar, View3D *v3d, int clear)
1460 {
1461         View3DAfter *v3da, *next;
1462         int doit= 0;
1463         
1464         for(v3da= v3d->afterdraw.first; v3da; v3da= v3da->next)
1465                 if(v3da->type==V3D_XRAY) doit= 1;
1466         
1467         if(doit) {
1468                 if(clear && v3d->zbuf) glClear(GL_DEPTH_BUFFER_BIT);
1469                 v3d->xray= TRUE;
1470                 
1471                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1472                         next= v3da->next;
1473                         if(v3da->type==V3D_XRAY) {
1474                                 draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1475                                 BLI_remlink(&v3d->afterdraw, v3da);
1476                                 MEM_freeN(v3da);
1477                         }
1478                 }
1479                 v3d->xray= FALSE;
1480         }
1481 }
1482
1483 /* disables write in zbuffer and draws it over */
1484 static void view3d_draw_transp(Scene *scene, ARegion *ar, View3D *v3d)
1485 {
1486         View3DAfter *v3da, *next;
1487         
1488         glDepthMask(0);
1489         v3d->transp= TRUE;
1490         
1491         for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1492                 next= v3da->next;
1493                 if(v3da->type==V3D_TRANSP) {
1494                         draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1495                         BLI_remlink(&v3d->afterdraw, v3da);
1496                         MEM_freeN(v3da);
1497                 }
1498         }
1499         v3d->transp= FALSE;
1500         
1501         glDepthMask(1);
1502         
1503 }
1504
1505 /* *********************** */
1506
1507 /*
1508         In most cases call draw_dupli_objects,
1509         draw_dupli_objects_color was added because when drawing set dupli's
1510         we need to force the color
1511  */
1512 static void draw_dupli_objects_color(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int color)
1513 {       
1514         RegionView3D *rv3d= ar->regiondata;
1515         ListBase *lb;
1516         DupliObject *dob;
1517         Base tbase;
1518         BoundBox *bb= NULL;
1519         GLuint displist=0;
1520         short transflag, use_displist= -1;      /* -1 is initialize */
1521         char dt, dtx;
1522         
1523         if (base->object->restrictflag & OB_RESTRICT_VIEW) return;
1524         
1525         tbase.flag= OB_FROMDUPLI|base->flag;
1526         lb= object_duplilist(scene, base->object);
1527         
1528         for(dob= lb->first; dob; dob= dob->next) {
1529                 if(dob->no_draw);
1530                 else {
1531                         tbase.object= dob->ob;
1532                         
1533                         /* extra service: draw the duplicator in drawtype of parent */
1534                         /* MIN2 for the drawtype to allow bounding box objects in groups for lods */
1535                         dt= tbase.object->dt;   tbase.object->dt= MIN2(tbase.object->dt, base->object->dt);
1536                         dtx= tbase.object->dtx; tbase.object->dtx= base->object->dtx;
1537                         
1538                         /* negative scale flag has to propagate */
1539                         transflag= tbase.object->transflag;
1540                         if(base->object->transflag & OB_NEG_SCALE)
1541                                 tbase.object->transflag ^= OB_NEG_SCALE;
1542                         
1543                         UI_ThemeColorBlend(color, TH_BACK, 0.5);
1544                         
1545                         /* generate displist, test for new object */
1546                         if(use_displist==1 && dob->prev && dob->prev->ob!=dob->ob) {
1547                                 use_displist= -1;
1548                                 glDeleteLists(displist, 1);
1549                         }
1550                         /* generate displist */
1551                         if(use_displist == -1) {
1552                                 
1553                                 /* lamp drawing messes with matrices, could be handled smarter... but this works */
1554                                 if(dob->ob->type==OB_LAMP || dob->type==OB_DUPLIGROUP)
1555                                         use_displist= 0;
1556                                 else {
1557                                         /* disable boundbox check for list creation */
1558                                         object_boundbox_flag(dob->ob, OB_BB_DISABLED, 1);
1559                                         /* need this for next part of code */
1560                                         bb= object_get_boundbox(dob->ob);
1561                                         
1562                                         Mat4One(dob->ob->obmat);        /* obmat gets restored */
1563                                         
1564                                         displist= glGenLists(1);
1565                                         glNewList(displist, GL_COMPILE);
1566                                         draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
1567                                         glEndList();
1568                                         
1569                                         use_displist= 1;
1570                                         object_boundbox_flag(dob->ob, OB_BB_DISABLED, 0);
1571                                 }
1572                         }
1573                         if(use_displist) {
1574                                 wmMultMatrix(dob->mat);
1575                                 if(boundbox_clip(rv3d, dob->mat, bb))
1576                                         glCallList(displist);
1577                                 wmLoadMatrix(rv3d->viewmat);
1578                         }
1579                         else {
1580                                 Mat4CpyMat4(dob->ob->obmat, dob->mat);
1581                                 draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
1582                         }
1583                         
1584                         tbase.object->dt= dt;
1585                         tbase.object->dtx= dtx;
1586                         tbase.object->transflag= transflag;
1587                 }
1588         }
1589         
1590         /* Transp afterdraw disabled, afterdraw only stores base pointers, and duplis can be same obj */
1591         
1592         free_object_duplilist(lb);      /* does restore */
1593         
1594         if(use_displist)
1595                 glDeleteLists(displist, 1);
1596 }
1597
1598 static void draw_dupli_objects(Scene *scene, ARegion *ar, View3D *v3d, Base *base)
1599 {
1600         /* define the color here so draw_dupli_objects_color can be called
1601         * from the set loop */
1602         
1603         int color= (base->flag & SELECT)?TH_SELECT:TH_WIRE;
1604         /* debug */
1605         if(base->object->dup_group && base->object->dup_group->id.us<1)
1606                 color= TH_REDALERT;
1607         
1608         draw_dupli_objects_color(scene, ar, v3d, base, color);
1609 }
1610
1611
1612 void view3d_update_depths(ARegion *ar, View3D *v3d)
1613 {
1614         RegionView3D *rv3d= ar->regiondata;
1615         
1616         /* Create storage for, and, if necessary, copy depth buffer */
1617         if(!rv3d->depths) rv3d->depths= MEM_callocN(sizeof(ViewDepths),"ViewDepths");
1618         if(rv3d->depths) {
1619                 ViewDepths *d= rv3d->depths;
1620                 if(d->w != ar->winx ||
1621                    d->h != ar->winy ||
1622                    !d->depths) {
1623                         d->w= ar->winx;
1624                         d->h= ar->winy;
1625                         if(d->depths)
1626                                 MEM_freeN(d->depths);
1627                         d->depths= MEM_mallocN(sizeof(float)*d->w*d->h,"View depths");
1628                         d->damaged= 1;
1629                 }
1630                 
1631                 if(d->damaged) {
1632                         glReadPixels(ar->winrct.xmin,ar->winrct.ymin,d->w,d->h,
1633                                                  GL_DEPTH_COMPONENT,GL_FLOAT, d->depths);
1634                         
1635                         glGetDoublev(GL_DEPTH_RANGE,d->depth_range);
1636                         
1637                         d->damaged= 0;
1638                 }
1639         }
1640 }
1641
1642 /* Enable sculpting in wireframe mode by drawing sculpt object only to the depth buffer */
1643 static void draw_sculpt_depths(Scene *scene, ARegion *ar, View3D *v3d)
1644 {
1645         Object *ob = OBACT;
1646         
1647         int dt= MIN2(v3d->drawtype, ob->dt);
1648         if(v3d->zbuf==0 && dt>OB_WIRE)
1649                 dt= OB_WIRE;
1650         if(dt == OB_WIRE) {
1651                 GLboolean depth_on;
1652                 int orig_vdt = v3d->drawtype;
1653                 int orig_zbuf = v3d->zbuf;
1654                 int orig_odt = ob->dt;
1655                 
1656                 glGetBooleanv(GL_DEPTH_TEST, &depth_on);
1657                 v3d->drawtype = ob->dt = OB_SOLID;
1658                 v3d->zbuf = 1;
1659                 
1660                 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1661                 glEnable(GL_DEPTH_TEST);
1662                 draw_object(scene, ar, v3d, BASACT, 0);
1663                 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1664                 if(!depth_on)
1665                         glDisable(GL_DEPTH_TEST);
1666                 
1667                 v3d->drawtype = orig_vdt;
1668                 v3d->zbuf = orig_zbuf;
1669                 ob->dt = orig_odt;
1670         }
1671 }
1672
1673 void draw_depth(Scene *scene, ARegion *ar, View3D *v3d, int (* func)(void *))
1674 {
1675         RegionView3D *rv3d= ar->regiondata;
1676         Base *base;
1677         Scene *sce;
1678         short zbuf, flag;
1679         float glalphaclip;
1680         /* temp set drawtype to solid */
1681         
1682         /* Setting these temporarily is not nice */
1683         zbuf = v3d->zbuf;
1684         flag = v3d->flag;
1685         glalphaclip = U.glalphaclip;
1686         
1687         U.glalphaclip = 0.5; /* not that nice but means we wont zoom into billboards */
1688         v3d->flag &= ~V3D_SELECT_OUTLINE;
1689         
1690         setwinmatrixview3d(ar, v3d, NULL);      /* 0= no pick rect */
1691         setviewmatrixview3d(scene, v3d, rv3d);  /* note: calls where_is_object for camera... */
1692         
1693         Mat4MulMat4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1694         Mat4Invert(rv3d->persinv, rv3d->persmat);
1695         Mat4Invert(rv3d->viewinv, rv3d->viewmat);
1696         
1697         glClear(GL_DEPTH_BUFFER_BIT);
1698         
1699         wmLoadMatrix(rv3d->viewmat);
1700 //      persp(PERSP_STORE);  // store correct view for persp(PERSP_VIEW) calls
1701         
1702         if(rv3d->rflag & RV3D_CLIPPING) {
1703                 view3d_set_clipping(rv3d);
1704         }
1705         
1706         v3d->zbuf= TRUE;
1707         glEnable(GL_DEPTH_TEST);
1708         
1709         /* draw set first */
1710         if(scene->set) {
1711                 for(SETLOOPER(scene->set, base)) {
1712                         if(v3d->lay & base->lay) {
1713                                 if (func == NULL || func(base)) {
1714                                         draw_object(scene, ar, v3d, base, 0);
1715                                         if(base->object->transflag & OB_DUPLI) {
1716                                                 draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
1717                                         }
1718                                 }
1719                         }
1720                 }
1721         }
1722         
1723         for(base= scene->base.first; base; base= base->next) {
1724                 if(v3d->lay & base->lay) {
1725                         if (func == NULL || func(base)) {
1726                                 /* dupli drawing */
1727                                 if(base->object->transflag & OB_DUPLI) {
1728                                         draw_dupli_objects(scene, ar, v3d, base);
1729                                 }
1730                                 draw_object(scene, ar, v3d, base, 0);
1731                         }
1732                 }
1733         }
1734         
1735         /* this isnt that nice, draw xray objects as if they are normal */
1736         if (v3d->afterdraw.first) {
1737                 View3DAfter *v3da, *next;
1738                 int num = 0;
1739                 v3d->xray= TRUE;
1740                 
1741                 glDepthFunc(GL_ALWAYS); /* always write into the depth bufer, overwriting front z values */
1742                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1743                         next= v3da->next;
1744                         if(v3da->type==V3D_XRAY) {
1745                                 draw_object(scene, ar, v3d, v3da->base, 0);
1746                                 num++;
1747                         }
1748                         /* dont remove this time */
1749                 }
1750                 v3d->xray= FALSE;
1751                 
1752                 glDepthFunc(GL_LEQUAL); /* Now write the depth buffer normally */
1753                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
1754                         next= v3da->next;
1755                         if(v3da->type==V3D_XRAY) {
1756                                 v3d->xray= TRUE; v3d->transp= FALSE;  
1757                         } else if (v3da->type==V3D_TRANSP) {
1758                                 v3d->xray= FALSE; v3d->transp= TRUE;
1759                         }
1760                         
1761                         draw_object(scene, ar, v3d, v3da->base, 0); /* Draw Xray or Transp objects normally */
1762                         BLI_remlink(&v3d->afterdraw, v3da);
1763                         MEM_freeN(v3da);
1764                 }
1765                 v3d->xray= FALSE;
1766                 v3d->transp= FALSE;
1767         }
1768         
1769         v3d->zbuf = zbuf;
1770         U.glalphaclip = glalphaclip;
1771         v3d->flag = flag;
1772 }
1773
1774 typedef struct View3DShadow {
1775         struct View3DShadow *next, *prev;
1776         GPULamp *lamp;
1777 } View3DShadow;
1778
1779 static void gpu_render_lamp_update(Scene *scene, View3D *v3d, Object *ob, Object *par, float obmat[][4], ListBase *shadows)
1780 {
1781         GPULamp *lamp;
1782         Lamp *la = (Lamp*)ob->data;
1783         View3DShadow *shadow;
1784         
1785         lamp = GPU_lamp_from_blender(scene, ob, par);
1786         
1787         if(lamp) {
1788                 GPU_lamp_update(lamp, ob->lay, obmat);
1789                 GPU_lamp_update_colors(lamp, la->r, la->g, la->b, la->energy);
1790                 
1791                 if((ob->lay & v3d->lay) && GPU_lamp_has_shadow_buffer(lamp)) {
1792                         shadow= MEM_callocN(sizeof(View3DShadow), "View3DShadow");
1793                         shadow->lamp = lamp;
1794                         BLI_addtail(shadows, shadow);
1795                 }
1796         }
1797 }
1798
1799 static void gpu_update_lamps_shadows(Scene *scene, View3D *v3d)
1800 {
1801         ListBase shadows;
1802         View3DShadow *shadow;
1803         Scene *sce;
1804         Base *base;
1805         Object *ob;
1806         
1807         shadows.first= shadows.last= NULL;
1808         
1809         /* update lamp transform and gather shadow lamps */
1810         for(SETLOOPER(scene, base)) {
1811                 ob= base->object;
1812                 
1813                 if(ob->type == OB_LAMP)
1814                         gpu_render_lamp_update(scene, v3d, ob, NULL, ob->obmat, &shadows);
1815                 
1816                 if (ob->transflag & OB_DUPLI) {
1817                         DupliObject *dob;
1818                         ListBase *lb = object_duplilist(scene, ob);
1819                         
1820                         for(dob=lb->first; dob; dob=dob->next)
1821                                 if(dob->ob->type==OB_LAMP)
1822                                         gpu_render_lamp_update(scene, v3d, dob->ob, ob, dob->mat, &shadows);
1823                         
1824                         free_object_duplilist(lb);
1825                 }
1826         }
1827         
1828         /* render shadows after updating all lamps, nested object_duplilist
1829                 * don't work correct since it's replacing object matrices */
1830         for(shadow=shadows.first; shadow; shadow=shadow->next) {
1831                 /* this needs to be done better .. */
1832                 float viewmat[4][4], winmat[4][4];
1833                 int drawtype, lay, winsize, flag2;
1834                 
1835                 drawtype= v3d->drawtype;
1836                 lay= v3d->lay;
1837                 flag2= v3d->flag2 & V3D_SOLID_TEX;
1838                 
1839                 v3d->drawtype = OB_SOLID;
1840                 v3d->lay &= GPU_lamp_shadow_layer(shadow->lamp);
1841                 v3d->flag2 &= ~V3D_SOLID_TEX;
1842                 
1843                 GPU_lamp_shadow_buffer_bind(shadow->lamp, viewmat, &winsize, winmat);
1844 // XXX          drawview3d_render(v3d, viewmat, winsize, winsize, winmat, 1);
1845                 GPU_lamp_shadow_buffer_unbind(shadow->lamp);
1846                 
1847                 v3d->drawtype= drawtype;
1848                 v3d->lay= lay;
1849                 v3d->flag2 |= flag2;
1850         }
1851         
1852         BLI_freelistN(&shadows);
1853 }
1854
1855 /* *********************** customdata **************** */
1856
1857 /* goes over all modes and view3d settings */
1858 static CustomDataMask get_viewedit_datamask(bScreen *screen, Object *ob)
1859 {
1860         CustomDataMask mask = CD_MASK_BAREMESH;
1861         ScrArea *sa;
1862         
1863         /* check if we need tfaces & mcols due to face select or texture paint */
1864         if(paint_facesel_test(ob) || (ob && ob->mode & OB_MODE_TEXTURE_PAINT))
1865                 mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
1866         
1867         /* check if we need tfaces & mcols due to view mode */
1868         for(sa = screen->areabase.first; sa; sa = sa->next) {
1869                 if(sa->spacetype == SPACE_VIEW3D) {
1870                         View3D *view = sa->spacedata.first;
1871                         if(view->drawtype == OB_SHADED) {
1872                                 /* this includes normals for mesh_create_shadedColors */
1873                                 mask |= CD_MASK_MTFACE | CD_MASK_MCOL | CD_MASK_NORMAL | CD_MASK_ORCO;
1874                         }
1875                         if((view->drawtype == OB_TEXTURE) || ((view->drawtype == OB_SOLID) && (view->flag2 & V3D_SOLID_TEX))) {
1876                                 mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
1877                                 
1878                                 if((G.fileflags & G_FILE_GAME_MAT) &&
1879                                    (G.fileflags & G_FILE_GAME_MAT_GLSL)) {
1880                                         mask |= CD_MASK_ORCO;
1881                                 }
1882                         }
1883                 }
1884         }
1885         
1886         /* check if we need mcols due to vertex paint or weightpaint */
1887         if(ob) {
1888                 if(ob->mode & OB_MODE_VERTEX_PAINT)
1889                         mask |= CD_MASK_MCOL;
1890                 if(ob->mode & OB_MODE_WEIGHT_PAINT)
1891                         mask |= CD_MASK_WEIGHT_MCOL;
1892                 if(ob->mode & OB_MODE_SCULPT)
1893                         mask |= CD_MASK_MDISPS;
1894         }
1895
1896         return mask;
1897 }
1898
1899 void view3d_main_area_draw(const bContext *C, ARegion *ar)
1900 {
1901         Scene *scene= CTX_data_scene(C);
1902         View3D *v3d = CTX_wm_view3d(C);
1903         RegionView3D *rv3d= CTX_wm_region_view3d(C);
1904         Scene *sce;
1905         Base *base;
1906         Object *ob;
1907         int retopo= 0, sculptparticle= 0;
1908         Object *obact = OBACT;
1909         char *grid_unit= NULL;
1910         
1911         /* from now on all object derived meshes check this */
1912         v3d->customdata_mask= get_viewedit_datamask(CTX_wm_screen(C), obact);
1913         
1914         /* shadow buffers, before we setup matrices */
1915         if(draw_glsl_material(scene, NULL, v3d, v3d->drawtype))
1916                 gpu_update_lamps_shadows(scene, v3d);
1917         
1918         setwinmatrixview3d(ar, v3d, NULL);      /* 0= no pick rect */
1919         setviewmatrixview3d(scene, v3d, rv3d);  /* note: calls where_is_object for camera... */
1920         
1921         Mat4MulMat4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
1922         Mat4Invert(rv3d->persinv, rv3d->persmat);
1923         Mat4Invert(rv3d->viewinv, rv3d->viewmat);
1924         
1925         /* calculate pixelsize factor once, is used for lamps and obcenters */
1926         {
1927                 float len1, len2, vec[3];
1928                 
1929                 VECCOPY(vec, rv3d->persinv[0]);
1930                 len1= Normalize(vec);
1931                 VECCOPY(vec, rv3d->persinv[1]);
1932                 len2= Normalize(vec);
1933                 
1934                 rv3d->pixsize= 2.0f*(len1>len2?len1:len2);
1935                 
1936                 /* correct for window size */
1937                 if(ar->winx > ar->winy) rv3d->pixsize/= (float)ar->winx;
1938                 else rv3d->pixsize/= (float)ar->winy;
1939         }
1940         
1941         if(v3d->drawtype > OB_WIRE) {
1942                 float col[3];
1943                 UI_GetThemeColor3fv(TH_BACK, col);
1944                 glClearColor(col[0], col[1], col[2], 0.0); 
1945                 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
1946                 
1947                 glLoadIdentity();
1948         }
1949         else {
1950                 float col[3];
1951                 UI_GetThemeColor3fv(TH_BACK, col);
1952                 glClearColor(col[0], col[1], col[2], 0.0);
1953                 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
1954         }
1955         
1956         wmLoadMatrix(rv3d->viewmat);
1957         
1958         if(rv3d->rflag & RV3D_CLIPPING)
1959                 view3d_draw_clipping(rv3d);
1960         
1961         /* set zbuffer after we draw clipping region */
1962         if(v3d->drawtype > OB_WIRE) {
1963                 v3d->zbuf= TRUE;
1964                 glEnable(GL_DEPTH_TEST);
1965         }
1966         
1967         // needs to be done always, gridview is adjusted in drawgrid() now
1968         v3d->gridview= v3d->grid;
1969         
1970         if(rv3d->view==0 || rv3d->persp!=0) {
1971                 drawfloor(scene, v3d);
1972                 if(rv3d->persp==2) {
1973                         if(scene->world) {
1974                                 if(scene->world->mode & WO_STARS) {
1975                                         RE_make_stars(NULL, scene, star_stuff_init_func, star_stuff_vertex_func,
1976                                                                   star_stuff_term_func);
1977                                 }
1978                         }
1979                         if(v3d->flag & V3D_DISPBGPIC) draw_bgpic(scene, ar, v3d);
1980                 }
1981         }
1982         else {
1983                 ED_region_pixelspace(ar);
1984                 drawgrid(&scene->unit, ar, v3d, &grid_unit);
1985                 /* XXX make function? replaces persp(1) */
1986                 glMatrixMode(GL_PROJECTION);
1987                 wmLoadMatrix(rv3d->winmat);
1988                 glMatrixMode(GL_MODELVIEW);
1989                 wmLoadMatrix(rv3d->viewmat);
1990                 
1991                 if(v3d->flag & V3D_DISPBGPIC) {
1992                         draw_bgpic(scene, ar, v3d);
1993                 }
1994         }
1995         
1996         if(rv3d->rflag & RV3D_CLIPPING)
1997                 view3d_set_clipping(rv3d);
1998         
1999         /* draw set first */
2000         if(scene->set) {
2001                 for(SETLOOPER(scene->set, base)) {
2002                         
2003                         if(v3d->lay & base->lay) {
2004                                 
2005                                 UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
2006                                 draw_object(scene, ar, v3d, base, DRAW_CONSTCOLOR|DRAW_SCENESET);
2007                                 
2008                                 if(base->object->transflag & OB_DUPLI) {
2009                                         draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
2010                                 }
2011                         }
2012                 }
2013                 
2014                 /* Transp and X-ray afterdraw stuff for sets is done later */
2015         }
2016         
2017         /* extra service in layerbuttons, showing used layers */
2018         v3d->lay_used = 0;
2019         
2020         /* then draw not selected and the duplis, but skip editmode object */
2021         for(base= scene->base.first; base; base= base->next) {
2022                 v3d->lay_used |= base->lay;
2023                 
2024                 if(v3d->lay & base->lay) {
2025                         
2026                         /* dupli drawing */
2027                         if(base->object->transflag & OB_DUPLI) {
2028                                 draw_dupli_objects(scene, ar, v3d, base);
2029                         }
2030                         if((base->flag & SELECT)==0) {
2031                                 if(base->object!=scene->obedit) 
2032                                         draw_object(scene, ar, v3d, base, 0);
2033                         }
2034                 }
2035         }
2036
2037 //      retopo= retopo_mesh_check() || retopo_curve_check();
2038         sculptparticle= (obact && obact->mode & (OB_MODE_SCULPT|OB_MODE_PARTICLE_EDIT)) && !scene->obedit;
2039         if(retopo)
2040                 view3d_update_depths(ar, v3d);
2041         
2042         /* draw selected and editmode */
2043         for(base= scene->base.first; base; base= base->next) {
2044                 if(v3d->lay & base->lay) {
2045                         if (base->object==scene->obedit || ( base->flag & SELECT) ) 
2046                                 draw_object(scene, ar, v3d, base, 0);
2047                 }
2048         }
2049         
2050         if(!retopo && sculptparticle && !(obact && (obact->dtx & OB_DRAWXRAY))) {
2051                 if(obact && obact->mode & OB_MODE_SCULPT)
2052                         draw_sculpt_depths(scene, ar, v3d);
2053                 view3d_update_depths(ar, v3d);
2054         }
2055         
2056         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST);
2057         
2058 //      REEB_draw();
2059         
2060 //      if(scene->radio) RAD_drawall(v3d->drawtype>=OB_SOLID);
2061         
2062         /* Transp and X-ray afterdraw stuff */
2063         view3d_draw_transp(scene, ar, v3d);
2064         view3d_draw_xray(scene, ar, v3d, 1);    // clears zbuffer if it is used!
2065         
2066         if(!retopo && sculptparticle && (obact && (OBACT->dtx & OB_DRAWXRAY))) {
2067                 if(obact && obact->mode & OB_MODE_SCULPT)
2068                         draw_sculpt_depths(scene, ar, v3d);
2069                 view3d_update_depths(ar, v3d);
2070         }
2071         
2072         if(rv3d->rflag & RV3D_CLIPPING)
2073                 view3d_clr_clipping();
2074         
2075         BIF_draw_manipulator(C);
2076         
2077         if(v3d->zbuf) {
2078                 v3d->zbuf= FALSE;
2079                 glDisable(GL_DEPTH_TEST);
2080         }
2081         
2082         /* draw grease-pencil stuff */
2083 //      if (v3d->flag2 & V3D_DISPGP)
2084 //              draw_gpencil_3dview(ar, 1);
2085         
2086         BDR_drawSketch(C);
2087         
2088         ED_region_pixelspace(ar);
2089         
2090         /* Draw Sculpt Mode brush XXX (removed) */
2091         
2092 //      retopo_paint_view_update(v3d);
2093 //      retopo_draw_paint_lines();
2094         
2095         /* Draw particle edit brush XXX (removed) */
2096         
2097         if(rv3d->persp>1) drawviewborder(scene, ar, v3d);
2098         if(rv3d->rflag & RV3D_FLYMODE) drawviewborder_flymode(ar);
2099         
2100         /* draw grease-pencil stuff */
2101 //      if (v3d->flag2 & V3D_DISPGP)
2102 //              draw_gpencil_3dview(ar, 0);
2103
2104         drawcursor(scene, ar, v3d);
2105         
2106         if(U.uiflag & USER_SHOW_ROTVIEWICON)
2107                 draw_view_axis(rv3d);
2108         else    
2109                 draw_view_icon(rv3d);
2110         
2111         /* XXX removed viewport fps */
2112         if(U.uiflag & USER_SHOW_VIEWPORTNAME) {
2113                 draw_viewport_name(ar, v3d);
2114         }
2115         if (grid_unit) { /* draw below the viewport name */
2116                 UI_ThemeColor(TH_TEXT_HI);
2117                 BLF_draw_default(10,  ar->winy-(USER_SHOW_VIEWPORTNAME?40:20), 0.0f, grid_unit);
2118         }
2119
2120         ob= OBACT;
2121         if(U.uiflag & USER_DRAWVIEWINFO) 
2122                 draw_selected_name(scene, ob, v3d);
2123         
2124         /* XXX here was the blockhandlers for floating panels */
2125
2126         if(ob && ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)) {
2127                 v3d->flag |= V3D_NEEDBACKBUFDRAW;
2128                 // XXX addafterqueue(ar->win, BACKBUFDRAW, 1);
2129         }
2130
2131         if((ob && ob->mode & OB_MODE_PARTICLE_EDIT) && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT)) {
2132                 v3d->flag |= V3D_NEEDBACKBUFDRAW;
2133                 // XXX addafterqueue(ar->win, BACKBUFDRAW, 1);
2134         }
2135
2136         // test for backbuf select
2137         if(scene->obedit && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT)) {
2138                 
2139                 v3d->flag |= V3D_NEEDBACKBUFDRAW;
2140                 // XXX if(afterqtest(ar->win, BACKBUFDRAW)==0) {
2141                 //      addafterqueue(ar->win, BACKBUFDRAW, 1);
2142                 //}
2143         }
2144 }
2145
2146