fixed minor bugs, opengl example is rendering now the lighthouse within the hmd

This commit is contained in:
Rene Eichhorn
2016-05-06 14:39:12 +02:00
parent 5801325a54
commit f085d27003
7 changed files with 169 additions and 19 deletions

View File

@ -4,6 +4,12 @@ extern crate nalgebra;
#[macro_use]
extern crate glium;
use std::convert::From;
use nalgebra::Inverse;
use glium::framebuffer::ToColorAttachment;
use glium::framebuffer::ToDepthAttachment;
use glium::GlObject;
#[derive(Copy, Clone)]
struct Vertex {
position: [f32; 3],
@ -35,17 +41,19 @@ pub fn main() {
for device in system.tracked_devices(0.0).connected_iter() {
println!("device found :) -> {}",
device.get_property_string(openvr::ETrackedDeviceProperty_Prop_RenderModelName_String).unwrap_or_else(|_| { panic!("No render model")} ));
println!("\t{:?}", device);
println!("\t{:?}", device.device_class());
}
// init compositor subsystem
/*let comp = match openvr::compositor() {
let comp = match openvr::compositor() {
Ok(ext) => ext,
Err(err) => {
println!("Failed to create IVRCompositor subsystem {:?}", err);
return;
}
};*/
};
// create glium window and context
use glium::{DisplayBuild, Surface};
@ -54,6 +62,43 @@ pub fn main() {
.build_glium()
.unwrap();
// create frame buffer for hmd
let texture_size = system.recommended_render_target_size();
let left_eye_depth = glium::framebuffer::DepthRenderBuffer::new(
&display,
glium::texture::DepthFormat::I24,
texture_size.width,
texture_size.height).unwrap();
let left_eye_texture = glium::framebuffer::RenderBuffer::new(
&display,
glium::texture::UncompressedFloatFormat::U8U8U8U8,
texture_size.width,
texture_size.height).unwrap();
let mut left_eye_framebuffer = glium::framebuffer::SimpleFrameBuffer::with_depth_buffer
(
&display, left_eye_texture.to_color_attachment(), left_eye_depth.to_depth_attachment()
).unwrap();
let right_eye_depth = glium::framebuffer::DepthRenderBuffer::new(
&display,
glium::texture::DepthFormat::I24,
texture_size.width,
texture_size.height).unwrap();
let right_eye_texture = glium::framebuffer::RenderBuffer::new(
&display,
glium::texture::UncompressedFloatFormat::U8U8U8U8,
texture_size.width,
texture_size.height).unwrap();
let mut right_eye_framebuffer = glium::framebuffer::SimpleFrameBuffer::with_depth_buffer
(
&display, right_eye_texture.to_color_attachment(), right_eye_depth.to_depth_attachment()
).unwrap();
// prepare shader
let vertex_shader_src = r#"
#version 140
@ -92,7 +137,7 @@ pub fn main() {
let program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap();
// load controller models
let controller = models.load(String::from("generic_hmd")).unwrap_or_else(|err| {
let controller = models.load(String::from("lh_basestation_vive")).unwrap_or_else(|err| {
openvr::shutdown(); panic!("controller render model not found: {:?}", err) });
let mut controller_vertices: Vec<Vertex> = Vec::new();
@ -115,21 +160,100 @@ pub fn main() {
let image = glium::texture::RawImage2d::from_raw_rgba(controller_texture_response.to_vec(), dimension);
let controller_texture = glium::texture::Texture2d::new(&display, image).unwrap();
// get static jmatrices
let left_projection = {
let raw = system.projection_matrix(openvr::Eye::Left, 0.01, 1000.0);
let mat = nalgebra::Matrix4::new(
raw[0][0], raw[0][1], raw[0][2], raw[0][3],
raw[1][0], raw[1][1], raw[1][2], raw[1][3],
raw[2][0], raw[2][1], raw[2][2], raw[2][3],
raw[3][0], raw[3][1], raw[3][2], raw[3][3]);
mat
};
let left_eye_transform = {
let raw = system.eye_to_head_transform(openvr::Eye::Left);
let mat = nalgebra::Matrix4::new(
raw[0][0], raw[1][0], raw[2][0], 0.0,
raw[0][1], raw[1][1], raw[2][1], 0.0,
raw[0][2], raw[1][2], raw[2][2], 0.0,
raw[0][3], raw[1][3], raw[2][3], 1.0);
mat.inverse().unwrap()
};
let right_projection = {
let raw = system.projection_matrix(openvr::Eye::Right, 0.01, 1000.0);
let mat = nalgebra::Matrix4::new(
raw[0][0], raw[0][1], raw[0][2], raw[0][3],
raw[1][0], raw[1][1], raw[1][2], raw[1][3],
raw[2][0], raw[2][1], raw[2][2], raw[2][3],
raw[3][0], raw[3][1], raw[3][2], raw[3][3]);
mat
};
let right_eye_transform = {
let raw = system.eye_to_head_transform(openvr::Eye::Right);
let mat = nalgebra::Matrix4::new(
raw[0][0], raw[1][0], raw[2][0], 0.0,
raw[0][1], raw[1][1], raw[2][1], 0.0,
raw[0][2], raw[1][2], raw[2][2], 0.0,
raw[0][3], raw[1][3], raw[2][3], 1.0);
mat.inverse().unwrap()
};
'render: loop {
// this is important to make sure frames are synced correctly
//let _ = comp.wait_get_poses();
let tracked_devices = comp.wait_get_poses();
let mut left_matrix = left_projection * left_eye_transform;
let mut right_matrix = right_projection * right_eye_transform;
let mut once = false;
for device in tracked_devices.connected_iter() {
match device.device_class() {
openvr::ETrackedDeviceClass_TrackedDeviceClass_HMD => {
let matrix = {
let raw = device.to_device;
let mat = nalgebra::Matrix4::new(
raw[0][0], raw[0][1], raw[0][2], raw[0][3],
raw[1][0], raw[1][1], raw[1][2], raw[1][3],
raw[2][0], raw[2][1], raw[2][2], raw[2][3],
0.0, 0.0, 0.0, 1.0);
mat.inverse().unwrap()
};
left_matrix *= matrix;
right_matrix *= matrix;
},
openvr::ETrackedDeviceClass_TrackedDeviceClass_TrackingReference => {
if once { continue; }
once = true;
let matrix = {
let raw = device.to_device;
let mat = nalgebra::Matrix4::new(
raw[0][0], raw[0][1], raw[0][2], raw[0][3],
raw[1][0], raw[1][1], raw[1][2], raw[1][3],
raw[2][0], raw[2][1], raw[2][2], raw[2][3],
0.0, 0.0, 0.0, 1.0);
mat
};
left_matrix *= matrix;
right_matrix *= matrix;
},
_ => { }
};
}
// render 2d display output
let mut target = display.draw();
target.clear_color_and_depth((0.0, 0.0, 1.0, 1.0), 1.0);
let uniforms = uniform! {
matrix: [
[5.0, 0.0, 0.0, 0.0],
[0.0, 5.0, 0.0, 0.0],
[0.0, 0.0, 5.0, 0.0],
[0.0 , 0.0, 0.0, 1.0f32],
],
let left_uniforms = uniform! {
matrix: *left_matrix.as_ref(),
diffuse: &controller_texture
};
let right_uniforms = uniform! {
matrix: *right_matrix.as_ref(),
diffuse: &controller_texture
};
@ -139,18 +263,26 @@ pub fn main() {
write: true,
.. Default::default()
},
backface_culling: glium::draw_parameters::BackfaceCullingMode::CullCounterClockwise,
backface_culling: glium::draw_parameters::BackfaceCullingMode::CullClockwise,
.. Default::default()
};
target.draw(&controller_vertex_buffer, &controller_index_buffer, &program, &uniforms, &params).unwrap();
// render 2d display output
target.draw(&controller_vertex_buffer, &controller_index_buffer, &program, &left_uniforms, &params).unwrap();
// render hmd eye outputs
left_eye_framebuffer.clear_color_and_depth((0.0, 0.0, 1.0, 1.0), 1.0);
right_eye_framebuffer.clear_color_and_depth((0.0, 0.0, 1.0, 1.0), 1.0);
left_eye_framebuffer.draw(&controller_vertex_buffer, &controller_index_buffer, &program, &left_uniforms, &params).unwrap();
right_eye_framebuffer.draw(&controller_vertex_buffer, &controller_index_buffer, &program, &right_uniforms, &params).unwrap();
// finish all rendering
target.finish().unwrap();
// submit to hmd
comp.submit(openvr::Eye::Left, left_eye_texture.get_id() as usize, openvr::common::TextureBounds::new((0.0, 1.0), (0.0, 1.0)));
comp.submit(openvr::Eye::Right, right_eye_texture.get_id() as usize, openvr::common::TextureBounds::new((0.0, 1.0), (0.0, 1.0)));
// handle window events
for ev in display.poll_events() {