Untitled diff

Created Diff never expires
/*
/*
* ***** BEGIN GPL LICENSE BLOCK *****
* ***** BEGIN GPL LICENSE BLOCK *****
*
*
* This program is free software; you can redistribute it and/or
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* of the License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* GNU General Public License for more details.
*
*
* You should have received a copy of the GNU General Public License
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
* The Original Code is Copyright (C) 2008 Blender Foundation.
* The Original Code is Copyright (C) 2008 Blender Foundation.
* All rights reserved.
* All rights reserved.
*
*
*
*
* Contributor(s): Blender Foundation
* Contributor(s): Blender Foundation
*
*
* ***** END GPL LICENSE BLOCK *****
* ***** END GPL LICENSE BLOCK *****
*/
*/


/** \file blender/editors/space_view3d/view3d_draw.c
/** \file blender/editors/space_view3d/view3d_draw.c
* \ingroup spview3d
* \ingroup spview3d
*/
*/


#include <math.h>
#include <math.h>


#include "BLI_listbase.h"
#include "BLI_listbase.h"
#include "BLI_math.h"
#include "BLI_math.h"
#include "BLI_rect.h"
#include "BLI_rect.h"
#include "BLI_string.h"
#include "BLI_string.h"
#include "BLI_threads.h"
#include "BLI_threads.h"
#include "BLI_jitter_2d.h"
#include "BLI_jitter_2d.h"


#include "BIF_gl.h"
#include "BIF_gl.h"
#include "BIF_glutil.h"
#include "BIF_glutil.h"


#include "BKE_camera.h"
#include "BKE_camera.h"
#include "BKE_collection.h"
#include "BKE_collection.h"
#include "BKE_context.h"
#include "BKE_context.h"
#include "BKE_global.h"
#include "BKE_global.h"
#include "BKE_key.h"
#include "BKE_key.h"
#include "BKE_main.h"
#include "BKE_main.h"
#include "BKE_scene.h"
#include "BKE_scene.h"
#include "BKE_object.h"
#include "BKE_object.h"
#include "BKE_paint.h"
#include "BKE_paint.h"
#include "BKE_unit.h"
#include "BKE_unit.h"


#include "BLF_api.h"
#include "BLF_api.h"


#include "BLT_translation.h"
#include "BLT_translation.h"


#include "DNA_armature_types.h"
#include "DNA_armature_types.h"
#include "DNA_brush_types.h"
#include "DNA_brush_types.h"
#include "DNA_camera_types.h"
#include "DNA_camera_types.h"
#include "DNA_key_types.h"
#include "DNA_key_types.h"
#include "DNA_mesh_types.h"
#include "DNA_mesh_types.h"
#include "DNA_object_types.h"
#include "DNA_object_types.h"
#include "DNA_view3d_types.h"
#include "DNA_view3d_types.h"
#include "DNA_windowmanager_types.h"
#include "DNA_windowmanager_types.h"


#include "DRW_engine.h"
#include "DRW_engine.h"


#include "ED_armature.h"
#include "ED_armature.h"
#include "ED_keyframing.h"
#include "ED_keyframing.h"
#include "ED_gpencil.h"
#include "ED_gpencil.h"
#include "ED_screen.h"
#include "ED_screen.h"
#include "ED_transform.h"
#include "ED_transform.h"


#include "DEG_depsgraph_query.h"
#include "DEG_depsgraph_query.h"


#include "GPU_batch.h"
#include "GPU_batch.h"
#include "GPU_batch_presets.h"
#include "GPU_batch_presets.h"
#include "GPU_draw.h"
#include "GPU_draw.h"
#include "GPU_matrix.h"
#include "GPU_matrix.h"
#include "GPU_immediate.h"
#include "GPU_immediate.h"
#include "GPU_immediate_util.h"
#include "GPU_immediate_util.h"
#include "GPU_material.h"
#include "GPU_material.h"
#include "GPU_viewport.h"
#include "GPU_viewport.h"
#include "GPU_state.h"
#include "GPU_state.h"
#include "GPU_framebuffer.h"
#include "GPU_framebuffer.h"


#include "MEM_guardedalloc.h"
#include "MEM_guardedalloc.h"


#include "UI_interface.h"
#include "UI_interface.h"
#include "UI_resources.h"
#include "UI_resources.h"


#include "RE_engine.h"
#include "RE_engine.h"


#include "WM_api.h"
#include "WM_api.h"
#include "WM_types.h"
#include "WM_types.h"


#include "RNA_access.h"
#include "RNA_access.h"


#include "IMB_imbuf.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
#include "IMB_imbuf_types.h"


#include "view3d_intern.h" /* own include */
#include "view3d_intern.h" /* own include */


/* ******************** general functions ***************** */
/* ******************** general functions ***************** */


/**
/**
* \note keep this synced with #ED_view3d_mats_rv3d_backup/#ED_view3d_mats_rv3d_restore
* \note keep this synced with #ED_view3d_mats_rv3d_backup/#ED_view3d_mats_rv3d_restore
*/
*/
void ED_view3d_update_viewmat(
void ED_view3d_update_viewmat(
Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *ar,
Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *ar,
float viewmat[4][4], float winmat[4][4], const rcti *rect)
float viewmat[4][4], float winmat[4][4], const rcti *rect)
{
{
RegionView3D *rv3d = ar->regiondata;
RegionView3D *rv3d = ar->regiondata;


/* setup window matrices */
/* setup window matrices */
if (winmat)
if (winmat)
copy_m4_m4(rv3d->winmat, winmat);
copy_m4_m4(rv3d->winmat, winmat);
else
else
view3d_winmatrix_set(depsgraph, ar, v3d, rect);
view3d_winmatrix_set(depsgraph, ar, v3d, rect);


/* setup view matrix */
/* setup view matrix */
if (viewmat) {
if (viewmat) {
copy_m4_m4(rv3d->viewmat, viewmat);
copy_m4_m4(rv3d->viewmat, viewmat);
}
}
else {
else {
float rect_scale[2];
float rect_scale[2];
if (rect) {
if (rect) {
rect_scale[0] = (float)BLI_rcti_size_x(rect) / (float)ar->winx;
rect_scale[0] = (float)BLI_rcti_size_x(rect) / (float)ar->winx;
rect_scale[1] = (float)BLI_rcti_size_y(rect) / (float)ar->winy;
rect_scale[1] = (float)BLI_rcti_size_y(rect) / (float)ar->winy;
}
}
/* note: calls BKE_object_where_is_calc for camera... */
/* note: calls BKE_object_where_is_calc for camera... */
view3d_viewmatrix_set(depsgraph, scene, v3d, rv3d, rect ? rect_scale : NULL);
view3d_viewmatrix_set(depsgraph, scene, v3d, rv3d, rect ? rect_scale : NULL);
}
}
/* update utility matrices */
/* update utility matrices */
mul_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
mul_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
invert_m4_m4(rv3d->persinv, rv3d->persmat);
invert_m4_m4(rv3d->persinv, rv3d->persmat);
invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
invert_m4_m4(rv3d->viewinv, rv3d->viewmat);


/* calculate GLSL view dependent values */
/* calculate GLSL view dependent values */


/* store window coordinates scaling/offset */
/* store window coordinates scaling/offset */
if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
rctf cameraborder;
rctf cameraborder;
ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &cameraborder, false);
ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &cameraborder, false);
rv3d->viewcamtexcofac[0] = (float)ar->winx / BLI_rctf_size_x(&cameraborder);
rv3d->viewcamtexcofac[0] = (float)ar->winx / BLI_rctf_size_x(&cameraborder);
rv3d->viewcamtexcofac[1] = (float)ar->winy / BLI_rctf_size_y(&cameraborder);
rv3d->viewcamtexcofac[1] = (float)ar->winy / BLI_rctf_size_y(&cameraborder);


