point light

This commit is contained in:
Confideo-IOM
2026-07-17 21:06:44 +01:00
parent 2fc305bc4d
commit b7aa111438
48 changed files with 405 additions and 186 deletions
@@ -11,6 +11,11 @@ uint METAL_ROUGH_TEXTURE_ID = 2;
uint NORMAL_TEXTURE_ID = 3;
uint POSITION_TEXTIURE_ID = 4;
uint HYDRA_LIGHT_DIRECTIONAL = 1;
uint HYDRA_LIGHT_POINT = 2;
uint HYDRA_LIGHT_SPOT = 3;
struct light_structure
{
vec4 light_colour;
@@ -156,6 +161,59 @@ vec3 DirectionalShading(vec3 normal, vec3 diffuse, float roughness, float metall
return Lo;
}
vec3 PointShading(vec3 normal, vec3 diffuse, float roughness, float metallic, float light_intensity, vec3 lightColour, vec3 lightDir, vec3 lightPos, vec3 worldPos, vec3 viewPos)
{
vec3 N = normalize(normal);
vec3 V = normalize(viewPos - worldPos);
// calculate reflectance at normal incidence; if dia-electric (like plastic) use F0
// of 0.04 and if it's a metal, use the albedo color as F0 (metallic workflow)
vec3 F0 = vec3(0.04);
F0 = mix(F0, diffuse, metallic);
// reflectance equation
vec3 Lo = vec3(0.0);
// calculate per-light radiance
vec3 L = normalize(lightPos - worldPos);
vec3 H = normalize(V + L);
float distance = length(lightPos - worldPos);
float attenuation = 1.0;
attenuation = light_intensity / (distance * distance);
vec3 radiance = lightColour * attenuation;
// Cook-Torrance BRDF
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
vec3 F = fresnelSchlick(clamp(dot(H, V), 0.0, 1.0), F0);
vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001; // + 0.0001 to prevent divide by zero
vec3 specular = numerator / denominator;
// kS is equal to Fresnel
vec3 kS = F;
// for energy conservation, the diffuse and specular light can't
// be above 1.0 (unless the surface emits light); to preserve this
// relationship the diffuse component (kD) should equal 1.0 - kS.
vec3 kD = vec3(1.0) - kS;
// multiply kD by the inverse metalness such that only non-metals
// have diffuse lighting, or a linear blend if partly metal (pure metals
// have no diffuse light).
kD *= 1.0 - metallic;
// scale light by NdotL
float NdotL = max(dot(N, L), 0.0);
// add to outgoing radiance Lo
Lo += (kD * diffuse / PI + specular) * radiance * NdotL; // note that we already multiplied the BRDF by the Fresnel (kS) so we won't multiply by kS again
return Lo;
}
mat3 GetRotationOnlyMatrix(mat4 model_mat)
{
mat3 rot_mat;
@@ -192,9 +250,15 @@ void main()
vec3 light_direction = rot_mat * vec3(0,0,1);
vec3 light_pos = model[3].xyz;
if(lights[i].light_type == HYDRA_LIGHT_DIRECTIONAL)
{
Lo+= DirectionalShading(normal, diffuse.xyz, rough, metal, lights[i].light_intensity, lights[i].light_colour.xyz, light_direction, light_pos, world_pos, ubo_per_frame.viewer_pos);
}
else if(lights[i].light_type == HYDRA_LIGHT_POINT)
{
Lo+= PointShading(normal, diffuse.xyz, rough, metal, lights[i].light_intensity, lights[i].light_colour.xyz, light_direction, light_pos, world_pos, ubo_per_frame.viewer_pos);
}
vec3 colour = DirectionalShading(normal, diffuse.xyz, rough, metal, lights[i].light_intensity, lights[i].light_colour.xyz, light_direction, light_pos, world_pos, ubo_per_frame.viewer_pos);
Lo = colour;
//uFragColor = vec4(light_direction, 1);
}
@@ -1,5 +1,5 @@
#ifndef HYDRALIGHTACTOR
#define HYDRALIGHTACTOR
#ifndef HYDRADIRECTIONALLIGHTACTOR
#define HYDRADIRECTIONALLIGHTACTOR
#include "HydraActor.h"
class CLASS_DEFINE HydraDirectionalLightActor : public HydraActor
@@ -15,4 +15,4 @@ class CLASS_DEFINE HydraDirectionalLightActor : public HydraActor
};
#endif /* HYDRALIGHTACTOR */
#endif /* HYDRADIRECTIONALLIGHTACTOR */
@@ -0,0 +1,37 @@
#include "HydraPointLightActor.h"
#include "../engine/HydraEngine.h"
#include "../actor/HydraActorManager.h"
#include "../task/HydraTask.h"
#include "../ecs/HydraECS.h"
#include "../ecs/components/HydraPositionComponent.h"
#include "../ecs/components/HydraMeshComponent.h"
#include "../ecs/components/HydraRenderableComponent.h"
#include "../ecs/components/HydraMaterialComponent.h"
#include "../ecs/components/HydraLightComponent.h"
#include "../buffer/HydraLightBufferManager.h"
#include "../buffer/HydraLightBuffer.h"
void HydraPointLightActor::InitialiseComponents()
{
HydraLightComponent light_comp = {};
HydraECS *ecs = GetEngine()->ECS();
light_comp.light_colour = glm::vec4(1,1,1,1);
light_comp.light_intensity = 10.0f;
light_comp.light_type = static_cast<uint32_t>(HYDRA_LIGHT_TYPE::HYDRA_LIGHT_POINT);
light_comp.entity_id = GetID();
ecs->AddComponent<HydraLightComponent>(GetID(), light_comp);
HydraPositionComponent position_component;
ecs->AddComponent<HydraPositionComponent>(GetID(), position_component);
}
void HydraPointLightActor::Destroy()
{
}
@@ -0,0 +1,21 @@
#ifndef HYDRAPOINTLIGHTACTOR
#define HYDRAPOINTLIGHTACTOR
#include "HydraActor.h"
class CLASS_DEFINE HydraPointLightActor : public HydraActor
{
private:
protected:
public:
virtual void InitialiseComponents() override;
virtual void Destroy() override;
};
#endif /* HYDRAPOINTLIGHTACTOR */