Blender  V3.3
select_draw_utils.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2019 Blender Foundation. */
3 
10 #include "BKE_editmesh.h"
11 #include "BKE_mesh.h"
12 #include "BKE_object.h"
13 
14 #include "DNA_mesh_types.h"
15 #include "DNA_scene_types.h"
16 
17 #include "ED_view3d.h"
18 
19 #include "DEG_depsgraph.h"
20 #include "DEG_depsgraph_query.h"
21 
22 #include "DRW_select_buffer.h"
23 
24 #include "draw_cache_impl.h"
25 
26 #include "select_private.h"
27 
28 /* -------------------------------------------------------------------- */
32 void select_id_object_min_max(Object *obj, float r_min[3], float r_max[3])
33 {
34  const BoundBox *bb;
36  if (em) {
37  bb = BKE_editmesh_cage_boundbox_get(obj, em);
38  }
39  else {
40  bb = BKE_object_boundbox_get(obj);
41  }
42  copy_v3_v3(r_min, bb->vec[0]);
43  copy_v3_v3(r_max, bb->vec[6]);
44 }
45 
47 {
48  short r_select_mode = 0;
50  /* In order to sample flat colors for vertex weights / texture-paint / vertex-paint
51  * we need to be in SCE_SELECT_FACE mode so select_cache_init() correctly sets up
52  * a shgroup with select_id_flat.
53  * Note this is not working correctly for vertex-paint (yet), but has been discussed
54  * in T66645 and there is a solution by @mano-wii in P1032.
55  * So OB_MODE_VERTEX_PAINT is already included here [required for P1032 I guess]. */
56  Mesh *me_orig = DEG_get_original_object(ob)->data;
57  if (me_orig->editflag & ME_EDIT_PAINT_VERT_SEL) {
58  r_select_mode = SCE_SELECT_VERTEX;
59  }
60  else {
61  r_select_mode = SCE_SELECT_FACE;
62  }
63  }
64  else {
65  r_select_mode = scene->toolsettings->selectmode;
66  }
67 
68  return r_select_mode;
69 }
70 
71 static bool check_ob_drawface_dot(short select_mode, const View3D *v3d, eDrawType dt)
72 {
73  if (select_mode & SCE_SELECT_FACE) {
74  if ((dt < OB_SOLID) || XRAY_FLAG_ENABLED(v3d)) {
75  return true;
76  }
78  return true;
79  }
80  }
81  return false;
82 }
83 
85  Object *ob,
86  short select_mode,
87  bool draw_facedot,
88  uint initial_offset,
89  uint *r_vert_offset,
90  uint *r_edge_offset,
91  uint *r_face_offset)
92 {
93  Mesh *me = ob->data;
94  BMEditMesh *em = me->edit_mesh;
95 
97 
98  if (select_mode & SCE_SELECT_FACE) {
101  DRW_shgroup_uniform_int_copy(face_shgrp, "offset", *(int *)&initial_offset);
102  DRW_shgroup_call_no_cull(face_shgrp, geom_faces, ob);
103 
104  if (draw_facedot) {
106  DRW_shgroup_call_no_cull(face_shgrp, geom_facedots, ob);
107  }
108  *r_face_offset = initial_offset + em->bm->totface;
109  }
110  else {
111  if (ob->dt >= OB_SOLID) {
112 #ifdef USE_CAGE_OCCLUSION
114 #else
115  struct GPUBatch *geom_faces = DRW_mesh_batch_cache_get_surface(me);
116 #endif
117  DRWShadingGroup *face_shgrp = stl->g_data->shgrp_face_unif;
118  DRW_shgroup_call_no_cull(face_shgrp, geom_faces, ob);
119  }
120  *r_face_offset = initial_offset;
121  }
122 
123  /* Unlike faces, only draw edges if edge select mode. */
124  if (select_mode & SCE_SELECT_EDGE) {
127  DRW_shgroup_uniform_int_copy(edge_shgrp, "offset", *(int *)r_face_offset);
128  DRW_shgroup_call_no_cull(edge_shgrp, geom_edges, ob);
129  *r_edge_offset = *r_face_offset + em->bm->totedge;
130  }
131  else {
132  /* Note that `r_vert_offset` is calculated from `r_edge_offset`.
133  * Otherwise the first vertex is never selected, see: T53512. */
134  *r_edge_offset = *r_face_offset;
135  }
136 
137  /* Unlike faces, only verts if vert select mode. */
138  if (select_mode & SCE_SELECT_VERTEX) {
141  DRW_shgroup_uniform_int_copy(vert_shgrp, "offset", *(int *)r_edge_offset);
142  DRW_shgroup_call_no_cull(vert_shgrp, geom_verts, ob);
143  *r_vert_offset = *r_edge_offset + em->bm->totvert;
144  }
145  else {
146  *r_vert_offset = *r_edge_offset;
147  }
148 }
149 
151  Object *ob,
152  short select_mode,
153  uint initial_offset,
154  uint *r_vert_offset,
155  uint *r_edge_offset,
156  uint *r_face_offset)
157 {
158  Mesh *me = ob->data;
159 
161  DRWShadingGroup *face_shgrp;
162  if (select_mode & SCE_SELECT_FACE) {
163  face_shgrp = DRW_shgroup_create_sub(stl->g_data->shgrp_face_flat);
164  DRW_shgroup_uniform_int_copy(face_shgrp, "offset", *(int *)&initial_offset);
165  *r_face_offset = initial_offset + me->totpoly;
166  }
167  else {
168  /* Only draw faces to mask out verts, we don't want their selection ID's. */
169  face_shgrp = stl->g_data->shgrp_face_unif;
170  *r_face_offset = initial_offset;
171  }
172  DRW_shgroup_call_no_cull(face_shgrp, geom_faces, ob);
173 
174  if (select_mode & SCE_SELECT_EDGE) {
177  DRW_shgroup_uniform_int_copy(edge_shgrp, "offset", *(int *)r_face_offset);
178  DRW_shgroup_call_no_cull(edge_shgrp, geom_edges, ob);
179  *r_edge_offset = *r_face_offset + me->totedge;
180  }
181  else {
182  *r_edge_offset = *r_face_offset;
183  }
184 
185  if (select_mode & SCE_SELECT_VERTEX) {
188  DRW_shgroup_uniform_int_copy(vert_shgrp, "offset", *r_edge_offset);
189  DRW_shgroup_call_no_cull(vert_shgrp, geom_verts, ob);
190  *r_vert_offset = *r_edge_offset + me->totvert;
191  }
192  else {
193  *r_vert_offset = *r_edge_offset;
194  }
195 }
196 
197 void select_id_draw_object(void *vedata,
198  View3D *v3d,
199  Object *ob,
200  short select_mode,
201  uint initial_offset,
202  uint *r_vert_offset,
203  uint *r_edge_offset,
204  uint *r_face_offset)
205 {
206  SELECTID_StorageList *stl = ((SELECTID_Data *)vedata)->stl;
207 
208  BLI_assert(initial_offset > 0);
209 
210  switch (ob->type) {
211  case OB_MESH:
212  if (ob->mode & OB_MODE_EDIT) {
213  bool draw_facedot = check_ob_drawface_dot(select_mode, v3d, ob->dt);
215  ob,
216  select_mode,
217  draw_facedot,
218  initial_offset,
219  r_vert_offset,
220  r_edge_offset,
221  r_face_offset);
222  }
223  else {
225  stl, ob, select_mode, initial_offset, r_vert_offset, r_edge_offset, r_face_offset);
226  }
227  break;
228  case OB_CURVES_LEGACY:
229  case OB_SURF:
230  break;
231  }
232 }
233 
236 #undef SELECT_ENGINE
struct BoundBox * BKE_editmesh_cage_boundbox_get(struct Object *object, BMEditMesh *em)
BMEditMesh * BKE_editmesh_from_object(struct Object *ob)
Return the BMEditMesh for a given object.
Definition: editmesh.c:58
General operations, lookup, etc. for blender objects.
const struct BoundBox * BKE_object_boundbox_get(struct Object *ob)
Definition: object.cc:3684
#define BLI_assert(a)
Definition: BLI_assert.h:46
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition: BLI_sys_types.h:67
struct Object * DEG_get_original_object(struct Object *object)
@ ME_EDIT_PAINT_VERT_SEL
eDrawType
@ OB_SOLID
@ OB_MODE_EDIT
@ OB_MODE_WEIGHT_PAINT
@ OB_MODE_TEXTURE_PAINT
@ OB_MODE_VERTEX_PAINT
@ OB_SURF
@ OB_MESH
@ OB_CURVES_LEGACY
#define SCE_SELECT_FACE
#define SCE_SELECT_VERTEX
#define SCE_SELECT_EDGE
@ V3D_OVERLAY_EDIT_FACE_DOT
#define DRW_shgroup_call_no_cull(shgroup, geom, ob)
Definition: DRW_render.h:431
#define XRAY_FLAG_ENABLED(v3d)
Definition: ED_view3d.h:1298
GPUBatch
Definition: GPU_batch.h:78
@ BM_FACE
Definition: bmesh_class.h:386
@ BM_VERT
Definition: bmesh_class.h:383
@ BM_EDGE
Definition: bmesh_class.h:384
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
Definition: bmesh_mesh.cc:558
Scene scene
struct GPUBatch * DRW_mesh_batch_cache_get_triangles_with_select_id(struct Mesh *me)
struct GPUBatch * DRW_mesh_batch_cache_get_edges_with_select_id(struct Mesh *me)
struct GPUBatch * DRW_mesh_batch_cache_get_facedots_with_select_id(struct Mesh *me)
struct GPUBatch * DRW_mesh_batch_cache_get_verts_with_select_id(struct Mesh *me)
struct GPUBatch * DRW_mesh_batch_cache_get_surface(struct Mesh *me)
void DRW_shgroup_uniform_int_copy(DRWShadingGroup *shgroup, const char *name, const int value)
DRWShadingGroup * DRW_shgroup_create_sub(DRWShadingGroup *shgroup)
static bool check_ob_drawface_dot(short select_mode, const View3D *v3d, eDrawType dt)
short select_id_get_object_select_mode(Scene *scene, Object *ob)
static void draw_select_id_edit_mesh(SELECTID_StorageList *stl, Object *ob, short select_mode, bool draw_facedot, uint initial_offset, uint *r_vert_offset, uint *r_edge_offset, uint *r_face_offset)
void select_id_object_min_max(Object *obj, float r_min[3], float r_max[3])
void select_id_draw_object(void *vedata, View3D *v3d, Object *ob, short select_mode, uint initial_offset, uint *r_vert_offset, uint *r_edge_offset, uint *r_face_offset)
static void draw_select_id_mesh(SELECTID_StorageList *stl, Object *ob, short select_mode, uint initial_offset, uint *r_vert_offset, uint *r_edge_offset, uint *r_face_offset)
struct BMesh * bm
Definition: BKE_editmesh.h:40
int totvert
Definition: bmesh_class.h:297
int totedge
Definition: bmesh_class.h:297
int totface
Definition: bmesh_class.h:297
float vec[8][3]
struct BMEditMesh * edit_mesh
int totedge
char editflag
int totvert
int totpoly
void * data
DRWShadingGroup * shgrp_face_unif
DRWShadingGroup * shgrp_edge
DRWShadingGroup * shgrp_face_flat
DRWShadingGroup * shgrp_vert
struct SELECTID_PrivateData * g_data
struct ToolSettings * toolsettings
View3DOverlay overlay