rv3d->viewcamtexcofac[2] = -rv3d->viewcamtexcofac[0] * cameraborder.xmin / (float)ar->winx;
rv3d->viewcamtexcofac[2] = -rv3d->viewcamtexcofac[0] * cameraborder.xmin / (float)ar->winx;
rv3d->viewcamtexcofac[3] = -rv3d->viewcamtexcofac[1] * cameraborder.ymin / (float)ar->winy;
rv3d->viewcamtexcofac[3] = -rv3d->viewcamtexcofac[1] * cameraborder.ymin / (float)ar->winy;
}
}
else {
else {
rv3d->viewcamtexcofac[0] = rv3d->viewcamtexcofac[1] = 1.0f;
rv3d->viewcamtexcofac[0] = rv3d->viewcamtexcofac[1] = 1.0f;
rv3d->viewcamtexcofac[2] = rv3d->viewcamtexcofac[3] = 0.0f;
rv3d->viewcamtexcofac[2] = rv3d->viewcamtexcofac[3] = 0.0f;
}
}


/* calculate pixelsize factor once, is used for lamps and obcenters */
/* calculate pixelsize factor once, is used for lamps and obcenters */
{
{
/* note: '1.0f / len_v3(v1)' replaced 'len_v3(rv3d->viewmat[0])'
/* note: '1.0f / len_v3(v1)' replaced 'len_v3(rv3d->viewmat[0])'
* because of float point precision problems at large values [#23908] */
* because of float point precision problems at large values [#23908] */
float v1[3], v2[3];
float v1[3], v2[3];
float len_px, len_sc;
float len_px, len_sc;


v1[0] = rv3d->persmat[0][0];
v1[0] = rv3d->persmat[0][0];
v1[1] = rv3d->persmat[1][0];
v1[1] = rv3d->persmat[1][0];
v1[2] = rv3d->persmat[2][0];
v1[2] = rv3d->persmat[2][0];


v2[0] = rv3d->persmat[0][1];
v2[0] = rv3d->persmat[0][1];
v2[1] = rv3d->persmat[1][1];
v2[1] = rv3d->persmat[1][1];
v2[2] = rv3d->persmat[2][1];
v2[2] = rv3d->persmat[2][1];


len_px = 2.0f / sqrtf(min_ff(len_squared_v3(v1), len_squared_v3(v2)));
len_px = 2.0f / sqrtf(min_ff(len_squared_v3(v1), len_squared_v3(v2)));
len_sc = (float)MAX2(ar->winx, ar->winy);
len_sc = (float)MAX2(ar->winx, ar->winy);


rv3d->pixsize = len_px / len_sc;
rv3d->pixsize = len_px / len_sc;
}
}
}
}


static void view3d_main_region_setup_view(
static void view3d_main_region_setup_view(
Depsgraph *depsgraph, Scene *scene,
Depsgraph *depsgraph, Scene *scene,
View3D *v3d, ARegion *ar, float viewmat[4][4], float winmat[4][4], const rcti *rect)
View3D *v3d, ARegion *ar, float viewmat[4][4], float winmat[4][4], const rcti *rect)
{
{
RegionView3D *rv3d = ar->regiondata;
RegionView3D *rv3d = ar->regiondata;


ED_view3d_update_viewmat(depsgraph, scene, v3d, ar, viewmat, winmat, rect);
ED_view3d_update_viewmat(depsgraph, scene, v3d, ar, viewmat, winmat, rect);


/* set for opengl */
/* set for opengl */
GPU_matrix_projection_set(rv3d->winmat);
GPU_matrix_projection_set(rv3d->winmat);
GPU_matrix_set(rv3d->viewmat);
GPU_matrix_set(rv3d->viewmat);
}
}


static bool view3d_stereo3d_active(wmWindow *win, Scene *scene, View3D *v3d, RegionView3D *rv3d)
static bool view3d_stereo3d_active(wmWindow *win, Scene *scene, View3D *v3d, RegionView3D *rv3d)
{
{
if ((scene->r.scemode & R_MULTIVIEW) == 0) {
if ((scene->r.scemode & R_MULTIVIEW) == 0) {
return false;
return false;
}
}


if ((v3d->camera == NULL) || (v3d->camera->type != OB_CAMERA) || rv3d->persp != RV3D_CAMOB) {
if ((v3d->camera == NULL) || (v3d->camera->type != OB_CAMERA) || rv3d->persp != RV3D_CAMOB) {
return false;
return false;
}
}


switch (v3d->stereo3d_camera) {
switch (v3d->stereo3d_camera) {
case STEREO_MONO_ID:
case STEREO_MONO_ID:
return false;
return false;
break;
break;
case STEREO_3D_ID:
case STEREO_3D_ID:
/* win will be NULL when calling this from the selection or draw loop. */
/* win will be NULL when calling this from the selection or draw loop. */
if ((win == NULL) || (WM_stereo3d_enabled(win, true) == false)) {
if ((win == NULL) || (WM_stereo3d_enabled(win, true) == false)) {
return false;
return false;
}
}
if (((scene->r.views_format & SCE_VIEWS_FORMAT_MULTIVIEW) != 0) &&
if (((scene->r.views_format & SCE_VIEWS_FORMAT_MULTIVIEW) != 0) &&
!BKE_scene_multiview_is_stereo3d(&scene->r))
!BKE_scene_multiview_is_stereo3d(&scene->r))
{
{
return false;
return false;
}
}
break;
break;
/* We always need the stereo calculation for left and right cameras. */
/* We always need the stereo calculation for left and right cameras. */
case STEREO_LEFT_ID:
case STEREO_LEFT_ID:
case STEREO_RIGHT_ID:
case STEREO_RIGHT_ID:
default:
default:
break;
break;
}
}
return true;
return true;
}
}




