From 54063f255397475ed54fa76eb97e06f7ba2c3bd0 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sat, 25 Apr 2020 17:19:49 -0400 Subject: [PATCH] improve xy_table generation (still not fixed) The visual output is a vertical line with camera input colorization --- src/graphics_imgui.rs | 9 ++- src/point_cloud/point_cloud_compute_shader.rs | 66 +++++++++++++++---- .../point_cloud_renderer_shader.rs | 33 +++++----- src/point_cloud/point_cloud_visualiser.rs | 27 ++++++-- 4 files changed, 99 insertions(+), 36 deletions(-) diff --git a/src/graphics_imgui.rs b/src/graphics_imgui.rs index 5ce5157969..ad9d9b86f1 100644 --- a/src/graphics_imgui.rs +++ b/src/graphics_imgui.rs @@ -55,8 +55,8 @@ pub fn run(capture_provider: Arc, calibration_data: k4a_sys::k4 let imgui_texture_id = TextureId::from(gl_texture_id as usize); let mut visualizer = PointCloudVisualizer::new( - false, - ColorizationStrategy::Simple, + true, + ColorizationStrategy::Color, calibration_data ); @@ -147,7 +147,7 @@ pub fn run(capture_provider: Arc, calibration_data: k4a_sys::k4 ui.separator(); // Depth image dimensions: 640x576 // Take transformed depth image... 1280x720 [Depth16] - Image::new(imgui_texture_id_3, [1280.0, 720.0]).build(&ui); + Image::new(imgui_texture_id_2, [1280.0, 720.0]).build(&ui); //Image::new(imgui_texture_id_4, [1280.0, 720.0]).build(&ui); }); @@ -162,9 +162,8 @@ pub fn run(capture_provider: Arc, calibration_data: k4a_sys::k4 window.gl_swap_window(); if let Some(capture) = capture_provider.get_capture() { - visualizer.update_texture_id(gl_texture_id_3, capture) + visualizer.update_texture_id(texture.texture_id(), capture) .map(|_| { - println!("UPDATED TEXTURE!"); }) .map_err(|err| { match err { diff --git a/src/point_cloud/point_cloud_compute_shader.rs b/src/point_cloud/point_cloud_compute_shader.rs index cdbc5c8bef..befc39bdca 100644 --- a/src/point_cloud/point_cloud_compute_shader.rs +++ b/src/point_cloud/point_cloud_compute_shader.rs @@ -242,13 +242,19 @@ impl PointCloudComputeShader { let mut depth_src = depth_image.get_buffer(); + // TODO TESTING + //std::ptr::write_bytes(texture_mapped_buffer as *mut u8, 120, num_bytes as usize); + std::ptr::copy_nonoverlapping::(depth_src, texture_mapped_buffer as *mut u8, num_bytes as usize); + //std::ptr::copy::(depth_src, texture_mapped_buffer as *mut u8, num_bytes as usize); + let result = gl::UnmapBuffer(gl::PIXEL_UNPACK_BUFFER); if result == gl::FALSE { return Err(PointCloudComputeError::UnknownError); } + // TODO: Changing the bounds here affected the "line" that is being rendered. gl::TexSubImage2D( gl::TEXTURE_2D, // target 0, // level @@ -328,6 +334,8 @@ impl PointCloudComputeShader { let width = xy_table.get_width_pixels() as i32; let height = xy_table.get_height_pixels() as i32; + println!("set_active_xy_table dims: {}x{}", width, height); + // Upload the XY table as a texture so we can use it as a uniform self.xy_table_texture.init(); @@ -376,7 +384,6 @@ impl PointCloudComputeShader { ); gl::BindBuffer(gl::PIXEL_UNPACK_BUFFER, 0); - gl::BindTexture(gl::TEXTURE_2D, self.depth_image_texture.id()); gl::TexStorage2D( @@ -434,9 +441,6 @@ impl PointCloudComputeShader { stride_bytes, ).map_err(|_| PointCloudComputeError::UnknownError)?; - // k4a_float2_t *tableData = reinterpret_cast(xyTable.get_buffer()); - let mut xy_table_buffer = xy_table.get_buffer(); - // typedef union // { // // XY or array representation of vector @@ -474,11 +478,14 @@ impl PointCloudComputeShader { }; let mut idx = 0; - - let length = width*height; + //let length = width*height; unsafe { - let mut xy_table_buffer2: *mut k4a_sys::k4a_float2_t = std::mem::transmute_copy(&xy_table_buffer); - let mut xy_table_buffer3 = std::slice::from_raw_parts_mut(xy_table_buffer2, length as usize); + //let mut xy_table_buffer2: *mut k4a_sys::k4a_float2_t = std::mem::transmute(&table_data); + //let mut xy_table_buffer3 = std::slice::from_raw_parts_mut(xy_table_buffer2, length as usize); + //let xy_table_buffer2 = table_data as *mut k4a_sys::k4a_float2_t; // TODO: Don't use transmute! + + let mut table_data = xy_table.get_buffer(); + let mut typed_buffer = table_data as *mut k4a_sys::k4a_float2_t; for y in 0..height { p.xy.y = y as f32; @@ -509,20 +516,55 @@ impl PointCloudComputeShader { /*if y % 10_000 == 0 { println!("This pixel is GOOD: {}, {}", ray.xyz.x, ray.xyz.y); }*/ - xy_table_buffer3[idx].xy.x = ray.xyz.x; - xy_table_buffer3[idx].xy.y = ray.xyz.y; + //typed_buffer[idx].xy.x = ray.xyz.x; + //typed_buffer[idx].xy.y = ray.xyz.y; + (*typed_buffer.offset(idx)).xy.x = ray.xyz.x; + (*typed_buffer.offset(idx)).xy.y = ray.xyz.y; + //xy_table_buffer3[idx].xy.x = 1.0; + //xy_table_buffer3[idx].xy.y = 1.0; } } else { unsafe { // This pixel is invalid - xy_table_buffer3[idx].xy.x = 0.0; - xy_table_buffer3[idx].xy.y = 0.0; + (*typed_buffer.offset(idx)).xy.x = 0.0; + (*typed_buffer.offset(idx)).xy.y = 0.0; } } idx += 1; } } + + //let byte_size = width * height * size_of::() as u32; + //std::ptr::write_bytes(table_data as *mut u8, 0, byte_size as usize); + /*println!("Table size: {} x {}", width , height); + + // k4a_float2_t *tableData = reinterpret_cast(xyTable.get_buffer()); + + println!("Created XY Table reports: {} x {} (stride = {}", + xy_table.get_width_pixels(), + xy_table.get_height_pixels(), + xy_table.get_stride_bytes(), + ); + + //let mut xy_table_buffer2: *mut k4a_sys::k4a_float2_t = std::mem::transmute::< k4a_sys::k4a_float2_t>(&table_data); + let mut xy_table_buffer2 = table_data as *mut k4a_sys::k4a_float2_t; + // TODO: When this is actually working, it should render a diagonal line... + let mut idx = 0; + for y in 0..height { + //println!("Y: {}", y); + for x in 0 .. width { + //println!("X: {}", x); + //println!("idx: {}", idx); + (*xy_table_buffer2.offset(idx)).xy.x = 0.0; + (*xy_table_buffer2.offset(idx)).xy.y = 0.0; + idx += 1; + } + } + + let byte_size = width * height * size_of::() as u32; + std::ptr::write_bytes(table_data, 0, byte_size as usize);*/ + } Ok(xy_table) diff --git a/src/point_cloud/point_cloud_renderer_shader.rs b/src/point_cloud/point_cloud_renderer_shader.rs index f1436e8c4c..05a3c96c9f 100644 --- a/src/point_cloud/point_cloud_renderer_shader.rs +++ b/src/point_cloud/point_cloud_renderer_shader.rs @@ -92,8 +92,8 @@ void main() ivec2 currentDepthPixelCoordinates = ivec2(gl_VertexID % pointCloudSize.x, gl_VertexID / pointCloudSize.x); vec3 vertexPosition = imageLoad(pointCloudTexture, currentDepthPixelCoordinates).xyz; - mat4 view2 = view; // Can't manipulate uniforms. - mat4 projection2 = view; // Can't manipulate uniforms. + //mat4 view2 = view; // Can't manipulate uniforms. + //mat4 projection2 = view; // Can't manipulate uniforms. //view2[0][3] = 2000.0f; //view2[1][3] = 2000.0f; @@ -105,6 +105,7 @@ void main() //gl_Position = projection2 * view2 * vec4(vertexPosition, 1); + gl_Position = projection * view * vec4(vertexPosition, 1); vertexColor = inColor; @@ -113,9 +114,9 @@ void main() // if (vertexPosition.z == 0.0f) { - //vertexColor.a = 0.0f; - vertexColor.a = 0.5f; - vertexColor.r = 0.5f; + vertexColor.a = 0.0f; + //vertexColor.a = 0.5f; + //vertexColor.r = 0.5f; } if (enableShading) @@ -409,11 +410,15 @@ impl PointCloudRendererShader { let mut color_src = color_image.get_buffer(); let result = unsafe { - //std::ptr::copy::(color_src, vertex_mapped_buffer as *mut u8, - // color_image_size_bytes as usize); - std::ptr::copy_nonoverlapping::(color_src, vertex_mapped_buffer as *mut u8, + std::ptr::copy::(color_src, vertex_mapped_buffer as *mut u8, color_image_size_bytes as usize); + //std::ptr::copy_nonoverlapping::(color_src, vertex_mapped_buffer as *mut u8, + // color_image_size_bytes as usize); + + // TODO: Based on writing pure white, this isn't the problem: + //std::ptr::write_bytes(vertex_mapped_buffer as *mut u8, 255, color_image_size_bytes as usize); + gl::UnmapBuffer(gl::ARRAY_BUFFER) }; @@ -432,12 +437,11 @@ impl PointCloudRendererShader { get_stride::(0), get_pointer_offset::(0), ); - gl::UseProgram(self.shader_program_id); - } - // Uniforms - // Bind our point cloud texture (which was written by the compute shader) - unsafe { + gl::UseProgram(self.shader_program_id); + + // Uniforms + // Bind our point cloud texture (which was written by the compute shader) gl::ActiveTexture(gl::TEXTURE0); gl::BindTexture(gl::TEXTURE_2D, point_cloud_texture.id()); gl::BindImageTexture( @@ -470,11 +474,10 @@ impl PointCloudRendererShader { // Update view/projection matrices in shader gl::UniformMatrix4fv(self.view_index, 1, gl::FALSE, self.view.as_ptr()); gl::UniformMatrix4fv(self.projection_index, 1, gl::FALSE, self.projection.as_ptr()); - // glUniformMatrix4fv(m_projectionIndex, 1, GL_FALSE, reinterpret_cast(m_projection)); // Update render settings in shader let enable_shading = if self.enable_shading { 1 } else { 0 }; - let enable_shading = 0; // TODO FIXME FIXME FIXME + //let enable_shading = 1; // TODO FIXME FIXME FIXME gl::Uniform1i(self.enable_shading_index, enable_shading); diff --git a/src/point_cloud/point_cloud_visualiser.rs b/src/point_cloud/point_cloud_visualiser.rs index fc634c5d86..778f4bdb72 100644 --- a/src/point_cloud/point_cloud_visualiser.rs +++ b/src/point_cloud/point_cloud_visualiser.rs @@ -137,6 +137,10 @@ impl Drop for CleanupGuard { } impl PointCloudVisualizer { + + /// + /// + /// pub fn new(enable_color_point_cloud: bool, initial_colorization_strategy: ColorizationStrategy, calibration_data: k4a_sys::k4a_calibration_t) -> Self @@ -205,14 +209,23 @@ impl PointCloudVisualizer { visualizer } + /// + /// + /// pub fn set_point_size(&mut self, point_size: u8) { self.point_cloud_renderer.set_point_size(point_size); } + /// + /// + /// pub fn update_texture(&mut self, texture: &ViewerImage, capture: Capture) -> Result<()> { self.update_texture_id(texture.texture_id(), capture) } + /// + /// + /// pub fn update_texture_id(&mut self, texture_id: GLuint, capture: Capture) -> Result<()> { // Update the point cloud renderer with the latest point data self.update_point_clouds(capture)?; @@ -267,6 +280,9 @@ impl PointCloudVisualizer { .map_err(|err| PointCloudVisualizerError::PointCloudRendererError(err)) } + /// + /// + /// fn update_point_clouds(&mut self, capture: Capture) -> Result<()> { let mut depth_image = match capture.get_depth_image() { Ok(img) => img, @@ -289,10 +305,10 @@ impl PointCloudVisualizer { .expect("Must be present");*/ if let Some(transformed_depth_image) = self.transformed_depth_image.as_ref() { - println!("Take transformed depth image... {}x{} [{:?}]", + /*println!("Take transformed depth image... {}x{} [{:?}]", transformed_depth_image.get_width_pixels(), transformed_depth_image.get_height_pixels(), - transformed_depth_image.get_format()); + transformed_depth_image.get_format());*/ unsafe { // TODO: Totally missed error handling here. @@ -307,7 +323,7 @@ impl PointCloudVisualizer { return Err(PointCloudVisualizerError::DepthToColorConversionFailed); } - println!("Saving transformed depth image: {:?}", transformed_depth_image.get_format()); + //println!("Saving transformed depth image: {:?}", transformed_depth_image.get_format()); depth_image = transformed_depth_image.clone(); } } @@ -381,6 +397,9 @@ impl PointCloudVisualizer { ).map_err(|err| PointCloudVisualizerError::PointCloudRendererError(err)) } + /// + /// + /// pub fn set_colorization_strategy(&mut self, strategy: ColorizationStrategy) -> Result<()> { if strategy == ColorizationStrategy::Color && !self.enable_color_point_cloud { return Err(PointCloudVisualizerError::UnsupportedMode); @@ -408,7 +427,7 @@ impl PointCloudVisualizer { let stride = width as i32 * size_of::() as i32; // Calibration resolution: 640 x 576 (stride: 2560) -- really!? That's low res. - println!("Calibration resolution: {} x {} (stride: {})", width, height, stride); + //println!("Calibration resolution: {} x {} (stride: {})", width, height, stride); self.point_cloud_colorization = Some(Image::create( ImageFormat::ColorBgra32,