diff --git a/shaders/intersect.glsl b/shaders/intersect.glsl index a5ba242..3623cba 100644 --- a/shaders/intersect.glsl +++ b/shaders/intersect.glsl @@ -36,18 +36,24 @@ bool intersectPlane(Ray ray, GPUObject obj, out hitInfo hit) hit.t = t; hit.position = ray.origin + ray.direction * t; hit.normal = d < 0.0 ? obj.normal : -obj.normal; - + + vec3 U = normalize((abs(obj.normal.x) > abs(obj.normal.z)) ? vec3(-obj.normal.y, obj.normal.x, 0.0) : vec3(0.0, -obj.normal.z, obj.normal.y)); + vec3 V = normalize(cross(obj.normal, U)); + + vec3 localPos = hit.position - obj.position; + hit.u = dot(localPos, U) * 1.0; + hit.v = dot(localPos, V) * 1.0; + return (valid); } bool intersectQuad(Ray ray, GPUObject obj, out hitInfo hit) { - vec3 normal = obj.normal; - float d = dot(normal, ray.direction); + float d = dot(obj.normal, ray.direction); if (d == 0.0 || (obj.radius != 0.0 && d <= 0)) return (false); // double sided or not - float t = dot(obj.position - ray.origin, normal) / d; + float t = dot(obj.position - ray.origin, obj.normal) / d; if (t <= 0.0) return (false); @@ -59,12 +65,16 @@ bool intersectQuad(Ray ray, GPUObject obj, out hitInfo hit) float l1 = dot(obj.vertex1, obj.vertex1); float l2 = dot(obj.vertex2, obj.vertex2); + bool inside = e1 >= 0.0 && e1 <= l1 && e2 >= 0.0 && e2 <= l2; hit.t = t; hit.position = p + obj.position; - hit.normal = normal * -sign(d); + hit.normal = obj.normal * -sign(d); // hit.normal = normal; + + hit.u = e1 / l1; + hit.v = e2 / l2; return (inside); } diff --git a/shaders/light.glsl b/shaders/light.glsl index 06d9613..2176895 100644 --- a/shaders/light.glsl +++ b/shaders/light.glsl @@ -115,6 +115,17 @@ vec2 getTextureColor(hitInfo hit) return (vec2(uv.x, 1 - uv.y)); } +vec3 getCheckerboardColor(GPUMaterial mat, hitInfo hit) +{ + float scale = mat.refraction; //scale + int check = int(floor(hit.u * scale) + floor(hit.v * scale)) % 2; + + vec3 color1 = mat.color; + vec3 color2 = vec3(0.0); + + return check == 0 ? color1 : color2; +} + void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inout vec3 light, inout uint rng_state) { if (mat.texture_index != -1) @@ -131,8 +142,9 @@ void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inou } else { - color *= mat.color; - light += mat.emission * mat.color; + vec3 mat_color = (mat.type == 3) ? getCheckerboardColor(mat, hit) : mat.color; + color *= mat_color; + light += mat.emission * mat_color; // if (mat.emission == 0.0) // light += sampleLights(hit.position, rng_state); } diff --git a/shaders/raytracing.glsl b/shaders/raytracing.glsl index f75a881..afaa8bc 100644 --- a/shaders/raytracing.glsl +++ b/shaders/raytracing.glsl @@ -202,10 +202,10 @@ vec3[2] pathtrace(Ray ray, inout uint rng_state) if (i == 0) { - // imageStore(normal_texture, ivec2(gl_GlobalInvocationID.xy), vec4(normalize(hit.normal), 1.0)); - // imageStore(position_texture, ivec2(gl_GlobalInvocationID.xy), vec4(normalize(hit.position), 1.0)); - vec4 accum_normal = accumulate(normal_texture, accum_normal, normalize(hit.normal)); - vec4 accum_position = accumulate(position_texture, accum_position, normalize(hit.position)); + imageStore(normal_texture, ivec2(gl_GlobalInvocationID.xy), vec4(normalize(hit.normal), 1.0)); + imageStore(position_texture, ivec2(gl_GlobalInvocationID.xy), vec4(normalize(hit.position), 1.0)); + // vec4 accum_normal = accumulate(normal_texture, accum_normal, normalize(hit.normal)); + // vec4 accum_position = accumulate(position_texture, accum_position, normalize(hit.position)); } float p = max(color.r, max(color.g, color.b)); diff --git a/shaders/scatter.glsl b/shaders/scatter.glsl index f03342c..aff2269 100644 --- a/shaders/scatter.glsl +++ b/shaders/scatter.glsl @@ -84,7 +84,7 @@ Ray newRay(hitInfo hit, Ray ray, inout uint rng_state) mat = materials[hit.mat_index]; - if (mat.type == 0) + if (mat.type == 0 || mat.type == 3) return (lambertRay(hit, ray, mat, rng_state)); else if (mat.type == 1) return (dieletricRay(hit, ray, mat)); diff --git a/shaders/trace.glsl b/shaders/trace.glsl index a94c2d4..2f4b011 100644 --- a/shaders/trace.glsl +++ b/shaders/trace.glsl @@ -48,6 +48,8 @@ hitInfo traceScene(Ray ray) hit.mat_index = obj.mat_index; hit.position = temp_hit.position; hit.normal = temp_hit.normal; + hit.u = temp_hit.u; + hit.v = temp_hit.v; } } diff --git a/srcs/class/SceneParser.cpp b/srcs/class/SceneParser.cpp index bef35fb..adf5d59 100644 --- a/srcs/class/SceneParser.cpp +++ b/srcs/class/SceneParser.cpp @@ -30,7 +30,7 @@ void SceneParser::parseMaterial(std::stringstream &line) float rough_refrac; float metallic; std::string type; - int texture_index; + float texture_index; Material *mat; @@ -46,7 +46,7 @@ void SceneParser::parseMaterial(std::stringstream &line) mat->roughness = rough_refrac; mat->metallic = metallic; mat->refraction = rough_refrac; - + mat->type = -1; if (type == "LAM") mat->type = 0; @@ -54,6 +54,8 @@ void SceneParser::parseMaterial(std::stringstream &line) mat->type = 1; else if (type == "TRN") mat->type = 2; + else if (type == "CHK") + mat->type = 3; texture_index = -1; if (mat->type != -1) @@ -61,7 +63,13 @@ void SceneParser::parseMaterial(std::stringstream &line) else mat->type = 0; - mat->texture_index = texture_index; + if (mat->type == 3) // i know it's ugly + { + mat->refraction = texture_index; + texture_index = -1; + } + + mat->texture_index = (int)texture_index; mat->emission_texture_index = -1; _scene->addMaterial(mat); } diff --git a/srcs/class/Window.cpp b/srcs/class/Window.cpp index 0e61e31..0ef2952 100644 --- a/srcs/class/Window.cpp +++ b/srcs/class/Window.cpp @@ -248,7 +248,13 @@ void Window::imGuiRender(ShaderProgram &raytracing_program) 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); + else if (mat.type == 3) + { + has_changed |= ImGui::SliderFloat("Checker Scale", &mat.refraction, 0.0f, 40.0f); + 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, 3); ImGui::PopID();