/* setup the view and win matrices for the multiview cameras
/* setup the view and win matrices for the multiview cameras
*
*
* unlike view3d_stereo3d_setup_offscreen, when view3d_stereo3d_setup is called
* unlike view3d_stereo3d_setup_offscreen, when view3d_stereo3d_setup is called
* we have no winmatrix (i.e., projection matrix) defined at that time.
* we have no winmatrix (i.e., projection matrix) defined at that time.
* Since the camera and the camera shift are needed for the winmat calculation
* Since the camera and the camera shift are needed for the winmat calculation
* we do a small hack to replace it temporarily so we don't need to change the
* we do a small hack to replace it temporarily so we don't need to change the
* view3d)main_region_setup_view() code to account for that.
* view3d)main_region_setup_view() code to account for that.
*/
*/
static void view3d_stereo3d_setup(
static void view3d_stereo3d_setup(
Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *ar, const rcti *rect)
Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *ar, const rcti *rect)
{
{
bool is_left;
bool is_left;
const char *names[2] = { STEREO_LEFT_NAME, STEREO_RIGHT_NAME };
const char *names[2] = { STEREO_LEFT_NAME, STEREO_RIGHT_NAME };
const char *viewname;
const char *viewname;


/* show only left or right camera */
/* show only left or right camera */
if (v3d->stereo3d_camera != STEREO_3D_ID)
if (v3d->stereo3d_camera != STEREO_3D_ID)
v3d->multiview_eye = v3d->stereo3d_camera;
v3d->multiview_eye = v3d->stereo3d_camera;


is_left = v3d->multiview_eye == STEREO_LEFT_ID;
is_left = v3d->multiview_eye == STEREO_LEFT_ID;
viewname = names[is_left ? STEREO_LEFT_ID : STEREO_RIGHT_ID];
viewname = names[is_left ? STEREO_LEFT_ID : STEREO_RIGHT_ID];


/* update the viewport matrices with the new camera */
/* update the viewport matrices with the new camera */
if (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D) {
if (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D) {
Camera *data;
Camera *data;
float viewmat[4][4];
float viewmat[4][4];
float shiftx;
float shiftx;


data = (Camera *)v3d->camera->data;
data = (Camera *)v3d->camera->data;
shiftx = data->shiftx;
shiftx = data->shiftx;


BLI_thread_lock(LOCK_VIEW3D);
BLI_thread_lock(LOCK_VIEW3D);
data->shiftx = BKE_camera_multiview_shift_x(&scene->r, v3d->camera, viewname);
data->shiftx = BKE_camera_multiview_shift_x(&scene->r, v3d->camera, viewname);


BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat);
BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat);
view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, NULL, rect);
view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, NULL, rect);


data->shiftx = shiftx;
data->shiftx = shiftx;
BLI_thread_unlock(LOCK_VIEW3D);
BLI_thread_unlock(LOCK_VIEW3D);
}
}
else { /* SCE_VIEWS_FORMAT_MULTIVIEW */
else { /* SCE_VIEWS_FORMAT_MULTIVIEW */
float viewmat[4][4];
float viewmat[4][4];
Object *view_ob = v3d->camera;
Object *view_ob = v3d->camera;
Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);


BLI_thread_lock(LOCK_VIEW3D);
BLI_thread_lock(LOCK_VIEW3D);
v3d->camera = camera;
v3d->camera = camera;


BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat);
BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat);
view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, NULL, rect);
view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, NULL, rect);


v3d->camera = view_ob;
v3d->camera = view_ob;
BLI_thread_unlock(LOCK_VIEW3D);
BLI_thread_unlock(LOCK_VIEW3D);
}
}
}
}


/**
/**
* Set the correct matrices
* Set the correct matrices
*/
*/
void ED_view3d_draw_setup_view(
void ED_view3d_draw_setup_view(
wmWindow *win, Depsgraph *depsgraph, Scene *scene, ARegion *ar, View3D *v3d,
wmWindow *win, Depsgraph *depsgraph, Scene *scene, ARegion *ar, View3D *v3d,
float viewmat[4][4], float winmat[4][4], const rcti *rect)
float viewmat[4][4], float winmat[4][4], const rcti *rect)
{
{
RegionView3D *rv3d = ar->regiondata;
RegionView3D *rv3d = ar->regiondata;


/* Setup the view matrix. */
/* Setup the view matrix. */
if (view3d_stereo3d_active(win, scene, v3d, rv3d)) {
if (view3d_stereo3d_active(win, scene, v3d, rv3d)) {
view3d_stereo3d_setup(depsgraph, scene, v3d, ar, rect);
view3d_stereo3d_setup(depsgraph, scene, v3d, ar, rect);
}
}
else {
else {
view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, rect);
view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, rect);
}
}
}
}


/* ******************** view border ***************** */
/* ******************** view border ***************** */


