Blender V5.3
overlay_relation.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BKE_constraint.h"
15#include "DNA_modifier_types.h"
16#include "DNA_rigidbody_types.h"
17
18#include "overlay_base.hh"
19
20namespace blender::draw::overlay {
21
27
28 private:
29 PassSimple ps_ = {"Relations"};
30
31 LinePrimitiveBuf relations_buf_;
32 PointPrimitiveBuf points_buf_;
33
34 public:
35 Relations(SelectionType selection_type)
36 : relations_buf_(selection_type, "relations_buf_"),
37 points_buf_(selection_type, "points_buf_")
38 {
39 }
40
41 void begin_sync(Resources &res, const State &state) final
42 {
43 enabled_ = state.is_space_v3d();
44 enabled_ &= (state.v3d_flag & V3D_HIDE_HELPLINES) == 0;
45 enabled_ &= !res.is_selection();
46
47 points_buf_.clear();
48 relations_buf_.clear();
49 }
50
51 void object_sync(Manager & /*manager*/,
52 const ObjectRef &ob_ref,
53 Resources &res,
54 const State &state) final
55 {
56 if (!enabled_) {
57 return;
58 }
59
60 /* Don't show object extras in set's. */
61 if (is_from_dupli_or_set(ob_ref)) {
62 return;
63 }
64
65 Object *ob = ob_ref.object;
66 const float4 &relation_color = res.theme.colors.wire;
67 /* TODO (not_mark): pick literally anything else that's sensible. */
68 const float4 &constraint_color = res.theme.colors.grid_axis_z; /* ? */
69
71 const float3 &parent_pos = ob->runtime->parent_display_origin;
72 /* Reverse order to have less stipple overlap. */
73 relations_buf_.append(ob->object_to_world().location(), parent_pos, relation_color);
74 }
75
76 /* Drawing the hook lines. */
78 if (md->type == eModifierType_Hook) {
79 HookModifierData *hmd = reinterpret_cast<HookModifierData *>(md);
80 const float3 center = math::transform_point(ob->object_to_world(), float3(hmd->cent));
81 if (hmd->object) {
82 relations_buf_.append(hmd->object->object_to_world().location(), center, relation_color);
83 }
84 points_buf_.append(center, relation_color);
85 }
86 }
87
88 for (GpencilModifierData *md :
90 {
91 if (md->type == eGpencilModifierType_Hook) {
92 HookGpencilModifierData *hmd = reinterpret_cast<HookGpencilModifierData *>(md);
93 const float3 center = math::transform_point(ob->object_to_world(), float3(hmd->cent));
94 if (hmd->object) {
95 relations_buf_.append(hmd->object->object_to_world().location(), center, relation_color);
96 }
97 points_buf_.append(center, relation_color);
98 }
99 }
100
101 if (ob->rigidbody_constraint) {
102 Object *rbc_ob1 = ob->rigidbody_constraint->ob1;
103 Object *rbc_ob2 = ob->rigidbody_constraint->ob2;
104 if (rbc_ob1 && (DRW_object_visibility_in_active_context(rbc_ob1) & OB_VISIBLE_SELF)) {
105
106 relations_buf_.append(rbc_ob1->object_to_world().location(),
107 ob->object_to_world().location(),
108 relation_color);
109 }
110 if (rbc_ob2 && (DRW_object_visibility_in_active_context(rbc_ob2) & OB_VISIBLE_SELF)) {
111 relations_buf_.append(rbc_ob2->object_to_world().location(),
112 ob->object_to_world().location(),
113 relation_color);
114 }
115 }
116
117 /* Drawing the constraint lines */
118 if (!ob->constraints.is_empty()) {
119 Scene *scene = const_cast<Scene *>(state.scene);
121 state.depsgraph, scene, ob, nullptr, CONSTRAINT_OBTYPE_OBJECT);
122
123 for (bConstraint *constraint : ListBaseWrapper<bConstraint>(ob->constraints)) {
125 /* special case for object solver and follow track constraints because they don't fill
126 * constraint targets properly (design limitation -- scene is needed for their target
127 * but it can't be accessed from get_targets callback) */
128 Object *camob = nullptr;
129
130 if (constraint->type == CONSTRAINT_TYPE_FOLLOWTRACK) {
131 bFollowTrackConstraint *data = static_cast<bFollowTrackConstraint *>(constraint->data);
132 camob = data->camera ? data->camera : scene->camera;
133 }
134 else if (constraint->type == CONSTRAINT_TYPE_OBJECTSOLVER) {
136 constraint->data);
137 camob = data->camera ? data->camera : scene->camera;
138 }
139
140 if (camob) {
141 relations_buf_.append(camob->object_to_world().location(),
142 ob->object_to_world().location(),
143 constraint_color);
144 }
145 }
146 else {
147 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(constraint);
148 ListBaseT<bConstraintTarget> targets = {nullptr, nullptr};
149
150 if ((constraint->ui_expand_flag & (1 << 0)) &&
151 BKE_constraint_targets_get(constraint, &targets))
152 {
154
156 /* Calculate target's position. */
157 float3 target_pos = float3(0.0f);
158 bool has_target = false;
159 if (target->flag & CONSTRAINT_TAR_CUSTOM_SPACE) {
160 target_pos = cob->space_obj_world_matrix[3];
161 has_target = true;
162 }
163 else if (cti->get_target_matrix &&
164 cti->get_target_matrix(state.depsgraph,
165 constraint,
166 cob,
167 target,
168 DEG_get_ctime(state.depsgraph)))
169 {
170 has_target = true;
171 target_pos = target->matrix[3];
172 }
173
174 if (has_target) {
175 /* Only draw this relationship line when there is actually a target. Otherwise it
176 * would always draw to the world origin, which is visually rather noisy and not
177 * that useful. */
178 relations_buf_.append(
179 target_pos, ob->object_to_world().location(), constraint_color);
180 }
181 }
182
183 BKE_constraint_targets_flush(constraint, &targets, true);
184 }
185 }
186 }
187 /* NOTE: Don't use #BKE_constraints_clear_evalob here as that will reset `ob->constinv`.
188 */
189 MEM_delete(cob);
190 }
191 }
192
193 void end_sync(Resources &res, const State &state) final
194 {
195 if (!enabled_) {
196 return;
197 }
198
199 ps_.init();
200 ps_.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
201 ps_.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
202 res.select_bind(ps_);
203 {
204 PassSimple::Sub &sub_pass = ps_.sub("lines");
207 state.clipping_plane_count);
208 sub_pass.shader_set(res.shaders->extra_wire.get());
209 relations_buf_.end_sync(sub_pass);
210 }
211 {
212 PassSimple::Sub &sub_pass = ps_.sub("loose_points");
215 state.clipping_plane_count);
216 sub_pass.shader_set(res.shaders->extra_loose_points.get());
217 points_buf_.end_sync(sub_pass);
218 }
219 }
220
221 void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
222 {
223 if (!enabled_) {
224 return;
225 }
226
227 GPU_framebuffer_bind(framebuffer);
228 manager.submit(ps_, view);
229 }
230};
231
232} // namespace blender::draw::overlay
Types and defines for representing Rigid Body entities.
#define ELEM(...)
void shader_set(gpu::Shader *shader)
void state_set(DRWState state, int clip_plane_count=0)
detail::PassBase< command::DrawCommandBuf > Sub
Definition draw_pass.hh:509
void begin_sync(Resources &res, const State &state) final
void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
void object_sync(Manager &, const ObjectRef &ob_ref, Resources &res, const State &state) final
Relations(SelectionType selection_type)
void end_sync(Resources &res, const State &state) final
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
const int state
select::SelectionType SelectionType
detail::Pass< command::DrawCommandBuf > PassSimple
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
void BKE_constraint_targets_flush(struct bConstraint *con, ListBaseT< bConstraintTarget > *targets, bool no_copy)
struct bConstraintOb * BKE_constraints_make_evalob(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, void *subdata, short datatype)
int DRW_object_visibility_in_active_context(const Object *ob)
const bConstraintTypeInfo * BKE_constraint_typeinfo_get(struct bConstraint *con)
void BKE_constraint_custom_object_space_init(struct bConstraintOb *cob, struct bConstraint *con)
ListBaseWrapperTemplate< ListBase, T > ListBaseWrapper
BMesh const char void * data
int BKE_constraint_targets_get(struct bConstraint *con, ListBaseT< bConstraintTarget > *r_targets)
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:40
float DEG_get_ctime(const Depsgraph *graph)
@ OB_VISIBLE_SELF
struct Object * parent
ListBaseT< ModifierData > modifiers
bke::ObjectRuntime * runtime
ListBaseT< GpencilModifierData > greasepencil_modifiers
ListBaseT< bConstraint > constraints
struct RigidBodyCon * rigidbody_constraint
struct Object * camera
float space_obj_world_matrix[4][4]
bool(*) get_target_matrix(struct Depsgraph *depsgraph, struct bConstraint *con, struct bConstraintOb *cob, struct bConstraintTarget *ct, float ctime)