strafe-project/src/main.rs

183 lines
6.0 KiB
Rust
Raw Normal View History

use std::time::Instant;
2023-10-20 02:19:05 +00:00
use physics::PhysicsInstruction;
use render_thread::InputInstruction;
2023-09-20 00:53:29 +00:00
use instruction::{TimedInstruction, InstructionConsumer};
2023-08-30 01:20:58 +00:00
2023-10-06 03:32:25 +00:00
mod bvh;
2023-10-06 03:32:02 +00:00
mod aabb;
2023-09-22 22:21:13 +00:00
mod model;
2023-10-20 22:50:26 +00:00
mod window;
2023-10-04 22:58:02 +00:00
mod worker;
2023-10-20 22:50:26 +00:00
mod zeroes;
2023-10-19 00:17:21 +00:00
mod integer;
2023-10-04 22:58:02 +00:00
mod physics;
2023-10-19 00:17:21 +00:00
mod graphics;
2023-10-10 00:09:24 +00:00
mod settings;
2023-09-23 02:41:27 +00:00
mod primitives;
2023-09-22 22:19:44 +00:00
mod instruction;
mod load_roblox;
mod render_thread;
2023-10-20 22:50:26 +00:00
mod model_graphics;
mod graphics_context;
2023-10-05 03:04:04 +00:00
pub struct GlobalState{
start_time: std::time::Instant,
manual_mouse_lock:bool,
mouse:std::sync::Arc<std::sync::Mutex<physics::MouseState>>,
2023-10-10 02:44:49 +00:00
user_settings:settings::UserSettings,
//Ideally the graphics thread worker description is:
/*
WorkerDescription{
input:Immediate,
output:Realtime(PoolOrdering::Ordered(3)),
}
*/
//up to three frames in flight, dropping new frame requests when all three are busy, and dropping output frames when one renders out of order
graphics_thread:worker::INWorker<graphics::GraphicsInstruction>,
physics_thread:worker::QNWorker<TimedInstruction<InputInstruction>>,
}
2023-10-20 02:19:05 +00:00
fn load_file(path: std::path::PathBuf)->Option<model::IndexedModelInstances>{
println!("Loading file: {:?}", &path);
//oh boy! let's load the map!
if let Ok(file)=std::fs::File::open(path){
let mut input = std::io::BufReader::new(file);
let mut first_8=[0u8;8];
//.rbxm roblox binary = "<roblox!"
//.rbxmx roblox xml = "<roblox "
//.bsp = "VBSP"
//.vmf =
//.snf = "SNMF"
//.snf = "SNBF"
if let (Ok(()),Ok(()))=(std::io::Read::read_exact(&mut input, &mut first_8),std::io::Seek::rewind(&mut input)){
match &first_8[0..4]{
b"<rob"=>{
match match &first_8[4..8]{
b"lox!"=>rbx_binary::from_reader(input).map_err(|e|format!("{:?}",e)),
b"lox "=>rbx_xml::from_reader(input,rbx_xml::DecodeOptions::default()).map_err(|e|format!("{:?}",e)),
other=>Err(format!("Unknown Roblox file type {:?}",other)),
}{
Ok(dom)=>Some(load_roblox::generate_indexed_models(dom)),
Err(e)=>{
println!("Error loading roblox file:{:?}",e);
None
},
}
},
//b"VBSP"=>Some(load_bsp::generate_indexed_models(input)),
//b"SNFM"=>Some(sniffer::generate_indexed_models(input)),
//b"SNFB"=>Some(sniffer::load_bot(input)),
other=>{
println!("loser file {:?}",other);
None
},
}
}else{
println!("Failed to read first 8 bytes and seek back to beginning of file.");
None
}
}else{
println!("Could not open file");
None
}
}
2023-10-20 22:50:26 +00:00
impl GlobalState {
2023-10-20 02:29:40 +00:00
fn init() -> Self {
2023-10-10 02:44:49 +00:00
//wee
let user_settings=settings::read_user_settings();
let mut indexed_models = Vec::new();
2023-09-27 09:12:20 +00:00
indexed_models.append(&mut model::generate_indexed_model_list_from_obj(obj::ObjData::load_buf(&include_bytes!("../models/teslacyberv3.0.obj")[..]).unwrap(),glam::Vec4::ONE));
2023-09-30 23:18:23 +00:00
indexed_models.push(primitives::unit_sphere());
indexed_models.push(primitives::unit_cylinder());
indexed_models.push(primitives::unit_cube());
println!("models.len = {:?}", indexed_models.len());
2023-09-27 09:12:20 +00:00
indexed_models[0].instances.push(model::ModelInstance{
transform:integer::Planar64Affine3::try_from(glam::Affine3A::from_translation(glam::vec3(10.,0.,-10.))).unwrap(),
..Default::default()
2023-09-21 20:02:01 +00:00
});
//quad monkeys
2023-09-27 09:12:20 +00:00
indexed_models[1].instances.push(model::ModelInstance{
transform:integer::Planar64Affine3::try_from(glam::Affine3A::from_translation(glam::vec3(10.,5.,10.))).unwrap(),
..Default::default()
2023-09-21 20:02:01 +00:00
});
2023-09-27 09:12:20 +00:00
indexed_models[1].instances.push(model::ModelInstance{
transform:integer::Planar64Affine3::try_from(glam::Affine3A::from_translation(glam::vec3(20.,5.,10.))).unwrap(),
2023-09-21 20:02:01 +00:00
color:glam::vec4(1.0,0.0,0.0,1.0),
..Default::default()
2023-09-21 20:02:01 +00:00
});
2023-09-27 09:12:20 +00:00
indexed_models[1].instances.push(model::ModelInstance{
transform:integer::Planar64Affine3::try_from(glam::Affine3A::from_translation(glam::vec3(10.,5.,20.))).unwrap(),
2023-09-21 20:02:01 +00:00
color:glam::vec4(0.0,1.0,0.0,1.0),
..Default::default()
2023-09-21 20:02:01 +00:00
});
2023-09-27 09:12:20 +00:00
indexed_models[1].instances.push(model::ModelInstance{
transform:integer::Planar64Affine3::try_from(glam::Affine3A::from_translation(glam::vec3(20.,5.,20.))).unwrap(),
2023-09-21 20:02:01 +00:00
color:glam::vec4(0.0,0.0,1.0,1.0),
..Default::default()
2023-10-03 23:34:54 +00:00
});
//decorative monkey
2023-09-27 09:12:20 +00:00
indexed_models[1].instances.push(model::ModelInstance{
transform:integer::Planar64Affine3::try_from(glam::Affine3A::from_translation(glam::vec3(15.,10.,15.))).unwrap(),
2023-10-03 23:34:54 +00:00
color:glam::vec4(0.5,0.5,0.5,0.5),
attributes:model::CollisionAttributes::Decoration,
..Default::default()
2023-09-21 20:02:01 +00:00
});
//teapot
2023-09-27 09:12:20 +00:00
indexed_models[2].instances.push(model::ModelInstance{
transform:integer::Planar64Affine3::try_from(glam::Affine3A::from_scale_rotation_translation(glam::vec3(0.5, 1.0, 0.2),glam::quat(-0.22248298016985793,-0.839457167990537,-0.05603504040830783,-0.49261857546227916),glam::vec3(-10.,7.,10.))).unwrap(),
..Default::default()
2023-09-21 20:02:01 +00:00
});
//ground
2023-09-27 09:12:20 +00:00
indexed_models[3].instances.push(model::ModelInstance{
transform:integer::Planar64Affine3::try_from(glam::Affine3A::from_translation(glam::vec3(0.,0.,0.))*glam::Affine3A::from_scale(glam::vec3(160.0, 1.0, 160.0))).unwrap(),
..Default::default()
2023-09-21 20:02:01 +00:00
});
2023-09-06 21:39:44 +00:00
2023-10-19 00:17:21 +00:00
let mut graphics=GraphicsState::new();
2023-09-21 05:36:42 +00:00
2023-10-10 02:44:49 +00:00
graphics.load_user_settings(&user_settings);
let indexed_model_instances=model::IndexedModelInstances{
textures:Vec::new(),
models:indexed_models,
2023-09-27 09:12:20 +00:00
spawn_point:integer::Planar64Vec3::Y*50,
2023-10-04 02:42:07 +00:00
modes:Vec::new(),
};
2023-10-05 03:04:04 +00:00
//how to multithread
//1. build
physics.generate_models(&indexed_model_instances);
//2. move
let physics_thread=physics.into_worker();
//3. forget
let mut state=GlobalState{
start_time:Instant::now(),
manual_mouse_lock:false,
mouse:physics::MouseState::default(),
2023-10-10 02:44:49 +00:00
user_settings,
2023-10-05 03:04:04 +00:00
graphics,
physics_thread,
};
state.generate_model_graphics(&device,&queue,indexed_model_instances);
2023-09-21 05:36:42 +00:00
2023-10-02 00:18:50 +00:00
let args:Vec<String>=std::env::args().collect();
if args.len()==2{
2023-10-20 02:19:05 +00:00
let indexed_model_instances=load_file(std::path::PathBuf::from(&args[1]));
state.render_thread=RenderThread::new(user_settings,indexed_model_instances);
2023-10-02 00:18:50 +00:00
}
return state;
2023-09-06 21:39:44 +00:00
}
2023-08-30 01:20:58 +00:00
}
2023-10-20 22:50:26 +00:00
fn main(){
let title=format!("Strafe Client v{}",env!("CARGO_PKG_VERSION")).as_str();
graphics_context::setup(title).start();
2023-08-30 01:20:58 +00:00
}