static void view3d_camera_border(
static void view3d_camera_border(
const Scene *scene, struct Depsgraph *depsgraph,
const Scene *scene, struct Depsgraph *depsgraph,
const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
rctf *r_viewborder, const bool no_shift, const bool no_zoom)
rctf *r_viewborder, const bool no_shift, const bool no_zoom)
{
{
CameraParams params;
CameraParams params;
rctf rect_view, rect_camera;
rctf rect_view, rect_camera;
Object *camera_eval = DEG_get_evaluated_object(depsgraph, v3d->camera);
Object *camera_eval = DEG_get_evaluated_object(depsgraph, v3d->camera);


/* get viewport viewplane */
/* get viewport viewplane */
BKE_camera_params_init(&params);
BKE_camera_params_init(&params);
BKE_camera_params_from_view3d(&params, depsgraph, v3d, rv3d);
BKE_camera_params_from_view3d(&params, depsgraph, v3d, rv3d);
if (no_zoom)
if (no_zoom)
params.zoom = 1.0f;
params.zoom = 1.0f;
BKE_camera_params_compute_viewplane(&params, ar->winx, ar->winy, 1.0f, 1.0f);
BKE_camera_params_compute_viewplane(&params, ar->winx, ar->winy, 1.0f, 1.0f);
rect_view = params.viewplane;
rect_view = params.viewplane;


/* get camera viewplane */
/* get camera viewplane */
BKE_camera_params_init(&params);
BKE_camera_params_init(&params);
/* fallback for non camera objects */
/* fallback for non camera objects */
params.clipsta = v3d->near;
params.clipsta = v3d->near;
params.clipend = v3d->far;
params.clipend = v3d->far;
BKE_camera_params_from_object(&params, camera_eval);
BKE_camera_params_from_object(&params, camera_eval);
if (no_shift) {
if (no_shift) {
params.shiftx = 0.0f;
params.shiftx = 0.0f;
params.shifty = 0.0f;
params.shifty = 0.0f;
}
}
BKE_camera_params_compute_viewplane(&params, scene->r.xsch, scene->r.ysch, scene->r.xasp, scene->r.yasp);
BKE_camera_params_compute_viewplane(&params, scene->r.xsch, scene->r.ysch, scene->r.xasp, scene->r.yasp);
rect_camera = params.viewplane;
rect_camera = params.viewplane;


/* get camera border within viewport */
/* get camera border within viewport */
r_viewborder->xmin = ((rect_camera.xmin - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) * ar->winx;
r_viewborder->xmin = ((rect_camera.xmin - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) * ar->winx;
r_viewborder->xmax = ((rect_camera.xmax - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) * ar->winx;
r_viewborder->xmax = ((rect_camera.xmax - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) * ar->winx;
r_viewborder->ymin = ((rect_camera.ymin - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) * ar->winy;
r_viewborder->ymin = ((rect_camera.ymin - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) * ar->winy;
r_viewborder->ymax = ((rect_camera.ymax - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) * ar->winy;
r_viewborder->ymax = ((rect_camera.ymax - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) * ar->winy;
}
}


void ED_view3d_calc_camera_border_size(
void ED_view3d_calc_camera_border_size(
const Scene *scene, Depsgraph *depsgraph,
const Scene *scene, Depsgraph *depsgraph,
const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
float r_size[2])
float r_size[2])
{
{
rctf viewborder;
rctf viewborder;


view3d_camera_border(scene, depsgraph, ar, v3d, rv3d, &viewborder, true, true);
view3d_camera_border(scene, depsgraph, ar, v3d, rv3d, &viewborder, true, true);
r_size[0] = BLI_rctf_size_x(&viewborder);
r_size[0] = BLI_rctf_size_x(&viewborder);
r_size[1] = BLI_rctf_size_y(&viewborder);
r_size[1] = BLI_rctf_size_y(&viewborder);
}
}


void ED_view3d_calc_camera_border(
void ED_view3d_calc_camera_border(
const Scene *scene, Depsgraph *depsgraph,
const Scene *scene, Depsgraph *depsgraph,
const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
rctf *r_viewborder, const bool no_shift)
rctf *r_viewborder, const bool no_shift)
{
{
view3d_camera_border(scene, depsgraph, ar, v3d, rv3d, r_viewborder, no_shift, false);
view3d_camera_border(scene, depsgraph, ar, v3d, rv3d, r_viewborder, no_shift, false);
}
}


static void drawviewborder_grid3(uint shdr_pos, float x1, float x2, float y1, float y2, float fac)
static void drawviewborder_grid3(uint shdr_pos, float x1, float x2, float y1, float y2, float fac)
{
{
float x3, y3, x4, y4;
float x3, y3, x4, y4;


x3 = x1 + fac * (x2 - x1);
x3 = x1 + fac * (x2 - x1);
y3 = y1 + fac * (y2 - y1);
y3 = y1 + fac * (y2 - y1);
x4 = x1 + (1.0f - fac) * (x2 - x1);
x4 = x1 + (1.0f - fac) * (x2 - x1);
y4 = y1 + (1.0f - fac) * (y2 - y1);
y4 = y1 + (1.0f - fac) * (y2 - y1);


immBegin(GPU_PRIM_LINES, 8);
immBegin(GPU_PRIM_LINES, 8);


immVertex2f(shdr_pos, x1, y3);
immVertex2f(shdr_pos, x1, y3);
immVertex2f(shdr_pos, x2, y3);
immVertex2f(shdr_pos, x2, y3);


immVertex2f(shdr_pos, x1, y4);
immVertex2f(shdr_pos, x1, y4);
immVertex2f(shdr_pos, x2, y4);
immVertex2f(shdr_pos, x2, y4);


immVertex2f(shdr_pos, x3, y1);
immVertex2f(shdr_pos, x3, y1);
immVertex2f(shdr_pos, x3, y2);
immVertex2f(shdr_pos, x3, y2);


immVertex2f(shdr_pos, x4, y1);
immVertex2f(shdr_pos, x4, y1);
immVertex2f(shdr_pos, x4, y2);
immVertex2f(shdr_pos, x4, y2);


immEnd();
immEnd();
}
}


/* harmonious triangle */
/* harmonious triangle */
static void drawviewborder_triangle(
static void drawviewborder_triangle(
uint shdr_pos, float x1, float x2, float y1, float y2, const char golden, const char dir)
uint shdr_pos, float x1, float x2, float y1, float y2, const char golden, const char dir)
{
{
float ofs;
float ofs;
float w = x2 - x1;
float w = x2 - x1;
float h = y2 - y1;
float h = y2 - y1;


immBegin(GPU_PRIM_LINES, 6);
immBegin(GPU_PRIM_LINES, 6);


if (w > h) {
if (w > h) {
if (golden) {
if (golden) {
ofs = w * (1.0f - (1.0f / 1.61803399f));
ofs = w * (1.0f - (1.0f / 1.61803399f));
}
}
else {
else {
ofs = h * (h / w);
ofs = h * (h / w);
}
}
if (dir == 'B') SWAP(float, y1, y2);
if (dir == 'B') SWAP(float, y1, y2);


immVertex2f(shdr_pos, x1, y1);
immVertex2f(shdr_pos, x1, y1);
immVertex2f(shdr_pos, x2, y2);
immVertex2f(shdr_pos, x2, y2);


immVertex2f(shdr_pos, x2, y1);
immVertex2f(shdr_pos, x2, y1);
immVertex2f(shdr_pos, x1 + (w - ofs), y2);
immVertex2f(shdr_pos, x1 + (w - ofs), y2);


immVertex2f(shdr_pos, x1, y2);
immVertex2f(shdr_pos, x1, y2);
immVertex2f(shdr_pos, x1 + ofs, y1);
immVertex2f(shdr_pos, x1 + ofs, y1);
}
}
else {
else {
if (golden) {
if (golden) {
ofs = h * (1.0f - (1.0f / 1.61803399f));
ofs = h * (1.0f - (1.0f / 1.61803399f));
}
}
else {
else {
ofs = w * (w / h);
ofs = w * (w / h);
}
}
if (dir == 'B') SWAP(float, x1, x2);
if (dir == 'B') SWAP(float, x1, x2);


immVertex2f(shdr_pos, x1, y1);
immVertex2f(shdr_pos, x1, y1);
immVertex2f(shdr_pos, x2, y2);
immVertex2f(shdr_pos, x2, y2);


immVertex2f(shdr_pos, x2, y1);
immVertex2f(shdr_pos, x2, y1);
immVertex2f(shdr_pos, x1, y1 + ofs);
immVertex2f(shdr_pos, x1, y1 + ofs);


immVertex2f(shdr_pos, x1, y2);
immVertex2f(shdr_pos, x1, y2);
immVertex2f(shdr_pos, x2, y1 + (h - ofs));
immVertex2f(shdr_pos, x2, y1 + (h - ofs));
}
}


immEnd();
immEnd();
}
}


static void drawviewborder(Scene *scene, Depsgraph *depsgraph, ARegion *ar, View3D *v3d)
static void drawviewborder(Scene *scene, Depsgraph *depsgraph, ARegion *ar, View3D *v3d)
{
{
float x1, x2, y1, y2;
float x1, x2, y1, y2;
float x1i, x2i, y1i, y2i;
float x1i, x2i, y1i, y2i;


rctf viewborder;
rctf viewborder;
Camera *ca = NULL;
Camera *ca = NULL;
RegionView3D *rv3d = ar->regiondata;
RegionView3D *rv3d = ar->regiondata;


if (v3d->camera == NULL)
if (v3d->camera == NULL)
return;
return;
if (v3d->camera->type == OB_CAMERA)
if (v3d->camera->type == OB_CAMERA)
ca = v3d->camera->data;
ca = v3d->camera->data;


ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &viewborder, false);
ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &viewborder, false);
/* the offsets */
/* the offsets */
x1 = viewborder.xmin;
x1 = viewborder.xmin;
y1 = viewborder.ymin;
y1 = viewborder.ymin;
x2 = viewborder.xmax;
x2 = viewborder.xmax;
y2 = viewborder.ymax;
y2 = viewborder.ymax;


GPU_line_width(1.0f);
GPU_line_width(1.0f);


/* apply offsets so the real 3D camera shows through */
/* apply offsets so the real 3D camera shows through */


/* note: quite un-scientific but without this bit extra
/* note: quite un-scientific but without this bit extra
* 0.0001 on the lower left the 2D border sometimes
* 0.0001 on the lower left the 2D border sometimes
* obscures the 3D camera border */
* obscures the 3D camera border */
/* note: with VIEW3D_CAMERA_BORDER_HACK defined this error isn't noticeable
/* note: with VIEW3D_CAMERA_BORDER_HACK defined this error isn't noticeable
* but keep it here in case we need to remove the workaround */
* but keep it here in case we need to remove the workaround */
x1i = (int)(x1 - 1.0001f);
x1i = (int)(x1 - 1.0001f);
y1i = (int)(y1 - 1.0001f);
y1i = (int)(y1 - 1.0001f);
x2i = (int)(x2 + (1.0f - 0.0001f));
x2i = (int)(x2 + (1.0f - 0.0001f));
y2i = (int)(y2 + (1.0f - 0.0001f));
y2i = (int)(y2 + (1.0f - 0.0001f));


uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);


/* First, solid lines. */
/* First, solid lines. */
{
{
immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);


/* passepartout, specified in camera edit buttons */
/* passepartout, specified in camera edit buttons */
if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001f) {
if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001f) {
const float winx = (ar->winx + 1);
const float winx = (ar->winx + 1);
const float winy = (ar->winy + 1);
const float winy = (ar->winy + 1);


float alpha = 1.0f;
float alpha = 1.0f;


if (ca->passepartalpha != 1.0f) {
if (ca->passepartalpha != 1.0f) {
GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
GPU_blend(true);
GPU_blend(true);
alpha = ca->passepartalpha;
alpha = ca->passepartalpha;
}
}


immUniformColor4f(0.0f, 0.0f, 0.0f, alpha);
immUniformColor4f(0.0f, 0.0f, 0.0f, alpha);


if (x1i > 0.0f)
if (x1i > 0.0f)
immRectf(shdr_pos, 0.0f, winy, x1i, 0.0f);
immRectf(shdr_pos, 0.0f, winy, x1i, 0.0f);
if (x2i < winx)
if (x2i < winx)
immRectf(shdr_pos, x2i, winy, winx, 0.0f);
immRectf(shdr_pos, x2i, winy, winx, 0.0f);
if (y2i < winy)
if (y2i < winy)
immRectf(shdr_pos, x1i, winy, x2i, y2i);
immRectf(shdr_pos, x1i, winy, x2i, y2i);
if (y2i > 0.0f)
if (y2i > 0.0f)
immRectf(shdr_pos, x1i, y1i, x2i, 0.0f);
immRectf(shdr_pos, x1i, y1i, x2i, 0.0f);


GPU_blend(false);
GPU_blend(false);
}
}


immUniformThemeColor(TH_BACK);
immUniformThemeColor(TH_BACK);
imm_draw_box_wire_2d(shdr_pos, x1i, y1i, x2i, y2i);
imm_draw_box_wire_2d(shdr_pos, x1i, y1i, x2i, y2i);


#ifdef VIEW3D_CAMERA_BORDER_HACK
#ifdef VIEW3D_CAMERA_BORDER_HACK
if (view3d_camera_border_hack_test == true) {
if (view3d_camera_border_hack_test == true) {
immUniformColor3ubv(view3d_camera_border_hack_col);
immUniformColor3ubv(view3d_camera_border_hack_col);
imm_draw_box_wire_2d(shdr_pos, x1i + 1, y1i + 1, x2i - 1, y2i - 1);
imm_draw_box_wire_2d(shdr_pos, x1i + 1, y1i + 1, x2i - 1, y2i - 1);
view3d_camera_border_hack_test = false;
view3d_camera_border_hack_test = false;
}
}
#endif
#endif


immUnbindProgram();
immUnbindProgram();
}
}


/* And now, the dashed lines! */
/* And now, the dashed lines! */
immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);


{
{
float viewport_size[4];
float viewport_size[4];
GPU_viewport_size_get_f(viewport_size);
GPU_viewport_size_get_f(viewport_size);
immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);


immUniform1i("colors_len", 0); /* "simple" mode */
immUniform1i("colors_len", 0); /* "simple" mode */
immUniform1f("dash_width", 6.0f);
immUniform1f("dash_width", 6.0f);
immUniform1f("dash_factor", 0.5f);
immUniform1f("dash_factor", 0.5f);


/* outer line not to confuse with object selection */
/* outer line not to confuse with object selection */
if (v3d->flag2 & V3D_LOCK_CAMERA) {
if (v3d->flag2 & V3D_LOCK_CAMERA) {
immUniformThemeColor(TH_REDALERT);
immUniformThemeColor(TH_REDALERT);
imm_draw_box_wire_2d(shdr_pos, x1i - 1, y1i - 1, x2i + 1, y2i + 1);
imm_draw_box_wire_2d(shdr_pos, x1i - 1, y1i - 1, x2i + 1, y2i + 1);
}
}


immUniformThemeColor(TH_VIEW_OVERLAY);
immUniformThemeColor(TH_VIEW_OVERLAY);
imm_draw_box_wire_2d(shdr_pos, x1i, y1i, x2i, y2i);
imm_draw_box_wire_2d(shdr_pos, x1i, y1i, x2i, y2i);
}
}


/* border */
/* border */
if (scene->r.mode & R_BORDER) {
if (scene->r.mode & R_BORDER) {
float x3, y3, x4, y4;
float x3, y3, x4, y4;


x3 = floorf(x1 + (scene->r.border.xmin * (x2 - x1))) - 1;
x3 = floorf(x1 + (scene->r.border.xmin * (x2 - x1))) - 1;
y3 = floorf(y1 + (scene->r.border.ymin * (y2 - y1))) - 1;
y3 = floorf(y1 + (scene->r.border.ymin * (y2 - y1))) - 1;
x4 = floorf(x1 + (scene->r.border.xmax * (x2 - x1))) + (U.pixelsize - 1);
x4 = floorf(x1 + (scene->r.border.xmax * (x2 - x1))) + (U.pixelsize - 1);
y4 = floorf(y1 + (scene->r.border.ymax * (y2 - y1))) + (U.pixelsize - 1);
y4 = floorf(y1 + (scene->r.border.ymax * (y2 - y1))) + (U.pixelsize - 1);


immUniformColor3f(1.0f, 0.25f, 0.25f);
immUniformColor3f(1.0f, 0.25f, 0.25f);
imm_draw_box_wire_2d(shdr_pos, x3, y3, x4, y4);
imm_draw_box_wire_2d(shdr_pos, x3, y3, x4, y4);
}
}


/* safety border */
/* safety border */
if (ca) {
if (ca) {
immUniformThemeColorBlend(TH_VIEW_OVERLAY, TH_BACK, 0.25f);
immUniformThemeColorBlend(TH_VIEW_OVERLAY, TH_BACK, 0.25f);


if (ca->dtx & CAM_DTX_CENTER) {
if (ca->dtx & CAM_DTX_CENTER) {
float x3, y3;
float x3, y3;


x3 = x1 + 0.5f * (x2 - x1);
x3 = x1 + 0.5f * (x2 - x1);
y3 = y1 + 0.5f * (y2 - y1);
y3 = y1 + 0.5f * (y2 - y1);


immBegin(GPU_PRIM_LINES, 4);
immBegin(GPU_PRIM_LINES, 4);


immVertex2f(shdr_pos, x1, y3);
immVertex2f(shdr_pos, x1, y3);
immVertex2f(shdr_pos, x2, y3);
immVertex2f(shdr_pos, x2, y3);


immVertex2f(shdr_pos, x3, y1);
immVertex2f(shdr_pos, x3, y1);
immVertex2f(shdr_pos, x3, y2);
immVertex2f(shdr_pos, x3, y2);


immEnd();
immEnd();
}
}


if (ca->dtx & CAM_DTX_CENTER_DIAG) {
if (ca->dtx & CAM_DTX_CENTER_DIAG) {
immBegin(GPU_PRIM_LINES, 4);
immBegin(GPU_PRIM_LINES, 4);


immVertex2f(shdr_pos, x1, y1);
immVertex2f(shdr_pos, x1, y1);
immVertex2f(shdr_pos, x2, y2);
immVertex2f(shdr_pos, x2, y2);


immVertex2f(shdr_pos, x1, y2);
immVertex2f(shdr_pos, x1, y2);
immVertex2f(shdr_pos, x2, y1);
immVertex2f(shdr_pos, x2, y1);


immEnd();
immEnd();
}
}


if (ca->dtx & CAM_DTX_THIRDS) {
if (ca->dtx & CAM_DTX_THIRDS) {
drawviewborder_grid3(shdr_pos, x1, x2, y1, y2, 1.0f / 3.0f);
drawviewborder_grid3(shdr_pos, x1, x2, y1, y2, 1.0f / 3.0f);
}
}


if (ca->dtx & CAM_DTX_GOLDEN) {
if (ca->dtx & CAM_DTX_GOLDEN) {
drawviewborder_grid3(shdr_pos, x1, x2, y1, y2, 1.0f - (1.0f / 1.61803399f));
drawviewborder_grid3(shdr_pos, x1, x2, y1, y2, 1.0f - (1.0f / 1.61803399f));
}
}


if (ca->dtx & CAM_DTX_GOLDEN_TRI_A) {
if (ca->dtx & CAM_DTX_GOLDEN_TRI_A) {
drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 0, 'A');
drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 0, 'A');
}
}


if (ca->dtx & CAM_DTX_GOLDEN_TRI_B) {
if (ca->dtx & CAM_DTX_GOLDEN_TRI_B) {
drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 0, 'B');
drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 0, 'B');
}
}


