Blender V5.3
uvproject.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cmath>
10
11#include "MEM_guardedalloc.h"
12
13#include "DNA_camera_types.h"
14#include "DNA_object_types.h"
15
16#include "BLI_math_matrix_c.hh"
18#include "BLI_math_vector_c.hh"
19
20#include "BKE_uvproject.h"
21
22namespace blender {
23
25 float camangle;
26 float camsize;
27 float xasp, yasp;
28 float shiftx, shifty;
29 float rotmat[4][4];
30 float caminv[4][4];
32};
33
34void BKE_uvproject_from_camera(float target[2], const float source[3], const ProjCameraInfo *uci)
35{
36 float pv4[4];
37
38 copy_v3_v3(pv4, source);
39 pv4[3] = 1.0;
40
41 /* rotmat is the object matrix in this case */
42 if (uci->do_rotmat) {
43 mul_m4_v4(uci->rotmat, pv4);
44 }
45
46 /* caminv is the inverse camera matrix */
47 mul_m4_v4(uci->caminv, pv4);
48
49 if (uci->do_pano) {
50 float angle = atan2f(pv4[0], -pv4[2]) / (float(M_PI) * 2.0f); /* angle around the camera */
51 if (uci->do_persp == false) {
52 target[0] = angle; /* No correct method here, just map to 0-1. */
53 target[1] = pv4[1] / uci->camsize;
54 }
55 else {
56 float vec2d[2]; /* 2D position from the camera */
57 vec2d[0] = pv4[0];
58 vec2d[1] = pv4[2];
59 target[0] = angle * (float(M_PI) / uci->camangle);
60 target[1] = pv4[1] / (len_v2(vec2d) * (uci->camsize * 2.0f));
61 }
62 }
63 else {
64 if (pv4[2] == 0.0f) {
65 pv4[2] = 0.00001f; /* don't allow div by 0 */
66 }
67
68 if (uci->do_persp == false) {
69 target[0] = (pv4[0] / uci->camsize);
70 target[1] = (pv4[1] / uci->camsize);
71 }
72 else {
73 target[0] = (-pv4[0] * ((1.0f / uci->camsize) / pv4[2])) / 2.0f;
74 target[1] = (-pv4[1] * ((1.0f / uci->camsize) / pv4[2])) / 2.0f;
75 }
76 }
77
78 target[0] *= uci->xasp;
79 target[1] *= uci->yasp;
80
81 /* adds camera shift + 0.5 */
82 target[0] += uci->shiftx;
83 target[1] += uci->shifty;
84}
85
86void BKE_uvproject_from_view(float target[2],
87 const float source[3],
88 const float persmat[4][4],
89 const float rotmat[4][4],
90 float winx,
91 float winy)
92{
93 float pv4[4], x = 0.0, y = 0.0;
94
95 copy_v3_v3(pv4, source);
96 pv4[3] = 1.0;
97
98 /* rotmat is the object matrix in this case */
99 mul_m4_v4(rotmat, pv4);
100
101 /* almost ED_view3d_project_short */
102 mul_m4_v4(persmat, pv4);
103 if (fabsf(pv4[3]) > 0.00001f) { /* avoid division by zero */
104 target[0] = winx / 2.0f + (winx / 2.0f) * pv4[0] / pv4[3];
105 target[1] = winy / 2.0f + (winy / 2.0f) * pv4[1] / pv4[3];
106 }
107 else {
108 /* scaling is lost but give a valid result */
109 target[0] = winx / 2.0f + (winx / 2.0f) * pv4[0];
110 target[1] = winy / 2.0f + (winy / 2.0f) * pv4[1];
111 }
112
113 /* v3d->persmat seems to do this funky scaling */
114 if (winx > winy) {
115 y = (winx - winy) / 2.0f;
116 winy = winx;
117 }
118 else {
119 x = (winy - winx) / 2.0f;
120 winx = winy;
121 }
122
123 target[0] = (x + target[0]) / winx;
124 target[1] = (y + target[1]) / winy;
125}
126
128 const float rotmat[4][4],
129 float winx,
130 float winy)
131{
132 ProjCameraInfo uci;
133 const Camera *camera = id_cast<Camera *>(ob->data);
134
135 uci.do_pano = (camera->type == CAM_PANO);
136 uci.do_persp = ELEM(camera->type, CAM_PERSP, CAM_CUSTOM);
137
138 uci.camangle = focallength_to_fov(camera->lens, camera->sensor_x) / 2.0f;
139 uci.camsize = uci.do_persp ? tanf(uci.camangle) : camera->ortho_scale;
140
141 /* account for scaled cameras */
142 copy_m4_m4(uci.caminv, ob->object_to_world().ptr());
143 normalize_m4(uci.caminv);
144
145 if (invert_m4(uci.caminv)) {
146 ProjCameraInfo *uci_pt;
147
148 /* normal projection */
149 if (rotmat) {
150 copy_m4_m4(uci.rotmat, rotmat);
151 uci.do_rotmat = true;
152 }
153 else {
154 uci.do_rotmat = false;
155 }
156
157 /* also make aspect ratio adjustment factors */
158 if (winx > winy) {
159 uci.xasp = 1.0f;
160 uci.yasp = winx / winy;
161 }
162 else {
163 uci.xasp = winy / winx;
164 uci.yasp = 1.0f;
165 }
166
167 /* include 0.5f here to move the UVs into the center */
168 uci.shiftx = 0.5f - (camera->shiftx * uci.xasp);
169 uci.shifty = 0.5f - (camera->shifty * uci.yasp);
170
171 uci_pt = MEM_new_uninitialized<ProjCameraInfo>(__func__);
172 *uci_pt = uci;
173 return uci_pt;
174 }
175
176 return nullptr;
177}
178
180{
181 MEM_delete(uci);
182}
183
184void BKE_uvproject_from_view_ortho(float target[2],
185 const float source[3],
186 const float rotmat[4][4])
187{
188 float pv[3];
189
190 mul_v3_m4v3(pv, rotmat, source);
191
192 /* ortho projection */
193 target[0] = -pv[0];
194 target[1] = pv[2];
195}
196
197void BKE_uvproject_camera_info_scale(ProjCameraInfo *uci, float scale_x, float scale_y)
198{
199 uci->xasp *= scale_x;
200 uci->yasp *= scale_y;
201}
202
203} // namespace blender
#define M_PI
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define ELEM(...)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
GREAL2 vec2d[2]
Double vector 2D.
void camera(KernelGlobals &kg, const ShadingData &sd, float3 &outview, float &outdepth, float &outdist)
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void BKE_uvproject_camera_info_scale(ProjCameraInfo *uci, float scale_x, float scale_y)
Definition uvproject.cc:197
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
void BKE_uvproject_from_view(float target[2], const float source[3], const float persmat[4][4], const float rotmat[4][4], float winx, float winy)
Definition uvproject.cc:86
void BKE_uvproject_from_camera(float target[2], const float source[3], const struct ProjCameraInfo *uci)
bool invert_m4(float mat[4][4])
void BKE_uvproject_camera_info_free(ProjCameraInfo *uci)
Definition uvproject.cc:179
void normalize_m4(float R[4][4]) ATTR_NONNULL()
float focallength_to_fov(float focal_length, float sensor)
void BKE_uvproject_from_view_ortho(float target[2], const float source[3], const float rotmat[4][4])
Definition uvproject.cc:184
void mul_m4_v4(const float mat[4][4], float r[4])
void copy_m4_m4(float m1[4][4], const float m2[4][4])
MINLINE float len_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
struct ProjCameraInfo * BKE_uvproject_camera_info(const struct Object *ob, const float rotmat[4][4], float winx, float winy)
#define fabsf
#define tanf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float