7c12816741d54acd3ef66cd0175091196e097648
[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                 
1150                 cpack(0);
1151                 x3 = x1 + scene->r.border.xmin * (x2 - x1);
1152                 y3 = y1 + scene->r.border.ymin * (y2 - y1);
1153                 x4 = x1 + scene->r.border.xmax * (x2 - x1);
1154                 y4 = y1 + scene->r.border.ymax * (y2 - y1);
1155                 
1156                 cpack(0x4040FF);
1157                 glRectf(x3,  y3,  x4,  y4); 
1158         }
1159
1160         /* safety border */
1161         if (ca) {
1162                 if (ca->dtx & CAM_DTX_CENTER) {
1163                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1164
1165                         x3 = x1 + 0.5f * (x2 - x1);
1166                         y3 = y1 + 0.5f * (y2 - y1);
1167
1168                         glBegin(GL_LINES);
1169                         glVertex2f(x1, y3);
1170                         glVertex2f(x2, y3);
1171
1172                         glVertex2f(x3, y1);
1173                         glVertex2f(x3, y2);
1174                         glEnd();
1175                 }
1176
1177                 if (ca->dtx & CAM_DTX_CENTER_DIAG) {
1178                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1179
1180                         glBegin(GL_LINES);
1181                         glVertex2f(x1, y1);
1182                         glVertex2f(x2, y2);
1183
1184                         glVertex2f(x1, y2);
1185                         glVertex2f(x2, y1);
1186                         glEnd();
1187                 }
1188
1189                 if (ca->dtx & CAM_DTX_THIRDS) {
1190                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1191                         drawviewborder_grid3(x1, x2, y1, y2, 1.0f / 3.0f);
1192                 }
1193
1194                 if (ca->dtx & CAM_DTX_GOLDEN) {
1195                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1196                         drawviewborder_grid3(x1, x2, y1, y2, 1.0f - (1.0f / 1.61803399f));
1197                 }
1198
1199                 if (ca->dtx & CAM_DTX_GOLDEN_TRI_A) {
1200                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1201                         drawviewborder_triangle(x1, x2, y1, y2, 0, 'A');
1202                 }
1203
1204                 if (ca->dtx & CAM_DTX_GOLDEN_TRI_B) {
1205                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1206                         drawviewborder_triangle(x1, x2, y1, y2, 0, 'B');
1207                 }
1208
1209                 if (ca->dtx & CAM_DTX_HARMONY_TRI_A) {
1210                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1211                         drawviewborder_triangle(x1, x2, y1, y2, 1, 'A');
1212                 }
1213
1214                 if (ca->dtx & CAM_DTX_HARMONY_TRI_B) {
1215                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1216                         drawviewborder_triangle(x1, x2, y1, y2, 1, 'B');
1217                 }
1218
1219                 if (ca->flag & CAM_SHOWTITLESAFE) {
1220                         fac = 0.1;
1221
1222                         a = fac * (x2 - x1);
1223                         x1 += a;
1224                         x2 -= a;
1225
1226                         a = fac * (y2 - y1);
1227                         y1 += a;
1228                         y2 -= a;
1229
1230                         UI_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
1231
1232                         uiSetRoundBox(UI_CNR_ALL);
1233                         uiDrawBox(GL_LINE_LOOP, x1, y1, x2, y2, 12.0);
1234                 }
1235                 if (ca && (ca->flag & CAM_SHOWSENSOR)) {
1236                         /* determine sensor fit, and get sensor x/y, for auto fit we
1237                          * assume and square sensor and only use sensor_x */
1238                         float sizex = scene->r.xsch * scene->r.xasp;
1239                         float sizey = scene->r.ysch * scene->r.yasp;
1240                         int sensor_fit = BKE_camera_sensor_fit(ca->sensor_fit, sizex, sizey);
1241                         float sensor_x = ca->sensor_x;
1242                         float sensor_y = (ca->sensor_fit == CAMERA_SENSOR_FIT_AUTO) ? ca->sensor_x : ca->sensor_y;
1243
1244                         /* determine sensor plane */
1245                         rctf rect;
1246
1247                         if (sensor_fit == CAMERA_SENSOR_FIT_HOR) {
1248                                 float sensor_scale = (x2i - x1i) / sensor_x;
1249                                 float sensor_height = sensor_scale * sensor_y;
1250
1251                                 rect.xmin = x1i;
1252                                 rect.xmax = x2i;
1253                                 rect.ymin = (y1i + y2i) * 0.5f - sensor_height * 0.5f;
1254                                 rect.ymax = rect.ymin + sensor_height;
1255                         }
1256                         else {
1257                                 float sensor_scale = (y2i - y1i) / sensor_y;
1258                                 float sensor_width = sensor_scale * sensor_x;
1259
1260                                 rect.xmin = (x1i + x2i) * 0.5f - sensor_width * 0.5f;
1261                                 rect.xmax = rect.xmin + sensor_width;
1262                                 rect.ymin = y1i;
1263                                 rect.ymax = y2i;
1264                         }
1265
1266                         /* draw */
1267                         UI_ThemeColorShade(TH_WIRE, 100);
1268                         uiDrawBox(GL_LINE_LOOP, rect.xmin, rect.ymin, rect.xmax, rect.ymax, 2.0f);
1269                 }
1270         }
1271
1272         setlinestyle(0);
1273         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1274
1275         /* camera name - draw in highlighted text color */
1276         if (ca && (ca->flag & CAM_SHOWNAME)) {
1277                 UI_ThemeColor(TH_TEXT_HI);
1278                 BLF_draw_default(x1i, y1i - 15, 0.0f, v3d->camera->id.name + 2, sizeof(v3d->camera->id.name) - 2);
1279                 UI_ThemeColor(TH_WIRE);
1280         }
1281 }
1282
1283 /* *********************** backdraw for selection *************** */
1284
1285 static void backdrawview3d(Scene *scene, ARegion *ar, View3D *v3d)
1286 {
1287         RegionView3D *rv3d = ar->regiondata;
1288         struct Base *base = scene->basact;
1289         int multisample_enabled;
1290         rcti winrct;
1291
1292         BLI_assert(ar->regiontype == RGN_TYPE_WINDOW);
1293
1294         if (base && (base->object->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT) ||
1295                      paint_facesel_test(base->object)))
1296         {
1297                 /* do nothing */
1298         }
1299         else if ((base && (base->object->mode & OB_MODE_TEXTURE_PAINT)) &&
1300                  scene->toolsettings && (scene->toolsettings->imapaint.flag & IMAGEPAINT_PROJECT_DISABLE))
1301         {
1302                 /* do nothing */
1303         }
1304         else if ((base && (base->object->mode & OB_MODE_PARTICLE_EDIT)) &&
1305                  v3d->drawtype > OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT))
1306         {
1307                 /* do nothing */
1308         }
1309         else if (scene->obedit && v3d->drawtype > OB_WIRE &&
1310                  (v3d->flag & V3D_ZBUF_SELECT))
1311         {
1312                 /* do nothing */
1313         }
1314         else {
1315                 v3d->flag &= ~V3D_INVALID_BACKBUF;
1316                 return;
1317         }
1318
1319         if (!(v3d->flag & V3D_INVALID_BACKBUF) ) return;
1320
1321 //      if (test) {
1322 //              if (qtest()) {
1323 //                      addafterqueue(ar->win, BACKBUFDRAW, 1);
1324 //                      return;
1325 //              }
1326 //      }
1327
1328         if (v3d->drawtype > OB_WIRE) v3d->zbuf = TRUE;
1329         
1330         /* dithering and AA break color coding, so disable */
1331         glDisable(GL_DITHER);
1332
1333         multisample_enabled = glIsEnabled(GL_MULTISAMPLE_ARB);
1334         if (multisample_enabled)
1335                 glDisable(GL_MULTISAMPLE_ARB);
1336
1337         region_scissor_winrct(ar, &winrct);
1338         glScissor(winrct.xmin, winrct.ymin, winrct.xmax - winrct.xmin, winrct.ymax - winrct.ymin);
1339
1340         glClearColor(0.0, 0.0, 0.0, 0.0); 
1341         if (v3d->zbuf) {
1342                 glEnable(GL_DEPTH_TEST);
1343                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1344         }
1345         else {
1346                 glClear(GL_COLOR_BUFFER_BIT);
1347                 glDisable(GL_DEPTH_TEST);
1348         }
1349         
1350         if (rv3d->rflag & RV3D_CLIPPING)
1351                 ED_view3d_clipping_set(rv3d);
1352         
1353         G.f |= G_BACKBUFSEL;
1354         
1355         if (base && (base->lay & v3d->lay))
1356                 draw_object_backbufsel(scene, v3d, rv3d, base->object);
1357
1358         v3d->flag &= ~V3D_INVALID_BACKBUF;
1359         ar->swap = 0; /* mark invalid backbuf for wm draw */
1360
1361         G.f &= ~G_BACKBUFSEL;
1362         v3d->zbuf = FALSE;
1363         glDisable(GL_DEPTH_TEST);
1364         glEnable(GL_DITHER);
1365         if (multisample_enabled)
1366                 glEnable(GL_MULTISAMPLE_ARB);
1367
1368         if (rv3d->rflag & RV3D_CLIPPING)
1369                 ED_view3d_clipping_disable();
1370
1371         /* it is important to end a view in a transform compatible with buttons */
1372 //      persp(PERSP_WIN);  // set ortho
1373
1374 }
1375
1376 void view3d_validate_backbuf(ViewContext *vc)
1377 {
1378         if (vc->v3d->flag & V3D_INVALID_BACKBUF)
1379                 backdrawview3d(vc->scene, vc->ar, vc->v3d);
1380 }
1381
1382 /* samples a single pixel (copied from vpaint) */
1383 unsigned int view3d_sample_backbuf(ViewContext *vc, int x, int y)
1384 {
1385         unsigned int col;
1386         
1387         if (x >= vc->ar->winx || y >= vc->ar->winy) return 0;
1388         x += vc->ar->winrct.xmin;
1389         y += vc->ar->winrct.ymin;
1390         
1391         view3d_validate_backbuf(vc);
1392
1393         glReadPixels(x,  y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,  &col);
1394         glReadBuffer(GL_BACK);  
1395         
1396         if (ENDIAN_ORDER == B_ENDIAN) SWITCH_INT(col);
1397         
1398         return WM_framebuffer_to_index(col);
1399 }
1400
1401 /* reads full rect, converts indices */
1402 ImBuf *view3d_read_backbuf(ViewContext *vc, short xmin, short ymin, short xmax, short ymax)
1403 {
1404         unsigned int *dr, *rd;
1405         struct ImBuf *ibuf, *ibuf1;
1406         int a;
1407         short xminc, yminc, xmaxc, ymaxc, xs, ys;
1408         
1409         /* clip */
1410         if (xmin < 0) xminc = 0; else xminc = xmin;
1411         if (xmax >= vc->ar->winx) xmaxc = vc->ar->winx - 1; else xmaxc = xmax;
1412         if (xminc > xmaxc) return NULL;
1413
1414         if (ymin < 0) yminc = 0; else yminc = ymin;
1415         if (ymax >= vc->ar->winy) ymaxc = vc->ar->winy - 1; else ymaxc = ymax;
1416         if (yminc > ymaxc) return NULL;
1417         
1418         ibuf = IMB_allocImBuf((xmaxc - xminc + 1), (ymaxc - yminc + 1), 32, IB_rect);
1419
1420         view3d_validate_backbuf(vc); 
1421         
1422         glReadPixels(vc->ar->winrct.xmin + xminc,
1423                      vc->ar->winrct.ymin + yminc,
1424                      (xmaxc - xminc + 1),
1425                      (ymaxc - yminc + 1),
1426                      GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
1427
1428         glReadBuffer(GL_BACK);  
1429
1430         if (ENDIAN_ORDER == B_ENDIAN) IMB_convert_rgba_to_abgr(ibuf);
1431
1432         a = (xmaxc - xminc + 1) * (ymaxc - yminc + 1);
1433         dr = ibuf->rect;
1434         while (a--) {
1435                 if (*dr) *dr = WM_framebuffer_to_index(*dr);
1436                 dr++;
1437         }
1438         
1439         /* put clipped result back, if needed */
1440         if (xminc == xmin && xmaxc == xmax && yminc == ymin && ymaxc == ymax)
1441                 return ibuf;
1442         
1443         ibuf1 = IMB_allocImBuf( (xmax - xmin + 1), (ymax - ymin + 1), 32, IB_rect);
1444         rd = ibuf->rect;
1445         dr = ibuf1->rect;
1446
1447         for (ys = ymin; ys <= ymax; ys++) {
1448                 for (xs = xmin; xs <= xmax; xs++, dr++) {
1449                         if (xs >= xminc && xs <= xmaxc && ys >= yminc && ys <= ymaxc) {
1450                                 *dr = *rd;
1451                                 rd++;
1452                         }
1453                 }
1454         }
1455         IMB_freeImBuf(ibuf);
1456         return ibuf1;
1457 }
1458
1459 /* smart function to sample a rect spiralling outside, nice for backbuf selection */
1460 unsigned int view3d_sample_backbuf_rect(ViewContext *vc, const int mval[2], int size,
1461                                         unsigned int min, unsigned int max, int *dist, short strict,
1462                                         void *handle, unsigned int (*indextest)(void *handle, unsigned int index))
1463 {
1464         struct ImBuf *buf;
1465         unsigned int *bufmin, *bufmax, *tbuf;
1466         int minx, miny;
1467         int a, b, rc, nr, amount, dirvec[4][2];
1468         int distance = 0;
1469         unsigned int index = 0;
1470         short indexok = 0;      
1471
1472         amount = (size - 1) / 2;
1473
1474         minx = mval[0] - (amount + 1);
1475         miny = mval[1] - (amount + 1);
1476         buf = view3d_read_backbuf(vc, minx, miny, minx + size - 1, miny + size - 1);
1477         if (!buf) return 0;
1478
1479         rc = 0;
1480         
1481         dirvec[0][0] = 1; dirvec[0][1] = 0;
1482         dirvec[1][0] = 0; dirvec[1][1] = -size;
1483         dirvec[2][0] = -1; dirvec[2][1] = 0;
1484         dirvec[3][0] = 0; dirvec[3][1] = size;
1485         
1486         bufmin = buf->rect;
1487         tbuf = buf->rect;
1488         bufmax = buf->rect + size * size;
1489         tbuf += amount * size + amount;
1490         
1491         for (nr = 1; nr <= size; nr++) {
1492                 
1493                 for (a = 0; a < 2; a++) {
1494                         for (b = 0; b < nr; b++, distance++) {
1495                                 if (*tbuf && *tbuf >= min && *tbuf < max) { //we got a hit
1496                                         if (strict) {
1497                                                 indexok =  indextest(handle, *tbuf - min + 1);
1498                                                 if (indexok) {
1499                                                         *dist = (short) sqrt( (float)distance);
1500                                                         index = *tbuf - min + 1;
1501                                                         goto exit; 
1502                                                 }                                               
1503                                         }
1504                                         else {
1505                                                 *dist = (short) sqrt( (float)distance); // XXX, this distance is wrong -
1506                                                 index = *tbuf - min + 1; // messy yah, but indices start at 1
1507                                                 goto exit;
1508                                         }                       
1509                                 }
1510                                 
1511                                 tbuf += (dirvec[rc][0] + dirvec[rc][1]);
1512                                 
1513                                 if (tbuf < bufmin || tbuf >= bufmax) {
1514                                         goto exit;
1515                                 }
1516                         }
1517                         rc++;
1518                         rc &= 3;
1519                 }
1520         }
1521
1522 exit:
1523         IMB_freeImBuf(buf);
1524         return index;
1525 }
1526
1527
1528 /* ************************************************************* */
1529
1530 static void view3d_draw_bgpic(Scene *scene, ARegion *ar, View3D *v3d,
1531                               const short do_forground, const short do_camera_frame)
1532 {
1533         RegionView3D *rv3d = ar->regiondata;
1534         BGpic *bgpic;
1535         Image *ima;
1536         MovieClip *clip;
1537         ImBuf *ibuf = NULL, *freeibuf;
1538         float vec[4], fac, asp, zoomx, zoomy;
1539         float x1, y1, x2, y2, cx, cy;
1540         int fg_flag = do_forground ? V3D_BGPIC_FOREGROUND : 0;
1541
1542         for (bgpic = v3d->bgpicbase.first; bgpic; bgpic = bgpic->next) {
1543
1544                 if ((bgpic->flag & V3D_BGPIC_FOREGROUND) != fg_flag)
1545                         continue;
1546
1547                 if ((bgpic->view == 0) || /* zero for any */
1548                     (bgpic->view & (1 << rv3d->view)) || /* check agaist flags */
1549                     (rv3d->persp == RV3D_CAMOB && bgpic->view == (1 << RV3D_VIEW_CAMERA)))
1550                 {
1551                         /* disable individual images */
1552                         if ((bgpic->flag & V3D_BGPIC_DISABLED))
1553                                 continue;
1554
1555                         freeibuf = NULL;
1556                         if (bgpic->source == V3D_BGPIC_IMAGE) {
1557                                 ima = bgpic->ima;
1558                                 if (ima == NULL)
1559                                         continue;
1560                                 BKE_image_user_frame_calc(&bgpic->iuser, CFRA, 0);
1561                                 ibuf = BKE_image_get_ibuf(ima, &bgpic->iuser);
1562                         }
1563                         else if (bgpic->source == V3D_BGPIC_MOVIE) {
1564                                 clip = NULL;
1565
1566                                 if (bgpic->flag & V3D_BGPIC_CAMERACLIP) {
1567                                         if (scene->camera)
1568                                                 clip = BKE_object_movieclip_get(scene, scene->camera, 1);
1569                                 }
1570                                 else clip = bgpic->clip;
1571
1572                                 if (clip == NULL)
1573                                         continue;
1574
1575                                 BKE_movieclip_user_set_frame(&bgpic->cuser, CFRA);
1576                                 ibuf = BKE_movieclip_get_ibuf(clip, &bgpic->cuser);
1577
1578                                 /* working with ibuf from image and clip has got different workflow now.
1579                                  * ibuf acquired from clip is referenced by cache system and should
1580                                  * be dereferenced after usage. */
1581                                 freeibuf = ibuf;
1582                         }
1583
1584                         if (ibuf == NULL)
1585                                 continue;
1586
1587                         if ((ibuf->rect == NULL && ibuf->rect_float == NULL) || ibuf->channels != 4) { /* invalid image format */
1588                                 if (freeibuf)
1589                                         IMB_freeImBuf(freeibuf);
1590
1591                                 continue;
1592                         }
1593
1594                         if (ibuf->rect == NULL)
1595                                 IMB_rect_from_float(ibuf);
1596
1597                         if (rv3d->persp == RV3D_CAMOB) {
1598
1599                                 if (do_camera_frame) {
1600                                         rctf vb;
1601                                         ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &vb, FALSE);
1602                                         x1 = vb.xmin;
1603                                         y1 = vb.ymin;
1604                                         x2 = vb.xmax;
1605                                         y2 = vb.ymax;
1606                                 }
1607                                 else {
1608                                         x1 = ar->winrct.xmin;
1609                                         y1 = ar->winrct.ymin;
1610                                         x2 = ar->winrct.xmax;
1611                                         y2 = ar->winrct.ymax;
1612                                 }
1613                         }
1614                         else {
1615                                 float sco[2];
1616                                 const float mval_f[2] = {1.0f, 0.0f};
1617
1618                                 /* calc window coord */
1619                                 initgrabz(rv3d, 0.0, 0.0, 0.0);
1620                                 ED_view3d_win_to_delta(ar, mval_f, vec);
1621                                 fac = maxf(fabsf(vec[0]), maxf(fabsf(vec[1]), fabsf(vec[2]))); /* largest abs axis */
1622                                 fac = 1.0f / fac;
1623
1624                                 asp = ( (float)ibuf->y) / (float)ibuf->x;
1625
1626                                 zero_v3(vec);
1627                                 ED_view3d_project_float_v2(ar, vec, sco, rv3d->persmat);
1628                                 cx = sco[0];
1629                                 cy = sco[1];
1630
1631                                 x1 =  cx + fac * (bgpic->xof - bgpic->size);
1632                                 y1 =  cy + asp * fac * (bgpic->yof - bgpic->size);
1633                                 x2 =  cx + fac * (bgpic->xof + bgpic->size);
1634                                 y2 =  cy + asp * fac * (bgpic->yof + bgpic->size);
1635                         }
1636
1637                         /* complete clip? */
1638
1639                         if (x2 < 0 || y2 < 0 || x1 > ar->winx || y1 > ar->winy) {
1640                                 if (freeibuf)
1641                                         IMB_freeImBuf(freeibuf);
1642
1643                                 continue;
1644                         }
1645
1646                         zoomx = (x2 - x1) / ibuf->x;
1647                         zoomy = (y2 - y1) / ibuf->y;
1648
1649                         /* for some reason; zoomlevels down refuses to use GL_ALPHA_SCALE */
1650                         if (zoomx < 1.0f || zoomy < 1.0f) {
1651                                 float tzoom = MIN2(zoomx, zoomy);
1652                                 int mip = 0;
1653
1654                                 if ((ibuf->userflags & IB_MIPMAP_INVALID) != 0) {
1655                                         IMB_remakemipmap(ibuf, 0);
1656                                         ibuf->userflags &= ~IB_MIPMAP_INVALID;
1657                                 }
1658                                 else if (ibuf->mipmap[0] == NULL)
1659                                         IMB_makemipmap(ibuf, 0);
1660
1661                                 while (tzoom < 1.0f && mip < 8 && ibuf->mipmap[mip]) {
1662                                         tzoom *= 2.0f;
1663                                         zoomx *= 2.0f;
1664                                         zoomy *= 2.0f;
1665                                         mip++;
1666                                 }
1667                                 if (mip > 0)
1668                                         ibuf = ibuf->mipmap[mip - 1];
1669                         }
1670
1671                         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
1672                         glDepthMask(0);
1673
1674                         glEnable(GL_BLEND);
1675                         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA);
1676
1677                         glMatrixMode(GL_PROJECTION);
1678                         glPushMatrix();
1679                         glMatrixMode(GL_MODELVIEW);
1680                         glPushMatrix();
1681                         ED_region_pixelspace(ar);
1682
1683                         glPixelZoom(zoomx, zoomy);
1684                         glColor4f(1.0f, 1.0f, 1.0f, 1.0f - bgpic->blend);
1685                         glaDrawPixelsTex(x1, y1, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
1686
1687                         glPixelZoom(1.0, 1.0);
1688                         glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
1689
1690                         glMatrixMode(GL_PROJECTION);
1691                         glPopMatrix();
1692                         glMatrixMode(GL_MODELVIEW);
1693                         glPopMatrix();
1694
1695                         glDisable(GL_BLEND);
1696
1697                         glDepthMask(1);
1698                         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
1699
1700                         if (freeibuf)
1701                                 IMB_freeImBuf(freeibuf);
1702                 }
1703         }
1704 }
1705
1706 static void view3d_draw_bgpic_test(Scene *scene, ARegion *ar, View3D *v3d,
1707                                    const short do_forground, const short do_camera_frame)
1708 {
1709         RegionView3D *rv3d = ar->regiondata;
1710
1711         if ((v3d->flag & V3D_DISPBGPICS) == 0)
1712                 return;
1713
1714         if (v3d->flag2 & V3D_RENDER_OVERRIDE)
1715                 return;
1716
1717         if ((rv3d->view == RV3D_VIEW_USER) || (rv3d->persp != RV3D_ORTHO)) {
1718                 if (rv3d->persp == RV3D_CAMOB) {
1719                         view3d_draw_bgpic(scene, ar, v3d, do_forground, do_camera_frame);
1720                 }
1721         }
1722         else {
1723                 view3d_draw_bgpic(scene, ar, v3d, do_forground, do_camera_frame);
1724         }
1725 }
1726
1727 /* ****************** View3d afterdraw *************** */
1728
1729 typedef struct View3DAfter {
1730         struct View3DAfter *next, *prev;
1731         struct Base *base;
1732         int flag;
1733 } View3DAfter;
1734
1735 /* temp storage of Objects that need to be drawn as last */
1736 void add_view3d_after(ListBase *lb, Base *base, int flag)
1737 {
1738         View3DAfter *v3da = MEM_callocN(sizeof(View3DAfter), "View 3d after");
1739         BLI_assert((base->flag & OB_FROMDUPLI) == 0);
1740         BLI_addtail(lb, v3da);
1741         v3da->base = base;
1742         v3da->flag = flag;
1743 }
1744
1745 /* disables write in zbuffer and draws it over */
1746 static void view3d_draw_transp(Scene *scene, ARegion *ar, View3D *v3d)
1747 {
1748         View3DAfter *v3da, *next;
1749         
1750         glDepthMask(0);
1751         v3d->transp = TRUE;
1752         
1753         for (v3da = v3d->afterdraw_transp.first; v3da; v3da = next) {
1754                 next = v3da->next;
1755                 draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1756                 BLI_remlink(&v3d->afterdraw_transp, v3da);
1757                 MEM_freeN(v3da);
1758         }
1759         v3d->transp = FALSE;
1760         
1761         glDepthMask(1);
1762         
1763 }
1764
1765 /* clears zbuffer and draws it over */
1766 static void view3d_draw_xray(Scene *scene, ARegion *ar, View3D *v3d, int clear)
1767 {
1768         View3DAfter *v3da, *next;
1769
1770         if (clear && v3d->zbuf)
1771                 glClear(GL_DEPTH_BUFFER_BIT);
1772
1773         v3d->xray = TRUE;
1774         for (v3da = v3d->afterdraw_xray.first; v3da; v3da = next) {
1775                 next = v3da->next;
1776                 draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1777                 BLI_remlink(&v3d->afterdraw_xray, v3da);
1778                 MEM_freeN(v3da);
1779         }
1780         v3d->xray = FALSE;
1781 }
1782
1783
1784 /* clears zbuffer and draws it over */
1785 static void view3d_draw_xraytransp(Scene *scene, ARegion *ar, View3D *v3d, int clear)
1786 {
1787         View3DAfter *v3da, *next;
1788
1789         if (clear && v3d->zbuf)
1790                 glClear(GL_DEPTH_BUFFER_BIT);
1791
1792         v3d->xray = TRUE;
1793         v3d->transp = TRUE;
1794         
1795         for (v3da = v3d->afterdraw_xraytransp.first; v3da; v3da = next) {
1796                 next = v3da->next;
1797                 draw_object(scene, ar, v3d, v3da->base, v3da->flag);
1798                 BLI_remlink(&v3d->afterdraw_xraytransp, v3da);
1799                 MEM_freeN(v3da);
1800         }
1801
1802         v3d->transp = FALSE;
1803         v3d->xray = FALSE;
1804
1805 }
1806
1807 /* *********************** */
1808
1809 /*
1810  * In most cases call draw_dupli_objects,
1811  * draw_dupli_objects_color was added because when drawing set dupli's
1812  * we need to force the color
1813  */
1814
1815 #if 0
1816 int dupli_ob_sort(void *arg1, void *arg2)
1817 {
1818         void *p1 = ((DupliObject *)arg1)->ob;
1819         void *p2 = ((DupliObject *)arg2)->ob;
1820         int val = 0;
1821         if (p1 < p2) val = -1;
1822         else if (p1 > p2) val = 1;
1823         return val;
1824 }
1825 #endif
1826
1827
1828 static DupliObject *dupli_step(DupliObject *dob)
1829 {
1830         while (dob && dob->no_draw)
1831                 dob = dob->next;
1832         return dob;
1833 }
1834
1835 static void draw_dupli_objects_color(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int color)
1836 {
1837         RegionView3D *rv3d = ar->regiondata;
1838         ListBase *lb;
1839         DupliObject *dob_prev = NULL, *dob, *dob_next = NULL;
1840         Base tbase = {NULL};
1841         BoundBox bb, *bb_tmp; /* use a copy because draw_object, calls clear_mesh_caches */
1842         GLuint displist = 0;
1843         short transflag, use_displist = -1;  /* -1 is initialize */
1844         char dt, dtx;
1845         
1846         if (base->object->restrictflag & OB_RESTRICT_VIEW) return;
1847         
1848         tbase.flag = OB_FROMDUPLI | base->flag;
1849         lb = object_duplilist(scene, base->object);
1850         // BLI_sortlist(lb, dupli_ob_sort); // might be nice to have if we have a dupli list with mixed objects.
1851
1852         dob = dupli_step(lb->first);
1853         if (dob) dob_next = dupli_step(dob->next);
1854
1855         for (; dob; dob_prev = dob, dob = dob_next, dob_next = dob_next ? dupli_step(dob_next->next) : NULL) {
1856                 tbase.object = dob->ob;
1857
1858                 /* extra service: draw the duplicator in drawtype of parent */
1859                 /* MIN2 for the drawtype to allow bounding box objects in groups for lods */
1860                 dt = tbase.object->dt;   tbase.object->dt = MIN2(tbase.object->dt, base->object->dt);
1861                 dtx = tbase.object->dtx; tbase.object->dtx = base->object->dtx;
1862
1863                 /* negative scale flag has to propagate */
1864                 transflag = tbase.object->transflag;
1865                 if (base->object->transflag & OB_NEG_SCALE)
1866                         tbase.object->transflag ^= OB_NEG_SCALE;
1867
1868                 UI_ThemeColorBlend(color, TH_BACK, 0.5);
1869
1870                 /* generate displist, test for new object */
1871                 if (dob_prev && dob_prev->ob != dob->ob) {
1872                         if (use_displist == TRUE)
1873                                 glDeleteLists(displist, 1);
1874
1875                         use_displist = -1;
1876                 }
1877
1878                 /* generate displist */
1879                 if (use_displist == -1) {
1880
1881                         /* note, since this was added, its checked (dob->type == OB_DUPLIGROUP)
1882                          * however this is very slow, it was probably needed for the NLA
1883                          * offset feature (used in group-duplicate.blend but no longer works in 2.5)
1884                          * so for now it should be ok to - campbell */
1885
1886                         if ( /* if this is the last no need  to make a displist */
1887                             (dob_next == NULL || dob_next->ob != dob->ob) ||
1888                             /* lamp drawing messes with matrices, could be handled smarter... but this works */
1889                             (dob->ob->type == OB_LAMP) ||
1890                             (dob->type == OB_DUPLIGROUP && dob->animated) ||
1891                             !(bb_tmp = BKE_object_boundbox_get(dob->ob)))
1892                         {
1893                                 // printf("draw_dupli_objects_color: skipping displist for %s\n", dob->ob->id.name+2);
1894                                 use_displist = FALSE;
1895                         }
1896                         else {
1897                                 // printf("draw_dupli_objects_color: using displist for %s\n", dob->ob->id.name+2);
1898                                 bb = *bb_tmp; /* must make a copy  */
1899
1900                                 /* disable boundbox check for list creation */
1901                                 BKE_object_boundbox_flag(dob->ob, OB_BB_DISABLED, 1);
1902                                 /* need this for next part of code */
1903                                 unit_m4(dob->ob->obmat);    /* obmat gets restored */
1904
1905                                 displist = glGenLists(1);
1906                                 glNewList(displist, GL_COMPILE);
1907                                 draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
1908                                 glEndList();
1909
1910                                 use_displist = TRUE;
1911                                 BKE_object_boundbox_flag(dob->ob, OB_BB_DISABLED, 0);
1912                         }
1913                 }
1914                 if (use_displist) {
1915                         glMultMatrixf(dob->mat);
1916                         if (ED_view3d_boundbox_clip(rv3d, dob->mat, &bb))
1917                                 glCallList(displist);
1918                         glLoadMatrixf(rv3d->viewmat);
1919                 }
1920                 else {
1921                         copy_m4_m4(dob->ob->obmat, dob->mat);
1922                         draw_object(scene, ar, v3d, &tbase, DRAW_CONSTCOLOR);
1923                 }
1924
1925                 tbase.object->dt = dt;
1926                 tbase.object->dtx = dtx;
1927                 tbase.object->transflag = transflag;
1928         }
1929         
1930         /* Transp afterdraw disabled, afterdraw only stores base pointers, and duplis can be same obj */
1931         
1932         free_object_duplilist(lb);  /* does restore */
1933         
1934         if (use_displist)
1935                 glDeleteLists(displist, 1);
1936 }
1937
1938 static void draw_dupli_objects(Scene *scene, ARegion *ar, View3D *v3d, Base *base)
1939 {
1940         /* define the color here so draw_dupli_objects_color can be called
1941          * from the set loop */
1942         
1943         int color = (base->flag & SELECT) ? TH_SELECT : TH_WIRE;
1944         /* debug */
1945         if (base->object->dup_group && base->object->dup_group->id.us < 1)
1946                 color = TH_REDALERT;
1947         
1948         draw_dupli_objects_color(scene, ar, v3d, base, color);
1949 }
1950
1951 void view3d_update_depths_rect(ARegion *ar, ViewDepths *d, rcti *rect)
1952 {
1953         int x, y, w, h; 
1954         rcti r;
1955         /* clamp rect by area */
1956
1957         r.xmin = 0;
1958         r.xmax = ar->winx - 1;
1959         r.ymin = 0;
1960         r.ymax = ar->winy - 1;
1961
1962         /* Constrain rect to depth bounds */
1963         BLI_isect_rcti(&r, rect, rect);
1964
1965         /* assign values to compare with the ViewDepths */
1966         x = rect->xmin;
1967         y = rect->ymin;
1968
1969         w = rect->xmax - rect->xmin;
1970         h = rect->ymax - rect->ymin;
1971
1972         if (w <= 0 || h <= 0) {
1973                 if (d->depths)
1974                         MEM_freeN(d->depths);
1975                 d->depths = NULL;
1976
1977                 d->damaged = FALSE;
1978         }
1979         else if (d->w != w ||
1980                  d->h != h ||
1981                  d->x != x ||
1982                  d->y != y ||
1983                  d->depths == NULL
1984                  )
1985         {
1986                 d->x = x;
1987                 d->y = y;
1988                 d->w = w;
1989                 d->h = h;
1990
1991                 if (d->depths)
1992                         MEM_freeN(d->depths);
1993
1994                 d->depths = MEM_mallocN(sizeof(float) * d->w * d->h, "View depths Subset");
1995                 
1996                 d->damaged = TRUE;
1997         }
1998
1999         if (d->damaged) {
2000                 glReadPixels(ar->winrct.xmin + d->x, ar->winrct.ymin + d->y, d->w, d->h, GL_DEPTH_COMPONENT, GL_FLOAT, d->depths);
2001                 glGetDoublev(GL_DEPTH_RANGE, d->depth_range);
2002                 d->damaged = FALSE;
2003         }
2004 }
2005
2006 /* note, with nouveau drivers the glReadPixels() is very slow. [#24339] */
2007 void ED_view3d_depth_update(ARegion *ar)
2008 {
2009         RegionView3D *rv3d = ar->regiondata;
2010         
2011         /* Create storage for, and, if necessary, copy depth buffer */
2012         if (!rv3d->depths) rv3d->depths = MEM_callocN(sizeof(ViewDepths), "ViewDepths");
2013         if (rv3d->depths) {
2014                 ViewDepths *d = rv3d->depths;
2015                 if (d->w != ar->winx ||
2016                     d->h != ar->winy ||
2017                     !d->depths)
2018                 {
2019                         d->w = ar->winx;
2020                         d->h = ar->winy;
2021                         if (d->depths)
2022                                 MEM_freeN(d->depths);
2023                         d->depths = MEM_mallocN(sizeof(float) * d->w * d->h, "View depths");
2024                         d->damaged = 1;
2025                 }
2026                 
2027                 if (d->damaged) {
2028                         glReadPixels(ar->winrct.xmin, ar->winrct.ymin, d->w, d->h,
2029                                      GL_DEPTH_COMPONENT, GL_FLOAT, d->depths);
2030                         
2031                         glGetDoublev(GL_DEPTH_RANGE, d->depth_range);
2032                         
2033                         d->damaged = 0;
2034                 }
2035         }
2036 }
2037
2038 /* utility function to find the closest Z value, use for autodepth */
2039 float view3d_depth_near(ViewDepths *d)
2040 {
2041         /* convert to float for comparisons */
2042         const float near = (float)d->depth_range[0];
2043         const float far_real = (float)d->depth_range[1];
2044         float far = far_real;
2045
2046         const float *depths = d->depths;
2047         float depth = FLT_MAX;
2048         int i = (int)d->w * (int)d->h; /* cast to avoid short overflow */
2049
2050         /* far is both the starting 'far' value
2051          * and the closest value found. */      
2052         while (i--) {
2053                 depth = *depths++;
2054                 if ((depth < far) && (depth > near)) {
2055                         far = depth;
2056                 }
2057         }
2058
2059         return far == far_real ? FLT_MAX : far;
2060 }
2061
2062 void draw_depth_gpencil(Scene *scene, ARegion *ar, View3D *v3d)
2063 {
2064         short zbuf = v3d->zbuf;
2065         RegionView3D *rv3d = ar->regiondata;
2066
2067         setwinmatrixview3d(ar, v3d, NULL);  /* 0= no pick rect */
2068         setviewmatrixview3d(scene, v3d, rv3d);  /* note: calls BKE_object_where_is_calc for camera... */
2069
2070         mult_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
2071         invert_m4_m4(rv3d->persinv, rv3d->persmat);
2072         invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
2073
2074         glClear(GL_DEPTH_BUFFER_BIT);
2075
2076         glLoadMatrixf(rv3d->viewmat);
2077
2078         v3d->zbuf = TRUE;
2079         glEnable(GL_DEPTH_TEST);
2080
2081         draw_gpencil_view3d(scene, v3d, ar, 1);
2082         
2083         v3d->zbuf = zbuf;
2084
2085 }
2086
2087 void draw_depth(Scene *scene, ARegion *ar, View3D *v3d, int (*func)(void *))
2088 {
2089         RegionView3D *rv3d = ar->regiondata;
2090         Base *base;
2091         short zbuf = v3d->zbuf;
2092         short flag = v3d->flag;
2093         float glalphaclip = U.glalphaclip;
2094         int obcenter_dia = U.obcenter_dia;
2095         /* temp set drawtype to solid */
2096         
2097         /* Setting these temporarily is not nice */
2098         v3d->flag &= ~V3D_SELECT_OUTLINE;
2099         U.glalphaclip = 0.5; /* not that nice but means we wont zoom into billboards */
2100         U.obcenter_dia = 0;
2101         
2102         setwinmatrixview3d(ar, v3d, NULL);  /* 0= no pick rect */
2103         setviewmatrixview3d(scene, v3d, rv3d);  /* note: calls BKE_object_where_is_calc for camera... */
2104         
2105         mult_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
2106         invert_m4_m4(rv3d->persinv, rv3d->persmat);
2107         invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
2108         
2109         glClear(GL_DEPTH_BUFFER_BIT);
2110         
2111         glLoadMatrixf(rv3d->viewmat);
2112 //      persp(PERSP_STORE);  // store correct view for persp(PERSP_VIEW) calls
2113         
2114         if (rv3d->rflag & RV3D_CLIPPING) {
2115                 ED_view3d_clipping_set(rv3d);
2116         }
2117         
2118         v3d->zbuf = TRUE;
2119         glEnable(GL_DEPTH_TEST);
2120         
2121         /* draw set first */
2122         if (scene->set) {
2123                 Scene *sce_iter;
2124                 for (SETLOOPER(scene->set, sce_iter, base)) {
2125                         if (v3d->lay & base->lay) {
2126                                 if (func == NULL || func(base)) {
2127                                         draw_object(scene, ar, v3d, base, 0);
2128                                         if (base->object->transflag & OB_DUPLI) {
2129                                                 draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
2130                                         }
2131                                 }
2132                         }
2133                 }
2134         }
2135         
2136         for (base = scene->base.first; base; base = base->next) {
2137                 if (v3d->lay & base->lay) {
2138                         if (func == NULL || func(base)) {
2139                                 /* dupli drawing */
2140                                 if (base->object->transflag & OB_DUPLI) {
2141                                         draw_dupli_objects(scene, ar, v3d, base);
2142                                 }
2143                                 draw_object(scene, ar, v3d, base, 0);
2144                         }
2145                 }
2146         }
2147         
2148         /* this isn't that nice, draw xray objects as if they are normal */
2149         if (v3d->afterdraw_transp.first ||
2150             v3d->afterdraw_xray.first ||
2151             v3d->afterdraw_xraytransp.first)
2152         {
2153                 View3DAfter *v3da, *next;
2154                 int mask_orig;
2155
2156                 v3d->xray = TRUE;
2157                 
2158                 /* transp materials can change the depth mask, see #21388 */
2159                 glGetIntegerv(GL_DEPTH_WRITEMASK, &mask_orig);
2160
2161
2162                 if (v3d->afterdraw_xray.first || v3d->afterdraw_xraytransp.first) {
2163                         glDepthFunc(GL_ALWAYS); /* always write into the depth bufer, overwriting front z values */
2164                         for (v3da = v3d->afterdraw_xray.first; v3da; v3da = next) {
2165                                 next = v3da->next;
2166                                 draw_object(scene, ar, v3d, v3da->base, 0);
2167                         }
2168                         glDepthFunc(GL_LEQUAL); /* Now write the depth buffer normally */
2169                 }
2170
2171                 /* draw 3 passes, transp/xray/xraytransp */
2172                 v3d->xray = FALSE;
2173                 v3d->transp = TRUE;
2174                 for (v3da = v3d->afterdraw_transp.first; v3da; v3da = next) {
2175                         next = v3da->next;
2176                         draw_object(scene, ar, v3d, v3da->base, 0);
2177                         BLI_remlink(&v3d->afterdraw_transp, v3da);
2178                         MEM_freeN(v3da);
2179                 }
2180
2181                 v3d->xray = TRUE;
2182                 v3d->transp = FALSE;
2183                 for (v3da = v3d->afterdraw_xray.first; v3da; v3da = next) {
2184                         next = v3da->next;
2185                         draw_object(scene, ar, v3d, v3da->base, 0);
2186                         BLI_remlink(&v3d->afterdraw_xray, v3da);
2187                         MEM_freeN(v3da);
2188                 }
2189
2190                 v3d->xray = TRUE;
2191                 v3d->transp = TRUE;
2192                 for (v3da = v3d->afterdraw_xraytransp.first; v3da; v3da = next) {
2193                         next = v3da->next;
2194                         draw_object(scene, ar, v3d, v3da->base, 0);
2195                         BLI_remlink(&v3d->afterdraw_xraytransp, v3da);
2196                         MEM_freeN(v3da);
2197                 }
2198
2199                 
2200                 v3d->xray = FALSE;
2201                 v3d->transp = FALSE;
2202
2203                 glDepthMask(mask_orig);
2204         }
2205         
2206         if (rv3d->rflag & RV3D_CLIPPING)
2207                 ED_view3d_clipping_disable();
2208         
2209         v3d->zbuf = zbuf;
2210         if (!v3d->zbuf) glDisable(GL_DEPTH_TEST);
2211
2212         U.glalphaclip = glalphaclip;
2213         v3d->flag = flag;
2214         U.obcenter_dia = obcenter_dia;
2215 }
2216
2217 typedef struct View3DShadow {
2218         struct View3DShadow *next, *prev;
2219         GPULamp *lamp;
2220 } View3DShadow;
2221
2222 static void gpu_render_lamp_update(Scene *scene, View3D *v3d, Object *ob, Object *par,
2223                                    float obmat[][4], ListBase *shadows)
2224 {
2225         GPULamp *lamp;
2226         Lamp *la = (Lamp *)ob->data;
2227         View3DShadow *shadow;
2228         
2229         lamp = GPU_lamp_from_blender(scene, ob, par);
2230         
2231         if (lamp) {
2232                 GPU_lamp_update(lamp, ob->lay, (ob->restrictflag & OB_RESTRICT_RENDER), obmat);
2233                 GPU_lamp_update_colors(lamp, la->r, la->g, la->b, la->energy);
2234                 
2235                 if ((ob->lay & v3d->lay) && GPU_lamp_has_shadow_buffer(lamp)) {
2236                         shadow = MEM_callocN(sizeof(View3DShadow), "View3DShadow");
2237                         shadow->lamp = lamp;
2238                         BLI_addtail(shadows, shadow);
2239                 }
2240         }
2241 }
2242
2243 static void gpu_update_lamps_shadows(Scene *scene, View3D *v3d)
2244 {
2245         ListBase shadows;
2246         View3DShadow *shadow;
2247         Scene *sce_iter;
2248         Base *base;
2249         Object *ob;
2250         
2251         shadows.first = shadows.last = NULL;
2252         
2253         /* update lamp transform and gather shadow lamps */
2254         for (SETLOOPER(scene, sce_iter, base)) {
2255                 ob = base->object;
2256                 
2257                 if (ob->type == OB_LAMP)
2258                         gpu_render_lamp_update(scene, v3d, ob, NULL, ob->obmat, &shadows);
2259                 
2260                 if (ob->transflag & OB_DUPLI) {
2261                         DupliObject *dob;
2262                         ListBase *lb = object_duplilist(scene, ob);
2263                         
2264                         for (dob = lb->first; dob; dob = dob->next)
2265                                 if (dob->ob->type == OB_LAMP)
2266                                         gpu_render_lamp_update(scene, v3d, dob->ob, ob, dob->mat, &shadows);
2267                         
2268                         free_object_duplilist(lb);
2269                 }
2270         }
2271         
2272         /* render shadows after updating all lamps, nested object_duplilist
2273          * don't work correct since it's replacing object matrices */
2274         for (shadow = shadows.first; shadow; shadow = shadow->next) {
2275                 /* this needs to be done better .. */
2276                 float viewmat[4][4], winmat[4][4];
2277                 int drawtype, lay, winsize, flag2 = v3d->flag2;
2278                 ARegion ar = {NULL};
2279                 RegionView3D rv3d = {{{0}}};
2280                 
2281                 drawtype = v3d->drawtype;
2282                 lay = v3d->lay;
2283                 
2284                 v3d->drawtype = OB_SOLID;
2285                 v3d->lay &= GPU_lamp_shadow_layer(shadow->lamp);
2286                 v3d->flag2 &= ~V3D_SOLID_TEX;
2287                 v3d->flag2 |= V3D_RENDER_OVERRIDE | V3D_RENDER_SHADOW;
2288                 
2289                 GPU_lamp_shadow_buffer_bind(shadow->lamp, viewmat, &winsize, winmat);
2290
2291                 ar.regiondata = &rv3d;
2292                 ar.regiontype = RGN_TYPE_WINDOW;
2293                 rv3d.persp = RV3D_CAMOB;
2294                 copy_m4_m4(rv3d.winmat, winmat);
2295                 copy_m4_m4(rv3d.viewmat, viewmat);
2296                 invert_m4_m4(rv3d.viewinv, rv3d.viewmat);
2297                 mult_m4_m4m4(rv3d.persmat, rv3d.winmat, rv3d.viewmat);
2298                 invert_m4_m4(rv3d.persinv, rv3d.viewinv);
2299
2300                 ED_view3d_draw_offscreen(scene, v3d, &ar, winsize, winsize, viewmat, winmat, FALSE);
2301                 GPU_lamp_shadow_buffer_unbind(shadow->lamp);
2302                 
2303                 v3d->drawtype = drawtype;
2304                 v3d->lay = lay;
2305                 v3d->flag2 = flag2;
2306         }
2307         
2308         BLI_freelistN(&shadows);
2309 }
2310
2311 /* *********************** customdata **************** */
2312
2313 CustomDataMask ED_view3d_datamask(Scene *scene, View3D *v3d)
2314 {
2315         CustomDataMask mask = 0;
2316
2317         if (ELEM(v3d->drawtype, OB_TEXTURE, OB_MATERIAL) ||
2318             ((v3d->drawtype == OB_SOLID) && (v3d->flag2 & V3D_SOLID_TEX)))
2319         {
2320                 mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
2321
2322                 if (BKE_scene_use_new_shading_nodes(scene)) {
2323                         if (v3d->drawtype == OB_MATERIAL)
2324                                 mask |= CD_MASK_ORCO;
2325                 }
2326                 else {
2327                         if (scene->gm.matmode == GAME_MAT_GLSL)
2328                                 mask |= CD_MASK_ORCO;
2329                 }
2330         }
2331
2332         return mask;
2333 }
2334
2335 CustomDataMask ED_view3d_object_datamask(Scene *scene)
2336 {
2337         Object *ob = scene->basact ? scene->basact->object : NULL;
2338         CustomDataMask mask = 0;
2339
2340         if (ob) {
2341                 /* check if we need tfaces & mcols due to face select or texture paint */
2342                 if (paint_facesel_test(ob) || (ob->mode & OB_MODE_TEXTURE_PAINT)) {
2343                         mask |= CD_MASK_MTFACE | CD_MASK_MCOL;
2344                 }
2345
2346                 /* check if we need mcols due to vertex paint or weightpaint */
2347                 if (ob->mode & OB_MODE_VERTEX_PAINT) {
2348                         mask |= CD_MASK_MCOL;
2349                 }
2350
2351                 if (ob->mode & OB_MODE_WEIGHT_PAINT) {
2352                         mask |= CD_MASK_PREVIEW_MCOL;
2353                 }
2354
2355                 if (ob->mode & OB_MODE_EDIT)
2356                         mask |= CD_MASK_MVERT_SKIN;
2357         }
2358
2359         return mask;
2360 }
2361
2362 /* goes over all modes and view3d settings */
2363 CustomDataMask ED_view3d_screen_datamask(bScreen *screen)
2364 {
2365         Scene *scene = screen->scene;
2366         CustomDataMask mask = CD_MASK_BAREMESH;
2367         ScrArea *sa;
2368         
2369         /* check if we need tfaces & mcols due to view mode */
2370         for (sa = screen->areabase.first; sa; sa = sa->next) {
2371                 if (sa->spacetype == SPACE_VIEW3D) {
2372                         mask |= ED_view3d_datamask(scene, (View3D *)sa->spacedata.first);
2373                 }
2374         }
2375
2376         mask |= ED_view3d_object_datamask(scene);
2377
2378         return mask;
2379 }
2380
2381 void ED_view3d_update_viewmat(Scene *scene, View3D *v3d, ARegion *ar, float viewmat[][4], float winmat[][4])
2382 {
2383         RegionView3D *rv3d = ar->regiondata;
2384
2385         /* setup window matrices */
2386         if (winmat)
2387                 copy_m4_m4(rv3d->winmat, winmat);
2388         else
2389                 setwinmatrixview3d(ar, v3d, NULL);  /* NULL= no pickrect */
2390
2391         /* setup view matrix */
2392         if (viewmat)
2393                 copy_m4_m4(rv3d->viewmat, viewmat);
2394         else
2395                 setviewmatrixview3d(scene, v3d, rv3d);  /* note: calls BKE_object_where_is_calc for camera... */
2396
2397         /* update utilitity matrices */
2398         mult_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
2399         invert_m4_m4(rv3d->persinv, rv3d->persmat);
2400         invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
2401
2402         /* calculate pixelsize factor once, is used for lamps and obcenters */
2403         {
2404                 /* note:  '1.0f / len_v3(v1)'  replaced  'len_v3(rv3d->viewmat[0])'
2405                  * because of float point precision problems at large values [#23908] */
2406                 float v1[3], v2[3];
2407                 float len1, len2;
2408
2409                 v1[0] = rv3d->persmat[0][0];
2410                 v1[1] = rv3d->persmat[1][0];
2411                 v1[2] = rv3d->persmat[2][0];
2412
2413                 v2[0] = rv3d->persmat[0][1];
2414                 v2[1] = rv3d->persmat[1][1];
2415                 v2[2] = rv3d->persmat[2][1];
2416
2417                 len1 = 1.0f / len_v3(v1);
2418                 len2 = 1.0f / len_v3(v2);
2419
2420                 rv3d->pixsize = (2.0f * MAX2(len1, len2)) / (float)MAX2(ar->winx, ar->winy);
2421         }
2422 }
2423
2424 static void view3d_main_area_setup_view(Scene *scene, View3D *v3d, ARegion *ar, float viewmat[][4], float winmat[][4])
2425 {
2426         RegionView3D *rv3d = ar->regiondata;
2427
2428         ED_view3d_update_viewmat(scene, v3d, ar, viewmat, winmat);
2429
2430         /* set for opengl */
2431         glMatrixMode(GL_PROJECTION);
2432         glLoadMatrixf(rv3d->winmat);
2433         glMatrixMode(GL_MODELVIEW);
2434         glLoadMatrixf(rv3d->viewmat);
2435 }
2436
2437 void ED_view3d_draw_offscreen(Scene *scene, View3D *v3d, ARegion *ar,
2438                               int winx, int winy, float viewmat[][4], float winmat[][4],
2439                               int do_bgpic)
2440 {
2441         RegionView3D *rv3d = ar->regiondata;
2442         Base *base;
2443         float backcol[3];
2444         int bwinx, bwiny;
2445         rcti brect;
2446
2447         glPushMatrix();
2448
2449         /* set temporary new size */
2450         bwinx = ar->winx;
2451         bwiny = ar->winy;
2452         brect = ar->winrct;
2453         
2454         ar->winx = winx;
2455         ar->winy = winy;
2456         ar->winrct.xmin = 0;
2457         ar->winrct.ymin = 0;
2458         ar->winrct.xmax = winx;
2459         ar->winrct.ymax = winy;
2460
2461         /* set theme */
2462         UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
2463         
2464         /* set flags */
2465         G.f |= G_RENDER_OGL;
2466
2467         /* free images which can have changed on frame-change
2468          * warning! can be slow so only free animated images - campbell */
2469         GPU_free_images_anim();
2470         
2471         /* shadow buffers, before we setup matrices */
2472         if (draw_glsl_material(scene, NULL, v3d, v3d->drawtype))
2473                 gpu_update_lamps_shadows(scene, v3d);
2474
2475         /* set background color, fallback on the view background color
2476          * (if active clip is set but frame is failed to load fallback to horizon color as background) */
2477         if (scene->world) {
2478                 if (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
2479                         linearrgb_to_srgb_v3_v3(backcol, &scene->world->horr);
2480                 else
2481                         copy_v3_v3(backcol, &scene->world->horr);
2482                 glClearColor(backcol[0], backcol[1], backcol[2], 0.0);
2483         }
2484         else {
2485                 UI_ThemeClearColor(TH_BACK);
2486         }
2487
2488
2489         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2490
2491         /* setup view matrices */
2492         view3d_main_area_setup_view(scene, v3d, ar, viewmat, winmat);
2493
2494         if (rv3d->rflag & RV3D_CLIPPING)
2495                 view3d_draw_clipping(rv3d);
2496
2497         /* set zbuffer */
2498         if (v3d->drawtype > OB_WIRE) {
2499                 v3d->zbuf = TRUE;
2500                 glEnable(GL_DEPTH_TEST);
2501         }
2502         else
2503                 v3d->zbuf = FALSE;
2504
2505         /* important to do before clipping */
2506         if (do_bgpic) {
2507                 view3d_draw_bgpic_test(scene, ar, v3d, FALSE, FALSE);
2508         }
2509
2510         if (rv3d->rflag & RV3D_CLIPPING)
2511                 ED_view3d_clipping_set(rv3d);
2512
2513         /* draw set first */
2514         if (scene->set) {
2515                 Scene *sce_iter;
2516                 for (SETLOOPER(scene->set, sce_iter, base)) {
2517                         if (v3d->lay & base->lay) {
2518                                 UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
2519                                 draw_object(scene, ar, v3d, base, DRAW_CONSTCOLOR | DRAW_SCENESET);
2520                                 
2521                                 if (base->object->transflag & OB_DUPLI)
2522                                         draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
2523                         }
2524                 }
2525         }
2526         
2527         /* then draw not selected and the duplis, but skip editmode object */
2528         for (base = scene->base.first; base; base = base->next) {
2529                 if (v3d->lay & base->lay) {
2530                         /* dupli drawing */
2531                         if (base->object->transflag & OB_DUPLI)
2532                                 draw_dupli_objects(scene, ar, v3d, base);
2533
2534                         draw_object(scene, ar, v3d, base, 0);
2535                 }
2536         }
2537
2538         /* must be before xray draw which clears the depth buffer */
2539         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2540         draw_gpencil_view3d(scene, v3d, ar, 1);
2541         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2542
2543         /* transp and X-ray afterdraw stuff */
2544         if (v3d->afterdraw_transp.first)     view3d_draw_transp(scene, ar, v3d);
2545         if (v3d->afterdraw_xray.first)       view3d_draw_xray(scene, ar, v3d, 1);       /* clears zbuffer if it is used! */
2546         if (v3d->afterdraw_xraytransp.first) view3d_draw_xraytransp(scene, ar, v3d, 1);
2547
2548         if (rv3d->rflag & RV3D_CLIPPING)
2549                 ED_view3d_clipping_disable();
2550
2551         /* important to do after clipping */
2552         if (do_bgpic) {
2553                 view3d_draw_bgpic_test(scene, ar, v3d, TRUE, FALSE);
2554         }
2555
2556         /* cleanup */
2557         if (v3d->zbuf) {
2558                 v3d->zbuf = FALSE;
2559                 glDisable(GL_DEPTH_TEST);
2560         }
2561
2562         /* draw grease-pencil stuff */
2563         ED_region_pixelspace(ar);
2564
2565         /* draw grease-pencil stuff - needed to get paint-buffer shown too (since it's 2D) */
2566         draw_gpencil_view3d(scene, v3d, ar, 0);
2567
2568         /* freeing the images again here could be done after the operator runs, leaving for now */
2569         GPU_free_images_anim();
2570
2571         /* restore size */
2572         ar->winx = bwinx;
2573         ar->winy = bwiny;
2574         ar->winrct = brect;
2575
2576         glPopMatrix();
2577
2578         // XXX, without this the sequencer flickers with opengl draw enabled, need to find out why - campbell
2579         glColor4ub(255, 255, 255, 255);
2580
2581         G.f &= ~G_RENDER_OGL;
2582 }
2583
2584 /* utility func for ED_view3d_draw_offscreen */
2585 ImBuf *ED_view3d_draw_offscreen_imbuf(Scene *scene, View3D *v3d, ARegion *ar,
2586                                       int sizex, int sizey, unsigned int flag, int draw_background, char err_out[256])
2587 {
2588         RegionView3D *rv3d = ar->regiondata;
2589         ImBuf *ibuf;
2590         GPUOffScreen *ofs;
2591         
2592         /* state changes make normal drawing go weird otherwise */
2593         glPushAttrib(GL_LIGHTING_BIT);
2594
2595         /* bind */
2596         ofs = GPU_offscreen_create(sizex, sizey, err_out);
2597         if (ofs == NULL)
2598                 return NULL;
2599
2600         GPU_offscreen_bind(ofs);
2601
2602         /* render 3d view */
2603         if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
2604                 CameraParams params;
2605
2606                 BKE_camera_params_init(&params);
2607                 BKE_camera_params_from_object(&params, v3d->camera);
2608                 BKE_camera_params_compute_viewplane(&params, sizex, sizey, scene->r.xasp, scene->r.yasp);
2609                 BKE_camera_params_compute_matrix(&params);
2610
2611                 ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, params.winmat, draw_background);
2612         }
2613         else {
2614                 ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, NULL, draw_background);
2615         }
2616
2617         /* read in pixels & stamp */
2618         ibuf = IMB_allocImBuf(sizex, sizey, 32, flag);
2619
2620         if (ibuf->rect_float)
2621                 GPU_offscreen_read_pixels(ofs, GL_FLOAT, ibuf->rect_float);
2622         else if (ibuf->rect)
2623                 GPU_offscreen_read_pixels(ofs, GL_UNSIGNED_BYTE, ibuf->rect);
2624         
2625         //if ((scene->r.stamp & R_STAMP_ALL) && (scene->r.stamp & R_STAMP_DRAW))
2626         //      BKE_stamp_buf(scene, NULL, rr->rectf, rr->rectx, rr->recty, 4);
2627
2628         /* unbind */
2629         GPU_offscreen_unbind(ofs);
2630         GPU_offscreen_free(ofs);
2631
2632         glPopAttrib();
2633         
2634         if (ibuf->rect_float && ibuf->rect)
2635                 IMB_rect_from_float(ibuf);
2636         
2637         return ibuf;
2638 }
2639
2640 /* creates own 3d views, used by the sequencer */
2641 ImBuf *ED_view3d_draw_offscreen_imbuf_simple(Scene *scene, Object *camera, int width, int height,
2642                                              unsigned int flag, int drawtype, int draw_background, char err_out[256])
2643 {
2644         View3D v3d = {NULL};
2645         ARegion ar = {NULL};
2646         RegionView3D rv3d = {{{0}}};
2647
2648         /* connect data */
2649         v3d.regionbase.first = v3d.regionbase.last = &ar;
2650         ar.regiondata = &rv3d;
2651         ar.regiontype = RGN_TYPE_WINDOW;
2652
2653         v3d.camera = camera;
2654         v3d.lay = scene->lay;
2655         v3d.drawtype = drawtype;
2656         v3d.flag2 = V3D_RENDER_OVERRIDE;
2657
2658         rv3d.persp = RV3D_CAMOB;
2659
2660         copy_m4_m4(rv3d.viewinv, v3d.camera->obmat);
2661         normalize_m4(rv3d.viewinv);
2662         invert_m4_m4(rv3d.viewmat, rv3d.viewinv);
2663
2664         {
2665                 CameraParams params;
2666
2667                 BKE_camera_params_init(&params);
2668                 BKE_camera_params_from_object(&params, v3d.camera);
2669                 BKE_camera_params_compute_viewplane(&params, width, height, scene->r.xasp, scene->r.yasp);
2670                 BKE_camera_params_compute_matrix(&params);
2671
2672                 copy_m4_m4(rv3d.winmat, params.winmat);
2673                 v3d.near = params.clipsta;
2674                 v3d.far = params.clipend;
2675                 v3d.lens = params.lens;
2676         }
2677
2678         mult_m4_m4m4(rv3d.persmat, rv3d.winmat, rv3d.viewmat);
2679         invert_m4_m4(rv3d.persinv, rv3d.viewinv);
2680
2681         return ED_view3d_draw_offscreen_imbuf(scene, &v3d, &ar, width, height, flag, draw_background, err_out);
2682
2683         // seq_view3d_cb(scene, cfra, render_size, seqrectx, seqrecty);
2684 }
2685
2686
2687 /* NOTE: the info that this uses is updated in ED_refresh_viewport_fps(), 
2688  * which currently gets called during SCREEN_OT_animation_step.
2689  */
2690 static void draw_viewport_fps(Scene *scene, ARegion *ar)
2691 {
2692         ScreenFrameRateInfo *fpsi = scene->fps_info;
2693         float fps;
2694         char printable[16];
2695         int i, tot;
2696         
2697         if (!fpsi || !fpsi->lredrawtime || !fpsi->redrawtime)
2698                 return;
2699         
2700         printable[0] = '\0';
2701         
2702 #if 0
2703         /* this is too simple, better do an average */
2704         fps = (float)(1.0 / (fpsi->lredrawtime - fpsi->redrawtime))
2705 #else
2706         fpsi->redrawtimes_fps[fpsi->redrawtime_index] = (float)(1.0 / (fpsi->lredrawtime - fpsi->redrawtime));
2707         
2708         for (i = 0, tot = 0, fps = 0.0f; i < REDRAW_FRAME_AVERAGE; i++) {
2709                 if (fpsi->redrawtimes_fps[i]) {
2710                         fps += fpsi->redrawtimes_fps[i];
2711                         tot++;
2712                 }
2713         }
2714         if (tot) {
2715                 fpsi->redrawtime_index = (fpsi->redrawtime_index + 1) % REDRAW_FRAME_AVERAGE;
2716                 
2717                 //fpsi->redrawtime_index++;
2718                 //if (fpsi->redrawtime >= REDRAW_FRAME_AVERAGE)
2719                 //      fpsi->redrawtime = 0;
2720                 
2721                 fps = fps / tot;
2722         }
2723 #endif
2724
2725         /* is this more then half a frame behind? */
2726         if (fps + 0.5f < (float)(FPS)) {
2727                 UI_ThemeColor(TH_REDALERT);
2728                 BLI_snprintf(printable, sizeof(printable), "fps: %.2f", fps);
2729         } 
2730         else {
2731                 UI_ThemeColor(TH_TEXT_HI);
2732                 BLI_snprintf(printable, sizeof(printable), "fps: %i", (int)(fps + 0.5f));
2733         }
2734         
2735         BLF_draw_default_ascii(22,  ar->winy - 17, 0.0f, printable, sizeof(printable));
2736 }
2737
2738 static int view3d_main_area_draw_engine(const bContext *C, ARegion *ar)
2739 {
2740         Scene *scene = CTX_data_scene(C);
2741         View3D *v3d = CTX_wm_view3d(C);
2742         RegionView3D *rv3d = CTX_wm_region_view3d(C);
2743         RenderEngineType *type;
2744
2745         /* create render engine */
2746         if (!rv3d->render_engine) {
2747                 type = RE_engines_find(scene->r.engine);
2748
2749                 if (!(type->view_update && type->view_draw))
2750                         return 0;
2751
2752                 rv3d->render_engine = RE_engine_create(type);
2753                 type->view_update(rv3d->render_engine, C);
2754         }
2755
2756         /* setup view matrices */
2757         view3d_main_area_setup_view(scene, v3d, ar, NULL, NULL);
2758
2759         /* background draw */
2760         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
2761         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2762
2763         ED_region_pixelspace(ar);
2764
2765         /* render result draw */
2766         if (v3d->flag & V3D_DISPBGPICS)
2767                 view3d_draw_bgpic(scene, ar, v3d, FALSE, TRUE);
2768         else
2769                 fdrawcheckerboard(0, 0, ar->winx, ar->winy);
2770
2771         type = rv3d->render_engine->type;
2772         type->view_draw(rv3d->render_engine, C);
2773
2774         if (v3d->flag & V3D_DISPBGPICS)
2775                 view3d_draw_bgpic(scene, ar, v3d, TRUE, TRUE);
2776
2777         return 1;
2778 }
2779
2780 static void view3d_main_area_draw_engine_info(RegionView3D *rv3d, ARegion *ar)
2781 {
2782         if (!rv3d->render_engine || !rv3d->render_engine->text)
2783                 return;
2784
2785         ED_region_info_draw(ar, rv3d->render_engine->text, 1, 0.25);
2786 }
2787
2788 /* warning: this function has duplicate drawing in ED_view3d_draw_offscreen() */
2789 static void view3d_main_area_draw_objects(const bContext *C, ARegion *ar, const char **grid_unit)
2790 {
2791         Scene *scene = CTX_data_scene(C);
2792         View3D *v3d = CTX_wm_view3d(C);
2793         RegionView3D *rv3d = CTX_wm_region_view3d(C);
2794         Base *base;
2795         float backcol[3];
2796         unsigned int lay_used;
2797
2798         /* shadow buffers, before we setup matrices */
2799         if (draw_glsl_material(scene, NULL, v3d, v3d->drawtype))
2800                 gpu_update_lamps_shadows(scene, v3d);
2801         
2802         /* reset default OpenGL lights if needed (i.e. after preferences have been altered) */
2803         if (rv3d->rflag & RV3D_GPULIGHT_UPDATE) {
2804                 rv3d->rflag &= ~RV3D_GPULIGHT_UPDATE;
2805                 GPU_default_lights();
2806         }
2807
2808         /* clear background */
2809         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) && scene->world) {
2810                 if (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT)
2811                         linearrgb_to_srgb_v3_v3(backcol, &scene->world->horr);
2812                 else
2813                         copy_v3_v3(backcol, &scene->world->horr);
2814                 glClearColor(backcol[0], backcol[1], backcol[2], 0.0);
2815         }
2816         else
2817                 UI_ThemeClearColor(TH_BACK);
2818
2819         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2820         
2821         /* setup view matrices */
2822         view3d_main_area_setup_view(scene, v3d, ar, NULL, NULL);
2823
2824         ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
2825
2826         if (rv3d->rflag & RV3D_CLIPPING)
2827                 view3d_draw_clipping(rv3d);
2828         
2829         /* set zbuffer after we draw clipping region */
2830         if (v3d->drawtype > OB_WIRE) {
2831                 v3d->zbuf = TRUE;
2832                 glEnable(GL_DEPTH_TEST);
2833         }
2834         else
2835                 v3d->zbuf = FALSE;
2836
2837         /* enables anti-aliasing for 3D view drawing */
2838 #if 0
2839         if (!(U.gameflags & USER_DISABLE_AA))
2840                 glEnable(GL_MULTISAMPLE_ARB);
2841 #endif
2842
2843         // needs to be done always, gridview is adjusted in drawgrid() now
2844         rv3d->gridview = v3d->grid;
2845
2846         if ((rv3d->view == RV3D_VIEW_USER) || (rv3d->persp != RV3D_ORTHO)) {
2847                 if ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) {
2848                         drawfloor(scene, v3d, grid_unit);
2849                 }
2850                 if (rv3d->persp == RV3D_CAMOB) {
2851                         if (scene->world) {
2852                                 if (scene->world->mode & WO_STARS) {
2853                                         RE_make_stars(NULL, scene, star_stuff_init_func, star_stuff_vertex_func,
2854                                                       star_stuff_term_func);
2855                                 }
2856                         }
2857                 }
2858         }
2859         else {
2860                 if ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) {
2861                         ED_region_pixelspace(ar);
2862                         drawgrid(&scene->unit, ar, v3d, grid_unit);
2863                         /* XXX make function? replaces persp(1) */
2864                         glMatrixMode(GL_PROJECTION);
2865                         glLoadMatrixf(rv3d->winmat);
2866                         glMatrixMode(GL_MODELVIEW);
2867                         glLoadMatrixf(rv3d->viewmat);
2868                 }
2869         }
2870
2871         view3d_draw_bgpic_test(scene, ar, v3d, FALSE, TRUE);
2872
2873         if (rv3d->rflag & RV3D_CLIPPING)
2874                 ED_view3d_clipping_set(rv3d);
2875
2876         /* draw set first */
2877         if (scene->set) {
2878                 Scene *sce_iter;
2879                 for (SETLOOPER(scene->set, sce_iter, base)) {
2880                         
2881                         if (v3d->lay & base->lay) {
2882                                 
2883                                 UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
2884                                 draw_object(scene, ar, v3d, base, DRAW_CONSTCOLOR | DRAW_SCENESET);
2885                                 
2886                                 if (base->object->transflag & OB_DUPLI) {
2887                                         draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
2888                                 }
2889                         }
2890                 }
2891                 
2892                 /* Transp and X-ray afterdraw stuff for sets is done later */
2893         }
2894
2895         lay_used = 0;
2896
2897         /* then draw not selected and the duplis, but skip editmode object */
2898         for (base = scene->base.first; base; base = base->next) {
2899                 lay_used |= base->lay & ((1 << 20) - 1);
2900
2901                 if (v3d->lay & base->lay) {
2902                         
2903                         /* dupli drawing */
2904                         if (base->object->transflag & OB_DUPLI) {
2905                                 draw_dupli_objects(scene, ar, v3d, base);
2906                         }
2907                         if ((base->flag & SELECT) == 0) {
2908                                 if (base->object != scene->obedit)
2909                                         draw_object(scene, ar, v3d, base, 0);
2910                         }
2911                 }
2912         }
2913
2914         if (v3d->lay_used != lay_used) { /* happens when loading old files or loading with UI load */
2915                 /* find header and force tag redraw */
2916                 ScrArea *sa = CTX_wm_area(C);
2917                 ARegion *ar_header = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
2918                 ED_region_tag_redraw(ar_header); /* can be NULL */
2919                 v3d->lay_used = lay_used;
2920         }
2921
2922         /* draw selected and editmode */
2923         for (base = scene->base.first; base; base = base->next) {
2924                 if (v3d->lay & base->lay) {
2925                         if (base->object == scene->obedit || (base->flag & SELECT) )
2926                                 draw_object(scene, ar, v3d, base, 0);
2927                 }
2928         }
2929
2930 //      REEB_draw();
2931
2932         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) {
2933                 /* must be before xray draw which clears the depth buffer */
2934                 if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2935                 draw_gpencil_view3d(scene, v3d, ar, 1);
2936                 if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2937         }
2938
2939         /* Transp and X-ray afterdraw stuff */
2940         if (v3d->afterdraw_transp.first) view3d_draw_transp(scene, ar, v3d);
2941         if (v3d->afterdraw_xray.first) view3d_draw_xray(scene, ar, v3d, 1);         // clears zbuffer if it is used!
2942         if (v3d->afterdraw_xraytransp.first) view3d_draw_xraytransp(scene, ar, v3d, 1);
2943         
2944         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
2945
2946         if (rv3d->rflag & RV3D_CLIPPING)
2947                 ED_view3d_clipping_disable();
2948         
2949         /* important to do after clipping */
2950         view3d_draw_bgpic_test(scene, ar, v3d, TRUE, TRUE);
2951
2952         BIF_draw_manipulator(C);
2953
2954 #if 0
2955         /* Disable back anti-aliasing */
2956         if (!(U.gameflags & USER_DISABLE_AA))
2957                 glDisable(GL_MULTISAMPLE_ARB);
2958 #endif
2959
2960         if (v3d->zbuf) {
2961                 v3d->zbuf = FALSE;
2962                 glDisable(GL_DEPTH_TEST);
2963         }
2964
2965         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) {
2966                 BDR_drawSketch(C);
2967         }
2968
2969         if ((U.ndof_flag & NDOF_SHOW_GUIDE) && (rv3d->viewlock != RV3D_LOCKED) && (rv3d->persp != RV3D_CAMOB))
2970                 // TODO: draw something else (but not this) during fly mode
2971                 draw_rotation_guide(rv3d);
2972
2973 }
2974
2975 static void view3d_main_area_draw_info(const bContext *C, ARegion *ar, const char *grid_unit)
2976 {
2977         wmWindowManager *wm = CTX_wm_manager(C);
2978         Scene *scene = CTX_data_scene(C);
2979         View3D *v3d = CTX_wm_view3d(C);
2980         RegionView3D *rv3d = CTX_wm_region_view3d(C);
2981
2982         Object *ob;
2983
2984         if (rv3d->persp == RV3D_CAMOB)
2985                 drawviewborder(scene, ar, v3d);
2986
2987         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) {
2988                 /* draw grease-pencil stuff - needed to get paint-buffer shown too (since it's 2D) */
2989                 //      if (v3d->flag2 & V3D_DISPGP)
2990                 draw_gpencil_view3d(scene, v3d, ar, 0);
2991
2992                 drawcursor(scene, ar, v3d);
2993         }
2994         
2995         if (U.uiflag & USER_SHOW_ROTVIEWICON)
2996                 draw_view_axis(rv3d);
2997         else
2998                 draw_view_icon(rv3d);
2999         
3000         ob = OBACT;
3001         if (U.uiflag & USER_DRAWVIEWINFO)
3002                 draw_selected_name(scene, ob);
3003
3004         if (rv3d->render_engine) {
3005                 view3d_main_area_draw_engine_info(rv3d, ar);
3006                 return;
3007         }
3008
3009         if ((U.uiflag & USER_SHOW_FPS) && ED_screen_animation_playing(wm)) {
3010                 draw_viewport_fps(scene, ar);
3011         }
3012         else if (U.uiflag & USER_SHOW_VIEWPORTNAME) {
3013                 draw_viewport_name(ar, v3d);
3014         }
3015
3016         if (grid_unit) { /* draw below the viewport name */
3017                 char numstr[32] = "";
3018
3019                 UI_ThemeColor(TH_TEXT_HI);
3020                 if (v3d->grid != 1.0f) {
3021                         BLI_snprintf(numstr, sizeof(numstr), "%s x %.4g", grid_unit, v3d->grid);
3022                 }
3023
3024                 BLF_draw_default_ascii(22,  ar->winy - (USER_SHOW_VIEWPORTNAME ? 40 : 20), 0.0f,
3025                                        numstr[0] ? numstr : grid_unit, sizeof(numstr));
3026         }
3027 }
3028
3029 void view3d_main_area_draw(const bContext *C, ARegion *ar)
3030 {
3031         View3D *v3d = CTX_wm_view3d(C);
3032         const char *grid_unit = NULL;
3033
3034         /* draw viewport using external renderer? */
3035         if (!(v3d->drawtype == OB_RENDER && view3d_main_area_draw_engine(C, ar))) {
3036                 /* draw viewport using opengl */
3037                 view3d_main_area_draw_objects(C, ar, &grid_unit);
3038                 ED_region_pixelspace(ar);
3039         }
3040         
3041         view3d_main_area_draw_info(C, ar, grid_unit);
3042
3043         v3d->flag |= V3D_INVALID_BACKBUF;
3044 }
3045