2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2010-03-23 18:28:38 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
2011-02-27 20:37:56 +00:00
|
|
|
/** \file blender/blenlib/intern/uvproject.c
|
|
|
|
* \ingroup bli
|
|
|
|
*/
|
|
|
|
|
2010-03-23 18:28:38 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
#include "DNA_camera_types.h"
|
|
|
|
#include "DNA_object_types.h"
|
|
|
|
|
|
|
|
#include "BLI_math.h"
|
2011-02-14 17:55:27 +00:00
|
|
|
#include "BLI_uvproject.h"
|
2010-03-23 18:28:38 +00:00
|
|
|
|
2012-05-05 00:23:55 +00:00
|
|
|
typedef struct ProjCameraInfo {
|
2010-03-23 18:28:38 +00:00
|
|
|
float camangle;
|
|
|
|
float camsize;
|
|
|
|
float xasp, yasp;
|
|
|
|
float shiftx, shifty;
|
|
|
|
float rotmat[4][4];
|
|
|
|
float caminv[4][4];
|
2010-03-23 20:04:05 +00:00
|
|
|
short do_persp, do_pano, do_rotmat;
|
2012-05-05 00:23:55 +00:00
|
|
|
} ProjCameraInfo;
|
2010-03-23 18:28:38 +00:00
|
|
|
|
2012-05-05 00:23:55 +00:00
|
|
|
void BLI_uvproject_from_camera(float target[2], float source[3], ProjCameraInfo *uci)
|
2010-03-23 18:28:38 +00:00
|
|
|
{
|
|
|
|
float pv4[4];
|
|
|
|
|
|
|
|
copy_v3_v3(pv4, source);
|
2012-03-11 23:47:41 +00:00
|
|
|
pv4[3] = 1.0;
|
2010-03-23 18:28:38 +00:00
|
|
|
|
|
|
|
/* rotmat is the object matrix in this case */
|
2012-03-11 23:47:41 +00:00
|
|
|
if (uci->do_rotmat)
|
2010-03-23 20:04:05 +00:00
|
|
|
mul_m4_v4(uci->rotmat, pv4);
|
2010-03-23 18:28:38 +00:00
|
|
|
|
|
|
|
/* caminv is the inverse camera matrix */
|
|
|
|
mul_m4_v4(uci->caminv, pv4);
|
|
|
|
|
2012-03-11 23:47:41 +00:00
|
|
|
if (uci->do_pano) {
|
|
|
|
float angle = atan2f(pv4[0], -pv4[2]) / ((float)M_PI * 2.0f); /* angle around the camera */
|
2012-05-19 13:28:19 +00:00
|
|
|
if (uci->do_persp == FALSE) {
|
2012-03-11 23:47:41 +00:00
|
|
|
target[0] = angle; /* no correct method here, just map to 0-1 */
|
|
|
|
target[1] = pv4[1] / uci->camsize;
|
2010-03-23 18:28:38 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-11-03 06:31:53 +00:00
|
|
|
float vec2d[2]; /* 2D position from the camera */
|
2012-03-11 23:47:41 +00:00
|
|
|
vec2d[0] = pv4[0];
|
|
|
|
vec2d[1] = pv4[2];
|
|
|
|
target[0] = angle * ((float)M_PI / uci->camangle);
|
|
|
|
target[1] = pv4[1] / (len_v2(vec2d) * (uci->camsize * 2.0f));
|
2010-03-23 18:28:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-03-11 23:47:41 +00:00
|
|
|
if (pv4[2] == 0.0f)
|
2012-05-12 15:13:06 +00:00
|
|
|
pv4[2] = 0.00001f; /* don't allow div by 0 */
|
2010-03-23 18:28:38 +00:00
|
|
|
|
2012-05-19 13:28:19 +00:00
|
|
|
if (uci->do_persp == FALSE) {
|
2012-03-11 23:47:41 +00:00
|
|
|
target[0] = (pv4[0] / uci->camsize);
|
|
|
|
target[1] = (pv4[1] / uci->camsize);
|
2010-03-23 18:28:38 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-03-11 23:47:41 +00:00
|
|
|
target[0] = (-pv4[0] * ((1.0f / uci->camsize) / pv4[2])) / 2.0f;
|
|
|
|
target[1] = (-pv4[1] * ((1.0f / uci->camsize) / pv4[2])) / 2.0f;
|
2010-03-23 18:28:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-24 16:20:13 +00:00
|
|
|
target[0] *= uci->xasp;
|
|
|
|
target[1] *= uci->yasp;
|
2012-03-11 23:47:41 +00:00
|
|
|
|
2010-03-23 18:28:38 +00:00
|
|
|
/* adds camera shift + 0.5 */
|
|
|
|
target[0] += uci->shiftx;
|
|
|
|
target[1] += uci->shifty;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* could rv3d->persmat */
|
2012-05-05 00:23:55 +00:00
|
|
|
void BLI_uvproject_from_view(float target[2], float source[3], float persmat[4][4], float rotmat[4][4], float winx, float winy)
|
2010-03-23 18:28:38 +00:00
|
|
|
{
|
2013-04-27 21:56:28 +00:00
|
|
|
float pv4[4], x = 0.0, y = 0.0;
|
2010-03-23 18:28:38 +00:00
|
|
|
|
|
|
|
copy_v3_v3(pv4, source);
|
2012-03-11 23:47:41 +00:00
|
|
|
pv4[3] = 1.0;
|
2010-03-23 18:28:38 +00:00
|
|
|
|
|
|
|
/* rotmat is the object matrix in this case */
|
2012-03-11 23:47:41 +00:00
|
|
|
mul_m4_v4(rotmat, pv4);
|
2010-03-23 18:28:38 +00:00
|
|
|
|
2012-09-20 04:56:24 +00:00
|
|
|
/* almost ED_view3d_project_short */
|
2010-03-23 18:28:38 +00:00
|
|
|
mul_m4_v4(persmat, pv4);
|
2012-03-11 23:47:41 +00:00
|
|
|
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];
|
2010-03-23 18:28:38 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* scaling is lost but give a valid result */
|
2012-03-11 23:47:41 +00:00
|
|
|
target[0] = winx / 2.0f + (winx / 2.0f) * pv4[0];
|
|
|
|
target[1] = winy / 2.0f + (winy / 2.0f) * pv4[1];
|
2010-03-23 18:28:38 +00:00
|
|
|
}
|
|
|
|
|
2012-03-11 23:47:41 +00:00
|
|
|
/* v3d->persmat seems to do this funky scaling */
|
|
|
|
if (winx > winy) {
|
|
|
|
y = (winx - winy) / 2.0f;
|
2010-03-23 18:28:38 +00:00
|
|
|
winy = winx;
|
|
|
|
}
|
|
|
|
else {
|
2012-03-11 23:47:41 +00:00
|
|
|
x = (winy - winx) / 2.0f;
|
2010-03-23 18:28:38 +00:00
|
|
|
winx = winy;
|
|
|
|
}
|
|
|
|
|
2012-03-11 23:47:41 +00:00
|
|
|
target[0] = (x + target[0]) / winx;
|
|
|
|
target[1] = (y + target[1]) / winy;
|
2010-03-23 18:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 'rotmat' can be obedit->obmat when uv project is used.
|
2012-03-11 23:47:41 +00:00
|
|
|
* 'winx' and 'winy' can be from scene->r.xsch/ysch */
|
2012-05-05 00:23:55 +00:00
|
|
|
ProjCameraInfo *BLI_uvproject_camera_info(Object *ob, float(*rotmat)[4], float winx, float winy)
|
2010-03-23 18:28:38 +00:00
|
|
|
{
|
2012-05-05 00:23:55 +00:00
|
|
|
ProjCameraInfo uci;
|
2012-03-11 23:47:41 +00:00
|
|
|
Camera *camera = ob->data;
|
2010-03-23 18:28:38 +00:00
|
|
|
|
2012-05-04 16:20:51 +00:00
|
|
|
uci.do_pano = (camera->type == CAM_PANO);
|
2012-03-11 23:47:41 +00:00
|
|
|
uci.do_persp = (camera->type == CAM_PERSP);
|
2010-03-23 18:28:38 +00:00
|
|
|
|
2012-03-11 23:47:41 +00:00
|
|
|
uci.camangle = focallength_to_fov(camera->lens, camera->sensor_x) / 2.0f;
|
|
|
|
uci.camsize = uci.do_persp ? tanf(uci.camangle) : camera->ortho_scale;
|
2010-03-23 18:28:38 +00:00
|
|
|
|
2011-07-03 10:48:18 +00:00
|
|
|
/* account for scaled cameras */
|
|
|
|
copy_m4_m4(uci.caminv, ob->obmat);
|
|
|
|
normalize_m4(uci.caminv);
|
|
|
|
|
|
|
|
if (invert_m4(uci.caminv)) {
|
2012-05-05 00:23:55 +00:00
|
|
|
ProjCameraInfo *uci_pt;
|
2010-03-23 18:28:38 +00:00
|
|
|
|
|
|
|
/* normal projection */
|
2012-03-11 23:47:41 +00:00
|
|
|
if (rotmat) {
|
2010-03-23 20:04:05 +00:00
|
|
|
copy_m4_m4(uci.rotmat, rotmat);
|
2012-05-19 13:28:19 +00:00
|
|
|
uci.do_rotmat = TRUE;
|
2010-03-23 20:04:05 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-05-19 13:28:19 +00:00
|
|
|
uci.do_rotmat = FALSE;
|
2010-03-23 20:04:05 +00:00
|
|
|
}
|
2010-03-23 18:28:38 +00:00
|
|
|
|
|
|
|
/* also make aspect ratio adjustment factors */
|
|
|
|
if (winx > winy) {
|
2012-03-11 23:47:41 +00:00
|
|
|
uci.xasp = 1.0f;
|
|
|
|
uci.yasp = winx / winy;
|
2010-03-23 18:28:38 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-03-11 23:47:41 +00:00
|
|
|
uci.xasp = winy / winx;
|
|
|
|
uci.yasp = 1.0f;
|
2010-03-23 18:28:38 +00:00
|
|
|
}
|
2012-03-11 23:47:41 +00:00
|
|
|
|
2010-03-23 18:28:38 +00:00
|
|
|
/* include 0.5f here to move the UVs into the center */
|
2011-07-03 07:37:33 +00:00
|
|
|
uci.shiftx = 0.5f - (camera->shiftx * uci.xasp);
|
|
|
|
uci.shifty = 0.5f - (camera->shifty * uci.yasp);
|
2012-03-11 23:47:41 +00:00
|
|
|
|
2012-05-05 00:23:55 +00:00
|
|
|
uci_pt = MEM_mallocN(sizeof(ProjCameraInfo), "ProjCameraInfo");
|
2012-03-11 23:47:41 +00:00
|
|
|
*uci_pt = uci;
|
2010-03-23 18:28:38 +00:00
|
|
|
return uci_pt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-05-05 00:23:55 +00:00
|
|
|
void BLI_uvproject_from_view_ortho(float target[2], float source[3], float rotmat[4][4])
|
2010-03-23 18:28:38 +00:00
|
|
|
{
|
|
|
|
float pv[3];
|
|
|
|
|
2010-03-23 20:04:05 +00:00
|
|
|
mul_v3_m4v3(pv, rotmat, source);
|
2010-03-23 18:28:38 +00:00
|
|
|
|
|
|
|
/* ortho projection */
|
|
|
|
target[0] = -pv[0];
|
|
|
|
target[1] = pv[2];
|
|
|
|
}
|
2010-06-15 21:46:02 +00:00
|
|
|
|
2012-05-05 00:23:55 +00:00
|
|
|
void BLI_uvproject_camera_info_scale(ProjCameraInfo *uci, float scale_x, float scale_y)
|
2010-06-15 21:46:02 +00:00
|
|
|
{
|
|
|
|
uci->xasp *= scale_x;
|
|
|
|
uci->yasp *= scale_y;
|
|
|
|
}
|