better to use a struct for the raw mesh data

This commit is contained in:
2021-02-19 21:18:47 -08:00
parent e5815ce0d6
commit 85376e5b17
3 changed files with 52 additions and 40 deletions

View File

@@ -26,7 +26,7 @@ use winit_24::window::Window;
use crate::camera::{Camera, CameraController};
use crate::components::{Mesh, Position, RangeCopy};
use crate::current_ui;
use crate::geometry::{load_obj, Vertex};
use crate::geometry::{load_obj, Vertex, RawMesh};
use crate::imgui_supp::imgui_support::{ImguiContext, ImguiPlatform};
use crate::light::{DirectionalLight, LightRaw};
use crate::render::{EntityUniforms, ShadowUniforms, ForwardUniforms};
@@ -112,21 +112,30 @@ impl RenderState {
/// TODO I really should remove this / consolidate it
fn create_buffer(
device: &wgpu::Device,
indices: Vec<u32>,
vertices: Vec<Vertex>,
raw_mesh: RawMesh,
) -> (Arc<Buffer>, Arc<Buffer>) {
let vertex_buf = Arc::new(
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("vertex-buffer"),
contents: bytemuck::cast_slice(&vertices),
contents: bytemuck::cast_slice(&raw_mesh.vertices),
usage: wgpu::BufferUsage::VERTEX,
}),
);
//println!("{:x?}", raw_mesh.indices);
// let mut hack = Vec::<u32>::new();
// for ind_chunk in raw_mesh.indices {
// hack.push(ind_chunk[0]);
// hack.push(ind_chunk[1]);
// hack.push(ind_chunk[2]);
// }
let index_buf = Arc::new(
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("index-buffer"),
contents: bytemuck::cast_slice(&indices),
contents: bytemuck::cast_slice(&raw_mesh.indices),
usage: wgpu::BufferUsage::INDEX,
}),
);
@@ -134,11 +143,16 @@ impl RenderState {
(vertex_buf, index_buf)
}
pub fn load_mesh_to_buffer(&self, filepath: &str, color: Option<wgpu::Color>) -> Mesh {
let (vertices, indices) = load_obj(filepath);
let index_count = indices.len();
/// Take a meshes
pub fn upload_mesh_to_buffer(mesh: RawMesh) {
let (vertex_buf, index_buf) = RenderState::create_buffer(&self.device, indices, vertices);
}
pub fn load_mesh_to_buffer(&self, filepath: &str, color: Option<wgpu::Color>) -> Mesh {
let raw_mesh = load_obj(filepath);
let index_count = raw_mesh.indices.len() * 3; // TODO bad bad bad bad!
let (vertex_buf, index_buf) = RenderState::create_buffer(&self.device, raw_mesh);
let uniform_size = mem::size_of::<EntityUniforms>() as wgpu::BufferAddress;