2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2008 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/space_view3d/view3d_draw.c
33 #include "BLI_listbase.h"
36 #include "BLI_string.h"
37 #include "BLI_threads.h"
38 #include "BLI_jitter_2d.h"
41 #include "BIF_glutil.h"
43 #include "BKE_camera.h"
44 #include "BKE_collection.h"
45 #include "BKE_context.h"
46 #include "BKE_global.h"
49 #include "BKE_scene.h"
50 #include "BKE_object.h"
51 #include "BKE_paint.h"
56 #include "BLT_translation.h"
58 #include "DNA_armature_types.h"
59 #include "DNA_brush_types.h"
60 #include "DNA_camera_types.h"
61 #include "DNA_key_types.h"
62 #include "DNA_mesh_types.h"
63 #include "DNA_object_types.h"
64 #include "DNA_view3d_types.h"
65 #include "DNA_windowmanager_types.h"
67 #include "DRW_engine.h"
69 #include "ED_armature.h"
70 #include "ED_keyframing.h"
71 #include "ED_gpencil.h"
72 #include "ED_screen.h"
73 #include "ED_transform.h"
75 #include "DEG_depsgraph_query.h"
77 #include "GPU_batch.h"
78 #include "GPU_batch_presets.h"
80 #include "GPU_matrix.h"
81 #include "GPU_immediate.h"
82 #include "GPU_immediate_util.h"
83 #include "GPU_material.h"
84 #include "GPU_viewport.h"
85 #include "GPU_state.h"
86 #include "GPU_framebuffer.h"
88 #include "MEM_guardedalloc.h"
90 #include "UI_interface.h"
91 #include "UI_resources.h"
93 #include "RE_engine.h"
98 #include "RNA_access.h"
100 #include "IMB_imbuf.h"
101 #include "IMB_imbuf_types.h"
103 #include "view3d_intern.h" /* own include */
105 /* ******************** general functions ***************** */
108 * \note keep this synced with #ED_view3d_mats_rv3d_backup/#ED_view3d_mats_rv3d_restore
110 void ED_view3d_update_viewmat(
111 Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *ar,
112 float viewmat[4][4], float winmat[4][4], const rcti *rect)
114 RegionView3D *rv3d = ar->regiondata;
116 /* setup window matrices */
118 copy_m4_m4(rv3d->winmat, winmat);
120 view3d_winmatrix_set(depsgraph, ar, v3d, rect);
122 /* setup view matrix */
124 copy_m4_m4(rv3d->viewmat, viewmat);
129 rect_scale[0] = (float)BLI_rcti_size_x(rect) / (float)ar->winx;
130 rect_scale[1] = (float)BLI_rcti_size_y(rect) / (float)ar->winy;
132 /* note: calls BKE_object_where_is_calc for camera... */
133 view3d_viewmatrix_set(depsgraph, scene, v3d, rv3d, rect ? rect_scale : NULL);
135 /* update utility matrices */
136 mul_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
137 invert_m4_m4(rv3d->persinv, rv3d->persmat);
138 invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
140 /* calculate GLSL view dependent values */
142 /* store window coordinates scaling/offset */
143 if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
145 ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &cameraborder, false);
146 rv3d->viewcamtexcofac[0] = (float)ar->winx / BLI_rctf_size_x(&cameraborder);
147 rv3d->viewcamtexcofac[1] = (float)ar->winy / BLI_rctf_size_y(&cameraborder);
149 rv3d->viewcamtexcofac[2] = -rv3d->viewcamtexcofac[0] * cameraborder.xmin / (float)ar->winx;
150 rv3d->viewcamtexcofac[3] = -rv3d->viewcamtexcofac[1] * cameraborder.ymin / (float)ar->winy;
153 rv3d->viewcamtexcofac[0] = rv3d->viewcamtexcofac[1] = 1.0f;
154 rv3d->viewcamtexcofac[2] = rv3d->viewcamtexcofac[3] = 0.0f;
157 /* calculate pixelsize factor once, is used for lamps and obcenters */
159 /* note: '1.0f / len_v3(v1)' replaced 'len_v3(rv3d->viewmat[0])'
160 * because of float point precision problems at large values [#23908] */
162 float len_px, len_sc;
164 v1[0] = rv3d->persmat[0][0];
165 v1[1] = rv3d->persmat[1][0];
166 v1[2] = rv3d->persmat[2][0];
168 v2[0] = rv3d->persmat[0][1];
169 v2[1] = rv3d->persmat[1][1];
170 v2[2] = rv3d->persmat[2][1];
172 len_px = 2.0f / sqrtf(min_ff(len_squared_v3(v1), len_squared_v3(v2)));
173 len_sc = (float)MAX2(ar->winx, ar->winy);
175 rv3d->pixsize = len_px / len_sc;
179 static void view3d_main_region_setup_view(
180 Depsgraph *depsgraph, Scene *scene,
181 View3D *v3d, ARegion *ar, float viewmat[4][4], float winmat[4][4], const rcti *rect)
183 RegionView3D *rv3d = ar->regiondata;
185 ED_view3d_update_viewmat(depsgraph, scene, v3d, ar, viewmat, winmat, rect);
188 GPU_matrix_projection_set(rv3d->winmat);
189 GPU_matrix_set(rv3d->viewmat);
192 static bool view3d_stereo3d_active(wmWindow *win, Scene *scene, View3D *v3d, RegionView3D *rv3d)
194 if ((scene->r.scemode & R_MULTIVIEW) == 0) {
198 if ((v3d->camera == NULL) || (v3d->camera->type != OB_CAMERA) || rv3d->persp != RV3D_CAMOB) {
202 switch (v3d->stereo3d_camera) {
207 /* win will be NULL when calling this from the selection or draw loop. */
208 if ((win == NULL) || (WM_stereo3d_enabled(win, true) == false)) {
211 if (((scene->r.views_format & SCE_VIEWS_FORMAT_MULTIVIEW) != 0) &&
212 !BKE_scene_multiview_is_stereo3d(&scene->r))
217 /* We always need the stereo calculation for left and right cameras. */
219 case STEREO_RIGHT_ID:
227 /* setup the view and win matrices for the multiview cameras
229 * unlike view3d_stereo3d_setup_offscreen, when view3d_stereo3d_setup is called
230 * we have no winmatrix (i.e., projection matrix) defined at that time.
231 * Since the camera and the camera shift are needed for the winmat calculation
232 * we do a small hack to replace it temporarily so we don't need to change the
233 * view3d)main_region_setup_view() code to account for that.
235 static void view3d_stereo3d_setup(
236 Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *ar, const rcti *rect)
239 const char *names[2] = { STEREO_LEFT_NAME, STEREO_RIGHT_NAME };
240 const char *viewname;
242 /* show only left or right camera */
243 if (v3d->stereo3d_camera != STEREO_3D_ID)
244 v3d->multiview_eye = v3d->stereo3d_camera;
246 is_left = v3d->multiview_eye == STEREO_LEFT_ID;
247 viewname = names[is_left ? STEREO_LEFT_ID : STEREO_RIGHT_ID];
249 /* update the viewport matrices with the new camera */
250 if (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D) {
255 data = (Camera *)v3d->camera->data;
256 shiftx = data->shiftx;
258 BLI_thread_lock(LOCK_VIEW3D);
259 data->shiftx = BKE_camera_multiview_shift_x(&scene->r, v3d->camera, viewname);
261 BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat);
262 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, NULL, rect);
264 data->shiftx = shiftx;
265 BLI_thread_unlock(LOCK_VIEW3D);
267 else { /* SCE_VIEWS_FORMAT_MULTIVIEW */
269 Object *view_ob = v3d->camera;
270 Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
272 BLI_thread_lock(LOCK_VIEW3D);
273 v3d->camera = camera;
275 BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat);
276 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, NULL, rect);
278 v3d->camera = view_ob;
279 BLI_thread_unlock(LOCK_VIEW3D);
284 * Set the correct matrices
286 void ED_view3d_draw_setup_view(
287 wmWindow *win, Depsgraph *depsgraph, Scene *scene, ARegion *ar, View3D *v3d,
288 float viewmat[4][4], float winmat[4][4], const rcti *rect)
290 RegionView3D *rv3d = ar->regiondata;
292 /* Setup the view matrix. */
293 if (view3d_stereo3d_active(win, scene, v3d, rv3d)) {
294 view3d_stereo3d_setup(depsgraph, scene, v3d, ar, rect);
297 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, rect);
301 /* ******************** view border ***************** */
303 static void view3d_camera_border(
304 const Scene *scene, struct Depsgraph *depsgraph,
305 const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
306 rctf *r_viewborder, const bool no_shift, const bool no_zoom)
309 rctf rect_view, rect_camera;
310 Object *camera_eval = DEG_get_evaluated_object(depsgraph, v3d->camera);
312 /* get viewport viewplane */
313 BKE_camera_params_init(¶ms);
314 BKE_camera_params_from_view3d(¶ms, depsgraph, v3d, rv3d);
317 BKE_camera_params_compute_viewplane(¶ms, ar->winx, ar->winy, 1.0f, 1.0f);
318 rect_view = params.viewplane;
320 /* get camera viewplane */
321 BKE_camera_params_init(¶ms);
322 /* fallback for non camera objects */
323 params.clipsta = v3d->near;
324 params.clipend = v3d->far;
325 BKE_camera_params_from_object(¶ms, camera_eval);
327 params.shiftx = 0.0f;
328 params.shifty = 0.0f;
330 BKE_camera_params_compute_viewplane(¶ms, scene->r.xsch, scene->r.ysch, scene->r.xasp, scene->r.yasp);
331 rect_camera = params.viewplane;
333 /* get camera border within viewport */
334 r_viewborder->xmin = ((rect_camera.xmin - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) * ar->winx;
335 r_viewborder->xmax = ((rect_camera.xmax - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) * ar->winx;
336 r_viewborder->ymin = ((rect_camera.ymin - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) * ar->winy;
337 r_viewborder->ymax = ((rect_camera.ymax - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) * ar->winy;
340 void ED_view3d_calc_camera_border_size(
341 const Scene *scene, Depsgraph *depsgraph,
342 const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
347 view3d_camera_border(scene, depsgraph, ar, v3d, rv3d, &viewborder, true, true);
348 r_size[0] = BLI_rctf_size_x(&viewborder);
349 r_size[1] = BLI_rctf_size_y(&viewborder);
352 void ED_view3d_calc_camera_border(
353 const Scene *scene, Depsgraph *depsgraph,
354 const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
355 rctf *r_viewborder, const bool no_shift)
357 view3d_camera_border(scene, depsgraph, ar, v3d, rv3d, r_viewborder, no_shift, false);
360 static void drawviewborder_grid3(uint shdr_pos, float x1, float x2, float y1, float y2, float fac)
362 float x3, y3, x4, y4;
364 x3 = x1 + fac * (x2 - x1);
365 y3 = y1 + fac * (y2 - y1);
366 x4 = x1 + (1.0f - fac) * (x2 - x1);
367 y4 = y1 + (1.0f - fac) * (y2 - y1);
369 immBegin(GPU_PRIM_LINES, 8);
371 immVertex2f(shdr_pos, x1, y3);
372 immVertex2f(shdr_pos, x2, y3);
374 immVertex2f(shdr_pos, x1, y4);
375 immVertex2f(shdr_pos, x2, y4);
377 immVertex2f(shdr_pos, x3, y1);
378 immVertex2f(shdr_pos, x3, y2);
380 immVertex2f(shdr_pos, x4, y1);
381 immVertex2f(shdr_pos, x4, y2);
386 /* harmonious triangle */
387 static void drawviewborder_triangle(
388 uint shdr_pos, float x1, float x2, float y1, float y2, const char golden, const char dir)
394 immBegin(GPU_PRIM_LINES, 6);
398 ofs = w * (1.0f - (1.0f / 1.61803399f));
403 if (dir == 'B') SWAP(float, y1, y2);
405 immVertex2f(shdr_pos, x1, y1);
406 immVertex2f(shdr_pos, x2, y2);
408 immVertex2f(shdr_pos, x2, y1);
409 immVertex2f(shdr_pos, x1 + (w - ofs), y2);
411 immVertex2f(shdr_pos, x1, y2);
412 immVertex2f(shdr_pos, x1 + ofs, y1);
416 ofs = h * (1.0f - (1.0f / 1.61803399f));
421 if (dir == 'B') SWAP(float, x1, x2);
423 immVertex2f(shdr_pos, x1, y1);
424 immVertex2f(shdr_pos, x2, y2);
426 immVertex2f(shdr_pos, x2, y1);
427 immVertex2f(shdr_pos, x1, y1 + ofs);
429 immVertex2f(shdr_pos, x1, y2);
430 immVertex2f(shdr_pos, x2, y1 + (h - ofs));
436 static void drawviewborder(Scene *scene, Depsgraph *depsgraph, ARegion *ar, View3D *v3d)
438 float x1, x2, y1, y2;
439 float x1i, x2i, y1i, y2i;
443 RegionView3D *rv3d = ar->regiondata;
445 if (v3d->camera == NULL)
447 if (v3d->camera->type == OB_CAMERA)
448 ca = v3d->camera->data;
450 ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &viewborder, false);
452 x1 = viewborder.xmin;
453 y1 = viewborder.ymin;
454 x2 = viewborder.xmax;
455 y2 = viewborder.ymax;
457 GPU_line_width(1.0f);
459 /* apply offsets so the real 3D camera shows through */
461 /* note: quite un-scientific but without this bit extra
462 * 0.0001 on the lower left the 2D border sometimes
463 * obscures the 3D camera border */
464 /* note: with VIEW3D_CAMERA_BORDER_HACK defined this error isn't noticeable
465 * but keep it here in case we need to remove the workaround */
466 x1i = (int)(x1 - 1.0001f);
467 y1i = (int)(y1 - 1.0001f);
468 x2i = (int)(x2 + (1.0f - 0.0001f));
469 y2i = (int)(y2 + (1.0f - 0.0001f));
471 uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
473 /* First, solid lines. */
475 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
477 /* passepartout, specified in camera edit buttons */
478 if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001f) {
479 const float winx = (ar->winx + 1);
480 const float winy = (ar->winy + 1);
484 if (ca->passepartalpha != 1.0f) {
485 GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
487 alpha = ca->passepartalpha;
490 immUniformColor4f(0.0f, 0.0f, 0.0f, alpha);
493 immRectf(shdr_pos, 0.0f, winy, x1i, 0.0f);
495 immRectf(shdr_pos, x2i, winy, winx, 0.0f);
497 immRectf(shdr_pos, x1i, winy, x2i, y2i);
499 immRectf(shdr_pos, x1i, y1i, x2i, 0.0f);
504 immUniformThemeColor(TH_BACK);
505 imm_draw_box_wire_2d(shdr_pos, x1i, y1i, x2i, y2i);
507 #ifdef VIEW3D_CAMERA_BORDER_HACK
508 if (view3d_camera_border_hack_test == true) {
509 immUniformColor3ubv(view3d_camera_border_hack_col);
510 imm_draw_box_wire_2d(shdr_pos, x1i + 1, y1i + 1, x2i - 1, y2i - 1);
511 view3d_camera_border_hack_test = false;
518 /* When overlays are disabled, only show camera outline & passepartout. */
519 if (v3d->flag2 & V3D_RENDER_OVERRIDE) {
523 /* And now, the dashed lines! */
524 immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
527 float viewport_size[4];
528 GPU_viewport_size_get_f(viewport_size);
529 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
531 immUniform1i("colors_len", 0); /* "simple" mode */
532 immUniform1f("dash_width", 6.0f);
533 immUniform1f("dash_factor", 0.5f);
535 /* outer line not to confuse with object selection */
536 if (v3d->flag2 & V3D_LOCK_CAMERA) {
537 immUniformThemeColor(TH_REDALERT);
538 imm_draw_box_wire_2d(shdr_pos, x1i - 1, y1i - 1, x2i + 1, y2i + 1);
541 immUniformThemeColor(TH_VIEW_OVERLAY);
542 imm_draw_box_wire_2d(shdr_pos, x1i, y1i, x2i, y2i);
546 if (scene->r.mode & R_BORDER) {
547 float x3, y3, x4, y4;
549 x3 = floorf(x1 + (scene->r.border.xmin * (x2 - x1))) - 1;
550 y3 = floorf(y1 + (scene->r.border.ymin * (y2 - y1))) - 1;
551 x4 = floorf(x1 + (scene->r.border.xmax * (x2 - x1))) + (U.pixelsize - 1);
552 y4 = floorf(y1 + (scene->r.border.ymax * (y2 - y1))) + (U.pixelsize - 1);
554 immUniformColor3f(1.0f, 0.25f, 0.25f);
555 imm_draw_box_wire_2d(shdr_pos, x3, y3, x4, y4);
560 immUniformThemeColorBlend(TH_VIEW_OVERLAY, TH_BACK, 0.25f);
562 if (ca->dtx & CAM_DTX_CENTER) {
565 x3 = x1 + 0.5f * (x2 - x1);
566 y3 = y1 + 0.5f * (y2 - y1);
568 immBegin(GPU_PRIM_LINES, 4);
570 immVertex2f(shdr_pos, x1, y3);
571 immVertex2f(shdr_pos, x2, y3);
573 immVertex2f(shdr_pos, x3, y1);
574 immVertex2f(shdr_pos, x3, y2);
579 if (ca->dtx & CAM_DTX_CENTER_DIAG) {
580 immBegin(GPU_PRIM_LINES, 4);
582 immVertex2f(shdr_pos, x1, y1);
583 immVertex2f(shdr_pos, x2, y2);
585 immVertex2f(shdr_pos, x1, y2);
586 immVertex2f(shdr_pos, x2, y1);
591 if (ca->dtx & CAM_DTX_THIRDS) {
592 drawviewborder_grid3(shdr_pos, x1, x2, y1, y2, 1.0f / 3.0f);
595 if (ca->dtx & CAM_DTX_GOLDEN) {
596 drawviewborder_grid3(shdr_pos, x1, x2, y1, y2, 1.0f - (1.0f / 1.61803399f));
599 if (ca->dtx & CAM_DTX_GOLDEN_TRI_A) {
600 drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 0, 'A');
603 if (ca->dtx & CAM_DTX_GOLDEN_TRI_B) {
604 drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 0, 'B');
607 if (ca->dtx & CAM_DTX_HARMONY_TRI_A) {
608 drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 1, 'A');
611 if (ca->dtx & CAM_DTX_HARMONY_TRI_B) {
612 drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 1, 'B');
615 if (ca->flag & CAM_SHOW_SAFE_MARGINS) {
617 shdr_pos, x1, x2, y1, y2,
618 scene->safe_areas.title, scene->safe_areas.action);
620 if (ca->flag & CAM_SHOW_SAFE_CENTER) {
622 shdr_pos, x1, x2, y1, y2,
623 scene->safe_areas.title_center, scene->safe_areas.action_center);
627 if (ca->flag & CAM_SHOWSENSOR) {
628 /* determine sensor fit, and get sensor x/y, for auto fit we
629 * assume and square sensor and only use sensor_x */
630 float sizex = scene->r.xsch * scene->r.xasp;
631 float sizey = scene->r.ysch * scene->r.yasp;
632 int sensor_fit = BKE_camera_sensor_fit(ca->sensor_fit, sizex, sizey);
633 float sensor_x = ca->sensor_x;
634 float sensor_y = (ca->sensor_fit == CAMERA_SENSOR_FIT_AUTO) ? ca->sensor_x : ca->sensor_y;
636 /* determine sensor plane */
639 if (sensor_fit == CAMERA_SENSOR_FIT_HOR) {
640 float sensor_scale = (x2i - x1i) / sensor_x;
641 float sensor_height = sensor_scale * sensor_y;
645 rect.ymin = (y1i + y2i) * 0.5f - sensor_height * 0.5f;
646 rect.ymax = rect.ymin + sensor_height;
649 float sensor_scale = (y2i - y1i) / sensor_y;
650 float sensor_width = sensor_scale * sensor_x;
652 rect.xmin = (x1i + x2i) * 0.5f - sensor_width * 0.5f;
653 rect.xmax = rect.xmin + sensor_width;
659 immUniformThemeColorShade(TH_VIEW_OVERLAY, 100);
661 /* TODO Was using UI_draw_roundbox_4fv(false, rect.xmin, rect.ymin, rect.xmax, rect.ymax, 2.0f, color).
662 * We'll probably need a new imm_draw_line_roundbox_dashed dor that - though in practice the
663 * 2.0f round corner effect was nearly not visible anyway... */
664 imm_draw_box_wire_2d(shdr_pos, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
669 /* end dashed lines */
671 /* camera name - draw in highlighted text color */
672 if (ca && ((v3d->overlay.flag & V3D_OVERLAY_HIDE_TEXT) == 0) && (ca->flag & CAM_SHOWNAME)) {
673 UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
675 x1i, y1i - (0.7f * U.widget_unit), 0.0f,
676 v3d->camera->id.name + 2, sizeof(v3d->camera->id.name) - 2);
680 static void drawrenderborder(ARegion *ar, View3D *v3d)
682 /* use the same program for everything */
683 uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
685 GPU_line_width(1.0f);
687 immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
689 float viewport_size[4];
690 GPU_viewport_size_get_f(viewport_size);
691 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
693 immUniform1i("colors_len", 0); /* "simple" mode */
694 immUniform4f("color", 1.0f, 0.25f, 0.25f, 1.0f);
695 immUniform1f("dash_width", 6.0f);
696 immUniform1f("dash_factor", 0.5f);
698 imm_draw_box_wire_2d(shdr_pos,
699 v3d->render_border.xmin * ar->winx, v3d->render_border.ymin * ar->winy,
700 v3d->render_border.xmax * ar->winx, v3d->render_border.ymax * ar->winy);
705 void ED_view3d_draw_depth(
706 Depsgraph *depsgraph,
707 ARegion *ar, View3D *v3d, bool alphaoverride)
709 struct bThemeState theme_state;
710 Scene *scene = DEG_get_evaluated_scene(depsgraph);
711 RegionView3D *rv3d = ar->regiondata;
713 short flag = v3d->flag;
714 float glalphaclip = U.glalphaclip;
715 int obcenter_dia = U.obcenter_dia;
716 /* temp set drawtype to solid */
717 /* Setting these temporarily is not nice */
718 v3d->flag &= ~V3D_SELECT_OUTLINE;
719 U.glalphaclip = alphaoverride ? 0.5f : glalphaclip; /* not that nice but means we wont zoom into billboards */
722 /* Tools may request depth outside of regular drawing code. */
723 UI_Theme_Store(&theme_state);
724 UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
726 ED_view3d_draw_setup_view(NULL, depsgraph, scene, ar, v3d, NULL, NULL, NULL);
728 GPU_clear(GPU_DEPTH_BIT);
730 if (rv3d->rflag & RV3D_CLIPPING) {
731 ED_view3d_clipping_set(rv3d);
733 /* get surface depth without bias */
734 rv3d->rflag |= RV3D_ZOFFSET_DISABLED;
736 GPU_depth_test(true);
738 DRW_draw_depth_loop(depsgraph, ar, v3d);
740 if (rv3d->rflag & RV3D_CLIPPING) {
741 ED_view3d_clipping_disable();
743 rv3d->rflag &= ~RV3D_ZOFFSET_DISABLED;
745 /* Reset default for UI */
746 GPU_depth_test(false);
748 U.glalphaclip = glalphaclip;
750 U.obcenter_dia = obcenter_dia;
752 UI_Theme_Restore(&theme_state);
755 /* ******************** other elements ***************** */
757 /** could move this elsewhere, but tied into #ED_view3d_grid_scale */
758 float ED_scene_grid_scale(Scene *scene, const char **grid_unit)
761 if (scene->unit.system) {
765 bUnit_GetSystem(scene->unit.system, B_UNIT_LENGTH, &usys, &len);
768 int i = bUnit_GetBaseUnit(usys);
770 *grid_unit = bUnit_GetNameDisplay(usys, i);
771 return (float)bUnit_GetScaler(usys, i) / scene->unit.scale_length;
778 float ED_view3d_grid_scale(Scene *scene, View3D *v3d, const char **grid_unit)
780 return v3d->grid * ED_scene_grid_scale(scene, grid_unit);
783 /* Simulates the grid scale that is visualized by the shaders drawing functions.
784 * The actual code is seen in `object_grid_frag.glsl` when you get the `grid_res` value.
785 * Currently the simulation is done only when RV3D_VIEW_IS_AXIS. */
786 float ED_view3d_grid_view_scale(
787 Scene *scene, View3D *v3d, RegionView3D *rv3d, const char **grid_unit)
789 float grid_scale = ED_view3d_grid_scale(scene, v3d, grid_unit);
790 if (!rv3d->is_persp && RV3D_VIEW_IS_AXIS(rv3d->view)) {
791 /* Decrease the distance between grid snap points depending on zoom. */
792 float grid_subdiv = v3d->gridsubdiv;
793 if (grid_subdiv > 1) {
794 float grid_distance = rv3d->dist;
795 float lvl = (logf(grid_distance / grid_scale) / logf(grid_subdiv));
797 /* Negative values need an offset for correct casting.
798 * By convention, the minimum lvl is limited to -2 (see `objec_mode.c`) */
806 grid_scale *= pow(grid_subdiv, (int)lvl - 1);
813 static void draw_view_axis(RegionView3D *rv3d, const rcti *rect)
815 const float k = U.rvisize * U.pixelsize; /* axis size */
816 const int bright = - 20 * (10 - U.rvibright); /* axis alpha offset (rvibright has range 0-10) */
818 /* Axis center in screen coordinates.
820 * - Unit size offset so small text doesn't draw outside the screen
821 * - Extra X offset because of the panel expander.
823 const float startx = rect->xmax - (k + UI_UNIT_X * 1.5);
824 const float starty = rect->ymax - (k + UI_UNIT_Y);
826 float axis_pos[3][2];
827 unsigned char axis_col[3][4];
829 int axis_order[3] = {0, 1, 2};
830 axis_sort_v3(rv3d->viewinv[2], axis_order);
832 for (int axis_i = 0; axis_i < 3; axis_i++) {
833 int i = axis_order[axis_i];
835 /* get position of each axis tip on screen */
836 float vec[3] = { 0.0f };
838 mul_qt_v3(rv3d->viewquat, vec);
839 axis_pos[i][0] = startx + vec[0] * k;
840 axis_pos[i][1] = starty + vec[1] * k;
842 /* get color of each axis */
843 UI_GetThemeColorShade3ubv(TH_AXIS_X + i, bright, axis_col[i]); /* rgb */
844 axis_col[i][3] = 255 * hypotf(vec[0], vec[1]); /* alpha */
847 /* draw axis lines */
848 GPU_line_width(2.0f);
849 GPU_line_smooth(true);
851 GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
853 GPUVertFormat *format = immVertexFormat();
854 uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
855 uint col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
857 immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
858 immBegin(GPU_PRIM_LINES, 6);
860 for (int axis_i = 0; axis_i < 3; axis_i++) {
861 int i = axis_order[axis_i];
863 immAttr4ubv(col, axis_col[i]);
864 immVertex2f(pos, startx, starty);
865 immAttr4ubv(col, axis_col[i]);
866 immVertex2fv(pos, axis_pos[i]);
871 GPU_line_smooth(false);
873 /* draw axis names */
874 for (int axis_i = 0; axis_i < 3; axis_i++) {
875 int i = axis_order[axis_i];
877 const char axis_text[2] = {'x' + i, '\0'};
878 BLF_color4ubv(BLF_default(), axis_col[i]);
879 BLF_draw_default_ascii(axis_pos[i][0] + 2, axis_pos[i][1] + 2, 0.0f, axis_text, 1);
883 #ifdef WITH_INPUT_NDOF
884 /* draw center and axis of rotation for ongoing 3D mouse navigation */
885 static void draw_rotation_guide(const RegionView3D *rv3d)
887 float o[3]; /* center of rotation */
888 float end[3]; /* endpoints for drawing */
890 GLubyte color[4] = {0, 108, 255, 255}; /* bright blue so it matches device LEDs */
892 negate_v3_v3(o, rv3d->ofs);
895 GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
896 glDepthMask(GL_FALSE); /* don't overwrite zbuf */
898 GPUVertFormat *format = immVertexFormat();
899 uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
900 uint col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
902 immBindBuiltinProgram(GPU_SHADER_3D_SMOOTH_COLOR);
904 if (rv3d->rot_angle != 0.0f) {
905 /* -- draw rotation axis -- */
906 float scaled_axis[3];
907 const float scale = rv3d->dist;
908 mul_v3_v3fl(scaled_axis, rv3d->rot_axis, scale);
911 immBegin(GPU_PRIM_LINE_STRIP, 3);
912 color[3] = 0; /* more transparent toward the ends */
913 immAttr4ubv(col, color);
914 add_v3_v3v3(end, o, scaled_axis);
915 immVertex3fv(pos, end);
918 color[3] = 0.2f + fabsf(rv3d->rot_angle); /* modulate opacity with angle */
919 /* ^^ neat idea, but angle is frame-rate dependent, so it's usually close to 0.2 */
922 color[3] = 127; /* more opaque toward the center */
923 immAttr4ubv(col, color);
924 immVertex3fv(pos, o);
927 immAttr4ubv(col, color);
928 sub_v3_v3v3(end, o, scaled_axis);
929 immVertex3fv(pos, end);
932 /* -- draw ring around rotation center -- */
934 #define ROT_AXIS_DETAIL 13
936 const float s = 0.05f * scale;
937 const float step = 2.0f * (float)(M_PI / ROT_AXIS_DETAIL);
939 float q[4]; /* rotate ring so it's perpendicular to axis */
940 const int upright = fabsf(rv3d->rot_axis[2]) >= 0.95f;
942 const float up[3] = {0.0f, 0.0f, 1.0f};
943 float vis_angle, vis_axis[3];
945 cross_v3_v3v3(vis_axis, up, rv3d->rot_axis);
946 vis_angle = acosf(dot_v3v3(up, rv3d->rot_axis));
947 axis_angle_to_quat(q, vis_axis, vis_angle);
950 immBegin(GPU_PRIM_LINE_LOOP, ROT_AXIS_DETAIL);
951 color[3] = 63; /* somewhat faint */
952 immAttr4ubv(col, color);
954 for (int i = 0; i < ROT_AXIS_DETAIL; ++i, angle += step) {
955 float p[3] = {s * cosf(angle), s * sinf(angle), 0.0f};
962 immVertex3fv(pos, p);
966 #undef ROT_AXIS_DETAIL
969 color[3] = 255; /* solid dot */
972 color[3] = 127; /* see-through dot */
976 /* -- draw rotation center -- */
977 immBindBuiltinProgram(GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR);
978 GPU_point_size(5.0f);
979 immBegin(GPU_PRIM_POINTS, 1);
980 immAttr4ubv(col, color);
981 immVertex3fv(pos, o);
986 /* find screen coordinates for rotation center, then draw pretty icon */
987 mul_m4_v3(rv3d->persinv, rot_center);
988 UI_icon_draw(rot_center[0], rot_center[1], ICON_NDOF_TURN);
989 /* ^^ just playing around, does not work */
993 glDepthMask(GL_TRUE);
995 #endif /* WITH_INPUT_NDOF */
997 /* ******************** info ***************** */
1000 * Render and camera border
1002 static void view3d_draw_border(const bContext *C, ARegion *ar)
1004 Scene *scene = CTX_data_scene(C);
1005 Depsgraph *depsgraph = CTX_data_depsgraph(C);
1006 RegionView3D *rv3d = ar->regiondata;
1007 View3D *v3d = CTX_wm_view3d(C);
1009 if (rv3d->persp == RV3D_CAMOB) {
1010 drawviewborder(scene, depsgraph, ar, v3d);
1012 else if (v3d->flag2 & V3D_RENDER_BORDER) {
1013 drawrenderborder(ar, v3d);
1020 static void view3d_draw_grease_pencil(const bContext *UNUSED(C))
1028 static const char *view3d_get_name(View3D *v3d, RegionView3D *rv3d)
1030 const char *name = NULL;
1032 switch (rv3d->view) {
1033 case RV3D_VIEW_FRONT:
1034 if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Front Orthographic");
1035 else name = IFACE_("Front Perspective");
1037 case RV3D_VIEW_BACK:
1038 if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Back Orthographic");
1039 else name = IFACE_("Back Perspective");
1042 if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Top Orthographic");
1043 else name = IFACE_("Top Perspective");
1045 case RV3D_VIEW_BOTTOM:
1046 if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Bottom Orthographic");
1047 else name = IFACE_("Bottom Perspective");
1049 case RV3D_VIEW_RIGHT:
1050 if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Right Orthographic");
1051 else name = IFACE_("Right Perspective");
1053 case RV3D_VIEW_LEFT:
1054 if (rv3d->persp == RV3D_ORTHO) name = IFACE_("Left Orthographic");
1055 else name = IFACE_("Left Perspective");
1059 if (rv3d->persp == RV3D_CAMOB) {
1060 if ((v3d->camera) && (v3d->camera->type == OB_CAMERA)) {
1062 cam = v3d->camera->data;
1063 if (cam->type == CAM_PERSP) {
1064 name = IFACE_("Camera Perspective");
1066 else if (cam->type == CAM_ORTHO) {
1067 name = IFACE_("Camera Orthographic");
1070 BLI_assert(cam->type == CAM_PANO);
1071 name = IFACE_("Camera Panoramic");
1075 name = IFACE_("Object as Camera");
1079 name = (rv3d->persp == RV3D_ORTHO) ? IFACE_("User Orthographic") : IFACE_("User Perspective");
1086 static void draw_viewport_name(ARegion *ar, View3D *v3d, int xoffset, int *yoffset)
1088 RegionView3D *rv3d = ar->regiondata;
1089 const char *name = view3d_get_name(v3d, rv3d);
1090 const int font_id = BLF_default();
1092 /* increase size for unicode languages (Chinese in utf-8...) */
1093 #ifdef WITH_INTERNATIONAL
1099 BLF_enable(font_id, BLF_SHADOW);
1100 BLF_shadow(font_id, 5, (const float[4]){0.0f, 0.0f, 0.0f, 1.0f});
1101 BLF_shadow_offset(font_id, 1, -1);
1104 BLI_snprintf(tmpstr, sizeof(tmpstr), IFACE_("%s (Local)"), name);
1108 UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
1110 *yoffset -= U.widget_unit;
1112 #ifdef WITH_INTERNATIONAL
1113 BLF_draw_default(xoffset, *yoffset, 0.0f, name, sizeof(tmpstr));
1115 BLF_draw_default_ascii(xoffset, *yoffset, 0.0f, name, sizeof(tmpstr));
1118 BLF_disable(font_id, BLF_SHADOW);
1122 * draw info beside axes in bottom left-corner:
1123 * framenum, collection, object name, bone name (if available), marker name (if available)
1126 static void draw_selected_name(Scene *scene, ViewLayer *view_layer, Object *ob, int xoffset, int *yoffset)
1128 const int cfra = CFRA;
1129 const char *msg_pin = " (Pinned)";
1130 const char *msg_sep = " : ";
1132 const int font_id = BLF_default();
1137 s += sprintf(s, "(%d)", cfra);
1139 if ((ob == NULL) || (ob->mode == OB_MODE_OBJECT)) {
1140 LayerCollection *layer_collection = view_layer->active_collection;
1141 s += sprintf(s, " %s%s", BKE_collection_ui_name_get(layer_collection->collection), (ob == NULL) ? "" : " |");
1147 * - a collection name (MAX_NAME + 3)
1148 * - 3 object names (MAX_NAME)
1149 * - 2 BREAD_CRUMB_SEPARATORs (6)
1150 * - a SHAPE_KEY_PINNED marker and a trailing '\0' (9+1) - translated, so give some room!
1151 * - a marker name (MAX_NAME + 3)
1154 /* get name of marker on current frame (if available) */
1155 const char *markern = BKE_scene_find_marker_name(scene, cfra);
1157 /* check if there is an object */
1160 s += BLI_strcpy_rlen(s, ob->id.name + 2);
1162 /* name(s) to display depends on type of object */
1163 if (ob->type == OB_ARMATURE) {
1164 bArmature *arm = ob->data;
1166 /* show name of active bone too (if possible) */
1168 if (arm->act_edbone) {
1169 s += BLI_strcpy_rlen(s, msg_sep);
1170 s += BLI_strcpy_rlen(s, arm->act_edbone->name);
1173 else if (ob->mode & OB_MODE_POSE) {
1174 if (arm->act_bone) {
1176 if (arm->act_bone->layer & arm->layer) {
1177 s += BLI_strcpy_rlen(s, msg_sep);
1178 s += BLI_strcpy_rlen(s, arm->act_bone->name);
1183 else if (ELEM(ob->type, OB_MESH, OB_LATTICE, OB_CURVE)) {
1184 /* try to display active bone and active shapekey too (if they exist) */
1186 if (ob->type == OB_MESH && ob->mode & OB_MODE_WEIGHT_PAINT) {
1187 Object *armobj = BKE_object_pose_armature_get(ob);
1188 if (armobj && armobj->mode & OB_MODE_POSE) {
1189 bArmature *arm = armobj->data;
1190 if (arm->act_bone) {
1191 if (arm->act_bone->layer & arm->layer) {
1192 s += BLI_strcpy_rlen(s, msg_sep);
1193 s += BLI_strcpy_rlen(s, arm->act_bone->name);
1199 Key *key = BKE_key_from_object(ob);
1201 KeyBlock *kb = BLI_findlink(&key->block, ob->shapenr - 1);
1203 s += BLI_strcpy_rlen(s, msg_sep);
1204 s += BLI_strcpy_rlen(s, kb->name);
1205 if (ob->shapeflag & OB_SHAPE_LOCK) {
1206 s += BLI_strcpy_rlen(s, IFACE_(msg_pin));
1212 /* color depends on whether there is a keyframe */
1213 if (id_frame_has_keyframe((ID *)ob, /* BKE_scene_frame_get(scene) */ (float)cfra, ANIMFILTER_KEYS_LOCAL))
1214 UI_FontThemeColor(font_id, TH_TIME_KEYFRAME);
1215 else if (ED_gpencil_has_keyframe_v3d(scene, ob, cfra))
1216 UI_FontThemeColor(font_id, TH_TIME_GP_KEYFRAME);
1218 UI_FontThemeColor(font_id, TH_TEXT_HI);
1222 if (ED_gpencil_has_keyframe_v3d(scene, NULL, cfra))
1223 UI_FontThemeColor(font_id, TH_TIME_GP_KEYFRAME);
1225 UI_FontThemeColor(font_id, TH_TEXT_HI);
1229 s += sprintf(s, " <%s>", markern);
1232 BLF_enable(font_id, BLF_SHADOW);
1233 BLF_shadow(font_id, 5, (const float[4]){0.0f, 0.0f, 0.0f, 1.0f});
1234 BLF_shadow_offset(font_id, 1, -1);
1236 *yoffset -= U.widget_unit;
1237 BLF_draw_default(xoffset, *yoffset, 0.0f, info, sizeof(info));
1239 BLF_disable(font_id, BLF_SHADOW);
1242 /* ******************** view loop ***************** */
1245 * Information drawn on top of the solid plates and composed data
1247 void view3d_draw_region_info(const bContext *C, ARegion *ar)
1249 RegionView3D *rv3d = ar->regiondata;
1250 View3D *v3d = CTX_wm_view3d(C);
1251 Scene *scene = CTX_data_scene(C);
1252 wmWindowManager *wm = CTX_wm_manager(C);
1254 #ifdef WITH_INPUT_NDOF
1255 if ((U.ndof_flag & NDOF_SHOW_GUIDE) &&
1256 ((rv3d->viewlock & RV3D_LOCKED) == 0) &&
1257 (rv3d->persp != RV3D_CAMOB))
1259 /* TODO: draw something else (but not this) during fly mode */
1260 draw_rotation_guide(rv3d);
1264 /* correct projection matrix */
1265 ED_region_pixelspace(ar);
1267 /* local coordinate visible rect inside region, to accommodate overlapping ui */
1269 ED_region_visible_rect(ar, &rect);
1272 view3d_draw_border(C, ar);
1273 view3d_draw_grease_pencil(C);
1275 BLF_batch_draw_begin();
1277 if ((U.uiflag & USER_SHOW_GIZMO_AXIS) ||
1278 (v3d->flag2 & V3D_RENDER_OVERRIDE) ||
1279 /* No need to display gizmo and this info. */
1280 (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_NAVIGATE)))
1285 draw_view_axis(rv3d, &rect);
1288 int xoffset = rect.xmin + U.widget_unit;
1289 int yoffset = rect.ymax;
1291 if ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0 &&
1292 (v3d->overlay.flag & V3D_OVERLAY_HIDE_TEXT) == 0)
1294 if ((U.uiflag & USER_SHOW_FPS) && ED_screen_animation_no_scrub(wm)) {
1295 ED_scene_draw_fps(scene, xoffset, &yoffset);
1297 else if (U.uiflag & USER_SHOW_VIEWPORTNAME) {
1298 draw_viewport_name(ar, v3d, xoffset, &yoffset);
1301 if (U.uiflag & USER_DRAWVIEWINFO) {
1302 ViewLayer *view_layer = CTX_data_view_layer(C);
1303 Object *ob = OBACT(view_layer);
1304 draw_selected_name(scene, view_layer, ob, xoffset, &yoffset);
1308 if (grid_unit) { /* draw below the viewport name */
1309 char numstr[32] = "";
1311 UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
1312 if (v3d->grid != 1.0f) {
1313 BLI_snprintf(numstr, sizeof(numstr), "%s x %.4g", grid_unit, v3d->grid);
1316 *yoffset -= U.widget_unit;
1317 BLF_draw_default_ascii(xoffset, *yoffset, numstr[0] ? numstr : grid_unit, sizeof(numstr));
1322 if ((v3d->overlay.flag & V3D_OVERLAY_HIDE_TEXT) == 0) {
1323 DRW_draw_region_engine_info(xoffset, yoffset);
1326 BLF_batch_draw_end();
1329 static void view3d_draw_view(const bContext *C, ARegion *ar)
1331 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);
1333 /* Only 100% compliant on new spec goes below */
1337 RenderEngineType *ED_view3d_engine_type(Scene *scene, int drawtype)
1340 * Temporary viewport draw modes until we have a proper system.
1341 * all modes are done in the draw manager, except
1342 * cycles material as it is an external render engine.
1344 if (strcmp(scene->r.engine, RE_engine_id_CYCLES) == 0 && drawtype == OB_MATERIAL) {
1345 return RE_engines_find(RE_engine_id_BLENDER_EEVEE);
1347 return RE_engines_find(scene->r.engine);
1350 void view3d_main_region_draw(const bContext *C, ARegion *ar)
1352 Main *bmain = CTX_data_main(C);
1353 View3D *v3d = CTX_wm_view3d(C);
1355 view3d_draw_view(C, ar);
1357 GPU_free_images_old(bmain);
1358 GPU_pass_cache_garbage_collect();
1360 /* XXX This is in order to draw UI batches with the DRW
1361 * olg context since we now use it for drawing the entire area */
1362 gpu_batch_presets_reset();
1364 /* No depth test for drawing action zones afterwards. */
1365 GPU_depth_test(false);
1367 v3d->flag |= V3D_INVALID_BACKBUF;
1370 /* -------------------------------------------------------------------- */
1372 /** \name Offscreen Drawing
1375 static void view3d_stereo3d_setup_offscreen(
1376 Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *ar,
1377 float winmat[4][4], const char *viewname)
1379 /* update the viewport matrices with the new camera */
1380 if (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D) {
1381 float viewmat[4][4];
1382 const bool is_left = STREQ(viewname, STEREO_LEFT_NAME);
1384 BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat);
1385 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, NULL);
1387 else { /* SCE_VIEWS_FORMAT_MULTIVIEW */
1388 float viewmat[4][4];
1389 Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
1391 BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat);
1392 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, NULL);
1396 void ED_view3d_draw_offscreen(
1397 Depsgraph *depsgraph, Scene *scene,
1399 View3D *v3d, ARegion *ar, int winx, int winy,
1400 float viewmat[4][4], float winmat[4][4],
1401 bool do_sky, bool UNUSED(is_persp), const char *viewname,
1402 GPUFXSettings *UNUSED(fx_settings),
1403 GPUOffScreen *ofs, GPUViewport *viewport)
1405 RegionView3D *rv3d = ar->regiondata;
1406 RenderEngineType *engine_type = ED_view3d_engine_type(scene, drawtype);
1408 /* set temporary new size */
1409 int bwinx = ar->winx;
1410 int bwiny = ar->winy;
1411 rcti brect = ar->winrct;
1415 ar->winrct.xmin = 0;
1416 ar->winrct.ymin = 0;
1417 ar->winrct.xmax = winx;
1418 ar->winrct.ymax = winy;
1420 struct bThemeState theme_state;
1421 UI_Theme_Store(&theme_state);
1422 UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
1425 G.f |= G_RENDER_OGL;
1427 if ((v3d->flag2 & V3D_RENDER_SHADOW) == 0) {
1428 /* free images which can have changed on frame-change
1429 * warning! can be slow so only free animated images - campbell */
1430 GPU_free_images_anim(G.main); /* XXX :((( */
1433 GPU_matrix_push_projection();
1434 GPU_matrix_identity_set();
1436 GPU_matrix_identity_set();
1438 if ((viewname != NULL && viewname[0] != '\0') && (viewmat == NULL) && rv3d->persp == RV3D_CAMOB && v3d->camera)
1439 view3d_stereo3d_setup_offscreen(depsgraph, scene, v3d, ar, winmat, viewname);
1441 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, NULL);
1443 /* main drawing call */
1444 DRW_draw_render_loop_offscreen(
1445 depsgraph, engine_type, ar, v3d,
1446 do_sky, ofs, viewport);
1453 GPU_matrix_pop_projection();
1456 UI_Theme_Restore(&theme_state);
1458 G.f &= ~G_RENDER_OGL;
1462 * Utility func for ED_view3d_draw_offscreen
1464 * \param ofs: Optional off-screen buffer, can be NULL.
1465 * (avoids re-creating when doing multiple GL renders).
1467 ImBuf *ED_view3d_draw_offscreen_imbuf(
1468 Depsgraph *depsgraph, Scene *scene,
1470 View3D *v3d, ARegion *ar, int sizex, int sizey,
1471 unsigned int flag, unsigned int draw_flags,
1472 int alpha_mode, int samples, const char *viewname,
1474 GPUOffScreen *ofs, char err_out[256])
1476 RegionView3D *rv3d = ar->regiondata;
1477 const bool draw_sky = (alpha_mode == R_ADDSKY);
1478 const bool use_full_sample = (draw_flags & V3D_OFSDRAW_USE_FULL_SAMPLE);
1481 GPUFXSettings fx_settings = v3d->fx_settings;
1482 bool is_ortho = false;
1485 if (ofs && ((GPU_offscreen_width(ofs) != sizex) || (GPU_offscreen_height(ofs) != sizey))) {
1486 /* sizes differ, can't reuse */
1490 GPUFrameBuffer *old_fb = GPU_framebuffer_active_get();
1493 GPU_framebuffer_restore();
1496 const bool own_ofs = (ofs == NULL);
1497 DRW_opengl_context_enable();
1501 ofs = GPU_offscreen_create(sizex, sizey, use_full_sample ? 0 : samples, true, false, err_out);
1503 DRW_opengl_context_disable();
1508 GPU_offscreen_bind(ofs, true);
1510 /* read in pixels & stamp */
1511 ImBuf *ibuf = IMB_allocImBuf(sizex, sizey, 32, flag);
1513 /* render 3d view */
1514 if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
1515 CameraParams params;
1516 Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
1517 const Object *camera_eval = DEG_get_evaluated_object(
1521 BKE_camera_params_init(¶ms);
1522 /* fallback for non camera objects */
1523 params.clipsta = v3d->near;
1524 params.clipend = v3d->far;
1525 BKE_camera_params_from_object(¶ms, camera_eval);
1526 BKE_camera_multiview_params(&scene->r, ¶ms, camera_eval, viewname);
1527 BKE_camera_params_compute_viewplane(¶ms, sizex, sizey, scene->r.xasp, scene->r.yasp);
1528 BKE_camera_params_compute_matrix(¶ms);
1530 BKE_camera_to_gpu_dof(camera, &fx_settings);
1532 is_ortho = params.is_ortho;
1533 copy_m4_m4(winmat, params.winmat);
1537 float clipsta, clipend;
1539 is_ortho = ED_view3d_viewplane_get(depsgraph, v3d, rv3d, sizex, sizey, &viewplane, &clipsta, &clipend, NULL);
1541 orthographic_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, -clipend, clipend);
1544 perspective_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, clipsta, clipend);
1548 if ((samples && use_full_sample) == 0) {
1549 /* Single-pass render, common case */
1550 ED_view3d_draw_offscreen(
1551 depsgraph, scene, drawtype,
1552 v3d, ar, sizex, sizey, NULL, winmat,
1553 draw_sky, !is_ortho, viewname,
1554 &fx_settings, ofs, NULL);
1556 if (ibuf->rect_float) {
1557 GPU_offscreen_read_pixels(ofs, GL_FLOAT, ibuf->rect_float);
1559 else if (ibuf->rect) {
1560 GPU_offscreen_read_pixels(ofs, GL_UNSIGNED_BYTE, ibuf->rect);
1564 /* Multi-pass render, use accumulation buffer & jitter for 'full' oversampling.
1565 * Use because OpenGL may use a lower quality MSAA, and only over-sample edges. */
1566 static float jit_ofs[32][2];
1567 float winmat_jitter[4][4];
1568 float *rect_temp = (ibuf->rect_float) ? ibuf->rect_float : MEM_mallocN(sizex * sizey * sizeof(float[4]), "rect_temp");
1569 float *accum_buffer = MEM_mallocN(sizex * sizey * sizeof(float[4]), "accum_buffer");
1570 GPUViewport *viewport = GPU_viewport_create_from_offscreen(ofs);
1572 BLI_jitter_init(jit_ofs, samples);
1574 /* first sample buffer, also initializes 'rv3d->persmat' */
1575 ED_view3d_draw_offscreen(
1576 depsgraph, scene, drawtype,
1577 v3d, ar, sizex, sizey, NULL, winmat,
1578 draw_sky, !is_ortho, viewname,
1579 &fx_settings, ofs, viewport);
1580 GPU_offscreen_read_pixels(ofs, GL_FLOAT, accum_buffer);
1582 /* skip the first sample */
1583 for (int j = 1; j < samples; j++) {
1584 copy_m4_m4(winmat_jitter, winmat);
1585 window_translate_m4(
1586 winmat_jitter, rv3d->persmat,
1587 (jit_ofs[j][0] * 2.0f) / sizex,
1588 (jit_ofs[j][1] * 2.0f) / sizey);
1590 ED_view3d_draw_offscreen(
1591 depsgraph, scene, drawtype,
1592 v3d, ar, sizex, sizey, NULL, winmat_jitter,
1593 draw_sky, !is_ortho, viewname,
1594 &fx_settings, ofs, viewport);
1595 GPU_offscreen_read_pixels(ofs, GL_FLOAT, rect_temp);
1597 unsigned int i = sizex * sizey * 4;
1599 accum_buffer[i] += rect_temp[i];
1604 /* don't free data owned by 'ofs' */
1605 GPU_viewport_clear_from_offscreen(viewport);
1606 GPU_viewport_free(viewport);
1609 if (ibuf->rect_float == NULL) {
1610 MEM_freeN(rect_temp);
1613 if (ibuf->rect_float) {
1614 float *rect_float = ibuf->rect_float;
1615 unsigned int i = sizex * sizey * 4;
1617 rect_float[i] = accum_buffer[i] / samples;
1621 unsigned char *rect_ub = (unsigned char *)ibuf->rect;
1622 unsigned int i = sizex * sizey * 4;
1624 rect_ub[i] = (unsigned char)(255.0f * accum_buffer[i] / samples);
1628 MEM_freeN(accum_buffer);
1632 GPU_offscreen_unbind(ofs, true);
1635 GPU_offscreen_free(ofs);
1638 DRW_opengl_context_disable();
1641 GPU_framebuffer_bind(old_fb);
1644 if (ibuf->rect_float && ibuf->rect)
1645 IMB_rect_from_float(ibuf);
1651 * Creates own fake 3d views (wrapping #ED_view3d_draw_offscreen_imbuf)
1653 * \param ofs: Optional off-screen buffer can be NULL.
1654 * (avoids re-creating when doing multiple GL renders).
1656 * \note used by the sequencer
1658 ImBuf *ED_view3d_draw_offscreen_imbuf_simple(
1659 Depsgraph *depsgraph, Scene *scene,
1661 Object *camera, int width, int height,
1662 unsigned int flag, unsigned int draw_flags,
1663 int alpha_mode, int samples, const char *viewname,
1664 GPUOffScreen *ofs, char err_out[256])
1666 View3D v3d = {NULL};
1667 ARegion ar = {NULL};
1668 RegionView3D rv3d = {{{0}}};
1671 v3d.regionbase.first = v3d.regionbase.last = &ar;
1672 ar.regiondata = &rv3d;
1673 ar.regiontype = RGN_TYPE_WINDOW;
1675 v3d.camera = camera;
1676 v3d.shading.type = drawtype;
1677 v3d.flag2 = V3D_RENDER_OVERRIDE;
1679 if (draw_flags & V3D_OFSDRAW_USE_GPENCIL) {
1680 v3d.flag2 |= V3D_SHOW_ANNOTATION;
1682 if (draw_flags & V3D_OFSDRAW_USE_SOLID_TEX) {
1683 v3d.flag2 |= V3D_SOLID_TEX;
1686 v3d.shading.background_type = V3D_SHADING_BACKGROUND_WORLD;
1688 if (draw_flags & V3D_OFSDRAW_USE_CAMERA_DOF) {
1689 if (camera->type == OB_CAMERA) {
1690 v3d.fx_settings.dof = &((Camera *)camera->data)->gpu_dof;
1691 v3d.fx_settings.fx_flag |= GPU_FX_FLAG_DOF;
1695 rv3d.persp = RV3D_CAMOB;
1697 copy_m4_m4(rv3d.viewinv, v3d.camera->obmat);
1698 normalize_m4(rv3d.viewinv);
1699 invert_m4_m4(rv3d.viewmat, rv3d.viewinv);
1702 CameraParams params;
1703 const Object *view_camera_eval = DEG_get_evaluated_object(
1705 BKE_camera_multiview_render(scene, v3d.camera, viewname));
1707 BKE_camera_params_init(¶ms);
1708 BKE_camera_params_from_object(¶ms, view_camera_eval);
1709 BKE_camera_multiview_params(&scene->r, ¶ms, view_camera_eval, viewname);
1710 BKE_camera_params_compute_viewplane(¶ms, width, height, scene->r.xasp, scene->r.yasp);
1711 BKE_camera_params_compute_matrix(¶ms);
1713 copy_m4_m4(rv3d.winmat, params.winmat);
1714 v3d.near = params.clipsta;
1715 v3d.far = params.clipend;
1716 v3d.lens = params.lens;
1719 mul_m4_m4m4(rv3d.persmat, rv3d.winmat, rv3d.viewmat);
1720 invert_m4_m4(rv3d.persinv, rv3d.viewinv);
1722 return ED_view3d_draw_offscreen_imbuf(
1723 depsgraph, scene, drawtype,
1724 &v3d, &ar, width, height, flag,
1725 draw_flags, alpha_mode, samples, viewname, ofs, err_out);