if (ca->dtx & CAM_DTX_HARMONY_TRI_A) {
if (ca->dtx & CAM_DTX_HARMONY_TRI_A) {
drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 1, 'A');
drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 1, 'A');
}
}


if (ca->dtx & CAM_DTX_HARMONY_TRI_B) {
if (ca->dtx & CAM_DTX_HARMONY_TRI_B) {
drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 1, 'B');
drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 1, 'B');
}
}


if (ca->flag & CAM_SHOW_SAFE_MARGINS) {
if (ca->flag & CAM_SHOW_SAFE_MARGINS) {
UI_draw_safe_areas(
UI_draw_safe_areas(
shdr_pos, x1, x2, y1, y2,
shdr_pos, x1, x2, y1, y2,
scene->safe_areas.title, scene->safe_areas.action);
scene->safe_areas.title, scene->safe_areas.action);


if (ca->flag & CAM_SHOW_SAFE_CENTER) {
if (ca->flag & CAM_SHOW_SAFE_CENTER) {
UI_draw_safe_areas(
UI_draw_safe_areas(
shdr_pos, x1, x2, y1, y2,
shdr_pos, x1, x2, y1, y2,
scene->safe_areas.title_center, scene->safe_areas.action_center);
scene->safe_areas.title_center, scene->safe_areas.action_center);
}
}
}
}


