aboutsummaryrefslogtreecommitdiff
path: root/src/pipeline/debug_render_pipeline/debug_render_backend.rs
blob: 84aa2ccd0b6a0991a6323e43dd2b2ae0672ca7a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use crate::dynamics::{
    ImpulseJoint, ImpulseJointHandle, Multibody, MultibodyLink, RigidBody, RigidBodyHandle,
};
use crate::geometry::{Aabb, Collider, ContactPair};
use crate::math::*;
use crate::prelude::{ColliderHandle, MultibodyJointHandle};

/// The object currently being rendered by the debug-renderer.
#[derive(Copy, Clone)]
pub enum DebugRenderObject<'a> {
    /// A rigid-body is being rendered.
    RigidBody(RigidBodyHandle, &'a RigidBody),
    /// A collider is being rendered.
    Collider(ColliderHandle, &'a Collider),
    /// The AABB of a collider is being rendered.
    ColliderAabb(ColliderHandle, &'a Collider, &'a Aabb),
    /// An impulse-joint is being rendered.
    ImpulseJoint(ImpulseJointHandle, &'a ImpulseJoint),
    /// A multibody joint is being rendered.
    MultibodyJoint(MultibodyJointHandle, &'a Multibody, &'a MultibodyLink),
    /// The contacts of a contact-pair are being rendered.
    ContactPair(&'a ContactPair, &'a Collider, &'a Collider),
}

/// Trait implemented by graphics backends responsible for rendering the physics scene.
///
/// The only thing that is required from the graphics backend is to be able to render
/// a colored line. Note that the color is only a suggestion and is computed from the
/// `DebugRenderStyle`. The backend is free to apply its own style, for example based on
/// the `object` being rendered.
pub trait DebugRenderBackend {
    /// Predicate to filter-out some objects from the debug-rendering.
    fn filter_object(&self, _object: DebugRenderObject) -> bool {
        true
    }

    /// Draws a colored line.
    ///
    /// Note that this method can be called multiple time for the same `object`.
    fn draw_line(&mut self, object: DebugRenderObject, a: Point, b: Point, color: [f32; 4]);

    /// Draws a set of line.
    fn draw_polyline(
        &mut self,
        object: DebugRenderObject,
        vertices: &[Point],
        indices: &[[u32; 2]],
        transform: &Isometry,
        scale: &Vector,
        color: [f32; 4],
    ) {
        for idx in indices {
            let a = transform.transform_point(&Point::from(
                vertices[idx[0] as usize].as_vector().component_mul(scale),
            ));
            let b = transform.transform_point(&Point::from(
                vertices[idx[1] as usize].as_vector().component_mul(scale),
            ));
            self.draw_line(object, a, b, color);
        }
    }

    /// Draws a chain of line.
    fn draw_line_strip(
        &mut self,
        object: DebugRenderObject,
        vertices: &[Point],
        transform: &Isometry,
        scale: &Vector,
        color: [f32; 4],
        closed: bool,
    ) {
        for vtx in vertices.windows(2) {
            let a =
                transform.transform_point(&Point::from(vtx[0].as_vector().component_mul(scale)));
            let b =
                transform.transform_point(&Point::from(vtx[1].as_vector().component_mul(scale)));
            self.draw_line(object, a, b, color);
        }

        if closed && vertices.len() > 2 {
            let a = transform
                .transform_point(&Point::from(vertices[0].as_vector().component_mul(scale)));
            let b = transform.transform_point(&Point::from(
                vertices.last().unwrap().as_vector().component_mul(scale),
            ));
            self.draw_line(object, a, b, color);
        }
    }
}