//! The pointcloud Renderer for persistant pointcloud. //! It uses OpenGL.shader for fast computation. use kiss3d::camera::Camera; use kiss3d::context::Context; use kiss3d::renderer::Renderer; use kiss3d::resource::{Effect, GPUVec, ShaderAttribute, ShaderUniform}; use na::{Matrix4, Point3}; use nalgebra as na; /// Structure which manages the display of persistant Pointcloud. pub struct PointCloudRenderer { shader: Effect, pos: ShaderAttribute>, color: ShaderAttribute>, proj: ShaderUniform>, view: ShaderUniform>, colored_points: GPUVec>, point_size: f32, } impl PointCloudRenderer { /// Creates a new points renderer. pub fn new(point_size: f32, point_cloud_data: GPUVec>) -> PointCloudRenderer { let mut shader = Effect::new_from_str(VERTEX_SHADER_SRC, FRAGMENT_SHADER_SRC); shader.use_program(); PointCloudRenderer { colored_points: point_cloud_data, pos: shader.get_attrib::>("position").unwrap(), color: shader.get_attrib::>("color").unwrap(), proj: shader.get_uniform::>("proj").unwrap(), view: shader.get_uniform::>("view").unwrap(), shader, point_size, } } /// Compute the number of points in the PointCloud. pub fn num_points(&self) -> usize { self.colored_points.len() / 2 } } impl Renderer for PointCloudRenderer { /// Actually draws the points. fn render(&mut self, pass: usize, camera: &mut dyn Camera) { if self.colored_points.len() == 0 { return; } self.shader.use_program(); self.pos.enable(); self.color.enable(); camera.upload(pass, &mut self.proj, &mut self.view); self.color.bind_sub_buffer(&mut self.colored_points, 1, 1); self.pos.bind_sub_buffer(&mut self.colored_points, 1, 0); let ctxt = Context::get(); ctxt.point_size(self.point_size); ctxt.draw_arrays(Context::POINTS, 0, (self.colored_points.len() - 10) as i32); self.pos.disable(); self.color.disable(); } } const VERTEX_SHADER_SRC: &str = "#version 100 attribute vec3 position; attribute vec3 color; varying vec3 Color; uniform mat4 proj; uniform mat4 view; void main() { gl_Position = proj * view * vec4(position, 1.0); Color = color; }"; const FRAGMENT_SHADER_SRC: &str = "#version 100 #ifdef GL_FRAGMENT_PRECISION_HIGH precision highp float; #else precision mediump float; #endif varying vec3 Color; void main() { gl_FragColor = vec4(Color, 1.0); }";