[Bf-blender-cvs] [66356dae943] master: Fix T67638: Stretched Camera Background Images

Jeroen Bakker noreply at git.blender.org
Thu Aug 8 07:55:43 CEST 2019


Commit: 66356dae943d50215be7699d842989b5e7fe0398
Author: Jeroen Bakker
Date:   Wed Aug 7 15:43:44 2019 +0200
Branches: master
https://developer.blender.org/rB66356dae943d50215be7699d842989b5e7fe0398

Fix T67638: Stretched Camera Background Images

The matrices that projects background images in the 3d view
were incorrect. The root cause was that the coordinate systems were not
respected, that was most noticeable when rotating a stretched image.

We re-validated conversions of coordinate spaces (UV -> Image -> Camera -> Window)
and made sure that the rotation is done in image space.

Reviewed By: campbellbarton

Differential Revision: https://developer.blender.org/D5431

===================================================================

M	source/blender/draw/modes/object_mode.c

===================================================================

diff --git a/source/blender/draw/modes/object_mode.c b/source/blender/draw/modes/object_mode.c
index 98c52c300cf..87fc74f1f72 100644
--- a/source/blender/draw/modes/object_mode.c
+++ b/source/blender/draw/modes/object_mode.c
@@ -1254,9 +1254,9 @@ static void DRW_shgroup_camera_background_images(OBJECT_Shaders *sh_data,
       float camera_aspect_y = 1.0;
       float camera_offset_x = 0.0;
       float camera_offset_y = 0.0;
-      float camera_aspect = 1.0;
       float camera_width = size[0];
       float camera_height = size[1];
+      float camera_aspect = camera_width / camera_height;
 
       if (!DRW_state_is_image_render()) {
         rctf render_border;
@@ -1284,48 +1284,52 @@ static void DRW_shgroup_camera_background_images(OBJECT_Shaders *sh_data,
       uv2img_space[0][0] = image_width;
       uv2img_space[1][1] = image_height;
 
-      img2cam_space[0][0] = (1.0 / image_width);
-      img2cam_space[1][1] = (1.0 / 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;
 
       /* Update scaling based on image and camera framing */
       float scale_x = bgpic->scale;
       float scale_y = bgpic->scale;
 
       if (bgpic->flag & CAM_BGIMG_FLAG_CAMERA_ASPECT) {
-        float fit_scale = image_aspect / camera_aspect;
         if (bgpic->flag & CAM_BGIMG_FLAG_CAMERA_CROP) {
           if (image_aspect > camera_aspect) {
             scale_x *= fit_scale;
-          }
-          else {
-            scale_y /= fit_scale;
+            scale_y *= fit_scale;
           }
         }
         else {
           if (image_aspect > camera_aspect) {
+            scale_x /= fit_scale;
             scale_y /= fit_scale;
           }
           else {
             scale_x *= fit_scale;
+            scale_y *= fit_scale;
           }
         }
       }
+      else {
+        /* Stretch image to camera aspect */
+        scale_y /= 1.0 / fit_scale;
+      }
 
       // scale image to match the desired aspect ratio
       scale_m4[0][0] = scale_x;
       scale_m4[1][1] = scale_y;
 
-      /* Translate, using coordinates that aren't squashed by the aspect. */
-      translate_m4[3][0] = bgpic->offset[0] * 2.0f * max_ff(1.0f, 1.0f / camera_aspect);
-      translate_m4[3][1] = bgpic->offset[1] * 2.0f * max_ff(1.0f, camera_aspect);
+      /* Translate */
+      translate_m4[3][0] = image_width * bgpic->offset[0] * 2.0f;
+      translate_m4[3][1] = image_height * bgpic->offset[1] * 2.0f;
 
       mul_m4_series(bg_data->transform_mat,
                     win_m4_translate,
                     win_m4_scale,
-                    translate_m4,
                     img2cam_space,
-                    scale_m4,
+                    translate_m4,
                     rot_m4,
+                    scale_m4,
                     uv2img_space);
 
       DRWPass *pass = (bgpic->flag & CAM_BGIMG_FLAG_FOREGROUND) ? psl->camera_images_front :



More information about the Bf-blender-cvs mailing list