Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_view3d / view3d_draw.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_view3d/view3d_draw.c
28  *  \ingroup spview3d
29  */
30
31 #include <math.h>
32
33 #include "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         const float startx = rect->xmin + k + 1.0f;  /* axis center in screen coordinates, x=y */
781         const float starty = rect->ymin + k + 1.0f;
782
783         float axis_pos[3][2];
784         unsigned char axis_col[3][4];
785
786         int axis_order[3] = {0, 1, 2};
787         axis_sort_v3(rv3d->viewinv[2], axis_order);
788
789         for (int axis_i = 0; axis_i < 3; axis_i++) {
790                 int i = axis_order[axis_i];
791
792                 /* get position of each axis tip on screen */
793                 float vec[3] = { 0.0f };
794                 vec[i] = 1.0f;
795                 mul_qt_v3(rv3d->viewquat, vec);
796                 axis_pos[i][0] = startx + vec[0] * k;
797                 axis_pos[i][1] = starty + vec[1] * k;
798
799                 /* get color of each axis */
800                 UI_GetThemeColorShade3ubv(TH_AXIS_X + i, bright, axis_col[i]); /* rgb */
801                 axis_col[i][3] = 255 * hypotf(vec[0], vec[1]); /* alpha */
802         }
803
804         /* draw axis lines */
805         glLineWidth(2.0f);
806         glEnable(GL_LINE_SMOOTH);
807         glEnable(GL_BLEND);
808         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
809
810         Gwn_VertFormat *format = immVertexFormat();
811         unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
812         unsigned int col = GWN_vertformat_attr_add(format, "color", GWN_COMP_U8, 4, GWN_FETCH_INT_TO_FLOAT_UNIT);
813
814         immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
815         immBegin(GWN_PRIM_LINES, 6);
816
817         for (int axis_i = 0; axis_i < 3; axis_i++) {
818                 int i = axis_order[axis_i];
819
820                 immAttrib4ubv(col, axis_col[i]);
821                 immVertex2f(pos, startx, starty);
822                 immAttrib4ubv(col, axis_col[i]);
823                 immVertex2fv(pos, axis_pos[i]);
824         }
825
826         immEnd();
827         immUnbindProgram();
828         glDisable(GL_LINE_SMOOTH);
829
830         /* draw axis names */
831         for (int axis_i = 0; axis_i < 3; axis_i++) {
832                 int i = axis_order[axis_i];
833
834                 const char axis_text[2] = {'x' + i, '\0'};
835                 BLF_color4ubv(BLF_default(), axis_col[i]);
836                 BLF_draw_default_ascii(axis_pos[i][0] + 2, axis_pos[i][1] + 2, 0.0f, axis_text, 1);
837         }
838 }
839
840 #ifdef WITH_INPUT_NDOF
841 /* draw center and axis of rotation for ongoing 3D mouse navigation */
842 static void UNUSED_FUNCTION(draw_rotation_guide)(RegionView3D *rv3d)
843 {
844         float o[3];    /* center of rotation */
845         float end[3];  /* endpoints for drawing */
846
847         GLubyte color[4] = {0, 108, 255, 255};  /* bright blue so it matches device LEDs */
848
849         negate_v3_v3(o, rv3d->ofs);
850
851         glEnable(GL_BLEND);
852         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
853         glDepthMask(GL_FALSE);  /* don't overwrite zbuf */
854
855         Gwn_VertFormat *format = immVertexFormat();
856         unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
857         unsigned int col = GWN_vertformat_attr_add(format, "color", GWN_COMP_U8, 4, GWN_FETCH_INT_TO_FLOAT_UNIT);
858
859         immBindBuiltinProgram(GPU_SHADER_3D_SMOOTH_COLOR);
860
861         if (rv3d->rot_angle != 0.0f) {
862                 /* -- draw rotation axis -- */
863                 float scaled_axis[3];
864                 const float scale = rv3d->dist;
865                 mul_v3_v3fl(scaled_axis, rv3d->rot_axis, scale);
866
867
868                 immBegin(GWN_PRIM_LINE_STRIP, 3);
869                 color[3] = 0; /* more transparent toward the ends */
870                 immAttrib4ubv(col, color);
871                 add_v3_v3v3(end, o, scaled_axis);
872                 immVertex3fv(pos, end);
873
874 #if 0
875                 color[3] = 0.2f + fabsf(rv3d->rot_angle);  /* modulate opacity with angle */
876                 /* ^^ neat idea, but angle is frame-rate dependent, so it's usually close to 0.2 */
877 #endif
878
879                 color[3] = 127; /* more opaque toward the center */
880                 immAttrib4ubv(col, color);
881                 immVertex3fv(pos, o);
882
883                 color[3] = 0;
884                 immAttrib4ubv(col, color);
885                 sub_v3_v3v3(end, o, scaled_axis);
886                 immVertex3fv(pos, end);
887                 immEnd();
888
889                 /* -- draw ring around rotation center -- */
890                 {
891 #define     ROT_AXIS_DETAIL 13
892
893                         const float s = 0.05f * scale;
894                         const float step = 2.0f * (float)(M_PI / ROT_AXIS_DETAIL);
895
896                         float q[4];  /* rotate ring so it's perpendicular to axis */
897                         const int upright = fabsf(rv3d->rot_axis[2]) >= 0.95f;
898                         if (!upright) {
899                                 const float up[3] = {0.0f, 0.0f, 1.0f};
900                                 float vis_angle, vis_axis[3];
901
902                                 cross_v3_v3v3(vis_axis, up, rv3d->rot_axis);
903                                 vis_angle = acosf(dot_v3v3(up, rv3d->rot_axis));
904                                 axis_angle_to_quat(q, vis_axis, vis_angle);
905                         }
906
907                         immBegin(GWN_PRIM_LINE_LOOP, ROT_AXIS_DETAIL);
908                         color[3] = 63; /* somewhat faint */
909                         immAttrib4ubv(col, color);
910                         float angle = 0.0f;
911                         for (int i = 0; i < ROT_AXIS_DETAIL; ++i, angle += step) {
912                                 float p[3] = {s * cosf(angle), s * sinf(angle), 0.0f};
913
914                                 if (!upright) {
915                                         mul_qt_v3(q, p);
916                                 }
917
918                                 add_v3_v3(p, o);
919                                 immVertex3fv(pos, p);
920                         }
921                         immEnd();
922
923 #undef      ROT_AXIS_DETAIL
924                 }
925
926                 color[3] = 255;  /* solid dot */
927         }
928         else
929                 color[3] = 127;  /* see-through dot */
930
931         immUnbindProgram();
932
933         /* -- draw rotation center -- */
934         immBindBuiltinProgram(GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR);
935         glPointSize(5.0f);
936         immBegin(GWN_PRIM_POINTS, 1);
937         immAttrib4ubv(col, color);
938         immVertex3fv(pos, o);
939         immEnd();
940         immUnbindProgram();
941
942 #if 0
943         /* find screen coordinates for rotation center, then draw pretty icon */
944         mul_m4_v3(rv3d->persinv, rot_center);
945         UI_icon_draw(rot_center[0], rot_center[1], ICON_NDOF_TURN);
946         /* ^^ just playing around, does not work */
947 #endif
948
949         glDisable(GL_BLEND);
950         glDepthMask(GL_TRUE);
951 }
952 #endif /* WITH_INPUT_NDOF */
953
954 /* ******************** info ***************** */
955
956 /**
957 * Render and camera border
958 */
959 static void view3d_draw_border(const bContext *C, ARegion *ar)
960 {
961         Scene *scene = CTX_data_scene(C);
962         Depsgraph *depsgraph = CTX_data_depsgraph(C);
963         RegionView3D *rv3d = ar->regiondata;
964         View3D *v3d = CTX_wm_view3d(C);
965
966         if (rv3d->persp == RV3D_CAMOB) {
967                 drawviewborder(scene, depsgraph, ar, v3d);
968         }
969         else if (v3d->flag2 & V3D_RENDER_BORDER) {
970                 drawrenderborder(ar, v3d);
971         }
972 }
973
974 /**
975 * Grease Pencil
976 */
977 static void view3d_draw_grease_pencil(const bContext *UNUSED(C))
978 {
979         /* TODO viewport */
980 }
981
982 /**
983 * Viewport Name
984 */
985 static const char *view3d_get_name(View3D *v3d, RegionView3D *rv3d)
986 {
987         const char *name = NULL;
988
989         switch (rv3d->view) {
990                 case RV3D_VIEW_FRONT:
991                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Front Ortho");
992                         else name = IFACE_("Front Persp");
993                         break;
994                 case RV3D_VIEW_BACK:
995                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Back Ortho");
996                         else name = IFACE_("Back Persp");
997                         break;
998                 case RV3D_VIEW_TOP:
999                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Top Ortho");
1000                         else name = IFACE_("Top Persp");
1001                         break;
1002                 case RV3D_VIEW_BOTTOM:
1003                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Bottom Ortho");
1004                         else name = IFACE_("Bottom Persp");
1005                         break;
1006                 case RV3D_VIEW_RIGHT:
1007                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Right Ortho");
1008                         else name = IFACE_("Right Persp");
1009                         break;
1010                 case RV3D_VIEW_LEFT:
1011                         if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Left Ortho");
1012                         else name = IFACE_("Left Persp");
1013                         break;
1014
1015                 default:
1016                         if (rv3d->persp == RV3D_CAMOB) {
1017                                 if ((v3d->camera) && (v3d->camera->type == OB_CAMERA)) {
1018                                         Camera *cam;
1019                                         cam = v3d->camera->data;
1020                                         if (cam->type == CAM_PERSP) {
1021                                                 name = IFACE_("Camera Persp");
1022                                         }
1023                                         else if (cam->type == CAM_ORTHO) {
1024                                                 name = IFACE_("Camera Ortho");
1025                                         }
1026                                         else {
1027                                                 BLI_assert(cam->type == CAM_PANO);
1028                                                 name = IFACE_("Camera Pano");
1029                                         }
1030                                 }
1031                                 else {
1032                                         name = IFACE_("Object as Camera");
1033                                 }
1034                         }
1035                         else {
1036                                 name = (rv3d->persp == RV3D_ORTHO) ? IFACE_("User Ortho") : IFACE_("User Persp");
1037                         }
1038         }
1039
1040         return name;
1041 }
1042
1043 static void draw_viewport_name(ARegion *ar, View3D *v3d, const rcti *rect)
1044 {
1045         RegionView3D *rv3d = ar->regiondata;
1046         const char *name = view3d_get_name(v3d, rv3d);
1047         /* increase size for unicode languages (Chinese in utf-8...) */
1048 #ifdef WITH_INTERNATIONAL
1049         char tmpstr[96];
1050 #else
1051         char tmpstr[32];
1052 #endif
1053
1054         if (v3d->localvd) {
1055                 BLI_snprintf(tmpstr, sizeof(tmpstr), IFACE_("%s (Local)"), name);
1056                 name = tmpstr;
1057         }
1058
1059         UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
1060 #ifdef WITH_INTERNATIONAL
1061         BLF_draw_default(U.widget_unit + rect->xmin,  rect->ymax - U.widget_unit, 0.0f, name, sizeof(tmpstr));
1062 #else
1063         BLF_draw_default_ascii(U.widget_unit + rect->xmin,  rect->ymax - U.widget_unit, 0.0f, name, sizeof(tmpstr));
1064 #endif
1065 }
1066
1067 /**
1068  * draw info beside axes in bottom left-corner:
1069  * framenum, object name, bone name (if available), marker name (if available)
1070  */
1071
1072 static void draw_selected_name(Scene *scene, Object *ob, rcti *rect)
1073 {
1074         const int cfra = CFRA;
1075         const char *msg_pin = " (Pinned)";
1076         const char *msg_sep = " : ";
1077
1078         const int font_id = BLF_default();
1079
1080         char info[300];
1081         char *s = info;
1082         short offset = 1.5f * UI_UNIT_X + rect->xmin;
1083
1084         s += sprintf(s, "(%d)", cfra);
1085
1086         /*
1087          * info can contain:
1088          * - a frame (7 + 2)
1089          * - 3 object names (MAX_NAME)
1090          * - 2 BREAD_CRUMB_SEPARATORs (6)
1091          * - a SHAPE_KEY_PINNED marker and a trailing '\0' (9+1) - translated, so give some room!
1092          * - a marker name (MAX_NAME + 3)
1093          */
1094
1095         /* get name of marker on current frame (if available) */
1096         const char *markern = BKE_scene_find_marker_name(scene, cfra);
1097
1098         /* check if there is an object */
1099         if (ob) {
1100                 *s++ = ' ';
1101                 s += BLI_strcpy_rlen(s, ob->id.name + 2);
1102
1103                 /* name(s) to display depends on type of object */
1104                 if (ob->type == OB_ARMATURE) {
1105                         bArmature *arm = ob->data;
1106
1107                         /* show name of active bone too (if possible) */
1108                         if (arm->edbo) {
1109                                 if (arm->act_edbone) {
1110                                         s += BLI_strcpy_rlen(s, msg_sep);
1111                                         s += BLI_strcpy_rlen(s, arm->act_edbone->name);
1112                                 }
1113                         }
1114                         else if (ob->mode & OB_MODE_POSE) {
1115                                 if (arm->act_bone) {
1116
1117                                         if (arm->act_bone->layer & arm->layer) {
1118                                                 s += BLI_strcpy_rlen(s, msg_sep);
1119                                                 s += BLI_strcpy_rlen(s, arm->act_bone->name);
1120                                         }
1121                                 }
1122                         }
1123                 }
1124                 else if (ELEM(ob->type, OB_MESH, OB_LATTICE, OB_CURVE)) {
1125                         /* try to display active bone and active shapekey too (if they exist) */
1126
1127                         if (ob->type == OB_MESH && ob->mode & OB_MODE_WEIGHT_PAINT) {
1128                                 Object *armobj = BKE_object_pose_armature_get(ob);
1129                                 if (armobj  && armobj->mode & OB_MODE_POSE) {
1130                                         bArmature *arm = armobj->data;
1131                                         if (arm->act_bone) {
1132                                                 if (arm->act_bone->layer & arm->layer) {
1133                                                         s += BLI_strcpy_rlen(s, msg_sep);
1134                                                         s += BLI_strcpy_rlen(s, arm->act_bone->name);
1135                                                 }
1136                                         }
1137                                 }
1138                         }
1139
1140                         Key *key = BKE_key_from_object(ob);
1141                         if (key) {
1142                                 KeyBlock *kb = BLI_findlink(&key->block, ob->shapenr - 1);
1143                                 if (kb) {
1144                                         s += BLI_strcpy_rlen(s, msg_sep);
1145                                         s += BLI_strcpy_rlen(s, kb->name);
1146                                         if (ob->shapeflag & OB_SHAPE_LOCK) {
1147                                                 s += BLI_strcpy_rlen(s, IFACE_(msg_pin));
1148                                         }
1149                                 }
1150                         }
1151                 }
1152
1153                 /* color depends on whether there is a keyframe */
1154                 if (id_frame_has_keyframe((ID *)ob, /* BKE_scene_frame_get(scene) */ (float)cfra, ANIMFILTER_KEYS_LOCAL))
1155                         UI_FontThemeColor(font_id, TH_TIME_KEYFRAME);
1156                 else if (ED_gpencil_has_keyframe_v3d(scene, ob, cfra))
1157                         UI_FontThemeColor(font_id, TH_TIME_GP_KEYFRAME);
1158                 else
1159                         UI_FontThemeColor(font_id, TH_TEXT_HI);
1160         }
1161         else {
1162                 /* no object */
1163                 if (ED_gpencil_has_keyframe_v3d(scene, NULL, cfra))
1164                         UI_FontThemeColor(font_id, TH_TIME_GP_KEYFRAME);
1165                 else
1166                         UI_FontThemeColor(font_id, TH_TEXT_HI);
1167         }
1168
1169         if (markern) {
1170                 s += sprintf(s, " <%s>", markern);
1171         }
1172
1173         if (U.uiflag & USER_SHOW_ROTVIEWICON)
1174                 offset = U.widget_unit + (U.rvisize * 2) + rect->xmin;
1175
1176         BLF_draw_default(offset, rect->ymin + 0.5f * U.widget_unit, 0.0f, info, sizeof(info));
1177 }
1178
1179 /* ******************** view loop ***************** */
1180
1181 /**
1182 * Information drawn on top of the solid plates and composed data
1183 */
1184 void view3d_draw_region_info(const bContext *C, ARegion *ar, const int offset)
1185 {
1186         RegionView3D *rv3d = ar->regiondata;
1187         View3D *v3d = CTX_wm_view3d(C);
1188         Scene *scene = CTX_data_scene(C);
1189         wmWindowManager *wm = CTX_wm_manager(C);
1190
1191         /* correct projection matrix */
1192         ED_region_pixelspace(ar);
1193
1194         /* local coordinate visible rect inside region, to accomodate overlapping ui */
1195         rcti rect;
1196         ED_region_visible_rect(ar, &rect);
1197
1198         /* Leave room for previously drawn info. */
1199         rect.ymax -= offset;
1200
1201         view3d_draw_border(C, ar);
1202         view3d_draw_grease_pencil(C);
1203
1204         BLF_batch_draw_begin();
1205
1206         if (U.uiflag & USER_SHOW_ROTVIEWICON) {
1207                 draw_view_axis(rv3d, &rect);
1208         }
1209
1210         if ((v3d->overlay.flag & V3D_OVERLAY_HIDE_TEXT) == 0) {
1211                 if ((U.uiflag & USER_SHOW_FPS) && ED_screen_animation_no_scrub(wm)) {
1212                         ED_scene_draw_fps(scene, &rect);
1213                 }
1214                 else if (U.uiflag & USER_SHOW_VIEWPORTNAME) {
1215                         draw_viewport_name(ar, v3d, &rect);
1216                 }
1217
1218                 if (U.uiflag & USER_DRAWVIEWINFO) {
1219                         ViewLayer *view_layer = CTX_data_view_layer(C);
1220                         Object *ob = OBACT(view_layer);
1221                         draw_selected_name(scene, ob, &rect);
1222                 }
1223
1224 #if 0 /* TODO */
1225                 if (grid_unit) { /* draw below the viewport name */
1226                         char numstr[32] = "";
1227
1228                         UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
1229                         if (v3d->grid != 1.0f) {
1230                                 BLI_snprintf(numstr, sizeof(numstr), "%s x %.4g", grid_unit, v3d->grid);
1231                         }
1232
1233                         BLF_draw_default_ascii(
1234                                 rect.xmin + U.widget_unit,
1235                                 rect.ymax - (USER_SHOW_VIEWPORTNAME ? 2 * U.widget_unit : U.widget_unit), 0.0f,
1236                                 numstr[0] ? numstr : grid_unit, sizeof(numstr));
1237                 }
1238 #endif
1239         }
1240
1241         BLF_batch_draw_end();
1242 }
1243
1244 static void view3d_draw_view(const bContext *C, ARegion *ar)
1245 {
1246         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);
1247
1248         /* Only 100% compliant on new spec goes bellow */
1249         DRW_draw_view(C);
1250 }
1251
1252 RenderEngineType *ED_view3d_engine_type(Scene *scene, int drawtype)
1253 {
1254         /*
1255          * Tempory viewport draw modes until we have a proper system.
1256          * all modes are done in the draw manager, except
1257          * cycles material as it is an external render engine.
1258          */
1259         if (strcmp(scene->r.engine, RE_engine_id_CYCLES) == 0 && drawtype == OB_MATERIAL) {
1260                 return RE_engines_find(RE_engine_id_BLENDER_EEVEE);
1261         }
1262         return RE_engines_find(scene->r.engine);
1263 }
1264
1265 void view3d_main_region_draw(const bContext *C, ARegion *ar)
1266 {
1267         View3D *v3d = CTX_wm_view3d(C);
1268
1269         view3d_draw_view(C, ar);
1270
1271         GPU_free_images_old();
1272         GPU_pass_cache_garbage_collect();
1273
1274         /* XXX This is in order to draw UI batches with the DRW
1275          * olg context since we now use it for drawing the entire area */
1276         gpu_batch_presets_reset();
1277
1278         /* No depth test for drawing action zones afterwards. */
1279         glDisable(GL_DEPTH_TEST);
1280
1281         v3d->flag |= V3D_INVALID_BACKBUF;
1282 }
1283
1284 /* -------------------------------------------------------------------- */
1285
1286 /** \name Offscreen Drawing
1287  * \{ */
1288
1289 static void view3d_stereo3d_setup_offscreen(
1290         Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *ar,
1291         float winmat[4][4], const char *viewname)
1292 {
1293         /* update the viewport matrices with the new camera */
1294         if (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D) {
1295                 float viewmat[4][4];
1296                 const bool is_left = STREQ(viewname, STEREO_LEFT_NAME);
1297
1298                 BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat);
1299                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, NULL);
1300         }
1301         else { /* SCE_VIEWS_FORMAT_MULTIVIEW */
1302                 float viewmat[4][4];
1303                 Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
1304
1305                 BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat);
1306                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, NULL);
1307         }
1308 }
1309
1310 void ED_view3d_draw_offscreen(
1311         Depsgraph *depsgraph, Scene *scene,
1312         int drawtype,
1313         View3D *v3d, ARegion *ar, int winx, int winy,
1314         float viewmat[4][4], float winmat[4][4],
1315         bool do_sky, bool UNUSED(is_persp), const char *viewname,
1316         GPUFXSettings *UNUSED(fx_settings),
1317         GPUOffScreen *ofs, GPUViewport *viewport)
1318 {
1319         RegionView3D *rv3d = ar->regiondata;
1320         RenderEngineType *engine_type = ED_view3d_engine_type(scene, drawtype);
1321
1322         /* set temporary new size */
1323         int bwinx = ar->winx;
1324         int bwiny = ar->winy;
1325         rcti brect = ar->winrct;
1326
1327         ar->winx = winx;
1328         ar->winy = winy;
1329         ar->winrct.xmin = 0;
1330         ar->winrct.ymin = 0;
1331         ar->winrct.xmax = winx;
1332         ar->winrct.ymax = winy;
1333
1334         struct bThemeState theme_state;
1335         UI_Theme_Store(&theme_state);
1336         UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
1337
1338         /* set flags */
1339         G.f |= G_RENDER_OGL;
1340
1341         if ((v3d->flag2 & V3D_RENDER_SHADOW) == 0) {
1342                 /* free images which can have changed on frame-change
1343                  * warning! can be slow so only free animated images - campbell */
1344                 GPU_free_images_anim();
1345         }
1346
1347         gpuPushProjectionMatrix();
1348         gpuLoadIdentity();
1349         gpuPushMatrix();
1350         gpuLoadIdentity();
1351
1352         if ((viewname != NULL && viewname[0] != '\0') && (viewmat == NULL) && rv3d->persp == RV3D_CAMOB && v3d->camera)
1353                 view3d_stereo3d_setup_offscreen(depsgraph, scene, v3d, ar, winmat, viewname);
1354         else
1355                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, NULL);
1356
1357         /* main drawing call */
1358         DRW_draw_render_loop_offscreen(
1359                 depsgraph, engine_type, ar, v3d,
1360                 do_sky, ofs, viewport);
1361
1362         /* restore size */
1363         ar->winx = bwinx;
1364         ar->winy = bwiny;
1365         ar->winrct = brect;
1366
1367         gpuPopProjectionMatrix();
1368         gpuPopMatrix();
1369
1370         UI_Theme_Restore(&theme_state);
1371
1372         G.f &= ~G_RENDER_OGL;
1373 }
1374
1375 /**
1376  * Utility func for ED_view3d_draw_offscreen
1377  *
1378  * \param ofs: Optional off-screen buffer, can be NULL.
1379  * (avoids re-creating when doing multiple GL renders).
1380  */
1381 ImBuf *ED_view3d_draw_offscreen_imbuf(
1382         Depsgraph *depsgraph, Scene *scene,
1383         int drawtype,
1384         View3D *v3d, ARegion *ar, int sizex, int sizey,
1385         unsigned int flag, unsigned int draw_flags,
1386         int alpha_mode, int samples, const char *viewname,
1387         /* output vars */
1388         GPUOffScreen *ofs, char err_out[256])
1389 {
1390         RegionView3D *rv3d = ar->regiondata;
1391         const bool draw_sky = (alpha_mode == R_ADDSKY);
1392         const bool use_full_sample = (draw_flags & V3D_OFSDRAW_USE_FULL_SAMPLE);
1393
1394         /* view state */
1395         GPUFXSettings fx_settings = v3d->fx_settings;
1396         bool is_ortho = false;
1397         float winmat[4][4];
1398
1399         if (ofs && ((GPU_offscreen_width(ofs) != sizex) || (GPU_offscreen_height(ofs) != sizey))) {
1400                 /* sizes differ, can't reuse */
1401                 ofs = NULL;
1402         }
1403
1404         const bool own_ofs = (ofs == NULL);
1405         DRW_opengl_context_enable();
1406
1407         if (own_ofs) {
1408                 /* bind */
1409                 ofs = GPU_offscreen_create(sizex, sizey, use_full_sample ? 0 : samples, true, false, err_out);
1410                 if (ofs == NULL) {
1411                         DRW_opengl_context_disable();
1412                         return NULL;
1413                 }
1414         }
1415
1416         GPU_offscreen_bind(ofs, true);
1417
1418         /* read in pixels & stamp */
1419         ImBuf *ibuf = IMB_allocImBuf(sizex, sizey, 32, flag);
1420
1421         /* render 3d view */
1422         if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
1423                 CameraParams params;
1424                 Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
1425                 const Object *camera_eval = DEG_get_evaluated_object(
1426                                                 depsgraph,
1427                                                 camera);
1428
1429                 BKE_camera_params_init(&params);
1430                 /* fallback for non camera objects */
1431                 params.clipsta = v3d->near;
1432                 params.clipend = v3d->far;
1433                 BKE_camera_params_from_object(&params, camera_eval);
1434                 BKE_camera_multiview_params(&scene->r, &params, camera_eval, viewname);
1435                 BKE_camera_params_compute_viewplane(&params, sizex, sizey, scene->r.xasp, scene->r.yasp);
1436                 BKE_camera_params_compute_matrix(&params);
1437
1438                 BKE_camera_to_gpu_dof(camera, &fx_settings);
1439
1440                 is_ortho = params.is_ortho;
1441                 copy_m4_m4(winmat, params.winmat);
1442         }
1443         else {
1444                 rctf viewplane;
1445                 float clipsta, clipend;
1446
1447                 is_ortho = ED_view3d_viewplane_get(depsgraph, v3d, rv3d, sizex, sizey, &viewplane, &clipsta, &clipend, NULL);
1448                 if (is_ortho) {
1449                         orthographic_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, -clipend, clipend);
1450                 }
1451                 else {
1452                         perspective_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, clipsta, clipend);
1453                 }
1454         }
1455
1456         if ((samples && use_full_sample) == 0) {
1457                 /* Single-pass render, common case */
1458                 ED_view3d_draw_offscreen(
1459                         depsgraph, scene, drawtype,
1460                         v3d, ar, sizex, sizey, NULL, winmat,
1461                         draw_sky, !is_ortho, viewname,
1462                         &fx_settings, ofs, NULL);
1463
1464                 if (ibuf->rect_float) {
1465                         GPU_offscreen_read_pixels(ofs, GL_FLOAT, ibuf->rect_float);
1466                 }
1467                 else if (ibuf->rect) {
1468                         GPU_offscreen_read_pixels(ofs, GL_UNSIGNED_BYTE, ibuf->rect);
1469                 }
1470         }
1471         else {
1472                 /* Multi-pass render, use accumulation buffer & jitter for 'full' oversampling.
1473                  * Use because OpenGL may use a lower quality MSAA, and only over-sample edges. */
1474                 static float jit_ofs[32][2];
1475                 float winmat_jitter[4][4];
1476                 float *rect_temp = (ibuf->rect_float) ? ibuf->rect_float : MEM_mallocN(sizex * sizey * sizeof(float[4]), "rect_temp");
1477                 float *accum_buffer = MEM_mallocN(sizex * sizey * sizeof(float[4]), "accum_buffer");
1478                 GPUViewport *viewport = GPU_viewport_create_from_offscreen(ofs);
1479
1480                 BLI_jitter_init(jit_ofs, samples);
1481
1482                 /* first sample buffer, also initializes 'rv3d->persmat' */
1483                 ED_view3d_draw_offscreen(
1484                         depsgraph, scene, drawtype,
1485                         v3d, ar, sizex, sizey, NULL, winmat,
1486                         draw_sky, !is_ortho, viewname,
1487                         &fx_settings, ofs, viewport);
1488                 GPU_offscreen_read_pixels(ofs, GL_FLOAT, accum_buffer);
1489
1490                 /* skip the first sample */
1491                 for (int j = 1; j < samples; j++) {
1492                         copy_m4_m4(winmat_jitter, winmat);
1493                         window_translate_m4(
1494                                 winmat_jitter, rv3d->persmat,
1495                                 (jit_ofs[j][0] * 2.0f) / sizex,
1496                                 (jit_ofs[j][1] * 2.0f) / sizey);
1497
1498                         ED_view3d_draw_offscreen(
1499                                 depsgraph, scene, drawtype,
1500                                 v3d, ar, sizex, sizey, NULL, winmat_jitter,
1501                                 draw_sky, !is_ortho, viewname,
1502                                 &fx_settings, ofs, viewport);
1503                         GPU_offscreen_read_pixels(ofs, GL_FLOAT, rect_temp);
1504
1505                         unsigned int i = sizex * sizey * 4;
1506                         while (i--) {
1507                                 accum_buffer[i] += rect_temp[i];
1508                         }
1509                 }
1510
1511                 {
1512                         /* don't free data owned by 'ofs' */
1513                         GPU_viewport_clear_from_offscreen(viewport);
1514                         GPU_viewport_free(viewport);
1515                 }
1516
1517                 if (ibuf->rect_float == NULL) {
1518                         MEM_freeN(rect_temp);
1519                 }
1520
1521                 if (ibuf->rect_float) {
1522                         float *rect_float = ibuf->rect_float;
1523                         unsigned int i = sizex * sizey * 4;
1524                         while (i--) {
1525                                 rect_float[i] = accum_buffer[i] / samples;
1526                         }
1527                 }
1528                 else {
1529                         unsigned char *rect_ub = (unsigned char *)ibuf->rect;
1530                         unsigned int i = sizex * sizey * 4;
1531                         while (i--) {
1532                                 rect_ub[i] = (unsigned char)(255.0f * accum_buffer[i] / samples);
1533                         }
1534                 }
1535
1536                 MEM_freeN(accum_buffer);
1537         }
1538
1539         /* unbind */
1540         GPU_offscreen_unbind(ofs, true);
1541
1542         if (own_ofs) {
1543                 GPU_offscreen_free(ofs);
1544         }
1545
1546         DRW_opengl_context_disable();
1547
1548         if (ibuf->rect_float && ibuf->rect)
1549                 IMB_rect_from_float(ibuf);
1550
1551         return ibuf;
1552 }
1553
1554 /**
1555  * Creates own fake 3d views (wrapping #ED_view3d_draw_offscreen_imbuf)
1556  *
1557  * \param ofs: Optional off-screen buffer can be NULL.
1558  * (avoids re-creating when doing multiple GL renders).
1559  *
1560  * \note used by the sequencer
1561  */
1562 ImBuf *ED_view3d_draw_offscreen_imbuf_simple(
1563         Depsgraph *depsgraph, Scene *scene,
1564         int drawtype,
1565         Object *camera, int width, int height,
1566         unsigned int flag, unsigned int draw_flags,
1567         int alpha_mode, int samples, const char *viewname,
1568         GPUOffScreen *ofs, char err_out[256])
1569 {
1570         View3D v3d = {NULL};
1571         ARegion ar = {NULL};
1572         RegionView3D rv3d = {{{0}}};
1573
1574         /* connect data */
1575         v3d.regionbase.first = v3d.regionbase.last = &ar;
1576         ar.regiondata = &rv3d;
1577         ar.regiontype = RGN_TYPE_WINDOW;
1578
1579         v3d.camera = camera;
1580         v3d.lay = scene->lay;
1581         v3d.drawtype = drawtype;
1582         v3d.flag2 = V3D_RENDER_OVERRIDE;
1583
1584         if (draw_flags & V3D_OFSDRAW_USE_GPENCIL) {
1585                 v3d.flag2 |= V3D_SHOW_GPENCIL;
1586         }
1587         if (draw_flags & V3D_OFSDRAW_USE_SOLID_TEX) {
1588                 v3d.flag2 |= V3D_SOLID_TEX;
1589         }
1590
1591         v3d.flag3 |= V3D_SHOW_WORLD;
1592
1593         if (draw_flags & V3D_OFSDRAW_USE_CAMERA_DOF) {
1594                 if (camera->type == OB_CAMERA) {
1595                         v3d.fx_settings.dof = &((Camera *)camera->data)->gpu_dof;
1596                         v3d.fx_settings.fx_flag |= GPU_FX_FLAG_DOF;
1597                 }
1598         }
1599
1600         rv3d.persp = RV3D_CAMOB;
1601
1602         copy_m4_m4(rv3d.viewinv, v3d.camera->obmat);
1603         normalize_m4(rv3d.viewinv);
1604         invert_m4_m4(rv3d.viewmat, rv3d.viewinv);
1605
1606         {
1607                 CameraParams params;
1608                 const Object *view_camera_eval = DEG_get_evaluated_object(
1609                                                      depsgraph,
1610                                                      BKE_camera_multiview_render(scene, v3d.camera, viewname));
1611
1612                 BKE_camera_params_init(&params);
1613                 BKE_camera_params_from_object(&params, view_camera_eval);
1614                 BKE_camera_multiview_params(&scene->r, &params, view_camera_eval, viewname);
1615                 BKE_camera_params_compute_viewplane(&params, width, height, scene->r.xasp, scene->r.yasp);
1616                 BKE_camera_params_compute_matrix(&params);
1617
1618                 copy_m4_m4(rv3d.winmat, params.winmat);
1619                 v3d.near = params.clipsta;
1620                 v3d.far = params.clipend;
1621                 v3d.lens = params.lens;
1622         }
1623
1624         mul_m4_m4m4(rv3d.persmat, rv3d.winmat, rv3d.viewmat);
1625         invert_m4_m4(rv3d.persinv, rv3d.viewinv);
1626
1627         return ED_view3d_draw_offscreen_imbuf(
1628                 depsgraph, scene, drawtype,
1629                 &v3d, &ar, width, height, flag,
1630                 draw_flags, alpha_mode, samples, viewname, ofs, err_out);
1631 }
1632
1633 /** \} */