cleaned up HMDInfo

This commit is contained in:
Colin Sherratt
2014-02-05 23:20:02 -05:00
parent fd783b25e6
commit 768ed4916a
5 changed files with 142 additions and 95 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "OculusSDK"]
path = OculusSDK
url = https://github.com/csherratt/OculusSDK.git

View File

@ -1,18 +1,11 @@
LIBOVR_INCLUDE_PATH=../OculusSDK/LibOVR/Include/ LIBOVR_INCLUDE_PATH=OculusSDK/LibOVR/Include/
LIBOVR_LIB_PATH=../OculusSDK/LibOVR/Lib/Linux/Release/x86_64/libovr.a LIBOVR_LIB_PATH=OculusSDK/LibOVR/Lib/Linux/Release/x86_64/libovr.a
all: libovr_wrapper.a
all: test
libovr_wrapper.a: wrapper.o libovr_wrapper.a: wrapper.o
ar rcs libovr_wrapper.a wrapper.o ar rcs libovr_wrapper.a wrapper.o
wrapper.o: wrapper.cpp wrapper.o: wrapper.cpp
c++ -fPIC -I $(LIBOVR_INCLUDE_PATH) -c -o wrapper.o wrapper.cpp c++ -fPIC -I $(LIBOVR_INCLUDE_PATH) -c -o wrapper.o wrapper.cpp
libovr-rs-44316370-0.1.so: libovr_wrapper.a lib.rs
rustc lib.rs -L .
test: test.rs libovr-rs-44316370-0.1.so
rustc test.rs -L .

1
OculusSDK Submodule

Submodule OculusSDK added at 0d284f51c3

176
lib.rs
View File

