Blender V5.3
view3d_camera_control.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
27
28#include "DNA_camera_types.h"
29#include "DNA_object_types.h"
30#include "DNA_scene_types.h"
31
32#include "MEM_guardedalloc.h"
33
34#include "BLI_math_matrix_c.hh"
36#include "BLI_math_vector_c.hh"
37
38#include "BKE_object.hh"
39
40#include "DEG_depsgraph.hh"
41
42#include "view3d_intern.hh" /* own include */
43
44namespace blender {
45
47
48 /* -------------------------------------------------------------------- */
49 /* Context (assign these to vars before use) */
53
54 /* -------------------------------------------------------------------- */
55 /* internal vars */
56
57 /* for parenting calculation */
58 float view_mat_prev[4][4];
59
60 /* -------------------------------------------------------------------- */
61 /* optional capabilities */
62
64
65 /* -------------------------------------------------------------------- */
66 /* initial values */
67
70
71 /* Backup values. */
72
76 float ofs_backup[3];
77
79 float rot_backup[4];
85
92
94 void *obtfm;
95};
96
98{
99 return vctrl->root_parent ? vctrl->root_parent : vctrl->ctx_v3d->camera;
100}
101
103{
104 RegionView3D *rv3d = vctrl->ctx_rv3d;
105
106 if (rv3d->persp == RV3D_CAMOB) {
107 return view3d_cameracontrol_object(vctrl);
108 }
109
110 return nullptr;
111}
112
114 Scene *scene,
115 View3D *v3d,
116 RegionView3D *rv3d)
117{
118 View3DCameraControl *vctrl;
119
120 vctrl = MEM_new_zeroed<View3DCameraControl>(__func__);
121
122 /* Store context */
123 vctrl->ctx_scene = scene;
124 vctrl->ctx_v3d = v3d;
125 vctrl->ctx_rv3d = rv3d;
126
127 vctrl->use_parent_root = v3d->camera != nullptr &&
129
130 vctrl->persp_backup = rv3d->persp;
131 vctrl->dist_backup = rv3d->dist;
132
133 /* check for flying ortho camera - which we can't support well
134 * we _could_ also check for an ortho camera but this is easier */
135 if ((rv3d->persp == RV3D_CAMOB) && (rv3d->is_persp == false)) {
136 (id_cast<Camera *>(v3d->camera->data))->type = CAM_PERSP;
137 vctrl->is_ortho_cam = true;
138 }
139
140 if (rv3d->persp == RV3D_CAMOB) {
141 Object *ob_back;
142 if (vctrl->use_parent_root && (vctrl->root_parent = v3d->camera->parent)) {
143 while (vctrl->root_parent->parent) {
144 vctrl->root_parent = vctrl->root_parent->parent;
145 }
146 ob_back = vctrl->root_parent;
147 }
148 else {
149 ob_back = v3d->camera;
150 }
151
152 /* store the original camera loc and rot */
153 vctrl->obtfm = BKE_object_tfm_backup(ob_back);
154
155 BKE_object_where_is_calc(depsgraph, scene, v3d->camera);
156 negate_v3_v3(rv3d->ofs, v3d->camera->object_to_world().location());
157
158 rv3d->dist = 0.0;
159 }
160 else {
161 /* perspective or ortho */
162 if (rv3d->persp == RV3D_ORTHO) {
163 /* if ortho projection, make perspective */
164 rv3d->persp = RV3D_PERSP;
165 }
166
167 copy_qt_qt(vctrl->rot_backup, rv3d->viewquat);
168 copy_v3_v3(vctrl->ofs_backup, rv3d->ofs);
169
170 /* The dist defines a vector that is in front of the offset
171 * to rotate the view about.
172 * this is no good for fly mode because we
173 * want to rotate about the viewers center.
174 * but to correct the dist removal we must
175 * alter offset so the view doesn't jump. */
176
177 ED_view3d_distance_set(rv3d, 0.0f);
178 /* Done with correcting for the dist */
179 }
180
181 ED_view3d_to_m4(vctrl->view_mat_prev, rv3d->ofs, rv3d->viewquat, rv3d->dist, rv3d->camroll);
182
183 return vctrl;
184}
185
196 Object *ob,
197 const float obmat[4][4],
198 const bool use_parent, /* Only use when applying lock. */
199 RegionView3D *rv3d,
200 const float view_mat[4][4])
201{
202 const bool use_protect = (ob->protectflag != 0);
203 bool view_changed = false;
204
206 if (use_protect) {
208 }
209
210 BKE_object_apply_mat4(ob, obmat, true, use_parent);
211
212 if (use_protect) {
213 float obmat_noprotect[4][4], obmat_protect[4][4];
214
215 BKE_object_to_mat4(ob, obmat_noprotect);
217 BKE_object_to_mat4(ob, obmat_protect);
218
219 if (!equals_m4m4(obmat_noprotect, obmat_protect)) {
220 /* Apply the lock protection back to the view, without this the view
221 * keeps moving, ignoring the object locking, causing jittering in some cases. */
222 float diff_mat[4][4];
223 float view_mat_protect[4][4];
224 float obmat_noprotect_inv[4][4];
225 invert_m4_m4(obmat_noprotect_inv, obmat_noprotect);
226 mul_m4_m4m4(diff_mat, obmat_protect, obmat_noprotect_inv);
227
228 mul_m4_m4m4(view_mat_protect, diff_mat, view_mat);
229 ED_view3d_from_m4(view_mat_protect, rv3d->ofs, rv3d->viewquat, &rv3d->dist, 0.0f);
230 view_changed = true;
231 }
232 }
233 return view_changed;
234}
235
236void ED_view3d_cameracontrol_update(View3DCameraControl *vctrl, /* args for keyframing */
237 const bool use_autokey,
238 bContext *C,
239 const bool do_rotate,
240 const bool do_translate)
241{
242 /* We are in camera view so apply the view offset and rotation to the view matrix
243 * and set the camera to the view. */
244
245 Scene *scene = vctrl->ctx_scene;
246 View3D *v3d = vctrl->ctx_v3d;
247 RegionView3D *rv3d = vctrl->ctx_rv3d;
248
249 ID *id_key;
250
251 float view_mat[4][4];
252 ED_view3d_to_m4(view_mat, rv3d->ofs, rv3d->viewquat, rv3d->dist, rv3d->camroll);
253
254 /* transform the parent or the camera? */
255 if (vctrl->root_parent) {
256 Object *ob_update;
257
258 float prev_view_imat[4][4];
259 float diff_mat[4][4];
260 float parent_mat[4][4];
261
262 invert_m4_m4(prev_view_imat, vctrl->view_mat_prev);
263 mul_m4_m4m4(diff_mat, view_mat, prev_view_imat);
264 mul_m4_m4m4(parent_mat, diff_mat, vctrl->root_parent->object_to_world().ptr());
265
266 if (object_apply_mat4_with_protect(vctrl->root_parent, parent_mat, false, rv3d, view_mat)) {
267 /* Calculate again since the view locking changes the matrix. */
268 ED_view3d_to_m4(view_mat, rv3d->ofs, rv3d->viewquat, rv3d->dist, rv3d->camroll);
269 }
270
271 ob_update = v3d->camera->parent;
272 while (ob_update) {
274 ob_update = ob_update->parent;
275 }
276
277 copy_m4_m4(vctrl->view_mat_prev, view_mat);
278
279 id_key = &vctrl->root_parent->id;
280 }
281 else {
282 float scale_mat[4][4];
283 float scale_back[3];
284
285 /* even though we handle the scale matrix, this still changes over time */
286 copy_v3_v3(scale_back, v3d->camera->scale);
287
288 size_to_mat4(scale_mat, v3d->camera->scale);
289 mul_m4_m4m4(view_mat, view_mat, scale_mat);
290
291 object_apply_mat4_with_protect(v3d->camera, view_mat, true, rv3d, view_mat);
292
294
295 copy_v3_v3(v3d->camera->scale, scale_back);
296
297 id_key = &v3d->camera->id;
298 }
299
300 /* record the motion */
301 if (use_autokey) {
302 ED_view3d_camera_autokey(scene, id_key, C, do_rotate, do_translate);
303 }
304}
305
307{
308 View3D *v3d = vctrl->ctx_v3d;
309 RegionView3D *rv3d = vctrl->ctx_rv3d;
310
311 if (restore) {
312 /* Revert to original view? */
313 if (vctrl->persp_backup == RV3D_CAMOB) { /* a camera view */
314 Object *ob_back = view3d_cameracontrol_object(vctrl);
315
316 /* store the original camera loc and rot */
317 BKE_object_tfm_restore(ob_back, vctrl->obtfm);
318
320 }
321 else {
322 /* Non Camera we need to reset the view back
323 * to the original location because the user canceled. */
324 copy_qt_qt(rv3d->viewquat, vctrl->rot_backup);
325 rv3d->persp = vctrl->persp_backup;
326 }
327 /* always, is set to zero otherwise */
328 copy_v3_v3(rv3d->ofs, vctrl->ofs_backup);
329 rv3d->dist = vctrl->dist_backup;
330 }
331 else if (vctrl->persp_backup == RV3D_CAMOB) { /* camera */
333
334 /* always, is set to zero otherwise */
335 copy_v3_v3(rv3d->ofs, vctrl->ofs_backup);
336 rv3d->dist = vctrl->dist_backup;
337 }
338 else { /* not camera */
339 /* Apply the fly mode view */
340 /* restore the dist */
342 /* Done with correcting for the dist */
343 }
344
345 if (vctrl->is_ortho_cam) {
346 (id_cast<Camera *>(v3d->camera->data))->type = CAM_ORTHO;
347 }
348
349 if (vctrl->obtfm) {
351 }
352
353 MEM_delete(vctrl);
354}
355
356} // namespace blender
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define BLI_INLINE
BMesh Iterators.
MINLINE void negate_v3_v3(float r[3], const float a[3])
static bool object_apply_mat4_with_protect(Object *ob, const float obmat[4][4], const bool use_parent, RegionView3D *rv3d, const float view_mat[4][4])
void BKE_object_tfm_protected_backup(const Object *ob, ObjectTfmProtectedChannels *obtfm)
Definition object.cc:3025
MINLINE void copy_v3_v3(float r[3], const float a[3])
void ED_view3d_distance_set(RegionView3D *rv3d, float dist)
Object * ED_view3d_cameracontrol_object_get(View3DCameraControl *vctrl)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
bool ED_view3d_camera_autokey(const Scene *scene, ID *id_key, bContext *C, bool do_rotate, bool do_translate)
BLI_INLINE Object * view3d_cameracontrol_object(const View3DCameraControl *vctrl)
void BKE_object_apply_mat4(Object *ob, const float mat[4][4], bool use_compat, bool use_parent)
Definition object.cc:3663
void ED_view3d_cameracontrol_release(View3DCameraControl *vctrl, const bool restore)
void size_to_mat4(float R[4][4], const float size[3])
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void copy_qt_qt(float q[4], const float a[4])
void BKE_object_to_mat4(const Object *ob, float r_mat[4][4])
Definition object.cc:3125
void DEG_id_tag_update(ID *id, unsigned int flags)
View3DCameraControl * ED_view3d_cameracontrol_acquire(Depsgraph *depsgraph, Scene *scene, View3D *v3d, RegionView3D *rv3d)
void * BKE_object_tfm_backup(Object *ob)
Definition object.cc:4141
bool equals_m4m4(const float mat1[4][4], const float mat2[4][4])
void BKE_object_tfm_restore(Object *ob, void *obtfm_pt)
Definition object.cc:4164
void ED_view3d_to_m4(float mat[4][4], const float ofs[3], const float quat[4], float dist, float roll)
@ OB_TRANSFORM_ADJUST_ROOT_PARENT_FOR_VIEW_LOCK
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
void copy_m4_m4(float m1[4][4], const float m2[4][4])
void BKE_object_where_is_calc(Depsgraph *depsgraph, Scene *scene, Object *ob)
Definition object.cc:3577
void ED_view3d_cameracontrol_update(View3DCameraControl *vctrl, const bool use_autokey, bContext *C, const bool do_rotate, const bool do_translate)
void BKE_object_tfm_free(void *obtfm_pt)
Definition object.cc:4185
void ED_view3d_from_m4(const float mat[4][4], float ofs[3], float quat[4], const float *dist, float roll)
void BKE_object_tfm_protected_restore(Object *ob, const ObjectTfmProtectedChannels *obtfm, short protectflag)
Definition object.cc:3050
struct Object * parent
eObject_TransFlag transflag
eObject_ProtectFlag protectflag
eRegionView3D_Persp persp
struct Object * camera