Blender V5.3
overlay_bounds.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_mball.hh"
12
13#include "BLI_bounds_types.hh"
14#include "BLI_utildefines.hh"
15
16#include "DNA_rigidbody_types.h"
17
18#include "overlay_base.hh"
19
20namespace blender::draw::overlay {
21
33class Bounds : Overlay {
34 using BoundsInstanceBuf = ShapeInstanceBuf<ExtraInstanceData>;
35
36 private:
37 PassSimple ps_ = {"Bounds"};
38
39 struct CallBuffers {
40 const SelectionType selection_type_;
41
42 BoundsInstanceBuf box = {selection_type_, "bound_box"};
43 BoundsInstanceBuf sphere = {selection_type_, "bound_sphere"};
44 BoundsInstanceBuf cylinder = {selection_type_, "bound_cylinder"};
45 BoundsInstanceBuf cone = {selection_type_, "bound_cone"};
46 BoundsInstanceBuf capsule_body = {selection_type_, "bound_capsule_body"};
47 BoundsInstanceBuf capsule_cap = {selection_type_, "bound_capsule_cap"};
48 } call_buffers_;
49
50 public:
51 Bounds(const SelectionType selection_type) : call_buffers_{selection_type} {}
52
53 void begin_sync(Resources & /*res*/, const State & /*state*/) final
54 {
55 call_buffers_.box.clear();
56 call_buffers_.sphere.clear();
57 call_buffers_.cylinder.clear();
58 call_buffers_.cone.clear();
59 call_buffers_.capsule_body.clear();
60 call_buffers_.capsule_cap.clear();
61 }
62
63 void object_sync(Manager & /*manager*/,
64 const ObjectRef &ob_ref,
65 Resources &res,
66 const State &state) final
67 {
68 const Object *ob = ob_ref.object;
69 const bool from_dupli = is_from_dupli_or_set(ob);
70 const bool empty_with_geometry = (ob->type == OB_EMPTY) && ob->runtime->geometry_set_eval &&
72 const bool has_bounds = (!ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER) &&
73 (ob->type != OB_MBALL || BKE_mball_is_basis(ob))) ||
74 empty_with_geometry;
75 const bool show_extras = !from_dupli && state.show_extras();
76
77 /* Ignore `show_extras` when the objects draw-type is already bound-box,
78 * otherwise the object would not draw at all. */
79 const bool draw_bounds = has_bounds && ((ob->dt == OB_BOUNDBOX) ||
80 ((ob->dtx & OB_DRAWBOUNDOX) && show_extras));
81 const float4 color = res.object_wire_color(ob_ref, state);
82
83 auto add_bounds_ex = [&](const float3 center, const float3 size, const char bound_type) {
84 const float4x4 object_mat{ob->object_to_world().ptr()};
85 const select::ID select_id = res.select_id(ob_ref);
86 switch (bound_type) {
87 case OB_BOUND_BOX: {
89 scale.location() = center;
90 ExtraInstanceData data(object_mat * scale, color, 1.0f);
91 call_buffers_.box.append(data, select_id);
92 break;
93 }
94 case OB_BOUND_SPHERE: {
96 scale.location() = center;
97 ExtraInstanceData data(object_mat * scale, color, 1.0f);
98 call_buffers_.sphere.append(data, select_id);
99 break;
100 }
101 case OB_BOUND_CYLINDER: {
103 float3(float2(math::max(size.x, size.y)), size.z));
104 scale.location() = center;
105 ExtraInstanceData data(object_mat * scale, color, 1.0f);
106 call_buffers_.cylinder.append(data, select_id);
107 break;
108 }
109 case OB_BOUND_CONE: {
111 float3(float2(math::max(size.x, size.y)), size.z));
112 mat.location() = center;
113 /* Cone batch has base at 0 and is pointing towards +Y. */
114 std::swap(mat[1], mat[2]);
115 mat.location().z -= size.z;
116 ExtraInstanceData data(object_mat * mat, color, 1.0f);
117 call_buffers_.cone.append(data, select_id);
118 break;
119 }
120 case OB_BOUND_CAPSULE: {
121 float4x4 mat = math::from_scale<float4x4>(float3(math::max(size.x, size.y)));
122 mat.location() = center;
123 mat.location().z = center.z + std::max(0.0f, size.z - size.x);
124 ExtraInstanceData data(object_mat * mat, color, 1.0f);
125 call_buffers_.capsule_cap.append(data, select_id);
126 mat.z_axis() *= -1;
127 mat.location().z = center.z - std::max(0.0f, size.z - size.x);
128 data.object_to_world = object_mat * mat;
129 call_buffers_.capsule_cap.append(data, select_id);
130 mat.z_axis().z = std::max(0.0f, size.z * 2.0f - size.x * 2.0f);
131 data.object_to_world = object_mat * mat;
132 call_buffers_.capsule_body.append(data, select_id);
133 break;
134 }
135 }
136 };
137
138 auto add_bounds = [&](const bool around_origin, const char bound_type) {
139 const std::optional<blender::Bounds<float3>> bounds_opt =
142 const blender::Bounds<float3> bounds = bounds_opt.value_or(
143 blender::Bounds(float3(-1.0f), float3(1.0f)));
144 const float3 size = (bounds.max - bounds.min) * 0.5f;
145 const float3 center = around_origin ? float3(0) : math::midpoint(bounds.min, bounds.max);
146 add_bounds_ex(center, size, bound_type);
147 };
148
149 /* Bounds */
150 if (draw_bounds) {
151 add_bounds(false, ob->boundtype);
152 }
153
154 /* Rigid Body Shape */
155 if (show_extras && ob->rigidbody_object != nullptr) {
156 switch (ob->rigidbody_object->shape) {
157 case RB_SHAPE_BOX:
158 add_bounds(true, OB_BOUND_BOX);
159 break;
160 case RB_SHAPE_SPHERE:
161 add_bounds(true, OB_BOUND_SPHERE);
162 break;
163 case RB_SHAPE_CONE:
164 add_bounds(true, OB_BOUND_CONE);
165 break;
167 add_bounds(true, OB_BOUND_CYLINDER);
168 break;
169 case RB_SHAPE_CAPSULE:
170 add_bounds(true, OB_BOUND_CAPSULE);
171 break;
172 case RB_SHAPE_CONVEXH:
173 case RB_SHAPE_TRIMESH:
175 break;
176 };
177 }
178
179 /* Texture Space */
180 if (show_extras && (ob->data != nullptr) && (ob->dtx & OB_TEXSPACE)) {
181 switch (GS(ob->data->name)) {
182 case ID_ME: {
185 add_bounds_ex(me.texspace_location, me.texspace_size, OB_BOUND_BOX);
186 break;
187 }
188 case ID_CU_LEGACY: {
191 add_bounds_ex(cu.texspace_location, cu.texspace_size, OB_BOUND_BOX);
192 break;
193 }
194 case ID_MB: {
196 add_bounds_ex(mb.texspace_location, mb.texspace_size, OB_BOUND_BOX);
197 break;
198 }
199 case ID_CV:
200 case ID_PT:
201 case ID_VO: {
202 /* No user defined texture space support. */
203 add_bounds(false, OB_BOUND_BOX);
204 break;
205 }
206 default:
208 }
209 }
210 }
211
212 void end_sync(Resources &res, const State &state) final
213 {
214 ps_.init();
216 state.clipping_plane_count);
217 ps_.shader_set(res.shaders->extra_shape.get());
218 ps_.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
219 ps_.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
220 res.select_bind(ps_);
221
222 call_buffers_.box.end_sync(ps_, res.shapes.cube.get());
223 call_buffers_.sphere.end_sync(ps_, res.shapes.empty_sphere.get());
224 call_buffers_.cylinder.end_sync(ps_, res.shapes.cylinder.get());
225 call_buffers_.cone.end_sync(ps_, res.shapes.empty_cone.get());
226 call_buffers_.capsule_body.end_sync(ps_, res.shapes.capsule_body.get());
227 call_buffers_.capsule_cap.end_sync(ps_, res.shapes.capsule_cap.get());
228 }
229
230 void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
231 {
232 GPU_framebuffer_bind(framebuffer);
233 manager.submit(ps_, view);
234 }
235};
236} // namespace blender::draw::overlay
Types and defines for representing Rigid Body entities.
#define BLI_assert_unreachable()
#define ELEM(...)
void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
void begin_sync(Resources &, const State &) final
Bounds(const SelectionType selection_type)
void end_sync(Resources &res, const State &state) final
void object_sync(Manager &, const ObjectRef &ob_ref, Resources &res, const State &state) final
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
float3x3 scale(float3x3 mat, float2 scale)
const int state
select::SelectionType SelectionType
detail::Pass< command::DrawCommandBuf > PassSimple
T reduce_max(const VecBase< T, Size > &a)
T midpoint(const T &a, const T &b)
MatT from_scale(const VecBase< typename MatT::base_type, ScaleDim > &scale)
T max(const T &a, const T &b)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
bool BKE_mball_is_basis(const Object *ob)
Definition mball.cc:255
void BKE_curve_texspace_ensure(Curve *cu)
Definition curve.cc:524
T & DRW_object_get_data_for_drawing(const Object &object)
BMesh const char void * data
void BKE_mesh_texspace_ensure(Mesh *mesh)
@ 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
std::optional< Bounds< float3 > > BKE_object_evaluated_geometry_bounds(const Object *ob)
Definition object.cc:3766
ID_Type GS(const char *name)
Definition DNA_ID.h:410
std::optional< Bounds< float3 > > BKE_object_boundbox_get(const Object *ob)
Definition object.cc:3726
float texspace_location[3]
char name[258]
Definition DNA_ID.h:433
float texspace_location[3]
float texspace_size[3]
eObject_DrawExtraFlag dtx
bke::ObjectRuntime * runtime
struct RigidBodyOb * rigidbody_object
eObject_BoundType boundtype