if (ca->flag & CAM_SHOWSENSOR) {
if (ca->flag & CAM_SHOWSENSOR) {
/* determine sensor fit, and get sensor x/y, for auto fit we
/* determine sensor fit, and get sensor x/y, for auto fit we
* assume and square sensor and only use sensor_x */
* assume and square sensor and only use sensor_x */
float sizex = scene->r.xsch * scene->r.xasp;
float sizex = scene->r.xsch * scene->r.xasp;
float sizey = scene->r.ysch * scene->r.yasp;
float sizey = scene->r.ysch * scene->r.yasp;
int sensor_fit = BKE_camera_sensor_fit(ca->sensor_fit, sizex, sizey);
int sensor_fit = BKE_camera_sensor_fit(ca->sensor_fit, sizex, sizey);
float sensor_x = ca->sensor_x;
float sensor_x = ca->sensor_x;
float sensor_y = (ca->sensor_fit == CAMERA_SENSOR_FIT_AUTO) ? ca->sensor_x : ca->sensor_y;
float sensor_y = (ca->sensor_fit == CAMERA_SENSOR_FIT_AUTO) ? ca->sensor_x : ca->sensor_y;


/* determine sensor plane */
/* determine sensor plane */
rctf rect;
rctf rect;


if (sensor_fit == CAMERA_SENSOR_FIT_HOR) {
if (sensor_fit == CAMERA_SENSOR_FIT_HOR) {
float sensor_scale = (x2i - x1i) / sensor_x;
float sensor_scale = (x2i - x1i) / sensor_x;
float sensor_height = sensor_scale * sensor_y;
float sensor_height = sensor_scale * sensor_y;


rect.xmin = x1i;
rect.xmin = x1i;
rect.xmax = x2i;
rect.xmax = x2i;
rect.ymin = (y1i + y2i) * 0.5f - sensor_height * 0.5f;
rect.ymin = (y1i + y2i) * 0.5f - sensor_height * 0.5f;
rect.ymax = rect.ymin + sensor_height;
rect.ymax = rect.ymin + sensor_height;
}
}
else {
else {
float sensor_scale = (y2i - y1i) / sensor_y;
float sensor_scale = (y2i - y1i) / sensor_y;
float sensor_width = sensor_scale * sensor_x;
float sensor_width = sensor_scale * sensor_x;


rect.xmin = (x1i + x2i) * 0.5f - sensor_width * 0.5f;
rect.xmin = (x1i + x2i) * 0.5f - sensor_width * 0.5f;
rect.xmax = rect.xmin + sensor_width;
rect.xmax = rect.xmin + sensor_width;
rect.ymin = y1i;
rect.ymin = y1i;
rect.ymax = y2i;
rect.ymax = y2i;
}
}


/* draw */
/* draw */
immUniformThemeColorShade(TH_VIEW_OVERLAY, 100);
immUniformThemeColorShade(TH_VIEW_OVERLAY, 100);


/* TODO Was using UI_draw_roundbox_4fv(false, rect.xmin, rect.ymin, rect.xmax, rect.ymax, 2.0f, color).
/* TODO Was using UI_draw_roundbox_4fv(false, rect.xmin, rect.ymin, rect.xmax, rect.ymax, 2.0f, color).
* We'll probably need a new imm_draw_line_roundbox_dashed dor that - though in practice the
* We'll probably need a new imm_draw_line_roundbox_dashed dor that - though in practice the
* 2.0f round corner effect was nearly not visible anyway... */
* 2.0f round corner effect was nearly not visible anyway... */
imm_draw_box_wire_2d(shdr_pos, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
imm_draw_box_wire_2d(shdr_pos, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
}
}
}
}


