Point lights

This commit is contained in:
2024-12-04 23:41:52 +01:00
parent 71668cc2f5
commit c4073d7d91
5 changed files with 66 additions and 5 deletions

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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);

View File

@@ -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 :: () {

41
renderer/point_light.jai Normal file
View File

@@ -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;
}