Load multiple textures

This commit is contained in:
2020-10-26 10:00:01 +01:00
parent c055ea19ed
commit 9687f5ec89
10 changed files with 73 additions and 32 deletions

View File

@@ -1,5 +1,5 @@
use std::sync::Arc;
use crate::vulkan::RendererDescriptorSets;
use crate::vulkan::{RendererDescriptorSets, TextureHandle};
use cgmath::{Matrix4, Vector3, Quaternion, Euler, Deg};
use crate::vulkan::{MeshHandle, VulkanRenderer};
use crate::input::InputState;
@@ -7,7 +7,8 @@ use crate::input::InputState;
#[derive(Clone)]
pub struct GameObject {
pub mesh_index: usize,
pub texture_index: usize,
pub texture_index: TextureHandle,
pub normal_map_index: TextureHandle,
pub position: Vector3<f32>,
pub rotation: Quaternion<f32>,
pub scale: Vector3<f32>,
@@ -16,8 +17,8 @@ pub struct GameObject {
}
impl GameObject {
pub fn new(mesh: MeshHandle, texture_index: usize) -> GameObject {
GameObject { mesh_index: mesh, texture_index, position: Vector3::new(0.0, 0.0, 0.0),
pub fn new(mesh: MeshHandle) -> GameObject {
GameObject { mesh_index: mesh.index, texture_index: mesh.diffuse_handle, normal_map_index: mesh.normal_handle, position: Vector3::new(0.0, 0.0, 0.0),
rotation: Quaternion::new(1.0, 0.0, 0.0, 0.0), scale: Vector3::new(1.0, 1.0, 1.0), children: vec![],
descriptor_sets: vec![] }
}

View File

@@ -1,13 +1,12 @@
use crate::vulkan::TextureHandle;
use crate::player::{Player};
use cgmath::{Matrix4, Vector3, vec3, Vector4};
use cgmath::{Matrix4, vec3, Vector3, Vector4};
use winit::event::Event;
use crate::config::LogConfig;
use crate::gameobject::{GameObject, GameObjectHandle, Updatable};
use crate::input::InputState;
use crate::player::Player;
use crate::vulkan::{Game, LinePoint, MeshHandle, VulkanRenderer};
use crate::vulkan::vs::ty::ObjectUniformData;
use crate::gameobject::{GameObject, GameObjectHandle, Updatable};
mod vulkan;
mod input;
@@ -19,7 +18,7 @@ mod player;
struct TestGame {
input: InputState,
player: Player,
meshes: Vec<(MeshHandle, TextureHandle)>,
meshes: Vec<MeshHandle>,
game_objects: Vec<GameObjectHandle>,
log_config: LogConfig,
texture_index_counter: usize,
@@ -117,6 +116,13 @@ impl TestGame {
println!("Game loaded!");
}
fn offset_texture_id(&mut self, local_tex_id: Option<usize>) -> usize {
match local_tex_id {
Some(local_id) => local_id + self.texture_index_counter,
None => 0,
}
}
fn load_gltf(&mut self, renderer: &mut VulkanRenderer, gltf_path: &str) -> Vec<MeshHandle> {
let mut mesh_handles = Vec::new();
@@ -125,23 +131,23 @@ impl TestGame {
for cpu_mesh in meshes.into_iter() {
// Convert file texture id to game texture id
let id = match cpu_mesh.texture_index {
Some(local_tex_id) => local_tex_id + self.texture_index_counter,
None => 0,
};
if cpu_mesh.texture_index.is_some() {
self.texture_index_counter += 1;
}
let diffuse_id = self.offset_texture_id(cpu_mesh.local_texture_index);
let normal_id = self.offset_texture_id(cpu_mesh.local_normal_map_index);
// Upload mesh
let mesh_handle = renderer.upload_mesh(cpu_mesh);
self.meshes.push((mesh_handle, id));
let mesh_id = renderer.upload_mesh(cpu_mesh);
let mesh_handle = MeshHandle {
index: mesh_id,
diffuse_handle: diffuse_id,
normal_handle: normal_id,
};
self.meshes.push(mesh_handle);
mesh_handles.push(mesh_handle);
}
// TODO: this assumes each texture is actually used in a mesh above
textures.iter().for_each(|tex| {
renderer.upload_texture(tex);
self.texture_index_counter += 1;
});
mesh_handles
}
@@ -152,8 +158,7 @@ impl TestGame {
}
fn add_game_object(&mut self, renderer: &mut VulkanRenderer, mesh: MeshHandle) -> &mut GameObjectHandle {
let (mesh_id, mesh_texture_id) = self.meshes[mesh];
let obj = GameObject::new(mesh_id, mesh_texture_id);
let obj = GameObject::new(mesh);
let obj_handle = renderer.add_game_object(obj);
self.game_objects.push(obj_handle);
self.game_objects.last_mut().unwrap()

View File

@@ -28,7 +28,8 @@ impl From<String> for LoadError {
pub struct CPUMesh {
pub vertices: Vec<Vertex>,
pub indices: Vec<u32>,
pub texture_index: Option<usize>,
pub local_texture_index: Option<usize>,
pub local_normal_map_index: Option<usize>,
pub name: Option<String>,
}
@@ -49,6 +50,7 @@ pub fn load_mesh(mesh_path: &str, print_status: bool) -> Result<(Vec<CPUMesh>, V
for mesh in document.meshes() {
for primitive in mesh.primitives() {
let texture_index = primitive.material().pbr_metallic_roughness().base_color_texture().map(|tex_info| tex_info.texture().index());
let normal_map_index = primitive.material().normal_texture().map(|tex_info| tex_info.texture().index());
let reader = primitive.reader(|buffer| Some(&buffers[buffer.index()]));
let indices = reader.read_indices().ok_or(NoIndices)?;
@@ -57,7 +59,8 @@ pub fn load_mesh(mesh_path: &str, print_status: bool) -> Result<(Vec<CPUMesh>, V
let cpu_mesh = CPUMesh {
vertices,
indices: indices.into_u32().collect(),
texture_index,
local_texture_index: texture_index,
local_normal_map_index: normal_map_index,
name: mesh.name().map(|n| n.to_owned()),
};
if print_status {

View File

@@ -34,7 +34,6 @@ use crate::mesh::CPUMesh;
use crate::gameobject::{GameObject, GameObjectHandle};
const VALIDATION_LAYERS: &[&str] = &[
"VK_LAYER_LUNARG_standard_validation"
];
#[derive(Default, Debug, Clone)]
@@ -65,7 +64,13 @@ pub struct Mesh {
index_buffer: Arc<CpuAccessibleBuffer<[u32]>>,
}
pub(crate) type MeshHandle = usize;
#[derive(Debug, Copy, Clone)]
pub struct MeshHandle {
pub index: usize,
pub diffuse_handle: TextureHandle,
pub normal_handle: TextureHandle,
}
pub(crate) type TextureHandle = usize;
pub struct GameData {
@@ -437,7 +442,7 @@ impl VulkanRenderer {
};
}
pub fn upload_mesh(self: &mut Self, mesh: CPUMesh) -> MeshHandle {
pub fn upload_mesh(self: &mut Self, mesh: CPUMesh) -> usize {
let vertex_buffer = CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::vertex_buffer(), false, mesh.vertices.into_iter()).unwrap();
let index_buffer = CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::index_buffer(), false, mesh.indices.into_iter()).unwrap();
self.game_data.meshes.push(Mesh { vertex_buffer, index_buffer });
@@ -463,11 +468,14 @@ impl VulkanRenderer {
pub fn add_game_object(self: &mut Self, mut game_object: GameObject) -> GameObjectHandle {
let descriptor_set_layout = self.pipeline.descriptor_set_layout(0).unwrap().clone();
println!("Diff: {:?}, Norm: {:?}", game_object.texture_index, game_object.normal_map_index);
let descriptor_sets = self.uniform_buffers.iter().map(|uniform_buffer| {
let builder = PersistentDescriptorSet::start(descriptor_set_layout.clone());
let result: Arc<RendererDescriptorSets> = Arc::new(builder
.add_buffer(uniform_buffer.clone()).unwrap()
.add_sampled_image(self.game_data.textures[game_object.texture_index].clone(), self.sampler.clone()).unwrap()
.add_sampled_image(self.game_data.textures[game_object.normal_map_index].clone(), self.sampler.clone()).unwrap()
.build().unwrap());
result
}).collect();