+ | Volumetric ligthing

This commit is contained in:
TheRedShip
2025-01-13 23:45:13 +01:00
parent 819c07f90d
commit 1d3627000d
5 changed files with 113 additions and 16 deletions

View File

@ -7,10 +7,10 @@ Pos=1515,93
Size=368,276
[Window][Material]
Pos=42,246
Pos=1620,226
Size=290,238
[Window][Camera]
Pos=609,294
Pos=1624,25
Size=279,183

View File

@ -10,16 +10,16 @@ MAT 30 30 30 0.0 0.0 0.0 //black
MAT 255 255 255 5.0 0.0 0.0 //light
# sp -100 30 50 50 6
sp -100 30 50 50 6
#sp 0 -25 0 50 4
#
#sp 1 1 -0.5 2 0
#sp -0.8 0.8 0 1.5 1
#sp -2.2 0.4 0.25 1 2
#sp -3 0.2 0.50 0.75 3
#sp -3.8 -0.1 0.60 0.5 5
#
#cy 0 1 2 0.5 2 -1.5 0 0.75 1
sp 0 -25 0 50 4
OBJ obj/teapot.obj
sp 1 1 -0.5 2 0
sp -0.8 0.8 0 1.5 1
sp -2.2 0.4 0.25 1 2
sp -3 0.2 0.50 0.75 3
sp -3.8 -0.1 0.60 0.5 5
cy 0 1 2 0.5 2 -1.5 0 0.75 1
# OBJ obj/teapot.obj

View File

@ -3,8 +3,11 @@ CAM 11.1641 1.64529 -17.9646 -4.2 -237.4 0 1 90 5
MAT 255 255 255 0.0 1.0 0.0 //white
MAT 150 150 150 0.0 1.0 0.0 //grey
MAT 255 255 255 10.0 0.0 0.0 //light
pl 0 0 0 0 1 0 1
sp -100 50 0 50 2
qu 0 0 0 0 6 0 18 0 0 0
qu 0 0 -18 0 6 0 18 0 0 0

View File

@ -139,21 +139,114 @@ hitInfo traceRay(Ray ray)
return (hit);
}
float sampleHG(float g, inout uint rng_state)
{
if (abs(g) < 0.001)
return 2.0 * randomValue(rng_state) - 1.0;
float sqr_term = (1.0 - g * g) / (1.0 + g - 2.0 * g * randomValue(rng_state));
return (1.0 + g * g - sqr_term * sqr_term) / (2.0 * g);
}
vec3 sampleDirection(vec3 forward, float cos_theta, inout uint rng_state)
{
float phi = 2.0 * M_PI * randomValue(rng_state);
float sin_theta = sqrt(max(0.0, 1.0 - cos_theta * cos_theta));
vec3 dir;
dir.x = sin_theta * cos(phi);
dir.y = sin_theta * sin(phi);
dir.z = cos_theta;
vec3 up = abs(forward.z) < 0.999 ? vec3(0, 0, 1) : vec3(1, 0, 0);
vec3 right = normalize(cross(up, forward));
up = cross(forward, right);
return normalize(
dir.x * right +
dir.y * up +
dir.z * forward
);
}
vec3 sampleLights(vec3 position)
{
vec3 light = vec3(0.0);
for (int i = 0; i < u_objectsNum; i++)
{
GPUObject obj = objects[i];
GPUMaterial mat = materials[obj.mat_index];
if (obj.type == 0 && mat.emission > 0.0)
{
vec3 light_dir = normalize(obj.position - position);
float light_dist = length(obj.position - position);
Ray shadow_ray = Ray(position + light_dir * 0.01, light_dir);
hitInfo shadow_hit = traceRay(shadow_ray);
if (shadow_hit.obj_index == i)
light += mat.emission * mat.color / (light_dist);
}
}
return (light);
}
struct VolumeProperties {
vec3 sigma_a; // absorption coefficient
vec3 sigma_s; // scattering coefficient
vec3 sigma_t; // extinction coefficient
float g; // phase function parameter
};
vec3 pathtrace(Ray ray, inout uint rng_state)
{
vec3 color = vec3(1.0);
vec3 light = vec3(0.0);
vec3 transmittance = vec3(1.0);
VolumeProperties volume;
volume.sigma_a = vec3(0.0001);
volume.sigma_s = vec3(0.0800);
volume.sigma_t = volume.sigma_a + volume.sigma_s;
volume.g = 1.;
for (int i = 0; i < camera.bounce; i++)
{
hitInfo hit = traceRay(ray);
float t_scatter = -log(randomValue(rng_state)) / volume.sigma_t.x;
float t_surface = hit.t;
if (t_scatter < t_surface && volume.sigma_t.x > 0.0)
{
vec3 scatter_pos = ray.origin + ray.direction * t_scatter;
transmittance *= exp(-volume.sigma_t * t_scatter);
color *= volume.sigma_s / volume.sigma_t;
light += transmittance * color * sampleLights(scatter_pos);
float cos_theta = sampleHG(volume.g, rng_state);
vec3 new_dir = sampleDirection(ray.direction, cos_theta, rng_state);
ray.origin = scatter_pos;
ray.direction = new_dir;
continue;
}
if (hit.obj_index == -1)
{
light += GetEnvironmentLight(ray);
light += transmittance * GetEnvironmentLight(ray);
// light += vec3(135 / 255.0f, 206 / 255.0f, 235 / 255.0f); //ambient color
break;
}
transmittance *= exp(-volume.sigma_t * t_surface);
GPUObject obj = objects[hit.obj_index];
GPUMaterial mat = materials[obj.mat_index];
@ -166,6 +259,7 @@ vec3 pathtrace(Ray ray, inout uint rng_state)
color *= mat.color;
light += mat.emission * mat.color;
// light += sampleLights(hit.position);
if (mat.emission > 0.0)
break;

View File

@ -219,7 +219,7 @@ void Window::imGuiRender()
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
if (has_changed)
_frameCount = -1;
_frameCount = (accumulate == 0) + 1;
}
GLFWwindow *Window::getWindow(void) const
@ -251,7 +251,7 @@ int Window::getPixelisation(void)
{
if(_fps < 60 && _pixelisation < 16)
_pixelisation++;
if(_fps > 90 && _pixelisation > 0)
if(_fps > 120 && _pixelisation > 0)
_pixelisation--;
}
else if(_pixelisation)