Blender V5.3
blenkernel/intern/camera.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10#include <optional>
11
12/* Allow using deprecated functionality for .blend file I/O. */
13#define DNA_DEPRECATED_ALLOW
14
15#include "DNA_ID.h"
16#include "DNA_camera_types.h"
17#include "DNA_light_types.h"
18#include "DNA_object_types.h"
19#include "DNA_scene_types.h"
20#include "DNA_text_types.h"
21#include "DNA_view3d_types.h"
22
23#include "BLI_listbase.hh"
24#include "BLI_math_geom_c.hh"
25#include "BLI_math_matrix.hh"
26#include "BLI_math_matrix_c.hh"
27#include "BLI_math_vector_c.hh"
29#include "BLI_rect.hh"
30#include "BLI_string.hh"
31#include "BLI_utildefines.hh"
32
33#include "BKE_action.hh"
34#include "BKE_bpath.hh"
35#include "BKE_camera.h"
36#include "BKE_idprop.hh"
37#include "BKE_idtype.hh"
38#include "BKE_lib_id.hh"
39#include "BKE_lib_query.hh"
40#include "BKE_object.hh"
41#include "BKE_scene.hh"
42#include "BKE_screen.hh"
43
44#include "BLT_translation.hh"
45
47
48#include "MEM_guardedalloc.h"
49
50#include "BLO_read_write.hh"
51
52namespace blender {
53
54/* -------------------------------------------------------------------- */
57
58static void camera_init_data(ID *id)
59{
60 Camera *cam = id_cast<Camera *>(id);
62}
63
74static void camera_copy_data(Main * /*bmain*/,
75 std::optional<Library *> /*owner_library*/,
76 ID *id_dst,
77 const ID *id_src,
78 const int flag)
79{
80 Camera *cam_dst = id_cast<Camera *>(id_dst);
81 const Camera *cam_src = id_cast<const Camera *>(id_src);
82
83 /* We never handle user-count here for owned data. */
84 const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
85
86 cam_dst->bg_images.clear_no_delete();
87 for (CameraBGImage &bgpic_src : cam_src->bg_images) {
88 CameraBGImage *bgpic_dst = BKE_camera_background_image_copy(&bgpic_src, flag_subdata);
89 BLI_addtail(&cam_dst->bg_images, bgpic_dst);
90 }
91
92 if (cam_src->custom_bytecode) {
93 cam_dst->custom_bytecode = MEM_dupalloc(cam_src->custom_bytecode);
94 }
95}
96
98static void camera_free_data(ID *id)
99{
100 Camera *cam = id_cast<Camera *>(id);
101 cam->bg_images.free_no_destruct();
102 if (cam->custom_bytecode) {
103 MEM_delete(cam->custom_bytecode);
104 }
105}
106
124
125static void camera_foreach_path(ID *id, BPathForeachPathData *bpath_data)
126{
127 Camera *camera = reinterpret_cast<Camera *>(id);
128
129 if (camera->custom_filepath[0]) {
131 bpath_data, camera->custom_filepath, sizeof(camera->custom_filepath));
132 }
133}
134
139
141{
142 auto cycles_data_ensure = [](IDProperty *group) {
143 IDProperty *prop = IDP_GetPropertyTypeFromGroup(group, "cycles", IDP_GROUP);
144 if (prop) {
145 return prop;
146 }
147 prop = bke::idprop::create_group("cycles").release();
148 IDP_AddToGroup(group, prop);
149 return prop;
150 };
151
152 auto cycles_property_int_set = [](IDProperty *idprop, const char *name, int value) {
154 IDP_int_set(prop, value);
155 }
156 else {
157 IDP_AddToGroup(idprop, bke::idprop::create(name, value).release());
158 }
159 };
160
161 auto cycles_property_float_set = [](IDProperty *idprop, const char *name, float value) {
163 IDP_float_set(prop, value);
164 }
165 else {
166 IDP_AddToGroup(idprop, bke::idprop::create(name, value).release());
167 }
168 };
169
170 /* For forward compatibility, still write panoramic properties as ID properties for
171 * previous blender versions. */
172 IDProperty *idprop_prev = IDP_ID_system_properties_get(id);
173 /* Make a copy to avoid modifying the original. */
174 IDProperty *idprop_temp = idprop_prev ? IDP_CopyProperty(idprop_prev) :
176
177 Camera *cam = id_cast<Camera *>(id);
178 IDProperty *cycles_cam = cycles_data_ensure(idprop_temp);
179 cycles_property_int_set(cycles_cam, "panorama_type", cam->panorama_type);
180 cycles_property_float_set(cycles_cam, "fisheye_fov", cam->fisheye_fov);
181 cycles_property_float_set(cycles_cam, "fisheye_lens", cam->fisheye_lens);
182 cycles_property_float_set(cycles_cam, "latitude_min", cam->latitude_min);
183 cycles_property_float_set(cycles_cam, "latitude_max", cam->latitude_max);
184 cycles_property_float_set(cycles_cam, "longitude_min", cam->longitude_min);
185 cycles_property_float_set(cycles_cam, "longitude_max", cam->longitude_max);
186 cycles_property_float_set(cycles_cam, "fisheye_polynomial_k0", cam->fisheye_polynomial_k0);
187 cycles_property_float_set(cycles_cam, "fisheye_polynomial_k1", cam->fisheye_polynomial_k1);
188 cycles_property_float_set(cycles_cam, "fisheye_polynomial_k2", cam->fisheye_polynomial_k2);
189 cycles_property_float_set(cycles_cam, "fisheye_polynomial_k3", cam->fisheye_polynomial_k3);
190 cycles_property_float_set(cycles_cam, "fisheye_polynomial_k4", cam->fisheye_polynomial_k4);
191
192 id->system_properties = idprop_temp;
193
194 return {idprop_prev, idprop_temp};
195}
196
199{
200 id->system_properties = data.idprop_prev;
201 data.idprop_prev = nullptr;
202
203 if (data.idprop_temp) {
204 IDP_FreeProperty(data.idprop_temp);
205 data.idprop_temp = nullptr;
206 }
207}
208
209static void camera_blend_write(BlendWriter *writer, ID *id, const void *id_address)
210{
211 const bool is_undo = writer->is_undo();
212 Camera *cam = id_cast<Camera *>(id);
213
215 if (!is_undo) {
217 }
218
219 /* write LibData */
220 writer->write_id_struct(id_address, cam, [](BlendStructWriter<Camera> &struct_writer) {
221 struct_writer.shallow_data.runtime = {};
222 });
223 BKE_id_blend_write(writer, &cam->id);
224
225 for (CameraBGImage &bgpic : cam->bg_images) {
226 writer->write_struct(&bgpic);
227 }
228
229 if (!is_undo) {
231 }
232
233 if (cam->custom_bytecode) {
234 writer->write_string(cam->custom_bytecode);
235 }
236}
237
239{
240 Camera *ca = id_cast<Camera *>(id);
241
242 BLO_read_struct_list(reader, CameraBGImage, &ca->bg_images);
243
244 for (CameraBGImage &bgpic : ca->bg_images) {
245 bgpic.iuser.scene = nullptr;
246
247 /* If linking from a library, clear 'local' library override flag. */
248 if (ID_IS_LINKED(ca)) {
250 }
251 }
252
253 BLO_read_string(reader, &ca->custom_bytecode);
254}
255
257 .id_code = Camera::id_type,
258 .id_filter = FILTER_ID_CA,
259 .dependencies_id_types = FILTER_ID_OB | FILTER_ID_IM,
260 .main_listbase_index = INDEX_ID_CA,
261 .struct_size = sizeof(Camera),
262 .name = "Camera",
263 .name_plural = N_("cameras"),
264 .translation_context = BLT_I18NCONTEXT_ID_CAMERA,
266 .asset_type_info = nullptr,
267
268 .init_data = camera_init_data,
269 .copy_data = camera_copy_data,
270 .free_data = camera_free_data,
271 .make_local = nullptr,
272 .foreach_id = camera_foreach_id,
273 .foreach_cache = nullptr,
274 .foreach_path = camera_foreach_path,
275 .foreach_working_space_color = nullptr,
276 .foreach_asset_weak_reference = nullptr,
277 .owner_pointer_get = nullptr,
278
279 .blend_write = camera_blend_write,
280 .blend_read_data = camera_blend_read_data,
281 .blend_read_after_liblink = nullptr,
282
283 .blend_read_undo_preserve = nullptr,
284
285 .lib_override_apply_post = nullptr,
286};
287
289
290/* -------------------------------------------------------------------- */
293
294Camera *BKE_camera_add(Main *bmain, const char *name)
295{
296 Camera *cam;
297
298 cam = BKE_id_new<Camera>(bmain, name);
299
300 return cam;
301}
302
304{
305 const Camera *cam = id_cast<const Camera *>(ob->data);
306 if (ob->type != OB_CAMERA) {
307 return 0.0f;
308 }
309 if (cam->dof.focus_object) {
310 float view_dir[3], dof_dir[3];
311 normalize_v3_v3(view_dir, ob->object_to_world().ptr()[2]);
312 bPoseChannel *pchan = BKE_pose_channel_find_name(cam->dof.focus_object->pose,
313 cam->dof.focus_subtarget);
314 if (pchan) {
315 float posemat[4][4];
316 mul_m4_m4m4(posemat, cam->dof.focus_object->object_to_world().ptr(), pchan->pose_mat);
317 sub_v3_v3v3(dof_dir, ob->object_to_world().location(), posemat[3]);
318 }
319 else {
320 sub_v3_v3v3(dof_dir,
321 ob->object_to_world().location(),
322 cam->dof.focus_object->object_to_world().location());
323 }
324 return fmax(fabsf(dot_v3v3(view_dir, dof_dir)), 1e-5f);
325 }
326 return fmax(cam->dof.focus_distance, 1e-5f);
327}
328
329float BKE_camera_sensor_size(int sensor_fit, float sensor_x, float sensor_y)
330{
331 /* sensor size used to fit to. for auto, sensor_x is both x and y. */
332 if (sensor_fit == CAMERA_SENSOR_FIT_VERT) {
333 return sensor_y;
334 }
335
336 return sensor_x;
337}
338
339int BKE_camera_sensor_fit(int sensor_fit, float sizex, float sizey)
340{
341 if (sensor_fit == CAMERA_SENSOR_FIT_AUTO) {
342 if (sizex >= sizey) {
344 }
345
347 }
348
349 return sensor_fit;
350}
351
352float2 BKE_camera_frame_size(const float winx, const float winy, const float frame_aspect)
353{
354 const float frame_x = math::min(winx, winy / frame_aspect);
355
356 return float2(frame_x, frame_x * frame_aspect);
357}
358
360
361/* -------------------------------------------------------------------- */
364
366{
367 *params = CameraParams();
368
369 /* defaults */
373
374 params->zoom = 1.0f;
375
376 /* fallback for non camera objects */
377 params->clip_start = 0.1f;
378 params->clip_end = 100.0f;
379}
380
382{
383 if (!cam_ob) {
384 return;
385 }
386
387 if (cam_ob->type == OB_CAMERA) {
388 /* camera object */
389 const Camera *cam = id_cast<const Camera *>(cam_ob->data);
390
391 if (cam->type == CAM_ORTHO) {
392 params->is_ortho = true;
393 }
394 params->lens = cam->lens;
395 params->ortho_scale = cam->ortho_scale;
396
397 params->shiftx = cam->shiftx;
398 params->shifty = cam->shifty;
399
400 params->sensor_x = cam->sensor_x;
401 params->sensor_y = cam->sensor_y;
402 params->sensor_fit = cam->sensor_fit;
403
404 params->clip_start = cam->clip_start;
405 params->clip_end = cam->clip_end;
406 }
407 else if (cam_ob->type == OB_LAMP) {
408 /* light object */
409 Light *la = id_cast<Light *>(cam_ob->data);
410 params->lens = 16.0f / tanf(la->spotsize * 0.5f);
411 if (params->lens == 0.0f) {
412 params->lens = 35.0f;
413 }
414 }
415 else {
416 params->lens = 35.0f;
417 }
418
419 /* Ensure it's possible to compute a valid projection matrix. */
420 params->lens = math::max(params->lens, 1e-9f);
421 params->ortho_scale = math::max(params->ortho_scale, 1e-9f);
422 params->clip_end = math::max(params->clip_end, params->clip_start + 1e-3f);
423}
424
426 const Depsgraph *depsgraph,
427 const View3D *v3d,
428 const RegionView3D *rv3d)
429{
430 /* common */
431 params->lens = v3d->lens;
432 params->clip_start = v3d->clip_start;
433 params->clip_end = v3d->clip_end;
434
435 if (rv3d->persp == RV3D_CAMOB) {
436 /* camera view */
437 const Object *ob_camera_eval = DEG_get_evaluated(depsgraph, v3d->camera);
438 BKE_camera_params_from_object(params, ob_camera_eval);
439
440 /* Override the aspect, so change in viewport framing is minimized when camera parameters
441 * change. */
442 const Scene *scene = DEG_get_evaluated_scene(depsgraph);
443 params->use_aspect_override = true;
444 params->aspect_override = (float(scene->r.ysch) * scene->r.yasp) /
445 (float(scene->r.xsch) * scene->r.xasp);
446
448
449 params->offsetx = 2.0f * rv3d->camdx * params->zoom;
450 params->offsety = 2.0f * rv3d->camdy * params->zoom;
451 params->roll = rv3d->camroll;
452 params->is_flipped_x = (rv3d->rflag & RV3D_FLIP_X) != 0;
453
454 params->shiftx *= params->zoom;
455 params->shifty *= params->zoom;
456
457 params->zoom = CAMERA_PARAM_ZOOM_INIT_CAMOB / params->zoom;
458 }
459 else if (rv3d->persp == RV3D_ORTHO) {
460 /* orthographic view */
461 float sensor_size = BKE_camera_sensor_size(
462 params->sensor_fit, params->sensor_x, params->sensor_y);
463 /* Halve, otherwise too extreme low Z-buffer quality. */
464 params->clip_end *= 0.5f;
465 params->clip_start = -params->clip_end;
466
467 params->is_ortho = true;
468 /* make sure any changes to this match ED_view3d_radius_to_dist_ortho() */
469 params->ortho_scale = rv3d->dist * sensor_size / v3d->lens;
471 }
472 else {
473 /* perspective view */
475 }
476
477 /* Ensure it's possible to compute a valid projection matrix. */
478 params->lens = math::max(params->lens, 1e-9f);
479 params->ortho_scale = math::max(params->ortho_scale, 1e-9f);
480 params->clip_end = math::max(params->clip_end, params->clip_start + 1e-3f);
481}
482
484 CameraParams *params, int winx, int winy, float aspx, float aspy)
485{
486 rctf viewplane;
487 float pixsize, viewfac, sensor_size;
488 int sensor_fit;
489
490 params->ycor = aspy / aspx;
491
492 const float winy_cor = params->ycor * float(winy);
493 const float frame_aspect = params->use_aspect_override ? params->aspect_override :
494 winy_cor / float(winx);
495 const float2 frame_size = BKE_camera_frame_size(float(winx), winy_cor, frame_aspect);
496
497 if (params->is_ortho) {
498 /* orthographic camera */
499 /* scale == 1.0 means exact 1 to 1 mapping */
500 pixsize = params->ortho_scale;
501 }
502 else {
503 /* perspective camera */
504 sensor_size = BKE_camera_sensor_size(params->sensor_fit, params->sensor_x, params->sensor_y);
505 pixsize = (sensor_size * params->clip_start) / params->lens;
506 }
507
508 /* determine sensor fit */
509 sensor_fit = BKE_camera_sensor_fit(params->sensor_fit, 1.0f, frame_aspect);
510
511 viewfac = (sensor_fit == CAMERA_SENSOR_FIT_HOR) ? frame_size.x : frame_size.y;
512
513 pixsize /= viewfac;
514
515 /* extra zoom factor */
516 pixsize *= params->zoom;
517
518 /* compute view plane:
519 * Fully centered, Z-buffer fills in jittered between `-.5` and `+.5`. */
520 viewplane.xmin = -0.5f * float(winx);
521 viewplane.ymin = -0.5f * params->ycor * float(winy);
522 viewplane.xmax = 0.5f * float(winx);
523 viewplane.ymax = 0.5f * params->ycor * float(winy);
524
525 /* Lens shift and offset. */
526 float2 dxy(params->shiftx * viewfac + winx * params->offsetx,
527 params->shifty * viewfac + winy * params->offsety);
528 dxy = BKE_camera_viewplane_offset_transform(params->roll, params->is_flipped_x, dxy);
529 BLI_rctf_translate(&viewplane, dxy.x, dxy.y);
530
531 /* the window matrix is used for clipping, and not changed during OSA steps */
532 /* using an offset of +0.5 here would give clip errors on edges */
533 BLI_rctf_mul(&viewplane, pixsize);
534
535 /* Used for rendering (offset by near-clip with perspective views), passed to RE_SetPixelSize.
536 * For viewport drawing 'RegionView3D.pixsize'. */
537 params->viewdx = pixsize;
538 params->viewdy = params->ycor * pixsize;
539 params->viewplane = viewplane;
540}
541
543 const bool is_flipped_x,
544 const float2 offset)
545{
547
548 if (is_flipped_x) {
549 result.x = -result.x;
550 }
551
552 return result;
553}
554
555void BKE_camera_params_crop_viewplane(rctf *viewplane, int winx, int winy, const rcti *region)
556{
557 float pix_size_x = BLI_rctf_size_x(viewplane) / winx;
558 float pix_size_y = BLI_rctf_size_y(viewplane) / winy;
559
560 viewplane->xmin += pix_size_x * region->xmin;
561 viewplane->ymin += pix_size_y * region->ymin;
562
563 viewplane->xmax = viewplane->xmin + pix_size_x * BLI_rcti_size_x(region);
564 viewplane->ymax = viewplane->ymin + pix_size_y * BLI_rcti_size_y(region);
565}
566
568{
569 rctf viewplane = params->viewplane;
570
571 /* compute projection matrix */
572 if (params->is_ortho) {
573 orthographic_m4(params->winmat,
574 viewplane.xmin,
575 viewplane.xmax,
576 viewplane.ymin,
577 viewplane.ymax,
578 params->clip_start,
579 params->clip_end);
580 }
581 else {
582 perspective_m4(params->winmat,
583 viewplane.xmin,
584 viewplane.xmax,
585 viewplane.ymin,
586 viewplane.ymax,
587 params->clip_start,
588 params->clip_end);
589 }
590}
591
593
594/* -------------------------------------------------------------------- */
597
599 const Depsgraph *depsgraph,
600 const View3D *v3d,
601 const RegionView3D *rv3d,
602 const int winx,
603 const int winy,
604 const bool no_shift,
605 const bool no_zoom,
606 const bool no_roll)
607{
608 CameraParams params;
609 rctf rect_view, rect_camera;
610 const Object *camera_eval = DEG_get_evaluated(depsgraph, v3d->camera);
611
612 /* Get viewport view plane. */
613 BKE_camera_params_init(&params);
614 BKE_camera_params_from_view3d(&params, depsgraph, v3d, rv3d);
615 if (no_zoom) {
616 params.zoom = 1.0f;
617 }
618 if (no_roll) {
619 params.roll = 0.0f;
620 params.is_flipped_x = false;
621 }
622 BKE_camera_params_compute_viewplane(&params, winx, winy, 1.0f, 1.0f);
623 rect_view = params.viewplane;
624
625 /* Get camera view plane. */
626 BKE_camera_params_init(&params);
627 params.clip_start = v3d->clip_start;
628 params.clip_end = v3d->clip_end;
629 BKE_camera_params_from_object(&params, camera_eval);
630 if (no_shift) {
631 params.shiftx = 0.0f;
632 params.shifty = 0.0f;
633 }
635 &params, scene->r.xsch, scene->r.ysch, scene->r.xasp, scene->r.yasp);
636 rect_camera = params.viewplane;
637
638 /* Get camera border within viewport. */
639 rctf border;
640 border.xmin = ((rect_camera.xmin - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) * winx;
641 border.xmax = ((rect_camera.xmax - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) * winx;
642 border.ymin = ((rect_camera.ymin - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) * winy;
643 border.ymax = ((rect_camera.ymax - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) * winy;
644
645 return border;
646}
647
648/* Test if the camera frame is surrounded by a fully opaque passepartout, hiding
649 * everything outside of it. */
651{
652 if ((v3d->flag2 & V3D_SHOW_CAMERA_PASSEPARTOUT) == 0) {
653 return false;
654 }
655 if (v3d->camera->type != OB_CAMERA) {
656 return false;
657 }
658
659 const Camera *camera = id_cast<const Camera *>(v3d->camera->data);
660 return (camera->flag & CAM_SHOWPASSEPARTOUT) && camera->passepartalpha == 1.0f;
661}
662
664 const Depsgraph *depsgraph,
665 const View3D *v3d,
666 const RegionView3D *rv3d,
667 const int winx,
668 const int winy,
669 rctf *r_border,
670 rctf *r_unrolled_border)
671{
672 const bool is_camera_view = (rv3d->persp == RV3D_CAMOB) && (v3d->camera != nullptr);
673 rctf border;
674
675 if (is_camera_view) {
676 const bool use_camera_frame = camera_passepartout_is_opaque(v3d);
677
678 if ((scene->r.mode & R_BORDER) == 0 && !use_camera_frame) {
679 return false;
680 }
681
682 /* The border is relative to the camera frame, which is not rolled. */
683 const rctf viewborder = BKE_camera_view_border(
684 scene, depsgraph, v3d, rv3d, winx, winy, false, false, true);
685
686 if (scene->r.mode & R_BORDER) {
687 border.xmin = viewborder.xmin + scene->r.border.xmin * BLI_rctf_size_x(&viewborder);
688 border.ymin = viewborder.ymin + scene->r.border.ymin * BLI_rctf_size_y(&viewborder);
689 border.xmax = viewborder.xmin + scene->r.border.xmax * BLI_rctf_size_x(&viewborder);
690 border.ymax = viewborder.ymin + scene->r.border.ymax * BLI_rctf_size_y(&viewborder);
691 }
692 else {
693 border = viewborder;
694 }
695
696 if ((rv3d->rflag & RV3D_FLIP_X) != 0) {
697 std::swap(border.xmin, border.xmax);
698 border.xmin = winx - border.xmin;
699 border.xmax = winx - border.xmax;
700 }
701 }
702 else {
703 if ((v3d->flag2 & V3D_RENDER_BORDER) == 0) {
704 return false;
705 }
706
707 border.xmin = v3d->render_border.xmin * winx;
708 border.ymin = v3d->render_border.ymin * winy;
709 border.xmax = v3d->render_border.xmax * winx;
710 border.ymax = v3d->render_border.ymax * winy;
711 }
712
713 if (r_unrolled_border) {
714 *r_unrolled_border = border;
715 }
716
717 /* The camera view roll rotates the border around the center of the viewport. */
718 if (is_camera_view && rv3d->camroll != 0.0f) {
719 const float pivot[2] = {winx / 2.0f, winy / 2.0f};
720 if ((rv3d->rflag & RV3D_FLIP_X) != 0) {
721 BLI_rctf_rotate_expand_around(&border, &border, pivot, -rv3d->camroll);
722 }
723 else {
724 BLI_rctf_rotate_expand_around(&border, &border, pivot, rv3d->camroll);
725 }
726 }
727
728 *r_border = border;
729 return true;
730}
731
733
734/* -------------------------------------------------------------------- */
737
739 const Camera *camera,
740 const float drawsize,
741 const bool do_clip,
742 const float scale[3],
743 float r_asp[2],
744 float r_shift[2],
745 float *r_drawsize,
746 float r_vec[4][3])
747{
748 float facx, facy;
749 float depth;
750
751 /* aspect correction */
752 if (scene) {
753 float aspx = float(scene->r.xsch) * scene->r.xasp;
754 float aspy = float(scene->r.ysch) * scene->r.yasp;
755 int sensor_fit = BKE_camera_sensor_fit(camera->sensor_fit, aspx, aspy);
756
757 if (sensor_fit == CAMERA_SENSOR_FIT_HOR) {
758 r_asp[0] = 1.0;
759 r_asp[1] = aspy / aspx;
760 }
761 else {
762 r_asp[0] = aspx / aspy;
763 r_asp[1] = 1.0;
764 }
765 }
766 else {
767 r_asp[0] = 1.0f;
768 r_asp[1] = 1.0f;
769 }
770
771 if (camera->type == CAM_ORTHO) {
772 facx = 0.5f * camera->ortho_scale * r_asp[0] * scale[0];
773 facy = 0.5f * camera->ortho_scale * r_asp[1] * scale[1];
774 r_shift[0] = camera->shiftx * camera->ortho_scale * scale[0];
775 r_shift[1] = camera->shifty * camera->ortho_scale * scale[1];
776 depth = -drawsize * scale[2];
777
778 *r_drawsize = 0.5f * camera->ortho_scale;
779 }
780 else {
781 /* that way it's always visible - clip_start+0.1 */
782 float fac, scale_x, scale_y;
783 float half_sensor = 0.5f * ((camera->sensor_fit == CAMERA_SENSOR_FIT_VERT) ?
784 (camera->sensor_y) :
785 (camera->sensor_x));
786
787 /* fixed size, variable depth (stays a reasonable size in the 3D view) */
788 *r_drawsize = (drawsize / 2.0f) / ((scale[0] + scale[1] + scale[2]) / 3.0f);
789 depth = *r_drawsize * camera->lens / (-half_sensor) * scale[2];
790 fac = *r_drawsize;
791 scale_x = scale[0];
792 scale_y = scale[1];
793
794 facx = fac * r_asp[0] * scale_x;
795 facy = fac * r_asp[1] * scale_y;
796 r_shift[0] = camera->shiftx * fac * 2.0f * scale_x;
797 r_shift[1] = camera->shifty * fac * 2.0f * scale_y;
798 }
799
800 r_vec[0][0] = r_shift[0] + facx;
801 r_vec[0][1] = r_shift[1] + facy;
802 r_vec[0][2] = depth;
803 r_vec[1][0] = r_shift[0] + facx;
804 r_vec[1][1] = r_shift[1] - facy;
805 r_vec[1][2] = depth;
806 r_vec[2][0] = r_shift[0] - facx;
807 r_vec[2][1] = r_shift[1] - facy;
808 r_vec[2][2] = depth;
809 r_vec[3][0] = r_shift[0] - facx;
810 r_vec[3][1] = r_shift[1] + facy;
811 r_vec[3][2] = depth;
812
813 if (do_clip) {
814 /* Ensure the frame isn't behind the near clipping plane, #62814. */
815 float fac = ((camera->clip_start + 0.1f) / -r_vec[0][2]) * scale[2];
816 for (uint i = 0; i < 4; i++) {
817 if (camera->type == CAM_ORTHO) {
818 r_vec[i][2] *= fac;
819 }
820 else {
821 mul_v3_fl(r_vec[i], fac);
822 }
823 }
824 }
825}
826
827void BKE_camera_view_frame(const Scene *scene, const Camera *camera, float r_vec[4][3])
828{
829 float dummy_asp[2];
830 float dummy_shift[2];
831 float dummy_drawsize;
832 const float dummy_scale[3] = {1.0f, 1.0f, 1.0f};
833
835 scene, camera, 1.0, false, dummy_scale, dummy_asp, dummy_shift, &dummy_drawsize, r_vec);
836}
837
839
840/* -------------------------------------------------------------------- */
843
844#define CAMERA_VIEWFRAME_NUM_PLANES 4
845
846#define Y_MIN 0
847#define Y_MAX 1
848#define Z_MIN 2
849#define Z_MAX 3
850
852 float plane_tx[CAMERA_VIEWFRAME_NUM_PLANES][4]; /* 4 planes normalized */
853 float dist_vals[CAMERA_VIEWFRAME_NUM_PLANES]; /* distance (signed) */
854 float camera_no[3];
855 float z_range[2];
857
859
860 /* Not used by callbacks... */
861 float camera_rotmat[3][3];
862};
863
864static void camera_to_frame_view_cb(const float co[3], void *user_data)
865{
866 CameraViewFrameData *data = static_cast<CameraViewFrameData *>(user_data);
867
868 for (uint i = 0; i < CAMERA_VIEWFRAME_NUM_PLANES; i++) {
869 const float nd = plane_point_side_v3(data->plane_tx[i], co);
870 CLAMP_MAX(data->dist_vals[i], nd);
871 }
872
873 if (data->do_zrange) {
874 const float d = dot_v3v3(data->camera_no, co);
875 CLAMP_MAX(data->z_range[0], d);
876 CLAMP_MIN(data->z_range[1], d);
877 }
878
879 data->tot++;
880}
881
882static void camera_frame_fit_data_init(const Scene *scene,
883 const Object *ob,
884 const bool do_clip_dists,
885 CameraParams *params,
887{
888 float camera_rotmat_transposed_inversed[4][4];
889
890 /* setup parameters */
893
894 /* Compute matrix, view-plane, etc. */
895 if (scene) {
897 params, scene->r.xsch, scene->r.ysch, scene->r.xasp, scene->r.yasp);
898 }
899 else {
900 BKE_camera_params_compute_viewplane(params, 1, 1, 1.0f, 1.0f);
901 }
903
904 /* initialize callback data */
905 copy_m3_m4(data->camera_rotmat, const_cast<float (*)[4]>(ob->object_to_world().ptr()));
906 normalize_m3(data->camera_rotmat);
907 /* To transform a plane which is in its homogeneous representation (4d vector),
908 * we need the inverse of the transpose of the transform matrix... */
909 copy_m4_m3(camera_rotmat_transposed_inversed, data->camera_rotmat);
910 transpose_m4(camera_rotmat_transposed_inversed);
911 invert_m4(camera_rotmat_transposed_inversed);
912
913 /* Extract frustum planes from projection matrix. */
915 data->plane_tx[Y_MIN],
916 data->plane_tx[Y_MAX],
917 data->plane_tx[Z_MIN],
918 data->plane_tx[Z_MAX],
919 nullptr,
920 nullptr);
921
922 /* Rotate planes and get normals from them */
923 for (uint i = 0; i < CAMERA_VIEWFRAME_NUM_PLANES; i++) {
924 mul_m4_v4(camera_rotmat_transposed_inversed, data->plane_tx[i]);
925 /* Normalize. */
926 data->plane_tx[i][3] /= normalize_v3(data->plane_tx[i]);
927
928 data->dist_vals[i] = FLT_MAX;
929 }
930
931 data->tot = 0;
932 data->do_zrange = params->is_ortho || do_clip_dists;
933
934 if (data->do_zrange) {
935 /* We want (0, 0, -1) transformed by camera_rotmat, this is a quicker shortcut. */
936 negate_v3_v3(data->camera_no, data->camera_rotmat[2]);
937 data->z_range[0] = FLT_MAX;
938 data->z_range[1] = -FLT_MAX;
939 }
940}
941
944 float r_co[3],
945 float *r_scale,
946 float *r_clip_start,
947 float *r_clip_end)
948{
949 float plane_tx[CAMERA_VIEWFRAME_NUM_PLANES][4];
950
951 if (data->tot <= 1) {
952 return false;
953 }
954
955 if (params->is_ortho) {
956 const float *cam_axis_x = data->camera_rotmat[0];
957 const float *cam_axis_y = data->camera_rotmat[1];
958 const float *cam_axis_z = data->camera_rotmat[2];
959 const float *dists = data->dist_vals;
960 const float dist_span_y = dists[Y_MIN] + dists[Y_MAX];
961 const float dist_span_z = dists[Z_MIN] + dists[Z_MAX];
962 const float dist_mid_y = (dists[Y_MIN] - dists[Y_MAX]) * 0.5f;
963 const float dist_mid_z = (dists[Z_MIN] - dists[Z_MAX]) * 0.5f;
964 const float scale_diff = (dist_span_z < dist_span_y) ?
965 (dist_span_z * (BLI_rctf_size_x(&params->viewplane) /
966 BLI_rctf_size_y(&params->viewplane))) :
967 (dist_span_y * (BLI_rctf_size_y(&params->viewplane) /
968 BLI_rctf_size_x(&params->viewplane)));
969
970 *r_scale = params->ortho_scale - scale_diff;
971
972 zero_v3(r_co);
973 madd_v3_v3fl(r_co, cam_axis_x, dist_mid_y + (params->shiftx * scale_diff));
974 madd_v3_v3fl(r_co, cam_axis_y, dist_mid_z + (params->shifty * scale_diff));
975 madd_v3_v3fl(r_co, cam_axis_z, -(data->z_range[0] - 1.0f - params->clip_start));
976 }
977 else {
978 float plane_isect_1[3], plane_isect_1_no[3], plane_isect_1_other[3];
979 float plane_isect_2[3], plane_isect_2_no[3], plane_isect_2_other[3];
980
981 float plane_isect_pt_1[3], plane_isect_pt_2[3];
982
983 /* apply the dist-from-plane's to the transformed plane points */
984 for (int i = 0; i < CAMERA_VIEWFRAME_NUM_PLANES; i++) {
985 float co[3];
986 mul_v3_v3fl(co, data->plane_tx[i], data->dist_vals[i]);
987 plane_from_point_normal_v3(plane_tx[i], co, data->plane_tx[i]);
988 }
989
990 if (!isect_plane_plane_v3(plane_tx[Y_MIN], plane_tx[Y_MAX], plane_isect_1, plane_isect_1_no) ||
991 !isect_plane_plane_v3(plane_tx[Z_MIN], plane_tx[Z_MAX], plane_isect_2, plane_isect_2_no))
992 {
993 return false;
994 }
995
996 add_v3_v3v3(plane_isect_1_other, plane_isect_1, plane_isect_1_no);
997 add_v3_v3v3(plane_isect_2_other, plane_isect_2, plane_isect_2_no);
998
999 if (!isect_line_line_v3(plane_isect_1,
1000 plane_isect_1_other,
1001 plane_isect_2,
1002 plane_isect_2_other,
1003 plane_isect_pt_1,
1004 plane_isect_pt_2))
1005 {
1006 return false;
1007 }
1008
1009 float cam_plane_no[3];
1010 float plane_isect_delta[3];
1011
1012 const float shift_fac = BKE_camera_sensor_size(
1013 params->sensor_fit, params->sensor_x, params->sensor_y) /
1014 params->lens;
1015
1016 /* we want (0, 0, -1) transformed by camera_rotmat, this is a quicker shortcut. */
1017 negate_v3_v3(cam_plane_no, data->camera_rotmat[2]);
1018
1019 sub_v3_v3v3(plane_isect_delta, plane_isect_pt_2, plane_isect_pt_1);
1020 const float plane_isect_delta_len = len_v3(plane_isect_delta);
1021
1022 if (dot_v3v3(plane_isect_delta, cam_plane_no) > 0.0f) {
1023 copy_v3_v3(r_co, plane_isect_pt_1);
1024
1025 /* offset shift */
1026 normalize_v3(plane_isect_1_no);
1027 madd_v3_v3fl(r_co, plane_isect_1_no, params->shifty * plane_isect_delta_len * shift_fac);
1028 }
1029 else {
1030 copy_v3_v3(r_co, plane_isect_pt_2);
1031
1032 /* offset shift */
1033 normalize_v3(plane_isect_2_no);
1034 madd_v3_v3fl(r_co, plane_isect_2_no, params->shiftx * plane_isect_delta_len * shift_fac);
1035 }
1036 }
1037
1038 if (r_clip_start && r_clip_end) {
1039 const float z_offs = dot_v3v3(r_co, data->camera_no);
1040 *r_clip_start = data->z_range[0] - z_offs;
1041 *r_clip_end = data->z_range[1] - z_offs;
1042 }
1043 return true;
1044}
1045
1046#undef Y_MIN
1047#undef Y_MAX
1048#undef Z_MIN
1049#undef Z_MAX
1050
1051bool BKE_camera_view_frame_fit_to_scene(Depsgraph *depsgraph,
1052 const Scene *scene,
1053 Object *camera_ob,
1054 float r_co[3],
1055 float *r_scale,
1056 float *r_clip_start,
1057 float *r_clip_end)
1058{
1059 CameraParams params;
1060 CameraViewFrameData data_cb;
1061
1062 /* just in case */
1063 *r_scale = 1.0f;
1064
1065 camera_frame_fit_data_init(scene, camera_ob, r_clip_start && r_clip_end, &params, &data_cb);
1066
1067 /* run callback on all visible points */
1069
1071 &params, &data_cb, r_co, r_scale, r_clip_start, r_clip_end);
1072}
1073
1074bool BKE_camera_view_frame_fit_to_coords(const Depsgraph *depsgraph,
1075 const float (*cos)[3],
1076 int num_cos,
1077 Object *camera_ob,
1078 float r_co[3],
1079 float *r_scale)
1080{
1081 Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
1082 Object *camera_ob_eval = DEG_get_evaluated(depsgraph, camera_ob);
1083 CameraParams params;
1084 CameraViewFrameData data_cb;
1085
1086 /* just in case */
1087 *r_scale = 1.0f;
1088
1089 camera_frame_fit_data_init(scene_eval, camera_ob_eval, false, &params, &data_cb);
1090
1091 /* run callback on all given coordinates */
1092 while (num_cos--) {
1093 camera_to_frame_view_cb(cos[num_cos], &data_cb);
1094 }
1095
1096 return camera_frame_fit_calc_from_data(&params, &data_cb, r_co, r_scale, nullptr, nullptr);
1097}
1098
1100
1101/* -------------------------------------------------------------------- */
1104
1105static void camera_model_matrix(const Object *camera, float r_modelmat[4][4])
1106{
1107 copy_m4_m4(r_modelmat, camera->object_to_world().ptr());
1108}
1109
1111 const bool is_left,
1112 float r_modelmat[4][4])
1113{
1114 const Camera *data = id_cast<const Camera *>(camera->data);
1115 float interocular_distance, convergence_distance;
1116 short convergence_mode, pivot;
1117 float sizemat[4][4];
1118
1119 float fac = 1.0f;
1120 float fac_signed;
1121
1122 interocular_distance = data->stereo.interocular_distance;
1123 convergence_distance = data->stereo.convergence_distance;
1124 convergence_mode = data->stereo.convergence_mode;
1125 pivot = data->stereo.pivot;
1126
1127 if (((pivot == CAM_S3D_PIVOT_LEFT) && is_left) || ((pivot == CAM_S3D_PIVOT_RIGHT) && !is_left)) {
1128 camera_model_matrix(camera, r_modelmat);
1129 return;
1130 }
1131
1132 float size[3];
1133 mat4_to_size(size, camera->object_to_world().ptr());
1134 size_to_mat4(sizemat, size);
1135
1136 if (pivot == CAM_S3D_PIVOT_CENTER) {
1137 fac = 0.5f;
1138 }
1139
1140 fac_signed = is_left ? fac : -fac;
1141
1142 /* rotation */
1143 if (convergence_mode == CAM_S3D_TOE) {
1144 float angle;
1145 float angle_sin, angle_cos;
1146 float toeinmat[4][4];
1147 float rotmat[4][4];
1148
1149 unit_m4(rotmat);
1150
1151 if (pivot == CAM_S3D_PIVOT_CENTER) {
1152 fac = -fac;
1153 fac_signed = -fac_signed;
1154 }
1155
1156 angle = atanf((interocular_distance * 0.5f) / convergence_distance) / fac;
1157
1158 angle_cos = cosf(angle * fac_signed);
1159 angle_sin = sinf(angle * fac_signed);
1160
1161 rotmat[0][0] = angle_cos;
1162 rotmat[2][0] = -angle_sin;
1163 rotmat[0][2] = angle_sin;
1164 rotmat[2][2] = angle_cos;
1165
1166 if (pivot == CAM_S3D_PIVOT_CENTER) {
1167 /* set the rotation */
1168 copy_m4_m4(toeinmat, rotmat);
1169 /* set the translation */
1170 toeinmat[3][0] = interocular_distance * fac_signed;
1171
1172 /* transform */
1173 normalize_m4_m4(r_modelmat, camera->object_to_world().ptr());
1174 mul_m4_m4m4(r_modelmat, r_modelmat, toeinmat);
1175
1176 /* scale back to the original size */
1177 mul_m4_m4m4(r_modelmat, r_modelmat, sizemat);
1178 }
1179 else { /* CAM_S3D_PIVOT_LEFT, CAM_S3D_PIVOT_RIGHT */
1180 /* rotate perpendicular to the interocular line */
1181 normalize_m4_m4(r_modelmat, camera->object_to_world().ptr());
1182 mul_m4_m4m4(r_modelmat, r_modelmat, rotmat);
1183
1184 /* translate along the interocular line */
1185 unit_m4(toeinmat);
1186 toeinmat[3][0] = -interocular_distance * fac_signed;
1187 mul_m4_m4m4(r_modelmat, r_modelmat, toeinmat);
1188
1189 /* rotate to toe-in angle */
1190 mul_m4_m4m4(r_modelmat, r_modelmat, rotmat);
1191
1192 /* scale back to the original size */
1193 mul_m4_m4m4(r_modelmat, r_modelmat, sizemat);
1194 }
1195 }
1196 else {
1197 normalize_m4_m4(r_modelmat, camera->object_to_world().ptr());
1198
1199 /* translate - no rotation in CAM_S3D_OFFAXIS, CAM_S3D_PARALLEL */
1200 translate_m4(r_modelmat, -interocular_distance * fac_signed, 0.0f, 0.0f);
1201
1202 /* scale back to the original size */
1203 mul_m4_m4m4(r_modelmat, r_modelmat, sizemat);
1204 }
1205}
1206
1208 const Object *camera,
1209 const bool is_left,
1210 float r_viewmat[4][4])
1211{
1213 rd, camera, is_left ? STEREO_LEFT_NAME : STEREO_RIGHT_NAME, r_viewmat);
1214 invert_m4(r_viewmat);
1215}
1216
1217/* left is the default */
1218static bool camera_is_left(const char *viewname)
1219{
1220 if (viewname && viewname[0] != '\0') {
1221 return !STREQ(viewname, STEREO_RIGHT_NAME);
1222 }
1223 return true;
1224}
1225
1227 const Object *camera,
1228 const char *viewname,
1229 float r_modelmat[4][4])
1230{
1231 BKE_camera_multiview_model_matrix_scaled(rd, camera, viewname, r_modelmat);
1232 normalize_m4(r_modelmat);
1233}
1234
1236 const Object *camera,
1237 const char *viewname,
1238 float r_modelmat[4][4])
1239{
1240 const bool is_multiview = (rd && rd->scemode & R_MULTIVIEW) != 0;
1241
1242 if (!is_multiview) {
1243 camera_model_matrix(camera, r_modelmat);
1244 }
1245 else if (rd->views_format == SCE_VIEWS_FORMAT_MULTIVIEW) {
1246 camera_model_matrix(camera, r_modelmat);
1247 }
1248 else { /* SCE_VIEWS_SETUP_BASIC */
1249 const bool is_left = camera_is_left(viewname);
1250 camera_stereo3d_model_matrix(camera, is_left, r_modelmat);
1251 }
1252}
1253
1255 const Object *camera,
1256 const char *viewname,
1257 float r_winmat[4][4])
1258{
1259 CameraParams params;
1260
1261 /* Setup parameters */
1262 BKE_camera_params_init(&params);
1264 BKE_camera_multiview_params(rd, &params, camera, viewname);
1265
1266 /* Compute matrix, view-plane, etc. */
1267 BKE_camera_params_compute_viewplane(&params, rd->xsch, rd->ysch, rd->xasp, rd->yasp);
1269
1270 copy_m4_m4(r_winmat, params.winmat);
1271}
1272
1274{
1275 const bool is_multiview = (rd && rd->scemode & R_MULTIVIEW) != 0;
1276
1277 if (!is_multiview) {
1278 return false;
1279 }
1280
1281 if (camera->type != OB_CAMERA) {
1282 return false;
1283 }
1284
1285 const Camera *cam = id_cast<const Camera *>(camera->data);
1286
1288 ELEM(cam->type, CAM_PANO, CAM_PERSP, CAM_CUSTOM) &&
1289 ((cam->stereo.flag & CAM_S3D_SPHERICAL) != 0))
1290 {
1291 return true;
1292 }
1293
1294 return false;
1295}
1296
1298 const Scene *scene,
1299 Object *camera,
1300 const char *suffix)
1301{
1302 char name[MAX_NAME];
1303 const char *camera_name = camera->id.name + 2;
1304 const int len_name = strlen(camera_name);
1305 int len_suffix_max = -1;
1306
1307 name[0] = '\0';
1308
1309 /* we need to take the better match, thus the len_suffix_max test */
1310 for (const SceneRenderView &srv : scene->r.views) {
1311 const int len_suffix = strlen(srv.suffix);
1312
1313 if ((len_suffix < len_suffix_max) || (len_name < len_suffix)) {
1314 continue;
1315 }
1316
1317 if (STREQ(camera_name + (len_name - len_suffix), srv.suffix)) {
1318 SNPRINTF(name, "%.*s%s", (len_name - len_suffix), camera_name, suffix);
1319 len_suffix_max = len_suffix;
1320 }
1321 }
1322
1323 if (name[0] != '\0') {
1324 Object *ob = BKE_scene_object_find_by_name(bmain, scene, name);
1325 if (ob != nullptr) {
1326 return ob;
1327 }
1328 }
1329
1330 return camera;
1331}
1332
1334 const Scene *scene,
1335 Object *camera,
1336 const char *viewname)
1337{
1338 const bool is_multiview = (camera != nullptr) && (scene->r.scemode & R_MULTIVIEW) != 0;
1339
1340 if (!is_multiview) {
1341 return camera;
1342 }
1344 return camera;
1345 }
1346 /* SCE_VIEWS_FORMAT_MULTIVIEW */
1347 const char *suffix = BKE_scene_multiview_view_suffix_get(&scene->r, viewname);
1348 return camera_multiview_advanced(bmain, scene, camera, suffix);
1349}
1350
1351static float camera_stereo3d_shift_x(const Object *camera, const char *viewname)
1352{
1353 const Camera *data = id_cast<const Camera *>(camera->data);
1354 float shift = data->shiftx;
1355 float interocular_distance, convergence_distance;
1356 short convergence_mode, pivot;
1357 bool is_left = true;
1358
1359 float fac = 1.0f;
1360 float fac_signed;
1361
1362 if (viewname && viewname[0]) {
1363 is_left = STREQ(viewname, STEREO_LEFT_NAME);
1364 }
1365
1366 interocular_distance = data->stereo.interocular_distance;
1367 convergence_distance = data->stereo.convergence_distance;
1368 convergence_mode = data->stereo.convergence_mode;
1369 pivot = data->stereo.pivot;
1370
1371 if (convergence_mode != CAM_S3D_OFFAXIS) {
1372 return shift;
1373 }
1374
1375 if (((pivot == CAM_S3D_PIVOT_LEFT) && is_left) || ((pivot == CAM_S3D_PIVOT_RIGHT) && !is_left)) {
1376 return shift;
1377 }
1378
1379 if (pivot == CAM_S3D_PIVOT_CENTER) {
1380 fac = 0.5f;
1381 }
1382
1383 fac_signed = is_left ? fac : -fac;
1384 shift += ((interocular_distance / data->sensor_x) * (data->lens / convergence_distance)) *
1385 fac_signed;
1386
1387 return shift;
1388}
1389
1391 const Object *camera,
1392 const char *viewname)
1393{
1394 const bool is_multiview = (rd && rd->scemode & R_MULTIVIEW) != 0;
1395 const Camera *data = id_cast<const Camera *>(camera->data);
1396
1397 BLI_assert(camera->type == OB_CAMERA);
1398
1399 if (!is_multiview) {
1400 return data->shiftx;
1401 }
1403 return data->shiftx;
1404 }
1405 if (data->type == CAM_PANO) {
1406 return data->shiftx;
1407 }
1408 /* SCE_VIEWS_SETUP_BASIC */
1409 return camera_stereo3d_shift_x(camera, viewname);
1410}
1411
1413 CameraParams *params,
1414 const Object *camera,
1415 const char *viewname)
1416{
1417 if (camera->type == OB_CAMERA) {
1418 params->shiftx = BKE_camera_multiview_shift_x(rd, camera, viewname);
1419 }
1420}
1421
1423
1424/* -------------------------------------------------------------------- */
1427
1429{
1430 CameraBGImage *bgpic = MEM_new<CameraBGImage>("Background Image");
1431
1432 bgpic->scale = 1.0f;
1433 bgpic->alpha = 0.5f;
1434 bgpic->iuser.flag |= IMA_ANIM_ALWAYS;
1437
1438 BLI_addtail(&cam->bg_images, bgpic);
1439
1440 return bgpic;
1441}
1442
1444{
1445 CameraBGImage *bgpic_dst = MEM_dupalloc(bgpic_src);
1446
1447 bgpic_dst->next = bgpic_dst->prev = nullptr;
1448
1449 if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
1450 id_us_plus(id_cast<ID *>(bgpic_dst->ima));
1451 id_us_plus(id_cast<ID *>(bgpic_dst->clip));
1452 }
1453
1456 }
1457
1458 return bgpic_dst;
1459}
1460
1462{
1463 BLI_remlink(&cam->bg_images, bgpic);
1464
1465 MEM_delete(bgpic);
1466}
1467
1469{
1470 CameraBGImage *bgpic = cam->bg_images.first();
1471
1472 while (bgpic) {
1473 CameraBGImage *next_bgpic = bgpic->next;
1474
1476
1477 bgpic = next_bgpic;
1478 }
1479}
1480
1482
1483} // namespace blender
Blender kernel action and pose functionality.
Camera data-block and utility functions.
#define CAMERA_PARAM_ZOOM_INIT_PERSP
Definition BKE_camera.h:100
#define CAMERA_PARAM_ZOOM_INIT_CAMOB
Definition BKE_camera.h:99
#define IDP_int_set(prop, value)
#define IDP_float_set(prop, value)
#define BKE_LIB_FOREACHID_PROCESS_IDSUPER(data_, id_super_, cb_flag_)
General operations, lookup, etc. for blender objects.
#define SNPRINTF(dst, format,...)
#define BLO_read_struct_list(reader, struct_name, list)
#define BLT_I18NCONTEXT_ID_CAMERA
ID and Library types, which are fundamental for SDNA.
#define FILTER_ID_OB
Definition DNA_ID.h:1216
#define FILTER_ID_CA
Definition DNA_ID.h:1202
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define FILTER_ID_IM
Definition DNA_ID.h:1206
#define DEFAULT_SENSOR_HEIGHT
#define DEFAULT_SENSOR_WIDTH
#define MAX_NAME
Definition DNA_defs.h:63
Object is a sort of wrapper for general info.
#define STEREO_LEFT_NAME
#define STEREO_RIGHT_NAME
Read Blender memory allocation and freeing.
#define Z_MAX
#define CAMERA_VIEWFRAME_NUM_PLANES
#define Y_MIN
#define Y_MAX
#define Z_MIN
#define CLAMP_MAX(a, c)
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define ELEM(...)
#define STREQ(a, b)
#define CLAMP_MIN(a, b)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
#define FLT_MAX
#define cos
void camera(KernelGlobals &kg, const ShadingData &sd, float3 &outview, float &outdepth, float &outdist)
float3x3 scale(float3x3 mat, float2 scale)
std::unique_ptr< IDProperty, IDPropertyDeleter > create(StringRef prop_name, int32_t value, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_INT, set its name and value.
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
AngleRadianBase< float > AngleRadian
T min(const T &a, const T &b)
T max(const T &a, const T &b)
MatT from_rotation(const RotationT &rotation)
BMesh Iterators.
struct CameraBGImage * BKE_camera_background_image_copy(const struct CameraBGImage *bgpic_src, int flag)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE void negate_v3_v3(float r[3], const float a[3])
bool isect_plane_plane_v3(const float plane_a[4], const float plane_b[4], float r_isect_co[3], float r_isect_no[3]) ATTR_WARN_UNUSED_RESULT
static void camera_foreach_path(ID *id, BPathForeachPathData *bpath_data)
static bool camera_frame_fit_calc_from_data(CameraParams *params, CameraViewFrameData *data, float r_co[3], float *r_scale, float *r_clip_start, float *r_clip_end)
void BKE_camera_background_image_remove(struct Camera *cam, struct CameraBGImage *bgpic)
void BKE_camera_multiview_view_matrix(const struct RenderData *rd, const struct Object *camera, bool is_left, float r_viewmat[4][4])
static void camera_write_cycles_compatibility_data_clear(ID *id, CameraCyclesCompatibilityData &data)
void BKE_camera_view_frame(const struct Scene *scene, const struct Camera *camera, float r_vec[4][3])
IDProperty * IDP_GetPropertyTypeFromGroup(const IDProperty *prop, StringRef name, char type) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:839
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void BLI_rctf_rotate_expand_around(rctf *dst, const rctf *src, const float pivot[2], float angle)
Definition rct.cc:1145
void copy_m3_m4(float m1[3][3], const float m2[4][4])
void id_us_plus(ID *id)
Definition lib_id.cc:433
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
IDProperty * IDP_ID_system_properties_ensure(ID *id) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition idprop.cc:1023
void copy_m4_m3(float m1[4][4], const float m2[3][3])
@ IDWALK_DO_DEPRECATED_POINTERS
bool IDP_AddToGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:788
bool BKE_camera_view_render_border(const struct Scene *scene, const struct Depsgraph *depsgraph, const struct View3D *v3d, const struct RegionView3D *rv3d, int winx, int winy, rctf *r_border, rctf *r_unrolled_border)
IDProperty * IDP_CopyProperty(const IDProperty *prop) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:986
void normalize_m3(float R[3][3]) ATTR_NONNULL()
void planes_from_projmat(const float mat[4][4], float left[4], float right[4], float bottom[4], float top[4], float near[4], float far[4])
static CameraCyclesCompatibilityData camera_write_cycles_compatibility_data_create(ID *id)
float2 BKE_camera_viewplane_offset_transform(float roll, bool is_flipped_x, float2 offset)
void BKE_camera_params_compute_viewplane(CameraParams *params, int winx, int winy, float aspx, float aspy)
void BKE_camera_multiview_model_matrix(const struct RenderData *rd, const struct Object *camera, const char *viewname, float r_modelmat[4][4])
const char * BKE_scene_multiview_view_suffix_get(const RenderData *rd, const char *viewname)
Definition scene.cc:3390
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
bool invert_m4(float mat[4][4])
void translate_m4(float mat[4][4], float Tx, float Ty, float Tz)
static void camera_free_data(ID *id)
@ CAMERA_SENSOR_FIT_VERT
@ CAMERA_SENSOR_FIT_AUTO
@ CAMERA_SENSOR_FIT_HOR
LibraryForeachIDFlag BKE_lib_query_foreachid_process_flags_get(const LibraryForeachIDData *data)
Definition lib_query.cc:133
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
IDProperty * IDP_ID_system_properties_get(ID *id) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition idprop.cc:1018
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:225
static void camera_init_data(ID *id)
float2 BKE_camera_frame_size(float winx, float winy, float frame_aspect)
void perspective_m4(float mat[4][4], float left, float right, float bottom, float top, float nearClip, float farClip)
@ V3D_SHOW_CAMERA_PASSEPARTOUT
@ LIB_ID_CREATE_NO_USER_REFCOUNT
@ LIB_ID_COPY_NO_LIB_OVERRIDE_LOCAL_DATA_FLAG
MINLINE void mul_v3_fl(float r[3], float f)
void normalize_m4(float R[4][4]) ATTR_NONNULL()
void BKE_camera_multiview_params(const struct RenderData *rd, struct CameraParams *params, const struct Object *camera, const char *viewname)
void orthographic_m4(float mat[4][4], float left, float right, float bottom, float top, float nearClip, float farClip)
void BKE_camera_params_compute_matrix(CameraParams *params)
float BKE_camera_multiview_shift_x(const struct RenderData *rd, const struct Object *camera, const char *viewname)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
float BKE_camera_object_dof_distance(const struct Object *ob)
void BKE_camera_background_image_clear(struct Camera *cam)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
void BLI_rctf_mul(struct rctf *rect, float factor)
Definition rct.cc:602
void size_to_mat4(float R[4][4], const float size[3])
VecBase< float, 2 > float2
void BKE_camera_params_from_view3d(CameraParams *params, const struct Depsgraph *depsgraph, const struct View3D *v3d, const struct RegionView3D *rv3d)
struct Object * BKE_camera_multiview_render(const Main &bmain, const struct Scene *scene, struct Object *camera, const char *viewname)
static void camera_foreach_id(ID *id, LibraryForeachIDData *data)
void BKE_camera_multiview_window_matrix(const struct RenderData *rd, const struct Object *camera, const char *viewname, float r_winmat[4][4])
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
@ CAM_BGIMG_FLAG_OVERRIDE_LIBRARY_LOCAL
@ CAM_BGIMG_FLAG_EXPANDED
@ CAM_BGIMG_FLAG_CAMERA_ASPECT
BMesh const char void * data
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
struct Camera * BKE_camera_add(struct Main *bmain, const char *name)
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
static void camera_to_frame_view_cb(const float co[3], void *user_data)
void BKE_camera_params_from_object(CameraParams *params, const struct Object *cam_ob)
rctf BKE_camera_view_border(const struct Scene *scene, const struct Depsgraph *depsgraph, const struct View3D *v3d, const struct RegionView3D *rv3d, int winx, int winy, bool no_shift, bool no_zoom, bool no_roll)
static void camera_stereo3d_model_matrix(const Object *camera, const bool is_left, float r_modelmat[4][4])
void BKE_scene_foreach_display_point(Depsgraph *depsgraph, void(*func_cb)(const float[3], void *), void *user_data)
Definition object.cc:4103
static void camera_copy_data(Main *, std::optional< Library * >, ID *id_dst, const ID *id_src, const int flag)
MINLINE float plane_point_side_v3(const float plane[4], const float co[3])
void unit_m4(float m[4][4])
bool BKE_camera_multiview_spherical_stereo(const struct RenderData *rd, const struct Object *camera)
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void zero_v3(float r[3])
void mul_m4_v4(const float mat[4][4], float r[4])
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
int BKE_camera_sensor_fit(int sensor_fit, float sizex, float sizey)
bool BKE_camera_view_frame_fit_to_scene(struct Depsgraph *depsgraph, const struct Scene *scene, struct Object *camera_ob, float r_co[3], float *r_scale, float *r_clip_start, float *r_clip_end)
MINLINE float normalize_v3_v3(float r[3], const float a[3])
void mat4_to_size(float size[3], const float M[4][4])
static bool camera_passepartout_is_opaque(const View3D *v3d)
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
static void camera_blend_write(BlendWriter *writer, ID *id, const void *id_address)
static float camera_stereo3d_shift_x(const Object *camera, const char *viewname)
MINLINE float normalize_v3(float n[3])
float BKE_camera_sensor_size(int sensor_fit, float sensor_x, float sensor_y)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
struct CameraBGImage * BKE_camera_background_image_new(struct Camera *cam)
static void camera_model_matrix(const Object *camera, float r_modelmat[4][4])
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void transpose_m4(float R[4][4])
void BKE_camera_multiview_model_matrix_scaled(const struct RenderData *rd, const struct Object *camera, const char *viewname, float r_modelmat[4][4])
bool BKE_bpath_foreach_path_fixed_process(BPathForeachPathData *bpath_data, char *path, size_t path_maxncpy)
Definition bpath.cc:130
@ INDEX_ID_CA
Definition DNA_ID.h:1335
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1403
void BKE_camera_view_frame_ex(const struct Scene *scene, const struct Camera *camera, float drawsize, bool do_clip, const float scale[3], float r_asp[2], float r_shift[2], float *r_drawsize, float r_vec[4][3])
static void camera_frame_fit_data_init(const Scene *scene, const Object *ob, const bool do_clip_dists, CameraParams *params, CameraViewFrameData *data)
static bool camera_is_left(const char *viewname)
void BLI_rctf_translate(struct rctf *rect, float x, float y)
Definition rct.cc:587
float BKE_screen_view3d_zoom_to_fac(float camzoom)
Definition screen.cc:1283
@ SCE_VIEWS_FORMAT_MULTIVIEW
@ SCE_VIEWS_FORMAT_STEREO_3D
bool BKE_camera_view_frame_fit_to_coords(const struct Depsgraph *depsgraph, const float(*cos)[3], int num_cos, struct Object *camera_ob, float r_co[3], float *r_scale)
Object * BKE_scene_object_find_by_name(const Main &bmain, const Scene *scene, const char *name)
Definition scene.cc:2220
void copy_m4_m4(float m1[4][4], const float m2[4][4])
void BKE_camera_params_init(CameraParams *params)
void normalize_m4_m4(float rmat[4][4], const float mat[4][4]) ATTR_NONNULL()
static void camera_blend_read_data(BlendDataReader *reader, ID *id)
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
static Object * camera_multiview_advanced(const Main &bmain, const Scene *scene, Object *camera, const char *suffix)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
int isect_line_line_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3], float r_i1[3], float r_i2[3])
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:50
void BKE_camera_params_crop_viewplane(rctf *viewplane, int winx, int winy, const rcti *region)
#define fabsf
#define atanf
#define sinf
#define tanf
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
void write_string(const char *data)
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
bool is_undo() const
struct CameraBGImage * next
struct MovieClip * clip
struct CameraBGImage * prev
eCamera_BGImage_Flag flag
float plane_tx[CAMERA_VIEWFRAME_NUM_PLANES][4]
float dist_vals[CAMERA_VIEWFRAME_NUM_PLANES]
eImageUser_Flag flag
struct Scene * scene
eRegionView3D_Flag rflag
eRegionView3D_Persp persp
ListBaseT< SceneRenderView > views
eSceneViews_Format views_format
struct RenderData r
eView3D_Flag2 flag2
struct Object * camera
#define N_(msgid)