diff --git a/.idea/runConfigurations/debug.xml b/.idea/runConfigurations/debug.xml
index 1c381d5..f8c23cb 100644
--- a/.idea/runConfigurations/debug.xml
+++ b/.idea/runConfigurations/debug.xml
@@ -8,6 +8,9 @@
-
+
+
+
+
\ No newline at end of file
diff --git a/build/quick-clean.ps1 b/build/quick-clean.ps1
new file mode 100644
index 0000000..cd34d9f
--- /dev/null
+++ b/build/quick-clean.ps1
@@ -0,0 +1,2 @@
+rm ..\target\debug\deps\rust-engine*
+rm ..\target\debug\deps\rust_engine*
\ No newline at end of file
diff --git a/shaders/triangle.frag b/shaders/triangle.frag
index f020d33..597469c 100644
--- a/shaders/triangle.frag
+++ b/shaders/triangle.frag
@@ -4,9 +4,28 @@
layout(binding = 1) uniform sampler2D tex;
layout(location = 0) in vec2 tex_coords;
+layout(location = 1) in vec3 normal_cam;
+layout(location = 2) in vec3 position_cam;
+layout(location = 3) in vec3 light_direction_cam;
layout(location = 0) out vec4 out_color;
void main() {
- out_color = texture(tex, tex_coords);
+ vec3 normal_cam_u = normalize(normal_cam);
+ vec3 light_direction_cam_u = normalize(light_direction_cam);
+
+ float ambient_strength = 0.1;
+ vec3 light_color = vec3(1.0, 1.0, 1.0);
+ vec3 ambient_color = ambient_strength * light_color;
+
+ float diffuse_strength = max(0.0, dot(normal_cam_u, light_direction_cam_u));
+ vec3 diffuse_color = diffuse_strength * light_color;
+
+// float specular_value = 1.0;
+// vec3 view_direction = normalize(position_cam);
+// vec3 reflect_direction = reflect(-light_direction_cam_nm, normal_cam_nm);
+// float specular_strength = pow(max(dot(view_direction, reflect_direction), 0.0), 32);
+// vec3 specular_color = specular_value * specular_strength * light_color;
+
+ out_color = vec4(ambient_color + diffuse_color, 1.0) * texture(tex, tex_coords);
}
\ No newline at end of file
diff --git a/shaders/triangle.vert b/shaders/triangle.vert
index aafc3f2..d4ec7ec 100644
--- a/shaders/triangle.vert
+++ b/shaders/triangle.vert
@@ -1,20 +1,25 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
+layout(push_constant) uniform PushConstants {
+ mat4 model;
+} push;
+
layout(binding = 0) uniform ObjectUniformData {
mat4 view;
mat4 projection;
float time;
+ vec3 light_position;
} ubo;
layout(location = 0) in vec3 position;
layout(location = 1) in vec2 uv;
+layout(location = 2) in vec3 normal;
layout(location = 0) out vec2 tex_coords;
-
-layout(push_constant) uniform PushConstants {
- mat4 model;
-} push;
+layout(location = 1) out vec3 normal_cam;
+layout(location = 2) out vec3 position_cam;
+layout(location = 3) out vec3 light_direction_cam;
out gl_PerVertex {
vec4 gl_Position;
@@ -23,4 +28,11 @@ out gl_PerVertex {
void main() {
gl_Position = ubo.projection * ubo.view * push.model * vec4(position, 1.0);
tex_coords = uv;
+
+ position_cam = vec3(ubo.view * push.model * vec4(position, 1.0));
+ // TODO: use inverse transpose so this doesn't break when scaling
+ normal_cam = vec3(ubo.view * push.model * vec4(normal, 1.0));
+
+ vec3 light_position_cam = vec3(ubo.view * vec4(ubo.light_position, 1.0));
+ light_direction_cam = light_position_cam - position_cam;
}
\ No newline at end of file
diff --git a/src/gameobject.rs b/src/gameobject.rs
index 506af77..136f5ad 100644
--- a/src/gameobject.rs
+++ b/src/gameobject.rs
@@ -13,10 +13,10 @@ impl GameObject {
GameObject { mesh_index: mesh, texture_index, model_matrix: Matrix4::identity() }
}
- pub fn set_position(&mut self, x: f32, y: f32, z: f32) {
- self.model_matrix.w.x = x;
- self.model_matrix.w.y = y;
- self.model_matrix.w.z = z;
+ pub fn set_position(&mut self, pos: (f32, f32, f32)) {
+ self.model_matrix.w.x = pos.0;
+ self.model_matrix.w.y = pos.1;
+ self.model_matrix.w.z = pos.2;
}
pub fn get_position(&self) -> Vector3 {
diff --git a/src/main.rs b/src/main.rs
index 0c7c24c..9b596ab 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,4 +1,4 @@
-use cgmath::{Deg, Matrix4, One, Quaternion, Rad, Rotation, Rotation3, Vector3};
+use cgmath::{Deg, Matrix4, One, Quaternion, Rad, Rotation, Rotation3, Vector3, vec3, Vector4};
use winit::event::Event;
use crate::config::LogConfig;
@@ -53,6 +53,9 @@ impl Game for TestGame {
println!("{:.0} ms / {:.0} FPS", frame_time * 1000.0, 1.0 / frame_time);
}
+ let light_pos = vec3(f32::sin(time) * 10.0, f32::cos(time) * 10.0, 0.0);
+ self.game_objects[0].get_game_object(renderer).unwrap().set_position(light_pos.into());
+
self.cam_rotation = self.cam_rotation * Quaternion::from_angle_y(Deg(self.input.get_axis("look_horizontal") * 0.05));
self.cam_rotation = Quaternion::from_angle_x(Deg(self.input.get_axis("look_vertical") * 0.05)) * self.cam_rotation;
self.cam_position += self.cam_rotation.invert().rotate_vector(Vector3::new(
@@ -82,19 +85,27 @@ impl Game for TestGame {
view: view.into(),
projection: proj.into(),
time,
+ light_position: light_pos.into(),
+ _dummy0: [0; 12],
}
}
}
+fn matrix_vector_mul(matrix: &Matrix4, vector: &Vector3) -> Vector3 {
+ let v4 = Vector4::new(vector.x, vector.y, vector.z, 1.0);
+ let out = matrix * v4;
+ Vector3::new(out.x, out.y, out.z)
+}
+
impl TestGame {
fn game_start(self: &mut Self, renderer: &mut VulkanRenderer) {
self.load_gltf(renderer, "models/box.gltf.glb");
self.load_gltf(renderer, "models/sphere.glb");
let cube = self.add_game_object(renderer, 0, 0);
- cube.get_game_object(renderer).unwrap().set_position(0.0, 2.0, 0.0);
+ cube.get_game_object(renderer).unwrap().set_position((3.0, 4.0, 5.0));
let sphere = self.add_game_object(renderer, 1, 0);
- sphere.get_game_object(renderer).unwrap().set_position(2.0, 0.0, 0.0);
+ sphere.get_game_object(renderer).unwrap().set_position((0.0, 0.0, 0.0));
println!("Game loaded!");
}
diff --git a/src/vulkan.rs b/src/vulkan.rs
index 6426fcf..9b44dcd 100644
--- a/src/vulkan.rs
+++ b/src/vulkan.rs
@@ -1,7 +1,7 @@
use std::sync::Arc;
use std::time::SystemTime;
-use cgmath::{Matrix4, SquareMatrix};
+use cgmath::{Matrix4, SquareMatrix, vec3};
use image::{ImageBuffer, ImageFormat, Rgb, Rgba};
use image::buffer::ConvertBuffer;
use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer};
@@ -276,7 +276,13 @@ impl VulkanRenderer {
let framebuffers = window_size_dependent_setup(device.clone(), &images, render_pass.clone(), &mut dynamic_state);
let mut uniform_buffers = Vec::new();
- let uniform_buffer = vs::ty::ObjectUniformData { view: Matrix4::identity().into(), projection: Matrix4::identity().into(), time: 0.0 };
+ let uniform_buffer = vs::ty::ObjectUniformData {
+ view: Matrix4::identity().into(),
+ projection: Matrix4::identity().into(),
+ time: 0.0,
+ light_position: vec3(0.0, 0.0, 0.0).into(),
+ _dummy0: [0; 12],
+ };
for _ in 0..swapchain.num_images() {
uniform_buffers.push(CpuAccessibleBuffer::from_data(