Merge branch 'master' into blender2.8
[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 #include <math.h>
32
33 #include "BIF_gl.h"
34 #include "BIF_glutil.h"
35
36 #include "BKE_camera.h"
37 #include "BKE_context.h"
38 #include "BKE_key.h"
39 #include "BKE_scene.h"
40 #include "BKE_object.h"
41 #include "BKE_paint.h"
42 #include "BKE_unit.h"
43
44 #include "BLF_api.h"
45
46 #include "BLI_listbase.h"
47 #include "BLI_math.h"
48 #include "BLI_rect.h"
49 #include "BLI_string.h"
50 #include "BLI_threads.h"
51
52 #include "BLT_translation.h"
53
54 #include "DNA_armature_types.h"
55 #include "DNA_brush_types.h"
56 #include "DNA_camera_types.h"
57 #include "DNA_key_types.h"
58 #include "DNA_mesh_types.h"
59 #include "DNA_object_types.h"
60 #include "DNA_view3d_types.h"
61 #include "DNA_windowmanager_types.h"
62
63 #include "ED_keyframing.h"
64 #include "ED_armature.h"
65 #include "ED_keyframing.h"
66 #include "ED_gpencil.h"
67 #include "ED_screen.h"
68 #include "ED_transform.h"
69 #include "ED_gpencil.h"
70
71 #include "GPU_matrix.h"
72 #include "GPU_immediate.h"
73 #include "GPU_material.h"
74 #include "GPU_viewport.h"
75
76 #include "MEM_guardedalloc.h"
77
78 #include "UI_interface.h"
79 #include "UI_resources.h"
80
81 #include "RE_engine.h"
82
83 #include "WM_api.h"
84
85 #include "view3d_intern.h"  /* own include */
86
87 /* prototypes */
88 static void draw_all_objects(const bContext *C, ARegion *ar, const bool only_depth, const bool use_depth);
89
90 typedef struct DrawData {
91         rcti border_rect;
92         bool render_border;
93         bool clip_border;
94         bool is_render;
95         GPUViewport *viewport;
96 } DrawData;
97
98 static void view3d_draw_data_init(const bContext *C, ARegion *ar, RegionView3D *rv3d, DrawData *draw_data)
99 {
100         Scene *scene = CTX_data_scene(C);
101         View3D *v3d = CTX_wm_view3d(C);
102
103         draw_data->is_render = (v3d->drawtype == OB_RENDER);
104
105         draw_data->render_border = ED_view3d_calc_render_border(scene, v3d, ar, &draw_data->border_rect);
106         draw_data->clip_border = (draw_data->render_border && !BLI_rcti_compare(&ar->drawrct, &draw_data->border_rect));
107
108         draw_data->viewport = rv3d->viewport;
109 }
110
111 /* ******************** general functions ***************** */
112
113 static bool use_depth_doit(Scene *scene, View3D *v3d)
114 {
115         if (v3d->drawtype > OB_WIRE)
116                 return true;
117
118         /* special case (depth for wire color) */
119         if (v3d->drawtype <= OB_WIRE) {
120                 if (scene->obedit && scene->obedit->type == OB_MESH) {
121                         Mesh *me = scene->obedit->data;
122                         if (me->drawflag & ME_DRAWEIGHT) {
123                                 return true;
124                         }
125                 }
126         }
127         return false;
128 }
129
130 static bool use_depth(const bContext *C)
131 {
132         View3D *v3d = CTX_wm_view3d(C);
133         Scene *scene = CTX_data_scene(C);
134         return use_depth_doit(scene, v3d);
135 }
136
137 /**
138  * \note keep this synced with #ED_view3d_mats_rv3d_backup/#ED_view3d_mats_rv3d_restore
139  */
140 void ED_view3d_update_viewmat(Scene *scene, View3D *v3d, ARegion *ar, float viewmat[4][4], float winmat[4][4])
141 {
142         RegionView3D *rv3d = ar->regiondata;
143
144
145         /* setup window matrices */
146         if (winmat)
147                 copy_m4_m4(rv3d->winmat, winmat);
148         else
149                 view3d_winmatrix_set(ar, v3d, NULL);
150
151         /* setup view matrix */
152         if (viewmat)
153                 copy_m4_m4(rv3d->viewmat, viewmat);
154         else
155                 view3d_viewmatrix_set(scene, v3d, rv3d);  /* note: calls BKE_object_where_is_calc for camera... */
156
157         /* update utility matrices */
158         mul_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
159         invert_m4_m4(rv3d->persinv, rv3d->persmat);
160         invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
161
162         /* calculate GLSL view dependent values */
163
164         /* store window coordinates scaling/offset */
165         if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
166                 rctf cameraborder;
167                 ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &cameraborder, false);
168                 rv3d->viewcamtexcofac[0] = (float)ar->winx / BLI_rctf_size_x(&cameraborder);
169                 rv3d->viewcamtexcofac[1] = (float)ar->winy / BLI_rctf_size_y(&cameraborder);
170
171                 rv3d->viewcamtexcofac[2] = -rv3d->viewcamtexcofac[0] * cameraborder.xmin / (float)ar->winx;
172                 rv3d->viewcamtexcofac[3] = -rv3d->viewcamtexcofac[1] * cameraborder.ymin / (float)ar->winy;
173         }
174         else {
175                 rv3d->viewcamtexcofac[0] = rv3d->viewcamtexcofac[1] = 1.0f;
176                 rv3d->viewcamtexcofac[2] = rv3d->viewcamtexcofac[3] = 0.0f;
177         }
178
179         /* calculate pixelsize factor once, is used for lamps and obcenters */
180         {
181                 /* note:  '1.0f / len_v3(v1)'  replaced  'len_v3(rv3d->viewmat[0])'
182                 * because of float point precision problems at large values [#23908] */
183                 float v1[3], v2[3];
184                 float len_px, len_sc;
185
186                 v1[0] = rv3d->persmat[0][0];
187                 v1[1] = rv3d->persmat[1][0];
188                 v1[2] = rv3d->persmat[2][0];
189
190                 v2[0] = rv3d->persmat[0][1];
191                 v2[1] = rv3d->persmat[1][1];
192                 v2[2] = rv3d->persmat[2][1];
193
194                 len_px = 2.0f / sqrtf(min_ff(len_squared_v3(v1), len_squared_v3(v2)));
195                 len_sc = (float)MAX2(ar->winx, ar->winy);
196
197                 rv3d->pixsize = len_px / len_sc;
198         }
199 }
200
201 static void view3d_main_region_setup_view(Scene *scene, View3D *v3d, ARegion *ar, float viewmat[4][4], float winmat[4][4])
202 {
203         RegionView3D *rv3d = ar->regiondata;
204
205         ED_view3d_update_viewmat(scene, v3d, ar, viewmat, winmat);
206
207         /* set for opengl */
208         /* TODO(merwin): transition to GPU_matrix API */
209         glMatrixMode(GL_PROJECTION);
210         glLoadMatrixf(rv3d->winmat);
211         glMatrixMode(GL_MODELVIEW);
212         glLoadMatrixf(rv3d->viewmat);
213 }
214
215 static bool view3d_stereo3d_active(const bContext *C, Scene *scene, View3D *v3d, RegionView3D *rv3d)
216 {
217         wmWindow *win = CTX_wm_window(C);
218
219         if ((scene->r.scemode & R_MULTIVIEW) == 0)
220                 return false;
221
222         if (WM_stereo3d_enabled(win, true) == false)
223                 return false;
224
225         if ((v3d->camera == NULL) || (v3d->camera->type != OB_CAMERA) || rv3d->persp != RV3D_CAMOB)
226                 return false;
227
228         if (scene->r.views_format & SCE_VIEWS_FORMAT_MULTIVIEW) {
229                 if (v3d->stereo3d_camera == STEREO_MONO_ID)
230                         return false;
231
232                 return BKE_scene_multiview_is_stereo3d(&scene->r);
233         }
234
235         return true;
236 }
237
238 /* setup the view and win matrices for the multiview cameras
239  *
240  * unlike view3d_stereo3d_setup_offscreen, when view3d_stereo3d_setup is called
241  * we have no winmatrix (i.e., projection matrix) defined at that time.
242  * Since the camera and the camera shift are needed for the winmat calculation
243  * we do a small hack to replace it temporarily so we don't need to change the
244  * view3d)main_region_setup_view() code to account for that.
245  */
246 static void view3d_stereo3d_setup(Scene *scene, View3D *v3d, ARegion *ar)
247 {
248         bool is_left;
249         const char *names[2] = { STEREO_LEFT_NAME, STEREO_RIGHT_NAME };
250         const char *viewname;
251
252         /* show only left or right camera */
253         if (v3d->stereo3d_camera != STEREO_3D_ID)
254                 v3d->multiview_eye = v3d->stereo3d_camera;
255
256         is_left = v3d->multiview_eye == STEREO_LEFT_ID;
257         viewname = names[is_left ? STEREO_LEFT_ID : STEREO_RIGHT_ID];
258
259         /* update the viewport matrices with the new camera */
260         if (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D) {
261                 Camera *data;
262                 float viewmat[4][4];
263                 float shiftx;
264
265                 data = (Camera *)v3d->camera->data;
266                 shiftx = data->shiftx;
267
268                 BLI_lock_thread(LOCK_VIEW3D);
269                 data->shiftx = BKE_camera_multiview_shift_x(&scene->r, v3d->camera, viewname);
270
271                 BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat);
272                 view3d_main_region_setup_view(scene, v3d, ar, viewmat, NULL);
273
274                 data->shiftx = shiftx;
275                 BLI_unlock_thread(LOCK_VIEW3D);
276         }
277         else { /* SCE_VIEWS_FORMAT_MULTIVIEW */
278                 float viewmat[4][4];
279                 Object *view_ob = v3d->camera;
280                 Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
281
282                 BLI_lock_thread(LOCK_VIEW3D);
283                 v3d->camera = camera;
284
285                 BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat);
286                 view3d_main_region_setup_view(scene, v3d, ar, viewmat, NULL);
287
288                 v3d->camera = view_ob;
289                 BLI_unlock_thread(LOCK_VIEW3D);
290         }
291 }
292
293 /* ******************** debug ***************** */
294
295 #define VIEW3D_DRAW_DEBUG 1
296 /* TODO: expand scope of this flag so UI reflects the underlying code */
297
298 #if VIEW3D_DRAW_DEBUG
299
300 static void view3d_draw_debug_store_depth(ARegion *UNUSED(ar), DrawData *draw_data)
301 {
302         GPUViewport *viewport = draw_data->viewport;
303         GLint viewport_size[4];
304         glGetIntegerv(GL_VIEWPORT, viewport_size);
305
306         const int x = viewport_size[0];
307         const int y = viewport_size[1];
308         const int w = viewport_size[2];
309         const int h = viewport_size[3];
310
311         if (GPU_viewport_debug_depth_is_valid(viewport)) {
312                 if ((GPU_viewport_debug_depth_width(viewport) != w) ||
313                     (GPU_viewport_debug_depth_height(viewport) != h))
314                 {
315                         GPU_viewport_debug_depth_free(viewport);
316                 }
317         }
318
319         if (!GPU_viewport_debug_depth_is_valid(viewport)) {
320                 char error[256];
321                 if (!GPU_viewport_debug_depth_create(viewport, w, h, error)) {
322                         fprintf(stderr, "Failed to create depth buffer for debug: %s\n", error);
323                         return;
324                 }
325         }
326
327         GPU_viewport_debug_depth_store(viewport, x, y);
328 }
329
330 static void view3d_draw_debug_post_solid(const bContext *C, ARegion *ar, DrawData *draw_data)
331 {
332         View3D *v3d = CTX_wm_view3d(C);
333
334         if ((v3d->tmp_compat_flag & V3D_DEBUG_SHOW_SCENE_DEPTH) != 0) {
335                 view3d_draw_debug_store_depth(ar, draw_data);
336         }
337 }
338
339 static void view3d_draw_debug(const bContext *C, ARegion *ar, DrawData *draw_data)
340 {
341         View3D *v3d = CTX_wm_view3d(C);
342
343         if ((v3d->tmp_compat_flag & V3D_DEBUG_SHOW_COMBINED_DEPTH) != 0) {
344                 /* store */
345                 view3d_draw_debug_store_depth(ar, draw_data);
346         }
347
348         if (((v3d->tmp_compat_flag & V3D_DEBUG_SHOW_SCENE_DEPTH) != 0) ||
349             ((v3d->tmp_compat_flag & V3D_DEBUG_SHOW_COMBINED_DEPTH) != 0))
350         {
351                 /* draw */
352                 if (GPU_viewport_debug_depth_is_valid(draw_data->viewport)) {
353                         GPU_viewport_debug_depth_draw(draw_data->viewport, v3d->debug.znear, v3d->debug.zfar);
354                 }
355         }
356         else {
357                 /* cleanup */
358                 GPU_viewport_debug_depth_free(draw_data->viewport);
359         }
360 }
361
362 #endif /* VIEW3D_DRAW_DEBUG */
363
364 /* ******************** view border ***************** */
365
366 static void view3d_camera_border(
367         const Scene *scene, const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
368         rctf *r_viewborder, const bool no_shift, const bool no_zoom)
369 {
370         CameraParams params;
371         rctf rect_view, rect_camera;
372
373         /* get viewport viewplane */
374         BKE_camera_params_init(&params);
375         BKE_camera_params_from_view3d(&params, v3d, rv3d);
376         if (no_zoom)
377                 params.zoom = 1.0f;
378         BKE_camera_params_compute_viewplane(&params, ar->winx, ar->winy, 1.0f, 1.0f);
379         rect_view = params.viewplane;
380
381         /* get camera viewplane */
382         BKE_camera_params_init(&params);
383         /* fallback for non camera objects */
384         params.clipsta = v3d->near;
385         params.clipend = v3d->far;
386         BKE_camera_params_from_object(&params, v3d->camera);
387         if (no_shift) {
388                 params.shiftx = 0.0f;
389                 params.shifty = 0.0f;
390         }
391         BKE_camera_params_compute_viewplane(&params, scene->r.xsch, scene->r.ysch, scene->r.xasp, scene->r.yasp);
392         rect_camera = params.viewplane;
393
394         /* get camera border within viewport */
395         r_viewborder->xmin = ((rect_camera.xmin - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) * ar->winx;
396         r_viewborder->xmax = ((rect_camera.xmax - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) * ar->winx;
397         r_viewborder->ymin = ((rect_camera.ymin - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) * ar->winy;
398         r_viewborder->ymax = ((rect_camera.ymax - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) * ar->winy;
399 }
400
401 void ED_view3d_calc_camera_border_size(
402         const Scene *scene, const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
403         float r_size[2])
404 {
405         rctf viewborder;
406
407         view3d_camera_border(scene, ar, v3d, rv3d, &viewborder, true, true);
408         r_size[0] = BLI_rctf_size_x(&viewborder);
409         r_size[1] = BLI_rctf_size_y(&viewborder);
410 }
411
412 void ED_view3d_calc_camera_border(
413         const Scene *scene, const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
414         rctf *r_viewborder, const bool no_shift)
415 {
416         view3d_camera_border(scene, ar, v3d, rv3d, r_viewborder, no_shift, false);
417 }
418
419 static void drawviewborder_grid3(unsigned pos, float x1, float x2, float y1, float y2, float fac)
420 {
421         float x3, y3, x4, y4;
422
423         x3 = x1 + fac * (x2 - x1);
424         y3 = y1 + fac * (y2 - y1);
425         x4 = x1 + (1.0f - fac) * (x2 - x1);
426         y4 = y1 + (1.0f - fac) * (y2 - y1);
427
428         immBegin(GL_LINES, 8);
429         immVertex2f(pos, x1, y3);
430         immVertex2f(pos, x2, y3);
431
432         immVertex2f(pos, x1, y4);
433         immVertex2f(pos, x2, y4);
434
435         immVertex2f(pos, x3, y1);
436         immVertex2f(pos, x3, y2);
437
438         immVertex2f(pos, x4, y1);
439         immVertex2f(pos, x4, y2);
440         immEnd();
441 }
442
443 /* harmonious triangle */
444 static void drawviewborder_triangle(unsigned pos, float x1, float x2, float y1, float y2, const char golden, const char dir)
445 {
446         float ofs;
447         float w = x2 - x1;
448         float h = y2 - y1;
449
450         immBegin(GL_LINES, 6);
451         if (w > h) {
452                 if (golden) {
453                         ofs = w * (1.0f - (1.0f / 1.61803399f));
454                 }
455                 else {
456                         ofs = h * (h / w);
457                 }
458                 if (dir == 'B') SWAP(float, y1, y2);
459
460                 immVertex2f(pos, x1, y1);
461                 immVertex2f(pos, x2, y2);
462
463                 immVertex2f(pos, x2, y1);
464                 immVertex2f(pos, x1 + (w - ofs), y2);
465
466                 immVertex2f(pos, x1, y2);
467                 immVertex2f(pos, x1 + ofs, y1);
468         }
469         else {
470                 if (golden) {
471                         ofs = h * (1.0f - (1.0f / 1.61803399f));
472                 }
473                 else {
474                         ofs = w * (w / h);
475                 }
476                 if (dir == 'B') SWAP(float, x1, x2);
477
478                 immVertex2f(pos, x1, y1);
479                 immVertex2f(pos, x2, y2);
480
481                 immVertex2f(pos, x2, y1);
482                 immVertex2f(pos, x1, y1 + ofs);
483
484                 immVertex2f(pos, x1, y2);
485                 immVertex2f(pos, x2, y1 + (h - ofs));
486         }
487         immEnd();
488 }
489
490 static void drawviewborder(Scene *scene, ARegion *ar, View3D *v3d)
491 {
492         float x1, x2, y1, y2;
493         float x1i, x2i, y1i, y2i;
494
495         rctf viewborder;
496         Camera *ca = NULL;
497         RegionView3D *rv3d = ar->regiondata;
498
499         if (v3d->camera == NULL)
500                 return;
501         if (v3d->camera->type == OB_CAMERA)
502                 ca = v3d->camera->data;
503
504         ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &viewborder, false);
505         /* the offsets */
506         x1 = viewborder.xmin;
507         y1 = viewborder.ymin;
508         x2 = viewborder.xmax;
509         y2 = viewborder.ymax;
510
511         glLineWidth(1.0f);
512
513         /* apply offsets so the real 3D camera shows through */
514
515         /* note: quite un-scientific but without this bit extra
516          * 0.0001 on the lower left the 2D border sometimes
517          * obscures the 3D camera border */
518         /* note: with VIEW3D_CAMERA_BORDER_HACK defined this error isn't noticeable
519          * but keep it here in case we need to remove the workaround */
520         x1i = (int)(x1 - 1.0001f);
521         y1i = (int)(y1 - 1.0001f);
522         x2i = (int)(x2 + (1.0f - 0.0001f));
523         y2i = (int)(y2 + (1.0f - 0.0001f));
524
525         /* use the same program for everything */
526         unsigned pos = add_attrib(immVertexFormat(), "pos", COMP_F32, 2, KEEP_FLOAT);
527         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
528
529         /* passepartout, specified in camera edit buttons */
530         if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001f) {
531                 const float winx = (ar->winx + 1);
532                 const float winy = (ar->winy + 1);
533
534                 float alpha = 1.0f;
535
536                 if (ca->passepartalpha != 1.0f) {
537                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
538                         glEnable(GL_BLEND);
539                         alpha = ca->passepartalpha;
540                 }
541
542                 immUniformColor4f(0.0f, 0.0f, 0.0f, alpha);
543
544                 if (x1i > 0.0f)
545                         immRectf(pos, 0.0f, winy, x1i, 0.0f);
546                 if (x2i < winx)
547                         immRectf(pos, x2i, winy, winx, 0.0f);
548                 if (y2i < winy)
549                         immRectf(pos, x1i, winy, x2i, y2i);
550                 if (y2i > 0.0f)
551                         immRectf(pos, x1i, y1i, x2i, 0.0f);
552
553                 glDisable(GL_BLEND);
554         }
555
556         setlinestyle(0);
557
558         immUniformThemeColor(TH_BACK);
559         imm_draw_line_box(pos, x1i, y1i, x2i, y2i);
560
561 #ifdef VIEW3D_CAMERA_BORDER_HACK
562         if (view3d_camera_border_hack_test == true) {
563                 immUniformColor3ubv(view3d_camera_border_hack_col);
564                 imm_draw_line_box(pos, x1i + 1, y1i + 1, x2i - 1, y2i - 1);
565                 view3d_camera_border_hack_test = false;
566         }
567 #endif
568
569         setlinestyle(3);
570
571         /* outer line not to confuse with object selecton */
572         if (v3d->flag2 & V3D_LOCK_CAMERA) {
573                 immUniformThemeColor(TH_REDALERT);
574                 imm_draw_line_box(pos, x1i - 1, y1i - 1, x2i + 1, y2i + 1);
575         }
576
577         immUniformThemeColor(TH_VIEW_OVERLAY);
578         imm_draw_line_box(pos, x1i, y1i, x2i, y2i);
579
580         /* border */
581         if (scene->r.mode & R_BORDER) {
582                 float x3, y3, x4, y4;
583
584                 x3 = floorf(x1 + (scene->r.border.xmin * (x2 - x1))) - 1;
585                 y3 = floorf(y1 + (scene->r.border.ymin * (y2 - y1))) - 1;
586                 x4 = floorf(x1 + (scene->r.border.xmax * (x2 - x1))) + (U.pixelsize - 1);
587                 y4 = floorf(y1 + (scene->r.border.ymax * (y2 - y1))) + (U.pixelsize - 1);
588
589                 imm_cpack(0x4040FF);
590                 imm_draw_line_box(pos, x3, y3, x4, y4);
591         }
592         immUnbindProgram();
593
594         /* safety border */
595         if (ca) {
596                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
597                 if (ca->dtx & CAM_DTX_CENTER) {
598                         float x3, y3;
599
600                         x3 = x1 + 0.5f * (x2 - x1);
601                         y3 = y1 + 0.5f * (y2 - y1);
602
603                         immUniformThemeColorBlendShade(TH_VIEW_OVERLAY, TH_BACK, 0.25f, 0);
604                         immBegin(GL_LINES, 4);
605
606                         immVertex2f(pos, x1, y3);
607                         immVertex2f(pos, x2, y3);
608
609                         immVertex2f(pos, x3, y1);
610                         immVertex2f(pos, x3, y2);
611
612                         immEnd();
613                 }
614
615                 if (ca->dtx & CAM_DTX_CENTER_DIAG) {
616
617                         immUniformThemeColorBlendShade(TH_VIEW_OVERLAY, TH_BACK, 0.25f, 0);
618                         immBegin(GL_LINES, 4);
619
620                         immVertex2f(pos, x1, y1);
621                         immVertex2f(pos, x2, y2);
622
623                         immVertex2f(pos, x1, y2);
624                         immVertex2f(pos, x2, y1);
625
626                         immEnd();
627                 }
628
629                 if (ca->dtx & CAM_DTX_THIRDS) {
630                         immUniformThemeColorBlendShade(TH_VIEW_OVERLAY, TH_BACK, 0.25f, 0);
631                         drawviewborder_grid3(pos, x1, x2, y1, y2, 1.0f / 3.0f);
632                 }
633
634                 if (ca->dtx & CAM_DTX_GOLDEN) {
635                         immUniformThemeColorBlendShade(TH_VIEW_OVERLAY, TH_BACK, 0.25f, 0);
636                         drawviewborder_grid3(pos, x1, x2, y1, y2, 1.0f - (1.0f / 1.61803399f));
637                 }
638
639                 if (ca->dtx & CAM_DTX_GOLDEN_TRI_A) {
640                         immUniformThemeColorBlendShade(TH_VIEW_OVERLAY, TH_BACK, 0.25f, 0);
641                         drawviewborder_triangle(pos, x1, x2, y1, y2, 0, 'A');
642                 }
643
644                 if (ca->dtx & CAM_DTX_GOLDEN_TRI_B) {
645                         immUniformThemeColorBlendShade(TH_VIEW_OVERLAY, TH_BACK, 0.25f, 0);
646                         drawviewborder_triangle(pos, x1, x2, y1, y2, 0, 'B');
647                 }
648
649                 if (ca->dtx & CAM_DTX_HARMONY_TRI_A) {
650                         immUniformThemeColorBlendShade(TH_VIEW_OVERLAY, TH_BACK, 0.25f, 0);
651                         drawviewborder_triangle(pos, x1, x2, y1, y2, 1, 'A');
652                 }
653
654                 if (ca->dtx & CAM_DTX_HARMONY_TRI_B) {
655                         immUniformThemeColorBlendShade(TH_VIEW_OVERLAY, TH_BACK, 0.25f, 0);
656                         drawviewborder_triangle(pos, x1, x2, y1, y2, 1, 'B');
657                 }
658
659                 if (ca->flag & CAM_SHOW_SAFE_MARGINS) {
660                         UI_draw_safe_areas(
661                                 pos, x1, x2, y1, y2,
662                                 scene->safe_areas.title,
663                                 scene->safe_areas.action);
664
665                         if (ca->flag & CAM_SHOW_SAFE_CENTER) {
666                                 UI_draw_safe_areas(
667                                         pos, x1, x2, y1, y2,
668                                         scene->safe_areas.title_center,
669                                         scene->safe_areas.action_center);
670                         }
671                 }
672                 immUnbindProgram();
673
674                 if (ca->flag & CAM_SHOWSENSOR) {
675                         /* determine sensor fit, and get sensor x/y, for auto fit we
676                          * assume and square sensor and only use sensor_x */
677                         float sizex = scene->r.xsch * scene->r.xasp;
678                         float sizey = scene->r.ysch * scene->r.yasp;
679                         int sensor_fit = BKE_camera_sensor_fit(ca->sensor_fit, sizex, sizey);
680                         float sensor_x = ca->sensor_x;
681                         float sensor_y = (ca->sensor_fit == CAMERA_SENSOR_FIT_AUTO) ? ca->sensor_x : ca->sensor_y;
682
683                         /* determine sensor plane */
684                         rctf rect;
685
686                         if (sensor_fit == CAMERA_SENSOR_FIT_HOR) {
687                                 float sensor_scale = (x2i - x1i) / sensor_x;
688                                 float sensor_height = sensor_scale * sensor_y;
689
690                                 rect.xmin = x1i;
691                                 rect.xmax = x2i;
692                                 rect.ymin = (y1i + y2i) * 0.5f - sensor_height * 0.5f;
693                                 rect.ymax = rect.ymin + sensor_height;
694                         }
695                         else {
696                                 float sensor_scale = (y2i - y1i) / sensor_y;
697                                 float sensor_width = sensor_scale * sensor_x;
698
699                                 rect.xmin = (x1i + x2i) * 0.5f - sensor_width * 0.5f;
700                                 rect.xmax = rect.xmin + sensor_width;
701                                 rect.ymin = y1i;
702                                 rect.ymax = y2i;
703                         }
704
705                         /* draw */
706                         float color[4];
707                         UI_GetThemeColorShade4fv(TH_VIEW_OVERLAY, 100, color);
708                         UI_draw_roundbox_gl_mode(GL_LINE_LOOP, rect.xmin, rect.ymin, rect.xmax, rect.ymax, 2.0f, color);
709                 }
710         }
711
712         setlinestyle(0);
713
714         /* camera name - draw in highlighted text color */
715         if (ca && (ca->flag & CAM_SHOWNAME)) {
716                 UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
717                 BLF_draw_default(
718                         x1i, y1i - (0.7f * U.widget_unit), 0.0f,
719                         v3d->camera->id.name + 2, sizeof(v3d->camera->id.name) - 2);
720         }
721
722 }
723
724 static void drawrenderborder(ARegion *ar, View3D *v3d)
725 {
726         /* use the same program for everything */
727         unsigned pos = add_attrib(immVertexFormat(), "pos", COMP_F32, 2, KEEP_FLOAT);
728         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
729
730         glLineWidth(1.0f);
731         setlinestyle(3);
732         imm_cpack(0x4040FF);
733
734         imm_draw_line_box(
735             pos, v3d->render_border.xmin * ar->winx, v3d->render_border.ymin * ar->winy,
736             v3d->render_border.xmax * ar->winx, v3d->render_border.ymax * ar->winy);
737
738         setlinestyle(0);
739
740         immUnbindProgram();
741 }
742
743 /* ******************** offline engine ***************** */
744
745 static bool view3d_draw_render_draw(const bContext *C, Scene *scene,
746     ARegion *ar, View3D *UNUSED(v3d),
747     bool clip_border, const rcti *border_rect)
748 {
749         RegionView3D *rv3d = ar->regiondata;
750         RenderEngineType *type;
751         GLint scissor[4];
752
753         /* create render engine */
754         if (!rv3d->render_engine) {
755                 RenderEngine *engine;
756
757                 type = RE_engines_find(scene->r.engine);
758
759                 if (!(type->view_update && type->view_draw))
760                         return false;
761
762                 engine = RE_engine_create_ex(type, true);
763
764                 engine->tile_x = scene->r.tilex;
765                 engine->tile_y = scene->r.tiley;
766
767                 type->view_update(engine, C);
768
769                 rv3d->render_engine = engine;
770         }
771
772         /* background draw */
773         glMatrixMode(GL_PROJECTION);
774         glPushMatrix();
775         glMatrixMode(GL_MODELVIEW);
776         glPushMatrix();
777         ED_region_pixelspace(ar);
778
779         if (clip_border) {
780                 /* for border draw, we only need to clear a subset of the 3d view */
781                 if (border_rect->xmax > border_rect->xmin && border_rect->ymax > border_rect->ymin) {
782                         glGetIntegerv(GL_SCISSOR_BOX, scissor);
783                         glScissor(border_rect->xmin, border_rect->ymin,
784                                 BLI_rcti_size_x(border_rect), BLI_rcti_size_y(border_rect));
785                 }
786                 else {
787                         return false;
788                 }
789         }
790
791         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
792         /* don't change depth buffer */
793         glClear(GL_COLOR_BUFFER_BIT); /* is this necessary? -- merwin */
794
795         /* render result draw */
796         type = rv3d->render_engine->type;
797         type->view_draw(rv3d->render_engine, C);
798
799         if (clip_border) {
800                 /* restore scissor as it was before */
801                 glScissor(scissor[0], scissor[1], scissor[2], scissor[3]);
802         }
803
804         glMatrixMode(GL_PROJECTION);
805         glPopMatrix();
806         glMatrixMode(GL_MODELVIEW);
807         glPopMatrix();
808
809         return true;
810 }
811
812 /* ******************** background plates ***************** */
813
814 static void view3d_draw_background_gradient(void)
815 {
816         gpuMatrixBegin3D(); /* TODO: finish 2D API */
817
818         glClear(GL_DEPTH_BUFFER_BIT);
819
820         VertexFormat *format = immVertexFormat();
821         unsigned pos = add_attrib(format, "pos", COMP_F32, 2, KEEP_FLOAT);
822         unsigned color = add_attrib(format, "color", COMP_U8, 3, NORMALIZE_INT_TO_FLOAT);
823         unsigned char col_hi[3], col_lo[3];
824
825         immBindBuiltinProgram(GPU_SHADER_2D_SMOOTH_COLOR);
826
827         UI_GetThemeColor3ubv(TH_LOW_GRAD, col_lo);
828         UI_GetThemeColor3ubv(TH_HIGH_GRAD, col_hi);
829
830         immBegin(GL_QUADS, 4);
831         immAttrib3ubv(color, col_lo);
832         immVertex2f(pos, -1.0f, -1.0f);
833         immVertex2f(pos, 1.0f, -1.0f);
834
835         immAttrib3ubv(color, col_hi);
836         immVertex2f(pos, 1.0f, 1.0f);
837         immVertex2f(pos, -1.0f, 1.0f);
838         immEnd();
839
840         immUnbindProgram();
841
842         gpuMatrixEnd();
843 }
844
845 static void view3d_draw_background_none(void)
846 {
847         if (UI_GetThemeValue(TH_SHOW_BACK_GRAD)) {
848                 view3d_draw_background_gradient();
849         }
850         else {
851                 UI_ThemeClearColorAlpha(TH_HIGH_GRAD, 1.0f);
852                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
853         }
854 }
855
856 static void view3d_draw_background_world(Scene *scene, View3D *v3d, RegionView3D *rv3d)
857 {
858         if (scene->world) {
859                 GPUMaterial *gpumat = GPU_material_world(scene, scene->world);
860
861                 /* calculate full shader for background */
862                 GPU_material_bind(gpumat, 1, 1, 1.0f, false, rv3d->viewmat, rv3d->viewinv, rv3d->viewcamtexcofac, (v3d->scenelock != 0));
863
864                 if (GPU_material_bound(gpumat)) {
865
866                         glClear(GL_DEPTH_BUFFER_BIT);
867
868                         /* TODO viewport (dfelinto): GPU_material_bind relies on immediate mode,
869                         * we can't get rid of the following code without a bigger refactor
870                         * or we dropping this functionality. */
871
872                         glBegin(GL_TRIANGLE_STRIP);
873                         glVertex2f(-1.0f, -1.0f);
874                         glVertex2f(1.0f, -1.0f);
875                         glVertex2f(-1.0f, 1.0f);
876                         glVertex2f(1.0f, 1.0f);
877                         glEnd();
878
879                         GPU_material_unbind(gpumat);
880                 }
881                 else {
882                         view3d_draw_background_none();
883                 }
884         }
885         else {
886                 view3d_draw_background_none();
887         }
888 }
889
890 /* ******************** solid plates ***************** */
891
892 /**
893  * Clear the buffer and draw the proper shader
894  */
895 static void view3d_draw_background(const bContext *C)
896 {
897         Scene *scene = CTX_data_scene(C);
898         View3D *v3d = CTX_wm_view3d(C);
899         RegionView3D *rv3d = CTX_wm_region_view3d(C);
900
901         glDisable(GL_DEPTH_TEST);
902         glDepthMask(GL_TRUE);
903         /* Background functions do not read or write depth, but they do clear or completely
904          * overwrite color buffer. It's more efficient to clear color & depth in once call, so
905          * background functions do this even though they don't use depth.
906          */
907
908         switch (v3d->debug.background) {
909                 case V3D_DEBUG_BACKGROUND_WORLD:
910                         view3d_draw_background_world(scene, v3d, rv3d);
911                         break;
912                 case V3D_DEBUG_BACKGROUND_GRADIENT:
913                         view3d_draw_background_gradient();
914                         break;
915                 case V3D_DEBUG_BACKGROUND_NONE:
916                 default:
917                         view3d_draw_background_none();
918                         break;
919         }
920 }
921
922 /**
923  *
924  */
925 static void view3d_draw_render_solid_surfaces(const bContext *C, ARegion *ar, const bool UNUSED(run_screen_shaders))
926 {
927         /* TODO viewport */
928         draw_all_objects(C, ar, false, use_depth(C));
929 }
930
931 /**
932  *
933  */
934 static void view3d_draw_render_transparent_surfaces(const bContext *UNUSED(C))
935 {
936         /* TODO viewport */
937 }
938
939 /**
940  *
941  */
942 static void view3d_draw_post_draw(const bContext *UNUSED(C))
943 {
944         /* TODO viewport */
945 }
946
947 /* ******************** geometry overlay ***************** */
948
949 /**
950  * Front/back wire frames
951  */
952 static void view3d_draw_wire_plates(const bContext *UNUSED(C))
953 {
954         /* TODO viewport */
955 }
956
957 /**
958  * Special treatment for selected objects
959  */
960 static void view3d_draw_outline_plates(const bContext *UNUSED(C))
961 {
962         /* TODO viewport */
963 }
964
965 /* ******************** other elements ***************** */
966
967
968 #define DEBUG_GRID 0
969
970 static void gridline_range(double x0, double dx, double max, int *r_first, int *r_count)
971 {
972         /* determine range of gridlines that appear in this Area -- similar calc but separate ranges for x & y
973         * x0 is gridline 0, the axis in screen space
974         * Area covers [0 .. max) pixels */
975
976         int first = (int)ceil(-x0 / dx);
977         int last = (int)floor((max - x0) / dx);
978
979         if (first <= last) {
980                 *r_first = first;
981                 *r_count = last - first + 1;
982         }
983         else {
984                 *r_first = 0;
985                 *r_count = 0;
986         }
987
988 #if DEBUG_GRID
989         printf("   first %d * dx = %f\n", first, x0 + first * dx);
990         printf("   last %d * dx = %f\n", last, x0 + last * dx);
991         printf("   count = %d\n", *count_out);
992 #endif
993 }
994
995 static int gridline_count(ARegion *ar, double x0, double y0, double dx)
996 {
997         /* x0 & y0 establish the "phase" of the grid within this 2D region
998         * dx is the frequency, shared by x & y directions
999         * pass in dx of smallest (highest precision) grid we want to draw */
1000
1001 #if DEBUG_GRID
1002         printf("  %s(%f, %f, dx:%f)\n", __FUNCTION__, x0, y0, dx);
1003 #endif
1004
1005         int first, x_ct, y_ct;
1006
1007         gridline_range(x0, dx, ar->winx, &first, &x_ct);
1008         gridline_range(y0, dx, ar->winy, &first, &y_ct);
1009
1010         int total_ct = x_ct + y_ct;
1011
1012 #if DEBUG_GRID
1013         printf("   %d + %d = %d gridlines\n", x_ct, y_ct, total_ct);
1014 #endif
1015
1016         return total_ct;
1017 }
1018
1019 static bool drawgrid_draw(ARegion *ar, double x0, double y0, double dx, int skip_mod, unsigned pos, unsigned col, GLubyte col_value[3])
1020 {
1021         /* skip every skip_mod lines relative to each axis; they will be overlaid by another drawgrid_draw
1022         * always skip exact x0 & y0 axes; they will be drawn later in color
1023         *
1024         * set grid color once, just before the first line is drawn
1025         * it's harmless to set same color for every line, or every vertex
1026         * but if no lines are drawn, color must not be set! */
1027
1028 #if DEBUG_GRID
1029         printf("  %s(%f, %f, dx:%f, skip_mod:%d)\n", __FUNCTION__, x0, y0, dx, skip_mod);
1030 #endif
1031
1032         const float x_max = (float)ar->winx;
1033         const float y_max = (float)ar->winy;
1034
1035         int first, ct;
1036         int x_ct = 0, y_ct = 0; /* count of lines actually drawn */
1037         int lines_skipped_for_next_unit = 0;
1038
1039         /* draw vertical lines */
1040         gridline_range(x0, dx, x_max, &first, &ct);
1041
1042         for (int i = first; i < first + ct; ++i) {
1043                 if (i == 0)
1044                         continue;
1045                 else if (skip_mod && (i % skip_mod) == 0) {
1046                         ++lines_skipped_for_next_unit;
1047                         continue;
1048                 }
1049
1050                 if (x_ct == 0)
1051                         immAttrib3ub(col, col_value[0], col_value[1], col_value[2]);
1052
1053                 float x = (float)(x0 + i * dx);
1054                 immVertex2f(pos, x, 0.0f);
1055                 immVertex2f(pos, x, y_max);
1056                 ++x_ct;
1057         }
1058
1059         /* draw horizontal lines */
1060         gridline_range(y0, dx, y_max, &first, &ct);
1061
1062         for (int i = first; i < first + ct; ++i) {
1063                 if (i == 0)
1064                         continue;
1065                 else if (skip_mod && (i % skip_mod) == 0) {
1066                         ++lines_skipped_for_next_unit;
1067                         continue;
1068                 }
1069
1070                 if (x_ct + y_ct == 0)
1071                         immAttrib3ub(col, col_value[0], col_value[1], col_value[2]);
1072
1073                 float y = (float)(y0 + i * dx);
1074                 immVertex2f(pos, 0.0f, y);
1075                 immVertex2f(pos, x_max, y);
1076                 ++y_ct;
1077         }
1078
1079 #if DEBUG_GRID
1080         int total_ct = x_ct + y_ct;
1081         printf("    %d + %d = %d gridlines drawn, %d skipped for next unit\n", x_ct, y_ct, total_ct, lines_skipped_for_next_unit);
1082 #endif
1083
1084         return lines_skipped_for_next_unit > 0;
1085 }
1086
1087 #define GRID_MIN_PX_D 6.0
1088 #define GRID_MIN_PX_F 6.0f
1089
1090 static void drawgrid(UnitSettings *unit, ARegion *ar, View3D *v3d, const char **grid_unit)
1091 {
1092         RegionView3D *rv3d = ar->regiondata;
1093
1094 #if DEBUG_GRID
1095         printf("%s width %d, height %d\n", __FUNCTION__, ar->winx, ar->winy);
1096 #endif
1097
1098         double fx = rv3d->persmat[3][0];
1099         double fy = rv3d->persmat[3][1];
1100         double fw = rv3d->persmat[3][3];
1101
1102         const double wx = 0.5 * ar->winx;  /* use double precision to avoid rounding errors */
1103         const double wy = 0.5 * ar->winy;
1104
1105         double x = wx * fx / fw;
1106         double y = wy * fy / fw;
1107
1108         double vec4[4] = { v3d->grid, v3d->grid, 0.0, 1.0 };
1109         mul_m4_v4d(rv3d->persmat, vec4);
1110         fx = vec4[0];
1111         fy = vec4[1];
1112         fw = vec4[3];
1113
1114         double dx = fabs(x - wx * fx / fw);
1115         if (dx == 0) dx = fabs(y - wy * fy / fw);
1116
1117         x += wx;
1118         y += wy;
1119
1120         /* now x, y, and dx have their final values
1121         * (x,y) is the world origin (0,0,0) mapped to Area-relative screen space
1122         * dx is the distance in pixels between grid lines -- same for horiz or vert grid lines */
1123
1124         glLineWidth(1.0f);
1125
1126 #if 0 /* TODO: write to UI/widget depth buffer, not scene depth */
1127         glDepthMask(GL_FALSE);  /* disable write in zbuffer */
1128 #endif
1129
1130         VertexFormat *format = immVertexFormat();
1131         unsigned int pos = add_attrib(format, "pos", COMP_F32, 2, KEEP_FLOAT);
1132         unsigned int color = add_attrib(format, "color", COMP_U8, 3, NORMALIZE_INT_TO_FLOAT);
1133
1134         immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
1135
1136         unsigned char col[3], col2[3];
1137         UI_GetThemeColor3ubv(TH_GRID, col);
1138
1139         if (unit->system) {
1140                 const void *usys;
1141                 int len;
1142
1143                 bUnit_GetSystem(unit->system, B_UNIT_LENGTH, &usys, &len);
1144
1145                 bool first = true;
1146
1147                 if (usys) {
1148                         int i = len;
1149                         while (i--) {
1150                                 double scalar = bUnit_GetScaler(usys, i);
1151
1152                                 double dx_scalar = dx * scalar / (double)unit->scale_length;
1153                                 if (dx_scalar < (GRID_MIN_PX_D * 2.0)) {
1154                                         /* very very small grid items are less useful when dealing with units */
1155                                         continue;
1156                                 }
1157
1158                                 if (first) {
1159                                         first = false;
1160
1161                                         /* Store the smallest drawn grid size units name so users know how big each grid cell is */
1162                                         *grid_unit = bUnit_GetNameDisplay(usys, i);
1163                                         rv3d->gridview = (float)((scalar * (double)v3d->grid) / (double)unit->scale_length);
1164
1165                                         int gridline_ct = gridline_count(ar, x, y, dx_scalar);
1166                                         if (gridline_ct == 0)
1167                                                 goto drawgrid_cleanup; /* nothing to draw */
1168
1169                                         immBegin(GL_LINES, gridline_ct * 2);
1170                                 }
1171
1172                                 float blend_fac = 1.0f - ((GRID_MIN_PX_F * 2.0f) / (float)dx_scalar);
1173                                 /* tweak to have the fade a bit nicer */
1174                                 blend_fac = (blend_fac * blend_fac) * 2.0f;
1175                                 CLAMP(blend_fac, 0.3f, 1.0f);
1176
1177                                 UI_GetThemeColorBlend3ubv(TH_HIGH_GRAD, TH_GRID, blend_fac, col2);
1178
1179                                 const int skip_mod = (i == 0) ? 0 : (int)round(bUnit_GetScaler(usys, i - 1) / scalar);
1180 #if DEBUG_GRID
1181                                 printf("%s %f, ", bUnit_GetNameDisplay(usys, i), scalar);
1182                                 if (i > 0)
1183                                         printf("next unit is %d times larger\n", skip_mod);
1184                                 else
1185                                         printf("largest unit\n");
1186 #endif
1187                                 if (!drawgrid_draw(ar, x, y, dx_scalar, skip_mod, pos, color, col2))
1188                                         break;
1189                         }
1190                 }
1191         }
1192         else {
1193                 const double sublines = v3d->gridsubdiv;
1194                 const float  sublines_fl = v3d->gridsubdiv;
1195
1196                 int grids_to_draw = 2; /* first the faint fine grid, then the bold coarse grid */
1197
1198                 if (dx < GRID_MIN_PX_D) {
1199                         rv3d->gridview *= sublines_fl;
1200                         dx *= sublines;
1201                         if (dx < GRID_MIN_PX_D) {
1202                                 rv3d->gridview *= sublines_fl;
1203                                 dx *= sublines;
1204                                 if (dx < GRID_MIN_PX_D) {
1205                                         rv3d->gridview *= sublines_fl;
1206                                         dx *= sublines;
1207                                         grids_to_draw = (dx < GRID_MIN_PX_D) ? 0 : 1;
1208                                 }
1209                         }
1210                 }
1211                 else {
1212                         if (dx > (GRID_MIN_PX_D * 10.0)) {  /* start blending in */
1213                                 rv3d->gridview /= sublines_fl;
1214                                 dx /= sublines;
1215                                 if (dx > (GRID_MIN_PX_D * 10.0)) {  /* start blending in */
1216                                         rv3d->gridview /= sublines_fl;
1217                                         dx /= sublines;
1218                                         if (dx > (GRID_MIN_PX_D * 10.0)) {
1219                                                 grids_to_draw = 1;
1220                                         }
1221                                 }
1222                         }
1223                 }
1224
1225                 int gridline_ct = gridline_count(ar, x, y, dx);
1226                 if (gridline_ct == 0)
1227                         goto drawgrid_cleanup; /* nothing to draw */
1228
1229                 immBegin(GL_LINES, gridline_ct * 2);
1230
1231                 if (grids_to_draw == 2) {
1232                         UI_GetThemeColorBlend3ubv(TH_HIGH_GRAD, TH_GRID, dx / (GRID_MIN_PX_D * 6.0), col2);
1233                         if (drawgrid_draw(ar, x, y, dx, v3d->gridsubdiv, pos, color, col2))
1234                                 drawgrid_draw(ar, x, y, dx * sublines, 0, pos, color, col);
1235                 }
1236                 else if (grids_to_draw == 1) {
1237                         drawgrid_draw(ar, x, y, dx, 0, pos, color, col);
1238                 }
1239         }
1240
1241         /* draw visible axes */
1242         /* horizontal line */
1243         if (0 <= y && y < ar->winy) {
1244                 UI_make_axis_color(col, col2, ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT) ? 'Y' : 'X');
1245                 immAttrib3ub(color, col2[0], col2[1], col2[2]);
1246                 immVertex2f(pos, 0.0f, y);
1247                 immVertex2f(pos, (float)ar->winx, y);
1248         }
1249
1250         /* vertical line */
1251         if (0 <= x && x < ar->winx) {
1252                 UI_make_axis_color(col, col2, ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM) ? 'Y' : 'Z');
1253                 immAttrib3ub(color, col2[0], col2[1], col2[2]);
1254                 immVertex2f(pos, x, 0.0f);
1255                 immVertex2f(pos, x, (float)ar->winy);
1256         }
1257
1258         immEnd();
1259
1260 drawgrid_cleanup:
1261         immUnbindProgram();
1262
1263 #if 0 /* depth write is left enabled above */
1264         glDepthMask(GL_TRUE);  /* enable write in zbuffer */
1265 #endif
1266 }
1267
1268 #undef DEBUG_GRID
1269 #undef GRID_MIN_PX_D
1270 #undef GRID_MIN_PX_F
1271
1272 static void drawfloor(Scene *scene, View3D *v3d, const char **grid_unit, bool write_depth)
1273 {
1274         /* draw only if there is something to draw */
1275         if (v3d->gridflag & (V3D_SHOW_FLOOR | V3D_SHOW_X | V3D_SHOW_Y | V3D_SHOW_Z)) {
1276                 /* draw how many lines?
1277                 * trunc(v3d->gridlines / 2) * 4
1278                 * + 2 for xy axes (possibly with special colors)
1279                 * + 1 for z axis (the only line not in xy plane)
1280                 * even v3d->gridlines are honored, odd rounded down */
1281                 const int gridlines = v3d->gridlines / 2;
1282                 const float grid_scale = ED_view3d_grid_scale(scene, v3d, grid_unit);
1283                 const float grid = gridlines * grid_scale;
1284
1285                 const bool show_floor = (v3d->gridflag & V3D_SHOW_FLOOR) && gridlines >= 1;
1286
1287                 bool show_axis_x = v3d->gridflag & V3D_SHOW_X;
1288                 bool show_axis_y = v3d->gridflag & V3D_SHOW_Y;
1289                 bool show_axis_z = v3d->gridflag & V3D_SHOW_Z;
1290
1291                 unsigned char col_grid[3], col_axis[3];
1292
1293                 glLineWidth(1.0f);
1294
1295                 UI_GetThemeColor3ubv(TH_GRID, col_grid);
1296
1297                 if (!write_depth)
1298                         glDepthMask(GL_FALSE);
1299
1300                 if (show_floor) {
1301                         const unsigned vertex_ct = 2 * (gridlines * 4 + 2);
1302                         const int sublines = v3d->gridsubdiv;
1303
1304                         unsigned char col_bg[3], col_grid_emphasise[3], col_grid_light[3];
1305
1306                         VertexFormat *format = immVertexFormat();
1307                         unsigned int pos = add_attrib(format, "pos", COMP_F32, 2, KEEP_FLOAT);
1308                         unsigned int color = add_attrib(format, "color", COMP_U8, 3, NORMALIZE_INT_TO_FLOAT);
1309
1310                         immBindBuiltinProgram(GPU_SHADER_3D_FLAT_COLOR);
1311
1312                         immBegin(GL_LINES, vertex_ct);
1313
1314                         /* draw normal grid lines */
1315                         UI_GetColorPtrShade3ubv(col_grid, col_grid_light, 10);
1316
1317                         for (int a = 1; a <= gridlines; a++) {
1318                                 /* skip emphasised divider lines */
1319                                 if (a % sublines != 0) {
1320                                         const float line = a * grid_scale;
1321
1322                                         immAttrib3ubv(color, col_grid_light);
1323
1324                                         immVertex2f(pos, -grid, -line);
1325                                         immVertex2f(pos, +grid, -line);
1326                                         immVertex2f(pos, -grid, +line);
1327                                         immVertex2f(pos, +grid, +line);
1328
1329                                         immVertex2f(pos, -line, -grid);
1330                                         immVertex2f(pos, -line, +grid);
1331                                         immVertex2f(pos, +line, -grid);
1332                                         immVertex2f(pos, +line, +grid);
1333                                 }
1334                         }
1335
1336                         /* draw emphasised grid lines */
1337                         UI_GetThemeColor3ubv(TH_BACK, col_bg);
1338                         /* emphasise division lines lighter instead of darker, if background is darker than grid */
1339                         UI_GetColorPtrShade3ubv(col_grid, col_grid_emphasise,
1340                                 (col_grid[0] + col_grid[1] + col_grid[2] + 30 >
1341                                 col_bg[0] + col_bg[1] + col_bg[2]) ? 20 : -10);
1342
1343                         if (sublines <= gridlines) {
1344                                 immAttrib3ubv(color, col_grid_emphasise);
1345
1346                                 for (int a = sublines; a <= gridlines; a += sublines) {
1347                                         const float line = a * grid_scale;
1348
1349                                         immVertex2f(pos, -grid, -line);
1350                                         immVertex2f(pos, +grid, -line);
1351                                         immVertex2f(pos, -grid, +line);
1352                                         immVertex2f(pos, +grid, +line);
1353
1354                                         immVertex2f(pos, -line, -grid);
1355                                         immVertex2f(pos, -line, +grid);
1356                                         immVertex2f(pos, +line, -grid);
1357                                         immVertex2f(pos, +line, +grid);
1358                                 }
1359                         }
1360
1361                         /* draw X axis */
1362                         if (show_axis_x) {
1363                                 show_axis_x = false; /* drawing now, won't need to draw later */
1364                                 UI_make_axis_color(col_grid, col_axis, 'X');
1365                                 immAttrib3ubv(color, col_axis);
1366                         }
1367                         else
1368                                 immAttrib3ubv(color, col_grid_emphasise);
1369
1370                         immVertex2f(pos, -grid, 0.0f);
1371                         immVertex2f(pos, +grid, 0.0f);
1372
1373                         /* draw Y axis */
1374                         if (show_axis_y) {
1375                                 show_axis_y = false; /* drawing now, won't need to draw later */
1376                                 UI_make_axis_color(col_grid, col_axis, 'Y');
1377                                 immAttrib3ubv(color, col_axis);
1378                         }
1379                         else
1380                                 immAttrib3ubv(color, col_grid_emphasise);
1381
1382                         immVertex2f(pos, 0.0f, -grid);
1383                         immVertex2f(pos, 0.0f, +grid);
1384
1385                         immEnd();
1386                         immUnbindProgram();
1387
1388                         /* done with XY plane */
1389                 }
1390
1391                 if (show_axis_x || show_axis_y || show_axis_z) {
1392                         /* draw axis lines -- sometimes grid floor is off, other times we still need to draw the Z axis */
1393
1394                         VertexFormat *format = immVertexFormat();
1395                         unsigned int pos = add_attrib(format, "pos", COMP_F32, 3, KEEP_FLOAT);
1396                         unsigned int color = add_attrib(format, "color", COMP_U8, 3, NORMALIZE_INT_TO_FLOAT);
1397
1398                         immBindBuiltinProgram(GPU_SHADER_3D_FLAT_COLOR);
1399                         immBegin(GL_LINES, (show_axis_x + show_axis_y + show_axis_z) * 2);
1400
1401                         if (show_axis_x) {
1402                                 UI_make_axis_color(col_grid, col_axis, 'X');
1403                                 immAttrib3ubv(color, col_axis);
1404                                 immVertex3f(pos, -grid, 0.0f, 0.0f);
1405                                 immVertex3f(pos, +grid, 0.0f, 0.0f);
1406                         }
1407
1408                         if (show_axis_y) {
1409                                 UI_make_axis_color(col_grid, col_axis, 'Y');
1410                                 immAttrib3ubv(color, col_axis);
1411                                 immVertex3f(pos, 0.0f, -grid, 0.0f);
1412                                 immVertex3f(pos, 0.0f, +grid, 0.0f);
1413                         }
1414
1415                         if (show_axis_z) {
1416                                 UI_make_axis_color(col_grid, col_axis, 'Z');
1417                                 immAttrib3ubv(color, col_axis);
1418                                 immVertex3f(pos, 0.0f, 0.0f, -grid);
1419                                 immVertex3f(pos, 0.0f, 0.0f, +grid);
1420                         }
1421
1422                         immEnd();
1423                         immUnbindProgram();
1424                 }
1425
1426                 if (!write_depth)
1427                         glDepthMask(GL_TRUE);
1428         }
1429 }
1430
1431 /** could move this elsewhere, but tied into #ED_view3d_grid_scale */
1432 float ED_scene_grid_scale(Scene *scene, const char **grid_unit)
1433 {
1434         /* apply units */
1435         if (scene->unit.system) {
1436                 const void *usys;
1437                 int len;
1438
1439                 bUnit_GetSystem(scene->unit.system, B_UNIT_LENGTH, &usys, &len);
1440
1441                 if (usys) {
1442                         int i = bUnit_GetBaseUnit(usys);
1443                         if (grid_unit)
1444                                 *grid_unit = bUnit_GetNameDisplay(usys, i);
1445                         return (float)bUnit_GetScaler(usys, i) / scene->unit.scale_length;
1446                 }
1447         }
1448
1449         return 1.0f;
1450 }
1451
1452 float ED_view3d_grid_scale(Scene *scene, View3D *v3d, const char **grid_unit)
1453 {
1454         return v3d->grid * ED_scene_grid_scale(scene, grid_unit);
1455 }
1456
1457
1458 static void view3d_draw_grid(const bContext *C, ARegion *ar)
1459 {
1460         /* TODO viewport
1461          * Missing is the flags to check whether to draw it
1462          * for now now we are using the flags in v3d itself.
1463          *
1464          * Also for now always assume depth is there, so we
1465          * draw on top of it.
1466          */
1467         /**
1468          * Calculate pixel-size factor once, is used for lamps and object centers.
1469          * Used by #ED_view3d_pixel_size and typically not accessed directly.
1470          *
1471          * \note #BKE_camera_params_compute_viewplane' also calculates a pixel-size value,
1472          * passed to #RE_SetPixelSize, in ortho mode this is compatible with this value,
1473          * but in perspective mode its offset by the near-clip.
1474          *
1475          * 'RegionView3D.pixsize' is used for viewport drawing, not rendering.
1476          */
1477         Scene *scene = CTX_data_scene(C);
1478         View3D *v3d = CTX_wm_view3d(C);
1479         RegionView3D *rv3d = ar->regiondata;
1480
1481         const bool draw_floor = (rv3d->view == RV3D_VIEW_USER) || (rv3d->persp != RV3D_ORTHO);
1482         const char *grid_unit = NULL;
1483
1484         /* ortho grid goes first, does not write to depth buffer and doesn't need depth test so it will override
1485          * objects if done last
1486          * needs to be done always, gridview is adjusted in drawgrid() now, but only for ortho views.
1487          */
1488         rv3d->gridview = ED_view3d_grid_scale(scene, v3d, &grid_unit);
1489
1490         glEnable(GL_DEPTH_TEST);
1491         glDepthMask(GL_FALSE); /* read & test depth, but don't alter it. TODO: separate UI depth buffer */
1492
1493         if (!draw_floor) {
1494                 ED_region_pixelspace(ar);
1495                 *(&grid_unit) = NULL;  /* drawgrid need this to detect/affect smallest valid unit... */
1496                 drawgrid(&scene->unit, ar, v3d, &grid_unit);
1497
1498                 glMatrixMode(GL_PROJECTION);
1499                 glLoadMatrixf(rv3d->winmat);
1500                 glMatrixMode(GL_MODELVIEW);
1501                 glLoadMatrixf(rv3d->viewmat);
1502         }
1503         else {
1504                 drawfloor(scene, v3d, &grid_unit, false);
1505         }
1506
1507         glDisable(GL_DEPTH_TEST);
1508 }
1509
1510 static bool is_cursor_visible(Scene *scene, SceneLayer *sl)
1511 {
1512         Object *ob = OBACT_NEW;
1513
1514         /* don't draw cursor in paint modes, but with a few exceptions */
1515         if (ob && ob->mode & OB_MODE_ALL_PAINT) {
1516                 /* exception: object is in weight paint and has deforming armature in pose mode */
1517                 if (ob->mode & OB_MODE_WEIGHT_PAINT) {
1518                         if (BKE_object_pose_armature_get(ob) != NULL) {
1519                                 return true;
1520                         }
1521                 }
1522                 /* exception: object in texture paint mode, clone brush, use_clone_layer disabled */
1523                 else if (ob->mode & OB_MODE_TEXTURE_PAINT) {
1524                         const Paint *p = BKE_paint_get_active(scene);
1525
1526                         if (p && p->brush && p->brush->imagepaint_tool == PAINT_TOOL_CLONE) {
1527                                 if ((scene->toolsettings->imapaint.flag & IMAGEPAINT_PROJECT_LAYER_CLONE) == 0) {
1528                                         return true;
1529                                 }
1530                         }
1531                 }
1532
1533                 /* no exception met? then don't draw cursor! */
1534                 return false;
1535         }
1536
1537         return true;
1538 }
1539
1540 static void drawcursor(Scene *scene, ARegion *ar, View3D *v3d)
1541 {
1542         int co[2];
1543
1544         /* we don't want the clipping for cursor */
1545         if (ED_view3d_project_int_global(ar, ED_view3d_cursor3d_get(scene, v3d), co, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
1546                 const float f5 = 0.25f * U.widget_unit;
1547                 const float f10 = 0.5f * U.widget_unit;
1548                 const float f20 = U.widget_unit;
1549                 
1550                 glLineWidth(1.0f);
1551
1552                 VertexFormat *format = immVertexFormat();
1553                 unsigned int pos = add_attrib(format, "pos", COMP_F32, 2, KEEP_FLOAT);
1554                 unsigned int color = add_attrib(format, "color", COMP_U8, 3, NORMALIZE_INT_TO_FLOAT);
1555
1556                 immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
1557
1558                 const int segments = 16;
1559
1560                 immBegin(GL_LINE_LOOP, segments);
1561
1562                 for (int i = 0; i < segments; ++i) {
1563                         float angle = 2 * M_PI * ((float)i / (float)segments);
1564                         float x = co[0] + f10 * cosf(angle);
1565                         float y = co[1] + f10 * sinf(angle);
1566
1567                         if (i % 2 == 0)
1568                                 immAttrib3ub(color, 255, 0, 0);
1569                         else
1570                                 immAttrib3ub(color, 255, 255, 255);
1571
1572                         immVertex2f(pos, x, y);
1573                 }
1574                 immEnd();
1575
1576                 immUnbindProgram();
1577
1578                 VertexFormat_clear(format);
1579                 pos = add_attrib(format, "pos", COMP_F32, 2, KEEP_FLOAT);
1580
1581                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
1582
1583                 unsigned char crosshair_color[3];
1584                 UI_GetThemeColor3ubv(TH_VIEW_OVERLAY, crosshair_color);
1585                 immUniformColor3ubv(crosshair_color);
1586
1587                 immBegin(GL_LINES, 8);
1588                 immVertex2f(pos, co[0] - f20, co[1]);
1589                 immVertex2f(pos, co[0] - f5, co[1]);
1590                 immVertex2f(pos, co[0] + f5, co[1]);
1591                 immVertex2f(pos, co[0] + f20, co[1]);
1592                 immVertex2f(pos, co[0], co[1] - f20);
1593                 immVertex2f(pos, co[0], co[1] - f5);
1594                 immVertex2f(pos, co[0], co[1] + f5);
1595                 immVertex2f(pos, co[0], co[1] + f20);
1596                 immEnd();
1597
1598                 immUnbindProgram();
1599         }
1600 }
1601
1602 static void draw_view_axis(RegionView3D *rv3d, rcti *rect)
1603 {
1604         const float k = U.rvisize * U.pixelsize;  /* axis size */
1605         const int bright = - 20 * (10 - U.rvibright);  /* axis alpha offset (rvibright has range 0-10) */
1606
1607         const float startx = rect->xmin + k + 1.0f;  /* axis center in screen coordinates, x=y */
1608         const float starty = rect->ymin + k + 1.0f;
1609
1610         float axis_pos[3][2];
1611         unsigned char axis_col[3][4];
1612
1613         int axis_order[3] = {0, 1, 2};
1614         axis_sort_v3(rv3d->viewinv[2], axis_order);
1615
1616         for (int axis_i = 0; axis_i < 3; axis_i++) {
1617                 int i = axis_order[axis_i];
1618
1619                 /* get position of each axis tip on screen */
1620                 float vec[3] = { 0.0f };
1621                 vec[i] = 1.0f;
1622                 mul_qt_v3(rv3d->viewquat, vec);
1623                 axis_pos[i][0] = startx + vec[0] * k;
1624                 axis_pos[i][1] = starty + vec[1] * k;
1625
1626                 /* get color of each axis */
1627                 UI_GetThemeColorShade3ubv(TH_AXIS_X + i, bright, axis_col[i]); /* rgb */
1628                 axis_col[i][3] = 255 * hypotf(vec[0], vec[1]); /* alpha */
1629         }
1630
1631         /* draw axis lines */
1632         glLineWidth(2.0f);
1633         glEnable(GL_LINE_SMOOTH);
1634         glEnable(GL_BLEND);
1635         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1636
1637         VertexFormat *format = immVertexFormat();
1638         unsigned pos = add_attrib(format, "pos", COMP_F32, 2, KEEP_FLOAT);
1639         unsigned col = add_attrib(format, "color", COMP_U8, 4, NORMALIZE_INT_TO_FLOAT);
1640
1641         immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
1642         immBegin(GL_LINES, 6);
1643
1644         for (int axis_i = 0; axis_i < 3; axis_i++) {
1645                 int i = axis_order[axis_i];
1646
1647                 immAttrib4ubv(col, axis_col[i]);
1648                 immVertex2f(pos, startx, starty);
1649                 immVertex2fv(pos, axis_pos[i]);
1650         }
1651
1652         immEnd();
1653         immUnbindProgram();
1654         glDisable(GL_LINE_SMOOTH);
1655
1656         /* draw axis names */
1657         for (int axis_i = 0; axis_i < 3; axis_i++) {
1658                 int i = axis_order[axis_i];
1659
1660                 const char axis_text[2] = {'x' + i, '\0'};
1661                 BLF_color4ubv(BLF_default(), axis_col[i]);
1662                 BLF_draw_default_ascii(axis_pos[i][0] + 2, axis_pos[i][1] + 2, 0.0f, axis_text, 1);
1663         }
1664 }
1665
1666 #ifdef WITH_INPUT_NDOF
1667 /* draw center and axis of rotation for ongoing 3D mouse navigation */
1668 static void draw_rotation_guide(RegionView3D *rv3d)
1669 {
1670         float o[3];    /* center of rotation */
1671         float end[3];  /* endpoints for drawing */
1672
1673         GLubyte color[4] = {0, 108, 255, 255};  /* bright blue so it matches device LEDs */
1674
1675         negate_v3_v3(o, rv3d->ofs);
1676
1677         glEnable(GL_BLEND);
1678         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1679         glDepthMask(GL_FALSE);  /* don't overwrite zbuf */
1680
1681         VertexFormat *format = immVertexFormat();
1682         unsigned pos = add_attrib(format, "pos", COMP_F32, 3, KEEP_FLOAT);
1683         unsigned col = add_attrib(format, "color", COMP_U8, 4, NORMALIZE_INT_TO_FLOAT);
1684
1685         immBindBuiltinProgram(GPU_SHADER_3D_SMOOTH_COLOR);
1686
1687         if (rv3d->rot_angle != 0.0f) {
1688                 /* -- draw rotation axis -- */
1689                 float scaled_axis[3];
1690                 const float scale = rv3d->dist;
1691                 mul_v3_v3fl(scaled_axis, rv3d->rot_axis, scale);
1692
1693
1694                 immBegin(GL_LINE_STRIP, 3);
1695                 color[3] = 0; /* more transparent toward the ends */
1696                 immAttrib4ubv(col, color);
1697                 add_v3_v3v3(end, o, scaled_axis);
1698                 immVertex3fv(pos, end);
1699
1700 #if 0
1701                 color[3] = 0.2f + fabsf(rv3d->rot_angle);  /* modulate opacity with angle */
1702                 /* ^^ neat idea, but angle is frame-rate dependent, so it's usually close to 0.2 */
1703 #endif
1704
1705                 color[3] = 127; /* more opaque toward the center */
1706                 immAttrib4ubv(col, color);
1707                 immVertex3fv(pos, o);
1708
1709                 color[3] = 0;
1710                 immAttrib4ubv(col, color);
1711                 sub_v3_v3v3(end, o, scaled_axis);
1712                 immVertex3fv(pos, end);
1713                 immEnd();
1714                 
1715                 /* -- draw ring around rotation center -- */
1716                 {
1717 #define     ROT_AXIS_DETAIL 13
1718
1719                         const float s = 0.05f * scale;
1720                         const float step = 2.0f * (float)(M_PI / ROT_AXIS_DETAIL);
1721
1722                         float q[4];  /* rotate ring so it's perpendicular to axis */
1723                         const int upright = fabsf(rv3d->rot_axis[2]) >= 0.95f;
1724                         if (!upright) {
1725                                 const float up[3] = {0.0f, 0.0f, 1.0f};
1726                                 float vis_angle, vis_axis[3];
1727
1728                                 cross_v3_v3v3(vis_axis, up, rv3d->rot_axis);
1729                                 vis_angle = acosf(dot_v3v3(up, rv3d->rot_axis));
1730                                 axis_angle_to_quat(q, vis_axis, vis_angle);
1731                         }
1732
1733                         immBegin(GL_LINE_LOOP, ROT_AXIS_DETAIL);
1734                         color[3] = 63; /* somewhat faint */
1735                         immAttrib4ubv(col, color);
1736                         float angle = 0.0f;
1737                         for (int i = 0; i < ROT_AXIS_DETAIL; ++i, angle += step) {
1738                                 float p[3] = {s * cosf(angle), s * sinf(angle), 0.0f};
1739
1740                                 if (!upright) {
1741                                         mul_qt_v3(q, p);
1742                                 }
1743
1744                                 add_v3_v3(p, o);
1745                                 immVertex3fv(pos, p);
1746                         }
1747                         immEnd();
1748
1749 #undef      ROT_AXIS_DETAIL
1750                 }
1751
1752                 color[3] = 255;  /* solid dot */
1753         }
1754         else
1755                 color[3] = 127;  /* see-through dot */
1756
1757         immUnbindProgram();
1758
1759         /* -- draw rotation center -- */
1760         immBindBuiltinProgram(GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR);
1761         glPointSize(5.0f);
1762         immBegin(GL_POINTS, 1);
1763         immAttrib4ubv(col, color);
1764         immVertex3fv(pos, o);
1765         immEnd();
1766         immUnbindProgram();
1767
1768 #if 0
1769         /* find screen coordinates for rotation center, then draw pretty icon */
1770         mul_m4_v3(rv3d->persinv, rot_center);
1771         UI_icon_draw(rot_center[0], rot_center[1], ICON_NDOF_TURN);
1772         /* ^^ just playing around, does not work */
1773 #endif
1774
1775         glDisable(GL_BLEND);
1776         glDepthMask(GL_TRUE);
1777 }
1778 #endif /* WITH_INPUT_NDOF */
1779
1780 /* ******************** non-meshes ***************** */
1781
1782 static void view3d_draw_non_mesh(
1783 Scene *scene, SceneLayer *sl, Object *ob, Base *base, View3D *v3d,
1784 RegionView3D *rv3d, const bool is_boundingbox, const unsigned char color[4])
1785 {
1786         glMatrixMode(GL_PROJECTION);
1787         glPushMatrix();
1788         glMatrixMode(GL_MODELVIEW);
1789         glPushMatrix();
1790
1791         /* multiply view with object matrix.
1792         * local viewmat and persmat, to calculate projections */
1793         ED_view3d_init_mats_rv3d_gl(ob, rv3d);
1794
1795         switch (ob->type) {
1796                 case OB_MESH:
1797                 case OB_FONT:
1798                 case OB_CURVE:
1799                 case OB_SURF:
1800                 case OB_MBALL:
1801                         if (is_boundingbox) {
1802                                 draw_bounding_volume(ob, ob->boundtype);
1803                         }
1804                         break;
1805                 case OB_EMPTY:
1806                         drawaxes(rv3d->viewmatob, ob->empty_drawsize, ob->empty_drawtype, color);
1807                         break;
1808                 case OB_LAMP:
1809                         drawlamp(v3d, rv3d, base, OB_SOLID, DRAW_CONSTCOLOR, color, ob == OBACT_NEW);
1810                         break;
1811                 case OB_CAMERA:
1812                         drawcamera(scene, v3d, rv3d, base, DRAW_CONSTCOLOR, color);
1813                         break;
1814                 case OB_SPEAKER:
1815                         drawspeaker(color);
1816                         break;
1817                 case OB_LATTICE:
1818                         /* TODO */
1819                         break;
1820                 case OB_ARMATURE:
1821                         /* TODO */
1822                         break;
1823                 default:
1824                 /* TODO Viewport: handle the other cases*/
1825                         break;
1826         }
1827
1828         if (ob->rigidbody_object) {
1829                 draw_rigidbody_shape(ob);
1830         }
1831
1832         ED_view3d_clear_mats_rv3d(rv3d);
1833
1834         glMatrixMode(GL_PROJECTION);
1835         glPopMatrix();
1836         glMatrixMode(GL_MODELVIEW);
1837         glPopMatrix();
1838 }
1839
1840 /* ******************** info ***************** */
1841
1842 /**
1843 * Render and camera border
1844 */
1845 static void view3d_draw_border(const bContext *C, ARegion *ar)
1846 {
1847         Scene *scene = CTX_data_scene(C);
1848         RegionView3D *rv3d = ar->regiondata;
1849         View3D *v3d = CTX_wm_view3d(C);
1850
1851         if (rv3d->persp == RV3D_CAMOB) {
1852                 drawviewborder(scene, ar, v3d);
1853         }
1854         else if (v3d->flag2 & V3D_RENDER_BORDER) {
1855                 drawrenderborder(ar, v3d);
1856         }
1857 }
1858
1859 /**
1860 * Grease Pencil
1861 */
1862 static void view3d_draw_grease_pencil(const bContext *UNUSED(C))
1863 {
1864         /* TODO viewport */
1865 }
1866
1867
1868 /**
1869 * Viewport Name
1870 */
1871 static const char *view3d_get_name(View3D *v3d, RegionView3D *rv3d)
1872 {
1873         const char *name = NULL;
1874
1875         switch (rv3d->view) {
1876                 case RV3D_VIEW_FRONT:
1877                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Front Ortho");
1878                         else name = IFACE_("Front Persp");
1879                         break;
1880                 case RV3D_VIEW_BACK:
1881                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Back Ortho");
1882                         else name = IFACE_("Back Persp");
1883                         break;
1884                 case RV3D_VIEW_TOP:
1885                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Top Ortho");
1886                         else name = IFACE_("Top Persp");
1887                         break;
1888                 case RV3D_VIEW_BOTTOM:
1889                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Bottom Ortho");
1890                         else name = IFACE_("Bottom Persp");
1891                         break;
1892                 case RV3D_VIEW_RIGHT:
1893                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Right Ortho");
1894                         else name = IFACE_("Right Persp");
1895                         break;
1896                 case RV3D_VIEW_LEFT:
1897                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Left Ortho");
1898                         else name = IFACE_("Left Persp");
1899                         break;
1900
1901                 default:
1902                         if (rv3d->persp == RV3D_CAMOB) {
1903                                 if ((v3d->camera) && (v3d->camera->type == OB_CAMERA)) {
1904                                         Camera *cam;
1905                                         cam = v3d->camera->data;
1906                                         if (cam->type == CAM_PERSP) {
1907                                                 name = IFACE_("Camera Persp");
1908                                         }
1909                                         else if (cam->type == CAM_ORTHO) {
1910                                                 name = IFACE_("Camera Ortho");
1911                                         }
1912                                         else {
1913                                                 BLI_assert(cam->type == CAM_PANO);
1914                                                 name = IFACE_("Camera Pano");
1915                                         }
1916                                 }
1917                                 else {
1918                                         name = IFACE_("Object as Camera");
1919                                 }
1920                         }
1921                         else {
1922                                 name = (rv3d->persp == RV3D_ORTHO) ? IFACE_("User Ortho") : IFACE_("User Persp");
1923                         }
1924         }
1925
1926         return name;
1927 }
1928
1929 static void draw_viewport_name(ARegion *ar, View3D *v3d, rcti *rect)
1930 {
1931         RegionView3D *rv3d = ar->regiondata;
1932         const char *name = view3d_get_name(v3d, rv3d);
1933         /* increase size for unicode languages (Chinese in utf-8...) */
1934 #ifdef WITH_INTERNATIONAL
1935         char tmpstr[96];
1936 #else
1937         char tmpstr[32];
1938 #endif
1939
1940         if (v3d->localvd) {
1941                 BLI_snprintf(tmpstr, sizeof(tmpstr), IFACE_("%s (Local)"), name);
1942                 name = tmpstr;
1943         }
1944
1945         UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
1946 #ifdef WITH_INTERNATIONAL
1947         BLF_draw_default(U.widget_unit + rect->xmin,  rect->ymax - U.widget_unit, 0.0f, name, sizeof(tmpstr));
1948 #else
1949         BLF_draw_default_ascii(U.widget_unit + rect->xmin,  rect->ymax - U.widget_unit, 0.0f, name, sizeof(tmpstr));
1950 #endif
1951 }
1952
1953 /**
1954  * draw info beside axes in bottom left-corner:
1955  * framenum, object name, bone name (if available), marker name (if available)
1956  */
1957
1958 static void draw_selected_name(Scene *scene, Object *ob, rcti *rect)
1959 {
1960         const int cfra = CFRA;
1961         const char *msg_pin = " (Pinned)";
1962         const char *msg_sep = " : ";
1963
1964         const int font_id = BLF_default();
1965
1966         char info[300];
1967         char *s = info;
1968         short offset = 1.5f * UI_UNIT_X + rect->xmin;
1969
1970         s += sprintf(s, "(%d)", cfra);
1971
1972         /*
1973          * info can contain:
1974          * - a frame (7 + 2)
1975          * - 3 object names (MAX_NAME)
1976          * - 2 BREAD_CRUMB_SEPARATORs (6)
1977          * - a SHAPE_KEY_PINNED marker and a trailing '\0' (9+1) - translated, so give some room!
1978          * - a marker name (MAX_NAME + 3)
1979          */
1980
1981         /* get name of marker on current frame (if available) */
1982         const char *markern = BKE_scene_find_marker_name(scene, cfra);
1983
1984         /* check if there is an object */
1985         if (ob) {
1986                 *s++ = ' ';
1987                 s += BLI_strcpy_rlen(s, ob->id.name + 2);
1988
1989                 /* name(s) to display depends on type of object */
1990                 if (ob->type == OB_ARMATURE) {
1991                         bArmature *arm = ob->data;
1992
1993                         /* show name of active bone too (if possible) */
1994                         if (arm->edbo) {
1995                                 if (arm->act_edbone) {
1996                                         s += BLI_strcpy_rlen(s, msg_sep);
1997                                         s += BLI_strcpy_rlen(s, arm->act_edbone->name);
1998                                 }
1999                         }
2000                         else if (ob->mode & OB_MODE_POSE) {
2001                                 if (arm->act_bone) {
2002
2003                                         if (arm->act_bone->layer & arm->layer) {
2004                                                 s += BLI_strcpy_rlen(s, msg_sep);
2005                                                 s += BLI_strcpy_rlen(s, arm->act_bone->name);
2006                                         }
2007                                 }
2008                         }
2009                 }
2010                 else if (ELEM(ob->type, OB_MESH, OB_LATTICE, OB_CURVE)) {
2011                         /* try to display active bone and active shapekey too (if they exist) */
2012
2013                         if (ob->type == OB_MESH && ob->mode & OB_MODE_WEIGHT_PAINT) {
2014                                 Object *armobj = BKE_object_pose_armature_get(ob);
2015                                 if (armobj  && armobj->mode & OB_MODE_POSE) {
2016                                         bArmature *arm = armobj->data;
2017                                         if (arm->act_bone) {
2018                                                 if (arm->act_bone->layer & arm->layer) {
2019                                                         s += BLI_strcpy_rlen(s, msg_sep);
2020                                                         s += BLI_strcpy_rlen(s, arm->act_bone->name);
2021                                                 }
2022                                         }
2023                                 }
2024                         }
2025
2026                         Key *key = BKE_key_from_object(ob);
2027                         if (key) {
2028                                 KeyBlock *kb = BLI_findlink(&key->block, ob->shapenr - 1);
2029                                 if (kb) {
2030                                         s += BLI_strcpy_rlen(s, msg_sep);
2031                                         s += BLI_strcpy_rlen(s, kb->name);
2032                                         if (ob->shapeflag & OB_SHAPE_LOCK) {
2033                                                 s += BLI_strcpy_rlen(s, IFACE_(msg_pin));
2034                                         }
2035                                 }
2036                         }
2037                 }
2038
2039                 /* color depends on whether there is a keyframe */
2040                 if (id_frame_has_keyframe((ID *)ob, /* BKE_scene_frame_get(scene) */ (float)cfra, ANIMFILTER_KEYS_LOCAL))
2041                         UI_FontThemeColor(font_id, TH_TIME_KEYFRAME);
2042                 else if (ED_gpencil_has_keyframe_v3d(scene, ob, cfra))
2043                         UI_FontThemeColor(font_id, TH_TIME_GP_KEYFRAME);
2044                 else
2045                         UI_FontThemeColor(font_id, TH_TEXT_HI);
2046         }
2047         else {
2048                 /* no object */
2049                 if (ED_gpencil_has_keyframe_v3d(scene, NULL, cfra))
2050                         UI_FontThemeColor(font_id, TH_TIME_GP_KEYFRAME);
2051                 else
2052                         UI_FontThemeColor(font_id, TH_TEXT_HI);
2053         }
2054
2055         if (markern) {
2056                 s += sprintf(s, " <%s>", markern);
2057         }
2058
2059         if (U.uiflag & USER_SHOW_ROTVIEWICON)
2060                 offset = U.widget_unit + (U.rvisize * 2) + rect->xmin;
2061
2062         BLF_draw_default(offset, 0.5f * U.widget_unit, 0.0f, info, sizeof(info));
2063 }
2064
2065 /* ******************** view loop ***************** */
2066
2067 /**
2068  * Set the correct matrices
2069  */
2070 static void view3d_draw_setup_view(const bContext *C, ARegion *ar)
2071 {
2072         Scene *scene = CTX_data_scene(C);
2073         View3D *v3d = CTX_wm_view3d(C);
2074         RegionView3D *rv3d = ar->regiondata;
2075
2076         /* setup the view matrix */
2077         if (view3d_stereo3d_active(C, scene, v3d, rv3d))
2078                 view3d_stereo3d_setup(scene, v3d, ar);
2079         else
2080                 view3d_main_region_setup_view(scene, v3d, ar, NULL, NULL);
2081 }
2082
2083 static void draw_all_objects(const bContext *C, ARegion *ar, const bool only_depth, const bool use_depth)
2084 {
2085         Scene *scene = CTX_data_scene(C);
2086         SceneLayer *sl = CTX_data_scene_layer(C);
2087         View3D *v3d = CTX_wm_view3d(C);
2088
2089         if (only_depth)
2090                 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2091
2092         if (only_depth || use_depth) {
2093                 glEnable(GL_DEPTH_TEST);
2094                 glDepthFunc(GL_LESS);
2095                 glDepthMask(GL_TRUE);
2096                 v3d->zbuf = true;
2097         }
2098
2099         for (Base *base = sl->object_bases.first; base; base = base->next) {
2100                 if ((base->flag & BASE_VISIBLED) != 0) {
2101                         /* dupli drawing */
2102                         if (base->object->transflag & OB_DUPLI)
2103                                 draw_dupli_objects(scene, sl, ar, v3d, base);
2104
2105                         draw_object(scene, sl, ar, v3d, base, 0);
2106                 }
2107         }
2108
2109         if (only_depth)
2110                 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2111
2112         if (only_depth || use_depth) {
2113                 glDisable(GL_DEPTH_TEST);
2114                 v3d->zbuf = false;
2115         }
2116 }
2117
2118 /**
2119  * Draw only the scene depth buffer
2120  */
2121 static void draw_depth_buffer(const bContext *C, ARegion *ar)
2122 {
2123         draw_all_objects(C, ar, true, true);
2124 }
2125
2126 /**
2127  * Required if the shaders need it or external engines
2128  * (e.g., Cycles requires depth buffer handled separately).
2129  */
2130 static void view3d_draw_prerender_buffers(const bContext *C, ARegion *ar, DrawData *draw_data)
2131 {
2132         View3D *v3d = CTX_wm_view3d(C);
2133
2134         /* TODO viewport */
2135         if (draw_data->is_render && ((!draw_data->clip_border) || (v3d->drawtype <= OB_WIRE))) {
2136                 draw_depth_buffer(C, ar);
2137         }
2138 }
2139
2140 /**
2141  * Draw all the plates that will fill the RGBD buffer
2142  */
2143 static void view3d_draw_solid_plates(const bContext *C, ARegion *ar, DrawData *draw_data)
2144 {
2145         /* realtime plates */
2146         if ((!draw_data->is_render) || draw_data->clip_border) {
2147                 view3d_draw_background(C);
2148                 view3d_draw_render_solid_surfaces(C, ar, true);
2149                 view3d_draw_render_transparent_surfaces(C);
2150                 view3d_draw_post_draw(C);
2151         }
2152
2153         /* offline plates*/
2154         if (draw_data->is_render) {
2155                 Scene *scene = CTX_data_scene(C);
2156                 View3D *v3d = CTX_wm_view3d(C);
2157
2158                 view3d_draw_render_draw(C, scene, ar, v3d, draw_data->clip_border, &draw_data->border_rect);
2159         }
2160
2161 #if VIEW3D_DRAW_DEBUG
2162         view3d_draw_debug_post_solid(C, ar, draw_data);
2163 #endif
2164 }
2165
2166 /**
2167  * Wires, outline, ...
2168  */
2169 static void view3d_draw_geometry_overlay(const bContext *C)
2170 {
2171         view3d_draw_wire_plates(C);
2172         view3d_draw_outline_plates(C);
2173 }
2174
2175 /* drawing cameras, lamps, ... */
2176 static void view3d_draw_non_meshes(const bContext *C, ARegion *ar)
2177 {
2178         /* TODO viewport
2179          * for now we draw them all, in the near future
2180          * we filter them based on the plates/layers
2181          */
2182         Scene *scene = CTX_data_scene(C);
2183         SceneLayer *sl = CTX_data_scene_layer(C);
2184         View3D *v3d = CTX_wm_view3d(C);
2185         RegionView3D *rv3d = ar->regiondata;
2186
2187         bool is_boundingbox = ((v3d->drawtype == OB_BOUNDBOX) ||
2188                                 ((v3d->drawtype == OB_RENDER) && (v3d->prev_drawtype == OB_BOUNDBOX)));
2189
2190         glEnable(GL_DEPTH_TEST);
2191         glDepthMask(GL_TRUE);
2192         /* TODO Viewport
2193          * we are already temporarily writing to zbuffer in draw_object()
2194          * for now let's avoid writing again to zbuffer to prevent glitches
2195          */
2196
2197         for (Base *base = sl->object_bases.first; base; base = base->next) {
2198                 if ((base->flag & BASE_VISIBLED) != 0) {
2199                         Object *ob = base->object;
2200
2201                         unsigned char ob_wire_col[4];
2202                         draw_object_wire_color(scene, sl, base, ob_wire_col);
2203                         view3d_draw_non_mesh(scene, sl, ob, base, v3d, rv3d, is_boundingbox, ob_wire_col);
2204                 }
2205         }
2206
2207         glDepthMask(GL_FALSE);
2208         glDisable(GL_DEPTH_TEST);
2209 }
2210
2211 /**
2212 * Parent lines, grid, ...
2213 */
2214 static void view3d_draw_other_elements(const bContext *C, ARegion *ar)
2215 {
2216         view3d_draw_grid(C, ar);
2217
2218 #ifdef WITH_INPUT_NDOF
2219         RegionView3D *rv3d = ar->regiondata;
2220
2221         if ((U.ndof_flag & NDOF_SHOW_GUIDE) && ((rv3d->viewlock & RV3D_LOCKED) == 0) && (rv3d->persp != RV3D_CAMOB))
2222                 /* TODO: draw something else (but not this) during fly mode */
2223                 draw_rotation_guide(rv3d);
2224 #endif
2225 }
2226
2227 /**
2228  * Paint brushes, armatures, ...
2229  */
2230 static void view3d_draw_tool_ui(const bContext *UNUSED(C))
2231 {
2232         /* TODO viewport */
2233 }
2234
2235 /**
2236  * Blueprint images
2237  */
2238 static void view3d_draw_reference_images(const bContext *UNUSED(C))
2239 {
2240         /* TODO viewport */
2241 }
2242
2243 /**
2244 * 3D manipulators
2245 */
2246 static void view3d_draw_manipulator(const bContext *C)
2247 {
2248         View3D *v3d = CTX_wm_view3d(C);
2249         v3d->zbuf = false;
2250         BIF_draw_manipulator(C);
2251 }
2252
2253 /**
2254 * Information drawn on top of the solid plates and composed data
2255 */
2256 void view3d_draw_region_info(const bContext *C, ARegion *ar)
2257 {
2258         RegionView3D *rv3d = ar->regiondata;
2259         View3D *v3d = CTX_wm_view3d(C);
2260         Scene *scene = CTX_data_scene(C);
2261         wmWindowManager *wm = CTX_wm_manager(C);
2262
2263         /* correct projection matrix */
2264         ED_region_pixelspace(ar);
2265
2266         /* local coordinate visible rect inside region, to accomodate overlapping ui */
2267         rcti rect;
2268         ED_region_visible_rect(ar, &rect);
2269
2270         view3d_draw_border(C, ar);
2271         view3d_draw_grease_pencil(C);
2272
2273         if (U.uiflag & USER_SHOW_ROTVIEWICON) {
2274                 draw_view_axis(rv3d, &rect);
2275         }
2276
2277         if ((U.uiflag & USER_SHOW_FPS) && ED_screen_animation_no_scrub(wm)) {
2278                 ED_scene_draw_fps(scene, &rect);
2279         }
2280         else if (U.uiflag & USER_SHOW_VIEWPORTNAME) {
2281                 draw_viewport_name(ar, v3d, &rect);
2282         }
2283
2284         if (U.uiflag & USER_DRAWVIEWINFO) {
2285                 SceneLayer *sl = CTX_data_scene_layer(C);
2286                 Object *ob = OBACT_NEW;
2287                 draw_selected_name(scene, ob, &rect);
2288         }
2289 #if 0 /* TODO */
2290         if (grid_unit) { /* draw below the viewport name */
2291                 char numstr[32] = "";
2292
2293                 UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
2294                 if (v3d->grid != 1.0f) {
2295                         BLI_snprintf(numstr, sizeof(numstr), "%s x %.4g", grid_unit, v3d->grid);
2296                 }
2297
2298                 BLF_draw_default_ascii(rect.xmin + U.widget_unit,
2299                                        rect.ymax - (USER_SHOW_VIEWPORTNAME ? 2 * U.widget_unit : U.widget_unit), 0.0f,
2300                                        numstr[0] ? numstr : grid_unit, sizeof(numstr));
2301         }
2302 #endif
2303 }
2304
2305 /**
2306  * This could run once per view, or even in parallel
2307  * for each of them. What is a "view"?
2308  * - a viewport with the camera elsewhere
2309  * - left/right stereo
2310  * - panorama / fisheye individual cubemap faces
2311  */
2312 static void view3d_draw_view(const bContext *C, ARegion *ar, DrawData *draw_data)
2313 {
2314         /* TODO - Technically this should be drawn to a few FBO, so we can handle
2315          * compositing better, but for now this will get the ball rolling (dfelinto) */
2316
2317         view3d_draw_setup_view(C, ar);
2318         view3d_draw_prerender_buffers(C, ar, draw_data);
2319         view3d_draw_solid_plates(C, ar, draw_data);
2320         view3d_draw_geometry_overlay(C);
2321         view3d_draw_non_meshes(C, ar);
2322         view3d_draw_other_elements(C, ar);
2323         view3d_draw_tool_ui(C);
2324         view3d_draw_reference_images(C);
2325         view3d_draw_manipulator(C);
2326         view3d_draw_region_info(C, ar);
2327
2328 #if VIEW3D_DRAW_DEBUG
2329         view3d_draw_debug(C, ar, draw_data);
2330 #endif
2331 }
2332
2333 static void view3d_render_pass(const bContext *C, ARegion *UNUSED(ar))
2334 {
2335         Scene *scene = CTX_data_scene(C);
2336         RenderEngineType *type = RE_engines_find(scene->r.engine); /* In the future we should get that from Layers */
2337
2338         if (type->flag & RE_USE_OGL_PIPELINE) {
2339                 type->view_draw(NULL, C);
2340         }
2341         else {
2342                 // Offline Render engine
2343         }
2344 }
2345
2346 static void view3d_draw_view_new(const bContext *C, ARegion *ar, DrawData *UNUSED(draw_data))
2347 {
2348
2349         view3d_draw_setup_view(C, ar);
2350
2351         /* Only 100% compliant on new spec goes bellow */
2352         view3d_render_pass(C, ar);
2353 }
2354
2355
2356 void view3d_main_region_draw(const bContext *C, ARegion *ar)
2357 {
2358         Scene *scene = CTX_data_scene(C);
2359         View3D *v3d = CTX_wm_view3d(C);
2360         int mode = CTX_data_mode_enum(C);
2361         RegionView3D *rv3d = ar->regiondata;
2362         /* TODO layers - In the future we should get RE from Layers */
2363         RenderEngineType *type = RE_engines_find(scene->r.engine);
2364
2365         if (IS_VIEWPORT_LEGACY(v3d) && ((type->flag & RE_USE_OGL_PIPELINE) == 0)) {
2366                 view3d_main_region_draw_legacy(C, ar);
2367                 return;
2368         }
2369
2370         if (!rv3d->viewport)
2371                 rv3d->viewport = GPU_viewport_create();
2372
2373         GPU_viewport_bind(rv3d->viewport, &ar->winrct, scene->r.engine, mode);
2374
2375         /* TODO viewport - there is so much to be done, in fact a lot will need to happen in the space_view3d.c
2376          * before we even call the drawing routine, but let's move on for now (dfelinto)
2377          * but this is a provisory way to start seeing things in the viewport */
2378         DrawData draw_data;
2379         view3d_draw_data_init(C, ar, rv3d, &draw_data);
2380
2381         if (type->flag & RE_USE_OGL_PIPELINE)
2382                 view3d_draw_view_new(C, ar, &draw_data);
2383         else
2384                 view3d_draw_view(C, ar, &draw_data);
2385
2386         GPU_viewport_unbind(rv3d->viewport);
2387
2388         v3d->flag |= V3D_INVALID_BACKBUF;
2389 }
2390
2391 /* ******************** legacy interface ***************** */
2392 /**
2393  * This will be removed once the viewport gets replaced
2394  * meanwhile it should keep the old viewport working.
2395  */
2396
2397 void VP_legacy_drawcursor(Scene *scene, SceneLayer *sl, ARegion *ar, View3D *v3d)
2398 {
2399         if (is_cursor_visible(scene, sl)) {
2400                 drawcursor(scene, ar, v3d);
2401         }
2402 }
2403
2404 void VP_legacy_draw_view_axis(RegionView3D *rv3d, rcti *rect)
2405 {
2406         draw_view_axis(rv3d, rect);
2407 }
2408
2409 void VP_legacy_draw_viewport_name(ARegion *ar, View3D *v3d, rcti *rect)
2410 {
2411         draw_viewport_name(ar, v3d, rect);
2412 }
2413
2414 void VP_legacy_draw_selected_name(Scene *scene, Object *ob, rcti *rect)
2415 {
2416         draw_selected_name(scene, ob, rect);
2417 }
2418
2419 void VP_legacy_drawgrid(UnitSettings *unit, ARegion *ar, View3D *v3d, const char **grid_unit)
2420 {
2421         drawgrid(unit, ar, v3d, grid_unit);
2422 }
2423
2424 void VP_legacy_drawfloor(Scene *scene, View3D *v3d, const char **grid_unit, bool write_depth)
2425 {
2426         drawfloor(scene, v3d, grid_unit, write_depth);
2427 }
2428
2429 void VP_legacy_view3d_main_region_setup_view(Scene *scene, View3D *v3d, ARegion *ar, float viewmat[4][4], float winmat[4][4])
2430 {
2431         view3d_main_region_setup_view(scene, v3d, ar, viewmat, winmat);
2432 }
2433
2434 bool VP_legacy_view3d_stereo3d_active(const bContext *C, Scene *scene, View3D *v3d, RegionView3D *rv3d)
2435 {
2436         return view3d_stereo3d_active(C, scene, v3d, rv3d);
2437 }
2438
2439 void VP_legacy_view3d_stereo3d_setup(Scene *scene, View3D *v3d, ARegion *ar)
2440 {
2441         view3d_stereo3d_setup(scene, v3d, ar);
2442 }
2443
2444 bool VP_legacy_use_depth(Scene *scene, View3D *v3d)
2445 {
2446         return use_depth_doit(scene, v3d);
2447 }
2448
2449 void VP_drawviewborder(Scene *scene, ARegion *ar, View3D *v3d)
2450 {
2451         drawviewborder(scene, ar, v3d);
2452 }
2453
2454 void VP_drawrenderborder(ARegion *ar, View3D *v3d)
2455 {
2456         drawrenderborder(ar, v3d);
2457 }
2458
2459 void VP_view3d_draw_background_none(void)
2460 {
2461         view3d_draw_background_none();
2462 }
2463
2464 void VP_view3d_draw_background_world(Scene *scene, View3D *v3d, RegionView3D *rv3d)
2465 {
2466         view3d_draw_background_world(scene, v3d, rv3d);
2467 }