Load multiple textures
This commit is contained in:
39
src/main.rs
39
src/main.rs
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user