summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-08-17 13:43:45 +0200
committerHampusM <hampus@hampusmat.com>2024-08-17 13:43:45 +0200
commit60f6a0b7783d6d3684f22f8def3c1097963aa3ff (patch)
treef5df89fd47a802c68d0717cdc144a891750780cd
parentc4686c2992417545e7a05a6a40ee9f1a8bbf3b96 (diff)
refactor(engine): remove position field from Camera component
-rw-r--r--TODO.md3
-rw-r--r--engine/src/camera.rs2
-rw-r--r--engine/src/camera/fly.rs18
-rw-r--r--engine/src/renderer/opengl.rs26
4 files changed, 24 insertions, 25 deletions
diff --git a/TODO.md b/TODO.md
index ad62857..8c5fc45 100644
--- a/TODO.md
+++ b/TODO.md
@@ -10,8 +10,7 @@
- [x] Add component events (removal & addition) in ECS framework
- [x] Make renderer not create new buffers each frame
- [ ] Multiple rendering passes
-- [ ] Make projection changable
-- [ ] Remove position field from Camera component
+- [x] Remove position field from Camera component
- [ ] Effects using stencil buffer
- [ ] Remove possible edge cases in ECS component storage
- [ ] Fix OpenGL warning "Vertex shader in program 3 is being recompiled based on GL state".
diff --git a/engine/src/camera.rs b/engine/src/camera.rs
index 5b71e2f..46b7293 100644
--- a/engine/src/camera.rs
+++ b/engine/src/camera.rs
@@ -8,7 +8,6 @@ pub mod fly;
#[derive(Debug, Component)]
pub struct Camera
{
- pub position: Vec3<f32>,
pub target: Vec3<f32>,
pub global_up: Vec3<f32>,
pub current: bool,
@@ -20,7 +19,6 @@ impl Default for Camera
fn default() -> Self
{
Self {
- position: Vec3 { x: 0.0, y: 0.0, z: 3.0 },
target: Vec3::default(),
global_up: Vec3::UP,
current: false,
diff --git a/engine/src/camera/fly.rs b/engine/src/camera/fly.rs
index ed41253..5b55cec 100644
--- a/engine/src/camera/fly.rs
+++ b/engine/src/camera/fly.rs
@@ -8,6 +8,7 @@ use crate::camera::Camera;
use crate::delta_time::DeltaTime;
use crate::event::Update as UpdateEvent;
use crate::input::{Cursor, CursorFlags, Keys};
+use crate::transform::Position;
use crate::vector::{Vec2, Vec3};
/// Fly camera extension.
@@ -38,7 +39,7 @@ pub struct Options
}
fn update(
- camera_query: Query<(Camera,)>,
+ camera_query: Query<(Camera, Position)>,
keys: Single<Keys>,
cursor: Single<Cursor>,
cursor_flags: Single<CursorFlags>,
@@ -47,7 +48,7 @@ fn update(
options: Local<Options>,
)
{
- let Some((mut camera,)) = camera_query.iter().next() else {
+ let Some((mut camera, mut camera_pos)) = camera_query.iter().next() else {
#[cfg(feature = "debug")]
tracing::warn!("No camera");
@@ -91,26 +92,29 @@ fn update(
camera.global_up = cam_right.cross(&direction).normalize();
if matches!(keys.get_key_state(Key::W), KeyState::Pressed) {
- camera.position += direction * options.camera_speed * delta_time.as_secs_f32();
+ camera_pos.position +=
+ direction * options.camera_speed * delta_time.as_secs_f32();
}
if matches!(keys.get_key_state(Key::S), KeyState::Pressed) {
- camera.position -= direction * options.camera_speed * delta_time.as_secs_f32();
+ camera_pos.position -=
+ direction * options.camera_speed * delta_time.as_secs_f32();
}
if matches!(keys.get_key_state(Key::A), KeyState::Pressed) {
let cam_left = -direction.cross(&Vec3::UP).normalize();
- camera.position += cam_left * options.camera_speed * delta_time.as_secs_f32();
+ camera_pos.position += cam_left * options.camera_speed * delta_time.as_secs_f32();
}
if matches!(keys.get_key_state(Key::D), KeyState::Pressed) {
let cam_right = direction.cross(&Vec3::UP).normalize();
- camera.position += cam_right * options.camera_speed * delta_time.as_secs_f32();
+ camera_pos.position +=
+ cam_right * options.camera_speed * delta_time.as_secs_f32();
}
- camera.target = camera.position + direction;
+ camera.target = camera_pos.position + direction;
}
#[derive(Debug, Component)]
diff --git a/engine/src/renderer/opengl.rs b/engine/src/renderer/opengl.rs
index 5c7435c..a90c069 100644
--- a/engine/src/renderer/opengl.rs
+++ b/engine/src/renderer/opengl.rs
@@ -115,20 +115,16 @@ fn render(
query: Query<RenderableEntity, Not<With<NoDraw>>>,
point_light_query: Query<(PointLight,)>,
directional_lights: Query<(DirectionalLight,)>,
- camera_query: Query<(Camera,)>,
+ camera_query: Query<(Camera, Position)>,
window: Single<Window>,
global_light: Single<GlobalLight>,
mut gl_objects: Local<GlobalGlObjects>,
mut actions: Actions,
)
{
- let Some(camera) = camera_query.iter().find_map(|(camera,)| {
- if !camera.current {
- return None;
- }
-
- Some(camera)
- }) else {
+ let Some((camera, camera_pos)) =
+ camera_query.iter().find(|(camera, _)| camera.current)
+ else {
#[cfg(feature = "debug")]
tracing::warn!("No current camera. Nothing will be rendered");
return;
@@ -196,6 +192,7 @@ fn render(
},
shader_program,
&camera,
+ &camera_pos,
window.size().expect("Failed to get window size"),
);
@@ -210,7 +207,7 @@ fn render(
.map(|(dir_light,)| &**dir_light)
.collect::<Vec<_>>()
.as_slice(),
- &camera,
+ &camera_pos,
);
for texture in &material.textures {
@@ -430,6 +427,7 @@ fn apply_transformation_matrices(
transformation: Transformation,
gl_shader_program: &mut GlShaderProgram,
camera: &Camera,
+ camera_pos: &Position,
window_size: Dimens<u32>,
)
{
@@ -438,7 +436,7 @@ fn apply_transformation_matrices(
&create_transformation_matrix(transformation),
);
- let view = create_view(camera);
+ let view = create_view(camera, camera_pos);
gl_shader_program.set_uniform_matrix_4fv(cstr!("view"), &view);
@@ -460,7 +458,7 @@ fn apply_light<PointLightHolder>(
gl_shader_program: &mut GlShaderProgram,
point_lights: &[PointLightHolder],
directional_lights: &[&DirectionalLight],
- camera: &Camera,
+ camera_pos: &Position,
) where
PointLightHolder: Deref<Target = PointLight>,
{
@@ -559,7 +557,7 @@ fn apply_light<PointLightHolder>(
gl_shader_program.set_uniform_1fv(cstr!("material.shininess"), material.shininess);
- gl_shader_program.set_uniform_vec_3fv(cstr!("view_pos"), &camera.position);
+ gl_shader_program.set_uniform_vec_3fv(cstr!("view_pos"), &camera_pos.position);
}
fn set_light_attenuation_uniforms(
@@ -656,11 +654,11 @@ fn create_light_uniform_name(
}
}
-fn create_view(camera: &Camera) -> Matrix<f32, 4, 4>
+fn create_view(camera: &Camera, camera_pos: &Position) -> Matrix<f32, 4, 4>
{
let mut view = Matrix::new();
- view.look_at(&camera.position, &camera.target, &camera.global_up);
+ view.look_at(&camera_pos.position, &camera.target, &camera.global_up);
view
}