+ | Transparency Material

This commit is contained in:
TheRedShip
2025-01-16 00:12:31 +01:00
parent a892d5e7e6
commit 787833ac84
12 changed files with 6203 additions and 6118 deletions

View File

@ -3,14 +3,14 @@ Pos=60,60
Size=400,400
[Window][Camera]
Pos=60,60
Size=128,184
Pos=1655,22
Size=229,184
[Window][Material]
Pos=1561,418
Pos=1648,220
Size=266,285
[Window][Fog settings]
Pos=1583,141
Size=251,265
Pos=1654,508
Size=247,157

View File

@ -31,6 +31,7 @@
# include "imgui/imgui_impl_glfw.h"
# include "imgui/imgui_impl_opengl3.h"
# include <string.h>
# include <iostream>
# include <fstream>
# include <sstream>

File diff suppressed because it is too large Load Diff

View File

@ -10,7 +10,10 @@ 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
MAT 255 255 255 0.0 0.0 0.0 TRN // glass
sp -5 2 0 2 6
# qu -5 0 0 0 2 0 0 0 2 6
sp 0 -25 0 50 4
@ -20,6 +23,8 @@ 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
sp 0 1 5 1 7
cy 0 1 2 0.5 2 -1.5 0 0.75 1
OBJ obj/10517_Motorcycle_Helmet_v01_L3.obj
OBJ obj/Lowpoly_tree_sample.obj

View File

@ -3,11 +3,12 @@ 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
MAT 255 255 255 5.0 0.0 0.0 //light
pl 0 0 0 0 1 0 1
sp -10 10 -9 10 2
sp -10 10 -9 0.1 2
# qu -10 5 -12 0 6 0 0 0 6 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

@ -86,6 +86,7 @@ struct Ray
struct hitInfo
{
float t;
float last_t;
vec3 normal;
vec3 position;
int obj_index;
@ -141,6 +142,7 @@ hitInfo traceRay(Ray ray)
if (intersect(ray, obj, temp_hit) && temp_hit.t > 0.0f && temp_hit.t < hit.t + 0.0001)
{
hit.t = temp_hit.t;
hit.last_t = temp_hit.last_t;
hit.obj_index = i;
hit.position = temp_hit.position;
hit.normal = temp_hit.normal;

View File

@ -7,9 +7,17 @@ bool intersectSphere(Ray ray, GPUObject obj, out hitInfo hit)
float h = b * b - c;
float t = -b - sqrt(h);
t = mix(t, -b + sqrt(h), step(t, 0.0));
float last_t = -b + sqrt(h);
if (t > last_t)
{
float temp = t;
t = last_t;
last_t = temp;
}
hit.t = t;
hit.last_t = last_t;
hit.position = ray.origin + ray.direction * t;
hit.normal = normalize(hit.position - obj.position);

View File

@ -64,32 +64,29 @@ vec3 sampleQuadLight(vec3 position, GPUObject obj, GPUMaterial mat, inout uint r
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);
return mat.emission * mat.color / (light_dist);
}
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++;
{
GPUObject obj = objects[i];
GPUMaterial mat = materials[obj.mat_index];
if (mat.emission > 0.0)
{
vec3 light_dir = normalize(obj.position - position);
float light_dist = length(obj.position - position);
int target_light = int(floor(randomValue(rng_state) * float(emissive_count)));
Ray shadow_ray = Ray(position + light_dir * 0.01, light_dir);
hitInfo shadow_hit = traceRay(shadow_ray);
GPUObject obj = objects[target_light];
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);
hitInfo shadow_hit = traceRay(shadow_ray);
if (shadow_hit.obj_index == target_light)
light += mat.emission * mat.color / (light_dist);
if (shadow_hit.obj_index == i)
light += mat.emission * mat.color / (light_dist);
}
}
return (light);
}
@ -98,24 +95,20 @@ 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.));
// GPUObject obj = objects[i];
// GPUMaterial mat = materials[obj.mat_index];
// int target_light = int(floor(randomValue(rng_state) * float(emissive_count)));
// if (mat.emission == 0.0)
// continue ;
// 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);
// 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);
// }

View File

