rapier3d/pipeline/debug_render_pipeline/
debug_render_backend.rs

1use super::DebugColor;
2use crate::dynamics::{
3    ImpulseJoint, ImpulseJointHandle, Multibody, MultibodyLink, RigidBody, RigidBodyHandle,
4};
5use crate::geometry::{Aabb, Collider, ContactPair};
6use crate::math::{Isometry, Point, Real, Vector};
7use crate::prelude::{ColliderHandle, MultibodyJointHandle};
8use na::Scale;
9
10/// The object currently being rendered by the debug-renderer.
11#[derive(Copy, Clone)]
12pub enum DebugRenderObject<'a> {
13    /// A rigid-body is being rendered.
14    RigidBody(RigidBodyHandle, &'a RigidBody),
15    /// A collider is being rendered.
16    Collider(ColliderHandle, &'a Collider),
17    /// The AABB of a collider is being rendered.
18    ColliderAabb(ColliderHandle, &'a Collider, &'a Aabb),
19    /// An impulse-joint is being rendered.
20    ImpulseJoint(ImpulseJointHandle, &'a ImpulseJoint),
21    /// A multibody joint is being rendered.
22    MultibodyJoint(MultibodyJointHandle, &'a Multibody, &'a MultibodyLink),
23    /// The contacts of a contact-pair are being rendered.
24    ContactPair(&'a ContactPair, &'a Collider, &'a Collider),
25}
26
27/// Trait implemented by graphics backends responsible for rendering the physics scene.
28///
29/// The only thing that is required from the graphics backend is to be able to render
30/// a colored line. Note that the color is only a suggestion and is computed from the
31/// `DebugRenderStyle`. The backend is free to apply its own style, for example based on
32/// the `object` being rendered.
33pub trait DebugRenderBackend {
34    /// Predicate to filter-out some objects from the debug-rendering.
35    fn filter_object(&self, _object: DebugRenderObject) -> bool {
36        true
37    }
38
39    /// Draws a colored line.
40    ///
41    /// Note that this method can be called multiple time for the same `object`.
42    fn draw_line(
43        &mut self,
44        object: DebugRenderObject,
45        a: Point<Real>,
46        b: Point<Real>,
47        color: DebugColor,
48    );
49
50    /// Draws a set of lines.
51    fn draw_polyline(
52        &mut self,
53        object: DebugRenderObject,
54        vertices: &[Point<Real>],
55        indices: &[[u32; 2]],
56        transform: &Isometry<Real>,
57        scale: &Vector<Real>,
58        color: DebugColor,
59    ) {
60        for idx in indices {
61            let a = transform * (Scale::from(*scale) * vertices[idx[0] as usize]);
62            let b = transform * (Scale::from(*scale) * vertices[idx[1] as usize]);
63            self.draw_line(object, a, b, color);
64        }
65    }
66
67    /// Draws a chain of lines.
68    fn draw_line_strip(
69        &mut self,
70        object: DebugRenderObject,
71        vertices: &[Point<Real>],
72        transform: &Isometry<Real>,
73        scale: &Vector<Real>,
74        color: DebugColor,
75        closed: bool,
76    ) {
77        for vtx in vertices.windows(2) {
78            let a = transform * (Scale::from(*scale) * vtx[0]);
79            let b = transform * (Scale::from(*scale) * vtx[1]);
80            self.draw_line(object, a, b, color);
81        }
82
83        if closed && vertices.len() > 2 {
84            let a = transform * (Scale::from(*scale) * vertices[0]);
85            let b = transform * (Scale::from(*scale) * vertices.last().unwrap());
86            self.draw_line(object, a, b, color);
87        }
88    }
89}