working on loading materials
This commit is contained in:
@@ -1,22 +1,23 @@
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use nalgebra::Vector4;
|
||||
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Vertex {
|
||||
pos: [f32; 4],
|
||||
normal: [f32; 4],
|
||||
uv: [f32; 2],
|
||||
}
|
||||
|
||||
impl Vertex {
|
||||
pub fn position(&self) -> Vector4<f32> {
|
||||
Vector4::new(self.pos[0], self.pos[1], self.pos[2], self.pos[3])
|
||||
}
|
||||
pub fn from(pos: [f32; 3], nor: [f32; 3]) -> Vertex {
|
||||
pub fn from(pos: [f32; 3], nor: [f32; 3], uv: [f32; 2]) -> Vertex {
|
||||
Vertex {
|
||||
pos: [pos[0], pos[1], pos[2], 1.0],
|
||||
normal: [nor[0], nor[1], nor[2], 0.0],
|
||||
uv: [0.0, 0.0],
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,16 +25,21 @@ impl Vertex {
|
||||
unsafe impl Pod for Vertex {}
|
||||
unsafe impl Zeroable for Vertex {}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RawMesh {
|
||||
vertices: Vec<Vertex>,
|
||||
normals: Vec<[u32; 3]>,
|
||||
}
|
||||
|
||||
pub fn load_obj(obj_path: &str, mtl_path: Option<&str>) -> (Vec<Vertex>, Vec<u32>) {
|
||||
|
||||
pub fn load_obj(mesh_path: &str) -> (Vec<Vertex>, Vec<u32>) {
|
||||
|
||||
let (models, materials) = tobj::load_obj(mesh_path, false).expect("Failed to load file");
|
||||
let (models, materials) = tobj::load_obj(obj_path, true).expect("Failed to load file");
|
||||
|
||||
println!("# of models: {}", models.len());
|
||||
println!("# of materials: {}", materials.len());
|
||||
|
||||
let mut index_data : Vec<u32> = Vec::new();
|
||||
|
||||
let mut index_data: Vec<u32> = Vec::new();
|
||||
let mut vertex_data = Vec::new();
|
||||
|
||||
for model in models {
|
||||
@@ -49,24 +55,30 @@ pub fn load_obj(mesh_path: &str) -> (Vec<Vertex>, Vec<u32>) {
|
||||
next_face = end;
|
||||
}
|
||||
|
||||
// Normals and texture coordinates are also loaded, but not printed in this example
|
||||
assert!(mesh.positions.len() % 3 == 0);
|
||||
assert!(mesh.texcoords.len() % 2 == 0);
|
||||
|
||||
for v in 0..mesh.positions.len() / 3 {
|
||||
vertex_data.push(
|
||||
Vertex::from([
|
||||
mesh.positions[3 * v],
|
||||
mesh.positions[3 * v + 1],
|
||||
mesh.positions[3 * v + 2]
|
||||
],
|
||||
[
|
||||
mesh.normals[3 * v],
|
||||
mesh.normals[3 * v + 1],
|
||||
mesh.normals[3 * v + 2]
|
||||
],
|
||||
));
|
||||
let texcoords = if mesh.texcoords.len() == 0 {
|
||||
[0.0, 0.0]
|
||||
} else {
|
||||
[mesh.texcoords[2 * v], mesh.texcoords[2 * v + 1]]
|
||||
};
|
||||
|
||||
vertex_data.push(Vertex::from(
|
||||
[
|
||||
mesh.positions[3 * v],
|
||||
mesh.positions[3 * v + 1],
|
||||
mesh.positions[3 * v + 2],
|
||||
],
|
||||
[
|
||||
mesh.normals[3 * v],
|
||||
mesh.normals[3 * v + 1],
|
||||
mesh.normals[3 * v + 2],
|
||||
],
|
||||
texcoords,
|
||||
));
|
||||
}
|
||||
}
|
||||
(vertex_data.to_vec(), index_data.to_vec())
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
27
src/main.rs
27
src/main.rs
@@ -49,6 +49,7 @@ use crate::imgui_supp::extended_winit_imgui_support;
|
||||
use crate::imgui_supp::imgui_support::{ImguiContext, ImguiPlatform};
|
||||
use crate::owned_event::{OwnedEvent, OwnedEventExtension};
|
||||
use crate::physics::state::PhysicsState;
|
||||
use std::fs;
|
||||
|
||||
mod camera;
|
||||
mod components;
|
||||
@@ -83,6 +84,7 @@ ECS
|
||||
input/io (yep!)
|
||||
collision / physics (yep!)
|
||||
entities & behaviours (got the entities!)
|
||||
scripting!
|
||||
|
||||
*/
|
||||
|
||||
@@ -156,6 +158,7 @@ fn main() {
|
||||
|
||||
// The renderer
|
||||
let mut renderer = render::state::RenderState::init(&window, &mut imgui_context);
|
||||
preload_meshes("./resources");
|
||||
entity_loading(&mut world, &mut renderer);
|
||||
|
||||
resources.insert(renderer);
|
||||
@@ -305,6 +308,21 @@ fn main() {
|
||||
});
|
||||
}
|
||||
|
||||
pub fn preload_meshes(resources_path: &str) -> (){
|
||||
println!("Preloading meshes...");
|
||||
let paths = fs::read_dir(resources_path).unwrap();
|
||||
for file in paths {
|
||||
let file = file.unwrap().file_name();
|
||||
let filename = file.to_str().unwrap();
|
||||
if filename.ends_with(".obj") {
|
||||
println!("Loading {:?} ...", filename);
|
||||
|
||||
}
|
||||
}()
|
||||
// I guess it's fine to have them loaded to the gpu
|
||||
// But I also want to preserve the raw data
|
||||
}
|
||||
|
||||
pub fn load_colliding_mesh_entity(world: &mut World, renderer: &mut render::state::RenderState, mesh_path: &str) {
|
||||
|
||||
let mut static_floor_body = RigidBodyBuilder::new_static()
|
||||
@@ -366,6 +384,11 @@ pub fn load_colliding_mesh_entity(world: &mut World, renderer: &mut render::stat
|
||||
pub fn entity_loading(world: &mut World, renderer: &mut render::state::RenderState) {
|
||||
let monkey_mesh =
|
||||
renderer.load_mesh_to_buffer("./resources/monkey.obj", Some(wgpu::Color::GREEN));
|
||||
let light_mesh =
|
||||
renderer.load_mesh_to_buffer("./resources/light.obj", Some(wgpu::Color::BLACK));
|
||||
let ball_mesh =
|
||||
renderer.load_mesh_to_buffer("./resources/ball.obj", Some(wgpu::Color::BLUE));
|
||||
|
||||
|
||||
let camera_ent: Entity = world.push((
|
||||
Camera {
|
||||
@@ -380,8 +403,7 @@ pub fn entity_loading(world: &mut World, renderer: &mut render::state::RenderSta
|
||||
CameraController::new(3.0, 1.0),
|
||||
));
|
||||
|
||||
let light_mesh =
|
||||
renderer.load_mesh_to_buffer("./resources/light.obj", Some(wgpu::Color::BLACK));
|
||||
|
||||
|
||||
let light_entity: Entity = world.push((
|
||||
Position {
|
||||
@@ -457,7 +479,6 @@ pub fn entity_loading(world: &mut World, renderer: &mut render::state::RenderSta
|
||||
|
||||
|
||||
|
||||
let ball_mesh = renderer.load_mesh_to_buffer("./resources/ball.obj", Some(wgpu::Color::BLUE));
|
||||
|
||||
let ball_mesh: Entity = world.push((
|
||||
Position {
|
||||
|
||||
@@ -110,7 +110,7 @@ impl RenderState {
|
||||
|
||||
/// Create a bare vertex & indices buffer
|
||||
/// TODO I really should remove this / consolidate it
|
||||
pub fn create_buffer(
|
||||
fn create_buffer(
|
||||
device: &wgpu::Device,
|
||||
indices: Vec<u32>,
|
||||
vertices: Vec<Vertex>,
|
||||
@@ -271,7 +271,7 @@ impl RenderState {
|
||||
// Though the attr thing is still a macro. Which would cause issues if
|
||||
// I wanted to get tricky with the 0,1 types
|
||||
let vertex_size = mem::size_of::<Vertex>();
|
||||
let vertex_attr = wgpu::vertex_attr_array![0 => Float4, 1 => Float4];
|
||||
let vertex_attr = wgpu::vertex_attr_array![0 => Float4, 1 => Float4, 2 => Float2];
|
||||
let vb_desc = wgpu::VertexBufferLayout {
|
||||
array_stride: vertex_size as wgpu::BufferAddress,
|
||||
step_mode: wgpu::InputStepMode::Vertex,
|
||||
|
||||
Reference in New Issue
Block a user