rust_webvr_api/
vr_frame_data.rs

1use VRPose;
2use std::mem;
3use std::ptr;
4
5/// Represents all the information needed to render a single frame of a VR scene
6#[derive(Debug, Clone)]
7#[cfg_attr(feature = "serde-serialization", derive(Deserialize, Serialize))]
8pub struct VRFrameData {
9    /// Monotonically increasing value that allows the author 
10    /// to determine if position state data been updated from the hardware
11    pub timestamp: f64,
12
13    /// major order column matrix describing the projection to be used for the left eye’s rendering
14    pub left_projection_matrix: [f32; 16],
15
16    /// major order column matrix describing the view transform to be used for the left eye’s rendering
17    pub left_view_matrix: [f32; 16],
18
19    /// major order column matrix describing the projection to be used for the right eye’s rendering
20    pub right_projection_matrix: [f32; 16],
21
22    /// major order column matrix describing the view transform to be used for the right eye’s rendering
23    pub right_view_matrix: [f32; 16],
24 
25    /// VRPose containing the future predicted pose of the VRDisplay
26    /// when the current frame will be presented.
27    pub pose: VRPose,
28}
29
30impl Default for VRFrameData {
31    fn default() -> VRFrameData {
32        VRFrameData {
33            timestamp: 0f64,
34            left_projection_matrix: identity_matrix!(),
35            left_view_matrix: identity_matrix!(),
36            right_projection_matrix: identity_matrix!(),
37            right_view_matrix: identity_matrix!(),
38            pose: VRPose::default(),
39        }
40    }
41}
42
43impl VRFrameData {
44    pub fn to_bytes(&self) -> Vec<u8> {
45        let mut vec = vec![0u8; mem::size_of::<VRFrameData>()];
46        unsafe {
47            ptr::copy_nonoverlapping(self,
48                                    vec.as_mut_ptr() as *mut VRFrameData,
49                                    mem::size_of::<VRFrameData>());
50        }
51        vec
52    }
53
54    pub fn from_bytes(bytes: &[u8]) -> VRFrameData {
55        unsafe {
56            let mut result = mem::MaybeUninit::uninit();
57            ptr::copy_nonoverlapping(bytes.as_ptr(),
58                                     result.as_mut_ptr() as *mut u8,
59                                     mem::size_of::<VRFrameData>());
60            result.assume_init()
61        }
62    }
63}