Blender V5.3
overlay_camera.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BKE_camera.h"
12#include "BKE_image_gpu.hh"
13#include "BKE_tracking.hh"
14#include "BLI_math_color_c.hh"
15#include "BLI_math_matrix_c.hh"
18#include "DNA_camera_types.h"
19#include "DRW_render.hh"
20#include "ED_view3d.hh"
21
22#include "draw_manager_text.hh"
23#include "overlay_base.hh"
24#include "overlay_empty.hh"
25
26namespace blender::draw::overlay {
28 public:
29 float &volume_start = color_[2];
30 float &volume_end = color_[3];
31 float &depth = color_[3];
32 float &focus = color_[3];
34 float &dist_color_id = matrix[0][3];
35 float &corner_x = matrix[0][3];
36 float &corner_y = matrix[1][3];
37 float &center_x = matrix[2][3];
38 float &clip_start = matrix[2][3];
39 float &mist_start = matrix[2][3];
40 float &center_y = matrix[3][3];
41 float &clip_end = matrix[3][3];
42 float &mist_end = matrix[3][3];
43
48
49 CameraInstanceData(const float4x4 &p_matrix, const float4 &color)
50 : ExtraInstanceData(p_matrix, color, 1.0f) {};
51};
52
58/* TODO(fclem): Split into multiple overlay classes. */
60 using CameraInstanceBuf = ShapeInstanceBuf<ExtraInstanceData>;
61
62 private:
63 PassSimple ps_ = {"Cameras"};
64
65 /* Camera background images with "Depth" switched to "Back".
66 * Shown in camera view behind all objects. */
67 PassMain background_ps_ = {"background_ps_"};
68 /* Camera background images with "Depth" switched to "Front".
69 * Shown in camera view in front of all objects. */
70 PassMain foreground_ps_ = {"foreground_ps_"};
71
72 /* Same as `background_ps_` with "View as Render" checked. */
73 PassMain background_scene_ps_ = {"background_scene_ps_"};
74 /* Same as `foreground_ps_` with "View as Render" checked. */
75 PassMain foreground_scene_ps_ = {"foreground_scene_ps_"};
76
77 struct CallBuffers {
78 const SelectionType selection_type_;
79 CameraInstanceBuf distances_buf = {selection_type_, "camera_distances_buf"};
80 CameraInstanceBuf frame_buf = {selection_type_, "camera_frame_buf"};
81 CameraInstanceBuf tria_buf = {selection_type_, "camera_tria_buf"};
82 CameraInstanceBuf tria_wire_buf = {selection_type_, "camera_tria_wire_buf"};
83 CameraInstanceBuf volume_buf = {selection_type_, "camera_volume_buf"};
84 CameraInstanceBuf volume_wire_buf = {selection_type_, "camera_volume_wire_buf"};
85 CameraInstanceBuf sphere_solid_buf = {selection_type_, "camera_sphere_solid_buf"};
86 LinePrimitiveBuf stereo_connect_lines = {selection_type_, "camera_dashed_lines_buf"};
87 LinePrimitiveBuf tracking_path = {selection_type_, "camera_tracking_path_buf"};
88 Empties::CallBuffers empties{selection_type_};
89 } call_buffers_;
90
91 bool images_enabled_ = false;
92 bool extras_enabled_ = false;
93 bool motion_tracking_enabled_ = false;
94
95 View::OffsetData offset_data_;
96 float4x4 depth_bias_winmat_;
97
98 public:
99 Cameras(const SelectionType selection_type) : call_buffers_{selection_type} {};
100
101 void begin_sync(Resources &res, const State &state) final
102 {
103 enabled_ = state.is_space_v3d();
104 extras_enabled_ = enabled_ && state.show_extras();
105 motion_tracking_enabled_ = enabled_ && state.v3d->flag2 & V3D_SHOW_RECONSTRUCTION;
106 images_enabled_ = enabled_ && !res.is_selection() && !state.is_depth_only_drawing;
107 enabled_ = extras_enabled_ || images_enabled_ || motion_tracking_enabled_;
108
109 offset_data_ = state.offset_data_get();
110
111 if (extras_enabled_ || motion_tracking_enabled_) {
112 call_buffers_.distances_buf.clear();
113 call_buffers_.frame_buf.clear();
114 call_buffers_.tria_buf.clear();
115 call_buffers_.tria_wire_buf.clear();
116 call_buffers_.volume_buf.clear();
117 call_buffers_.volume_wire_buf.clear();
118 call_buffers_.sphere_solid_buf.clear();
119 call_buffers_.stereo_connect_lines.clear();
120 call_buffers_.tracking_path.clear();
121 Empties::begin_sync(call_buffers_.empties);
122 }
123
124 if (images_enabled_) {
125 /* Init image passes. */
126 auto init_pass = [&](PassMain &pass, DRWState draw_state) {
127 pass.init();
128 pass.state_set(draw_state, state.clipping_plane_count);
129 pass.shader_set(res.shaders->image_plane_depth_bias.get());
130 pass.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
131 pass.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
132 pass.push_constant("depth_bias_winmat", &depth_bias_winmat_);
133 res.select_bind(pass);
134 };
135
136 DRWState draw_state;
138 init_pass(background_ps_, draw_state);
139
141 init_pass(background_scene_ps_, draw_state);
142
144 init_pass(foreground_ps_, draw_state);
145 init_pass(foreground_scene_ps_, draw_state);
146 }
147 }
148
149 void object_sync(Manager &manager,
150 const ObjectRef &ob_ref,
151 Resources &res,
152 const State &state) final
153 {
154 if (!enabled_) {
155 return;
156 }
157
158 const select::ID select_id = res.select_id(ob_ref);
159
160 object_sync_extras(ob_ref, select_id, state, res);
161
162 object_sync_motion_paths(ob_ref, res, state);
163
164 object_sync_images(ob_ref, select_id, manager, state, res);
165 }
166
167 void end_sync(Resources &res, const State &state) final
168 {
169 if (!extras_enabled_ && !motion_tracking_enabled_) {
170 return;
171 }
172
173 ps_.init();
174 ps_.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
175 ps_.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
176 res.select_bind(ps_);
177
178 {
179 PassSimple::Sub &sub_pass = ps_.sub("volume");
182 state.clipping_plane_count);
183 sub_pass.shader_set(res.shaders->extra_shape.get());
184 call_buffers_.volume_buf.end_sync(sub_pass, res.shapes.camera_volume.get());
185 }
186 {
187 PassSimple::Sub &sub_pass = ps_.sub("volume_wire");
190 state.clipping_plane_count);
191 sub_pass.shader_set(res.shaders->extra_shape.get());
192 call_buffers_.volume_wire_buf.end_sync(sub_pass, res.shapes.camera_volume_wire.get());
193 }
194
195 {
196 PassSimple::Sub &sub_pass = ps_.sub("camera_shapes");
199 state.clipping_plane_count);
200 sub_pass.shader_set(res.shaders->extra_shape.get());
201 call_buffers_.distances_buf.end_sync(sub_pass, res.shapes.camera_distances.get());
202 call_buffers_.frame_buf.end_sync(sub_pass, res.shapes.camera_frame.get());
203 call_buffers_.tria_buf.end_sync(sub_pass, res.shapes.camera_tria.get());
204 call_buffers_.tria_wire_buf.end_sync(sub_pass, res.shapes.camera_tria_wire.get());
205 call_buffers_.sphere_solid_buf.end_sync(sub_pass, res.shapes.sphere_low_detail.get());
206 }
207
208 {
209 PassSimple::Sub &sub_pass = ps_.sub("camera_extra_wire");
212 state.clipping_plane_count);
213 sub_pass.shader_set(res.shaders->extra_wire.get());
214 call_buffers_.stereo_connect_lines.end_sync(sub_pass);
215 call_buffers_.tracking_path.end_sync(sub_pass);
216 }
217
218 PassSimple::Sub &sub_pass = ps_.sub("empties");
219 Empties::end_sync(res, state, sub_pass, call_buffers_.empties);
220 }
221
222 void pre_draw(Manager &manager, View &view) final
223 {
224 if (!images_enabled_) {
225 return;
226 }
227
228 manager.generate_commands(background_scene_ps_, view);
229 manager.generate_commands(foreground_scene_ps_, view);
230 manager.generate_commands(background_ps_, view);
231 manager.generate_commands(foreground_ps_, view);
232
233 depth_bias_winmat_ = offset_data_.winmat_polygon_offset(view.winmat(), -1.0f);
234 }
235
236 void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
237 {
238 if (!extras_enabled_ && !motion_tracking_enabled_) {
239 return;
240 }
241
242 GPU_framebuffer_bind(framebuffer);
243 manager.submit(ps_, view);
244 }
245
246 void draw_scene_background_images(gpu::FrameBuffer *framebuffer, Manager &manager, View &view)
247 {
248 if (!images_enabled_) {
249 return;
250 }
251
252 GPU_framebuffer_bind(framebuffer);
253 manager.submit_only(background_scene_ps_, view);
254 manager.submit_only(foreground_scene_ps_, view);
255 }
256
257 void draw_background_images(Framebuffer &framebuffer, Manager &manager, View &view)
258 {
259 if (!images_enabled_) {
260 return;
261 }
262
263 GPU_framebuffer_bind(framebuffer);
264 manager.submit_only(background_ps_, view);
265 }
266
267 void draw_in_front(Framebuffer &framebuffer, Manager &manager, View &view)
268 {
269 if (!images_enabled_) {
270 return;
271 }
272
273 GPU_framebuffer_bind(framebuffer);
274 manager.submit_only(foreground_ps_, view);
275 }
276
277 private:
278 void object_sync_extras(const ObjectRef &ob_ref,
279 select::ID select_id,
280 const State &state,
281 Resources &res)
282 {
283 if (!extras_enabled_) {
284 return;
285 }
286
287 Object *ob = ob_ref.object;
288 float4x4 mat = ob->object_to_world();
289 /* Normalize matrix scale. */
290 mat.view<3, 3>() = math::normalize(mat.view<3, 3>());
292
293 const View3D *v3d = state.v3d;
294 const Scene *scene = state.scene;
295 const RegionView3D *rv3d = state.rv3d;
296
298 const Object *camera_object = DEG_get_evaluated(state.depsgraph, v3d->camera);
299 const bool is_select = res.is_selection();
300 const bool is_active = (ob == camera_object);
301 const bool is_camera_view = (is_active && (rv3d->persp == RV3D_CAMOB));
302
303 const bool is_multiview = (scene->r.scemode & R_MULTIVIEW) != 0;
304 const bool is_stereo3d_view = (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D);
305 const bool is_stereo3d_display_extra = is_active && is_multiview && (!is_camera_view) &&
306 ((v3d->stereo3d_flag) != 0);
307 const bool is_selection_camera_stereo = is_select && is_camera_view && is_multiview &&
308 is_stereo3d_view;
309
310 float3 scale = math::to_scale(ob->object_to_world());
311 /* BKE_camera_multiview_model_matrix already accounts for scale, don't do it here. */
312 if (is_selection_camera_stereo) {
313 scale = float3(1.0f);
314 }
315 else if (ELEM(0.0f, scale.x, scale.y, scale.z)) {
316 /* Avoid division by 0. */
317 return;
318 }
319
320 float4x3 vecs;
321 float2 aspect_ratio;
322 float2 shift;
323 float drawsize;
325 &cam,
326 cam.drawsize,
327 is_camera_view,
328 1.0f / scale,
329 aspect_ratio,
330 shift,
331 &drawsize,
332 vecs.ptr());
333
334 /* Apply scale to simplify the rest of the drawing. */
335 for (int i = 0; i < 4; i++) {
336 vecs[i] *= scale;
337 /* Project to z=-1 plane. Makes positioning / scaling easier. (see shader) */
338 mul_v2_fl(vecs[i], 1.0f / std::abs(vecs[i].z));
339 }
340
341 /* Frame coords */
342 const float2 center = (vecs[0].xy() + vecs[2].xy()) * 0.5f;
343 const float2 corner = vecs[0].xy() - center.xy();
344 data.corner_x = corner.x;
345 data.corner_y = corner.y;
346 data.center_x = center.x;
347 data.center_y = center.y;
348 data.depth = vecs[0].z;
349
350 if (is_camera_view) {
351 if (!state.is_image_render) {
352 /* Only draw the frame. */
353 if (is_multiview) {
354 float4x4 mat;
355 const bool is_right = v3d->multiview_eye == STEREO_RIGHT_ID;
356 const char *view_name = is_right ? STEREO_RIGHT_NAME : STEREO_LEFT_NAME;
357 BKE_camera_multiview_model_matrix(&scene->r, ob, view_name, mat.ptr());
358 data.center_x += camera_offaxis_shiftx_get(scene, ob, data.corner_x, is_right);
359 for (int i : IndexRange(4)) {
360 /* Partial copy to avoid overriding packed data. */
361 for (int j : IndexRange(3)) {
362 data.matrix[i][j] = mat[i][j];
363 }
364 }
365 }
366 data.depth *= -1.0f; /* Hides the back of the camera wires (see shader). */
367 call_buffers_.frame_buf.append(data, select_id);
368 }
369 }
370 else {
371 /* Stereo cameras, volumes, plane drawing. */
372 if (is_stereo3d_display_extra) {
373 sync_stereoscopy_extra(
374 *DEG_get_bmain(state.depsgraph), data, select_id, scene, v3d, res, ob);
375 }
376 else {
377 call_buffers_.frame_buf.append(data, select_id);
378 }
379 }
380
381 if (!is_camera_view) {
382 /* Triangle. */
383 float tria_size = 0.7f * drawsize / fabsf(data.depth);
384 float tria_margin = 0.1f * drawsize / fabsf(data.depth);
385 data.center_x = center.x;
386 data.center_y = center.y + data.corner_y + tria_margin + tria_size;
387 data.corner_x = data.corner_y = -tria_size;
388 (is_active ? call_buffers_.tria_buf : call_buffers_.tria_wire_buf).append(data, select_id);
389 }
390
391 if (cam.flag & CAM_SHOWLIMITS) {
392 /* Scale focus point. */
393 data.matrix.x_axis() *= cam.drawsize;
394 data.matrix.y_axis() *= cam.drawsize;
395
396 data.dist_color_id = (is_active) ? 3 : 2;
398 data.clip_start = cam.clip_start;
399 data.clip_end = cam.clip_end;
400 call_buffers_.distances_buf.append(data, select_id);
401 }
402
403 if (cam.flag & CAM_SHOWMIST) {
404 World *world = scene->world;
405 if (world) {
406 data.dist_color_id = (is_active) ? 1 : 0;
407 data.focus = 1.0f; /* Disable */
408 data.mist_start = world->miststa;
409 data.mist_end = world->miststa + world->mistdist;
410 call_buffers_.distances_buf.append(data, select_id);
411 }
412 }
413 }
414
415 void object_sync_motion_paths(const ObjectRef &ob_ref, Resources &res, const State &state)
416 {
417 if (!motion_tracking_enabled_) {
418 return;
419 }
420
421 Object *ob = ob_ref.object;
422 const View3D *v3d = state.v3d;
423 const Scene *scene = state.scene;
424
425 MovieClip *clip = BKE_object_movieclip_get(const_cast<Scene *>(scene), ob, false);
426 if (clip == nullptr) {
427 return;
428 }
429
430 const float4 &color = res.object_wire_color(ob_ref, state);
431
432 const bool is_selection = res.is_selection();
433 const bool is_solid_bundle = (v3d->bundle_drawtype == OB_EMPTY_SPHERE) &&
434 ((v3d->shading.type != OB_SOLID) || !XRAY_FLAG_ENABLED(v3d));
435
436 MovieTracking *tracking = &clip->tracking;
437 /* Index must start in 1, to mimic BKE_tracking_track_get_for_selection_index. */
438 int track_index = 1;
439
440 float4 bundle_color_custom;
441 float *bundle_color_solid = res.theme.colors.bundle_solid;
442 float *bundle_color_unselected = res.theme.colors.wire;
443 uchar4 text_color_selected, text_color_unselected;
444 /* Color Management: Exception here as texts are drawn in sRGB space directly. */
445 ui::theme::get_color_4ubv(TH_SELECT, text_color_selected);
446 ui::theme::get_color_4ubv(TH_TEXT, text_color_unselected);
447
448 float4x4 camera_mat;
450
451 const float4x4 object_to_world{ob->object_to_world().ptr()};
452
453 for (MovieTrackingObject *tracking_object :
454 ListBaseWrapper<MovieTrackingObject>(&tracking->objects))
455 {
456 float4x4 tracking_object_mat;
457
458 if (tracking_object->flag & TRACKING_OBJECT_CAMERA) {
459 tracking_object_mat = camera_mat;
460 }
461 else {
462 const int framenr = BKE_movieclip_remap_scene_to_clip_frame(
463 clip, DEG_get_ctime(state.depsgraph));
464
465 float4x4 object_mat;
467 tracking, tracking_object, framenr, object_mat.ptr());
468
469 tracking_object_mat = object_to_world * math::invert(object_mat);
470 }
471
472 for (MovieTrackingTrack *track :
473 ListBaseWrapper<MovieTrackingTrack>(&tracking_object->tracks))
474 {
475 if ((track->flag & TRACK_HAS_BUNDLE) == 0) {
476 continue;
477 }
478 bool is_selected = TRACK_SELECTED(track);
479
480 float4x4 bundle_mat = math::translate(tracking_object_mat, float3(track->bundle_pos));
481
482 const float *bundle_color;
483 if (track->flag & TRACK_CUSTOMCOLOR) {
484 /* Meh, hardcoded srgb transform here. */
485 /* TODO: change the actual DNA color to be linear. */
486 srgb_to_linearrgb_v3_v3(bundle_color_custom, track->color);
487 bundle_color_custom[3] = 1.0;
488
489 bundle_color = bundle_color_custom;
490 }
491 else if (is_solid_bundle) {
492 bundle_color = bundle_color_solid;
493 }
494 else if (is_selected) {
495 bundle_color = color;
496 }
497 else {
498 bundle_color = bundle_color_unselected;
499 }
500
501 const select::ID track_select_id = res.select_id(ob_ref, track_index++ << 16);
502 if (is_solid_bundle) {
503 if (is_selected) {
504 Empties::object_sync(track_select_id,
505 bundle_mat,
506 v3d->bundle_size,
507 v3d->bundle_drawtype,
508 color,
509 call_buffers_.empties);
510 }
511
512 call_buffers_.sphere_solid_buf.append(
513 ExtraInstanceData{bundle_mat, float4(float3(bundle_color), 1.0f), v3d->bundle_size},
514 track_select_id);
515 }
516 else {
517 Empties::object_sync(track_select_id,
518 bundle_mat,
519 v3d->bundle_size,
520 v3d->bundle_drawtype,
521 bundle_color,
522 call_buffers_.empties);
523 }
524
525 if ((v3d->flag2 & V3D_SHOW_BUNDLENAME) && !is_selection) {
527 bundle_mat[3],
528 track->name,
529 strlen(track->name),
530 10,
531 0,
533 is_selected ? text_color_selected : text_color_unselected);
534 }
535 }
536
537 if ((v3d->flag2 & V3D_SHOW_CAMERAPATH) && (tracking_object->flag & TRACKING_OBJECT_CAMERA) &&
538 !is_selection)
539 {
540 const MovieTrackingReconstruction *reconstruction = &tracking_object->reconstruction;
541
542 if (reconstruction->camnr) {
543 const MovieReconstructedCamera *camera = reconstruction->cameras;
544 float3 v0, v1 = float3(0.0f);
545 for (int a = 0; a < reconstruction->camnr; a++, camera++) {
546 v0 = v1;
547 v1 = math::transform_point(camera_mat, float3(camera->mat[3]));
548 if (a > 0) {
549 /* This one is suboptimal (gl_lines instead of gl_line_strip)
550 * but we keep this for simplicity */
551 call_buffers_.tracking_path.append(v0, v1, TH_CAMERA_PATH);
552 }
553 }
554 }
555 }
556 }
557 }
558
559 void object_sync_images(const ObjectRef &ob_ref,
560 select::ID select_id,
561 Manager &manager,
562 const State &state,
563 Resources &res)
564 {
565 Object *ob = ob_ref.object;
566 const Camera &cam = DRW_object_get_data_for_drawing<Camera>(*ob_ref.object);
567 const Object *camera_object = DEG_get_evaluated(state.depsgraph, state.v3d->camera);
568
569 const bool is_active = ob_ref.object == camera_object;
570 const bool is_camera_view = (is_active && (state.rv3d->persp == RV3D_CAMOB));
571 const bool show_image = (cam.flag & CAM_SHOW_BG_IMAGE) && !cam.bg_images.is_empty();
572 const bool show_frame = BKE_object_empty_image_frame_is_visible_in_view3d(ob, state.rv3d);
573
574 if (!images_enabled_ || !is_camera_view || !show_image || !show_frame) {
575 return;
576 }
577
578 const bool stereo_eye = Images::images_stereo_eye(state.scene, state.v3d) == STEREO_LEFT_ID;
579 const char *viewname = (stereo_eye == STEREO_LEFT_ID) ? STEREO_RIGHT_NAME : STEREO_LEFT_NAME;
580 float4x4 modelmat;
581 BKE_camera_multiview_model_matrix(&state.scene->r, ob, viewname, modelmat.ptr());
582
583 for (const CameraBGImage *bgpic : ConstListBaseWrapper<CameraBGImage>(&cam.bg_images)) {
584 if (bgpic->flag & CAM_BGIMG_FLAG_DISABLED) {
585 continue;
586 }
587
588 float aspect = 1.0;
589 bool use_alpha_premult;
590 bool use_view_transform = false;
591 float4x4 mat;
592
593 /* retrieve the image we want to show, continue to next when no image could be found */
594 gpu::Texture *tex = image_camera_background_texture_get(
595 bgpic, state, res, aspect, use_alpha_premult, use_view_transform);
596
597 if (tex) {
598 image_camera_background_matrix_get(&cam, bgpic, state, aspect, mat);
599
600 /* Apply roll and flip to image. */
601 if (state.rv3d->camroll != 0.0f || (state.rv3d->rflag & RV3D_FLIP_X) != 0) {
602 transpose_m4(mat.ptr());
603 rotate_m4(mat.ptr(), 'Z', -state.rv3d->camroll);
604 if ((state.rv3d->rflag & RV3D_FLIP_X) != 0) {
605 negate_v4(mat[0]);
606 }
607 transpose_m4(mat.ptr());
608 }
609
610 const bool is_foreground = (bgpic->flag & CAM_BGIMG_FLAG_FOREGROUND) != 0;
611 /* Alpha is clamped just below 1.0 to fix background images to interfere with foreground
612 * images. Without this a background image with 1.0 will be rendered on top of a
613 * transparent foreground image due to the different blending modes they use. */
614 const float4 color_premult_alpha{1.0f, 1.0f, 1.0f, std::min(bgpic->alpha, 0.999999f)};
615
616 PassMain &pass = is_foreground ?
617 (use_view_transform ? foreground_scene_ps_ : foreground_ps_) :
618 (use_view_transform ? background_scene_ps_ : background_ps_);
619 pass.bind_texture("img_tx", tex);
620 pass.push_constant("img_premultiplied", use_alpha_premult);
621 pass.push_constant("img_alpha_blend", true);
622 pass.push_constant("is_camera_background", true);
623 pass.push_constant("depth_set", true);
624 pass.push_constant("ucolor", color_premult_alpha);
625 ResourceHandleRange res_handle = manager.resource_handle(mat);
626 pass.draw(res.shapes.quad_solid.get(), res_handle, select_id.get());
627 }
628 }
629 }
630
631 static void image_camera_background_matrix_get(const Camera *cam,
632 const CameraBGImage *bgpic,
633 const State &state,
634 const float image_aspect,
635 float4x4 &rmat)
636 {
638
639 axis_angle_to_mat4_single(rotate.ptr(), 'Z', -bgpic->rotation);
640
641 /* Normalized Object space camera frame corners. */
642 float cam_corners[4][3];
643 BKE_camera_view_frame(state.scene, cam, cam_corners);
644 float cam_width = fabsf(cam_corners[0][0] - cam_corners[3][0]);
645 float cam_height = fabsf(cam_corners[0][1] - cam_corners[1][1]);
646 float cam_aspect = cam_width / cam_height;
647
648 if (bgpic->flag & CAM_BGIMG_FLAG_CAMERA_CROP) {
649 /* Crop. */
650 if (image_aspect > cam_aspect) {
651 scale[0][0] *= cam_height * image_aspect;
652 scale[1][1] *= cam_height;
653 }
654 else {
655 scale[0][0] *= cam_width;
656 scale[1][1] *= cam_width / image_aspect;
657 }
658 }
659 else if (bgpic->flag & CAM_BGIMG_FLAG_CAMERA_ASPECT) {
660 /* Fit. */
661 if (image_aspect > cam_aspect) {
662 scale[0][0] *= cam_width;
663 scale[1][1] *= cam_width / image_aspect;
664 }
665 else {
666 scale[0][0] *= cam_height * image_aspect;
667 scale[1][1] *= cam_height;
668 }
669 }
670 else {
671 /* Stretch. */
672 scale[0][0] *= cam_width;
673 scale[1][1] *= cam_height;
674 }
675
676 translate[3][0] = bgpic->offset[0];
677 translate[3][1] = bgpic->offset[1];
678 translate[3][2] = cam_corners[0][2];
679 if (cam->type == CAM_ORTHO) {
680 translate[3][0] *= cam->ortho_scale;
681 translate[3][1] *= cam->ortho_scale;
682 }
683 /* These lines are for keeping 2.80 behavior and could be removed to keep 2.79 behavior. */
684 translate[3][0] *= min_ff(1.0f, cam_aspect);
685 translate[3][1] /= max_ff(1.0f, cam_aspect) * (image_aspect / cam_aspect);
686 /* quad is -1..1 so divide by 2. */
687 scale[0][0] *= 0.5f * bgpic->scale * ((bgpic->flag & CAM_BGIMG_FLAG_FLIP_X) ? -1.0 : 1.0);
688 scale[1][1] *= 0.5f * bgpic->scale * ((bgpic->flag & CAM_BGIMG_FLAG_FLIP_Y) ? -1.0 : 1.0);
689 /* Camera shift. (middle of cam_corners) */
690 translate[3][0] += (cam_corners[0][0] + cam_corners[2][0]) * 0.5f;
691 translate[3][1] += (cam_corners[0][1] + cam_corners[2][1]) * 0.5f;
692
693 rmat = translate * rotate * scale;
694 }
695
696 gpu::Texture *image_camera_background_texture_get(const CameraBGImage *bgpic,
697 const State &state,
698 Resources &res,
699 float &r_aspect,
700 bool &r_use_alpha_premult,
701 bool &r_use_view_transform)
702 {
703 blender::Image *image = bgpic->ima;
704 ImageUser *iuser = const_cast<ImageUser *>(&bgpic->iuser);
705 MovieClip *clip = nullptr;
706 gpu::Texture *tex = nullptr;
707 float aspect_x, aspect_y;
708 int width, height;
709 int ctime = int(DEG_get_ctime(state.depsgraph));
710 r_use_alpha_premult = false;
711 r_use_view_transform = false;
712
713 switch (bgpic->source) {
715 if (image == nullptr) {
716 return nullptr;
717 }
718 r_use_alpha_premult = (image->alpha_mode == IMA_ALPHA_PREMUL);
719 r_use_view_transform = (image->flag & IMA_VIEW_AS_RENDER) != 0;
720
721 BKE_image_user_frame_calc(image, iuser, ctime);
722 if (image->source == IMA_SRC_SEQUENCE && !(iuser->flag & IMA_USER_FRAME_IN_RANGE)) {
723 /* Frame is out of range, don't show. */
724 return nullptr;
725 }
726
727 Images::stereo_setup(state.scene, state.v3d, image, iuser);
728
729 iuser->scene = const_cast<Scene *>(state.scene);
730 tex = BKE_image_acquire_gpu_viewer_texture(image, iuser);
731 iuser->scene = nullptr;
732
733 if (tex == nullptr) {
734 return nullptr;
735 }
737
738 width = GPU_texture_original_width(tex);
739 height = GPU_texture_original_height(tex);
740
741 aspect_x = bgpic->ima->aspx;
742 aspect_y = bgpic->ima->aspy;
743 break;
744 }
745
747 if (bgpic->flag & CAM_BGIMG_FLAG_CAMERACLIP) {
748 if (state.scene->camera) {
750 const_cast<Scene *>(state.scene), state.scene->camera, true);
751 }
752 }
753 else {
754 clip = bgpic->clip;
755 }
756
757 if (clip == nullptr) {
758 return nullptr;
759 }
760
761 BKE_movieclip_user_set_frame(const_cast<MovieClipUser *>(&bgpic->cuser), ctime);
762 tex = BKE_movieclip_get_gpu_texture(clip, const_cast<MovieClipUser *>(&bgpic->cuser));
763 if (tex == nullptr) {
764 return nullptr;
765 }
766
767 aspect_x = clip->aspx;
768 aspect_y = clip->aspy;
769 r_use_view_transform = true;
770
771 BKE_movieclip_get_size(clip, &bgpic->cuser, &width, &height);
772
773 /* Save for freeing. */
774 res.bg_movie_clips.append(clip);
775 break;
776 }
777
778 default:
779 /* Unsupported type. */
780 return nullptr;
781 }
782
783 r_aspect = (width * aspect_x) / (height * aspect_y);
784 return tex;
785 }
786
791 void sync_stereoscopy_extra(const Main &bmain,
792 const CameraInstanceData &instdata,
793 const select::ID cam_select_id,
794 const Scene *scene,
795 const View3D *v3d,
796 Resources &res,
797 Object *ob)
798 {
799 CameraInstanceData stereodata = instdata;
800
802 const char *viewnames[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME};
803
804 const bool is_stereo3d_cameras = (v3d->stereo3d_flag & V3D_S3D_DISPCAMERAS) != 0;
805 const bool is_stereo3d_plane = (v3d->stereo3d_flag & V3D_S3D_DISPPLANE) != 0;
806 const bool is_stereo3d_volume = (v3d->stereo3d_flag & V3D_S3D_DISPVOLUME) != 0;
807 const bool is_selection = res.is_selection();
808
809 if (!is_stereo3d_cameras) {
810 /* Draw single camera. */
811 call_buffers_.frame_buf.append(instdata, cam_select_id);
812 }
813
814 for (const int eye : IndexRange(2)) {
815 ob = BKE_camera_multiview_render(bmain, scene, ob, viewnames[eye]);
816 BKE_camera_multiview_model_matrix(&scene->r, ob, viewnames[eye], stereodata.matrix.ptr());
817
818 stereodata.corner_x = instdata.corner_x;
819 stereodata.corner_y = instdata.corner_y;
820 stereodata.center_x = instdata.center_x;
821 stereodata.center_y = instdata.center_y;
822 stereodata.depth = instdata.depth;
823
824 stereodata.center_x += camera_offaxis_shiftx_get(scene, ob, instdata.corner_x, eye);
825
826 if (is_stereo3d_cameras) {
827 call_buffers_.frame_buf.append(stereodata, cam_select_id);
828
829 /* Connecting line between cameras. */
830 call_buffers_.stereo_connect_lines.append(stereodata.matrix.location(),
831 instdata.object_to_world.location(),
832 res.theme.colors.wire,
833 cam_select_id);
834 }
835
836 if (is_stereo3d_volume && !is_selection) {
837 float r = (eye == 1) ? 2.0f : 1.0f;
838
839 stereodata.volume_start = -cam.clip_start;
840 stereodata.volume_end = -cam.clip_end;
841 /* Encode eye + intensity and alpha (see shader) */
842 stereodata.color_.x = r + 0.15f;
843 stereodata.color_.y = 1.0f;
844 call_buffers_.volume_wire_buf.append(stereodata, cam_select_id);
845
846 if (v3d->stereo3d_volume_alpha > 0.0f) {
847 /* Encode eye + intensity and alpha (see shader) */
848 stereodata.color_.x = r + 0.999f;
849 stereodata.color_.y = v3d->stereo3d_volume_alpha;
850 call_buffers_.volume_buf.append(stereodata, cam_select_id);
851 }
852 /* restore */
853 stereodata.color_.x = instdata.color_.x;
854 stereodata.color_.y = instdata.color_.y;
855 stereodata.color_.z = instdata.color_.z;
856 }
857 }
858
859 if (is_stereo3d_plane && !is_selection) {
860 if (cam.stereo.convergence_mode == CAM_S3D_TOE) {
861 /* There is no real convergence plane but we highlight the center
862 * point where the views are pointing at. */
863 // stereodata.matrix.x_axis() = float3(0.0f); /* We reconstruct from Z and Y */
864 // stereodata.matrix.y_axis() = float3(0.0f); /* Y doesn't change */
865 stereodata.matrix.z_axis() = float3(0.0f);
866 stereodata.matrix.location() = float3(0.0f);
867 for (int i : IndexRange(2)) {
868 float4x4 mat;
869 /* Need normalized version here. */
870 BKE_camera_multiview_model_matrix(&scene->r, ob, viewnames[i], mat.ptr());
871 stereodata.matrix.z_axis() += mat.z_axis();
872 stereodata.matrix.location() += mat.location() * 0.5f;
873 }
874 stereodata.matrix.z_axis() = math::normalize(stereodata.matrix.z_axis());
875 stereodata.matrix.x_axis() = math::cross(stereodata.matrix.y_axis(),
876 stereodata.matrix.z_axis());
877 }
878 else if (cam.stereo.convergence_mode == CAM_S3D_PARALLEL) {
879 /* Show plane at the given distance between the views even if it makes no sense. */
880 stereodata.matrix.location() = float3(0.0f);
881 for (int i : IndexRange(2)) {
882 float4x4 mat;
883 BKE_camera_multiview_model_matrix_scaled(&scene->r, ob, viewnames[i], mat.ptr());
884 stereodata.matrix.location() += mat.location() * 0.5f;
885 }
886 }
887 else if (cam.stereo.convergence_mode == CAM_S3D_OFFAXIS) {
888 /* Nothing to do. Everything is already setup. */
889 }
890 stereodata.volume_start = -cam.stereo.convergence_distance;
891 stereodata.volume_end = -cam.stereo.convergence_distance;
892 /* Encode eye + intensity and alpha (see shader) */
893 stereodata.color_.x = 0.1f;
894 stereodata.color_.y = 1.0f;
895 call_buffers_.volume_wire_buf.append(stereodata, cam_select_id);
896
897 if (v3d->stereo3d_convergence_alpha > 0.0f) {
898 /* Encode eye + intensity and alpha (see shader) */
899 stereodata.color_.x = 0.0f;
900 stereodata.color_.y = v3d->stereo3d_convergence_alpha;
901 call_buffers_.volume_buf.append(stereodata, cam_select_id);
902 }
903 }
904 }
905
906 static float camera_offaxis_shiftx_get(const Scene *scene,
907 const Object *ob,
908 float corner_x,
909 bool right_eye)
910 {
912 if (cam.stereo.convergence_mode == CAM_S3D_OFFAXIS) {
913 const char *viewnames[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME};
914 const float shiftx = BKE_camera_multiview_shift_x(&scene->r, ob, viewnames[right_eye]);
915 const float delta_shiftx = shiftx - cam.shiftx;
916 const float width = corner_x * 2.0f;
917 return delta_shiftx * width;
918 }
919
920 return 0.0;
921 }
922};
923
924} // namespace blender::draw::overlay
Camera data-block and utility functions.
#define TRACK_SELECTED(track)
#define STEREO_LEFT_NAME
#define STEREO_RIGHT_NAME
#define XRAY_FLAG_ENABLED(v3d)
#define ELEM(...)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
constexpr IndexRange()=default
void hold_texture(gpu::Texture *texture)
void submit_only(PassMain &pass, View &view)
void shader_set(gpu::Shader *shader)
void state_set(DRWState state, int clip_plane_count=0)
void bind_ubo(const char *name, gpu::UniformBuf *buffer)
void push_constant(const char *name, const float &data)
detail::PassBase< command::DrawCommandBuf > Sub
Definition draw_pass.hh:509
void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
void object_sync(Manager &manager, const ObjectRef &ob_ref, Resources &res, const State &state) final
void draw_background_images(Framebuffer &framebuffer, Manager &manager, View &view)
void pre_draw(Manager &manager, View &view) final
void draw_scene_background_images(gpu::FrameBuffer *framebuffer, Manager &manager, View &view)
void begin_sync(Resources &res, const State &state) final
void draw_in_front(Framebuffer &framebuffer, Manager &manager, View &view)
Cameras(const SelectionType selection_type)
void end_sync(Resources &res, const State &state) final
void begin_sync(Resources &res, const State &state) final
void end_sync(Resources &res, const State &state) final
void object_sync(Manager &manager, const ObjectRef &ob_ref, Resources &res, const State &state) final
static void stereo_setup(const Scene *scene, const View3D *v3d, blender::Image *ima, ImageUser *iuser)
static eStereoViews images_stereo_eye(const Scene *scene, const View3D *v3d)
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
#define image(slot, qualifiers, format)
MatBase< 4, 3 > float4x3
void camera(KernelGlobals &kg, const ShadingData &sd, float3 &outview, float &outdepth, float &outdist)
float3x3 scale(float3x3 mat, float2 scale)
float4x4 translate(float4x4 mat, float3 translation)
float3x3 rotate(float3x3 mat, AxisAngle rotation)
const int state
select::SelectionType SelectionType
detail::Pass< command::DrawCommandBuf > PassSimple
detail::Pass< command::DrawMultiBuf > PassMain
CartesianBasis invert(const CartesianBasis &basis)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
MatBase< T, NumCol, NumRow > translate(const MatBase< T, NumCol, NumRow > &mat, const VectorT &translation)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
VecBase< T, 3 > to_scale(const MatBase< T, NumCol, NumRow > &mat)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
void get_color_4ubv(int colorid, unsigned char col[4])
MINLINE float min_ff(float a, float b)
void BKE_image_user_frame_calc(Image *ima, ImageUser *iuser, int cfra)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
VecBase< uint8_t, 4 > uchar4
void BKE_movieclip_get_size(MovieClip *clip, const MovieClipUser *user, int *r_width, int *r_height)
bool BKE_object_empty_image_frame_is_visible_in_view3d(const Object *ob, const RegionView3D *rv3d)
Definition object.cc:3877
void BKE_camera_view_frame(const struct Scene *scene, const struct Camera *camera, float r_vec[4][3])
MINLINE void mul_v2_fl(float r[2], float f)
void srgb_to_linearrgb_v3_v3(float linear[3], const float srgb[3])
@ DRW_TEXT_CACHE_GLOBALSPACE
@ DRW_TEXT_CACHE_STRING_PTR
void BKE_camera_multiview_model_matrix(const struct RenderData *rd, const struct Object *camera, const char *viewname, float r_modelmat[4][4])
MatBase< float, 4, 4 > float4x4
MovieClip * BKE_object_movieclip_get(Scene *scene, const Object *ob, bool use_default)
Definition object.cc:5154
@ V3D_SHOW_RECONSTRUCTION
ListBaseWrapperTemplate< const ListBase, const T > ConstListBaseWrapper
float BKE_camera_multiview_shift_x(const struct RenderData *rd, const struct Object *camera, const char *viewname)
float BKE_camera_object_dof_distance(const struct Object *ob)
struct Object * BKE_camera_multiview_render(const Main &bmain, const struct Scene *scene, struct Object *camera, const char *viewname)
T & DRW_object_get_data_for_drawing(const Object &object)
ListBaseWrapperTemplate< ListBase, T > ListBaseWrapper
@ CAM_BGIMG_FLAG_CAMERACLIP
@ CAM_BGIMG_FLAG_FOREGROUND
@ CAM_BGIMG_FLAG_CAMERA_ASPECT
@ CAM_BGIMG_FLAG_CAMERA_CROP
@ CAM_BGIMG_FLAG_DISABLED
void DRW_text_cache_add(DRWTextStore *dt, const float co[3], const char *str, const int str_len, short xoffs, short yoffs, short flag, const uchar col[4], const bool shadow, const bool align_center)
Main * DEG_get_bmain(const Depsgraph *graph)
BMesh const char void * data
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
gpu::Texture * BKE_image_acquire_gpu_viewer_texture(Image *image, ImageUser *iuser, bool only_full_resolution=false)
Definition image_gpu.cc:749
void BKE_tracking_camera_get_reconstructed_interpolate(MovieTracking *tracking, MovieTrackingObject *tracking_object, float framenr, float mat[4][4])
Definition tracking.cc:2151
@ DRW_STATE_CULL_BACK
Definition draw_state.hh:45
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_BLEND_ALPHA
Definition draw_state.hh:57
@ DRW_STATE_BLEND_ALPHA_UNDER_PREMUL
Definition draw_state.hh:67
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_BLEND_ALPHA_PREMUL
Definition draw_state.hh:59
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:40
void BKE_movieclip_user_set_frame(MovieClipUser *user, int framenr)
float BKE_movieclip_remap_scene_to_clip_frame(const MovieClip *clip, float framenr)
void axis_angle_to_mat4_single(float R[4][4], char axis, float angle)
void rotate_m4(float mat[4][4], char axis, float angle)
@ IMA_USER_FRAME_IN_RANGE
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])
MINLINE float max_ff(float a, float b)
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])
draw::Manager * DRW_manager_get()
MINLINE void negate_v4(float r[4])
void BKE_tracking_get_camera_object_matrix(const Object *camera_object, float mat[4][4])
Definition tracking.cc:372
@ SCE_VIEWS_FORMAT_STEREO_3D
VecBase< float, 3 > float3
int GPU_texture_original_height(const gpu::Texture *texture)
gpu::Texture * BKE_movieclip_get_gpu_texture(MovieClip *clip, MovieClipUser *cuser)
float DEG_get_ctime(const Depsgraph *graph)
int GPU_texture_original_width(const gpu::Texture *texture)
Point object_to_world(Point pt, float4x4 object_to_world)
#define fabsf
ExtraInstanceData(const float4x4 &object_to_world, const float4 &color, float draw_size)
const NodeType * type
Definition graph/node.h:179
const c_style_mat & ptr() const
const MatView< T, ViewNumCol, ViewNumRow, NumCol, NumRow, SrcStartCol, SrcStartRow, Alignment > view() const
eRegionView3D_Persp persp
eSceneViews_Format views_format
struct World * world
struct RenderData r
struct Object * camera
eView3D_StereoFlag stereo3d_flag
CameraInstanceData(const float4x4 &p_matrix, const float4 &color)
CameraInstanceData(const CameraInstanceData &data)
const float4 & object_wire_color(const ObjectRef &ob_ref, ThemeColorID theme_id) const
float x
float y