immUnbindProgram();
immUnbindProgram();
/* end dashed lines */
/* end dashed lines */


/* camera name - draw in highlighted text color */
/* camera name - draw in highlighted text color */
if (ca && (ca->flag & CAM_SHOWNAME)) {
if (ca && (ca->flag & CAM_SHOWNAME)) {
UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
BLF_draw_default(
BLF_draw_default(
x1i, y1i - (0.7f * U.widget_unit), 0.0f,
x1i, y1i - (0.7f * U.widget_unit), 0.0f,
v3d->camera->id.name + 2, sizeof(v3d->camera->id.name) - 2);
v3d->camera->id.name + 2, sizeof(v3d->camera->id.name) - 2);
}
}
}
}


static void drawrenderborder(ARegion *ar, View3D *v3d)
static void drawrenderborder(ARegion *ar, View3D *v3d)
{
{
/* use the same program for everything */
/* use the same program for everything */
uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);


GPU_line_width(1.0f);
GPU_line_width(1.0f);


immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);


float viewport_size[4];
float viewport_size[4];
GPU_viewport_size_get_f(viewport_size);
GPU_viewport_size_get_f(viewport_size);
immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);


immUniform1i("colors_len", 0); /* "simple" mode */
immUniform1i("colors_len", 0); /* "simple" mode */
immUniform4f("color", 1.0f, 0.25f, 0.25f, 1.0f);
immUniform4f("color", 1.0f, 0.25f, 0.25f, 1.0f);
immUniform1f("dash_width", 6.0f);
immUniform1f("dash_width", 6.0f);
immUniform1f("dash_factor", 0.5f);
immUniform1f("dash_factor", 0.5f);


imm_draw_box_wire_2d(shdr_pos,
imm_draw_box_wire_2d(shdr_pos,
v3d->render_border.xmin * ar->winx, v3d->render_border.ymin * ar->winy,
v3d->render_border.xmin * ar->winx, v3d->render_border.ymin * ar->winy,
v3d->render_border.xmax * ar->winx, v3d->render_border.ymax * ar->winy);
v3d->render_border.xmax * ar->winx, v3d->render_border.ymax * ar->winy);


immUnbindProgram();
immUnbindProgram();
}
}


void ED_view3d_draw_depth(
void ED_view3d_draw_depth(
Depsgraph *depsgraph,
Depsgraph *depsgraph,
ARegion *ar, View3D *v3d, bool alphaoverride)
ARegion *ar, View3D *v3d, bool alphaoverride)
{
{
struct bThemeState theme_state;
struct bThemeState theme_state;
Scene *scene = DEG_get_evaluated_scene(depsgraph);
Scene *scene = DEG_get_evaluated_scene(depsgraph);
RegionView3D *rv3d = ar->regiondata;
RegionView3D *rv3d = ar->regiondata;


short flag = v3d->flag;
short flag = v3d->flag;
float glalphaclip = U.glalphaclip;
float glalphaclip = U.glalphaclip;
int obcenter_dia = U.obcenter_dia;
int obcenter_dia = U.obcenter_dia;
/* temp set drawtype to solid */
/* temp set drawtype to solid */
/* Setting these temporarily is not nice */
/* Setting these temporarily is not nice */
v3d->flag &= ~V3D_SELECT_OUTLINE;
v3d->flag &= ~V3D_SELECT_OUTLINE;
U.glalphaclip = alphaoverride ? 0.5f : glalphaclip; /* not that nice but means we wont zoom into billboards */
U.glalphaclip = alphaoverride ? 0.5f : glalphaclip; /* not that nice but means we wont zoom into billboards */
U.obcenter_dia = 0;
U.obcenter_dia = 0;


/* Tools may request depth outside of regular drawing code. */
/* Tools may request depth outside of regular drawing code. */
UI_Theme_Store(&theme_state);
UI_Theme_Store(&theme_state);
UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);


ED_view3d_draw_setup_view(NULL, depsgraph, scene, ar, v3d, NULL, NULL, NULL);
ED_view3d_draw_setup_view(NULL, depsgraph, scene, ar, v3d, NULL, NULL, NULL);


GPU_clear(GPU_DEPTH_BIT);
GPU_clear(GPU_DEPTH_BIT);


if (rv3d->rflag & RV3D_CLIPPING) {
if (rv3d->rflag & RV3D_CLIPPING) {
ED_view3d_clipping_set(rv3d);
ED_view3d_clipping_set(rv3d);
}
}
/* get surface depth without bias */
/* get surface depth without bias */
rv3d->rflag |= RV3D_ZOFFSET_DISABLED;
rv3d->rflag |= RV3D_ZOFFSET_DISABLED;


GPU_depth_test(true);
GPU_depth_test(true);


DRW_draw_depth_loop(depsgraph, ar, v3d);
DRW_draw_depth_loop(depsgraph, ar, v3d);


if (rv3d->rflag & RV3D_CLIPPING) {
if (rv3d->rflag & RV3D_CLIPPING) {
ED_view3d_clipping_disable();
ED_view3d_clipping_disable();
}
}
rv3d->rflag &= ~RV3D_ZOFFSET_DISABLED;
rv3d->rflag &= ~RV3D_ZOFFSET_DISABLED;


/* Reset default for UI */
/* Reset default for UI */
GPU_depth_test(false);
GPU_depth_test(false);


U.glalphaclip = glalphaclip;
U.glalphaclip = glalphaclip;
v3d->flag = flag;
v3d->flag = flag;
U.obcenter_dia = obcenter_dia;
U.obcenter_dia = obcenter_dia;


UI_Theme_Restore(&theme_state);
UI_Theme_Restore(&theme_state);
}
}


/* ******************** other elements ***************** */
/* ******************** other elements ***************** */


