From 7c4ffd21224f15e0b66c3b8660c5a6c525705dc3 Mon Sep 17 00:00:00 2001 From: AJMC2002 Date: Wed, 15 Nov 2023 10:52:58 +1000 Subject: [PATCH] Presentation edition --- src/graphics/mesh.rs | 38 ++- src/graphics/wrapper/texture.rs | 1 + src/main.rs | 429 +++++++++++--------------------- 3 files changed, 167 insertions(+), 301 deletions(-) diff --git a/src/graphics/mesh.rs b/src/graphics/mesh.rs index 6646458..ffd28fa 100644 --- a/src/graphics/mesh.rs +++ b/src/graphics/mesh.rs @@ -1,8 +1,4 @@ -use std::{ - mem::{self, size_of}, - os::raw::c_void, - ptr, -}; +use std::{mem::size_of, ptr}; use gl::types::{GLfloat, GLsizei}; use obj::Obj; @@ -11,7 +7,7 @@ use crate::maths::{Matrix, Vector}; use super::{ camera::Camera, - wrapper::{shader_program, ShaderProgram, Texture2D, VertexAttrib, BO, EBO, VAO, VBO}, + wrapper::{ShaderProgram, Texture2D, VertexAttrib, BO, EBO, VAO, VBO}, }; pub struct Cube<'a> { @@ -42,21 +38,21 @@ impl<'a> Cube<'a> { -0.5, -0.5, -0.5, //7 ]; - match texture { - Some(texture) => { - let tex_coords = tex_coords.unwrap_or(vec![ - 1.0, 1.0, //0 - 0.0, 1.0, //1 - 1.0, 0.0, //2 - 0.0, 0.0, //3 - 0.0, 1.0, //4 - 1.0, 1.0, //5 - 0.0, 0.0, //6 - 1.0, 0.0, //7 - ]); - } - None => (), - } + // match texture { + // Some(texture) => { + // let tex_coords = tex_coords.unwrap_or(vec![ + // 1.0, 1.0, //0 + // 0.0, 1.0, //1 + // 1.0, 0.0, //2 + // 0.0, 0.0, //3 + // 0.0, 1.0, //4 + // 1.0, 1.0, //5 + // 0.0, 0.0, //6 + // 1.0, 0.0, //7 + // ]); + // } + // None => (), + // } let vao = VAO::new(); let vbo: VBO = BO::new(gl::STATIC_DRAW, vertex_data); diff --git a/src/graphics/wrapper/texture.rs b/src/graphics/wrapper/texture.rs index b18cd20..df73cb0 100644 --- a/src/graphics/wrapper/texture.rs +++ b/src/graphics/wrapper/texture.rs @@ -1,6 +1,7 @@ use gl::types::*; use image::GenericImageView; +#[derive(Debug)] pub struct Texture2D { id: GLuint, } diff --git a/src/main.rs b/src/main.rs index 7cad713..9703fdc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,135 +1,18 @@ use doom_engine::graphics::mesh::Cube; use doom_engine::graphics::{wrapper::*, Window}; use doom_engine::maths::*; -use doom_engine::{matrix, vector}; +use doom_engine::vector; use egui::{Align2, RichText}; use egui_glfw::egui; use gl::types::*; use std::error::Error; -use std::f32::consts::PI; -use std::fs::File; -use std::io::BufReader; use std::{mem, os::raw::c_void, ptr}; -use tobj::{load_obj, LoadOptions}; static WIDTH: u32 = 1920; static HEIGHT: u32 = 1080; fn main() -> Result<(), Box> { - // let (models, materials) = load_obj( - // "resources/objects/crash/crashbandicoot.obj", - // &LoadOptions::default(), - // ) - // .expect("obj loading"); - // let materials = materials.expect("material loading"); - - // println!("Number of models = {}", models.len()); - // println!("Number of materials = {}", materials.len()); - - // for (i, m) in models.iter().enumerate() { - // let mesh = &m.mesh; - // println!(""); - // println!("model[{}].name = \'{}\'", i, m.name); - // println!("model[{}].mesh.material_id = {:?}", i, mesh.material_id); - - // println!( - // "model[{}].face_count = {}", - // i, - // mesh.face_arities.len() - // ); - - // let mut next_face = 0; - // for face in 0..mesh.face_arities.len() { - // let end = next_face + mesh.face_arities[face] as usize; - - // let face_indices = &mesh.indices[next_face..end]; - // println!(" face[{}].indices = {:?}", face, face_indices); - - // if !mesh.texcoord_indices.is_empty() { - // let texcoord_face_indices = &mesh.texcoord_indices[next_face..end]; - // println!( - // " face[{}].texcoord_indices = {:?}", - // face, texcoord_face_indices - // ); - // } - // if !mesh.normal_indices.is_empty() { - // let normal_face_indices = &mesh.normal_indices[next_face..end]; - // println!( - // " face[{}].normal_indices = {:?}", - // face, normal_face_indices - // ); - // } - - // next_face = end; - // } - - // // Normals and texture coordinates are also loaded, but not printed in - // // this example. - // println!( - // "model[{}].positions = {}", - // i, - // mesh.positions.len() / 3 - // ); - // assert!(mesh.positions.len() % 3 == 0); - - // for vtx in 0..mesh.positions.len() / 3 { - // println!( - // " position[{}] = ({}, {}, {})", - // vtx, - // mesh.positions[3 * vtx], - // mesh.positions[3 * vtx + 1], - // mesh.positions[3 * vtx + 2] - // ); - // } - // } - - // for (i, m) in materials.iter().enumerate() { - // println!("material[{}].name = \'{}\'", i, m.name); - // println!( - // " material.Ka = ({:#?})", - // m.ambient.unwrap_or([-1., -1., -1.]) - // ); - // println!( - // " material.Kd = ({:#?})", - // m.diffuse.unwrap_or([-1., -1., -1.]) - // ); - // println!( - // " material.Ks = ({:#?})", - // m.specular.unwrap_or([-1., -1., -1.]) - // ); - // println!(" material.Ns = {}", m.shininess.unwrap_or(-1.)); - // println!(" material.d = {}", m.dissolve.unwrap_or(-1.)); - // println!( - // " material.map_Ka = {}", - // m.ambient_texture.as_ref().unwrap_or(&"NONE".to_string()) - // ); - // println!( - // " material.map_Kd = {}", - // m.diffuse_texture.as_ref().unwrap_or(&"NONE".to_string()) - // ); - // println!( - // " material.map_Ks = {}", - // m.specular_texture.as_ref().unwrap_or(&"NONE".to_string()) - // ); - // println!( - // " material.map_Ns = {}", - // m.shininess_texture.as_ref().unwrap_or(&"NONE".to_string()) - // ); - // println!( - // " material.map_Bump = {}", - // m.normal_texture.as_ref().unwrap_or(&"NONE".to_string()) - // ); - // println!( - // " material.map_d = {}", - // m.dissolve_texture.as_ref().unwrap_or(&"NONE".to_string()) - // ); - - // for (k, v) in &m.unknown_param { - // println!(" material.{} = {}", k, v); - // } - // } - // return Ok(()); let mut window = Window::new(WIDTH, HEIGHT, "Doom Engine"); let mut shader_program = ShaderProgram::new( @@ -140,6 +23,8 @@ fn main() -> Result<(), Box> { let texture_gato = Texture2D::new("resources/textures/cat.jpg"); let texture_gatorrito = Texture2D::new("resources/textures/gatorrito.jpg"); let texture_pog = Texture2D::new("resources/textures/pog.jpg"); + let mut textures = [&texture_gato, &texture_gatorrito, &texture_pog]; + let mut main_texture = 0; // All Buffer Objects are binded and the data is stored on creation let _vao = VAO::new(); @@ -231,23 +116,6 @@ fn main() -> Result<(), Box> { texture_gatorrito.unbind(); shader_program.unbind(); - let mut scale = ( - 1.0.to_string().to_owned(), - 1.0.to_string().to_owned(), - 1.0.to_string().to_owned(), - ); - let mut rotate = (0.0, 0.0, 0.0); - let mut translate = ( - 0.0.to_string().to_owned(), - 0.0.to_string().to_owned(), - 0.0.to_string().to_owned(), - ); - - let mut basic_shader = ShaderProgram::new( - "resources/shaders/basic.vert", - "resources/shaders/basic.frag", - ); - let mut light_shader = ShaderProgram::new( "resources/shaders/light.vert", "resources/shaders/light.frag", @@ -263,7 +131,6 @@ fn main() -> Result<(), Box> { None, ); - let mut t; unsafe { gl::ClearColor(154. / 258., 127. / 258., 174. / 258., 1.0); } @@ -273,22 +140,19 @@ fn main() -> Result<(), Box> { gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT); } - { - t = window.glfw_handle().get_time() as f32; - println!("cum"); - shader_program.bind(); - //_pos_attrib.enable(); - //_tex_attrib.enable(); - _vao.bind(); - shader_program.uniform_matrix_4fv("proj", &window.camera_handle().proj()); - shader_program.uniform_matrix_4fv("view", &window.camera_handle().view()); - shader_program.uniform_3fv("color", &vector![1.0, 1.0, 1.0]); - shader_program.uniform_3fv("light_color", &vector![1.0, 1.0, 1.0]); - shader_program.uniform_3fv("light_pos", &light.pos()); - shader_program.uniform_3fv("view_pos", &window.camera_handle().pos()); - - cube_pos.iter().for_each(|pos| { - let m = Matrix::translation(pos.clone()) + shader_program.bind(); + //_pos_attrib.enable(); + //_tex_attrib.enable(); + _vao.bind(); + shader_program.uniform_matrix_4fv("proj", &window.camera_handle().proj()); + shader_program.uniform_matrix_4fv("view", &window.camera_handle().view()); + shader_program.uniform_3fv("color", &vector![1.0, 1.0, 1.0]); + shader_program.uniform_3fv("light_color", &vector![1.0, 1.0, 1.0]); + shader_program.uniform_3fv("light_pos", &light.pos()); + shader_program.uniform_3fv("view_pos", &window.camera_handle().pos()); + + cube_pos.iter().for_each(|pos| { + let m = Matrix::translation(pos.clone()) //* Matrix::translation(( // translate.0.parse::().unwrap_or(0.0), // translate.1.parse::().unwrap_or(0.0), @@ -300,145 +164,150 @@ fn main() -> Result<(), Box> { // scale.2.parse::().unwrap_or(1.0), //)) ; - shader_program.uniform_matrix_4fv("model", &m); - shader_program.uniform_matrix_3fv("normal", &m.to_normal()); + shader_program.uniform_matrix_4fv("model", &m); + shader_program.uniform_matrix_3fv("normal", &m.to_normal()); - shader_program.uniform_2dtex("tex", &texture_gatorrito); - unsafe { gl::DrawArrays(gl::TRIANGLES, 0, 36) } - texture_gatorrito.unbind(); - }); + shader_program.uniform_2dtex("tex", textures[main_texture]); + unsafe { gl::DrawArrays(gl::TRIANGLES, 0, 36) } + }); + + _vao.unbind(); + _pos_attrib.disable(); + _tex_attrib.disable(); - _vao.unbind(); - _pos_attrib.disable(); - _tex_attrib.disable(); - } println!("Draw cube"); light.draw(window.camera_handle(), &mut light_shader); - { - window.begin_ui(); - - egui::SidePanel::left("my_side_panel").resizable(true).show( - &window.ui_handle().get_egui_ctx().to_owned(), - |ui| { - ui.heading("Hello World!"); - if ui.button("Quit").clicked() { - window.window_handle_mut().set_should_close(true); - } - - egui::ComboBox::from_label("Version") - .width(150.0) - .selected_text("foo") - .show_ui(ui, |ui| { - egui::CollapsingHeader::new("Dev") - .default_open(true) - .show(ui, |ui| { - ui.label("contents"); - }); - }); - - ui.label(format!( - "window content scale: {:?}", - window.window_handle().get_content_scale() - )); - ui.label(format!( - "monitor content scale: {:?}", - window - .glfw_handle_mut() - .with_connected_monitors(|_, monitors| { - monitors - .iter() - .map(|monitor| monitor.get_content_scale()) - .collect::>() - }) - )); - ui.label(format!( - "monitor physical size in mm: {:?}", - window - .glfw_handle_mut() - .with_connected_monitors(|_, monitors| { - monitors - .iter() - .map(|monitor| monitor.get_physical_size()) - .collect::>() - }) - )); - ui.label(format!( - "monitor physical size in inch: {:?}", - window - .glfw_handle_mut() - .with_connected_monitors(|_, monitors| { - monitors - .iter() - .map(|monitor| { - let mm = monitor.get_physical_size(); - (mm.0 as f32 / 25.4, mm.1 as f32 / 25.4) - }) - .collect::>() - }) - )); - ui.label(format!( - "monitor positions: {:?}", - window - .glfw_handle_mut() - .with_connected_monitors(|_, monitors| { - monitors - .iter() - .map(|monitor| monitor.get_pos()) - .collect::>() - }) - )); - ui.label(format!( - "window position: {:?}", - window.window_handle().get_pos() - )); - ui.label(format!("camera: {:#?}", window.camera_handle())); - }, - ); - - if window.is_camera_still() { - egui::Window::new("") - .title_bar(false) - .collapsible(false) - .resizable(false) - .anchor(Align2::RIGHT_TOP, (0.0, 0.0)) - .show(&window.ui_handle().get_egui_ctx().to_owned(), |ui| { - ui.label(RichText::new("Camera rotation is set still.").size(15.0)) + window.begin_ui(); + + egui::SidePanel::left("my_side_panel").resizable(true).show( + &window.ui_handle().get_egui_ctx().to_owned(), + |ui| { + ui.heading("Hello World!"); + if ui.button("Quit").clicked() { + window.window_handle_mut().set_should_close(true); + } + + egui::ComboBox::from_label("Version") + .width(150.0) + .selected_text("foo") + .show_ui(ui, |ui| { + egui::CollapsingHeader::new("Dev") + .default_open(true) + .show(ui, |ui| { + ui.label("contents"); + }); }); - } - egui::Window::new("Light").show(&window.ui_handle().get_egui_ctx().to_owned(), |ui| { - ui.set_max_width(280.0); - ui.group(|ui| { - ui.horizontal(|ui| { - ui.label("time"); - ui.label(window.glfw_handle().get_time().to_string()); - ui.label("s."); - }); - let mut light_pos = light.pos(); - ui.horizontal(|ui| { - ui.label("light_pos"); - ui.vertical(|ui| { - ui.horizontal(|ui| { - ui.label("x"); - ui.add(egui::Slider::new(&mut light_pos[0], -15.0..=15.0)); - }); - ui.horizontal(|ui| { - ui.label("y"); - ui.add(egui::Slider::new(&mut light_pos[1], -15.0..=15.0)); - }); - ui.horizontal(|ui| { - ui.label("z"); - ui.add(egui::Slider::new(&mut light_pos[2], -15.0..=15.0)); - }); + ui.label(format!( + "window content scale: {:?}", + window.window_handle().get_content_scale() + )); + ui.label(format!( + "monitor content scale: {:?}", + window + .glfw_handle_mut() + .with_connected_monitors(|_, monitors| { + monitors + .iter() + .map(|monitor| monitor.get_content_scale()) + .collect::>() }) - }); - light.set_pos(light_pos); + )); + ui.label(format!( + "monitor physical size in mm: {:?}", + window + .glfw_handle_mut() + .with_connected_monitors(|_, monitors| { + monitors + .iter() + .map(|monitor| monitor.get_physical_size()) + .collect::>() + }) + )); + ui.label(format!( + "monitor physical size in inch: {:?}", + window + .glfw_handle_mut() + .with_connected_monitors(|_, monitors| { + monitors + .iter() + .map(|monitor| { + let mm = monitor.get_physical_size(); + (mm.0 as f32 / 25.4, mm.1 as f32 / 25.4) + }) + .collect::>() + }) + )); + ui.label(format!( + "monitor positions: {:?}", + window + .glfw_handle_mut() + .with_connected_monitors(|_, monitors| { + monitors + .iter() + .map(|monitor| monitor.get_pos()) + .collect::>() + }) + )); + ui.label(format!( + "window position: {:?}", + window.window_handle().get_pos() + )); + ui.label(format!("camera: {:#?}", window.camera_handle())); + }, + ); + + if window.is_camera_still() { + egui::Window::new("") + .title_bar(false) + .collapsible(false) + .resizable(false) + .anchor(Align2::RIGHT_TOP, (0.0, 0.0)) + .show(&window.ui_handle().get_egui_ctx().to_owned(), |ui| { + ui.label(RichText::new("Camera rotation is set still.").size(15.0)) + }); + } + + egui::Window::new("Objects").show(&window.ui_handle().get_egui_ctx().to_owned(), |ui| { + ui.set_max_width(280.0); + ui.group(|ui| { + ui.horizontal(|ui| { + egui::ComboBox::from_label("Choose a texture:") + .selected_text(format!("{:?}", main_texture)) + .show_index(ui, &mut main_texture, 3, |i| format!("Texture No. {}", i)) + }) + }); + ui.group(|ui| { + ui.label("Light"); + ui.horizontal(|ui| { + ui.label("time"); + ui.label(window.glfw_handle().get_time().to_string()); + ui.label("s."); }); + let mut light_pos = light.pos(); + ui.horizontal(|ui| { + ui.label("light_pos"); + ui.vertical(|ui| { + ui.horizontal(|ui| { + ui.label("x"); + ui.add(egui::Slider::new(&mut light_pos[0], -15.0..=15.0)); + }); + ui.horizontal(|ui| { + ui.label("y"); + ui.add(egui::Slider::new(&mut light_pos[1], -15.0..=15.0)); + }); + ui.horizontal(|ui| { + ui.label("z"); + ui.add(egui::Slider::new(&mut light_pos[2], -15.0..=15.0)); + }); + }) + }); + light.set_pos(light_pos); }); + }); - window.end_ui(); - } + window.end_ui(); window.update(); }