1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright 2019, Blender Foundation.
*/
/** \file
* \ingroup draw_engine
*/
#include "DRW_render.h"
#include "BKE_camera.h"
#include "BKE_image.h"
#include "BKE_movieclip.h"
#include "BKE_object.h"
#include "BLI_listbase.h"
#include "DNA_camera_types.h"
#include "DNA_screen_types.h"
#include "DEG_depsgraph_query.h"
#include "ED_view3d.h"
#include "IMB_imbuf_types.h"
#include "overlay_private.h"
void OVERLAY_image_init(OVERLAY_Data *vedata)
{
const DRWContextState *draw_ctx = DRW_context_state_get();
OVERLAY_PrivateData *pd = vedata->stl->pd;
DRWView *default_view = (DRWView *)DRW_view_default_get();
pd->view_reference_images = DRW_view_create_with_zoffset(default_view, draw_ctx->rv3d, -1.0f);
}
void OVERLAY_image_cache_init(OVERLAY_Data *vedata)
{
OVERLAY_PassList *psl = vedata->psl;
OVERLAY_PrivateData *pd = vedata->stl->pd;
DRWState state;
state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_GREATER | DRW_STATE_BLEND_ALPHA_PREMUL;
DRW_PASS_CREATE(psl->image_background_ps, state);
state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS;
DRW_PASS_CREATE(psl->image_empties_ps, state | pd->clipping_state);
state = DRW_STATE_WRITE_COLOR | DRW_STATE_DEPTH_LESS_EQUAL | DRW_STATE_BLEND_ALPHA;
DRW_PASS_CREATE(psl->image_empties_back_ps, state | pd->clipping_state);
DRW_PASS_CREATE(psl->image_empties_blend_ps, state | pd->clipping_state);
state = DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_ALPHA;
DRW_PASS_CREATE(psl->image_empties_front_ps, state);
DRW_PASS_CREATE(psl->image_foreground_ps, state);
}
static void overlay_image_calc_aspect(Image *ima, const int size[2], float r_image_aspect[2])
{
float ima_x, ima_y;
if (ima) {
ima_x = size[0];
ima_y = size[1];
}
else {
/* if no image, make it a 1x1 empty square, honor scale & offset */
ima_x = ima_y = 1.0f;
}
/* Get the image aspect even if the buffer is invalid */
float sca_x = 1.0f, sca_y = 1.0f;
if (ima) {
if (ima->aspx > ima->aspy) {
sca_y = ima->aspy / ima->aspx;
}
else if (ima->aspx < ima->aspy) {
sca_x = ima->aspx / ima->aspy;
}
}
const float scale_x_inv = ima_x * sca_x;
const float scale_y_inv = ima_y * sca_y;
if (scale_x_inv > scale_y_inv) {
r_image_aspect[0] = 1.0f;
r_image_aspect[1] = scale_y_inv / scale_x_inv;
}
else {
r_image_aspect[0] = scale_x_inv / scale_y_inv;
r_image_aspect[1] = 1.0f;
}
}
static eStereoViews camera_background_images_stereo_eye(const Scene *scene, const View3D *v3d)
{
if ((scene->r.scemode & R_MULTIVIEW) == 0) {
return STEREO_LEFT_ID;
}
else if (v3d->stereo3d_camera != STEREO_3D_ID) {
/* show only left or right camera */
return v3d->stereo3d_camera;
}
else {
return v3d->multiview_eye;
}
}
static void camera_background_images_stereo_setup(const Scene *scene,
const View3D *v3d,
Image *ima,
ImageUser *iuser)
{
if (BKE_image_is_stereo(ima)) {
iuser->flag |= IMA_SHOW_STEREO;
iuser->multiview_eye = camera_background_images_stereo_eye(scene, v3d);
BKE_image_multiview_index(ima, iuser);
}
else {
iuser->flag &= ~IMA_SHOW_STEREO;
}
}
static struct GPUTexture *image_camera_background_texture_get(CameraBGImage *bgpic,
const DRWContextState *draw_ctx,
OVERLAY_PrivateData *pd,
float *r_aspect,
bool *r_use_alpha_premult)
{
void *lock;
Image *image = bgpic->ima;
ImageUser *iuser = &bgpic->iuser;
MovieClip *clip = NULL;
GPUTexture *tex = NULL;
Scene *scene = draw_ctx->scene;
float aspect_x, aspect_y;
int width, height;
int ctime = (int)DEG_get_ctime(draw_ctx->depsgraph);
*r_use_alpha_premult = false;
switch (bgpic->source) {
case CAM_BGIMG_SOURCE_IMAGE:
if (image == NULL) {
return NULL;
}
*r_use_alpha_premult = (image->alpha_mode == IMA_ALPHA_PREMUL);
BKE_image_user_frame_calc(image, iuser, ctime);
if (image->source == IMA_SRC_SEQUENCE && !(iuser->flag & IMA_USER_FRAME_IN_RANGE)) {
/* Frame is out of range, dont show. */
return NULL;
}
else {
camera_background_images_stereo_setup(scene, draw_ctx->v3d, image, iuser);
}
iuser->scene = draw_ctx->scene;
ImBuf *ibuf = BKE_image_acquire_ibuf(image, iuser, &lock);
if (ibuf == NULL) {
BKE_image_release_ibuf(image, ibuf, lock);
iuser->scene = NULL;
return NULL;
}
width = ibuf->x;
height = ibuf->y;
tex = GPU_texture_from_blender(image, iuser, ibuf, GL_TEXTURE_2D);
BKE_image_release_ibuf(image, ibuf, lock);
iuser->scene = NULL;
if (tex == NULL) {
return NULL;
}
aspect_x = bgpic->ima->aspx;
aspect_y = bgpic->ima->aspy;
break;
case CAM_BGIMG_SOURCE_MOVIE:
if (bgpic->flag & CAM_BGIMG_FLAG_CAMERACLIP) {
if (scene->camera) {
clip = BKE_object_movieclip_get(scene, scene->camera, true);
}
}
else {
clip = bgpic->clip;
}
if (clip == NULL) {
return NULL;
}
BKE_movieclip_user_set_frame(&bgpic->cuser, ctime);
tex = GPU_texture_from_movieclip(clip, &bgpic->cuser, GL_TEXTURE_2D);
if (tex == NULL) {
return NULL;
}
aspect_x = clip->aspx;
aspect_y = clip->aspy;
BKE_movieclip_get_size(clip, &bgpic->cuser, &width, &height);
/* Save for freeing. */
BLI_addtail(&pd->bg_movie_clips, BLI_genericNodeN(clip));
break;
default:
/* Unsupported type. */
return NULL;
}
*r_aspect = (width * aspect_x) / (height * aspect_y);
return tex;
}
static void OVERLAY_image_free_movieclips_textures(OVERLAY_Data *data)
{
/* Free Movie clip textures after rendering */
LinkData *link;
while ((link = BLI_pophead(&data->stl->pd->bg_movie_clips))) {
MovieClip *clip = (MovieClip *)link->data;
GPU_free_texture_movieclip(clip);
MEM_freeN(link);
}
}
static void image_camera_background_matrix_get(const Camera *cam,
const CameraBGImage *bgpic,
const DRWContextState *draw_ctx,
const float image_aspect,
float rmat[4][4])
{
float rotate[4][4], scale[4][4], translate[4][4];
axis_angle_to_mat4_single(rotate, 'Z', -bgpic->rotation);
unit_m4(scale);
unit_m4(translate);
/* Normalized Object space camera frame corners. */
float cam_corners[4][3];
BKE_camera_view_frame(draw_ctx->scene, cam, cam_corners);
float cam_width = fabsf(cam_corners[0][0] - cam_corners[3][0]);
float cam_height = fabsf(cam_corners[0][1] - cam_corners[1][1]);
float cam_aspect = cam_width / cam_height;
if (bgpic->flag & CAM_BGIMG_FLAG_CAMERA_CROP) {
/* Crop. */
if (image_aspect > cam_aspect) {
scale[0][0] *= cam_height * image_aspect;
scale[1][1] *= cam_height;
}
else {
scale[0][0] *= cam_width;
scale[1][1] *= cam_width / image_aspect;
}
}
else if (bgpic->flag & CAM_BGIMG_FLAG_CAMERA_ASPECT) {
/* Fit. */
if (image_aspect > cam_aspect) {
scale[0][0] *= cam_width;
scale[1][1] *= cam_width / image_aspect;
}
else {
scale[0][0] *= cam_height * image_aspect;
scale[1][1] *= cam_height;
}
}
else {
/* Stretch. */
scale[0][0] *= cam_width;
scale[1][1] *= cam_height;
}
translate[3][0] = bgpic->offset[0];
translate[3][1] = bgpic->offset[1];
translate[3][2] = cam_corners[0][2];
if (cam->type == CAM_ORTHO) {
mul_v2_fl(translate[3], cam->ortho_scale);
}
/* These lines are for keeping 2.80 behavior and could be removed to keep 2.79 behavior. */
translate[3][0] *= min_ff(1.0f, cam_aspect);
translate[3][1] /= max_ff(1.0f, cam_aspect) * (image_aspect / cam_aspect);
/* quad is -1..1 so divide by 2. */
scale[0][0] *= 0.5f * bgpic->scale * ((bgpic->flag & CAM_BGIMG_FLAG_FLIP_X) ? -1.0 : 1.0);
scale[1][1] *= 0.5f * bgpic->scale * ((bgpic->flag & CAM_BGIMG_FLAG_FLIP_Y) ? -1.0 : 1.0);
/* Camera shift. (middle of cam_corners) */
translate[3][0] += (cam_corners[0][0] + cam_corners[2][0]) * 0.5f;
translate[3][1] += (cam_corners[0][1] + cam_corners[2][1]) * 0.5f;
mul_m4_series(rmat, translate, rotate, scale);
}
void OVERLAY_image_camera_cache_populate(OVERLAY_Data *vedata, Object *ob)
{
OVERLAY_PrivateData *pd = vedata->stl->pd;
OVERLAY_PassList *psl = vedata->psl;
const DRWContextState *draw_ctx = DRW_context_state_get();
const View3D *v3d = draw_ctx->v3d;
const Scene *scene = draw_ctx->scene;
Camera *cam = ob->data;
const bool show_frame = BKE_object_empty_image_frame_is_visible_in_view3d(ob, draw_ctx->rv3d);
if (!show_frame || DRW_state_is_select()) {
return;
}
const bool stereo_eye = camera_background_images_stereo_eye(scene, v3d) == STEREO_LEFT_ID;
const char *viewname = (stereo_eye == STEREO_LEFT_ID) ? STEREO_RIGHT_NAME : STEREO_LEFT_NAME;
float modelmat[4][4];
BKE_camera_multiview_model_matrix(&scene->r, ob, viewname, modelmat);
LISTBASE_FOREACH (CameraBGImage *, bgpic, &cam->bg_images) {
if (bgpic->flag & CAM_BGIMG_FLAG_DISABLED) {
continue;
}
float aspect = 1.0;
bool use_alpha_premult;
float mat[4][4];
/* retrieve the image we want to show, continue to next when no image could be found */
GPUTexture *tex = image_camera_background_texture_get(
bgpic, draw_ctx, pd, &aspect, &use_alpha_premult);
if (tex) {
image_camera_background_matrix_get(cam, bgpic, draw_ctx, aspect, mat);
mul_m4_m4m4(mat, modelmat, mat);
const bool is_foreground = (bgpic->flag & CAM_BGIMG_FLAG_FOREGROUND) != 0;
float color_premult_alpha[4] = {bgpic->alpha, bgpic->alpha, bgpic->alpha, bgpic->alpha};
DRWPass *pass = is_foreground ? psl->image_foreground_ps : psl->image_background_ps;
GPUShader *sh = OVERLAY_shader_image();
DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
DRW_shgroup_uniform_texture(grp, "imgTexture", tex);
DRW_shgroup_uniform_bool_copy(grp, "imgPremultiplied", use_alpha_premult);
DRW_shgroup_uniform_bool_copy(grp, "imgAlphaBlend", true);
DRW_shgroup_uniform_bool_copy(grp, "depthSet", true);
DRW_shgroup_uniform_vec4_copy(grp, "color", color_premult_alpha);
DRW_shgroup_call_obmat(grp, DRW_cache_quad_get(), mat);
}
}
}
void OVERLAY_image_empty_cache_populate(OVERLAY_Data *vedata, Object *ob)
{
OVERLAY_PassList *psl = vedata->psl;
const DRWContextState *draw_ctx = DRW_context_state_get();
const RegionView3D *rv3d = draw_ctx->rv3d;
GPUTexture *tex = NULL;
Image *ima = ob->data;
float mat[4][4];
const bool show_frame = BKE_object_empty_image_frame_is_visible_in_view3d(ob, rv3d);
const bool show_image = show_frame && BKE_object_empty_image_data_is_visible_in_view3d(ob, rv3d);
const bool use_alpha_blend = (ob->empty_image_flag & OB_EMPTY_IMAGE_USE_ALPHA_BLEND) != 0;
const bool use_alpha_premult = ima && (ima->alpha_mode == IMA_ALPHA_PREMUL);
if (!show_frame) {
return;
}
{
/* Calling 'BKE_image_get_size' may free the texture. Get the size from 'tex' instead,
* see: T59347 */
int size[2] = {0};
if (ima != NULL) {
ImageUser iuser = *ob->iuser;
camera_background_images_stereo_setup(draw_ctx->scene, draw_ctx->v3d, ima, &iuser);
tex = GPU_texture_from_blender(ima, &iuser, NULL, GL_TEXTURE_2D);
if (tex) {
size[0] = GPU_texture_orig_width(tex);
size[1] = GPU_texture_orig_height(tex);
}
}
CLAMP_MIN(size[0], 1);
CLAMP_MIN(size[1], 1);
float image_aspect[2];
overlay_image_calc_aspect(ob->data, size, image_aspect);
copy_m4_m4(mat, ob->obmat);
mul_v3_fl(mat[0], image_aspect[0] * 0.5f * ob->empty_drawsize);
mul_v3_fl(mat[1], image_aspect[1] * 0.5f * ob->empty_drawsize);
madd_v3_v3fl(mat[3], mat[0], ob->ima_ofs[0] * 2.0f + 1.0f);
madd_v3_v3fl(mat[3], mat[1], ob->ima_ofs[1] * 2.0f + 1.0f);
}
/* Use the actual depth if we are doing depth tests to determine the distance to the object */
char depth_mode = DRW_state_is_depth() ? OB_EMPTY_IMAGE_DEPTH_DEFAULT : ob->empty_image_depth;
DRWPass *pass = NULL;
if ((ob->dtx & OB_DRAWXRAY) != 0) {
/* Object In Front overrides image empty depth mode. */
pass = psl->image_empties_front_ps;
}
else {
switch (depth_mode) {
case OB_EMPTY_IMAGE_DEPTH_DEFAULT:
pass = (use_alpha_blend) ? psl->image_empties_blend_ps : psl->image_empties_ps;
break;
case OB_EMPTY_IMAGE_DEPTH_BACK:
pass = psl->image_empties_back_ps;
break;
case OB_EMPTY_IMAGE_DEPTH_FRONT:
pass = psl->image_empties_front_ps;
break;
}
}
if (show_frame) {
OVERLAY_ExtraCallBuffers *cb = OVERLAY_extra_call_buffer_get(vedata, ob);
float *color;
DRW_object_wire_theme_get(ob, draw_ctx->view_layer, &color);
OVERLAY_empty_shape(cb, mat, 1.0f, OB_EMPTY_IMAGE, color);
}
if (show_image && tex && ((ob->color[3] > 0.0f) || !use_alpha_blend)) {
GPUShader *sh = OVERLAY_shader_image();
DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
DRW_shgroup_uniform_texture(grp, "imgTexture", tex);
DRW_shgroup_uniform_bool_copy(grp, "imgPremultiplied", use_alpha_premult);
DRW_shgroup_uniform_bool_copy(grp, "imgAlphaBlend", use_alpha_blend);
DRW_shgroup_uniform_bool_copy(grp, "depthSet", depth_mode != OB_EMPTY_IMAGE_DEPTH_DEFAULT);
DRW_shgroup_uniform_vec4_copy(grp, "color", ob->color);
DRW_shgroup_call_obmat(grp, DRW_cache_quad_get(), mat);
}
}
void OVERLAY_image_cache_finish(OVERLAY_Data *vedata)
{
OVERLAY_PassList *psl = vedata->psl;
DRW_pass_sort_shgroup_z(psl->image_empties_blend_ps);
DRW_pass_sort_shgroup_z(psl->image_empties_front_ps);
DRW_pass_sort_shgroup_z(psl->image_empties_back_ps);
}
void OVERLAY_image_background_draw(OVERLAY_Data *vedata)
{
OVERLAY_PassList *psl = vedata->psl;
DRW_draw_pass(psl->image_background_ps);
DRW_draw_pass(psl->image_empties_back_ps);
}
void OVERLAY_image_draw(OVERLAY_Data *vedata)
{
OVERLAY_PassList *psl = vedata->psl;
OVERLAY_PrivateData *pd = vedata->stl->pd;
DRW_view_set_active(pd->view_reference_images);
DRW_draw_pass(psl->image_empties_ps);
DRW_draw_pass(psl->image_empties_blend_ps);
DRW_view_set_active(NULL);
}
void OVERLAY_image_in_front_draw(OVERLAY_Data *vedata)
{
OVERLAY_PassList *psl = vedata->psl;
OVERLAY_PrivateData *pd = vedata->stl->pd;
DRW_view_set_active(pd->view_reference_images);
DRW_draw_pass(psl->image_empties_front_ps);
DRW_draw_pass(psl->image_foreground_ps);
DRW_view_set_active(NULL);
OVERLAY_image_free_movieclips_textures(vedata);
}
|