From c4073d7d91a68f1591027e580b2083c5abb72020 Mon Sep 17 00:00:00 2001 From: Daniel Bross Date: Wed, 4 Dec 2024 23:41:52 +0100 Subject: [PATCH] Point lights --- core/scene.jai | 3 ++- core/transform.jai | 12 +++++++---- module.jai | 6 ++++++ renderer/engine_buffers.jai | 9 ++++++++ renderer/point_light.jai | 41 +++++++++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 renderer/point_light.jai diff --git a/core/scene.jai b/core/scene.jai index cc3bc69..5758948 100644 --- a/core/scene.jai +++ b/core/scene.jai @@ -1,4 +1,5 @@ #load "../renderer/directional_light.jai"; +#load "../renderer/point_light.jai"; #load "particles.jai"; MAX_CACHED_PILES :: 8; @@ -160,7 +161,7 @@ create_scene :: (name: string = "", max_entities: s64 = 256) -> *Scene { array_reserve(*scene.entities, max_entities); - scene.directional_light.color_and_intensity = .{1,1,1,2}; + scene.directional_light.color_and_intensity = .{1,1,1,1}; scene.directional_light.direction = to_v4(normalize(Vector3.{0.2, -0.7, 0.4})); dir_light_data : Directional_Light_Buffer_Data; diff --git a/core/transform.jai b/core/transform.jai index 8912761..932538f 100644 --- a/core/transform.jai +++ b/core/transform.jai @@ -72,7 +72,7 @@ translate :: (transform: *Transform, translation: Vector3, calculate_matrix: boo } euler_to_quaternion :: (value: Vector3) -> Quaternion { - return euler_to_quaternion(value.x, value.y, value.z); + return euler_to_quaternion(value.y, value.x, value.z); } euler_to_quaternion :: (yaw: float, pitch: float, roll: float) -> Quaternion { @@ -102,8 +102,8 @@ quaternion_to_euler_v3 :: (q: Quaternion) -> Vector3 { yaw, pitch, roll := quaternion_to_euler(q); v : Vector3 = ---; - v.x = yaw; - v.y = pitch; + v.x = pitch; + v.y = yaw; v.z = roll; return v; @@ -130,7 +130,7 @@ quaternion_to_euler :: (q: Quaternion) -> yaw: float, pitch: float, roll: float cosy_cosp := 1.0 - 2.0 * (q.y * q.y + q.z * q.z); yaw = atan2(siny_cosp, cosy_cosp); - return yaw, pitch, roll; + return pitch, yaw, roll; } set_rotation :: (e: *Entity, orientation: Quaternion, calculate_matrix: bool = true) { @@ -148,6 +148,10 @@ set_rotation :: (transform: *Transform, euler_angles: Vector3, calculate_matrix: if calculate_matrix update_matrix(transform); } +set_rotation :: (e: *Entity, euler_angles: Vector3, calculate_matrix: bool = true) { + set_rotation(*e.transform, euler_angles, calculate_matrix); +} + set_scale :: (e: *Entity, scale: Vector3, calculate_matrix: bool = true) { set_scale(*e.transform, scale, calculate_matrix); } diff --git a/module.jai b/module.jai index cdf7050..2161f0b 100644 --- a/module.jai +++ b/module.jai @@ -32,6 +32,7 @@ Engine_Core :: struct { time_buffer : Buffer_Handle; screen_data_buffer : Buffer_Handle; directional_light_buffer : Buffer_Handle; + point_light_buffer : Buffer_Handle; procs: struct { on_scene_loaded: (*Scene, Engine_Mode); @@ -41,6 +42,9 @@ Engine_Core :: struct { } paused: bool; + + time: float; + dt: float; } engine: Engine_Core; @@ -94,6 +98,8 @@ coven_run :: (game_update_proc: (float), game_update_post_physics_proc: (float)) now : float = xx seconds_since_init(); dt = now - time; time = now; + engine.time = time; + engine.dt = dt; update_fps_counter(dt); diff --git a/renderer/engine_buffers.jai b/renderer/engine_buffers.jai index 0347bc1..0406ab4 100644 --- a/renderer/engine_buffers.jai +++ b/renderer/engine_buffers.jai @@ -61,6 +61,15 @@ update_light_buffer :: () { light_data.light_matrix = light_matrix; upload_data_to_buffer(engine.renderer, engine.directional_light_buffer, *light_data, size_of(Directional_Light_Buffer_Data)); + + point_light_array: Point_Light_Array; + for light: engine.current_scene.by_type._Point_Light { + shd_point_light := to_shader_point_light(light); + point_light_array.point_lights[point_light_array.num_point_lights] = shd_point_light; + point_light_array.num_point_lights += 1; + } + + upload_data_to_buffer(engine.renderer, engine.point_light_buffer, *point_light_array, size_of(Point_Light_Array)); } sync_engine_buffers :: () { diff --git a/renderer/point_light.jai b/renderer/point_light.jai new file mode 100644 index 0000000..5019f69 --- /dev/null +++ b/renderer/point_light.jai @@ -0,0 +1,41 @@ +Point_Light :: struct { + using #as entity : Entity; + entity.type = Point_Light; + + color: Vector3; + intensity: float = 1.0; + attenuation_radius: float = 3.0; +} + +Point_Light_Shader_Data :: struct { + position: Vector4; + color: Vector4; + attenuation_radius: float; + + pad: Vector3; +} + +MAX_POINT_LIGHTS :: 64; + +Point_Light_Array :: struct { + point_lights: [MAX_POINT_LIGHTS] Point_Light_Shader_Data; + num_point_lights: s32; + + padding: Vector3; +} + +init_entity :: (light: *Point_Light) { + light.intensity = 1.0; + light.color = .{1,1,1}; + light.attenuation_radius = 3.0; +} + +to_shader_point_light :: (point_light: *Point_Light) -> Point_Light_Shader_Data { + data : Point_Light_Shader_Data; + + data.position = to_v4(get_position(point_light.transform)); + data.color = to_v4(point_light.color*point_light.intensity); + data.attenuation_radius = point_light.attenuation_radius; + + return data; +} \ No newline at end of file