3D View: move screen overlays to the top
[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_2d.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_batch.h"
76 #include "GPU_draw.h"
77 #include "GPU_matrix.h"
78 #include "GPU_immediate.h"
79 #include "GPU_immediate_util.h"
80 #include "GPU_material.h"
81 #include "GPU_viewport.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 /**
103  * \note keep this synced with #ED_view3d_mats_rv3d_backup/#ED_view3d_mats_rv3d_restore
104  */
105 void ED_view3d_update_viewmat(
106         Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *ar,
107         float viewmat[4][4], float winmat[4][4], const rcti *rect)
108 {
109         RegionView3D *rv3d = ar->regiondata;
110
111         /* setup window matrices */
112         if (winmat)
113                 copy_m4_m4(rv3d->winmat, winmat);
114         else
115                 view3d_winmatrix_set(depsgraph, ar, v3d, rect);
116
117         /* setup view matrix */
118         if (viewmat) {
119                 copy_m4_m4(rv3d->viewmat, viewmat);
120         }
121         else {
122                 float rect_scale[2];
123                 if (rect) {
124                         rect_scale[0] = (float)BLI_rcti_size_x(rect) / (float)ar->winx;
125                         rect_scale[1] = (float)BLI_rcti_size_y(rect) / (float)ar->winy;
126                 }
127                 /* note: calls BKE_object_where_is_calc for camera... */
128                 view3d_viewmatrix_set(depsgraph, scene, v3d, rv3d, rect ? rect_scale : NULL);
129         }
130         /* update utility matrices */
131         mul_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
132         invert_m4_m4(rv3d->persinv, rv3d->persmat);
133         invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
134
135         /* calculate GLSL view dependent values */
136
137         /* store window coordinates scaling/offset */
138         if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
139                 rctf cameraborder;
140                 ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &cameraborder, false);
141                 rv3d->viewcamtexcofac[0] = (float)ar->winx / BLI_rctf_size_x(&cameraborder);
142                 rv3d->viewcamtexcofac[1] = (float)ar->winy / BLI_rctf_size_y(&cameraborder);
143
144                 rv3d->viewcamtexcofac[2] = -rv3d->viewcamtexcofac[0] * cameraborder.xmin / (float)ar->winx;
145                 rv3d->viewcamtexcofac[3] = -rv3d->viewcamtexcofac[1] * cameraborder.ymin / (float)ar->winy;
146         }
147         else {
148                 rv3d->viewcamtexcofac[0] = rv3d->viewcamtexcofac[1] = 1.0f;
149                 rv3d->viewcamtexcofac[2] = rv3d->viewcamtexcofac[3] = 0.0f;
150         }
151
152         /* calculate pixelsize factor once, is used for lamps and obcenters */
153         {
154                 /* note:  '1.0f / len_v3(v1)'  replaced  'len_v3(rv3d->viewmat[0])'
155                  * because of float point precision problems at large values [#23908] */
156                 float v1[3], v2[3];
157                 float len_px, len_sc;
158
159                 v1[0] = rv3d->persmat[0][0];
160                 v1[1] = rv3d->persmat[1][0];
161                 v1[2] = rv3d->persmat[2][0];
162
163                 v2[0] = rv3d->persmat[0][1];
164                 v2[1] = rv3d->persmat[1][1];
165                 v2[2] = rv3d->persmat[2][1];
166
167                 len_px = 2.0f / sqrtf(min_ff(len_squared_v3(v1), len_squared_v3(v2)));
168                 len_sc = (float)MAX2(ar->winx, ar->winy);
169
170                 rv3d->pixsize = len_px / len_sc;
171         }
172 }
173
174 static void view3d_main_region_setup_view(
175         Depsgraph *depsgraph, Scene *scene,
176         View3D *v3d, ARegion *ar, float viewmat[4][4], float winmat[4][4], const rcti *rect)
177 {
178         RegionView3D *rv3d = ar->regiondata;
179
180         ED_view3d_update_viewmat(depsgraph, scene, v3d, ar, viewmat, winmat, rect);
181
182         /* set for opengl */
183         gpuLoadProjectionMatrix(rv3d->winmat);
184         gpuLoadMatrix(rv3d->viewmat);
185 }
186
187 static bool view3d_stereo3d_active(wmWindow *win, Scene *scene, View3D *v3d, RegionView3D *rv3d)
188 {
189         if ((scene->r.scemode & R_MULTIVIEW) == 0) {
190                 return false;
191         }
192
193         if ((v3d->camera == NULL) || (v3d->camera->type != OB_CAMERA) || rv3d->persp != RV3D_CAMOB) {
194                 return false;
195         }
196
197         switch (v3d->stereo3d_camera) {
198                 case STEREO_MONO_ID:
199                         return false;
200                         break;
201                 case STEREO_3D_ID:
202                         /* win will be NULL when calling this from the selection or draw loop. */
203                         if ((win == NULL) || (WM_stereo3d_enabled(win, true) == false)) {
204                                 return false;
205                         }
206                         if (((scene->r.views_format & SCE_VIEWS_FORMAT_MULTIVIEW) != 0) &&
207                             !BKE_scene_multiview_is_stereo3d(&scene->r))
208                         {
209                                 return false;
210                         }
211                         break;
212                 /* We always need the stereo calculation for left and right cameras. */
213                 case STEREO_LEFT_ID:
214                 case STEREO_RIGHT_ID:
215                 default:
216                         break;
217         }
218         return true;
219 }
220
221
222 /* setup the view and win matrices for the multiview cameras
223  *
224  * unlike view3d_stereo3d_setup_offscreen, when view3d_stereo3d_setup is called
225  * we have no winmatrix (i.e., projection matrix) defined at that time.
226  * Since the camera and the camera shift are needed for the winmat calculation
227  * we do a small hack to replace it temporarily so we don't need to change the
228  * view3d)main_region_setup_view() code to account for that.
229  */
230 static void view3d_stereo3d_setup(
231         Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *ar, const rcti *rect)
232 {
233         bool is_left;
234         const char *names[2] = { STEREO_LEFT_NAME, STEREO_RIGHT_NAME };
235         const char *viewname;
236
237         /* show only left or right camera */
238         if (v3d->stereo3d_camera != STEREO_3D_ID)
239                 v3d->multiview_eye = v3d->stereo3d_camera;
240
241         is_left = v3d->multiview_eye == STEREO_LEFT_ID;
242         viewname = names[is_left ? STEREO_LEFT_ID : STEREO_RIGHT_ID];
243
244         /* update the viewport matrices with the new camera */
245         if (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D) {
246                 Camera *data;
247                 float viewmat[4][4];
248                 float shiftx;
249
250                 data = (Camera *)v3d->camera->data;
251                 shiftx = data->shiftx;
252
253                 BLI_thread_lock(LOCK_VIEW3D);
254                 data->shiftx = BKE_camera_multiview_shift_x(&scene->r, v3d->camera, viewname);
255
256                 BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat);
257                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, NULL, rect);
258
259                 data->shiftx = shiftx;
260                 BLI_thread_unlock(LOCK_VIEW3D);
261         }
262         else { /* SCE_VIEWS_FORMAT_MULTIVIEW */
263                 float viewmat[4][4];
264                 Object *view_ob = v3d->camera;
265                 Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
266
267                 BLI_thread_lock(LOCK_VIEW3D);
268                 v3d->camera = camera;
269
270                 BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat);
271                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, NULL, rect);
272
273                 v3d->camera = view_ob;
274                 BLI_thread_unlock(LOCK_VIEW3D);
275         }
276 }
277
278 /**
279  * Set the correct matrices
280  */
281 void ED_view3d_draw_setup_view(
282         wmWindow *win, Depsgraph *depsgraph, Scene *scene, ARegion *ar, View3D *v3d,
283         float viewmat[4][4], float winmat[4][4], const rcti *rect)
284 {
285         RegionView3D *rv3d = ar->regiondata;
286
287         /* Setup the view matrix. */
288         if (view3d_stereo3d_active(win, scene, v3d, rv3d)) {
289                 view3d_stereo3d_setup(depsgraph, scene, v3d, ar, rect);
290         }
291         else {
292                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, rect);
293         }
294 }
295
296 /* ******************** view border ***************** */
297
298 static void view3d_camera_border(
299         const Scene *scene, struct Depsgraph *depsgraph,
300         const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
301         rctf *r_viewborder, const bool no_shift, const bool no_zoom)
302 {
303         CameraParams params;
304         rctf rect_view, rect_camera;
305         Object *camera_eval = DEG_get_evaluated_object(depsgraph, v3d->camera);
306
307         /* get viewport viewplane */
308         BKE_camera_params_init(&params);
309         BKE_camera_params_from_view3d(&params, depsgraph, v3d, rv3d);
310         if (no_zoom)
311                 params.zoom = 1.0f;
312         BKE_camera_params_compute_viewplane(&params, ar->winx, ar->winy, 1.0f, 1.0f);
313         rect_view = params.viewplane;
314
315         /* get camera viewplane */
316         BKE_camera_params_init(&params);
317         /* fallback for non camera objects */
318         params.clipsta = v3d->near;
319         params.clipend = v3d->far;
320         BKE_camera_params_from_object(&params, camera_eval);
321         if (no_shift) {
322                 params.shiftx = 0.0f;
323                 params.shifty = 0.0f;
324         }
325         BKE_camera_params_compute_viewplane(&params, scene->r.xsch, scene->r.ysch, scene->r.xasp, scene->r.yasp);
326         rect_camera = params.viewplane;
327
328         /* get camera border within viewport */
329         r_viewborder->xmin = ((rect_camera.xmin - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) * ar->winx;
330         r_viewborder->xmax = ((rect_camera.xmax - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) * ar->winx;
331         r_viewborder->ymin = ((rect_camera.ymin - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) * ar->winy;
332         r_viewborder->ymax = ((rect_camera.ymax - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) * ar->winy;
333 }
334
335 void ED_view3d_calc_camera_border_size(
336         const Scene *scene, Depsgraph *depsgraph,
337         const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
338         float r_size[2])
339 {
340         rctf viewborder;
341
342         view3d_camera_border(scene, depsgraph, ar, v3d, rv3d, &viewborder, true, true);
343         r_size[0] = BLI_rctf_size_x(&viewborder);
344         r_size[1] = BLI_rctf_size_y(&viewborder);
345 }
346
347 void ED_view3d_calc_camera_border(
348         const Scene *scene, Depsgraph *depsgraph,
349         const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
350         rctf *r_viewborder, const bool no_shift)
351 {
352         view3d_camera_border(scene, depsgraph, ar, v3d, rv3d, r_viewborder, no_shift, false);
353 }
354
355 static void drawviewborder_grid3(uint shdr_pos, float x1, float x2, float y1, float y2, float fac)
356 {
357         float x3, y3, x4, y4;
358
359         x3 = x1 + fac * (x2 - x1);
360         y3 = y1 + fac * (y2 - y1);
361         x4 = x1 + (1.0f - fac) * (x2 - x1);
362         y4 = y1 + (1.0f - fac) * (y2 - y1);
363
364         immBegin(GWN_PRIM_LINES, 8);
365
366         immVertex2f(shdr_pos, x1, y3);
367         immVertex2f(shdr_pos, x2, y3);
368
369         immVertex2f(shdr_pos, x1, y4);
370         immVertex2f(shdr_pos, x2, y4);
371
372         immVertex2f(shdr_pos, x3, y1);
373         immVertex2f(shdr_pos, x3, y2);
374
375         immVertex2f(shdr_pos, x4, y1);
376         immVertex2f(shdr_pos, x4, y2);
377
378         immEnd();
379 }
380
381 /* harmonious triangle */
382 static void drawviewborder_triangle(
383         uint shdr_pos, float x1, float x2, float y1, float y2, const char golden, const char dir)
384 {
385         float ofs;
386         float w = x2 - x1;
387         float h = y2 - y1;
388
389         immBegin(GWN_PRIM_LINES, 6);
390
391         if (w > h) {
392                 if (golden) {
393                         ofs = w * (1.0f - (1.0f / 1.61803399f));
394                 }
395                 else {
396                         ofs = h * (h / w);
397                 }
398                 if (dir == 'B') SWAP(float, y1, y2);
399
400                 immVertex2f(shdr_pos, x1, y1);
401                 immVertex2f(shdr_pos, x2, y2);
402
403                 immVertex2f(shdr_pos, x2, y1);
404                 immVertex2f(shdr_pos, x1 + (w - ofs), y2);
405
406                 immVertex2f(shdr_pos, x1, y2);
407                 immVertex2f(shdr_pos, x1 + ofs, y1);
408         }
409         else {
410                 if (golden) {
411                         ofs = h * (1.0f - (1.0f / 1.61803399f));
412                 }
413                 else {
414                         ofs = w * (w / h);
415                 }
416                 if (dir == 'B') SWAP(float, x1, x2);
417
418                 immVertex2f(shdr_pos, x1, y1);
419                 immVertex2f(shdr_pos, x2, y2);
420
421                 immVertex2f(shdr_pos, x2, y1);
422                 immVertex2f(shdr_pos, x1, y1 + ofs);
423
424                 immVertex2f(shdr_pos, x1, y2);
425                 immVertex2f(shdr_pos, x2, y1 + (h - ofs));
426         }
427
428         immEnd();
429 }
430
431 static void drawviewborder(Scene *scene, Depsgraph *depsgraph, ARegion *ar, View3D *v3d)
432 {
433         float x1, x2, y1, y2;
434         float x1i, x2i, y1i, y2i;
435
436         rctf viewborder;
437         Camera *ca = NULL;
438         RegionView3D *rv3d = ar->regiondata;
439
440         if (v3d->camera == NULL)
441                 return;
442         if (v3d->camera->type == OB_CAMERA)
443                 ca = v3d->camera->data;
444
445         ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &viewborder, false);
446         /* the offsets */
447         x1 = viewborder.xmin;
448         y1 = viewborder.ymin;
449         x2 = viewborder.xmax;
450         y2 = viewborder.ymax;
451
452         glLineWidth(1.0f);
453
454         /* apply offsets so the real 3D camera shows through */
455
456         /* note: quite un-scientific but without this bit extra
457          * 0.0001 on the lower left the 2D border sometimes
458          * obscures the 3D camera border */
459         /* note: with VIEW3D_CAMERA_BORDER_HACK defined this error isn't noticeable
460          * but keep it here in case we need to remove the workaround */
461         x1i = (int)(x1 - 1.0001f);
462         y1i = (int)(y1 - 1.0001f);
463         x2i = (int)(x2 + (1.0f - 0.0001f));
464         y2i = (int)(y2 + (1.0f - 0.0001f));
465
466         uint shdr_pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
467
468         /* First, solid lines. */
469         {
470                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
471
472                 /* passepartout, specified in camera edit buttons */
473                 if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001f) {
474                         const float winx = (ar->winx + 1);
475                         const float winy = (ar->winy + 1);
476
477                         float alpha = 1.0f;
478
479                         if (ca->passepartalpha != 1.0f) {
480                                 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
481                                 glEnable(GL_BLEND);
482                                 alpha = ca->passepartalpha;
483                         }
484
485                         immUniformColor4f(0.0f, 0.0f, 0.0f, alpha);
486
487                         if (x1i > 0.0f)
488                                 immRectf(shdr_pos, 0.0f, winy, x1i, 0.0f);
489                         if (x2i < winx)
490                                 immRectf(shdr_pos, x2i, winy, winx, 0.0f);
491                         if (y2i < winy)
492                                 immRectf(shdr_pos, x1i, winy, x2i, y2i);
493                         if (y2i > 0.0f)
494                                 immRectf(shdr_pos, x1i, y1i, x2i, 0.0f);
495
496                         glDisable(GL_BLEND);
497                 }
498
499                 immUniformThemeColor(TH_BACK);
500                 imm_draw_box_wire_2d(shdr_pos, x1i, y1i, x2i, y2i);
501
502 #ifdef VIEW3D_CAMERA_BORDER_HACK
503                 if (view3d_camera_border_hack_test == true) {
504                         immUniformColor3ubv(view3d_camera_border_hack_col);
505                         imm_draw_box_wire_2d(shdr_pos, x1i + 1, y1i + 1, x2i - 1, y2i - 1);
506                         view3d_camera_border_hack_test = false;
507                 }
508 #endif
509
510                 immUnbindProgram();
511         }
512
513         /* And now, the dashed lines! */
514         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
515
516         {
517                 float viewport_size[4];
518                 glGetFloatv(GL_VIEWPORT, viewport_size);
519                 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
520
521                 immUniform1i("num_colors", 0);  /* "simple" mode */
522                 immUniform1f("dash_width", 6.0f);
523                 immUniform1f("dash_factor", 0.5f);
524
525                 /* outer line not to confuse with object selection */
526                 if (v3d->flag2 & V3D_LOCK_CAMERA) {
527                         immUniformThemeColor(TH_REDALERT);
528                         imm_draw_box_wire_2d(shdr_pos, x1i - 1, y1i - 1, x2i + 1, y2i + 1);
529                 }
530
531                 immUniformThemeColor(TH_VIEW_OVERLAY);
532                 imm_draw_box_wire_2d(shdr_pos, x1i, y1i, x2i, y2i);
533         }
534
535         /* border */
536         if (scene->r.mode & R_BORDER) {
537                 float x3, y3, x4, y4;
538
539                 x3 = floorf(x1 + (scene->r.border.xmin * (x2 - x1))) - 1;
540                 y3 = floorf(y1 + (scene->r.border.ymin * (y2 - y1))) - 1;
541                 x4 = floorf(x1 + (scene->r.border.xmax * (x2 - x1))) + (U.pixelsize - 1);
542                 y4 = floorf(y1 + (scene->r.border.ymax * (y2 - y1))) + (U.pixelsize - 1);
543
544                 immUniformColor3f(1.0f, 0.25f, 0.25f);
545                 imm_draw_box_wire_2d(shdr_pos, x3, y3, x4, y4);
546         }
547
548         /* safety border */
549         if (ca) {
550                 immUniformThemeColorBlend(TH_VIEW_OVERLAY, TH_BACK, 0.25f);
551
552                 if (ca->dtx & CAM_DTX_CENTER) {
553                         float x3, y3;
554
555                         x3 = x1 + 0.5f * (x2 - x1);
556                         y3 = y1 + 0.5f * (y2 - y1);
557
558                         immBegin(GWN_PRIM_LINES, 4);
559
560                         immVertex2f(shdr_pos, x1, y3);
561                         immVertex2f(shdr_pos, x2, y3);
562
563                         immVertex2f(shdr_pos, x3, y1);
564                         immVertex2f(shdr_pos, x3, y2);
565
566                         immEnd();
567                 }
568
569                 if (ca->dtx & CAM_DTX_CENTER_DIAG) {
570                         immBegin(GWN_PRIM_LINES, 4);
571
572                         immVertex2f(shdr_pos, x1, y1);
573                         immVertex2f(shdr_pos, x2, y2);
574
575                         immVertex2f(shdr_pos, x1, y2);
576                         immVertex2f(shdr_pos, x2, y1);
577
578                         immEnd();
579                 }
580
581                 if (ca->dtx & CAM_DTX_THIRDS) {
582                         drawviewborder_grid3(shdr_pos, x1, x2, y1, y2, 1.0f / 3.0f);
583                 }
584
585                 if (ca->dtx & CAM_DTX_GOLDEN) {
586                         drawviewborder_grid3(shdr_pos, x1, x2, y1, y2, 1.0f - (1.0f / 1.61803399f));
587                 }
588
589                 if (ca->dtx & CAM_DTX_GOLDEN_TRI_A) {
590                         drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 0, 'A');
591                 }
592
593                 if (ca->dtx & CAM_DTX_GOLDEN_TRI_B) {
594                         drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 0, 'B');
595                 }
596
597                 if (ca->dtx & CAM_DTX_HARMONY_TRI_A) {
598                         drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 1, 'A');
599                 }
600
601                 if (ca->dtx & CAM_DTX_HARMONY_TRI_B) {
602                         drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 1, 'B');
603                 }
604
605                 if (ca->flag & CAM_SHOW_SAFE_MARGINS) {
606                         UI_draw_safe_areas(
607                                 shdr_pos, x1, x2, y1, y2,
608                                 scene->safe_areas.title, scene->safe_areas.action);
609
610                         if (ca->flag & CAM_SHOW_SAFE_CENTER) {
611                                 UI_draw_safe_areas(
612                                         shdr_pos, x1, x2, y1, y2,
613                                         scene->safe_areas.title_center, scene->safe_areas.action_center);
614                         }
615                 }
616
617                 if (ca->flag & CAM_SHOWSENSOR) {
618                         /* determine sensor fit, and get sensor x/y, for auto fit we
619                          * assume and square sensor and only use sensor_x */
620                         float sizex = scene->r.xsch * scene->r.xasp;
621                         float sizey = scene->r.ysch * scene->r.yasp;
622                         int sensor_fit = BKE_camera_sensor_fit(ca->sensor_fit, sizex, sizey);
623                         float sensor_x = ca->sensor_x;
624                         float sensor_y = (ca->sensor_fit == CAMERA_SENSOR_FIT_AUTO) ? ca->sensor_x : ca->sensor_y;
625
626                         /* determine sensor plane */
627                         rctf rect;
628
629                         if (sensor_fit == CAMERA_SENSOR_FIT_HOR) {
630                                 float sensor_scale = (x2i - x1i) / sensor_x;
631                                 float sensor_height = sensor_scale * sensor_y;
632
633                                 rect.xmin = x1i;
634                                 rect.xmax = x2i;
635                                 rect.ymin = (y1i + y2i) * 0.5f - sensor_height * 0.5f;
636                                 rect.ymax = rect.ymin + sensor_height;
637                         }
638                         else {
639                                 float sensor_scale = (y2i - y1i) / sensor_y;
640                                 float sensor_width = sensor_scale * sensor_x;
641
642                                 rect.xmin = (x1i + x2i) * 0.5f - sensor_width * 0.5f;
643                                 rect.xmax = rect.xmin + sensor_width;
644                                 rect.ymin = y1i;
645                                 rect.ymax = y2i;
646                         }
647
648                         /* draw */
649                         immUniformThemeColorShade(TH_VIEW_OVERLAY, 100);
650
651                         /* TODO Was using UI_draw_roundbox_4fv(false, rect.xmin, rect.ymin, rect.xmax, rect.ymax, 2.0f, color).
652                          * We'll probably need a new imm_draw_line_roundbox_dashed dor that - though in practice the
653                          * 2.0f round corner effect was nearly not visible anyway... */
654                         imm_draw_box_wire_2d(shdr_pos, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
655                 }
656         }
657
658         immUnbindProgram();
659         /* end dashed lines */
660
661         /* camera name - draw in highlighted text color */
662         if (ca && (ca->flag & CAM_SHOWNAME)) {
663                 UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
664                 BLF_draw_default(
665                         x1i, y1i - (0.7f * U.widget_unit), 0.0f,
666                         v3d->camera->id.name + 2, sizeof(v3d->camera->id.name) - 2);
667         }
668 }
669
670 static void drawrenderborder(ARegion *ar, View3D *v3d)
671 {
672         /* use the same program for everything */
673         uint shdr_pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
674
675         glLineWidth(1.0f);
676
677         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
678
679         float viewport_size[4];
680         glGetFloatv(GL_VIEWPORT, viewport_size);
681         immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
682
683         immUniform1i("num_colors", 0);  /* "simple" mode */
684         immUniform4f("color", 1.0f, 0.25f, 0.25f, 1.0f);
685         immUniform1f("dash_width", 6.0f);
686         immUniform1f("dash_factor", 0.5f);
687
688         imm_draw_box_wire_2d(shdr_pos,
689                           v3d->render_border.xmin * ar->winx, v3d->render_border.ymin * ar->winy,
690                           v3d->render_border.xmax * ar->winx, v3d->render_border.ymax * ar->winy);
691
692         immUnbindProgram();
693 }
694
695 void ED_view3d_draw_depth(
696         Depsgraph *depsgraph,
697         ARegion *ar, View3D *v3d, bool alphaoverride)
698 {
699         struct bThemeState theme_state;
700         Scene *scene = DEG_get_evaluated_scene(depsgraph);
701         RegionView3D *rv3d = ar->regiondata;
702
703         short zbuf = v3d->zbuf;
704         short flag = v3d->flag;
705         float glalphaclip = U.glalphaclip;
706         int obcenter_dia = U.obcenter_dia;
707         /* temp set drawtype to solid */
708         /* Setting these temporarily is not nice */
709         v3d->flag &= ~V3D_SELECT_OUTLINE;
710         U.glalphaclip = alphaoverride ? 0.5f : glalphaclip; /* not that nice but means we wont zoom into billboards */
711         U.obcenter_dia = 0;
712
713         /* Tools may request depth outside of regular drawing code. */
714         UI_Theme_Store(&theme_state);
715         UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
716
717         ED_view3d_draw_setup_view(NULL, depsgraph, scene, ar, v3d, NULL, NULL, NULL);
718
719         glClear(GL_DEPTH_BUFFER_BIT);
720
721         if (rv3d->rflag & RV3D_CLIPPING) {
722                 ED_view3d_clipping_set(rv3d);
723         }
724         /* get surface depth without bias */
725         rv3d->rflag |= RV3D_ZOFFSET_DISABLED;
726
727         v3d->zbuf = true;
728         glEnable(GL_DEPTH_TEST);
729
730         DRW_draw_depth_loop(depsgraph, ar, v3d);
731
732         if (rv3d->rflag & RV3D_CLIPPING) {
733                 ED_view3d_clipping_disable();
734         }
735         rv3d->rflag &= ~RV3D_ZOFFSET_DISABLED;
736
737         v3d->zbuf = zbuf;
738         if (!v3d->zbuf) glDisable(GL_DEPTH_TEST);
739
740         U.glalphaclip = glalphaclip;
741         v3d->flag = flag;
742         U.obcenter_dia = obcenter_dia;
743
744         UI_Theme_Restore(&theme_state);
745 }
746
747 /* ******************** other elements ***************** */
748
749 /** could move this elsewhere, but tied into #ED_view3d_grid_scale */
750 float ED_scene_grid_scale(Scene *scene, const char **grid_unit)
751 {
752         /* apply units */
753         if (scene->unit.system) {
754                 const void *usys;
755                 int len;
756
757                 bUnit_GetSystem(scene->unit.system, B_UNIT_LENGTH, &usys, &len);
758
759                 if (usys) {
760                         int i = bUnit_GetBaseUnit(usys);
761                         if (grid_unit)
762                                 *grid_unit = bUnit_GetNameDisplay(usys, i);
763                         return (float)bUnit_GetScaler(usys, i) / scene->unit.scale_length;
764                 }
765         }
766
767         return 1.0f;
768 }
769
770 float ED_view3d_grid_scale(Scene *scene, View3D *v3d, const char **grid_unit)
771 {
772         return v3d->grid * ED_scene_grid_scale(scene, grid_unit);
773 }
774
775 static void draw_view_axis(RegionView3D *rv3d, const rcti *rect)
776 {
777         const float k = U.rvisize * U.pixelsize;  /* axis size */
778         const int bright = - 20 * (10 - U.rvibright);  /* axis alpha offset (rvibright has range 0-10) */
779
780         /* Axis center in screen coordinates.
781          *
782          * - Unit size offset so small text doesn't draw outside the screen
783          * - Extra X offset because of the panel expander.
784          */
785         const float startx = rect->xmax - (k + UI_UNIT_X * 1.5);
786         const float starty = rect->ymax - (k + UI_UNIT_Y);
787
788         float axis_pos[3][2];
789         unsigned char axis_col[3][4];
790
791         int axis_order[3] = {0, 1, 2};
792         axis_sort_v3(rv3d->viewinv[2], axis_order);
793
794         for (int axis_i = 0; axis_i < 3; axis_i++) {
795                 int i = axis_order[axis_i];
796
797                 /* get position of each axis tip on screen */
798                 float vec[3] = { 0.0f };
799                 vec[i] = 1.0f;
800                 mul_qt_v3(rv3d->viewquat, vec);
801                 axis_pos[i][0] = startx + vec[0] * k;
802                 axis_pos[i][1] = starty + vec[1] * k;
803
804                 /* get color of each axis */
805                 UI_GetThemeColorShade3ubv(TH_AXIS_X + i, bright, axis_col[i]); /* rgb */
806                 axis_col[i][3] = 255 * hypotf(vec[0], vec[1]); /* alpha */
807         }
808
809         /* draw axis lines */
810         glLineWidth(2.0f);
811         glEnable(GL_LINE_SMOOTH);
812         glEnable(GL_BLEND);
813         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
814
815         Gwn_VertFormat *format = immVertexFormat();
816         unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
817         unsigned int col = GWN_vertformat_attr_add(format, "color", GWN_COMP_U8, 4, GWN_FETCH_INT_TO_FLOAT_UNIT);
818
819         immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
820         immBegin(GWN_PRIM_LINES, 6);
821
822         for (int axis_i = 0; axis_i < 3; axis_i++) {
823                 int i = axis_order[axis_i];
824
825                 immAttrib4ubv(col, axis_col[i]);
826                 immVertex2f(pos, startx, starty);
827                 immAttrib4ubv(col, axis_col[i]);
828                 immVertex2fv(pos, axis_pos[i]);
829         }
830
831         immEnd();
832         immUnbindProgram();
833         glDisable(GL_LINE_SMOOTH);
834
835         /* draw axis names */
836         for (int axis_i = 0; axis_i < 3; axis_i++) {
837                 int i = axis_order[axis_i];
838
839                 const char axis_text[2] = {'x' + i, '\0'};
840                 BLF_color4ubv(BLF_default(), axis_col[i]);
841                 BLF_draw_default_ascii(axis_pos[i][0] + 2, axis_pos[i][1] + 2, 0.0f, axis_text, 1);
842         }
843 }
844
845 #ifdef WITH_INPUT_NDOF
846 /* draw center and axis of rotation for ongoing 3D mouse navigation */
847 static void UNUSED_FUNCTION(draw_rotation_guide)(RegionView3D *rv3d)
848 {
849         float o[3];    /* center of rotation */
850         float end[3];  /* endpoints for drawing */
851
852         GLubyte color[4] = {0, 108, 255, 255};  /* bright blue so it matches device LEDs */
853
854         negate_v3_v3(o, rv3d->ofs);
855
856         glEnable(GL_BLEND);
857         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
858         glDepthMask(GL_FALSE);  /* don't overwrite zbuf */
859
860         Gwn_VertFormat *format = immVertexFormat();
861         unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
862         unsigned int col = GWN_vertformat_attr_add(format, "color", GWN_COMP_U8, 4, GWN_FETCH_INT_TO_FLOAT_UNIT);
863
864         immBindBuiltinProgram(GPU_SHADER_3D_SMOOTH_COLOR);
865
866         if (rv3d->rot_angle != 0.0f) {
867                 /* -- draw rotation axis -- */
868                 float scaled_axis[3];
869                 const float scale = rv3d->dist;
870                 mul_v3_v3fl(scaled_axis, rv3d->rot_axis, scale);
871
872
873                 immBegin(GWN_PRIM_LINE_STRIP, 3);
874                 color[3] = 0; /* more transparent toward the ends */
875                 immAttrib4ubv(col, color);
876                 add_v3_v3v3(end, o, scaled_axis);
877                 immVertex3fv(pos, end);
878
879 #if 0
880                 color[3] = 0.2f + fabsf(rv3d->rot_angle);  /* modulate opacity with angle */
881                 /* ^^ neat idea, but angle is frame-rate dependent, so it's usually close to 0.2 */
882 #endif
883
884                 color[3] = 127; /* more opaque toward the center */
885                 immAttrib4ubv(col, color);
886                 immVertex3fv(pos, o);
887
888                 color[3] = 0;
889                 immAttrib4ubv(col, color);
890                 sub_v3_v3v3(end, o, scaled_axis);
891                 immVertex3fv(pos, end);
892                 immEnd();
893
894                 /* -- draw ring around rotation center -- */
895                 {
896 #define     ROT_AXIS_DETAIL 13
897
898                         const float s = 0.05f * scale;
899                         const float step = 2.0f * (float)(M_PI / ROT_AXIS_DETAIL);
900
901                         float q[4];  /* rotate ring so it's perpendicular to axis */
902                         const int upright = fabsf(rv3d->rot_axis[2]) >= 0.95f;
903                         if (!upright) {
904                                 const float up[3] = {0.0f, 0.0f, 1.0f};
905                                 float vis_angle, vis_axis[3];
906
907                                 cross_v3_v3v3(vis_axis, up, rv3d->rot_axis);
908                                 vis_angle = acosf(dot_v3v3(up, rv3d->rot_axis));
909                                 axis_angle_to_quat(q, vis_axis, vis_angle);
910                         }
911
912                         immBegin(GWN_PRIM_LINE_LOOP, ROT_AXIS_DETAIL);
913                         color[3] = 63; /* somewhat faint */
914                         immAttrib4ubv(col, color);
915                         float angle = 0.0f;
916                         for (int i = 0; i < ROT_AXIS_DETAIL; ++i, angle += step) {
917                                 float p[3] = {s * cosf(angle), s * sinf(angle), 0.0f};
918
919                                 if (!upright) {
920                                         mul_qt_v3(q, p);
921                                 }
922
923                                 add_v3_v3(p, o);
924                                 immVertex3fv(pos, p);
925                         }
926                         immEnd();
927
928 #undef      ROT_AXIS_DETAIL
929                 }
930
931                 color[3] = 255;  /* solid dot */
932         }
933         else
934                 color[3] = 127;  /* see-through dot */
935
936         immUnbindProgram();
937
938         /* -- draw rotation center -- */
939         immBindBuiltinProgram(GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR);
940         glPointSize(5.0f);
941         immBegin(GWN_PRIM_POINTS, 1);
942         immAttrib4ubv(col, color);
943         immVertex3fv(pos, o);
944         immEnd();
945         immUnbindProgram();
946
947 #if 0
948         /* find screen coordinates for rotation center, then draw pretty icon */
949         mul_m4_v3(rv3d->persinv, rot_center);
950         UI_icon_draw(rot_center[0], rot_center[1], ICON_NDOF_TURN);
951         /* ^^ just playing around, does not work */
952 #endif
953
954         glDisable(GL_BLEND);
955         glDepthMask(GL_TRUE);
956 }
957 #endif /* WITH_INPUT_NDOF */
958
959 /* ******************** info ***************** */
960
961 /**
962 * Render and camera border
963 */
964 static void view3d_draw_border(const bContext *C, ARegion *ar)
965 {
966         Scene *scene = CTX_data_scene(C);
967         Depsgraph *depsgraph = CTX_data_depsgraph(C);
968         RegionView3D *rv3d = ar->regiondata;
969         View3D *v3d = CTX_wm_view3d(C);
970
971         if (rv3d->persp == RV3D_CAMOB) {
972                 drawviewborder(scene, depsgraph, ar, v3d);
973         }
974         else if (v3d->flag2 & V3D_RENDER_BORDER) {
975                 drawrenderborder(ar, v3d);
976         }
977 }
978
979 /**
980 * Grease Pencil
981 */
982 static void view3d_draw_grease_pencil(const bContext *UNUSED(C))
983 {
984         /* TODO viewport */
985 }
986
987 /**
988 * Viewport Name
989 */
990 static const char *view3d_get_name(View3D *v3d, RegionView3D *rv3d)
991 {
992         const char *name = NULL;
993
994         switch (rv3d->view) {
995                 case RV3D_VIEW_FRONT:
996                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Front Ortho");
997                         else name = IFACE_("Front Persp");
998                         break;
999                 case RV3D_VIEW_BACK:
1000                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Back Ortho");
1001                         else name = IFACE_("Back Persp");
1002                         break;
1003                 case RV3D_VIEW_TOP:
1004                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Top Ortho");
1005                         else name = IFACE_("Top Persp");
1006                         break;
1007                 case RV3D_VIEW_BOTTOM:
1008                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Bottom Ortho");
1009                         else name = IFACE_("Bottom Persp");
1010                         break;
1011                 case RV3D_VIEW_RIGHT:
1012                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Right Ortho");
1013                         else name = IFACE_("Right Persp");
1014                         break;
1015                 case RV3D_VIEW_LEFT:
1016                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Left Ortho");
1017                         else name = IFACE_("Left Persp");
1018                         break;
1019
1020                 default:
1021                         if (rv3d->persp == RV3D_CAMOB) {
1022                                 if ((v3d->camera) && (v3d->camera->type == OB_CAMERA)) {
1023                                         Camera *cam;
1024                                         cam = v3d->camera->data;
1025                                         if (cam->type == CAM_PERSP) {
1026                                                 name = IFACE_("Camera Persp");
1027                                         }
1028                                         else if (cam->type == CAM_ORTHO) {
1029                                                 name = IFACE_("Camera Ortho");
1030                                         }
1031                                         else {
1032                                                 BLI_assert(cam->type == CAM_PANO);
1033                                                 name = IFACE_("Camera Pano");
1034                                         }
1035                                 }
1036                                 else {
1037                                         name = IFACE_("Object as Camera");
1038                                 }
1039                         }
1040                         else {
1041                                 name = (rv3d->persp == RV3D_ORTHO) ? IFACE_("User Ortho") : IFACE_("User Persp");
1042                         }
1043         }
1044
1045         return name;
1046 }
1047
1048 static void draw_viewport_name(ARegion *ar, View3D *v3d, const rcti *rect)
1049 {
1050         RegionView3D *rv3d = ar->regiondata;
1051         const char *name = view3d_get_name(v3d, rv3d);
1052         /* increase size for unicode languages (Chinese in utf-8...) */
1053 #ifdef WITH_INTERNATIONAL
1054         char tmpstr[96];
1055 #else
1056         char tmpstr[32];
1057 #endif
1058
1059         if (v3d->localvd) {
1060                 BLI_snprintf(tmpstr, sizeof(tmpstr), IFACE_("%s (Local)"), name);
1061                 name = tmpstr;
1062         }
1063
1064         UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
1065 #ifdef WITH_INTERNATIONAL
1066         BLF_draw_default(U.widget_unit + rect->xmin,  rect->ymax - U.widget_unit, 0.0f, name, sizeof(tmpstr));
1067 #else
1068         BLF_draw_default_ascii(U.widget_unit + rect->xmin,  rect->ymax - U.widget_unit, 0.0f, name, sizeof(tmpstr));
1069 #endif
1070 }
1071
1072 /**
1073  * draw info beside axes in bottom left-corner:
1074  * framenum, object name, bone name (if available), marker name (if available)
1075  */
1076
1077 static void draw_selected_name(Scene *scene, Object *ob, rcti *rect)
1078 {
1079         const int cfra = CFRA;
1080         const char *msg_pin = " (Pinned)";
1081         const char *msg_sep = " : ";
1082
1083         const int font_id = BLF_default();
1084
1085         char info[300];
1086         char *s = info;
1087
1088         s += sprintf(s, "(%d)", cfra);
1089
1090         /*
1091          * info can contain:
1092          * - a frame (7 + 2)
1093          * - 3 object names (MAX_NAME)
1094          * - 2 BREAD_CRUMB_SEPARATORs (6)
1095          * - a SHAPE_KEY_PINNED marker and a trailing '\0' (9+1) - translated, so give some room!
1096          * - a marker name (MAX_NAME + 3)
1097          */
1098
1099         /* get name of marker on current frame (if available) */
1100         const char *markern = BKE_scene_find_marker_name(scene, cfra);
1101
1102         /* check if there is an object */
1103         if (ob) {
1104                 *s++ = ' ';
1105                 s += BLI_strcpy_rlen(s, ob->id.name + 2);
1106
1107                 /* name(s) to display depends on type of object */
1108                 if (ob->type == OB_ARMATURE) {
1109                         bArmature *arm = ob->data;
1110
1111                         /* show name of active bone too (if possible) */
1112                         if (arm->edbo) {
1113                                 if (arm->act_edbone) {
1114                                         s += BLI_strcpy_rlen(s, msg_sep);
1115                                         s += BLI_strcpy_rlen(s, arm->act_edbone->name);
1116                                 }
1117                         }
1118                         else if (ob->mode & OB_MODE_POSE) {
1119                                 if (arm->act_bone) {
1120
1121                                         if (arm->act_bone->layer & arm->layer) {
1122                                                 s += BLI_strcpy_rlen(s, msg_sep);
1123                                                 s += BLI_strcpy_rlen(s, arm->act_bone->name);
1124                                         }
1125                                 }
1126                         }
1127                 }
1128                 else if (ELEM(ob->type, OB_MESH, OB_LATTICE, OB_CURVE)) {
1129                         /* try to display active bone and active shapekey too (if they exist) */
1130
1131                         if (ob->type == OB_MESH && ob->mode & OB_MODE_WEIGHT_PAINT) {
1132                                 Object *armobj = BKE_object_pose_armature_get(ob);
1133                                 if (armobj  && armobj->mode & OB_MODE_POSE) {
1134                                         bArmature *arm = armobj->data;
1135                                         if (arm->act_bone) {
1136                                                 if (arm->act_bone->layer & arm->layer) {
1137                                                         s += BLI_strcpy_rlen(s, msg_sep);
1138                                                         s += BLI_strcpy_rlen(s, arm->act_bone->name);
1139                                                 }
1140                                         }
1141                                 }
1142                         }
1143
1144                         Key *key = BKE_key_from_object(ob);
1145                         if (key) {
1146                                 KeyBlock *kb = BLI_findlink(&key->block, ob->shapenr - 1);
1147                                 if (kb) {
1148                                         s += BLI_strcpy_rlen(s, msg_sep);
1149                                         s += BLI_strcpy_rlen(s, kb->name);
1150                                         if (ob->shapeflag & OB_SHAPE_LOCK) {
1151                                                 s += BLI_strcpy_rlen(s, IFACE_(msg_pin));
1152                                         }
1153                                 }
1154                         }
1155                 }
1156
1157                 /* color depends on whether there is a keyframe */
1158                 if (id_frame_has_keyframe((ID *)ob, /* BKE_scene_frame_get(scene) */ (float)cfra, ANIMFILTER_KEYS_LOCAL))
1159                         UI_FontThemeColor(font_id, TH_TIME_KEYFRAME);
1160                 else if (ED_gpencil_has_keyframe_v3d(scene, ob, cfra))
1161                         UI_FontThemeColor(font_id, TH_TIME_GP_KEYFRAME);
1162                 else
1163                         UI_FontThemeColor(font_id, TH_TEXT_HI);
1164         }
1165         else {
1166                 /* no object */
1167                 if (ED_gpencil_has_keyframe_v3d(scene, NULL, cfra))
1168                         UI_FontThemeColor(font_id, TH_TIME_GP_KEYFRAME);
1169                 else
1170                         UI_FontThemeColor(font_id, TH_TEXT_HI);
1171         }
1172
1173         if (markern) {
1174                 s += sprintf(s, " <%s>", markern);
1175         }
1176
1177         BLF_draw_default(rect->xmin + UI_UNIT_X, rect->ymax - (2 * U.widget_unit), 0.0f, info, sizeof(info));
1178 }
1179
1180 /* ******************** view loop ***************** */
1181
1182 /**
1183 * Information drawn on top of the solid plates and composed data
1184 */
1185 void view3d_draw_region_info(const bContext *C, ARegion *ar, const int offset)
1186 {
1187         RegionView3D *rv3d = ar->regiondata;
1188         View3D *v3d = CTX_wm_view3d(C);
1189         Scene *scene = CTX_data_scene(C);
1190         wmWindowManager *wm = CTX_wm_manager(C);
1191
1192         /* correct projection matrix */
1193         ED_region_pixelspace(ar);
1194
1195         /* local coordinate visible rect inside region, to accomodate overlapping ui */
1196         rcti rect;
1197         ED_region_visible_rect(ar, &rect);
1198
1199         /* Leave room for previously drawn info. */
1200         rect.ymax -= offset;
1201
1202         view3d_draw_border(C, ar);
1203         view3d_draw_grease_pencil(C);
1204
1205         BLF_batch_draw_begin();
1206
1207         if (((U.uiflag & USER_SHOW_ROTVIEWICON) != 0) &&
1208             ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) &&
1209             /* No need to display manipulator and this info. */
1210             ((U.manipulator_flag & USER_MANIPULATOR_DRAW_NAVIGATE) == 0))
1211         {
1212                 draw_view_axis(rv3d, &rect);
1213         }
1214
1215         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0 &&
1216             (v3d->overlay.flag & V3D_OVERLAY_HIDE_TEXT) == 0)
1217         {
1218                 if ((U.uiflag & USER_SHOW_FPS) && ED_screen_animation_no_scrub(wm)) {
1219                         ED_scene_draw_fps(scene, &rect);
1220                 }
1221                 else if (U.uiflag & USER_SHOW_VIEWPORTNAME) {
1222                         draw_viewport_name(ar, v3d, &rect);
1223                 }
1224
1225                 if (U.uiflag & USER_DRAWVIEWINFO) {
1226                         ViewLayer *view_layer = CTX_data_view_layer(C);
1227                         Object *ob = OBACT(view_layer);
1228                         draw_selected_name(scene, ob, &rect);
1229                 }
1230
1231 #if 0 /* TODO */
1232                 if (grid_unit) { /* draw below the viewport name */
1233                         char numstr[32] = "";
1234
1235                         UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
1236                         if (v3d->grid != 1.0f) {
1237                                 BLI_snprintf(numstr, sizeof(numstr), "%s x %.4g", grid_unit, v3d->grid);
1238                         }
1239
1240                         BLF_draw_default_ascii(
1241                                 rect.xmin + U.widget_unit,
1242                                 rect.ymax - (USER_SHOW_VIEWPORTNAME ? 2 * U.widget_unit : U.widget_unit), 0.0f,
1243                                 numstr[0] ? numstr : grid_unit, sizeof(numstr));
1244                 }
1245 #endif
1246         }
1247
1248         BLF_batch_draw_end();
1249 }
1250
1251 static void view3d_draw_view(const bContext *C, ARegion *ar)
1252 {
1253         ED_view3d_draw_setup_view(CTX_wm_window(C), CTX_data_depsgraph(C), CTX_data_scene(C), ar, CTX_wm_view3d(C), NULL, NULL, NULL);
1254
1255         /* Only 100% compliant on new spec goes bellow */
1256         DRW_draw_view(C);
1257 }
1258
1259 RenderEngineType *ED_view3d_engine_type(Scene *scene, int drawtype)
1260 {
1261         /*
1262          * Tempory viewport draw modes until we have a proper system.
1263          * all modes are done in the draw manager, except
1264          * cycles material as it is an external render engine.
1265          */
1266         if (strcmp(scene->r.engine, RE_engine_id_CYCLES) == 0 && drawtype == OB_MATERIAL) {
1267                 return RE_engines_find(RE_engine_id_BLENDER_EEVEE);
1268         }
1269         return RE_engines_find(scene->r.engine);
1270 }
1271
1272 void view3d_main_region_draw(const bContext *C, ARegion *ar)
1273 {
1274         View3D *v3d = CTX_wm_view3d(C);
1275
1276         view3d_draw_view(C, ar);
1277
1278         GPU_free_images_old();
1279         GPU_pass_cache_garbage_collect();
1280
1281         /* XXX This is in order to draw UI batches with the DRW
1282          * olg context since we now use it for drawing the entire area */
1283         gpu_batch_presets_reset();
1284
1285         /* No depth test for drawing action zones afterwards. */
1286         glDisable(GL_DEPTH_TEST);
1287
1288         v3d->flag |= V3D_INVALID_BACKBUF;
1289 }
1290
1291 /* -------------------------------------------------------------------- */
1292
1293 /** \name Offscreen Drawing
1294  * \{ */
1295
1296 static void view3d_stereo3d_setup_offscreen(
1297         Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *ar,
1298         float winmat[4][4], const char *viewname)
1299 {
1300         /* update the viewport matrices with the new camera */
1301         if (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D) {
1302                 float viewmat[4][4];
1303                 const bool is_left = STREQ(viewname, STEREO_LEFT_NAME);
1304
1305                 BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat);
1306                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, NULL);
1307         }
1308         else { /* SCE_VIEWS_FORMAT_MULTIVIEW */
1309                 float viewmat[4][4];
1310                 Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
1311
1312                 BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat);
1313                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, NULL);
1314         }
1315 }
1316
1317 void ED_view3d_draw_offscreen(
1318         Depsgraph *depsgraph, Scene *scene,
1319         int drawtype,
1320         View3D *v3d, ARegion *ar, int winx, int winy,
1321         float viewmat[4][4], float winmat[4][4],
1322         bool do_sky, bool UNUSED(is_persp), const char *viewname,
1323         GPUFXSettings *UNUSED(fx_settings),
1324         GPUOffScreen *ofs, GPUViewport *viewport)
1325 {
1326         RegionView3D *rv3d = ar->regiondata;
1327         RenderEngineType *engine_type = ED_view3d_engine_type(scene, drawtype);
1328
1329         /* set temporary new size */
1330         int bwinx = ar->winx;
1331         int bwiny = ar->winy;
1332         rcti brect = ar->winrct;
1333
1334         ar->winx = winx;
1335         ar->winy = winy;
1336         ar->winrct.xmin = 0;
1337         ar->winrct.ymin = 0;
1338         ar->winrct.xmax = winx;
1339         ar->winrct.ymax = winy;
1340
1341         struct bThemeState theme_state;
1342         UI_Theme_Store(&theme_state);
1343         UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
1344
1345         /* set flags */
1346         G.f |= G_RENDER_OGL;
1347
1348         if ((v3d->flag2 & V3D_RENDER_SHADOW) == 0) {
1349                 /* free images which can have changed on frame-change
1350                  * warning! can be slow so only free animated images - campbell */
1351                 GPU_free_images_anim();
1352         }
1353
1354         gpuPushProjectionMatrix();
1355         gpuLoadIdentity();
1356         gpuPushMatrix();
1357         gpuLoadIdentity();
1358
1359         if ((viewname != NULL && viewname[0] != '\0') && (viewmat == NULL) && rv3d->persp == RV3D_CAMOB && v3d->camera)
1360                 view3d_stereo3d_setup_offscreen(depsgraph, scene, v3d, ar, winmat, viewname);
1361         else
1362                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, NULL);
1363
1364         /* main drawing call */
1365         DRW_draw_render_loop_offscreen(
1366                 depsgraph, engine_type, ar, v3d,
1367                 do_sky, ofs, viewport);
1368
1369         /* restore size */
1370         ar->winx = bwinx;
1371         ar->winy = bwiny;
1372         ar->winrct = brect;
1373
1374         gpuPopProjectionMatrix();
1375         gpuPopMatrix();
1376
1377         UI_Theme_Restore(&theme_state);
1378
1379         G.f &= ~G_RENDER_OGL;
1380 }
1381
1382 /**
1383  * Utility func for ED_view3d_draw_offscreen
1384  *
1385  * \param ofs: Optional off-screen buffer, can be NULL.
1386  * (avoids re-creating when doing multiple GL renders).
1387  */
1388 ImBuf *ED_view3d_draw_offscreen_imbuf(
1389         Depsgraph *depsgraph, Scene *scene,
1390         int drawtype,
1391         View3D *v3d, ARegion *ar, int sizex, int sizey,
1392         unsigned int flag, unsigned int draw_flags,
1393         int alpha_mode, int samples, const char *viewname,
1394         /* output vars */
1395         GPUOffScreen *ofs, char err_out[256])
1396 {
1397         RegionView3D *rv3d = ar->regiondata;
1398         const bool draw_sky = (alpha_mode == R_ADDSKY);
1399         const bool use_full_sample = (draw_flags & V3D_OFSDRAW_USE_FULL_SAMPLE);
1400
1401         /* view state */
1402         GPUFXSettings fx_settings = v3d->fx_settings;
1403         bool is_ortho = false;
1404         float winmat[4][4];
1405
1406         if (ofs && ((GPU_offscreen_width(ofs) != sizex) || (GPU_offscreen_height(ofs) != sizey))) {
1407                 /* sizes differ, can't reuse */
1408                 ofs = NULL;
1409         }
1410
1411         const bool own_ofs = (ofs == NULL);
1412         DRW_opengl_context_enable();
1413
1414         if (own_ofs) {
1415                 /* bind */
1416                 ofs = GPU_offscreen_create(sizex, sizey, use_full_sample ? 0 : samples, true, false, err_out);
1417                 if (ofs == NULL) {
1418                         DRW_opengl_context_disable();
1419                         return NULL;
1420                 }
1421         }
1422
1423         GPU_offscreen_bind(ofs, true);
1424
1425         /* read in pixels & stamp */
1426         ImBuf *ibuf = IMB_allocImBuf(sizex, sizey, 32, flag);
1427
1428         /* render 3d view */
1429         if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
1430                 CameraParams params;
1431                 Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
1432                 const Object *camera_eval = DEG_get_evaluated_object(
1433                                                 depsgraph,
1434                                                 camera);
1435
1436                 BKE_camera_params_init(&params);
1437                 /* fallback for non camera objects */
1438                 params.clipsta = v3d->near;
1439                 params.clipend = v3d->far;
1440                 BKE_camera_params_from_object(&params, camera_eval);
1441                 BKE_camera_multiview_params(&scene->r, &params, camera_eval, viewname);
1442                 BKE_camera_params_compute_viewplane(&params, sizex, sizey, scene->r.xasp, scene->r.yasp);
1443                 BKE_camera_params_compute_matrix(&params);
1444
1445                 BKE_camera_to_gpu_dof(camera, &fx_settings);
1446
1447                 is_ortho = params.is_ortho;
1448                 copy_m4_m4(winmat, params.winmat);
1449         }
1450         else {
1451                 rctf viewplane;
1452                 float clipsta, clipend;
1453
1454                 is_ortho = ED_view3d_viewplane_get(depsgraph, v3d, rv3d, sizex, sizey, &viewplane, &clipsta, &clipend, NULL);
1455                 if (is_ortho) {
1456                         orthographic_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, -clipend, clipend);
1457                 }
1458                 else {
1459                         perspective_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, clipsta, clipend);
1460                 }
1461         }
1462
1463         if ((samples && use_full_sample) == 0) {
1464                 /* Single-pass render, common case */
1465                 ED_view3d_draw_offscreen(
1466                         depsgraph, scene, drawtype,
1467                         v3d, ar, sizex, sizey, NULL, winmat,
1468                         draw_sky, !is_ortho, viewname,
1469                         &fx_settings, ofs, NULL);
1470
1471                 if (ibuf->rect_float) {
1472                         GPU_offscreen_read_pixels(ofs, GL_FLOAT, ibuf->rect_float);
1473                 }
1474                 else if (ibuf->rect) {
1475                         GPU_offscreen_read_pixels(ofs, GL_UNSIGNED_BYTE, ibuf->rect);
1476                 }
1477         }
1478         else {
1479                 /* Multi-pass render, use accumulation buffer & jitter for 'full' oversampling.
1480                  * Use because OpenGL may use a lower quality MSAA, and only over-sample edges. */
1481                 static float jit_ofs[32][2];
1482                 float winmat_jitter[4][4];
1483                 float *rect_temp = (ibuf->rect_float) ? ibuf->rect_float : MEM_mallocN(sizex * sizey * sizeof(float[4]), "rect_temp");
1484                 float *accum_buffer = MEM_mallocN(sizex * sizey * sizeof(float[4]), "accum_buffer");
1485                 GPUViewport *viewport = GPU_viewport_create_from_offscreen(ofs);
1486
1487                 BLI_jitter_init(jit_ofs, samples);
1488
1489                 /* first sample buffer, also initializes 'rv3d->persmat' */
1490                 ED_view3d_draw_offscreen(
1491                         depsgraph, scene, drawtype,
1492                         v3d, ar, sizex, sizey, NULL, winmat,
1493                         draw_sky, !is_ortho, viewname,
1494                         &fx_settings, ofs, viewport);
1495                 GPU_offscreen_read_pixels(ofs, GL_FLOAT, accum_buffer);
1496
1497                 /* skip the first sample */
1498                 for (int j = 1; j < samples; j++) {
1499                         copy_m4_m4(winmat_jitter, winmat);
1500                         window_translate_m4(
1501                                 winmat_jitter, rv3d->persmat,
1502                                 (jit_ofs[j][0] * 2.0f) / sizex,
1503                                 (jit_ofs[j][1] * 2.0f) / sizey);
1504
1505                         ED_view3d_draw_offscreen(
1506                                 depsgraph, scene, drawtype,
1507                                 v3d, ar, sizex, sizey, NULL, winmat_jitter,
1508                                 draw_sky, !is_ortho, viewname,
1509                                 &fx_settings, ofs, viewport);
1510                         GPU_offscreen_read_pixels(ofs, GL_FLOAT, rect_temp);
1511
1512                         unsigned int i = sizex * sizey * 4;
1513                         while (i--) {
1514                                 accum_buffer[i] += rect_temp[i];
1515                         }
1516                 }
1517
1518                 {
1519                         /* don't free data owned by 'ofs' */
1520                         GPU_viewport_clear_from_offscreen(viewport);
1521                         GPU_viewport_free(viewport);
1522                 }
1523
1524                 if (ibuf->rect_float == NULL) {
1525                         MEM_freeN(rect_temp);
1526                 }
1527
1528                 if (ibuf->rect_float) {
1529                         float *rect_float = ibuf->rect_float;
1530                         unsigned int i = sizex * sizey * 4;
1531                         while (i--) {
1532                                 rect_float[i] = accum_buffer[i] / samples;
1533                         }
1534                 }
1535                 else {
1536                         unsigned char *rect_ub = (unsigned char *)ibuf->rect;
1537                         unsigned int i = sizex * sizey * 4;
1538                         while (i--) {
1539                                 rect_ub[i] = (unsigned char)(255.0f * accum_buffer[i] / samples);
1540                         }
1541                 }
1542
1543                 MEM_freeN(accum_buffer);
1544         }
1545
1546         /* unbind */
1547         GPU_offscreen_unbind(ofs, true);
1548
1549         if (own_ofs) {
1550                 GPU_offscreen_free(ofs);
1551         }
1552
1553         DRW_opengl_context_disable();
1554
1555         if (ibuf->rect_float && ibuf->rect)
1556                 IMB_rect_from_float(ibuf);
1557
1558         return ibuf;
1559 }
1560
1561 /**
1562  * Creates own fake 3d views (wrapping #ED_view3d_draw_offscreen_imbuf)
1563  *
1564  * \param ofs: Optional off-screen buffer can be NULL.
1565  * (avoids re-creating when doing multiple GL renders).
1566  *
1567  * \note used by the sequencer
1568  */
1569 ImBuf *ED_view3d_draw_offscreen_imbuf_simple(
1570         Depsgraph *depsgraph, Scene *scene,
1571         int drawtype,
1572         Object *camera, int width, int height,
1573         unsigned int flag, unsigned int draw_flags,
1574         int alpha_mode, int samples, const char *viewname,
1575         GPUOffScreen *ofs, char err_out[256])
1576 {
1577         View3D v3d = {NULL};
1578         ARegion ar = {NULL};
1579         RegionView3D rv3d = {{{0}}};
1580
1581         /* connect data */
1582         v3d.regionbase.first = v3d.regionbase.last = &ar;
1583         ar.regiondata = &rv3d;
1584         ar.regiontype = RGN_TYPE_WINDOW;
1585
1586         v3d.camera = camera;
1587         v3d.lay = scene->lay;
1588         v3d.drawtype = drawtype;
1589         v3d.flag2 = V3D_RENDER_OVERRIDE;
1590
1591         if (draw_flags & V3D_OFSDRAW_USE_GPENCIL) {
1592                 v3d.flag2 |= V3D_SHOW_GPENCIL;
1593         }
1594         if (draw_flags & V3D_OFSDRAW_USE_SOLID_TEX) {
1595                 v3d.flag2 |= V3D_SOLID_TEX;
1596         }
1597
1598         v3d.flag3 |= V3D_SHOW_WORLD;
1599
1600         if (draw_flags & V3D_OFSDRAW_USE_CAMERA_DOF) {
1601                 if (camera->type == OB_CAMERA) {
1602                         v3d.fx_settings.dof = &((Camera *)camera->data)->gpu_dof;
1603                         v3d.fx_settings.fx_flag |= GPU_FX_FLAG_DOF;
1604                 }
1605         }
1606
1607         rv3d.persp = RV3D_CAMOB;
1608
1609         copy_m4_m4(rv3d.viewinv, v3d.camera->obmat);
1610         normalize_m4(rv3d.viewinv);
1611         invert_m4_m4(rv3d.viewmat, rv3d.viewinv);
1612
1613         {
1614                 CameraParams params;
1615                 const Object *view_camera_eval = DEG_get_evaluated_object(
1616                                                      depsgraph,
1617                                                      BKE_camera_multiview_render(scene, v3d.camera, viewname));
1618
1619                 BKE_camera_params_init(&params);
1620                 BKE_camera_params_from_object(&params, view_camera_eval);
1621                 BKE_camera_multiview_params(&scene->r, &params, view_camera_eval, viewname);
1622                 BKE_camera_params_compute_viewplane(&params, width, height, scene->r.xasp, scene->r.yasp);
1623                 BKE_camera_params_compute_matrix(&params);
1624
1625                 copy_m4_m4(rv3d.winmat, params.winmat);
1626                 v3d.near = params.clipsta;
1627                 v3d.far = params.clipend;
1628                 v3d.lens = params.lens;
1629         }
1630
1631         mul_m4_m4m4(rv3d.persmat, rv3d.winmat, rv3d.viewmat);
1632         invert_m4_m4(rv3d.persinv, rv3d.viewinv);
1633
1634         return ED_view3d_draw_offscreen_imbuf(
1635                 depsgraph, scene, drawtype,
1636                 &v3d, &ar, width, height, flag,
1637                 draw_flags, alpha_mode, samples, viewname, ofs, err_out);
1638 }
1639
1640 /** \} */