Blender  V3.3
drawobject.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
3 
8 #include "DNA_mesh_types.h"
9 #include "DNA_meshdata_types.h"
10 #include "DNA_object_types.h"
11 #include "DNA_scene_types.h"
12 
13 #include "BLI_math_vector.h"
14 
15 #include "BKE_DerivedMesh.h"
16 #include "BKE_customdata.h"
17 #include "BKE_editmesh.h"
18 #include "BKE_global.h"
19 #include "BKE_object.h"
20 
21 #include "DEG_depsgraph.h"
22 #include "DEG_depsgraph_query.h"
23 
24 #include "GPU_batch.h"
25 #include "GPU_immediate.h"
26 #include "GPU_shader.h"
27 #include "GPU_state.h"
28 
29 #include "ED_mesh.h"
30 
31 #include "UI_resources.h"
32 
33 #include "DRW_engine.h"
34 
35 #include "view3d_intern.h" /* bad level include */
36 
37 #ifdef VIEW3D_CAMERA_BORDER_HACK
40 #endif
41 
42 /* ***************** BACKBUF SEL (BBS) ********* */
43 
45  Object *ob,
46  const float col[4],
47  const int facemap)
48 {
49  /* happens on undo */
50  if (ob->type != OB_MESH || !ob->data) {
51  return;
52  }
53 
54  const Mesh *me = ob->data;
55  {
57  const Mesh *me_eval = BKE_object_get_evaluated_mesh(ob_eval);
58  if (me_eval != NULL) {
59  me = me_eval;
60  }
61  }
62 
64 
65  /* Just to create the data to pass to immediate mode! (sigh) */
66  const int *facemap_data = CustomData_get_layer(&me->pdata, CD_FACEMAP);
67  if (facemap_data) {
69 
70  const MVert *mvert = me->mvert;
71  const MPoly *mpoly = me->mpoly;
72  const MLoop *mloop = me->mloop;
73 
74  int mpoly_len = me->totpoly;
75  int mloop_len = me->totloop;
76 
77  facemap_data = CustomData_get_layer(&me->pdata, CD_FACEMAP);
78 
79  /* Make a batch and free it each time for now. */
80  const int looptris_len = poly_to_tri_count(mpoly_len, mloop_len);
81  const int vbo_len_capacity = looptris_len * 3;
82  int vbo_len_used = 0;
83 
84  GPUVertFormat format_pos = {0};
85  const uint pos_id = GPU_vertformat_attr_add(
86  &format_pos, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
87 
88  GPUVertBuf *vbo_pos = GPU_vertbuf_create_with_format(&format_pos);
89  GPU_vertbuf_data_alloc(vbo_pos, vbo_len_capacity);
90 
91  GPUVertBufRaw pos_step;
92  GPU_vertbuf_attr_get_raw_data(vbo_pos, pos_id, &pos_step);
93 
94  const MPoly *mp;
95  int i;
96  if (me->runtime.looptris.array) {
97  const MLoopTri *mlt = me->runtime.looptris.array;
98  for (mp = mpoly, i = 0; i < mpoly_len; i++, mp++) {
99  if (facemap_data[i] == facemap) {
100  for (int j = 2; j < mp->totloop; j++) {
101  copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[mloop[mlt->tri[0]].v].co);
102  copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[mloop[mlt->tri[1]].v].co);
103  copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[mloop[mlt->tri[2]].v].co);
104  vbo_len_used += 3;
105  mlt++;
106  }
107  }
108  else {
109  mlt += mp->totloop - 2;
110  }
111  }
112  }
113  else {
114  /* No tessellation data, fan-fill. */
115  for (mp = mpoly, i = 0; i < mpoly_len; i++, mp++) {
116  if (facemap_data[i] == facemap) {
117  const MLoop *ml_start = &mloop[mp->loopstart];
118  const MLoop *ml_a = ml_start + 1;
119  const MLoop *ml_b = ml_start + 2;
120  for (int j = 2; j < mp->totloop; j++) {
121  copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[ml_start->v].co);
122  copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[ml_a->v].co);
123  copy_v3_v3(GPU_vertbuf_raw_step(&pos_step), mvert[ml_b->v].co);
124  vbo_len_used += 3;
125 
126  ml_a++;
127  ml_b++;
128  }
129  }
130  }
131  }
132 
133  if (vbo_len_capacity != vbo_len_used) {
134  GPU_vertbuf_data_resize(vbo_pos, vbo_len_used);
135  }
136 
137  GPUBatch *draw_batch = GPU_batch_create(GPU_PRIM_TRIS, vbo_pos, NULL);
139  GPU_batch_uniform_4fv(draw_batch, "color", col);
140  GPU_batch_draw(draw_batch);
141  GPU_batch_discard(draw_batch);
142  GPU_vertbuf_discard(vbo_pos);
143 
145  }
146 }
CustomData interface, see also DNA_customdata_types.h.
void * CustomData_get_layer(const struct CustomData *data, int type)
General operations, lookup, etc. for blender objects.
struct Mesh * BKE_object_get_evaluated_mesh(const struct Object *object)
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned char uchar
Definition: BLI_sys_types.h:70
unsigned int uint
Definition: BLI_sys_types.h:67
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
struct Object * DEG_get_evaluated_object(const struct Depsgraph *depsgraph, struct Object *object)
@ CD_FACEMAP
Object is a sort of wrapper for general info.
@ OB_MESH
@ OB_NEG_SCALE
GPUBatch
Definition: GPU_batch.h:78
void GPU_batch_discard(GPUBatch *)
Definition: gpu_batch.cc:109
#define GPU_batch_create(prim, verts, elem)
Definition: GPU_batch.h:95
void GPU_batch_program_set_builtin(GPUBatch *batch, eGPUBuiltinShader shader_id)
Definition: gpu_batch.cc:287
void GPU_batch_draw(GPUBatch *batch)
Definition: gpu_batch.cc:223
#define GPU_batch_uniform_4fv(batch, name, val)
Definition: GPU_batch.h:152
@ GPU_PRIM_TRIS
Definition: GPU_primitive.h:21
@ GPU_SHADER_3D_UNIFORM_COLOR
Definition: GPU_shader.h:230
@ GPU_BLEND_NONE
Definition: GPU_state.h:60
@ GPU_BLEND_ALPHA
Definition: GPU_state.h:62
void GPU_blend(eGPUBlend blend)
Definition: gpu_state.cc:39
void GPU_front_facing(bool invert)
Definition: gpu_state.cc:55
#define GPU_vertbuf_create_with_format(format)
void GPU_vertbuf_discard(GPUVertBuf *)
struct GPUVertBuf GPUVertBuf
void GPU_vertbuf_data_alloc(GPUVertBuf *, uint v_len)
GPU_INLINE void * GPU_vertbuf_raw_step(GPUVertBufRaw *a)
void GPU_vertbuf_attr_get_raw_data(GPUVertBuf *, uint a_idx, GPUVertBufRaw *access)
void GPU_vertbuf_data_resize(GPUVertBuf *, uint v_len)
@ GPU_FETCH_FLOAT
uint GPU_vertformat_attr_add(GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_F32
const int facemap[6][4]
Definition: Projections.cpp:43
const Depsgraph * depsgraph
void ED_draw_object_facemap(Depsgraph *depsgraph, Object *ob, const float col[4], const int facemap)
Definition: drawobject.c:44
uchar view3d_camera_border_hack_col[3]
Definition: drawobject.c:38
bool view3d_camera_border_hack_test
Definition: drawobject.c:39
uint col
struct MLoopTri * array
unsigned int tri[3]
unsigned int v
float co[3]
struct MLoopTri_Store looptris
struct MVert * mvert
struct MLoop * mloop
Mesh_Runtime runtime
CustomData pdata
int totpoly
int totloop
struct MPoly * mpoly
short transflag
void * data