fix: rendering now displays using camera uniforms
This commit is contained in:
1 parent
2a1beaca43
commit
41ed63e732
5 files changed
+66
-39
No files matched your search
+13
-10
@@ -11,7 +11,7 @@ use crate::gpu::camera::CameraUniform;
|
||||
/// A camera set within 3D space.
|
||||
pub struct Camera {
|
||||
/// The position of the camera.
|
||||
position: Vector3<f32>,
|
||||
pub position: Vector3<f32>,
|
||||
/// The forward direction of the camera.
|
||||
forward: Vector3<f32>,
|
||||
/// The upward direction of the camera.
|
||||
@@ -31,17 +31,20 @@ impl Camera {
|
||||
extents: impl Into<Vector2<u32>>
|
||||
) -> Self {
|
||||
let extents = extents.into();
|
||||
let position = Vector3::new(1.0, 1.0, 1.0);
|
||||
let position = Vector3::new(20.0, 20.0, 20.0);
|
||||
let target = Vector3::zeros();
|
||||
|
||||
// Traveling along this vector moves us upward.
|
||||
let up = Vector3::new(0.0, 1.0, 0.0);
|
||||
// Traveling along this vector moves us toward the target.
|
||||
let forward = (target - position).normalize();
|
||||
|
||||
// A world up vector.
|
||||
let up_world = Vector3::y_axis();
|
||||
|
||||
// Traveling along this vector moves us to the right.
|
||||
let right = up.cross(&(position - target)).normalize();
|
||||
let right = up_world.cross(&forward).normalize();
|
||||
|
||||
// Traveling along this vector moves us toward the target.
|
||||
let forward = target - position;
|
||||
// Traveling along this vector moves us upward.
|
||||
let up = forward.cross(&right).normalize();
|
||||
|
||||
Self {
|
||||
position,
|
||||
@@ -55,17 +58,17 @@ impl Camera {
|
||||
|
||||
/// Moves the camera forward, or backward if given negative speed.
|
||||
pub fn move_forward(&mut self, speed: f32) {
|
||||
self.forward += self.forward * speed;
|
||||
self.position += self.forward * speed;
|
||||
}
|
||||
|
||||
/// Moves the camera to the right, or left if given negative speed.
|
||||
pub fn move_right(&mut self, speed: f32) {
|
||||
self.right += self.right * speed;
|
||||
self.position += self.right * speed;
|
||||
}
|
||||
|
||||
/// Moves the camera upwards, or downward if given negative speed.
|
||||
pub fn move_up(&mut self, speed: f32) {
|
||||
self.up += self.up * speed;
|
||||
self.position += self.up * speed;
|
||||
}
|
||||
|
||||
/// Sets the extents of the viewport.
|
||||
|
||||
+20
-13
@@ -58,16 +58,21 @@ impl MeshRegistry {
|
||||
})
|
||||
.collect::<HashMap<String, Obj>>();
|
||||
|
||||
let mut verts = Vec::new();
|
||||
let mut indices = Vec::new();
|
||||
let mut buf_verts = Vec::new();
|
||||
let mut buf_indices = Vec::new();
|
||||
let mut mesh_list = HashMap::new();
|
||||
meshes.into_iter().for_each(|(id, obj)| {
|
||||
// Information will be added directly to the verts and indices vectors, and a new Mesh
|
||||
// struct will be created and inserted into the mesh_list.
|
||||
let vert_start = verts.len();
|
||||
let all_verts = obj.vertices();
|
||||
let vert_len = all_verts.len();
|
||||
verts.extend(all_verts.map(|v| Vertex3::new(v.position(), v.normal().unwrap_or_default())));
|
||||
let vert_start = buf_verts.len();
|
||||
let all_verts = obj.positions();
|
||||
let all_norms = obj.normals();
|
||||
let obj_num_verts = all_verts.len();
|
||||
all_verts.iter().zip(all_norms.iter())
|
||||
.for_each(|(pos, norm)| {
|
||||
buf_verts.push(Vertex3::new(*pos, *norm));
|
||||
});
|
||||
// buf_verts.extend(all_verts.map(|v| Vertex3::new(v.position(), v.normal().unwrap_or_default())));
|
||||
|
||||
let index_list = obj
|
||||
.triangles()
|
||||
@@ -77,9 +82,11 @@ impl MeshRegistry {
|
||||
.collect::<Vec<u32>>()
|
||||
})
|
||||
.collect::<Vec<u32>>();
|
||||
let index_start = indices.len();
|
||||
let index_len = index_list.len();
|
||||
indices.extend(index_list);
|
||||
let index_start = buf_indices.len();
|
||||
let obj_num_idx = index_list.len();
|
||||
buf_indices.extend(index_list);
|
||||
|
||||
println!("Loaded mesh {} with {} vertices and {} indices", id, obj_num_verts, obj_num_idx);
|
||||
|
||||
// TODO Create bounding boxes for each mesh and associate them with the struct.
|
||||
mesh_list.insert(
|
||||
@@ -87,16 +94,16 @@ impl MeshRegistry {
|
||||
Mesh {
|
||||
id,
|
||||
vertex_loc: vert_start,
|
||||
vertex_count: vert_len,
|
||||
vertex_count: obj_num_verts,
|
||||
index_loc: index_start,
|
||||
index_count: index_len,
|
||||
index_count: obj_num_idx,
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
// Allocate buffers for the information.
|
||||
let vertex_buffer = Self::alloc_vert_buffer(allocator.clone(), verts);
|
||||
let index_buffer = Self::alloc_index_buffer(allocator.clone(), indices);
|
||||
let vertex_buffer = Self::alloc_vert_buffer(allocator.clone(), buf_verts);
|
||||
let index_buffer = Self::alloc_index_buffer(allocator.clone(), buf_indices);
|
||||
|
||||
// Finally, return the struct.
|
||||
Self {
|
||||
|
||||
Reference in new issue
Block a user