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