Blender  V2.93
overlay_metaball.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright 2019, Blender Foundation.
17  */
18 
23 #include "DRW_render.h"
24 
25 #include "DNA_meta_types.h"
26 
27 #include "BKE_object.h"
28 
29 #include "DEG_depsgraph_query.h"
30 
31 #include "ED_mball.h"
32 
33 #include "overlay_private.h"
34 
36 {
37  OVERLAY_PassList *psl = vedata->psl;
38  OVERLAY_PrivateData *pd = vedata->stl->pd;
39 
41 
42 #define BUF_INSTANCE DRW_shgroup_call_buffer_instance
43 
44  for (int i = 0; i < 2; i++) {
45  DRWState infront_state = (DRW_state_is_select() && (i == 1)) ? DRW_STATE_IN_FRONT_SELECT : 0;
47  DRW_PASS_CREATE(psl->metaball_ps[i], state | pd->clipping_state | infront_state);
48 
49  /* Reuse armature shader as it's perfect to outline ellipsoids. */
50  struct GPUVertFormat *format = formats->instance_bone;
51  struct GPUShader *sh = OVERLAY_shader_armature_sphere(true);
53  DRW_shgroup_uniform_block(grp, "globalsBlock", G_draw.block_ubo);
55  }
56 }
57 
59  BoneInstanceData *data, Object *ob, const float *pos, const float radius, const float color[4])
60 {
61  /* Bone point radius is 0.05. Compensate for that. */
62  mul_v3_v3fl(data->mat[0], ob->obmat[0], radius / 0.05f);
63  mul_v3_v3fl(data->mat[1], ob->obmat[1], radius / 0.05f);
64  mul_v3_v3fl(data->mat[2], ob->obmat[2], radius / 0.05f);
65  mul_v3_m4v3(data->mat[3], ob->obmat, pos);
66  /* WATCH: Reminder, alpha is wire-size. */
68 }
69 
71 {
72  const bool do_in_front = (ob->dtx & OB_DRAW_IN_FRONT) != 0;
73  const bool is_select = DRW_state_is_select();
74  OVERLAY_PrivateData *pd = vedata->stl->pd;
75  MetaBall *mb = ob->data;
76 
77  const float *color;
78  const float *col_radius = G_draw.block.colorMballRadius;
79  const float *col_radius_select = G_draw.block.colorMballRadiusSelect;
80  const float *col_stiffness = G_draw.block.colorMballStiffness;
81  const float *col_stiffness_select = G_draw.block.colorMballStiffnessSelect;
82 
83  int select_id = 0;
84  if (is_select) {
85  select_id = ob->runtime.select_id;
86  }
87 
88  LISTBASE_FOREACH (MetaElem *, ml, mb->editelems) {
89  const bool is_selected = (ml->flag & SELECT) != 0;
90  const bool is_scale_radius = (ml->flag & MB_SCALE_RAD) != 0;
91  float stiffness_radius = ml->rad * atanf(ml->s) / (float)M_PI_2;
92  BoneInstanceData instdata;
93 
94  if (is_select) {
96  }
97  color = (is_selected && is_scale_radius) ? col_radius_select : col_radius;
98  metaball_instance_data_set(&instdata, ob, &ml->x, ml->rad, color);
99  DRW_buffer_add_entry_struct(pd->mball.handle[do_in_front], &instdata);
100 
101  if (is_select) {
102  DRW_select_load_id(select_id | MBALLSEL_STIFF);
103  }
104  color = (is_selected && !is_scale_radius) ? col_stiffness_select : col_stiffness;
105  metaball_instance_data_set(&instdata, ob, &ml->x, stiffness_radius, color);
106  DRW_buffer_add_entry_struct(pd->mball.handle[do_in_front], &instdata);
107 
108  select_id += 0x10000;
109  }
110 }
111 
113 {
114  const bool do_in_front = (ob->dtx & OB_DRAW_IN_FRONT) != 0;
115  OVERLAY_PrivateData *pd = vedata->stl->pd;
116  MetaBall *mb = ob->data;
117  const DRWContextState *draw_ctx = DRW_context_state_get();
118 
119  float *color;
120  DRW_object_wire_theme_get(ob, draw_ctx->view_layer, &color);
121 
122  LISTBASE_FOREACH (MetaElem *, ml, &mb->elems) {
123  /* Draw radius only. */
124  BoneInstanceData instdata;
125  metaball_instance_data_set(&instdata, ob, &ml->x, ml->rad, color);
126  DRW_buffer_add_entry_struct(pd->mball.handle[do_in_front], &instdata);
127  }
128 }
129 
131 {
132  OVERLAY_PassList *psl = vedata->psl;
133 
134  DRW_draw_pass(psl->metaball_ps[0]);
135 }
136 
138 {
139  OVERLAY_PassList *psl = vedata->psl;
140 
141  DRW_draw_pass(psl->metaball_ps[1]);
142 }
typedef float(TangentPoint)[2]
General operations, lookup, etc. for blender objects.
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
#define M_PI_2
Definition: BLI_math_base.h:41
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:742
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
#define MB_SCALE_RAD
@ OB_DRAW_IN_FRONT
DRWState
Definition: DRW_render.h:312
@ DRW_STATE_IN_FRONT_SELECT
Definition: DRW_render.h:352
@ DRW_STATE_WRITE_DEPTH
Definition: DRW_render.h:314
@ DRW_STATE_WRITE_COLOR
Definition: DRW_render.h:315
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition: DRW_render.h:323
#define DRW_PASS_CREATE(pass, state)
Definition: DRW_render.h:593
#define MBALLSEL_STIFF
Definition: ED_mball.h:60
#define MBALLSEL_RADIUS
Definition: ED_mball.h:61
struct GPUShader GPUShader
Definition: GPU_shader.h:33
#define SELECT
GPUBatch * DRW_cache_bone_point_wire_outline_get(void)
Definition: draw_cache.c:2328
int DRW_object_wire_theme_get(Object *ob, ViewLayer *view_layer, float **r_color)
Definition: draw_common.c:299
struct DRW_Global G_draw
Definition: draw_common.c:45
bool DRW_state_is_select(void)
const DRWContextState * DRW_context_state_get(void)
void DRW_shgroup_uniform_block(DRWShadingGroup *shgroup, const char *name, const GPUUniformBuf *ubo)
void DRW_buffer_add_entry_struct(DRWCallBuffer *callbuf, const void *data)
DRWShadingGroup * DRW_shgroup_create(struct GPUShader *shader, DRWPass *pass)
void DRW_draw_pass(DRWPass *pass)
void DRW_select_load_id(uint id)
uint pos
#define atanf(x)
format
Definition: logImageCore.h:47
static ulong state[N]
void OVERLAY_bone_instance_data_set_color(BoneInstanceData *data, const float bone_color[4])
void OVERLAY_metaball_cache_populate(OVERLAY_Data *vedata, Object *ob)
#define BUF_INSTANCE
void OVERLAY_metaball_in_front_draw(OVERLAY_Data *vedata)
void OVERLAY_edit_metaball_cache_populate(OVERLAY_Data *vedata, Object *ob)
static void metaball_instance_data_set(BoneInstanceData *data, Object *ob, const float *pos, const float radius, const float color[4])
void OVERLAY_metaball_draw(OVERLAY_Data *vedata)
void OVERLAY_metaball_cache_init(OVERLAY_Data *vedata)
GPUShader * OVERLAY_shader_armature_sphere(bool use_outline)
OVERLAY_InstanceFormats * OVERLAY_shader_instance_formats_get(void)
struct ViewLayer * view_layer
Definition: DRW_render.h:746
GlobalsUboStorage block
Definition: draw_common.h:208
struct GPUUniformBuf * block_ubo
Definition: draw_common.h:210
float colorMballRadius[4]
Definition: draw_common.h:126
float colorMballRadiusSelect[4]
Definition: draw_common.h:127
float colorMballStiffness[4]
Definition: draw_common.h:128
float colorMballStiffnessSelect[4]
Definition: draw_common.h:129
ListBase elems
ListBase * editelems
OVERLAY_PassList * psl
OVERLAY_StorageList * stl
struct GPUVertFormat * instance_bone
DRWPass * metaball_ps[2]
struct OVERLAY_PrivateData::@247 mball
DRWCallBuffer * handle[2]
struct OVERLAY_PrivateData * pd
Object_Runtime runtime
float obmat[4][4]
void * data