/** could move this elsewhere, but tied into #ED_view3d_grid_scale */
/** could move this elsewhere, but tied into #ED_view3d_grid_scale */
float ED_scene_grid_scale(Scene *scene, const char **grid_unit)
float ED_scene_grid_scale(Scene *scene, const char **grid_unit)
{
{
/* apply units */
/* apply units */
if (scene->unit.system) {
if (scene->unit.system) {
const void *usys;
const void *usys;
int len;
int len;


bUnit_GetSystem(scene->unit.system, B_UNIT_LENGTH, &usys, &len);
bUnit_GetSystem(scene->unit.system, B_UNIT_LENGTH, &usys, &len);


if (usys) {
if (usys) {
int i = bUnit_GetBaseUnit(usys);
int i = bUnit_GetBaseUnit(usys);
if (grid_unit)
if (grid_unit)
*grid_unit = bUnit_GetNameDisplay(usys, i);
*grid_unit = bUnit_GetNameDisplay(usys, i);
return (float)bUnit_GetScaler(usys, i) / scene->unit.scale_length;
return (float)bUnit_GetScaler(usys, i) / scene->unit.scale_length;
}
}
}
}


return 1.0f;
return 1.0f;
}
}


float ED_view3d_grid_scale(Scene *scene, View3D *v3d, const char **grid_unit)
float ED_view3d_grid_scale(Scene *scene, View3D *v3d, const char **grid_unit)
{
{
return v3d->grid * ED_scene_grid_scale(scene, grid_unit);
return v3d->grid * ED_scene_grid_scale(scene, grid_unit);
}
}


/* Simulates the grid scale that is visualized by the shaders drawing functions.
/* Simulates the grid scale that is visualized by the shaders drawing functions.
* The actual code is seen in `object_grid_frag.glsl` when you get the `grid_res` value.
* The actual code is seen in `object_grid_frag.glsl` when you get the `grid_res` value.
* Currently the simulation is done only when RV3D_VIEW_IS_AXIS. */
* Currently the simulation is done only when RV3D_VIEW_IS_AXIS. */
float ED_view3d_grid_view_scale(
float ED_view3d_grid_view_scale(
Scene *scene, View3D *v3d, RegionView3D *rv3d, const char **grid_unit)
Scene *scene, View3D *v3d, RegionView3D *rv3d, const char **grid_unit)
{
{
float grid_scale = ED_view3d_grid_scale(scene, v3d, grid_unit);
float grid_scale = ED_view3d_grid_scale(scene, v3d, grid_unit);
if (!rv3d->is_persp && RV3D_VIEW_IS_AXIS(rv3d->view)) {
if (!rv3d->is_persp && RV3D_VIEW_IS_AXIS(rv3d->view)) {
/* Decrease the distance between grid snap points depending on zoom. */
/* Decrease the distance between grid snap points depending on zoom. */
float grid_subdiv = v3d->gridsubdiv;
float grid_subdiv = v3d->gridsubdiv;
if (grid_subdiv > 1) {
if (grid_subdiv > 1) {
float grid_distance = rv3d->dist;
float grid_distance = rv3d->dist;
float lvl = (logf(grid_distance / grid_scale) / logf(grid_subdiv));
float lvl = (logf(grid_distance / grid_scale) / logf(grid_subdiv));
if (lvl < 0.0f) {
if (lvl < 0.0f) {
/* Negative values need an offset for correct casting.
/* Negative values need an offset for correct casting.
* By convention, the minimum lvl is limited to -2 (see `objec_mode.c`) */
* By convention, the minimum lvl is limited to -2 (see `objec_mode.c`) */
if (lvl > -2.0f) {
if (lvl > -2.0f) {
lvl -= 1.0f;
lvl -= 1.0f;
}
}
else {
else {
lvl = -2.0f;
lvl = -2.0f;
}
}
}
}
grid_scale *= pow(grid_subdiv, (int)lvl - 1);
grid_scale *= pow(grid_subdiv, (int)lvl - 1);
}
}
}
}


return v3d->grid * grid_scale;
return v3d->grid * grid_scale;
}
}


static void draw_view_axis(RegionView3D *rv3d, const rcti *rect)
static void draw_view_axis(RegionView3D *rv3d, const rcti *rect)
{
{
const float k = U.rvisize * U.pixelsize; /* axis size */
const float k = U.rvisize * U.pixelsize; /* axis size */
const int bright = - 20 * (10 - U.rvibright); /* axis alpha offset (rvibright has range 0-10) */
const int bright = - 20 * (10 - U.rvibright); /* axis alpha offset (rvibright has range 0-10) */


/* Axis center in screen coordinates.
/* Axis center in screen coordinates.
*
*
* - Unit size offset so small text doesn't draw outside the screen
* - Unit size offset so small text doesn't draw outside the screen
* - Extra X offset because of the panel expander.
* - Extra X offset because of the panel expander.
*/
*/
const float startx = rect->xmax - (k + UI_UNIT_X * 1.5);
const float startx = rect->xmax - (k + UI_UNIT_X * 1.5);
const float starty = rect->ymax - (k + UI_UNIT_Y);
const float starty = rect->ymax - (k + UI_UNIT_Y);


float axis_pos[3][2];
float axis_pos[3][2];
unsigned char axis_col[3][4];
unsigned char axis_col[3][4];


int axis_order[3] = {0, 1, 2};
int axis_order[3] = {0, 1, 2};
axis_sort_v3(rv3d->viewinv[2], axis_order);
axis_sort_v3(rv3d->viewinv[2], axis_order);


for (int axis_i = 0; axis_i < 3; axis_i++) {
for (int axis_i = 0; axis_i < 3; axis_i++) {
int i = axis_order[axis_i];
int i = axis_order[axis_i];


/* get position of each axis tip on screen */
/* get position of each axis tip on screen */
float vec[3] = { 0.0f };
float vec[3] = { 0.0f };
vec[i] = 1.0f;
vec[i] = 1.0f;
mul_qt_v3(rv3d->viewquat, vec);
mul_qt_v3(rv3d->viewquat, vec);
axis_pos[i][0] = startx + vec[0] * k;
axis_pos[i][0] = startx + vec[0] * k;
axis_pos[i][1] = starty + vec[1] * k;
axis_pos[i][1] = starty + vec[1] * k;


/* get color of each axis */
/* get color of each axis */
UI_GetThemeColorShade3ubv(TH_AXIS_X + i, bright, axis_col[i]); /* rgb */
UI_GetThemeColorShade3ubv(TH_AXIS_X + i, bright, axis_col[i]); /* rgb */
axis_col[i][3] = 255 * hypotf(vec[0], vec[1]); /* alpha */
axis_col[i][3] = 255 * hypotf(vec[0], vec[1]); /* alpha */
}
}


/* draw axis lines */
/* draw axis lines */
GPU_line_width(2.0f);
GPU_line_width(2.0f);
GPU_line_smooth(true);
GPU_line_smooth(true);
GPU_blend(true);
GPU_blend(true);
GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);


GPUVertFormat *format = immVertexFormat();
GPUVertFormat *format = immVertexFormat();
uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
uint col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
uint col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);


immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
immBegin(GPU_PRIM_LINES, 6);
immBegin(GPU_PRIM_LINES, 6);


for (int axis_i = 0; axis_i < 3; axis_i++) {
for (int axis_i = 0; axis_i < 3; axis_i++) {
int i = axis_order[axis_i];
int i = axis_order[axis_i];


immAttr4ubv(col, axis_col[i]
immAttr4ubv(col, axis_col[i]