This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/blenlib/intern/uvproject.c

199 lines
5.0 KiB
C
Raw Normal View History

/*
* ***** 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
*/
#include <math.h>
#include "MEM_guardedalloc.h"
#include "DNA_camera_types.h"
#include "DNA_object_types.h"
#include "BLI_math.h"
#include "BLI_uvproject.h"
typedef struct ProjCameraInfo {
float camangle;
float camsize;
float xasp, yasp;
float shiftx, shifty;
float rotmat[4][4];
float caminv[4][4];
short do_persp, do_pano, do_rotmat;
} ProjCameraInfo;
void BLI_uvproject_from_camera(float target[2], float source[3], ProjCameraInfo *uci)
{
float pv4[4];
copy_v3_v3(pv4, source);
2012-03-11 23:47:41 +00:00
pv4[3] = 1.0;
/* rotmat is the object matrix in this case */
2012-03-11 23:47:41 +00:00
if (uci->do_rotmat)
mul_m4_v4(uci->rotmat, pv4);
/* 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 */
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;
}
else {
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));
}
}
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 */
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);
}
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;
}
}
target[0] *= uci->xasp;
target[1] *= uci->yasp;
2012-03-11 23:47:41 +00:00
/* adds camera shift + 0.5 */
target[0] += uci->shiftx;
target[1] += uci->shifty;
}
/* could rv3d->persmat */
void BLI_uvproject_from_view(float target[2], float source[3], float persmat[4][4], float rotmat[4][4], float winx, float winy)
{
float pv4[4], x = 0.0, y = 0.0;
copy_v3_v3(pv4, source);
2012-03-11 23:47:41 +00:00
pv4[3] = 1.0;
/* rotmat is the object matrix in this case */
2012-03-11 23:47:41 +00:00
mul_m4_v4(rotmat, pv4);
/* almost ED_view3d_project_short */
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];
}
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];
}
2012-03-11 23:47:41 +00:00
/* v3d->persmat seems to do this funky scaling */
if (winx > winy) {
y = (winx - winy) / 2.0f;
winy = winx;
}
else {
2012-03-11 23:47:41 +00:00
x = (winy - winx) / 2.0f;
winx = winy;
}
2012-03-11 23:47:41 +00:00
target[0] = (x + target[0]) / winx;
target[1] = (y + target[1]) / winy;
}
/* '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 */
ProjCameraInfo *BLI_uvproject_camera_info(Object *ob, float(*rotmat)[4], float winx, float winy)
{
ProjCameraInfo uci;
2012-03-11 23:47:41 +00:00
Camera *camera = ob->data;
Fisheye Camera for Cycles For sample images see: http://www.dalaifelinto.com/?p=399 (equisolid) http://www.dalaifelinto.com/?p=389 (equidistant) The 'use_panorama' option is now part of a new Camera type: 'Panorama'. Created two other panorama cameras: - Equisolid: most of lens in the market simulate this lens - e.g. Nikon, Canon, ...) this works as a real lens up to an extent. The final result takes the sensor dimensions into account also. .:. to simulate a Nikon DX2S with a 10.5mm lens do: sensor: 23.7 x 15.7 fisheye lens: 10.5 fisheye fov: 180 render dimensions: 4288 x 2848 - Equidistant: this is not a real lens model. Although the old equidistant lens simulate this lens. The result is always as a circular fisheye that takes the whole sensor (in other words, it doesn't take the sensor into consideration). This is perfect for fulldomes ;) For the UI we have 10 to 360 as soft values and 10 to 3600 as hard values (because we can). Reference material: http://www.hdrlabs.com/tutorials/downloads_files/HDRI%20for%20CGI.pdf http://www.bobatkins.com/photography/technical/field_of_view.html Note, this is not a real simulation of the light path through the lens. The ideal solution would be this: https://graphics.stanford.edu/wikis/cs348b-11/Assignment3 http://www.graphics.stanford.edu/papers/camera/ Thanks Brecht for the fix, suggestions and code review. Kudos for the dome community for keeping me stimulated on the topic since 2009 ;) Patch partly implemented during lab time at VisGraf, IMPA - Rio de Janeiro.
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);
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;
/* account for scaled cameras */
copy_m4_m4(uci.caminv, ob->obmat);
normalize_m4(uci.caminv);
if (invert_m4(uci.caminv)) {
ProjCameraInfo *uci_pt;
/* normal projection */
2012-03-11 23:47:41 +00:00
if (rotmat) {
copy_m4_m4(uci.rotmat, rotmat);
uci.do_rotmat = TRUE;
}
else {
uci.do_rotmat = FALSE;
}
/* 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;
}
else {
2012-03-11 23:47:41 +00:00
uci.xasp = winy / winx;
uci.yasp = 1.0f;
}
2012-03-11 23:47:41 +00:00
/* include 0.5f here to move the UVs into the center */
uci.shiftx = 0.5f - (camera->shiftx * uci.xasp);
uci.shifty = 0.5f - (camera->shifty * uci.yasp);
2012-03-11 23:47:41 +00:00
uci_pt = MEM_mallocN(sizeof(ProjCameraInfo), "ProjCameraInfo");
2012-03-11 23:47:41 +00:00
*uci_pt = uci;
return uci_pt;
}
return NULL;
}
void BLI_uvproject_from_view_ortho(float target[2], float source[3], float rotmat[4][4])
{
float pv[3];
mul_v3_m4v3(pv, rotmat, source);
/* ortho projection */
target[0] = -pv[0];
target[1] = pv[2];
}
void BLI_uvproject_camera_info_scale(ProjCameraInfo *uci, float scale_x, float scale_y)
{
uci->xasp *= scale_x;
uci->yasp *= scale_y;
}