2023-06-15 05:09:04 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2019 Blender Foundation
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2019-05-18 15:12:47 +02:00
|
|
|
|
|
|
|
/** \file
|
2019-08-07 17:43:04 +02:00
|
|
|
* \ingroup draw_engine
|
2019-05-20 05:16:29 +02:00
|
|
|
*
|
2019-08-07 17:43:04 +02:00
|
|
|
* Utilities to read id buffer created in select_engine.
|
2019-05-18 15:12:47 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
2021-03-09 20:02:40 +01:00
|
|
|
#include "BLI_array_utils.h"
|
2019-05-18 15:12:47 +02:00
|
|
|
#include "BLI_bitmap.h"
|
|
|
|
#include "BLI_bitmap_draw_2d.h"
|
|
|
|
#include "BLI_rect.h"
|
2019-08-07 17:43:04 +02:00
|
|
|
|
|
|
|
#include "DNA_screen_types.h"
|
|
|
|
|
|
|
|
#include "GPU_select.h"
|
2019-05-18 15:12:47 +02:00
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
#include "DEG_depsgraph.h"
|
|
|
|
#include "DEG_depsgraph_query.h"
|
|
|
|
|
2019-08-05 23:02:43 +02:00
|
|
|
#include "DRW_engine.h"
|
2019-08-07 17:43:04 +02:00
|
|
|
#include "DRW_select_buffer.h"
|
|
|
|
|
2019-08-12 17:10:44 +02:00
|
|
|
#include "draw_manager.h"
|
2019-08-07 17:43:04 +02:00
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
#include "../engines/select/select_engine.h"
|
|
|
|
|
2019-08-07 17:43:04 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Buffer of select ID's
|
|
|
|
* \{ */
|
|
|
|
|
2023-06-03 00:36:28 +02:00
|
|
|
uint *DRW_select_buffer_read(
|
|
|
|
Depsgraph *depsgraph, ARegion *region, View3D *v3d, const rcti *rect, uint *r_buf_len)
|
2019-08-07 17:43:04 +02:00
|
|
|
{
|
2019-08-15 15:31:54 +02:00
|
|
|
uint *r_buf = NULL;
|
|
|
|
uint buf_len = 0;
|
2019-08-07 17:43:04 +02:00
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
/* Clamp rect. */
|
2019-08-07 17:43:04 +02:00
|
|
|
rcti r = {
|
|
|
|
.xmin = 0,
|
2020-03-06 16:56:42 +01:00
|
|
|
.xmax = region->winx,
|
2019-08-07 17:43:04 +02:00
|
|
|
.ymin = 0,
|
2020-03-06 16:56:42 +01:00
|
|
|
.ymax = region->winy,
|
2019-08-07 17:43:04 +02:00
|
|
|
};
|
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
/* Make sure that the rect is within the bounds of the viewport.
|
|
|
|
* Some GPUs have problems reading pixels off limits. */
|
2019-08-07 17:43:04 +02:00
|
|
|
rcti rect_clamp = *rect;
|
|
|
|
if (BLI_rcti_isect(&r, &rect_clamp, &rect_clamp)) {
|
2023-06-03 00:36:28 +02:00
|
|
|
SELECTID_Context *select_ctx = DRW_select_engine_context_get();
|
2019-08-07 17:43:04 +02:00
|
|
|
|
2023-06-08 15:46:53 +02:00
|
|
|
DRW_gpu_context_enable();
|
2019-08-15 15:31:54 +02:00
|
|
|
/* Update the drawing. */
|
2020-03-06 16:56:42 +01:00
|
|
|
DRW_draw_select_id(depsgraph, region, v3d, rect);
|
2019-08-15 15:31:54 +02:00
|
|
|
|
|
|
|
if (select_ctx->index_drawn_len > 1) {
|
2020-03-06 16:56:42 +01:00
|
|
|
BLI_assert(region->winx == GPU_texture_width(DRW_engine_select_texture_get()) &&
|
|
|
|
region->winy == GPU_texture_height(DRW_engine_select_texture_get()));
|
2019-08-15 15:31:54 +02:00
|
|
|
|
|
|
|
/* Read the UI32 pixels. */
|
|
|
|
buf_len = BLI_rcti_size_x(rect) * BLI_rcti_size_y(rect);
|
|
|
|
r_buf = MEM_mallocN(buf_len * sizeof(*r_buf), __func__);
|
|
|
|
|
2019-08-20 16:36:18 +02:00
|
|
|
GPUFrameBuffer *select_id_fb = DRW_engine_select_framebuffer_get();
|
|
|
|
GPU_framebuffer_bind(select_id_fb);
|
2020-07-16 02:50:55 +02:00
|
|
|
GPU_framebuffer_read_color(select_id_fb,
|
|
|
|
rect_clamp.xmin,
|
|
|
|
rect_clamp.ymin,
|
|
|
|
BLI_rcti_size_x(&rect_clamp),
|
|
|
|
BLI_rcti_size_y(&rect_clamp),
|
|
|
|
1,
|
|
|
|
0,
|
2021-02-17 16:38:21 +01:00
|
|
|
GPU_DATA_UINT,
|
2020-07-16 02:50:55 +02:00
|
|
|
r_buf);
|
2019-08-15 15:31:54 +02:00
|
|
|
|
|
|
|
if (!BLI_rcti_compare(rect, &rect_clamp)) {
|
|
|
|
/* The rect has been clamped so you need to realign the buffer and fill in the blanks */
|
|
|
|
GPU_select_buffer_stride_realign(rect, &rect_clamp, r_buf);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 17:43:04 +02:00
|
|
|
|
|
|
|
GPU_framebuffer_restore();
|
2023-06-08 15:46:53 +02:00
|
|
|
DRW_gpu_context_disable();
|
2019-08-15 15:31:54 +02:00
|
|
|
}
|
2019-08-07 17:43:04 +02:00
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
if (r_buf_len) {
|
|
|
|
*r_buf_len = buf_len;
|
2019-08-07 17:43:04 +02:00
|
|
|
}
|
2019-08-15 15:31:54 +02:00
|
|
|
|
|
|
|
return r_buf;
|
2019-08-07 17:43:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** \} */
|
2019-05-18 15:12:47 +02:00
|
|
|
|
2019-05-20 05:16:29 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Select Bitmap from ID's
|
|
|
|
*
|
|
|
|
* Given a buffer of select ID's, fill in a booleans (true/false) per index.
|
2019-07-07 07:38:41 +02:00
|
|
|
* #BLI_bitmap is used for memory efficiency.
|
2019-05-20 05:16:29 +02:00
|
|
|
*
|
|
|
|
* \{ */
|
|
|
|
|
2023-06-03 00:36:28 +02:00
|
|
|
uint *DRW_select_buffer_bitmap_from_rect(
|
|
|
|
Depsgraph *depsgraph, ARegion *region, View3D *v3d, const rcti *rect, uint *r_bitmap_len)
|
2019-05-18 15:12:47 +02:00
|
|
|
{
|
2023-06-03 00:36:28 +02:00
|
|
|
SELECTID_Context *select_ctx = DRW_select_engine_context_get();
|
2019-08-07 17:43:04 +02:00
|
|
|
|
2019-08-03 23:41:16 +02:00
|
|
|
rcti rect_px = *rect;
|
|
|
|
rect_px.xmax += 1;
|
|
|
|
rect_px.ymax += 1;
|
|
|
|
|
2019-05-18 15:12:47 +02:00
|
|
|
uint buf_len;
|
2020-03-06 16:56:42 +01:00
|
|
|
uint *buf = DRW_select_buffer_read(depsgraph, region, v3d, &rect_px, &buf_len);
|
2019-05-18 15:12:47 +02:00
|
|
|
if (buf == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
BLI_assert(select_ctx->index_drawn_len > 0);
|
|
|
|
const uint bitmap_len = select_ctx->index_drawn_len - 1;
|
|
|
|
|
2019-05-18 15:12:47 +02:00
|
|
|
BLI_bitmap *bitmap_buf = BLI_BITMAP_NEW(bitmap_len, __func__);
|
2019-08-07 17:43:04 +02:00
|
|
|
const uint *buf_iter = buf;
|
2019-05-18 15:12:47 +02:00
|
|
|
while (buf_len--) {
|
|
|
|
const uint index = *buf_iter - 1;
|
|
|
|
if (index < bitmap_len) {
|
|
|
|
BLI_BITMAP_ENABLE(bitmap_buf, index);
|
|
|
|
}
|
|
|
|
buf_iter++;
|
|
|
|
}
|
2019-08-07 17:43:04 +02:00
|
|
|
MEM_freeN((void *)buf);
|
2019-08-07 20:36:21 +02:00
|
|
|
|
|
|
|
if (r_bitmap_len) {
|
|
|
|
*r_bitmap_len = bitmap_len;
|
|
|
|
}
|
|
|
|
|
2019-05-18 15:12:47 +02:00
|
|
|
return bitmap_buf;
|
|
|
|
}
|
|
|
|
|
2023-06-03 00:36:28 +02:00
|
|
|
uint *DRW_select_buffer_bitmap_from_circle(Depsgraph *depsgraph,
|
|
|
|
ARegion *region,
|
|
|
|
View3D *v3d,
|
2019-08-15 15:31:54 +02:00
|
|
|
const int center[2],
|
2019-08-07 17:43:04 +02:00
|
|
|
const int radius,
|
|
|
|
uint *r_bitmap_len)
|
2019-05-18 15:12:47 +02:00
|
|
|
{
|
2023-06-03 00:36:28 +02:00
|
|
|
SELECTID_Context *select_ctx = DRW_select_engine_context_get();
|
2019-08-07 17:43:04 +02:00
|
|
|
|
2019-08-03 23:41:16 +02:00
|
|
|
const rcti rect = {
|
|
|
|
.xmin = center[0] - radius,
|
|
|
|
.xmax = center[0] + radius + 1,
|
|
|
|
.ymin = center[1] - radius,
|
|
|
|
.ymax = center[1] + radius + 1,
|
|
|
|
};
|
2019-05-18 15:12:47 +02:00
|
|
|
|
2020-03-06 16:56:42 +01:00
|
|
|
const uint *buf = DRW_select_buffer_read(depsgraph, region, v3d, &rect, NULL);
|
2019-08-05 23:02:43 +02:00
|
|
|
|
2019-05-18 15:12:47 +02:00
|
|
|
if (buf == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
BLI_assert(select_ctx->index_drawn_len > 0);
|
|
|
|
const uint bitmap_len = select_ctx->index_drawn_len - 1;
|
2019-05-18 15:12:47 +02:00
|
|
|
|
|
|
|
BLI_bitmap *bitmap_buf = BLI_BITMAP_NEW(bitmap_len, __func__);
|
2019-08-15 15:31:54 +02:00
|
|
|
const uint *buf_iter = buf;
|
2019-05-18 15:12:47 +02:00
|
|
|
const int radius_sq = radius * radius;
|
|
|
|
for (int yc = -radius; yc <= radius; yc++) {
|
|
|
|
for (int xc = -radius; xc <= radius; xc++, buf_iter++) {
|
|
|
|
if (xc * xc + yc * yc < radius_sq) {
|
|
|
|
/* Intentionally wrap to max value if this is zero. */
|
|
|
|
const uint index = *buf_iter - 1;
|
|
|
|
if (index < bitmap_len) {
|
|
|
|
BLI_BITMAP_ENABLE(bitmap_buf, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MEM_freeN((void *)buf);
|
2019-08-05 23:02:43 +02:00
|
|
|
|
|
|
|
if (r_bitmap_len) {
|
|
|
|
*r_bitmap_len = bitmap_len;
|
|
|
|
}
|
|
|
|
|
2019-05-18 15:12:47 +02:00
|
|
|
return bitmap_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PolyMaskData {
|
|
|
|
BLI_bitmap *px;
|
|
|
|
int width;
|
|
|
|
};
|
|
|
|
|
2019-08-07 17:43:04 +02:00
|
|
|
static void drw_select_mask_px_cb(int x, int x_end, int y, void *user_data)
|
2019-05-18 15:12:47 +02:00
|
|
|
{
|
|
|
|
struct PolyMaskData *data = user_data;
|
|
|
|
BLI_bitmap *px = data->px;
|
|
|
|
int i = (y * data->width) + x;
|
|
|
|
do {
|
|
|
|
BLI_BITMAP_ENABLE(px, i);
|
|
|
|
i++;
|
|
|
|
} while (++x != x_end);
|
|
|
|
}
|
|
|
|
|
2023-06-03 00:36:28 +02:00
|
|
|
uint *DRW_select_buffer_bitmap_from_poly(Depsgraph *depsgraph,
|
|
|
|
ARegion *region,
|
|
|
|
View3D *v3d,
|
2019-08-15 15:31:54 +02:00
|
|
|
const int poly[][2],
|
2023-07-24 22:06:55 +02:00
|
|
|
const int face_len,
|
2019-08-15 15:31:54 +02:00
|
|
|
const rcti *rect,
|
|
|
|
uint *r_bitmap_len)
|
2019-05-18 15:12:47 +02:00
|
|
|
{
|
2023-06-03 00:36:28 +02:00
|
|
|
SELECTID_Context *select_ctx = DRW_select_engine_context_get();
|
2019-08-07 17:43:04 +02:00
|
|
|
|
2019-08-03 23:41:16 +02:00
|
|
|
rcti rect_px = *rect;
|
|
|
|
rect_px.xmax += 1;
|
|
|
|
rect_px.ymax += 1;
|
|
|
|
|
2019-05-18 15:12:47 +02:00
|
|
|
uint buf_len;
|
2020-03-06 16:56:42 +01:00
|
|
|
uint *buf = DRW_select_buffer_read(depsgraph, region, v3d, &rect_px, &buf_len);
|
2019-05-18 15:12:47 +02:00
|
|
|
if (buf == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BLI_bitmap *buf_mask = BLI_BITMAP_NEW(buf_len, __func__);
|
2019-08-07 17:43:04 +02:00
|
|
|
|
|
|
|
struct PolyMaskData poly_mask_data;
|
2019-05-18 15:12:47 +02:00
|
|
|
poly_mask_data.px = buf_mask;
|
|
|
|
poly_mask_data.width = (rect->xmax - rect->xmin) + 1;
|
|
|
|
|
2019-08-03 23:41:16 +02:00
|
|
|
BLI_bitmap_draw_2d_poly_v2i_n(rect_px.xmin,
|
|
|
|
rect_px.ymin,
|
|
|
|
rect_px.xmax,
|
|
|
|
rect_px.ymax,
|
2019-05-18 15:12:47 +02:00
|
|
|
poly,
|
2023-07-24 22:06:55 +02:00
|
|
|
face_len,
|
2019-08-07 17:43:04 +02:00
|
|
|
drw_select_mask_px_cb,
|
2019-05-18 15:12:47 +02:00
|
|
|
&poly_mask_data);
|
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
BLI_assert(select_ctx->index_drawn_len > 0);
|
|
|
|
const uint bitmap_len = select_ctx->index_drawn_len - 1;
|
|
|
|
|
2019-05-18 15:12:47 +02:00
|
|
|
BLI_bitmap *bitmap_buf = BLI_BITMAP_NEW(bitmap_len, __func__);
|
2019-08-07 17:43:04 +02:00
|
|
|
const uint *buf_iter = buf;
|
2019-05-18 15:12:47 +02:00
|
|
|
int i = 0;
|
|
|
|
while (buf_len--) {
|
|
|
|
const uint index = *buf_iter - 1;
|
|
|
|
if (index < bitmap_len && BLI_BITMAP_TEST(buf_mask, i)) {
|
|
|
|
BLI_BITMAP_ENABLE(bitmap_buf, index);
|
|
|
|
}
|
|
|
|
buf_iter++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
MEM_freeN((void *)buf);
|
|
|
|
MEM_freeN(buf_mask);
|
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
if (r_bitmap_len) {
|
|
|
|
*r_bitmap_len = bitmap_len;
|
|
|
|
}
|
|
|
|
|
2019-05-18 15:12:47 +02:00
|
|
|
return bitmap_buf;
|
|
|
|
}
|
2019-05-20 05:16:29 +02:00
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Find Single Select ID's
|
|
|
|
*
|
|
|
|
* Given a buffer of select ID's, find the a single select id.
|
|
|
|
*
|
|
|
|
* \{ */
|
|
|
|
|
2023-06-03 00:36:28 +02:00
|
|
|
uint DRW_select_buffer_sample_point(Depsgraph *depsgraph,
|
|
|
|
ARegion *region,
|
|
|
|
View3D *v3d,
|
2019-08-15 15:31:54 +02:00
|
|
|
const int center[2])
|
2019-05-20 05:16:29 +02:00
|
|
|
{
|
2019-08-15 15:31:54 +02:00
|
|
|
uint ret = 0;
|
|
|
|
|
2019-08-03 23:41:16 +02:00
|
|
|
const rcti rect = {
|
|
|
|
.xmin = center[0],
|
|
|
|
.xmax = center[0] + 1,
|
|
|
|
.ymin = center[1],
|
|
|
|
.ymax = center[1] + 1,
|
|
|
|
};
|
|
|
|
|
2019-05-20 05:16:29 +02:00
|
|
|
uint buf_len;
|
2020-03-06 16:56:42 +01:00
|
|
|
uint *buf = DRW_select_buffer_read(depsgraph, region, v3d, &rect, &buf_len);
|
2019-08-15 15:31:54 +02:00
|
|
|
if (buf) {
|
|
|
|
BLI_assert(0 != buf_len);
|
|
|
|
ret = buf[0];
|
|
|
|
MEM_freeN(buf);
|
|
|
|
}
|
|
|
|
|
2019-05-20 05:16:29 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-09 20:02:40 +01:00
|
|
|
struct SelectReadData {
|
|
|
|
const void *val_ptr;
|
|
|
|
uint id_min;
|
|
|
|
uint id_max;
|
|
|
|
uint r_index;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool select_buffer_test_fn(const void *__restrict value, void *__restrict userdata)
|
|
|
|
{
|
|
|
|
struct SelectReadData *data = userdata;
|
|
|
|
uint hit_id = *(uint *)value;
|
|
|
|
if (hit_id && hit_id >= data->id_min && hit_id < data->id_max) {
|
|
|
|
/* Start at 1 to confirm. */
|
|
|
|
data->val_ptr = value;
|
|
|
|
data->r_index = (hit_id - data->id_min) + 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-03 00:36:28 +02:00
|
|
|
uint DRW_select_buffer_find_nearest_to_point(Depsgraph *depsgraph,
|
|
|
|
ARegion *region,
|
|
|
|
View3D *v3d,
|
2019-08-15 15:31:54 +02:00
|
|
|
const int center[2],
|
2019-08-07 17:43:04 +02:00
|
|
|
const uint id_min,
|
|
|
|
const uint id_max,
|
|
|
|
uint *dist)
|
2019-05-20 05:16:29 +02:00
|
|
|
{
|
|
|
|
/* Create region around center (typically the mouse cursor).
|
2021-03-09 20:02:40 +01:00
|
|
|
* This must be square and have an odd width. */
|
2019-08-05 23:02:43 +02:00
|
|
|
|
2019-05-20 05:16:29 +02:00
|
|
|
rcti rect;
|
|
|
|
BLI_rcti_init_pt_radius(&rect, center, *dist);
|
|
|
|
rect.xmax += 1;
|
|
|
|
rect.ymax += 1;
|
|
|
|
|
|
|
|
int width = BLI_rcti_size_x(&rect);
|
|
|
|
int height = width;
|
|
|
|
|
|
|
|
/* Read from selection framebuffer. */
|
|
|
|
|
|
|
|
uint buf_len;
|
2020-03-06 16:56:42 +01:00
|
|
|
const uint *buf = DRW_select_buffer_read(depsgraph, region, v3d, &rect, &buf_len);
|
2019-08-05 23:02:43 +02:00
|
|
|
|
|
|
|
if (buf == NULL) {
|
2021-03-09 20:02:40 +01:00
|
|
|
return 0;
|
2019-08-05 23:02:43 +02:00
|
|
|
}
|
|
|
|
|
2021-03-09 20:02:40 +01:00
|
|
|
const int shape[2] = {height, width};
|
|
|
|
const int center_yx[2] = {(height - 1) / 2, (width - 1) / 2};
|
|
|
|
struct SelectReadData data = {NULL, id_min, id_max, 0};
|
|
|
|
BLI_array_iter_spiral_square(buf, shape, center_yx, select_buffer_test_fn, &data);
|
2019-05-20 05:16:29 +02:00
|
|
|
|
2021-03-09 20:02:40 +01:00
|
|
|
if (data.val_ptr) {
|
|
|
|
size_t offset = ((size_t)data.val_ptr - (size_t)buf) / sizeof(*buf);
|
|
|
|
int hit_x = offset % width;
|
|
|
|
int hit_y = offset / width;
|
|
|
|
*dist = (uint)(abs(hit_y - center_yx[0]) + abs(hit_x - center_yx[1]));
|
2019-05-20 05:16:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MEM_freeN((void *)buf);
|
2021-03-09 20:02:40 +01:00
|
|
|
return data.r_index;
|
2019-05-20 05:16:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** \} */
|
2019-08-07 17:43:04 +02:00
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Object Utils
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
bool DRW_select_buffer_elem_get(const uint sel_id,
|
|
|
|
uint *r_elem,
|
|
|
|
uint *r_base_index,
|
|
|
|
char *r_elem_type)
|
|
|
|
{
|
2023-06-03 00:36:28 +02:00
|
|
|
SELECTID_Context *select_ctx = DRW_select_engine_context_get();
|
2019-08-07 17:43:04 +02:00
|
|
|
|
|
|
|
char elem_type = 0;
|
2019-08-15 15:31:54 +02:00
|
|
|
uint elem_id = 0;
|
2019-08-07 17:43:04 +02:00
|
|
|
uint base_index = 0;
|
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
for (; base_index < select_ctx->objects_drawn_len; base_index++) {
|
2019-08-12 17:29:18 +02:00
|
|
|
struct ObjectOffsets *base_ofs = &select_ctx->index_offsets[base_index];
|
2019-08-07 17:43:04 +02:00
|
|
|
|
|
|
|
if (base_ofs->face > sel_id) {
|
|
|
|
elem_id = sel_id - base_ofs->face_start;
|
|
|
|
elem_type = SCE_SELECT_FACE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (base_ofs->edge > sel_id) {
|
|
|
|
elem_id = sel_id - base_ofs->edge_start;
|
|
|
|
elem_type = SCE_SELECT_EDGE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (base_ofs->vert > sel_id) {
|
|
|
|
elem_id = sel_id - base_ofs->vert_start;
|
|
|
|
elem_type = SCE_SELECT_VERTEX;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 22:08:38 +02:00
|
|
|
if (base_index == select_ctx->objects_drawn_len) {
|
2019-08-07 17:43:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*r_elem = elem_id;
|
|
|
|
|
|
|
|
if (r_base_index) {
|
2019-08-15 15:31:54 +02:00
|
|
|
Object *obj_orig = DEG_get_original_object(select_ctx->objects_drawn[base_index]);
|
|
|
|
*r_base_index = obj_orig->runtime.select_id;
|
2019-08-07 17:43:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r_elem_type) {
|
|
|
|
*r_elem_type = elem_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
uint DRW_select_buffer_context_offset_for_object_elem(Depsgraph *depsgraph,
|
|
|
|
Object *object,
|
|
|
|
char elem_type)
|
2019-08-07 17:43:04 +02:00
|
|
|
{
|
2023-06-03 00:36:28 +02:00
|
|
|
SELECTID_Context *select_ctx = DRW_select_engine_context_get();
|
2019-08-15 15:31:54 +02:00
|
|
|
|
|
|
|
Object *ob_eval = DEG_get_evaluated_object(depsgraph, object);
|
|
|
|
|
|
|
|
SELECTID_ObjectData *sel_data = (SELECTID_ObjectData *)DRW_drawdata_get(
|
|
|
|
&ob_eval->id, &draw_engine_select_type);
|
|
|
|
|
|
|
|
if (!sel_data || !sel_data->is_drawn) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ObjectOffsets *base_ofs = &select_ctx->index_offsets[sel_data->drawn_index];
|
2019-08-07 17:43:04 +02:00
|
|
|
|
|
|
|
if (elem_type == SCE_SELECT_VERTEX) {
|
2019-08-15 15:31:54 +02:00
|
|
|
return base_ofs->vert_start;
|
2019-08-07 17:43:04 +02:00
|
|
|
}
|
|
|
|
if (elem_type == SCE_SELECT_EDGE) {
|
2019-08-15 15:31:54 +02:00
|
|
|
return base_ofs->edge_start;
|
2019-08-07 17:43:04 +02:00
|
|
|
}
|
|
|
|
if (elem_type == SCE_SELECT_FACE) {
|
2019-08-15 15:31:54 +02:00
|
|
|
return base_ofs->face_start;
|
2019-08-07 17:43:04 +02:00
|
|
|
}
|
|
|
|
BLI_assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Context
|
|
|
|
* \{ */
|
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
void DRW_select_buffer_context_create(Base **bases, const uint bases_len, short select_mode)
|
2019-08-07 17:43:04 +02:00
|
|
|
{
|
2023-06-03 00:36:28 +02:00
|
|
|
SELECTID_Context *select_ctx = DRW_select_engine_context_get();
|
2019-08-07 17:43:04 +02:00
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
select_ctx->objects = MEM_reallocN(select_ctx->objects,
|
|
|
|
sizeof(*select_ctx->objects) * bases_len);
|
2019-08-07 17:43:04 +02:00
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
select_ctx->index_offsets = MEM_reallocN(select_ctx->index_offsets,
|
|
|
|
sizeof(*select_ctx->index_offsets) * bases_len);
|
2019-08-07 17:43:04 +02:00
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
select_ctx->objects_drawn = MEM_reallocN(select_ctx->objects_drawn,
|
|
|
|
sizeof(*select_ctx->objects_drawn) * bases_len);
|
2019-08-07 17:43:04 +02:00
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
for (uint base_index = 0; base_index < bases_len; base_index++) {
|
|
|
|
Object *obj = bases[base_index]->object;
|
|
|
|
select_ctx->objects[base_index] = obj;
|
2019-08-07 17:43:04 +02:00
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
/* Weak but necessary for `DRW_select_buffer_elem_get`. */
|
|
|
|
obj->runtime.select_id = base_index;
|
|
|
|
}
|
2019-08-07 17:43:04 +02:00
|
|
|
|
2019-08-15 15:31:54 +02:00
|
|
|
select_ctx->objects_len = bases_len;
|
|
|
|
select_ctx->select_mode = select_mode;
|
|
|
|
memset(select_ctx->persmat, 0, sizeof(select_ctx->persmat));
|
|
|
|
}
|
2021-12-14 05:49:31 +01:00
|
|
|
|
2019-08-07 17:43:04 +02:00
|
|
|
/** \} */
|