Get something on the screen

This commit is contained in:
hodasemi 2023-01-15 06:58:23 +01:00
parent 52b6e96f83
commit 7b1df782da
3 changed files with 79 additions and 43 deletions

View file

@ -106,6 +106,8 @@ impl Overlay {
swapchain.height(), swapchain.height(),
) )
.ok(); .ok();
write_log!("created RFactorData");
} }
// check twice for rfactor data, because of borrowing rules // check twice for rfactor data, because of borrowing rules

View file

@ -52,7 +52,6 @@ pub struct Rendering {
render_target: RenderTarget, render_target: RenderTarget,
command_buffer: Arc<CommandBuffer>, command_buffer: Arc<CommandBuffer>,
device: Arc<Device>,
queue: Arc<Mutex<Queue>>, queue: Arc<Mutex<Queue>>,
} }
@ -99,7 +98,6 @@ impl Rendering {
render_target, render_target,
command_buffer: CommandBuffer::new_primary().build(device.clone(), queue.clone())?, command_buffer: CommandBuffer::new_primary().build(device.clone(), queue.clone())?,
device,
queue, queue,
}) })
} }

View file

@ -3,9 +3,10 @@ use cgmath::{ortho, vec2, vec3, InnerSpace, Matrix4, Vector2, Vector3, VectorSpa
use rfactor_sm_reader::*; use rfactor_sm_reader::*;
use vulkan_rs::prelude::*; use vulkan_rs::prelude::*;
use std::sync::Arc; use std::{sync::Arc, time::Instant};
use super::rendering::PositionOnlyVertex; use super::rendering::PositionOnlyVertex;
use crate::write_log;
fn convert_vec(v: rF2Vec3) -> Vector3<f32> { fn convert_vec(v: rF2Vec3) -> Vector3<f32> {
vec3(v.x as f32, v.y as f32, v.z as f32) vec3(v.x as f32, v.y as f32, v.z as f32)
@ -44,9 +45,12 @@ pub struct RFactorData {
ortho: Matrix4<f32>, ortho: Matrix4<f32>,
window_width: u32, window_width: u32,
window_height: u32, window_height: u32,
radar_extent: f32,
car_width: f32, car_width: f32,
car_height: f32, car_height: f32,
start_time: Instant,
device: Arc<Device>, device: Arc<Device>,
descriptor_layout: Arc<DescriptorSetLayout>, descriptor_layout: Arc<DescriptorSetLayout>,
} }
@ -59,6 +63,8 @@ impl RFactorData {
width: u32, width: u32,
height: u32, height: u32,
) -> Result<Self> { ) -> Result<Self> {
write_log!(" =================== create RFactorData ===================");
let radar_extent = width as f32 * 0.075 * config.radar_scale; let radar_extent = width as f32 * 0.075 * config.radar_scale;
let car_height = radar_extent * 0.2; let car_height = radar_extent * 0.2;
let car_width = car_height * 0.5; let car_width = car_height * 0.5;
@ -68,12 +74,13 @@ impl RFactorData {
); );
let ortho = ortho(0.0, width as f32, 0.0, height as f32, -1.0, 1.0); let ortho = ortho(0.0, width as f32, 0.0, height as f32, -1.0, 1.0);
let start_time = Instant::now();
Ok(Self { Ok(Self {
config, config,
telemetry_reader: TelemetryReader::new()?, telemetry_reader: TelemetryReader::new(start_time.elapsed().as_secs_f32())?,
scoring_reader: ScoringReader::new()?, scoring_reader: ScoringReader::new(start_time.elapsed().as_secs_f32())?,
background: RadarObject::new( background: RadarObject::new(
device.clone(), device.clone(),
@ -111,15 +118,20 @@ impl RFactorData {
ortho, ortho,
window_width: width, window_width: width,
window_height: height, window_height: height,
radar_extent,
car_width, car_width,
car_height, car_height,
start_time,
device, device,
descriptor_layout: descriptor_layout.clone(), descriptor_layout: descriptor_layout.clone(),
}) })
} }
fn create_car_object(&self, offset: Vector2<f32>, color: [f32; 4]) -> Result<RadarObject> { fn create_car_object(&self, offset: Vector2<f32>, color: [f32; 4]) -> Result<RadarObject> {
write_log!(" =================== create car object ===================");
RadarObject::new( RadarObject::new(
self.device.clone(), self.device.clone(),
&self.descriptor_layout, &self.descriptor_layout,
@ -148,59 +160,81 @@ impl RFactorData {
) )
} }
pub fn update(&mut self) -> Result<()> { fn now(&self) -> f32 {
// get scoring info self.start_time.elapsed().as_secs_f32()
let (scoring_info, vehicle_scorings) = self.scoring_reader.vehicle_scoring(); }
// check for player id pub fn update(&mut self) -> Result<()> {
if scoring_info.mNumVehicles == 0 { write_log!(" =================== update RFactorData ===================");
self.player_id = None;
} else if self.player_id.is_none() { // get scoring info
for vehicle_scoring in vehicle_scorings.iter() { if let Some((scoring_info, vehicle_scorings)) =
if vehicle_scoring.mIsPlayer != 0 { self.scoring_reader.vehicle_scoring(self.now())
self.player_id = Some(vehicle_scoring.mID); {
write_log!(format!(
"new scoring info: vehicles: {}",
scoring_info.mNumVehicles
));
// check for player id
if scoring_info.mNumVehicles == 0 {
self.player_id = None;
} else if self.player_id.is_none() {
for vehicle_scoring in vehicle_scorings.iter() {
if vehicle_scoring.mIsPlayer != 0 {
write_log!(format!("player found: {}", vehicle_scoring.mID));
self.player_id = Some(vehicle_scoring.mID);
break;
}
} }
} }
} }
// if player id is set (a map is loaded), check telemetry data // if player id is set (a map is loaded), check telemetry data
if let Some(player_id) = &self.player_id { if let Some(player_id) = &self.player_id {
let telemetries = self.telemetry_reader.query_telemetry(); write_log!("before telemetry update");
if let Some(telemetries) = self.telemetry_reader.query_telemetry(self.now()) {
write_log!("new telemetry update");
let mut player_position = CarPosition::default(); let mut player_position = CarPosition::default();
let mut other_positions = Vec::new(); let mut other_positions = Vec::new();
for telemetry in telemetries { for telemetry in telemetries {
if telemetry.id == *player_id { if telemetry.id == *player_id {
player_position.position = convert_vec(telemetry.position); player_position.position = convert_vec(telemetry.position);
player_position.local_rotation = convert_vec(telemetry.local_rotation); player_position.local_rotation = convert_vec(telemetry.local_rotation);
} else { } else {
other_positions.push(CarPosition { other_positions.push(CarPosition {
position: convert_vec(telemetry.position), position: convert_vec(telemetry.position),
local_rotation: convert_vec(telemetry.local_rotation), local_rotation: convert_vec(telemetry.local_rotation),
}); });
}
} }
}
// update radar objects // update radar objects
// naive way: clear cars and create them new if near enough // naive way: clear cars and create them new if near enough
self.cars.clear(); self.cars.clear();
for other_position in other_positions { for other_position in other_positions {
let diff = player_position.position - other_position.position; let diff = player_position.position - other_position.position;
let distance = diff.magnitude(); let distance = diff.magnitude();
// check if car is close enough the players car // check if car is close enough the players car
if distance < self.config.radar_car_distance { if distance < self.config.radar_car_distance {
let offset = diff.truncate(); let distance_ratio = distance / self.config.radar_car_distance;
let color = self.config.danger_color.lerp(
self.config.safe_color,
distance / self.config.radar_car_distance,
);
self.cars let offset =
.push(self.create_car_object(offset, [color.x, color.y, color.z, 0.9])?); diff.truncate() * (self.radar_extent / self.config.radar_car_distance);
let color = self
.config
.danger_color
.lerp(self.config.safe_color, distance_ratio);
self.cars.push(
self.create_car_object(offset, [color.x, color.y, color.z, 0.9])?,
);
}
} }
} }
} }
@ -209,6 +243,8 @@ impl RFactorData {
} }
pub fn objects(&self) -> Vec<&dyn RenderObject> { pub fn objects(&self) -> Vec<&dyn RenderObject> {
write_log!(" =================== get objects of RFactorData ===================");
let mut objects: Vec<&dyn RenderObject> = Vec::new(); let mut objects: Vec<&dyn RenderObject> = Vec::new();
// only draw radar when player is loaded into a map // only draw radar when player is loaded into a map