Cycles: border render now works in the viewport, when looking through the
[blender.git] / source / blender / editors / space_view3d / view3d_draw.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_view3d/view3d_draw.c
28  *  \ingroup spview3d
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34 #include <math.h>
35
36 #include "DNA_armature_types.h"
37 #include "DNA_camera_types.h"
38 #include "DNA_customdata_types.h"
39 #include "DNA_object_types.h"
40 #include "DNA_group_types.h"
41 #include "DNA_key_types.h"
42 #include "DNA_lamp_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_world_types.h"
45
46 #include "MEM_guardedalloc.h"
47
48 #include "BLI_blenlib.h"
49 #include "BLI_math.h"
50 #include "BLI_rand.h"
51 #include "BLI_utildefines.h"
52
53 #include "BKE_anim.h"
54 #include "BKE_camera.h"
55 #include "BKE_context.h"
56 #include "BKE_customdata.h"
57 #include "BKE_image.h"
58 #include "BKE_key.h"
59 #include "BKE_object.h"
60 #include "BKE_global.h"
61 #include "BKE_paint.h"
62 #include "BKE_scene.h"
63 #include "BKE_screen.h"
64 #include "BKE_unit.h"
65 #include "BKE_movieclip.h"
66
67 #include "RE_engine.h"
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_gpencil.h"
82 #include "ED_screen.h"
83 #include "ED_space_api.h"
84 #include "ED_screen_types.h"
85 #include "ED_transform.h"
86
87 #include "UI_interface.h"
88 #include "UI_interface_icons.h"
89 #include "UI_resources.h"
90
91 #include "GPU_draw.h"
92 #include "GPU_material.h"
93 #include "GPU_extensions.h"
94
95 #include "view3d_intern.h"  // own include
96
97
98
99 static void star_stuff_init_func(void)
100 {
101         cpack(0xFFFFFF);
102         glPointSize(1.0);
103         glBegin(GL_POINTS);
104 }
105 static void star_stuff_vertex_func(float *i)
106 {
107         glVertex3fv(i);
108 }
109 static void star_stuff_term_func(void)
110 {
111         glEnd();
112 }
113
114 void circf(float x, float y, float rad)
115 {
116         GLUquadricObj *qobj = gluNewQuadric(); 
117         
118         gluQuadricDrawStyle(qobj, GLU_FILL); 
119         
120         glPushMatrix(); 
121         
122         glTranslatef(x, y, 0.0);
123         
124         gluDisk(qobj, 0.0,  rad, 32, 1);
125         
126         glPopMatrix(); 
127         
128         gluDeleteQuadric(qobj);
129 }
130
131 void circ(float x, float y, float rad)
132 {
133         GLUquadricObj *qobj = gluNewQuadric(); 
134         
135         gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
136         
137         glPushMatrix(); 
138         
139         glTranslatef(x, y, 0.0);
140         
141         gluDisk(qobj, 0.0,  rad, 32, 1);
142         
143         glPopMatrix(); 
144         
145         gluDeleteQuadric(qobj);
146 }
147
148
149 /* ********* custom clipping *********** */
150
151 static void view3d_draw_clipping(RegionView3D *rv3d)
152 {
153         BoundBox *bb = rv3d->clipbb;
154
155         if (bb) {
156                 static unsigned int clipping_index[6][4] = {{0, 1, 2, 3},
157                                                             {0, 4, 5, 1},
158                                                             {4, 7, 6, 5},
159                                                             {7, 3, 2, 6},
160                                                             {1, 5, 6, 2},
161                                                             {7, 4, 0, 3}};
162
163                 /* fill in zero alpha for rendering & re-projection [#31530] */
164                 unsigned char col[4];
165                 UI_GetThemeColorShade3ubv(TH_BACK, -8, col);
166                 col[3] = 0;
167                 glColor4ubv(col);
168
169                 glEnableClientState(GL_VERTEX_ARRAY);
170                 glVertexPointer(3, GL_FLOAT, 0, bb->vec);
171                 glDrawElements(GL_QUADS, sizeof(clipping_index) / sizeof(unsigned int), GL_UNSIGNED_INT, clipping_index);
172                 glDisableClientState(GL_VERTEX_ARRAY);
173
174         }
175 }
176
177 void ED_view3d_clipping_set(RegionView3D *rv3d)
178 {
179         double plane[4];
180         const unsigned int tot = (rv3d->viewlock) ? 4 : 6;
181         unsigned int a;
182
183         for (a = 0; a < tot; a++) {
184                 copy_v4db_v4fl(plane, rv3d->clip[a]);
185                 glClipPlane(GL_CLIP_PLANE0 + a, plane);
186                 glEnable(GL_CLIP_PLANE0 + a);
187         }
188 }
189
190 /* use these to temp disable/enable clipping when 'rv3d->rflag & RV3D_CLIPPING' is set */
191 void ED_view3d_clipping_disable(void)
192 {
193         unsigned int a;
194
195         for (a = 0; a < 6; a++) {
196                 glDisable(GL_CLIP_PLANE0 + a);
197         }
198 }
199 void ED_view3d_clipping_enable(void)
200 {
201         unsigned int a;
202
203         for (a = 0; a < 6; a++) {
204                 glEnable(GL_CLIP_PLANE0 + a);
205         }
206 }
207
208 static int view3d_clipping_test(const float vec[3], float clip[][4])
209 {
210         float view[3];
211         copy_v3_v3(view, vec);
212
213         if (0.0f < clip[0][3] + dot_v3v3(view, clip[0]))
214                 if (0.0f < clip[1][3] + dot_v3v3(view, clip[1]))
215                         if (0.0f < clip[2][3] + dot_v3v3(view, clip[2]))
216                                 if (0.0f < clip[3][3] + dot_v3v3(view, clip[3]))
217                                         return 0;
218
219         return 1;
220 }
221
222 /* for 'local' ED_view3d_clipping_local must run first
223  * then all comparisons can be done in localspace */
224 int ED_view3d_clipping_test(RegionView3D *rv3d, const float vec[3], const int is_local)
225 {
226         return view3d_clipping_test(vec, is_local ? rv3d->clip_local : rv3d->clip);
227 }
228
229 /* ********* end custom clipping *********** */
230
231
232 static void drawgrid_draw(ARegion *ar, double wx, double wy, double x, double y, double dx)
233 {       
234         double verts[2][2];
235
236         x += (wx);
237         y += (wy);
238
239         /* set fixed 'Y' */
240         verts[0][1] = 0.0f;
241         verts[1][1] = (double)ar->winy;
242
243         /* iter over 'X' */
244         verts[0][0] = verts[1][0] = x - dx *floor(x / dx);
245         glEnableClientState(GL_VERTEX_ARRAY);
246         glVertexPointer(2, GL_DOUBLE, 0, verts);
247
248         while (verts[0][0] < ar->winx) {
249                 glDrawArrays(GL_LINES, 0, 2);
250                 verts[0][0] = verts[1][0] = verts[0][0] + dx;
251         }
252
253         /* set fixed 'X' */
254         verts[0][0] = 0.0f;
255         verts[1][0] = (double)ar->winx;
256
257         /* iter over 'Y' */
258         verts[0][1] = verts[1][1] = y - dx *floor(y / dx);
259         while (verts[0][1] < ar->winy) {
260                 glDrawArrays(GL_LINES, 0, 2);
261                 verts[0][1] = verts[1][1] = verts[0][1] + dx;
262         }
263
264         glDisableClientState(GL_VERTEX_ARRAY);
265 }
266
267 #define GRID_MIN_PX_D   6.0
268 #define GRID_MIN_PX_F 6.0f
269
270 static void drawgrid(UnitSettings *unit, ARegion *ar, View3D *v3d, const char **grid_unit)
271 {
272         /* extern short bgpicmode; */
273         RegionView3D *rv3d = ar->regiondata;
274         double wx, wy, x, y, fw, fx, fy, dx;
275         double vec4[4];
276         unsigned char col[3], col2[3];
277
278         fx = rv3d->persmat[3][0];
279         fy = rv3d->persmat[3][1];
280         fw = rv3d->persmat[3][3];
281
282         wx = (ar->winx / 2.0); /* because of rounding errors, grid at wrong location */
283         wy = (ar->winy / 2.0);
284
285         x = (wx) * fx / fw;
286         y = (wy) * fy / fw;
287
288         vec4[0] = vec4[1] = v3d->grid;
289
290         vec4[2] = 0.0;
291         vec4[3] = 1.0;
292         mul_m4_v4d(rv3d->persmat, vec4);
293         fx = vec4[0];
294         fy = vec4[1];
295         fw = vec4[3];
296
297         dx = fabs(x - (wx) * fx / fw);
298         if (dx == 0) dx = fabs(y - (wy) * fy / fw);
299         
300         glDepthMask(0);     // disable write in zbuffer
301
302         /* check zoom out */
303         UI_ThemeColor(TH_GRID);
304         
305         if (unit->system) {
306                 /* Use GRID_MIN_PX*2 for units because very very small grid
307                  * items are less useful when dealing with units */
308                 void *usys;
309                 int len, i;
310                 double dx_scalar;
311                 float blend_fac;
312
313                 bUnit_GetSystem(&usys, &len, unit->system, B_UNIT_LENGTH);
314
315                 if (usys) {
316                         i = len;
317                         while (i--) {
318                                 double scalar = bUnit_GetScaler(usys, i);
319
320                                 dx_scalar = dx * scalar / (double)unit->scale_length;
321                                 if (dx_scalar < (GRID_MIN_PX_D * 2.0))
322                                         continue;
323
324                                 /* Store the smallest drawn grid size units name so users know how big each grid cell is */
325                                 if (*grid_unit == NULL) {
326                                         *grid_unit = bUnit_GetNameDisplay(usys, i);
327                                         rv3d->gridview = (float)((scalar * (double)v3d->grid) / (double)unit->scale_length);
328                                 }
329                                 blend_fac = 1.0f - ((GRID_MIN_PX_F * 2.0f) / (float)dx_scalar);
330
331                                 /* tweak to have the fade a bit nicer */
332                                 blend_fac = (blend_fac * blend_fac) * 2.0f;
333                                 CLAMP(blend_fac, 0.3f, 1.0f);
334
335
336                                 UI_ThemeColorBlend(TH_BACK, TH_GRID, blend_fac);
337
338                                 drawgrid_draw(ar, wx, wy, x, y, dx_scalar);
339                         }
340                 }
341         }
342         else {
343                 short sublines = v3d->gridsubdiv;
344
345                 if (dx < GRID_MIN_PX_D) {
346                         rv3d->gridview *= sublines;
347                         dx *= sublines;
348
349                         if (dx < GRID_MIN_PX_D) {
350                                 rv3d->gridview *= sublines;
351                                 dx *= sublines;
352
353                                 if (dx < GRID_MIN_PX_D) {
354                                         rv3d->gridview *= sublines;
355                                         dx *= sublines;
356                                         if (dx < GRID_MIN_PX_D) ;
357                                         else {
358                                                 UI_ThemeColor(TH_GRID);
359                                                 drawgrid_draw(ar, wx, wy, x, y, dx);
360                                         }
361                                 }
362                                 else {  // start blending out
363                                         UI_ThemeColorBlend(TH_BACK, TH_GRID, dx / (GRID_MIN_PX_D * 6.0));
364                                         drawgrid_draw(ar, wx, wy, x, y, dx);
365
366                                         UI_ThemeColor(TH_GRID);
367                                         drawgrid_draw(ar, wx, wy, x, y, sublines * dx);
368                                 }
369                         }
370                         else {  // start blending out (GRID_MIN_PX < dx < (GRID_MIN_PX*10))
371                                 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx / (GRID_MIN_PX_D * 6.0));
372                                 drawgrid_draw(ar, wx, wy, x, y, dx);
373
374                                 UI_ThemeColor(TH_GRID);
375                                 drawgrid_draw(ar, wx, wy, x, y, sublines * dx);
376                         }
377                 }
378                 else {
379                         if (dx > (GRID_MIN_PX_D * 10.0)) {      // start blending in
380                                 rv3d->gridview /= sublines;
381                                 dx /= sublines;
382                                 if (dx > (GRID_MIN_PX_D * 10.0)) {      // start blending in
383                                         rv3d->gridview /= sublines;
384                                         dx /= sublines;
385                                         if (dx > (GRID_MIN_PX_D * 10.0)) {
386                                                 UI_ThemeColor(TH_GRID);
387                                                 drawgrid_draw(ar, wx, wy, x, y, dx);
388                                         }
389                                         else {
390                                                 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx / (GRID_MIN_PX_D * 6.0));
391                                                 drawgrid_draw(ar, wx, wy, x, y, dx);
392                                                 UI_ThemeColor(TH_GRID);
393                                                 drawgrid_draw(ar, wx, wy, x, y, dx * sublines);
394                                         }
395                                 }
396                                 else {
397                                         UI_ThemeColorBlend(TH_BACK, TH_GRID, dx / (GRID_MIN_PX_D * 6.0));
398                                         drawgrid_draw(ar, wx, wy, x, y, dx);
399                                         UI_ThemeColor(TH_GRID);
400                                         drawgrid_draw(ar, wx, wy, x, y, dx * sublines);
401                                 }
402                         }
403                         else {
404                                 UI_ThemeColorBlend(TH_BACK, TH_GRID, dx / (GRID_MIN_PX_D * 6.0));
405                                 drawgrid_draw(ar, wx, wy, x, y, dx);
406                                 UI_ThemeColor(TH_GRID);
407                                 drawgrid_draw(ar, wx, wy, x, y, dx * sublines);
408                         }
409                 }
410         }
411
412
413         x += (wx);
414         y += (wy);
415         UI_GetThemeColor3ubv(TH_GRID, col);
416
417         setlinestyle(0);
418         
419         /* center cross */
420         /* horizontal line */
421         if (ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT))
422                 UI_make_axis_color(col, col2, 'Y');
423         else UI_make_axis_color(col, col2, 'X');
424         glColor3ubv(col2);
425         
426         fdrawline(0.0,  y,  (float)ar->winx,  y); 
427         
428         /* vertical line */
429         if (ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM))
430                 UI_make_axis_color(col, col2, 'Y');
431         else UI_make_axis_color(col, col2, 'Z');
432         glColor3ubv(col2);
433
434         fdrawline(x, 0.0, x, (float)ar->winy); 
435
436         glDepthMask(1);     // enable write in zbuffer
437 }
438 #undef GRID_MIN_PX
439
440 float ED_view3d_grid_scale(Scene *scene, View3D *v3d, const char **grid_unit)
441 {
442         float grid_scale = v3d->grid;
443
444         /* apply units */
445         if (scene->unit.system) {
446                 void *usys;
447                 int len;
448
449                 bUnit_GetSystem(&usys, &len, scene->unit.system, B_UNIT_LENGTH);
450
451                 if (usys) {
452                         int i = bUnit_GetBaseUnit(usys);
453                         if (grid_unit)
454                                 *grid_unit = bUnit_GetNameDisplay(usys, i);
455                         grid_scale = (grid_scale * (float)bUnit_GetScaler(usys, i)) / scene->unit.scale_length;
456                 }
457         }
458
459         return grid_scale;
460 }
461
462 static void drawfloor(Scene *scene, View3D *v3d, const char **grid_unit)
463 {
464         float grid, grid_scale;
465         unsigned char col_grid[3];
466         const int gridlines = v3d->gridlines / 2;
467
468         if (v3d->gridlines < 3) return;
469         
470         /* use 'grid_scale' instead of 'v3d->grid' from now on */
471         grid_scale = ED_view3d_grid_scale(scene, v3d, grid_unit);
472         grid = gridlines * grid_scale;
473
474         if (v3d->zbuf && scene->obedit) glDepthMask(0);  // for zbuffer-select
475
476         UI_GetThemeColor3ubv(TH_GRID, col_grid);
477
478         /* draw the Y axis and/or grid lines */
479         if (v3d->gridflag & V3D_SHOW_FLOOR) {
480                 float vert[4][3] = {{0.0f}};
481                 unsigned char col_bg[3];
482                 unsigned char col_grid_emphasise[3], col_grid_light[3];
483                 int a;
484                 int prev_emphasise = -1;
485
486                 UI_GetThemeColor3ubv(TH_BACK, col_bg);
487
488                 /* emphasise division lines lighter instead of darker, if background is darker than grid */
489                 UI_GetColorPtrShade3ubv(col_grid, col_grid_light, 10);
490                 UI_GetColorPtrShade3ubv(col_grid, col_grid_emphasise,
491                                         (((col_grid[0] + col_grid[1] + col_grid[2]) + 30) >
492                                          (col_bg[0] + col_bg[1] + col_bg[2])) ? 20 : -10);
493
494                 /* set fixed axis */
495                 vert[0][0] = vert[2][1] = grid;
496                 vert[1][0] = vert[3][1] = -grid;
497
498                 glEnableClientState(GL_VERTEX_ARRAY);
499                 glVertexPointer(3, GL_FLOAT, 0, vert);
500
501                 for (a = -gridlines; a <= gridlines; a++) {
502                         const float line = a * grid_scale;
503                         const int is_emphasise = (a % 10) == 0;
504
505                         if (is_emphasise != prev_emphasise) {
506                                 glColor3ubv(is_emphasise ? col_grid_emphasise : col_grid_light);
507                                 prev_emphasise = is_emphasise;
508                         }
509
510                         /* set variable axis */
511                         vert[0][1] = vert[1][1] = vert[2][0] = vert[3][0] = line;
512
513                         glDrawArrays(GL_LINES, 0, 4);
514                 }
515
516                 glDisableClientState(GL_VERTEX_ARRAY);
517
518                 GPU_print_error("sdsd");
519         }
520         
521         /* draw the Z axis line */      
522         /* check for the 'show Z axis' preference */
523         if (v3d->gridflag & (V3D_SHOW_X | V3D_SHOW_Y | V3D_SHOW_Z)) {
524                 int axis;
525                 for (axis = 0; axis < 3; axis++) {
526                         if (v3d->gridflag & (V3D_SHOW_X << axis)) {
527                                 float vert[3];
528                                 unsigned char tcol[3];
529
530                                 UI_make_axis_color(col_grid, tcol, 'X' + axis);
531                                 glColor3ubv(tcol);
532
533                                 glBegin(GL_LINE_STRIP);
534                                 zero_v3(vert);
535                                 vert[axis] = grid;
536                                 glVertex3fv(vert);
537                                 vert[axis] = -grid;
538                                 glVertex3fv(vert);
539                                 glEnd();
540                         }
541                 }
542         }
543
544
545
546
547         if (v3d->zbuf && scene->obedit) glDepthMask(1);
548         
549 }
550
551 static void drawcursor(Scene *scene, ARegion *ar, View3D *v3d)
552 {
553         int mx, my, co[2];
554         int flag;
555         
556         /* we don't want the clipping for cursor */
557         flag = v3d->flag;
558         v3d->flag = 0;
559         project_int(ar, give_cursor(scene, v3d), co);
560         v3d->flag = flag;
561         
562         mx = co[0];
563         my = co[1];
564         
565         if (mx != IS_CLIPPED) {
566                 setlinestyle(0); 
567                 cpack(0xFF);
568                 circ((float)mx, (float)my, 10.0);
569                 setlinestyle(4); 
570                 cpack(0xFFFFFF);
571                 circ((float)mx, (float)my, 10.0);
572                 setlinestyle(0);
573                 cpack(0x0);
574                 
575                 sdrawline(mx - 20, my, mx - 5, my);
576                 sdrawline(mx + 5, my, mx + 20, my);
577                 sdrawline(mx, my - 20, mx, my - 5);
578                 sdrawline(mx, my + 5, mx, my + 20);
579         }
580 }
581
582 /* Draw a live substitute of the view icon, which is always shown
583  * colors copied from transform_manipulator.c, we should keep these matching. */
584 static void draw_view_axis(RegionView3D *rv3d)
585 {
586         const float k = U.rvisize;   /* axis size */
587         const float toll = 0.5;      /* used to see when view is quasi-orthogonal */
588         const float start = k + 1.0f; /* axis center in screen coordinates, x=y */
589         float ydisp = 0.0;          /* vertical displacement to allow obj info text */
590         int bright = 25 * (float)U.rvibright + 5; /* axis alpha (rvibright has range 0-10) */
591
592         float vec[3];
593         float dx, dy;
594         
595         /* thickness of lines is proportional to k */
596         glLineWidth(2);
597
598         glEnable(GL_BLEND);
599         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
600
601         /* X */
602         vec[0] = 1;
603         vec[1] = vec[2] = 0;
604         mul_qt_v3(rv3d->viewquat, vec);
605         dx = vec[0] * k;
606         dy = vec[1] * k;
607
608         glColor4ub(220, 0, 0, bright);
609         glBegin(GL_LINES);
610         glVertex2f(start, start + ydisp);
611         glVertex2f(start + dx, start + dy + ydisp);
612         glEnd();
613
614         if (fabsf(dx) > toll || fabsf(dy) > toll) {
615                 BLF_draw_default_ascii(start + dx + 2, start + dy + ydisp + 2, 0.0f, "x", 1);
616         }
617         
618         /* BLF_draw_default disables blending */
619         glEnable(GL_BLEND);
620
621         /* Y */
622         vec[1] = 1;
623         vec[0] = vec[2] = 0;
624         mul_qt_v3(rv3d->viewquat, vec);
625         dx = vec[0] * k;
626         dy = vec[1] * k;
627
628         glColor4ub(0, 220, 0, bright);
629         glBegin(GL_LINES);
630         glVertex2f(start, start + ydisp);
631         glVertex2f(start + dx, start + dy + ydisp);
632         glEnd();
633
634         if (fabsf(dx) > toll || fabsf(dy) > toll) {
635                 BLF_draw_default_ascii(start + dx + 2, start + dy + ydisp + 2, 0.0f, "y", 1);
636         }
637
638         glEnable(GL_BLEND);
639         
640         /* Z */
641         vec[2] = 1;
642         vec[1] = vec[0] = 0;
643         mul_qt_v3(rv3d->viewquat, vec);
644         dx = vec[0] * k;
645         dy = vec[1] * k;
646
647         glColor4ub(30, 30, 220, bright);
648         glBegin(GL_LINES);
649         glVertex2f(start, start + ydisp);
650         glVertex2f(start + dx, start + dy + ydisp);
651         glEnd();
652
653         if (fabsf(dx) > toll || fabsf(dy) > toll) {
654                 BLF_draw_default_ascii(start + dx + 2, start + dy + ydisp + 2, 0.0f, "z", 1);
655         }
656
657         /* restore line-width */
658         
659         glLineWidth(1.0);
660         glDisable(GL_BLEND);
661 }
662
663 /* draw center and axis of rotation for ongoing 3D mouse navigation */
664 static void draw_rotation_guide(RegionView3D *rv3d)
665 {
666         float o[3]; // center of rotation
667         float end[3]; // endpoints for drawing
668
669         float color[4] = {0.0f, 0.4235f, 1.0f, 1.0f}; // bright blue so it matches device LEDs
670
671         negate_v3_v3(o, rv3d->ofs);
672
673         glEnable(GL_BLEND);
674         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
675         glShadeModel(GL_SMOOTH);
676         glPointSize(5);
677         glEnable(GL_POINT_SMOOTH);
678         glDepthMask(0); // don't overwrite zbuf
679
680         if (rv3d->rot_angle != 0.f) {
681                 // -- draw rotation axis --
682                 float scaled_axis[3];
683                 const float scale = rv3d->dist;
684                 mul_v3_v3fl(scaled_axis, rv3d->rot_axis, scale);
685
686
687                 glBegin(GL_LINE_STRIP);
688                 color[3] = 0.f; // more transparent toward the ends
689                 glColor4fv(color);
690                 add_v3_v3v3(end, o, scaled_axis);
691                 glVertex3fv(end);
692
693                 // color[3] = 0.2f + fabsf(rv3d->rot_angle); // modulate opacity with angle
694                 // ^^ neat idea, but angle is frame-rate dependent, so it's usually close to 0.2
695
696                 color[3] = 0.5f; // more opaque toward the center
697                 glColor4fv(color);
698                 glVertex3fv(o);
699
700                 color[3] = 0.f;
701                 glColor4fv(color);
702                 sub_v3_v3v3(end, o, scaled_axis);
703                 glVertex3fv(end);
704                 glEnd();
705                 
706                 // -- draw ring around rotation center --
707                 {
708 #define     ROT_AXIS_DETAIL 13
709
710                         const float s = 0.05f * scale;
711                         const float step = 2.f * (float)(M_PI / ROT_AXIS_DETAIL);
712                         float angle;
713                         int i;
714
715                         float q[4]; // rotate ring so it's perpendicular to axis
716                         const int upright = fabsf(rv3d->rot_axis[2]) >= 0.95f;
717                         if (!upright) {
718                                 const float up[3] = {0.f, 0.f, 1.f};
719                                 float vis_angle, vis_axis[3];
720
721                                 cross_v3_v3v3(vis_axis, up, rv3d->rot_axis);
722                                 vis_angle = acosf(dot_v3v3(up, rv3d->rot_axis));
723                                 axis_angle_to_quat(q, vis_axis, vis_angle);
724                         }
725
726                         color[3] = 0.25f; // somewhat faint
727                         glColor4fv(color);
728                         glBegin(GL_LINE_LOOP);
729                         for (i = 0, angle = 0.f; i < ROT_AXIS_DETAIL; ++i, angle += step) {
730                                 float p[3] = {s *cosf(angle), s * sinf(angle), 0.0f};
731
732                                 if (!upright) {
733                                         mul_qt_v3(q, p);
734                                 }
735
736                                 add_v3_v3(p, o);
737                                 glVertex3fv(p);
738                         }
739                         glEnd();
740
741 #undef      ROT_AXIS_DETAIL
742                 }
743
744                 color[3] = 1.f; // solid dot
745         }
746         else
747                 color[3] = 0.5f;  // see-through dot
748
749         // -- draw rotation center --
750         glColor4fv(color);
751         glBegin(GL_POINTS);
752         glVertex3fv(o);
753         glEnd();
754
755         // find screen coordinates for rotation center, then draw pretty icon
756         // mul_m4_v3(rv3d->persinv, rot_center);
757         // UI_icon_draw(rot_center[0], rot_center[1], ICON_NDOF_TURN);
758         // ^^ just playing around, does not work
759
760         glDisable(GL_BLEND);
761         glDisable(GL_POINT_SMOOTH);
762         glDepthMask(1);
763 }
764
765 static void draw_view_icon(RegionView3D *rv3d)
766 {
767         BIFIconID icon;
768         
769         if (ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM))
770                 icon = ICON_AXIS_TOP;
771         else if (ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK))
772                 icon = ICON_AXIS_FRONT;
773         else if (ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT))
774                 icon = ICON_AXIS_SIDE;
775         else return;
776         
777         glEnable(GL_BLEND);
778         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
779         
780         UI_icon_draw(5.0, 5.0, icon);
781         
782         glDisable(GL_BLEND);
783 }
784
785 static const char *view3d_get_name(View3D *v3d, RegionView3D *rv3d)
786 {
787         const char *name = NULL;
788         
789         switch (rv3d->view) {
790                 case RV3D_VIEW_FRONT:
791                         if (rv3d->persp == RV3D_ORTHO) name = "Front Ortho";
792                         else name = "Front Persp";
793                         break;
794                 case RV3D_VIEW_BACK:
795                         if (rv3d->persp == RV3D_ORTHO) name = "Back Ortho";
796                         else name = "Back Persp";
797                         break;
798                 case RV3D_VIEW_TOP:
799                         if (rv3d->persp == RV3D_ORTHO) name = "Top Ortho";
800                         else name = "Top Persp";
801                         break;
802                 case RV3D_VIEW_BOTTOM:
803                         if (rv3d->persp == RV3D_ORTHO) name = "Bottom Ortho";
804                         else name = "Bottom Persp";
805                         break;
806                 case RV3D_VIEW_RIGHT:
807                         if (rv3d->persp == RV3D_ORTHO) name = "Right Ortho";
808                         else name = "Right Persp";
809                         break;
810                 case RV3D_VIEW_LEFT:
811                         if (rv3d->persp == RV3D_ORTHO) name = "Left Ortho";
812                         else name = "Left Persp";
813                         break;
814                         
815                 default:
816                         if (rv3d->persp == RV3D_CAMOB) {
817                                 if ((v3d->camera) && (v3d->camera->type == OB_CAMERA)) {
818                                         Camera *cam;
819                                         cam = v3d->camera->data;
820                                         name = (cam->type != CAM_ORTHO) ? "Camera Persp" : "Camera Ortho";
821                                 }
822                                 else {
823                                         name = "Object as Camera";
824                                 }
825                         }
826                         else {
827                                 name = (rv3d->persp == RV3D_ORTHO) ? "User Ortho" : "User Persp";
828                         }
829                         break;
830         }
831         
832         return name;
833 }
834
835 static void draw_viewport_name(ARegion *ar, View3D *v3d)
836 {
837         RegionView3D *rv3d = ar->regiondata;
838         const char *name = view3d_get_name(v3d, rv3d);
839         char tmpstr[24];
840         
841         if (v3d->localvd) {
842                 BLI_snprintf(tmpstr, sizeof(tmpstr), "%s (Local)", name);
843                 name = tmpstr;
844         }
845
846         if (name) {
847                 UI_ThemeColor(TH_TEXT_HI);
848                 BLF_draw_default_ascii(22,  ar->winy - 17, 0.0f, name, sizeof(tmpstr));
849         }
850 }
851
852 /* draw info beside axes in bottom left-corner: 
853  * framenum, object name, bone name (if available), marker name (if available)
854  */
855 static void draw_selected_name(Scene *scene, Object *ob)
856 {
857         char info[256], *markern;
858         short offset = 30;
859         
860         /* get name of marker on current frame (if available) */
861         markern = BKE_scene_find_marker_name(scene, CFRA);
862         
863         /* check if there is an object */
864         if (ob) {
865                 /* name(s) to display depends on type of object */
866                 if (ob->type == OB_ARMATURE) {
867                         bArmature *arm = ob->data;
868                         char *name = NULL;
869                         
870                         /* show name of active bone too (if possible) */
871                         if (arm->edbo) {
872
873                                 if (arm->act_edbone)
874                                         name = ((EditBone *)arm->act_edbone)->name;
875
876                         }
877                         else if (ob->mode & OB_MODE_POSE) {
878                                 if (arm->act_bone) {
879
880                                         if (arm->act_bone->layer & arm->layer)
881                                                 name = arm->act_bone->name;
882
883                                 }
884                         }
885                         if (name && markern)
886                                 BLI_snprintf(info, sizeof(info), "(%d) %s %s <%s>", CFRA, ob->id.name + 2, name, markern);
887                         else if (name)
888                                 BLI_snprintf(info, sizeof(info), "(%d) %s %s", CFRA, ob->id.name + 2, name);
889                         else
890                                 BLI_snprintf(info, sizeof(info), "(%d) %s", CFRA, ob->id.name + 2);
891                 }
892                 else if (ELEM3(ob->type, OB_MESH, OB_LATTICE, OB_CURVE)) {
893                         Key *key = NULL;
894                         KeyBlock *kb = NULL;
895                         char shapes[MAX_NAME + 10];
896                         
897                         /* try to display active shapekey too */
898                         shapes[0] = '\0';
899                         key = ob_get_key(ob);
900                         if (key) {
901                                 kb = BLI_findlink(&key->block, ob->shapenr - 1);
902                                 if (kb) {
903                                         BLI_snprintf(shapes, sizeof(shapes), ": %s ", kb->name);
904                                         if (ob->shapeflag == OB_SHAPE_LOCK) {
905                                                 strcat(shapes, " (Pinned)");
906                                         }
907                                 }
908                         }
909                         
910                         if (markern)
911                                 BLI_snprintf(info, sizeof(info), "(%d) %s %s <%s>", CFRA, ob->id.name + 2, shapes, markern);
912                         else
913                                 BLI_snprintf(info, sizeof(info), "(%d) %s %s", CFRA, ob->id.name + 2, shapes);
914                 }
915                 else {
916                         /* standard object */
917                         if (markern)
918                                 BLI_snprintf(info, sizeof(info), "(%d) %s <%s>", CFRA, ob->id.name + 2, markern);
919                         else
920                                 BLI_snprintf(info, sizeof(info), "(%d) %s", CFRA, ob->id.name + 2);
921                 }
922                 
923                 /* color depends on whether there is a keyframe */
924                 if (id_frame_has_keyframe((ID *)ob, /*BKE_scene_frame_get(scene)*/ (float)(CFRA), ANIMFILTER_KEYS_LOCAL))
925                         UI_ThemeColor(TH_VERTEX_SELECT);
926                 else
927                         UI_ThemeColor(TH_TEXT_HI);
928         }
929         else {
930                 /* no object */
931                 if (markern)
932                         BLI_snprintf(info, sizeof(info), "(%d) <%s>", CFRA, markern);
933                 else
934                         BLI_snprintf(info, sizeof(info), "(%d)", CFRA);
935                 
936                 /* color is always white */
937                 UI_ThemeColor(TH_TEXT_HI);
938         }
939         
940         if (U.uiflag & USER_SHOW_ROTVIEWICON)
941                 offset = 14 + (U.rvisize * 2);
942
943         BLF_draw_default(offset,  10, 0.0f, info, sizeof(info));
944 }
945
946 static void view3d_camera_border(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D *rv3d,
947                                  rctf *viewborder_r, short no_shift, short no_zoom)
948 {
949         CameraParams params;
950         rctf rect_view, rect_camera;
951
952         /* get viewport viewplane */
953         BKE_camera_params_init(&params);
954         BKE_camera_params_from_view3d(&params, v3d, rv3d);
955         if (no_zoom)
956                 params.zoom = 1.0f;
957         BKE_camera_params_compute_viewplane(&params, ar->winx, ar->winy, 1.0f, 1.0f);
958         rect_view = params.viewplane;
959
960         /* get camera viewplane */
961         BKE_camera_params_init(&params);
962         BKE_camera_params_from_object(&params, v3d->camera);
963         if (no_shift) {
964                 params.shiftx = 0.0f;
965                 params.shifty = 0.0f;
966         }
967         BKE_camera_params_compute_viewplane(&params, scene->r.xsch, scene->r.ysch, scene->r.xasp, scene->r.yasp);
968         rect_camera = params.viewplane;
969
970         /* get camera border within viewport */
971         viewborder_r->xmin = ((rect_camera.xmin - rect_view.xmin) / (rect_view.xmax - rect_view.xmin)) * ar->winx;
972         viewborder_r->xmax = ((rect_camera.xmax - rect_view.xmin) / (rect_view.xmax - rect_view.xmin)) * ar->winx;
973         viewborder_r->ymin = ((rect_camera.ymin - rect_view.ymin) / (rect_view.ymax - rect_view.ymin)) * ar->winy;
974         viewborder_r->ymax = ((rect_camera.ymax - rect_view.ymin) / (rect_view.ymax - rect_view.ymin)) * ar->winy;
975 }
976
977 void ED_view3d_calc_camera_border_size(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D *rv3d, float size_r[2])
978 {
979         rctf viewborder;
980
981         view3d_camera_border(scene, ar, v3d, rv3d, &viewborder, TRUE, TRUE);
982         size_r[0] = viewborder.xmax - viewborder.xmin;
983         size_r[1] = viewborder.ymax - viewborder.ymin;
984 }
985
986 void ED_view3d_calc_camera_border(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D *rv3d,
987                                   rctf *viewborder_r, short no_shift)
988 {
989         view3d_camera_border(scene, ar, v3d, rv3d, viewborder_r, no_shift, FALSE);
990 }
991
992 static void drawviewborder_grid3(float x1, float x2, float y1, float y2, float fac)
993 {
994         float x3, y3, x4, y4;
995
996         x3 = x1 + fac * (x2 - x1);
997         y3 = y1 + fac * (y2 - y1);
998         x4 = x1 + (1.0f - fac) * (x2 - x1);
999         y4 = y1 + (1.0f - fac) * (y2 - y1);
1000
1001         glBegin(GL_LINES);
1002         glVertex2f(x1, y3);
1003         glVertex2f(x2, y3);
1004
1005         glVertex2f(x1, y4);
1006         glVertex2f(x2, y4);
1007
1008         glVertex2f(x3, y1);
1009         glVertex2f(x3, y2);
1010
1011         glVertex2f(x4, y1);
1012         glVertex2f(x4, y2);
1013         glEnd();
1014 }
1015
1016 /* harmonious triangle */
1017 static void drawviewborder_triangle(float x1, float x2, float y1, float y2, const char golden, const char dir)
1018 {
1019         float ofs;
1020         float w = x2 - x1;
1021         float h = y2 - y1;
1022
1023         glBegin(GL_LINES);
1024         if (w > h) {
1025                 if (golden) {
1026                         ofs = w * (1.0f - (1.0f / 1.61803399f));
1027                 }
1028                 else {
1029                         ofs = h * (h / w);
1030                 }
1031                 if (dir == 'B') SWAP(float, y1, y2);
1032
1033                 glVertex2f(x1, y1);
1034                 glVertex2f(x2, y2);
1035
1036                 glVertex2f(x2, y1);
1037                 glVertex2f(x1 + (w - ofs), y2);
1038
1039                 glVertex2f(x1, y2);
1040                 glVertex2f(x1 + ofs, y1);
1041         }
1042         else {
1043                 if (golden) {
1044                         ofs = h * (1.0f - (1.0f / 1.61803399f));
1045                 }
1046                 else {
1047                         ofs = w * (w / h);
1048                 }
1049                 if (dir == 'B') SWAP(float, x1, x2);
1050
1051                 glVertex2f(x1, y1);
1052                 glVertex2f(x2, y2);
1053
1054                 glVertex2f(x2, y1);
1055                 glVertex2f(x1, y1 + ofs);
1056
1057                 glVertex2f(x1, y2);
1058                 glVertex2f(x2, y1 + (h - ofs));
1059         }
1060         glEnd();
1061 }
1062
1063 static void drawviewborder(Scene *scene, ARegion *ar, View3D *v3d)
1064 {
1065         float fac, a;
1066         float x1, x2, y1, y2;
1067         float x1i, x2i, y1i, y2i;
1068         float x3, y3, x4, y4;
1069         rctf viewborder;
1070         Camera *ca = NULL;
1071         RegionView3D *rv3d = (RegionView3D *)ar->regiondata;
1072         
1073         if (v3d->camera == NULL)
1074                 return;
1075         if (v3d->camera->type == OB_CAMERA)
1076                 ca = v3d->camera->data;
1077         
1078         ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &viewborder, FALSE);
1079         /* the offsets */
1080         x1 = viewborder.xmin;
1081         y1 = viewborder.ymin;
1082         x2 = viewborder.xmax;
1083         y2 = viewborder.ymax;
1084         
1085         /* apply offsets so the real 3D camera shows through */
1086
1087         /* note: quite un-scientific but without this bit extra
1088          * 0.0001 on the lower left the 2D border sometimes
1089          * obscures the 3D camera border */
1090         /* note: with VIEW3D_CAMERA_BORDER_HACK defined this error isn't noticeable
1091          * but keep it here in case we need to remove the workaround */
1092         x1i = (int)(x1 - 1.0001f);
1093         y1i = (int)(y1 - 1.0001f);
1094         x2i = (int)(x2 + (1.0f - 0.0001f));
1095         y2i = (int)(y2 + (1.0f - 0.0001f));
1096         
1097         /* passepartout, specified in camera edit buttons */
1098         if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001f) {
1099                 if (ca->passepartalpha == 1.0f) {
1100                         glColor3f(0, 0, 0);
1101                 }
1102                 else {
1103                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1104                         glEnable(GL_BLEND);
1105                         glColor4f(0, 0, 0, ca->passepartalpha);
1106                 }
1107                 if (x1i > 0.0f)
1108                         glRectf(0.0, (float)ar->winy, x1i, 0.0);
1109                 if (x2i < (float)ar->winx)
1110                         glRectf(x2i, (float)ar->winy, (float)ar->winx, 0.0);
1111                 if (y2i < (float)ar->winy)
1112                         glRectf(x1i, (float)ar->winy, x2i, y2i);
1113                 if (y2i > 0.0f)
1114                         glRectf(x1i, y1i, x2i, 0.0);
1115                 
1116                 glDisable(GL_BLEND);
1117         }
1118
1119         /* edge */
1120         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);      
1121
1122         setlinestyle(0);
1123
1124         UI_ThemeColor(TH_BACK);
1125                 
1126         glRectf(x1i, y1i, x2i, y2i);
1127
1128 #ifdef VIEW3D_CAMERA_BORDER_HACK
1129         if (view3d_camera_border_hack_test == TRUE) {
1130                 glColor3ubv(view3d_camera_border_hack_col);
1131                 glRectf(x1i + 1, y1i + 1, x2i - 1, y2i - 1);
1132                 view3d_camera_border_hack_test = FALSE;
1133         }
1134 #endif
1135
1136         setlinestyle(3);
1137
1138         /* outer line not to confuse with object selecton */
1139         if (v3d->flag2 & V3D_LOCK_CAMERA) {
1140                 UI_ThemeColor(TH_REDALERT);
1141                 glRectf(x1i - 1, y1i - 1, x2i + 1, y2i + 1);
1142         }
1143
1144         UI_ThemeColor(TH_WIRE);
1145         glRectf(x1i, y1i, x2i, y2i);
1146
1147         /* border */
1148         if (scene->r.mode & R_BORDER) {
1149                 cpack(0);
1150                 x3 = x1 + scene->r.border.xmin * (x2 - x1);
1151                 y3 = y1 + scene->r.border.ymin * (y2 - y1);
1152                 x4 = x1 + scene->r.border.xmax * (x2 - x1);
1153                 y4 = y1 + scene->r.border.ymax * (y2 - y1);
1154                 
1155                 cpack(0x4040FF);
1156                 glRectf(x3,  y3,  x4,  y4); 
1157         }
1158
1159         /* safety border */
1160         if (ca) {
1161                 if (ca->dtx & CAM_DTX_CENTER) {
1162                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1163
1164                         x3 = x1 + 0.5f * (x2 - x1);
1165                         y3 = y1 + 0.5f * (y2 - y1);
1166
1167                         glBegin(GL_LINES);
1168                         glVertex2f(x1, y3);
1169                         glVertex2f(x2, y3);
1170
1171                         glVertex2f(x3, y1);
1172                         glVertex2f(x3, y2);
1173                         glEnd();
1174                 }
1175
1176                 if (ca->dtx & CAM_DTX_CENTER_DIAG) {
1177                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1178
1179                         glBegin(GL_LINES);
1180                         glVertex2f(x1, y1);
1181                         glVertex2f(x2, y2);
1182
1183                         glVertex2f(x1, y2);
1184                         glVertex2f(x2, y1);
1185                         glEnd();
1186                 }
1187
1188                 if (ca->dtx & CAM_DTX_THIRDS) {
1189                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1190                         drawviewborder_grid3(x1, x2, y1, y2, 1.0f / 3.0f);
1191                 }
1192
1193                 if (ca->dtx & CAM_DTX_GOLDEN) {
1194                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1195                         drawviewborder_grid3(x1, x2, y1, y2, 1.0f - (1.0f / 1.61803399f));
1196                 }
1197
1198                 if (ca->dtx & CAM_DTX_GOLDEN_TRI_A) {
1199                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1200                         drawviewborder_triangle(x1, x2, y1, y2, 0, 'A');
1201                 }
1202
1203                 if (ca->dtx & CAM_DTX_GOLDEN_TRI_B) {
1204                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1205                         drawviewborder_triangle(x1, x2, y1, y2, 0, 'B');
1206                 }
1207
1208                 if (ca->dtx & CAM_DTX_HARMONY_TRI_A) {
1209                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1210                         drawviewborder_triangle(x1, x2, y1, y2, 1, 'A');
1211                 }
1212
1213                 if (ca->dtx & CAM_DTX_HARMONY_TRI_B) {
1214                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1215                         drawviewborder_triangle(x1, x2, y1, y2, 1, 'B');
1216                 }
1217
1218                 if (ca->flag & CAM_SHOWTITLESAFE) {
1219                         fac = 0.1;
1220
1221                         a = fac * (x2 - x1);
1222                         x1 += a;
1223                         x2 -= a;
1224
1225                         a = fac * (y2 - y1);
1226                         y1 += a;
1227                         y2 -= a;
1228
1229                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1230
1231                         uiSetRoundBox(UI_CNR_ALL);
1232                         uiDrawBox(GL_LINE_LOOP, x1, y1, x2, y2, 12.0);
1233                 }
1234                 if (ca && (ca->flag & CAM_SHOWSENSOR)) {
1235                         /* determine sensor fit, and get sensor x/y, for auto fit we
1236                          * assume and square sensor and only use sensor_x */
1237                         float sizex = scene->r.xsch * scene->r.xasp;
1238                         float sizey = scene->r.ysch * scene->r.yasp;
1239                         int sensor_fit = BKE_camera_sensor_fit(ca->sensor_fit, sizex, sizey);
1240                         float sensor_x = ca->sensor_x;
1241                         float sensor_y = (ca->sensor_fit == CAMERA_SENSOR_FIT_AUTO) ? ca->sensor_x : ca->sensor_y;
1242
1243                         /* determine sensor plane */
1244                         rctf rect;
1245
1246                         if (sensor_fit == CAMERA_SENSOR_FIT_HOR) {
1247                                 float sensor_scale = (x2i - x1i) / sensor_x;
1248                                 float sensor_height = sensor_scale * sensor_y;
1249
1250                                 rect.xmin = x1i;
1251                                 rect.xmax = x2i;
1252                                 rect.ymin = (y1i + y2i) * 0.5f - sensor_height * 0.5f;
1253                                 rect.ymax = rect.ymin + sensor_height;
1254                         }
1255                         else {
1256                                 float sensor_scale = (y2i - y1i) / sensor_y;
1257                                 float sensor_width = sensor_scale * sensor_x;
1258
1259                                 rect.xmin = (x1i + x2i) * 0.5f - sensor_width * 0.5f;
1260                                 rect.xmax = rect.xmin + sensor_width;
1261                                 rect.ymin = y1i;
1262                                 rect.ymax = y2i;
1263                         }
1264
1265                         /* draw */
1266                         UI_ThemeColorShade(TH_WIRE, 100);
1267                         uiDrawBox(GL_LINE_LOOP, rect.xmin, rect.ymin, rect.xmax, rect.ymax, 2.0f);
1268                 }
1269         }
1270
1271         setlinestyle(0);
1272         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1273
1274         /* camera name - draw in highlighted text color */
1275         if (ca && (ca->flag & CAM_SHOWNAME)) {
1276                 UI_ThemeColor(TH_TEXT_HI);
1277                 BLF_draw_default(x1i, y1i - 15, 0.0f, v3d->camera->id.name + 2, sizeof(v3d->camera->id.name) - 2);
1278                 UI_ThemeColor(TH_WIRE);
1279         }
1280 }
1281
1282 /* *********************** backdraw for selection *************** */
1283
1284 static void backdrawview3d(Scene *scene, ARegion *ar, View3D *v3d)
1285 {
1286         RegionView3D *rv3d = ar->regiondata;
1287         struct Base *base = scene->basact;
1288         int multisample_enabled;
1289         rcti winrct;
1290
1291         BLI_assert(ar->regiontype == RGN_TYPE_WINDOW);
1292
1293         if (base && (base->object->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT) ||
1294                      paint_facesel_test(base->object)))
1295         {
1296                 /* do nothing */
1297         }
1298         else if ((base && (base->object->mode & OB_MODE_TEXTURE_PAINT)) &&
1299                  scene->toolsettings && (scene->toolsettings->imapaint.flag & IMAGEPAINT_PROJECT_DISABLE))
1300         {
1301                 /* do nothing */
1302         }
1303         else if ((base && (base->object->mode & OB_MODE_PARTICLE_EDIT)) &&
1304                  v3d->drawtype > OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT))
1305         {
1306                 /* do nothing */
1307         }
1308         else if (scene->obedit && v3d->drawtype > OB_WIRE &&
1309                  (v3d->flag & V3D_ZBUF_SELECT))
1310         {
1311                 /* do nothing */
1312         }
1313         else {
1314                 v3d->flag &= ~V3D_INVALID_BACKBUF;
1315                 return;
1316         }
1317
1318         if (!(v3d->flag & V3D_INVALID_BACKBUF) ) return;
1319
1320 //      if (test) {
1321 //              if (qtest()) {
1322 //                      addafterqueue(ar->win, BACKBUFDRAW, 1);
1323 //                      return;
1324 //              }
1325 //      }
1326
1327         if (v3d->drawtype > OB_WIRE) v3d->zbuf = TRUE;
1328         
1329         /* dithering and AA break color coding, so disable */
1330         glDisable(GL_DITHER);
1331
1332         multisample_enabled = glIsEnabled(GL_MULTISAMPLE_ARB);
1333         if (multisample_enabled)
1334                 glDisable(GL_MULTISAMPLE_ARB);
1335
1336         region_scissor_winrct(ar, &winrct);
1337         glScissor(winrct.xmin, winrct.ymin, winrct.xmax - winrct.xmin, winrct.ymax - winrct.ymin);
1338
1339         glClearColor(0.0, 0.0, 0.0, 0.0); 
1340         if (v3d->zbuf) {
1341                 glEnable(GL_DEPTH_TEST);
1342                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1343         }
1344         else {
1345                 glClear(GL_COLOR_BUFFER_BIT);
1346                 glDisable(GL_DEPTH_TEST);
1347         }
1348         
1349         if (rv3d->rflag & RV3D_CLIPPING)
1350                 ED_view3d_clipping_set(rv3d);
1351         
1352         G.f |= G_BACKBUFSEL;
1353         
1354         if (base && (base->lay & v3d->lay))
1355                 draw_object_backbufsel(scene, v3d, rv3d, base->object);
1356
1357         v3d->flag &= ~V3D_INVALID_BACKBUF;
1358         ar->swap = 0; /* mark invalid backbuf for wm draw */
1359
1360         G.f &= ~G_BACKBUFSEL;
1361         v3d->zbuf = FALSE;
1362         glDisable(GL_DEPTH_TEST);
1363         glEnable(GL_DITHER);
1364         if (multisample_enabled)
1365                 glEnable(GL_MULTISAMPLE_ARB);
1366
1367         if (rv3d->rflag & RV3D_CLIPPING)
1368                 ED_view3d_clipping_disable();
1369
1370         /* it is important to end a view in a transform compatible with buttons */
1371 //      persp(PERSP_WIN);  // set ortho
1372
1373 }
1374
1375 void view3d_validate_backbuf(ViewContext *vc)
1376 {
1377         if (vc->v3d->flag & V3D_INVALID_BACKBUF)
1378                 backdrawview3d(vc->scene, vc->ar, vc->v3d);
1379 }
1380
1381 /* samples a single pixel (copied from vpaint) */
1382 unsigned int view3d_sample_backbuf(ViewContext *vc, int x, int y)
1383 {
1384         unsigned int col;
1385         
1386         if (x >= vc->ar->winx || y >= vc->ar->winy) return 0;
1387         x += vc->ar->winrct.xmin;
1388         y += vc->ar->winrct.ymin;
1389         
1390         view3d_validate_backbuf(vc);
1391
1392         glReadPixels(x,  y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,  &col);
1393         glReadBuffer(GL_BACK);  
1394         
1395         if (ENDIAN_ORDER == B_ENDIAN) SWITCH_INT(col);
1396         
1397         return WM_framebuffer_to_index(col);
1398 }
1399
1400 /* reads full rect, converts indices */
1401 ImBuf *view3d_read_backbuf(ViewContext *vc, short xmin, short ymin, short xmax, short ymax)
1402 {
1403         unsigned int *dr, *rd;
1404         struct ImBuf *ibuf, *ibuf1;
1405         int a;
1406         short xminc, yminc, xmaxc, ymaxc, xs, ys;
1407         
1408         /* clip */
1409         if (xmin < 0) xminc = 0; else xminc = xmin;
1410         if (xmax >= vc->ar->winx) xmaxc = vc->ar->winx - 1; else xmaxc = xmax;
1411         if (xminc > xmaxc) return NULL;
1412
1413         if (ymin < 0) yminc = 0; else yminc = ymin;
1414         if (ymax >= vc->ar->winy) ymaxc = vc->ar->winy - 1; else ymaxc = ymax;
1415         if (yminc > ymaxc) return NULL;
1416         
1417         ibuf = IMB_allocImBuf((xmaxc - xminc + 1), (ymaxc - yminc + 1), 32, IB_rect);
1418
1419         view3d_validate_backbuf(vc); 
1420         
1421         glReadPixels(vc->ar->winrct.xmin + xminc,
1422                      vc->ar->winrct.ymin + yminc,
1423                      (xmaxc - xminc + 1),
1424                      (ymaxc - yminc + 1),
1425                      GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
1426
1427         glReadBuffer(GL_BACK);  
1428
1429         if (ENDIAN_ORDER == B_ENDIAN) IMB_convert_rgba_to_abgr(ibuf);
1430
1431         a = (xmaxc - xminc + 1) * (ymaxc - yminc + 1);
1432         dr = ibuf->rect;
1433         while (a--) {
1434                 if (*dr) *dr = WM_framebuffer_to_index(*dr);
1435                 dr++;
1436         }
1437         
1438         /* put clipped result back, if needed */
1439         if (xminc == xmin && xmaxc == xmax && yminc == ymin && ymaxc == ymax)
1440                 return ibuf;
1441         
1442         ibuf1 = IMB_allocImBuf( (xmax - xmin + 1), (ymax - ymin + 1), 32, IB_rect);
1443         rd = ibuf->rect;
1444         dr = ibuf1->rect;
1445
1446         for (ys = ymin; ys <= ymax; ys++) {
1447                 for (xs = xmin; xs <= xmax; xs++, dr++) {
1448                         if (xs >= xminc && xs <= xmaxc && ys >= yminc && ys <= ymaxc) {
1449                                 *dr = *rd;
1450                                 rd++;
1451                         }
1452                 }
1453         }
1454         IMB_freeImBuf(ibuf);
1455         return ibuf1;
1456 }
1457
1458 /* smart function to sample a rect spiralling outside, nice for backbuf selection */
1459 unsigned int view3d_sample_backbuf_rect(ViewContext *vc, const int mval[2], int size,
1460                                         unsigned int min, unsigned int max, int *dist, short strict,
1461                                         void *handle, unsigned int (*indextest)(void *handle, unsigned int index))
1462 {
1463         struct ImBuf *buf;
1464         unsigned int *bufmin, *bufmax, *tbuf;
1465         int minx, miny;
1466         int a, b, rc, nr, amount, dirvec[4][2];
1467         int distance = 0;
1468         unsigned int index = 0;
1469         short indexok = 0;      
1470
1471         amount = (size - 1) / 2;
1472
1473         minx = mval[0] - (amount + 1);
1474         miny = mval[1] - (amount + 1);
1475         buf = view3d_read_backbuf(vc, minx, miny, minx + size - 1, miny + size - 1);
1476         if (!buf) return 0;
1477
1478         rc = 0;
1479         
1480         dirvec[0][0] = 1; dirvec[0][1] = 0;
1481         dirvec[1][0] = 0; dirvec[1][1] = -size;
1482         dirvec[2][0] = -1; dirvec[2][1] = 0;
1483         dirvec[3][0] = 0; dirvec[3][1] = size;
1484         
1485         bufmin = buf->rect;
1486         tbuf = buf->rect;
1487         bufmax = buf->rect + size * size;
1488         tbuf += amount * size + amount;
1489         
1490         for (nr = 1; nr <= size; nr++) {
1491                 
1492                 for (a = 0; a < 2; a++) {
1493                         for (b = 0; b < nr; b++, distance++) {
1494                                 if (*tbuf && *tbuf >= min && *tbuf < max) { //we got a hit
1495                                         if (strict) {
1496                                                 indexok =  indextest(handle, *tbuf - min + 1);
1497                                                 if (indexok) {
1498                                                         *dist = (short) sqrt( (float)distance);
1499                                                         index = *tbuf - min + 1;
1500                                                         goto exit; 
1501                                                 }                                               
1502                                         }
1503                                         else {
1504                                                 *dist = (short) sqrt( (float)distance); // XXX, this distance is wrong -
1505                                                 index = *tbuf - min + 1; // messy yah, but indices start at 1
1506                                                 goto exit;
1507                                         }                       
1508                                 }
1509                                 
1510                                 tbuf += (dirvec[rc][0] + dirvec[rc][1]);
1511                                 
1512                                 if (tbuf < bufmin || tbuf >= bufmax) {
1513                                         goto exit;
1514                                 }
1515                         }
1516                         rc++;
1517                         rc &= 3;
1518                 }
1519         }
1520
1521 exit:
1522         IMB_freeImBuf(buf);
1523         return index;
1524 }
1525
1526
1527 /* ************************************************************* */
1528
1529 static void view3d_draw_bgpic(Scene *scene, ARegion *ar, View3D *v3d,
1530                               const short do_forground, const short do_camera_frame)
1531 {
1532         RegionView3D *rv3d = ar->regiondata;
1533         BGpic *bgpic;
1534         Image *ima;
1535         MovieClip *clip;
1536         ImBuf *ibuf = NULL, *freeibuf;
1537         float vec[4], fac, asp, zoomx, zoomy;
1538         float x1, y1, x2, y2, cx, cy;
1539         int fg_flag = do_forground ? V3D_BGPIC_FOREGROUND : 0;
1540
1541         for (bgpic = v3d->bgpicbase.first; bgpic; bgpic = bgpic->next) {
1542
1543                 if ((bgpic->flag & V3D_BGPIC_FOREGROUND) != fg_flag)
1544                         continue;
1545
1546                 if ((bgpic->view == 0) || /* zero for any */
1547                     (bgpic->view & (1 << rv3d->view)) || /* check agaist flags */
1548                     (rv3d->persp == RV3D_CAMOB && bgpic->view == (1 << RV3D_VIEW_CAMERA)))
1549                 {
1550                         /* disable individual images */
1551                         if ((bgpic->flag & V3D_BGPIC_DISABLED))
1552                                 continue;
1553
1554                         freeibuf = NULL;
1555                         if (bgpic->source == V3D_BGPIC_IMAGE) {
1556                                 ima = bgpic->ima;
1557                                 if (ima == NULL)
1558                                         continue;
1559                                 BKE_image_user_frame_calc(&bgpic->iuser, CFRA, 0);
1560                                 ibuf = BKE_image_get_ibuf(ima, &bgpic->iuser);
1561                         }
1562                         else if (bgpic->source == V3D_BGPIC_MOVIE) {
1563                                 clip = NULL;
1564
1565                                 if (bgpic->flag & V3D_BGPIC_CAMERACLIP) {
1566                                         if (scene->camera)
1567                                                 clip = BKE_object_movieclip_get(scene, scene->camera, 1);
1568                                 }
1569                                 else clip = bgpic->clip;
1570
1571                                 if (clip == NULL)
1572                                         continue;
1573
1574                                 BKE_movieclip_user_set_frame(&bgpic->cuser, CFRA);
1575                                 ibuf = BKE_movieclip_get_ibuf(clip, &bgpic->cuser);
1576
1577                                 /* working with ibuf from image and clip has got different workflow now.
1578                                  * ibuf acquired from clip is referenced by cache system and should
1579                                  * be dereferenced after usage. */
1580                                 freeibuf = ibuf;
1581                         }
1582
1583                         if (ibuf == NULL)
1584                                 continue;
1585
1586                         if ((ibuf->rect == NULL && ibuf->rect_float == NULL) || ibuf->channels != 4) { /* invalid image format */
1587                                 if (freeibuf)
1588                                         IMB_freeImBuf(freeibuf);
1589
1590                                 continue;
1591                         }
1592
1593                         if (ibuf->rect == NULL)
1594                                 IMB_rect_from_float(ibuf);
1595
1596                         if (rv3d->persp == RV3D_CAMOB) {
1597
1598                                 if (do_camera_frame) {
1599                                         rctf vb;
1600                                         ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &vb, FALSE);
1601                                         x1 = vb.xmin;
1602                                         y1 = vb.ymin;
1603                                         x2 = vb.xmax;
1604                                         y2 = vb.ymax;
1605                                 }
1606                                 else {
1607                                         x1 = ar->winrct.xmin;
1608                                         y1 = ar->winrct.ymin;
1609                                         x2 = ar->winrct.xmax;
1610                                         y2 = ar->winrct.ymax;
1611                                 }
1612                         }
1613                         else {
1614                                 float sco[2];
1615                                 const float mval_f[2] = {1.0f, 0.0f};
1616
1617                                 /* calc window coord */
1618                                 initgrabz(rv3d, 0.0, 0.0, 0.0);
1619                                 ED_view3d_win_to_delta(ar, mval_f, vec);
1620                                 fac = maxf(fabsf(vec[0]), maxf(fabsf(vec[1]), fabsf(vec[2]))); /* largest abs axis */
1621                                 fac = 1.0f / fac;
1622
1623                                 asp = ( (float)ibuf->y) / (float)ibuf->x;
1624
1625                                 zero_v3(vec);
1626                                 ED_view3d_project_float_v2(ar, vec, sco, rv3d->persmat);
1627                                 cx = sco[0];
1628                                 cy = sco[1];
1629
1630                                 x1 =  cx + fac * (bgpic->xof - bgpic->size);
1631                                 y1 =  cy + asp * fac * (bgpic->yof - bgpic->size);
1632                                 x2 =  cx + fac * (bgpic->xof + bgpic->size);
1633                                 y2 =  cy + asp * fac * (bgpic->yof + bgpic->size);
1634                         }
1635
1636                         /* complete clip? */
1637
1638                         if (x2 < 0 || y2 < 0 || x1 > ar->winx || y1 > ar->winy) {
1639                                 if (freeibuf)
1640                                         IMB_freeImBuf(freeibuf);
1641
1642                                 continue;
1643                         }
1644
1645                         zoomx = (x2 - x1) / ibuf->x;
1646                         zoomy = (y2 - y1) / ibuf->y;
1647
1648                         /* for some reason; zoomlevels down refuses to use GL_ALPHA_SCALE */
1649                         if (zoomx < 1.0f || zoomy < 1.0f) {
1650                                 float tzoom = MIN2(zoomx, zoomy);
1651                                 int mip = 0;
1652
1653                                 if ((ibuf->userflags & IB_MIPMAP_INVALID) != 0) {
1654                                         IMB_remakemipmap(ibuf, 0);
1655                                         ibuf->userflags &= ~IB_MIPMAP_INVALID;
1656                                 }
1657                                 else if (ibuf->mipmap[0] == NULL)
1658                                         IMB_makemipmap(ibuf, 0);
1659
1660                                 while (tzoom < 1.0f && mip < 8 && ibuf->mipmap[mip]) {
1661                                         tzoom *= 2.0f;
1662                                         zoomx *= 2.0f;
1663                                         zoomy *= 2.0f;
1664                                         mip++;
1665                                 }
1666                                 if (mip > 0)
1667                                         ibuf = ibuf->mipmap[mip - 1];
1668                         }
1669
1670                         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
1671                         glDepthMask(0);
1672
1673                         glEnable(GL_BLEND);
1674                         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA);
1675
1676                         glMatrixMode(GL_PROJECTION);
1677                         glPushMatrix();
1678                         glMatrixMode(GL_MODELVIEW);
1679                         glPushMatrix();
1680                         ED_region_pixelspace(ar);
1681
1682                         glPixelZoom(zoomx, zoomy);
1683                         glColor4f(1.0f, 1.0f, 1.0f, 1.0f - bgpic->blend);
1684                         glaDrawPixelsTex(x1, y1, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
1685
1686                         glPixelZoom(1.0, 1.0);
1687                         glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
1688
1689                         glMatrixMode(GL_PROJECTION);
1690                         glPopMatrix();
1691                         glMatrixMode(GL_MODELVIEW);
1692                         glPopMatrix();
1693
1694                         glDisable(GL_BLEND);
1695
1696                         glDepthMask(1);
1697                         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
1698
1699                         if (freeibuf)
1700                                 IMB_freeImBuf(freeibuf);
1701                 }
1702         }
1703 }
1704
1705 static void view3d_draw_bgpic_test(Scene *scene, ARegion *ar, View3D *v3d,
1706                                    const short do_forground, const short do_camera_frame)
1707 {
1708         RegionView3D *rv3d = ar->regiondata;
1709
1710         if ((v3d->flag & V3D_DISPBGPICS) == 0)
1711                 return;
1712
1713         if (v3d->flag2 & V3D_RENDER_OVERRIDE)
1714                 return;
1715
1716         if ((rv3d->view == RV3D_VIEW_USER) || (rv3d->persp != RV3D_ORTHO)) {
1717                 if (rv3d->persp == RV3D_CAMOB) {
1718                         view3d_draw_bgpic(scene, ar, v3d, do_forground, do_camera_frame);
1719                 }
1720         }
1721         else {
1722                 view3d_draw_bgpic(scene, ar, v3d, do_forground, do_camera_frame);
1723         }
1724 }
1725
1726 /* ****************** View3d afterdraw *************** */
1727
1728 typedef struct View3DAfter {
1729         struct View3DAfter *next, *prev;
1730         struct Base *base;
1731         int flag;
1732 } View3DAfter;
1733
1734 /* temp storage of Objects that need to be drawn as last */
1735 void add_view3d_after(ListBase *lb, Base *base, int flag)
1736 {
1737         View3DAfter *v3da = MEM_callocN(sizeof(View3DAfter), "View 3d after");
1738         BLI_assert((base->flag & OB_FROMDUPLI) == 0);
1739         BLI_addtail(lb, v3da);
1740         v3da->base = base;
1741         v3da->flag = flag;
1742 }
1743
1744 /* disables write in zbuffer and draws it over */
1745 static void view3d_draw_transp(Scene *scene, ARegion *ar, View3D *v3d)
1746 {
1747         View3DAfter *v3da, *next;
1748         
1749         glDepthMask(0);
1750         v3d->transp = TRUE;
1751         
1752         for (v3da = v3d->afterdraw_transp.first; v3da; v3da = next) {
1753                 next = v3da->next;
1754                 draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1755                 BLI_remlink(&v3d->afterdraw_transp, v3da);
1756                 MEM_freeN(v3da);
1757         }
1758         v3d->transp = FALSE;
1759         
1760         glDepthMask(1);
1761         
1762 }
1763
1764 /* clears zbuffer and draws it over */
1765 static void view3d_draw_xray(Scene *scene, ARegion *ar, View3D *v3d, int clear)
1766 {
1767         View3DAfter *v3da, *next;
1768
1769         if (clear && v3d->zbuf)
1770                 glClear(GL_DEPTH_BUFFER_BIT);
1771
1772         v3d->xray = TRUE;
1773         for (v3da = v3d->afterdraw_xray.first; v3da; v3da = next) {
1774                 next = v3da->next;
1775                 draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1776                 BLI_remlink(&v3d->afterdraw_xray, v3da);
1777                 MEM_freeN(v3da);
1778         }
1779         v3d->xray = FALSE;
1780 }
1781
1782
1783 /* clears zbuffer and draws it over */
1784 static void view3d_draw_xraytransp(Scene *scene, ARegion *ar, View3D *v3d, int clear)
1785 {
1786         View3DAfter *v3da, *next;
1787
1788         if (clear && v3d->zbuf)
1789                 glClear(GL_DEPTH_BUFFER_BIT);
1790
1791         v3d->xray = TRUE;
1792         v3d->transp = TRUE;
1793         
1794         for (v3da = v3d->afterdraw_xraytransp.first; v3da; v3da = next) {
1795                 next = v3da->next;
1796                 draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1797                 BLI_remlink(&v3d->afterdraw_xraytransp, v3da);
1798                 MEM_freeN(v3da);
1799         }
1800
1801         v3d->transp = FALSE;
1802         v3d->xray = FALSE;
1803
1804 }
1805
1806 /* *********************** */
1807
1808 /*
1809  * In most cases call draw_dupli_objects,
1810  * draw_dupli_objects_color was added because when drawing set dupli's
1811  * we need to force the color
1812  */
1813
1814 #if 0
1815 int dupli_ob_sort(void *arg1, void *arg2)
1816 {
1817         void *p1 = ((DupliObject *)arg1)->ob;
1818         void *p2 = ((DupliObject *)arg2)->ob;
1819         int val = 0;
1820         if (p1 < p2) val = -1;
1821         else if (p1 > p2) val = 1;
1822         return val;
1823 }
1824 #endif
1825
1826
1827 static DupliObject *dupli_step(DupliObject *dob)
1828 {
1829         while (dob && dob->no_draw)
1830                 dob = dob->next;
1831         return dob;
1832 }
1833
1834 static void draw_dupli_objects_color(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int color)
1835 {
1836         RegionView3D *rv3d = ar->regiondata;
1837         ListBase *lb;
1838         DupliObject *dob_prev = NULL, *dob, *dob_next = NULL;
1839         Base tbase = {NULL};
1840         BoundBox bb, *bb_tmp; /* use a copy because draw_object, calls clear_mesh_caches */
1841         GLuint displist = 0;
1842         short transflag, use_displist = -1;  /* -1 is initialize */
1843         char dt, dtx;
1844         
1845         if (base->object->restrictflag & OB_RESTRICT_VIEW) return;
1846         
1847         tbase.flag = OB_FROMDUPLI | base->flag;
1848         lb = object_duplilist(scene, base->object);
1849         // BLI_sortlist(lb, dupli_ob_sort); // might be nice to have if we have a dupli list with mixed objects.
1850
1851         dob = dupli_step(lb->first);
1852         if (dob) dob_next = dupli_step(dob->next);
1853
1854         for (; dob; dob_prev = dob, dob = dob_next, dob_next = dob_next ? dupli_step(dob_next->next) : NULL) {
1855                 tbase.object = dob->ob;
1856
1857                 /* extra service: draw the duplicator in drawtype of parent */
1858                 /* MIN2 for the drawtype to allow bounding box objects in groups for lods */
1859                 dt = tbase.object->dt;   tbase.object->dt = MIN2(tbase.object->dt, base->object->dt);
1860                 dtx = tbase.object->dtx; tbase.object->dtx = base->object->dtx;
1861
1862                 /* negative scale flag has to propagate */
1863                 transflag = tbase.object->transflag;
1864                 if (base->object->transflag & OB_NEG_SCALE)
1865                         tbase.object->transflag ^= OB_NEG_SCALE;
1866
1867                 UI_ThemeColorBlend(color, TH_BACK, 0.5);
1868
1869                 /* generate displist, test for new object */
1870                 if (dob_prev && dob_prev->ob != dob->ob) {
1871                         if (use_displist == TRUE)
1872                                 glDeleteLists(displist, 1);
1873
1874                         use_displist = -1;
1875                 }
1876
1877                 /* generate displist */
1878                 if (use_displist == -1) {
1879
1880                         /* note, since this was added, its checked (dob->type == OB_DUPLIGROUP)
1881                          * however this is very slow, it was probably needed for the NLA
1882                          * offset feature (used in group-duplicate.blend but no longer works in 2.5)
1883                          * so for now it should be ok to - campbell */
1884
1885                         if ( /* if this is the last no need  to make a displist */
1886                             (dob_next == NULL || dob_next->ob != dob->ob) ||
1887                             /* lamp drawing messes with matrices, could be handled smarter... but this works */
1888                             (dob->ob->type == OB_LAMP) ||
1889                             (dob->type == OB_DUPLIGROUP && dob->animated) ||
1890                             !(bb_tmp = BKE_object_boundbox_get(dob->ob)))
1891                         {
1892                                 // printf("draw_dupli_objects_color: skipping displist for %s\n", dob->ob->id.name+2);
1893                                 use_displist = FALSE;
1894                         }
1895                         else {
1896                                 // printf("draw_dupli_objects_color: using displist for %s\n", dob->ob->id.name+2);
1897                                 bb = *bb_tmp; /* must make a copy  */
1898
1899                                 /* disable boundbox check for list creation */
1900                                 BKE_object_boundbox_flag(dob->ob, OB_BB_DISABLED, 1);
1901                                 /* need this for next part of code */
1902                                 unit_m4(dob->ob->obmat);    /* obmat gets restored */
1903
1904                                 displist = glGenLists(1);
1905                                 glNewList(displist, GL_COMPILE);
1906                                 draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
1907                                 glEndList();
1908
1909                                 use_displist = TRUE;
1910                                 BKE_object_boundbox_flag(dob->ob, OB_BB_DISABLED, 0);
1911                         }
1912                 }
1913                 if (use_displist) {
1914                         glMultMatrixf(dob->mat);
1915                         if (ED_view3d_boundbox_clip(rv3d, dob->mat, &bb))
1916                                 glCallList(displist);
1917                         glLoadMatrixf(rv3d->viewmat);
1918                 }
1919                 else {
1920                         copy_m4_m4(dob->ob->obmat, dob->mat);
1921                         draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
1922                 }
1923
1924                 tbase.object->dt = dt;
1925                 tbase.object->dtx = dtx;
1926                 tbase.object->transflag = transflag;
1927         }
1928         
1929         /* Transp afterdraw disabled, afterdraw only stores base pointers, and duplis can be same obj */
1930         
1931         free_object_duplilist(lb);  /* does restore */
1932         
1933         if (use_displist)
1934                 glDeleteLists(displist, 1);
1935 }
1936
1937 static void draw_dupli_objects(Scene *scene, ARegion *ar, View3D *v3d, Base *base)
1938 {
1939         /* define the color here so draw_dupli_objects_color can be called
1940          * from the set loop */
1941         
1942         int color = (base->flag & SELECT) ? TH_SELECT : TH_WIRE;
1943         /* debug */
1944         if (base->object->dup_group && base->object->dup_group->id.us < 1)
1945                 color = TH_REDALERT;
1946         
1947         draw_dupli_objects_color(scene, ar, v3d, base, color);
1948 }
1949
1950 void view3d_update_depths_rect(ARegion *ar, ViewDepths *d, rcti *rect)
1951 {
1952         int x, y, w, h; 
1953         rcti r;
1954         /* clamp rect by area */
1955
1956         r.xmin = 0;
1957         r.xmax = ar->winx - 1;
1958         r.ymin = 0;
1959         r.ymax = ar->winy - 1;
1960
1961         /* Constrain rect to depth bounds */
1962         BLI_isect_rcti(&r, rect, rect);
1963
1964         /* assign values to compare with the ViewDepths */
1965         x = rect->xmin;
1966         y = rect->ymin;
1967
1968         w = rect->xmax - rect->xmin;
1969         h = rect->ymax - rect->ymin;
1970
1971         if (w <= 0 || h <= 0) {
1972                 if (d->depths)
1973                         MEM_freeN(d->depths);
1974                 d->depths = NULL;
1975
1976                 d->damaged = FALSE;
1977         }
1978         else if (d->w != w ||
1979                  d->h != h ||
1980                  d->x != x ||
1981                  d->y != y ||
1982                  d->depths == NULL
1983                  )
1984         {
1985                 d->x = x;
1986                 d->y = y;
1987                 d->w = w;
1988                 d->h = h;
1989
1990                 if (d->depths)
1991                         MEM_freeN(d->depths);
1992
1993                 d->depths = MEM_mallocN(sizeof(float) * d->w * d->h, "View depths Subset");
1994                 
1995                 d->damaged = TRUE;
1996         }
1997
1998         if (d->damaged) {
1999                 glReadPixels(ar->winrct.xmin + d->x, ar->winrct.ymin + d->y, d->w, d->h, GL_DEPTH_COMPONENT, GL_FLOAT, d->depths);
2000                 glGetDoublev(GL_DEPTH_RANGE, d->depth_range);
2001                 d->damaged = FALSE;
2002         }
2003 }
2004
2005 /* note, with nouveau drivers the glReadPixels() is very slow. [#24339] */
2006 void ED_view3d_depth_update(ARegion *ar)
2007 {
2008         RegionView3D *rv3d = ar->regiondata;
2009         
2010         /* Create storage for, and, if necessary, copy depth buffer */
2011         if (!rv3d->depths) rv3d->depths = MEM_callocN(sizeof(ViewDepths), "ViewDepths");
2012         if (rv3d->depths) {
2013                 ViewDepths *d = rv3d->depths;
2014                 if (d->w != ar->winx ||
2015                     d->h != ar->winy ||
2016                     !d->depths)
2017                 {
2018                         d->w = ar->winx;
2019                         d->h = ar->winy;
2020                         if (d->depths)
2021                                 MEM_freeN(d->depths);
2022                         d->depths = MEM_mallocN(sizeof(float) * d->w * d->h, "View depths");
2023                         d->damaged = 1;
2024                 }
2025                 
2026                 if (d->damaged) {
2027                         glReadPixels(ar->winrct.xmin, ar->winrct.ymin, d->w, d->h,
2028                                      GL_DEPTH_COMPONENT, GL_FLOAT, d->depths);
2029                         
2030                         glGetDoublev(GL_DEPTH_RANGE, d->depth_range);
2031                         
2032                         d->damaged = 0;
2033                 }
2034         }
2035 }
2036
2037 /* utility function to find the closest Z value, use for autodepth */
2038 float view3d_depth_near(ViewDepths *d)
2039 {
2040         /* convert to float for comparisons */
2041         const float near = (float)d->depth_range[0];
2042         const float far_real = (float)d->depth_range[1];
2043         float far = far_real;
2044
2045         const float *depths = d->depths;
2046         float depth = FLT_MAX;
2047         int i = (int)d->w * (int)d->h; /* cast to avoid short overflow */
2048
2049         /* far is both the starting 'far' value
2050          * and the closest value found. */      
2051         while (i--) {
2052                 depth = *depths++;
2053                 if ((depth < far) && (depth > near)) {
2054                         far = depth;
2055                 }
2056         }
2057
2058         return far == far_real ? FLT_MAX : far;
2059 }
2060
2061 void draw_depth_gpencil(Scene *scene, ARegion *ar, View3D *v3d)
2062 {
2063         short zbuf = v3d->zbuf;
2064         RegionView3D *rv3d = ar->regiondata;
2065
2066         setwinmatrixview3d(ar, v3d, NULL);  /* 0= no pick rect */
2067         setviewmatrixview3d(scene, v3d, rv3d);  /* note: calls BKE_object_where_is_calc for camera... */
2068
2069         mult_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
2070         invert_m4_m4(rv3d->persinv, rv3d->persmat);
2071         invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
2072
2073         glClear(GL_DEPTH_BUFFER_BIT);
2074
2075         glLoadMatrixf(rv3d->viewmat);
2076
2077         v3d->zbuf = TRUE;
2078         glEnable(GL_DEPTH_TEST);
2079
2080         draw_gpencil_view3d(scene, v3d, ar, 1);
2081         
2082         v3d->zbuf = zbuf;
2083
2084 }
2085
2086 void draw_depth(Scene *scene, ARegion *ar, View3D *v3d, int (*func)(void *))
2087 {
2088         RegionView3D *rv3d = ar->regiondata;
2089         Base *base;
2090         short zbuf = v3d->zbuf;
2091         short flag = v3d->flag;
2092         float glalphaclip = U.glalphaclip;
2093         int obcenter_dia = U.obcenter_dia;
2094         /* temp set drawtype to solid */
2095         
2096         /* Setting these temporarily is not nice */
2097         v3d->flag &= ~V3D_SELECT_OUTLINE;
2098         U.glalphaclip = 0.5; /* not that nice but means we wont zoom into billboards */
2099         U.obcenter_dia = 0;
2100         
2101         setwinmatrixview3d(ar, v3d, NULL);  /* 0= no pick rect */
2102         setviewmatrixview3d(scene, v3d, rv3d);  /* note: calls BKE_object_where_is_calc for camera... */
2103         
2104         mult_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
2105         invert_m4_m4(rv3d->persinv, rv3d->persmat);
2106         invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
2107         
2108         glClear(GL_DEPTH_BUFFER_BIT);
2109         
2110         glLoadMatrixf(rv3d->viewmat);
2111 //      persp(PERSP_STORE);  // store correct view for persp(PERSP_VIEW) calls
2112         
2113         if (rv3d->rflag & RV3D_CLIPPING) {
2114                 ED_view3d_clipping_set(rv3d);
2115         }
2116         
2117         v3d->zbuf = TRUE;
2118         glEnable(GL_DEPTH_TEST);
2119         
2120         /* draw set first */
2121         if (scene->set) {
2122                 Scene *sce_iter;
2123                 for (SETLOOPER(scene->set, sce_iter, base)) {
2124                         if (v3d->lay & base->lay) {
2125                                 if (func == NULL || func(base)) {
2126                                         draw_object(scene, ar, v3d, base, 0);
2127                                         if (base->object->transflag & OB_DUPLI) {
2128                                                 draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
2129                                         }
2130                                 }
2131                         }
2132                 }
2133         }
2134         
2135         for (base = scene->base.first; base; base = base->next) {
2136                 if (v3d->lay & base->lay) {
2137                         if (func == NULL || func(base)) {
2138                                 /* dupli drawing */
2139                                 if (base->object->transflag & OB_DUPLI) {
2140                                         draw_dupli_objects(scene, ar, v3d, base);
2141                                 }
2142                                 draw_object(scene, ar, v3d, base, 0);
2143                         }
2144                 }
2145         }
2146         
2147         /* this isn't that nice, draw xray objects as if they are normal */
2148         if (v3d->afterdraw_transp.first ||
2149             v3d->afterdraw_xray.first ||
2150             v3d->afterdraw_xraytransp.first)
2151         {
2152                 View3DAfter *v3da, *next;
2153                 int mask_orig;
2154
2155                 v3d->xray = TRUE;
2156                 
2157                 /* transp materials can change the depth mask, see #21388 */
2158                 glGetIntegerv(GL_DEPTH_WRITEMASK, &mask_orig);
2159
2160
2161                 if (v3d->afterdraw_xray.first || v3d->afterdraw_xraytransp.first) {
2162                         glDepthFunc(GL_ALWAYS); /* always write into the depth bufer, overwriting front z values */
2163                         for (v3da = v3d->afterdraw_xray.first; v3da; v3da = next) {
2164                                 next = v3da->next;
2165                                 draw_object(scene, ar, v3d, v3da->base, 0);
2166                         }
2167                         glDepthFunc(GL_LEQUAL); /* Now write the depth buffer normally */
2168                 }
2169
2170                 /* draw 3 passes, transp/xray/xraytransp */
2171                 v3d->xray = FALSE;
2172                 v3d->transp = TRUE;
2173                 for (v3da = v3d->afterdraw_transp.first; v3da; v3da = next) {
2174                         next = v3da->next;
2175                         draw_object(scene, ar, v3d, v3da->base, 0);
2176                         BLI_remlink(&v3d->afterdraw_transp, v3da);
2177                         MEM_freeN(v3da);
2178                 }
2179
2180                 v3d->xray = TRUE;
2181                 v3d->transp = FALSE;
2182                 for (v3da = v3d->afterdraw_xray.first; v3da; v3da = next) {
2183                         next = v3da->next;
2184                         draw_object(scene, ar, v3d, v3da->base, 0);
2185                         BLI_remlink(&v3d->afterdraw_xray, v3da);
2186                         MEM_freeN(v3da);
2187                 }
2188
2189                 v3d->xray = TRUE;
2190                 v3d->transp = TRUE;
2191                 for (v3da = v3d->afterdraw_xraytransp.first; v3da; v3da = next) {
2192                         next = v3da->next;
2193                         draw_object(scene, ar, v3d, v3da->base, 0);
2194                         BLI_remlink(&v3d->afterdraw_xraytransp, v3da);
2195                         MEM_freeN(v3da);
2196                 }
2197
2198                 
2199                 v3d->xray = FALSE;
2200                 v3d->transp = FALSE;
2201
2202                 glDepthMask(mask_orig);
2203         }
2204         
2205         if (rv3d->rflag & RV3D_CLIPPING)
2206                 ED_view3d_clipping_disable();
2207         
2208         v3d->zbuf = zbuf;
2209         if (!v3d->zbuf) glDisable(GL_DEPTH_TEST);
2210
2211         U.glalphaclip = glalphaclip;
2212         v3d->flag = flag;
2213         U.obcenter_dia = obcenter_dia;
2214 }
2215
2216 typedef struct View3DShadow {
2217         struct View3DShadow *next, *prev;
2218         GPULamp *lamp;
2219 } View3DShadow;
2220
2221 static void gpu_render_lamp_update(Scene *scene, View3D *v3d, Object *ob, Object *par,
2222                                    float obmat[][4], ListBase *shadows)
2223 {
2224         GPULamp *lamp;
2225         Lamp *la = (Lamp *)ob->data;
2226         View3DShadow *shadow;
2227         
2228         lamp = GPU_lamp_from_blender(scene, ob, par);
2229         
2230         if (lamp) {
2231                 GPU_lamp_update(lamp, ob->lay, (ob->restrictflag & OB_RESTRICT_RENDER), obmat);
2232                 GPU_lamp_update_colors(lamp, la->r, la->g, la->b, la->energy);
2233                 
2234                 if ((ob->lay & v3d->lay) && GPU_lamp_has_shadow_buffer(lamp)) {
2235                         shadow = MEM_callocN(sizeof(View3DShadow), "View3DShadow");
2236                         shadow->lamp = lamp;
2237                         BLI_addtail(shadows, shadow);
2238                 }
2239         }
2240 }
2241
2242 static void gpu_update_lamps_shadows(Scene *scene, View3D *v3d)
2243 {
2244         ListBase shadows;
2245         View3DShadow *shadow;
2246         Scene *sce_iter;
2247         Base *base;
2248         Object *ob;
2249         
2250         shadows.first = shadows.last = NULL;
2251         
2252         /* update lamp transform and gather shadow lamps */
2253         for (SETLOOPER(scene, sce_iter, base)) {
2254                 ob = base->object;
2255                 
2256                 if (ob->type == OB_LAMP)
2257                         gpu_render_lamp_update(scene, v3d, ob, NULL, ob->obmat, &shadows);
2258                 
2259                 if (ob->transflag & OB_DUPLI) {
2260                         DupliObject *dob;
2261                         ListBase *lb = object_duplilist(scene, ob);
2262                         
2263                         for (dob = lb->first; dob; dob = dob->next)
2264                                 if (dob->ob->type == OB_LAMP)
2265                                         gpu_render_lamp_update(scene, v3d, dob->ob, ob, dob->mat, &shadows);
2266                         
2267                         free_object_duplilist(lb);
2268                 }
2269         }
2270         
2271         /* render shadows after updating all lamps, nested object_duplilist
2272          * don't work correct since it's replacing object matrices */
2273         for (shadow = shadows.first; shadow; shadow = shadow->next) {
2274                 /* this needs to be done better .. */
2275                 float viewmat[4][4], winmat[4][4];
2276                 int drawtype, lay, winsize, flag2 = v3d->flag2;
2277                 ARegion ar = {NULL};
2278                 RegionView3D rv3d = {{{0}}};
2279                 
2280                 drawtype = v3d->drawtype;
2281                 lay = v3d->lay;
2282                 
2283                 v3d->drawtype = OB_SOLID;
2284                 v3d->lay &= GPU_lamp_shadow_layer(shadow->lamp);
2285                 v3d->flag2 &= ~V3D_SOLID_TEX;
2286                 v3d->flag2 |= V3D_RENDER_OVERRIDE | V3D_RENDER_SHADOW;
2287                 
2288                 GPU_lamp_shadow_buffer_bind(shadow->lamp, viewmat, &winsize, winmat);
2289
2290                 ar.regiondata = &rv3d;
2291                 ar.regiontype = RGN_TYPE_WINDOW;
2292                 rv3d.persp = RV3D_CAMOB;
2293                 copy_m4_m4(rv3d.winmat, winmat);
2294                 copy_m4_m4(rv3d.viewmat, viewmat);
2295                 invert_m4_m4(rv3d.viewinv, rv3d.viewmat);
2296                 mult_m4_m4m4(rv3d.persmat, rv3d.winmat, rv3d.viewmat);
2297                 invert_m4_m4(rv3d.persinv, rv3d.viewinv);
2298
2299                 ED_view3d_draw_offscreen(scene, v3d, &ar, winsize, winsize, viewmat, winmat, FALSE);
2300                 GPU_lamp_shadow_buffer_unbind(shadow->lamp);
2301                 
2302                 v3d->drawtype = drawtype;
2303                 v3d->lay = lay;
2304                 v3d->flag2 = flag2;
2305         }
2306         
2307         BLI_freelistN(&shadows);
2308 }
2309
2310 /* *********************** customdata **************** */
2311
2312 CustomDataMask ED_view3d_datamask(Scene *scene, View3D *v3d)
2313 {
2314         CustomDataMask mask = 0;
2315
2316         if (ELEM(v3d->drawtype, OB_TEXTURE, OB_MATERIAL) ||
2317             ((v3d->drawtype == OB_SOLID) && (v3d->flag2 & V3D_SOLID_TEX)))
2318         {
2319                 mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
2320
2321                 if (BKE_scene_use_new_shading_nodes(scene)) {
2322                         if (v3d->drawtype == OB_MATERIAL)
2323                                 mask |= CD_MASK_ORCO;
2324                 }
2325                 else {
2326                         if (scene->gm.matmode == GAME_MAT_GLSL)
2327                                 mask |= CD_MASK_ORCO;
2328                 }
2329         }
2330
2331         return mask;
2332 }
2333
2334 CustomDataMask ED_view3d_object_datamask(Scene *scene)
2335 {
2336         Object *ob = scene->basact ? scene->basact->object : NULL;
2337         CustomDataMask mask = 0;
2338
2339         if (ob) {
2340                 /* check if we need tfaces & mcols due to face select or texture paint */
2341                 if (paint_facesel_test(ob) || (ob->mode & OB_MODE_TEXTURE_PAINT)) {
2342                         mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
2343                 }
2344
2345                 /* check if we need mcols due to vertex paint or weightpaint */
2346                 if (ob->mode & OB_MODE_VERTEX_PAINT) {
2347                         mask |= CD_MASK_MCOL;
2348                 }
2349
2350                 if (ob->mode & OB_MODE_WEIGHT_PAINT) {
2351                         mask |= CD_MASK_PREVIEW_MCOL;
2352                 }
2353
2354                 if (ob->mode & OB_MODE_EDIT)
2355                         mask |= CD_MASK_MVERT_SKIN;
2356         }
2357
2358         return mask;
2359 }
2360
2361 /* goes over all modes and view3d settings */
2362 CustomDataMask ED_view3d_screen_datamask(bScreen *screen)
2363 {
2364         Scene *scene = screen->scene;
2365         CustomDataMask mask = CD_MASK_BAREMESH;
2366         ScrArea *sa;
2367         
2368         /* check if we need tfaces & mcols due to view mode */
2369         for (sa = screen->areabase.first; sa; sa = sa->next) {
2370                 if (sa->spacetype == SPACE_VIEW3D) {
2371                         mask |= ED_view3d_datamask(scene, (View3D *)sa->spacedata.first);
2372                 }
2373         }
2374
2375         mask |= ED_view3d_object_datamask(scene);
2376
2377         return mask;
2378 }
2379
2380 void ED_view3d_update_viewmat(Scene *scene, View3D *v3d, ARegion *ar, float viewmat[][4], float winmat[][4])
2381 {
2382         RegionView3D *rv3d = ar->regiondata;
2383
2384         /* setup window matrices */
2385         if (winmat)
2386                 copy_m4_m4(rv3d->winmat, winmat);
2387         else
2388                 setwinmatrixview3d(ar, v3d, NULL);  /* NULL= no pickrect */
2389
2390         /* setup view matrix */
2391         if (viewmat)
2392                 copy_m4_m4(rv3d->viewmat, viewmat);
2393         else
2394                 setviewmatrixview3d(scene, v3d, rv3d);  /* note: calls BKE_object_where_is_calc for camera... */
2395
2396         /* update utilitity matrices */
2397         mult_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
2398         invert_m4_m4(rv3d->persinv, rv3d->persmat);
2399         invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
2400
2401         /* calculate pixelsize factor once, is used for lamps and obcenters */
2402         {
2403                 /* note:  '1.0f / len_v3(v1)'  replaced  'len_v3(rv3d->viewmat[0])'
2404                  * because of float point precision problems at large values [#23908] */
2405                 float v1[3], v2[3];
2406                 float len1, len2;
2407
2408                 v1[0] = rv3d->persmat[0][0];
2409                 v1[1] = rv3d->persmat[1][0];
2410                 v1[2] = rv3d->persmat[2][0];
2411
2412                 v2[0] = rv3d->persmat[0][1];
2413                 v2[1] = rv3d->persmat[1][1];
2414                 v2[2] = rv3d->persmat[2][1];
2415
2416                 len1 = 1.0f / len_v3(v1);
2417                 len2 = 1.0f / len_v3(v2);
2418
2419                 rv3d->pixsize = (2.0f * MAX2(len1, len2)) / (float)MAX2(ar->winx, ar->winy);
2420         }
2421 }
2422
2423 static void view3d_main_area_setup_view(Scene *scene, View3D *v3d, ARegion *ar, float viewmat[][4], float winmat[][4])
2424 {
2425         RegionView3D *rv3d = ar->regiondata;
2426
2427         ED_view3d_update_viewmat(scene, v3d, ar, viewmat, winmat);
2428
2429         /* set for opengl */
2430         glMatrixMode(GL_PROJECTION);
2431         glLoadMatrixf(rv3d->winmat);
2432         glMatrixMode(GL_MODELVIEW);
2433         glLoadMatrixf(rv3d->viewmat);
2434 }
2435
2436 void ED_view3d_draw_offscreen(Scene *scene, View3D *v3d, ARegion *ar,
2437                               int winx, int winy, float viewmat[][4], float winmat[][4],
2438                               int do_bgpic)
2439 {
2440         RegionView3D *rv3d = ar->regiondata;
2441         Base *base;
2442         float backcol[3];
2443         int bwinx, bwiny;
2444         rcti brect;
2445
2446         glPushMatrix();
2447
2448         /* set temporary new size */
2449         bwinx = ar->winx;
2450         bwiny = ar->winy;
2451         brect = ar->winrct;
2452         
2453         ar->winx = winx;
2454         ar->winy = winy;
2455         ar->winrct.xmin = 0;
2456         ar->winrct.ymin = 0;
2457         ar->winrct.xmax = winx;
2458         ar->winrct.ymax = winy;
2459
2460         /* set theme */
2461         UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
2462         
2463         /* set flags */
2464         G.f |= G_RENDER_OGL;
2465
2466         /* free images which can have changed on frame-change
2467          * warning! can be slow so only free animated images - campbell */
2468         GPU_free_images_anim();
2469         
2470         /* shadow buffers, before we setup matrices */
2471         if (draw_glsl_material(scene, NULL, v3d, v3d->drawtype))
2472                 gpu_update_lamps_shadows(scene, v3d);
2473
2474         /* set background color, fallback on the view background color
2475          * (if active clip is set but frame is failed to load fallback to horizon color as background) */
2476         if (scene->world) {
2477                 if (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
2478                         linearrgb_to_srgb_v3_v3(backcol, &scene->world->horr);
2479                 else
2480                         copy_v3_v3(backcol, &scene->world->horr);
2481                 glClearColor(backcol[0], backcol[1], backcol[2], 0.0);
2482         }
2483         else {
2484                 UI_ThemeClearColor(TH_BACK);
2485         }
2486
2487
2488         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2489
2490         /* setup view matrices */
2491         view3d_main_area_setup_view(scene, v3d, ar, viewmat, winmat);
2492
2493         if (rv3d->rflag & RV3D_CLIPPING)
2494                 view3d_draw_clipping(rv3d);
2495
2496         /* set zbuffer */
2497         if (v3d->drawtype > OB_WIRE) {
2498                 v3d->zbuf = TRUE;
2499                 glEnable(GL_DEPTH_TEST);
2500         }
2501         else
2502                 v3d->zbuf = FALSE;
2503
2504         /* important to do before clipping */
2505         if (do_bgpic) {
2506                 view3d_draw_bgpic_test(scene, ar, v3d, FALSE, FALSE);
2507         }
2508
2509         if (rv3d->rflag & RV3D_CLIPPING)
2510                 ED_view3d_clipping_set(rv3d);
2511
2512         /* draw set first */
2513         if (scene->set) {
2514                 Scene *sce_iter;
2515                 for (SETLOOPER(scene->set, sce_iter, base)) {
2516                         if (v3d->lay & base->lay) {
2517                                 UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
2518                                 draw_object(scene, ar, v3d, base, DRAW_CONSTCOLOR | DRAW_SCENESET);
2519                                 
2520                                 if (base->object->transflag & OB_DUPLI)
2521                                         draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
2522                         }
2523                 }
2524         }
2525         
2526         /* then draw not selected and the duplis, but skip editmode object */
2527         for (base = scene->base.first; base; base = base->next) {
2528                 if (v3d->lay & base->lay) {
2529                         /* dupli drawing */
2530                         if (base->object->transflag & OB_DUPLI)
2531                                 draw_dupli_objects(scene, ar, v3d, base);
2532
2533                         draw_object(scene, ar, v3d, base, 0);
2534                 }
2535         }
2536
2537         /* must be before xray draw which clears the depth buffer */
2538         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2539         draw_gpencil_view3d(scene, v3d, ar, 1);
2540         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2541
2542         /* transp and X-ray afterdraw stuff */
2543         if (v3d->afterdraw_transp.first)     view3d_draw_transp(scene, ar, v3d);
2544         if (v3d->afterdraw_xray.first)       view3d_draw_xray(scene, ar, v3d, 1);       /* clears zbuffer if it is used! */
2545         if (v3d->afterdraw_xraytransp.first) view3d_draw_xraytransp(scene, ar, v3d, 1);
2546
2547         if (rv3d->rflag & RV3D_CLIPPING)
2548                 ED_view3d_clipping_disable();
2549
2550         /* important to do after clipping */
2551         if (do_bgpic) {
2552                 view3d_draw_bgpic_test(scene, ar, v3d, TRUE, FALSE);
2553         }
2554
2555         /* cleanup */
2556         if (v3d->zbuf) {
2557                 v3d->zbuf = FALSE;
2558                 glDisable(GL_DEPTH_TEST);
2559         }
2560
2561         /* draw grease-pencil stuff */
2562         ED_region_pixelspace(ar);
2563
2564         /* draw grease-pencil stuff - needed to get paint-buffer shown too (since it's 2D) */
2565         draw_gpencil_view3d(scene, v3d, ar, 0);
2566
2567         /* freeing the images again here could be done after the operator runs, leaving for now */
2568         GPU_free_images_anim();
2569
2570         /* restore size */
2571         ar->winx = bwinx;
2572         ar->winy = bwiny;
2573         ar->winrct = brect;
2574
2575         glPopMatrix();
2576
2577         // XXX, without this the sequencer flickers with opengl draw enabled, need to find out why - campbell
2578         glColor4ub(255, 255, 255, 255);
2579
2580         G.f &= ~G_RENDER_OGL;
2581 }
2582
2583 /* utility func for ED_view3d_draw_offscreen */
2584 ImBuf *ED_view3d_draw_offscreen_imbuf(Scene *scene, View3D *v3d, ARegion *ar,
2585                                       int sizex, int sizey, unsigned int flag, int draw_background, char err_out[256])
2586 {
2587         RegionView3D *rv3d = ar->regiondata;
2588         ImBuf *ibuf;
2589         GPUOffScreen *ofs;
2590         
2591         /* state changes make normal drawing go weird otherwise */
2592         glPushAttrib(GL_LIGHTING_BIT);
2593
2594         /* bind */
2595         ofs = GPU_offscreen_create(sizex, sizey, err_out);
2596         if (ofs == NULL)
2597                 return NULL;
2598
2599         GPU_offscreen_bind(ofs);
2600
2601         /* render 3d view */
2602         if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
2603                 CameraParams params;
2604
2605                 BKE_camera_params_init(&params);
2606                 BKE_camera_params_from_object(&params, v3d->camera);
2607                 BKE_camera_params_compute_viewplane(&params, sizex, sizey, scene->r.xasp, scene->r.yasp);
2608                 BKE_camera_params_compute_matrix(&params);
2609
2610                 ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, params.winmat, draw_background);
2611         }
2612         else {
2613                 ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, NULL, draw_background);
2614         }
2615
2616         /* read in pixels & stamp */
2617         ibuf = IMB_allocImBuf(sizex, sizey, 32, flag);
2618
2619         if (ibuf->rect_float)
2620                 GPU_offscreen_read_pixels(ofs, GL_FLOAT, ibuf->rect_float);
2621         else if (ibuf->rect)
2622                 GPU_offscreen_read_pixels(ofs, GL_UNSIGNED_BYTE, ibuf->rect);
2623         
2624         //if ((scene->r.stamp & R_STAMP_ALL) && (scene->r.stamp & R_STAMP_DRAW))
2625         //      BKE_stamp_buf(scene, NULL, rr->rectf, rr->rectx, rr->recty, 4);
2626
2627         /* unbind */
2628         GPU_offscreen_unbind(ofs);
2629         GPU_offscreen_free(ofs);
2630
2631         glPopAttrib();
2632         
2633         if (ibuf->rect_float && ibuf->rect)
2634                 IMB_rect_from_float(ibuf);
2635         
2636         return ibuf;
2637 }
2638
2639 /* creates own 3d views, used by the sequencer */
2640 ImBuf *ED_view3d_draw_offscreen_imbuf_simple(Scene *scene, Object *camera, int width, int height,
2641                                              unsigned int flag, int drawtype, int draw_background, char err_out[256])
2642 {
2643         View3D v3d = {NULL};
2644         ARegion ar = {NULL};
2645         RegionView3D rv3d = {{{0}}};
2646
2647         /* connect data */
2648         v3d.regionbase.first = v3d.regionbase.last = &ar;
2649         ar.regiondata = &rv3d;
2650         ar.regiontype = RGN_TYPE_WINDOW;
2651
2652         v3d.camera = camera;
2653         v3d.lay = scene->lay;
2654         v3d.drawtype = drawtype;
2655         v3d.flag2 = V3D_RENDER_OVERRIDE;
2656
2657         rv3d.persp = RV3D_CAMOB;
2658
2659         copy_m4_m4(rv3d.viewinv, v3d.camera->obmat);
2660         normalize_m4(rv3d.viewinv);
2661         invert_m4_m4(rv3d.viewmat, rv3d.viewinv);
2662
2663         {
2664                 CameraParams params;
2665
2666                 BKE_camera_params_init(&params);
2667                 BKE_camera_params_from_object(&params, v3d.camera);
2668                 BKE_camera_params_compute_viewplane(&params, width, height, scene->r.xasp, scene->r.yasp);
2669                 BKE_camera_params_compute_matrix(&params);
2670
2671                 copy_m4_m4(rv3d.winmat, params.winmat);
2672                 v3d.near = params.clipsta;
2673                 v3d.far = params.clipend;
2674                 v3d.lens = params.lens;
2675         }
2676
2677         mult_m4_m4m4(rv3d.persmat, rv3d.winmat, rv3d.viewmat);
2678         invert_m4_m4(rv3d.persinv, rv3d.viewinv);
2679
2680         return ED_view3d_draw_offscreen_imbuf(scene, &v3d, &ar, width, height, flag, draw_background, err_out);
2681
2682         // seq_view3d_cb(scene, cfra, render_size, seqrectx, seqrecty);
2683 }
2684
2685
2686 /* NOTE: the info that this uses is updated in ED_refresh_viewport_fps(), 
2687  * which currently gets called during SCREEN_OT_animation_step.
2688  */
2689 static void draw_viewport_fps(Scene *scene, ARegion *ar)
2690 {
2691         ScreenFrameRateInfo *fpsi = scene->fps_info;
2692         float fps;
2693         char printable[16];
2694         int i, tot;
2695         
2696         if (!fpsi || !fpsi->lredrawtime || !fpsi->redrawtime)
2697                 return;
2698         
2699         printable[0] = '\0';
2700         
2701 #if 0
2702         /* this is too simple, better do an average */
2703         fps = (float)(1.0 / (fpsi->lredrawtime - fpsi->redrawtime))
2704 #else
2705         fpsi->redrawtimes_fps[fpsi->redrawtime_index] = (float)(1.0 / (fpsi->lredrawtime - fpsi->redrawtime));
2706         
2707         for (i = 0, tot = 0, fps = 0.0f; i < REDRAW_FRAME_AVERAGE; i++) {
2708                 if (fpsi->redrawtimes_fps[i]) {
2709                         fps += fpsi->redrawtimes_fps[i];
2710                         tot++;
2711                 }
2712         }
2713         if (tot) {
2714                 fpsi->redrawtime_index = (fpsi->redrawtime_index + 1) % REDRAW_FRAME_AVERAGE;
2715                 
2716                 //fpsi->redrawtime_index++;
2717                 //if (fpsi->redrawtime >= REDRAW_FRAME_AVERAGE)
2718                 //      fpsi->redrawtime = 0;
2719                 
2720                 fps = fps / tot;
2721         }
2722 #endif
2723
2724         /* is this more then half a frame behind? */
2725         if (fps + 0.5f < (float)(FPS)) {
2726                 UI_ThemeColor(TH_REDALERT);
2727                 BLI_snprintf(printable, sizeof(printable), "fps: %.2f", fps);
2728         } 
2729         else {
2730                 UI_ThemeColor(TH_TEXT_HI);
2731                 BLI_snprintf(printable, sizeof(printable), "fps: %i", (int)(fps + 0.5f));
2732         }
2733         
2734         BLF_draw_default_ascii(22,  ar->winy - 17, 0.0f, printable, sizeof(printable));
2735 }
2736
2737 static void view3d_main_area_draw_objects(const bContext *C, ARegion *ar, const char **grid_unit);
2738
2739 static int view3d_main_area_draw_engine(const bContext *C, ARegion *ar, int draw_border)
2740 {
2741         Scene *scene = CTX_data_scene(C);
2742         View3D *v3d = CTX_wm_view3d(C);
2743         RegionView3D *rv3d = CTX_wm_region_view3d(C);
2744         RenderEngineType *type;
2745         GLint scissor[4];
2746
2747         /* create render engine */
2748         if (!rv3d->render_engine) {
2749                 type = RE_engines_find(scene->r.engine);
2750
2751                 if (!(type->view_update && type->view_draw))
2752                         return 0;
2753
2754                 rv3d->render_engine = RE_engine_create(type);
2755                 type->view_update(rv3d->render_engine, C);
2756         }
2757
2758         /* setup view matrices */
2759         view3d_main_area_setup_view(scene, v3d, ar, NULL, NULL);
2760
2761         /* background draw */
2762         ED_region_pixelspace(ar);
2763
2764         if (draw_border) {
2765                 /* for border draw, we only need to clear a subset of the 3d view */
2766                 rctf viewborder;
2767                 rcti cliprct;
2768
2769                 ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &viewborder, FALSE);
2770
2771                 cliprct.xmin = viewborder.xmin + scene->r.border.xmin * (viewborder.xmax - viewborder.xmin);
2772                 cliprct.ymin = viewborder.ymin + scene->r.border.ymin * (viewborder.ymax - viewborder.ymin);
2773                 cliprct.xmax = viewborder.xmin + scene->r.border.xmax * (viewborder.xmax - viewborder.xmin);
2774                 cliprct.ymax = viewborder.ymin + scene->r.border.ymax * (viewborder.ymax - viewborder.ymin);
2775
2776                 cliprct.xmin += ar->winrct.xmin;
2777                 cliprct.xmax += ar->winrct.xmin;
2778                 cliprct.ymin += ar->winrct.ymin;
2779                 cliprct.ymax += ar->winrct.ymin;
2780
2781                 cliprct.xmin = MAX2(cliprct.xmin, ar->winrct.xmin);
2782                 cliprct.ymin = MAX2(cliprct.ymin, ar->winrct.ymin);
2783                 cliprct.xmax = MIN2(cliprct.xmax, ar->winrct.xmax);
2784                 cliprct.ymax = MIN2(cliprct.ymax, ar->winrct.ymax);
2785
2786                 glGetIntegerv(GL_SCISSOR_BOX, scissor);
2787                 glScissor(cliprct.xmin, cliprct.ymin, cliprct.xmax - cliprct.xmin, cliprct.ymax - cliprct.ymin);
2788         }
2789
2790         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
2791         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2792
2793         if (v3d->flag & V3D_DISPBGPICS)
2794                 view3d_draw_bgpic(scene, ar, v3d, FALSE, TRUE);
2795         else
2796                 fdrawcheckerboard(0, 0, ar->winx, ar->winy);
2797
2798         if (draw_border) {
2799                 /* restore scissor as it was before */
2800                 glScissor(scissor[0], scissor[1], scissor[2], scissor[3]);
2801         }
2802
2803         /* render result draw */
2804         type = rv3d->render_engine->type;
2805         type->view_draw(rv3d->render_engine, C);
2806
2807         if (v3d->flag & V3D_DISPBGPICS)
2808                 view3d_draw_bgpic(scene, ar, v3d, TRUE, TRUE);
2809
2810         return 1;
2811 }
2812
2813 static void view3d_main_area_draw_engine_info(RegionView3D *rv3d, ARegion *ar)
2814 {
2815         if (!rv3d->render_engine || !rv3d->render_engine->text)
2816                 return;
2817
2818         ED_region_info_draw(ar, rv3d->render_engine->text, 1, 0.25);
2819 }
2820
2821 /* warning: this function has duplicate drawing in ED_view3d_draw_offscreen() */
2822 static void view3d_main_area_draw_objects(const bContext *C, ARegion *ar, const char **grid_unit)
2823 {
2824         Scene *scene = CTX_data_scene(C);
2825         View3D *v3d = CTX_wm_view3d(C);
2826         RegionView3D *rv3d = CTX_wm_region_view3d(C);
2827         Base *base;
2828         float backcol[3];
2829         unsigned int lay_used;
2830
2831         /* shadow buffers, before we setup matrices */
2832         if (draw_glsl_material(scene, NULL, v3d, v3d->drawtype))
2833                 gpu_update_lamps_shadows(scene, v3d);
2834         
2835         /* reset default OpenGL lights if needed (i.e. after preferences have been altered) */
2836         if (rv3d->rflag & RV3D_GPULIGHT_UPDATE) {
2837                 rv3d->rflag &= ~RV3D_GPULIGHT_UPDATE;
2838                 GPU_default_lights();
2839         }
2840
2841         /* clear background */
2842         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) && scene->world) {
2843                 if (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
2844                         linearrgb_to_srgb_v3_v3(backcol, &scene->world->horr);
2845                 else
2846                         copy_v3_v3(backcol, &scene->world->horr);
2847                 glClearColor(backcol[0], backcol[1], backcol[2], 0.0);
2848         }
2849         else
2850                 UI_ThemeClearColor(TH_BACK);
2851
2852         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2853         
2854         /* setup view matrices */
2855         view3d_main_area_setup_view(scene, v3d, ar, NULL, NULL);
2856
2857         ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
2858
2859         if (rv3d->rflag & RV3D_CLIPPING)
2860                 view3d_draw_clipping(rv3d);
2861         
2862         /* set zbuffer after we draw clipping region */
2863         if (v3d->drawtype > OB_WIRE) {
2864                 v3d->zbuf = TRUE;
2865                 glEnable(GL_DEPTH_TEST);
2866         }
2867         else
2868                 v3d->zbuf = FALSE;
2869
2870         /* enables anti-aliasing for 3D view drawing */
2871 #if 0
2872         if (!(U.gameflags & USER_DISABLE_AA))
2873                 glEnable(GL_MULTISAMPLE_ARB);
2874 #endif
2875
2876         // needs to be done always, gridview is adjusted in drawgrid() now
2877         rv3d->gridview = v3d->grid;
2878
2879         if ((rv3d->view == RV3D_VIEW_USER) || (rv3d->persp != RV3D_ORTHO)) {
2880                 if ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) {
2881                         drawfloor(scene, v3d, grid_unit);
2882                 }
2883                 if (rv3d->persp == RV3D_CAMOB) {
2884                         if (scene->world) {
2885                                 if (scene->world->mode & WO_STARS) {
2886                                         RE_make_stars(NULL, scene, star_stuff_init_func, star_stuff_vertex_func,
2887                                                       star_stuff_term_func);
2888                                 }
2889                         }
2890                 }
2891         }
2892         else {
2893                 if ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) {
2894                         ED_region_pixelspace(ar);
2895                         drawgrid(&scene->unit, ar, v3d, grid_unit);
2896                         /* XXX make function? replaces persp(1) */
2897                         glMatrixMode(GL_PROJECTION);
2898                         glLoadMatrixf(rv3d->winmat);
2899                         glMatrixMode(GL_MODELVIEW);
2900                         glLoadMatrixf(rv3d->viewmat);
2901                 }
2902         }
2903
2904         view3d_draw_bgpic_test(scene, ar, v3d, FALSE, TRUE);
2905
2906         if (rv3d->rflag & RV3D_CLIPPING)
2907                 ED_view3d_clipping_set(rv3d);
2908
2909         /* draw set first */
2910         if (scene->set) {
2911                 Scene *sce_iter;
2912                 for (SETLOOPER(scene->set, sce_iter, base)) {
2913                         
2914                         if (v3d->lay & base->lay) {
2915                                 
2916                                 UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
2917                                 draw_object(scene, ar, v3d, base, DRAW_CONSTCOLOR | DRAW_SCENESET);
2918                                 
2919                                 if (base->object->transflag & OB_DUPLI) {
2920                                         draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
2921                                 }
2922                         }
2923                 }
2924                 
2925                 /* Transp and X-ray afterdraw stuff for sets is done later */
2926         }
2927
2928         lay_used = 0;
2929
2930         /* then draw not selected and the duplis, but skip editmode object */
2931         for (base = scene->base.first; base; base = base->next) {
2932                 lay_used |= base->lay & ((1 << 20) - 1);
2933
2934                 if (v3d->lay & base->lay) {
2935                         
2936                         /* dupli drawing */
2937                         if (base->object->transflag & OB_DUPLI) {
2938                                 draw_dupli_objects(scene, ar, v3d, base);
2939                         }
2940                         if ((base->flag & SELECT) == 0) {
2941                                 if (base->object != scene->obedit)
2942                                         draw_object(scene, ar, v3d, base, 0);
2943                         }
2944                 }
2945         }
2946
2947         if (v3d->lay_used != lay_used) { /* happens when loading old files or loading with UI load */
2948                 /* find header and force tag redraw */
2949                 ScrArea *sa = CTX_wm_area(C);
2950                 ARegion *ar_header = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
2951                 ED_region_tag_redraw(ar_header); /* can be NULL */
2952                 v3d->lay_used = lay_used;
2953         }
2954
2955         /* draw selected and editmode */
2956         for (base = scene->base.first; base; base = base->next) {
2957                 if (v3d->lay & base->lay) {
2958                         if (base->object == scene->obedit || (base->flag & SELECT) )
2959                                 draw_object(scene, ar, v3d, base, 0);
2960                 }
2961         }
2962
2963 //      REEB_draw();
2964
2965         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) {
2966                 /* must be before xray draw which clears the depth buffer */
2967                 if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2968                 draw_gpencil_view3d(scene, v3d, ar, 1);
2969                 if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2970         }
2971
2972         /* Transp and X-ray afterdraw stuff */
2973         if (v3d->afterdraw_transp.first) view3d_draw_transp(scene, ar, v3d);
2974         if (v3d->afterdraw_xray.first) view3d_draw_xray(scene, ar, v3d, 1);         // clears zbuffer if it is used!
2975         if (v3d->afterdraw_xraytransp.first) view3d_draw_xraytransp(scene, ar, v3d, 1);
2976         
2977         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
2978
2979         if (rv3d->rflag & RV3D_CLIPPING)
2980                 ED_view3d_clipping_disable();
2981         
2982         /* important to do after clipping */
2983         view3d_draw_bgpic_test(scene, ar, v3d, TRUE, TRUE);
2984
2985         BIF_draw_manipulator(C);
2986
2987 #if 0
2988         /* Disable back anti-aliasing */
2989         if (!(U.gameflags & USER_DISABLE_AA))
2990                 glDisable(GL_MULTISAMPLE_ARB);
2991 #endif
2992
2993         if (v3d->zbuf) {
2994                 v3d->zbuf = FALSE;
2995                 glDisable(GL_DEPTH_TEST);
2996         }
2997
2998         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) {
2999                 BDR_drawSketch(C);
3000         }
3001
3002         if ((U.ndof_flag & NDOF_SHOW_GUIDE) && (rv3d->viewlock != RV3D_LOCKED) && (rv3d->persp != RV3D_CAMOB))
3003                 // TODO: draw something else (but not this) during fly mode
3004                 draw_rotation_guide(rv3d);
3005
3006 }
3007
3008 static void view3d_main_area_draw_info(const bContext *C, ARegion *ar, const char *grid_unit)
3009 {
3010         wmWindowManager *wm = CTX_wm_manager(C);
3011         Scene *scene = CTX_data_scene(C);
3012         View3D *v3d = CTX_wm_view3d(C);
3013         RegionView3D *rv3d = CTX_wm_region_view3d(C);
3014
3015         Object *ob;
3016
3017         if (rv3d->persp == RV3D_CAMOB)
3018                 drawviewborder(scene, ar, v3d);
3019
3020         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) {
3021                 /* draw grease-pencil stuff - needed to get paint-buffer shown too (since it's 2D) */
3022                 //      if (v3d->flag2 & V3D_DISPGP)
3023                 draw_gpencil_view3d(scene, v3d, ar, 0);
3024
3025                 drawcursor(scene, ar, v3d);
3026         }
3027         
3028         if (U.uiflag & USER_SHOW_ROTVIEWICON)
3029                 draw_view_axis(rv3d);
3030         else
3031                 draw_view_icon(rv3d);
3032         
3033         ob = OBACT;
3034         if (U.uiflag & USER_DRAWVIEWINFO)
3035                 draw_selected_name(scene, ob);
3036
3037         if (rv3d->render_engine) {
3038                 view3d_main_area_draw_engine_info(rv3d, ar);
3039                 return;
3040         }
3041
3042         if ((U.uiflag & USER_SHOW_FPS) && ED_screen_animation_playing(wm)) {
3043                 draw_viewport_fps(scene, ar);
3044         }
3045         else if (U.uiflag & USER_SHOW_VIEWPORTNAME) {
3046                 draw_viewport_name(ar, v3d);
3047         }
3048
3049         if (grid_unit) { /* draw below the viewport name */
3050                 char numstr[32] = "";
3051
3052                 UI_ThemeColor(TH_TEXT_HI);
3053                 if (v3d->grid != 1.0f) {
3054                         BLI_snprintf(numstr, sizeof(numstr), "%s x %.4g", grid_unit, v3d->grid);
3055                 }
3056
3057                 BLF_draw_default_ascii(22,  ar->winy - (USER_SHOW_VIEWPORTNAME ? 40 : 20), 0.0f,
3058                                        numstr[0] ? numstr : grid_unit, sizeof(numstr));
3059         }
3060 }
3061
3062 void view3d_main_area_draw(const bContext *C, ARegion *ar)
3063 {
3064         Scene *scene = CTX_data_scene(C);
3065         View3D *v3d = CTX_wm_view3d(C);
3066         RegionView3D *rv3d = CTX_wm_region_view3d(C);
3067         const char *grid_unit = NULL;
3068         int draw_border = (rv3d->persp == RV3D_CAMOB && (scene->r.mode & R_BORDER));
3069
3070         /* draw viewport using opengl */
3071         if (v3d->drawtype != OB_RENDER || draw_border) {
3072                 view3d_main_area_draw_objects(C, ar, &grid_unit);
3073                 ED_region_pixelspace(ar);
3074         }
3075
3076         /* draw viewport using external renderer */
3077         if (v3d->drawtype == OB_RENDER)
3078                 view3d_main_area_draw_engine(C, ar, draw_border);
3079         
3080         view3d_main_area_draw_info(C, ar, grid_unit);
3081
3082         v3d->flag |= V3D_INVALID_BACKBUF;
3083 }
3084