+ | Graphics tweaks

This commit is contained in:
TheRedShip
2025-01-26 16:03:27 +01:00
parent e1e53b99fe
commit 9798a9942b
5 changed files with 2142910 additions and 47 deletions

View File

@ -1,5 +1,4 @@
#version 430 core
#extension GL_NV_gpu_shader5 : enable
layout(local_size_x = 16, local_size_y = 16) in;
layout(binding = 0, rgba32f) uniform image2D output_image;
@ -80,7 +79,6 @@ struct GPUBvh
int index;
int primitive_count;
};
layout(std430, binding = 1) buffer ObjectBuffer
@ -169,21 +167,28 @@ vec3 pathtrace(Ray ray, inout uint rng_state)
{
hitInfo hit = traceRay(ray);
float t_scatter = 0.0;
float scatter_valid = float(volume.enabled != 0 && atmosScatter(hit, t_scatter, rng_state));
#if 0
float t_scatter = 0.0;
bool scatter_valid = bool(volume.enabled != 0 && atmosScatter(hit, t_scatter, rng_state));
if (scatter_valid)
{
calculateVolumetricLight(t_scatter, ray, color, light, transmittance, rng_state);
continue ;
}
#endif
float miss_condition = float(hit.obj_index == -1);
light += miss_condition * transmittance * GetEnvironmentLight(ray);
float p = max(color.r, max(color.g, color.b));
float rr_continue = float(randomValue(rng_state) <= p);
color /= max(p, 0.001);
GPUMaterial mat = materials[hit.mat_index];
float break_condition = miss_condition + (1.0 - rr_continue);
if (break_condition > 0.0) break;
color /= max(p, 0.001);
GPUMaterial mat = materials[hit.mat_index];
calculateLightColor(mat, hit, color, light, rng_state);
if (mat.emission > 0.0)
@ -198,8 +203,7 @@ vec3 pathtrace(Ray ray, inout uint rng_state)
Ray initRay(vec2 uv, inout uint rng_state)
{
float fov = camera.fov;
float focal_length = 1.0 / tan(radians(fov) / 2.0);
float focal_length = 1.0 / tan(radians(camera.fov) / 2.0);
vec3 origin = camera.position;
vec3 view_space_ray = normalize(vec3(uv.x, uv.y, -focal_length));

View File

@ -2,7 +2,7 @@ hitInfo traceRay(Ray ray);
vec3 GetEnvironmentLight(Ray ray)
{
// return vec3(0.);
return vec3(0.);
vec3 sun_pos = vec3(-0.5, 0.5, 0.5);
float SunFocus = 1.5;
float SunIntensity = 1.;
@ -41,7 +41,7 @@ vec3 sampleSphereLight(vec3 position, GPUObject obj, int light_index, GPUMateria
return vec3(0.0);
float cos_theta = max(0.0, -dot(light_dir, normalize(sample_point - obj.position)));
return mat.emission * mat.color / (light_dist);
return mat.emission * mat.color / (light_dist * light_dist) * cos_theta / (4.0 * M_PI * (obj.radius / 2.0) * (obj.radius / 2.0));
}
vec3 sampleQuadLight(vec3 position, GPUObject obj, int light_index, GPUMaterial mat, inout uint rng_state)
@ -65,54 +65,29 @@ vec3 sampleQuadLight(vec3 position, GPUObject obj, int light_index, GPUMaterial
vec3 normal = normalize(crossQuad);
float cos_theta = max(0.0, dot(normal, -light_dir));
return mat.emission * mat.color / (light_dist);
return mat.emission * mat.color / (light_dist * light_dist) * cos_theta / pdf;
}
vec3 sampleLights(vec3 position, inout uint rng_state)
vec3 sampleLights(vec3 position, inout uint rng_state)
{
vec3 light = vec3(0.0);
for (int i = 0; i < u_lightsNum; i++)
{
int light_index = lightsIndex[i];
GPUObject obj = objects[light_index];
GPUMaterial mat = materials[obj.mat_index];
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, (1.0 / light_dir));
hitInfo shadow_hit = traceRay(shadow_ray);
if (shadow_hit.obj_index == light_index)
light += mat.emission * mat.color / (light_dist);
if (obj.type == 0)
light += sampleSphereLight(position, obj, light_index, mat, rng_state);
else if (obj.type == 2)
light += sampleQuadLight(position, obj, light_index, mat, rng_state);
}
return (light);
}
// vec3 sampleLights(vec3 position, inout uint rng_state)
// {
// vec3 light = vec3(0.0);
// for (int i = 0; i < u_lightsNum; i++)
// {
// int light_index = lightsIndex[i];
// GPUObject obj = objects[light_index];
// GPUMaterial mat = materials[obj.mat_index];
// if (obj.type == 0)
// light += sampleSphereLight(position, obj, light_index, mat, rng_state);
// else if (obj.type == 2)
// light += sampleQuadLight(position, obj, light_index, mat, rng_state);
// }
// return (light);
// }
void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inout vec3 light, inout uint rng_state)
{
color *= mat.color;

View File

@ -45,9 +45,8 @@ float fresnel(vec3 incident, vec3 normal, float eta)
float etai = 1.0, etat = eta;
if (cosi > 0.0) swap(etai, etat);
float sint = etai / etat * sqrt(max(0.0, 1.0 - cosi * cosi));
if (sint >= 1.0) {
return 1.0; // Total internal reflection
}
if (sint >= 1.0)
return (1.0); // Total internal reflection
float cost = sqrt(max(0.0, 1.0 - sint * sint));
cosi = abs(cosi);
float Rs = ((etat * cosi) - (etai * cost)) / ((etat * cosi) + (etai * cost));