mirror of
https://github.com/storytold/cloudcam.git
synced 2026-12-01 02:24:17 +01:00
point cloud code builds, but isn't correct yet
This commit is contained in:
1 parent
6c9740d86c
commit
3a56e9e0e2
1 file changed
+210
-58
@@ -1,4 +1,8 @@
|
||||
//! This is a port of libk4a's `tools/k4aviewer/graphics/shaders/gpudepthtopointcloudconverter.h`.
|
||||
//! This code turns depth images into point clouds.
|
||||
|
||||
use std::ffi::CString;
|
||||
use std::os::raw::c_void;
|
||||
use std::ptr;
|
||||
use std::str;
|
||||
use libc;
|
||||
@@ -9,6 +13,27 @@ use gl::types::*;
|
||||
use k4a_sys_wrapper;
|
||||
use std::mem::size_of;
|
||||
use std::ptr::null;
|
||||
use std::fmt::{Formatter, Error};
|
||||
|
||||
pub type Result<T> = std::result::Result<T,PointCloudError>;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum PointCloudError {
|
||||
UnknownError,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for PointCloudError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "point cloud error")
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for PointCloudError {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error +'static)> {
|
||||
// Generic error, no backtrace.
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// This is taken from Microsoft's MIT-licensed k4a libraries.
|
||||
/// From the file `tools/k4aviewer/graphics/shaders/gpudepthtopointcloudconverter.cpp`
|
||||
@@ -59,6 +84,17 @@ const POINT_CLOUD_TEXTURE_FORMAT : GLuint = gl::RGBA32F;
|
||||
pub struct PointCloudComputeShader {
|
||||
program_id: GLuint,
|
||||
shader_id: GLuint,
|
||||
|
||||
xy_table_texture_id: Option<GLuint>,
|
||||
depth_image_texture_id: Option<GLuint>,
|
||||
depth_image_pixel_buffer_id: Option<GLuint>,
|
||||
|
||||
// TODO: Not sure if I need all of these things...
|
||||
dest_tex_id: Option<GLint>,
|
||||
xy_table_id: Option<GLint>,
|
||||
depth_image_id: Option<GLint>,
|
||||
|
||||
// TODO: I invented this. Not sure if I need it.
|
||||
output_texture: GLuint,
|
||||
}
|
||||
|
||||
@@ -75,8 +111,14 @@ impl PointCloudComputeShader {
|
||||
unsafe { gl::GenTextures(1, &mut output_texture); }
|
||||
|
||||
PointCloudComputeShader {
|
||||
shader_id: shader,
|
||||
program_id: program,
|
||||
shader_id: shader,
|
||||
dest_tex_id: None,
|
||||
xy_table_id: None,
|
||||
depth_image_id: None,
|
||||
depth_image_texture_id: None,
|
||||
depth_image_pixel_buffer_id: None,
|
||||
xy_table_texture_id: None,
|
||||
output_texture,
|
||||
}
|
||||
}
|
||||
@@ -91,15 +133,85 @@ impl PointCloudComputeShader {
|
||||
// p is the index of a given pixel.
|
||||
//
|
||||
pub fn generate_xy_table(calibration: k4a_sys::k4a_calibration_t,
|
||||
calibration_type: k4a_sys::k4a_calibration_type_t) -> Result<k4a_sys_wrapper::Image, ()> {
|
||||
calibration_type: k4a_sys::k4a_calibration_type_t) -> Result<k4a_sys_wrapper::Image> {
|
||||
|
||||
// TODO: Just a static function
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
// Set the XY table that will be used by future calls to Convert(). Get an XY table by calling
|
||||
// GenerateXyTable().
|
||||
pub fn set_active_xy_table(&mut self, xy_table: k4a_sys_wrapper::Image) {
|
||||
|
||||
let width = xy_table.get_width_pixels();
|
||||
let height = xy_table.get_height_pixels();
|
||||
|
||||
// OpenGL resource DSL
|
||||
//
|
||||
// OpenGL::Texture m_depthImageTexture;
|
||||
//
|
||||
// m_xyTableTexture.Init();
|
||||
//
|
||||
// ----- openglhelpers.h -----
|
||||
// using Texture = Internal::BasicWrapper<Internal::GenTextures, Internal::DeleteTextures>;
|
||||
//
|
||||
// template<void (*createFn)(GLsizei, GLuint *), void (*deleteFn)(GLsizei, GLuint *)> class BasicWrapper
|
||||
//
|
||||
// inline void GenTextures(GLsizei n, GLuint *textures)
|
||||
// {
|
||||
// glGenTextures(n, textures);
|
||||
// }
|
||||
//
|
||||
// void Init()
|
||||
// {
|
||||
// Reset();
|
||||
// createFn(1, &m_id);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// void Reset()
|
||||
// {
|
||||
// if (m_id != 0)
|
||||
// {
|
||||
// deleteFn(1, &m_id);
|
||||
// m_id = 0;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
|
||||
// Upload the XY table as a texture so we can use it as a uniform
|
||||
|
||||
unsafe {
|
||||
let mut xy_table_texture_id = 0;
|
||||
gl::GenTextures(1, &mut xy_table_texture_id);
|
||||
self.xy_table_texture_id = Some(xy_table_texture_id);
|
||||
|
||||
gl::BindTexture(gl::TEXTURE_2D, xy_table_texture_id);
|
||||
// constexpr GLenum xyTableInternalFormat = GL_RG32F;
|
||||
gl::TexStorage2D(gl::TEXTURE_2D, 1, gl::RGB32F, width as i32, height as i32);
|
||||
|
||||
let xy_table_buffer = xy_table.get_buffer();
|
||||
|
||||
gl::TexSubImage2D(
|
||||
gl::TEXTURE_2D,
|
||||
0, // level
|
||||
0, // xoffset
|
||||
0, // yoffset
|
||||
width as i32,
|
||||
height as i32,
|
||||
gl::RG, //constexpr GLenum xyTableDataFormat = GL_RG;
|
||||
gl::FLOAT, //constexpr GLenum xyTableDataType = GL_FLOAT;
|
||||
xy_table_buffer as *const c_void,
|
||||
);
|
||||
|
||||
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::NEAREST as i32);
|
||||
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::NEAREST as i32);
|
||||
|
||||
// TODO: Continue implementation...
|
||||
}
|
||||
|
||||
unimplemented!();
|
||||
|
||||
}
|
||||
|
||||
// Takes depth data and turns it into a texture containing the XYZ coordinates of the depth map
|
||||
@@ -118,86 +230,126 @@ impl PointCloudComputeShader {
|
||||
// by this function, provided the depth image and XY table previously used was for the same
|
||||
// sized texture.
|
||||
//
|
||||
pub fn convert(&self, depth_image: k4a_sys_wrapper::Image) -> Result<(),()> {
|
||||
/*// TODO xy_table_texture
|
||||
pub fn convert(&self, depth_image: k4a_sys_wrapper::Image) -> Result<()> {
|
||||
// TODO xy_table_texture
|
||||
|
||||
let width = depth_image.get_width_pixels();
|
||||
let height = depth_image.get_height_pixels();
|
||||
|
||||
// TODO - init output_texture
|
||||
gl::ActiveTexture(gl::TEXTURE0);
|
||||
gl::BindTexture(gl::TEXTURE_2D, self.output_texture);
|
||||
unsafe {
|
||||
// TODO - init output_texture
|
||||
gl::ActiveTexture(gl::TEXTURE0);
|
||||
gl::BindTexture(gl::TEXTURE_2D, self.output_texture);
|
||||
|
||||
// The format that the point cloud texture uses internally to store points.
|
||||
// If you want to use the texture that this outputs from your shader, you
|
||||
// need to pass this as the format argument to glBindImageTexture().
|
||||
// static constexpr GLenum PointCloudTextureFormat = GL_RGBA32F;
|
||||
gl::TexStorage2d(gl::TEXTURE_2D, 1, POINT_CLOUD_TEXTURE_FORMAT, width, height);
|
||||
// The format that the point cloud texture uses internally to store points.
|
||||
// If you want to use the texture that this outputs from your shader, you
|
||||
// need to pass this as the format argument to glBindImageTexture().
|
||||
// static constexpr GLenum PointCloudTextureFormat = GL_RGBA32F;
|
||||
gl::TexStorage2D(gl::TEXTURE_2D, 1, POINT_CLOUD_TEXTURE_FORMAT, width as i32, height as i32);
|
||||
|
||||
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::NEAREST);
|
||||
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::NEAREST);
|
||||
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::NEAREST as i32);
|
||||
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::NEAREST as i32);
|
||||
|
||||
// Upload data to uniform texture
|
||||
gl::BindBuffer(gl::PIXEL_UNPACK_BUFFER, depth_image_pixel_buffer);
|
||||
gl::BindTexture(gl::TEXTURE_2D, depth_image_texture);
|
||||
let depth_image_pixel_buffer_id = self.depth_image_pixel_buffer_id
|
||||
.ok_or(PointCloudError::UnknownError)?;
|
||||
let depth_image_texture_id = self.depth_image_texture_id
|
||||
.ok_or(PointCloudError::UnknownError)?;
|
||||
let depth_image_id = self.depth_image_id
|
||||
.ok_or(PointCloudError::UnknownError)?;
|
||||
let xy_table_texture_id = self.xy_table_texture_id
|
||||
.ok_or(PointCloudError::UnknownError)?;
|
||||
let xy_table_id = self.xy_table_id
|
||||
.ok_or(PointCloudError::UnknownError)?;
|
||||
|
||||
let num_bytes : GLuint = width * height * size_of::<u16>(); // libc::uint16_t = u16
|
||||
// Upload data to uniform texture
|
||||
gl::BindBuffer(gl::PIXEL_UNPACK_BUFFER, depth_image_pixel_buffer_id);
|
||||
gl::BindTexture(gl::TEXTURE_2D, depth_image_texture_id);
|
||||
|
||||
// TODO: GLubyte *textureMappedBuffer = reinterpret_cast<GLubyte *>(...)
|
||||
let texture_mapped_buffer =
|
||||
gl::MapBufferRange(gl::PIXEL_UNPACK_BUFFER, 0, gl::MAP_WRITE_BIT | gl::MAP_INVALIDATE_BUFFER_BIT);
|
||||
let num_bytes: GLuint = (width * height * size_of::<u16>()) as GLuint; // libc::uint16_t = u16
|
||||
|
||||
// TODO: Handle error.
|
||||
// TODO: GLubyte *textureMappedBuffer = reinterpret_cast<GLubyte *>(...)
|
||||
let texture_mapped_buffer = gl::MapBufferRange(
|
||||
gl::PIXEL_UNPACK_BUFFER,
|
||||
0,
|
||||
num_bytes as isize,
|
||||
gl::MAP_WRITE_BIT | gl::MAP_INVALIDATE_BUFFER_BIT
|
||||
);
|
||||
|
||||
let mut depth_src = depth_image.get_buffer();
|
||||
// TODO: Handle error.
|
||||
|
||||
// TODO: std::copy(depthSrc, depthSrc + numBytes, textureMappedBuffer);
|
||||
let mut depth_src = depth_image.get_buffer();
|
||||
|
||||
gl::UnmapBuffer(gl::PIXEL_UNPACK_BUFFER);
|
||||
// TODO: std::copy(depthSrc, depthSrc + numBytes, textureMappedBuffer);
|
||||
|
||||
// TODO: Handle error.
|
||||
gl::UnmapBuffer(gl::PIXEL_UNPACK_BUFFER);
|
||||
|
||||
gl::TexSubImage2D(
|
||||
gl::TEXTURE_2D, // target
|
||||
0, // level
|
||||
0, // x offset
|
||||
0, // y offset
|
||||
width,
|
||||
height,
|
||||
depth_image_data_format,
|
||||
depth_image_data_type,
|
||||
null(), // data
|
||||
);
|
||||
// TODO: Handle error.
|
||||
|
||||
gl::BindBuffer(gl::PIXEL_UNPACK_BUFFER, 0);
|
||||
gl::TexSubImage2D(
|
||||
gl::TEXTURE_2D, // target
|
||||
0, // level
|
||||
0, // x offset
|
||||
0, // y offset
|
||||
width as i32,
|
||||
height as i32,
|
||||
gl::RED_INTEGER, //constexpr GLenum depthImageDataFormat = GL_RED_INTEGER;
|
||||
gl::UNSIGNED_SHORT, //constexpr GLenum depthImageDataType = GL_UNSIGNED_SHORT;
|
||||
null(), // data
|
||||
);
|
||||
|
||||
gl::UseProgram(self.shader_id);
|
||||
gl::BindBuffer(gl::PIXEL_UNPACK_BUFFER, 0);
|
||||
|
||||
// Bind textures that we're going to pass to the texture
|
||||
gl::ActiveTexture(gl::TEXTURE0);
|
||||
gl::BindTexture(gl::TEXTURE_2D, self.output_texture);
|
||||
gl::BindImageTexture(0, self.output_texture, 0, gl::FALSE, 0, gl::WRITE_ONLY, POINT_CLOUD_TEXTURE_FORMAT);
|
||||
gl::UseProgram(self.shader_id);
|
||||
|
||||
gl::ActiveTexture(gl::TEXTURE1);
|
||||
gl::BindTexture(gl::TEXTURE_2D, depth_image_texture);
|
||||
gl::BindImageTexture(1, depth_image_texture, 0, gl::FALSE, 0, gl::READ_ONLY, depth_image_internal_format);
|
||||
gl::Uniform1i(depth_image_id, 1);
|
||||
// Bind textures that we're going to pass to the texture
|
||||
gl::ActiveTexture(gl::TEXTURE0);
|
||||
gl::BindTexture(gl::TEXTURE_2D, self.output_texture);
|
||||
gl::BindImageTexture(
|
||||
0,
|
||||
self.output_texture,
|
||||
0,
|
||||
gl::FALSE,
|
||||
0,
|
||||
gl::WRITE_ONLY,
|
||||
POINT_CLOUD_TEXTURE_FORMAT
|
||||
);
|
||||
|
||||
gl::ActiveTexture(GL_TEXTURE2);
|
||||
gl::BindTexture(GL_TEXTURE_2D, xy_table_texture_id);
|
||||
gl::BindImageTexture(2, xy_table_texture_id, 0, gl::FALSE, 0, gl::READ_ONLY, xy_table_internal_format);
|
||||
gl::Uniform1i(xy_table_id, 2);
|
||||
gl::ActiveTexture(gl::TEXTURE1);
|
||||
gl::BindTexture(gl::TEXTURE_2D, depth_image_texture_id);
|
||||
gl::BindImageTexture(
|
||||
1,
|
||||
depth_image_texture_id,
|
||||
0,
|
||||
gl::FALSE,
|
||||
0,
|
||||
gl::READ_ONLY,
|
||||
gl::R16UI, //constexpr GLenum depthImageInternalFormat = GL_R16UI;
|
||||
);
|
||||
gl::Uniform1i(depth_image_id, 1);
|
||||
|
||||
// Render point cloud
|
||||
//gl::DispatchCompute(static_cast<GLuint>(depth.get_width_pixels()), static_cast<GLuint>(depth.get_height_pixels()), 1);
|
||||
gl::ActiveTexture(gl::TEXTURE2);
|
||||
gl::BindTexture(gl::TEXTURE_2D, xy_table_texture_id);
|
||||
gl::BindImageTexture(
|
||||
2,
|
||||
xy_table_texture_id,
|
||||
0,
|
||||
gl::FALSE,
|
||||
0,
|
||||
gl::READ_ONLY,
|
||||
gl::RG32F, //constexpr GLenum xyTableInternalFormat = GL_RG32F;
|
||||
);
|
||||
gl::Uniform1i(xy_table_id, 2);
|
||||
|
||||
// Wait for the rendering to finish before allowing reads to the texture we just wrote
|
||||
gl::MemoryBarrier(gl::TEXTURE_FETCH_BARRIER_BIT);
|
||||
// Render point cloud
|
||||
//gl::DispatchCompute(static_cast<GLuint>(depth.get_width_pixels()), static_cast<GLuint>(depth.get_height_pixels()), 1);
|
||||
|
||||
let status = gl::GetError();
|
||||
// Wait for the rendering to finish before allowing reads to the texture we just wrote
|
||||
gl::MemoryBarrier(gl::TEXTURE_FETCH_BARRIER_BIT);
|
||||
|
||||
*/
|
||||
unimplemented!();
|
||||
let status = gl::GetError();
|
||||
|
||||
Err(PointCloudError::UnknownError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user