From 3f143011a0272f0aeb98bbdbdd09ac7d1b6ebbfb Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Thu, 20 Aug 2020 23:51:25 -0400 Subject: [PATCH] break out renderableobject as its own thing --- Cargo.lock | 90 +++++++++++++++++++++++ Cargo.toml | 3 +- src/assets/mod.rs | 3 +- src/assets/renderable_object.rs | 93 ++++++++++++++++++++++++ src/point_cloud/point_cloud_renderer.rs | 97 +------------------------ 5 files changed, 190 insertions(+), 96 deletions(-) create mode 100644 src/assets/renderable_object.rs diff --git a/Cargo.lock b/Cargo.lock index 8a61c18ab0..defba50785 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -105,6 +105,11 @@ dependencies = [ "libc 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "base64" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "bindgen" version = "0.52.0" @@ -575,6 +580,40 @@ name = "glob" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "gltf" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "base64 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "gltf-json 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)", + "image 0.23.2 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "gltf-derive" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "inflections 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "gltf-json" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gltf-derive 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.113 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "glutin" version = "0.23.0" @@ -763,6 +802,11 @@ dependencies = [ "adler32 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "inflections" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "instant" version = "0.1.2" @@ -776,6 +820,11 @@ dependencies = [ "libc 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "itoa" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "jobserver" version = "0.1.21" @@ -1469,6 +1518,11 @@ dependencies = [ "stb_truetype 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "same-file" version = "1.0.6" @@ -1520,6 +1574,31 @@ name = "semver-parser" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "serde" +version = "1.0.115" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde_derive" +version = "1.0.113" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_json" +version = "1.0.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "itoa 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "ryu 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "shared_library" version = "0.1.9" @@ -1580,6 +1659,7 @@ dependencies = [ "glfw 0.37.0 (registry+https://github.com/rust-lang/crates.io-index)", "glfw-sys 3.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "glium 0.26.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gltf 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)", "image 0.23.2 (registry+https://github.com/rust-lang/crates.io-index)", "imgui 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "imgui-glium-renderer 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1927,6 +2007,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" "checksum backtrace 0.3.46 (registry+https://github.com/rust-lang/crates.io-index)" = "b1e692897359247cc6bb902933361652380af0f1b7651ae5c5013407f30e109e" "checksum backtrace-sys 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "7de8aba10a69c8e8d7622c5710229485ec32e9d55fdad160ea559c086fdcd118" +"checksum base64 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7" "checksum bindgen 0.52.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f1c85344eb535a31b62f0af37be84441ba9e7f0f4111eb0530f43d15e513fe57" "checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" "checksum block 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" @@ -1979,6 +2060,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum glfw-sys 3.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0b567b13b593ec58ea912b2658ee6230ffe20a069fa5b771800acc69bb3a157e" "checksum glium 0.26.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a46e88b2c64cfa63d54ed54dc2a0d25136470eb1721e10be49d857ab583f8663" "checksum glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" +"checksum gltf 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d6fb0d1d772daf10ea74528c3aeb12215f6d5b820adf2ecfc93a6578d6779c3c" +"checksum gltf-derive 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6636de7bf52227363554f1ca2d9cd180fc666129ddd0933097e1f227dfa7293" +"checksum gltf-json 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e3fc3deb81e6fa04bf808f6be7c3983229552a95b77f687ad96af00f6d3e7d6c" "checksum glutin 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cf22d4e90c55d9be9f2ad52410e7a2c0d7e9c99d93a13df73a672e7ef4e8c7f7" "checksum glutin_egl_sys 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "772edef3b28b8ad41e4ea202748e65eefe8e5ffd1f4535f1219793dbb20b3d4c" "checksum glutin_emscripten_sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "80de4146df76e8a6c32b03007bc764ff3249dcaeb4f675d68a06caf1bac363f1" @@ -1998,8 +2082,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum imgui-winit-support 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5685c7d17aee9555377f9c4f7b6fac96899a7015f6b41950c613a3df034aff08" "checksum indexmap 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "86b45e59b16c76b11bf9738fd5d38879d3bd28ad292d7b313608becb17ae2df9" "checksum inflate 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "1cdb29978cc5797bd8dcc8e5bf7de604891df2a8dc576973d71a281e916db2ff" +"checksum inflections 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a257582fdcde896fd96463bf2d40eefea0580021c0712a0e2b028b60b47a837a" "checksum instant 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6c346c299e3fe8ef94dc10c2c0253d858a69aac1245157a3bf4125915d528caf" "checksum iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" +"checksum itoa 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" "checksum jobserver 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)" = "5c71313ebb9439f74b00d9d2dcec36440beaf57a6aa0623068441dd7cd81a7f2" "checksum jpeg-decoder 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "0256f0aec7352539102a9efbcb75543227b7ab1117e0f95450023af730128451" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" @@ -2080,6 +2166,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rustc-hash 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" "checksum rusttype 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)" = "310942406a39981bed7e12b09182a221a29e0990f3e7e0c971f131922ed135d5" "checksum rusttype 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "14a911032fb5791ccbeec9f28fdcb9bf0983b81f227bafdfd227c658d0731c8a" +"checksum ryu 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" "checksum same-file 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" "checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" "checksum scopeguard 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" @@ -2087,6 +2174,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum sdl2-sys 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c2e1deb61ff274d29fb985017d4611d4004b113676eaa9c06754194caf82094e" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" +"checksum serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)" = "e54c9a88f2da7238af84b5101443f0c0d0a3bbdc455e34a5c9497b1903ed55d5" +"checksum serde_derive 1.0.113 (registry+https://github.com/rust-lang/crates.io-index)" = "93c5eaa17d0954cb481cdcfffe9d84fcfa7a1a9f2349271e678677be4c26ae31" +"checksum serde_json 1.0.57 (registry+https://github.com/rust-lang/crates.io-index)" = "164eacbdb13512ec2745fb09d51fd5b22b0d65ed294a1dcf7285a360c80a675c" "checksum shared_library 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5a9e7e0f2bfae24d8a5b5a66c5b257a83c7412304311512a0c054cd5e619da11" "checksum shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" diff --git a/Cargo.toml b/Cargo.toml index cb13bb1668..d1af9a740b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,13 +20,14 @@ gl = "0.14.0" # this looks like bare bones OpenGL glfw = "0.37.0" # GL framework - windows, contexts, devices glfw-sys = "3.3.2" # Does this have linear algebra? glium = { version = "0.26.0", default-features = true } # high-level OpenGL abstraction (*not* vanilla OpenGL) +gltf = "0.15.2" # glTF loader (more modern OBJ) image = "0.23.2" imgui = "0.3.0" # OpenGL windowing toolkit (experimental bindings) imgui-glium-renderer = "0.3.0" imgui-opengl-renderer = "0.7.0" imgui-sdl2 = "0.9.0" # for imgui-opengl-renderer imgui-winit-support = "0.3.1" -k4a-sys = { path = "/home/bt/dev/3rd/k4a-sys" } # k4a-sys = "0.2.0" +k4a-sys = { path = "/home/bt/dev/3rd/k4a-sys" } # k4a-sys = "0.2.0" TODO: Check these modifications into source control libc = "0.2.68" opencv = "0.33.1" rand = "0.7.3" diff --git a/src/assets/mod.rs b/src/assets/mod.rs index 86f4efa25e..40df146d38 100644 --- a/src/assets/mod.rs +++ b/src/assets/mod.rs @@ -1 +1,2 @@ -pub mod obj_loader; \ No newline at end of file +pub mod obj_loader; +pub mod renderable_object; \ No newline at end of file diff --git a/src/assets/renderable_object.rs b/src/assets/renderable_object.rs new file mode 100644 index 0000000000..33650f1ab5 --- /dev/null +++ b/src/assets/renderable_object.rs @@ -0,0 +1,93 @@ +use assets::obj_loader::ExtractedVertex; +use gl::types::*; +use opengl::wrapper::buffer::Buffer; +use opengl::wrapper::texture::Texture; +use opengl::wrapper::vertex_array::VertexArray; +use std::ffi::{c_void, CString}; +use std::mem::size_of; +use std::os::raw::c_char; +use std::ptr::null; + +/// A collection of all the graphics-related data to render: vertices, normals, etc. +pub struct RenderableObject { + pub vao: VertexArray, + pub vertex_buffer: Buffer, + pub normal_buffer: Buffer, + pub color_buffer: Buffer, + pub texture_coords_buffer: Buffer, + pub texture: Texture, + pub num_vertices: usize, + pub buffered: bool, +} + +impl RenderableObject { + pub fn new() -> Self { + unsafe { + let vao = VertexArray::new_initialized(); + vao.bind(); + + let vertex_buffer = Buffer::new_initialized(); + let normal_buffer = Buffer::new_initialized(); + let color_buffer = Buffer::new_initialized(); + let texture_coords_buffer = Buffer::new_initialized(); + let texture = Texture::new_initialized(); + + Self { + vao, + vertex_buffer, + normal_buffer, + color_buffer, + texture_coords_buffer, + texture, + num_vertices: 0, + buffered: false, + } + } + } + + pub fn load_vertices(&mut self, vertex_data: &Vec, shader_id: GLuint) { + self.vao.bind(); + self.vertex_buffer.bind_as_array_buffer(); + + let size = vertex_data.len() * 3 * size_of::(); + let mut vertices : Vec = Vec::with_capacity(vertex_data.len() * 3); + + for v in vertex_data.into_iter() { + vertices.extend(&v.position); + } + + self.num_vertices = vertex_data.len(); + + println!("Object vertices loaded: {}", vertices.len()); + + unsafe { + let vertices_ptr = vertices.as_ptr() as *const c_void; + + gl::BufferData( + gl::ARRAY_BUFFER, + size as isize, + vertices_ptr, + gl::STATIC_DRAW, + ); + + let name : CString = CString::new("position").expect("string is correct"); + let name_ptr : *const c_char = name.as_ptr() as *const c_char; + + let loc = gl::GetAttribLocation(shader_id, name_ptr); + println!("Attrib location: {}", loc); + + gl::EnableVertexAttribArray(loc as u32); + + gl::VertexAttribPointer(loc as u32, 3, gl::FLOAT, gl::FALSE, 0, null()); + } + } + + pub fn draw(&self) { + self.vao.bind(); + unsafe { + //gl::DrawArrays(gl::TRIANGLES, 0, self.num_vertices as i32); + gl::DrawArrays(gl::QUADS, 0, self.num_vertices as i32); + } + } +} + diff --git a/src/point_cloud/point_cloud_renderer.rs b/src/point_cloud/point_cloud_renderer.rs index 140c710ad2..e85e92a83b 100644 --- a/src/point_cloud/point_cloud_renderer.rs +++ b/src/point_cloud/point_cloud_renderer.rs @@ -32,6 +32,7 @@ use std::io::{Read, Write, BufReader}; use std::time::{SystemTime, UNIX_EPOCH}; use tobj::load_obj; use assets::obj_loader::{load_wavefront, ExtractedVertex}; +use assets::renderable_object::RenderableObject; pub type Result = std::result::Result; @@ -67,96 +68,6 @@ impl std::error::Error for PointCloudRendererError { } } -// TODO: Going to experiment with rendering more things. -pub struct RenderableObject { - vao : GLuint, - color_buffer : GLuint, - buffered: bool, -} - -pub struct RenderableObject2 { - pub vao: VertexArray, - pub vertex_buffer: Buffer, - pub normal_buffer: Buffer, - pub color_buffer: Buffer, - pub texture_coords_buffer: Buffer, - pub texture: Texture, - pub num_vertices: usize, - pub buffered: bool, -} - -impl RenderableObject2 { - pub fn new() -> Self { - unsafe { - let vao = VertexArray::new_initialized(); - vao.bind(); - - let vertex_buffer = Buffer::new_initialized(); - let normal_buffer = Buffer::new_initialized(); - let color_buffer = Buffer::new_initialized(); - let texture_coords_buffer = Buffer::new_initialized(); - let texture = Texture::new_initialized(); - - Self { - vao, - vertex_buffer, - normal_buffer, - color_buffer, - texture_coords_buffer, - texture, - num_vertices: 0, - buffered: false, - } - } - } - - pub fn load_vertices(&mut self, vertex_data: &Vec, shader_id: GLuint) { - self.vao.bind(); - self.vertex_buffer.bind_as_array_buffer(); - - let size = vertex_data.len() * 3 * size_of::(); - let mut vertices : Vec = Vec::with_capacity(vertex_data.len() * 3); - - for v in vertex_data.into_iter() { - vertices.extend(&v.position); - } - - self.num_vertices = vertex_data.len(); - - println!("Object vertices loaded: {}", vertices.len()); - - unsafe { - let vertices_ptr = vertices.as_ptr() as *const c_void; - - gl::BufferData( - gl::ARRAY_BUFFER, - size as isize, - vertices_ptr, - gl::STATIC_DRAW, - ); - - let name : CString = CString::new("position").expect("string is correct"); - let name_ptr : *const c_char = name.as_ptr() as *const c_char; - - let loc = gl::GetAttribLocation(shader_id, name_ptr); - println!("Attrib location: {}", loc); - - gl::EnableVertexAttribArray(loc as u32); - - gl::VertexAttribPointer(loc as u32, 3, gl::FLOAT, gl::FALSE, 0, null()); - } - } - - pub fn draw(&self) { - self.vao.bind(); - unsafe { - //gl::DrawArrays(gl::TRIANGLES, 0, self.num_vertices as i32); - gl::DrawArrays(gl::QUADS, 0, self.num_vertices as i32); - } - } -} - - pub struct PointCloudRenderer { num_cameras: usize, @@ -204,8 +115,7 @@ pub struct PointCloudRenderer { /// 'in vec4 inColor' color_vertex_attribute_location: GLint, - renderable_objects: Vec, - renderable_object: Option + renderable_object: Option } const fn translation_matrix_4x4(x: f32, y: f32, z: f32) -> [f32; 16] { @@ -358,7 +268,6 @@ impl PointCloudRenderer { fragment_shader_id, point_size: 1, enable_shading: false, - renderable_objects: Vec::new(), view_index, projection_index, enable_shading_index, @@ -404,7 +313,7 @@ impl PointCloudRenderer { let path = Path::new(filename); let vertices = load_wavefront(&path)?; - let mut renderable_object = RenderableObject2::new(); + let mut renderable_object = RenderableObject::new(); renderable_object.load_vertices(&vertices, self.shader_program_id); self.renderable_object = Some(renderable_object);