This commit is contained in:
2021-08-11 20:00:46 +02:00
parent b933a6dd35
commit c619f945a3
9 changed files with 96 additions and 37 deletions

View File

@@ -1,7 +1,8 @@
use std::time::SystemTime;
use cgmath::{Deg, Euler, Quaternion, vec3};
use glyph_brush::{BrushAction, BrushError, GlyphBrushBuilder, Section, Text};
use glyph_brush::ab_glyph::FontArc;
use vulkano::format::Format;
use winit::event::Event;
use level::{load_level, save_level};
@@ -9,9 +10,9 @@ use player::Player;
use crate::{config::LogConfig, vulkan};
use crate::input::InputState;
use crate::vulkan::{Game, MeshHandle, VulkanRenderer};
use crate::vulkan::{Game, MeshHandle, TextureHandle, Vertex, VulkanRenderer};
use crate::vulkan::gameobject::{GameObject, GameObjectHandle, Updatable};
use crate::vulkan::mesh;
use crate::vulkan::mesh::{self, CPUMesh};
use crate::vulkan::pipelines::vs::ty::ObjectUniformData;
pub mod player;
@@ -79,7 +80,6 @@ impl Game for TestGame {
if !self.paused {
self.player.update(frame_time, &self.input, renderer);
}
// self.game_objects[1].get_game_object_mut(renderer).unwrap().rotation = Quaternion::from_angle_y(Deg(time * -20.)).normalize();
// End frame
self.last_time = time;
@@ -116,6 +116,55 @@ impl TestGame {
pub fn game_start(self: &mut Self, renderer: &mut VulkanRenderer) {
load_level("levels/test.lvl", self, renderer).unwrap();
println!("Game loaded!");
let font = FontArc::try_from_slice(include_bytes!("../../models/OverpassRegular.ttf")).unwrap();
let mut glyph_brush = GlyphBrushBuilder::using_font(font).build();
glyph_brush.queue(Section::default().add_text(Text::new("rRRRRRRRRRRRRRr")));
match glyph_brush.process_queued(|rect, text_data| {
renderer.upload_texture(text_data, rect.width(), rect.height(), Format::R8Srgb, renderer.device.clone());
self.texture_index_counter += 1;
}, |vertex_data| {
vec![
Vertex { position: [vertex_data.pixel_coords.min.x, vertex_data.pixel_coords.min.y, 0.], uv: [vertex_data.tex_coords.min.x, vertex_data.tex_coords.min.y], normal: [0., 0., 1.], ..Default::default() },
Vertex { position: [vertex_data.pixel_coords.min.x, vertex_data.pixel_coords.max.y, 0.], uv: [vertex_data.tex_coords.min.x, vertex_data.tex_coords.max.y], normal: [0., 0., 1.], ..Default::default() },
]
}) {
Ok(BrushAction::Draw(quads)) => {
let mut final_vertices = vec![];
let mut final_indices = vec![];
let mut height = 0.2;
let mut index_offset = 2;
final_vertices.push(Vertex { position: [0., 0., 0.], uv: [0., 0.], normal: [0., 0., 1.], ..Default::default() });
final_vertices.push(Vertex { position: [0., height, 0.], uv: [0., 1.], normal: [0., 0., 1.], ..Default::default() });
for quad in quads {
final_vertices.append(&mut quad.clone());
final_indices.append(&mut [0, 2, 3, 0, 3, 1].iter().map(|x| *x + index_offset - 2).collect());
index_offset += quad.len() as u32;
}
println!("v: {:?}", final_vertices);
println!("i: {:?}", final_indices);
let mesh = CPUMesh {
vertices: final_vertices,
indices: final_indices,
local_texture_index: Some(self.texture_index_counter - 1),
local_normal_map_index: None,
name: Some("font_texture".to_string()),
};
let mesh_index = renderer.upload_mesh(mesh, None);
let mesh_handle = MeshHandle {
index: mesh_index,
diffuse_handle: self.texture_index_counter - 1,
normal_handle: None,
original_path: None,
};
let mut game_object_handle = self.add_game_object(renderer, mesh_handle);
game_object_handle.get_game_object_mut(renderer).unwrap().scale = vec3(10., 10., 10.);
self.game_objects.push(game_object_handle);
},
Ok(BrushAction::ReDraw) => {},
Err(BrushError::TextureTooSmall { suggested: _ }) => { println!("texture too small!"); },
}
}
pub fn offset_texture_id(&mut self, local_tex_id: Option<usize>) -> usize {
@@ -137,12 +186,12 @@ impl TestGame {
let normal_id = self.offset_texture_id(cpu_mesh.local_normal_map_index);
// Upload mesh
let mesh_id = renderer.upload_mesh(cpu_mesh, gltf_path.to_string());
let mesh_id = renderer.upload_mesh(cpu_mesh, Some(gltf_path.to_string()));
let mesh_handle = MeshHandle {
index: mesh_id,
diffuse_handle: diffuse_id,
normal_handle: normal_id,
original_path: gltf_path.to_string()
normal_handle: Some(normal_id),
original_path: Some(gltf_path.to_string())
};
mesh_handles.push(mesh_handle);
}