@ -32,6 +32,72 @@ Ray dieletricRay(hitInfo hit, Ray ray, GPUMaterial mat)
return (ray);
}
void swap(inout float a, inout float b)
{
float temp = a;
a = b;
b = temp;
}
float fresnel(vec3 incident, vec3 normal, float eta)
{
float cosi = clamp(dot(incident, normal), -1.0, 1.0);
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
}
float cost = sqrt(max(0.0, 1.0 - sint * sint));
cosi = abs(cosi);
float Rs = ((etat * cosi) - (etai * cost)) / ((etat * cosi) + (etai * cost));
float Rp = ((etai * cosi) - (etat * cost)) / ((etai * cosi) + (etat * cost));
return (Rs * Rs + Rp * Rp) * 0.5;
}
Ray transparencyRay(hitInfo hit, Ray ray, GPUMaterial mat, inout uint rng_state)
{
Ray newRay;
float eta = mat.refraction;
vec3 refractedDir = refract(ray.direction, hit.normal, 1.0 / eta);
float kr = fresnel(ray.direction, hit.normal, eta);
float randVal = randomValue(rng_state);
if (randVal < mat.metallic || length(refractedDir) == 0.0)
{
newRay.origin = hit.position + hit.normal * 1e-4;
newRay.direction = reflect(ray.direction, hit.normal);
}
else
{
newRay.origin = hit.position - hit.normal * 1e-4;
newRay.direction = refractedDir;
}
return newRay;
}
// Ray transparencyRay(hitInfo hit, Ray ray, GPUMaterial mat, inout uint rng_state)
// {
// vec3 specular_origin = hit.position + hit.normal * 0.001;
// vec3 specular_dir = mix(normalize(reflect(ray.direction, hit.normal)), lambertRay(hit, ray, mat, rng_state).direction, mat.roughness);
// vec3 transparency_origin = ray.origin + ray.direction * hit.last_t + ray.direction * 0.001;
// vec3 transparency_dir = ray.direction;
// Ray specular_ray = Ray(specular_origin, specular_dir);
// Ray transparency_ray = Ray(transparency_origin, transparency_dir);
// bool is_transparent = (mat.metallic >= randomValue(rng_state));
// if (is_transparent)
// return (transparency_ray);
// return (specular_ray);
// }
Ray newRay(hitInfo hit, Ray ray, inout uint rng_state)
{
@ -45,5 +111,7 @@ Ray newRay(hitInfo hit, Ray ray, inout uint rng_state)
return (lambertRay(hit, ray, mat, rng_state));
else if (mat.type == 1)
return (dieletricRay(hit, ray, mat));
else if (mat.type == 2)
return (transparencyRay(hit, ray, mat, rng_state));
return (ray);
}

View File

@ -17,7 +17,7 @@ Scene::Scene()
_camera = new Camera(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), -90.0f, 0.0f);
_gpu_volume.enabled = 0;
_gpu_volume.sigma_a = glm::vec3(0.0f);
_gpu_volume.sigma_a = glm::vec3(0.0001f);
_gpu_volume.sigma_s = glm::vec3(0.0800f);
_gpu_volume.sigma_t = _gpu_volume.sigma_a + _gpu_volume.sigma_s;
_gpu_volume.g = 0.9f;

View File

@ -52,6 +52,8 @@ void SceneParser::parseMaterial(std::stringstream &line)
mat->type = 0;
else if (type == "DIE")
mat->type = 1;
else if (type == "TRN")
mat->type = 2;
_scene->addMaterial(mat);
}
@ -156,7 +158,7 @@ void SceneParser::parseMtl(std::stringstream &input_line, std::map<std::string,
if(matName.empty())
throw std::runtime_error("OBJ: syntax error in material file, missing material name");
mat = new Material;
bzero(mat, sizeof(Material));
memset(mat, 0, sizeof(Material));
mat->metallic = 1.0f;
continue;
}

View File

@ -199,14 +199,20 @@ void Window::imGuiRender()
has_changed |= ImGui::ColorEdit3("Color", &mat.color[0]);
has_changed |= ImGui::SliderFloat("Emission", &mat.emission, 0.0f, 10.0f);
if (mat.type == 1)
has_changed |= ImGui::SliderFloat("Refraction", &mat.refraction, 1.0f, 5.0f);
else
if (mat.type == 0)
{
has_changed |= ImGui::SliderFloat("Roughness", &mat.roughness, 0.0f, 1.0f);
has_changed |= ImGui::SliderFloat("Metallic", &mat.metallic, 0.0f, 1.0f);
}
has_changed |= ImGui::SliderInt("Type", &mat.type, 0, 1);
else if (mat.type == 1)
has_changed |= ImGui::SliderFloat("Refraction", &mat.refraction, 1.0f, 5.0f);
else if (mat.type == 2)
{
has_changed |= ImGui::SliderFloat("Transparency", &mat.roughness, 0.0f, 1.0f);
has_changed |= ImGui::SliderFloat("Refraction", &mat.refraction, 1.0f, 2.0f);
has_changed |= ImGui::SliderFloat("Proba", &mat.metallic, 0., 1.);
}
has_changed |= ImGui::SliderInt("Type", &mat.type, 0, 2);
ImGui::PopID();