From f9b714b4f9fb1e199b3d9493bcb4b661678b3525 Mon Sep 17 00:00:00 2001 From: Colin Sherratt Date: Sat, 24 May 2014 01:11:33 -0400 Subject: [PATCH] WIP added RenderConfig --- src/oculus-info/main.rs | 4 +- src/oculus-vr/lib.rs | 185 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 169 insertions(+), 20 deletions(-) diff --git a/src/oculus-info/main.rs b/src/oculus-info/main.rs index 5e954f0..4004aa6 100644 --- a/src/oculus-info/main.rs +++ b/src/oculus-info/main.rs @@ -49,8 +49,8 @@ fn main() { println!("Distorion Capabilities: {:?}", hmd_desc.distortion_capabilities); println!("Resolution: {:?}", hmd_desc.resolution); println!("Window Position: {:?}", hmd_desc.window_position); - println!("right: {:?}", hmd_desc.right); - println!("left {:?}", hmd_desc.left); + println!("right: {:?}", hmd_desc.eye_fovs.right); + println!("left {:?}", hmd_desc.eye_fovs.left); println!("Eyes render order: {:?}", hmd_desc.eye_render_order); println!("Display device name: {:s}", hmd_desc.display_device_name); println!("Display idr: {}", hmd_desc.display_id); diff --git a/src/oculus-vr/lib.rs b/src/oculus-vr/lib.rs index dc555fd..3afa5df 100644 --- a/src/oculus-vr/lib.rs +++ b/src/oculus-vr/lib.rs @@ -5,14 +5,14 @@ extern crate cgmath; extern crate libc; -use libc::{c_int, c_uint, c_float, time_t}; +use libc::{c_int, c_uint, c_float, time_t, c_void}; use std::c_str::ToCStr; use std::str::raw::from_c_str; use std::ptr; use std::default::Default; use cgmath::quaternion::Quaternion; -use cgmath::vector::Vector3; +use cgmath::vector::{Vector2, Vector3}; use cgmath::matrix::{Matrix, Matrix4, ToMatrix4}; use cgmath::projection::perspective; use cgmath::transform::Transform3D; @@ -44,10 +44,23 @@ pub mod ll { use std::default::Default; #[deriving(Clone, Default)] - pub struct Vector2i{pub x: c_int, pub y: c_int} + pub struct Vector2i { + pub x: c_int, + pub y: c_int + } #[deriving(Clone, Default)] - pub struct Sizei{pub x: c_int, pub y: c_int} + pub struct Sizei { + pub x: c_int, + pub y: c_int + } + + #[deriving(Clone, Default)] + pub struct Recti { + pub pos: Vector2i, + pub size: Sizei + } + #[deriving(Clone, Default)] pub struct FovPort { @@ -57,6 +70,9 @@ pub mod ll { pub rightTan: c_float } + #[deriving(Clone, Default)] + pub struct Vector2f {pub x: c_float, pub y: c_float} + #[deriving(Clone, Default)] pub struct Vector3f {pub x: c_float, pub y: c_float, pub z: c_float} @@ -140,6 +156,27 @@ pub mod ll { pub serial_number: [c_char, ..24] } + #[deriving(Clone, Default)] + pub struct EyeRenderDesc { + pub eye: c_uint, + pub fov: FovPort, + pub distorted_viewport: Recti, + pub pixels_per_tan_angle_at_center: Vector2f, + pub view_adjust: Vector3f + } + + pub struct RenderApiConfigHeader { + pub render_api_type: c_uint, + pub rt_size: Sizei, + pub multisample: c_int, + } + + pub struct RenderApiConfig { + pub header: RenderApiConfigHeader, + pub display: *c_void, + pub window: *c_void + } + pub static Hmd_None : c_int = 0; pub static Hmd_DK1 : c_int = 3; pub static Hmd_DKHD : c_int = 4; @@ -172,6 +209,14 @@ pub mod ll { pub static Eye_Left : c_uint = 0; pub static Eye_Right : c_uint = 1; + pub static RenderAPI_None : c_uint = 0; + pub static RenderAPI_OpenGL : c_uint = 1; + pub static RenderAPI_Android_GLES : c_uint = 2; + pub static RenderAPI_D3D9 : c_uint = 3; + pub static RenderAPI_D3D10 : c_uint = 4; + pub static RenderAPI_D3D11 : c_uint = 5; + pub static RenderAPI_Count : c_uint = 6; + extern "C" { pub fn ovr_Initialize() -> bool; pub fn ovr_Shutdown(); @@ -197,6 +242,11 @@ pub mod ll { eye: c_uint, fov: FovPort, pixels: c_float) -> Sizei; + pub fn ovrHmd_ConfigureRendering(hmd: *Hmd, + apiConfig: *RenderApiConfig, + distortionCaps: c_uint, + fov_in: *FovPort, + render_desc_out: *EyeRenderDesc) -> bool; } } @@ -375,6 +425,30 @@ impl Hmd { pixels_per_display_pixel) } } + + pub fn configure_rendering(&self, + api_config: &RC, + cap: DistortionCapabilities, + eye_fov: PerEye) -> Option> { + unsafe { + let out: PerEye + = PerEye::new(Default::default(), + Default::default()); + let was_started = ll::ovrHmd_ConfigureRendering( + self.ptr, + &api_config.to_render_config(), + cap.flags, + eye_fov.ptr(), + out.ptr() + ); + + if was_started { + Some(out.map(|_, d| EyeRenderDescriptor::from_ll(d))) + } else { + None + } + } + } } pub struct HmdCapabilities { @@ -698,6 +772,38 @@ impl EyeType { } } +pub struct PerEye { + pub left: T, + pub right: T +} + +impl PerEye { + pub fn new(left: T, right: T) -> PerEye { + PerEye { + left: left, + right: right + } + } + + pub fn eye<'a>(&'a self, eye: EyeType) -> &'a T { + match eye { + EyeLeft => &self.left, + EyeRight => &self.right + } + } + + pub fn map(&self, f: |EyeType, &T| -> U) -> PerEye { + PerEye::new( + f(EyeLeft, &self.left), + f(EyeRight, &self.right) + ) + } + + unsafe fn ptr(&self) -> *T { + &self.left as *T + } +} + pub struct HmdDescriptionEye { default_eye_fov: ll::FovPort, max_eye_fov: ll::FovPort, @@ -712,8 +818,7 @@ pub struct HmdDescription { pub distortion_capabilities: DistortionCapabilities, pub resolution: ll::Sizei, pub window_position: ll::Vector2i, - pub left: HmdDescriptionEye, - pub right: HmdDescriptionEye, + pub eye_fovs: PerEye, pub eye_render_order: [EyeType, ..2], pub display_device_name: ~str, pub display_id: c_int @@ -737,14 +842,16 @@ impl HmdDescription { }, resolution: sd.resolution, window_position: sd.window_position, - left: HmdDescriptionEye { - default_eye_fov: sd.default_eye_fov[ll::Eye_Left as uint], - max_eye_fov: sd.max_eye_fov[ll::Eye_Left as uint] - }, - right: HmdDescriptionEye { - default_eye_fov: sd.default_eye_fov[ll::Eye_Right as uint], - max_eye_fov: sd.max_eye_fov[ll::Eye_Right as uint] - }, + eye_fovs: PerEye::new( + HmdDescriptionEye { + default_eye_fov: sd.default_eye_fov[ll::Eye_Left as uint], + max_eye_fov: sd.max_eye_fov[ll::Eye_Left as uint] + }, + HmdDescriptionEye { + default_eye_fov: sd.default_eye_fov[ll::Eye_Right as uint], + max_eye_fov: sd.max_eye_fov[ll::Eye_Right as uint] + } + ), eye_render_order: [EyeType::from_ll(sd.eye_render_order[0]), EyeType::from_ll(sd.eye_render_order[1])], display_device_name: from_c_str(sd.display_device_name), @@ -752,11 +859,53 @@ impl HmdDescription { } } } +} - pub fn eye<'a>(&'a self, eye: EyeType) -> &'a HmdDescriptionEye { - match eye { - EyeLeft => &self.left, - EyeRight => &self.right +pub struct EyeRenderDescriptor { + pub eye: EyeType, + pub fov: ll::FovPort, + pub distorted_viewport: ll::Recti, + pub pixels_per_tan_angle_at_center: Vector2, + pub view_adjust: Vector3 +} + +impl EyeRenderDescriptor { + fn from_ll(d: &ll::EyeRenderDesc) -> EyeRenderDescriptor { + EyeRenderDescriptor { + eye: EyeType::from_ll(d.eye), + fov: d.fov, + distorted_viewport: d.distorted_viewport, + pixels_per_tan_angle_at_center: + Vector2::new(d.pixels_per_tan_angle_at_center.x, + d.pixels_per_tan_angle_at_center.y), + view_adjust: Vector3::new(d.view_adjust.x, + d.view_adjust.y, + d.view_adjust.z) + } + } +} + +pub struct RenderGLConfig { + pub size: ll::Sizei, + pub multisample: int, + pub display: *c_void, + pub window: *c_void +} + +pub trait ToRenderConfig { + fn to_render_config(&self) -> ll::RenderApiConfig; +} + +impl ToRenderConfig for RenderGLConfig { + fn to_render_config(&self) -> ll::RenderApiConfig { + ll::RenderApiConfig { + header: ll::RenderApiConfigHeader { + render_api_type: ll::RenderAPI_OpenGL, + rt_size: self.size, + multisample: self.multisample as c_int, + }, + display: self.display, + window: self.window } } } \ No newline at end of file