implementation of simple/color shading (perfect!)

This commit is contained in:
Brandon Thomas committed 2020-04-26 21:28:16 -04:00
1 parent c591d0d6c1
commit 4f3e5f0e5c
6 files changed
+109 -62

No files matched your search

+1 -1
View File
@@ -59,7 +59,7 @@ pub fn run(capture_provider: Arc<CaptureProvider>, calibration_data: k4a_sys::k4
let mut visualizer = PointCloudVisualizer::new(
true,
ColorizationStrategy::Color,
ColorizationStrategy::Simple,
calibration_data
);
+8 -13
View File
@@ -88,9 +88,8 @@ void main()
//
if (xyValue.x == 0.0f && xyValue.y == 0.0f)
{
// TODO: FIXME
//alpha = 0.0f;
//vertexValue = 0.0f;
alpha = 0.0f;
vertexValue = 0.0f;
}
// Vertex positions are in millimeters, but everything else is in meters, so we need to convert
@@ -165,19 +164,15 @@ impl GpuPointCloudConverter {
depth_image_id = gl::GetUniformLocation(program_id, DEPTH_IMAGE_PTR);
}
println!("Uniform: dest_tex_id location = {:?}", dest_tex_id);
println!("Uniform: xy_table_id location = {:?}", xy_table_id);
println!("Uniform: depth_image_id location = {:?}", depth_image_id);
GpuPointCloudConverter {
shader_program_id: program_id,
//shader_id,
//dest_tex_id,
xy_table_id,
depth_image_id,
depth_image_texture: Texture::new_initialized(),
xy_table_texture: Texture::new_initialized(),
depth_image_pixel_buffer: Buffer::new_initialized(),
depth_image_texture: Texture::new(),
xy_table_texture: Texture::new(),
depth_image_pixel_buffer: Buffer::new(),
}
}
@@ -387,7 +382,7 @@ impl GpuPointCloudConverter {
let height = xy_table.get_height_pixels() as i32;
// Upload the XY table as a texture so we can use it as a uniform
//self.xy_table_texture.init();
self.xy_table_texture.init();
unsafe {
gl::BindTexture(gl::TEXTURE_2D, self.xy_table_texture.id());
@@ -419,8 +414,8 @@ impl GpuPointCloudConverter {
// Pre-allocate a texture for the depth images so we don't have to
// reallocate on every frame
//self.depth_image_texture.init();
//self.depth_image_pixel_buffer.init();
self.depth_image_texture.init();
self.depth_image_pixel_buffer.init();
let depth_image_size_bytes = (width * height * size_of::<u16>() as i32) as isize; // libc::uint16_t = u16
+2 -1
View File
@@ -3,4 +3,5 @@ pub mod point_cloud_renderer;
pub mod point_cloud_visualiser;
pub mod compile_shader;
pub mod pixel_structs;
pub mod viewer_image;
pub mod viewer_image;
pub mod util;
+14 -29
View File
@@ -92,36 +92,17 @@ void main()
{
ivec2 pointCloudSize = imageSize(pointCloudTexture);
ivec2 currentDepthPixelCoordinates = ivec2(gl_VertexID % pointCloudSize.x, gl_VertexID / pointCloudSize.x);
//ivec2 currentDepthPixelCoordinates = ivec2(gl_VertexID / pointCloudSize.y, gl_VertexID % pointCloudSize.y);
vec3 vertexPosition = imageLoad(pointCloudTexture, currentDepthPixelCoordinates).xyz;
//mat4 view2 = view; // Can't manipulate uniforms.
//mat4 projection2 = view; // Can't manipulate uniforms.
//view2[0][3] = 2000.0f;
//view2[1][3] = 2000.0f;
//view2[2][3] = 8000.0f;
//projection2[0][0] = 109349.0f;
//projection2[1][3] = -100349.0f;
//projection2[3][2] = 10049.0f;
//gl_Position = projection2 * view2 * vec4(vertexPosition, 1);
gl_Position = projection * view * vec4(vertexPosition, 1);
//gl_Position = vec4(vertexPosition, 1);
//gl_Position = gl_VertexID;
vertexColor = inColor;
//vertexColor = vec4(1.0, 0.5, 0.5, 1.0);
// Pass along the 'invalid pixel' flag as the alpha channel
//
if (vertexPosition.z == 0.0f)
{
//vertexColor.a = 0.0f;
//vertexColor.a = 0.5f;
//vertexColor.r = 0.5f;
vertexColor.a = 0.0f;
}
if (enableShading)
@@ -194,10 +175,9 @@ void main()
{
if (vertexColor.a == 0.0f)
{
//discard;
discard;
}
//fragmentColor = vec4(0.5, 1.0, 0.0, 1.0);
fragmentColor = vertexColor;
}
";
@@ -352,7 +332,7 @@ impl PointCloudRenderer {
shader_program_id: program_id,
vertex_shader_id,
fragment_shader_id,
point_size: 5,
point_size: 2,
enable_shading: false,
vertex_array_size_bytes: 0,
view_index,
@@ -385,11 +365,11 @@ impl PointCloudRenderer {
.save(Path::new("debug_images/final_color_image.png"))
.expect("should save");*/
println!("update_point_clouds() color_image: {}x{} (format={:?})",
/*println!("update_point_clouds() color_image: {}x{} (format={:?})",
color_image.get_width_pixels(),
color_image.get_height_pixels(),
color_image.get_format());
println!("updating point_cloud_texture: {}", point_cloud_texture.id());
println!("updating point_cloud_texture: {}", point_cloud_texture.id());*/
unsafe {
gl::BindVertexArray(self.vertex_array_object.id());
@@ -399,7 +379,7 @@ impl PointCloudRenderer {
}
let color_image_size_bytes = color_image.get_size() as i32; // TODO: Seems wrong!
let color_image_size_bytes = ( color_image.get_width_pixels() * color_image.get_height_pixels() * size_of::<BgraPixel>() ) as i32;
//let color_image_size_bytes = ( color_image.get_width_pixels() * color_image.get_height_pixels() * size_of::<BgraPixel>() ) as i32;
if self.vertex_array_size_bytes != color_image_size_bytes {
self.vertex_array_size_bytes = color_image_size_bytes;
@@ -420,7 +400,7 @@ impl PointCloudRenderer {
0,
color_image_size_bytes as isize,
gl::MAP_WRITE_BIT | gl::MAP_INVALIDATE_BUFFER_BIT
)
) as *mut GLubyte
};
if vertex_mapped_buffer as usize == 0 {
@@ -429,6 +409,9 @@ impl PointCloudRenderer {
}
let mut color_src = color_image.get_buffer();
//let mut typed_color_src = color_src as *const BgraPixel;
let mut typed_color_src = color_src as *const GLubyte;
//let length = color_image.get_width_pixels() * color_image.get_height_pixels();
let result = unsafe {
//std::ptr::copy_nonoverlapping::<u8>(color_src, vertex_mapped_buffer as *mut u8,
@@ -437,7 +420,9 @@ impl PointCloudRenderer {
// TODO TESTING - writing pure white changes the color of the final output "line" to white:
//std::ptr::write_bytes(vertex_mapped_buffer as *mut u8, 255, color_image_size_bytes as usize);
std::ptr::copy::<u8>(color_src, vertex_mapped_buffer as *mut u8, color_image_size_bytes as usize);
//std::ptr::copy::<u8>(color_src, vertex_mapped_buffer as *mut u8, color_image_size_bytes as usize);
std::ptr::copy::<GLubyte>(typed_color_src, vertex_mapped_buffer, color_image_size_bytes as usize);
gl::UnmapBuffer(gl::ARRAY_BUFFER)
};
@@ -506,7 +491,7 @@ impl PointCloudRenderer {
// Render point cloud
gl::BindVertexArray(self.vertex_array_object.id());
let size = self.vertex_array_size_bytes;// / size_of::<BgraPixel>() as i32; // todo // todo // todo // todo
let size = self.vertex_array_size_bytes / size_of::<BgraPixel>() as i32;
gl::DrawArrays(gl::POINTS, 0, size);
gl::BindVertexArray(0);
+16 -18
View File
@@ -17,6 +17,7 @@ use std::fmt::{Error, Formatter};
use std::mem::size_of;
use conversion::k4a_image_to_rust_image_for_debug;
use std::path::Path;
use point_cloud::util::{ValueRange, get_depth_mode_range, colorize_depth_blue_to_red};
pub type Result<T> = std::result::Result<T, PointCloudVisualizerError>;
@@ -89,7 +90,6 @@ pub enum ColorizationStrategy {
}
pub struct PointCloudVisualizer {
// TODO: std::pair<DepthPixel, DepthPixel> m_expectedValueRange;
// TODO: ViewControl m_viewControl;
// TODO: linmath::mat4x4 m_projection{};
// TODO: linmath::mat4x4 m_view{};
@@ -125,6 +125,9 @@ pub struct PointCloudVisualizer {
color_xy_table: Image,
depth_xy_table: Image,
/// Near and far minima/maxima for the current depth sensor mode.
depth_value_range: ValueRange,
}
// TODO: Dedup
@@ -181,6 +184,9 @@ impl PointCloudVisualizer {
k4a_sys::k4a_calibration_type_t_K4A_CALIBRATION_TYPE_DEPTH,
).unwrap();
let expected_value_range = get_depth_mode_range(calibration_data.depth_mode)
.expect("Should be in correct depth sensor mode.");
let mut visualizer = Self {
m_dimensions_width: width, // Resolution of the point cloud texture
m_dimensions_height: height, // ImageDimensions PointCloudVisualizerTextureDimensions = { 1280, 1152 };
@@ -198,6 +204,7 @@ impl PointCloudVisualizer {
transformed_depth_image: None,
point_cloud_colorization: None,
xyz_texture: Texture::new(),
depth_value_range: expected_value_range,
};
visualizer.set_colorization_strategy(initial_colorization_strategy).expect("Should work");
@@ -362,8 +369,8 @@ impl PointCloudVisualizer {
unsafe {
// src: DepthPixel
//let mut src_pixel_buffer = depth_image.get_buffer();
//let mut typed_src_pixel_buffer = src_pixel_buffer as *const DepthPixel;
let mut src_pixel_buffer = depth_image.get_buffer();
let mut typed_src_pixel_buffer = src_pixel_buffer as *const DepthPixel;
// dst: BgraPixel
let mut dst_pixel_buffer = self.point_cloud_colorization
@@ -378,22 +385,13 @@ impl PointCloudVisualizer {
let mut typed_dst_pixel_buffer = dst_pixel_buffer as *mut BgraPixel;
// TODO: This should help us see output.
// TODO: This requires implementation of `K4ADepthPixelColorizer::ColorizeBlueToRed()`
for i in 0 .. dst_length as isize {
/*let src_pixel = (*typed_src_pixel_buffer.offset(i));
// Default to opaque black
let output_pixel = BgraPixel {
blue: 0,
green: 0,
red: 0,
alpha: 255,
};*/
(*typed_dst_pixel_buffer.offset(i)).red = 255;
(*typed_dst_pixel_buffer.offset(i)).green = 0;
(*typed_dst_pixel_buffer.offset(i)).blue = 0;
(*typed_dst_pixel_buffer.offset(i)).alpha = 255;
let src_pixel = *typed_src_pixel_buffer.offset(i);
let pixel = colorize_depth_blue_to_red(src_pixel, &self.depth_value_range);
(*typed_dst_pixel_buffer.offset(i)).red = pixel.red;
(*typed_dst_pixel_buffer.offset(i)).green = pixel.green;
(*typed_dst_pixel_buffer.offset(i)).blue = pixel.blue;
(*typed_dst_pixel_buffer.offset(i)).alpha = pixel.alpha;
}
/*k4a_image_to_rust_image_for_debug(self.point_cloud_colorization.as_ref().expect("should exist"))
+68
View File
@@ -0,0 +1,68 @@
use point_cloud::pixel_structs::{DepthPixel, BgraPixel};
use imgui::sys::igColorConvertHSVtoRGB;
/// Ranges of values we can expect from the depth sensor, depending on its current configuration.
pub struct ValueRange {
/// Lowest (nearest) value reported from the depth sensor in millimeters.
pub near: DepthPixel,
/// Highest (furthest) value reported from the depth sensor in millimeters.
pub far: DepthPixel,
}
/// Gets the range of values that we expect to see from the depth camera
/// when using a given depth mode, in millimeters
pub fn get_depth_mode_range(depth_mode: k4a_sys::k4a_depth_mode_t) -> Option<ValueRange> {
match depth_mode {
k4a_sys::k4a_depth_mode_t_K4A_DEPTH_MODE_NFOV_2X2BINNED => Some(ValueRange { near: 500, far: 5800 }),
k4a_sys::k4a_depth_mode_t_K4A_DEPTH_MODE_NFOV_UNBINNED => Some(ValueRange { near: 500, far: 4000 }),
k4a_sys::k4a_depth_mode_t_K4A_DEPTH_MODE_WFOV_2X2BINNED => Some(ValueRange { near: 250, far: 3000 }),
k4a_sys::k4a_depth_mode_t_K4A_DEPTH_MODE_WFOV_UNBINNED => Some(ValueRange { near: 250, far: 2500 }),
k4a_sys::k4a_depth_mode_t_K4A_DEPTH_MODE_PASSIVE_IR => None, // TODO: Return error
_ => None, // TODO: Return error
}
}
/// This is a port of Microsoft's `ColorizeBlueToRed` in the open source / MIT-licensed libk4a
pub fn colorize_depth_blue_to_red(depth_pixel: DepthPixel, value_range: &ValueRange) -> BgraPixel {
let pixel_max = libc::uint8_t::max_value();
let mut result = BgraPixel { blue: 0, green: 0, red: 0, alpha: pixel_max };
if depth_pixel == 0 {
// If the pixel is actually zero and not just below the min value, make it black.
return result;
}
let clamped_value = depth_pixel
.min(value_range.far)
.max(value_range.near);
// Normalize to [0, 1]
let mut hue = (clamped_value - value_range.near) as f32 / (value_range.far - value_range.near) as f32;
// The 'hue' coordinate in HSV is a polar coordinate, so it 'wraps'.
// Purple starts after blue and is close enough to red to be a bit unclear,
// so we want to go from blue to red. Purple starts around .6666667,
// so we want to normalize to [0, .6666667].
let range = 2.0f32 / 3.0;
hue *= range;
// We want blue to be close and red to be far, so we need to reflect the
// hue across the middle of the range.
hue = range - hue;
let mut red = 0.0f32;
let mut green= 0.0f32;
let mut blue = 0.0f32;
unsafe {
igColorConvertHSVtoRGB(hue, 1.0, 1.0, &mut red, &mut green, &mut blue);
}
let pixel_max = pixel_max as f32;
result.red = (red * pixel_max) as u8;
result.green = (green * pixel_max) as u8;
result.blue = (blue * pixel_max) as u8;
result
}