@ -15,18 +15,35 @@ use cgmath::transform::Transform3D;
use cgmath::angle::rad; use cgmath::angle::rad;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
#[link(name="udev")]
#[link(name="stdc++")]
#[link(name="Xinerama")]
#[link(name="ovr_wrapper")] #[link(name="ovr_wrapper")]
#[link(name="ovr")] #[link(name="OculusVR")]
#[link(name="stdc++")]
#[link(name="udev")]
#[link(name="Xinerama")]
#[link(name="edid")]
extern {} extern {}
pub mod ll { pub mod ll {
use std::libc::{c_uint, c_int, c_float, c_long, c_char, time_t}; use std::libc::{c_uint, c_int, c_float, c_long, c_char, time_t};
pub enum DeviceManager {} pub enum DeviceManager {}
pub enum HMDInfo {} pub struct HMDInfo {
HResolution: c_uint,
VResolution: c_uint,
HScreenSize: c_float,
VScreenSize: c_float,
VScreenCenter: c_float,
EyeToScreenDistance: c_float,
LensSeparationDistance: c_float,
InterpupillaryDistance: c_float,
DistortionK: [c_float, ..4],
ChromaAbCorrection: [c_float, ..4],
DesktopX: c_int,
DesktopY: c_int,
DisplayDeviceName: [c_char, ..32],
DisplayId: c_long
}
pub enum HMDDevice {} pub enum HMDDevice {}
pub enum SensorDevice {} pub enum SensorDevice {}
pub enum SensorFusion {} pub enum SensorFusion {}
@ -42,7 +59,7 @@ pub mod ll {
pub fn OVR_system_init(); pub fn OVR_system_init();
pub fn OVR_DeviceManager_Create() -> *DeviceManager; pub fn OVR_DeviceManager_Create() -> *DeviceManager;
pub fn OVR_DeviceManager_EnumerateDevices(dm :*DeviceManager) -> *HMDDevice; pub fn OVR_DeviceManager_EnumerateDevices(dm :*DeviceManager) -> *HMDDevice;
pub fn OVR_HDMDevice_GetDeviceInfo(hmd: *HMDDevice) -> *HMDInfo; pub fn OVR_HDMDevice_GetDeviceInfo(hmd: *HMDDevice) -> HMDInfo;
pub fn OVR_HDMDevice_GetSensor(hmd: *HMDDevice) -> *SensorDevice; pub fn OVR_HDMDevice_GetSensor(hmd: *HMDDevice) -> *SensorDevice;
pub fn OVR_SensorFusion() -> *SensorFusion; pub fn OVR_SensorFusion() -> *SensorFusion;
@ -80,20 +97,6 @@ pub mod ll {
//pub fn OVR_SensorFusion_OnMessage(sf: *SensorFusion, msg: *MessageBodyFrame) //pub fn OVR_SensorFusion_OnMessage(sf: *SensorFusion, msg: *MessageBodyFrame)
//pub fn OVR_SensorFusion_SetDelegateMessageHandler(sf: *SensorFusion, handle: *MessageHandler) //pub fn OVR_SensorFusion_SetDelegateMessageHandler(sf: *SensorFusion, handle: *MessageHandler)
pub fn OVR_HMDInfo_GetScreenHResolution(info: *HMDInfo) -> c_uint;
pub fn OVR_HMDInfo_GetScreenVResolution(info: *HMDInfo) -> c_uint;
pub fn OVR_HMDInfo_GetHScreenSize(info: *HMDInfo) -> c_float;
pub fn OVR_HMDInfo_GetVScreenSize(info: *HMDInfo) -> c_float;
pub fn OVR_HMDInfo_GetVScreenCenter(info: *HMDInfo) -> c_float;
pub fn OVR_HMDInfo_GetEyeToScreenDistance(info: *HMDInfo) -> c_float;
pub fn OVR_HMDInfo_GetLensSeparationDistance(info: *HMDInfo) -> c_float;
pub fn OVR_HMDInfo_GetInterpupillaryDistance(info: *HMDInfo) -> c_float;
pub fn OVR_HMDInfo_GetDistortionK(info: *HMDInfo, idx: c_int) -> c_float;
pub fn OVR_HMDInfo_GetChromaAbCorrection(info: *HMDInfo, idx: c_int) -> c_float;
pub fn OVR_HMDInfo_GetDesktopX(info: *HMDInfo) -> c_uint;
pub fn OVR_HMDInfo_GetDesktopY(info: *HMDInfo) -> c_uint;
pub fn OVR_HMDInfo_GetDisplayDeviceName(info: *HMDInfo) -> *c_char;
pub fn OVR_HMDInfo_GetDisplayId(info: *HMDInfo) -> c_long;
} }
} }
@ -145,17 +148,12 @@ pub struct HMDDevice {
} }
impl HMDDevice { impl HMDDevice {
pub fn get_info(&self) -> Option<HMDInfo> pub fn get_info(&self) -> HMDInfo
{ {
unsafe { unsafe {
let ptr = ll::OVR_HDMDevice_GetDeviceInfo(self.ptr); println!("{:?}", self);
HMDInfo{
if ptr.is_null() { dat: ll::OVR_HDMDevice_GetDeviceInfo(self.ptr)
None
} else {
Some(HMDInfo{
ptr: ptr
})
} }
} }
} }
@ -163,6 +161,7 @@ impl HMDDevice {
pub fn get_sensor(&self) -> Option<SensorDevice> pub fn get_sensor(&self) -> Option<SensorDevice>
{ {
unsafe { unsafe {
println!("{:?}", self);
let ptr = ll::OVR_HDMDevice_GetSensor(self.ptr); let ptr = ll::OVR_HDMDevice_GetSensor(self.ptr);
if ptr.is_null() { if ptr.is_null() {
@ -177,101 +176,72 @@ impl HMDDevice {
} }
pub struct HMDInfo { pub struct HMDInfo {
priv ptr: *ll::HMDInfo priv dat: ll::HMDInfo
} }
impl HMDInfo impl HMDInfo
{ {
pub fn resolution(&self) -> (uint, uint) pub fn resolution(&self) -> (uint, uint)
{ {
unsafe { (self.dat.HResolution as uint, self.dat.VResolution as uint)
let h = ll::OVR_HMDInfo_GetScreenHResolution(self.ptr);
let v = ll::OVR_HMDInfo_GetScreenVResolution(self.ptr);
(h as uint, v as uint)
}
} }
pub fn size(&self) -> (f32, f32) pub fn size(&self) -> (f32, f32)
{ {
unsafe { (self.dat.HScreenSize as f32, self.dat.VScreenSize as f32)
let h = ll::OVR_HMDInfo_GetHScreenSize(self.ptr);
let v = ll::OVR_HMDInfo_GetVScreenSize(self.ptr);
(h as f32, v as f32)
}
} }
pub fn desktop(&self) -> (int, int) pub fn desktop(&self) -> (int, int)
{ {
unsafe { (self.dat.DesktopX as int, self.dat.DesktopY as int)
let h = ll::OVR_HMDInfo_GetDesktopX(self.ptr);
let v = ll::OVR_HMDInfo_GetDesktopY(self.ptr);
(h as int, v as int)
}
} }
pub fn vertical_center(&self) -> f32 pub fn vertical_center(&self) -> f32
{ {
unsafe { self.dat.VScreenCenter as f32
ll::OVR_HMDInfo_GetVScreenCenter(self.ptr) as f32
}
} }
pub fn eye_to_screen_distance(&self) -> f32 pub fn eye_to_screen_distance(&self) -> f32
{ {
unsafe { self.dat.EyeToScreenDistance as f32
ll::OVR_HMDInfo_GetEyeToScreenDistance(self.ptr) as f32
}
} }
pub fn lens_separation_distance(&self) -> f32 pub fn lens_separation_distance(&self) -> f32
{ {
unsafe { self.dat.LensSeparationDistance as f32
ll::OVR_HMDInfo_GetLensSeparationDistance(self.ptr) as f32
}
} }
pub fn interpupillary_distance(&self) -> f32 pub fn interpupillary_distance(&self) -> f32
{ {
unsafe { self.dat.InterpupillaryDistance as f32
ll::OVR_HMDInfo_GetInterpupillaryDistance(self.ptr) as f32
}
} }
pub fn distortion_K(&self) -> [f32, ..4] pub fn distortion_K(&self) -> [f32, ..4]
{ {
unsafe { [self.dat.DistortionK[0] as f32,
[ll::OVR_HMDInfo_GetDistortionK(self.ptr, 0) as f32, self.dat.DistortionK[1] as f32,
ll::OVR_HMDInfo_GetDistortionK(self.ptr, 1) as f32, self.dat.DistortionK[2] as f32,
ll::OVR_HMDInfo_GetDistortionK(self.ptr, 2) as f32, self.dat.DistortionK[3] as f32]
ll::OVR_HMDInfo_GetDistortionK(self.ptr, 3) as f32]
}
} }
pub fn chroma_ab_correction(&self) -> [f32, ..4] pub fn chroma_ab_correction(&self) -> [f32, ..4]
{ {
unsafe { [self.dat.ChromaAbCorrection[0] as f32,
[ll::OVR_HMDInfo_GetChromaAbCorrection(self.ptr, 0) as f32, self.dat.ChromaAbCorrection[1] as f32,
ll::OVR_HMDInfo_GetChromaAbCorrection(self.ptr, 1) as f32, self.dat.ChromaAbCorrection[2] as f32,
ll::OVR_HMDInfo_GetChromaAbCorrection(self.ptr, 2) as f32, self.dat.ChromaAbCorrection[3] as f32]
ll::OVR_HMDInfo_GetChromaAbCorrection(self.ptr, 3) as f32]
}
} }
pub fn name(&self) -> ~str pub fn name(&self) -> ~str
{ {
unsafe { unsafe {
std::str::raw::from_c_str(ll::OVR_HMDInfo_GetDisplayDeviceName(self.ptr)) std::str::raw::from_c_str(&self.dat.DisplayDeviceName[0])
} }
} }
pub fn id(&self) -> int pub fn id(&self) -> int
{ {
unsafe { self.dat.DisplayId as int
ll::OVR_HMDInfo_GetDisplayId(self.ptr) as int
}
} }
} }
@ -538,8 +508,8 @@ pub struct SensorDevice {
} }
pub fn create_reference_matrices(hmd: &HMDInfo, view_center: &Mat4<f32>) -> ((Mat4<f32>, Mat4<f32>), pub fn create_reference_matrices(hmd: &HMDInfo, view_center: &Mat4<f32>, scale: f32) -> ((Mat4<f32>, Mat4<f32>),
(Mat4<f32>, Mat4<f32>)) (Mat4<f32>, Mat4<f32>))
{ {
let (h_res, v_res) = hmd.resolution(); let (h_res, v_res) = hmd.resolution();
let (h_size, v_size) = hmd.size(); let (h_size, v_size) = hmd.size();
@ -549,12 +519,12 @@ pub fn create_reference_matrices(hmd: &HMDInfo, view_center: &Mat4<f32>) -> ((Ma
let aspect_ratio = (h_res as f32 * 0.5) / (v_res as f32); let aspect_ratio = (h_res as f32 * 0.5) / (v_res as f32);
let half_screen_distance = v_size / 2f32; let half_screen_distance = v_size / 2f32;
let yfov = 2f32 * (half_screen_distance/eye_to_screen).atan(); let yfov = 2f32 * (half_screen_distance*scale/eye_to_screen).atan();
let eye_project_shift = h_size * 0.25 - lens_separation_distance*0.5f32; let eye_project_shift = h_size * 0.25 - lens_separation_distance*0.5f32;
let proj_off_center = 4f32 * eye_project_shift / h_size; let proj_off_center = 4f32 * eye_project_shift / h_size;
let proj_center = perspective(rad(yfov), aspect_ratio, 0.3f32, 1000f32); let proj_center = perspective(rad(yfov), aspect_ratio, 0.1f32, 1000f32);
let proj_left = Transform3D::new(1., Quat::zero(), Vec3::new(proj_off_center, 0., 0.)).get().to_mat4(); let proj_left = Transform3D::new(1., Quat::zero(), Vec3::new(proj_off_center, 0., 0.)).get().to_mat4();
let proj_right = Transform3D::new(1., Quat::zero(), Vec3::new(-proj_off_center, 0., 0.)).get().to_mat4(); let proj_right = Transform3D::new(1., Quat::zero(), Vec3::new(-proj_off_center, 0., 0.)).get().to_mat4();
let proj_left = proj_left.mul_m(&proj_center); let proj_left = proj_left.mul_m(&proj_center);
@ -570,3 +540,49 @@ pub fn create_reference_matrices(hmd: &HMDInfo, view_center: &Mat4<f32>) -> ((Ma
(view_left, view_right)) (view_left, view_right))
} }
pub static SHADER_FRAG_CHROMAB: &'static str =
"#version 400
uniform vec2 LensCenter;
uniform vec2 ScreenCenter;
uniform vec2 ScaleIn;
uniform vec2 ScaleOut;
uniform vec4 HmdWarpParam;
uniform vec4 ChromAbParam;
uniform sampler2D Texture0;
in vec2 TexPos;
out vec4 color;
void main()
{
vec2 pos = vec2(TexPos.x, TexPos.y);
vec2 theta = (pos - LensCenter) * ScaleIn;
float rSq = theta.x * theta.x + theta.y * theta.y;
vec2 theta1 = theta * (HmdWarpParam.x + HmdWarpParam.y * rSq +
HmdWarpParam.z * rSq * rSq +
HmdWarpParam.w * rSq * rSq * rSq);
vec2 thetaBlue = theta1 * (ChromAbParam.z + ChromAbParam.w * rSq);
vec2 tcBlue = LensCenter + ScaleOut * thetaBlue;
if (!all(equal(clamp(tcBlue, ScreenCenter-vec2(0.25,0.5), ScreenCenter+vec2(0.25,0.5)), tcBlue))) {
color = vec4(0.);
return;
}
float blue = texture2D(Texture0, tcBlue).b;
vec2 tcGreen = LensCenter + ScaleOut * theta1;
vec4 center = texture2D(Texture0, tcGreen);
vec2 thetaRed = theta1 * (ChromAbParam.x + ChromAbParam.y * rSq);
vec2 tcRed = LensCenter + ScaleOut * thetaRed;
float red = texture2D(Texture0, tcRed).r;
color = vec4(red, center.g, blue, center.a);
}
";

View File

@ -17,6 +17,23 @@ extern "C"
m41, m42, m43, m44; m41, m42, m43, m44;
}; };
struct HMDInfoC {
uint HResolution;
uint VResolution;
float HScreenSize;
float VScreenSize;
float VScreenCenter;
float EyeToScreenDistance;
float LensSeparationDistance;
float InterpupillaryDistance;
float DistortionK[4];
float ChromaAbCorrection[4];
int DesktopX;
int DesktopY;
char DisplayDeviceName[32];
long DisplayId;
};
void OVR_system_init(void) void OVR_system_init(void)
{ {
OVR::System::Init(OVR::Log::ConfigureDefaultLog(OVR::LogMask_All)); OVR::System::Init(OVR::Log::ConfigureDefaultLog(OVR::LogMask_All));
@ -32,11 +49,28 @@ extern "C"
return pManager->EnumerateDevices<OVR::HMDDevice>().CreateDevice(); return pManager->EnumerateDevices<OVR::HMDDevice>().CreateDevice();
} }
OVR::HMDInfo* OVR_HDMDevice_GetDeviceInfo(OVR::HMDDevice* pHMD) struct HMDInfoC OVR_HDMDevice_GetDeviceInfo(OVR::HMDDevice* pHMD)
{ {
OVR::HMDInfo *hdm = new OVR::HMDInfo; OVR::HMDInfo hmd;
pHMD->GetDeviceInfo(hdm); struct HMDInfoC out_hmd;
return hdm; pHMD->GetDeviceInfo(&hmd);
out_hmd.HResolution = hmd.HResolution;
out_hmd.VResolution = hmd.VResolution;
out_hmd.HScreenSize = hmd.HScreenSize;
out_hmd.VScreenSize = hmd.VScreenSize;
out_hmd.VScreenCenter = hmd.VScreenCenter;
out_hmd.EyeToScreenDistance = hmd.EyeToScreenDistance;
out_hmd.LensSeparationDistance = hmd.LensSeparationDistance;
out_hmd.InterpupillaryDistance = hmd.InterpupillaryDistance;
out_hmd.DesktopX = hmd.DesktopX;
out_hmd.DesktopY = hmd.DesktopY;
out_hmd.DisplayId = hmd.DisplayId;
memcpy(out_hmd.DistortionK, hmd.DistortionK, sizeof(hmd.DistortionK));
memcpy(out_hmd.ChromaAbCorrection, hmd.ChromaAbCorrection, sizeof(hmd.ChromaAbCorrection));
memcpy(out_hmd.DisplayDeviceName, hmd.DisplayDeviceName, sizeof(hmd.DisplayDeviceName));
return out_hmd;
} }
OVR::SensorDevice* OVR_HDMDevice_GetSensor(OVR::HMDDevice* pHMD) OVR::SensorDevice* OVR_HDMDevice_GetSensor(OVR::HMDDevice* pHMD)