Compare commits

...

6 Commits

Author SHA1 Message Date
13a1db86e8 implement config 2023-10-09 19:44:49 -07:00
832b359cca fix and tweak config 2023-10-09 19:43:55 -07:00
ce9a461887 implement calculate functions in UserSettings 2023-10-09 19:43:33 -07:00
31b2dfe314 calculators 2023-10-09 18:29:25 -07:00
726a66d955 write it 2023-10-09 17:55:02 -07:00
4864c12779 settings module 2023-10-09 17:09:24 -07:00
3 changed files with 151 additions and 10 deletions

View File

@ -10,6 +10,7 @@ mod model;
mod zeroes;
mod worker;
mod physics;
mod settings;
mod framework;
mod primitives;
mod instruction;
@ -64,10 +65,10 @@ fn perspective_rh(fov_x_slope: f32, fov_y_slope: f32, z_near: f32, z_far: f32) -
)
}
impl GraphicsCamera{
pub fn new(screen_size:glam::UVec2,fov_y:f32)->Self{
pub fn new(screen_size:glam::UVec2,fov:glam::Vec2)->Self{
Self{
screen_size,
fov: glam::vec2(fov_y*(screen_size.x as f32)/(screen_size.y as f32),fov_y),
fov,
}
}
pub fn proj(&self)->glam::Mat4{
@ -77,10 +78,6 @@ impl GraphicsCamera{
//f32 good enough for view matrix
glam::Mat4::from_translation(pos) * glam::Mat4::from_euler(glam::EulerRot::YXZ, angles.x, angles.y, 0f32)
}
pub fn set_screen_size(&mut self,screen_size:glam::UVec2){
self.screen_size=screen_size;
self.fov.x=self.fov.y*(screen_size.x as f32)/(screen_size.y as f32);
}
pub fn to_uniform_data(&self,(pos,angles): (glam::Vec3,glam::Vec2)) -> [f32; 16 * 4] {
let proj=self.proj();
@ -114,12 +111,16 @@ impl GraphicsState{
pub fn clear(&mut self){
self.models.clear();
}
pub fn load_user_settings(&mut self,user_settings:&settings::UserSettings){
self.camera.fov=user_settings.calculate_fov(1.0,&self.camera.screen_size).as_vec2();
}
}
pub struct GlobalState{
start_time: std::time::Instant,
manual_mouse_lock:bool,
mouse:physics::MouseState,
user_settings:settings::UserSettings,
graphics:GraphicsState,
physics_thread:worker::CompatWorker<TimedInstruction<InputInstruction>,physics::PhysicsOutputState,Box<dyn FnMut(TimedInstruction<InputInstruction>)->physics::PhysicsOutputState>>,
}
@ -413,6 +414,8 @@ impl framework::Example for GlobalState {
device: &wgpu::Device,
queue: &wgpu::Queue,
) -> Self {
//wee
let user_settings=settings::read_user_settings();
let mut indexed_models = Vec::new();
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.as_ref()));
indexed_models.push(primitives::unit_sphere());
@ -752,7 +755,11 @@ impl framework::Example for GlobalState {
let mut physics = physics::PhysicsState::default();
let camera=GraphicsCamera::new(glam::uvec2(config.width,config.height), 1.0);
physics.load_user_settings(&user_settings);
let screen_size=glam::uvec2(config.width,config.height);
let camera=GraphicsCamera::new(screen_size,user_settings.calculate_fov(1.0,&screen_size).as_vec2());
let camera_uniforms = camera.to_uniform_data(physics.output().adjust_mouse(&physics.next_mouse));
let camera_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Camera"),
@ -787,7 +794,7 @@ impl framework::Example for GlobalState {
let depth_view = Self::create_depth_texture(config, device);
let graphics=GraphicsState {
let mut graphics=GraphicsState {
pipelines:GraphicsPipelines{
skybox:sky_pipeline,
model:model_pipeline
@ -806,6 +813,8 @@ impl framework::Example for GlobalState {
temp_squid_texture_view: squid_texture_view,
};
graphics.load_user_settings(&user_settings);
let indexed_model_instances=model::IndexedModelInstances{
textures:Vec::new(),
models:indexed_models,
@ -827,6 +836,7 @@ impl framework::Example for GlobalState {
start_time:Instant::now(),
manual_mouse_lock:false,
mouse:physics::MouseState::default(),
user_settings,
graphics,
physics_thread,
};
@ -888,9 +898,11 @@ impl framework::Example for GlobalState {
time:physics.time,
instruction: PhysicsInstruction::Input(physics::PhysicsInputInstruction::Reset),
});
physics.load_user_settings(&self.user_settings);
physics.generate_models(&indexed_model_instances);
self.physics_thread=physics.into_worker();
//graphics.load_user_settings(&self.user_settings);
self.generate_model_graphics(device,queue,indexed_model_instances);
//manual reset
}else{
@ -1029,7 +1041,8 @@ impl framework::Example for GlobalState {
_queue: &wgpu::Queue,
) {
self.graphics.depth_view = Self::create_depth_texture(config, device);
self.graphics.camera.set_screen_size(glam::uvec2(config.width, config.height));
self.graphics.camera.screen_size=glam::uvec2(config.width, config.height);
self.graphics.load_user_settings(&self.user_settings);
}
fn render(

View File

@ -175,7 +175,7 @@ impl PhysicsCamera {
Self{
offset,
angles: glam::DVec2::ZERO,
sensitivity: glam::dvec2(1.0/16384.0,1.0/16384.0),
sensitivity: glam::dvec2(1.0/1024.0,1.0/1024.0),
mouse:MouseState{pos:glam::IVec2::ZERO,time:-1},//escape initialization hell divide by zero
}
}
@ -617,6 +617,10 @@ impl PhysicsState {
println!("Physics Objects: {}",self.models.len());
}
pub fn load_user_settings(&mut self,user_settings:&crate::settings::UserSettings){
self.camera.sensitivity=user_settings.calculate_sensitivity();
}
pub fn get_mode(&self,mode_id:u32)->Option<&crate::model::ModeDescription>{
if let Some(&mode)=self.mode_from_mode_id.get(&mode_id){
self.modes.get(mode)

124
src/settings.rs Normal file
View File

@ -0,0 +1,124 @@
struct Ratio{
ratio:f64,
}
enum DerivedFov{
FromScreenAspect,
FromAspect(Ratio),
}
enum Fov{
Exactly{x:f64,y:f64},
DeriveX{x:DerivedFov,y:f64},
DeriveY{x:f64,y:DerivedFov},
}
impl Default for Fov{
fn default() -> Self {
Fov::DeriveX{x:DerivedFov::FromScreenAspect,y:1.0}
}
}
enum Sensitivity{
Exactly{x:f64,y:f64},
DeriveX{x:Ratio,y:f64},
DeriveY{x:f64,y:Ratio},
}
impl Default for Sensitivity{
fn default() -> Self {
Sensitivity::DeriveY{x:0.001,y:Ratio{ratio:1.0}}
}
}
#[derive(Default)]
pub struct UserSettings{
fov:Fov,
sensitivity:Sensitivity,
}
impl UserSettings{
pub fn calculate_fov(&self,zoom:f64,screen_size:&glam::UVec2)->glam::DVec2{
zoom*match &self.fov{
&Fov::Exactly{x,y}=>glam::dvec2(x,y),
Fov::DeriveX{x,y}=>match x{
DerivedFov::FromScreenAspect=>glam::dvec2(y*(screen_size.x as f64/screen_size.y as f64),*y),
DerivedFov::FromAspect(ratio)=>glam::dvec2(y*ratio.ratio,*y),
},
Fov::DeriveY{x,y}=>match y{
DerivedFov::FromScreenAspect=>glam::dvec2(*x,x*(screen_size.y as f64/screen_size.x as f64)),
DerivedFov::FromAspect(ratio)=>glam::dvec2(*x,x*ratio.ratio),
},
}
}
pub fn calculate_sensitivity(&self)->glam::DVec2{
match &self.sensitivity{
&Sensitivity::Exactly{x,y}=>glam::dvec2(x,y),
Sensitivity::DeriveX{x,y}=>glam::dvec2(y*x.ratio,*y),
Sensitivity::DeriveY{x,y}=>glam::dvec2(*x,x*y.ratio),
}
}
}
/*
//sensitivity is raw input dots (i.e. dpi = dots per inch) to radians conversion factor
sensitivity_x=0.001
sensitivity_y_from_x_ratio=1
Sensitivity::DeriveY{x:0.0.001,y:DerivedSensitivity{ratio:1.0}}
*/
pub fn read_user_settings()->UserSettings{
let mut cfg=configparser::ini::Ini::new();
if let Ok(_)=cfg.load("settings.conf"){
let (cfg_fov_x,cfg_fov_y)=(cfg.getfloat("camera","fov_x"),cfg.getfloat("camera","fov_y"));
let fov=match(cfg_fov_x,cfg_fov_y){
(Ok(Some(fov_x)),Ok(Some(fov_y)))=>Fov::Exactly {
x:fov_x,
y:fov_y
},
(Ok(Some(fov_x)),Ok(None))=>Fov::DeriveY{
x:fov_x,
y:if let Ok(Some(fov_y_from_x_ratio))=cfg.getfloat("camera","fov_y_from_x_ratio"){
DerivedFov::FromAspect(Ratio{ratio:fov_y_from_x_ratio})
}else{
DerivedFov::FromScreenAspect
}
},
(Ok(None),Ok(Some(fov_y)))=>Fov::DeriveX{
x:if let Ok(Some(fov_x_from_y_ratio))=cfg.getfloat("camera","fov_x_from_y_ratio"){
DerivedFov::FromAspect(Ratio{ratio:fov_x_from_y_ratio})
}else{
DerivedFov::FromScreenAspect
},
y:fov_y,
},
_=>{
Fov::default()
},
};
let (cfg_sensitivity_x,cfg_sensitivity_y)=(cfg.getfloat("camera","sensitivity_x"),cfg.getfloat("camera","sensitivity_y"));
let sensitivity=match(cfg_sensitivity_x,cfg_sensitivity_y){
(Ok(Some(sensitivity_x)),Ok(Some(sensitivity_y)))=>Sensitivity::Exactly {
x:sensitivity_x,
y:sensitivity_y
},
(Ok(Some(sensitivity_x)),Ok(None))=>Sensitivity::DeriveY{
x:sensitivity_x,
y:Ratio{
ratio:if let Ok(Some(sensitivity_y_from_x_ratio))=cfg.getfloat("camera","sensitivity_y_from_x_ratio"){sensitivity_y_from_x_ratio}else{1.0}
}
},
(Ok(None),Ok(Some(sensitivity_y)))=>Sensitivity::DeriveX{
x:Ratio{
ratio:if let Ok(Some(sensitivity_x_from_y_ratio))=cfg.getfloat("camera","sensitivity_x_from_y_ratio"){sensitivity_x_from_y_ratio}else{1.0}
},
y:sensitivity_y,
},
_=>{
Sensitivity::default()
},
};
UserSettings{
fov,
sensitivity,
}
}else{
UserSettings::default()
}
}