+ | New window2 scene + better light direct sampling, to tweak and see

This commit is contained in:
TheRedShip
2025-01-15 12:18:21 +01:00
parent 2899055b51
commit 8048d0b799
6 changed files with 113 additions and 11 deletions

View File

@ -7,7 +7,7 @@ MAT 255 255 255 10.0 0.0 0.0 //light
pl 0 0 0 0 1 0 1
sp -10 10 -9 0.01 2
sp -10 10 -9 10 2
qu 0 0 0 0 6 0 18 0 0 0
qu 0 0 -18 0 6 0 18 0 0 0

30
scenes/window2.rt Normal file
View File

@ -0,0 +1,30 @@
CAM 0 2 -2 0 90
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
sp -5 5 0 1 2
# qu -5 0 -2.5 0 5 0 0 0 5 2
pl 0 0 0 0 1 0 1
qu 2.5 0 -2.5 0 5 0 0 0 5 0
qu -2.5 0 -2.5 0 5 0 5 0 0 0
qu -2.5 0 2.5 0 5 0 5 0 0 0
qu -2.5 5 -2.5 0 0 5 5 0 0 0
qu -2.5 0 -2.5 0 2.5 0 0 0 5 0
qu -2.5 3.5 -2.5 0 1.5 0 0 0 5 0
qu -2.5 2.5 0.5 0 1 0 0 0 2 0
qu -2.5 2.5 -2.5 0 1 0 0 0 2 0

View File

@ -192,7 +192,7 @@ vec3 pathtrace(Ray ray, inout uint rng_state)
color /= p;
//
calculateLightColor(color, light, mat, hit);
calculateLightColor(mat, hit, color, light, rng_state);
if (mat.emission > 0.0)
break;

View File

@ -1,6 +1,9 @@
hitInfo traceRay(Ray ray);
vec3 GetEnvironmentLight(Ray ray)
{
return vec3(0.);
vec3 sun_pos = vec3(-0.5, 0.5, 0.5);
float SunFocus = 1.5;
float SunIntensity = 1.;
@ -18,9 +21,55 @@ vec3 GetEnvironmentLight(Ray ray)
return composite;
}
hitInfo traceRay(Ray ray);
vec3 sampleSphereLight(vec3 position, GPUObject obj, GPUMaterial mat, inout uint rng_state)
{
float theta = 2.0 * M_PI * randomValue(rng_state);
float phi = acos(2.0 * randomValue(rng_state) - 1.0);
vec3 sample_point = obj.position + obj.radius * vec3(
sin(phi) * cos(theta),
sin(phi) * sin(theta),
cos(phi)
);
vec3 light_dir = normalize(sample_point - position);
float light_dist = length(sample_point - position);
Ray shadow_ray = Ray(position + light_dir * 0.001, light_dir);
hitInfo shadow_hit = traceRay(shadow_ray);
if (shadow_hit.obj_index != -1 && shadow_hit.t < light_dist)
return vec3(0.0);
vec3 sampleLights(vec3 position)
float cos_theta = max(0.0, -dot(light_dir, normalize(sample_point - obj.position)));
return mat.emission * mat.color / (light_dist);
}
vec3 sampleQuadLight(vec3 position, GPUObject obj, GPUMaterial mat, inout uint rng_state)
{
float u = randomValue(rng_state);
float v = randomValue(rng_state);
vec3 sample_point = obj.position + u * obj.vertex1 + v * obj.vertex2;
vec3 light_dir = normalize(sample_point - position);
float light_dist = length(sample_point - position);
Ray shadow_ray = Ray(position + light_dir * 0.001, light_dir);
hitInfo shadow_hit = traceRay(shadow_ray);
if (shadow_hit.obj_index != -1 && shadow_hit.t < light_dist)
return vec3(0.0);
vec3 crossQuad = cross(obj.vertex1, obj.vertex2);
float area = length(crossQuad);
float pdf = 1.0 / area;
vec3 normal = normalize(crossQuad);
float cos_theta = max(0.0, dot(normal, -light_dir));
return mat.emission * mat.color * cos_theta / (pdf * light_dist * light_dist);
}
vec3 sampleLights(vec3 position, inout uint rng_state)
{
vec3 light = vec3(0.0);
@ -45,9 +94,35 @@ vec3 sampleLights(vec3 position)
return (light);
}
void calculateLightColor(inout vec3 color, inout vec3 light, GPUMaterial mat, hitInfo hit)
// vec3 sampleLights(vec3 position, inout uint rng_state)
// {
// vec3 light = vec3(0.0);
// int emissive_count = 0;
// for (int i = 0; i < u_objectsNum; i++)
// if (materials[objects[i].mat_index].emission > 0.0)
// emissive_count++;
// if (emissive_count == 0)
// return (vec3(0.));
// int target_light = int(floor(randomValue(rng_state) * float(emissive_count)));
// GPUObject obj = objects[target_light];
// GPUMaterial mat = materials[obj.mat_index];
// if (obj.type == 0)
// light = sampleSphereLight(position, obj, mat, rng_state);
// else if (obj.type == 2)
// light = sampleQuadLight(position, obj, mat, rng_state);
// return (light);
// }
void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inout vec3 light, inout uint rng_state)
{
color *= mat.color;
light += mat.emission * mat.color;
// light += sampleLights(hit.position);
// light += sampleLights(hit.position, rng_state);
}

View File

@ -44,7 +44,7 @@ void calculateVolumetricLight(float t_scatter, inout Ray ray, inout vec3 color,
transmittance *= exp(-volume.sigma_t * t_scatter);
color *= volume.sigma_s / volume.sigma_t;
light += transmittance * color * sampleLights(scatter_pos);
light += transmittance * color * sampleLights(scatter_pos, rng_state);
float cos_theta = sampleHG(volume.g, rng_state);
vec3 new_dir = sampleDirection(ray.direction, cos_theta, rng_state);

View File

@ -124,10 +124,7 @@ Triangle *SceneParser::getFace(std::stringstream &line, std::vector<glm::vec3> &
triangle[0] = vertices[getVertexIndex(line, vertices.size())];
triangle[1] = vertices[getVertexIndex(line, vertices.size())];
triangle[2] = vertices[getVertexIndex(line, vertices.size())];
std::cout << triangle[0].x << " " << triangle[0].y << " " << triangle[0].z << std::endl;
std::cout << triangle[1].x << " " << triangle[1].y << " " << triangle[1].z << std::endl;
std::cout << triangle[2].x << " " << triangle[2].y << " " << triangle[2].z << std::endl;
return(new Triangle(triangle[0], triangle[1], triangle[2], 0));
return (new Triangle(triangle[0], triangle[1], triangle[2], 0));
}
void SceneParser::parseObj(std::stringstream &objInfo)