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