fix [#27839] UV 'Project from view' ignores camera lens shift
[blender.git] / source / blender / blenlib / intern / uvproject.c
index b08f05b4fc8dd496bf6c20674d5b7352a2540bd9..ecb42315ee3a4c33a3007dad76138784915fdcf7 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
  * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
  * ***** END GPL LICENSE BLOCK *****
  */
 
  * ***** END GPL LICENSE BLOCK *****
  */
 
+/** \file blender/blenlib/intern/uvproject.c
+ *  \ingroup bli
+ */
+
+
 #include <math.h>
 
 #include "MEM_guardedalloc.h"
 #include <math.h>
 
 #include "MEM_guardedalloc.h"
@@ -28,6 +33,7 @@
 #include "DNA_object_types.h"
 
 #include "BLI_math.h"
 #include "DNA_object_types.h"
 
 #include "BLI_math.h"
+#include "BLI_uvproject.h"
 
 typedef struct UvCameraInfo {
        float camangle;
 
 typedef struct UvCameraInfo {
        float camangle;
@@ -54,7 +60,7 @@ void project_from_camera(float target[2], float source[3], UvCameraInfo *uci)
        mul_m4_v4(uci->caminv, pv4);
 
        if(uci->do_pano) {
        mul_m4_v4(uci->caminv, pv4);
 
        if(uci->do_pano) {
-               float angle= atan2f(pv4[0], -pv4[2]) / (M_PI * 2.0); /* angle around the camera */
+               float angle= atan2f(pv4[0], -pv4[2]) / ((float)M_PI * 2.0f); /* angle around the camera */
                if (uci->do_persp==0) {
                        target[0]= angle; /* no correct method here, just map to  0-1 */
                        target[1]= pv4[1] / uci->camsize;
                if (uci->do_persp==0) {
                        target[0]= angle; /* no correct method here, just map to  0-1 */
                        target[1]= pv4[1] / uci->camsize;
@@ -63,7 +69,7 @@ void project_from_camera(float target[2], float source[3], UvCameraInfo *uci)
                        float vec2d[2]; /* 2D position from the camera */
                        vec2d[0]= pv4[0];
                        vec2d[1]= pv4[2];
                        float vec2d[2]; /* 2D position from the camera */
                        vec2d[0]= pv4[0];
                        vec2d[1]= pv4[2];
-                       target[0]= angle * (M_PI / uci->camangle);
+                       target[0]= angle * ((float)M_PI / uci->camangle);
                        target[1]= pv4[1] / (len_v2(vec2d) * uci->camsize);
                }
        }
                        target[1]= pv4[1] / (len_v2(vec2d) * uci->camsize);
                }
        }
@@ -103,23 +109,23 @@ void project_from_view(float target[2], float source[3], float persmat[4][4], fl
 
        /* almost project_short */
        mul_m4_v4(persmat, pv4);
 
        /* almost project_short */
        mul_m4_v4(persmat, pv4);
-       if(fabs(pv4[3]) > 0.00001) { /* avoid division by zero */
-               target[0] = winx/2.0 + (winx/2.0) * pv4[0] / pv4[3];
-               target[1] = winy/2.0 + (winy/2.0) * pv4[1] / pv4[3];
+       if(fabsf(pv4[3]) > 0.00001f) { /* avoid division by zero */
+               target[0] = winx/2.0f + (winx/2.0f) * pv4[0] / pv4[3];
+               target[1] = winy/2.0f + (winy/2.0f) * pv4[1] / pv4[3];
        }
        else {
                /* scaling is lost but give a valid result */
        }
        else {
                /* scaling is lost but give a valid result */
-               target[0] = winx/2.0 + (winx/2.0) * pv4[0];
-               target[1] = winy/2.0 + (winy/2.0) * pv4[1];
+               target[0] = winx/2.0f + (winx/2.0f) * pv4[0];
+               target[1] = winy/2.0f + (winy/2.0f) * pv4[1];
        }
 
        /* v3d->persmat seems to do this funky scaling */ 
        if(winx > winy) {
        }
 
        /* v3d->persmat seems to do this funky scaling */ 
        if(winx > winy) {
-               y= (winx - winy)/2.0;
+               y= (winx - winy)/2.0f;
                winy = winx;
        }
        else {
                winy = winx;
        }
        else {
-               x= (winy - winx)/2.0;
+               x= (winy - winx)/2.0f;
                winx = winy;
        }
 
                winx = winy;
        }
 
@@ -163,8 +169,8 @@ UvCameraInfo *project_camera_info(Object *ob, float (*rotmat)[4], float winx, fl
                }
                
                /* include 0.5f here to move the UVs into the center */
                }
                
                /* include 0.5f here to move the UVs into the center */
-               uci.shiftx = 0.5f - camera->shiftx;
-               uci.shifty = 0.5f - camera->shifty;
+               uci.shiftx = 0.5f - (camera->shiftx * uci.xasp);
+               uci.shifty = 0.5f - (camera->shifty * uci.yasp);
                
                uci_pt= MEM_mallocN(sizeof(UvCameraInfo), "UvCameraInfo");
                *uci_pt= uci;
                
                uci_pt= MEM_mallocN(sizeof(UvCameraInfo), "UvCameraInfo");
                *uci_pt= uci;