Fix T48034: Camera image offsets were scaled by image aspect

Offset now matches Blender 2.7x.
This commit is contained in:
Campbell Barton 2019-11-02 07:47:27 +11:00
parent 0dd9e55d21
commit 4fec2b0660
Notes: blender-bot 2023-02-14 08:01:52 +01:00
Referenced by issue #48034, Background image offset Y depends on image aspect ratio
1 changed files with 35 additions and 10 deletions

View File

@ -1235,22 +1235,42 @@ static void DRW_shgroup_camera_background_images(OBJECT_Shaders *sh_data,
uv2img_space[1][1] = image_height;
const float fit_scale = image_aspect / camera_aspect;
img2cam_space[0][0] = 1.0 / image_width;
img2cam_space[1][1] = 1.0 / fit_scale / image_height;
if (camera_aspect < image_aspect) {
img2cam_space[0][0] = 1.0 / (1.0 / fit_scale) / image_width;
img2cam_space[1][1] = 1.0 / image_height;
}
else {
img2cam_space[0][0] = 1.0 / image_width;
img2cam_space[1][1] = 1.0 / fit_scale / image_height;
}
/* Update scaling based on image and camera framing */
float scale_x = bgpic->scale;
float scale_y = bgpic->scale;
float scale_x_offset = image_width;
float scale_y_offset = image_height;
if (image_aspect > 1.0f) {
scale_x_offset /= image_aspect;
if (camera_aspect > 1.0f) {
scale_x_offset *= min_ff(image_aspect, camera_aspect);
scale_y_offset *= min_ff(image_aspect, camera_aspect);
}
}
else {
scale_y_offset *= image_aspect;
if (camera_aspect < 1.0f) {
scale_x_offset /= max_ff(image_aspect, camera_aspect);
scale_y_offset /= max_ff(image_aspect, camera_aspect);
}
}
if (bgpic->flag & CAM_BGIMG_FLAG_CAMERA_ASPECT) {
if (bgpic->flag & CAM_BGIMG_FLAG_CAMERA_CROP) {
if (image_aspect > camera_aspect) {
scale_x *= fit_scale;
scale_y *= fit_scale;
}
/* pass */
}
else {
if (image_aspect > camera_aspect) {
if (camera_aspect < image_aspect) {
scale_x /= fit_scale;
scale_y /= fit_scale;
}
@ -1262,7 +1282,12 @@ static void DRW_shgroup_camera_background_images(OBJECT_Shaders *sh_data,
}
else {
/* Stretch image to camera aspect */
scale_y /= 1.0 / fit_scale;
if (camera_aspect < image_aspect) {
scale_x /= fit_scale;
}
else {
scale_y *= fit_scale;
}
}
// scale image to match the desired aspect ratio
@ -1270,8 +1295,8 @@ static void DRW_shgroup_camera_background_images(OBJECT_Shaders *sh_data,
scale_m4[1][1] = scale_y;
/* Translate */
translate_m4[3][0] = image_width * bgpic->offset[0] * 2.0f;
translate_m4[3][1] = image_height * bgpic->offset[1] * 2.0f;
translate_m4[3][0] = bgpic->offset[0] * 2.0f * scale_x_offset;
translate_m4[3][1] = bgpic->offset[1] * 2.0f * scale_y_offset;
mul_m4_series(bg_data->transform_mat,
win_m4_translate,