Different textures per object
This commit is contained in:
Binary file not shown.
BIN
models/plane.glb
BIN
models/plane.glb
Binary file not shown.
@@ -1,46 +1,50 @@
|
||||
use std::sync::Arc;
|
||||
use crate::vulkan::RendererDescriptorSets;
|
||||
use cgmath::{Matrix4, Vector3, Quaternion, Euler, Deg};
|
||||
use crate::vulkan::{MeshHandle, VulkanRenderer};
|
||||
use crate::input::InputState;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Clone)]
|
||||
pub struct GameObject {
|
||||
pub mesh_index: usize,
|
||||
pub texture_index: usize,
|
||||
pub position: Vector3<f32>,
|
||||
pub rotation: Quaternion<f32>,
|
||||
pub scale: Vector3<f32>,
|
||||
pub children: Vec<GameObject>
|
||||
pub children: Vec<GameObject>,
|
||||
pub descriptor_sets: Vec<Arc<RendererDescriptorSets>>
|
||||
}
|
||||
|
||||
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),
|
||||
rotation: Quaternion::new(1.0, 0.0, 0.0, 0.0), scale: Vector3::new(1.0, 1.0, 1.0), children: vec![] }
|
||||
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![] }
|
||||
}
|
||||
|
||||
pub fn set_position(&mut self, x: f32, y: f32, z: f32) {
|
||||
pub fn _set_position(&mut self, x: f32, y: f32, z: f32) {
|
||||
self.position.x = x;
|
||||
self.position.y = y;
|
||||
self.position.z = z;
|
||||
}
|
||||
|
||||
pub fn set_scale(&mut self, x: f32, y: f32, z: f32) {
|
||||
pub fn _set_scale(&mut self, x: f32, y: f32, z: f32) {
|
||||
self.scale.x = x;
|
||||
self.scale.y = y;
|
||||
self.scale.z = z;
|
||||
}
|
||||
|
||||
pub fn set_rotation(&mut self, euler_x: f32, euler_y: f32, euler_z: f32) {
|
||||
pub fn _set_rotation(&mut self, euler_x: f32, euler_y: f32, euler_z: f32) {
|
||||
self.rotation = Quaternion::from(Euler::new(Deg(euler_x), Deg(euler_y), Deg(euler_z)));
|
||||
}
|
||||
|
||||
pub fn translate(&mut self, x: f32, y: f32, z: f32) {
|
||||
pub fn _translate(&mut self, x: f32, y: f32, z: f32) {
|
||||
self.position.x += x;
|
||||
self.position.y += y;
|
||||
self.position.z += z;
|
||||
}
|
||||
|
||||
pub fn rotate(&mut self, x: f32, y: f32, z: f32) {
|
||||
pub fn _rotate(&mut self, x: f32, y: f32, z: f32) {
|
||||
self.rotation = self.rotation * Quaternion::from(Euler::new(Deg(x), Deg(y), Deg(z)));
|
||||
}
|
||||
|
||||
|
||||
54
src/main.rs
54
src/main.rs
@@ -1,5 +1,6 @@
|
||||
use crate::player::{Camera, Player};
|
||||
use cgmath::{Deg, Matrix4, Quaternion, Rotation, Rotation3, Vector3, vec3, Vector4};
|
||||
use crate::vulkan::TextureHandle;
|
||||
use crate::player::{Player};
|
||||
use cgmath::{Matrix4, Vector3, vec3, Vector4};
|
||||
use winit::event::Event;
|
||||
|
||||
use crate::config::LogConfig;
|
||||
@@ -18,13 +19,12 @@ mod player;
|
||||
struct TestGame {
|
||||
input: InputState,
|
||||
player: Player,
|
||||
meshes: Vec<(MeshHandle, usize)>,
|
||||
meshes: Vec<(MeshHandle, TextureHandle)>,
|
||||
game_objects: Vec<GameObjectHandle>,
|
||||
log_config: LogConfig,
|
||||
texture_index_counter: usize,
|
||||
last_time: f32,
|
||||
components: Vec<Box<dyn Updatable>>,
|
||||
show_grid: bool,
|
||||
}
|
||||
|
||||
impl TestGame {
|
||||
@@ -38,7 +38,6 @@ impl TestGame {
|
||||
texture_index_counter: 1,
|
||||
last_time: 0.0,
|
||||
components: vec![],
|
||||
show_grid: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,9 +78,7 @@ impl Game for TestGame {
|
||||
}
|
||||
|
||||
// Custom game object stuff
|
||||
let light_pos = vec3(f32::sin(time) * 2.0 + 2.0, f32::cos(time) * 2.0 + 2.0, 2.0);
|
||||
self.game_objects[2].get_game_object(renderer).unwrap().position = light_pos;
|
||||
|
||||
let light_pos = self.player.camera.position * -1.0;
|
||||
self.player.update(frame_time, input, renderer);
|
||||
|
||||
// End frame
|
||||
@@ -108,20 +105,13 @@ fn _matrix_vector_mul(matrix: &Matrix4<f32>, vector: &Vector3<f32>) -> Vector3<f
|
||||
|
||||
impl TestGame {
|
||||
fn game_start(self: &mut Self, renderer: &mut VulkanRenderer) {
|
||||
let cube_meshes = self.load_gltf(renderer, "models/box.glb");
|
||||
let sphere_meshes = self.load_gltf(renderer, "models/sphere.glb");
|
||||
let plane_meshes = self.load_gltf(renderer, "models/plane.glb");
|
||||
|
||||
let cube = self.add_game_object(renderer, *cube_meshes.first().unwrap(), 0);
|
||||
let cube = self.add_from_gltf(renderer, "models/box.glb");
|
||||
cube.get_game_object(renderer).unwrap().position = vec3(3.0, 4.0, 5.0);
|
||||
|
||||
let sphere = self.add_game_object(renderer, *sphere_meshes.first().unwrap(), 0);
|
||||
let sphere = self.add_from_gltf(renderer, "models/sphere.glb");
|
||||
sphere.get_game_object(renderer).unwrap().position = vec3(0.0, 0.0, 0.0);
|
||||
|
||||
let light_sphere = self.add_game_object(renderer, *sphere_meshes.first().unwrap(), 0);
|
||||
light_sphere.get_game_object(renderer).unwrap().scale = vec3(0.1, 0.1, 0.1);
|
||||
|
||||
let plane = self.add_game_object(renderer, *plane_meshes.first().unwrap(), 0);
|
||||
let plane = self.add_from_gltf(renderer, "models/plane.glb");
|
||||
plane.get_game_object(renderer).unwrap().position = vec3(0.0, -1.0, 0.0);
|
||||
|
||||
println!("Game loaded!");
|
||||
@@ -129,23 +119,41 @@ impl TestGame {
|
||||
|
||||
fn load_gltf(&mut self, renderer: &mut VulkanRenderer, gltf_path: &str) -> Vec<MeshHandle> {
|
||||
let mut mesh_handles = Vec::new();
|
||||
|
||||
// Load file
|
||||
let (meshes, textures) = mesh::load_mesh(gltf_path, self.log_config.mesh_load_info).unwrap();
|
||||
|
||||
for cpu_mesh in meshes.into_iter() {
|
||||
// Convert file texture id to game texture id
|
||||
let id = match cpu_mesh.texture_index {
|
||||
Some(tex_id) => tex_id + self.texture_index_counter,
|
||||
Some(local_tex_id) => local_tex_id + self.texture_index_counter,
|
||||
None => 0,
|
||||
};
|
||||
self.texture_index_counter += 1;
|
||||
if cpu_mesh.texture_index.is_some() {
|
||||
self.texture_index_counter += 1;
|
||||
}
|
||||
|
||||
// Upload mesh
|
||||
let mesh_handle = renderer.upload_mesh(cpu_mesh);
|
||||
self.meshes.push((mesh_handle, id));
|
||||
mesh_handles.push(mesh_handle);
|
||||
}
|
||||
textures.iter().for_each(|tex| renderer.upload_texture(tex));
|
||||
|
||||
// TODO: this assumes each texture is actually used in a mesh above
|
||||
textures.iter().for_each(|tex| {
|
||||
renderer.upload_texture(tex);
|
||||
});
|
||||
mesh_handles
|
||||
}
|
||||
|
||||
fn add_game_object(&mut self, renderer: &mut VulkanRenderer, mesh: MeshHandle, texture_index: usize) -> &mut GameObjectHandle {
|
||||
let obj = GameObject::new(mesh, texture_index);
|
||||
fn add_from_gltf(&mut self, renderer: &mut VulkanRenderer, path: &str) -> &mut GameObjectHandle {
|
||||
let meshes = self.load_gltf(renderer, path);
|
||||
self.add_game_object(renderer, *meshes.first().unwrap())
|
||||
}
|
||||
|
||||
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_handle = renderer.add_game_object(obj);
|
||||
self.game_objects.push(obj_handle);
|
||||
self.game_objects.last_mut().unwrap()
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use cgmath::{vec3, One, SquareMatrix, Deg, Rad, Quaternion, Vector3, Matrix4, Rotation, Rotation3, InnerSpace};
|
||||
use crate::{
|
||||
VulkanRenderer,
|
||||
GameObjectHandle,
|
||||
input::InputState,
|
||||
gameobject::Updatable
|
||||
};
|
||||
|
||||
@@ -66,6 +66,7 @@ pub struct Mesh {
|
||||
}
|
||||
|
||||
pub(crate) type MeshHandle = usize;
|
||||
pub(crate) type TextureHandle = usize;
|
||||
|
||||
pub struct GameData {
|
||||
pub start_time: SystemTime,
|
||||
@@ -81,7 +82,7 @@ pub struct GameData {
|
||||
pub use_line_pipeline: bool,
|
||||
}
|
||||
|
||||
type RendererDescriptorSets = dyn DescriptorSet + Send + Sync;
|
||||
pub(crate) type RendererDescriptorSets = dyn DescriptorSet + Send + Sync;
|
||||
|
||||
pub struct VulkanRenderer {
|
||||
pub game_data: GameData,
|
||||
@@ -100,7 +101,6 @@ pub struct VulkanRenderer {
|
||||
pub debug_callback: Option<DebugCallback>,
|
||||
pub previous_frame_end: Option<Box<dyn GpuFuture>>,
|
||||
pub uniform_buffers: Vec<Arc<CpuAccessibleBuffer<vs::ty::ObjectUniformData>>>,
|
||||
pub descriptor_sets: Vec<Arc<RendererDescriptorSets>>,
|
||||
}
|
||||
|
||||
impl VulkanRenderer {
|
||||
@@ -299,16 +299,7 @@ impl VulkanRenderer {
|
||||
).unwrap());
|
||||
}
|
||||
|
||||
let descriptor_set_layout = pipeline.descriptor_set_layout(0).unwrap().clone();
|
||||
|
||||
let descriptor_sets = 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(default_tex.clone(), sampler.clone()).unwrap()
|
||||
.build().unwrap());
|
||||
result
|
||||
}).collect();
|
||||
|
||||
|
||||
data.textures.push(default_tex);
|
||||
|
||||
@@ -321,7 +312,7 @@ impl VulkanRenderer {
|
||||
let previous_frame_end = Some(Box::new(sync::now(device.clone())) as Box<dyn GpuFuture>);
|
||||
|
||||
(VulkanRenderer { game_data: data, device, framebuffers, sampler,
|
||||
dynamic_state, pipeline, line_pipeline, uniform_buffers, descriptor_sets,
|
||||
dynamic_state, pipeline, line_pipeline, uniform_buffers,
|
||||
surface, swapchain, render_pass, queue, line_vertex_buffer,
|
||||
recreate_swapchain: false, debug_callback, previous_frame_end }, events_loop)
|
||||
}
|
||||
@@ -343,7 +334,7 @@ impl VulkanRenderer {
|
||||
&self.dynamic_state,
|
||||
vec![mesh.vertex_buffer.clone()],
|
||||
mesh.index_buffer.clone(),
|
||||
self.descriptor_sets[fb_index].clone(),
|
||||
game_object.descriptor_sets[fb_index].clone(),
|
||||
self.game_data.push_constants.clone()).unwrap();
|
||||
}
|
||||
|
||||
@@ -469,8 +460,21 @@ impl VulkanRenderer {
|
||||
self.game_data.textures.push(image_view);
|
||||
}
|
||||
|
||||
pub fn add_game_object(self: &mut Self, game_object: GameObject) -> GameObjectHandle {
|
||||
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();
|
||||
|
||||
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()
|
||||
.build().unwrap());
|
||||
result
|
||||
}).collect();
|
||||
|
||||
game_object.descriptor_sets = descriptor_sets;
|
||||
self.game_data.game_objects.push(game_object);
|
||||
|
||||
GameObjectHandle {
|
||||
object_index: self.game_data.game_objects.len() - 1
|
||||
}
|
||||
@@ -478,20 +482,14 @@ impl VulkanRenderer {
|
||||
}
|
||||
|
||||
pub fn start_event_loop(mut renderer: VulkanRenderer, mut game: Box<dyn Game>, event_loop: EventLoop<()>) {
|
||||
let mut recreate_swapchain = false;
|
||||
let mut ubo = game.update(&mut renderer);
|
||||
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
game.on_window_event(&event);
|
||||
match event {
|
||||
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
|
||||
*control_flow = ControlFlow::Exit;
|
||||
},
|
||||
Event::WindowEvent { event: WindowEvent::Resized(_), .. } => {
|
||||
recreate_swapchain = true;
|
||||
},
|
||||
Event::RedrawEventsCleared => {
|
||||
ubo = game.update(&mut renderer);
|
||||
let ubo = game.update(&mut renderer);
|
||||
renderer.render_loop(ubo);
|
||||
},
|
||||
_ => {}
|
||||
|
||||
Reference in New Issue
Block a user